A production-ready template for building Multi-Agent RAG (Retrieval-Augmented Generation) systems using the Swarms Framework. This template demonstrates how to create a collaborative team of AI agents that work together to process, analyze, and generate insights from documents.
-
Plug-and-Play Agent Architecture
- Easily swap or modify agents without disrupting the system
- Add custom agents with specialized capabilities
- Define your own agent interaction patterns
- Scale from 2 to 100+ agents seamlessly
- Any LLM can be used, this template uses GROQ but you can use any other LLM such as OpenAI, Anthropic, Cohere, etc.
-
Adaptable Document Processing
- Support for any document format through custom extractors
- Flexible document storage options (local, cloud, or hybrid)
- Customizable chunking and embedding strategies
- Dynamic index updates without system restart
- Any RAG system can be used, this template uses LlamaIndexDB but you can use any other RAG system.
-
Configurable Workflows
- Design custom agent communication patterns
- Implement parallel or sequential processing
- Add conditional logic and branching workflows
- Adjust system behavior through environment variables
- Clone the Repository
git clone https://github.com/The-Swarm-Corporation/Multi-Agent-RAG-Template.git
cd Multi-Agent-RAG-Template
- Set Up Environment
# Create and activate virtual environment (optional but recommended)
python -m venv venv
source venv/bin/activate # On Windows: .\venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
- Configure Environment Variables
# Create .env file
# Edit .env file with your credentials
GROQ_API_KEY="your-api-key-here"
WORKSPACE_DIR="agent_workspace"
OPENAI_API_KEY="your-openai-api-key-here"
- Run the Example
python main.py
Multi-Agent-RAG-Template/
βββ main.py # Main entry point
βββ multi_agent_rag/
β βββ agents.py # Agent definitions
β βββ memory.py # RAG implementation
βββ docs/ # Place your documents here
βββ requirements.txt # Project dependencies
βββ .env # Environment variables
- Open
multi_agent_rag/agents.py
- Create a new agent using the Agent class:
new_agent = Agent(
agent_name="New-Agent",
system_prompt="Your system prompt here",
llm=model,
max_loops=1,
# ... additional configuration
)
In main.py
, update the flow
parameter in the AgentRearrange
initialization:
flow=f"{agent1.agent_name} -> {agent2.agent_name} -> {new_agent.agent_name}"
- The
memory_system
parameter in theAgentRearrange
initialization is used to configure the RAG system. - The
memory_system
parameter is an instance ofLlamaIndexDB
, which is a database class for storing and retrieving medical documents. - The
memory_system
class must have aquery(query: str)
method that returns a string for the agent to use it.
# Import the AgentRearrange class for coordinating multiple agents
from swarms import AgentRearrange
from multi_agent_rag.agents import (
diagnostic_specialist,
medical_data_extractor,
patient_care_coordinator,
specialist_consultant,
treatment_planner,
)
from multi_agent_rag.memory import LlamaIndexDB
router = AgentRearrange(
name="medical-diagnosis-treatment-swarm",
description="Collaborative medical team for comprehensive patient diagnosis and treatment planning",
max_loops=1,
agents=[
medical_data_extractor,
diagnostic_specialist,
treatment_planner,
specialist_consultant,
patient_care_coordinator,
],
memory_system=LlamaIndexDB(
data_dir="docs",
filename_as_id=True,
recursive=True,
similarity_top_k=10,
),
flow=f"{medical_data_extractor.agent_name} -> {diagnostic_specialist.agent_name} -> {treatment_planner.agent_name} -> {specialist_consultant.agent_name} -> {patient_care_coordinator.agent_name}",
)
if __name__ == "__main__":
router.run(
"Analyze this Lucas Brown's medical data to provide a diagnosis and treatment plan"
)
Here is an example of how to use Pinecone as the RAG system.
- Make sure you have a Pinecone index created and the
PINECONE_API_KEY
,PINECONE_INDEX_NAME
, andPINECONE_ENVIRONMENT
environment variables set. - See the
pinecone_swarm.py
file for the full example. - The
PineconeManager
class is used to interface with the Pinecone API.
import os
from swarms import AgentRearrange
from multi_agent_rag.agents import (
diagnostic_specialist,
medical_data_extractor,
patient_care_coordinator,
specialist_consultant,
treatment_planner,
)
from multi_agent_rag.pinecone_wrapper import PineconeManager
router = AgentRearrange(
name="medical-diagnosis-treatment-swarm",
description="Collaborative medical team for comprehensive patient diagnosis and treatment planning",
max_loops=1,
agents=[
medical_data_extractor,
diagnostic_specialist,
treatment_planner,
specialist_consultant,
patient_care_coordinator,
],
memory_system=PineconeManager(
api_key=os.getenv("PINECONE_API_KEY"),
index_name=os.getenv("PINECONE_INDEX_NAME"),
environment=os.getenv("PINECONE_ENVIRONMENT"),
),
flow=f"{medical_data_extractor.agent_name} -> {diagnostic_specialist.agent_name} -> {treatment_planner.agent_name} -> {specialist_consultant.agent_name} -> {patient_care_coordinator.agent_name}",
)
if __name__ == "__main__":
router.run(
"Analyze this Lucas Brown's medical data to provide a diagnosis and treatment plan"
)
For detailed documentation on:
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request g
- Swarms Framework
- Python 3.10+
- GROQ API Key or you can change it to use any model from Swarm Models
- LlamaIndexDB for storing and retrieving medical documents
Questions? Reach out:
- Twitter: @kyegomez
- Email: [email protected]
Book a call with here for real-time assistance:
β Star us on GitHub if this project helped you!
Built with β₯ using Swarms Framework