|
9 | 9 | from pathlib import Path |
10 | 10 | from typing import Optional |
11 | 11 |
|
| 12 | +from ..models import FinancialProfile # <-- NUOVO |
12 | 13 | from .base_agent import BaseAgent |
13 | 14 |
|
14 | 15 | logger = logging.getLogger(__name__) |
@@ -50,6 +51,10 @@ def __init__( |
50 | 51 | self.current_question_index = 0 |
51 | 52 | logger.debug("Question index initialized to 0") |
52 | 53 |
|
| 54 | + self.financial_profile_extraction_prompt = self._load_prompt_template( |
| 55 | + "financial_profile_extraction" |
| 56 | + ) |
| 57 | + |
53 | 58 | # Call parent init |
54 | 59 | super().__init__( |
55 | 60 | name=name, |
@@ -236,6 +241,45 @@ def reset_questions(self) -> None: |
236 | 241 | self.current_question_index = 0 |
237 | 242 | logger.info("Question index reset to 0") |
238 | 243 |
|
| 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 | + |
239 | 283 | # ==================== Conversation Utilities ==================== |
240 | 284 |
|
241 | 285 | def get_formatted_conversation_history(self) -> str: |
|
0 commit comments