anderson-ufrj commited on
Commit
d4c25be
·
1 Parent(s): c78f128

docs(examples): add comprehensive examples for dados.gov.br agent integration

Browse files

- Create example showing basic investigation with open data enrichment
- Add comparison example with/without enrichment to show benefits
- Include data availability analysis example
- Demonstrate practical usage patterns for developers

Files changed (1) hide show
  1. examples/agent_dados_gov_usage.py +227 -0
examples/agent_dados_gov_usage.py ADDED
@@ -0,0 +1,227 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Example: Using Zumbi agent with dados.gov.br integration
3
+
4
+ This example demonstrates how the Zumbi agent can use the dados.gov.br
5
+ integration to enrich investigation data with open government datasets.
6
+ """
7
+
8
+ import asyncio
9
+ import json
10
+ from datetime import datetime
11
+
12
+ from src.agents.zumbi import InvestigatorAgent, InvestigationRequest
13
+ from src.agents.deodoro import AgentContext
14
+
15
+
16
+ async def example_investigation_with_open_data():
17
+ """
18
+ Example of running an investigation with open data enrichment.
19
+ """
20
+ # Initialize the Zumbi agent
21
+ agent = InvestigatorAgent()
22
+ await agent.initialize()
23
+
24
+ # Create investigation request with open data enrichment enabled
25
+ request = InvestigationRequest(
26
+ query="Investigate health ministry contracts for anomalies",
27
+ organization_codes=["26000"], # Ministry of Health
28
+ anomaly_types=["price_anomaly", "vendor_concentration"],
29
+ max_records=50,
30
+ enable_open_data_enrichment=True # Enable dados.gov.br integration
31
+ )
32
+
33
+ # Create agent context
34
+ context = AgentContext(
35
+ investigation_id=f"inv_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
36
+ user_id="example_user",
37
+ correlation_id="example_001"
38
+ )
39
+
40
+ # Create agent message
41
+ message = {
42
+ "action": "investigate",
43
+ "payload": request.model_dump()
44
+ }
45
+
46
+ print("Starting investigation with open data enrichment...")
47
+ print(f"Investigation ID: {context.investigation_id}")
48
+ print(f"Query: {request.query}")
49
+ print(f"Organization: {request.organization_codes}")
50
+ print("-" * 50)
51
+
52
+ # Process the investigation
53
+ response = await agent.process(message, context)
54
+
55
+ # Display results
56
+ if response.status == "completed":
57
+ result = response.result
58
+
59
+ print(f"\n✅ Investigation Status: {result['status']}")
60
+ print(f"📊 Records Analyzed: {result['metadata']['records_analyzed']}")
61
+ print(f"⚠️ Anomalies Found: {result['metadata']['anomalies_detected']}")
62
+
63
+ # Show anomalies
64
+ if result['anomalies']:
65
+ print("\n🔍 Detected Anomalies:")
66
+ for i, anomaly in enumerate(result['anomalies'], 1):
67
+ print(f"\n{i}. {anomaly['anomaly_type'].upper()}")
68
+ print(f" Severity: {anomaly['severity']:.2f}")
69
+ print(f" Confidence: {anomaly['confidence']:.2f}")
70
+ print(f" Description: {anomaly['description']}")
71
+
72
+ # Check if open data was found
73
+ if anomaly.get('evidence', {}).get('open_data_available'):
74
+ datasets = anomaly['evidence'].get('related_datasets', [])
75
+ if datasets:
76
+ print(f" 📂 Related Open Datasets Found: {len(datasets)}")
77
+ for ds in datasets[:2]: # Show first 2 datasets
78
+ print(f" - {ds.get('title', 'Unknown dataset')}")
79
+
80
+ # Show summary
81
+ print(f"\n📋 Investigation Summary:")
82
+ print(f" Total Contracts: {result['summary']['total_records']}")
83
+ print(f" Anomalies Found: {result['summary']['anomalies_found']}")
84
+
85
+ # Check for open data enrichment
86
+ if 'open_data_stats' in result['summary']:
87
+ stats = result['summary']['open_data_stats']
88
+ print(f"\n📊 Open Data Enrichment:")
89
+ print(f" Organizations with Open Data: {stats['organizations_with_data']}")
90
+ print(f" Total Datasets Referenced: {stats['total_datasets']}")
91
+
92
+ else:
93
+ print(f"\n❌ Investigation Failed: {response.error}")
94
+
95
+ # Cleanup
96
+ await agent.shutdown()
97
+
98
+
99
+ async def example_compare_with_without_open_data():
100
+ """
101
+ Compare investigation results with and without open data enrichment.
102
+ """
103
+ agent = InvestigatorAgent()
104
+ await agent.initialize()
105
+
106
+ # Base request parameters
107
+ base_params = {
108
+ "query": "Find suspicious contracts in education sector",
109
+ "organization_codes": ["25000"], # Ministry of Education
110
+ "anomaly_types": ["price_anomaly"],
111
+ "max_records": 30
112
+ }
113
+
114
+ # Context
115
+ context = AgentContext(
116
+ investigation_id="comparison_test",
117
+ user_id="example_user",
118
+ correlation_id="compare_001"
119
+ )
120
+
121
+ print("🔬 Comparing investigations with and without open data enrichment\n")
122
+
123
+ # Run without open data
124
+ print("1️⃣ Investigation WITHOUT open data enrichment:")
125
+ request1 = InvestigationRequest(**base_params, enable_open_data_enrichment=False)
126
+ message1 = {"action": "investigate", "payload": request1.model_dump()}
127
+
128
+ start_time = asyncio.get_event_loop().time()
129
+ response1 = await agent.process(message1, context)
130
+ time1 = asyncio.get_event_loop().time() - start_time
131
+
132
+ anomalies1 = len(response1.result.get('anomalies', []))
133
+ print(f" ⏱️ Time: {time1:.2f}s")
134
+ print(f" ⚠️ Anomalies found: {anomalies1}")
135
+
136
+ # Run with open data
137
+ print("\n2️⃣ Investigation WITH open data enrichment:")
138
+ request2 = InvestigationRequest(**base_params, enable_open_data_enrichment=True)
139
+ message2 = {"action": "investigate", "payload": request2.model_dump()}
140
+
141
+ start_time = asyncio.get_event_loop().time()
142
+ response2 = await agent.process(message2, context)
143
+ time2 = asyncio.get_event_loop().time() - start_time
144
+
145
+ anomalies2 = len(response2.result.get('anomalies', []))
146
+ datasets_found = 0
147
+
148
+ # Count datasets found
149
+ for anomaly in response2.result.get('anomalies', []):
150
+ if anomaly.get('evidence', {}).get('related_datasets'):
151
+ datasets_found += len(anomaly['evidence']['related_datasets'])
152
+
153
+ print(f" ⏱️ Time: {time2:.2f}s")
154
+ print(f" ⚠️ Anomalies found: {anomalies2}")
155
+ print(f" 📂 Open datasets referenced: {datasets_found}")
156
+
157
+ print("\n📊 Comparison Summary:")
158
+ print(f" Time difference: +{time2-time1:.2f}s ({((time2-time1)/time1)*100:.1f}% slower)")
159
+ print(f" Additional context gained: {datasets_found} datasets")
160
+ print(f" Enhanced investigation: {'Yes' if datasets_found > 0 else 'No'}")
161
+
162
+ await agent.shutdown()
163
+
164
+
165
+ async def example_analyze_topic_availability():
166
+ """
167
+ Example of using dados.gov.br to analyze data availability before investigation.
168
+ """
169
+ from src.tools.dados_gov_tool import DadosGovTool
170
+
171
+ tool = DadosGovTool()
172
+
173
+ print("🔍 Analyzing open data availability for different government topics\n")
174
+
175
+ topics = ["saúde", "educação", "segurança pública", "transportes"]
176
+
177
+ for topic in topics:
178
+ print(f"\n📋 Topic: {topic.upper()}")
179
+
180
+ result = await tool._execute(
181
+ action="analyze",
182
+ topic=topic
183
+ )
184
+
185
+ if result.success:
186
+ data = result.data
187
+ print(f" Total datasets: {data['total_datasets']}")
188
+ print(f" Coverage: Federal({data['coverage']['federal']}), "
189
+ f"State({data['coverage']['state']}), "
190
+ f"Municipal({data['coverage']['municipal']})")
191
+ print(f" Formats: {', '.join(data['available_formats'][:5])}")
192
+
193
+ if data['top_organizations']:
194
+ print(f" Top publishers:")
195
+ for org, count in list(data['top_organizations'].items())[:3]:
196
+ print(f" - {org}: {count} datasets")
197
+ else:
198
+ print(f" ❌ Error: {result.error}")
199
+
200
+ await tool.service.close()
201
+
202
+
203
+ if __name__ == "__main__":
204
+ # Run examples
205
+ print("=" * 70)
206
+ print("CIDADÃO.AI - Zumbi Agent with dados.gov.br Integration Examples")
207
+ print("=" * 70)
208
+
209
+ # Choose which example to run
210
+ examples = {
211
+ "1": ("Basic investigation with open data", example_investigation_with_open_data),
212
+ "2": ("Compare with/without enrichment", example_compare_with_without_open_data),
213
+ "3": ("Analyze topic data availability", example_analyze_topic_availability),
214
+ }
215
+
216
+ print("\nAvailable examples:")
217
+ for key, (name, _) in examples.items():
218
+ print(f"{key}. {name}")
219
+
220
+ choice = input("\nSelect example (1-3): ").strip()
221
+
222
+ if choice in examples:
223
+ _, example_func = examples[choice]
224
+ asyncio.run(example_func())
225
+ else:
226
+ print("Invalid choice. Running default example...")
227
+ asyncio.run(example_investigation_with_open_data())