Skip to main content

A Backend application leverging OpenAI AssistantAPI

Project description

Assistant-Api

The assistant-api repository hosts the Assistants API designed to streamline inquiries through a ChatGPT-powered conversational interface. It combines the prowess of OpenAI's GPT model with Elasticsearch's data indexing for an intuitive and efficient user experience. This infrastructure allows users to connect to an assistant on OpenAI, interact with it, and store conversations in Elasticsearch for future reference and analysis.

Table of Contents

The following Diagram depicts the flow of a user's message from end to end. Assistant API Diagram

Getting Started

Prerequisites

  • Python 3.11
  • pip
  • Poetry

Setting Up a Virtual Environment

  1. Clone this repository:

    git clone https://github.com/williamzebrowskI/assistant-api.git
    cd assistant-api
    
  2. Create a .env File: Create a .env file in the root of your project and fill it with your OpenAI and Elasticsearch credentials:

    OPENAI_API_KEY=your_openai_api_key_here
    ASSISTANT_ID=your_assistants_id_here
    
    # Elasticsearch cloud authentication credentials
    ES_URL=your_elasticsearch_url_here
    ES_PORT=your_elasticsearch_port_here
    ES_INDEX=your_elasticsearch_index_name_here
    ES_API_KEY=your_elasticsearch_api_key_here
    
    CORS_ALLOWED_ORIGINS="http://127.0.0.1:8002"
    

    Ensure you replace the placeholder values with your actual credentials.

Configuration

The application can work with or without Elasticsearch. By default, Elasticsearch is disabled to avoid potential costs associated with using an Elasticsearch index. You can enable or disable Elasticsearch by setting the ELASTICSEARCH_ENABLED environment variable in your .env file:

ELASTICSEARCH_ENABLED=false # Set to true to enable Elasticsearch

When ELASTICSEARCH_ENABLED is set to false, the application will not attempt to connect to Elasticsearch, and conversation data will not be stored in an Elasticsearch index. This is useful for development and testing purposes or if you do not want to incur costs for Elasticsearch usage.

Startup

Running with Docker

  1. Build the Docker Image: Use the --no-cache option to ensure a fresh build:

    docker build --no-cache -t openai-assistant .
    
  2. Run the Docker Container:

    docker run -p 8002:8002 openai-assistant
    
  3. Accessing the Chat Interface: Once the server is up and running, open the index.html file in a web browser to see the chat interface. This file should be located in your project directory. If you're using an IDE that supports live previews, you can also use that feature to open the file.

Running without Docker

  1. Clone the Repository

    git clone https://github.com/williamzebrowskI/assistant-api.git
    cd assistant-api
    
  2. Create and Activate a Virtual Environment

    python -m venv venv
    source venv/bin/activate  # On Windows use `venv\Scripts\activate`
    
  3. Install Dependencies

    pip install poetry
    poetry install
    
  4. Set Up Environment Variables Create a .env file in the root of your project and fill it with your OpenAI and Elasticsearch credentials:

    OPENAI_API_KEY=your_openai_api_key_here
    ASSISTANT_ID=your_assistants_id_here
    # Elasticsearch cloud authentication credentials
    ES_URL=your_elasticsearch_url_here
    ES_PORT=your_elasticsearch_port_here
    ES_INDEX=your_elasticsearch_index_name_here
    ES_API_KEY=your_elasticsearch_api_key_here
    CORS_ALLOWED_ORIGINS="http://127.0.0.1:8002"
    ELASTICSEARCH_ENABLED=false  # Set to true to enable Elasticsearch
    
  5. Run the Application

    poetry run gunicorn --config ws/gunicorn_config.py --worker-class eventlet -w 1 app.main:app_instance -b 0.0.0.0:8002
    
  6. Access the Chat Interface Open the index.html file in a web browser to see the chat interface. This file should be located in your project directory. If you're using an IDE that supports live previews, you can also use that feature to open the file.

