---
title: 传统的 RAG 与 LanceDB
---

## 代码

```python
from agno.agent import Agent
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.models.openai import OpenAIChat
from agno.vectordb.lancedb import LanceDb, SearchType

# 从 URL 创建 PDF 知识库
knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    # 使用 LanceDB 作为向量数据库,并将嵌入存储在 `recipes` 表中
    vector_db=LanceDb(
        table_name="recipes",
        uri="tmp/lancedb",
        search_type=SearchType.vector,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
)
# 加载知识库:首次运行后可注释掉此行,因为知识库已加载
knowledge_base.load()

agent = Agent(
    model=OpenAIChat(id="gpt-4o"),
    knowledge=knowledge_base,
    # 通过将 AgentKnowledge 的引用添加到用户提示中来启用 RAG。
    add_references=True,
    # 设置为 False,因为代理默认启用 `search_knowledge=True`
    search_knowledge=False,
    show_tool_calls=True,
    markdown=True,
)
agent.print_response(
    "如何制作椰奶鸡肉高良姜汤", stream=True
)

用法

1

创建虚拟环境

打开 Terminal 并创建一个 python 虚拟环境。

python3 -m venv .venv
source .venv/bin/activate
2

设置您的 API 密钥

export OPENAI_API_KEY=xxx
3

安装库

pip install -U openai lancedb tantivy pypdf sqlalchemy agno
4

运行 Agent

python cookbook/agent_concepts/rag/traditional_rag_lancedb.py