Spaces:
Sleeping
Sleeping
Create interview_service.py
Browse files
src/services/interview_service.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
from typing import Dict, List, Any
|
| 3 |
+
from typing_extensions import TypedDict
|
| 4 |
+
from langchain_core.messages import AIMessage, SystemMessage, HumanMessage
|
| 5 |
+
from langgraph.graph import StateGraph, START, END
|
| 6 |
+
from langgraph.graph.message import add_messages
|
| 7 |
+
from langchain_openai import ChatOpenAI
|
| 8 |
+
from src.config import read_system_prompt, format_cv
|
| 9 |
+
|
| 10 |
+
class State(TypedDict):
|
| 11 |
+
messages: List[add_messages]
|
| 12 |
+
|
| 13 |
+
class InterviewService:
|
| 14 |
+
def __init__(self, models: Dict[str, Any]):
|
| 15 |
+
self.models = models
|
| 16 |
+
self.llm = self._get_llm()
|
| 17 |
+
self.system_prompt_template = self._load_prompt_template()
|
| 18 |
+
self.graph = self._build_graph()
|
| 19 |
+
|
| 20 |
+
def _get_llm(self) -> ChatOpenAI:
|
| 21 |
+
openai_api_key = os.getenv("OPENAI_API_KEY")
|
| 22 |
+
return ChatOpenAI(
|
| 23 |
+
temperature=0.6,
|
| 24 |
+
model_name="gpt-4o-mini",
|
| 25 |
+
api_key=openai_api_key
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
def _load_prompt_template(self) -> str:
|
| 29 |
+
return read_system_prompt('prompts/rag_prompt_old.txt')
|
| 30 |
+
|
| 31 |
+
def _chatbot_node(self, state: State) -> Dict[str, Any]:
|
| 32 |
+
messages = state["messages"]
|
| 33 |
+
formatted_cv_str = format_cv(self.cv_data)
|
| 34 |
+
|
| 35 |
+
system_prompt = self.system_prompt_template.format(
|
| 36 |
+
entreprise=self.job_offer.get('entreprise', 'notre entreprise'),
|
| 37 |
+
poste=self.job_offer.get('poste', 'ce poste'),
|
| 38 |
+
mission=self.job_offer.get('mission', 'Non spécifiée'),
|
| 39 |
+
profil_recherche=self.job_offer.get('profil_recherche', 'Non spécifié'),
|
| 40 |
+
competences=self.job_offer.get('competences', 'Non spécifiées'),
|
| 41 |
+
pole=self.job_offer.get('pole', 'Non spécifié'),
|
| 42 |
+
cv=formatted_cv_str
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
llm_messages = [SystemMessage(content=system_prompt)] + messages
|
| 46 |
+
response = self.llm.invoke(llm_messages)
|
| 47 |
+
return {"messages": [response]}
|
| 48 |
+
|
| 49 |
+
def _build_graph(self) -> any:
|
| 50 |
+
graph_builder = StateGraph(State)
|
| 51 |
+
|
| 52 |
+
graph_builder.add_node("chatbot", self._chatbot_node)
|
| 53 |
+
graph_builder.add_edge(START, "chatbot")
|
| 54 |
+
graph_builder.add_edge("chatbot", END)
|
| 55 |
+
|
| 56 |
+
return graph_builder.compile()
|
| 57 |
+
|
| 58 |
+
def process_conversation(
|
| 59 |
+
self,
|
| 60 |
+
cv_document: Dict[str, Any],
|
| 61 |
+
job_offer: Dict[str, Any],
|
| 62 |
+
conversation_history: List[Dict[str, Any]],
|
| 63 |
+
messages: List[Dict[str, Any]]
|
| 64 |
+
) -> Dict[str, Any]:
|
| 65 |
+
|
| 66 |
+
if not cv_document or 'candidat' not in cv_document:
|
| 67 |
+
raise ValueError("Document CV invalide fourni.")
|
| 68 |
+
|
| 69 |
+
if not job_offer:
|
| 70 |
+
raise ValueError("Données de l'offre d'emploi non fournies.")
|
| 71 |
+
|
| 72 |
+
self.job_offer = job_offer
|
| 73 |
+
self.cv_data = cv_document['candidat']
|
| 74 |
+
self.conversation_history = conversation_history
|
| 75 |
+
|
| 76 |
+
initial_state = conversation_history + messages
|
| 77 |
+
result = self.graph.invoke({"messages": initial_state})
|
| 78 |
+
|
| 79 |
+
response_content = result["messages"][-1].content
|
| 80 |
+
return {"response": response_content}
|