Running the Application

  1. Build the Docker Image: Use the --no-cache option to ensure a fresh build:

    docker build --no-cache -t openai-assistant .
    
  2. Run the Docker Container:

    docker run -p 8002:8002 openai-assistant
    
  3. Accessing the Chat Interface: Once the server is up and running, open the index.html file in a web browser to see the chat interface. This file should be located in your project directory. If you're using an IDE that supports live previews, you can also use that feature to open the file.

    Interact with the chat interface to send queries to the OpenAI Assistant and receive responses.

Assistant API Overview

The Assistant API is designed to facilitate interactions with users seeking guidance or information via a ChatGPT-powered conversational interface. This assistant leverages the OpenAI API for generating responses and Elasticsearch for logging and retrieving conversation histories.

Key Components

FastAPI Setup

  • FastAPI Application: Initiates a FastAPI server, enabling HTTP requests handling for the chat interface.
  • CORS Middleware: Configures Cross-Origin Resource Sharing (CORS) settings to allow web requests from different origins, ensuring the frontend can communicate with the backend.

OpenAI Assistant Integration

OpenAIAssistant Class Overview

The OpenAIAssistant class is designed to seamlessly integrate OpenAI's GPT models into our application, enabling the generation of dynamic, intelligent responses to user queries. This integration is pivotal for facilitating an engaging conversational experience in the Assistant API application.

Purpose

The primary purpose of the OpenAIAssistant class is to abstract the complexities involved in communicating with the OpenAI API. It manages the lifecycle of conversations, including initiating new threads, managing ongoing conversations, and generating responses based on user input.

Setup

To utilize the OpenAIAssistant, ensure you have the following configured:

  • API Key: Securely store the OpenAI API key in your environment variables or .env file. This key is required to authenticate your requests to the OpenAI API.
  • Assistant Configuration: Define the specific GPT model (e.g., gpt-3.5-turbo) and settings appropriate for your application's conversational needs.
Key Functionalities
initialize_conversation()
  • Initializes a new conversation thread with OpenAI, setting up the context and parameters for the conversation.
  • Stores conversation state to facilitate seamless continuation of the conversation.
generate_response(user_query)
  • Sends the user's query to OpenAI and retrieves a response based on the current conversation context.
  • Utilizes advanced natural language processing and generation techniques to ensure the response is relevant, accurate, and coherent.
manage_conversation_state()
  • Dynamically manages the conversation's state, including tracking the conversation history and context changes.
  • Ensures that each response is contextually appropriate, maintaining a natural and logical flow to the conversation.

Integration Benefits

Integrating the OpenAIAssistant class into our application brings several key benefits:

  • Enhanced User Experience: By leveraging OpenAI's advanced NLP capabilities, the assistant can provide users with highly relevant, informative, and engaging responses.
  • Scalability: The modular design of the OpenAIAssistant allows for easy updates and modifications, such as changing the GPT model or adjusting conversation parameters, without extensive code overhauls.
  • Simplicity: The abstraction provided by the class simplifies the process of integrating complex AI functionalities into the application, making the development process more efficient and less error-prone.
Conclusion

The OpenAIAssistant class represents a core component of our Assistant API application, bridging the gap between user queries and the sophisticated language understanding and generation capabilities of OpenAI's GPT models. Through this integration, we aim to deliver an exceptional conversational experience that aids users in navigating their inquiries.

Elasticsearch Connector

Elastic Data Model Integration

This documentation outlines the setup and usage of the Elastic Data Model within our AssistantAPI application. Our model leverages Elasticsearch for operations such as document creation, updates, and management within an Elasticsearch index. The integration is facilitated through the ElasticConnector class, which establishes a connection to Elasticsearch using environmental variables and provides asynchronous methods for interacting with the data.

Setup

Environment Variables

Before utilizing the ElasticConnector, ensure the following environmental variables are set in your environment or within a .env file:

  • ES_URL: The URL of your Elasticsearch instance.
  • ES_INDEX: The Elasticsearch index to which documents will be pushed and from which they will be retrieved.
  • ES_PORT: The port on which your Elasticsearch instance is running.
  • ES_API_KEY: The API key for authenticating with your Elasticsearch instance.

