llm_ticket3/agents/RAG/agent_rag_responder.py
2025-05-06 17:09:05 +02:00

40 lines
1.3 KiB
Python

# agents/RAG/agent_rag_responder.py
from ..base_agent import BaseAgent
class AgentRagResponder(BaseAgent):
def __init__(self, llm, ragflow):
"""
Initialise l'agent avec un LLM et un client Ragflow.
"""
super().__init__("AgentRagResponder", llm)
self.ragflow = ragflow
def executer(self, question: str, top_k: int = 5) -> str:
"""
Effectue une recherche dans Ragflow et interroge le LLM avec le contexte.
"""
# 1. Recherche les documents similaires
documents = self.ragflow.rechercher(question, top_k=top_k)
if not documents:
return "Aucun document pertinent trouvé dans la base de connaissance."
# 2. Construit le contexte à envoyer au LLM
contexte = "\n---\n".join([doc.get("content", "") for doc in documents])
# 3. Construit le prompt final
prompt = (
f"Voici des extraits de documents liés à la question suivante :\n\n"
f"{contexte}\n\n"
f"Réponds de manière précise et complète à la question suivante :\n{question}"
)
# 4. Interroge le LLM
reponse = self.llm.interroger(prompt)
# 5. Ajoute à l'historique (facultatif)
self.ajouter_historique(prompt, reponse)
return reponse