-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebate_example.py
More file actions
139 lines (108 loc) · 4.8 KB
/
debate_example.py
File metadata and controls
139 lines (108 loc) · 4.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
"""Multi - agent debate example with LLM - powered intelligence.
This example demonstrates how agents with different perspectives can debate
a topic and reach conclusions through collaboration using real LLM reasoning.
Difficulty: BEGINNER
Prerequisites: Optional - Ollama installed with llama3.2 model for LLM responses
Estimated time: 10 minutes
What you'll learn:
- Configuring LLM providers (Ollama or LiteLLM)
- Creating agents with contrasting perspectives
- Running multi - round debates
- Analyzing debate outcomes and statistics
Expected Output:
- Optimist presents positive perspectives on AI investment
- Pessimist provides critical analysis and risk assessment
- Moderator synthesizes viewpoints and facilitates discussion
- Final summary includes balanced conclusion from all perspectives
- Works with or without LLM (falls back to template responses)
"""
import asyncio
import sys
from pathlib import Path
# Add src to path for development
sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
from agentmind import Agent, AgentMind, AgentRole # noqa: E402
from agentmind.llm import OllamaProvider # noqa: E402
async def debate_example() -> None:
"""Run a debate between agents with different perspectives."""
print("=" * 60)
print("AgentMind - Multi - Agent Debate Example")
print("=" * 60)
# Configure LLM provider
# Option 1: Use Ollama for local inference (requires Ollama running)
# provider = OllamaProvider(model="llama3.2", temperature=0.8)
# Option 2: Use LiteLLM for cloud models (requires API key)
# Uncomment and set your API key:
# os.environ["OPENAI_API_KEY"] = "your - key - here"
# provider = LiteLLMProvider(model="gpt - 3.5 - turbo", temperature=0.8)
# For this demo, we'll try Ollama first, fall back to template mode
try:
provider = OllamaProvider(model="llama3.2", temperature=0.8)
# Test if Ollama is available
if not await provider.check_model_available():
print("[!] Ollama not available, using template - based responses")
provider = None
except Exception as e:
print(f"[!] Could not connect to Ollama: {e}")
print("[!] Using template - based responses")
provider = None
# Create AgentMind instance with LLM provider
mind = AgentMind(llm_provider=provider)
# Create agents with different perspectives
optimist = Agent(name="Optimist", role=AgentRole.CREATIVE.value, llm_provider=provider)
pessimist = Agent(name="Pessimist", role=AgentRole.CRITIC.value, llm_provider=provider)
moderator = Agent(name="Moderator", role=AgentRole.COORDINATOR.value, llm_provider=provider)
# Add agents to the mind
mind.add_agent(optimist)
mind.add_agent(pessimist)
mind.add_agent(moderator)
print(f"\n[+] Created debate team with {len(mind.agents)} agents")
print(f" - {optimist.name} ({optimist.role}) - Will present positive perspectives")
print(f" - {pessimist.name} ({pessimist.role}) - Will present critical analysis")
print(f" - {moderator.name} ({moderator.role}) - Will coordinate the discussion")
if provider:
print(f"\n[*] Using LLM: {provider.model}")
else:
print("\n[*] Using template - based responses (no LLM)")
# Start debate
print("\n" + "=" * 60)
print("Starting Debate")
print("=" * 60)
topic = "Should our company invest heavily in AI technology for the next year?"
print(f"\nTopic: {topic}\n")
result = await mind.start_collaboration(topic, max_rounds=3, use_llm=provider is not None)
# Display results
print("\n" + "=" * 60)
print("Debate Results")
print("=" * 60)
print(f"\nSuccess: {result.success}")
print(f"Total Rounds: {result.total_rounds}")
print(f"Total Messages: {result.total_messages}")
print("\nParticipant Contributions:")
for agent_name, count in result.agent_contributions.items():
print(f" - {agent_name}: {count} messages")
print("\n" + "=" * 60)
print("Debate Summary")
print("=" * 60)
print(f"\n{result.final_output}")
# Get conversation summary
summary = mind.get_conversation_summary()
print("\n" + "=" * 60)
print("Conversation Statistics")
print("=" * 60)
print(f"Total messages exchanged: {summary['total_messages']}")
print(f"Active agents: {summary['active_agents']}/{summary['total_agents']}")
# Show recent messages
if summary["recent_messages"]:
print("\nRecent messages:")
for msg in summary["recent_messages"][-3:]:
preview = msg[:100] + "..." if len(msg) > 100 else msg
print(f" • {preview}")
print("\n" + "=" * 60)
print("Debate Complete!")
print("=" * 60)
# Cleanup
if provider and hasattr(provider, "close"):
await provider.close()
if __name__ == "__main__":
asyncio.run(debate_example())