These variables are critical for establishing a connection to Elasticsearch. The connector will log warnings if any of these are unset.

ElasticConnector Class

Initialization

Upon instantiation, the ElasticConnector class initializes a connection to an Elasticsearch instance using the aforementioned environmental variables. This connection is essential for performing asynchronous operations against the Elasticsearch index.

Methods

async push_to_index(conversation_uuid, user_id, client_ip, thread_id, assistant_id)

This asynchronous method creates a new conversation document in Elasticsearch. The document includes metadata such as the user's ID, the client's IP address, the thread's ID, the assistant's ID, and a timestamp marking the creation time. This setup initializes an empty list for conversations to hold future interactions.

Parameters:

  • conversation_uuid (str): Unique identifier for the conversation document.
  • user_id (str): ID of the user initiating the conversation.
  • client_ip (str): IP address of the client.
  • thread_id (str): Unique identifier of the thread associated with the conversation.
  • assistant_id (str): ID of the assistant involved in the conversation.

async update_document(conversation_uuid, user_query, assistant_response)

This method appends a new interaction to the conversations array of an existing document. Each interaction consists of a user query and the corresponding assistant response, allowing for the historical tracking of interactions within a conversation.

Parameters:

  • conversation_uuid (str): Unique identifier for the conversation document.
  • user_query (str): The query submitted by the user.
  • assistant_response (str): The response generated by the assistant.

Both methods handle exceptions gracefully by logging errors, ensuring the application's stability in the face of Elasticsearch operation failures.

Conclusion

The ElasticConnector class provides a streamlined approach to integrating Elasticsearch into your application for handling conversation data. By following the setup instructions and utilizing the provided methods, you can efficiently manage conversation documents within your chosen Elasticsearch index.

Conversational Flow

  1. Client-Side Identification: When the user starts a conversation, a conversation_uuid and user_id are generated to uniquely identify the session and user across interactions.

  2. Thread Creation: Initiates a new conversation thread for first-time queries or appends messages to existing threads using the thread_id.

  3. Message Handling: Manages the exchange of messages between the user and the OpenAI Assistant, maintaining the context and continuity of the conversation.

  4. Elasticsearch Logging: Archives each conversation in Elasticsearch, tagging them with conversation_uuid and user_id to construct a detailed history for analysis and continued dialogue.

Running the Assistant

  • Execute python app.py to start the FastAPI server.
  • Access the chat interface through the provided HTML frontend to interact with the assistant.

Environment Configuration

Includes a .env file setup for securely managing API keys and Elasticsearch connection details, crucial for operational integrity and data security.

Logging and Debugging

Utilizes Python's logging module to track application events, aiding in monitoring and troubleshooting.


This assistant exemplifies a modern approach to building conversational AI applications, emphasizing modularity, scalability, and ease of integration with advanced NLP models and database technologies.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openai_assistants_api-1.0.0.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

openai_assistants_api-1.0.0-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file openai_assistants_api-1.0.0.tar.gz.

File metadata

  • Download URL: openai_assistants_api-1.0.0.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.11.7 Darwin/23.6.0

File hashes

Hashes for openai_assistants_api-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7de44a0272b395b01527f36dc652a195010d35e1ca5afb22f828643a0b0cf7c2
MD5 2d1a046410f549adf9dd8285122430cf
BLAKE2b-256 656f356fada7d630c3115daa3fd87df62b53cb10211bcfb7526a9743128cf834

See more details on using hashes here.

File details

Details for the file openai_assistants_api-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openai_assistants_api-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 91893108111d31f0fb3e13c0ef21271e340f5c68f2021243669c6fa961415150
MD5 a933023031bd34bcccfdc49be97c0021
BLAKE2b-256 21a803bea79edf04c1d75efa46f50c2505a69e58d50418818a919c7fbfd5e3cc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page