Update metaSearchAgent.ts

rag optimise
This commit is contained in:
Lucas 2025-01-07 08:25:42 +01:00 committed by GitHub
parent 19148eeba7
commit dda24ad349
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -297,7 +297,7 @@ export class MetaSearchAgent implements MetaSearchAgentType {
console.log('📚 Documents uploadés chargés:', uploadedDocs.length); console.log('📚 Documents uploadés chargés:', uploadedDocs.length);
// Utiliser RAGDocumentChain pour la recherche dans les documents // Utiliser RAGDocumentChain pour la recherche dans les documents
const ragChain = new RAGDocumentChain(); const ragChain = RAGDocumentChain.getInstance();
await ragChain.initializeVectorStoreFromDocuments(uploadedDocs, embeddings); await ragChain.initializeVectorStoreFromDocuments(uploadedDocs, embeddings);
// Utiliser le type 'specific' pour une recherche précise // Utiliser le type 'specific' pour une recherche précise
@ -808,16 +808,20 @@ Prends en compte:
console.log('📚 Documents uploadés chargés:', uploadedDocs.length); console.log('📚 Documents uploadés chargés:', uploadedDocs.length);
if (uploadedDocs.length > 0) { if (uploadedDocs.length > 0) {
// Création du vectorStore temporaire pour les documents try {
const vectorStore = await Chroma.fromDocuments(uploadedDocs, embeddings, { // Utiliser l'instance unique de RAGDocumentChain
collectionName: 'temp_docs', const ragChain = RAGDocumentChain.getInstance();
url: 'http://chroma:8000',
numDimensions: 1536
});
// Recherche sémantique sans filtre pour l'instant // Vérifier si déjà initialisé avec les mêmes documents
const relevantDocs = await vectorStore.similaritySearch(message, 5); if (!ragChain.isInitialized()) {
console.log('🔄 Initialisation du vector store...');
await ragChain.initializeVectorStoreFromDocuments(uploadedDocs, embeddings);
} else {
console.log('✅ Vector store déjà initialisé');
}
// Recherche sémantique
const relevantDocs = await ragChain.searchSimilarDocuments(message, 5);
console.log('📄 Documents pertinents trouvés:', relevantDocs.length); console.log('📄 Documents pertinents trouvés:', relevantDocs.length);
// Extraction du contexte pour enrichir la recherche // Extraction du contexte pour enrichir la recherche
@ -829,11 +833,17 @@ Prends en compte:
const documentTitle = uploadedDocs[0]?.metadata?.title || ''; const documentTitle = uploadedDocs[0]?.metadata?.title || '';
const enrichedQuery = `${message} ${documentTitle} ${documentContext}`; const enrichedQuery = `${message} ${documentTitle} ${documentContext}`;
// 2. Recherche d'experts en BDD // 2. Recherche d'experts si nécessaire
const expertResults = await this.searchExperts(message, embeddings, llm); let expertResults = [];
if (analysis.requiresExpertSearch) {
expertResults = await this.searchExperts(message, embeddings, llm);
}
// 3. Recherche web complémentaire avec le contexte enrichi // 3. Recherche web si nécessaire
const webResults = await this.searchWeb(enrichedQuery); let webResults = [];
if (analysis.requiresWebSearch) {
webResults = await this.searchWeb(enrichedQuery);
}
// Combinaison des résultats avec les scores appropriés // Combinaison des résultats avec les scores appropriés
const combinedResults = [ const combinedResults = [
@ -889,6 +899,28 @@ Prends en compte:
); );
this.handleStreamWithMemory(stream, emitter); this.handleStreamWithMemory(stream, emitter);
} catch (error) {
console.error('❌ Erreur lors de la gestion des documents:', error);
// Fallback en mode standard
const answeringChain = await this.createAnsweringChain(
llm,
fileIds,
embeddings,
effectiveMode
);
const stream = answeringChain.streamEvents(
{
chat_history: this.conversationHistory,
query: message
},
{
version: 'v1'
}
);
this.handleStreamWithMemory(stream, emitter);
}
} else { } else {
// Fallback sans documents uploadés // Fallback sans documents uploadés
const answeringChain = await this.createAnsweringChain( const answeringChain = await this.createAnsweringChain(
@ -935,6 +967,56 @@ Prends en compte:
return emitter; return emitter;
} }
private async handleFallback(
llm: BaseChatModel,
message: string,
history: BaseMessage[],
emitter: eventEmitter,
fileIds: string[],
embeddings: Embeddings,
mode: 'speed' | 'balanced' | 'quality'
) {
const answeringChain = await this.createAnsweringChain(
llm,
fileIds,
embeddings,
mode
);
const stream = answeringChain.streamEvents(
{
chat_history: history,
query: message
},
{
version: 'v1'
}
);
this.handleStreamWithMemory(stream, emitter);
}
private async ensureVectorStoreInitialized(documents: Document[], embeddings: Embeddings): Promise<RAGDocumentChain> {
const ragChain = RAGDocumentChain.getInstance();
try {
// Vérifier si le vectorStore est déjà initialisé avec des documents
const hasDocuments = ragChain.isInitialized();
if (!hasDocuments) {
console.log('🔄 Initialisation du vector store avec les documents...');
await ragChain.initializeVectorStoreFromDocuments(documents, embeddings);
} else {
console.log('✅ Vector store déjà initialisé avec des documents');
}
return ragChain;
} catch (error) {
console.error('❌ Erreur lors de l\'initialisation du vector store:', error);
throw error;
}
}
} }
export const searchHandlers: Record<string, MetaSearchAgentType> = { export const searchHandlers: Record<string, MetaSearchAgentType> = {
@ -951,7 +1033,10 @@ export const searchHandlers: Record<string, MetaSearchAgentType> = {
const emitter = new eventEmitter(); const emitter = new eventEmitter();
try { try {
const chain = new RAGDocumentChain(); // Fusionner l'historique si nécessaire
const mergedHistory: BaseMessage[] = history;
const chain = RAGDocumentChain.getInstance();
await chain.initializeVectorStoreFromDocuments( await chain.initializeVectorStoreFromDocuments(
fileIds.map(fileId => new Document({ fileIds.map(fileId => new Document({
pageContent: '', pageContent: '',
@ -963,7 +1048,7 @@ export const searchHandlers: Record<string, MetaSearchAgentType> = {
const searchChain = chain.createSearchChain(llm); const searchChain = chain.createSearchChain(llm);
const results = await searchChain.invoke({ const results = await searchChain.invoke({
query: message, query: message,
chat_history: history, chat_history: mergedHistory,
type: 'legal' type: 'legal'
}); });
@ -1006,38 +1091,27 @@ export const searchHandlers: Record<string, MetaSearchAgentType> = {
fileIds fileIds
) => { ) => {
const emitter = new eventEmitter(); const emitter = new eventEmitter();
const ragChain = new RAGDocumentChain();
try { try {
const docs = fileIds.map(fileId => { const chain = RAGDocumentChain.getInstance();
const filePath = path.join(process.cwd(), 'uploads', fileId); await chain.initializeVectorStoreFromDocuments(
const contentPath = filePath + '-extracted.json'; fileIds.map(fileId => new Document({
const content = JSON.parse(fs.readFileSync(contentPath, 'utf8')); pageContent: '',
return new Document<DocumentMetadata>({ metadata: { source: fileId }
pageContent: content.contents.join('\n'), })),
metadata: { embeddings
title: content.title, );
source: fileId
}
});
});
await ragChain.initializeVectorStoreFromDocuments(docs, embeddings); const searchChain = chain.createSearchChain(llm);
const chain = ragChain.createSearchChain(llm); const results = await searchChain.invoke({
const result = await chain.invoke({
query: message, query: message,
chat_history: history, chat_history: history,
type: 'document_search' type: 'documents'
}); });
// Convertir le résultat en objet SearchResponse
const response: SearchResponse = { const response: SearchResponse = {
text: result, text: results,
sources: docs.map(doc => ({ sources: []
title: doc.metadata?.title || '',
content: doc.pageContent,
source: doc.metadata?.source || 'uploaded_docs'
}))
}; };
emitter.emit( emitter.emit(
@ -1048,14 +1122,6 @@ export const searchHandlers: Record<string, MetaSearchAgentType> = {
}) })
); );
emitter.emit(
'data',
JSON.stringify({
type: 'sources',
data: response.sources
})
);
emitter.emit('end'); emitter.emit('end');
} catch (error) { } catch (error) {
emitter.emit( emitter.emit(
@ -1160,7 +1226,7 @@ export const searchHandlers: Record<string, MetaSearchAgentType> = {
const flatDocs = docs.flat(); const flatDocs = docs.flat();
console.log('📚 Nombre total de chunks:', flatDocs.length); console.log('📚 Nombre total de chunks:', flatDocs.length);
const ragChain = new RAGDocumentChain(); const ragChain = RAGDocumentChain.getInstance();
await ragChain.initializeVectorStoreFromDocuments(flatDocs, embeddings); await ragChain.initializeVectorStoreFromDocuments(flatDocs, embeddings);
const chain = ragChain.createSearchChain(llm); const chain = ragChain.createSearchChain(llm);