|
| 1 | +--- |
| 2 | +title: "Deploying Multi-Agent Systems as APIs" |
| 3 | +sidebarTitle: "Multi-Agent Deployment" |
| 4 | +description: "Learn how to deploy PraisonAI multi-agent systems as RESTful APIs for production environments" |
| 5 | +icon: "network-wired" |
| 6 | +--- |
| 7 | + |
| 8 | +# Deploying Multi-Agent Systems as APIs |
| 9 | + |
| 10 | +PraisonAI allows you to deploy sophisticated multi-agent systems as RESTful APIs, enabling seamless integration with various applications and services. This guide covers different approaches to deploying multi-agent systems. |
| 11 | + |
| 12 | +## Quick Start |
| 13 | + |
| 14 | +<Steps> |
| 15 | + <Step title="Install Dependencies"> |
| 16 | + Make sure you have the required packages installed: |
| 17 | + ```bash |
| 18 | + pip install "praisonaiagents[api]>=0.0.81" |
| 19 | + ``` |
| 20 | + </Step> |
| 21 | + <Step title="Set API Key"> |
| 22 | + ```bash |
| 23 | + export OPENAI_API_KEY="your_api_key" |
| 24 | + ``` |
| 25 | + </Step> |
| 26 | + <Step title="Deploy a Multi-Agent System"> |
| 27 | + Create a file named `multi-agents-api.py` with the following code: |
| 28 | + ```python |
| 29 | + from praisonaiagents import Agent, Agents, Tools |
| 30 | + |
| 31 | + research_agent = Agent(name="Research", instructions="You are a research agent to search internet about AI 2024", tools=[Tools.internet_search]) |
| 32 | + summarise_agent = Agent(name="Summarise", instructions="You are a summarize agent to summarise in points") |
| 33 | + agents = Agents(agents=[research_agent, summarise_agent]) |
| 34 | + agents.launch(path="/agents", port=3030) |
| 35 | + ``` |
| 36 | + </Step> |
| 37 | + <Step title="Run the API Server"> |
| 38 | + ```bash |
| 39 | + python multi-agents-api.py |
| 40 | + ``` |
| 41 | + |
| 42 | + Your multi-agent API will be available at `http://localhost:3030/agents` |
| 43 | + </Step> |
| 44 | +</Steps> |
| 45 | + |
| 46 | +## Making API Requests |
| 47 | + |
| 48 | +Once your multi-agent system is deployed, you can make POST requests to interact with it: |
| 49 | + |
| 50 | +```bash |
| 51 | +curl -X POST http://localhost:3030/agents \ |
| 52 | + -H "Content-Type: application/json" \ |
| 53 | + -d '{"message": "What are the latest developments in AI in 2024?"}' |
| 54 | +``` |
| 55 | + |
| 56 | +The response will include the collaborative output from both the research and summarization agents: |
| 57 | + |
| 58 | +```json |
| 59 | +{ |
| 60 | + "response": "# Latest AI Developments in 2024\n\n- Multimodal AI models have become mainstream, combining text, image, audio, and video understanding\n- Significant advancements in AI reasoning capabilities with models like GPT-4o and Claude 3\n- Increased focus on AI alignment and safety research\n- Emergence of specialized AI agents for specific domains\n- Growth in open-source AI models and frameworks\n- Regulatory frameworks for AI being established globally" |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +## Multiple Agent Groups |
| 65 | + |
| 66 | +You can deploy multiple agent groups on the same server, each with its own endpoint: |
| 67 | + |
| 68 | +```python |
| 69 | +from praisonaiagents import Agent, Agents, Tools |
| 70 | + |
| 71 | +research_agent = Agent(name="Research", instructions="You are a research agent to search internet about AI 2024", tools=[Tools.internet_search]) |
| 72 | +summarise_agent = Agent(name="Summarise", instructions="You are a summarize agent to summarise in points") |
| 73 | +agents = Agents(agents=[research_agent, summarise_agent]) |
| 74 | +agents2 = Agents(agents=[research_agent]) |
| 75 | +agents.launch(path="/agents", port=3030) |
| 76 | +agents2.launch(path="/agents2", port=3030) |
| 77 | +``` |
| 78 | + |
| 79 | +With this setup, you can access: |
| 80 | +- The full agent group at `http://localhost:3030/agents` |
| 81 | +- Just the research agent at `http://localhost:3030/agents2` |
| 82 | + |
| 83 | +## Independent Multi-Agent Deployment |
| 84 | + |
| 85 | +You can also deploy multiple independent agents on the same server: |
| 86 | + |
| 87 | +```python |
| 88 | +from praisonaiagents import Agent |
| 89 | + |
| 90 | +weather_agent = Agent( |
| 91 | + instructions="""You are a weather agent that can provide weather information for a given city.""", |
| 92 | + llm="gpt-4o-mini" |
| 93 | +) |
| 94 | + |
| 95 | +stock_agent = Agent( |
| 96 | + instructions="""You are a stock market agent that can provide information about stock prices and market trends.""", |
| 97 | + llm="gpt-4o-mini" |
| 98 | +) |
| 99 | + |
| 100 | +travel_agent = Agent( |
| 101 | + instructions="""You are a travel agent that can provide recommendations for destinations, hotels, and activities.""", |
| 102 | + llm="gpt-4o-mini" |
| 103 | +) |
| 104 | + |
| 105 | +weather_agent.launch(path="/weather", port=3030) |
| 106 | +stock_agent.launch(path="/stock", port=3030) |
| 107 | +travel_agent.launch(path="/travel", port=3030) |
| 108 | +``` |
| 109 | + |
| 110 | +## Production Deployment Options |
| 111 | + |
| 112 | +For production environments, consider the following deployment options: |
| 113 | + |
| 114 | +### Docker Deployment |
| 115 | + |
| 116 | +<Steps> |
| 117 | + <Step title="Create a Dockerfile"> |
| 118 | + ```dockerfile |
| 119 | + FROM python:3.11-slim |
| 120 | + |
| 121 | + WORKDIR /app |
| 122 | + |
| 123 | + COPY requirements.txt . |
| 124 | + RUN pip install --no-cache-dir -r requirements.txt |
| 125 | + |
| 126 | + COPY . . |
| 127 | + |
| 128 | + EXPOSE 3030 |
| 129 | + |
| 130 | + CMD ["python", "multi-agents-api.py"] |
| 131 | + ``` |
| 132 | + </Step> |
| 133 | + <Step title="Create requirements.txt"> |
| 134 | + ``` |
| 135 | + praisonaiagents[api]>=0.0.81 |
| 136 | + ``` |
| 137 | + </Step> |
| 138 | + <Step title="Build and Run Docker Container"> |
| 139 | + ```bash |
| 140 | + docker build -t praisonai-multi-agent . |
| 141 | + docker run -p 3030:3030 -e OPENAI_API_KEY=your_api_key praisonai-multi-agent |
| 142 | + ``` |
| 143 | + </Step> |
| 144 | +</Steps> |
| 145 | + |
| 146 | +### Cloud Deployment |
| 147 | + |
| 148 | +For detailed cloud deployment instructions, refer to: |
| 149 | +- [AWS Deployment Guide](/deploy/aws) |
| 150 | +- [Google Cloud Deployment Guide](/deploy/googlecloud) |
| 151 | + |
| 152 | +## Scaling Multi-Agent Systems |
| 153 | + |
| 154 | +When deploying multi-agent systems to production, consider these scaling strategies: |
| 155 | + |
| 156 | +1. **Horizontal Scaling**: Deploy multiple instances behind a load balancer |
| 157 | +2. **Vertical Scaling**: Allocate more CPU and memory resources for complex agent interactions |
| 158 | +3. **Caching**: Implement response caching for frequently asked questions |
| 159 | +4. **Asynchronous Processing**: Use message queues for handling long-running agent tasks |
| 160 | + |
| 161 | +## API Configuration Options |
| 162 | + |
| 163 | +When launching your multi-agent system as an API, you can customize various parameters: |
| 164 | + |
| 165 | +```python |
| 166 | +agents.launch( |
| 167 | + path="/custom-endpoint", # API endpoint path |
| 168 | + port=8080, # Port number |
| 169 | + host="0.0.0.0", # Host address (0.0.0.0 for external access) |
| 170 | + debug=True, # Enable debug mode |
| 171 | + cors_origins=["*"], # CORS configuration |
| 172 | + api_key="your-api-key" # Optional API key for authentication |
| 173 | +) |
| 174 | +``` |
| 175 | + |
| 176 | +## Securing Your Multi-Agent API |
| 177 | + |
| 178 | +For production deployments, consider implementing: |
| 179 | + |
| 180 | +1. **API Key Authentication**: Require API keys for all requests |
| 181 | +2. **Rate Limiting**: Limit the number of requests per client |
| 182 | +3. **HTTPS**: Use SSL/TLS certificates for encrypted communication |
| 183 | +4. **Input Validation**: Validate all input data before processing |
| 184 | +5. **Output Filtering**: Implement content filtering for agent responses |
| 185 | + |
| 186 | +## Monitoring and Logging |
| 187 | + |
| 188 | +For production environments, consider: |
| 189 | + |
| 190 | +1. **Centralized Logging**: Collect logs from all agents in a central location |
| 191 | +2. **Performance Metrics**: Track response times and resource usage |
| 192 | +3. **Error Tracking**: Monitor and alert on agent failures |
| 193 | +4. **Usage Analytics**: Track which agents are used most frequently |
| 194 | + |
| 195 | +## Features |
| 196 | + |
| 197 | +<CardGroup cols={2}> |
| 198 | + <Card title="Collaborative Agents" icon="users-gear"> |
| 199 | + Deploy agent systems that collaborate to solve complex problems. |
| 200 | + </Card> |
| 201 | + <Card title="Specialized Endpoints" icon="sitemap"> |
| 202 | + Create dedicated endpoints for different agent groups or individual agents. |
| 203 | + </Card> |
| 204 | + <Card title="Tool Integration" icon="screwdriver-wrench"> |
| 205 | + Deploy agents with specialized tools like web search capabilities. |
| 206 | + </Card> |
| 207 | + <Card title="Scalable Architecture" icon="server"> |
| 208 | + Scale your multi-agent systems to handle production workloads. |
| 209 | + </Card> |
| 210 | +</CardGroup> |
0 commit comments