|
|
||
|---|---|---|
| assistant | ||
| common | ||
| loop | ||
| mirror | ||
| .env.example | ||
| .gitignore | ||
| __init__.py | ||
| celery_config.py | ||
| main.py | ||
| Procfile | ||
| README.md | ||
| requirements.txt | ||
Unified FastAPI Microservices Project
This project consolidates three distinct microservices—Assistant, Loop, and Mirror—into a single, unified FastAPI application. This structure enhances modularity, maintainability, and scalability while providing a cohesive interface for interacting with each service.
Table of Contents
- Overview
- Prerequisites
- Installation
- Configuration
- Running the Application
- API Documentation
- Healthcheck Endpoints
- Testing
- Logging
- Contributing
- License
Overview
The Unified FastAPI Microservices Project combines three microservices into a single FastAPI application:
- Assistant: Manages assistant-related operations, including creation, updating, deletion, and message handling.
- Loop: Handles knowledge extraction, context augmentation, and interaction with GPT models for extracting information.
- Mirror: Facilitates document comparison and analysis using GPT models.
Leveraging FastAPI's robust features, this project ensures efficient request handling, easy scalability, and comprehensive API documentation.
Description of Key Components
- assistant/: Contains the Assistant microservice, including its routes, business logic, constants, and helper functions.
- loop/: Hosts the Loop microservice with its respective routes, context augmentation logic, GPT interactions, constants, and helpers.
- mirror/: Comprises the Mirror microservice, encapsulating its routes, GPT interactions, and helper utilities.
- common/: Includes shared resources like the logging configuration to ensure consistent logging across all microservices.
- main.py: The entry point of the FastAPI application that integrates all microservice routers.
- requirements.txt: Lists all Python dependencies required for the project.
- .env: Stores environment variables, including sensitive information like API keys.
Prerequisites
- Python 3.8 or higher
- pip package manager
- Virtualenv (optional but recommended)
Installation
-
Clone the Repository
git clone https://github.com/yourusername/unified_fastapi_project.git cd unified_fastapi_project -
Create a Virtual Environment
It's recommended to use a virtual environment to manage dependencies.
python3 -m venv venv source venv/bin/activate # On Windows, use `venv\Scripts\activate` -
Upgrade pip
Ensure that pip is up-to-date.
pip install --upgrade pip -
Install Dependencies
pip install -r requirements.txt
Configuration
The application relies on environment variables defined in the .env file. Ensure that this file is present in the root directory with the necessary configurations.
.env File
Create a .env file in the root directory and populate it with the following variables:
# OpenAI API Key
OPENAI_KEY=your_openai_api_key_here
# General Settings
MODEL=gpt-4o
TEMPERATURE=0.5
MAX_TOKENS=100
# Loop Service URL (if applicable)
KNOWLEDGE_EXTRACTOR_API_URL=http://localhost:8000/loop
Note: Replace your_openai_api_key_here with your actual OpenAI API key. Adjust other variables as needed based on your requirements.
Running the Application
You can start the FastAPI application using Uvicorn. Below are the commands to run the server.
Using main.py
python main.py
Directly with Uvicorn
uvicorn main:app --host 0.0.0.0 --port 8000 --reload
--host: Specifies the host address.0.0.0.0makes the server accessible externally.--port: Defines the port number. Default is8000.--reload: Enables auto-reload on code changes (useful for development).
Celery: "celery -A celery_config.celery_app worker --loglevel=info --pool=solo"
API Documentation
FastAPI automatically generates interactive API documentation. You can access it via the following URLs:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
These interfaces allow you to explore and test the API endpoints interactively.
Healthcheck Endpoints
Each microservice includes a Healthcheck endpoint to verify if the service is running correctly. These endpoints return a 200 OK status when operational.
Endpoints
-
Assistant Healthcheck
GET /assistant/ -
Loop Healthcheck
GET /loop/ -
Mirror Healthcheck
GET /mirror/
Example Using curl
curl -i http://localhost:8000/loop/
Expected Response:
HTTP/1.1 200 OK
content-length: 58
content-type: application/json
date: Sat, 27 Apr 2024 12:34:56 GMT
{
"status": "ok",
"message": "Loop service is running"
}
Running check_loop_service
The check_loop_service function verifies if the Loop service is running by hitting its healthcheck endpoint.
Function Implementation
import requests
from your_project_settings import settings # Adjust the import based on your project structure
def check_loop_service() -> bool:
"""
Check if the loop service is running
Returns:
bool: True if the service is running, False otherwise
"""
try:
url = settings.CONFIG.get('KNOWLEDGE_EXTRACTOR_API_URL', None)
if url is None:
return False
response = requests.get(f'{url}/loop/')
return response.status_code == 200
except requests.exceptions.RequestException:
return False
Note: Ensure that KNOWLEDGE_EXTRACTOR_API_URL in your .env file points to the correct Loop service URL.
Testing
To ensure that all services are running correctly:
-
Start the Application
uvicorn main:app --host 0.0.0.0 --port 8000 --reload -
Access Healthcheck Endpoints
Use your browser or tools like
curlorPostmanto access the healthcheck endpoints as described above. -
Interact with API Endpoints
Utilize the interactive API documentation at http://localhost:8000/docs to test various endpoints.
Logging
Logging is centralized in the common/logger.py module to ensure consistent logging across all microservices.
Configuration
# unified_fastapi_project/common/logger.py
import logging
import sys
def setup_logger():
logger = logging.getLogger("unified_fastapi_project")
logger.setLevel(logging.INFO)
if logger.hasHandlers():
logger.handlers.clear()
stream_handler = logging.StreamHandler(sys.stdout)
stream_handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
stream_handler.setFormatter(formatter)
logger.addHandler(stream_handler)
return logger
logger = setup_logger()
Usage
Import and use the logger in your modules:
from common.logger import logger
logger.info("This is an informational message.")
logger.error("This is an error message.")
Contributing
Contributions are welcome! Please follow these steps:
-
Fork the Repository
-
Create a Feature Branch
git checkout -b feature/YourFeature -
Commit Your Changes
git commit -m "Add some feature" -
Push to the Branch
git push origin feature/YourFeature -
Open a Pull Request
Describe your changes and submit a pull request for review.
License
This project is licensed under the MIT License.
Disclaimer: Ensure that sensitive information such as API keys is securely managed and not exposed in public repositories.