初始化 Milvus
设置 Milvus 服务器的 uri 和 token。
- 如果您只需要用于线下小规模数据或原型设计的本地矢量数据库,将 uri 设置为本地文件路径(例如
./milvus.db
)是最便捷的方式,它会自动利用 Milvus Lite 将所有数据存储在该文件中。
- 如果您有大规模数据,例如超过一百万个向量,您可以参考 Docker 或 Kubernetes 来设置更高效的 Milvus 服务器。
在此设置中,请使用服务器地址和端口作为您的 uri,例如
http://localhost:19530
。如果您在 Milvus 中启用了身份验证功能,请使用 your_username:your_password
作为 token;否则,请勿设置 token。
- 如果您使用 Zilliz Cloud,即 Milvus 的全托管云服务,请相应调整
uri
和 token
。这些值对应于 Zilliz Cloud 中的 公共端点和 API 密钥。
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)
异步支持 ⚡
Milvus 也支持异步操作,可以实现并发,从而获得更好的性能。
# 安装 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))
在吞吐量高的应用中,请使用 aload()
和 aprint_response()
方法配合 asyncio.run()
来实现非阻塞操作。
Milvus 参数
参数 | 类型 | 描述 | 默认值 |
---|
collection | str | Milvus collection 的名称 | Required |
embedder | Optional[Embedder] | 用于嵌入文档的 embedder | OpenAIEmbedder() |
distance | Distance | 用于向量相似度的距离度量 | Distance.cosine |
uri | str | Milvus 服务器的 URI 或本地文件路径 | "http://localhost:19530" |
token | Optional[str] | 与 Milvus 服务器进行身份验证的 token | None |
高级选项可以通过额外的关键字参数传递给 MilvusClient
构造函数。
开发者资源
Responses are generated using AI and may contain mistakes.