-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
375 lines (308 loc) · 11.4 KB
/
main.go
File metadata and controls
375 lines (308 loc) · 11.4 KB
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log/slog"
"net/http"
"os"
"github.com/jessevdk/go-flags"
"github.com/plexusone/agent-team-stats/pkg/config"
"github.com/plexusone/agent-team-stats/pkg/logging"
"github.com/plexusone/agent-team-stats/pkg/models"
)
var logger *slog.Logger
// Options defines the CLI options structure
type Options struct {
// Global options
Verbose bool `short:"v" long:"verbose" description:"Show verbose debug information"`
Version bool `long:"version" description:"Show version information"`
// Commands
Search SearchCommand `command:"search" description:"Search for verified statistics on a topic"`
}
// SearchCommand defines options for the search command
type SearchCommand struct {
// Positional arguments
Args struct {
Topic string `positional-arg-name:"topic" description:"Topic to search for statistics"`
} `positional-args:"yes" required:"yes"`
// Search options
MinStats int `short:"m" long:"min-stats" default:"10" description:"Minimum number of verified statistics required"`
MaxCandidates int `short:"c" long:"max-candidates" default:"50" description:"Maximum number of candidate statistics to gather"`
ReputableOnly bool `short:"r" long:"reputable-only" description:"Only use reputable sources"`
Output string `short:"o" long:"output" default:"both" choice:"json" choice:"text" choice:"both" description:"Output format"`
Direct bool `short:"d" long:"direct" description:"Use direct LLM search (faster, like ChatGPT)"`
DirectVerify bool `long:"direct-verify" description:"Verify LLM claims with verification agent (requires --direct and verification agent running)"`
// Orchestrator options
OrchestratorURL string `long:"orchestrator-url" description:"Orchestrator URL (overrides env var)" env:"ORCHESTRATOR_URL"`
}
// Execute runs the search command
func (cmd *SearchCommand) Execute([]string) error { // param `args []string`
topic := cmd.Args.Topic
cfg := config.LoadConfig()
fmt.Printf("Searching for statistics about: %s\n", topic)
fmt.Printf("Target: %d verified statistics\n", cmd.MinStats)
var resp *models.OrchestrationResponse
var err error
// Use direct LLM mode if requested
if cmd.Direct {
if cmd.DirectVerify {
fmt.Println("mode: Direct LLM search + Verification Agent (hybrid)")
} else {
fmt.Println("mode: Direct LLM search (fast, like ChatGPT)")
}
fmt.Println()
resp, err = callDirectLLMSearch(topic, cmd.MinStats, cmd.DirectVerify)
if err != nil {
return fmt.Errorf("direct LLM search failed: %w", err)
}
// Direct mode - just print results, no retry loop
printResults(resp, cmd.Output)
return nil
}
fmt.Println("mode: Multi-agent verification pipeline")
fmt.Println()
// Override orchestrator URL if provided
if cmd.OrchestratorURL != "" {
cfg.OrchestratorURL = cmd.OrchestratorURL
}
// Create orchestration request
req := &models.OrchestrationRequest{
Topic: topic,
MinVerifiedStats: cmd.MinStats,
MaxCandidates: cmd.MaxCandidates,
ReputableOnly: cmd.ReputableOnly,
}
// Call orchestration agent
resp, err = callOrchestrator(cfg, req)
if err != nil {
return fmt.Errorf("orchestration failed: %w", err)
}
// Handle partial results with retry logic
allStatistics := resp.Statistics
totalVerified := resp.VerifiedCount
retryCount := 0
maxRetries := 3
for resp.Partial && retryCount < maxRetries {
fmt.Printf("\n⚠️ PARTIAL RESULTS: Found %d/%d statistics\n\n", resp.VerifiedCount, resp.TargetCount)
// Print what we have so far
printResults(resp, cmd.Output)
// Ask user if they want to continue
fmt.Printf("\n\nWould you like to search for more statistics? (y/n): ")
var answer string
if _, err := fmt.Scanln(&answer); err != nil {
return fmt.Errorf("orchestration failed: %w", err)
}
if answer != "y" && answer != "Y" && answer != "yes" {
fmt.Printf("\nStopping with %d verified statistics.\n", totalVerified)
break
}
// Continue searching
fmt.Println("\nContinuing search for more statistics...")
retryCount++
// Calculate how many more we need
stillNeeded := req.MinVerifiedStats - totalVerified
fmt.Printf("Attempting to find %d more statistics (attempt %d/%d)...\n\n", stillNeeded, retryCount, maxRetries)
// Make another request with increased candidates limit
continueReq := &models.OrchestrationRequest{
Topic: topic,
MinVerifiedStats: stillNeeded,
MaxCandidates: cmd.MaxCandidates + (retryCount * 20), // Increase search space
ReputableOnly: cmd.ReputableOnly,
}
continueResp, err := callOrchestrator(cfg, continueReq)
if err != nil {
fmt.Printf("⚠️ Continuation failed: %v\n", err)
fmt.Printf("Stopping with %d verified statistics.\n", totalVerified)
break
}
// Merge new statistics with existing ones
allStatistics = append(allStatistics, continueResp.Statistics...)
totalVerified += continueResp.VerifiedCount
// Update response for next iteration
resp = continueResp
resp.VerifiedCount = totalVerified
resp.Statistics = allStatistics
resp.Partial = totalVerified < req.MinVerifiedStats
if !resp.Partial {
fmt.Printf("\n✓ Target reached! Found %d verified statistics total.\n\n", totalVerified)
}
}
if retryCount >= maxRetries && resp.Partial {
fmt.Printf("\n⚠️ Maximum retries (%d) reached. Found %d/%d statistics.\n\n", maxRetries, totalVerified, req.MinVerifiedStats)
}
// Print final results if not already printed
if !resp.Partial {
printResults(resp, cmd.Output)
}
return nil
}
func main() {
logger = logging.NewAgentLogger("cli")
var opts Options
parser := flags.NewParser(&opts, flags.Default)
parser.LongDescription = `Statistics Agent - Multi-Agent System for Finding Verified Statistics
ARCHITECTURE:
This system uses a 4-agent architecture:
1. Research Agent (port 8001)
- Searches for statistics from web sources via Serper/SerpAPI
- Returns URLs with metadata
2. Synthesis Agent (port 8004)
- Fetches webpage content from URLs
- Uses LLM to extract statistics intelligently
3. Verification Agent (port 8002)
- Re-fetches source URLs
- Validates statistics in their sources
- Checks for exact excerpts and values
4. Orchestration Agent (port 8000 ADK / 8003 Eino)
- Coordinates the 4-agent workflow
- Manages retry logic
- Ensures quality standards
ENVIRONMENT VARIABLES:
LLM_PROVIDER LLM provider (gemini, claude, openai, ollama)
GEMINI_API_KEY API key for Gemini
CLAUDE_API_KEY API key for Claude
OPENAI_API_KEY API key for OpenAI
SEARCH_PROVIDER Search provider (serper, serpapi)
SERPER_API_KEY API key for Serper
SERPAPI_API_KEY API key for SerpAPI
ORCHESTRATOR_URL Orchestrator URL (default: http://localhost:8000)
EXAMPLES:
stats-agent search "climate change"
stats-agent search "AI adoption rates" --min-stats 15
stats-agent search "cybersecurity 2024" --output json
stats-agent search "renewable energy" --reputable-only
`
// Parse arguments
_, err := parser.Parse()
if err != nil {
if flagsErr, ok := err.(*flags.Error); ok {
if flagsErr.Type == flags.ErrHelp {
os.Exit(0)
}
}
os.Exit(1)
}
// Handle version flag
if opts.Version {
fmt.Println("stats-agent version 1.0.0")
fmt.Println("Multi-LLM support: Gemini, Claude, OpenAI, Ollama")
os.Exit(0)
}
}
func callDirectLLMSearch(topic string, minStats int, verify bool) (*models.OrchestrationResponse, error) {
// Get direct agent URL from config or use default
directURL := os.Getenv("DIRECT_AGENT_URL")
if directURL == "" {
directURL = "http://localhost:8005"
}
// Create request
type DirectSearchRequest struct {
Topic string `json:"topic"`
MinStats int `json:"min_stats"`
VerifyWithWeb bool `json:"verify_with_web"`
}
reqBody := DirectSearchRequest{
Topic: topic,
MinStats: minStats,
VerifyWithWeb: verify,
}
reqData, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Call direct agent
url := fmt.Sprintf("%s/search", directURL)
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", url, bytes.NewReader(reqData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
httpResp, err := client.Do(httpReq) //nolint:gosec // G704: URL from config, not user input
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d: %s", httpResp.StatusCode, httpResp.Status)
}
var resp models.OrchestrationResponse
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &resp, nil
}
func callOrchestrator(cfg *config.Config, req *models.OrchestrationRequest) (*models.OrchestrationResponse, error) {
reqData, err := json.Marshal(req)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
url := fmt.Sprintf("%s/orchestrate", cfg.OrchestratorURL)
httpReq, err := http.NewRequestWithContext(context.Background(), "POST", url, bytes.NewReader(reqData))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}
httpReq.Header.Set("Content-Type", "application/json")
client := &http.Client{}
httpResp, err := client.Do(httpReq) //nolint:gosec // G704: URL from config, not user input
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer httpResp.Body.Close()
if httpResp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("HTTP %d: %s", httpResp.StatusCode, httpResp.Status)
}
var resp models.OrchestrationResponse
if err := json.NewDecoder(httpResp.Body).Decode(&resp); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}
return &resp, nil
}
func printResults(resp *models.OrchestrationResponse, outputFormat string) {
if outputFormat == "json" {
// JSON only
jsonData, err := json.MarshalIndent(resp.Statistics, "", " ")
if err != nil {
logger.Error("failed to marshal JSON", "error", err)
return
}
fmt.Println(string(jsonData))
return
}
// Text format (header + stats)
fmt.Printf("=== Statistics Search Results ===\n\n")
fmt.Printf("Topic: %s\n", resp.Topic)
fmt.Printf("Found: %d verified statistics (from %d candidates)\n", resp.VerifiedCount, resp.TotalCandidates)
fmt.Printf("Failed verification: %d\n", resp.FailedCount)
fmt.Printf("Timestamp: %s\n\n", resp.Timestamp.Format("2006-01-02 15:04:05"))
if len(resp.Statistics) == 0 {
fmt.Println("No verified statistics found.")
return
}
if outputFormat == "both" {
// Print JSON
fmt.Println("=== Verified Statistics (JSON) ===")
fmt.Println()
jsonData, err := json.MarshalIndent(resp.Statistics, "", " ")
if err != nil {
logger.Error("failed to marshal JSON", "error", err)
return
}
fmt.Println(string(jsonData))
fmt.Println()
}
// Human-readable format
fmt.Println("=== Human-Readable Format ===")
fmt.Println()
for i, stat := range resp.Statistics {
fmt.Printf("%d. %s\n", i+1, stat.Name)
fmt.Printf(" Value: %v %s\n", stat.Value, stat.Unit)
fmt.Printf(" Source: %s\n", stat.Source)
fmt.Printf(" URL: %s\n", stat.SourceURL)
fmt.Printf(" Excerpt: \"%s\"\n", stat.Excerpt)
fmt.Printf(" Verified: ✓\n")
fmt.Printf(" Date Found: %s\n\n", stat.DateFound.Format("2006-01-02"))
}
}