Learn how to deploy PraisonAI agents as RESTful APIs for production environments
PraisonAI agents can be easily deployed as RESTful APIs, allowing you to integrate them into various applications and services. This guide covers how to deploy both single and multiple agents as APIs.
Once your agent is deployed, you can make POST requests to interact with it:
Copy
curl -X POST http://localhost:3030/ask \ -H "Content-Type: application/json" \ -d '{"message": "What is artificial intelligence?"}'
The response will be in JSON format:
Copy
{ "response": "Artificial intelligence (AI) refers to the simulation of human intelligence in machines that are programmed to think and learn like humans. It encompasses various technologies including machine learning, natural language processing, computer vision, and robotics. AI systems can perform tasks that typically require human intelligence such as understanding natural language, recognizing patterns, solving problems, and making decisions."}
You can deploy multiple agents on the same server, each with its own endpoint:
Copy
from praisonaiagents import Agentweather_agent = Agent( instructions="""You are a weather agent that can provide weather information for a given city.""", llm="gpt-4o-mini")stock_agent = Agent( instructions="""You are a stock market agent that can provide information about stock prices and market trends.""", llm="gpt-4o-mini")travel_agent = Agent( instructions="""You are a travel agent that can provide recommendations for destinations, hotels, and activities.""", llm="gpt-4o-mini")weather_agent.launch(path="/weather", port=3030)stock_agent.launch(path="/stock", port=3030)travel_agent.launch(path="/travel", port=3030)
When launching your agent as an API, you can customize various parameters:
Copy
agent.launch( path="/custom-endpoint", # API endpoint path port=8080, # Port number host="0.0.0.0", # Host address (0.0.0.0 for external access) debug=True, # Enable debug mode cors_origins=["*"], # CORS configuration api_key="your-api-key" # Optional API key for authentication)