A Python-based solution that employs Multi-Agent Reasoning, where multiple AI agents collaborate to generate optimal responses to user prompts. By simulating interactions between agents and integrating the Swarm Framework for Intelligence, the system enhances reasoning capabilities to deliver accurate and refined answers. Custom agents can be added via JSON, allowing you to customize their personalities, interaction styles, and more. The system leverages Prompt Caching to optimize performance and reduce latency and costs for repeated prompts.
The Multi-Agent Reasoning script creates an interactive chatbot experience where multiple AI agents collaborate through a structured reasoning process to provide optimal answers. Each agent brings unique perspectives and expertise, and through iterative steps of discussion, verification, critique, and refinement, they converge on a high-quality, accurate response.
Additionally, the system integrates the Swarm Framework for Intelligence to enhance collaboration among agents. Swarm allows agents to coordinate efficiently, leveraging collective intelligence to solve complex tasks.
Users can also chat with individual agents. Agents are aware of each other, including their personalities and quirks, and can answer questions about one another, providing a rich and interactive experience.
Clone the repository:
git clone https://github.com/AdieLaine/multi-agent-reasoning.git
Navigate to the project directory:
cd multi-agent-reasoning
Install the required packages:
pip install openai colorama tiktoken
Install Swarm:
pip install git+ssh://[email protected]/openai/swarm.git
or
pip install git+https://github.com/openai/swarm.git
Refer to Swarm's GitHub repository for detailed installation instructions.
Set your OpenAI API key:
Set your API key as an environment variable:
export OPENAI_API_KEY='your-api-key-here'
Alternatively, you can set it directly in your script or use a .env
file.
Run the script using Python:
python reasoning.py
Upon running the script, you will be presented with a menu:
═════════════════════════════════════════════════════════════════════════════════════════════
║ Multi-Agent Reasoning Chatbot ║
═════════════════════════════════════════════════════════════════════════════════════════════
Please select an option:
1. Chat with an agent
2. Use reasoning logic
3. Use Swarm-based reasoning
4. Exit
Enter your choice (1/2/3/4):
Option 1: Chat with an agent
Option 2: Use reasoning logic
Option 3: Use Swarm-based reasoning
Option 4: Exit
The Multi-Agent Reasoning system uses specific OpenAI models:
o1-preview-2024-09-12
model for reasoning tasks.gpt-4o
model for chat interactions with agents.gpt-4o
.These models support advanced features and token usage reporting, allowing the system to provide detailed token usage information after each response.
Objective: Allows the user to chat directly with a selected agent.
Example:
The core of the chatbot's functionality lies in the reasoning process employed by the agents. This process is designed to simulate a collaborative environment where agents think critically, verify facts, challenge each other's perspectives, and refine their responses based on constructive feedback.
Objective: Agents generate their initial responses to the user's prompt based on their individual reasoning and knowledge.
Example:
Objective: Agents verify the accuracy and validity of their own responses to ensure factual correctness.
Example:
Objective: Agents critique each other's verified responses to identify areas of improvement, omissions, or biases.
Example:
Objective: Agents refine their own responses by incorporating feedback from critiques and improving upon their initial reasoning.
Example:
Objective: Combine the refined responses from all agents into a single, cohesive, and comprehensive answer.
blend_responses
function.Example:
Objective: Incorporate the user's feedback to further refine the response, ensuring satisfaction and accuracy.
MAX_REFINEMENT_ATTEMPTS
.Example:
Objective: Allow the conversation to maintain context across multiple user prompts for a coherent dialogue.
Example:
Swarm Integration enhances the Multi-Agent Reasoning system by enabling dynamic agent coordination and task delegation. Swarm allows agents to collaborate efficiently, leveraging collective intelligence to solve complex tasks and improve responsiveness.
Swarm focuses on making agent coordination and execution lightweight, highly controllable, and easily testable. It achieves this through two primitive abstractions: Agents and Handoffs. An Agent encompasses instructions and tools and can, at any point, choose to hand off a conversation to another Agent.
Swarm Client Initialization: The system initializes a Swarm client to manage agent interactions.
from swarm import Agent, Swarm
client = Swarm()
Agent Initialization:
agents.json
.Conversation Handling:
Objective: Utilize Swarm Framework for Intelligence to coordinate agents dynamically, allowing for efficient collaboration and task delegation.
Initialization:
agents.json
configuration file.Discussion:
client.run()
method.Verification:
Critiquing:
Refinement:
Blending Responses:
blend_responses
function.Example:
Objective: Provide a chat interface that leverages Swarm's capabilities for seamless agent interaction.
Swarm Agent for Chat:
Conversation Handling:
def swarm_chat_interface(conversation_history):
# Load Swarm agent's configuration
swarm_agent = ... # Initialize Swarm agent
messages = [{"role": "system", "content": swarm_agent.instructions}]
messages.extend(conversation_history)
response = client.run(agent=swarm_agent, messages=messages)
swarm_reply = response.messages[-1]['content'].strip()
return swarm_reply
Dynamic Responses:
Example:
Agent Design:
Function Definitions:
Context Variables:
Error Handling:
Testing:
What is Swarm, and how does it enhance the system?
Do I need to modify my existing agents to work with Swarm?
Agent
instances. Existing agents can be adapted by incorporating Swarm's structure and conventions.Can I add more agents to the Swarm system?
agents.json
file and initialize them in the system.How does Swarm handle agent handoffs?
Is Swarm compatible with the models used in the system?
gpt-4o
.Prompt Caching enhances the efficiency of the Multi-Agent Reasoning system by reducing latency and cost when handling repeated or long prompts. It works by caching the longest common prefixes of prompts, allowing for faster processing of subsequent requests that reuse these prefixes.
Cache Duration:
usage
field displaying token usage details."usage": {
"prompt_tokens": 2006,
"completion_tokens": 300,
"total_tokens": 2306,
"prompt_tokens_details": {
"cached_tokens": 1920
},
"completion_tokens_details": {
"reasoning_tokens": 0
}
}
cached_tokens
indicates how many prompt tokens were retrieved from the cache.Agents are configured via an agents.json
file, allowing easy customization of their attributes.
Location: Must be placed in the same directory as the reasoning.py
script.
Structure:
{
"agents": [
{
"name": "Agent 47",
"system_purpose": "You are a logical and analytical assistant, focusing on facts and clear reasoning.",
"interaction_style": { ... },
"ethical_conduct": { ... },
"capabilities_limitations": { ... },
"context_awareness": { ... },
"adaptability_engagement": { ... },
"responsiveness": { ... },
"additional_tools_modules": { ... },
"personality": {
"logical": "Yes",
"analytical": "Yes",
"humor_style": "...",
"friendly_demeanor": "...",
"personality_traits": ["Methodical", "Precise"],
"empathy_level": "Moderate",
"interaction_style_with_humor": "Dry wit",
"quirks": ["Uses technical jargon"]
}
},
{
"name": "Agent 74",
"system_purpose": "You are a creative and empathetic assistant, emphasizing imaginative solutions and understanding.",
"interaction_style": { ... },
"ethical_conduct": { ... },
"capabilities_limitations": { ... },
"context_awareness": { ... },
"adaptability_engagement": { ... },
"responsiveness": { ... },
"additional_tools_modules": { ... },
"personality": {
"creative": "Yes",
"empathetic": "Yes",
"humor_style": "...",
"friendly_demeanor": "...",
"personality_traits": ["Imaginative", "Caring"],
"empathy_level": "High",
"interaction_style_with_humor": "Playful",
"quirks": ["Uses metaphors"]
}
},
{
"name": "Swarm Agent",
"system_purpose": "You are a collaborative AI assistant composed of multiple expert agents. You coordinate tasks among agents to provide comprehensive and accurate responses.",
"interaction_style": { ... },
"personality": {
"coordinator": "Yes",
"collaborative": "Yes",
"personality_traits": ["Organized", "Facilitator"],
"quirks": ["Ensures all perspectives are considered"]
}
}
]
}
Customization:
Example:
The code is structured to facilitate both the reasoning process and chat interactions with agents. It also incorporates Swarm Framework for enhanced agent coordination.
Libraries:
os
, time
, logging
, json
: For system operations, timing, logging, and JSON handling.colorama
: For colored console output.swarm
: For implementing Swarm Intelligence.tiktoken
: For accurate token counting (in other parts of the script).Initialization:
from swarm import Agent, Swarm
client = Swarm()
Agents are initialized from the agents.json
configuration file.
Each agent is created as a Swarm Agent
instance with specific instructions and attributes.
Agents are made aware of each other by appending information about other agents to their instructions.
def initialize_swarm_agents():
# Load agents from agents.json and create Swarm agents
agents = []
# ... Load and initialize agents with awareness of others
return agents
Function: swarm_chat_interface(conversation_history)
Purpose: Handles chat interactions with the Swarm agent.
Process:
def swarm_chat_interface(conversation_history):
# Prepare messages
messages = [{"role": "system", "content": swarm_agent.instructions}]
messages.extend(conversation_history)
# Run Swarm client
response = client.run(agent=swarm_agent, messages=messages)
swarm_reply = response.messages[-1]['content'].strip()
return swarm_reply
Function: run_swarm_reasoning(user_prompt)
Purpose: Uses Swarm agents to collaborate and respond to a user prompt following multiple reasoning stages.
Process:
blend_responses
function combines the refined responses into a final answer.Parallel Processing: Swarm allows agents to perform these steps concurrently, enhancing efficiency.
Example of Blending Function:
def blend_responses(agent_responses, user_prompt):
# Prepare combined prompt
combined_prompt = ...
# Initialize Blender agent
blender_agent = Agent(
name="Swarm Agent",
instructions="You are a collaborative AI assistant composed of multiple expert agents."
)
# Run blending process
response = client.run(agent=blender_agent, messages=[{"role": "user", "content": combined_prompt}])
blended_reply = response.messages[-1]['content']
return blended_reply
swarm_middle_agent_interface(user_prompt)
:
run_swarm_reasoning
with the user's prompt.swarm_chat_interface(conversation_history)
:
Below is an updated flowchart reflecting the new logic, including the chat mode, agents' awareness of each other, token usage transparency, prompt caching, and Swarm integration:
Contributions are welcome! To contribute:
This project is licensed under the MIT License.
To prepare the GitHub repository:
Create a new repository on GitHub named multi-agent-reasoning
.
Add the README.md
file with this content.
Include the reasoning.py
script in the root directory.
Include the agents.json
file in the root directory.
Create a .gitignore
file to exclude unnecessary files:
# Exclude log files
reasoning.log
swarm_middle_agent.log
# Exclude environment files
.env
# Python cache
__pycache__/
*.py[cod]
Commit and push the files to GitHub.
multi-agent-reasoning/
├── README.md
├── reasoning.py
├── swarm_middle_agent.py
├── agents.json
├── LICENSE
├── .gitignore
└── img/
├── reasoningbanner.png
├── reasoningflow.png
├── agents.png
└── promptcache.png
└── swarm.png
Feel free to explore the code, customize the agents, and engage with the Multi-Agent Reasoning chatbot!
If you have any questions or need assistance, please open an issue on GitHub.