A FastAPI-based demonstration project showcasing the implementation of AI agents using OpenAI's agent framework. This project demonstrates how to create, coordinate, and orchestrate multiple AI agents for different tasks.
This project serves as a practical example of building AI agent systems with the following key concepts:
- Multi-Agent Architecture: Demonstrates how to create specialized agents for specific tasks
- Agent Coordination: Shows how a manager agent can orchestrate multiple specialized agents
- Tool Integration: Illustrates how agents can use tools and functions to accomplish tasks
- API Integration: Provides RESTful endpoints to interact with AI agents
ai_agents_sample/
├── ai_agents/ # Translation agents module
│ ├── __init__.py
│ ├── manager_agent.py # Coordinates translation agents
│ ├── translator_agents.py # Language-specific translation agents
│ └── routes.py # Translation API endpoints
├── greeting_agent/ # Greeting agents module
│ ├── __init__.py
│ ├── goodbye_agent.py # Farewell message agent
│ ├── greeting_agent.py # Greeting message agent
│ ├── main_agent.py # Coordinates greeting/goodbye agents
│ └── routes.py # Greeting API endpoints
├── main.py # FastAPI application entry point
├── routes.py # Main application routes
└── requirements.txt # Python dependencies
- Manager Agent: Coordinates between different language translation agents
- Russian Translator: Specialized agent for English to Russian translation
- Ukrainian Translator: Specialized agent for English to Ukrainian translation
- Endpoint:
POST /ai/translate- Translates messages to multiple languages
- Main Agent: Coordinates between greeting and goodbye agents
- Greeting Agent: Generates friendly greeting messages with cultural variety
- Goodbye Agent: Creates appropriate farewell messages
- Endpoint:
POST /greeting/say-hi- Generates context-appropriate greetings
- FastAPI Integration: Modern, fast web framework with automatic API documentation
- Environment Configuration: Uses dotenv for environment variable management
- Pydantic Models: Type-safe request/response handling
- Modular Design: Clean separation of concerns with dedicated modules
- FastAPI: Modern, fast web framework for building APIs
- OpenAI Agents: Framework for creating and managing AI agents
- Pydantic: Data validation using Python type annotations
- Uvicorn: ASGI server for running FastAPI applications
- Python 3.8+: Modern Python features and syntax
-
Clone the repository
git clone https://github.com/mostopalove/ai_agents_sample cd ai_agents_sample -
Create and activate virtual environment
python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
-
Install dependencies
pip install -r requirements.txt
-
Set up environment variables Create a
.envfile in the root directory:OPENAI_API_KEY=your_openai_api_key_here
-
Start the FastAPI server
uvicorn main:app --reload
-
Access the application
- Main API: http://localhost:8000
- Interactive API docs: http://localhost:8000/docs
- Alternative API docs: http://localhost:8000/redoc
GET /- Welcome message
POST /ai/translate- Translate messages using coordinated agents{ "message": "Hello, how are you?" }
POST /greeting/say-hi- Generate context-appropriate greetings{ "message": "I want to say hello" }
- Specialized Agents: Each agent has a specific role (translation, greeting, etc.)
- Manager Agents: Coordinate multiple specialized agents for complex tasks
- Tool Integration: Agents can use custom functions and tools
- Context Awareness: Agents understand user intent and delegate appropriately
- User sends request to API endpoint
- Request is processed by the appropriate router
- Manager agent analyzes the request and determines which specialized agents to use
- Specialized agents process the request using their tools
- Results are coordinated and returned to the user
- Create a new agent module following the existing pattern
- Define agent instructions and tools
- Create a manager agent to coordinate if needed
- Add API routes for the new functionality
- Modify agent instructions in the respective agent files
- Add new tools using the
@function_tooldecorator - Update the manager agent's tool list
- Educational: Perfect for learning AI agent concepts
- Modular: Easy to extend and modify
- Production Ready: Built with FastAPI for scalability
- Well Documented: Clear code structure and comments
- API First: RESTful endpoints for easy integration
This project serves as a learning resource and can be extended with:
- Additional agent types
- More sophisticated coordination patterns
- Enhanced error handling
- Testing and validation
- Performance optimizations
This project is provided as a sample for educational purposes. Feel free to use, modify, and extend it according to your needs.