RAG (Retrieval-Augmented Generation) is a technique that allows you to use a knowledge base to answer questions.Create a file rag_agent.py with the following code:
rag_agent.py
Copy
Ask AI
from phi.agent import Agentfrom phi.model.openai import OpenAIChatfrom phi.embedder.openai import OpenAIEmbedderfrom phi.knowledge.pdf import PDFUrlKnowledgeBasefrom phi.vectordb.lancedb import LanceDb, SearchType# Create a knowledge base from a PDFknowledge_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="tmp/lancedb", search_type=SearchType.vector, embedder=OpenAIEmbedder(model="text-embedding-3-small"), ),)# Comment out after first run as the knowledge base is loadedknowledge_base.load(recreate=False)agent = Agent( model=OpenAIChat(id="gpt-4o"), # Add the knowledge base to the agent knowledge=knowledge_base, show_tool_calls=True, markdown=True,)agent.print_response("How do I make chicken and galangal in coconut milk soup", stream=True)