Update services/graph_service.py
Browse files- services/graph_service.py +121 -77
services/graph_service.py
CHANGED
|
@@ -1,99 +1,143 @@
|
|
| 1 |
import os
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
from langchain_openai import ChatOpenAI
|
| 4 |
-
from langchain_core.messages import BaseMessage, AIMessage, HumanMessage
|
| 5 |
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 6 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 7 |
from langgraph.graph import StateGraph, END
|
| 8 |
from tools.analysis_tools import trigger_interview_analysis
|
| 9 |
|
| 10 |
-
# --- 1. Définition de l'état du graphe ---
|
| 11 |
class AgentState(TypedDict):
|
|
|
|
| 12 |
user_id: str
|
| 13 |
job_offer_id: str
|
| 14 |
-
cv_document: dict
|
| 15 |
-
job_offer: dict
|
| 16 |
-
messages: Annotated[Sequence[BaseMessage], lambda x, y: x + y]
|
| 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 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
return "continue"
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
self.graph = build_graph()
|
| 77 |
-
|
| 78 |
-
def invoke(self, payload: dict):
|
| 79 |
-
# Prépare les messages pour le format LangChain
|
| 80 |
-
messages = [HumanMessage(content=m["content"]) if m["role"] == "user" else AIMessage(content=m["content"]) for m in payload["messages"]]
|
| 81 |
|
| 82 |
-
|
| 83 |
-
"user_id":
|
| 84 |
-
"job_offer_id":
|
| 85 |
-
"
|
| 86 |
-
"job_offer": payload["job_offer"],
|
| 87 |
-
"messages": messages,
|
| 88 |
}
|
| 89 |
|
| 90 |
-
final_state = self.graph.invoke(
|
| 91 |
|
| 92 |
-
# Détermine le statut final pour le front-end
|
| 93 |
last_message = final_state['messages'][-1]
|
| 94 |
-
status = "finished" if
|
| 95 |
|
| 96 |
return {
|
| 97 |
"response": last_message.content,
|
| 98 |
"status": status
|
| 99 |
-
}
|
|
|
|
| 1 |
import os
|
| 2 |
+
import logging
|
| 3 |
+
import json
|
| 4 |
+
from typing import TypedDict, Annotated, Sequence, Dict, Any, List
|
| 5 |
+
|
| 6 |
from langchain_openai import ChatOpenAI
|
| 7 |
+
from langchain_core.messages import BaseMessage, AIMessage, HumanMessage, SystemMessage
|
| 8 |
from langchain.agents import create_openai_tools_agent, AgentExecutor
|
| 9 |
from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
|
| 10 |
from langgraph.graph import StateGraph, END
|
| 11 |
from tools.analysis_tools import trigger_interview_analysis
|
| 12 |
|
|
|
|
| 13 |
class AgentState(TypedDict):
|
| 14 |
+
messages: Annotated[Sequence[BaseMessage], lambda x, y: x + y]
|
| 15 |
user_id: str
|
| 16 |
job_offer_id: str
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
+
class GraphInterviewProcessor:
|
| 19 |
+
"""
|
| 20 |
+
Cette classe encapsule la logique d'un entretien en utilisant LangGraph.
|
| 21 |
+
Elle prépare toutes les données nécessaires à l'initialisation, comme dans votre code original.
|
| 22 |
+
"""
|
| 23 |
+
def __init__(self, payload: Dict[str, Any]):
|
| 24 |
+
logging.info("Initialisation de GraphInterviewProcessor...")
|
| 25 |
+
|
| 26 |
+
self.user_id = payload["user_id"]
|
| 27 |
+
self.job_offer_id = payload["job_offer_id"]
|
| 28 |
+
self.job_offer = payload["job_offer"]
|
| 29 |
+
self.cv_data = payload.get("cv_document", {}).get('candidat', {})
|
| 30 |
+
|
| 31 |
+
if not self.cv_data:
|
| 32 |
+
raise ValueError("Données du candidat non trouvées dans le payload.")
|
| 33 |
+
|
| 34 |
+
self.system_prompt_template = self._load_prompt_template('prompts/rag_prompt_old.txt')
|
| 35 |
+
self.formatted_cv_str = self._format_cv_for_prompt()
|
| 36 |
+
self.skills_summary = self._extract_skills_summary()
|
| 37 |
+
self.reconversion_info = self._extract_reconversion_info()
|
| 38 |
+
|
| 39 |
+
self.agent_executor = self._create_agent_executor()
|
| 40 |
+
self.graph = self._build_graph()
|
| 41 |
+
logging.info("GraphInterviewProcessor initialisé avec succès.")
|
| 42 |
+
|
| 43 |
+
def _load_prompt_template(self, file_path: str) -> str:
|
| 44 |
+
"""Charge le template du prompt depuis un fichier."""
|
| 45 |
+
try:
|
| 46 |
+
with open(file_path, 'r', encoding='utf-8') as f:
|
| 47 |
+
return f.read()
|
| 48 |
+
except FileNotFoundError:
|
| 49 |
+
logging.error(f"Fichier prompt introuvable: {file_path}")
|
| 50 |
+
return "Vous êtes un assistant RH."
|
| 51 |
+
|
| 52 |
+
def _format_cv_for_prompt(self) -> str:
|
| 53 |
+
"""Formate le CV pour l'injecter dans le prompt."""
|
| 54 |
+
return json.dumps(self.cv_data, indent=2, ensure_ascii=False)
|
| 55 |
+
|
| 56 |
+
def _extract_skills_summary(self) -> str:
|
| 57 |
+
"""Extrait un résumé des compétences avec niveaux."""
|
| 58 |
+
competences = self.cv_data.get('analyse_competences', [])
|
| 59 |
+
if not competences:
|
| 60 |
+
return "Aucune analyse de compétences disponible."
|
| 61 |
+
summary = [f"{comp.get('skill', '')}: {comp.get('level', 'débutant')}" for comp in competences]
|
| 62 |
+
return "Niveaux de compétences du candidat: " + " | ".join(summary)
|
| 63 |
+
|
| 64 |
+
def _extract_reconversion_info(self) -> str:
|
| 65 |
+
"""Extrait les informations de reconversion."""
|
| 66 |
+
reconversion = self.cv_data.get('reconversion', {})
|
| 67 |
+
if reconversion.get('is_reconversion'):
|
| 68 |
+
return f"CANDIDAT EN RECONVERSION: {reconversion.get('analysis', '')}"
|
| 69 |
+
return "Le candidat n'est pas identifié comme étant en reconversion."
|
| 70 |
+
|
| 71 |
+
def _create_agent_executor(self) -> AgentExecutor:
|
| 72 |
+
"""Crée l'agent executor avec un prompt minimaliste."""
|
| 73 |
+
prompt = ChatPromptTemplate.from_messages([
|
| 74 |
+
SystemMessage(content="{system_prompt_content}"),
|
| 75 |
+
MessagesPlaceholder(variable_name="messages"),
|
| 76 |
+
MessagesPlaceholder(variable_name="agent_scratchpad"),
|
| 77 |
+
])
|
| 78 |
+
llm = ChatOpenAI(api_key=os.getenv("OPENAI_API_KEY"), model="gpt-4o-mini", temperature=0.7)
|
| 79 |
+
tools = [trigger_interview_analysis]
|
| 80 |
+
agent = create_openai_tools_agent(llm, tools, prompt)
|
| 81 |
+
return AgentExecutor(agent=agent, tools=tools, verbose=True)
|
| 82 |
+
|
| 83 |
+
def _agent_node(self, state: AgentState):
|
| 84 |
+
"""Prépare le prompt système dynamiquement et appelle l'agent."""
|
| 85 |
+
system_prompt_content = self.system_prompt_template.format(
|
| 86 |
+
entreprise=self.job_offer.get('entreprise', 'notre entreprise'),
|
| 87 |
+
poste=self.job_offer.get('poste', 'ce poste'),
|
| 88 |
+
mission=self.job_offer.get('mission', 'Non spécifiée'),
|
| 89 |
+
profil_recherche=self.job_offer.get('profil_recherche', 'Non spécifié'),
|
| 90 |
+
competences=self.job_offer.get('competences', 'Non spécifiées'),
|
| 91 |
+
pole=self.job_offer.get('pole', 'Non spécifié'),
|
| 92 |
+
cv=self.formatted_cv_str,
|
| 93 |
+
skills_analysis=self.skills_summary,
|
| 94 |
+
reconversion_analysis=self.reconversion_info
|
| 95 |
+
)
|
| 96 |
+
|
| 97 |
+
response = self.agent_executor.invoke({
|
| 98 |
+
"system_prompt_content": system_prompt_content,
|
| 99 |
+
"messages": state["messages"],
|
| 100 |
+
"user_id": state["user_id"],
|
| 101 |
+
"job_offer_id": state["job_offer_id"],
|
| 102 |
+
"conversation_history": state["messages"]
|
| 103 |
+
})
|
| 104 |
+
return {"messages": [response['output']]}
|
| 105 |
+
|
| 106 |
+
def _router(self, state: AgentState):
|
| 107 |
+
"""Décide du chemin à suivre après la réponse de l'agent."""
|
| 108 |
+
last_message = state["messages"][-1]
|
| 109 |
+
if hasattr(last_message, 'tool_calls') and last_message.tool_calls:
|
| 110 |
+
return "end"
|
| 111 |
return "continue"
|
| 112 |
|
| 113 |
+
def _build_graph(self) -> any:
|
| 114 |
+
"""Construit et compile le graphe d'états."""
|
| 115 |
+
graph = StateGraph(AgentState)
|
| 116 |
+
graph.add_node("agent", self._agent_node)
|
| 117 |
+
graph.add_conditional_edges(
|
| 118 |
+
"agent",
|
| 119 |
+
self._router,
|
| 120 |
+
{"continue": "agent", "end": END}
|
| 121 |
+
)
|
| 122 |
+
graph.set_entry_point("agent")
|
| 123 |
+
return graph.compile()
|
| 124 |
+
|
| 125 |
+
def invoke(self, messages: List[Dict[str, Any]]):
|
| 126 |
+
"""Point d'entrée pour lancer une conversation dans le graphe."""
|
| 127 |
+
langchain_messages = [HumanMessage(content=m["content"]) if m["role"] == "user" else AIMessage(content=m["content"]) for m in messages]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
|
| 129 |
+
initial_state = {
|
| 130 |
+
"user_id": self.user_id,
|
| 131 |
+
"job_offer_id": self.job_offer_id,
|
| 132 |
+
"messages": langchain_messages,
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
|
| 135 |
+
final_state = self.graph.invoke(initial_state)
|
| 136 |
|
|
|
|
| 137 |
last_message = final_state['messages'][-1]
|
| 138 |
+
status = "finished" if self._router(final_state) == 'end' else "interviewing"
|
| 139 |
|
| 140 |
return {
|
| 141 |
"response": last_message.content,
|
| 142 |
"status": status
|
| 143 |
+
}
|