• unwind ai
  • Posts
  • Build Agentic RAG with OpenAI GPT-5

Build Agentic RAG with OpenAI GPT-5

Fully functional agentic RAG app (100% opensource)

OpenAI just dropped GPT-5 yesterday and brings breakthrough reasoning capabilities and unified intelligence that makes building sophisticated AI systems easier than ever. The timing couldn't be better for developers looking to create next-generation applications that can think, reason, and act intelligently.

In this tutorial, we'll show you how to build an Agentic RAG (Retrieval-Augmented Generation) application using the Agno framework with GPT-5 Nano and LanceDB. This creates an intelligent agent that can search through documents, reason about information, and provide expert-level responses while maintaining excellent performance and cost efficiency.

Agno is a Python framework for building multi-agent systems with memory, knowledge, and reasoning capabilities. We're using OpenAI GPT-5 Nano that retains key reasoning capabilities while being optimized for developer tools and rapid interactions. Combined with LanceDB's lightweight vector database, you get a blazing-fast RAG system that doesn't compromise on intelligence.

Don’t forget to share this tutorial on your social channels and tag Unwind AI (X, LinkedIn, Threads, Facebook) to support us!

What We’re Building

This application implements an intelligent agentic RAG system using GPT-5 Nano's ultra-low-latency reasoning capabilities and LanceDB for efficient vector storage. Unlike traditional RAG systems, this agent delivers intelligent responses at lightning speed while maintaining cost efficiency for production use.

Features:

  • GPT-5: Latest OpenAI model for intelligent responses

  • LanceDB: Lightweight vector database for fast similarity search

  • Agentic RAG: Intelligent retrieval augmented generation

  • Markdown Formatting: Beautiful, structured responses

  • Dynamic Knowledge: Add URLs to expand knowledge base

  • Real-time Streaming: Watch answers generate live

  • Clean Interface: Simplified UI without configuration complexity

How The App Works

User Setup:

  1. API Key Input: User provides their OpenAI API key through the sidebar

  2. Knowledge Sources: User can add URLs to expand the knowledge base

  3. Query Input: User enters questions through the text area or clicks suggested prompts

Application Flow:

  1. Knowledge Loading: When the API key is provided, URLs are processed and stored in LanceDB using OpenAI's embeddings for semantic search

  2. Agent Initialization: Agno creates a fast, efficient agent that combines GPT-5 Nano with knowledge search capabilities

  3. Query Processing: When user submits a question, the agent automatically searches the knowledge base for relevant information

  4. Response Generation: GPT-5 Nano processes the retrieved context and generates an intelligent response

  5. Real-time Streaming: Responses stream in real-time, with event filtering to show only relevant content

  6. Dynamic Updates: New knowledge sources can be added without recreating the entire database

Technical Architecture:

  • Agno Framework: Handles agent orchestration, tool calling, and knowledge integration with minimal overhead

  • LanceDB: Provides fast vector similarity search for document retrieval

  • GPT-5 Nano: Delivers intelligent reasoning while maintaining cost efficiency

  • Streamlit: Creates an interactive web interface with caching for optimal performance

Prerequisites

Before we begin, make sure you have the following:

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

  2. Your OpenAI API key

  3. A code editor of your choice (we recommend VS Code or PyCharm for their excellent Python support)

  4. Basic familiarity with Python programming

Code Walkthrough

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 agentic_rag_gpt5 folder:

cd rag_tutorials/agentic_rag_gpt5
pip install -r requirements.txt
  1. Set up your OpenAI API key:

export OPENAI_API_KEY="your-api-key-here"

Or create a .env file:

OPENAI_API_KEY=your-api-key-here

Creating the Streamlit App

Let's create our app. Create a new file agentic_rag_gpt5.py and add the following code:

  1. Import necessary libraries:

import streamlit as st
import os
from agno.agent import Agent
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.url import UrlKnowledge
from agno.models.openai import OpenAIChat
from agno.vectordb.lancedb import LanceDb, SearchType
from dotenv import load_dotenv
  1. Set up Streamlit configuration and load environment:

load_dotenv()

st.set_page_config(
    page_title="Agentic RAG with GPT-5",
    page_icon="🧠",
    layout="wide"
)
  1. Create the main interface:

st.title("🧠 Agentic RAG with GPT-5")
st.markdown("""
This app demonstrates an intelligent AI agent that:
1. **Retrieves** relevant information from knowledge sources using LanceDB
2. **Reasons** about the information using GPT-5 Nano
3. **Responds** with contextual, intelligent answers

Enter your OpenAI API key in the sidebar to get started!
""")
  1. Set up sidebar for API key and knowledge management:

with st.sidebar:
    st.header("🔧 Configuration")
    
    openai_key = st.text_input(
        "OpenAI API Key",
        type="password", 
        value=os.getenv("OPENAI_API_KEY", ""),
        help="Get your key from https://platform.openai.com/"
    )

    st.subheader("🌐 Add Knowledge Sources")
    new_url = st.text_input(
        "Add URL",
        placeholder="https://docs.agno.com/introduction",
        help="Enter a URL to add to the knowledge base"
    )
    
    if st.button("➕ Add URL", type="primary"):
        if new_url:
            st.session_state.urls_to_add = new_url
            st.success(f"URL added to queue: {new_url}")
        else:
            st.error("Please enter a URL")
  1. Create cached knowledge base loading function:

