Agno框架:构建多模态代理的轻量级库

Agno框架:构建多模态代理的轻量级库

在开发代理人工智能时,开发人员经常会发现自己需要在速度、灵活性和资源效率之间权衡利弊。我一直在探索人工智能代理框架,并接触到了 Agno(早期是 Phi-data)。Agno 是一个用于构建多模式代理的轻量级框架。他们声称自己的速度是 LangGraph 的 1 万倍,内存占用是 LangGraph 的 1/50。听起来很吸引人,对吗?

Agno 和 LangGraph 提供了截然不同的体验。在亲身体验了 Agno 并将其性能和架构与 LangGraph 进行比较之后,下面将详细介绍它们之间的区别、各自的亮点以及 Agno 的优势所在。

  • 构建 TriSage 和营销分析代理
  • 如果您需要快速、低内存使用率、多模态功能以及使用模型/工具的灵活性,请使用 Agno。
  • 如果您喜欢基于流程的逻辑或结构化的执行路径,或者已经与 LangChain 的生态系统绑定,请使用 LangGraph。

Agno能提供什么?

Agno 在设计时非常注重性能和简约性。Agno 的核心是一个开源、模型无关的代理框架,专为多模态任务而构建,这意味着它可以原生处理文本、图像、音频和视频。它的独特之处在于,即使在协调大量具有内存、工具和矢量存储等附加复杂性的代理时,其引擎盖下的运行速度也非常轻快。

突出的主要优势:

  • 惊人的实例化速度:Agno 中每个代理的创建时间约为 2μs,比 LangGraph 快约 10000 倍。
  • 轻巧的内存占用:Agno 代理平均仅占用 ~3.75 KiB 内存,是 LangGraph 代理的 50 倍。
  • 多模态本地支持:无需黑客或插件–Agno 从底层开始构建,可与各种媒体类型无缝协作。
  • 与模型无关:Agno 不关心您使用的是 OpenAI、Claude、Gemini 还是开源 LLM。您不会被锁定在特定的提供商或运行时中。
  • 实时监控:可以通过 Agno 实时观察代理会话和性能,从而使调试和优化更加顺畅。

亲身体验 Agno:构建 TriSage 代理

使用 Agno 感觉非常高效。您可以组建整个代理舰队,这些代理不仅可以并行运行,还可以共享内存、工具和知识库。这些代理可以专业化,并组成多代理团队,内存层支持在持久数据库中存储会话和状态。

真正令人印象深刻的是 Agno 如何在不牺牲性能的前提下管理复杂性。它可以处理现实世界中的代理协调工作,如工具链、基于RAG 的检索或结构化输出生成,而不会成为性能瓶颈。

如果您使用过LangGraph或类似框架,您会立即发现 Agno 能够避免启动滞后和资源消耗。在大规模应用中,这将成为一个关键的差异化因素。让我们构建 TriSage 代理。

安装所需库函数

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
!pip install -U agno
!pip install duckduckgo-search
!pip install openai
!pip install pycountry
!pip install -U agno !pip install duckduckgo-search !pip install openai !pip install pycountry
!pip install -U agno
!pip install duckduckgo-search
!pip install openai
!pip install pycountry

这些是安装所需 Python 软件包的 shell 命令:

  • agno:用于定义和运行人工智能代理的核心框架。
  • duckduckgo-search:让代理使用 DuckDuckGo 搜索网络。
  • openai:用于连接 OpenAI 的 GPT-4 或 GPT-3.5 等模型。

所需导入

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.googlesearch import GoogleSearchTools
from agno.tools.dalle import DalleTools
from agno.team import Team
from textwrap import dedent
from agno.agent import Agent from agno.models.openai import OpenAIChat from agno.tools.duckduckgo import DuckDuckGoTools from agno.tools.googlesearch import GoogleSearchTools from agno.tools.dalle import DalleTools from agno.team import Team from textwrap import dedent
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.googlesearch import GoogleSearchTools
from agno.tools.dalle import DalleTools
from agno.team import Team
from textwrap import dedent

API密钥设置

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from getpass import getpass
OPENAI_KEY = getpass('Enter Open AI API Key: ')
import os
os.environ['OPENAI_API_KEY'] = OPENAI_KEY
from getpass import getpass OPENAI_KEY = getpass('Enter Open AI API Key: ') import os os.environ['OPENAI_API_KEY'] = OPENAI_KEY
from getpass import getpass
OPENAI_KEY = getpass('Enter Open AI API Key: ')
import os
os.environ['OPENAI_API_KEY'] = OPENAI_KEY
  • getpass():输入 API 密钥的安全方法(因此密钥不可见)。
  • 密钥会存储在环境中,以便 agno 框架在调用 OpenAI 的 API 时获取。

