@hazeljs/rag 0.2.0-beta.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/README.md +379 -0
- package/dist/agentic/agentic-rag.service.d.ts +49 -0
- package/dist/agentic/agentic-rag.service.d.ts.map +1 -0
- package/dist/agentic/agentic-rag.service.js +149 -0
- package/dist/agentic/agentic-rag.service.js.map +1 -0
- package/dist/agentic/decorators/active-learning.decorator.d.ts +19 -0
- package/dist/agentic/decorators/active-learning.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/active-learning.decorator.js +98 -0
- package/dist/agentic/decorators/active-learning.decorator.js.map +1 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts +16 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.js +114 -0
- package/dist/agentic/decorators/adaptive-retrieval.decorator.js.map +1 -0
- package/dist/agentic/decorators/cached.decorator.d.ts +18 -0
- package/dist/agentic/decorators/cached.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/cached.decorator.js +93 -0
- package/dist/agentic/decorators/cached.decorator.js.map +1 -0
- package/dist/agentic/decorators/context-aware.decorator.d.ts +16 -0
- package/dist/agentic/decorators/context-aware.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/context-aware.decorator.js +169 -0
- package/dist/agentic/decorators/context-aware.decorator.js.map +1 -0
- package/dist/agentic/decorators/corrective-rag.decorator.d.ts +15 -0
- package/dist/agentic/decorators/corrective-rag.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/corrective-rag.decorator.js +146 -0
- package/dist/agentic/decorators/corrective-rag.decorator.js.map +1 -0
- package/dist/agentic/decorators/hyde.decorator.d.ts +14 -0
- package/dist/agentic/decorators/hyde.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/hyde.decorator.js +90 -0
- package/dist/agentic/decorators/hyde.decorator.js.map +1 -0
- package/dist/agentic/decorators/index.d.ts +16 -0
- package/dist/agentic/decorators/index.d.ts.map +1 -0
- package/dist/agentic/decorators/index.js +32 -0
- package/dist/agentic/decorators/index.js.map +1 -0
- package/dist/agentic/decorators/multi-hop.decorator.d.ts +14 -0
- package/dist/agentic/decorators/multi-hop.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/multi-hop.decorator.js +120 -0
- package/dist/agentic/decorators/multi-hop.decorator.js.map +1 -0
- package/dist/agentic/decorators/query-planner.decorator.d.ts +19 -0
- package/dist/agentic/decorators/query-planner.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/query-planner.decorator.js +227 -0
- package/dist/agentic/decorators/query-planner.decorator.js.map +1 -0
- package/dist/agentic/decorators/query-rewriter.decorator.d.ts +15 -0
- package/dist/agentic/decorators/query-rewriter.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/query-rewriter.decorator.js +140 -0
- package/dist/agentic/decorators/query-rewriter.decorator.js.map +1 -0
- package/dist/agentic/decorators/self-reflective.decorator.d.ts +18 -0
- package/dist/agentic/decorators/self-reflective.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/self-reflective.decorator.js +210 -0
- package/dist/agentic/decorators/self-reflective.decorator.js.map +1 -0
- package/dist/agentic/decorators/source-verification.decorator.d.ts +15 -0
- package/dist/agentic/decorators/source-verification.decorator.d.ts.map +1 -0
- package/dist/agentic/decorators/source-verification.decorator.js +121 -0
- package/dist/agentic/decorators/source-verification.decorator.js.map +1 -0
- package/dist/agentic/index.d.ts +9 -0
- package/dist/agentic/index.d.ts.map +1 -0
- package/dist/agentic/index.js +25 -0
- package/dist/agentic/index.js.map +1 -0
- package/dist/agentic/types.d.ts +209 -0
- package/dist/agentic/types.d.ts.map +1 -0
- package/dist/agentic/types.js +7 -0
- package/dist/agentic/types.js.map +1 -0
- package/dist/decorators/embeddable.decorator.d.ts +31 -0
- package/dist/decorators/embeddable.decorator.d.ts.map +1 -0
- package/dist/decorators/embeddable.decorator.js +44 -0
- package/dist/decorators/embeddable.decorator.js.map +1 -0
- package/dist/decorators/rag.decorator.d.ts +58 -0
- package/dist/decorators/rag.decorator.d.ts.map +1 -0
- package/dist/decorators/rag.decorator.js +78 -0
- package/dist/decorators/rag.decorator.js.map +1 -0
- package/dist/decorators/semantic-search.decorator.d.ts +69 -0
- package/dist/decorators/semantic-search.decorator.d.ts.map +1 -0
- package/dist/decorators/semantic-search.decorator.js +116 -0
- package/dist/decorators/semantic-search.decorator.js.map +1 -0
- package/dist/embeddings/cohere-embeddings.d.ts +33 -0
- package/dist/embeddings/cohere-embeddings.d.ts.map +1 -0
- package/dist/embeddings/cohere-embeddings.js +91 -0
- package/dist/embeddings/cohere-embeddings.js.map +1 -0
- package/dist/embeddings/openai-embeddings.d.ts +21 -0
- package/dist/embeddings/openai-embeddings.d.ts.map +1 -0
- package/dist/embeddings/openai-embeddings.js +53 -0
- package/dist/embeddings/openai-embeddings.js.map +1 -0
- package/dist/index.d.ts +27 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +54 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/index.d.ts +11 -0
- package/dist/memory/index.d.ts.map +1 -0
- package/dist/memory/index.js +31 -0
- package/dist/memory/index.js.map +1 -0
- package/dist/memory/memory-manager.d.ts +96 -0
- package/dist/memory/memory-manager.d.ts.map +1 -0
- package/dist/memory/memory-manager.js +369 -0
- package/dist/memory/memory-manager.js.map +1 -0
- package/dist/memory/memory-store.interface.d.ts +73 -0
- package/dist/memory/memory-store.interface.d.ts.map +1 -0
- package/dist/memory/memory-store.interface.js +6 -0
- package/dist/memory/memory-store.interface.js.map +1 -0
- package/dist/memory/stores/buffer-memory.d.ts +47 -0
- package/dist/memory/stores/buffer-memory.d.ts.map +1 -0
- package/dist/memory/stores/buffer-memory.js +280 -0
- package/dist/memory/stores/buffer-memory.js.map +1 -0
- package/dist/memory/stores/hybrid-memory.d.ts +49 -0
- package/dist/memory/stores/hybrid-memory.d.ts.map +1 -0
- package/dist/memory/stores/hybrid-memory.js +194 -0
- package/dist/memory/stores/hybrid-memory.js.map +1 -0
- package/dist/memory/stores/vector-memory.d.ts +48 -0
- package/dist/memory/stores/vector-memory.d.ts.map +1 -0
- package/dist/memory/stores/vector-memory.js +312 -0
- package/dist/memory/stores/vector-memory.js.map +1 -0
- package/dist/memory/types.d.ts +119 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +18 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/rag-pipeline-with-memory.d.ts +68 -0
- package/dist/rag-pipeline-with-memory.d.ts.map +1 -0
- package/dist/rag-pipeline-with-memory.js +186 -0
- package/dist/rag-pipeline-with-memory.js.map +1 -0
- package/dist/rag-pipeline.d.ts +59 -0
- package/dist/rag-pipeline.d.ts.map +1 -0
- package/dist/rag-pipeline.js +181 -0
- package/dist/rag-pipeline.js.map +1 -0
- package/dist/rag.module.d.ts +26 -0
- package/dist/rag.module.d.ts.map +1 -0
- package/dist/rag.module.js +40 -0
- package/dist/rag.module.js.map +1 -0
- package/dist/rag.service.d.ts +95 -0
- package/dist/rag.service.d.ts.map +1 -0
- package/dist/rag.service.js +174 -0
- package/dist/rag.service.js.map +1 -0
- package/dist/retrieval/bm25.d.ts +57 -0
- package/dist/retrieval/bm25.d.ts.map +1 -0
- package/dist/retrieval/bm25.js +106 -0
- package/dist/retrieval/bm25.js.map +1 -0
- package/dist/retrieval/hybrid-search.d.ts +48 -0
- package/dist/retrieval/hybrid-search.d.ts.map +1 -0
- package/dist/retrieval/hybrid-search.js +123 -0
- package/dist/retrieval/hybrid-search.js.map +1 -0
- package/dist/retrieval/multi-query.d.ts +38 -0
- package/dist/retrieval/multi-query.d.ts.map +1 -0
- package/dist/retrieval/multi-query.js +135 -0
- package/dist/retrieval/multi-query.js.map +1 -0
- package/dist/text-splitters/recursive-text-splitter.d.ts +21 -0
- package/dist/text-splitters/recursive-text-splitter.d.ts.map +1 -0
- package/dist/text-splitters/recursive-text-splitter.js +95 -0
- package/dist/text-splitters/recursive-text-splitter.js.map +1 -0
- package/dist/types/index.d.ts +144 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +16 -0
- package/dist/types/index.js.map +1 -0
- package/dist/utils/similarity.d.ts +16 -0
- package/dist/utils/similarity.d.ts.map +1 -0
- package/dist/utils/similarity.js +58 -0
- package/dist/utils/similarity.js.map +1 -0
- package/dist/vector-stores/chroma.store.d.ts +42 -0
- package/dist/vector-stores/chroma.store.d.ts.map +1 -0
- package/dist/vector-stores/chroma.store.js +242 -0
- package/dist/vector-stores/chroma.store.js.map +1 -0
- package/dist/vector-stores/memory-vector-store.d.ts +20 -0
- package/dist/vector-stores/memory-vector-store.d.ts.map +1 -0
- package/dist/vector-stores/memory-vector-store.js +94 -0
- package/dist/vector-stores/memory-vector-store.js.map +1 -0
- package/dist/vector-stores/pinecone.store.d.ts +34 -0
- package/dist/vector-stores/pinecone.store.d.ts.map +1 -0
- package/dist/vector-stores/pinecone.store.js +146 -0
- package/dist/vector-stores/pinecone.store.js.map +1 -0
- package/dist/vector-stores/qdrant.store.d.ts +31 -0
- package/dist/vector-stores/qdrant.store.d.ts.map +1 -0
- package/dist/vector-stores/qdrant.store.js +173 -0
- package/dist/vector-stores/qdrant.store.js.map +1 -0
- package/dist/vector-stores/weaviate.store.d.ts +37 -0
- package/dist/vector-stores/weaviate.store.d.ts.map +1 -0
- package/dist/vector-stores/weaviate.store.js +226 -0
- package/dist/vector-stores/weaviate.store.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Corrective RAG (CRAG) Decorator
|
|
4
|
+
* Self-corrects retrieval errors with fallback mechanisms
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.CorrectiveRAG = CorrectiveRAG;
|
|
8
|
+
exports.getCorrectiveRAGResult = getCorrectiveRAGResult;
|
|
9
|
+
require("reflect-metadata");
|
|
10
|
+
const CRAG_METADATA_KEY = Symbol('correctiveRAG');
|
|
11
|
+
function CorrectiveRAG(config = {}) {
|
|
12
|
+
return function (target, propertyKey, descriptor) {
|
|
13
|
+
const originalMethod = descriptor.value;
|
|
14
|
+
descriptor.value = async function (...args) {
|
|
15
|
+
let results = await originalMethod.apply(this, args);
|
|
16
|
+
const query = args[0];
|
|
17
|
+
// Evaluate results
|
|
18
|
+
const evaluation = await evaluateResults(results, query, config);
|
|
19
|
+
const corrections = [];
|
|
20
|
+
let fallbackUsed = false;
|
|
21
|
+
// Apply corrections if needed
|
|
22
|
+
if (evaluation.needsCorrection) {
|
|
23
|
+
for (const issue of evaluation.issues) {
|
|
24
|
+
corrections.push({
|
|
25
|
+
type: issue.type,
|
|
26
|
+
description: issue.description,
|
|
27
|
+
action: issue.action,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// Attempt to correct
|
|
31
|
+
const corrected = await correctResults(results, query, evaluation, config);
|
|
32
|
+
if (corrected) {
|
|
33
|
+
results = corrected;
|
|
34
|
+
}
|
|
35
|
+
else if (config.fallbackToWeb) {
|
|
36
|
+
// Fallback mechanism (placeholder)
|
|
37
|
+
fallbackUsed = true;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
const cragResult = {
|
|
41
|
+
results: Array.isArray(results) ? results : [results],
|
|
42
|
+
corrections,
|
|
43
|
+
fallbackUsed,
|
|
44
|
+
confidence: evaluation.confidence,
|
|
45
|
+
};
|
|
46
|
+
Reflect.defineMetadata(CRAG_METADATA_KEY, cragResult, target, propertyKey);
|
|
47
|
+
return results;
|
|
48
|
+
};
|
|
49
|
+
return descriptor;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
async function evaluateResults(
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
+
results, query, config) {
|
|
55
|
+
const threshold = config.relevanceThreshold || 0.7;
|
|
56
|
+
const issues = [];
|
|
57
|
+
if (!results || (Array.isArray(results) && results.length === 0)) {
|
|
58
|
+
issues.push({
|
|
59
|
+
type: 'missing_info',
|
|
60
|
+
description: 'No results returned',
|
|
61
|
+
action: 'Expand query or use fallback',
|
|
62
|
+
});
|
|
63
|
+
return { needsCorrection: true, confidence: 0, issues };
|
|
64
|
+
}
|
|
65
|
+
const resultArray = Array.isArray(results) ? results : [results];
|
|
66
|
+
// Check relevance scores
|
|
67
|
+
const avgScore = resultArray.reduce((sum, r) => sum + (r.score || 0), 0) / resultArray.length;
|
|
68
|
+
if (avgScore < threshold) {
|
|
69
|
+
issues.push({
|
|
70
|
+
type: 'low_relevance',
|
|
71
|
+
description: `Average relevance score ${avgScore.toFixed(2)} below threshold ${threshold}`,
|
|
72
|
+
action: 'Rewrite query or adjust retrieval strategy',
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
// Check for contradictions
|
|
76
|
+
if (resultArray.length > 1 && config.llmProvider) {
|
|
77
|
+
const hasContradictions = await checkContradictions(resultArray, config.llmProvider);
|
|
78
|
+
if (hasContradictions) {
|
|
79
|
+
issues.push({
|
|
80
|
+
type: 'contradictory',
|
|
81
|
+
description: 'Results contain contradictory information',
|
|
82
|
+
action: 'Filter or re-rank results',
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
needsCorrection: issues.length > 0,
|
|
88
|
+
confidence: Math.max(avgScore, 0.5),
|
|
89
|
+
issues,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
async function correctResults(
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
94
|
+
results, query,
|
|
95
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
|
+
evaluation, config
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
98
|
+
) {
|
|
99
|
+
if (!config.llmProvider) {
|
|
100
|
+
return null;
|
|
101
|
+
}
|
|
102
|
+
const prompt = `The following retrieval results have issues. Suggest corrections:
|
|
103
|
+
|
|
104
|
+
Query: ${query}
|
|
105
|
+
Issues: ${evaluation.issues.map((i) => i.description).join(', ')}
|
|
106
|
+
|
|
107
|
+
Provide corrected query or strategy in JSON:
|
|
108
|
+
{
|
|
109
|
+
"correctedQuery": "improved query",
|
|
110
|
+
"strategy": "new strategy",
|
|
111
|
+
"reasoning": "why this helps"
|
|
112
|
+
}`;
|
|
113
|
+
try {
|
|
114
|
+
await config.llmProvider.generateStructured(prompt, {});
|
|
115
|
+
// Return null to signal re-query needed
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
catch {
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
async function checkContradictions(
|
|
123
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
124
|
+
results, llmProvider) {
|
|
125
|
+
if (results.length < 2)
|
|
126
|
+
return false;
|
|
127
|
+
const contents = results
|
|
128
|
+
.slice(0, 3)
|
|
129
|
+
.map((r) => r.content)
|
|
130
|
+
.join('\n\n');
|
|
131
|
+
const prompt = `Do these texts contain contradictory information? Answer yes or no.
|
|
132
|
+
|
|
133
|
+
Texts:
|
|
134
|
+
${contents.slice(0, 1000)}`;
|
|
135
|
+
try {
|
|
136
|
+
const response = await llmProvider.generate(prompt, { maxTokens: 10 });
|
|
137
|
+
return response.toLowerCase().includes('yes');
|
|
138
|
+
}
|
|
139
|
+
catch {
|
|
140
|
+
return false;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function getCorrectiveRAGResult(target, propertyKey) {
|
|
144
|
+
return Reflect.getMetadata(CRAG_METADATA_KEY, target, propertyKey);
|
|
145
|
+
}
|
|
146
|
+
//# sourceMappingURL=corrective-rag.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"corrective-rag.decorator.js","sourceRoot":"","sources":["../../../src/agentic/decorators/corrective-rag.decorator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAcH,sCAgDC;AAuHD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CAAwC;QACxC,OAAO,IAAI,CAAC;IACd,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED,KAAK,UAAU,mBAAmB;AAChC,8DAA8D;AAC9D,OAAc,EACd,WAA+B;IAE/B,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;QAAE,OAAO,KAAK,CAAC;IAErC,MAAM,QAAQ,GAAG,OAAO;SACrB,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC;SACX,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC;SACrB,IAAI,CAAC,MAAM,CAAC,CAAC;IAChB,MAAM,MAAM,GAAG;;;EAGf,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,EAAE,CAAC;IAE1B,IAAI,CAAC;QACH,MAAM,QAAQ,GAAG,MAAM,WAAW,CAAC,QAAQ,CAAC,MAAM,EAAE,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC,CAAC;QACvE,OAAO,QAAQ,CAAC,WAAW,EAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC;IAChD,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,KAAK,CAAC;IACf,CAAC;AACH,CAAC;AAED,SAAgB,sBAAsB,CACpC,MAAc,EACd,WAA4B;IAE5B,OAAO,OAAO,CAAC,WAAW,CAAC,iBAAiB,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HyDE (Hypothetical Document Embeddings) Decorator
|
|
3
|
+
* Generates hypothetical documents to improve retrieval
|
|
4
|
+
*/
|
|
5
|
+
import 'reflect-metadata';
|
|
6
|
+
import { HyDEResult, AgenticLLMProvider } from '../types';
|
|
7
|
+
export interface HyDEConfig {
|
|
8
|
+
generateHypothesis?: boolean;
|
|
9
|
+
numHypotheses?: number;
|
|
10
|
+
llmProvider?: AgenticLLMProvider;
|
|
11
|
+
}
|
|
12
|
+
export declare function HyDE(config?: HyDEConfig): MethodDecorator;
|
|
13
|
+
export declare function getHyDEResult(target: object, propertyKey: string | symbol): HyDEResult | undefined;
|
|
14
|
+
//# sourceMappingURL=hyde.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyde.decorator.d.ts","sourceRoot":"","sources":["../../../src/agentic/decorators/hyde.decorator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE1D,MAAM,WAAW,UAAU;IACzB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAID,wBAAgB,IAAI,CAAC,MAAM,GAAE,UAAe,GAAG,eAAe,CAmD7D;AAkDD,wBAAgB,aAAa,CAC3B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAAG,MAAM,GAC3B,UAAU,GAAG,SAAS,CAExB"}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* HyDE (Hypothetical Document Embeddings) Decorator
|
|
4
|
+
* Generates hypothetical documents to improve retrieval
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.HyDE = HyDE;
|
|
8
|
+
exports.getHyDEResult = getHyDEResult;
|
|
9
|
+
require("reflect-metadata");
|
|
10
|
+
const HYDE_METADATA_KEY = Symbol('hyde');
|
|
11
|
+
function HyDE(config = {}) {
|
|
12
|
+
return function (target, propertyKey, descriptor) {
|
|
13
|
+
const originalMethod = descriptor.value;
|
|
14
|
+
descriptor.value = async function (...args) {
|
|
15
|
+
const query = args[0];
|
|
16
|
+
if (!config.generateHypothesis || !config.llmProvider) {
|
|
17
|
+
return originalMethod.apply(this, args);
|
|
18
|
+
}
|
|
19
|
+
// Generate hypothetical documents
|
|
20
|
+
const hypotheses = await generateHypotheticalDocuments(query, config.numHypotheses || 3, config.llmProvider);
|
|
21
|
+
// Retrieve using each hypothesis
|
|
22
|
+
const allResults = [];
|
|
23
|
+
const seenIds = new Set();
|
|
24
|
+
for (const hypothesis of hypotheses) {
|
|
25
|
+
const results = await originalMethod.apply(this, [hypothesis, ...args.slice(1)]);
|
|
26
|
+
if (Array.isArray(results)) {
|
|
27
|
+
for (const result of results) {
|
|
28
|
+
if (result.id && !seenIds.has(result.id)) {
|
|
29
|
+
seenIds.add(result.id);
|
|
30
|
+
allResults.push(result);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
// Re-rank by aggregated scores
|
|
36
|
+
const reranked = rerankResults(allResults, hypotheses.length);
|
|
37
|
+
const hydeResult = {
|
|
38
|
+
hypotheticalDocuments: hypotheses,
|
|
39
|
+
retrievedResults: reranked,
|
|
40
|
+
aggregatedScore: calculateAggregatedScore(reranked),
|
|
41
|
+
};
|
|
42
|
+
Reflect.defineMetadata(HYDE_METADATA_KEY, hydeResult, target, propertyKey);
|
|
43
|
+
return reranked;
|
|
44
|
+
};
|
|
45
|
+
return descriptor;
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
async function generateHypotheticalDocuments(query, count, llmProvider) {
|
|
49
|
+
const prompt = `Generate ${count} hypothetical documents that would perfectly answer this query.
|
|
50
|
+
Each document should be detailed and comprehensive.
|
|
51
|
+
|
|
52
|
+
Query: ${query}
|
|
53
|
+
|
|
54
|
+
Generate ${count} hypothetical documents (one per line):`;
|
|
55
|
+
try {
|
|
56
|
+
const response = await llmProvider.generate(prompt, {
|
|
57
|
+
temperature: 0.7,
|
|
58
|
+
maxTokens: 1000,
|
|
59
|
+
});
|
|
60
|
+
const documents = response
|
|
61
|
+
.split('\n')
|
|
62
|
+
.map((line) => line.replace(/^\d+\.\s*/, '').trim())
|
|
63
|
+
.filter((line) => line.length > 50)
|
|
64
|
+
.slice(0, count);
|
|
65
|
+
return documents.length > 0 ? documents : [query];
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
return [query];
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
72
|
+
function rerankResults(results, hypothesisCount) {
|
|
73
|
+
return results
|
|
74
|
+
.map((result) => ({
|
|
75
|
+
...result,
|
|
76
|
+
score: (result.score || 0) * (1 + 0.1 * hypothesisCount),
|
|
77
|
+
}))
|
|
78
|
+
.sort((a, b) => (b.score || 0) - (a.score || 0));
|
|
79
|
+
}
|
|
80
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
81
|
+
function calculateAggregatedScore(results) {
|
|
82
|
+
if (results.length === 0)
|
|
83
|
+
return 0;
|
|
84
|
+
const totalScore = results.reduce((sum, r) => sum + (r.score || 0), 0);
|
|
85
|
+
return totalScore / results.length;
|
|
86
|
+
}
|
|
87
|
+
function getHyDEResult(target, propertyKey) {
|
|
88
|
+
return Reflect.getMetadata(HYDE_METADATA_KEY, target, propertyKey);
|
|
89
|
+
}
|
|
90
|
+
//# sourceMappingURL=hyde.decorator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"hyde.decorator.js","sourceRoot":"","sources":["../../../src/agentic/decorators/hyde.decorator.ts"],"names":[],"mappings":";AAAA;;;GAGG;;AAaH,oBAmDC;AAkDD,sCAKC;AArHD,4BAA0B;AAS1B,MAAM,iBAAiB,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC;AAEzC,SAAgB,IAAI,CAAC,SAAqB,EAAE;IAC1C,OAAO,UAAU,MAAc,EAAE,WAA4B,EAAE,UAA8B;QAC3F,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAC;QAExC,UAAU,CAAC,KAAK,GAAG,KAAK,WA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CAAC,GAAG,CAAC,CAAC,GAAG,GAAG,GAAG,eAAe,CAAC;KACzD,CAAC,CAAC;SACF,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,CAAC;AACrD,CAAC;AAED,8DAA8D;AAC9D,SAAS,wBAAwB,CAAC,OAAc;IAC9C,IAAI,OAAO,CAAC,MAAM,KAAK,CAAC;QAAE,OAAO,CAAC,CAAC;IAEnC,MAAM,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACvE,OAAO,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC;AACrC,CAAC;AAED,SAAgB,aAAa,CAC3B,MAAc,EACd,WAA4B;IAE5B,OAAO,OAAO,CAAC,WAAW,CAAC,iBAAiB,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AACrE,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agentic RAG Decorators
|
|
3
|
+
* Export all decorators
|
|
4
|
+
*/
|
|
5
|
+
export * from './query-planner.decorator';
|
|
6
|
+
export * from './self-reflective.decorator';
|
|
7
|
+
export * from './adaptive-retrieval.decorator';
|
|
8
|
+
export * from './multi-hop.decorator';
|
|
9
|
+
export * from './hyde.decorator';
|
|
10
|
+
export * from './corrective-rag.decorator';
|
|
11
|
+
export * from './context-aware.decorator';
|
|
12
|
+
export * from './query-rewriter.decorator';
|
|
13
|
+
export * from './source-verification.decorator';
|
|
14
|
+
export * from './active-learning.decorator';
|
|
15
|
+
export * from './cached.decorator';
|
|
16
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/agentic/decorators/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,cAAc,2BAA2B,CAAC;AAC1C,cAAc,6BAA6B,CAAC;AAC5C,cAAc,gCAAgC,CAAC;AAC/C,cAAc,uBAAuB,CAAC;AACtC,cAAc,kBAAkB,CAAC;AACjC,cAAc,4BAA4B,CAAC;AAC3C,cAAc,2BAA2B,CAAC;AAC1C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,iCAAiC,CAAC;AAChD,cAAc,6BAA6B,CAAC;AAC5C,cAAc,oBAAoB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Agentic RAG Decorators
|
|
4
|
+
* Export all decorators
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
18
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
19
|
+
};
|
|
20
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
+
__exportStar(require("./query-planner.decorator"), exports);
|
|
22
|
+
__exportStar(require("./self-reflective.decorator"), exports);
|
|
23
|
+
__exportStar(require("./adaptive-retrieval.decorator"), exports);
|
|
24
|
+
__exportStar(require("./multi-hop.decorator"), exports);
|
|
25
|
+
__exportStar(require("./hyde.decorator"), exports);
|
|
26
|
+
__exportStar(require("./corrective-rag.decorator"), exports);
|
|
27
|
+
__exportStar(require("./context-aware.decorator"), exports);
|
|
28
|
+
__exportStar(require("./query-rewriter.decorator"), exports);
|
|
29
|
+
__exportStar(require("./source-verification.decorator"), exports);
|
|
30
|
+
__exportStar(require("./active-learning.decorator"), exports);
|
|
31
|
+
__exportStar(require("./cached.decorator"), exports);
|
|
32
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/agentic/decorators/index.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;AAEH,4DAA0C;AAC1C,8DAA4C;AAC5C,iEAA+C;AAC/C,wDAAsC;AACtC,mDAAiC;AACjC,6DAA2C;AAC3C,4DAA0C;AAC1C,6DAA2C;AAC3C,kEAAgD;AAChD,8DAA4C;AAC5C,qDAAmC"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Multi-Hop Decorator
|
|
3
|
+
* Performs multi-hop reasoning across documents
|
|
4
|
+
*/
|
|
5
|
+
import 'reflect-metadata';
|
|
6
|
+
import { ReasoningChain, AgenticLLMProvider } from '../types';
|
|
7
|
+
export interface MultiHopConfig {
|
|
8
|
+
maxHops?: number;
|
|
9
|
+
strategy?: 'breadth-first' | 'depth-first' | 'adaptive';
|
|
10
|
+
llmProvider?: AgenticLLMProvider;
|
|
11
|
+
}
|
|
12
|
+
export declare function MultiHop(config?: MultiHopConfig): MethodDecorator;
|
|
13
|
+
export declare function getReasoningChain(target: object, propertyKey: string | symbol): ReasoningChain | undefined;
|
|
14
|
+
//# sourceMappingURL=multi-hop.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-hop.decorator.d.ts","sourceRoot":"","sources":["../../../src/agentic/decorators/multi-hop.decorator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,cAAc,EAAgB,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE5E,MAAM,WAAW,cAAc;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,QAAQ,CAAC,EAAE,eAAe,GAAG,aAAa,GAAG,UAAU,CAAC;IACxD,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAID,wBAAgB,QAAQ,CAAC,MAAM,GAAE,cAAmB,GAAG,eAAe,CAuDrE;AAkFD,wBAAgB,iBAAiB,CAC/B,MAAM,EAAE,MAAM,EACd,WAAW,EAAE,MAAM,GAAG,MAAM,GAC3B,cAAc,GAAG,SAAS,CAE5B"}
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Multi-Hop Decorator
|
|
4
|
+
* Performs multi-hop reasoning across documents
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MultiHop = MultiHop;
|
|
8
|
+
exports.getReasoningChain = getReasoningChain;
|
|
9
|
+
require("reflect-metadata");
|
|
10
|
+
const MULTIHOP_METADATA_KEY = Symbol('multiHop');
|
|
11
|
+
function MultiHop(config = {}) {
|
|
12
|
+
return function (target, propertyKey, descriptor) {
|
|
13
|
+
const originalMethod = descriptor.value;
|
|
14
|
+
descriptor.value = async function (...args) {
|
|
15
|
+
const query = args[0];
|
|
16
|
+
const maxHops = config.maxHops || 3;
|
|
17
|
+
const chain = {
|
|
18
|
+
query,
|
|
19
|
+
hops: [],
|
|
20
|
+
finalAnswer: '',
|
|
21
|
+
confidence: 0,
|
|
22
|
+
sources: [],
|
|
23
|
+
};
|
|
24
|
+
let currentQuery = query;
|
|
25
|
+
for (let hop = 0; hop < maxHops; hop++) {
|
|
26
|
+
// Retrieve for current query
|
|
27
|
+
const results = await originalMethod.apply(this, [currentQuery, ...args.slice(1)]);
|
|
28
|
+
// Generate reasoning
|
|
29
|
+
const reasoning = await generateReasoning(currentQuery, results, config);
|
|
30
|
+
const hopResult = {
|
|
31
|
+
hopNumber: hop + 1,
|
|
32
|
+
query: currentQuery,
|
|
33
|
+
results: Array.isArray(results) ? results : [results],
|
|
34
|
+
reasoning: reasoning.reasoning,
|
|
35
|
+
nextQuery: reasoning.nextQuery,
|
|
36
|
+
};
|
|
37
|
+
chain.hops.push(hopResult);
|
|
38
|
+
chain.sources.push(...hopResult.results);
|
|
39
|
+
// Check if we should continue
|
|
40
|
+
if (!reasoning.nextQuery || reasoning.shouldStop) {
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
currentQuery = reasoning.nextQuery;
|
|
44
|
+
}
|
|
45
|
+
// Generate final answer
|
|
46
|
+
chain.finalAnswer = await synthesizeFinalAnswer(chain, config);
|
|
47
|
+
chain.confidence = calculateConfidence(chain);
|
|
48
|
+
Reflect.defineMetadata(MULTIHOP_METADATA_KEY, chain, target, propertyKey);
|
|
49
|
+
return chain;
|
|
50
|
+
};
|
|
51
|
+
return descriptor;
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async function generateReasoning(query,
|
|
55
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
56
|
+
results, config) {
|
|
57
|
+
if (!config.llmProvider) {
|
|
58
|
+
return {
|
|
59
|
+
reasoning: 'Retrieved results for: ' + query,
|
|
60
|
+
shouldStop: true,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
const resultsStr = JSON.stringify(results, null, 2).slice(0, 1500);
|
|
64
|
+
const prompt = `Analyze these search results and determine if we need more information.
|
|
65
|
+
|
|
66
|
+
Query: ${query}
|
|
67
|
+
Results: ${resultsStr}
|
|
68
|
+
|
|
69
|
+
Provide analysis in JSON:
|
|
70
|
+
{
|
|
71
|
+
"reasoning": "what we learned from these results",
|
|
72
|
+
"nextQuery": "follow-up query if needed, or null",
|
|
73
|
+
"shouldStop": true/false
|
|
74
|
+
}`;
|
|
75
|
+
try {
|
|
76
|
+
return await config.llmProvider.generateStructured(prompt, {});
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return {
|
|
80
|
+
reasoning: 'Retrieved results',
|
|
81
|
+
shouldStop: true,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
async function synthesizeFinalAnswer(chain, config) {
|
|
86
|
+
if (!config.llmProvider) {
|
|
87
|
+
return chain.hops.map((h) => h.reasoning).join('\n\n');
|
|
88
|
+
}
|
|
89
|
+
const hopsStr = chain.hops.map((h) => `Hop ${h.hopNumber}: ${h.reasoning}`).join('\n');
|
|
90
|
+
const prompt = `Synthesize a final answer from this multi-hop reasoning chain:
|
|
91
|
+
|
|
92
|
+
Original Query: ${chain.query}
|
|
93
|
+
|
|
94
|
+
Reasoning Chain:
|
|
95
|
+
${hopsStr}
|
|
96
|
+
|
|
97
|
+
Provide a comprehensive final answer:`;
|
|
98
|
+
try {
|
|
99
|
+
return await config.llmProvider.generate(prompt);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
return hopsStr;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
function calculateConfidence(chain) {
|
|
106
|
+
const hopCount = chain.hops.length;
|
|
107
|
+
const sourceCount = chain.sources.length;
|
|
108
|
+
let confidence = 0.5;
|
|
109
|
+
if (sourceCount >= 5)
|
|
110
|
+
confidence += 0.2;
|
|
111
|
+
if (hopCount >= 2)
|
|
112
|
+
confidence += 0.1;
|
|
113
|
+
if (chain.finalAnswer.length > 100)
|
|
114
|
+
confidence += 0.1;
|
|
115
|
+
return Math.min(confidence, 1.0);
|
|
116
|
+
}
|
|
117
|
+
function getReasoningChain(target, propertyKey) {
|
|
118
|
+
return Reflect.getMetadata(MULTIHOP_METADATA_KEY, target, propertyKey);
|
|
119
|
+
}
|
|
120
|
+
//# sourceMappingURL=multi-hop.decorator.js.map
|
|
@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ACrC,IAAI,KAAK,CAAC,WAAW,CAAC,MAAM,GAAG,GAAG;QAAE,UAAU,IAAI,GAAG,CAAC;IAEtD,OAAO,IAAI,CAAC,GAAG,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC;AACnC,CAAC;AAED,SAAgB,iBAAiB,CAC/B,MAAc,EACd,WAA4B;IAE5B,OAAO,OAAO,CAAC,WAAW,CAAC,qBAAqB,EAAE,MAAM,EAAE,WAAW,CAAC,CAAC;AACzE,CAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query Planner Decorator
|
|
3
|
+
* Automatically decomposes complex queries into sub-queries
|
|
4
|
+
*/
|
|
5
|
+
import 'reflect-metadata';
|
|
6
|
+
import { QueryPlan, AgenticLLMProvider } from '../types';
|
|
7
|
+
export interface QueryPlannerConfig {
|
|
8
|
+
decompose?: boolean;
|
|
9
|
+
rewrite?: boolean;
|
|
10
|
+
maxSubQueries?: number;
|
|
11
|
+
parallel?: boolean;
|
|
12
|
+
llmProvider?: AgenticLLMProvider;
|
|
13
|
+
}
|
|
14
|
+
export declare function QueryPlanner(config?: QueryPlannerConfig): MethodDecorator;
|
|
15
|
+
/**
|
|
16
|
+
* Get query plan metadata
|
|
17
|
+
*/
|
|
18
|
+
export declare function getQueryPlan(target: object, propertyKey: string | symbol): QueryPlan | undefined;
|
|
19
|
+
//# sourceMappingURL=query-planner.decorator.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-planner.decorator.d.ts","sourceRoot":"","sources":["../../../src/agentic/decorators/query-planner.decorator.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,kBAAkB,CAAC;AAC1B,OAAO,EAAE,SAAS,EAAY,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAEnE,MAAM,WAAW,kBAAkB;IACjC,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,kBAAkB,CAAC;CAClC;AAID,wBAAgB,YAAY,CAAC,MAAM,GAAE,kBAAuB,GAAG,eAAe,CA2B7E;AAwOD;;GAEG;AACH,wBAAgB,YAAY,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,GAAG,SAAS,CAEhG"}
|