@msbayindir/context-rag 1.0.0-beta.4 β†’ 1.0.0-beta.6

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 CHANGED
@@ -14,10 +14,11 @@
14
14
  |---------|-------------|
15
15
  | πŸš€ **Gemini Files API** | Upload PDF once, use cached URI for entire pipeline (90%+ bandwidth savings) |
16
16
  | 🧠 **Contextual Retrieval** | Anthropic-style context generation for each chunk (improves recall by ~49%) |
17
+ | 🎯 **Reranking** | Gemini or Cohere-powered relevance reranking (reduces retrieval failure by ~67%) |
17
18
  | πŸ” **Discovery Agent** | AI automatically analyzes documents and suggests optimal chunking strategies |
18
19
  | πŸ“„ **Multimodal Processing** | Uses Gemini Vision API to understand tables, charts, and layouts |
19
20
  | πŸ§ͺ **Experiment System** | A/B test different models on same document for comparison |
20
- | 🎯 **Hybrid Search** | Semantic (vector) + Keyword (full-text) search combination |
21
+ | πŸ”Ž **Hybrid Search** | Semantic (vector) + Keyword (full-text) search combination |
21
22
  | 🐘 **PostgreSQL Native** | No external vector DB needed, uses pgvector |
22
23
  | ⚑ **Batch Processing** | Concurrent processing with automatic retry |
23
24
 
@@ -186,6 +187,9 @@ npx prisma migrate dev --name add-context-rag
186
187
  ```env
187
188
  DATABASE_URL="postgresql://user:password@localhost:5432/mydb"
188
189
  GEMINI_API_KEY="your-gemini-api-key"
190
+
191
+ # Optional: Cohere API key for better reranking (free tier: 10K/month)
192
+ COHERE_API_KEY="your-cohere-api-key"
189
193
  ```
190
194
 
191
195
  ---
@@ -276,6 +280,109 @@ const rag = new ContextRAG({
276
280
 
277
281
  ---
278
282
 
283
+ ## 🎯 Reranking
284
+
285
+ Reranking improves search relevance by re-scoring candidates using AI models. Based on [Anthropic's Contextual Retrieval](https://www.anthropic.com/engineering/contextual-retrieval) research, it reduces retrieval failure rate by ~67%.
286
+
287
+ ### How It Works
288
+
289
+ 1. **Initial Retrieval:** Get top N candidates (e.g., 50) via vector similarity
290
+ 2. **Reranking:** AI model scores each candidate's relevance to the query
291
+ 3. **Final Selection:** Return top K (e.g., 5) based on reranked scores
292
+
293
+ ### Configuration
294
+
295
+ ```typescript
296
+ const rag = new ContextRAG({
297
+ prisma,
298
+ geminiApiKey: process.env.GEMINI_API_KEY!,
299
+
300
+ // Reranking configuration
301
+ rerankingConfig: {
302
+ enabled: true,
303
+ provider: 'gemini', // 'gemini' (free) or 'cohere' (10K/month free)
304
+ cohereApiKey: process.env.COHERE_API_KEY, // Required if provider is 'cohere'
305
+ defaultCandidates: 50, // Get 50 candidates from vector search
306
+ defaultTopK: 10, // Return top 10 after reranking
307
+ },
308
+ });
309
+ ```
310
+
311
+ ### Per-Query Reranking
312
+
313
+ ```typescript
314
+ const results = await rag.search({
315
+ query: 'metabolizma ve enerji ΓΌretimi',
316
+ limit: 5,
317
+ useReranking: true, // Enable reranking for this query
318
+ rerankCandidates: 50, // Get 50 candidates, rerank to top 5
319
+ });
320
+
321
+ // Results include reranking metadata
322
+ results.forEach(r => {
323
+ console.log(`Score: ${r.score}`);
324
+ console.log(`Reranked: ${r.explanation?.reranked}`);
325
+ console.log(`Original rank: ${r.explanation?.originalRank}`);
326
+ });
327
+ ```
328
+
329
+ ### Provider Comparison
330
+
331
+ | Provider | Cost | Quality | Best For |
332
+ |----------|------|---------|----------|
333
+ | **Gemini** | Free (uses existing quota) | Good | Cost-sensitive, general use |
334
+ | **Cohere** | Free tier: 10K/month | Excellent | Multilingual, production |
335
+
336
+ ## 🎯 Custom Prompt / Filtered Extraction
337
+
338
+ Extract only specific content types without going through the Discovery flow:
339
+
340
+ ```typescript
341
+ // Extract ONLY specific types with custom prompt
342
+ const result = await rag.ingest({
343
+ file: './book.pdf',
344
+ customPrompt: `
345
+ Extract ONLY these content types:
346
+ - TEXT: Normal paragraphs
347
+ - QUESTION: Multiple choice questions
348
+ - LIST: Bulleted or numbered lists
349
+ - TABLE: Data tables
350
+
351
+ SKIP these types:
352
+ - HEADING, CODE, QUOTE, IMAGE_REF
353
+ `,
354
+ // Context enrichment only for TEXT chunks (cost optimization)
355
+ // Configure via ragEnhancement.skipChunkTypes
356
+ });
357
+ ```
358
+
359
+ ### Configuration for Selective Context Enrichment
360
+
361
+ ```typescript
362
+ const rag = new ContextRAG({
363
+ prisma,
364
+ geminiApiKey: process.env.GEMINI_API_KEY!,
365
+
366
+ ragEnhancement: {
367
+ approach: 'anthropic_contextual',
368
+ strategy: 'llm',
369
+ // Only TEXT chunks get context enrichment
370
+ // Other types (TABLE, LIST, QUESTION) are extracted but not enriched
371
+ skipChunkTypes: ['HEADING', 'IMAGE_REF', 'TABLE', 'CODE', 'QUOTE', 'MIXED', 'QUESTION', 'LIST'],
372
+ },
373
+ });
374
+
375
+ // PromptConfig is auto-created when using customPrompt
376
+ await rag.ingest({
377
+ file: './document.pdf',
378
+ customPrompt: 'Your custom extraction instructions...',
379
+ });
380
+ ```
381
+
382
+ > **Note:** When using `customPrompt` without `promptConfigId`, the system automatically creates a PromptConfig for you.
383
+
384
+ ---
385
+
279
386
  ## βš™οΈ Configuration
280
387
 
281
388
  ```typescript