from agno.agent import Agent
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.url import UrlKnowledge
from agno.models.openai import OpenAIChat
from agno.tools.knowledge import KnowledgeTools
from agno.vectordb.lancedb import LanceDb, SearchType
# 创建一个包含来自 URL 的信息的知识库
agno_docs = UrlKnowledge(
urls=["https://docs.agno.com/llms-full.txt"],
# 使用 LanceDB 作为向量数据库,并将嵌入存储在 `agno_docs` 表中
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
embedder=OpenAIEmbedder(id="text-embedding-3-small"),
),
)
knowledge_tools = KnowledgeTools(
knowledge=agno_docs,
think=True,
search=True,
analyze=True,
add_few_shot=True,
)
agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[knowledge_tools],
show_tool_calls=True,
markdown=True,
)
if __name__ == "__main__":
# 加载知识库,首次运行时可注释掉
agno_docs.load(recreate=True)
agent.print_response("How do I build multi-agent teams with Agno?", stream=True)
创建虚拟环境
打开 Terminal
并创建一个 python 虚拟环境。
python3 -m venv .venv
source .venv/bin/activate
设置您的 API 密钥
export OPENAI_API_KEY=xxx
安装库
pip install -U openai lancedb tantivy sqlalchemy agno
运行示例
python cookbook/reasoning/tools/knowledge_tools.py