FRONTEND FOR LLM APPS
This project integrates Large Language Models with a Streamlit frontend, offering users an interactive platform for document-based inquiries and leveraging AI for dynamic Q&A sessions.
Project Introduction
Large Language Models (LLMs) like GPT excel in handling known data, but challenges arise when the questions pertain to newer or private material. This initiative addresses that by enabling the direct input of substantial volumes of data, including recent publications and confidential files, into the AI, ensuring the model's prowess extends to freshly acquired or exclusive information.
Project Structure
The project consists of two primary components, each leveraging the synergy between cutting-edge language models and user-friendly web interfaces.
​
​Together, these components form a seamless pipeline from document upload to insightful answers, encapsulating the power of AI in a user-centric application.
LLM Powered Q&A Application
Built with Python and LangChain, this backend component integrates with OpenAI's powerful language models. The application processes documents, creates searchable embeddings, and sets up a retrieval-based Q&A system. Core functionalities include document loading, text chunking, embedding generation, and question-answering logic.
​
Environment Configuration with Dotenv
The code enables secure and flexible configuration, setting up environment variables for the project. It utilizes the python-dotenv library to load settings from a .env file, ensuring sensitive information like API keys remains separate from the source code. This is a standard practice for maintaining privacy and adaptability in various development and production environments.
​
​
​
Document Import and Retrieval Functionality
The presented functions showcase the application's sophisticated approach to processing textual data. The first function, load_document, is adept at ingesting documents in various formats, specifically PDFs and Word documents, preparing them for in-depth analysis by the AI.
​
​
​
​
​
​
​
​
​
​
​
​
The second function, load_from_wikipedia, exemplifies the system's ability to fetch and integrate content from Wikipedia based on user queries.
​
​
​
​
Together, they form a robust framework for enriching the AI's dataset, crucial for delivering accurate and context-rich insights in the Q&A application.
​
Data Segmentation for AI Processing
This function, chunk_data, is essential for preparing large text data for AI processing. It employs the RecursiveCharacterTextSplitter to divide text into manageable segments, or 'chunks', of a specified size.
​
​
By doing so, it ensures that sizable documents are broken down into smaller parts that the AI model can comfortably analyze, enhancing the efficiency and scalability of the Q&A system. This method is particularly beneficial when dealing with extensive texts, allowing for a more detailed and nuanced understanding by the underlying AI.
​
Tokenization and Cost Analysis for AI Embeddings
This function, print_embedding_cost, provides a clear picture of the computational expense involved in processing text through AI models. Utilizing the tiktoken library, it calculates the number of tokens generated when encoding the text data, a fundamental step in preparing data for machine learning models.
​
It then computes and displays the cost of embeddings, translating the token count into a monetary value based on a given pricing model. This tool is invaluable for budgeting and managing resources in large-scale AI applications.
​
​
​
​
​
Vector Database Management for AI Applications
These functions manage the storage and retrieval of text embeddings within a vector database using Pinecone, a service optimized for vector search. insert_or_fetch_embeddings checks for an existing index and either fetches the stored embeddings or creates a new index and uploads the embeddings. This ensures efficient retrieval for AI-driven query processing.
​
​
​
​
​
​
​
​
​
​
​
The delete_pinecone_index function facilitates clean-up operations, allowing for the removal of an index or all indexes, helping maintain the database's organization and cost-effectiveness. These operations are critical for maintaining the performance and accuracy of the Q&A system at scale.
​
​
​
​
​
​
​
​
​
Intelligent Query Resolution with Contextual Memory
The ask_and_get_answer function demonstrates the application's ability to deliver precise answers by querying a vector database. It employs a retrieval-based QA chain that utilizes both a language model and vector similarity search to find the most relevant responses.
​
​
​
​
​
​
​
​
​
​
​
Expanding on this, ask_with_memory incorporates conversational context, allowing the system to consider previous interactions. This function utilizes a Conversational Retrieval Chain to maintain a chat history, enhancing the relevance of the AI's answers over the course of a session. These capabilities ensure that the application not only provides accurate information but also builds on the continuity of the conversation for more nuanced dialogue.
​
​
​
​
​
​
​
​
Demonstrating AI-Powered Document Analysis in Action
This script is a live demonstration of the AI's capability to process and understand complex documents. It begins by loading a specified document and provides a summary of its content, such as the number of pages and characters on a specific page. The document is then segmented into chunks, the cost of processing is calculated, and any pre-existing data indexes are cleared.
​
​
​
​
Subsequently, a new vector store is created or fetched, and an initial query about the document's overall content is posed. The script then enters an interactive loop, inviting users to ask further questions, showcasing the AI's responsiveness and depth of understanding, culminating in a user-friendly Q&A session.
​
Streamlit Frontend
The frontend is crafted using Streamlit, allowing for rapid development of interactive web interfaces in Python. Users can upload documents, configure app settings, and interact with the Q&A system through a sleek, intuitive interface. It features dynamic components like file uploaders, sliders, and text input fields, all tailored to enhance user experience.
​
Interactive AI-Powered Q&A Streamlit Interface
This section of the code establishes a Streamlit web application that serves as the front end for an AI-powered Q&A system.
-
Importing Libraries and Modules
At the beginning, necessary libraries and modules are imported. These include, streamlit, a framework for building interactive web apps. langchain, which is utilized for operations involving language models and text data.​ os, a module providing interfaces to interact with the operating system. openai, which facilitates interaction with OpenAI's API.
​
​
​
​
​
-
Loading Documents
The function load_document accepts a file path and determines the file type by its extension (e.g., .pdf, .docx, .txt). The appropriate method is then employed to load the content of the file into the application.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
-
Chunking Data
The chunk_data function is designed to divide large textual data into smaller segments or "chunks." This process facilitates easier handling and analysis of extensive documents.
​
​
​
​
​
-
Creating Embeddings
The function create_embeddings transforms the chunks of text into embeddings, a format interpretable and usable by machine learning models.
​
​
​
​
​
-
Asking Questions and Getting Answers
The ask_and_get_answer function enables posing queries about the processed text data. It identifies relevant text sections and engages a language model to formulate responses.
​
​
​
​
​
​
​
​
​
-
Calculating Embedding Costs
The purpose of the calculate_embedding_cost function is to ascertain the expenses associated with generating embeddings. Monitoring such costs is crucial, especially when using APIs that implement a usage-based pricing model.
​
​
​
​
​
-
Clearing Chat History
The clear_history function serves to reset the conversation history in the application, providing a fresh start.
​
​
​
​
-
Validating OpenAI API Key
The test_openai_api_key function is responsible for verifying the validity of the provided OpenAI API key, ensuring uninterrupted access to OpenAI's services.
​
​
​
​
​
​
​
​
​
-
Main App Logic with Streamlit
This segment marks the commencement of the Streamlit application. It orchestrates the user interface and incorporates the previously defined functions for file loading, data processing, and user interaction.
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
This comprehensive code snippet outlines the Streamlit frontend for an interactive Q&A application driven by a large language model. It handles user input through a sidebar where API credentials are securely entered and documents for analysis are uploaded. The app processes the uploaded documents, segmenting them into chunks and calculating the cost associated with embedding them for AI analysis.
​
-
Initial Setup and API Key Access
The application starts by displaying an image and a subheader, indicating the purpose of the app ("LLM Question-Answering Application").
-
Sidebar for User Input​​
A sidebar is created using st.sidebar. This sidebar is designed for user interactions, primarily for inputting the OpenAI API key and uploading documents. Users are prompted to input their OpenAI API key. The validity of this key is checked using the test_openai_api_key function.
-
File Upload and Processing
Users can upload files (PDF, DOCX, or TXT) and set parameters for chunking (chunk size and overlap). When the 'Add Data' button is clicked, the application reads the uploaded file, processes the file by chunking the text and creating embeddings for the chunks, the cost of embeddings is calculated and displayed, the processed data (embeddings) is stored in st.session_state, allowing it to be accessed across reruns of the app.
-
Question-Answering Interface
Users can input questions about the content of the uploaded file. If a question is asked and the API key is valid, the application retrieves the processed data (embeddings) from the session state, uses the ask_and_get_answer function to find relevant content and generate an answer, displays the generated answer to the user, errors during answer generation are caught, and an error message is displayed to the user.
-
Chat History Maintenance
The application maintains a chat history, showing the sequence of questions asked and answers received. The chat history is displayed in a text area, allowing users to review the interaction history.
​
This setup illustrates a full-cycle integration of backend AI processing with a frontend interface, facilitating a seamless user journey from document upload to AI-powered insights.
​
Get a glimpse of what our AI-powered Q&A application has to offer. Click on the image below to dive in and start discovering insights with our interactive tool.