设置
pip install chromadb
示例
agent_with_knowledge.py
import typer
from rich.prompt import Prompt
from typing import Optional
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.chroma import ChromaDb
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=ChromaDb(collection="recipes"),
)
def pdf_agent(user: str = "user"):
run_id: Optional[str] = None
agent = Agent(
run_id=run_id,
user_id=user,
knowledge_base=knowledge_base,
use_tools=True,
show_tool_calls=True,
debug_mode=True,
)
if run_id is None:
run_id = agent.run_id
print(f"Started Run: {run_id}\n")
else:
print(f"Continuing Run: {run_id}\n")
while True:
message = Prompt.ask(f"[bold] :sunglasses: {user} [/bold]")
if message in ("exit", "bye"):
break
agent.print_response(message)
if __name__ == "__main__":
# Comment out after first run
knowledge_base.load(recreate=False)
typer.run(pdf_agent)
异步支持 ⚡
ChromaDB 也支持异步操作,可以实现并发并带来更好的性能。
async_chroma_db.py
# install chromadb - `pip install chromadb`
import asyncio
from agno.agent import Agent
from agno.knowledge.pdf_url import PDFUrlKnowledgeBase
from agno.vectordb.chroma import ChromaDb
# Initialize ChromaDB
vector_db = ChromaDb(collection="recipes", path="tmp/chromadb", persistent_client=True)
# Create knowledge base
knowledge_base = PDFUrlKnowledgeBase(
urls=["https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"],
vector_db=vector_db,
)
# Create and use the agent
agent = Agent(knowledge=knowledge_base, show_tool_calls=True)
if __name__ == "__main__":
# Comment out after first run
asyncio.run(knowledge_base.aload(recreate=False))
# Create and use the agent
asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
在吞吐量高的应用程序中使用
aload() 和 aprint_response() 方法配合 asyncio.run() 进行非阻塞操作。ChromaDb 参数
| 参数 | 类型 | 默认值 | 描述 |
|---|---|---|---|
collection | str | - | 要使用的 collection 的名称。 |
embedder | Embedder | OpenAIEmbedder() | 用于嵌入文档内容的 embedder。 |
distance | Distance | cosine | 要使用的距离度量。 |
path | str | "tmp/chromadb" | ChromaDB 数据将存储的路径。 |
persistent_client | bool | False | 是否使用持久化的 ChromaDB 客户端。 |
开发者资源
- 查看 Cookbook (同步)
- 查看 Cookbook (异步)