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

# CSV 知识库

> 了解如何在知识库中使用本地 CSV 文件。

**CSVKnowledgeBase** 读取**本地 CSV** 文件，将其转换为向量嵌入，并加载到向量数据库中。

## 用法

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

```python theme={null}
from agno.knowledge.csv import CSVKnowledgeBase
from agno.vectordb.pgvector import PgVector

knowledge_base = CSVKnowledgeBase(
    path="data/csv",
    # 表名: ai.csv_documents
    vector_db=PgVector(
        table_name="csv_documents",
        db_url="postgresql+psycopg://ai:ai@localhost:5532/ai",
    ),
)
```

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

```python 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")
```

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

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

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

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

from agno.agent import Agent
from agno.knowledge.csv import CSVKnowledgeBase
from agno.vectordb.qdrant import Qdrant

COLLECTION_NAME = "csv-reader"

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


knowledge_base = CSVKnowledgeBase(
    path=Path("data/csv"),
    vector_db=vector_db,
    num_documents=5,  # 搜索时返回的文档数量
)

# 使用 knowledge_base 初始化 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("What is the csv file about", markdown=True))
```

## 参数

| 参数       | 类型                 | 默认值           | 描述                                                    |
| -------- | ------------------ | ------------- | ----------------------------------------------------- |
| `path`   | `Union[str, Path]` | -             | CSV 文件的路径                                             |
| `reader` | `CSVReader`        | `CSVReader()` | 用于读取 CSV 文件并将其转换为 `Documents` 以便加载到向量数据库的 `CSVReader` |

`CSVKnowledgeBase` 是 [AgentKnowledge](/reference/knowledge/base) 类的一个子类，并且拥有相同的参数。

## 开发者资源

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