Edurag_beta / app /llm_handling_2.py
Nugh75's picture
update ristrutturazione file app.py con divisione file
47e4aa2
raw
history blame
1.22 kB
import logging
from langchain_openai import ChatOpenAI
from app.config import OPENAI_API_KEY
def answer_question(question):
logging.info(f"Chiamata all'LLM con domanda: {question}")
sys = (
"Sei un assistente AI per la lingua Italiana di nome Counselorbot. "
"Rispondi nella lingua usata per la domanda in modo chiaro, semplice ed esaustivo."
)
messages = [
{"role": "system", "content": sys},
{"role": "user", "content": question}
]
logging.info(f"Messages sent to LLM: {messages}")
try:
llm = ChatOpenAI(
model="gpt-4o-mini",
openai_api_key=OPENAI_API_KEY,
temperature=0.6,
max_tokens=512,
top_p=0.9
)
response = llm.invoke(input=messages)
logging.info(f"Contesto RAG inviato all'LLM: {messages}")
logging.info(f"Risposta ricevuta dall'LLM: {response}")
answer = response.content.strip()
logging.info(f"Domanda: {question} | Risposta: {answer}")
return answer
except Exception as e:
logging.error(f"Errore durante la generazione della risposta: {e}")
return f"Errore durante la generazione della risposta: {e}"