anderson-ufrj
commited on
Commit
·
4a7ce2c
1
Parent(s):
0c769eb
feat(chat): enable Zumbi agent with dados.gov.br integration in chat flow
Browse files- Fix circular import issue with separate integration module
- Enable automatic dados.gov.br searches in investigations
- Add open data references to investigation results
- Include suggested actions for exploring related datasets
- Format responses to highlight open data availability
- src/api/routes/chat.py +62 -30
src/api/routes/chat.py
CHANGED
|
@@ -16,6 +16,7 @@ from src.core import get_logger
|
|
| 16 |
from src.core.exceptions import ValidationError
|
| 17 |
from src.api.dependencies import get_current_optional_user
|
| 18 |
from src.api.routes.chat_drummond_factory import get_drummond_agent
|
|
|
|
| 19 |
from src.agents.deodoro import AgentMessage, AgentContext, AgentResponse, AgentStatus
|
| 20 |
from src.agents.abaporu import MasterAgent
|
| 21 |
from src.services.chat_service import IntentDetector, IntentType
|
|
@@ -269,47 +270,69 @@ async def send_message(
|
|
| 269 |
max_results=10
|
| 270 |
)
|
| 271 |
|
| 272 |
-
# Run investigation
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
| 276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 277 |
return ChatResponse(
|
| 278 |
session_id=session_id,
|
| 279 |
-
agent_id="
|
| 280 |
-
agent_name="
|
| 281 |
-
message="
|
| 282 |
confidence=0.0,
|
| 283 |
-
metadata={"status": "
|
| 284 |
)
|
| 285 |
|
| 286 |
-
# Format response
|
| 287 |
-
message =
|
| 288 |
-
message += f"Fonte de dados: {investigation_result.data_source}\n"
|
| 289 |
-
message += f"Resultados encontrados: {investigation_result.total_found}\n"
|
| 290 |
-
message += f"Anomalias detectadas: {investigation_result.anomalies_detected}\n\n"
|
| 291 |
|
| 292 |
-
if
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
| 297 |
-
|
| 298 |
-
|
| 299 |
else:
|
| 300 |
-
|
|
|
|
| 301 |
|
| 302 |
response = AgentResponse(
|
| 303 |
agent_name="Zumbi dos Palmares",
|
| 304 |
status=AgentStatus.COMPLETED,
|
| 305 |
result={
|
| 306 |
"message": message,
|
| 307 |
-
"
|
| 308 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 309 |
},
|
| 310 |
metadata={
|
| 311 |
-
"confidence":
|
| 312 |
-
"
|
|
|
|
| 313 |
}
|
| 314 |
)
|
| 315 |
agent_id = "zumbi"
|
|
@@ -370,10 +393,19 @@ async def send_message(
|
|
| 370 |
|
| 371 |
# Prepare suggested actions based on response
|
| 372 |
suggested_actions = []
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 377 |
|
| 378 |
# Extract message from response
|
| 379 |
if hasattr(response, 'result') and isinstance(response.result, dict):
|
|
|
|
| 16 |
from src.core.exceptions import ValidationError
|
| 17 |
from src.api.dependencies import get_current_optional_user
|
| 18 |
from src.api.routes.chat_drummond_factory import get_drummond_agent
|
| 19 |
+
from src.api.routes.chat_zumbi_integration import run_zumbi_investigation, format_investigation_message
|
| 20 |
from src.agents.deodoro import AgentMessage, AgentContext, AgentResponse, AgentStatus
|
| 21 |
from src.agents.abaporu import MasterAgent
|
| 22 |
from src.services.chat_service import IntentDetector, IntentType
|
|
|
|
| 270 |
max_results=10
|
| 271 |
)
|
| 272 |
|
| 273 |
+
# Run investigation with Zumbi agent
|
| 274 |
+
logger.info("Running Zumbi investigation with dados.gov.br integration")
|
| 275 |
+
|
| 276 |
+
# Extract organization codes from intent entities if available
|
| 277 |
+
org_codes = None
|
| 278 |
+
if intent.entities:
|
| 279 |
+
orgs = [e.value for e in intent.entities if e.type == "organization"]
|
| 280 |
+
if orgs:
|
| 281 |
+
# Map organization names to codes if needed
|
| 282 |
+
org_codes = orgs # For now, use as-is
|
| 283 |
+
|
| 284 |
+
# Run investigation with dados.gov.br enabled
|
| 285 |
+
investigation_result = await run_zumbi_investigation(
|
| 286 |
+
query=request.message,
|
| 287 |
+
organization_codes=org_codes,
|
| 288 |
+
enable_open_data=True, # Always enable dados.gov.br search
|
| 289 |
+
session_id=session_id,
|
| 290 |
+
user_id=session.user_id
|
| 291 |
+
)
|
| 292 |
+
|
| 293 |
+
if investigation_result["status"] == "error":
|
| 294 |
+
# Return error response
|
| 295 |
return ChatResponse(
|
| 296 |
session_id=session_id,
|
| 297 |
+
agent_id="zumbi",
|
| 298 |
+
agent_name="Zumbi dos Palmares",
|
| 299 |
+
message=f"❌ Erro na investigação: {investigation_result['error']}",
|
| 300 |
confidence=0.0,
|
| 301 |
+
metadata={"status": "error", "error": investigation_result["error"]}
|
| 302 |
)
|
| 303 |
|
| 304 |
+
# Format response using the integration helper
|
| 305 |
+
message = format_investigation_message(investigation_result)
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
# Add suggested actions if anomalies were found
|
| 308 |
+
suggested_actions = []
|
| 309 |
+
if investigation_result["anomalies_found"] > 0:
|
| 310 |
+
suggested_actions.append("🔍 Ver detalhes das anomalias")
|
| 311 |
+
suggested_actions.append("📊 Gerar relatório completo")
|
| 312 |
+
if investigation_result.get("open_data_available"):
|
| 313 |
+
suggested_actions.append("📂 Explorar dados abertos relacionados")
|
| 314 |
else:
|
| 315 |
+
suggested_actions.append("🔎 Refinar busca")
|
| 316 |
+
suggested_actions.append("📈 Analisar outros períodos")
|
| 317 |
|
| 318 |
response = AgentResponse(
|
| 319 |
agent_name="Zumbi dos Palmares",
|
| 320 |
status=AgentStatus.COMPLETED,
|
| 321 |
result={
|
| 322 |
"message": message,
|
| 323 |
+
"investigation_summary": {
|
| 324 |
+
"anomalies_found": investigation_result["anomalies_found"],
|
| 325 |
+
"records_analyzed": investigation_result["records_analyzed"],
|
| 326 |
+
"open_data_available": investigation_result.get("open_data_available", False),
|
| 327 |
+
"datasets_count": len(investigation_result.get("related_datasets", []))
|
| 328 |
+
},
|
| 329 |
+
"status": "completed",
|
| 330 |
+
"suggested_actions": suggested_actions
|
| 331 |
},
|
| 332 |
metadata={
|
| 333 |
+
"confidence": 0.9,
|
| 334 |
+
"investigation_id": session.current_investigation_id,
|
| 335 |
+
"dados_gov_enabled": True
|
| 336 |
}
|
| 337 |
)
|
| 338 |
agent_id = "zumbi"
|
|
|
|
| 393 |
|
| 394 |
# Prepare suggested actions based on response
|
| 395 |
suggested_actions = []
|
| 396 |
+
|
| 397 |
+
# Check if response has custom suggested actions
|
| 398 |
+
if hasattr(response, 'result') and isinstance(response.result, dict):
|
| 399 |
+
custom_actions = response.result.get("suggested_actions", [])
|
| 400 |
+
if custom_actions:
|
| 401 |
+
suggested_actions = custom_actions
|
| 402 |
+
|
| 403 |
+
# Fall back to default actions if no custom ones
|
| 404 |
+
if not suggested_actions:
|
| 405 |
+
if intent.type == IntentType.INVESTIGATE and not session.current_investigation_id:
|
| 406 |
+
suggested_actions = ["start_investigation", "view_examples", "learn_more"]
|
| 407 |
+
elif session.current_investigation_id:
|
| 408 |
+
suggested_actions = ["view_progress", "generate_report", "new_investigation"]
|
| 409 |
|
| 410 |
# Extract message from response
|
| 411 |
if hasattr(response, 'result') and isinstance(response.result, dict):
|