python怎么写agno
时间: 2025-08-19 10:20:25 AIGC 浏览: 20
在 Python 中使用 `agno` 实现智能体(Agent)功能,主要涉及定义独立的智能体模块、指定模型和工具、创建多智能体团队以及执行任务等步骤。以下是一个详细的实现示例:
### 定义独立的智能体
每个智能体可以被赋予特定的角色,并通过绑定模型和工具来扩展其能力。
```python
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.yfinance import YFinanceTools
# 网络搜索智能体
web_agent = Agent(
name="Web Agent",
role="搜索互联网上的信息",
model=OpenAIChat(id="gpt-4o"),
tools=[DuckDuckGoTools()],
instructions="始终附上信息来源",
show_tool_calls=True,
markdown=True,
)
# 财经数据智能体
finance_agent = Agent(
name="Finance Agent",
role="获取金融数据",
model=OpenAIChat(id="gpt-4o"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions="使用表格展示数据",
show_tool_calls=True,
markdown=True,
)
```
### 创建多智能体团队
将多个智能体组合成一个团队,以便协作完成复杂任务。
```python
from agno.team import Team
# 多智能体团队
agent_team = Team(
mode="coordinate", # 协作模式
members=[web_agent, finance_agent],
model=OpenAIChat(id="gpt-4o"),
success_criteria="生成一份内容全面、结构清晰、数据驱动的 AI 半导体公司财经新闻报告",
instructions=["始终附上信息来源", "使用表格展示数据"],
show_tool_calls=True,
markdown=True,
)
```
### 执行任务
调用团队或单个智能体的方法,以执行用户定义的任务。
```python
# 执行任务
agent_team.print_response(
"请分析 AI 半导体公司的市场前景和财务表现",
stream=True
)
```
### 关键组件说明
1. **Agent**:每个智能体具有角色、模型、工具集和指令。
2. **Team**:用于协调多个智能体,支持协作模式并统一任务标准。
3. **Model**:绑定具体的语言模型(如 GPT-4),用于处理推理逻辑。
4. **Tools**:提供外部接口(如网络搜索、金融数据查询),扩展智能体的能力。
### 注意事项
- 确保已安装 `agno` 库,并正确配置了依赖项(如 `openai` 和 `yfinance`)[^1]。
- 模型 ID(如 `"gpt-4o"`)需根据实际使用的 API 进行调整。
- 可通过修改 `instructions` 来自定义输出格式和行为。
阅读全文
相关推荐















