Skip to content

CHATBOT Workflow

Prerequisites

  1. Python 3.9+: Download Python
  2. VS Code: A code editor for development.
  3. Key Components
  4. Key Features

Chatbot Application Documentation

Project Description

The application leverages OpenAI and LLMs like GPT-4 to provide intelligent responses to user inputs. It's designed to automate processes with meaningful interactions. The billing and token limitations of OpenAI are notable challenges. The application can process documents up to 50 pages, but larger documents can be handled by vector databases in future projects.

This document outlines the architecture, key features, and functionalities of our company's chatbot application. The chatbot leverages a combination of advanced technologies including LangChain, OpenAI (GPT-4), Pinecone, and FastAPI, integrating these tools to provide a seamless and intelligent user experience.

Project Setup

  1. Clone the Repository:

    powershellCopy code
    git clone https://github.com/Evans-musamia/openai_chatbot_cms_.git
    
  2. Navigate to Project Directory:

    powershellCopy code
    cd openai_chatbot_cms_
    

Key Components

1. LangChain

  • Purpose: Framework for data loading, chunking, embeddings creation, and prompt-based conversational agents.
  • Functionality: Interfaces with OpenAI for large language model access and prompt generation.

2. OpenAI (GPT-4)

  • Purpose: Access to advanced AI language models for generating responses and processing queries.
  • Functionality: Initialized with an API key for language model interactions.

3. Pinecone

  • Purpose: Vector search service for storing and retrieving query-related embeddings.
  • Functionality: Creates Pinecone objects and manages data indexing and retrieval.

4. FastAPI

  • Purpose: Web framework for creating REST APIs and facilitating backend-frontend communication.
  • Functionality: Uses WebSocket for real-time, bidirectional communication.

WORKFLOW ARCHITECTURAL DIAGRAM

Untitled

Lets break the daigram and explain it,

  1. Definition of Components:

  2. Each RECTANGLE line defines a component in the architecture. RECTANGLE is a custom-defined style for the elements in the diagram to make them visually distinct. The text within quotes after each as keyword gives a name and a brief description of each component. For example, RECTANGLE UserInterface as "User Interface\n(User Query Input)" defines a rectangle representing the User Interface where users input their queries.

2.Components Described:

  • UserInterface: Represents the user interface where queries are input.
  • FastAPIWebSocket: The FastAPI & WebSocket component that receives the query from the user interface.
  • LangChain: The processing step where the query is processed using LangChain.
  • Embeddings: The stage where embeddings are created from the processed query.
  • Pinecone: The Pinecone similarity search component that uses embeddings for retrieving relevant information.
  • OpenAI: The OpenAI GPT-4 component that generates a response based on the query and retrieved data.
  • ResponseDelivery: The final stage where the response is delivered back to the user from our backened.
  • Flow of Data:
    • Arrows (->) represent the flow of data or control between components. Each arrow is labeled with a description of the action. For example, UserInterface --> FastAPIWebSocket : Sends Query shows that the User Interface sends the query to the FastAPI & WebSocket component.
  • Overall Workflow:
    • The code sequentially links all the components from the UserInterface to ResponseDelivery and then back to the UserInterface. This flow illustrates the complete cycle of receiving a user query, processing it through various stages (including LangChain, Pinecone, and OpenAI GPT-4), and returning a response to the user.

Data Retrieval and Processing

  • Embeddings Creation: Generates embeddings from user queries for relevant information retrieval.
  • Similarity Search: Pinecone executes similarity searches using these embeddings.
  • Response Generation: Combines retrieved data with GPT-4 responses for user queries.

Understanding the Workflow

  • User Interaction: Via a UI, users send queries to the chatbot.
  • Backend Processing: Queries are processed, and embeddings are generated.
  • Data Retrieval: Pinecone retrieves information based on embeddings.
  • Response Generation: LangChain and GPT-4 provide coherent responses.
  • Real-Time Communication: FastAPI WebSocket ensures smooth interaction

Key Features

WebSocket Implementation

  • Setup: Integrated within the FastAPI framework, WebSocket facilitates real-time communication.
  • Operation: Upon a user connection request, the WebSocket endpoint (/ws) continuously receives and processes messages.
  • Data Processing: Incoming text data is processed using LangChain (qa.run(data)) and the response is sent back through the WebSocket.

Data Retrieval and Processing

  • Embeddings Creation: When a user submits a query, embeddings are created using the processed data.
  • Similarity Search: Pinecone performs a similarity search with these embeddings to find the most relevant information.
  • Response Generation: The retrieved data is then combined with OpenAI's GPT-4 model to generate an informative and contextually relevant response.

REST API and Query Handling

  • Endpoint Configuration: The /query/ endpoint receives user queries via POST requests.
  • JSON Payload: It expects a payload with a "query" field.
  • LangChain Processing: The query is processed using LangChain to generate a response.

Startup and Configuration

  • Initialization: On startup, Pinecone and OpenAI instances are initialized.
  • Template Configuration: Templates for input structuring are defined using PromptTemplate class.
  • Main Application Block: The application runs on a Uvicorn server, configured to host "0.0.0.0" and port 8111.

Understanding the Workflow

  • User Interaction: Users interact with the chatbot via a user interface, sending queries.
  • Backend Processing: Queries are sent to the backend, where they are processed, and embeddings are created.
  • Data Retrieval: Pinecone retrieves relevant information based on these embeddings.
  • Response Generation: The combined power of LangChain and GPT-4 generates coherent and context-aware responses.
  • Real-Time Communication: FastAPI WebSocket ensures that this interaction is smooth and real-time.

Conclusion

This documentation provides an overview of the key components and functionalities of our chatbot application. It is designed to help new team members understand the intricate workings of our system, highlighting the integration of LangChain, OpenAI, Pinecone, and FastAPI in creating a sophisticated and responsive chatbot solution.