OpenAI Assistant API integrated with Elasticsearch, Logstash & Kibana
Welcome to the assistant-api
package, which includes the AssistantsAPI backend designed to streamline inquiries through a chat widget. This application combines the capabilities of OpenAI’s Assistants with Elasticsearch (for data storage), Logstash (for real-time data processing and transformation), and Kibana (for data visualization) to create an intuitive and efficient system for conversational storage, management, and visualization.
This infrastructure allows users to connect to an Assistant on OpenAI, interact with it, and store conversational data in an Elasticsearch index for future reference and analysis. Logstash processes and enriches the incoming data before storing it, ensuring that the data is clean, structured, and ready for advanced analysis. With Kibana, users can visualize and explore the stored data. This combination ensures comprehensive observability and data-driven insights across all aspects of the application.
Clone this repository:
git clone https://github.com/williamzebrowskI/assistant-api.git
cd assistant-api
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
ELASTIC_VERSION=8.15.0
ES_URL=https://localhost
ES_PORT=9200
ES_INDEX=ai-index
ES_USERNAME=elastic
ELASTIC_PASSWORD='changeme'
KIBANA_SYSTEM_PASSWORD='changeme'
LOGSTASH_INTERNAL_PASSWORD='changeme'
CORS_ALLOWED_ORIGINS="http://127.0.0.1:8002"
Ensure you replace the placeholder values with your actual credentials.
Start the Docker Containers:
docker-compose up --build
Accessing the Chat Interface: Once the server is up and running, a link is provided in the logs directed to the chat widget, http://localhost:8001.
Accessing Kibana: Once Kibana is fully up and running, you can access it by navigating to http://localhost:5601 in your web browser. Kibana provides a powerful UI for visualizing and managing data in Elasticsearch. You can create dashboards, explore the data stored in your Elasticsearch indices, and set up alerts.
To get into Kibana, the username
and password
will be printed after. ie:
username: elastic
password: <generated password>
Clone the Repository
git clone https://github.com/williamzebrowskI/assistant-api.git
cd assistant-api
Create and Activate a Virtual Environment
python -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
Install Dependencies
pip install poetry
poetry install
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
ELASTIC_VERSION=8.15.0
ES_URL=https://localhost
ES_PORT=9200
ES_INDEX=ai-index
ES_USERNAME=elastic
CORS_ALLOWED_ORIGINS="http://127.0.0.1:8002"
ELASTIC_PASSWORD='changeme'
KIBANA_SYSTEM_PASSWORD='changeme'
LOGSTASH_INTERNAL_PASSWORD='changeme'
Run the Application
poetry run gunicorn --config app/ws/gunicorn_config.py --worker-class eventlet -w 1 app.main:app_instance -b 0.0.0.0:8002
To make it easier for users to install and run the application, you can pull and run the Docker image directly from Docker Hub.
Run the Docker Container:
docker run -d -p 8001:8001 -p 8002:8002 --name assistant-api --restart always \
-e FE_PORT=8001 \
-e ES_HOST=elasticsearch \
-e ES_PORT=9200 \
-e ES_INDEX=ai-index \
-e ES_USERNAME=elastic \
-e ES_PASSWORD_FILE=/app/es_config/es_output.txt \
-e OPENAI_API_KEY=your_openai_api_key_here \
-e ASSISTANT_ID=your_assistants_id_here \
-v shared_data:/app/es_config \
wzebrowski/assistant-api:<version number>
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.
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.
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.
To utilize the OpenAIAssistant
, ensure you have the following configured:
.env
file. This key is required to authenticate your requests to the OpenAI API.gpt-3.5-turbo
) and settings appropriate for your application's conversational needs.initialize_conversation()
generate_response(user_query)
manage_conversation_state()
Integrating the OpenAIAssistant
class into our application brings several key benefits:
OpenAIAssistant
allows for easy updates and modifications, such as changing the GPT model or adjusting conversation parameters, without extensive code overhauls.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.
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.
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.These variables are critical for establishing a connection to Elasticsearch. The connector will log warnings if any of these are unset.
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.
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.thread_id
(str): Unique identifier of the thread associated with the conversation.assistant_id
(str): ID of the assistant involved in the conversation.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.
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.
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.
Thread Creation: Initiates a new conversation thread for first-time queries or appends messages to existing threads using the thread_id
.
Message Handling: Manages the exchange of messages between the user and the OpenAI Assistant, maintaining the context and continuity of the conversation.
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.
python app.py
to start the FastAPI server.Includes a .env
file setup for securely managing API keys and Elasticsearch connection details, crucial for operational integrity and data security.
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.