mirror of
https://github.com/Ladebeze66/llm_ticket3.git
synced 2025-12-15 21:36:52 +01:00
107 lines
4.3 KiB
Python
107 lines
4.3 KiB
Python
from ..base_agent import BaseAgent
|
|
from typing import Dict, Any
|
|
import logging
|
|
import os
|
|
from datetime import datetime
|
|
from ..utils.pipeline_logger import sauvegarder_donnees
|
|
|
|
logger = logging.getLogger("AgentReportGenerator")
|
|
|
|
class AgentReportGenerator(BaseAgent):
|
|
def __init__(self, llm):
|
|
super().__init__("AgentReportGenerator", llm)
|
|
|
|
self.params = {
|
|
"temperature": 0.2,
|
|
"top_p": 0.8,
|
|
"max_tokens": 8000
|
|
}
|
|
|
|
self.system_prompt = """Tu es un expert en support technique chargé de générer un rapport final à partir des analyses d'un ticket de support.
|
|
Ton rôle est de croiser les informations provenant :
|
|
- de l'analyse textuelle du ticket client
|
|
- des analyses détaillées de plusieurs captures d'écran
|
|
|
|
Tu dois structurer ta réponse en format question/réponse de manière claire, en gardant l'intégralité des points importants.
|
|
|
|
Ne propose jamais de solution. Ne reformule pas le contexte.
|
|
Ta seule mission est de croiser les données textuelles et visuelles et d'en tirer des observations claires, en listant les éléments factuels visibles dans les captures qui appuient ou complètent le texte du ticket.
|
|
|
|
Structure du rapport attendu :
|
|
1. Contexte général (résumé du ticket textuel en une phrase)
|
|
2. Problèmes ou questions identifiés (sous forme de questions claires)
|
|
3. Résumé croisé image/texte pour chaque question
|
|
4. Liste d'observations supplémentaires pertinentes (si applicable)
|
|
|
|
Reste strictement factuel. Ne fais aucune hypothèse. Ne suggère pas d'étapes ni d'interprétation."""
|
|
|
|
self._appliquer_config_locale()
|
|
logger.info("AgentReportGenerator initialisé")
|
|
|
|
def _appliquer_config_locale(self) -> None:
|
|
if hasattr(self.llm, "prompt_system"):
|
|
self.llm.prompt_system = self.system_prompt
|
|
if hasattr(self.llm, "configurer"):
|
|
self.llm.configurer(**self.params)
|
|
|
|
def executer(self, rapport_data: Dict[str, Any], dossier_destination: str) -> str:
|
|
ticket_id = rapport_data.get("ticket_id", "Inconnu")
|
|
print(f"AgentReportGenerator : génération du rapport pour le ticket {ticket_id}")
|
|
|
|
try:
|
|
prompt = self._generer_prompt(rapport_data)
|
|
response = self.llm.interroger(prompt)
|
|
|
|
logger.info(f"Rapport généré pour le ticket {ticket_id}, longueur: {len(response)} caractères")
|
|
|
|
result = {
|
|
"prompt": prompt,
|
|
"response": response,
|
|
"metadata": {
|
|
"ticket_id": ticket_id,
|
|
"timestamp": self._get_timestamp(),
|
|
"source_agent": self.nom,
|
|
"model_info": {
|
|
"model": getattr(self.llm, "modele", str(type(self.llm))),
|
|
**getattr(self.llm, "params", {})
|
|
}
|
|
}
|
|
}
|
|
|
|
sauvegarder_donnees(ticket_id, "rapport_final", result, base_dir=dossier_destination, is_resultat=True)
|
|
|
|
self.ajouter_historique("rapport_final", {
|
|
"ticket_id": ticket_id,
|
|
"prompt": prompt,
|
|
"timestamp": self._get_timestamp()
|
|
}, response)
|
|
|
|
return response
|
|
|
|
except Exception as e:
|
|
logger.error(f"Erreur lors de la génération du rapport : {str(e)}")
|
|
return f"ERREUR: {str(e)}"
|
|
|
|
def _generer_prompt(self, rapport_data: Dict[str, Any]) -> str:
|
|
ticket_text = rapport_data.get("ticket_analyse", "")
|
|
image_blocs = []
|
|
analyses_images = rapport_data.get("analyse_images", {})
|
|
|
|
for chemin_image, analyse_obj in analyses_images.items():
|
|
analyse = analyse_obj.get("analysis", {}).get("analyse", "")
|
|
if analyse:
|
|
image_blocs.append(f"--- IMAGE : {os.path.basename(chemin_image)} ---\n{analyse}\n")
|
|
|
|
bloc_images = "\n".join(image_blocs)
|
|
|
|
return (
|
|
f"Voici les données d'analyse pour un ticket de support :\n\n"
|
|
f"=== ANALYSE DU TICKET ===\n{ticket_text}\n\n"
|
|
f"=== ANALYSES D'IMAGES ===\n{bloc_images}\n\n"
|
|
f"Génère un rapport croisé en suivant les instructions précédentes."
|
|
)
|
|
|
|
def _get_timestamp(self) -> str:
|
|
from datetime import datetime
|
|
return datetime.now().strftime("%Y%m%d_%H%M%S")
|