This project is designed to provide a robust backend service for managing conversations and generating responses using OpenAI's GPT-4 model. The system is built with FastAPI and MongoDB, ensuring high performance and scalability.
The system is composed of several key components:
- FastAPI Application: The core of the backend service, handling HTTP requests and routing.
- MongoDB: A NoSQL database used to store conversation data.
- OpenAI Integration: Utilizes OpenAI's API to generate responses based on user queries.
main.py
: Entry point of the application. Configures and starts the FastAPI server.routes.py
: Defines the API endpoints and their respective handlers.services.py
: Contains business logic for creating and updating conversations.db.py
: Manages database connections and collections.settings.py
: Configuration settings for the application, including environment variables.generatefakedata.py
: Utility script for generating and inserting sample data into the database.log.py
: Configures logging for the application.
The data models are defined in models.py
and include:
Conversation
: Represents a conversation document.Message
: Represents individual messages within a conversation.ChatCompletion
: Represents the completion response from OpenAI.
- Python 3.8+
- MongoDB
- OpenAI API Key
-
Clone the repository:
git clone https://github.com/your-repo/project-name.git cd project-name
-
Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install dependencies:
pip install -r requirements.txt
-
Set up environment variables: Create a
.env
file in the root directory and add the following:OPENAI_API_KEY=your_openai_api_key OPENAI_MODEL=gpt-4o-mini DB_URL=your_mongodb_url DB_NAME=your_db_name BASE_URL=127.0.0.1 PORT=8000
-
Run the FastAPI server:
ENVIRONMENT=dev python main.py
This will start the server at
http://127.0.0.1:8000
.
To generate and insert sample data into the MongoDB collection, run:
python generatefakedata.py
Logging is configured via logging.ini. Logs are written to a file named with the current date in the format YYYY-MM-DD_Uvicorn.log.
POST /conversations
: Create a new conversation.GET /conversations/{item_id}
: Retrieve a conversation by ID.GET /conversations/{item_id}/history
: Retrieve the history of a conversation.PUT /conversations/{item_id}
: Update a conversation by ID.DELETE /conversations/{item_id}
: Delete a conversation by ID.POST /conversations/{item_id}/prompts
: Add a prompt to an existing conversation.
- The
POST /conversations/{item_id}/prompts
API does not work as expected due to an error{"detail":[{"type":"model_attributes_type","loc":["body","prompt"],"msg":"Input should be a valid dictionary or object to extract fields from","input":"{\"role\": \"user\", \"content\": \"help me to create a credit report based on the file attached\", \"additionalProp1\": {}}"}]}
Please read CONTRIBUTING.md for details on our code of conduct and the process for submitting pull requests.
This project is licensed under the MIT License - see the LICENSE file for details.