n8n-nodes-query-retriever-rerank
Version:
Advanced n8n community node for intelligent document retrieval with multi-step reasoning, reranking, and comprehensive debugging
68 lines (67 loc) • 3.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SimpleQueryStrategy = void 0;
const BaseStrategy_1 = require("./BaseStrategy");
const reranking_1 = require("../shared/reranking");
class SimpleQueryStrategy extends BaseStrategy_1.BaseStrategy {
getName() {
return 'simple_query';
}
getDescription() {
return 'Generate an answer using all retrieved documents in a single prompt';
}
async execute(input, context) {
try {
// Initialize debug manager
this.initializeDebugManager('simple_query', context.config);
const retrievalStart = Date.now();
try {
// Perform initial document retrieval
const rawDocs = await context.vectorStore.similaritySearch(input, context.config.documentsToRetrieve);
this.debugManager.setDocumentFlow({ totalRetrieved: rawDocs.length });
// Use modular reranking function
const rerankResult = await reranking_1.RerankingManager.performReranking(rawDocs, input, context.embeddings, context.config.documentsToReturn, {
strategyType: 'simple_query',
label: 'simple query'
}, context.debugging);
const docs = rerankResult.docs;
// Store debug information
if (context.debugging && rerankResult.debugInfo) {
this.debugManager.setRerankingData({
simpleQuery: rerankResult.debugInfo
});
}
this.debugManager.addTiming('documentRetrieval', retrievalStart);
this.debugManager.setDocumentFlow({ finalCount: docs.length });
// Generate answer
const answer = await this.generateAnswer(docs, input, context);
// Handle debugging
await this.handleDebugging(input, context);
// Format and return result
return this.formatResult(answer, docs, context);
}
catch (searchError) {
// Try alternative search methods if the main one fails
try {
const rawDocs = await context.vectorStore.similaritySearch(input, context.config.documentsToRetrieve, {});
// Apply same reranking logic to fallback results
const docs = rawDocs.slice(0, context.config.documentsToReturn);
const answer = await this.generateAnswer(docs, input, context);
return this.formatResult(answer, docs, context);
}
catch (altSearchError) {
const errorMessage = searchError instanceof Error ? searchError.message : String(searchError);
return {
error: `Error searching for documents: ${errorMessage}. This might be due to vector store schema mismatch. Please check that the vector store is properly configured and the data types match the expected schema.`
};
}
}
}
catch (error) {
return {
error: error instanceof Error ? error.message : String(error)
};
}
}
}
exports.SimpleQueryStrategy = SimpleQueryStrategy;