Spaces:
Sleeping
Sleeping
Update src/interview_simulator/entretient_version_prod.py
Browse files
src/interview_simulator/entretient_version_prod.py
CHANGED
|
@@ -1,98 +1,98 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
import json
|
| 4 |
-
from typing import Dict, List, Any, Annotated
|
| 5 |
-
from typing_extensions import TypedDict
|
| 6 |
-
|
| 7 |
-
from langchain_core.messages import AIMessage, SystemMessage, HumanMessage, ToolMessage
|
| 8 |
-
from langchain_groq import ChatGroq
|
| 9 |
-
from langgraph.graph import StateGraph, START, END
|
| 10 |
-
from langgraph.graph.message import add_messages
|
| 11 |
-
from langgraph.prebuilt import ToolNode
|
| 12 |
-
from langchain_openai import ChatOpenAI
|
| 13 |
-
|
| 14 |
-
from src.config import read_system_prompt, format_cv
|
| 15 |
-
from src.crew.crew_pool import interview_analyser
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
class State(TypedDict):
|
| 19 |
-
messages: Annotated[list, add_messages]
|
| 20 |
-
|
| 21 |
-
class InterviewProcessor:
|
| 22 |
-
def __init__(self, cv_document: Dict[str, Any], job_offer: Dict[str, Any], conversation_history: List[Dict[str, Any]]):
|
| 23 |
-
if not cv_document or 'candidat' not in cv_document:
|
| 24 |
-
raise ValueError("Document CV invalide fourni.")
|
| 25 |
-
if not job_offer:
|
| 26 |
-
raise ValueError("Données de l'offre d'emploi non fournies.")
|
| 27 |
-
|
| 28 |
-
self.job_offer = job_offer
|
| 29 |
-
self.cv_data = cv_document['candidat']
|
| 30 |
-
self.conversation_history = conversation_history
|
| 31 |
-
self.tools = [interview_analyser]
|
| 32 |
-
self.llm = self._get_llm()
|
| 33 |
-
self.llm_with_tools = self.llm.bind_tools(self.tools)
|
| 34 |
-
|
| 35 |
-
self.system_prompt_template = self._load_prompt_template()
|
| 36 |
-
self.graph = self._build_graph()
|
| 37 |
-
|
| 38 |
-
def _get_llm(self) -> ChatOpenAI:
|
| 39 |
-
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 40 |
-
return ChatOpenAI(
|
| 41 |
-
temperature=0.6,
|
| 42 |
-
model_name="gpt-4o-mini",
|
| 43 |
-
api_key=openai_api_key
|
| 44 |
-
)
|
| 45 |
-
|
| 46 |
-
def _load_prompt_template(self) -> str:
|
| 47 |
-
return read_system_prompt('prompts/rag_prompt_old.txt')
|
| 48 |
-
|
| 49 |
-
def _chatbot_node(self, state: State) -> dict:
|
| 50 |
-
if state["messages"] and isinstance(state["messages"][-1], ToolMessage):
|
| 51 |
-
tool_message = state["messages"][-1]
|
| 52 |
-
return {"messages": [AIMessage(content=tool_message.content)]}
|
| 53 |
-
messages = state["messages"]
|
| 54 |
-
formatted_cv_str = format_cv(self.cv_data)
|
| 55 |
-
|
| 56 |
-
mission = self.job_offer.get('mission', 'Non spécifiée')
|
| 57 |
-
profil_recherche = self.job_offer.get('profil_recherche', 'Non spécifié')
|
| 58 |
-
competences = self.job_offer.get('competences', 'Non spécifiées')
|
| 59 |
-
pole = self.job_offer.get('pole', 'Non spécifié')
|
| 60 |
-
system_prompt = self.system_prompt_template.format(
|
| 61 |
-
entreprise=self.job_offer.get('entreprise', 'notre entreprise'),
|
| 62 |
-
poste=self.job_offer.get('poste', 'ce poste'),
|
| 63 |
-
mission=mission,
|
| 64 |
-
profil_recherche=profil_recherche,
|
| 65 |
-
competences=competences,
|
| 66 |
-
pole=pole,
|
| 67 |
-
cv=formatted_cv_str
|
| 68 |
-
)
|
| 69 |
-
llm_messages = [SystemMessage(content=system_prompt)] + messages
|
| 70 |
-
response = self.llm_with_tools.invoke(llm_messages)
|
| 71 |
-
return {"messages": [response]}
|
| 72 |
-
|
| 73 |
-
def _route_after_chatbot(self, state: State) -> str:
|
| 74 |
-
last_message = state["messages"][-1]
|
| 75 |
-
if last_message.tool_calls:
|
| 76 |
-
return "call_tool"
|
| 77 |
-
return END
|
| 78 |
-
|
| 79 |
-
def _build_graph(self) -> any:
|
| 80 |
-
graph_builder = StateGraph(State)
|
| 81 |
-
|
| 82 |
-
graph_builder.add_node("chatbot", self._chatbot_node)
|
| 83 |
-
graph_builder.add_node("call_tool", ToolNode(self.tools))
|
| 84 |
-
graph_builder.add_edge(START, "chatbot")
|
| 85 |
-
graph_builder.add_conditional_edges(
|
| 86 |
-
"chatbot",
|
| 87 |
-
self._route_after_chatbot,
|
| 88 |
-
{
|
| 89 |
-
"call_tool": "call_tool",
|
| 90 |
-
END: END
|
| 91 |
-
}
|
| 92 |
-
)
|
| 93 |
-
graph_builder.add_edge("call_tool", "chatbot")
|
| 94 |
-
return graph_builder.compile()
|
| 95 |
-
|
| 96 |
-
def run(self, messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 97 |
-
initial_state = self.conversation_history + messages
|
| 98 |
return self.graph.invoke({"messages": initial_state})
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
import json
|
| 4 |
+
from typing import Dict, List, Any, Annotated
|
| 5 |
+
from typing_extensions import TypedDict
|
| 6 |
+
|
| 7 |
+
from langchain_core.messages import AIMessage, SystemMessage, HumanMessage, ToolMessage
|
| 8 |
+
from langchain_groq import ChatGroq
|
| 9 |
+
from langgraph.graph import StateGraph, START, END
|
| 10 |
+
from langgraph.graph.message import add_messages
|
| 11 |
+
from langgraph.prebuilt import ToolNode
|
| 12 |
+
from langchain_openai import ChatOpenAI
|
| 13 |
+
|
| 14 |
+
from src.config import read_system_prompt, format_cv
|
| 15 |
+
from src.crew.crew_pool import interview_analyser
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
class State(TypedDict):
|
| 19 |
+
messages: Annotated[list, add_messages]
|
| 20 |
+
|
| 21 |
+
class InterviewProcessor:
|
| 22 |
+
def __init__(self, cv_document: Dict[str, Any], job_offer: Dict[str, Any], conversation_history: List[Dict[str, Any]]):
|
| 23 |
+
if not cv_document or 'candidat' not in cv_document:
|
| 24 |
+
raise ValueError("Document CV invalide fourni.")
|
| 25 |
+
if not job_offer:
|
| 26 |
+
raise ValueError("Données de l'offre d'emploi non fournies.")
|
| 27 |
+
|
| 28 |
+
self.job_offer = job_offer
|
| 29 |
+
self.cv_data = cv_document['candidat']
|
| 30 |
+
self.conversation_history = conversation_history
|
| 31 |
+
self.tools = [interview_analyser]
|
| 32 |
+
self.llm = self._get_llm()
|
| 33 |
+
self.llm_with_tools = self.llm.bind_tools(self.tools)
|
| 34 |
+
|
| 35 |
+
self.system_prompt_template = self._load_prompt_template()
|
| 36 |
+
self.graph = self._build_graph()
|
| 37 |
+
|
| 38 |
+
def _get_llm(self) -> ChatOpenAI:
|
| 39 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 40 |
+
return ChatOpenAI(
|
| 41 |
+
temperature=0.6,
|
| 42 |
+
model_name="gpt-4o-mini",
|
| 43 |
+
api_key=openai_api_key
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
def _load_prompt_template(self) -> str:
|
| 47 |
+
return read_system_prompt('prompts/rag_prompt_old.txt')
|
| 48 |
+
|
| 49 |
+
def _chatbot_node(self, state: State) -> dict:
|
| 50 |
+
if state["messages"] and isinstance(state["messages"][-1], ToolMessage):
|
| 51 |
+
tool_message = state["messages"][-1]
|
| 52 |
+
return {"messages": [AIMessage(content=tool_message.content)]}
|
| 53 |
+
messages = state["messages"]
|
| 54 |
+
formatted_cv_str = format_cv(self.cv_data)
|
| 55 |
+
|
| 56 |
+
mission = self.job_offer.get('mission', 'Non spécifiée')
|
| 57 |
+
profil_recherche = self.job_offer.get('profil_recherche', 'Non spécifié')
|
| 58 |
+
competences = self.job_offer.get('competences', 'Non spécifiées')
|
| 59 |
+
pole = self.job_offer.get('pole', 'Non spécifié')
|
| 60 |
+
system_prompt = self.system_prompt_template.format(
|
| 61 |
+
entreprise=self.job_offer.get('entreprise', 'notre entreprise'),
|
| 62 |
+
poste=self.job_offer.get('poste', 'ce poste'),
|
| 63 |
+
mission=mission,
|
| 64 |
+
profil_recherche=profil_recherche,
|
| 65 |
+
competences=competences,
|
| 66 |
+
pole=pole,
|
| 67 |
+
cv=formatted_cv_str
|
| 68 |
+
)
|
| 69 |
+
llm_messages = [SystemMessage(content=system_prompt)] + messages
|
| 70 |
+
response = self.llm_with_tools.invoke(llm_messages)
|
| 71 |
+
return {"messages": [response]}
|
| 72 |
+
|
| 73 |
+
def _route_after_chatbot(self, state: State) -> str:
|
| 74 |
+
last_message = state["messages"][-1]
|
| 75 |
+
if last_message.tool_calls:
|
| 76 |
+
return "call_tool"
|
| 77 |
+
return END
|
| 78 |
+
|
| 79 |
+
def _build_graph(self) -> any:
|
| 80 |
+
graph_builder = StateGraph(State)
|
| 81 |
+
|
| 82 |
+
graph_builder.add_node("chatbot", self._chatbot_node)
|
| 83 |
+
graph_builder.add_node("call_tool", ToolNode(self.tools))
|
| 84 |
+
graph_builder.add_edge(START, "chatbot")
|
| 85 |
+
graph_builder.add_conditional_edges(
|
| 86 |
+
"chatbot",
|
| 87 |
+
self._route_after_chatbot,
|
| 88 |
+
{
|
| 89 |
+
"call_tool": "call_tool",
|
| 90 |
+
END: END
|
| 91 |
+
}
|
| 92 |
+
)
|
| 93 |
+
graph_builder.add_edge("call_tool", "chatbot")
|
| 94 |
+
return graph_builder.compile()
|
| 95 |
+
|
| 96 |
+
def run(self, messages: List[Dict[str, Any]]) -> Dict[str, Any]:
|
| 97 |
+
initial_state = self.conversation_history + messages
|
| 98 |
return self.graph.invoke({"messages": initial_state})
|