Spaces:
Running
Running
Update graph_agentB.py
Browse files- graph_agentB.py +66 -57
graph_agentB.py
CHANGED
|
@@ -1,57 +1,66 @@
|
|
| 1 |
-
from typing import TypedDict, Annotated,
|
| 2 |
-
from langchain_core.messages import BaseMessage
|
| 3 |
-
from langgraph.graph import StateGraph, END
|
| 4 |
-
from langgraph.graph.message import add_messages
|
| 5 |
-
from config import llm, client, langsmith_project
|
| 6 |
-
from pinecone_utilsB import *
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
""
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
**
|
| 34 |
-
{
|
| 35 |
-
|
| 36 |
-
**
|
| 37 |
-
""
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
""
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from typing import TypedDict, Annotated, Sequence
|
| 2 |
+
from langchain_core.messages import BaseMessage
|
| 3 |
+
from langgraph.graph import StateGraph, END
|
| 4 |
+
from langgraph.graph.message import add_messages
|
| 5 |
+
from config import llm, client, langsmith_project
|
| 6 |
+
from pinecone_utilsB import *
|
| 7 |
+
|
| 8 |
+
search_engine = HybridSearchEngine()
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
class GraphState(TypedDict):
|
| 12 |
+
messages: Annotated[Sequence[BaseMessage], add_messages]
|
| 13 |
+
query: str
|
| 14 |
+
relevant_docs: list
|
| 15 |
+
response: str
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def retrieve_combined(state: GraphState) -> dict:
|
| 19 |
+
"""Récupération hybride : Pinecone (sémantique) + BM25 (mots-clés)."""
|
| 20 |
+
relevant_docs = search_engine.hybrid_search(state["query"])
|
| 21 |
+
return {"relevant_docs": relevant_docs}
|
| 22 |
+
|
| 23 |
+
def generate_response(state: GraphState) -> dict:
|
| 24 |
+
"""Génération de réponse en combinant informations sémantiques et mots-clés."""
|
| 25 |
+
context = "\n\n".join(state["relevant_docs"])
|
| 26 |
+
prompt = f"""
|
| 27 |
+
Vous êtes un expert en analyse de texte.
|
| 28 |
+
Votre réponse doit prendre en compte les éléments suivants :
|
| 29 |
+
|
| 30 |
+
- Recherche sémantique (contextes proches)
|
| 31 |
+
- Recherche par mots-clés (documents contenant les termes exacts)
|
| 32 |
+
|
| 33 |
+
**Informations pertinentes trouvées** :
|
| 34 |
+
{context}
|
| 35 |
+
|
| 36 |
+
**Question de l'utilisateur** :
|
| 37 |
+
{state["query"]}
|
| 38 |
+
|
| 39 |
+
**Réponse :**
|
| 40 |
+
"""
|
| 41 |
+
response = llm.invoke(prompt)
|
| 42 |
+
return {"response": response.content}
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def post_process_response(state: GraphState) -> dict:
|
| 46 |
+
"""Nettoie et valide la réponse."""
|
| 47 |
+
response = state["response"].strip()
|
| 48 |
+
|
| 49 |
+
# Vérifier si la réponse est pertinente
|
| 50 |
+
if not response or response.lower() in ["je ne sais pas", "i don't know"]:
|
| 51 |
+
response = "Désolé, je n'ai pas trouvé d'informations pertinentes pour votre question."
|
| 52 |
+
|
| 53 |
+
return {"response": response}
|
| 54 |
+
|
| 55 |
+
# Construction du graphe
|
| 56 |
+
graph_builder = StateGraph(GraphState)
|
| 57 |
+
graph_builder.add_node("retrieve", retrieve_combined)
|
| 58 |
+
graph_builder.add_node("generate", generate_response)
|
| 59 |
+
graph_builder.add_node("post_process", post_process_response)
|
| 60 |
+
|
| 61 |
+
graph_builder.set_entry_point("retrieve")
|
| 62 |
+
graph_builder.add_edge("retrieve", "generate")
|
| 63 |
+
graph_builder.add_edge("generate", "post_process")
|
| 64 |
+
graph_builder.add_edge("post_process", END)
|
| 65 |
+
|
| 66 |
+
agent = graph_builder.compile()
|