> ## 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.

# PDF URL 知识库

> 了解如何在知识库中使用远程 PDF。

**PDFUrlKnowledgeBase** 从 **URL 读取 PDF**，将其转换为向量嵌入，并加载到向量数据库中。

## 用法

<Note>
  我们在示例中使用了本地 PgVector 数据库。[确保它正在运行](https://docs.agno.com/vectordb/pgvector)
</Note>

```shell theme={null}
pip install pypdf
```

```python knowledge_base.py theme={null}
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.pgvector import PgVector

knowledge_base = PDFUrlKnowledgeBase(
    urls=["pdf_url"],
    # 表名：ai.pdf_documents
    vector_db=PgVector(
        table_name="pdf_documents",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
    ),
)
```

然后将 `knowledge_base` 与 Agent 一起使用：

```python agent.py theme={null}
from agno.agent import Agent
from knowledge_base import knowledge_base

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)
agent.knowledge.load(recreate=False)

agent.print_response("Ask me about something from the knowledge base")
```

#### PDFUrlKnowledgeBase 也支持异步加载。

```shell theme={null}
pip install qdrant-client
```

我们在示例中使用了本地 Qdrant 数据库。[确保它正在运行](https://docs.agno.com/vectordb/qdrant)

```python async_knowledge_base.py theme={null}
import asyncio

from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase, PDFUrlReader
from agno.vectordb.qdrant import Qdrant

COLLECTION_NAME = "pdf-url-reader"

vector_db = Qdrant(collection=COLLECTION_NAME, url="http://localhost:6333")

# 使用 data/pdfs 目录中的 PDF 创建一个知识库
knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
    reader=PDFUrlReader(chunk=True),
)

# 使用知识库创建一个 Agent
agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

if __name__ == "__main__":
    # 首次运行时注释掉
    asyncio.run(knowledge_base.aload(recreate=False))

    # 创建并使用 Agent
    asyncio.run(agent.aprint_response("How to make Thai curry?", markdown=True))
```

## 参数

| 参数       | 类型             | 默认值 | 描述                                                     |
| -------- | -------------- | --- | ------------------------------------------------------ |
| `urls`   | `List[str]`    | -   | `PDF` 文件的 URL。                                         |
| `reader` | `PDFUrlReader` | -   | 一个 `PDFUrlReader`，用于将 `PDF` 转换为 `Documents` 以便存入向量数据库。 |

`PDFUrlKnowledgeBase` 是 [`AgentKnowledge`](/reference/knowledge/base) 类的一个子类，拥有相同的参数访问权限。

## 开发者资源

* 查看 [同步加载食用指南](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/pdf_url_kb.py)
* 查看 [异步加载食用指南](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/pdf_url_kb_async.py)