@st.cache_resource(show_spinner="📚 Loading knowledge base...")
def load_knowledge() -> UrlKnowledge:
    """Load and initialize the knowledge base with LanceDB"""
    kb = UrlKnowledge(
        urls=["https://docs.agno.com/introduction/agents.md"],
        vector_db=LanceDb(
            uri="tmp/lancedb",
            table_name="agentic_rag_docs", 
            search_type=SearchType.vector,
            embedder=OpenAIEmbedder(api_key=openai_key),
        ),
    )
    kb.load(recreate=True)
    return kb
  1. Create the Agno agent with GPT-5 Nano:

@st.cache_resource(show_spinner="🤖 Loading agent...")
def load_agent(_kb: UrlKnowledge) -> Agent:
    """Create an agent using Agno framework with GPT-5 Nano"""
    return Agent(
        model=OpenAIChat(
            id="gpt-5-nano",
            api_key=openai_key
        ),
        knowledge=_kb,
        search_knowledge=True,
        instructions=[
            "Always search your knowledge before answering the question.",
            "Provide clear, well-structured answers in markdown format.",
            "Use proper markdown formatting with headers, lists, and emphasis where appropriate.", 
            "Structure your response with clear sections and bullet points when helpful.",
        ],
        markdown=True,
    )
  1. Handle URL additions and knowledge base updates:

# Check if API key is provided
if openai_key:
    # Load knowledge and agent
    knowledge = load_knowledge()
    agent = load_agent(knowledge)
    
    # Display current URLs in knowledge base
    if knowledge.urls:
        st.sidebar.subheader("📚 Current Knowledge Sources")
        for i, url in enumerate(knowledge.urls, 1):
            st.sidebar.markdown(f"{i}. {url}")
    
    # Handle URL additions
    if hasattr(st.session_state, 'urls_to_add') and st.session_state.urls_to_add:
        with st.spinner("📥 Loading new documents..."):
            knowledge.urls.append(st.session_state.urls_to_add)
            knowledge.load(
                recreate=False,
                upsert=True,
                skip_existing=True
            )
        st.success(f"✅ Added: {st.session_state.urls_to_add}")
        del st.session_state.urls_to_add
        st.rerun()
  1. Create the query interface with suggested prompts:

st.divider()
    st.subheader("🤔 Ask a Question")
    
    st.markdown("**Try these prompts:**")
    col1, col2, col3 = st.columns(3)
    with col1:
        if st.button("What is Agno?", use_container_width=True):
            st.session_state.query = "What is Agno and how do Agents work?"
    with col2:
        if st.button("Teams in Agno", use_container_width=True):
            st.session_state.query = "What are Teams in Agno and how do they work?"
    with col3:
        if st.button("Build RAG system", use_container_width=True):
            st.session_state.query = "Give me a step-by-step guide to building a RAG system."
    
    query = st.text_area(
        "Your question:",
        value=st.session_state.get("query", "What are AI Agents?"),
        height=100,
        help="Ask anything about the loaded knowledge sources"
    )
  1. Implement streaming response with event filtering😀 

if st.button("🚀 Get Answer", type="primary"):
        if query:
            st.markdown("### 💡 Answer")
            answer_container = st.container()
            answer_placeholder = answer_container.empty()
            
            answer_text = ""
            
            with st.spinner("🔍 Searching and generating answer..."):
                for chunk in agent.run(query, stream=True):
                    # Filter to only show content from RunResponseContent events
                    if hasattr(chunk, 'event') and chunk.event == "RunResponseContent":
                        if hasattr(chunk, 'content') and chunk.content and isinstance(chunk.content, str):
                            answer_text += chunk.content
                            answer_placeholder.markdown(answer_text, unsafe_allow_html=True)
        else:
            st.error("Please enter a question")

else:
    # Show instructions if API key is missing
    st.info("""
    👋 **Welcome! To use this app, you need:**
    
    - **OpenAI API Key** (set it in the sidebar)
      - Sign up at [platform.openai.com](https://platform.openai.com/)
      - Generate a new API key
    
    Once you enter the key, the app will load the knowledge base and agent.
    """)

Running the App

With our code in place, it's time to launch the app.

  1. In your terminal, navigate to the project folder and run:

streamlit run local_ai_real_estate_agent_team.py
  1. Streamlit will provide a local URL (typically http://localhost:8501). Open this in your web browser, enter your API key, and start asking questions about your knowledge base!

Working Application Demo

Conclusion

You've built an Agentic RAG system using the Agno framework with GPT-5 Nano and LanceDB. It's an intelligent agent that can understand context, search through knowledge efficiently, and provide thoughtful responses while maintaining excellent performance characteristics.

This setup can now be expanded further:

  1. Multi-Agent Teams: Use Agno to create teams of specialized agents that work together on complex tasks

  2. Advanced Memory: Implement long-term memory to track user preferences and conversation history

  3. Multi-Modal Extensions: Add support for images, audio, and video using Agno's native multi-modal capabilities

Keep experimenting with different configurations and features to build more sophisticated AI applications.

We share hands-on tutorials like this 2-3 times a week, to help you stay ahead in the world of AI. If you're serious about leveling up your AI skills and staying ahead of the curve, subscribe now and be the first to access our latest tutorials.

Don’t forget to share this tutorial on your social channels and tag Unwind AI (X, LinkedIn, Threads) to support us!

Reply

or to participate.