• unwind ai
  • Posts
  • Build Autonomous RAG App using GPT-4o and Vector Database

Build Autonomous RAG App using GPT-4o and Vector Database

Fully-functional RAG App in less than 100 lines of Python Code (step-by-step instructions)

An autonomous RAG assistant that can answer questions by combining web searches and a custom knowledge base. Sounds fancy, right? What if I told you that you could build this in less than 100 lines of Python code?

In this tutorial, we’ll walk through how to build an Autonomous Retrieval-Augmented Generation (AutoRAG) app using GPT-4o, Streamlit, and a vector database. This AI will automatically search knowledge bases, read chat history, or search the web to retrieve the information it needs to answer user questions, making it a highly intelligent assistant.

What We’re Building

AutoRAG is an advanced AI system that combines document understanding, vector search, and language models to provide enhanced question-answering experiences. It automatically retrieves relevant information from a knowledge base and generates accurate responses based on the retrieved context.

This AI app will automatically retrieve and generate answers based on information from a local knowledge base and the web. The app will:

  • Use a vector database to store and retrieve documents

  • Dynamically add PDFs to the knowledge base

  • Incorporate web search when local info isn’t enough

  • Provide a Streamlit-powered user interface for easy interaction

Prerequisites

Before we begin, make sure you have:

  1. Python installed on your machine (version 3.7 or higher is recommended)

  2. Basic familiarity with Python programming

  3. Your OpenAI API Key (or an alternative LLM provider’s API key)

  4. PostgreSQL with the pgvector extension

  5. A code editor of your choice (we recommend VSCode or Cursor for their excellent Python support)

Step-by-Step Instructions

Step 1: Setting Up the Environment

First, let's get our development environment ready:

  1. Clone the GitHub repository:

git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git
  1. Go to the Autonomous RAG folder

cd autonomous_rag
pip install -r requirements.txt
  1. Ensure PgVector Database is running

    The app expects PgVector to be running on localhost:5532. Adjust the configuration in the code if your setup is different. Install Docker Desktop if you don’t have it already.

docker run -d \
  -e POSTGRES_DB=ai \
  -e POSTGRES_USER=ai \
  -e POSTGRES_PASSWORD=ai \
  -e PGDATA=/var/lib/postgresql/data/pgdata \
  -v pgvolume:/var/lib/postgresql/data \
  -p 5532:5432 \
  --name pgvector \
  phidata/pgvector:16
  1. Get Your OpenAI API Key:


    Sign up for an OpenAI account and generate an API key.

Step 2: Creating the Streamlit App

Now that the environment is set, let’s create our Streamlit based RAG app. Create a new file named autorag.py and add the following code:

  1. Import Required Libraries: At the top of your file, add:

import streamlit as st
import nest_asyncio
from io import BytesIO
from phi.assistant import Assistant
from phi.document.reader.pdf import PDFReader
from phi.llm.openai import OpenAIChat
from phi.knowledge import AssistantKnowledge
from phi.tools.duckduckgo import DuckDuckGo
from phi.embedder.openai import OpenAIEmbedder
from phi.vectordb.pgvector import PgVector2
from phi.storage.assistant.postgres import PgAssistantStorage

nest_asyncio.apply()
  1. Set Up the Database Connection: Define your PostgreSQL database URL. Make sure PostgreSQL is running with the pgvector extension installed.:

DB_URL = "postgresql+psycopg://ai:ai@localhost:5532/ai"
  1. Set Up the Assistant: We will use a cached function in Streamlit to initialize the assistant:

@st.cache_resource
def setup_assistant(api_key: str) -> Assistant:
    llm = OpenAIChat(model="gpt-4o-mini", api_key=api_key)
    return Assistant(
        name="auto_rag_assistant",
        llm=llm,
        storage=PgAssistantStorage(table_name="auto_rag_storage", db_url=DB_URL),
        knowledge_base=AssistantKnowledge(
            vector_db=PgVector2(
                db_url=DB_URL,
                collection="auto_rag_docs",
                embedder=OpenAIEmbedder(model="text-embedding-ada-002", dimensions=1536, api_key=api_key),
            ),
            num_documents=3,
        ),
        tools=[DuckDuckGo()],
        instructions=[
            "Search your knowledge base first.",
            "If not found, search the internet.",
            "Provide clear and concise answers.",
        ],
        show_tool_calls=True,
        search_knowledge=True,
        read_chat_history=True,
        markdown=True,
        debug_mode=True,
    )

What’s happening in the above code?

  • OpenAIChat: GPT-4o-mini is used to generate responses.

  • PgVector2: The assistant connects to a vector database using PostgreSQL with pgvector.

  • DuckDuckGo: Used for web searches when the information is not found in the knowledge base.

  • PgAssistantStorage: Stores chat history and the assistant’s memory.

  1. Implementing Document Upload: To make our assistant more powerful, we can upload PDF documents to the knowledge base. Add the following function to handle document uploads:

def add_document(assistant: Assistant, file: BytesIO):
    reader = PDFReader()
    docs = reader.read(file)
    if docs:
        assistant.knowledge_base.load_documents(docs, upsert=True)
        st.success("Document added to the knowledge base.")
    else:
        st.error("Failed to read the document.")

This function reads the uploaded PDF file, processes it, and adds its content to the assistant’s knowledge base.

  1. Querying the Assistant: Now, let’s create a function that sends a query to the assistant and retrieves an answer:

def query_assistant(assistant: Assistant, question: str) -> str:
    return "".join([delta for delta in assistant.run(question)])

This function takes a question from the user and generates a response using the assistant’s knowledge base and web search capabilities.

  1. Building the Streamlit Interface: Now, we’ll create the front-end of the app where users can interact with the assistant. This is the main structure of the Streamlit app:

def main():
    st.set_page_config(page_title="AutoRAG", layout="wide")
    st.title("🤖 Auto-RAG: Autonomous RAG with GPT-4o")

    api_key = st.sidebar.text_input("Enter your OpenAI API Key 🔑", type="password")
    
    if not api_key:
        st.sidebar.warning("Enter your OpenAI API Key to proceed.")
        st.stop()

    assistant = setup_assistant(api_key)
    
    uploaded_file = st.sidebar.file_uploader("📄 Upload PDF", type=["pdf"])
    
    if uploaded_file and st.sidebar.button("🛠️ Add to Knowledge Base"):
        add_document(assistant, BytesIO(uploaded_file.read()))

    question = st.text_input("💬 Ask Your Question:")
    
    if st.button("🔍 Get Answer"):
        if question.strip():
            with st.spinner("🤔 Thinking..."):
                answer = query_assistant(assistant, question)
                st.write("📝 **Response:**", answer)
        else:
            st.error("Please enter a question.")

Step 3: Running the App

With our code in place, it’s time to run your AutoRAG application.

  1. Start the Streamlit App In your terminal, navigate to the project folder, and run the following command:

streamlit run autorag.py
  1. Access Your AI Assistant: Open the provided URL (usually http://localhost:8501) in your web browser to start interacting with the app.

Working Application Demo

Conclusion

Congratulations! You’ve just built an AutoRAG system that integrates GPT-4o, a vector database, and web search capabilities. This app dynamically retrieves documents, processes user questions, and provides answers by leveraging both the knowledge base and web search.

By combining powerful tools like PostgreSQL, vector search, and language models, you’ve created a robust system for question-answering and information retrieval. Feel free to expand on this foundation by adding more document types or integrating additional APIs!

If you enjoyed this tutorial, make sure to subscribe to Unwind AI for more tutorials like this. And don’t forget to share this guide with your friends!

Reply

or to participate.