@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,135 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Multi-Query Retrieval
|
|
4
|
+
* Generates multiple search queries from a single question to improve retrieval
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.MultiQueryRetrieval = void 0;
|
|
8
|
+
class MultiQueryRetrieval {
|
|
9
|
+
constructor(vectorStore, config = {}) {
|
|
10
|
+
this.vectorStore = vectorStore;
|
|
11
|
+
this.config = {
|
|
12
|
+
numQueries: config.numQueries || 3,
|
|
13
|
+
llmProvider: config.llmProvider || 'openai',
|
|
14
|
+
...config,
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Perform multi-query retrieval
|
|
19
|
+
*/
|
|
20
|
+
async retrieve(question, options) {
|
|
21
|
+
// Generate multiple queries
|
|
22
|
+
const queries = await this.generateQueries(question);
|
|
23
|
+
// Search with each query
|
|
24
|
+
const allResults = [];
|
|
25
|
+
const seenIds = new Set();
|
|
26
|
+
for (const query of queries) {
|
|
27
|
+
const results = await this.vectorStore.search(query, options);
|
|
28
|
+
// Deduplicate results
|
|
29
|
+
for (const result of results) {
|
|
30
|
+
if (!seenIds.has(result.id)) {
|
|
31
|
+
seenIds.add(result.id);
|
|
32
|
+
allResults.push(result);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
// Re-rank by average score across queries
|
|
37
|
+
const rankedResults = this.rankResults(allResults, queries);
|
|
38
|
+
// Return top K
|
|
39
|
+
const topK = options?.topK || 5;
|
|
40
|
+
return rankedResults.slice(0, topK);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Generate multiple search queries from a single question
|
|
44
|
+
*/
|
|
45
|
+
async generateQueries(question) {
|
|
46
|
+
const numQueries = this.config.numQueries;
|
|
47
|
+
// Use custom generator if provided
|
|
48
|
+
if (this.config.customGenerator) {
|
|
49
|
+
return this.config.customGenerator(question, numQueries);
|
|
50
|
+
}
|
|
51
|
+
// Use LLM to generate queries
|
|
52
|
+
if (this.config.llmProvider === 'openai') {
|
|
53
|
+
return this.generateWithOpenAI(question, numQueries);
|
|
54
|
+
}
|
|
55
|
+
// Fallback: simple query variations
|
|
56
|
+
return this.generateSimpleVariations(question, numQueries);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Generate queries using OpenAI
|
|
60
|
+
*/
|
|
61
|
+
async generateWithOpenAI(question, numQueries) {
|
|
62
|
+
try {
|
|
63
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
64
|
+
const { OpenAI } = require('openai');
|
|
65
|
+
const client = new OpenAI({ apiKey: this.config.apiKey });
|
|
66
|
+
const prompt = `You are a helpful assistant that generates search queries.
|
|
67
|
+
Given a question, generate ${numQueries} different search queries that could help find relevant information.
|
|
68
|
+
Each query should approach the question from a different angle.
|
|
69
|
+
|
|
70
|
+
Question: ${question}
|
|
71
|
+
|
|
72
|
+
Generate ${numQueries} search queries (one per line):`;
|
|
73
|
+
const response = await client.chat.completions.create({
|
|
74
|
+
model: this.config.model || 'gpt-3.5-turbo',
|
|
75
|
+
messages: [{ role: 'user', content: prompt }],
|
|
76
|
+
temperature: 0.7,
|
|
77
|
+
});
|
|
78
|
+
const content = response.choices[0].message.content || '';
|
|
79
|
+
const queries = content
|
|
80
|
+
.split('\n')
|
|
81
|
+
.map((q) => q.replace(/^\d+\.\s*/, '').trim())
|
|
82
|
+
.filter((q) => q.length > 0)
|
|
83
|
+
.slice(0, numQueries);
|
|
84
|
+
// Include original question
|
|
85
|
+
return [question, ...queries].slice(0, numQueries + 1);
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Fallback to simple variations on error
|
|
89
|
+
return this.generateSimpleVariations(question, numQueries);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Generate simple query variations (fallback)
|
|
94
|
+
*/
|
|
95
|
+
generateSimpleVariations(question, numQueries) {
|
|
96
|
+
const queries = [question];
|
|
97
|
+
// Add variations
|
|
98
|
+
const variations = [
|
|
99
|
+
`What is ${question.toLowerCase()}`,
|
|
100
|
+
`Explain ${question.toLowerCase()}`,
|
|
101
|
+
`How does ${question.toLowerCase()} work`,
|
|
102
|
+
`${question} details`,
|
|
103
|
+
`${question} information`,
|
|
104
|
+
];
|
|
105
|
+
queries.push(...variations.slice(0, numQueries - 1));
|
|
106
|
+
return queries.slice(0, numQueries);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Rank results by frequency and average score
|
|
110
|
+
*/
|
|
111
|
+
rankResults(results, queries) {
|
|
112
|
+
// Group results by ID and calculate average score
|
|
113
|
+
const scoreMap = new Map();
|
|
114
|
+
for (const result of results) {
|
|
115
|
+
if (!scoreMap.has(result.id)) {
|
|
116
|
+
scoreMap.set(result.id, { result, scores: [] });
|
|
117
|
+
}
|
|
118
|
+
scoreMap.get(result.id).scores.push(result.score);
|
|
119
|
+
}
|
|
120
|
+
// Calculate average score and frequency bonus
|
|
121
|
+
const rankedResults = Array.from(scoreMap.values()).map(({ result, scores }) => {
|
|
122
|
+
const avgScore = scores.reduce((sum, s) => sum + s, 0) / scores.length;
|
|
123
|
+
const frequencyBonus = scores.length / queries.length; // 0 to 1
|
|
124
|
+
const finalScore = avgScore * 0.7 + frequencyBonus * 0.3;
|
|
125
|
+
return {
|
|
126
|
+
...result,
|
|
127
|
+
score: finalScore,
|
|
128
|
+
};
|
|
129
|
+
});
|
|
130
|
+
// Sort by final score
|
|
131
|
+
return rankedResults.sort((a, b) => b.score - a.score);
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
exports.MultiQueryRetrieval = MultiQueryRetrieval;
|
|
135
|
+
//# sourceMappingURL=multi-query.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"multi-query.js","sourceRoot":"","sources":["../../src/retrieval/multi-query.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAYH,MAAa,mBAAmB;IAI9B,YAAY,WAAwB,EAAE,SAA2B,EAAE;QACjE,IAAI,CAAC,WAAW,GAAG,WAAW,CAAC;QAC/B,IAAI,CAAC,MAAM,GAAG;YACZ,UAAU,EAAE,MAAM,CAAC,UAAU,IAAI,CAAC;YAClC,WAAW,EAAE,MAAM,CAAC,WAAW,IAAI,QAAQ;YAC3C,GAAG,MAAM;SACV,CAAC;IACJ,CAAC;IAED;;OAEG;IACH,KAAK,CAAC,QAAQ,C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AC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QACrD,CAAC;QAED,8CAA8C;QAC9C,MAAM,aAAa,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE;YAC7E,MAAM,QAAQ,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,CAAC;YACvE,MAAM,cAAc,GAAG,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,SAAS;YAChE,MAAM,UAAU,GAAG,QAAQ,GAAG,GAAG,GAAG,cAAc,GAAG,GAAG,CAAC;YAEzD,OAAO;gBACL,GAAG,MAAM;gBACT,KAAK,EAAE,UAAU;aAClB,CAAC;QACJ,CAAC,CAAC,CAAC;QAEH,sBAAsB;QACtB,OAAO,aAAa,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC;IACzD,CAAC;CACF;AAtJD,kDAsJC"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Recursive Text Splitter
|
|
3
|
+
* Splits text recursively by different separators
|
|
4
|
+
*/
|
|
5
|
+
import { TextSplitter, Document } from '../types';
|
|
6
|
+
export interface RecursiveTextSplitterConfig {
|
|
7
|
+
chunkSize?: number;
|
|
8
|
+
chunkOverlap?: number;
|
|
9
|
+
separators?: string[];
|
|
10
|
+
}
|
|
11
|
+
export declare class RecursiveTextSplitter implements TextSplitter {
|
|
12
|
+
private chunkSize;
|
|
13
|
+
private chunkOverlap;
|
|
14
|
+
private separators;
|
|
15
|
+
constructor(config?: RecursiveTextSplitterConfig);
|
|
16
|
+
split(text: string): string[];
|
|
17
|
+
splitDocuments(documents: Document[]): Document[];
|
|
18
|
+
private splitTextRecursive;
|
|
19
|
+
private addOverlap;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=recursive-text-splitter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recursive-text-splitter.d.ts","sourceRoot":"","sources":["../../src/text-splitters/recursive-text-splitter.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,YAAY,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAElD,MAAM,WAAW,2BAA2B;IAC1C,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;CACvB;AAED,qBAAa,qBAAsB,YAAW,YAAY;IACxD,OAAO,CAAC,SAAS,CAAS;IAC1B,OAAO,CAAC,YAAY,CAAS;IAC7B,OAAO,CAAC,UAAU,CAAW;gBAEjB,MAAM,GAAE,2BAAgC;IAMpD,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE;IAI7B,cAAc,CAAC,SAAS,EAAE,QAAQ,EAAE,GAAG,QAAQ,EAAE;IAsBjD,OAAO,CAAC,kBAAkB;IA+C1B,OAAO,CAAC,UAAU;CAsBnB"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Recursive Text Splitter
|
|
4
|
+
* Splits text recursively by different separators
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.RecursiveTextSplitter = void 0;
|
|
8
|
+
class RecursiveTextSplitter {
|
|
9
|
+
constructor(config = {}) {
|
|
10
|
+
this.chunkSize = config.chunkSize || 1000;
|
|
11
|
+
this.chunkOverlap = config.chunkOverlap || 200;
|
|
12
|
+
this.separators = config.separators || ['\n\n', '\n', '. ', ' ', ''];
|
|
13
|
+
}
|
|
14
|
+
split(text) {
|
|
15
|
+
return this.splitTextRecursive(text, this.separators);
|
|
16
|
+
}
|
|
17
|
+
splitDocuments(documents) {
|
|
18
|
+
const chunks = [];
|
|
19
|
+
for (const doc of documents) {
|
|
20
|
+
const textChunks = this.split(doc.content);
|
|
21
|
+
for (let i = 0; i < textChunks.length; i++) {
|
|
22
|
+
chunks.push({
|
|
23
|
+
content: textChunks[i],
|
|
24
|
+
metadata: {
|
|
25
|
+
...doc.metadata,
|
|
26
|
+
chunkIndex: i,
|
|
27
|
+
totalChunks: textChunks.length,
|
|
28
|
+
sourceDocId: doc.id,
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return chunks;
|
|
34
|
+
}
|
|
35
|
+
splitTextRecursive(text, separators) {
|
|
36
|
+
const finalChunks = [];
|
|
37
|
+
// Get the first separator
|
|
38
|
+
const separator = separators[0];
|
|
39
|
+
let newSeparators;
|
|
40
|
+
if (separator === '') {
|
|
41
|
+
newSeparators = [];
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
newSeparators = separators.slice(1);
|
|
45
|
+
}
|
|
46
|
+
// Split by separator
|
|
47
|
+
const splits = separator === '' ? text.split('') : text.split(separator);
|
|
48
|
+
// Merge splits into chunks
|
|
49
|
+
let currentChunk = '';
|
|
50
|
+
for (const split of splits) {
|
|
51
|
+
const potentialChunk = currentChunk + (currentChunk ? separator : '') + split;
|
|
52
|
+
if (potentialChunk.length <= this.chunkSize) {
|
|
53
|
+
currentChunk = potentialChunk;
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
if (currentChunk) {
|
|
57
|
+
finalChunks.push(currentChunk);
|
|
58
|
+
}
|
|
59
|
+
// If split is still too large and we have more separators, recurse
|
|
60
|
+
if (split.length > this.chunkSize && newSeparators.length > 0) {
|
|
61
|
+
const subChunks = this.splitTextRecursive(split, newSeparators);
|
|
62
|
+
finalChunks.push(...subChunks);
|
|
63
|
+
currentChunk = '';
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
currentChunk = split;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
if (currentChunk) {
|
|
71
|
+
finalChunks.push(currentChunk);
|
|
72
|
+
}
|
|
73
|
+
// Add overlap
|
|
74
|
+
return this.addOverlap(finalChunks);
|
|
75
|
+
}
|
|
76
|
+
addOverlap(chunks) {
|
|
77
|
+
if (this.chunkOverlap === 0 || chunks.length <= 1) {
|
|
78
|
+
return chunks;
|
|
79
|
+
}
|
|
80
|
+
const overlappedChunks = [];
|
|
81
|
+
for (let i = 0; i < chunks.length; i++) {
|
|
82
|
+
let chunk = chunks[i];
|
|
83
|
+
// Add overlap from previous chunk
|
|
84
|
+
if (i > 0) {
|
|
85
|
+
const prevChunk = chunks[i - 1];
|
|
86
|
+
const overlapText = prevChunk.slice(-this.chunkOverlap);
|
|
87
|
+
chunk = overlapText + chunk;
|
|
88
|
+
}
|
|
89
|
+
overlappedChunks.push(chunk);
|
|
90
|
+
}
|
|
91
|
+
return overlappedChunks;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
exports.RecursiveTextSplitter = RecursiveTextSplitter;
|
|
95
|
+
//# sourceMappingURL=recursive-text-splitter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"recursive-text-splitter.js","sourceRoot":"","sources":["../../src/text-splitters/recursive-text-splitter.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAUH,MAAa,qBAAqB;IAKhC,YAAY,SAAsC,EAAE;QAClD,IAAI,CAAC,SAAS,GAAG,MAAM,CAAC,SAAS,IAAI,IAAI,CAAC;QAC1C,IAAI,CAAC,YAAY,GAAG,MAAM,CAAC,YAAY,IAAI,GAAG,CAAC;QAC/C,IAAI,CAAC,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,CAAC,MAAM,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,EA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,CAAC,EAAE,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACvC,IAAI,KAAK,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;YAEtB,kCAAkC;YAClC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;gBACV,MAAM,SAAS,GAAG,MAAM,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;gBAChC,MAAM,WAAW,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC,CAAC;gBACxD,KAAK,GAAG,WAAW,GAAG,KAAK,CAAC;YAC9B,CAAC;YAED,gBAAgB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;QAED,OAAO,gBAAgB,CAAC;IAC1B,CAAC;CACF;AA1GD,sDA0GC"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Core types for RAG package
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Document to be indexed
|
|
6
|
+
*/
|
|
7
|
+
export interface Document {
|
|
8
|
+
id?: string;
|
|
9
|
+
content: string;
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
embedding?: number[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Search result with similarity score
|
|
15
|
+
*/
|
|
16
|
+
export interface SearchResult {
|
|
17
|
+
id: string;
|
|
18
|
+
content: string;
|
|
19
|
+
metadata?: Record<string, unknown>;
|
|
20
|
+
score: number;
|
|
21
|
+
embedding?: number[];
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Query options for vector search
|
|
25
|
+
*/
|
|
26
|
+
export interface QueryOptions {
|
|
27
|
+
topK?: number;
|
|
28
|
+
filter?: Record<string, unknown>;
|
|
29
|
+
includeMetadata?: boolean;
|
|
30
|
+
includeEmbedding?: boolean;
|
|
31
|
+
minScore?: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Embedding provider interface
|
|
35
|
+
*/
|
|
36
|
+
export interface EmbeddingProvider {
|
|
37
|
+
/**
|
|
38
|
+
* Generate embeddings for text
|
|
39
|
+
*/
|
|
40
|
+
embed(text: string): Promise<number[]>;
|
|
41
|
+
/**
|
|
42
|
+
* Generate embeddings for multiple texts
|
|
43
|
+
*/
|
|
44
|
+
embedBatch(texts: string[]): Promise<number[][]>;
|
|
45
|
+
/**
|
|
46
|
+
* Get the dimension of embeddings
|
|
47
|
+
*/
|
|
48
|
+
getDimension(): number;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Vector store interface
|
|
52
|
+
*/
|
|
53
|
+
export interface VectorStore {
|
|
54
|
+
/**
|
|
55
|
+
* Initialize the vector store
|
|
56
|
+
*/
|
|
57
|
+
initialize(): Promise<void>;
|
|
58
|
+
/**
|
|
59
|
+
* Add documents to the store
|
|
60
|
+
*/
|
|
61
|
+
addDocuments(documents: Document[]): Promise<string[]>;
|
|
62
|
+
/**
|
|
63
|
+
* Search for similar documents
|
|
64
|
+
*/
|
|
65
|
+
search(query: string, options?: QueryOptions): Promise<SearchResult[]>;
|
|
66
|
+
/**
|
|
67
|
+
* Search using embedding vector
|
|
68
|
+
*/
|
|
69
|
+
searchByVector(embedding: number[], options?: QueryOptions): Promise<SearchResult[]>;
|
|
70
|
+
/**
|
|
71
|
+
* Delete documents by IDs
|
|
72
|
+
*/
|
|
73
|
+
deleteDocuments(ids: string[]): Promise<void>;
|
|
74
|
+
/**
|
|
75
|
+
* Update a document
|
|
76
|
+
*/
|
|
77
|
+
updateDocument(id: string, document: Partial<Document>): Promise<void>;
|
|
78
|
+
/**
|
|
79
|
+
* Get document by ID
|
|
80
|
+
*/
|
|
81
|
+
getDocument(id: string): Promise<Document | null>;
|
|
82
|
+
/**
|
|
83
|
+
* Clear all documents
|
|
84
|
+
*/
|
|
85
|
+
clear(): Promise<void>;
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Text splitter interface
|
|
89
|
+
*/
|
|
90
|
+
export interface TextSplitter {
|
|
91
|
+
/**
|
|
92
|
+
* Split text into chunks
|
|
93
|
+
*/
|
|
94
|
+
split(text: string): string[];
|
|
95
|
+
/**
|
|
96
|
+
* Split documents into chunks
|
|
97
|
+
*/
|
|
98
|
+
splitDocuments(documents: Document[]): Document[];
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Document loader interface
|
|
102
|
+
*/
|
|
103
|
+
export interface DocumentLoader {
|
|
104
|
+
/**
|
|
105
|
+
* Load documents from source
|
|
106
|
+
*/
|
|
107
|
+
load(): Promise<Document[]>;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* RAG configuration
|
|
111
|
+
*/
|
|
112
|
+
export interface RAGConfig {
|
|
113
|
+
vectorStore: VectorStore;
|
|
114
|
+
embeddingProvider: EmbeddingProvider;
|
|
115
|
+
textSplitter?: TextSplitter;
|
|
116
|
+
chunkSize?: number;
|
|
117
|
+
chunkOverlap?: number;
|
|
118
|
+
topK?: number;
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Retrieval strategy
|
|
122
|
+
*/
|
|
123
|
+
export declare enum RetrievalStrategy {
|
|
124
|
+
SIMILARITY = "similarity",
|
|
125
|
+
MMR = "mmr",// Maximal Marginal Relevance
|
|
126
|
+
HYBRID = "hybrid"
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* RAG query options
|
|
130
|
+
*/
|
|
131
|
+
export interface RAGQueryOptions extends QueryOptions {
|
|
132
|
+
strategy?: RetrievalStrategy;
|
|
133
|
+
llmPrompt?: string;
|
|
134
|
+
includeContext?: boolean;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* RAG response
|
|
138
|
+
*/
|
|
139
|
+
export interface RAGResponse {
|
|
140
|
+
answer: string;
|
|
141
|
+
sources: SearchResult[];
|
|
142
|
+
context: string;
|
|
143
|
+
}
|
|
144
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@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,EAAE,MAAM,CAAC;CACf;AAED;;GAEG;AACH,oBAAY,iBAAiB;IAC3B,UAAU,eAAe;IACzB,GAAG,QAAQ,CAAE,6BAA6B;IAC1C,MAAM,WAAW;CAClB;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,YAAY;IACnD,QAAQ,CAAC,EAAE,iBAAiB,CAAC;IAC7B,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,YAAY,EAAE,CAAC;IACxB,OAAO,EAAE,MAAM,CAAC;CACjB"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Core types for RAG package
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.RetrievalStrategy = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* Retrieval strategy
|
|
9
|
+
*/
|
|
10
|
+
var RetrievalStrategy;
|
|
11
|
+
(function (RetrievalStrategy) {
|
|
12
|
+
RetrievalStrategy["SIMILARITY"] = "similarity";
|
|
13
|
+
RetrievalStrategy["MMR"] = "mmr";
|
|
14
|
+
RetrievalStrategy["HYBRID"] = "hybrid";
|
|
15
|
+
})(RetrievalStrategy || (exports.RetrievalStrategy = RetrievalStrategy = {}));
|
|
16
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAwIH;;GAEG;AACH,IAAY,iBAIX;AAJD,WAAY,iBAAiB;IAC3B,8CAAyB,CAAA;IACzB,gCAAW,CAAA;IACX,sCAAiB,CAAA;AACnB,CAAC,EAJW,iBAAiB,iCAAjB,iBAAiB,QAI5B"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Similarity calculation utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Calculate cosine similarity between two vectors
|
|
6
|
+
*/
|
|
7
|
+
export declare function cosineSimilarity(a: number[], b: number[]): number;
|
|
8
|
+
/**
|
|
9
|
+
* Calculate Euclidean distance between two vectors
|
|
10
|
+
*/
|
|
11
|
+
export declare function euclideanDistance(a: number[], b: number[]): number;
|
|
12
|
+
/**
|
|
13
|
+
* Calculate dot product between two vectors
|
|
14
|
+
*/
|
|
15
|
+
export declare function dotProduct(a: number[], b: number[]): number;
|
|
16
|
+
//# sourceMappingURL=similarity.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"similarity.d.ts","sourceRoot":"","sources":["../../src/utils/similarity.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAuBjE;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAYlE;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,EAAE,CAAC,EAAE,MAAM,EAAE,GAAG,MAAM,CAW3D"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Similarity calculation utilities
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.cosineSimilarity = cosineSimilarity;
|
|
7
|
+
exports.euclideanDistance = euclideanDistance;
|
|
8
|
+
exports.dotProduct = dotProduct;
|
|
9
|
+
/**
|
|
10
|
+
* Calculate cosine similarity between two vectors
|
|
11
|
+
*/
|
|
12
|
+
function cosineSimilarity(a, b) {
|
|
13
|
+
if (a.length !== b.length) {
|
|
14
|
+
throw new Error('Vectors must have the same length');
|
|
15
|
+
}
|
|
16
|
+
let dotProduct = 0;
|
|
17
|
+
let normA = 0;
|
|
18
|
+
let normB = 0;
|
|
19
|
+
for (let i = 0; i < a.length; i++) {
|
|
20
|
+
dotProduct += a[i] * b[i];
|
|
21
|
+
normA += a[i] * a[i];
|
|
22
|
+
normB += b[i] * b[i];
|
|
23
|
+
}
|
|
24
|
+
normA = Math.sqrt(normA);
|
|
25
|
+
normB = Math.sqrt(normB);
|
|
26
|
+
if (normA === 0 || normB === 0) {
|
|
27
|
+
return 0;
|
|
28
|
+
}
|
|
29
|
+
return dotProduct / (normA * normB);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Calculate Euclidean distance between two vectors
|
|
33
|
+
*/
|
|
34
|
+
function euclideanDistance(a, b) {
|
|
35
|
+
if (a.length !== b.length) {
|
|
36
|
+
throw new Error('Vectors must have the same length');
|
|
37
|
+
}
|
|
38
|
+
let sum = 0;
|
|
39
|
+
for (let i = 0; i < a.length; i++) {
|
|
40
|
+
const diff = a[i] - b[i];
|
|
41
|
+
sum += diff * diff;
|
|
42
|
+
}
|
|
43
|
+
return Math.sqrt(sum);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Calculate dot product between two vectors
|
|
47
|
+
*/
|
|
48
|
+
function dotProduct(a, b) {
|
|
49
|
+
if (a.length !== b.length) {
|
|
50
|
+
throw new Error('Vectors must have the same length');
|
|
51
|
+
}
|
|
52
|
+
let sum = 0;
|
|
53
|
+
for (let i = 0; i < a.length; i++) {
|
|
54
|
+
sum += a[i] * b[i];
|
|
55
|
+
}
|
|
56
|
+
return sum;
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=similarity.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"similarity.js","sourceRoot":"","sources":["../../src/utils/similarity.ts"],"names":[],"mappings":";AAAA;;G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AAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,MAAM,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;QACzB,GAAG,IAAI,IAAI,GAAG,IAAI,CAAC;IACrB,CAAC;IAED,OAAO,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC;AAED;;GAEG;AACH,SAAgB,UAAU,CAAC,CAAW,EAAE,CAAW;IACjD,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,mCAAmC,CAAC,CAAC;IACvD,CAAC;IAED,IAAI,GAAG,GAAG,CAAC,CAAC;IACZ,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;QAClC,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;IACrB,CAAC;IAED,OAAO,GAAG,CAAC;AACb,CAAC"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ChromaDB Vector Store
|
|
3
|
+
* Lightweight, embedded vector database
|
|
4
|
+
*/
|
|
5
|
+
import { VectorStore, Document, SearchResult, QueryOptions } from '../types';
|
|
6
|
+
import { EmbeddingProvider } from '../types';
|
|
7
|
+
export interface ChromaConfig {
|
|
8
|
+
url?: string;
|
|
9
|
+
collectionName: string;
|
|
10
|
+
auth?: {
|
|
11
|
+
provider: 'token';
|
|
12
|
+
credentials: string;
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export declare class ChromaVectorStore implements VectorStore {
|
|
16
|
+
private client;
|
|
17
|
+
private collection;
|
|
18
|
+
private embeddingProvider;
|
|
19
|
+
private collectionName;
|
|
20
|
+
private initialized;
|
|
21
|
+
constructor(embeddingProvider: EmbeddingProvider, config: ChromaConfig);
|
|
22
|
+
initialize(): Promise<void>;
|
|
23
|
+
addDocuments(documents: Document[]): Promise<string[]>;
|
|
24
|
+
search(query: string, options?: QueryOptions): Promise<SearchResult[]>;
|
|
25
|
+
searchByVector(embedding: number[], options?: QueryOptions): Promise<SearchResult[]>;
|
|
26
|
+
deleteDocuments(ids: string[]): Promise<void>;
|
|
27
|
+
updateDocument(id: string, document: Partial<Document>): Promise<void>;
|
|
28
|
+
getDocument(id: string): Promise<Document | null>;
|
|
29
|
+
clear(): Promise<void>;
|
|
30
|
+
/**
|
|
31
|
+
* Get collection statistics
|
|
32
|
+
*/
|
|
33
|
+
getStats(): Promise<{
|
|
34
|
+
count: number;
|
|
35
|
+
}>;
|
|
36
|
+
/**
|
|
37
|
+
* Peek at first N documents
|
|
38
|
+
*/
|
|
39
|
+
peek(limit?: number): Promise<Document[]>;
|
|
40
|
+
private generateId;
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=chroma.store.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chroma.store.d.ts","sourceRoot":"","sources":["../../src/vector-stores/chroma.store.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,UAAU,CAAC;AAQ7C,MAAM,WAAW,YAAY;IAC3B,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,cAAc,EAAE,MAAM,CAAC;IACvB,IAAI,CAAC,EAAE;QACL,QAAQ,EAAE,OAAO,CAAC;QAClB,WAAW,EAAE,MAAM,CAAC;KACrB,CAAC;CACH;AAED,qBAAa,iBAAkB,YAAW,WAAW;IACnD,OAAO,CAAC,MAAM,CAAe;IAC7B,OAAO,CAAC,UAAU,CAA2B;IAC7C,OAAO,CAAC,iBAAiB,CAAoB;IA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