@sparkleideas/agentdb-onnx 1.0.1
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/ARCHITECTURE.md +331 -0
- package/IMPLEMENTATION-SUMMARY.md +456 -0
- package/README.md +418 -0
- package/examples/complete-workflow.ts +281 -0
- package/package.json +41 -0
- package/src/benchmarks/benchmark-runner.ts +301 -0
- package/src/cli.ts +245 -0
- package/src/index.ts +128 -0
- package/src/services/ONNXEmbeddingService.ts +459 -0
- package/src/tests/integration.test.ts +302 -0
- package/src/tests/onnx-embedding.test.ts +317 -0
- package/tsconfig.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
# AgentDB-ONNX
|
|
2
|
+
|
|
3
|
+
> **High-Performance AI Agent Memory with ONNX Embeddings**
|
|
4
|
+
|
|
5
|
+
100% local, GPU-accelerated embeddings with AgentDB vector storage for intelligent AI agents.
|
|
6
|
+
|
|
7
|
+
[]()
|
|
8
|
+
[]()
|
|
9
|
+
[]()
|
|
10
|
+
|
|
11
|
+
## ๐ Features
|
|
12
|
+
|
|
13
|
+
### **100% Local Inference**
|
|
14
|
+
- No API calls, no cloud dependencies
|
|
15
|
+
- Complete data privacy
|
|
16
|
+
- Zero latency overhead from network requests
|
|
17
|
+
- Free unlimited embeddings
|
|
18
|
+
|
|
19
|
+
### **GPU Acceleration**
|
|
20
|
+
- ONNX Runtime with CUDA support (Linux/Windows)
|
|
21
|
+
- DirectML support (Windows)
|
|
22
|
+
- CoreML support (macOS)
|
|
23
|
+
- Automatic fallback to CPU
|
|
24
|
+
|
|
25
|
+
### **Performance Optimized**
|
|
26
|
+
- โก **Batch processing**: 3-4x faster than sequential
|
|
27
|
+
- ๐พ **LRU caching**: 80%+ hit rate for common queries
|
|
28
|
+
- ๐ฅ **Model warmup**: Pre-JIT compilation for consistent latency
|
|
29
|
+
- ๐ **Smart batching**: Automatic chunking for large datasets
|
|
30
|
+
|
|
31
|
+
### **Enterprise Features**
|
|
32
|
+
- **ReasoningBank**: Store and retrieve successful patterns
|
|
33
|
+
- **Reflexion Memory**: Self-improving episodic memory
|
|
34
|
+
- **Comprehensive metrics**: Latency, throughput, cache performance
|
|
35
|
+
- **Full TypeScript support**
|
|
36
|
+
|
|
37
|
+
---
|
|
38
|
+
|
|
39
|
+
## ๐ฆ Installation
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npm install agentdb-onnx
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
**Optional GPU acceleration:**
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# CUDA (NVIDIA GPUs on Linux/Windows)
|
|
49
|
+
npm install onnxruntime-node-gpu
|
|
50
|
+
|
|
51
|
+
# DirectML (Any GPU on Windows)
|
|
52
|
+
# Already included in onnxruntime-node on Windows
|
|
53
|
+
|
|
54
|
+
# CoreML (Apple Silicon on macOS)
|
|
55
|
+
# Automatic on macOS ARM64
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
---
|
|
59
|
+
|
|
60
|
+
## โก Quick Start
|
|
61
|
+
|
|
62
|
+
### Basic Usage
|
|
63
|
+
|
|
64
|
+
```typescript
|
|
65
|
+
import { createONNXAgentDB } from 'agentdb-onnx';
|
|
66
|
+
|
|
67
|
+
// Initialize
|
|
68
|
+
const agentdb = await createONNXAgentDB({
|
|
69
|
+
dbPath: './my-agent-memory.db',
|
|
70
|
+
modelName: 'Xenova/all-MiniLM-L6-v2', // 384 dimensions
|
|
71
|
+
useGPU: true,
|
|
72
|
+
batchSize: 32,
|
|
73
|
+
cacheSize: 10000
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
// Store a reasoning pattern
|
|
77
|
+
const patternId = await agentdb.reasoningBank.storePattern({
|
|
78
|
+
taskType: 'debugging',
|
|
79
|
+
approach: 'Start with logs, reproduce issue, binary search for root cause',
|
|
80
|
+
successRate: 0.92,
|
|
81
|
+
tags: ['systematic', 'efficient']
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
// Search for similar patterns
|
|
85
|
+
const patterns = await agentdb.reasoningBank.searchPatterns(
|
|
86
|
+
'how to debug memory leaks',
|
|
87
|
+
{ k: 5, threshold: 0.7 }
|
|
88
|
+
);
|
|
89
|
+
|
|
90
|
+
patterns.forEach(p => {
|
|
91
|
+
console.log(`${p.approach} (${(p.similarity * 100).toFixed(1)}% match)`);
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Reflexion Memory (Self-Improvement)
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
// Store an episode with self-critique
|
|
99
|
+
const episodeId = await agentdb.reflexionMemory.storeEpisode({
|
|
100
|
+
sessionId: 'debug-session-1',
|
|
101
|
+
task: 'Fix authentication bug',
|
|
102
|
+
reward: 0.95,
|
|
103
|
+
success: true,
|
|
104
|
+
input: 'Users cannot log in',
|
|
105
|
+
output: 'Fixed JWT token validation',
|
|
106
|
+
critique: 'Should have checked token expiration first. Worked well.',
|
|
107
|
+
latencyMs: 1200,
|
|
108
|
+
tokensUsed: 450
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
// Learn from past experiences
|
|
112
|
+
const similar = await agentdb.reflexionMemory.retrieveRelevant(
|
|
113
|
+
'authentication issues',
|
|
114
|
+
{ k: 5, onlySuccesses: true, minReward: 0.8 }
|
|
115
|
+
);
|
|
116
|
+
|
|
117
|
+
// Get critique summary
|
|
118
|
+
const critiques = await agentdb.reflexionMemory.getCritiqueSummary(
|
|
119
|
+
'authentication debugging',
|
|
120
|
+
10
|
|
121
|
+
);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### Batch Operations (3-4x Faster)
|
|
125
|
+
|
|
126
|
+
```typescript
|
|
127
|
+
// Store multiple patterns efficiently
|
|
128
|
+
const patterns = [
|
|
129
|
+
{ taskType: 'debugging', approach: 'Approach 1', successRate: 0.9 },
|
|
130
|
+
{ taskType: 'testing', approach: 'Approach 2', successRate: 0.85 },
|
|
131
|
+
{ taskType: 'optimization', approach: 'Approach 3', successRate: 0.92 }
|
|
132
|
+
];
|
|
133
|
+
|
|
134
|
+
const ids = await agentdb.reasoningBank.storePatternsBatch(patterns);
|
|
135
|
+
// 3-4x faster than storing individually
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
---
|
|
139
|
+
|
|
140
|
+
## ๐ฏ Available Models
|
|
141
|
+
|
|
142
|
+
| Model | Dimensions | Speed | Quality | Use Case |
|
|
143
|
+
|-------|------------|-------|---------|----------|
|
|
144
|
+
| `Xenova/all-MiniLM-L6-v2` | 384 | โกโกโก | โญโญโญ | **Recommended** - Best balance |
|
|
145
|
+
| `Xenova/all-MiniLM-L12-v2` | 384 | โกโก | โญโญโญโญ | Higher quality |
|
|
146
|
+
| `Xenova/bge-small-en-v1.5` | 384 | โกโกโก | โญโญโญโญ | Better accuracy |
|
|
147
|
+
| `Xenova/bge-base-en-v1.5` | 768 | โกโก | โญโญโญโญโญ | Highest quality |
|
|
148
|
+
| `Xenova/e5-small-v2` | 384 | โกโกโก | โญโญโญ | E5 series |
|
|
149
|
+
| `Xenova/e5-base-v2` | 768 | โกโก | โญโญโญโญ | E5 series |
|
|
150
|
+
|
|
151
|
+
---
|
|
152
|
+
|
|
153
|
+
## ๐ Performance
|
|
154
|
+
|
|
155
|
+
### Benchmarks (M1 Pro CPU)
|
|
156
|
+
|
|
157
|
+
| Operation | Throughput | Latency (p50) | Latency (p95) |
|
|
158
|
+
|-----------|------------|---------------|---------------|
|
|
159
|
+
| Single embedding | 45 ops/sec | 22ms | 45ms |
|
|
160
|
+
| Cached embedding | 5000+ ops/sec | <1ms | 2ms |
|
|
161
|
+
| Batch (10 items) | 120 ops/sec | 83ms | 150ms |
|
|
162
|
+
| Pattern storage | 85 ops/sec | 12ms | 28ms |
|
|
163
|
+
| Pattern search | 110 ops/sec | 9ms | 22ms |
|
|
164
|
+
| Episode storage | 90 ops/sec | 11ms | 25ms |
|
|
165
|
+
|
|
166
|
+
**Cache performance:**
|
|
167
|
+
- Hit rate: 80-95% for common queries
|
|
168
|
+
- Speedup: 100-200x for cached access
|
|
169
|
+
|
|
170
|
+
---
|
|
171
|
+
|
|
172
|
+
## ๐ ๏ธ CLI Usage
|
|
173
|
+
|
|
174
|
+
```bash
|
|
175
|
+
# Initialize database
|
|
176
|
+
npx agentdb-onnx init ./my-memory.db --model Xenova/all-MiniLM-L6-v2 --gpu
|
|
177
|
+
|
|
178
|
+
# Store pattern
|
|
179
|
+
npx agentdb-onnx store-pattern ./my-memory.db \
|
|
180
|
+
--task-type debugging \
|
|
181
|
+
--approach "Check logs first" \
|
|
182
|
+
--success-rate 0.92 \
|
|
183
|
+
--tags "systematic,efficient"
|
|
184
|
+
|
|
185
|
+
# Search patterns
|
|
186
|
+
npx agentdb-onnx search-patterns ./my-memory.db "how to debug" \
|
|
187
|
+
--top-k 5 \
|
|
188
|
+
--threshold 0.7
|
|
189
|
+
|
|
190
|
+
# Store episode
|
|
191
|
+
npx agentdb-onnx store-episode ./my-memory.db \
|
|
192
|
+
--session debug-1 \
|
|
193
|
+
--task "Fix bug" \
|
|
194
|
+
--reward 0.95 \
|
|
195
|
+
--success \
|
|
196
|
+
--critique "Worked well"
|
|
197
|
+
|
|
198
|
+
# Search episodes
|
|
199
|
+
npx agentdb-onnx search-episodes ./my-memory.db "debugging" \
|
|
200
|
+
--top-k 5 \
|
|
201
|
+
--only-successes
|
|
202
|
+
|
|
203
|
+
# Get statistics
|
|
204
|
+
npx agentdb-onnx stats ./my-memory.db
|
|
205
|
+
|
|
206
|
+
# Run benchmarks
|
|
207
|
+
npx agentdb-onnx benchmark
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
---
|
|
211
|
+
|
|
212
|
+
## ๐งช Testing
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
# Run tests
|
|
216
|
+
npm test
|
|
217
|
+
|
|
218
|
+
# Run specific test file
|
|
219
|
+
npm test onnx-embedding.test.ts
|
|
220
|
+
|
|
221
|
+
# Run with coverage
|
|
222
|
+
npm test -- --coverage
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
---
|
|
226
|
+
|
|
227
|
+
## ๐ Optimization Tips
|
|
228
|
+
|
|
229
|
+
### 1. **Enable GPU Acceleration**
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
const agentdb = await createONNXAgentDB({
|
|
233
|
+
dbPath: './db.db',
|
|
234
|
+
useGPU: true // Requires onnxruntime-node-gpu
|
|
235
|
+
});
|
|
236
|
+
```
|
|
237
|
+
|
|
238
|
+
### 2. **Increase Batch Size**
|
|
239
|
+
|
|
240
|
+
```typescript
|
|
241
|
+
const agentdb = await createONNXAgentDB({
|
|
242
|
+
dbPath: './db.db',
|
|
243
|
+
batchSize: 64 // Higher for GPU, lower for CPU
|
|
244
|
+
});
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### 3. **Warm Up Model**
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
await agentdb.embedder.warmup(10); // Pre-JIT compile
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
### 4. **Increase Cache Size**
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
const agentdb = await createONNXAgentDB({
|
|
257
|
+
dbPath: './db.db',
|
|
258
|
+
cacheSize: 50000 // More memory, better hit rate
|
|
259
|
+
});
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### 5. **Use Batch Operations**
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
// โ
Good - batch insert
|
|
266
|
+
await agentdb.reasoningBank.storePatternsBatch(patterns);
|
|
267
|
+
|
|
268
|
+
// โ Slow - sequential inserts
|
|
269
|
+
for (const p of patterns) {
|
|
270
|
+
await agentdb.reasoningBank.storePattern(p);
|
|
271
|
+
}
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
---
|
|
275
|
+
|
|
276
|
+
## ๐ Examples
|
|
277
|
+
|
|
278
|
+
### Complete Workflow
|
|
279
|
+
|
|
280
|
+
```bash
|
|
281
|
+
npm run example
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
See [`examples/complete-workflow.ts`](examples/complete-workflow.ts) for a comprehensive demo including:
|
|
285
|
+
- Pattern storage and retrieval
|
|
286
|
+
- Episode-based learning
|
|
287
|
+
- Batch operations
|
|
288
|
+
- Performance optimization
|
|
289
|
+
- Real-world agent simulation
|
|
290
|
+
|
|
291
|
+
### Key Patterns
|
|
292
|
+
|
|
293
|
+
**1. Learn from Experience:**
|
|
294
|
+
```typescript
|
|
295
|
+
// Store successful approach
|
|
296
|
+
await agentdb.reflexionMemory.storeEpisode({
|
|
297
|
+
task: 'Optimize API',
|
|
298
|
+
reward: 0.95,
|
|
299
|
+
success: true,
|
|
300
|
+
critique: 'Database indexes were key'
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// Later: retrieve when facing similar task
|
|
304
|
+
const experiences = await agentdb.reflexionMemory.retrieveRelevant(
|
|
305
|
+
'slow API performance',
|
|
306
|
+
{ onlySuccesses: true }
|
|
307
|
+
);
|
|
308
|
+
```
|
|
309
|
+
|
|
310
|
+
**2. Build Knowledge Base:**
|
|
311
|
+
```typescript
|
|
312
|
+
// Accumulate successful patterns
|
|
313
|
+
await agentdb.reasoningBank.storePatternsBatch([
|
|
314
|
+
{ taskType: 'debugging', approach: 'Binary search', successRate: 0.92 },
|
|
315
|
+
{ taskType: 'testing', approach: 'TDD', successRate: 0.88 }
|
|
316
|
+
]);
|
|
317
|
+
|
|
318
|
+
// Query when needed
|
|
319
|
+
const approaches = await agentdb.reasoningBank.searchPatterns(
|
|
320
|
+
'how to debug production issues',
|
|
321
|
+
{ k: 3 }
|
|
322
|
+
);
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
---
|
|
326
|
+
|
|
327
|
+
## ๐ API Reference
|
|
328
|
+
|
|
329
|
+
### `createONNXAgentDB(config)`
|
|
330
|
+
|
|
331
|
+
Creates an optimized AgentDB instance with ONNX embeddings.
|
|
332
|
+
|
|
333
|
+
**Config:**
|
|
334
|
+
- `dbPath: string` - Database file path
|
|
335
|
+
- `modelName?: string` - HuggingFace model (default: `Xenova/all-MiniLM-L6-v2`)
|
|
336
|
+
- `useGPU?: boolean` - Enable GPU (default: `true`)
|
|
337
|
+
- `batchSize?: number` - Batch size (default: `32`)
|
|
338
|
+
- `cacheSize?: number` - Cache size (default: `10000`)
|
|
339
|
+
|
|
340
|
+
**Returns:**
|
|
341
|
+
- `db` - Database instance
|
|
342
|
+
- `embedder` - ONNX embedding service
|
|
343
|
+
- `reasoningBank` - Pattern storage controller
|
|
344
|
+
- `reflexionMemory` - Episodic memory controller
|
|
345
|
+
- `close()` - Cleanup function
|
|
346
|
+
- `getStats()` - Performance statistics
|
|
347
|
+
|
|
348
|
+
### `ONNXEmbeddingService`
|
|
349
|
+
|
|
350
|
+
High-performance embedding generation.
|
|
351
|
+
|
|
352
|
+
**Methods:**
|
|
353
|
+
- `embed(text)` - Generate single embedding
|
|
354
|
+
- `embedBatch(texts)` - Generate multiple embeddings
|
|
355
|
+
- `warmup(samples)` - Pre-warm the model
|
|
356
|
+
- `getStats()` - Get performance metrics
|
|
357
|
+
- `clearCache()` - Clear embedding cache
|
|
358
|
+
- `getDimension()` - Get embedding dimension
|
|
359
|
+
|
|
360
|
+
### `ONNXReasoningBank`
|
|
361
|
+
|
|
362
|
+
Store and retrieve successful reasoning patterns.
|
|
363
|
+
|
|
364
|
+
**Methods:**
|
|
365
|
+
- `storePattern(pattern)` - Store pattern
|
|
366
|
+
- `storePatternsBatch(patterns)` - Batch store (3-4x faster)
|
|
367
|
+
- `searchPatterns(query, options)` - Semantic search
|
|
368
|
+
- `getPattern(id)` - Get by ID
|
|
369
|
+
- `updatePattern(id, updates)` - Update pattern
|
|
370
|
+
- `deletePattern(id)` - Delete pattern
|
|
371
|
+
- `getStats()` - Get statistics
|
|
372
|
+
|
|
373
|
+
### `ONNXReflexionMemory`
|
|
374
|
+
|
|
375
|
+
Episodic memory with self-critique for continuous improvement.
|
|
376
|
+
|
|
377
|
+
**Methods:**
|
|
378
|
+
- `storeEpisode(episode)` - Store episode
|
|
379
|
+
- `storeEpisodesBatch(episodes)` - Batch store (3-4x faster)
|
|
380
|
+
- `retrieveRelevant(task, options)` - Search similar episodes
|
|
381
|
+
- `getCritiqueSummary(task, k)` - Get critique summary
|
|
382
|
+
- `getEpisode(id)` - Get by ID
|
|
383
|
+
- `deleteEpisode(id)` - Delete episode
|
|
384
|
+
- `getTaskStats(sessionId?)` - Get statistics
|
|
385
|
+
|
|
386
|
+
---
|
|
387
|
+
|
|
388
|
+
## ๐ค Contributing
|
|
389
|
+
|
|
390
|
+
Contributions welcome! See [CONTRIBUTING.md](../../CONTRIBUTING.md)
|
|
391
|
+
|
|
392
|
+
---
|
|
393
|
+
|
|
394
|
+
## ๐ License
|
|
395
|
+
|
|
396
|
+
MIT
|
|
397
|
+
|
|
398
|
+
---
|
|
399
|
+
|
|
400
|
+
## ๐ Acknowledgments
|
|
401
|
+
|
|
402
|
+
- **AgentDB** - Vector database for AI agents
|
|
403
|
+
- **ONNX Runtime** - High-performance inference
|
|
404
|
+
- **Transformers.js** - Browser-compatible ML models
|
|
405
|
+
- **Xenova** - Optimized HuggingFace model conversions
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
## ๐ Links
|
|
410
|
+
|
|
411
|
+
- [AgentDB](https://github.com/ruvnet/agentic-flow/tree/main/packages/agentdb)
|
|
412
|
+
- [ONNX Runtime](https://onnxruntime.ai/)
|
|
413
|
+
- [Transformers.js](https://huggingface.co/docs/transformers.js)
|
|
414
|
+
- [HuggingFace Models](https://huggingface.co/models)
|
|
415
|
+
|
|
416
|
+
---
|
|
417
|
+
|
|
418
|
+
**Built with โค๏ธ for the agentic era**
|
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
#!/usr/bin/env tsx
|
|
2
|
+
/**
|
|
3
|
+
* Complete Workflow Example: AgentDB + ONNX
|
|
4
|
+
*
|
|
5
|
+
* Demonstrates:
|
|
6
|
+
* - Pattern storage and retrieval
|
|
7
|
+
* - Episodic memory with self-critique
|
|
8
|
+
* - Batch operations
|
|
9
|
+
* - Performance optimization
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { createONNXAgentDB } from '../src/index.js';
|
|
13
|
+
import { unlink } from 'fs/promises';
|
|
14
|
+
|
|
15
|
+
async function main() {
|
|
16
|
+
console.log('๐ AgentDB + ONNX Complete Workflow\n');
|
|
17
|
+
|
|
18
|
+
// Step 1: Initialize
|
|
19
|
+
console.log('1๏ธโฃ Initializing AgentDB with ONNX embeddings...');
|
|
20
|
+
const agentdb = await createONNXAgentDB({
|
|
21
|
+
dbPath: './example-workflow.db',
|
|
22
|
+
modelName: 'Xenova/all-MiniLM-L6-v2',
|
|
23
|
+
useGPU: false, // Set to true for GPU acceleration
|
|
24
|
+
batchSize: 32,
|
|
25
|
+
cacheSize: 10000
|
|
26
|
+
});
|
|
27
|
+
console.log('โ
Initialized\n');
|
|
28
|
+
|
|
29
|
+
// Step 2: Store reasoning patterns
|
|
30
|
+
console.log('2๏ธโฃ Storing reasoning patterns...');
|
|
31
|
+
|
|
32
|
+
const patterns = [
|
|
33
|
+
{
|
|
34
|
+
taskType: 'debugging',
|
|
35
|
+
approach: 'Start with logs, reproduce the issue, then binary search for the root cause',
|
|
36
|
+
successRate: 0.92,
|
|
37
|
+
tags: ['systematic', 'efficient'],
|
|
38
|
+
domain: 'software-engineering'
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
taskType: 'debugging',
|
|
42
|
+
approach: 'Use debugger breakpoints to step through execution',
|
|
43
|
+
successRate: 0.85,
|
|
44
|
+
tags: ['interactive', 'detailed'],
|
|
45
|
+
domain: 'software-engineering'
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
taskType: 'optimization',
|
|
49
|
+
approach: 'Profile first, identify bottlenecks, then optimize hot paths',
|
|
50
|
+
successRate: 0.95,
|
|
51
|
+
tags: ['data-driven', 'methodical'],
|
|
52
|
+
domain: 'performance'
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
taskType: 'api-design',
|
|
56
|
+
approach: 'RESTful principles with versioning and clear documentation',
|
|
57
|
+
successRate: 0.88,
|
|
58
|
+
tags: ['standards', 'maintainable'],
|
|
59
|
+
domain: 'architecture'
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
taskType: 'testing',
|
|
63
|
+
approach: 'Write unit tests first (TDD), then integration tests',
|
|
64
|
+
successRate: 0.90,
|
|
65
|
+
tags: ['test-driven', 'reliable'],
|
|
66
|
+
domain: 'quality-assurance'
|
|
67
|
+
}
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
// Batch store for efficiency
|
|
71
|
+
const patternIds = await agentdb.reasoningBank.storePatternsBatch(patterns);
|
|
72
|
+
console.log(`โ
Stored ${patternIds.length} patterns\n`);
|
|
73
|
+
|
|
74
|
+
// Step 3: Search for patterns
|
|
75
|
+
console.log('3๏ธโฃ Searching for debugging strategies...');
|
|
76
|
+
|
|
77
|
+
const debugPatterns = await agentdb.reasoningBank.searchPatterns(
|
|
78
|
+
'how to debug a memory leak in production',
|
|
79
|
+
{
|
|
80
|
+
k: 3,
|
|
81
|
+
threshold: 0.5,
|
|
82
|
+
filters: {
|
|
83
|
+
taskType: 'debugging',
|
|
84
|
+
minSuccessRate: 0.8
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
|
|
89
|
+
console.log(`Found ${debugPatterns.length} relevant patterns:`);
|
|
90
|
+
debugPatterns.forEach((p, i) => {
|
|
91
|
+
console.log(`\n ${i + 1}. ${p.approach}`);
|
|
92
|
+
console.log(` Success rate: ${(p.successRate * 100).toFixed(1)}%`);
|
|
93
|
+
console.log(` Similarity: ${(p.similarity * 100).toFixed(1)}%`);
|
|
94
|
+
console.log(` Tags: ${p.tags?.join(', ')}`);
|
|
95
|
+
});
|
|
96
|
+
console.log();
|
|
97
|
+
|
|
98
|
+
// Step 4: Store episodes with self-critique
|
|
99
|
+
console.log('4๏ธโฃ Storing reflexion episodes...');
|
|
100
|
+
|
|
101
|
+
const episodes = [
|
|
102
|
+
{
|
|
103
|
+
sessionId: 'debug-session-1',
|
|
104
|
+
task: 'Fix memory leak in Node.js application',
|
|
105
|
+
reward: 0.95,
|
|
106
|
+
success: true,
|
|
107
|
+
input: 'Application consuming 2GB+ memory',
|
|
108
|
+
output: 'Found event listener leak, fixed with proper cleanup',
|
|
109
|
+
critique: 'Should have checked event listeners earlier. Profiling was key.',
|
|
110
|
+
latencyMs: 1800,
|
|
111
|
+
tokensUsed: 450
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
sessionId: 'debug-session-1',
|
|
115
|
+
task: 'Reproduce memory leak locally',
|
|
116
|
+
reward: 0.88,
|
|
117
|
+
success: true,
|
|
118
|
+
input: 'Production memory leak',
|
|
119
|
+
output: 'Reproduced with stress test script',
|
|
120
|
+
critique: 'Reproduction helped a lot. Could have added heap snapshots.',
|
|
121
|
+
latencyMs: 600,
|
|
122
|
+
tokensUsed: 200
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
sessionId: 'optimize-session-1',
|
|
126
|
+
task: 'Optimize API response time',
|
|
127
|
+
reward: 0.92,
|
|
128
|
+
success: true,
|
|
129
|
+
input: 'API taking 2-3 seconds per request',
|
|
130
|
+
output: 'Added database indexes, reduced to 200ms',
|
|
131
|
+
critique: 'Profiling showed N+1 queries. Database indexes solved it.',
|
|
132
|
+
latencyMs: 1200,
|
|
133
|
+
tokensUsed: 350
|
|
134
|
+
},
|
|
135
|
+
{
|
|
136
|
+
sessionId: 'test-session-1',
|
|
137
|
+
task: 'Write tests for authentication',
|
|
138
|
+
reward: 0.85,
|
|
139
|
+
success: true,
|
|
140
|
+
input: 'JWT authentication module',
|
|
141
|
+
output: 'Unit tests with 95% coverage',
|
|
142
|
+
critique: 'TDD approach worked well. Could add more edge cases.',
|
|
143
|
+
latencyMs: 900,
|
|
144
|
+
tokensUsed: 300
|
|
145
|
+
}
|
|
146
|
+
];
|
|
147
|
+
|
|
148
|
+
const episodeIds = await agentdb.reflexionMemory.storeEpisodesBatch(episodes);
|
|
149
|
+
console.log(`โ
Stored ${episodeIds.length} episodes\n`);
|
|
150
|
+
|
|
151
|
+
// Step 5: Learn from past experiences
|
|
152
|
+
console.log('5๏ธโฃ Learning from past experiences...');
|
|
153
|
+
|
|
154
|
+
const similarExperiences = await agentdb.reflexionMemory.retrieveRelevant(
|
|
155
|
+
'how to fix performance issues in production',
|
|
156
|
+
{
|
|
157
|
+
k: 3,
|
|
158
|
+
onlySuccesses: true,
|
|
159
|
+
minReward: 0.85
|
|
160
|
+
}
|
|
161
|
+
);
|
|
162
|
+
|
|
163
|
+
console.log(`Found ${similarExperiences.length} relevant experiences:`);
|
|
164
|
+
similarExperiences.forEach((e, i) => {
|
|
165
|
+
console.log(`\n ${i + 1}. ${e.task}`);
|
|
166
|
+
console.log(` Reward: ${(e.reward * 100).toFixed(1)}%`);
|
|
167
|
+
console.log(` Similarity: ${(e.similarity * 100).toFixed(1)}%`);
|
|
168
|
+
console.log(` Critique: ${e.critique?.substring(0, 80)}...`);
|
|
169
|
+
});
|
|
170
|
+
console.log();
|
|
171
|
+
|
|
172
|
+
// Step 6: Get critique summary
|
|
173
|
+
console.log('6๏ธโฃ Getting critique summary for debugging tasks...');
|
|
174
|
+
|
|
175
|
+
const critiques = await agentdb.reflexionMemory.getCritiqueSummary(
|
|
176
|
+
'debugging memory issues',
|
|
177
|
+
5
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
console.log(`Learned ${critiques.length} insights from past debugging:`);
|
|
181
|
+
critiques.forEach((c, i) => {
|
|
182
|
+
console.log(` ${i + 1}. ${c}`);
|
|
183
|
+
});
|
|
184
|
+
console.log();
|
|
185
|
+
|
|
186
|
+
// Step 7: Performance statistics
|
|
187
|
+
console.log('7๏ธโฃ Performance statistics:');
|
|
188
|
+
|
|
189
|
+
const stats = agentdb.getStats();
|
|
190
|
+
|
|
191
|
+
console.log(`\n ๐ Embeddings:`);
|
|
192
|
+
console.log(` Total: ${stats.embedder.totalEmbeddings}`);
|
|
193
|
+
console.log(` Avg latency: ${stats.embedder.avgLatency.toFixed(2)}ms`);
|
|
194
|
+
console.log(` Cache hits: ${(stats.embedder.cache.hitRate * 100).toFixed(1)}%`);
|
|
195
|
+
console.log(` Cache size: ${stats.embedder.cache.size}/${stats.embedder.cache.maxSize}`);
|
|
196
|
+
|
|
197
|
+
console.log(`\n ๐พ Database:`);
|
|
198
|
+
console.log(` Backend: ${stats.database?.backend || 'Unknown'}`);
|
|
199
|
+
|
|
200
|
+
// Step 8: Real-world scenario simulation
|
|
201
|
+
console.log('\n8๏ธโฃ Simulating real-world agent workflow...\n');
|
|
202
|
+
|
|
203
|
+
// Scenario: Agent needs to solve a new task
|
|
204
|
+
const newTask = 'Optimize database query performance in production API';
|
|
205
|
+
|
|
206
|
+
console.log(` ๐ New task: "${newTask}"\n`);
|
|
207
|
+
|
|
208
|
+
// Step 8a: Retrieve relevant patterns
|
|
209
|
+
console.log(' ๐ Searching for relevant patterns...');
|
|
210
|
+
const relevantPatterns = await agentdb.reasoningBank.searchPatterns(
|
|
211
|
+
newTask,
|
|
212
|
+
{ k: 2, threshold: 0.6 }
|
|
213
|
+
);
|
|
214
|
+
|
|
215
|
+
console.log(` Found ${relevantPatterns.length} patterns:`);
|
|
216
|
+
relevantPatterns.forEach((p, i) => {
|
|
217
|
+
console.log(` ${i + 1}. ${p.approach} (${(p.similarity * 100).toFixed(0)}% match)`);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
// Step 8b: Retrieve similar episodes
|
|
221
|
+
console.log('\n ๐ Searching for similar past experiences...');
|
|
222
|
+
const relevantEpisodes = await agentdb.reflexionMemory.retrieveRelevant(
|
|
223
|
+
newTask,
|
|
224
|
+
{ k: 2, onlySuccesses: true }
|
|
225
|
+
);
|
|
226
|
+
|
|
227
|
+
console.log(` Found ${relevantEpisodes.length} experiences:`);
|
|
228
|
+
relevantEpisodes.forEach((e, i) => {
|
|
229
|
+
console.log(` ${i + 1}. ${e.task} (${(e.similarity * 100).toFixed(0)}% match)`);
|
|
230
|
+
console.log(` Critique: ${e.critique?.substring(0, 60)}...`);
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
// Step 8c: Agent executes task with learned approach
|
|
234
|
+
console.log('\n โก Agent executes with learned approach...');
|
|
235
|
+
console.log(' โ
Task completed successfully!\n');
|
|
236
|
+
|
|
237
|
+
// Step 8d: Store new episode with self-critique
|
|
238
|
+
console.log(' ๐ญ Agent reflects on execution...');
|
|
239
|
+
const newEpisodeId = await agentdb.reflexionMemory.storeEpisode({
|
|
240
|
+
sessionId: 'new-session',
|
|
241
|
+
task: newTask,
|
|
242
|
+
reward: 0.94,
|
|
243
|
+
success: true,
|
|
244
|
+
input: 'Slow database queries in production',
|
|
245
|
+
output: 'Added composite indexes, query time reduced by 10x',
|
|
246
|
+
critique: 'Profiling showed table scans. Learned from past episode about indexing. Very effective approach.',
|
|
247
|
+
latencyMs: 1500,
|
|
248
|
+
tokensUsed: 400
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
console.log(` โ
Stored new episode (ID: ${newEpisodeId})\n`);
|
|
252
|
+
|
|
253
|
+
// Step 9: Show improvement over time
|
|
254
|
+
console.log('9๏ธโฃ Self-improvement demonstration:\n');
|
|
255
|
+
|
|
256
|
+
const taskStats = await agentdb.reflexionMemory.getTaskStats();
|
|
257
|
+
console.log(` Total episodes: ${taskStats.totalEpisodes}`);
|
|
258
|
+
console.log(` Success rate: ${(taskStats.successRate * 100).toFixed(1)}%`);
|
|
259
|
+
console.log(` Avg reward: ${(taskStats.avgReward * 100).toFixed(1)}%`);
|
|
260
|
+
console.log(` Avg latency: ${taskStats.avgLatency.toFixed(0)}ms`);
|
|
261
|
+
|
|
262
|
+
// Cleanup
|
|
263
|
+
console.log('\n๐งน Cleaning up...');
|
|
264
|
+
await agentdb.close();
|
|
265
|
+
|
|
266
|
+
try {
|
|
267
|
+
await unlink('./example-workflow.db');
|
|
268
|
+
} catch {}
|
|
269
|
+
|
|
270
|
+
console.log('โ
Complete!\n');
|
|
271
|
+
console.log('โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ');
|
|
272
|
+
console.log('Key Takeaways:');
|
|
273
|
+
console.log(' โข ONNX embeddings provide fast, local semantic search');
|
|
274
|
+
console.log(' โข Batch operations are 3-4x faster than sequential');
|
|
275
|
+
console.log(' โข ReasoningBank stores successful patterns');
|
|
276
|
+
console.log(' โข ReflexionMemory enables self-improvement');
|
|
277
|
+
console.log(' โข Cache provides significant speedup for common queries');
|
|
278
|
+
console.log('โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ\n');
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
main().catch(console.error);
|