@cosmocoder/mcp-web-docs 1.0.0
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/LICENSE +22 -0
- package/README.md +368 -0
- package/build/__mocks__/embeddings.d.ts +17 -0
- package/build/__mocks__/embeddings.js +66 -0
- package/build/__mocks__/embeddings.js.map +1 -0
- package/build/config.d.ts +44 -0
- package/build/config.js +158 -0
- package/build/config.js.map +1 -0
- package/build/config.test.d.ts +1 -0
- package/build/config.test.js +165 -0
- package/build/config.test.js.map +1 -0
- package/build/crawler/auth.d.ts +128 -0
- package/build/crawler/auth.js +546 -0
- package/build/crawler/auth.js.map +1 -0
- package/build/crawler/auth.test.d.ts +1 -0
- package/build/crawler/auth.test.js +174 -0
- package/build/crawler/auth.test.js.map +1 -0
- package/build/crawler/base.d.ts +24 -0
- package/build/crawler/base.js +149 -0
- package/build/crawler/base.js.map +1 -0
- package/build/crawler/base.test.d.ts +1 -0
- package/build/crawler/base.test.js +234 -0
- package/build/crawler/base.test.js.map +1 -0
- package/build/crawler/browser-config.d.ts +2 -0
- package/build/crawler/browser-config.js +29 -0
- package/build/crawler/browser-config.js.map +1 -0
- package/build/crawler/browser-config.test.d.ts +1 -0
- package/build/crawler/browser-config.test.js +56 -0
- package/build/crawler/browser-config.test.js.map +1 -0
- package/build/crawler/cheerio.d.ts +11 -0
- package/build/crawler/cheerio.js +134 -0
- package/build/crawler/cheerio.js.map +1 -0
- package/build/crawler/chromium.d.ts +21 -0
- package/build/crawler/chromium.js +596 -0
- package/build/crawler/chromium.js.map +1 -0
- package/build/crawler/content-extractor-types.d.ts +25 -0
- package/build/crawler/content-extractor-types.js +2 -0
- package/build/crawler/content-extractor-types.js.map +1 -0
- package/build/crawler/content-extractors.d.ts +9 -0
- package/build/crawler/content-extractors.js +9 -0
- package/build/crawler/content-extractors.js.map +1 -0
- package/build/crawler/content-utils.d.ts +2 -0
- package/build/crawler/content-utils.js +22 -0
- package/build/crawler/content-utils.js.map +1 -0
- package/build/crawler/content-utils.test.d.ts +1 -0
- package/build/crawler/content-utils.test.js +99 -0
- package/build/crawler/content-utils.test.js.map +1 -0
- package/build/crawler/crawlee-crawler.d.ts +63 -0
- package/build/crawler/crawlee-crawler.js +342 -0
- package/build/crawler/crawlee-crawler.js.map +1 -0
- package/build/crawler/crawlee-crawler.test.d.ts +1 -0
- package/build/crawler/crawlee-crawler.test.js +280 -0
- package/build/crawler/crawlee-crawler.test.js.map +1 -0
- package/build/crawler/default-extractor.d.ts +4 -0
- package/build/crawler/default-extractor.js +26 -0
- package/build/crawler/default-extractor.js.map +1 -0
- package/build/crawler/default-extractor.test.d.ts +1 -0
- package/build/crawler/default-extractor.test.js +200 -0
- package/build/crawler/default-extractor.test.js.map +1 -0
- package/build/crawler/default.d.ts +11 -0
- package/build/crawler/default.js +138 -0
- package/build/crawler/default.js.map +1 -0
- package/build/crawler/docs-crawler.d.ts +26 -0
- package/build/crawler/docs-crawler.js +97 -0
- package/build/crawler/docs-crawler.js.map +1 -0
- package/build/crawler/docs-crawler.test.d.ts +1 -0
- package/build/crawler/docs-crawler.test.js +185 -0
- package/build/crawler/docs-crawler.test.js.map +1 -0
- package/build/crawler/factory.d.ts +6 -0
- package/build/crawler/factory.js +83 -0
- package/build/crawler/factory.js.map +1 -0
- package/build/crawler/github-pages-extractor.d.ts +4 -0
- package/build/crawler/github-pages-extractor.js +33 -0
- package/build/crawler/github-pages-extractor.js.map +1 -0
- package/build/crawler/github-pages-extractor.test.d.ts +1 -0
- package/build/crawler/github-pages-extractor.test.js +184 -0
- package/build/crawler/github-pages-extractor.test.js.map +1 -0
- package/build/crawler/github.d.ts +20 -0
- package/build/crawler/github.js +181 -0
- package/build/crawler/github.js.map +1 -0
- package/build/crawler/github.test.d.ts +1 -0
- package/build/crawler/github.test.js +326 -0
- package/build/crawler/github.test.js.map +1 -0
- package/build/crawler/puppeteer.d.ts +16 -0
- package/build/crawler/puppeteer.js +191 -0
- package/build/crawler/puppeteer.js.map +1 -0
- package/build/crawler/queue-manager.d.ts +43 -0
- package/build/crawler/queue-manager.js +169 -0
- package/build/crawler/queue-manager.js.map +1 -0
- package/build/crawler/queue-manager.test.d.ts +1 -0
- package/build/crawler/queue-manager.test.js +509 -0
- package/build/crawler/queue-manager.test.js.map +1 -0
- package/build/crawler/site-rules.d.ts +11 -0
- package/build/crawler/site-rules.js +104 -0
- package/build/crawler/site-rules.js.map +1 -0
- package/build/crawler/site-rules.test.d.ts +1 -0
- package/build/crawler/site-rules.test.js +139 -0
- package/build/crawler/site-rules.test.js.map +1 -0
- package/build/crawler/storybook-extractor.d.ts +34 -0
- package/build/crawler/storybook-extractor.js +767 -0
- package/build/crawler/storybook-extractor.js.map +1 -0
- package/build/crawler/storybook-extractor.test.d.ts +1 -0
- package/build/crawler/storybook-extractor.test.js +491 -0
- package/build/crawler/storybook-extractor.test.js.map +1 -0
- package/build/embeddings/fastembed.d.ts +25 -0
- package/build/embeddings/fastembed.js +188 -0
- package/build/embeddings/fastembed.js.map +1 -0
- package/build/embeddings/fastembed.test.d.ts +1 -0
- package/build/embeddings/fastembed.test.js +307 -0
- package/build/embeddings/fastembed.test.js.map +1 -0
- package/build/embeddings/openai.d.ts +8 -0
- package/build/embeddings/openai.js +56 -0
- package/build/embeddings/openai.js.map +1 -0
- package/build/embeddings/types.d.ts +4 -0
- package/build/embeddings/types.js +2 -0
- package/build/embeddings/types.js.map +1 -0
- package/build/index.d.ts +2 -0
- package/build/index.js +1007 -0
- package/build/index.js.map +1 -0
- package/build/index.test.d.ts +1 -0
- package/build/index.test.js +364 -0
- package/build/index.test.js.map +1 -0
- package/build/indexing/queue-manager.d.ts +36 -0
- package/build/indexing/queue-manager.js +86 -0
- package/build/indexing/queue-manager.js.map +1 -0
- package/build/indexing/queue-manager.test.d.ts +1 -0
- package/build/indexing/queue-manager.test.js +257 -0
- package/build/indexing/queue-manager.test.js.map +1 -0
- package/build/indexing/status.d.ts +39 -0
- package/build/indexing/status.js +207 -0
- package/build/indexing/status.js.map +1 -0
- package/build/indexing/status.test.d.ts +1 -0
- package/build/indexing/status.test.js +246 -0
- package/build/indexing/status.test.js.map +1 -0
- package/build/processor/content.d.ts +16 -0
- package/build/processor/content.js +286 -0
- package/build/processor/content.js.map +1 -0
- package/build/processor/content.test.d.ts +1 -0
- package/build/processor/content.test.js +369 -0
- package/build/processor/content.test.js.map +1 -0
- package/build/processor/markdown.d.ts +11 -0
- package/build/processor/markdown.js +256 -0
- package/build/processor/markdown.js.map +1 -0
- package/build/processor/markdown.test.d.ts +1 -0
- package/build/processor/markdown.test.js +312 -0
- package/build/processor/markdown.test.js.map +1 -0
- package/build/processor/metadata-parser.d.ts +37 -0
- package/build/processor/metadata-parser.js +245 -0
- package/build/processor/metadata-parser.js.map +1 -0
- package/build/processor/metadata-parser.test.d.ts +1 -0
- package/build/processor/metadata-parser.test.js +357 -0
- package/build/processor/metadata-parser.test.js.map +1 -0
- package/build/processor/processor.d.ts +8 -0
- package/build/processor/processor.js +190 -0
- package/build/processor/processor.js.map +1 -0
- package/build/processor/processor.test.d.ts +1 -0
- package/build/processor/processor.test.js +357 -0
- package/build/processor/processor.test.js.map +1 -0
- package/build/rag/cache.d.ts +10 -0
- package/build/rag/cache.js +10 -0
- package/build/rag/cache.js.map +1 -0
- package/build/rag/code-generator.d.ts +11 -0
- package/build/rag/code-generator.js +30 -0
- package/build/rag/code-generator.js.map +1 -0
- package/build/rag/context-assembler.d.ts +23 -0
- package/build/rag/context-assembler.js +113 -0
- package/build/rag/context-assembler.js.map +1 -0
- package/build/rag/docs-search.d.ts +55 -0
- package/build/rag/docs-search.js +380 -0
- package/build/rag/docs-search.js.map +1 -0
- package/build/rag/pipeline.d.ts +26 -0
- package/build/rag/pipeline.js +91 -0
- package/build/rag/pipeline.js.map +1 -0
- package/build/rag/query-processor.d.ts +14 -0
- package/build/rag/query-processor.js +57 -0
- package/build/rag/query-processor.js.map +1 -0
- package/build/rag/reranker.d.ts +55 -0
- package/build/rag/reranker.js +210 -0
- package/build/rag/reranker.js.map +1 -0
- package/build/rag/response-generator.d.ts +20 -0
- package/build/rag/response-generator.js +101 -0
- package/build/rag/response-generator.js.map +1 -0
- package/build/rag/retriever.d.ts +19 -0
- package/build/rag/retriever.js +111 -0
- package/build/rag/retriever.js.map +1 -0
- package/build/rag/validator.d.ts +22 -0
- package/build/rag/validator.js +128 -0
- package/build/rag/validator.js.map +1 -0
- package/build/rag/version-manager.d.ts +23 -0
- package/build/rag/version-manager.js +98 -0
- package/build/rag/version-manager.js.map +1 -0
- package/build/setupTests.d.ts +4 -0
- package/build/setupTests.js +50 -0
- package/build/setupTests.js.map +1 -0
- package/build/storage/storage.d.ts +38 -0
- package/build/storage/storage.js +700 -0
- package/build/storage/storage.js.map +1 -0
- package/build/storage/storage.test.d.ts +1 -0
- package/build/storage/storage.test.js +338 -0
- package/build/storage/storage.test.js.map +1 -0
- package/build/types/rag.d.ts +27 -0
- package/build/types/rag.js +2 -0
- package/build/types/rag.js.map +1 -0
- package/build/types.d.ts +120 -0
- package/build/types.js +2 -0
- package/build/types.js.map +1 -0
- package/build/util/content-utils.d.ts +31 -0
- package/build/util/content-utils.js +120 -0
- package/build/util/content-utils.js.map +1 -0
- package/build/util/content.d.ts +1 -0
- package/build/util/content.js +16 -0
- package/build/util/content.js.map +1 -0
- package/build/util/docs.d.ts +1 -0
- package/build/util/docs.js +26 -0
- package/build/util/docs.js.map +1 -0
- package/build/util/docs.test.d.ts +1 -0
- package/build/util/docs.test.js +49 -0
- package/build/util/docs.test.js.map +1 -0
- package/build/util/favicon.d.ts +6 -0
- package/build/util/favicon.js +88 -0
- package/build/util/favicon.js.map +1 -0
- package/build/util/favicon.test.d.ts +1 -0
- package/build/util/favicon.test.js +140 -0
- package/build/util/favicon.test.js.map +1 -0
- package/build/util/logger.d.ts +17 -0
- package/build/util/logger.js +72 -0
- package/build/util/logger.js.map +1 -0
- package/build/util/logger.test.d.ts +1 -0
- package/build/util/logger.test.js +46 -0
- package/build/util/logger.test.js.map +1 -0
- package/build/util/security.d.ts +312 -0
- package/build/util/security.js +719 -0
- package/build/util/security.js.map +1 -0
- package/build/util/security.test.d.ts +1 -0
- package/build/util/security.test.js +524 -0
- package/build/util/security.test.js.map +1 -0
- package/build/util/site-detector.d.ts +22 -0
- package/build/util/site-detector.js +42 -0
- package/build/util/site-detector.js.map +1 -0
- package/package.json +112 -0
|
@@ -0,0 +1,380 @@
|
|
|
1
|
+
import { QueryProcessor } from "./query-processor.js";
|
|
2
|
+
import { ContextRetriever } from "./retriever.js";
|
|
3
|
+
import { ContextAssembler } from "./context-assembler.js";
|
|
4
|
+
import { ResponseGenerator } from "./response-generator.js";
|
|
5
|
+
import { ResponseValidator } from "./validator.js";
|
|
6
|
+
import OpenAI from "openai";
|
|
7
|
+
export class DocsSearch {
|
|
8
|
+
queryProcessor;
|
|
9
|
+
contextRetriever;
|
|
10
|
+
contextAssembler;
|
|
11
|
+
responseGenerator;
|
|
12
|
+
responseValidator;
|
|
13
|
+
store;
|
|
14
|
+
openai; // OpenAI client
|
|
15
|
+
constructor(openaiApiKey, store, embeddings) {
|
|
16
|
+
this.store = store;
|
|
17
|
+
this.queryProcessor = new QueryProcessor(openaiApiKey, embeddings);
|
|
18
|
+
this.contextRetriever = new ContextRetriever(store, embeddings, openaiApiKey);
|
|
19
|
+
this.contextAssembler = new ContextAssembler();
|
|
20
|
+
this.responseGenerator = new ResponseGenerator(openaiApiKey);
|
|
21
|
+
this.responseValidator = new ResponseValidator(openaiApiKey);
|
|
22
|
+
// Initialize OpenAI client for query expansion
|
|
23
|
+
this.openai = new OpenAI({ apiKey: openaiApiKey });
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Generate alternative phrasings of the query to improve search recall
|
|
27
|
+
* @param query The original query
|
|
28
|
+
* @param componentNames Optional component names to include in variations
|
|
29
|
+
* @param propNames Optional prop names to include in variations
|
|
30
|
+
* @returns Array of query variations
|
|
31
|
+
*/
|
|
32
|
+
async expandQuery(query, componentNames, propNames) {
|
|
33
|
+
try {
|
|
34
|
+
console.debug(`[DocsSearch] Expanding query: "${query}"`);
|
|
35
|
+
// Create a prompt that instructs the model to generate alternative phrasings
|
|
36
|
+
const prompt = `Generate 3-5 alternative phrasings of the following query to improve search recall.
|
|
37
|
+
Each phrasing should capture the same intent but use different wording, terminology, or structure.
|
|
38
|
+
|
|
39
|
+
Original query: "${query}"
|
|
40
|
+
|
|
41
|
+
${componentNames && componentNames.length > 0
|
|
42
|
+
? `Important component names to include: ${componentNames.join(', ')}`
|
|
43
|
+
: ''}
|
|
44
|
+
${propNames && propNames.length > 0
|
|
45
|
+
? `Important prop names to include: ${propNames.join(', ')}`
|
|
46
|
+
: ''}
|
|
47
|
+
|
|
48
|
+
Return only the alternative phrasings, one per line, without numbering or additional text.`;
|
|
49
|
+
// Call the OpenAI API to generate variations
|
|
50
|
+
const response = await this.openai.chat.completions.create({
|
|
51
|
+
model: "gpt-3.5-turbo",
|
|
52
|
+
messages: [{ role: "user", content: prompt }],
|
|
53
|
+
temperature: 0.7,
|
|
54
|
+
max_tokens: 300
|
|
55
|
+
});
|
|
56
|
+
// Extract the variations from the response
|
|
57
|
+
const content = response.choices[0].message.content;
|
|
58
|
+
if (!content) {
|
|
59
|
+
return [query]; // Return original query if no content
|
|
60
|
+
}
|
|
61
|
+
// Split the content by newlines and filter out empty lines
|
|
62
|
+
const variations = content
|
|
63
|
+
.split('\n')
|
|
64
|
+
.map((line) => line.trim())
|
|
65
|
+
.filter((line) => line.length > 0);
|
|
66
|
+
// Add the original query to the variations
|
|
67
|
+
const allVariations = [query, ...variations];
|
|
68
|
+
console.debug(`[DocsSearch] Generated ${variations.length} query variations`);
|
|
69
|
+
return allVariations;
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
console.error('[DocsSearch] Error expanding query:', error);
|
|
73
|
+
return [query]; // Return original query on error
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Search across multiple URLs and merge the results
|
|
78
|
+
* @param queryIntent The query intent
|
|
79
|
+
* @param urls Array of URLs to search
|
|
80
|
+
* @param options Retrieval options
|
|
81
|
+
* @returns Combined retrieval result
|
|
82
|
+
*/
|
|
83
|
+
async searchAcrossUrls(queryIntent, urls, options) {
|
|
84
|
+
console.debug(`[DocsSearch] Searching across ${urls.length} URLs`);
|
|
85
|
+
// Initialize combined results
|
|
86
|
+
const allChunks = [];
|
|
87
|
+
const allScores = [];
|
|
88
|
+
const seenIds = new Set();
|
|
89
|
+
// Search each URL
|
|
90
|
+
for (const url of urls) {
|
|
91
|
+
try {
|
|
92
|
+
// Clone options and set URL filter
|
|
93
|
+
const urlOptions = {
|
|
94
|
+
...options,
|
|
95
|
+
filterByUrl: url
|
|
96
|
+
};
|
|
97
|
+
// Retrieve context for this URL
|
|
98
|
+
const urlRetrieval = await this.contextRetriever.retrieveContext(queryIntent, urlOptions);
|
|
99
|
+
// Add unique chunks to combined results
|
|
100
|
+
for (let i = 0; i < urlRetrieval.chunks.length; i++) {
|
|
101
|
+
const chunk = urlRetrieval.chunks[i];
|
|
102
|
+
if (!seenIds.has(chunk.id)) {
|
|
103
|
+
allChunks.push(chunk);
|
|
104
|
+
allScores.push(urlRetrieval.relevanceScores[i]);
|
|
105
|
+
seenIds.add(chunk.id);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
console.debug(`[DocsSearch] Found ${urlRetrieval.chunks.length} chunks for URL: ${url}`);
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
console.error(`[DocsSearch] Error searching URL ${url}:`, error);
|
|
112
|
+
// Continue with other URLs
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
// Sort combined results by score
|
|
116
|
+
const combinedResults = allChunks.map((chunk, index) => ({
|
|
117
|
+
chunk,
|
|
118
|
+
score: allScores[index]
|
|
119
|
+
}));
|
|
120
|
+
combinedResults.sort((a, b) => b.score - a.score);
|
|
121
|
+
// Take top results
|
|
122
|
+
const limit = options.nFinal || 30;
|
|
123
|
+
const topResults = combinedResults.slice(0, limit);
|
|
124
|
+
console.debug(`[DocsSearch] Combined results: ${allChunks.length}, top results: ${topResults.length}`);
|
|
125
|
+
return {
|
|
126
|
+
chunks: topResults.map(r => r.chunk),
|
|
127
|
+
relevanceScores: topResults.map(r => r.score)
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
async search(query, options) {
|
|
131
|
+
console.debug(`[DocsSearch] Starting search for query: "${query}"`, {
|
|
132
|
+
urls: options.urls,
|
|
133
|
+
componentNames: options.componentNames,
|
|
134
|
+
propNames: options.propNames,
|
|
135
|
+
strictMode: options.strictMode,
|
|
136
|
+
advanced: options.advanced
|
|
137
|
+
});
|
|
138
|
+
try {
|
|
139
|
+
// Set up advanced options with defaults
|
|
140
|
+
const advanced = options.advanced || {};
|
|
141
|
+
const useQueryExpansion = advanced.useQueryExpansion ?? true;
|
|
142
|
+
const useMultiQuery = advanced.useMultiQuery ?? true;
|
|
143
|
+
const maxQueryVariations = advanced.maxQueryVariations ?? 3;
|
|
144
|
+
const mergeUrlResults = advanced.mergeUrlResults ?? (options.urls.length > 1);
|
|
145
|
+
// Process query and determine intent
|
|
146
|
+
const queryIntent = await this.queryProcessor.processQuery(query);
|
|
147
|
+
console.debug('[DocsSearch] Query intent classified:', queryIntent);
|
|
148
|
+
// Add component names and prop names from options
|
|
149
|
+
if (options.componentNames) {
|
|
150
|
+
queryIntent.componentNames = options.componentNames;
|
|
151
|
+
}
|
|
152
|
+
if (options.propNames) {
|
|
153
|
+
queryIntent.propNames = options.propNames;
|
|
154
|
+
}
|
|
155
|
+
// Set up retrieval options
|
|
156
|
+
const retrievalOptions = {
|
|
157
|
+
limit: 40, // Doubled for more comprehensive results
|
|
158
|
+
minScore: 0.6, // Further lowered threshold for better recall
|
|
159
|
+
filterByIntent: false, // Disable intent filtering for diverse results
|
|
160
|
+
nRetrieve: 100, // Significantly increased for better coverage
|
|
161
|
+
nFinal: 50, // Increased final results count
|
|
162
|
+
useReranking: true,
|
|
163
|
+
rerankerModel: 'openai',
|
|
164
|
+
// Enable advanced retrieval features
|
|
165
|
+
useMultiStageRetrieval: true, // Use multi-stage retrieval for comprehensive results
|
|
166
|
+
expandRelatedComponents: true // Discover and include related components
|
|
167
|
+
};
|
|
168
|
+
let retrieval;
|
|
169
|
+
// Determine search strategy based on options
|
|
170
|
+
if (useQueryExpansion && useMultiQuery) {
|
|
171
|
+
// Multi-query approach with query expansion
|
|
172
|
+
console.debug('[DocsSearch] Using multi-query approach with query expansion');
|
|
173
|
+
// Generate query variations
|
|
174
|
+
const queryVariations = await this.expandQuery(query, options.componentNames, options.propNames);
|
|
175
|
+
// Limit the number of variations to use
|
|
176
|
+
const limitedVariations = queryVariations.slice(0, maxQueryVariations);
|
|
177
|
+
console.debug(`[DocsSearch] Using ${limitedVariations.length} query variations`);
|
|
178
|
+
// Process each variation to get intents
|
|
179
|
+
const intents = [];
|
|
180
|
+
for (const variation of limitedVariations) {
|
|
181
|
+
const intent = await this.queryProcessor.processQuery(variation);
|
|
182
|
+
// Add component and prop names
|
|
183
|
+
if (options.componentNames) {
|
|
184
|
+
intent.componentNames = options.componentNames;
|
|
185
|
+
}
|
|
186
|
+
if (options.propNames) {
|
|
187
|
+
intent.propNames = options.propNames;
|
|
188
|
+
}
|
|
189
|
+
intents.push(intent);
|
|
190
|
+
}
|
|
191
|
+
// Combine results from all variations
|
|
192
|
+
const allChunks = [];
|
|
193
|
+
const allScores = [];
|
|
194
|
+
const seenIds = new Set();
|
|
195
|
+
// Process each intent
|
|
196
|
+
for (const intent of intents) {
|
|
197
|
+
let intentResults;
|
|
198
|
+
if (mergeUrlResults && options.urls.length > 1) {
|
|
199
|
+
// Search across all URLs and merge results
|
|
200
|
+
intentResults = await this.searchAcrossUrls(intent, options.urls, retrievalOptions);
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
// Use single URL filter if only one URL
|
|
204
|
+
const urlOptions = {
|
|
205
|
+
...retrievalOptions,
|
|
206
|
+
filterByUrl: options.urls.length === 1 ? options.urls[0] : undefined
|
|
207
|
+
};
|
|
208
|
+
intentResults = await this.contextRetriever.retrieveContext(intent, urlOptions);
|
|
209
|
+
}
|
|
210
|
+
// Add unique chunks to combined results
|
|
211
|
+
for (let i = 0; i < intentResults.chunks.length; i++) {
|
|
212
|
+
const chunk = intentResults.chunks[i];
|
|
213
|
+
if (!seenIds.has(chunk.id)) {
|
|
214
|
+
allChunks.push(chunk);
|
|
215
|
+
allScores.push(intentResults.relevanceScores[i]);
|
|
216
|
+
seenIds.add(chunk.id);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// Sort combined results by score
|
|
221
|
+
const combinedResults = allChunks.map((chunk, index) => ({
|
|
222
|
+
chunk,
|
|
223
|
+
score: allScores[index]
|
|
224
|
+
}));
|
|
225
|
+
combinedResults.sort((a, b) => b.score - a.score);
|
|
226
|
+
// Take top results
|
|
227
|
+
const limit = retrievalOptions.nFinal || 50;
|
|
228
|
+
const topResults = combinedResults.slice(0, limit);
|
|
229
|
+
retrieval = {
|
|
230
|
+
chunks: topResults.map(r => r.chunk),
|
|
231
|
+
relevanceScores: topResults.map(r => r.score)
|
|
232
|
+
};
|
|
233
|
+
console.debug(`[DocsSearch] Multi-query approach found ${retrieval.chunks.length} unique chunks`);
|
|
234
|
+
}
|
|
235
|
+
else {
|
|
236
|
+
// Standard single-query approach
|
|
237
|
+
if (mergeUrlResults && options.urls.length > 1) {
|
|
238
|
+
// Search across all URLs and merge results
|
|
239
|
+
retrieval = await this.searchAcrossUrls(queryIntent, options.urls, retrievalOptions);
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
// Use single URL filter if only one URL
|
|
243
|
+
retrievalOptions.filterByUrl = options.urls.length === 1 ? options.urls[0] : undefined;
|
|
244
|
+
retrieval = await this.contextRetriever.retrieveContext(queryIntent, retrievalOptions);
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
console.debug('[DocsSearch] Retrieved context chunks:', retrieval.chunks.length);
|
|
248
|
+
// Get raw search results for source attribution (using original query for consistency)
|
|
249
|
+
const rawResults = [];
|
|
250
|
+
for (const url of options.urls) {
|
|
251
|
+
try {
|
|
252
|
+
const results = await this.store.searchByText(query, {
|
|
253
|
+
limit: options.maxSourceChunks || 15,
|
|
254
|
+
filterByUrl: url
|
|
255
|
+
});
|
|
256
|
+
rawResults.push(...results);
|
|
257
|
+
}
|
|
258
|
+
catch (error) {
|
|
259
|
+
console.error(`[DocsSearch] Error searching text for URL ${url}:`, error);
|
|
260
|
+
// Continue with other URLs
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
console.debug('[DocsSearch] Retrieved raw search results:', rawResults.length);
|
|
264
|
+
// Assemble context with enhanced chunks from our improved retrieval
|
|
265
|
+
const assembled = await this.contextAssembler.assembleContext(retrieval.chunks);
|
|
266
|
+
console.debug('[DocsSearch] Assembled context with relationships from enhanced retrieval');
|
|
267
|
+
// Generate response
|
|
268
|
+
const generatedResponse = await this.responseGenerator.generateResponse(assembled, queryIntent);
|
|
269
|
+
console.debug('[DocsSearch] Generated response');
|
|
270
|
+
// Validate response
|
|
271
|
+
const validation = await this.responseValidator.validateResponse(generatedResponse, assembled);
|
|
272
|
+
console.debug('[DocsSearch] Validated response:', validation);
|
|
273
|
+
// Skip source attributions since they're not included in the response anymore
|
|
274
|
+
console.debug('[DocsSearch] Skipping source attributions since they\'re not included in the response');
|
|
275
|
+
// Identify limitations with advanced search information
|
|
276
|
+
const limitations = this.identifyLimitations(generatedResponse, validation, assembled, options.strictMode || false, options.advanced);
|
|
277
|
+
// If strict mode is enabled and validation failed, reject the response
|
|
278
|
+
if (options.strictMode &&
|
|
279
|
+
(!validation.factCheck || !validation.consistencyCheck || !validation.codeCheck)) {
|
|
280
|
+
console.debug('[DocsSearch] Strict mode enabled and validation failed, rejecting response');
|
|
281
|
+
// Collect all validation issues to explain why the response was rejected
|
|
282
|
+
const validationIssues = [
|
|
283
|
+
...(validation.details.factCheckDetails || []),
|
|
284
|
+
...(validation.details.consistencyCheckDetails || []),
|
|
285
|
+
...(validation.details.codeCheckDetails || [])
|
|
286
|
+
];
|
|
287
|
+
// Create a more informative rejection message that includes the specific issues
|
|
288
|
+
let rejectionMessage = "I cannot provide a reliable answer based on the documentation. The generated response contains information that is not directly supported by the documentation sources.";
|
|
289
|
+
if (validationIssues.length > 0) {
|
|
290
|
+
rejectionMessage += "\n\nSpecific issues identified:\n";
|
|
291
|
+
validationIssues.slice(0, 3).forEach(issue => {
|
|
292
|
+
rejectionMessage += `- ${issue}\n`;
|
|
293
|
+
});
|
|
294
|
+
if (validationIssues.length > 3) {
|
|
295
|
+
rejectionMessage += `- Plus ${validationIssues.length - 3} more issues\n`;
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
return {
|
|
299
|
+
answer: rejectionMessage,
|
|
300
|
+
codeExamples: [],
|
|
301
|
+
componentDetails: [],
|
|
302
|
+
validation: {
|
|
303
|
+
factCheck: validation.factCheck,
|
|
304
|
+
codeCheck: validation.codeCheck,
|
|
305
|
+
consistencyCheck: validation.consistencyCheck,
|
|
306
|
+
details: validationIssues
|
|
307
|
+
},
|
|
308
|
+
limitations: [
|
|
309
|
+
"The documentation does not contain sufficient information to answer this query accurately.",
|
|
310
|
+
"Consider rephrasing your query to focus on information that is explicitly documented."
|
|
311
|
+
]
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
// Return the combined result without source chunks to reduce noise
|
|
315
|
+
return {
|
|
316
|
+
answer: generatedResponse.text,
|
|
317
|
+
codeExamples: generatedResponse.codeExamples || [],
|
|
318
|
+
componentDetails: Object.entries(generatedResponse.componentDetails || {}).map(([name, details]) => ({
|
|
319
|
+
name,
|
|
320
|
+
...details
|
|
321
|
+
})),
|
|
322
|
+
validation: {
|
|
323
|
+
factCheck: validation.factCheck,
|
|
324
|
+
codeCheck: validation.codeCheck,
|
|
325
|
+
consistencyCheck: validation.consistencyCheck,
|
|
326
|
+
details: [
|
|
327
|
+
...(validation.details.factCheckDetails || []),
|
|
328
|
+
...(validation.details.codeCheckDetails || []),
|
|
329
|
+
...(validation.details.consistencyCheckDetails || [])
|
|
330
|
+
]
|
|
331
|
+
},
|
|
332
|
+
limitations
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
catch (error) {
|
|
336
|
+
console.error('[DocsSearch] Error during search:', error);
|
|
337
|
+
throw error;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
// Source attribution methods have been removed since they're not needed anymore
|
|
341
|
+
identifyLimitations(response, validation, context, strictMode, advanced) {
|
|
342
|
+
const limitations = [];
|
|
343
|
+
// Check if validation failed
|
|
344
|
+
if (!validation.factCheck) {
|
|
345
|
+
limitations.push("Some information may not be directly supported by the documentation.");
|
|
346
|
+
}
|
|
347
|
+
if (!validation.consistencyCheck) {
|
|
348
|
+
limitations.push("There may be inconsistencies between the response and the documentation.");
|
|
349
|
+
}
|
|
350
|
+
if (!validation.codeCheck) {
|
|
351
|
+
limitations.push("The code examples may contain syntax errors or inconsistencies.");
|
|
352
|
+
}
|
|
353
|
+
// Check if context is limited
|
|
354
|
+
if (context.hierarchicalContext.length < 5) {
|
|
355
|
+
limitations.push("Limited documentation context was available for this query.");
|
|
356
|
+
}
|
|
357
|
+
// Check if code examples are missing
|
|
358
|
+
if (response.codeExamples?.length === 0) {
|
|
359
|
+
limitations.push("No code examples were found in the documentation for this query.");
|
|
360
|
+
}
|
|
361
|
+
// Add a note about strict mode
|
|
362
|
+
if (strictMode) {
|
|
363
|
+
limitations.push("Strict mode is enabled: only information directly supported by the documentation is included.");
|
|
364
|
+
}
|
|
365
|
+
// Add information about advanced search features
|
|
366
|
+
if (advanced) {
|
|
367
|
+
if (advanced.useQueryExpansion && advanced.useMultiQuery) {
|
|
368
|
+
limitations.push("Enhanced search with query expansion and multi-query approach was used to improve results.");
|
|
369
|
+
}
|
|
370
|
+
else if (advanced.useQueryExpansion) {
|
|
371
|
+
limitations.push("Query expansion was used to improve search results.");
|
|
372
|
+
}
|
|
373
|
+
if (advanced.mergeUrlResults && context.hierarchicalContext.length > 10) {
|
|
374
|
+
limitations.push("Results from multiple documentation sources were combined to provide a more comprehensive answer.");
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
return limitations;
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
//# sourceMappingURL=docs-search.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"docs-search.js","sourceRoot":"","sources":["../../src/rag/docs-search.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAe,MAAM,sBAAsB,CAAC;AAGnE,OAAO,EAAE,gBAAgB,EAAqC,MAAM,gBAAgB,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAoB,MAAM,wBAAwB,CAAC;AAC5E,OAAO,EAAE,iBAAiB,EAAqB,MAAM,yBAAyB,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAoB,MAAM,gBAAgB,CAAC;AAGrE,OAAO,MAAM,MAAM,QAAQ,CAAC;AAoD5B,MAAM,OAAO,UAAU;IACb,cAAc,CAAiB;IAC/B,gBAAgB,CAAmB;IACnC,gBAAgB,CAAmB;IACnC,iBAAiB,CAAoB;IACrC,iBAAiB,CAAoB;IACrC,KAAK,CAAgB;IACrB,MAAM,CAAM,CAAC,gBAAgB;IAErC,YACE,YAAoB,EACpB,KAAoB,EACpB,UAA8B;QAE9B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,cAAc,GAAG,IAAI,cAAc,CAAC,YAAY,EAAE,UAAU,CAAC,CAAC;QACnE,IAAI,CAAC,gBAAgB,GAAG,IAAI,gBAAgB,CAAC,KAAK,EAAE,UAAU,EAAE,YAAY,CAAC,CAAC;QAC9E,IAAI,CAAC,gBAAgB,GAAG,IAAI,gBAAgB,EAAE,CAAC;QAC/C,IAAI,CAAC,iBAAiB,GAAG,IAAI,iBAAiB,CAAC,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CAAC;YACf,WAAW,CAAC,IAAI,CAAC,+FAA+F,CAAC,CAAC;QACpH,CAAC;QAED,iDAAiD;QACjD,IAAI,QAAQ,EAAE,CAAC;YACb,IAAI,QAAQ,CAAC,iBAAiB,IAAI,QAAQ,CAAC,aAAa,EAAE,CAAC;gBACzD,WAAW,CAAC,IAAI,CAAC,4FAA4F,CAAC,CAAC;YACjH,CAAC;iBAAM,IAAI,QAAQ,CAAC,iBAAiB,EAAE,CAAC;gBACtC,WAAW,CAAC,IAAI,CAAC,qDAAqD,CAAC,CAAC;YAC1E,CAAC;YAED,IAAI,QAAQ,CAAC,eAAe,IAAI,OAAO,CAAC,mBAAmB,CAAC,MAAM,GAAG,EAAE,EAAE,CAAC;gBACxE,WAAW,CAAC,IAAI,CAAC,mGAAmG,CAAC,CAAC;YACxH,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;CACF"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { GeneratedResponse } from "./response-generator.js";
|
|
2
|
+
import { ValidationResult } from "./validator.js";
|
|
3
|
+
export interface RAGResponse {
|
|
4
|
+
response: GeneratedResponse;
|
|
5
|
+
validation: ValidationResult;
|
|
6
|
+
}
|
|
7
|
+
export interface RAGConfig {
|
|
8
|
+
openaiApiKey: string;
|
|
9
|
+
dbPath: string;
|
|
10
|
+
vectorDbPath: string;
|
|
11
|
+
maxCacheSize?: number;
|
|
12
|
+
}
|
|
13
|
+
export declare class RAGPipeline {
|
|
14
|
+
private queryProcessor;
|
|
15
|
+
private contextRetriever;
|
|
16
|
+
private contextAssembler;
|
|
17
|
+
private responseGenerator;
|
|
18
|
+
private responseValidator;
|
|
19
|
+
private versionManager;
|
|
20
|
+
private cache;
|
|
21
|
+
private store;
|
|
22
|
+
private embeddings;
|
|
23
|
+
constructor(config: RAGConfig);
|
|
24
|
+
initialize(): Promise<void>;
|
|
25
|
+
process(query: string): Promise<RAGResponse>;
|
|
26
|
+
}
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { QueryProcessor } from "./query-processor.js";
|
|
2
|
+
import { DocumentStore } from "../storage/storage.js";
|
|
3
|
+
import { OpenAIEmbeddings } from "../embeddings/openai.js";
|
|
4
|
+
import { ContextRetriever } from "./retriever.js";
|
|
5
|
+
import { ContextAssembler } from "./context-assembler.js";
|
|
6
|
+
import { ResponseGenerator } from "./response-generator.js";
|
|
7
|
+
import { ResponseValidator } from "./validator.js";
|
|
8
|
+
import { VersionManager } from "./version-manager.js";
|
|
9
|
+
import { RAGCache } from "./cache.js";
|
|
10
|
+
import { logger } from "../util/logger.js";
|
|
11
|
+
export class RAGPipeline {
|
|
12
|
+
queryProcessor;
|
|
13
|
+
contextRetriever;
|
|
14
|
+
contextAssembler;
|
|
15
|
+
responseGenerator;
|
|
16
|
+
responseValidator;
|
|
17
|
+
versionManager;
|
|
18
|
+
cache;
|
|
19
|
+
store;
|
|
20
|
+
embeddings;
|
|
21
|
+
constructor(config) {
|
|
22
|
+
// Initialize embeddings provider
|
|
23
|
+
this.embeddings = new OpenAIEmbeddings(config.openaiApiKey);
|
|
24
|
+
// Initialize storage
|
|
25
|
+
this.store = new DocumentStore(config.dbPath, config.vectorDbPath, this.embeddings, config.maxCacheSize);
|
|
26
|
+
// Initialize RAG components
|
|
27
|
+
this.queryProcessor = new QueryProcessor(config.openaiApiKey, this.embeddings);
|
|
28
|
+
this.contextRetriever = new ContextRetriever(this.store, this.embeddings);
|
|
29
|
+
this.contextAssembler = new ContextAssembler();
|
|
30
|
+
this.responseGenerator = new ResponseGenerator(config.openaiApiKey);
|
|
31
|
+
this.responseValidator = new ResponseValidator(config.openaiApiKey);
|
|
32
|
+
this.versionManager = new VersionManager();
|
|
33
|
+
this.cache = new RAGCache();
|
|
34
|
+
}
|
|
35
|
+
async initialize() {
|
|
36
|
+
await this.store.initialize();
|
|
37
|
+
}
|
|
38
|
+
async process(query) {
|
|
39
|
+
// Check cache first
|
|
40
|
+
const cached = await this.cache.getCachedResponse(query);
|
|
41
|
+
if (cached) {
|
|
42
|
+
return {
|
|
43
|
+
response: cached.response,
|
|
44
|
+
validation: {
|
|
45
|
+
factCheck: true,
|
|
46
|
+
codeCheck: true,
|
|
47
|
+
consistencyCheck: true,
|
|
48
|
+
details: {
|
|
49
|
+
factCheckDetails: [],
|
|
50
|
+
codeCheckDetails: [],
|
|
51
|
+
consistencyCheckDetails: []
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
try {
|
|
57
|
+
// Process query and determine intent
|
|
58
|
+
const queryIntent = await this.queryProcessor.processQuery(query);
|
|
59
|
+
logger.debug('Query intent classified:', queryIntent);
|
|
60
|
+
// Retrieve relevant context
|
|
61
|
+
const retrieval = await this.contextRetriever.retrieveContext(queryIntent, {
|
|
62
|
+
limit: 10,
|
|
63
|
+
minScore: 0.7,
|
|
64
|
+
filterByIntent: true
|
|
65
|
+
});
|
|
66
|
+
logger.debug('Retrieved context chunks:', retrieval.chunks.length);
|
|
67
|
+
// Assemble context
|
|
68
|
+
const assembled = await this.contextAssembler.assembleContext(retrieval.chunks);
|
|
69
|
+
logger.debug('Assembled context with relationships');
|
|
70
|
+
// Apply version awareness
|
|
71
|
+
await this.versionManager.getVersionedContext(assembled, "1.0");
|
|
72
|
+
logger.debug('Applied version context');
|
|
73
|
+
// Generate response
|
|
74
|
+
const generatedResponse = await this.responseGenerator.generateResponse(assembled, queryIntent);
|
|
75
|
+
logger.debug('Generated response');
|
|
76
|
+
// Validate response
|
|
77
|
+
const validation = await this.responseValidator.validateResponse(generatedResponse, assembled);
|
|
78
|
+
logger.debug('Validated response:', validation);
|
|
79
|
+
// Cache if validation passed
|
|
80
|
+
if (validation.factCheck && validation.codeCheck && validation.consistencyCheck) {
|
|
81
|
+
await this.cache.cacheResponse(query, generatedResponse);
|
|
82
|
+
}
|
|
83
|
+
return { response: generatedResponse, validation };
|
|
84
|
+
}
|
|
85
|
+
catch (error) {
|
|
86
|
+
logger.debug('Error processing RAG pipeline:', error);
|
|
87
|
+
throw error;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
//# sourceMappingURL=pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pipeline.js","sourceRoot":"","sources":["../../src/rag/pipeline.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAEtD,OAAO,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAC3D,OAAO,EAAE,gBAAgB,EAAE,MAAM,gBAAgB,CAAC;AAClD,OAAO,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAC1D,OAAO,EAAE,iBAAiB,EAAqB,MAAM,yBAAyB,CAAC;AAC/E,OAAO,EAAE,iBAAiB,EAAoB,MAAM,gBAAgB,CAAC;AACrE,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AACtC,OAAO,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAc3C,MAAM,OAAO,WAAW;IACd,cAAc,CAAiB;IAC/B,gBAAgB,CAAmB;IACnC,gBAAgB,CAAmB;IACnC,iBAAiB,CAAoB;IACrC,iBAAiB,CAAoB;IACrC,cAAc,CAAiB;IAC/B,KAAK,CAAW;IAChB,KAAK,CAAgB;IACrB,UAAU,CAAqB;IAEvC,YAAY,MAAiB;QAC3B,iCAAiC;QACjC,IAAI,CAAC,UAAU,GAAG,IAAI,gBAAgB,CAAC,MAAM,CAAC,YAAY,CAAC,CAAC;QAE5D,qBAAqB;QACrB,IAAI,CAAC,KAAK,GAAG,IAAI,aAAa,CAC5B,MAAM,CAAC,MAAM,EACb,MAAM,CAAC,YAAY,EACnB,IAAI,CAAC,UAAU,EACf,MAAM,CAAC,YAAY,CACpB,C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AE,iBAAiB,EAAE,UAAU,EAAE,CAAC;QACrD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,MAAM,CAAC,KAAK,CAAC,gCAAgC,EAAE,KAAK,CAAC,CAAC;YACtD,MAAM,KAAK,CAAC;QACd,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { EmbeddingsProvider } from "../embeddings/types.js";
|
|
2
|
+
export type QueryIntentType = 'overview' | 'api' | 'example' | 'usage' | 'component_usage' | 'concept' | 'troubleshooting' | 'general';
|
|
3
|
+
export interface QueryIntent {
|
|
4
|
+
embedding: number[];
|
|
5
|
+
intent: QueryIntentType;
|
|
6
|
+
entities: string[];
|
|
7
|
+
confidence: number;
|
|
8
|
+
}
|
|
9
|
+
export declare class QueryProcessor {
|
|
10
|
+
private openai;
|
|
11
|
+
private embeddings;
|
|
12
|
+
constructor(apiKey: string, embeddings: EmbeddingsProvider);
|
|
13
|
+
processQuery(query: string): Promise<QueryIntent>;
|
|
14
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import OpenAI from "openai";
|
|
2
|
+
export class QueryProcessor {
|
|
3
|
+
openai;
|
|
4
|
+
embeddings;
|
|
5
|
+
constructor(apiKey, embeddings) {
|
|
6
|
+
this.openai = new OpenAI({ apiKey });
|
|
7
|
+
this.embeddings = embeddings;
|
|
8
|
+
}
|
|
9
|
+
async processQuery(query) {
|
|
10
|
+
// Generate embedding for semantic search
|
|
11
|
+
const embedding = await this.embeddings.embed(query);
|
|
12
|
+
// Classify intent using OpenAI
|
|
13
|
+
const completion = await this.openai.chat.completions.create({
|
|
14
|
+
model: "gpt-3.5-turbo",
|
|
15
|
+
messages: [
|
|
16
|
+
{
|
|
17
|
+
role: "system",
|
|
18
|
+
content: `You are a query intent classifier for a documentation search system.
|
|
19
|
+
|
|
20
|
+
Your task is to analyze the query and return a JSON response with the following fields:
|
|
21
|
+
{
|
|
22
|
+
"intent": "one of: overview, api, example, usage, component_usage, concept, troubleshooting, general",
|
|
23
|
+
"entities": ["array of extracted entities like component names, function names, concepts"],
|
|
24
|
+
"confidence": "number between 0 and 1"
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
Intent categories:
|
|
28
|
+
- overview: General overview or introduction to a topic
|
|
29
|
+
- api: API documentation and reference
|
|
30
|
+
- example: Code examples and demonstrations
|
|
31
|
+
- usage: How to use features or components
|
|
32
|
+
- component_usage: Specific component usage (alternative to usage)
|
|
33
|
+
- concept: Conceptual explanations and principles
|
|
34
|
+
- troubleshooting: Error resolution and debugging
|
|
35
|
+
- general: General queries that don't fit other categories`
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
role: "user",
|
|
39
|
+
content: query
|
|
40
|
+
}
|
|
41
|
+
],
|
|
42
|
+
response_format: { type: "json_object" }
|
|
43
|
+
});
|
|
44
|
+
const content = completion.choices[0].message.content;
|
|
45
|
+
if (!content) {
|
|
46
|
+
throw new Error("No content returned from OpenAI");
|
|
47
|
+
}
|
|
48
|
+
const result = JSON.parse(content);
|
|
49
|
+
return {
|
|
50
|
+
embedding,
|
|
51
|
+
intent: result.intent,
|
|
52
|
+
entities: result.entities,
|
|
53
|
+
confidence: result.confidence
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
//# sourceMappingURL=query-processor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-processor.js","sourceRoot":"","sources":["../../src/rag/query-processor.ts"],"names":[],"mappings":"AACA,OAAO,MAAM,MAAM,QAAQ,CAAC;AAmB5B,MAAM,OAAO,cAAc;IACjB,MAAM,CAAS;IACf,UAAU,CAAqB;IAEvC,YAAY,MAAc,EAAE,UAA8B;QACxD,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,CAAC,CAAC;QACrC,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAED,KAAK,CAAC,YAAY,CA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