Skip to content

Commit 5ae21f4

Browse files
author
Raffaele Crafa
committed
feat: tried to create an agent for RAG
1 parent b5034ee commit 5ae21f4

File tree

8 files changed

+959
-601
lines changed

8 files changed

+959
-601
lines changed

app.py

Lines changed: 222 additions & 143 deletions
Large diffs are not rendered by default.

src/core/chatbot.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pathlib import Path
1010
from typing import Optional
1111

12+
from ..models import FinancialProfile # <-- NUOVO
1213
from .base_agent import BaseAgent
1314

1415
logger = logging.getLogger(__name__)
@@ -50,6 +51,10 @@ def __init__(
5051
self.current_question_index = 0
5152
logger.debug("Question index initialized to 0")
5253

54+
self.financial_profile_extraction_prompt = self._load_prompt_template(
55+
"financial_profile_extraction"
56+
)
57+
5358
# Call parent init
5459
super().__init__(
5560
name=name,
@@ -236,6 +241,45 @@ def reset_questions(self) -> None:
236241
self.current_question_index = 0
237242
logger.info("Question index reset to 0")
238243

244+
245+
# ==================== Portfolio Extraction ====================
246+
247+
248+
def extract_financial_profile(self) -> Optional[FinancialProfile]:
249+
"""
250+
Analizza la memoria della conversazione corrente e estrae un
251+
oggetto FinancialProfile strutturato.
252+
"""
253+
logger.info("ChatbotAgent sta tentando di estrarre il profilo finanziario...")
254+
255+
# Prende l'intera conversazione dalla sua memoria
256+
conversation_summary = self.memory.get_full_conversation()
257+
258+
if not conversation_summary:
259+
logger.warning("La memoria della conversazione è vuota. Estrazione annullata.")
260+
return None
261+
262+
# Formatta il prompt di estrazione con la conversazione
263+
extraction_prompt = self.financial_profile_extraction_prompt.format(
264+
conversation_summary=conversation_summary
265+
)
266+
267+
# Chiama l'LLM chiedendo una risposta strutturata
268+
response = self._client.structured_response(
269+
input=extraction_prompt, output_cls=FinancialProfile
270+
)
271+
272+
# Controlla e restituisce i dati strutturati
273+
if hasattr(response, "structured_data") and response.structured_data:
274+
profile = response.structured_data[0]
275+
logger.info("Profilo estratto con successo dal ChatbotAgent.")
276+
return profile
277+
278+
logger.error(
279+
"Estrazione del profilo fallita: nessun dato strutturato ricevuto dall'LLM."
280+
)
281+
return None
282+
239283
# ==================== Conversation Utilities ====================
240284

241285
def get_formatted_conversation_history(self) -> str:

0 commit comments

Comments
 (0)