"""
Pattern 012: Debate/Discussion (Multi-Agent)
Description:
Multiple agents engage in debate or discussion to reach better conclusions.
Each agent represents different perspectives, argues for their position,
and the system synthesizes the best answer from the debate. This reduces
bias and improves reasoning quality through adversarial collaboration.
Key Concepts:
- Multiple Perspectives: Each agent takes different viewpoint
- Argument Generation: Agents present reasoned arguments
- Counter-Arguments: Agents respond to each other's points
- Synthesis: Final answer combines best insights from debate
- Moderation: Optional moderator guides discussion
Benefits:
- Reduced individual bias
- More thorough exploration of problem space
- Error correction through peer review
- Higher quality conclusions
Use Cases:
- Complex decision-making
- Ethical dilemmas
- Strategy formulation
- Content review and validation
LangChain Implementation:
Multiple LLM instances with different personas engaging in structured debate.
"""
import os
from typing import List, Dict, Any, Optional
from dataclasses import dataclass, field
from enum import Enum
from dotenv import load_dotenv
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_openai import ChatOpenAI
# Load environment variables
load_dotenv()
class AgentRole(Enum):
"""Roles agents can take in debate."""
PROPONENT = "proponent"
OPPONENT = "opponent"
SKEPTIC = "skeptic"
PRAGMATIST = "pragmatist"
MODERATOR = "moderator"
@dataclass
class DebateMessage:
"""Represents a message in the debate."""
round: int
agent_id: str
role: AgentRole
content: str
responding_to: Optional[str] = None
@dataclass
class DebateTranscript:
"""Complete transcript of a debate."""
question: str
messages: List[DebateMessage] = field(default_factory=list)
final_synthesis: str = ""
def add_message(self, message: DebateMessage):
"""Add a message to the transcript."""
self.messages.append(message)
def get_round_messages(self, round_num: int) -> List[DebateMessage]:
"""Get all messages from a specific round."""
return [msg for msg in self.messages if msg.round == round_num]
class DebateAgent:
"""Individual agent participating in debate."""
def __init__(self, agent_id: str, role: AgentRole,
model_name: str = "gpt-3.5-turbo"):
"""
Initialize a debate agent.
Args:
agent_id: Unique identifier for the agent
role: Role the agent plays in debate
model_name: Name of the OpenAI model
"""
self.agent_id = agent_id
self.role = role
self.llm = ChatOpenAI(
model=model_name,
temperature=0.7, # Some creativity for diverse perspectives
api_key=os.getenv("OPENAI_API_KEY")
)
# Role-specific system prompts
self.system_prompts = {
AgentRole.PROPONENT: "You are advocating FOR the position. Present strong arguments supporting it.",
AgentRole.OPPONENT: "You are arguing AGAINST the position. Present strong counterarguments.",
AgentRole.SKEPTIC: "You are a critical skeptic. Question assumptions and identify weaknesses.",
AgentRole.PRAGMATIST: "You focus on practical implications and real-world feasibility."
}
def generate_opening_statement(self, question: str) -> str:
"""Generate opening statement for the debate."""
prompt = ChatPromptTemplate.from_messages([
("system", f"""{self.system_prompts.get(self.role, 'You are a thoughtful debater.')}
This is your opening statement. Present your main position clearly and concisely."""),
("human", "Question: {question}\n\nYour opening statement:")
])
chain = prompt | self.llm | StrOutputParser()
return chain.invoke({"question": question})
def respond_to_arguments(self, question: str, previous_messages: List[DebateMessage]) -> str:
"""Generate response to previous arguments."""
# Build context from previous messages
context_parts = []
for msg in previous_messages:
context_parts.append(f"{msg.agent_id} ({msg.role.value}): {msg.content}")
context = "\n\n".join(context_parts)
prompt = ChatPromptTemplate.from_messages([
("system", f"""{self.system_prompts.get(self.role, 'You are a thoughtful debater.')}
Respond to the previous arguments. Address key points, present counter-arguments where appropriate,
and strengthen your position."""),
("human", """Question: {question}
Previous Arguments:
{context}
Your response:""")
])
chain = prompt | self.llm | StrOutputParser()
return chain.invoke({
"question": question,
"context": context
})
class DebateSystem:
"""System for managing multi-agent debate."""
def __init__(self, model_name: str = "gpt-3.5-turbo", num_rounds: int = 2):
"""
Initialize the debate system.
Args:
model_name: Name of the OpenAI model
num_rounds: Number of debate rounds
"""
self.model_name = model_name
self.num_rounds = num_rounds
self.moderator_llm = ChatOpenAI(
model=model_name,
temperature=0.3,
api_key=os.getenv("OPENAI_API_KEY")
)
def conduct_debate(self, question: str,
agent_roles: Optional[List[AgentRole]] = None,
verbose: bool = True) -> DebateTranscript:
"""
Conduct a structured debate on the question.
Args:
question: Question to debate
agent_roles: Roles for agents (default: proponent and opponent)
verbose: Whether to print debate progress
Returns:
Complete debate transcript with synthesis
"""
if agent_roles is None:
agent_roles = [AgentRole.PROPONENT, AgentRole.OPPONENT]
transcript = DebateTranscript(question=question)
if verbose:
print(f"\nQuestion: {question}\n")
print(f"{'='*60}")
print(f"MULTI-AGENT DEBATE")
print(f"{'='*60}")
print(f"Participants: {len(agent_roles)}")
print(f"Rounds: {self.num_rounds}\n")
# Create agents
agents = []
for i, role in enumerate(agent_roles):
agent = DebateAgent(
agent_id=f"Agent_{i+1}",
role=role,
model_name=self.model_name
)
agents.append(agent)
if verbose:
print(f" {agent.agent_id}: {role.value}")
print()
# Round 1: Opening statements
if verbose:
print(f"\n{'='*60}")
print(f"ROUND 1: Opening Statements")
print('='*60)
for agent in agents:
statement = agent.generate_opening_statement(question)
message = DebateMessage(
round=1,
agent_id=agent.agent_id,
role=agent.role,
content=statement
)
transcript.add_message(message)
if verbose:
print(f"\n{agent.agent_id} ({agent.role.value}):")
print(f"{statement}")
# Subsequent rounds: Responses and rebuttals
for round_num in range(2, self.num_rounds + 1):
if verbose:
print(f"\n{'='*60}")
print(f"ROUND {round_num}: Discussion")
print('='*60)
# Get previous round messages for context
prev_messages = transcript.get_round_messages(round_num - 1)
for agent in agents:
# Agent responds to previous arguments
response = agent.respond_to_arguments(question, prev_messages)
message = DebateMessage(
round=round_num,
agent_id=agent.agent_id,
role=agent.role,
content=response
)
transcript.add_message(message)
if verbose:
print(f"\n{agent.agent_id} ({agent.role.value}):")
print(f"{response}")
# Synthesis: Combine insights from debate
if verbose:
print(f"\n{'='*60}")
print("SYNTHESIS")
print('='*60)
synthesis = self._synthesize_debate(question, transcript)
transcript.final_synthesis = synthesis
if verbose:
print(f"\n{synthesis}")
return transcript
def _synthesize_debate(self, question: str, transcript: DebateTranscript) -> str:
"""
Synthesize the debate into a final answer.
Args:
question: Original question
transcript: Complete debate transcript
Returns:
Synthesized answer
"""
# Build debate summary
debate_text = []
for msg in transcript.messages:
debate_text.append(f"[Round {msg.round}] {msg.agent_id} ({msg.role.value}):\n{msg.content}")
debate_summary = "\n\n".join(debate_text)
prompt = ChatPromptTemplate.from_messages([
("system", """You are a moderator synthesizing a debate.
Review all arguments presented and create a balanced, thoughtful conclusion that:
1. Acknowledges strong points from all sides
2. Identifies areas of consensus
3. Notes remaining disagreements
4. Provides a nuanced final answer
Be objective and fair to all perspectives."""),
("human", """Question: {question}
Debate Transcript:
{debate}
Synthesis:""")
])
chain = prompt | self.moderator_llm | StrOutputParser()
synthesis = chain.invoke({
"question": question,
"debate": debate_summary
})
return synthesis
def demonstrate_debate_pattern():
"""Demonstrates the Debate/Discussion pattern."""
print("=" * 80)
print("PATTERN 012: Debate/Discussion (Multi-Agent)")
print("=" * 80)
print()
print("Multi-Agent Debate improves reasoning through:")
print("1. Multiple Perspectives: Different viewpoints represented")
print("2. Adversarial Collaboration: Agents challenge each other")
print("3. Iterative Refinement: Arguments evolve through rounds")
print("4. Synthesis: Best insights combined into final answer")
print()
# Create debate system
system = DebateSystem(num_rounds=2)
# Test questions
questions = [
"Should artificial intelligence development be regulated by governments?",
"Is remote work more productive than office work for software development?"
]
for idx, question in enumerate(questions, 1):
print(f"\n{'='*80}")
print(f"Debate {idx}")
print('='*80)
try:
# Conduct debate with different roles
roles = [AgentRole.PROPONENT, AgentRole.OPPONENT, AgentRole.SKEPTIC]
transcript = system.conduct_debate(
question=question,
agent_roles=roles,
verbose=True
)
print(f"\n\n{'='*80}")
print("DEBATE SUMMARY")
print('='*80)
print(f"\nQuestion: {transcript.question}")
print(f"Total Messages: {len(transcript.messages)}")
print(f"Rounds: {system.num_rounds}")
print(f"\nFinal Synthesis:\n{transcript.final_synthesis}")
except Exception as e:
print(f"\n✗ Error: {str(e)}")
import traceback
traceback.print_exc()
# Summary
print("\n\n" + "=" * 80)
print("DEBATE/DISCUSSION PATTERN DEMONSTRATION COMPLETE")
print("=" * 80)
print()
print("Key Features Demonstrated:")
print("1. Multiple Agent Roles: Proponent, Opponent, Skeptic")
print("2. Structured Rounds: Opening statements followed by discussion")
print("3. Contextual Responses: Agents respond to previous arguments")
print("4. Moderator Synthesis: Balanced conclusion from all perspectives")
print("5. Transcript Management: Complete debate history maintained")
print()
print("Advantages:")
print("- Reduces individual bias")
print("- More thorough problem exploration")
print("- Error correction through peer review")
print("- Higher quality conclusions")
print()
print("When to use Debate Pattern:")
print("- Complex decision-making")
print("- Ethical or controversial questions")
print("- Strategy formulation")
print("- Content validation and review")
print()
print("LangChain Components Used:")
print("- Multiple ChatOpenAI instances: Different agents")
print("- Role-based system prompts: Agent personas")
print("- Structured debate management: Rounds and synthesis")
print("- Context building: Previous arguments inform responses")
print()
if __name__ == "__main__":
# Check for API key
if not os.getenv("OPENAI_API_KEY"):
print("Error: OPENAI_API_KEY not found in environment variables.")
print("Please set it in your .env file or environment.")
exit(1)
demonstrate_debate_pattern()
Agentic Design Pattern: Debate Discussion
Pattern 012: Debate Discussion – Line-by-Line Explanation
Overview
This document provides a detailed line-by-line explanation of the Debate Discussion pattern implementation.
Pattern Description
Pattern 012: Debate/Discussion (Multi-Agent)
Description: Multiple agents engage in debate or discussion to reach better conclusions. Each agent represents different perspectives, argues for their position, and the system synthesizes the best answer from the debate. This reduces bias and improves reasoning quality through adversarial collaboration.
Key Concepts: – Multiple Perspectives: Each agent takes different viewpoint – Argument Generation: Agents present reasoned arguments – Counter-Arguments: Agents respond to each other’s points – Synthesis: Final answer combines best insights from debate – Moderation: Optional moderator guides discussion
Benefits: – Reduced individual bias – More thorough exploration of problem space – Error correction through peer review – Higher quality conclusions
Use Cases: – Complex decision-making – Ethical dilemmas – Strategy formulation – Content review and validation
LangChain Implementation: Multiple LLM instances with different personas engaging in structured debate.
File Statistics
- Total Lines: 409
- Classes: 5
- Functions: 1
- Constants: 0
Module Documentation
"""
Pattern 012: Debate/Discussion (Multi-Agent)
Description:
Multiple agents engage in debate or discussion to reach better conclusions.
Each agent represents different perspectives, argues for their position,
and the system synthesizes the best answer from the debate. This reduces
bias and improves reasoning quality through adversarial collaboration.
Key Concepts:
- Multiple Perspectives: Each agent takes different viewpoint
- Argument Generation: Agents present reasoned arguments
- Counter-Arguments: Agents respond to each other's points
- Synthesis: Final answer combines best insights from debate
- Moderation: Optional moderator guides discussion
Benefits:
- Reduced individual bias
- More thorough exploration of problem space
- Error correction through peer review
- Higher quality conclusions
Use Cases:
- Complex decision-making
- Ethical dilemmas
- Strategy formulation
- Content review and validation
LangChain Implementation:
Multiple LLM instances with different personas engaging in structured debate.
"""
Purpose: This module implements the Debate Discussion pattern, providing a structured approach to agent design and implementation.
Import Statements
Standard Library Imports
import dataclasses.dataclass
import dataclasses.field
import enum.Enum
import langchain_core.output_parsers.StrOutputParser
import langchain_core.prompts.ChatPromptTemplate
import os
import typing.Any
import typing.Dict
import typing.List
import typing.Optional
Purpose: Core Python functionality:
– dataclasses.dataclass: Simplified class definitions with @dataclass
– dataclasses.field: Simplified class definitions with @dataclass
– enum.Enum: Enumeration support
– langchain_core.output_parsers.StrOutputParser: Regular expression operations
– langchain_core.prompts.ChatPromptTemplate: Regular expression operations
– os: Environment variable access and system operations
– typing.Any: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Dict: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.List: Type hints for better code documentation (List, Dict, Any, Optional)
– typing.Optional: Type hints for better code documentation (List, Dict, Any, Optional)
LangChain Imports
from langchain_openai.ChatOpenAI
Purpose: LangChain framework components: – LLM Interface: Wrapper for OpenAI models – Chains: Composable building blocks for complex workflows
Other Imports
from dotenv.load_dotenv
from traceback
Purpose: Additional dependencies for specialized functionality.
Classes
Class: AgentRole
Lines 47-53
Inherits from: Enum
Purpose:
Roles agents can take in debate.
Class: DebateMessage
Lines 57-63
Purpose:
Represents a message in the debate.
Class: DebateTranscript
Lines 67-79
Purpose:
Complete transcript of a debate.
Methods:
add_message(self, message)
Lines 73-75
Purpose:
Add a message to the transcript.
Parameters:
– message: Message
get_round_messages(self, round_num)
Lines 77-79
Purpose:
Get all messages from a specific round.
Parameters:
– round_num: Round num
Returns: <ast.Subscript object at 0x000001A06D60F4D0>
Class: DebateAgent
Lines 82-149
Purpose:
Individual agent participating in debate.
Methods:
__init__(self, agent_id, role, model_name)
Lines 85-109
Purpose:
Initialize a debate agent.
Args:
agent_id: Unique identifier for the agent
role: Role the agent plays in debate
model_name: Name of the OpenAI model
Parameters:
– agent_id: Agent id
– role: Role
– model_name: Language model instance
generate_opening_statement(self, question)
Lines 111-121
Purpose:
Generate opening statement for the debate.
Parameters:
– question: Question to answer
Returns: str
respond_to_arguments(self, question, previous_messages)
Lines 123-149
Purpose:
Generate response to previous arguments.
Parameters:
– question: Question to answer
– previous_messages: Previous messages
Returns: str
Class: DebateSystem
Lines 152-315
Purpose:
System for managing multi-agent debate.
Methods:
__init__(self, model_name, num_rounds)
Lines 155-169
Purpose:
Initialize the debate system.
Args:
model_name: Name of the OpenAI model
num_rounds: Number of debate rounds
Parameters:
– model_name: Language model instance
– num_rounds: Num rounds
conduct_debate(self, question, agent_roles, verbose)
Lines 171-271
Purpose:
Conduct a structured debate on the question.
Args:
question: Question to debate
agent_roles: Roles for agents (default: proponent and opponent)
verbose: Whether to print debate progress
Returns:
Complete debate transcript with synthesis
Parameters:
– question: Question to answer
– agent_roles: Agent roles
– verbose: Enable detailed logging
Returns: DebateTranscript
_synthesize_debate(self, question, transcript)
Lines 273-315
Purpose:
Synthesize the debate into a final answer.
Args:
question: Original question
transcript: Complete debate transcript
Returns:
Synthesized answer
Parameters:
– question: Question to answer
– transcript: Transcript
Returns: str
Functions
demonstrate_debate_pattern()
Lines 318-398
Purpose:
Demonstrates the Debate/Discussion pattern.
Key Concepts Summary
Pattern Overview
The Debate Discussion pattern provides a structured approach to building AI agents with specific capabilities and behaviors. This implementation leverages LangChain’s composable components to create a robust and maintainable solution.
Core Components
- Agent Class: Main implementation of the pattern logic
- Prompt Engineering: Carefully crafted prompts to guide LLM behavior
- Tool Integration: Optional tools for extended functionality
- Memory Management: State handling across interactions
- Output Parsing: Structured response processing
When to Use
✅ Good for: – Scenarios requiring this specific pattern’s strengths – Applications needing structured agent behavior – Systems where explainability is important – Production environments requiring reliability
❌ Not ideal for: – Simple single-prompt tasks – Scenarios requiring different pattern characteristics – Resource-constrained environments (if computationally intensive)
Best Practices Demonstrated
1. Environment Configuration
- ✅ Use
.envfiles for API keys - ✅ Never hardcode credentials
- ✅ Validate environment variables on startup
2. Error Handling
- ✅ Graceful degradation on failures
- ✅ Informative error messages
- ✅ Logging for debugging
3. Prompt Engineering
- ✅ Clear, specific instructions
- ✅ Structured output formats
- ✅ Few-shot examples when beneficial
4. Code Organization
- ✅ Separation of concerns (class-based design)
- ✅ Type hints for clarity
- ✅ Docstrings for documentation
5. Performance Optimization
- ✅ Appropriate temperature settings
- ✅ Token budget management
- ✅ Caching where applicable
LangChain Components Used
- ChatOpenAI: OpenAI model interface
- Chains: Sequential component composition
Why LangChain? – Composable abstractions for LLM applications – Rich ecosystem of integrations – Production-ready patterns – Active community and development
Execution Flow
1. Initialize agent with configuration
↓
2. Load environment and dependencies
↓
3. Prepare prompt with input data
↓
4. Invoke LLM through LangChain
↓
5. Parse and validate response
↓
6. Return formatted result
Key Decision Points: – Model selection (temperature, model type) – Prompt structure (few-shot vs zero-shot) – Error handling strategy – Output format validation
Performance Considerations
Computational Cost
- LLM Calls: Primary cost factor (based on tokens)
- Latency: Depends on model and input size
- Memory: Minimal for most patterns
Optimization Strategies
- Prompt Optimization: Minimize tokens while maintaining clarity
- Caching: Cache repeated queries/responses
- Model Selection: Use smaller models when appropriate
- Batch Processing: Group similar requests
- Streaming: For long responses, stream tokens
Cost Estimates (Approximate)
- GPT-3.5-turbo: ~$0.002 per request
- GPT-4: ~$0.03 per request
- GPT-4-turbo: ~$0.01 per request
Actual costs vary based on input/output token counts
Common Pitfalls to Avoid
1. Prompt Design Issues
❌ Vague or ambiguous instructions
✅ Clear, specific, testable prompts
2. Error Handling
❌ Ignoring API errors or rate limits
✅ Implement retry logic and graceful degradation
3. Token Management
❌ Exceeding model token limits
✅ Truncate inputs, chunk processing
4. Security
❌ Exposing API keys in code
✅ Use environment variables
5. Testing
❌ Only testing happy paths
✅ Test edge cases, failures, malformed inputs
6. Cost Management
❌ Unmonitored API usage
✅ Implement budgets, alerts, and monitoring
Further Improvements
Production Enhancements
- Monitoring: Add logging, metrics, tracing
- Validation: Input/output schema validation
- Rate Limiting: Implement request throttling
- Caching: Redis or in-memory cache for responses
- Async Support: Non-blocking operations for scalability
Feature Extensions
- Multi-Model Support: Switch between providers
- Fallback Models: Graceful degradation strategy
- Custom Tools: Domain-specific function calling
- Advanced Memory: Vector stores for long-term memory
- Evaluation: Automated testing and benchmarking
Code Quality
- Testing: Unit tests, integration tests
- Documentation: API docs, usage examples
- Type Safety: Stricter type hints
- Configuration: Externalized config files
Related Patterns
- Pattern 011
- Pattern 013
- Other Multi-Agent Patterns (012-019)
Pattern Families: – Reasoning Patterns (001-011) – Multi-Agent Patterns (012-019) – Tool Use Patterns (020-025) – Memory Patterns (026-031) – And many more…
Summary
The Debate Discussion pattern provides a powerful approach to building AI agents with specific capabilities. This implementation demonstrates:
✅ Key Strengths: – Clean, maintainable code structure – LangChain integration for composability – Type hints and documentation – Production-ready patterns
🎯 Best Use Cases: – Applications requiring this pattern’s characteristics – Systems needing structured agent behavior – Production environments requiring reliability
📚 Learning Resources: – LangChain Documentation: https://python.langchain.com/ – Pattern Documentation: See related patterns – OpenAI API Reference: https://platform.openai.com/docs/
File: 012_debate_discussion.py
Total Lines: 409
Pattern Category: AI Agent Design Pattern #012
Generated from actual implementation code
For educational and reference purposes
Leave a Reply