A FastAPI-based REST API for managing conversations with OpenAI's GPT Assistant. This project provides a robust backend for handling chat threads, file uploads, and message management with OpenAI's Assistant API.
- 🤖 OpenAI GPT Assistant Integration
- 📁 File Upload Support (JPEG, PNG, GIF, WEBP)
- 💬 Thread Management
- 🔄 Message Regeneration
- 📝 Message History
- 🖼️ Image Analysis Support
- 🔗 Reply Threading
- 📊 Pagination Support
- FastAPI
- OpenAI API
- SQLite
- Python 3.x
- Markdown
- Regex
- Python 3.7+
- OpenAI API Key
- OpenAI Assistant ID
- Clone the repository:
git clone https://github.com/yourusername/gpt-assistant-api.git
cd gpt-assistant-api
- Install dependencies:
pip install -r requirements.txt
- Create a
conf.py
file with your OpenAI credentials:
openai_api = 'your-openai-api-key'
assistant_id = 'your-assistant-id'
- Initialize the database:
python database.py
- Start the server:
uvicorn main:app --reload
The base URL for all API requests is:
https://api.your-site.com/
GET /threads/{user_id}
- List user's conversation threadsDELETE /threads/{user_id}/{thread_id}
- Delete a threadGET /threads/{user_id}/{thread_id}
- Get thread messages
POST /ask
- Send a message or start a new conversationPOST /regenerate
- Regenerate assistant responseGET /threads/{user_id}/{thread_id}/messages-with-files
- Get messages with file attachments
POST /upload-file
- Upload a fileGET /get-file/{file_id}
- Download a fileGET /threads/{user_id}/{thread_id}/download-files
- Download all files from a thread
gpt-assistant-api/
├── main.py # FastAPI application and routes
├── database.py # Database operations and schema
├── conf.py # Configuration and API keys
├── requirements.txt # Project dependencies
└── README.md # Project documentation
user_id
(VARCHAR) - Primary Key
thread_id
(VARCHAR) - Primary Keyuser_id
(VARCHAR) - Foreign Keytitle
(TEXT)created_at
(DATETIME)updated_at
(DATETIME)
message_id
(INTEGER) - Primary Keythread_id
(VARCHAR) - Foreign Keyrole
(TEXT)content
(TEXT)image_id
(VARCHAR)message_time
(DATETIME)reply_to
(INTEGER) - Foreign Key
- API keys are stored in a separate configuration file
- CORS middleware with configurable origins
- Error handling and logging
- Input validation for file uploads
- Secure file handling
The API includes comprehensive error handling:
- Global error middleware
- Specific error responses for various scenarios
- Detailed logging
- 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
This project is licensed under the MIT License - see the LICENSE file for details.
- OpenAI for providing the GPT Assistant API
- FastAPI framework
- All contributors and users of this project