传统的软件遵循预先编程的步骤顺序。Agents 使用机器学习模型动态确定其行动方案,其核心组件包括:
- Model: 控制执行流程。它决定是进行推理、行动还是响应。
- Tools: 使 Agent 能够采取行动并与外部系统进行交互。
- Instructions: 是我们对 Agent 进行编程的方式,教它如何使用工具和进行响应。
Agents 还拥有记忆、知识、存储和推理能力:
- Reasoning: 使 Agents 在响应前能够“思考”并“分析”其行动(即工具调用)的结果,这提高了响应的可靠性和质量。
- Knowledge: 是 Agent 在运行时搜索以做出更好决策和提供准确响应的领域特定信息(RAG)。知识存储在向量数据库中,这种运行时搜索模式称为 Agentic RAG/Agentic Search。
- Storage: 用于 Agents 将会话历史和状态保存到数据库。模型 API 是无状态的,而存储使我们能够从中断的地方继续对话。这使得 Agents 具有状态,能够实现多轮、长期的对话。
- Memory: 使 Agents 能够存储和回忆先前交互的信息,从而允许它们学习用户偏好并个性化其响应。
让我们构建几个 Agents 来看看它们是如何工作的。
Level 1: 具有工具和指令的 Agents
最简单的 Agent 拥有模型、工具和指令。让我们构建一个可以使用 yfinance
库获取数据的 Agent,并附带显示结果的表格的指令。
from agno.agent import Agent
from agno.models.anthropic import Claude
from agno.tools.yfinance import YFinanceTools
agent = Agent(
model=Claude(id="claude-sonnet-4-20250514"),
tools=[YFinanceTools(stock_price=True)],
instructions="Use tables to display data. Don't include any other text.",
markdown=True,
)
agent.print_response("What is the stock price of Apple?", stream=True)
创建虚拟环境,安装依赖项,导出你的 API 密钥并运行 Agent。
设置你的虚拟环境
uv venv --python 3.12
source .venv/bin/activate
安装依赖项
uv pip install -U agno anthropic yfinance
导出你的 Anthropic 密钥
export ANTHROPIC_API_KEY=sk-***
运行 Agent
python agent_with_tools.py
设置 debug_mode=True
或 export AGNO_DEBUG=true
以查看系统提示和用户消息。
Level 2: 具有知识和存储的 Agents
Knowledge: 虽然模型拥有大量的训练数据,但我们几乎总是需要为它们提供特定领域的信息,以便做出更好的决策和提供准确的响应(RAG)。我们将这些信息存储在向量数据库中,并让 Agent 在运行时进行搜索。
Storage: 模型 API 是无状态的,Storage
驱动程序将聊天历史和状态保存到数据库。当 Agent 运行时,它从数据库读取聊天历史和状态,并将其添加到消息列表中,从而恢复对话并使 Agent 具有状态。
在这个示例中,我们将使用:
UrlKnowledge
来加载 Agno 文档到 LanceDB,使用 OpenAI 进行嵌入。
SqliteStorage
来将 Agent 的会话历史和状态保存在数据库中。
from agno.agent import Agent
from agno.embedder.openai import OpenAIEmbedder
from agno.knowledge.url import UrlKnowledge
from agno.models.anthropic import Claude
from agno.storage.sqlite import SqliteStorage
from agno.vectordb.lancedb import LanceDb, SearchType
# 在知识库中加载 Agno 文档
# 你也可以使用 `https://docs.agno.com/llms-full.txt` 来获取完整文档
knowledge = UrlKnowledge(
urls=["https://docs.agno.com/introduction.md"],
vector_db=LanceDb(
uri="tmp/lancedb",
table_name="agno_docs",
search_type=SearchType.hybrid,
# 使用 OpenAI 进行嵌入
embedder=OpenAIEmbedder(id="text-embedding-3-small", dimensions=1536),
),
)
# 将 Agent 会话存储在 SQLite 数据库中
storage = SqliteStorage(table_name="agent_sessions", db_file="tmp/agent.db")
agent = Agent(
name="Agno Assist",
model=Claude(id="claude-sonnet-4-20250514"),
instructions=[
"Search your knowledge before answering the question.",
"Only include the output in your response. No other text.",
],
knowledge=knowledge,
storage=storage,
add_datetime_to_instructions=True,
# 将聊天历史添加到消息中
add_history_to_messages=True,
# 历史运行次数
num_history_runs=3,
markdown=True,
)
if __name__ == "__main__":
# 加载知识库,首次运行时请注释掉此行
# 如果需要重新创建知识库,请将 recreate 设置为 True
agent.knowledge.load(recreate=False)
agent.print_response("What is Agno?", stream=True)
安装依赖项,导出你的 OPENAI_API_KEY
并运行 Agent
安装新依赖项
uv pip install -U lancedb tantivy openai sqlalchemy
Level 3: 具有记忆和推理的 Agents
- Reasoning: 使 Agents 能够**“思考”和“分析”**,提高可靠性和质量。
ReasoningTools
是提高 Agent 回应质量的最佳方法之一。
- Memory: 使 Agents 能够对用户偏好进行分类、存储和回忆,从而实现个性化响应。记忆帮助 Agent 构建用户画像并从先前的互动中学习。
from agno.agent import Agent
from agno.memory.v2.db.sqlite import SqliteMemoryDb
from agno.memory.v2.memory import Memory
from agno.models.anthropic import Claude
from agno.tools.reasoning import ReasoningTools
from agno.tools.yfinance import YFinanceTools
memory = Memory(
# 使用任何模型来创建和管理记忆
model=Claude(id="claude-sonnet-4-20250514"),
# 将记忆存储在 SQLite 数据库中
db=SqliteMemoryDb(table_name="user_memories", db_file="tmp/agent.db"),
# 默认禁用删除,如果需要请启用
delete_memories=True,
clear_memories=True,
)
agent = Agent(
model=Claude(id="claude-sonnet-4-20250514"),
tools=[
ReasoningTools(add_instructions=True),
YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True, company_news=True),
],
# 用于存储记忆的用户 ID,如果未提供则为 `default`
user_id="ava",
instructions=[
"Use tables to display data.",
"Include sources in your response.",
"Only include the report in your response. No other text.",
],
memory=memory,
# 让 Agent 管理其记忆
enable_agentic_memory=True,
markdown=True,
)
if __name__ == "__main__":
# 这将创建一个记忆,其中“ava”喜欢的股票是 NVIDIA 和 TSLA
agent.print_response(
"My favorite stocks are NVIDIA and TSLA",
stream=True,
show_full_reasoning=True,
stream_intermediate_steps=True,
)
# 这将利用记忆来回答问题
agent.print_response(
"Can you compare my favorite stocks?",
stream=True,
show_full_reasoning=True,
stream_intermediate_steps=True,
)
运行 Agent
你可以单独使用 Memory
和 Reasoning
,不必同时使用它们。
Responses are generated using AI and may contain mistakes.