"""
Pattern 5.1: Debate Pattern (CrewAI Implementation)
Multiple agents argue different perspectives and synthesize views.
"""
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
import os
def create_debate_crew(topic: str):
"""Create a debate crew with multiple perspectives."""
# Initialize LLM
llm = ChatOpenAI(model="gpt-4")
# Create agents with different perspectives
optimist = Agent(
role="Optimistic Analyst",
goal=f"Argue the positive aspects and opportunities of {topic}",
backstory="You are an optimistic analyst who always sees the bright side and potential benefits.",
verbose=True,
llm=llm
)
pessimist = Agent(
role="Critical Analyst",
goal=f"Identify risks, challenges, and potential problems with {topic}",
backstory="You are a critical thinker who identifies risks and challenges to ensure thorough analysis.",
verbose=True,
llm=llm
)
realist = Agent(
role="Pragmatic Analyst",
goal=f"Provide a balanced, practical perspective on {topic}",
backstory="You are a pragmatic analyst who balances optimism with realism and provides actionable insights.",
verbose=True,
llm=llm
)
synthesizer = Agent(
role="Synthesis Expert",
goal="Synthesize different perspectives into a comprehensive analysis",
backstory="You are an expert at combining different viewpoints into coherent, balanced conclusions.",
verbose=True,
llm=llm
)
# Create tasks
optimistic_task = Task(
description=f"Present the optimistic case for {topic}. Focus on benefits, opportunities, and positive outcomes.",
agent=optimist,
expected_output="Detailed optimistic analysis with specific benefits and opportunities"
)
critical_task = Task(
description=f"Present the critical analysis of {topic}. Focus on risks, challenges, and potential problems.",
agent=pessimist,
expected_output="Detailed critical analysis with specific risks and challenges"
)
realistic_task = Task(
description=f"Present the pragmatic view of {topic}. Balance pros and cons with practical recommendations.",
agent=realist,
expected_output="Balanced pragmatic analysis with actionable recommendations"
)
synthesis_task = Task(
description="""Synthesize the optimistic, critical, and pragmatic perspectives into a comprehensive final analysis.
Include: 1) Summary of each perspective, 2) Key insights, 3) Balanced recommendations""",
agent=synthesizer,
expected_output="Comprehensive synthesis with balanced recommendations",
context=[optimistic_task, critical_task, realistic_task]
)
# Create crew
crew = Crew(
agents=[optimist, pessimist, realist, synthesizer],
tasks=[optimistic_task, critical_task, realistic_task, synthesis_task],
process=Process.sequential,
verbose=True
)
return crew
def run_debate(topic: str):
"""Run the debate pattern."""
print(f"\n{'='*60}")
print(f"Debate Pattern: {topic}")
print(f"{'='*60}\n")
crew = create_debate_crew(topic)
result = crew.kickoff()
return result
if __name__ == "__main__":
# Example usage
topic = "Implementing AI in healthcare diagnostics"
result = run_debate(topic)
print(f"\n\n{'='*60}")
print("FINAL SYNTHESIS")
print(f"{'='*60}")
print(result)
Agentic Orchestration Pattern: Debate Pattern (CrewAI)
01 Debate Crewai Pattern – Detailed Documentation
Overview
File: 05_collaborative/01_debate_crewai.py
Pattern Category: 05 Collaborative
Implementation: LangGraph/CrewAI
Pattern Description
Pattern 5.1: Debate Pattern (CrewAI Implementation) Multiple agents argue different perspectives and synthesize views.
Code Structure
Imports
from crewai import Agent, Task, Crew, Process
from langchain_openai import ChatOpenAI
import os
Purpose of imports:
– ChatOpenAI: OpenAI LLM integration
– crewai: Multi-agent collaboration framework
State Definition
Functions
create_debate_crew()
Line: 10
Arguments: topic
Create a debate crew with multiple perspectives.
def create_debate_crew(topic: str):
"""Create a debate crew with multiple perspectives."""
# Initialize LLM
llm = ChatOpenAI(model="gpt-4")
# Create agents with different perspectives
optimist = Agent(
role="Optimistic Analyst",
goal=f"Argue the positive aspects and opportunities of {topic}",
backstory="You are an optimistic analyst who always sees the bright side and potential benefits.",
verbose=True,
llm=llm
)
pessimist = Agent(
role="Critical Analyst",
goal=f"Identify risks, challenges, and potential problems with {topic}",
backstory="You are a critical thinker who identifies risks and challenges to ensure thorough analysis.",
verbose=True,
llm=llm
)
realist = Agent(
role="Pragmatic Analyst",
goal=f"Provide a balanced, practical perspective on {topic}",
backstory="You are a pragmatic analyst who balances optimism with realism and provides actionable insights.",
verbose=True,
llm=llm
)
run_debate()
Line: 87
Arguments: topic
Run the debate pattern.
def run_debate(topic: str):
"""Run the debate pattern."""
print(f"\n{'='*60}")
print(f"Debate Pattern: {topic}")
print(f"{'='*60}\n")
crew = create_debate_crew(topic)
result = crew.kickoff()
return result
if __name__ == "__main__":
Line-by-Line Explanation
Line 5: Import statement
from crewai import Agent, Task, Crew, Process
Required import for the pattern implementation.
Line 6: Import statement
from langchain_openai import ChatOpenAI
Imports ChatOpenAI for LLM interactions.
Line 7: Import statement
import os
Required import for the pattern implementation.
Line 10: Function create_debate_crew definition
def create_debate_crew(topic: str):
Defines the create_debate_crew function that processes the workflow state.
Line 11: Module docstring
"""Create a debate crew with multiple perspectives."""
Describes the pattern and its purpose.
Line 87: Function run_debate definition
def run_debate(topic: str):
Defines the run_debate function that processes the workflow state.
Line 88: Module docstring
"""Run the debate pattern."""
Describes the pattern and its purpose.
Usage Example
if __name__ == "__main__":
# Example usage
topic = "Implementing AI in healthcare diagnostics"
result = run_debate(topic)
print(f"\n\n{'='*60}")
print("FINAL SYNTHESIS")
print(f"{'='*60}")
print(result)
Key Concepts
- State Management: Uses TypedDict to maintain workflow state
- Graph-Based Flow: LangGraph orchestrates execution flow
- Agent Functions: Each node processes and updates state
- LLM Integration: Uses ChatOpenAI for intelligent processing
Pattern Benefits
- ✅ Type-safe state management
- ✅ Clear workflow visualization
- ✅ Modular and reusable components
- ✅ Easy to test and debug
- ✅ Scalable architecture
Related Patterns
- See other patterns in the same category
- Explore combination possibilities
- Check the main documentation for pattern relationships
Leave a Reply