Agno 是一个 Python 框架,用于构建具有共享内存、知识和推理能力的多智能体系统。

主要功能

  • 支持构建从基础到高级的五种智能体层级:
    • Level 1:具有工具和指令的智能体
    • Level 2:具有知识和存储的智能体
    • Level 3:具有记忆和推理能力的智能体
    • Level 4:能够推理和协作的智能体团队
    • Level 5:具有状态和确定性的智能体工作流

核心特性

  • 模型无关:提供统一接口支持 23+ 模型提供商,无锁定效应
  • 高性能:智能体实例化仅需约 3μs,平均使用约 6.5Kib 内存
  • 原生支持推理能力:三种推理方法提升智能体可靠性
  • 多模态支持:接受文本、图像、音频和视频输入,生成多种类型输出
  • 先进的多智能体架构:支持具有推理、记忆和共享上下文的智能体团队
  • 内置智能检索功能:支持 20+ 向量数据库的实时信息检索
  • 内置记忆和会话存储:提供长期记忆和会话存储驱动
  • 结构化输出:支持全类型化响应
  • 预置 FastAPI 路由:快速部署到生产环境
  • 监控功能:通过 app.agno.com 实时监控智能体会话和性能

快速入门

构建第一个智能体,首先安装依赖:

pip install agno ddgs

接下来编写如下python代码:

"""
Agno 框架示例程序
功能:创建一个基于通义千问的 AI Agent,并执行简单查询
"""

# 导入必要的模块
from agno.agent import Agent           # Agent 类:创建智能代理
from agno.models.qwen import Qwen     # Qwen 类:通义千问模型接口
from agno.tools.duckduckgo import DuckDuckGoTools  # DuckDuckGoTools 类:网络搜索工具

# 系统模块
import os                             # 操作系统接口模块

# 配置 API 密钥
# 注意:在生产环境中应使用更安全的方式存储密钥,如环境变量或配置文件
os.environ["DASHSCOPE_API_KEY"] = "填入你自己的dashscope key"

# 创建 AI Agent 实例
# Agent 是一个智能代理,可以处理用户请求并生成响应
agent = Agent(
    # 配置使用的语言模型
    model=Qwen(
        id="qwen-flash",              # 模型标识符,使用快速版通义千问
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # API 地址
        top_p=0.7,                    # Top-p 采样参数,控制文本生成多样性
        temperature=0.95,             # 温度参数,控制生成文本的随机性
    ),
    
    # 配置可用工具列表
    tools=[DuckDuckGoTools()],        # 添加网络搜索工具
    
    # 配置 Agent 行为指令
    instructions="用表格展示数据。不要包含任何其他文本。",  # 指示 Agent 以表格形式返回数据
    
    # 启用 Markdown 格式支持
    markdown=True                     # 启用 Markdown 输出格式
)

# 执行查询并输出结果
# 向 Agent 提出问题并以流式方式打印响应
agent.print_response("中国2025年阅兵的时间是什么时候?", stream=True)

结果打印如下:

┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃ 中国2025年阅兵的时间是什么时候?                                              ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Tool Calls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃ • duckduckgo_search(query=中国 2025年 阅兵 时间)                             ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Response (4.8s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃                                                                              ┃
┃                                                                              ┃
┃   日期           时间   活动名称                            地点             ┃
┃  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━  ┃
┃   2025年9月3日   上午   纪念中国人民抗日战争胜利80周年阅…   北京天安门广场   ┃
┃                                                                              ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

如果没有阿里的百炼key,可以在这里申请:

https://bailian.console.aliyun.com/?tab=model#/api-key

具有知识和存储的智能体

知识:尽管模型拥有大量训练数据,但我们几乎始终需要为其提供特定领域的信息,以使其做出更优决策并提供准确响应(即检索增强生成,RAG)。我们将此类信息存储在向量数据库中,供智能体(Agent)在运行时进行检索。

存储:模型编程接口(API)是无状态的,而存储驱动程序会将聊天记录和状态保存到数据库中。当智能体(Agent)运行时,它会从数据库中读取聊天记录和状态,并将其添加到消息列表中,从而恢复对话并使智能体具备状态管理能力。

接下来我们使用argo实现这个RAG系统:

pip install fastembed sqlalchemy chromadb

接下来编写代码,实现国内本地可用的版本:

"""
Agno 框架 RAG 示例程序

功能:
- 创建一个基于通义千问的 AI Agent
- 集成向量数据库实现检索增强生成 (RAG)
- 从指定 URL 加载知识库
- 支持流式响应输出

说明:
本示例使用 ChromaDB 向量数据库替代 LanceDB,以解决 macOS 文件系统兼容性问题。
"""

# 导入必要的模块
from agno.agent import Agent                    # Agent 类:创建智能代理
from agno.embedder.fastembed import FastEmbedEmbedder  # FastEmbed 嵌入器:用于文本向量化
from agno.knowledge.url import UrlKnowledge    # UrlKnowledge 类:从 URL 加载知识
from agno.models.qwen import Qwen              # Qwen 类:通义千问模型接口
from agno.storage.sqlite import SqliteStorage  # SqliteStorage 类:会话存储
from agno.tools.duckduckgo import DuckDuckGoTools  # DuckDuckGoTools 类:网络搜索工具
from agno.vectordb.chroma import ChromaDb      # ChromaDb 类:Chroma 向量数据库

# 系统模块
import os                                      # 操作系统接口模块

# 配置 API 密钥
# 注意:在生产环境中应使用更安全的方式存储密钥,如环境变量或配置文件
os.environ["DASHSCOPE_API_KEY"] = "改成你自己的key"

# 确保临时目录存在
os.makedirs("./tmp", exist_ok=True)

# 配置知识库
# 从指定 URL 加载文档并存储到向量数据库中
knowledge = UrlKnowledge(
    # 指定要加载的文档 URL 列表
    urls=["https://docs.agno.com/introduction.md"],
    
    # 配置向量数据库
    vector_db=ChromaDb(
        # 数据库存储路径
        path="./tmp/chromadb",
        
        # 集合名称(类似表名)
        collection="agno_docs",
        
        # 文本嵌入器,用于将文本转换为向量表示
        embedder=FastEmbedEmbedder(),
    ),
)

# 配置会话存储
# 使用 SQLite 数据库存储 Agent 的对话历史
storage = SqliteStorage(
    table_name="agent_sessions",   # 数据库表名
    db_file="tmp/agent.db"         # 数据库文件路径
)

# 创建 AI Agent 实例
agent = Agent(
    # 配置语言模型
    model=Qwen(
        id="qwen-flash",              # 模型标识符,使用快速版通义千问
        base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",  # API 地址
        top_p=0.7,                    # Top-p 采样参数,控制文本生成多样性
        temperature=0.95,             # 温度参数,控制生成文本的随机性
    ),

    # Agent 行为指令
    instructions=[
        "在回答问题前先搜索你的知识库",
        "在响应中只包含输出内容,不要添加其他文本",
    ],
    
    # 关联知识库
    knowledge=knowledge,
    
    # 关联会话存储
    storage=storage,
    
    # 在指令中添加时间戳信息
    add_datetime_to_instructions=True,
    
    # 在消息中添加对话历史
    add_history_to_messages=True,
    
    # 保留的历史对话轮数
    num_history_runs=3,
    
    # 启用 Markdown 格式输出
    markdown=True,
)

# 加载知识库
# 首次运行时会加载并处理文档,后续运行可注释掉此行
# 设置 recreate=True 可以重新创建知识库
agent.knowledge.load(recreate=False)

# 执行查询并流式输出结果
agent.print_response("What is Agno?", stream=True)

执行结果如下:

┏━ Message ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃ What is Agno?                                                                ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Tool Calls ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃ • search_knowledge_base(query=What is Agno?)                                 ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
┏━ Response (6.6s) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
┃                                                                              ┃
┃ Agno is a Python framework for building multi-agent systems with shared      ┃
┃ memory, knowledge, and reasoning. It enables engineers and researchers to    ┃
┃ create agents at various levels of sophistication:                           ┃
┃                                                                              ┃
┃  • Level 1: Agents with tools and instructions.                              ┃
┃  • Level 2: Agents with knowledge and storage.                               ┃
┃  • Level 3: Agents with memory and reasoning.                                ┃
┃  • Level 4: Agent teams that can reason and collaborate.                     ┃
┃  • Level 5: Agentic workflows with state and determinism.                    ┃
┃                                                                              ┃
┃ Key features include:                                                        ┃
┃                                                                              ┃
┃  • Model agnostic: Supports 23+ model providers without lock-in.             ┃
┃  • High performance: Agents instantiate in ~3μs and use ~6.5KB memory on     ┃
┃    average.                                                                  ┃
┃  • First-class reasoning: Supports reasoning models, ReasoningTools, and     ┃
┃    chain-of-thought approaches.                                              ┃
┃  • Natively multi-modal: Accepts and generates text, image, audio, and       ┃
┃    video.                                                                    ┃
┃  • Advanced multi-agent architecture: Includes Agent Teams with shared       ┃
┃    context.                                                                  ┃
┃  • Built-in agentic search: Uses 20+ vector databases for runtime            ┃
┃    information retrieval.                                                    ┃
┃  • Built-in memory & storage: Provides long-term memory and session          ┃
┃    persistence.                                                              ┃
┃  • Structured outputs: Returns fully-typed responses via model-provided      ┃
┃    structured outputs or json_mode.                                          ┃
┃  • Pre-built FastAPI routes: Enables rapid deployment from development to    ┃
┃    production.                                                               ┃
┃  • Monitoring: Real-time monitoring available via app.agno.com.              ┃
┃                                                                              ┃
┃ Agno is designed to streamline the creation of high-performing, autonomous   ┃
┃ agentic systems.                                                             ┃
┃                                                                              ┃
┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛

这段代码完成了如下流程:

  • 从指定 URL 加载文档并构建知识库
  • 创建 AI Agent 并配置相关组件
  • 执行查询"What is Agno?"并流式输出结果
  • 该程序专门针对 macOS 文件系统兼容性问题,使用 ChromaDB 替代了默认的 LanceDB 向量数据库。
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