From 835dfd66be2bad1a0a37545bccc5b3cd5a19fe80 Mon Sep 17 00:00:00 2001 From: ShubhamSaboo Date: Sun, 20 Oct 2024 20:06:09 -0500 Subject: [PATCH] Added new demo --- agentic_rag/README.md | 45 ++++++++++++++++++++++++++++++++++++ agentic_rag/rag_agent.py | 30 ++++++++++++++++++++++++ agentic_rag/requirements.txt | 8 +++++++ 3 files changed, 83 insertions(+) create mode 100644 agentic_rag/README.md create mode 100644 agentic_rag/rag_agent.py create mode 100644 agentic_rag/requirements.txt diff --git a/agentic_rag/README.md b/agentic_rag/README.md new file mode 100644 index 0000000..5f28f62 --- /dev/null +++ b/agentic_rag/README.md @@ -0,0 +1,45 @@ +## 🗃️ AI RAG Agent with Web Access +This script demonstrates how to build a Retrieval-Augmented Generation (RAG) agent with web access using GPT-4o in just 15 lines of Python code. The agent uses a PDF knowledge base and has the ability to search the web using DuckDuckGo. + +### Features + +- Creates a RAG agent using GPT-4o +- Incorporates a PDF-based knowledge base +- Uses LanceDB as the vector database for efficient similarity search +- Includes web search capability through DuckDuckGo +- Provides a playground interface for easy interaction + +### How to get Started? + +1. Clone the GitHub repository +```bash +git clone https://github.com/Shubhamsaboo/awesome-llm-apps.git +``` + +2. Install the required dependencies: + +```bash +pip install -r requirements.txt +``` + +3. Get your OpenAI API Key + +- Sign up for an [OpenAI account](https://platform.openai.com/) (or the LLM provider of your choice) and obtain your API key. +- Set your OpenAI API key as an environment variable: +```bash +export OPENAI_API_KEY='your-api-key-here' +``` + +4. Run the AI RAG Agent +```bash +python3 rag_agent.py +``` +5. Open your web browser and navigate to the URL provided in the console output to interact with the RAG agent through the playground interface. + +### How it works? + +1. **Knowledge Base Creation:** The script creates a knowledge base from a PDF file hosted online. +2. **Vector Database Setup:** LanceDB is used as the vector database for efficient similarity search within the knowledge base. +3. **Agent Configuration:** An AI agent is created using GPT-4o as the underlying model, with the PDF knowledge base and DuckDuckGo search tool. +4. **Playground Setup:** A playground interface is set up for easy interaction with the RAG agent. + diff --git a/agentic_rag/rag_agent.py b/agentic_rag/rag_agent.py new file mode 100644 index 0000000..118bcaa --- /dev/null +++ b/agentic_rag/rag_agent.py @@ -0,0 +1,30 @@ +from phi.agent import Agent +from phi.model.openai import OpenAIChat +from phi.knowledge.pdf import PDFUrlKnowledgeBase +from phi.vectordb.lancedb import LanceDb, SearchType +from phi.playground import Playground, serve_playground_app +from phi.tools.duckduckgo import DuckDuckGo + +db_uri = "tmp/lancedb" +# Create a knowledge base from a PDF +knowledge_base = PDFUrlKnowledgeBase( + urls=["https://phi-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"], + # Use LanceDB as the vector database + vector_db=LanceDb(table_name="recipes", uri=db_uri, search_type=SearchType.vector), +) +# Load the knowledge base: Comment out after first run +knowledge_base.load(upsert=True) + +rag_agent = Agent( + model=OpenAIChat(id="gpt-4o"), + agent_id="rag-agent", + knowledge=knowledge_base, # Add the knowledge base to the agent + tools=[DuckDuckGo()], + show_tool_calls=True, + markdown=True, +) + +app = Playground(agents=[rag_agent]).get_app() + +if __name__ == "__main__": + serve_playground_app("rag_agent:app", reload=True) \ No newline at end of file diff --git a/agentic_rag/requirements.txt b/agentic_rag/requirements.txt new file mode 100644 index 0000000..845536c --- /dev/null +++ b/agentic_rag/requirements.txt @@ -0,0 +1,8 @@ +phidata +openai +lancedb +tantivy +pypdf +sqlalchemy +pgvector +psycopg[binary]