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

# Milvus Agent 知识库

## 设置

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

## 初始化 Milvus

设置 Milvus 服务器的 uri 和 token。

* 如果您只需要用于线下小规模数据或原型设计的本地矢量数据库，将 uri 设置为本地文件路径（例如 `./milvus.db`）是最便捷的方式，它会自动利用 [Milvus Lite](https://milvus.io/docs/milvus_lite.md) 将所有数据存储在该文件中。
* 如果您有大规模数据，例如超过一百万个向量，您可以参考 [Docker 或 Kubernetes](https://milvus.io/docs/quickstart.md) 来设置更高效的 Milvus 服务器。
  在此设置中，请使用服务器地址和端口作为您的 uri，例如 `http://localhost:19530`。如果您在 Milvus 中启用了身份验证功能，请使用 `your_username:your_password` 作为 token；否则，请勿设置 token。
* 如果您使用 [Zilliz Cloud](https://zilliz.com/cloud)，即 Milvus 的全托管云服务，请相应调整 `uri` 和 `token`。这些值对应于 Zilliz Cloud 中的 [公共端点和 API 密钥](https://docs.zilliz.com/docs/on-zilliz-cloud-console#cluster-details)。

## 示例

```python agent_with_knowledge.py theme={null}
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.milvus import Milvus

vector_db = Milvus(
    collection="recipes",
    uri="./milvus.db",
)
# 创建知识库
knowledge_base = PDFUrlKnowledgeBase(
    urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
    vector_db=vector_db,
)

knowledge_base.load(recreate=False)  # 首次运行后注释掉此行

# 创建并使用 Agent
agent = Agent(knowledge=knowledge_base, use_tools=True, show_tool_calls=True)
agent.print_response("How to make Tom Kha Gai", markdown=True)
agent.print_response("What was my last question?", stream=True)
```

<Card title="异步支持 ⚡">
  <div className="mt-2">
    <p>
      Milvus 也支持异步操作，可以实现并发，从而获得更好的性能。
    </p>

    ```python async_milvus_db.py theme={null}
    # 安装 pymilvus - `pip install pymilvus`
    import asyncio

    from agno.agent import Agent
    from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
    from agno.vectordb.milvus import Milvus

    # 使用本地文件初始化 Milvus
    vector_db = Milvus(
        collection="recipes",
        uri="tmp/milvus.db",  # 用于基于本地文件的存储
    )

    # 创建知识库
    knowledge_base = PDFUrlKnowledgeBase(
        urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
        vector_db=vector_db,
    )

    # 使用知识库创建 Agent
    agent = Agent(knowledge=knowledge_base)

    if __name__ == "__main__":
        # 异步加载知识库
        asyncio.run(knowledge_base.aload(recreate=False))  # 首次运行后注释掉此行

        # 异步查询 Agent
        asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
    ```

    <Tip className="mt-4">
      在吞吐量高的应用中，请使用 <code>aload()</code> 和 <code>aprint\_response()</code> 方法配合 <code>asyncio.run()</code> 来实现非阻塞操作。
    </Tip>
  </div>
</Card>

## Milvus 参数

<Snippet file="vectordb_milvus_params.mdx" />

高级选项可以通过额外的关键字参数传递给 `MilvusClient` 构造函数。

## 开发者资源

* 查看 [Cookbook (同步)](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/vector_dbs/milvus_db/milvus_db.py)
* 查看 [Cookbook (异步)](https://github.com/agno-agi/agno/blob/main/cookbook/agent_concepts/knowledge/vector_dbs/milvus_db/async_milvus_db.py)
