File size: 10,666 Bytes
dc1e705 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
#!/usr/bin/env python3
"""
Example: Maritaca AI integration with Drummond agent for conversational AI.
This example demonstrates how to use the Maritaca AI client (Sabiá-3 model)
with the Drummond agent for natural language generation in Brazilian Portuguese.
"""
import asyncio
import os
from datetime import datetime
from typing import List, Dict
from src.services.maritaca_client import create_maritaca_client, MaritacaModel
from src.agents.drummond import CommunicationAgent, AgentContext, AgentMessage
from src.core import get_logger
# Initialize logger
logger = get_logger(__name__)
async def example_maritaca_conversation():
"""Example of direct Maritaca AI conversation."""
print("\n=== Example: Direct Maritaca AI Conversation ===\n")
# Get API key from environment
api_key = os.getenv("MARITACA_API_KEY")
if not api_key:
print("❌ Please set MARITACA_API_KEY environment variable")
return
# Create Maritaca client
async with create_maritaca_client(
api_key=api_key,
model=MaritacaModel.SABIA_3
) as client:
# Example 1: Simple completion
print("1. Simple completion example:")
messages = [
{
"role": "system",
"content": "Você é um assistente especializado em transparência governamental brasileira."
},
{
"role": "user",
"content": "Explique brevemente o que é o Portal da Transparência."
}
]
response = await client.chat_completion(
messages=messages,
temperature=0.7,
max_tokens=200
)
print(f"Response: {response.content}")
print(f"Model: {response.model}")
print(f"Tokens used: {response.usage.get('total_tokens', 'N/A')}")
print(f"Response time: {response.response_time:.2f}s\n")
# Example 2: Streaming response
print("2. Streaming response example:")
messages.append({
"role": "assistant",
"content": response.content
})
messages.append({
"role": "user",
"content": "Como posso acessar dados de licitações?"
})
print("Streaming response: ", end="", flush=True)
async for chunk in await client.chat_completion(
messages=messages,
stream=True,
max_tokens=150
):
print(chunk, end="", flush=True)
print("\n")
# Example 3: Multi-turn conversation
print("3. Multi-turn conversation example:")
conversation = [
{
"role": "system",
"content": "Você é um especialista em análise de gastos públicos. Responda de forma clara e objetiva."
},
{
"role": "user",
"content": "Quais são os principais tipos de despesas do governo federal?"
}
]
# First turn
response = await client.chat_completion(conversation, max_tokens=200)
print(f"Assistant: {response.content}")
conversation.extend([
{"role": "assistant", "content": response.content},
{"role": "user", "content": "E como posso verificar essas despesas online?"}
])
# Second turn
response = await client.chat_completion(conversation, max_tokens=200)
print(f"Assistant: {response.content}")
async def example_drummond_with_maritaca():
"""Example of Drummond agent using Maritaca AI for NLG."""
print("\n=== Example: Drummond Agent with Maritaca AI ===\n")
# Get API key
api_key = os.getenv("MARITACA_API_KEY")
if not api_key:
print("❌ Please set MARITACA_API_KEY environment variable")
return
# Create context for Drummond agent
context = AgentContext(
user_id="example_user",
session_id="example_session",
metadata={
"llm_provider": "maritaca",
"llm_model": MaritacaModel.SABIA_3,
"api_key": api_key
}
)
# Initialize Drummond agent
drummond = CommunicationAgent()
# Example investigation data to communicate
investigation_data = {
"type": "anomaly_detection",
"title": "Despesas Irregulares em Contratos de TI",
"summary": "Análise identificou possíveis irregularidades em contratos de TI",
"findings": [
{
"contract_id": "CT-2024-001",
"supplier": "TechCorp Ltda",
"value": 5000000.00,
"anomaly_score": 0.92,
"issues": [
"Valor 300% acima da média de mercado",
"Fornecedor sem histórico anterior",
"Prazo de entrega incompatível"
]
},
{
"contract_id": "CT-2024-002",
"supplier": "DataSys S.A.",
"value": 3200000.00,
"anomaly_score": 0.85,
"issues": [
"Especificações técnicas genéricas",
"Ausência de justificativa para escolha"
]
}
],
"recommendations": [
"Realizar auditoria detalhada dos contratos",
"Verificar documentação dos fornecedores",
"Comparar com preços de referência do mercado"
]
}
# Create message for Drummond to process
message = AgentMessage(
sender="zumbi", # From Zumbi agent (anomaly detector)
receiver="drummond",
action="generate_report",
payload={
"investigation": investigation_data,
"target_audience": "citizens",
"language": "pt-BR",
"tone": "informative_accessible",
"channels": ["portal_web", "email"],
"use_maritaca": True # Signal to use Maritaca AI
}
)
print("Processing investigation report with Drummond + Maritaca AI...")
# Process with Drummond
# Note: This would normally use the agent's process method
# but for this example, we'll simulate the key parts
# Simulate Drummond using Maritaca for report generation
async with create_maritaca_client(api_key=api_key) as maritaca:
# Generate citizen-friendly report
report_prompt = f"""
Como especialista em comunicação governamental, crie um relatório acessível ao cidadão sobre a seguinte análise:
Tipo: {investigation_data['type']}
Título: {investigation_data['title']}
Resumo: {investigation_data['summary']}
Achados principais:
{format_findings(investigation_data['findings'])}
Recomendações:
{format_list(investigation_data['recommendations'])}
Requisitos:
- Linguagem clara e acessível
- Evite jargões técnicos
- Explique a importância para o cidadão
- Máximo 300 palavras
- Tom informativo mas não alarmista
"""
response = await maritaca.chat_completion(
messages=[
{
"role": "system",
"content": "Você é Carlos Drummond de Andrade, o comunicador oficial do sistema Cidadão.AI. Sua missão é traduzir análises técnicas em linguagem acessível ao cidadão brasileiro."
},
{
"role": "user",
"content": report_prompt
}
],
temperature=0.7,
max_tokens=500
)
print("\n📄 Relatório Gerado (via Maritaca AI):")
print("-" * 50)
print(response.content)
print("-" * 50)
# Generate email version
email_prompt = """
Agora crie uma versão resumida deste relatório para envio por email (máximo 150 palavras).
Inclua:
- Assunto sugestivo
- Resumo dos principais pontos
- Call-to-action para ver relatório completo
"""
response = await maritaca.chat_completion(
messages=[
{
"role": "system",
"content": "Você é um especialista em comunicação por email."
},
{
"role": "user",
"content": email_prompt
}
],
temperature=0.7,
max_tokens=200
)
print("\n📧 Versão Email (via Maritaca AI):")
print("-" * 50)
print(response.content)
print("-" * 50)
def format_findings(findings: List[Dict]) -> str:
"""Format findings for prompt."""
result = []
for i, finding in enumerate(findings, 1):
issues = ", ".join(finding['issues'])
result.append(
f"{i}. Contrato {finding['contract_id']} - {finding['supplier']}: "
f"R$ {finding['value']:,.2f} (Score anomalia: {finding['anomaly_score']:.0%}). "
f"Problemas: {issues}"
)
return "\n".join(result)
def format_list(items: List[str]) -> str:
"""Format list items."""
return "\n".join(f"- {item}" for item in items)
async def example_health_check():
"""Example of checking Maritaca AI service health."""
print("\n=== Example: Maritaca AI Health Check ===\n")
api_key = os.getenv("MARITACA_API_KEY")
if not api_key:
print("❌ Please set MARITACA_API_KEY environment variable")
return
async with create_maritaca_client(api_key=api_key) as client:
health = await client.health_check()
print(f"Status: {health['status']}")
print(f"Provider: {health['provider']}")
print(f"Model: {health['model']}")
print(f"Circuit Breaker: {health['circuit_breaker']}")
print(f"Timestamp: {health['timestamp']}")
if health.get('error'):
print(f"Error: {health['error']}")
async def main():
"""Run all examples."""
print("🤖 Maritaca AI + Drummond Agent Integration Examples")
print("=" * 60)
# Run examples
await example_health_check()
await example_maritaca_conversation()
await example_drummond_with_maritaca()
print("\n✅ All examples completed!")
if __name__ == "__main__":
# Note: Set MARITACA_API_KEY environment variable before running
asyncio.run(main()) |