web_agent – 搜索网页,writer_agent – 撰写文章,image_agent – 创建视觉效果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
web_agent = Agent(
name="Web Agent",
role="Search the web for information on Eiffel tower",
model=OpenAIChat(id="o3-mini"),
tools=[DuckDuckGoTools()],
instructions="Give historical information",
show_tool_calls=True,
markdown=True,
)
writer_agent = Agent(
name="Writer Agent",
role="Write comprehensive article on the provided topic",
model=OpenAIChat(id="o3-mini"),
tools=[GoogleSearchTools()],
instructions="Use outlines to write articles",
show_tool_calls=True,
markdown=True,
)
image_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DalleTools()],
description=dedent("""\
You are an experienced AI artist with expertise in various artistic styles,
from photorealism to abstract art. You have a deep understanding of composition,
color theory, and visual storytelling.\
"""),
instructions=dedent("""\
As an AI artist, follow these guidelines:
1. Analyze the user's request carefully to understand the desired style and mood
2. Before generating, enhance the prompt with artistic details like lighting, perspective, and atmosphere
3. Use the `create_image` tool with detailed, well-crafted prompts
4. Provide a brief explanation of the artistic choices made
5. If the request is unclear, ask for clarification about style preferences
Always aim to create visually striking and meaningful images that capture the user's vision!\
"""),
markdown=True,
show_tool_calls=True,
)
web_agent = Agent( name="Web Agent", role="Search the web for information on Eiffel tower", model=OpenAIChat(id="o3-mini"), tools=[DuckDuckGoTools()], instructions="Give historical information", show_tool_calls=True, markdown=True, ) writer_agent = Agent( name="Writer Agent", role="Write comprehensive article on the provided topic", model=OpenAIChat(id="o3-mini"), tools=[GoogleSearchTools()], instructions="Use outlines to write articles", show_tool_calls=True, markdown=True, ) image_agent = Agent( model=OpenAIChat(id="gpt-4o"), tools=[DalleTools()], description=dedent("""\ You are an experienced AI artist with expertise in various artistic styles, from photorealism to abstract art. You have a deep understanding of composition, color theory, and visual storytelling.\ """), instructions=dedent("""\ As an AI artist, follow these guidelines: 1. Analyze the user's request carefully to understand the desired style and mood 2. Before generating, enhance the prompt with artistic details like lighting, perspective, and atmosphere 3. Use the `create_image` tool with detailed, well-crafted prompts 4. Provide a brief explanation of the artistic choices made 5. If the request is unclear, ask for clarification about style preferences Always aim to create visually striking and meaningful images that capture the user's vision!\ """), markdown=True, show_tool_calls=True, )
web_agent = Agent(
name="Web Agent",
role="Search the web for information on Eiffel tower",
model=OpenAIChat(id="o3-mini"),
tools=[DuckDuckGoTools()],
instructions="Give historical information",
show_tool_calls=True,
markdown=True,
)
writer_agent = Agent(
name="Writer Agent",
role="Write comprehensive article on the provided topic",
model=OpenAIChat(id="o3-mini"),
tools=[GoogleSearchTools()],
instructions="Use outlines to write articles",
show_tool_calls=True,
markdown=True,
)
image_agent = Agent(
model=OpenAIChat(id="gpt-4o"),
tools=[DalleTools()],
description=dedent("""\
You are an experienced AI artist with expertise in various artistic styles,
from photorealism to abstract art. You have a deep understanding of composition,
color theory, and visual storytelling.\
"""),
instructions=dedent("""\
As an AI artist, follow these guidelines:
1. Analyze the user's request carefully to understand the desired style and mood
2. Before generating, enhance the prompt with artistic details like lighting, perspective, and atmosphere
3. Use the `create_image` tool with detailed, well-crafted prompts
4. Provide a brief explanation of the artistic choices made
5. If the request is unclear, ask for clarification about style preferences
Always aim to create visually striking and meaningful images that capture the user's vision!\
"""),
markdown=True,
show_tool_calls=True,
)

将三者组合起来

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
agent_team = Agent(
team=[web_agent, writer_agent, image_agent],
model=OpenAIChat(id="gpt-4o"),
instructions=["Give historical information", "Use outlines to write articles","Generate Image"],
show_tool_calls=True,
markdown=True,
)
agent_team = Agent( team=[web_agent, writer_agent, image_agent], model=OpenAIChat(id="gpt-4o"), instructions=["Give historical information", "Use outlines to write articles","Generate Image"], show_tool_calls=True, markdown=True, )
agent_team = Agent(
team=[web_agent, writer_agent, image_agent],
model=OpenAIChat(id="gpt-4o"),
instructions=["Give historical information", "Use outlines to write articles","Generate Image"],
show_tool_calls=True,
markdown=True,
)

运行整个过程

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
agent_team.print_response("Write an article on Eiffel towar and generate image", stream=True)
agent_team.print_response("Write an article on Eiffel towar and generate image", stream=True)
agent_team.print_response("Write an article on Eiffel towar and generate image", stream=True)

输出

运行整个agno任务过程输出结果

持续输出

运行整个agno任务过程输出结果

持续输出

运行整个agno任务过程输出结果

I have created a realistic image of the Eiffel Tower. The image captures the tower's full height and design,    ┃┃ beautifully highlighted by the late afternoon sun. You can view it by clicking here.

图像输出

埃菲尔铁塔

Agno实践:构建市场分析员代理

