@dudousxd/nestjs-agent-rag 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Davide Carvalho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # @dudousxd/nestjs-agent-rag
2
+
3
+ Retrieval-Augmented Generation for [`@dudousxd/nestjs-agent`](../nestjs). Chunking, ingestion, an
4
+ embedding-backed `Retriever`, and two vector stores — in-memory and pgvector. Framework-agnostic:
5
+ it depends only on `-core`, and you bring your own embedding model and (for pgvector) Postgres driver.
6
+
7
+ ## Install
8
+
9
+ ```bash
10
+ pnpm add @dudousxd/nestjs-agent-rag @dudousxd/nestjs-agent-core
11
+ ```
12
+
13
+ ## Build a retriever + ingest
14
+
15
+ ```ts
16
+ import { EmbeddingRetriever, MemoryVectorStore, ingestDocuments } from '@dudousxd/nestjs-agent-rag';
17
+ import { aiSdkEmbedding } from '@dudousxd/nestjs-agent-ai-sdk';
18
+ import { openai } from '@ai-sdk/openai';
19
+
20
+ const embedder = aiSdkEmbedding(openai.embedding('text-embedding-3-small'));
21
+ const store = new MemoryVectorStore(); // or PgVectorStore for production
22
+
23
+ await ingestDocuments(
24
+ [{ id: 'refund-policy', text: '…', source: 'docs/refunds' }],
25
+ { embedder, store, chunkSize: 800 },
26
+ );
27
+
28
+ const retriever = new EmbeddingRetriever(embedder, store);
29
+ ```
30
+
31
+ ## Two ways to use it with the agent
32
+
33
+ **Agentic (default) — the model decides when to search:**
34
+
35
+ ```ts
36
+ import { createRetrievalTool } from '@dudousxd/nestjs-agent-rag';
37
+ import { provideAgentTool } from '@dudousxd/nestjs-agent';
38
+
39
+ @Module({ providers: [provideAgentTool(createRetrievalTool(retriever))] })
40
+ export class AppModule {}
41
+ ```
42
+
43
+ **Inject (always-on) — augment every turn's prompt with retrieved context:**
44
+
45
+ ```ts
46
+ AgentModule.forRoot({
47
+ // …model, store, actorResolver…
48
+ retrieval: { mode: 'inject', retriever, topK: 5 },
49
+ });
50
+ ```
51
+
52
+ Either way, retrieved passages carry a `source` and surface as citations through the normal
53
+ tool-call machinery (rendered in the chat UI, shown in the `-telescope` Agent tab). Each retrieval
54
+ also emits an `aviary:agent:retrieved` diagnostics event.
55
+
56
+ ## pgvector store
57
+
58
+ `PgVectorStore` takes an injected `PgClient` — adapt your own `pg` / `postgres.js`:
59
+
60
+ ```ts
61
+ import { Pool } from 'pg';
62
+ import { PgVectorStore } from '@dudousxd/nestjs-agent-rag';
63
+
64
+ const pool = new Pool({ connectionString: process.env.DATABASE_URL });
65
+ const store = new PgVectorStore(
66
+ { query: (sql, params) => pool.query(sql, params).then((r) => r.rows) },
67
+ { dimensions: 1536 },
68
+ );
69
+ await store.ensureSchema(); // CREATE EXTENSION vector + table + HNSW cosine index
70
+ ```
71
+
72
+ ## API
73
+
74
+ - `chunkText(text, { chunkSize?, overlap? })` — overlapping, boundary-aware chunks.
75
+ - `ingestDocuments(docs, { embedder, store, chunkSize?, overlap? })` — chunk → embed → upsert.
76
+ - `EmbeddingRetriever(embedder, store)` — the core `Retriever` from an embedder + vector store.
77
+ - `MemoryVectorStore` / `PgVectorStore` — `VectorStore` adapters.
78
+ - `createRetrievalTool(retriever, { name?, description?, topK? })` — the agentic-retrieval tool.