-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.go
More file actions
318 lines (252 loc) · 9.23 KB
/
main.go
File metadata and controls
318 lines (252 loc) · 9.23 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
// Copyright 2026 Simone Vellei
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"bufio"
"context"
"flag"
"fmt"
"os"
"strings"
"time"
"github.com/henomis/phero/agent"
"github.com/henomis/phero/llm"
"github.com/henomis/phero/llm/middleware"
"github.com/henomis/phero/llm/openai"
simplemem "github.com/henomis/phero/memory/simple"
)
func main() {
var (
seedFlag string
numAgents int
numRounds int
topk int
timeout time.Duration
interact bool
)
flag.StringVar(&seedFlag, "seed",
"A controversial new municipal policy proposes banning private gas vehicles from the city center by 2027. "+
"Environmental groups, small business owners, commuters, taxi drivers, and tech startups are all reacting.",
"Seed text or path to a seed document file describing the scenario to simulate")
flag.IntVar(&numAgents, "agents", 8, "Number of persona agents to spawn (recommended max: 20)")
flag.IntVar(&numRounds, "rounds", 5, "Number of simulation rounds")
flag.IntVar(&topk, "topk", 15, "Number of recent feed entries visible to each agent per round")
flag.DurationVar(&timeout, "timeout", 15*time.Minute, "Overall timeout for the full pipeline")
flag.BoolVar(&interact, "interact", false, "Drop into interactive Q&A with the report agent after the simulation")
flag.Parse()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
llmClient, llmInfo := buildLLMFromEnv()
rateLimiter, stop, err := middleware.NewLimiter(1, 4)
if err != nil {
panic(fmt.Errorf("rate limiter: %w", err))
}
defer stop()
llmClient = llm.Use(llmClient, rateLimiter)
seedText := readSeed(seedFlag)
estimatedCalls := numAgents*numRounds + 3 // knowledge + personas + report
fmt.Println("multi-agent architecture example: social simulation")
fmt.Println("- llm:", llmInfo)
fmt.Printf("- agents: %d rounds: %d topk: %d\n", numAgents, numRounds, topk)
fmt.Printf("- estimated LLM calls: ~%d\n", estimatedCalls)
fmt.Println()
// Phase 1: Extract structured world facts from the seed material.
fmt.Println("phase 1/4: extracting world facts...")
worldFacts, err := extractWorldFacts(ctx, llmClient, seedText)
if err != nil {
panic(fmt.Errorf("world facts: %w", err))
}
fmt.Println("world facts extracted.")
fmt.Println()
// Phase 2: Generate personas grounded in the world facts.
fmt.Printf("phase 2/4: generating %d personas...\n", numAgents)
personas, err := generatePersonas(ctx, llmClient, worldFacts, numAgents)
if err != nil {
panic(fmt.Errorf("personas: %w", err))
}
fmt.Printf("%d personas generated:\n", len(personas))
for _, p := range personas {
fmt.Printf(" - %s (%s)\n", p.Name, p.Personality)
}
fmt.Println()
// Phase 3: Build agents and run simulation rounds.
paAgents := make([]*personaAgent, 0, len(personas))
for _, p := range personas {
pa, err := buildPersonaAgent(llmClient, p, numRounds)
if err != nil {
panic(err)
}
paAgents = append(paAgents, pa)
}
sim := newSimulation(paAgents, topk)
fmt.Printf("phase 3/4: running %d simulation rounds...\n", numRounds)
for round := 1; round <= numRounds; round++ {
fmt.Printf(" round %d/%d\n", round, numRounds)
err := sim.RunRound(ctx, round, numRounds, func(e FeedEntry) {
fmt.Printf(" [%s] %s\n", e.Author, truncate(e.Post, 80))
})
if err != nil {
panic(fmt.Errorf("round %d: %w", round, err))
}
}
fmt.Println()
// Phase 4: Synthesize a prediction report from the full transcript.
fmt.Println("phase 4/4: generating simulation report...")
reportAgent, err := buildReportAgent(llmClient)
if err != nil {
panic(err)
}
transcript := sim.Transcript()
if err := os.WriteFile("transcript.txt", []byte(transcript), 0o666); err != nil {
panic(fmt.Errorf("write transcript: %w", err))
}
reportPrompt := fmt.Sprintf(
"World facts:\n%s\n\nSimulation transcript:\n%s\n\nAnalyze this simulation and produce the report.",
worldFacts, transcript,
)
reportResult, err := reportAgent.Run(ctx, llm.Text(reportPrompt))
if err != nil {
panic(fmt.Errorf("report agent: %w", err))
}
fmt.Println()
fmt.Println("=== simulation report ===")
fmt.Println(strings.TrimSpace(reportResult.TextContent()))
// Optional phase 5: interactive Q&A with the report agent.
if interact {
fmt.Println()
fmt.Println("=== interactive mode (type /exit to quit) ===")
interactiveREPL(ctx, reportAgent)
}
}
// extractWorldFacts runs a knowledge-extraction agent over seedText and returns
// a concise neutral summary of the key facts, entities, and tensions.
func extractWorldFacts(ctx context.Context, llmClient llm.LLM, seedText string) (string, error) {
knowledgeAgent, err := agent.New(
llmClient,
"KnowledgeExtractor",
strings.TrimSpace(`You are a knowledge extraction specialist.
Read the provided source material and produce a concise, neutral "world facts" summary (200–300 words) covering:
- The central situation, event, or topic
- Key entities (people, organizations, groups, policies) and their relationships
- Main tensions and conflicting interests
- Current state and open questions
Be factual and neutral. Do not take a stance.`),
)
if err != nil {
return "", err
}
result, err := knowledgeAgent.Run(ctx, llm.Text("Extract world facts from this seed material:\n\n"+seedText))
if err != nil {
return "", err
}
return strings.TrimSpace(result.TextContent()), nil
}
// buildReportAgent creates the analyst agent used to synthesize the simulation transcript
// into a structured prediction report. It uses memory so follow-up questions in the
// interactive REPL have full context.
func buildReportAgent(llmClient llm.LLM) (*agent.Agent, error) {
a, err := agent.New(
llmClient,
"ReportAgent",
strings.TrimSpace(`You are a simulation analyst specializing in emergent social dynamics.
You receive the full transcript of a multi-agent social simulation. Produce a structured report with these sections:
## Opinion Evolution
How did individual and group opinions shift round by round?
## Coalitions & Dynamics
Which agents aligned, which opposed, and what social dynamics emerged?
## Key Inflection Points
Which moments (cite round and agent) most significantly changed the conversation?
## Final Outlook
Based on the simulation, what is the most likely outcome or trajectory?
Be analytical. Cite specific agents by name and reference round numbers for key events.`),
)
if err != nil {
return nil, err
}
// Large memory so the full transcript + report stay in context for the REPL.
a.SetMemory(simplemem.New(100))
return a, nil
}
// interactiveREPL runs a read-eval-print loop, passing each user line to reportAgent.
// The agent retains memory of the simulation transcript and its previous answers.
func interactiveREPL(ctx context.Context, reportAgent *agent.Agent) {
scanner := bufio.NewScanner(os.Stdin)
scanner.Buffer(make([]byte, 0, 64*1024), 512*1024)
fmt.Print("\n> ")
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" {
fmt.Print("> ")
continue
}
if line == "/exit" || line == "/quit" || line == "/q" {
break
}
turnCtx, cancel := context.WithTimeout(ctx, 2*time.Minute)
result, err := reportAgent.Run(turnCtx, llm.Text(line))
cancel()
if err != nil {
fmt.Printf("error: %v\n", err)
} else {
fmt.Printf("\n%s\n", strings.TrimSpace(result.TextContent()))
}
fmt.Print("\n> ")
}
fmt.Println("\nGoodbye!")
}
// readSeed reads seed text from a file path. If the path cannot be opened,
// it treats the argument as inline text.
func readSeed(s string) string {
data, err := os.ReadFile(s)
if err == nil {
return strings.TrimSpace(string(data))
}
return strings.TrimSpace(s)
}
// truncate shortens s to at most n characters (collapsing newlines) and appends "…".
func truncate(s string, n int) string {
s = strings.ReplaceAll(s, "\n", " ")
if len(s) <= n {
return s
}
return s[:n-1] + "…"
}
// buildLLMFromEnv constructs an OpenAI-compatible LLM client from environment
// variables: OPENAI_API_KEY, OPENAI_BASE_URL, OPENAI_MODEL.
// When no key or base URL are set, it defaults to a local Ollama endpoint.
func buildLLMFromEnv() (llm.LLM, string) {
apiKey := strings.TrimSpace(os.Getenv("OPENAI_API_KEY"))
baseURL := strings.TrimSpace(os.Getenv("OPENAI_BASE_URL"))
model := strings.TrimSpace(os.Getenv("OPENAI_MODEL"))
if apiKey == "" && baseURL == "" {
baseURL = openai.OllamaBaseURL
}
if model == "" {
if baseURL == openai.OllamaBaseURL && apiKey == "" {
model = "gpt-oss:20b-cloud"
} else {
model = openai.DefaultModel
}
}
opts := []openai.Option{openai.WithModel(model)}
if baseURL != "" {
opts = append(opts, openai.WithBaseURL(baseURL))
}
client := openai.New(apiKey, opts...)
info := fmt.Sprintf("model=%s base_url=%s", model, baseURL)
if baseURL == "" {
info = fmt.Sprintf("model=%s", model)
}
return client, info
}