Market Analyst Agent 是一个使用 Agno 的基于团队的系统,结合了通过 DuckDuckGo 获取实时信息的 Web Agent 和通过 Yahoo Finance 获取金融数据的 Finance Agent。该系统由 OpenAI 模型提供支持,使用表格、标记和源代码支持的内容提供市场洞察和 AI 公司业绩,以提高清晰度、深度和透明度。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.yfinance import YFinanceTools
from agno.team import Team
web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OpenAIChat(id="o3-mini"),
tools=[DuckDuckGoTools()],
instructions="Always include sources",
show_tool_calls=True,
markdown=True,
)
finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
model=OpenAIChat(id="o3-mini"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions="Use tables to display data",
show_tool_calls=True,
markdown=True,
)
agent_team = Agent(
team=[web_agent, finance_agent],
model=OpenAIChat(id="gpt-4o"),
instructions=["Always include sources", "Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team.print_response("What's the market outlook and financial performance of top AI companies of the world?", stream=True)
from agno.agent import Agent from agno.models.openai import OpenAIChat from agno.tools.duckduckgo import DuckDuckGoTools from agno.tools.yfinance import YFinanceTools from agno.team import Team web_agent = Agent( name="Web Agent", role="Search the web for information", model=OpenAIChat(id="o3-mini"), tools=[DuckDuckGoTools()], instructions="Always include sources", show_tool_calls=True, markdown=True, ) finance_agent = Agent( name="Finance Agent", role="Get financial data", model=OpenAIChat(id="o3-mini"), tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)], instructions="Use tables to display data", show_tool_calls=True, markdown=True, ) agent_team = Agent( team=[web_agent, finance_agent], model=OpenAIChat(id="gpt-4o"), instructions=["Always include sources", "Use tables to display data"], show_tool_calls=True, markdown=True, ) agent_team.print_response("What's the market outlook and financial performance of top AI companies of the world?", stream=True)
from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.tools.duckduckgo import DuckDuckGoTools
from agno.tools.yfinance import YFinanceTools
from agno.team import Team
web_agent = Agent(
name="Web Agent",
role="Search the web for information",
model=OpenAIChat(id="o3-mini"),
tools=[DuckDuckGoTools()],
instructions="Always include sources",
show_tool_calls=True,
markdown=True,
)
finance_agent = Agent(
name="Finance Agent",
role="Get financial data",
model=OpenAIChat(id="o3-mini"),
tools=[YFinanceTools(stock_price=True, analyst_recommendations=True, company_info=True)],
instructions="Use tables to display data",
show_tool_calls=True,
markdown=True,
)
agent_team = Agent(
team=[web_agent, finance_agent],
model=OpenAIChat(id="gpt-4o"),
instructions=["Always include sources", "Use tables to display data"],
show_tool_calls=True,
markdown=True,
)
agent_team.print_response("What's the market outlook and financial performance of top AI companies of the world?", stream=True)

输出

Agno实践:构建市场分析员代理

Agno vs LangGraph:性能对决

让我们来看看具体细节,Agno 的官方文档中包含了这一切:

指标 Agno LangGraph 对比结果
代理实例时间 ~2μs ~20ms ~10,000x 快(Agno比LangGraph)
每个代理的内存使用量 ~3.75 KiB ~137 KiB ~50x 高(LangGraph比Agno)
  • 性能测试在 Apple M4 MacBook Pro 上进行,使用 Python 的 tracemalloc 进行内存分析。
  • Agno 测量了 1000 次运行的平均实例化和内存使用情况,并隔离了 Agent 代码,以获得干净利落的 delta。

这种速度和内存效率不仅仅是数字问题,它还是可扩展性的关键。在实际的代理部署中,成千上万的代理可能需要同时启动,每一毫秒和每一千字节都至关重要。

LangGraph 虽然功能强大,结构更合理,适合某些基于流的应用,但除非经过大量优化,否则往往难以承受这种负载。这对于小规模应用来说可能不是问题,但在运行生产规模的代理时,代价就会变得非常高昂。

那么……Agno 比 LangGraph 更好吗?

不一定。这取决于您正在构建什么:

  • 如果您正在构建基于流程的代理逻辑(想想看:带有高级控制的有向步骤图),LangGraph 可能会提供更具表现力的结构。
  • 但是,如果您需要超快、低脚本、多模式的代理执行,尤其是在高并发或动态环境中,Agno 将以绝对优势胜出。

Agno 显然更倾向于速度和系统级效率,而 LangGraph 则偏重于结构化协调和可靠性。尽管如此,Agno 的开发人员自己也承认,准确性和可靠性基准同样重要,而且这些基准目前正在制定中。在这些基准出来之前,我们无法断定边缘情况下的正确性或弹性。

小结

从实际操作的角度来看,Agno 已经为实际工作负载做好了准备,尤其是对于大规模构建代理系统的团队而言。它的实时性能监控、对结构化输出的支持,以及插入内存和矢量知识的能力,使其成为快速构建强大应用程序的令人信服的平台。

LangGraph 并没有退出竞争,它的优势在于清晰、面向流程的控制逻辑。但是,如果您遇到了扩展障碍,或者需要运行成千上万的代理而又不影响基础架构,那么 Agno 值得您认真考虑。

评论留言