Silmaril Microservicio IA
Find a file
Enrique 882a34fc7f Merge branch 'mirror-context-error' into 'main'
Added Mirror Context Error

See merge request silmaril/ai_microservice!1
2024-11-14 22:45:23 +00:00
assistant Refactor Assistant WS name 2024-10-15 19:37:01 +02:00
common Creation of ai_microservice microservice: Integrated Assistant, Extractor and Mirror. 2024-10-07 21:31:25 +02:00
loop Refactor Assistant WS name 2024-10-15 19:37:01 +02:00
mirror Added Mirror Context Error 2024-10-22 16:08:39 +05:00
.env.example Creation of ai_microservice microservice: Integrated Assistant, Extractor and Mirror. 2024-10-07 21:31:25 +02:00
.gitignore Added Mirror Context Error 2024-10-22 16:08:39 +05:00
__init__.py Websockets Celery 2024-10-08 21:36:04 +02:00
celery_config.py Refactor Assistant WS name 2024-10-15 19:37:01 +02:00
main.py Add Sentry 2024-10-15 22:53:24 +02:00
Procfile Celery 4 threads 2024-10-15 22:28:21 +02:00
README.md Refactor Assistant WS name 2024-10-15 19:37:01 +02:00
requirements.txt Add Sentry 2024-10-15 22:53:24 +02:00

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

The Unified FastAPI Microservices Project combines three microservices into a single FastAPI application:

  1. Assistant: Manages assistant-related operations, including creation, updating, deletion, and message handling.
  2. Loop: Handles knowledge extraction, context augmentation, and interaction with GPT models for extracting information.
  3. 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

  1. Clone the Repository

    git clone https://github.com/yourusername/unified_fastapi_project.git
    cd unified_fastapi_project
    
  2. 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`
    
  3. Upgrade pip

    Ensure that pip is up-to-date.

    pip install --upgrade pip
    
  4. 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.0 makes the server accessible externally.
  • --port: Defines the port number. Default is 8000.
  • --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:

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:

  1. Start the Application

    uvicorn main:app --host 0.0.0.0 --port 8000 --reload
    
  2. Access Healthcheck Endpoints

    Use your browser or tools like curl or Postman to access the healthcheck endpoints as described above.

  3. 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:

  1. Fork the Repository

  2. Create a Feature Branch

    git checkout -b feature/YourFeature
    
  3. Commit Your Changes

    git commit -m "Add some feature"
    
  4. Push to the Branch

    git push origin feature/YourFeature
    
  5. 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.