-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebate.py
More file actions
452 lines (355 loc) Β· 14.8 KB
/
Copy pathdebate.py
File metadata and controls
452 lines (355 loc) Β· 14.8 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
import os
import json
import logging
from datetime import datetime
from typing import Dict, List, Any, Optional
from dataclasses import dataclass, asdict
from langchain_google_genai import ChatGoogleGenerativeAI
from langgraph.graph import StateGraph, END
from typing_extensions import TypedDict
# Configure logging to file (no console output)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler('debate_log.txt')
]
)
logger = logging.getLogger(__name__)
@dataclass
class Argument:
"""Represents a single argument in the debate"""
agent: str
round_num: int
content: str
timestamp: str
class DebateState(TypedDict):
"""State structure for the debate workflow"""
topic: str
current_round: int
current_agent: str
arguments: List[Dict[str, Any]]
memory_summary: str
debate_complete: bool
winner: Optional[str]
judgment_reason: Optional[str]
full_summary: Optional[str]
class DebateSystem:
def __init__(self, api_key: str):
"""Initialize the debate system with Gemini API"""
self.llm = ChatGoogleGenerativeAI(
model="gemini-2.0-flash",
google_api_key=api_key,
temperature=0.7
)
self.graph = None
self.build_graph()
def user_input_node(self, state: DebateState) -> DebateState:
"""Node to handle user input for debate topic"""
logger.info("=== USER INPUT NODE ===")
if not state.get("topic"):
topic = input("Enter topic for debate: ").strip()
state["topic"] = topic
state["current_round"] = 1
state["current_agent"] = "Scientist"
state["arguments"] = []
state["memory_summary"] = f"Debate Topic: {topic}"
state["debate_complete"] = False
print(f"\n{'='*60}")
print(f"π DEBATE: Scientist vs Philosopher")
print(f"π Topic: {topic}")
print(f"{'='*60}\n")
return state
def scientist_agent_node(self, state: DebateState) -> DebateState:
"""Scientist agent node - evidence-based arguments"""
logger.info(f"=== SCIENTIST AGENT NODE - Round {state['current_round']} ===")
# Prepare context
previous_args = "\n".join([
f"[Round {arg['round_num']}] {arg['agent']}: {arg['content']}"
for arg in state["arguments"]
])
prompt = f"""You are a Scientist participating in a structured debate.
Topic: {state['topic']}
Current Memory Summary: {state['memory_summary']}
Previous Arguments:
{previous_args}
This is Round {state['current_round']} of 8. You are making your {(state['current_round'] + 1) // 2} argument.
As a Scientist, base your argument on:
- Empirical evidence and data
- Scientific methodology
- Risk assessment and safety protocols
- Peer-reviewed research
- Quantifiable impacts
Make a compelling, evidence-based argument (2-3 sentences). Be persuasive but factual."""
try:
response = self.llm.invoke(prompt)
argument_content = response.content.strip()
# Create argument record
argument = Argument(
agent="Scientist",
round_num=state["current_round"],
content=argument_content,
timestamp=datetime.now().isoformat()
)
# Update state
state["arguments"].append(asdict(argument))
# log and Display argument
logger.info(f"Scientist argument: {argument_content}")
print(f"π¬[Round {state['current_round']}] Scientist:")
print(f" {argument_content}\n")
# Update for next turn
state["current_round"] += 1
state["current_agent"] = "Philosopher"
except Exception as e:
logger.error(f"Error in scientist agent: {e}")
# print(f"Error in scientist agent: {e}")
raise
return state
def philosopher_agent_node(self, state: DebateState) -> DebateState:
"""Philosopher agent node - conceptual and ethical arguments"""
logger.info(f"=== PHILOSOPHER AGENT NODE - Round {state['current_round']} ===")
# Prepare context
previous_args = "\n".join([
f"[Round {arg['round_num']}] {arg['agent']}: {arg['content']}"
for arg in state["arguments"]
])
prompt = f"""You are a Philosopher participating in a structured debate.
Topic: {state['topic']}
Current Memory Summary: {state['memory_summary']}
Previous Arguments:
{previous_args}
This is Round {state['current_round']} of 8. You are making your {state['current_round'] // 2} argument.
As a Philosopher, base your argument on:
- Ethical considerations and moral frameworks
- Historical precedents and lessons
- Conceptual analysis and definitions
- Social and cultural implications
- Individual rights and freedoms
- Long-term societal impact
Make a compelling, philosophically grounded argument (2-3 sentences). Be persuasive and thoughtful."""
try:
response = self.llm.invoke(prompt)
argument_content = response.content.strip()
# Create argument record
argument = Argument(
agent="Philosopher",
round_num=state["current_round"],
content=argument_content,
timestamp=datetime.now().isoformat()
)
# Update state
state["arguments"].append(asdict(argument))
# Log and display
logger.info(f"Philosopher argument: {argument_content}")
print(f"π€ [Round {state['current_round']}] Philosopher:")
print(f" {argument_content}\n")
# Update for next turn
if state["current_round"] < 8:
state["current_round"] += 1
state["current_agent"] = "Scientist"
else:
state["debate_complete"] = True
state["current_agent"] = "Judge"
except Exception as e:
logger.error(f"Error in philosopher agent: {e}")
# print(f"Error in philosopher agent: {e}")
raise
return state
def memory_node(self, state: DebateState) -> DebateState:
"""Node to update and maintain debate memory"""
logger.info("=== MEMORY NODE ===")
if not state["arguments"]:
return state
# Create structured summary of recent arguments
recent_args = state["arguments"][-2:] if len(state["arguments"]) >= 2 else state["arguments"]
summary_prompt = f"""Update the debate memory summary with the latest arguments.
Current Topic: {state['topic']}
Previous Summary: {state['memory_summary']}
Recent Arguments:
{chr(10).join([f"[Round {arg['round_num']}] {arg['agent']}: {arg['content']}" for arg in recent_args])}
Provide an updated summary that captures:
1. The main debate topic
2. Key points from both sides
3. Current trajectory of the debate
4. Notable patterns or themes
Keep it concise (3-4 sentences)."""
try:
response = self.llm.invoke(summary_prompt)
updated_summary = response.content.strip()
state["memory_summary"] = updated_summary
logger.info(f"Memory updated: {updated_summary}")
except Exception as e:
logger.error(f"Error updating memory: {e}")
return state
def judge_node(self, state: DebateState) -> DebateState:
"""Judge node to evaluate the debate and declare winner"""
logger.info("=== JUDGE NODE ===")
# Prepare full debate transcript
full_transcript = "\n".join([
f"[Round {arg['round_num']}] {arg['agent']}: {arg['content']}"
for arg in state["arguments"]
])
judgment_prompt = f"""You are an impartial judge evaluating a structured debate.
Topic: {state['topic']}
Full Debate Transcript:
{full_transcript}
Memory Summary: {state['memory_summary']}
As the judge, evaluate the debate based on:
1. Logical coherence and consistency
2. Quality and relevance of evidence
3. Persuasiveness of arguments
4. Addressing counterpoints
5. Overall strength of position
Provide:
1. A comprehensive summary of the debate (3-4 sentences)
2. The winner (either "Scientist" or "Philosopher")
3. Detailed reasoning for your decision (2-3 sentences)
Format your response as:
SUMMARY: [your summary]
WINNER: [Scientist or Philosopher]
REASON: [your reasoning]"""
try:
response = self.llm.invoke(judgment_prompt)
judgment = response.content.strip()
# Parse judgment
lines = judgment.split('\n')
summary_line = next((line for line in lines if line.startswith('SUMMARY:')), '')
winner_line = next((line for line in lines if line.startswith('WINNER:')), '')
reason_line = next((line for line in lines if line.startswith('REASON:')), '')
state["full_summary"] = summary_line.replace('SUMMARY:', '').strip()
state["winner"] = winner_line.replace('WINNER:', '').strip()
state["judgment_reason"] = reason_line.replace('REASON:', '').strip()
# Log judgment
logger.info(f"Judgment complete - Winner: {state['winner']}")
logger.info(f"Summary: {state['full_summary']}")
logger.info(f"Reason: {state['judgment_reason']}")
# Display results
print(f"{'='*60}")
print(f"\n[Judge] Summary of debate:")
print(f"{'='*60}")
print(f"{state['full_summary']}")
print(f"\n[Judge] Winner: {state['winner']}")
print(f"Reason: {state['judgment_reason']}")
print(f"{'='*60}")
except Exception as e:
logger.error(f"Error in judge node: {e}")
# print(f"Error in judge node: {e}")
state["winner"] = "Error in judgment"
state["judgment_reason"] = "Could not complete evaluation"
return state
def route_after_input(self, state: DebateState) -> str:
"""Route after user input - always go to scientist first"""
return "scientist"
def route_after_scientist(self, state: DebateState) -> str:
"""Route after scientist argument"""
return "memory"
def route_after_philosopher(self, state: DebateState) -> str:
"""Route after philosopher argument"""
if state["debate_complete"]:
return "judge"
else:
return "memory"
def route_after_memory(self, state: DebateState) -> str:
"""Route after memory update"""
if state["debate_complete"]:
return "judge"
elif state["current_agent"] == "Scientist":
return "scientist"
elif state["current_agent"] == "Philosopher":
return "philosopher"
else:
return "judge"
def build_graph(self):
"""Build the LangGraph workflow"""
workflow = StateGraph(DebateState)
# Add nodes
workflow.add_node("user_input", self.user_input_node)
workflow.add_node("scientist", self.scientist_agent_node)
workflow.add_node("philosopher", self.philosopher_agent_node)
workflow.add_node("memory", self.memory_node)
workflow.add_node("judge", self.judge_node)
# Add edges
workflow.set_entry_point("user_input")
# add conditional edges based on routing logic
workflow.add_conditional_edges(
"user_input",
self.route_after_input,
{"scientist": "scientist"}
)
workflow.add_conditional_edges(
"scientist",
self.route_after_scientist,
{"memory": "memory"}
)
workflow.add_conditional_edges(
"philosopher",
self.route_after_philosopher,
{
"memory": "memory",
"judge": "judge"
}
)
workflow.add_conditional_edges(
"memory",
self.route_after_memory,
{
"scientist": "scientist",
"philosopher": "philosopher",
"judge": "judge"
}
)
workflow.add_edge("judge", END)
self.graph = workflow.compile()
def show_workflow_diagram(self):
"""Display the workflow DAG diagram and save as image"""
try:
# Save the diagram as PNG file
diagram_data = self.graph.get_graph().draw_mermaid_png()
# Save to file
with open('debate_workflow.png', 'wb') as f:
f.write(diagram_data)
print("π Workflow DAG Diagram saved as 'debate_workflow.png'")
print("=" * 50)
except Exception as e:
print(f"Could not generate diagram: {e}")
print("π Workflow: user_input β scientist β memory β philosopher β memory β ... β judge β END")
def run_debate(self) -> DebateState:
"""Execute the debate workflow"""
logger.info("Starting debate system...")
# Show workflow diagram
self.show_workflow_diagram()
print("\n")
initial_state = DebateState(
topic="",
current_round=1,
current_agent="Scientist",
arguments=[],
memory_summary="",
debate_complete=False,
winner=None,
judgment_reason=None,
full_summary=None
)
try:
final_state = self.graph.invoke(initial_state)
# Save final log
self.save_debate_log(final_state)
return final_state
except Exception as e:
logger.error(f"Error running debate: {e}")
print(f"Error running debate: {e}")
raise
def save_debate_log(self, state: DebateState):
"""Save complete debate log to file"""
log_data = {
"timestamp": datetime.now().isoformat(),
"topic": state["topic"],
"arguments": state["arguments"],
"memory_summary": state["memory_summary"],
"full_summary": state["full_summary"],
"winner": state["winner"],
"judgment_reason": state["judgment_reason"]
}
with open('debate_log.json', 'w') as f:
json.dump(log_data, f, indent=2)
logger.info("Complete debate log saved to debate_log.json")