> ## 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 字节知识库

> 了解如何在您的知识库中使用内存中的 PDF 字节。

**PDFBytesKnowledgeBase** 从**字节或 IO 流中读取 PDF 内容**，将其转换为向量嵌入并加载到向量数据库中。这在使用动态生成的 PDF、API 响应或文件上传时非常有用，无需将文件保存到磁盘。

## 用法

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

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

```python knowledge_base.py theme={null}
from agno.agent import Agent
from agno.knowledge.pdf import PDFBytesKnowledgeBase
from agno.vectordb.lancedb import LanceDb

vector_db = LanceDb(
    table_name="recipes_async",
    uri="tmp/lancedb",
)

with open("data/pdfs/ThaiRecipes.pdf", "rb") as f:
    pdf_bytes = f.read()

knowledge_base = PDFBytesKnowledgeBase(
    pdfs=[pdf_bytes],
    vector_db=vector_db,
)
knowledge_base.load(recreate=False)  # 首次运行后注释掉此行

agent = Agent(
    knowledge=knowledge_base,
    search_knowledge=True,
)

agent.print_response("How to make Tom Kha Gai?", markdown=True)
```

## 参数

| 参数             | 类型                                | 默认值         | 描述                                                     |
| -------------- | --------------------------------- | ----------- | ------------------------------------------------------ |
| pdfs           | Union\[List\[bytes], List\[IO]]   | -           | PDF 内容列表（以字节或 IO 流形式）。                                 |
| exclude\_files | List\[str]                        | \[]         | 要排除的文件模式列表（从基类继承）。                                     |
| reader         | Union\[PDFReader, PDFImageReader] | PDFReader() | 将 PDF 转换为向量数据库的 Document 的 PDFReader 或 PDFImageReader。 |

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

## 开发者资源

* 查看 [同步加载食谱](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/pdf_bytes_kb.py)
* 查看 [异步加载食谱](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/pdf_bytes_kb_async.py)
