> ## Documentation Index
> Fetch the complete documentation index at: https://ikun.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Traditional rag lancedb

````md theme={null}
---
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
)
````

## 用法

<Steps>
  <Snippet file="create-venv-step.mdx" />

  <Step title="设置您的 API 密钥">
    ```bash theme={null}
    export OPENAI_API_KEY=xxx
    ```
  </Step>

  <Step title="安装库">
    ```bash theme={null}
    pip install -U openai lancedb tantivy pypdf sqlalchemy agno
    ```
  </Step>

  <Step title="运行 Agent">
    <CodeGroup>
      ```bash Mac theme={null}
      python cookbook/agent_concepts/rag/traditional_rag_lancedb.py
      ```

      ```bash Windows theme={null}
      python cookbook/agent_concepts/rag/traditional_rag_lancedb.py
      ```
    </CodeGroup>
  </Step>
</Steps>

```
```
