A FastAPI-based web service that integrates with OpenAI's Assistant API to create conversational AI applications with function calling capabilities.
- π€ OpenAI Assistant Integration: Seamless integration with OpenAI's Assistant API
- π§ Function Calling: Support for custom function execution during conversations
- π§΅ Thread Management: Conversation thread creation and management
- π FastAPI Framework: High-performance async web framework
- π Booking System: Example booking function implementation
- β‘ Real-time Processing: Asynchronous message processing
- Python 3.8+
- OpenAI API key
- OpenAI Assistant ID
-
Clone the repository
git clone <your-repository-url> cd <repository-name>
-
Install dependencies
pip install -r requirements.txt
-
Environment Setup
Create a
.envfile in the root directory and add your credentials:OPENAI_API_KEY=your_openai_api_key_here CHATBOT_ASSISTANT_ID=your_assistant_id_here
Important: Update the
main.pyfile to load these environment variables:import os from dotenv import load_dotenv load_dotenv() OPENAI_API_KEY = os.getenv("OPENAI_API_KEY") CHATBOT_ASSISTANT_ID = os.getenv("CHATBOT_ASSISTANT_ID")
Run the FastAPI server:
python main.pyThe server will start on http://0.0.0.0:8001
GET /startResponse:
{
"thread_id": "thread_abc123"
}POST /chatRequest Body:
{
"thread_id": "thread_abc123",
"message": "Hello, I'd like to make a booking"
}Response:
{
"response": "Hello! I'd be happy to help you with your booking..."
}-
Start a conversation:
curl -X GET http://localhost:8001/start
-
Send a message:
curl -X POST http://localhost:8001/chat \ -H "Content-Type: application/json" \ -d '{ "thread_id": "your_thread_id", "message": "I want to book an appointment" }'
The application includes an example booking function that can be called by the OpenAI Assistant:
def prenota(nome, numero_cel, riassunto_breve_chat):
"""
Example booking function
Args:
nome (str): Customer name
numero_cel (str): Phone number
riassunto_breve_chat (str): Brief chat summary
Returns:
str: Confirmation message
"""To add new functions:
- Define your function in
main.py - Add it to the
available_functionsdictionary - Configure the function in your OpenAI Assistant settings
.
βββ main.py # Main FastAPI application
βββ requirements.txt # Python dependencies
βββ README.md # Project documentation
βββ .env # Environment variables (create this)
- FastAPI: Modern web framework for building APIs
- OpenAI: Official OpenAI Python client
- Uvicorn: ASGI server for FastAPI
- Pydantic: Data validation using Python type annotations
- python-dotenv: Load environment variables from .env file
The application requires OpenAI Python client version 1.1.1 or higher. The version check is performed automatically on startup.
Default server configuration:
- Host:
0.0.0.0(all interfaces) - Port:
8001
To change these settings, modify the uvicorn.run() call in main.py.
The application handles various scenarios:
- Missing thread ID
- OpenAI API errors
- Function execution errors
- Assistant run failures
- Never commit API keys to version control
- Use environment variables for sensitive data
- Implement proper authentication for production use
- Validate and sanitize function inputs
- Set up CORS if needed for web frontend integration
For development with auto-reload:
uvicorn main:app --reload --host 0.0.0.0 --port 8001Test the API endpoints using:
- FastAPI's automatic interactive documentation at
http://localhost:8001/docs - Postman or similar API testing tools
- curl commands as shown in the usage examples
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
If you encounter any issues or have questions:
- Check the OpenAI API documentation
- Review the FastAPI documentation
- Open an issue in this repository
- Initial release
- OpenAI Assistant API integration
- Basic function calling support
- FastAPI web service implementation