@molecule/api-ai-rag-qa 1.0.0 → 1.0.2
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/README.md +224 -0
- package/package.json +17 -10
package/README.md
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AUTO-GENERATED — DO NOT EDIT THIS FILE.
|
|
3
|
+
Generated by `mlcl sync-docs` from the package's src/index.ts JSDoc + mlcl/registry.json.
|
|
4
|
+
Edits here are overwritten on the next commit (molecule's pre-commit hook regenerates).
|
|
5
|
+
To change this document, edit the module-level JSDoc in src/index.ts.
|
|
6
|
+
Generated: 2026-08-04T01:47:36.311Z
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
# @molecule/api-ai-rag-qa
|
|
10
|
+
|
|
11
|
+
> **Auto-generated, AI-first package reference** for the [molecule.dev](https://molecule.dev) ecosystem.
|
|
12
|
+
> It is written to be read by coding agents as much as by people, and is generated from this
|
|
13
|
+
> package's source — edit `src/index.ts` JSDoc, not this file.
|
|
14
|
+
|
|
15
|
+
`@molecule/api-ai-rag-qa` — RAG (retrieval-augmented generation) Q&A
|
|
16
|
+
pipeline. Chunk source documents, embed them, store in the vector
|
|
17
|
+
bond, then answer questions grounded in retrieved sources.
|
|
18
|
+
|
|
19
|
+
Composes the existing `@molecule/api-ai`, `@molecule/api-ai-embeddings`,
|
|
20
|
+
and `@molecule/api-ai-vector-store` bonds — works with any provider
|
|
21
|
+
mix (Anthropic + OpenAI embeddings + pgvector, etc.).
|
|
22
|
+
|
|
23
|
+
Extracted from the rag-knowledge-base flagship.
|
|
24
|
+
|
|
25
|
+
## Quick Start
|
|
26
|
+
|
|
27
|
+
```ts
|
|
28
|
+
import { indexDocument, answerQuestion } from '@molecule/api-ai-rag-qa'
|
|
29
|
+
|
|
30
|
+
await indexDocument({
|
|
31
|
+
collection: 'docs',
|
|
32
|
+
documentId: 'getting-started',
|
|
33
|
+
text: longMarkdown,
|
|
34
|
+
metadata: { source: 'README.md' },
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const { answer, sources } = await answerQuestion({
|
|
38
|
+
collection: 'docs',
|
|
39
|
+
question: 'How do I configure auth?',
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Type
|
|
44
|
+
|
|
45
|
+
`utility`
|
|
46
|
+
|
|
47
|
+
## Installation
|
|
48
|
+
|
|
49
|
+
```bash
|
|
50
|
+
npm install @molecule/api-ai-rag-qa @molecule/api-ai @molecule/api-ai-embeddings @molecule/api-ai-vector-store @molecule/api-bonds-default-express @molecule/api-database @molecule/api-i18n @molecule/api-middleware-validation
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## API
|
|
54
|
+
|
|
55
|
+
### Interfaces
|
|
56
|
+
|
|
57
|
+
#### `Chunk`
|
|
58
|
+
|
|
59
|
+
A chunk of source material to be embedded + indexed.
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
interface Chunk {
|
|
63
|
+
id: string
|
|
64
|
+
text: string
|
|
65
|
+
metadata?: Record<string, unknown>
|
|
66
|
+
}
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
#### `ChunkOptions`
|
|
70
|
+
|
|
71
|
+
Chunking options for `chunkText`.
|
|
72
|
+
|
|
73
|
+
```typescript
|
|
74
|
+
interface ChunkOptions {
|
|
75
|
+
/** Max characters per chunk. Default 1000. */
|
|
76
|
+
maxChars?: number
|
|
77
|
+
/** Overlap characters between adjacent chunks. Default 200. */
|
|
78
|
+
overlap?: number
|
|
79
|
+
/** Prefer chunking at paragraph boundaries when possible. Default true. */
|
|
80
|
+
preferParagraphs?: boolean
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### `GroundedAnswer`
|
|
85
|
+
|
|
86
|
+
Final grounded answer + the sources it cited.
|
|
87
|
+
|
|
88
|
+
```typescript
|
|
89
|
+
interface GroundedAnswer {
|
|
90
|
+
answer: string
|
|
91
|
+
sources: RetrievalHit[]
|
|
92
|
+
/** Token / chunk count used for the prompt (for cost telemetry). */
|
|
93
|
+
contextTokens?: number
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
#### `RetrievalHit`
|
|
98
|
+
|
|
99
|
+
A retrieval hit from the vector store.
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
interface RetrievalHit {
|
|
103
|
+
id: string
|
|
104
|
+
text: string
|
|
105
|
+
score: number
|
|
106
|
+
metadata?: Record<string, unknown>
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Functions
|
|
111
|
+
|
|
112
|
+
#### `answerQuestion(opts)`
|
|
113
|
+
|
|
114
|
+
Full RAG round-trip: retrieve top-K chunks, format them as numbered
|
|
115
|
+
sources, and ground a generated answer in them.
|
|
116
|
+
|
|
117
|
+
```typescript
|
|
118
|
+
function answerQuestion(opts: {
|
|
119
|
+
collection: string
|
|
120
|
+
question: string
|
|
121
|
+
topK?: number
|
|
122
|
+
filter?: MetadataFilter[]
|
|
123
|
+
promptTemplate?: string
|
|
124
|
+
model?: string
|
|
125
|
+
temperature?: number
|
|
126
|
+
}): Promise<GroundedAnswer>
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
#### `chunkText(text, opts?)`
|
|
130
|
+
|
|
131
|
+
Split a long text into overlapping chunks suitable for embedding.
|
|
132
|
+
|
|
133
|
+
Tries paragraph boundaries first, then sentence boundaries, then
|
|
134
|
+
character boundaries. Each chunk includes `overlap` characters from
|
|
135
|
+
the prior chunk to preserve context across boundaries.
|
|
136
|
+
|
|
137
|
+
```typescript
|
|
138
|
+
function chunkText(text: string, opts?: ChunkOptions): string[]
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
#### `deleteDocument(opts)`
|
|
142
|
+
|
|
143
|
+
Delete all chunks for a previously-indexed document.
|
|
144
|
+
|
|
145
|
+
```typescript
|
|
146
|
+
function deleteDocument(opts: {
|
|
147
|
+
collection: string
|
|
148
|
+
documentId: string
|
|
149
|
+
maxChunks?: number
|
|
150
|
+
}): Promise<void>
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
#### `indexDocument(opts)`
|
|
154
|
+
|
|
155
|
+
Index a long document by chunking, embedding, and upserting to the vector store.
|
|
156
|
+
|
|
157
|
+
```typescript
|
|
158
|
+
function indexDocument(opts: {
|
|
159
|
+
collection: string
|
|
160
|
+
documentId: string
|
|
161
|
+
text: string
|
|
162
|
+
metadata?: Record<string, unknown>
|
|
163
|
+
chunking?: ChunkOptions
|
|
164
|
+
}): Promise<string[]>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
#### `retrieve(opts)`
|
|
168
|
+
|
|
169
|
+
Retrieve top-K most similar chunks for a query.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
function retrieve(opts: {
|
|
173
|
+
collection: string
|
|
174
|
+
query: string
|
|
175
|
+
topK?: number
|
|
176
|
+
filter?: MetadataFilter[]
|
|
177
|
+
}): Promise<RetrievalHit[]>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
## Injection Notes
|
|
181
|
+
|
|
182
|
+
### Requirements
|
|
183
|
+
|
|
184
|
+
Peer dependencies:
|
|
185
|
+
|
|
186
|
+
- `@molecule/api-bonds-default-express` ^1.0.1
|
|
187
|
+
- `@molecule/api-database` ^1.0.1
|
|
188
|
+
- `@molecule/api-i18n` ^1.0.1
|
|
189
|
+
- `@molecule/api-middleware-validation` ^1.0.1
|
|
190
|
+
- `@molecule/api-ai` ^1.0.1
|
|
191
|
+
- `@molecule/api-ai-embeddings` ^1.0.1
|
|
192
|
+
- `@molecule/api-ai-vector-store` ^1.0.1
|
|
193
|
+
|
|
194
|
+
### Runtime Dependencies
|
|
195
|
+
|
|
196
|
+
- `@molecule/api-ai`
|
|
197
|
+
- `@molecule/api-ai-embeddings`
|
|
198
|
+
- `@molecule/api-ai-vector-store`
|
|
199
|
+
- `@molecule/api-bonds-default-express`
|
|
200
|
+
- `@molecule/api-database`
|
|
201
|
+
- `@molecule/api-i18n`
|
|
202
|
+
- `@molecule/api-middleware-validation`
|
|
203
|
+
|
|
204
|
+
Wiring — the three composed cores use TWO different mechanisms:
|
|
205
|
+
|
|
206
|
+
- `@molecule/api-ai-embeddings` and `@molecule/api-ai-vector-store` each
|
|
207
|
+
keep their OWN singleton: wire each with THAT core's `setProvider(...)`
|
|
208
|
+
(e.g. `setProvider(provider)` from `@molecule/api-ai-embeddings-local`).
|
|
209
|
+
A generic `bond('ai-embeddings', …)` / `bond('ai-vector-store', …)` call
|
|
210
|
+
is never seen by those cores — `indexDocument()` / `retrieve()` then throw
|
|
211
|
+
"provider not configured" even though the bond call appeared to succeed.
|
|
212
|
+
- `@molecule/api-ai` (used by `answerQuestion`) IS registry-based:
|
|
213
|
+
`bond('ai', provider)` or named providers work.
|
|
214
|
+
|
|
215
|
+
Vectors are only comparable within ONE embeddings model + dimension:
|
|
216
|
+
after switching embeddings providers/models, re-index the collection —
|
|
217
|
+
`retrieve()` against vectors from a different model returns meaningless
|
|
218
|
+
similarity scores, not an error.
|
|
219
|
+
|
|
220
|
+
`deleteDocument()` deletes constructed chunk ids (`<documentId>::0..N-1`,
|
|
221
|
+
default `maxChunks: 1000`) — pass a larger `maxChunks` if a document
|
|
222
|
+
chunked into more. `answerQuestion()` with zero retrieval hits resolves
|
|
223
|
+
with the literal "I don't know based on the provided sources." and
|
|
224
|
+
`sources: []` — no model call is made.
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@molecule/api-ai-rag-qa",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "RAG Q&A pipeline — chunk + embed + retrieve + ground answer with sources",
|
|
5
|
+
"homepage": "https://www.molecule.dev/packages/api-ai-rag-qa",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"main": "dist/index.js",
|
|
7
8
|
"types": "dist/index.d.ts",
|
|
@@ -17,7 +18,8 @@
|
|
|
17
18
|
}
|
|
18
19
|
},
|
|
19
20
|
"files": [
|
|
20
|
-
"dist"
|
|
21
|
+
"dist",
|
|
22
|
+
"README.md"
|
|
21
23
|
],
|
|
22
24
|
"keywords": [
|
|
23
25
|
"molecule",
|
|
@@ -29,18 +31,23 @@
|
|
|
29
31
|
"resource"
|
|
30
32
|
],
|
|
31
33
|
"license": "Apache-2.0",
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "https://github.com/molecule-dev/molecule.git",
|
|
37
|
+
"directory": "packages/api/ai/rag-qa"
|
|
38
|
+
},
|
|
32
39
|
"devDependencies": {
|
|
33
40
|
"@types/node": "26.1.2",
|
|
34
41
|
"typescript": "6.0.3",
|
|
35
|
-
"vitest": "4.1.
|
|
42
|
+
"vitest": "4.1.11"
|
|
36
43
|
},
|
|
37
44
|
"peerDependencies": {
|
|
38
|
-
"@molecule/api-bonds-default-express": "^1.0.
|
|
39
|
-
"@molecule/api-database": "^1.0.
|
|
40
|
-
"@molecule/api-i18n": "^1.0.
|
|
41
|
-
"@molecule/api-middleware-validation": "^1.0.
|
|
42
|
-
"@molecule/api-ai": "^1.0.
|
|
43
|
-
"@molecule/api-ai-embeddings": "^1.0.
|
|
44
|
-
"@molecule/api-ai-vector-store": "^1.0.
|
|
45
|
+
"@molecule/api-bonds-default-express": "^1.0.1",
|
|
46
|
+
"@molecule/api-database": "^1.0.1",
|
|
47
|
+
"@molecule/api-i18n": "^1.0.1",
|
|
48
|
+
"@molecule/api-middleware-validation": "^1.0.1",
|
|
49
|
+
"@molecule/api-ai": "^1.0.1",
|
|
50
|
+
"@molecule/api-ai-embeddings": "^1.0.1",
|
|
51
|
+
"@molecule/api-ai-vector-store": "^1.0.1"
|
|
45
52
|
}
|
|
46
53
|
}
|