-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfandango.py
More file actions
435 lines (354 loc) · 14.2 KB
/
fandango.py
File metadata and controls
435 lines (354 loc) · 14.2 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# =============================================================================
# File name: fandango.py
# Author: INV 4039 ; B 1231
# Date created: 2025-06-10
# Version = "1.0"
# License = "CC0 1.0"
# =============================================================================
""" A quirky multi-agent "gravity-powered" chat simulator (inspired by L.Cixin)."""
# =============================================================================
# Imports
import re
import csv
import json
import random
import requests
import subprocess
from datetime import datetime, timezone
from dataclasses import dataclass, field
from typing import List, Tuple
from pathlib import Path
import __hello__
# ---------------------------------------------------------------------------
# Configuration
@dataclass
class Config:
"""Global configuration toggles and file paths.
Attributes:
list_available_llms (bool): Whether to print the list of installed models.
ollama_host (str): Host URL for the local Ollama API.
full_log (bool): Whether to write detailed logs to file.
full_log_filepath (Path): Path to the detailed log file.
chat_only_log (bool): Whether to write chat-only logs to file.
chat_only_filepath (Path): Path to the chat-only log file.
export_as_dataset (bool): Whether to append conversation data to a CSV dataset.
csv_filepath (Path): Path to the conversation CSV dataset.
"""
list_available_llms: bool = False
ollama_host: str = "http://localhost:11434"
full_log: bool = True
if full_log:
full_log_filepath: Path = Path("full_log.txt")
chat_only_log: bool = True
if chat_only_log:
chat_only_filepath: Path = Path("chat_only.txt")
export_as_dataset: bool = True
if export_as_dataset:
csv_filepath: Path = Path("conversation_dataset.csv")
# Instantiate a global config
cfg = Config()
# Create a session for HTTP requests
HTTP = requests.Session()
HTTP.headers.update({"Content-Type": "application/json"})
# ---------------------------------------------------------------------------
# Functions and classes
def ensure_model(model_name: str) -> None:
"""Ensure the Ollama model is present locally, pulling if necessary.
Args:
model_name (str): Name of the Ollama model to check or pull.
Raises:
RuntimeError: If the `ollama` CLI is missing.
subprocess.CalledProcessError: If any CLI invocation fails.
"""
try:
result = subprocess.run(
["ollama", "list"], check=True, text=True, capture_output=True
)
except FileNotFoundError as exc:
raise RuntimeError(
"The 'ollama' CLI was not found. Please install Ollama or add it to PATH."
) from exc
installed = result.stdout
if cfg.list_available_llms:
print(installed)
if model_name in installed:
print(f"Model '{model_name}' already installed.")
else:
print(f"Pulling model '{model_name}'...")
subprocess.run(["ollama", "pull", model_name], check=True)
print("Pull complete.")
def log_full(agent: str, prompt: str, raw: str, utterance: str) -> None:
"""Append a full debug entry (prompt, raw response, extracted utterance).
Args:
agent (str): Name of the agent issuing the prompt.
prompt (str): The text prompt sent to Ollama.
raw (str): The raw response text received.
utterance (str): The cleaned final utterance extracted.
"""
if not cfg.full_log:
return
ts = datetime.now(timezone.utc).isoformat()
with cfg.full_log_filepath.open("a", encoding="utf-8") as f:
f.write(f"=== [{ts}] Agent: {agent} ===\n")
f.write("PROMPT:\n" + prompt + "\n\n")
f.write("RAW RESPONSE:\n" + raw + "\n\n")
f.write("UTTERANCE:\n" + utterance + "\n")
f.write("=" * 50 + "\n\n")
def log_chat(agent: str, utterance: str) -> None:
"""Append a chat-only log entry (agent + utterance).
Args:
agent (str): Name of the agent speaking.
utterance (str): The final cleaned utterance text.
"""
if not cfg.chat_only_log:
return
ts = datetime.now(timezone.utc).isoformat()
with cfg.chat_only_filepath.open("a", encoding="utf-8") as f:
f.write(f"{ts} | {agent}: {utterance}\n")
def log_csv(agent: str, turn: int, utterance: str, topic: float, tone: float) -> None:
"""Append the conversation to CSV (with header if new).
Args:
agent (str): Name of the agent.
turn (int): Conversation turn index.
utterance (str): Agent's spoken utterance.
topic (float): Agent's topic interest value.
tone (float): Agent's tone value.
"""
if not cfg.export_as_dataset:
return
is_new = not cfg.csv_filepath.exists()
with cfg.csv_filepath.open("a", newline="", encoding="utf-8") as csvf:
writer = csv.writer(csvf)
if is_new:
writer.writerow(
["Agent", "Turn", "Timestamp", "Utterance", "TopicInterest", "Tone"]
)
writer.writerow(
[
agent,
turn,
datetime.now(timezone.utc).isoformat(),
utterance,
f"{topic:.3f}",
f"{tone:.3f}",
]
)
def clean_utterance(utt: str) -> str:
"""Strip <think> sections, surrounding quotes, and whitespace from an utterance.
Args:
utt (str): Raw utterance string, possibly with <think> markers.
Returns:
str: Cleaned utterance.
"""
utt = re.sub(r"<think>.*?</think>", "", utt, flags=re.DOTALL)
return utt.strip().strip('"').strip()
def query_ollama(
model: str, prompt: str, max_tokens: int = 60
) -> Tuple[str, str]:
"""Send a prompt to the local Ollama API and parse the response.
Args:
model (str): Ollama model identifier.
prompt (str): The text prompt to send.
max_tokens (int): Maximum token output from model.
Returns:
Tuple[str, str]: (raw_response_text, cleaned_utterance).
"""
payload = {
"model": model,
"prompt": prompt,
"max_tokens": max_tokens,
"stream": False,
}
url = f"{cfg.ollama_host}/api/generate"
resp = HTTP.post(url, json=payload)
resp.raise_for_status()
raw_text = resp.text.strip()
lines = [line for line in raw_text.splitlines() if line.strip()]
parts: List[str] = []
for line in lines:
try:
obj = json.loads(line)
if "response" in obj:
parts.append(obj["response"])
elif "choices" in obj and obj["choices"]:
parts.append(obj["choices"][0].get("text", ""))
else:
parts.append(json.dumps(obj))
except json.JSONDecodeError:
parts.append(line)
final = " ".join(parts).strip()
return raw_text, final
@dataclass
class ConversationSummary:
"""Keeps a rolling context of the last N lines in the conversation.
Attributes:
max_lines (int): Maximum number of lines to keep in the summary.
lines (List[str]): Current summary lines.
"""
max_lines: int = 15
option1: str = "This conversation is just getting started."
option2: str = "For decades, scientists argued that rising carbon levels would cause an increasingly unstable ecosystem ☢️."
lines: List[str] = field(
default_factory=lambda: [ConversationSummary.option1]
)
@property
def text(self) -> str:
"""Current summary text joined by newlines."""
return "\n".join(self.lines)
def update(self, speaker: str, utterance: str, topic_interest: float, subj: str, tone: float, mood: str) -> None:
"""Add a new line to the summary, trimming old lines if needed.
Args:
speaker (str): Name of the agent who spoke.
utterance (str): The new utterance to add.
"""
self.lines.append(
f"{speaker} said: {utterance}. The topic_interest value was {abs(topic_interest)} ({subj}) and the tone value was {abs(tone)} ({mood}).")
if len(self.lines) > self.max_lines:
self.lines = self.lines[-self.max_lines:]
class ConversationalAgent:
"""Agent that holds an internal state and uses Ollama to speak."""
def __init__(
self,
name: str,
mass: float,
topic_interest: float,
tone: float,
summary: ConversationSummary,
) -> None:
"""Initialize the agent.
Args:
name (str): Agent's name.
mass (float): Mass parameter influencing state updates.
topic_interest (float): Initial topic interest value.
tone (float): Initial tone value.
summary (ConversationSummary): Shared conversation summary.
"""
self.name = name
self.mass = mass
self.topic_interest = topic_interest
self.tone = tone
self.topic_velocity = 0.0
self.summary = summary
def compute_gravitational_influence(
self, other: "ConversationalAgent", G: float = 1.0, epsilon: float = 1e-2
) -> float:
"""Compute a signed 'gravitational' force on topic_interest from another agent.
Args:
other (ConversationalAgent): The other agent.
G (float): Gravitational constant scaling factor.
epsilon (float): Small value to prevent division by zero.
Returns:
float: Signed force (positive if other pulls topic interest upward).
"""
dist = abs(self.topic_interest - other.topic_interest) + epsilon
force = G * self.mass * other.mass / (dist * dist)
sign = 1 if other.topic_interest > self.topic_interest else -1
return sign * force
def update_state(
self, agents: List["ConversationalAgent"], dt: float = 0.1, G: float = 1.0
) -> None:
"""Update this agent's topic_interest and tone based on others' influences.
Args:
agents (List[ConversationalAgent]): All agents in the conversation.
dt (float): Time step for integration.
G (float): Gravitational constant for influence computation.
"""
total_force = sum(
self.compute_gravitational_influence(o, G)
for o in agents
if o is not self
)
accel = total_force / self.mass
self.topic_velocity += accel * dt
self.topic_interest += self.topic_velocity * \
dt + random.uniform(-1e-2, 1e-2)
self.tone += self.topic_velocity * dt * \
0.5 + random.uniform(-5e-3, 5e-3)
def generate_prompt(self) -> str:
"""Build the natural-language prompt for the model.
Returns:
str: The prompt including conversation summary and internal state.
"""
self.subj = "science" if self.topic_interest > 0 else "philosophy"
self.mood = "calm, curious and cheerful." if self.tone > 0 else "angry, provocative and polarizing."
return (
"Below is the conversation so far:\n\n"
f"{self.summary.text}\n\n"
f"It is now {self.name}'s turn.\n"
f"Current internal state:\n"
f" • topic_interest: {abs(self.topic_interest):.2f} ({self.subj})\n"
f" • tone: {abs(self.tone):.2f} ({self.mood})\n\n"
f"Please write one short comment to advance the discussion on {self.subj} make sure to consider the evolution and scale of the topic_interest and tone values."
f"Answer with a semantic and syntactic diversity. Do NOT refer to yourself by name and do NOT mention your internal state."
)
def speak(self, model: str) -> str:
"""Query the model, log results, clean the response, and update summary.
Args:
model (str): The Ollama model to use.
Returns:
str: The cleaned utterance produced by the model.
"""
prompt = self.generate_prompt()
raw, resp = query_ollama(model, prompt)
# Logging
log_full(self.name, prompt, raw, resp)
utterance = clean_utterance(resp)
log_chat(self.name, utterance)
self.summary.update(self.name, utterance,
self.topic_interest, self.subj, self.tone, self.mood)
return utterance
def simulate_conversation(
agents: List[ConversationalAgent],
steps: int = 5,
dt: float = 0.1,
G: float = 1.0,
model: str = "qwen3:0.6b",
) -> None:
"""Run a full multi-agent conversation simulation.
Args:
agents (List[ConversationalAgent]): Agents to participate.
steps (int): Number of turns to simulate. (default: 5)
dt (float): Time step for state updates.
G (float): Gravitational constant for interactions. (default: 1.0)
model (str): Ollama model identifier. (default: "qwen3:0.6b")
"""
for turn in range(1, steps + 1):
print(f"\n=== Turn {turn} ===")
for agent in agents:
line = agent.speak(model)
log_csv(agent.name, turn, line, agent.topic_interest, agent.tone)
print(f"{agent.name} says: {line}")
# After each turn, update all agents' states
for agent in agents:
agent.update_state(agents, dt, G)
# ---------------------------------------------------------------------------
# Main Code
def main() -> None:
"""Entry point: ensure model, construct agents, and run the simulation."""
MODEL = "qwen3:0.6b"
ensure_model(MODEL)
summary = ConversationSummary()
names = ["Ava", "Wanda", "Lara"]
agents = [
ConversationalAgent(
name,
mass=random.uniform(0.0022, 317.0),
topic_interest=random.uniform(-1, 1),
tone=random.uniform(-1, 1),
summary=summary,
)
for name in names
]
simulate_conversation(agents, steps=2, model=MODEL)
output_message = "\nData saved to:"
if cfg.full_log:
output_message += f" {cfg.full_log_filepath},"
if cfg.chat_only_log:
output_message += f" {cfg.chat_only_filepath},"
if cfg.export_as_dataset:
output_message += f" {cfg.csv_filepath}."
print(output_message)
if __name__ == "__main__":
main()