@devilsdev/rag-pipeline-utils 2.1.11 → 2.1.12
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 +389 -391
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,67 +18,101 @@ npm install @devilsdev/rag-pipeline-utils
|
|
|
18
18
|
|
|
19
19
|
## Quick Start
|
|
20
20
|
|
|
21
|
-
### Basic Pipeline
|
|
21
|
+
### Basic Pipeline Setup
|
|
22
22
|
|
|
23
23
|
```javascript
|
|
24
24
|
const { createRagPipeline } = require("@devilsdev/rag-pipeline-utils");
|
|
25
25
|
|
|
26
|
-
//
|
|
26
|
+
// Create pipeline with your plugin implementations
|
|
27
27
|
const pipeline = createRagPipeline({
|
|
28
|
-
loader:
|
|
29
|
-
embedder:
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
}),
|
|
33
|
-
retriever: new ChromaRetriever({
|
|
34
|
-
url: "http://localhost:8000",
|
|
35
|
-
collection: "documents",
|
|
36
|
-
}),
|
|
37
|
-
llm: new OpenAILLM({
|
|
38
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
39
|
-
model: "gpt-4",
|
|
40
|
-
}),
|
|
28
|
+
loader: yourLoaderPlugin,
|
|
29
|
+
embedder: yourEmbedderPlugin,
|
|
30
|
+
retriever: yourRetrieverPlugin,
|
|
31
|
+
llm: yourLLMPlugin,
|
|
41
32
|
});
|
|
42
33
|
|
|
43
|
-
//
|
|
44
|
-
await pipeline.ingest("./
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
const result = await pipeline.query("What is the main conclusion?");
|
|
48
|
-
console.log(result.answer);
|
|
34
|
+
// Use the pipeline
|
|
35
|
+
await pipeline.ingest("./document.pdf");
|
|
36
|
+
const result = await pipeline.query("What is this document about?");
|
|
37
|
+
console.log(result);
|
|
49
38
|
```
|
|
50
39
|
|
|
51
|
-
###
|
|
40
|
+
### Using Configuration File
|
|
52
41
|
|
|
53
|
-
|
|
54
|
-
// Stream tokens in real-time
|
|
55
|
-
const stream = await pipeline.query("Explain the methodology", {
|
|
56
|
-
stream: true,
|
|
57
|
-
});
|
|
42
|
+
Create `.ragrc.json`:
|
|
58
43
|
|
|
59
|
-
|
|
60
|
-
|
|
44
|
+
```json
|
|
45
|
+
{
|
|
46
|
+
"loader": {
|
|
47
|
+
"pdf": "your-pdf-loader-plugin"
|
|
48
|
+
},
|
|
49
|
+
"embedder": {
|
|
50
|
+
"default": "your-embedder-plugin"
|
|
51
|
+
},
|
|
52
|
+
"retriever": {
|
|
53
|
+
"default": "your-vector-db-plugin"
|
|
54
|
+
},
|
|
55
|
+
"llm": {
|
|
56
|
+
"default": "your-llm-plugin"
|
|
57
|
+
}
|
|
61
58
|
}
|
|
62
59
|
```
|
|
63
60
|
|
|
64
|
-
|
|
61
|
+
Load and use configuration:
|
|
65
62
|
|
|
66
63
|
```javascript
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
);
|
|
64
|
+
const {
|
|
65
|
+
loadConfig,
|
|
66
|
+
createRagPipeline,
|
|
67
|
+
} = require("@devilsdev/rag-pipeline-utils");
|
|
68
|
+
|
|
69
|
+
const config = await loadConfig(".ragrc.json");
|
|
70
|
+
const pipeline = createRagPipeline(config);
|
|
75
71
|
```
|
|
76
72
|
|
|
77
73
|
## Core API
|
|
78
74
|
|
|
75
|
+
### Exported Modules
|
|
76
|
+
|
|
77
|
+
```javascript
|
|
78
|
+
const {
|
|
79
|
+
// Core pipeline
|
|
80
|
+
createRagPipeline,
|
|
81
|
+
|
|
82
|
+
// Configuration
|
|
83
|
+
loadConfig,
|
|
84
|
+
validateRagrc,
|
|
85
|
+
normalizeConfig,
|
|
86
|
+
|
|
87
|
+
// Plugin system
|
|
88
|
+
pluginRegistry,
|
|
89
|
+
|
|
90
|
+
// DAG workflow engine
|
|
91
|
+
DAGEngine,
|
|
92
|
+
|
|
93
|
+
// Observability
|
|
94
|
+
eventLogger,
|
|
95
|
+
metrics,
|
|
96
|
+
|
|
97
|
+
// Performance
|
|
98
|
+
ParallelProcessor,
|
|
99
|
+
|
|
100
|
+
// AI/ML
|
|
101
|
+
MultiModalProcessor,
|
|
102
|
+
AdaptiveRetrievalEngine,
|
|
103
|
+
|
|
104
|
+
// Enterprise
|
|
105
|
+
AuditLogger,
|
|
106
|
+
DataGovernance,
|
|
107
|
+
|
|
108
|
+
// Utilities
|
|
109
|
+
logger,
|
|
110
|
+
} = require("@devilsdev/rag-pipeline-utils");
|
|
111
|
+
```
|
|
112
|
+
|
|
79
113
|
### createRagPipeline(config)
|
|
80
114
|
|
|
81
|
-
Creates a pipeline instance
|
|
115
|
+
Creates a RAG pipeline instance.
|
|
82
116
|
|
|
83
117
|
**Parameters:**
|
|
84
118
|
|
|
@@ -88,76 +122,130 @@ Creates a pipeline instance with the specified plugins.
|
|
|
88
122
|
- `config.llm` - Language model plugin
|
|
89
123
|
- `config.reranker` (optional) - Result reranking plugin
|
|
90
124
|
|
|
91
|
-
**Returns:** Pipeline instance
|
|
125
|
+
**Returns:** Pipeline instance with `ingest()` and `query()` methods
|
|
126
|
+
|
|
127
|
+
### Configuration Management
|
|
92
128
|
|
|
93
|
-
|
|
129
|
+
```javascript
|
|
130
|
+
// Load configuration from file
|
|
131
|
+
const config = await loadConfig(".ragrc.json");
|
|
94
132
|
|
|
95
|
-
|
|
133
|
+
// Validate configuration
|
|
134
|
+
const isValid = validateRagrc(config);
|
|
135
|
+
|
|
136
|
+
// Normalize configuration
|
|
137
|
+
const normalized = normalizeConfig(config);
|
|
138
|
+
```
|
|
96
139
|
|
|
97
|
-
|
|
140
|
+
### Plugin Registry
|
|
98
141
|
|
|
99
142
|
```javascript
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
143
|
+
const { pluginRegistry } = require("@devilsdev/rag-pipeline-utils");
|
|
144
|
+
|
|
145
|
+
// Register a plugin
|
|
146
|
+
pluginRegistry.register("my-plugin", MyPluginClass);
|
|
147
|
+
|
|
148
|
+
// Get a plugin
|
|
149
|
+
const Plugin = pluginRegistry.get("my-plugin");
|
|
150
|
+
|
|
151
|
+
// List all plugins
|
|
152
|
+
const plugins = pluginRegistry.list();
|
|
153
|
+
|
|
154
|
+
// Check if plugin exists
|
|
155
|
+
const exists = pluginRegistry.has("my-plugin");
|
|
105
156
|
```
|
|
106
157
|
|
|
107
|
-
|
|
158
|
+
### DAG Workflow Engine
|
|
108
159
|
|
|
109
|
-
|
|
160
|
+
Build complex multi-step workflows:
|
|
110
161
|
|
|
111
162
|
```javascript
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
163
|
+
const { DAGEngine } = require("@devilsdev/rag-pipeline-utils");
|
|
164
|
+
|
|
165
|
+
const dag = new DAGEngine({
|
|
166
|
+
nodes: [
|
|
167
|
+
{
|
|
168
|
+
id: "load",
|
|
169
|
+
type: "loader",
|
|
170
|
+
config: {
|
|
171
|
+
/* loader config */
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
{
|
|
175
|
+
id: "embed",
|
|
176
|
+
type: "embedder",
|
|
177
|
+
dependencies: ["load"],
|
|
178
|
+
config: {
|
|
179
|
+
/* embedder config */
|
|
180
|
+
},
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
id: "store",
|
|
184
|
+
type: "retriever",
|
|
185
|
+
dependencies: ["embed"],
|
|
186
|
+
config: {
|
|
187
|
+
/* retriever config */
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
],
|
|
116
191
|
});
|
|
192
|
+
|
|
193
|
+
await dag.execute();
|
|
117
194
|
```
|
|
118
195
|
|
|
119
|
-
|
|
196
|
+
### Observability
|
|
120
197
|
|
|
121
198
|
```javascript
|
|
122
|
-
{
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
metrics: {
|
|
130
|
-
retrievalTime: number,
|
|
131
|
-
generationTime: number,
|
|
132
|
-
totalTime: number
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
```
|
|
199
|
+
const { eventLogger, metrics } = require("@devilsdev/rag-pipeline-utils");
|
|
200
|
+
|
|
201
|
+
// Configure event logging
|
|
202
|
+
eventLogger.configure({
|
|
203
|
+
level: "info",
|
|
204
|
+
format: "json",
|
|
205
|
+
});
|
|
136
206
|
|
|
137
|
-
|
|
207
|
+
// Log events
|
|
208
|
+
eventLogger.log("info", "Pipeline started", { pipelineId: "abc123" });
|
|
209
|
+
|
|
210
|
+
// Access metrics
|
|
211
|
+
const pipelineMetrics = metrics.getMetrics();
|
|
212
|
+
console.log(pipelineMetrics);
|
|
213
|
+
```
|
|
138
214
|
|
|
139
|
-
|
|
215
|
+
### Parallel Processing
|
|
140
216
|
|
|
141
217
|
```javascript
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
218
|
+
const { ParallelProcessor } = require("@devilsdev/rag-pipeline-utils");
|
|
219
|
+
|
|
220
|
+
const processor = new ParallelProcessor({
|
|
221
|
+
maxConcurrency: 5,
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
await processor.process(items, async (item) => {
|
|
225
|
+
// Process each item
|
|
226
|
+
return processItem(item);
|
|
145
227
|
});
|
|
146
228
|
```
|
|
147
229
|
|
|
148
|
-
## Plugin
|
|
230
|
+
## Plugin Development
|
|
149
231
|
|
|
150
|
-
|
|
232
|
+
This package provides a plugin-based architecture. You need to implement plugins for your specific use case.
|
|
151
233
|
|
|
152
|
-
|
|
234
|
+
### Plugin Contracts
|
|
153
235
|
|
|
154
236
|
#### Loader Plugin
|
|
155
237
|
|
|
156
238
|
```javascript
|
|
157
|
-
class
|
|
239
|
+
class MyLoader {
|
|
158
240
|
async load(filePath) {
|
|
241
|
+
// Load and parse document
|
|
159
242
|
// Return array of document chunks
|
|
160
|
-
return [
|
|
243
|
+
return [
|
|
244
|
+
{
|
|
245
|
+
content: "Document text...",
|
|
246
|
+
metadata: { page: 1, source: filePath },
|
|
247
|
+
},
|
|
248
|
+
];
|
|
161
249
|
}
|
|
162
250
|
}
|
|
163
251
|
```
|
|
@@ -165,19 +253,19 @@ class CustomLoader {
|
|
|
165
253
|
#### Embedder Plugin
|
|
166
254
|
|
|
167
255
|
```javascript
|
|
168
|
-
class
|
|
256
|
+
class MyEmbedder {
|
|
169
257
|
async embed(texts) {
|
|
258
|
+
// Generate embeddings for array of texts
|
|
170
259
|
// Return array of vectors
|
|
171
|
-
return [
|
|
260
|
+
return [
|
|
261
|
+
[0.1, 0.2, 0.3, ...], // vector for text[0]
|
|
262
|
+
[0.4, 0.5, 0.6, ...] // vector for text[1]
|
|
263
|
+
];
|
|
172
264
|
}
|
|
173
265
|
|
|
174
266
|
async embedQuery(query) {
|
|
175
|
-
//
|
|
176
|
-
return [0.
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
getDimensions() {
|
|
180
|
-
return 1536; // Vector dimensions
|
|
267
|
+
// Generate embedding for single query
|
|
268
|
+
return [0.7, 0.8, 0.9, ...];
|
|
181
269
|
}
|
|
182
270
|
}
|
|
183
271
|
```
|
|
@@ -185,15 +273,25 @@ class CustomEmbedder {
|
|
|
185
273
|
#### Retriever Plugin
|
|
186
274
|
|
|
187
275
|
```javascript
|
|
188
|
-
class
|
|
276
|
+
class MyRetriever {
|
|
189
277
|
async store(vectors, metadata) {
|
|
190
|
-
// Store vectors with metadata
|
|
278
|
+
// Store vectors with metadata in your vector database
|
|
191
279
|
}
|
|
192
280
|
|
|
193
281
|
async retrieve(queryVector, options = {}) {
|
|
194
282
|
const { topK = 5, threshold = 0.7 } = options;
|
|
195
|
-
|
|
196
|
-
|
|
283
|
+
|
|
284
|
+
// Search for similar vectors
|
|
285
|
+
// Return documents with similarity scores
|
|
286
|
+
return [
|
|
287
|
+
{
|
|
288
|
+
content: "Document text...",
|
|
289
|
+
metadata: {
|
|
290
|
+
/* document metadata */
|
|
291
|
+
},
|
|
292
|
+
score: 0.95,
|
|
293
|
+
},
|
|
294
|
+
];
|
|
197
295
|
}
|
|
198
296
|
}
|
|
199
297
|
```
|
|
@@ -201,19 +299,20 @@ class CustomRetriever {
|
|
|
201
299
|
#### LLM Plugin
|
|
202
300
|
|
|
203
301
|
```javascript
|
|
204
|
-
class
|
|
302
|
+
class MyLLM {
|
|
205
303
|
async generate(prompt, options = {}) {
|
|
206
|
-
//
|
|
207
|
-
return {
|
|
304
|
+
// Generate text from prompt
|
|
305
|
+
return {
|
|
306
|
+
text: "Generated response...",
|
|
307
|
+
usage: { tokens: 150 },
|
|
308
|
+
};
|
|
208
309
|
}
|
|
209
310
|
|
|
210
|
-
async stream(prompt, options = {}) {
|
|
211
|
-
//
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
return generate();
|
|
311
|
+
async *stream(prompt, options = {}) {
|
|
312
|
+
// Stream response chunks
|
|
313
|
+
yield "Generated ";
|
|
314
|
+
yield "response ";
|
|
315
|
+
yield "chunks...";
|
|
217
316
|
}
|
|
218
317
|
}
|
|
219
318
|
```
|
|
@@ -221,241 +320,163 @@ class CustomLLM {
|
|
|
221
320
|
#### Reranker Plugin (Optional)
|
|
222
321
|
|
|
223
322
|
```javascript
|
|
224
|
-
class
|
|
323
|
+
class MyReranker {
|
|
225
324
|
async rerank(query, documents, options = {}) {
|
|
226
|
-
//
|
|
227
|
-
return documents
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
325
|
+
// Rerank documents based on relevance to query
|
|
326
|
+
return documents
|
|
327
|
+
.map((doc) => ({
|
|
328
|
+
...doc,
|
|
329
|
+
score: calculateRelevanceScore(query, doc),
|
|
330
|
+
}))
|
|
331
|
+
.sort((a, b) => b.score - a.score);
|
|
231
332
|
}
|
|
232
333
|
}
|
|
233
334
|
```
|
|
234
335
|
|
|
235
|
-
## Configuration
|
|
236
|
-
|
|
237
|
-
### Environment Variables
|
|
336
|
+
## Environment Configuration
|
|
238
337
|
|
|
239
338
|
```bash
|
|
240
|
-
#
|
|
241
|
-
OPENAI_API_KEY=
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
# Pinecone
|
|
245
|
-
PINECONE_API_KEY=...
|
|
339
|
+
# Example environment variables for your plugins
|
|
340
|
+
OPENAI_API_KEY=your_api_key
|
|
341
|
+
PINECONE_API_KEY=your_api_key
|
|
246
342
|
PINECONE_ENVIRONMENT=us-east-1
|
|
247
|
-
|
|
248
|
-
# ChromaDB
|
|
249
343
|
CHROMA_URL=http://localhost:8000
|
|
250
344
|
|
|
251
|
-
# Performance
|
|
345
|
+
# Performance settings
|
|
252
346
|
RAG_MAX_CONCURRENCY=5
|
|
253
347
|
RAG_BATCH_SIZE=10
|
|
254
348
|
```
|
|
255
349
|
|
|
256
|
-
|
|
350
|
+
## Advanced Features
|
|
257
351
|
|
|
258
|
-
|
|
352
|
+
### Multi-Modal Processing
|
|
259
353
|
|
|
260
|
-
```
|
|
261
|
-
{
|
|
262
|
-
"loader": {
|
|
263
|
-
"pdf": "@devilsdev/pdf-loader",
|
|
264
|
-
"markdown": "@devilsdev/markdown-loader"
|
|
265
|
-
},
|
|
266
|
-
"embedder": {
|
|
267
|
-
"openai": "@devilsdev/openai-embedder"
|
|
268
|
-
},
|
|
269
|
-
"retriever": {
|
|
270
|
-
"chroma": "@devilsdev/chroma-retriever"
|
|
271
|
-
},
|
|
272
|
-
"llm": {
|
|
273
|
-
"openai": "@devilsdev/openai-llm"
|
|
274
|
-
},
|
|
275
|
-
"pipeline": {
|
|
276
|
-
"loader": "pdf",
|
|
277
|
-
"embedder": "openai",
|
|
278
|
-
"retriever": "chroma",
|
|
279
|
-
"llm": "openai"
|
|
280
|
-
},
|
|
281
|
-
"performance": {
|
|
282
|
-
"maxConcurrency": 5,
|
|
283
|
-
"enableStreaming": true
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
```
|
|
354
|
+
```javascript
|
|
355
|
+
const { MultiModalProcessor } = require("@devilsdev/rag-pipeline-utils");
|
|
287
356
|
|
|
288
|
-
|
|
357
|
+
const processor = new MultiModalProcessor({
|
|
358
|
+
// Configuration for processing text, images, audio, etc.
|
|
359
|
+
});
|
|
289
360
|
|
|
290
|
-
|
|
291
|
-
const { loadConfig } = require("@devilsdev/rag-pipeline-utils");
|
|
292
|
-
const config = await loadConfig(".ragrc.json");
|
|
361
|
+
await processor.process(multiModalData);
|
|
293
362
|
```
|
|
294
363
|
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
### Custom Pipeline Components
|
|
364
|
+
### Adaptive Retrieval
|
|
298
365
|
|
|
299
366
|
```javascript
|
|
300
|
-
const {
|
|
301
|
-
createRagPipeline,
|
|
302
|
-
pluginRegistry,
|
|
303
|
-
} = require("@devilsdev/rag-pipeline-utils");
|
|
304
|
-
|
|
305
|
-
// Register custom plugin
|
|
306
|
-
pluginRegistry.register("my-embedder", MyCustomEmbedder);
|
|
367
|
+
const { AdaptiveRetrievalEngine } = require("@devilsdev/rag-pipeline-utils");
|
|
307
368
|
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
loader: new PDFLoader(),
|
|
311
|
-
embedder: pluginRegistry.get("my-embedder"),
|
|
312
|
-
retriever: new ChromaRetriever({ url: "http://localhost:8000" }),
|
|
313
|
-
llm: new OpenAILLM({ model: "gpt-4" }),
|
|
369
|
+
const retriever = new AdaptiveRetrievalEngine({
|
|
370
|
+
// Configuration for adaptive retrieval strategies
|
|
314
371
|
});
|
|
315
|
-
```
|
|
316
372
|
|
|
317
|
-
|
|
373
|
+
const results = await retriever.retrieve(query, context);
|
|
374
|
+
```
|
|
318
375
|
|
|
319
|
-
|
|
376
|
+
### Enterprise Audit Logging
|
|
320
377
|
|
|
321
378
|
```javascript
|
|
322
|
-
const {
|
|
379
|
+
const { AuditLogger } = require("@devilsdev/rag-pipeline-utils");
|
|
323
380
|
|
|
324
|
-
const
|
|
325
|
-
|
|
326
|
-
{
|
|
327
|
-
id: "load",
|
|
328
|
-
type: "loader",
|
|
329
|
-
config: { source: "./docs" },
|
|
330
|
-
},
|
|
331
|
-
{
|
|
332
|
-
id: "embed",
|
|
333
|
-
type: "embedder",
|
|
334
|
-
dependencies: ["load"],
|
|
335
|
-
config: { model: "text-embedding-ada-002" },
|
|
336
|
-
},
|
|
337
|
-
{
|
|
338
|
-
id: "store",
|
|
339
|
-
type: "retriever",
|
|
340
|
-
dependencies: ["embed"],
|
|
341
|
-
config: { collection: "documents" },
|
|
342
|
-
},
|
|
343
|
-
],
|
|
381
|
+
const auditLogger = new AuditLogger({
|
|
382
|
+
destination: "./audit-logs",
|
|
344
383
|
});
|
|
345
384
|
|
|
346
|
-
|
|
385
|
+
auditLogger.log("query", {
|
|
386
|
+
user: "user@example.com",
|
|
387
|
+
query: "sensitive query",
|
|
388
|
+
timestamp: new Date(),
|
|
389
|
+
});
|
|
347
390
|
```
|
|
348
391
|
|
|
349
|
-
###
|
|
350
|
-
|
|
351
|
-
Enable monitoring and tracing:
|
|
392
|
+
### Data Governance
|
|
352
393
|
|
|
353
394
|
```javascript
|
|
354
|
-
const {
|
|
395
|
+
const { DataGovernance } = require("@devilsdev/rag-pipeline-utils");
|
|
355
396
|
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
397
|
+
const governance = new DataGovernance({
|
|
398
|
+
policies: {
|
|
399
|
+
retention: "90d",
|
|
400
|
+
encryption: true,
|
|
401
|
+
},
|
|
360
402
|
});
|
|
361
403
|
|
|
362
|
-
|
|
363
|
-
const pipelineMetrics = metrics.getMetrics();
|
|
364
|
-
console.log(pipelineMetrics.queries.total);
|
|
365
|
-
console.log(pipelineMetrics.queries.avgLatency);
|
|
366
|
-
```
|
|
367
|
-
|
|
368
|
-
### Error Handling
|
|
369
|
-
|
|
370
|
-
```javascript
|
|
371
|
-
try {
|
|
372
|
-
const result = await pipeline.query("What is AI?");
|
|
373
|
-
} catch (error) {
|
|
374
|
-
if (error.code === "RETRIEVAL_ERROR") {
|
|
375
|
-
console.error("Vector search failed:", error.message);
|
|
376
|
-
} else if (error.code === "LLM_ERROR") {
|
|
377
|
-
console.error("Generation failed:", error.message);
|
|
378
|
-
}
|
|
379
|
-
}
|
|
404
|
+
await governance.enforce(data);
|
|
380
405
|
```
|
|
381
406
|
|
|
382
407
|
## CLI Usage
|
|
383
408
|
|
|
384
|
-
The package includes a CLI for
|
|
409
|
+
The package includes a CLI tool for pipeline operations:
|
|
385
410
|
|
|
386
411
|
```bash
|
|
387
412
|
# Initialize configuration
|
|
388
413
|
npx rag-pipeline init
|
|
389
414
|
|
|
390
|
-
#
|
|
391
|
-
npx rag-pipeline ingest ./
|
|
392
|
-
|
|
393
|
-
# Query interactively
|
|
394
|
-
npx rag-pipeline query "Your question here"
|
|
415
|
+
# Run pipeline operations (requires plugin implementation)
|
|
416
|
+
npx rag-pipeline ingest ./documents
|
|
417
|
+
npx rag-pipeline query "Your question"
|
|
395
418
|
|
|
396
|
-
# Run
|
|
419
|
+
# Run system diagnostics
|
|
397
420
|
npx rag-pipeline doctor
|
|
398
421
|
```
|
|
399
422
|
|
|
400
|
-
##
|
|
401
|
-
|
|
402
|
-
### Document Q&A Systems
|
|
403
|
-
|
|
404
|
-
Build question-answering systems over proprietary documents:
|
|
423
|
+
## Error Handling
|
|
405
424
|
|
|
406
425
|
```javascript
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
})
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
// Answer questions
|
|
419
|
-
const answer = await pipeline.query("What is the vacation policy?");
|
|
426
|
+
try {
|
|
427
|
+
const result = await pipeline.query("What is AI?");
|
|
428
|
+
} catch (error) {
|
|
429
|
+
if (error.code === "PLUGIN_NOT_FOUND") {
|
|
430
|
+
console.error("Required plugin not found:", error.message);
|
|
431
|
+
} else if (error.code === "INVALID_CONFIG") {
|
|
432
|
+
console.error("Invalid configuration:", error.message);
|
|
433
|
+
} else {
|
|
434
|
+
console.error("Pipeline error:", error.message);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
420
437
|
```
|
|
421
438
|
|
|
422
|
-
|
|
439
|
+
## Use Cases
|
|
423
440
|
|
|
424
|
-
|
|
441
|
+
### Document Q&A System
|
|
425
442
|
|
|
426
443
|
```javascript
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
444
|
+
// Implement your plugins
|
|
445
|
+
const loader = new PDFLoaderPlugin();
|
|
446
|
+
const embedder = new OpenAIEmbedderPlugin({
|
|
447
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
431
448
|
});
|
|
449
|
+
const retriever = new ChromaRetrieverPlugin({ url: "http://localhost:8000" });
|
|
450
|
+
const llm = new OpenAILLMPlugin({ model: "gpt-4" });
|
|
432
451
|
|
|
433
|
-
|
|
434
|
-
|
|
452
|
+
// Create pipeline
|
|
453
|
+
const pipeline = createRagPipeline({
|
|
454
|
+
loader,
|
|
455
|
+
embedder,
|
|
456
|
+
retriever,
|
|
457
|
+
llm,
|
|
435
458
|
});
|
|
436
|
-
```
|
|
437
459
|
|
|
438
|
-
|
|
460
|
+
// Ingest documents
|
|
461
|
+
await pipeline.ingest("./company-handbook.pdf");
|
|
439
462
|
|
|
440
|
-
|
|
463
|
+
// Query
|
|
464
|
+
const answer = await pipeline.query("What is the vacation policy?");
|
|
465
|
+
console.log(answer);
|
|
466
|
+
```
|
|
467
|
+
|
|
468
|
+
### Knowledge Base Search
|
|
441
469
|
|
|
442
470
|
```javascript
|
|
443
|
-
//
|
|
471
|
+
// Use your custom database retriever
|
|
444
472
|
class PostgresRetriever {
|
|
445
473
|
constructor(connectionString) {
|
|
446
474
|
this.db = new PostgresClient(connectionString);
|
|
447
475
|
}
|
|
448
476
|
|
|
449
|
-
async store(vectors, metadata) {
|
|
450
|
-
await this.db.query(
|
|
451
|
-
"INSERT INTO embeddings (vector, metadata) VALUES ($1, $2)",
|
|
452
|
-
[vectors, metadata],
|
|
453
|
-
);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
477
|
async retrieve(queryVector, options) {
|
|
457
478
|
const results = await this.db.query(
|
|
458
|
-
"SELECT
|
|
479
|
+
"SELECT content, metadata, vector <-> $1 as score FROM embeddings ORDER BY score LIMIT $2",
|
|
459
480
|
[queryVector, options.topK],
|
|
460
481
|
);
|
|
461
482
|
return results.rows;
|
|
@@ -463,191 +484,169 @@ class PostgresRetriever {
|
|
|
463
484
|
}
|
|
464
485
|
|
|
465
486
|
const pipeline = createRagPipeline({
|
|
466
|
-
loader:
|
|
467
|
-
embedder:
|
|
487
|
+
loader: yourLoader,
|
|
488
|
+
embedder: yourEmbedder,
|
|
468
489
|
retriever: new PostgresRetriever("postgresql://localhost/kb"),
|
|
469
|
-
llm:
|
|
490
|
+
llm: yourLLM,
|
|
470
491
|
});
|
|
471
492
|
```
|
|
472
493
|
|
|
473
|
-
|
|
494
|
+
## Performance Tips
|
|
474
495
|
|
|
475
|
-
|
|
496
|
+
### Batch Processing
|
|
476
497
|
|
|
477
498
|
```javascript
|
|
478
|
-
const
|
|
479
|
-
{ type: "pdf", path: "./research/*.pdf" },
|
|
480
|
-
{ type: "markdown", path: "./docs/*.md" },
|
|
481
|
-
{ type: "api", url: "https://api.example.com/docs" },
|
|
482
|
-
];
|
|
483
|
-
|
|
484
|
-
for (const source of sources) {
|
|
485
|
-
const loader = getLoaderForType(source.type);
|
|
486
|
-
await pipeline.ingest(source.path || source.url, {
|
|
487
|
-
loader,
|
|
488
|
-
metadata: { source: source.type },
|
|
489
|
-
});
|
|
490
|
-
}
|
|
491
|
-
```
|
|
492
|
-
|
|
493
|
-
## Performance Optimization
|
|
499
|
+
const { ParallelProcessor } = require("@devilsdev/rag-pipeline-utils");
|
|
494
500
|
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
await pipeline.ingest("./large-document.pdf", {
|
|
499
|
-
chunkSize: 1000, // Characters per chunk
|
|
500
|
-
chunkOverlap: 200, // Overlap between chunks
|
|
501
|
-
chunkStrategy: "sentence", // 'sentence' | 'paragraph' | 'fixed'
|
|
501
|
+
const processor = new ParallelProcessor({
|
|
502
|
+
maxConcurrency: 5,
|
|
503
|
+
batchSize: 10,
|
|
502
504
|
});
|
|
503
|
-
```
|
|
504
|
-
|
|
505
|
-
### Concurrent Processing
|
|
506
505
|
|
|
507
|
-
|
|
508
|
-
await
|
|
509
|
-
|
|
510
|
-
maxConcurrency: 5, // Parallel operations
|
|
511
|
-
batchSize: 10, // Embeddings per batch
|
|
506
|
+
// Process documents in parallel
|
|
507
|
+
await processor.process(documents, async (doc) => {
|
|
508
|
+
return await pipeline.ingest(doc);
|
|
512
509
|
});
|
|
513
510
|
```
|
|
514
511
|
|
|
515
|
-
###
|
|
512
|
+
### Logging and Monitoring
|
|
516
513
|
|
|
517
514
|
```javascript
|
|
518
|
-
const
|
|
519
|
-
loader: new PDFLoader(),
|
|
520
|
-
embedder: new OpenAIEmbedder({
|
|
521
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
522
|
-
cache: true, // Cache embeddings
|
|
523
|
-
cacheTTL: 3600, // Cache lifetime in seconds
|
|
524
|
-
}),
|
|
525
|
-
retriever: new ChromaRetriever({ url: "http://localhost:8000" }),
|
|
526
|
-
llm: new OpenAILLM({ model: "gpt-4" }),
|
|
527
|
-
});
|
|
528
|
-
```
|
|
529
|
-
|
|
530
|
-
## TypeScript Support
|
|
531
|
-
|
|
532
|
-
Full TypeScript definitions included:
|
|
515
|
+
const { logger, metrics } = require("@devilsdev/rag-pipeline-utils");
|
|
533
516
|
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
createRagPipeline,
|
|
537
|
-
Pipeline,
|
|
538
|
-
LoaderPlugin,
|
|
539
|
-
EmbedderPlugin,
|
|
540
|
-
RetrieverPlugin,
|
|
541
|
-
LLMPlugin,
|
|
542
|
-
} from "@devilsdev/rag-pipeline-utils";
|
|
543
|
-
|
|
544
|
-
const pipeline: Pipeline = createRagPipeline({
|
|
545
|
-
loader: new PDFLoader(),
|
|
546
|
-
embedder: new OpenAIEmbedder({ apiKey: string }),
|
|
547
|
-
retriever: new ChromaRetriever({ url: string }),
|
|
548
|
-
llm: new OpenAILLM({ model: string }),
|
|
549
|
-
});
|
|
550
|
-
|
|
551
|
-
interface QueryResult {
|
|
552
|
-
answer: string;
|
|
553
|
-
sources: Source[];
|
|
554
|
-
metrics: Metrics;
|
|
555
|
-
}
|
|
517
|
+
// Set log level
|
|
518
|
+
logger.setLevel("debug");
|
|
556
519
|
|
|
557
|
-
|
|
520
|
+
// Monitor performance
|
|
521
|
+
const stats = metrics.getMetrics();
|
|
522
|
+
console.log("Total queries:", stats.queries?.total || 0);
|
|
523
|
+
console.log("Average latency:", stats.queries?.avgLatency || 0);
|
|
558
524
|
```
|
|
559
525
|
|
|
560
|
-
##
|
|
526
|
+
## Common Integration Examples
|
|
561
527
|
|
|
562
|
-
###
|
|
528
|
+
### With OpenAI
|
|
563
529
|
|
|
564
|
-
|
|
565
|
-
const {
|
|
566
|
-
// Core
|
|
567
|
-
createRagPipeline,
|
|
568
|
-
loadConfig,
|
|
530
|
+
You need to implement OpenAI plugin wrappers:
|
|
569
531
|
|
|
570
|
-
|
|
571
|
-
|
|
532
|
+
```javascript
|
|
533
|
+
class OpenAIEmbedder {
|
|
534
|
+
constructor({ apiKey, model = "text-embedding-ada-002" }) {
|
|
535
|
+
this.client = new OpenAI({ apiKey });
|
|
536
|
+
this.model = model;
|
|
537
|
+
}
|
|
572
538
|
|
|
573
|
-
|
|
574
|
-
|
|
539
|
+
async embed(texts) {
|
|
540
|
+
const response = await this.client.embeddings.create({
|
|
541
|
+
model: this.model,
|
|
542
|
+
input: texts,
|
|
543
|
+
});
|
|
544
|
+
return response.data.map((d) => d.embedding);
|
|
545
|
+
}
|
|
546
|
+
}
|
|
575
547
|
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
548
|
+
class OpenAILLM {
|
|
549
|
+
constructor({ apiKey, model = "gpt-4" }) {
|
|
550
|
+
this.client = new OpenAI({ apiKey });
|
|
551
|
+
this.model = model;
|
|
552
|
+
}
|
|
579
553
|
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
554
|
+
async generate(prompt) {
|
|
555
|
+
const response = await this.client.chat.completions.create({
|
|
556
|
+
model: this.model,
|
|
557
|
+
messages: [{ role: "user", content: prompt }],
|
|
558
|
+
});
|
|
559
|
+
return { text: response.choices[0].message.content };
|
|
560
|
+
}
|
|
561
|
+
}
|
|
583
562
|
```
|
|
584
563
|
|
|
585
|
-
###
|
|
564
|
+
### With ChromaDB
|
|
586
565
|
|
|
587
566
|
```javascript
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
567
|
+
class ChromaRetriever {
|
|
568
|
+
constructor({ url, collection }) {
|
|
569
|
+
this.client = new ChromaClient({ path: url });
|
|
570
|
+
this.collectionName = collection;
|
|
571
|
+
}
|
|
593
572
|
|
|
594
|
-
|
|
595
|
-
const
|
|
573
|
+
async store(vectors, metadata) {
|
|
574
|
+
const collection = await this.client.getOrCreateCollection({
|
|
575
|
+
name: this.collectionName,
|
|
576
|
+
});
|
|
577
|
+
|
|
578
|
+
await collection.add({
|
|
579
|
+
embeddings: vectors,
|
|
580
|
+
metadatas: metadata,
|
|
581
|
+
ids: metadata.map((_, i) => `doc_${i}`),
|
|
582
|
+
});
|
|
583
|
+
}
|
|
596
584
|
|
|
597
|
-
|
|
598
|
-
const
|
|
585
|
+
async retrieve(queryVector, options) {
|
|
586
|
+
const collection = await this.client.getCollection({
|
|
587
|
+
name: this.collectionName,
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
const results = await collection.query({
|
|
591
|
+
queryEmbeddings: [queryVector],
|
|
592
|
+
nResults: options.topK || 5,
|
|
593
|
+
});
|
|
594
|
+
|
|
595
|
+
return results.metadatas[0].map((meta, i) => ({
|
|
596
|
+
content: results.documents[0][i],
|
|
597
|
+
metadata: meta,
|
|
598
|
+
score: 1 - results.distances[0][i],
|
|
599
|
+
}));
|
|
600
|
+
}
|
|
601
|
+
}
|
|
599
602
|
```
|
|
600
603
|
|
|
601
604
|
## Troubleshooting
|
|
602
605
|
|
|
603
606
|
### Common Issues
|
|
604
607
|
|
|
605
|
-
**Error:
|
|
608
|
+
**Error: Cannot find module**
|
|
606
609
|
|
|
607
610
|
```bash
|
|
608
|
-
npm install
|
|
611
|
+
npm install @devilsdev/rag-pipeline-utils
|
|
609
612
|
```
|
|
610
613
|
|
|
611
|
-
**Error:
|
|
614
|
+
**Error: Plugin not registered**
|
|
612
615
|
|
|
613
616
|
```javascript
|
|
614
|
-
//
|
|
615
|
-
|
|
616
|
-
const embedder = new OpenAIEmbedder({
|
|
617
|
-
apiKey: process.env.OPENAI_API_KEY,
|
|
618
|
-
});
|
|
617
|
+
// Make sure to register your plugins
|
|
618
|
+
pluginRegistry.register("my-embedder", MyEmbedderClass);
|
|
619
619
|
```
|
|
620
620
|
|
|
621
|
-
**Error:
|
|
621
|
+
**Error: Invalid configuration**
|
|
622
622
|
|
|
623
|
-
```
|
|
624
|
-
|
|
625
|
-
|
|
623
|
+
```javascript
|
|
624
|
+
// Validate your configuration
|
|
625
|
+
const { validateRagrc } = require("@devilsdev/rag-pipeline-utils");
|
|
626
|
+
const isValid = validateRagrc(config);
|
|
627
|
+
if (!isValid) {
|
|
628
|
+
console.error("Configuration is invalid");
|
|
629
|
+
}
|
|
626
630
|
```
|
|
627
631
|
|
|
628
632
|
### Debug Mode
|
|
629
633
|
|
|
630
|
-
Enable verbose logging:
|
|
631
|
-
|
|
632
634
|
```javascript
|
|
633
635
|
const { logger } = require("@devilsdev/rag-pipeline-utils");
|
|
634
636
|
|
|
637
|
+
// Enable debug logging
|
|
635
638
|
logger.setLevel("debug");
|
|
636
639
|
```
|
|
637
640
|
|
|
638
|
-
##
|
|
639
|
-
|
|
640
|
-
See the [examples directory](https://github.com/DevilsDev/rag-pipeline-utils/tree/main/examples) for complete working examples:
|
|
641
|
+
## Documentation
|
|
641
642
|
|
|
642
|
-
-
|
|
643
|
-
-
|
|
644
|
-
-
|
|
645
|
-
- DAG workflows
|
|
646
|
-
- Multi-source ingestion
|
|
643
|
+
- [GitHub Repository](https://github.com/DevilsDev/rag-pipeline-utils)
|
|
644
|
+
- [Issue Tracker](https://github.com/DevilsDev/rag-pipeline-utils/issues)
|
|
645
|
+
- [Discussions](https://github.com/DevilsDev/rag-pipeline-utils/discussions)
|
|
647
646
|
|
|
648
647
|
## Contributing
|
|
649
648
|
|
|
650
|
-
Contributions welcome. Please
|
|
649
|
+
Contributions are welcome. Please see the [contribution guidelines](https://github.com/DevilsDev/rag-pipeline-utils/blob/main/docs/CONTRIBUTING.md).
|
|
651
650
|
|
|
652
651
|
## License
|
|
653
652
|
|
|
@@ -655,6 +654,5 @@ GPL-3.0 - See [LICENSE](https://github.com/DevilsDev/rag-pipeline-utils/blob/mai
|
|
|
655
654
|
|
|
656
655
|
## Support
|
|
657
656
|
|
|
658
|
-
- Documentation: https://devilsdev.github.io/rag-pipeline-utils/
|
|
659
657
|
- Issues: https://github.com/DevilsDev/rag-pipeline-utils/issues
|
|
660
|
-
- Discussions: https://github.com/DevilsDev/rag-pipeline-utils/
|
|
658
|
+
- Discussions: https://github.com/DevilsDev/rag-pipeline-utils/discussions
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@devilsdev/rag-pipeline-utils",
|
|
3
|
-
"version": "2.1.
|
|
3
|
+
"version": "2.1.12",
|
|
4
4
|
"description": "A comprehensive enterprise-grade toolkit for building RAG (Retrieval-Augmented Generation) pipelines with advanced AI/ML capabilities",
|
|
5
5
|
"main": "dist/index.cjs",
|
|
6
6
|
"type": "commonjs",
|