目录

14 - 学习系统:让 Agent 越用越聪明

一句话总结:用 LearningMachine 让 Agent 从每次对话中提取模式、洞察和最佳实践,随着使用次数增多而不断进化。


Memory 和 Learning 是两个层次

上一篇我们聊了 Memory – 让 Agent 记住”你叫什么”、”你喜欢什么”。这已经很好了,但还不够。

打个比方:

  • Memory 就像记住同事的名字和喜好 – “张三喜欢喝美式咖啡”
  • Learning 就像在工作中积累经验 – “技术方案评审时,先问清楚性能指标再开始设计”

Memory 是关于用户的事实,Learning 是从交互中提炼出来的知识。Memory 让 Agent 认识你,Learning 让 Agent 越来越擅长它的工作。

Agno 的学习系统能自动提取五种不同类型的知识,涵盖从用户画像到领域最佳实践的方方面面。


最简单的用法:learning=True

开启学习只需要一行配置:

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.db.sqlite import SqliteDb

db = SqliteDb(db_file="tmp/agents.db")

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    db=db,
    learning=True,  # 就这一行,开启默认学习
    instructions="你是一个编程助手",
    markdown=True,
)

# 第一次对话:Agent 在回复的同时自动提取信息
agent.print_response(
    "我在用FastAPI开发微服务,怎么做健康检查?",
    user_id="dev01",
    session_id="session-1",
)

# 第二次对话:新 session,Agent 已经知道你用 FastAPI
agent.print_response(
    "帮我设计一个API接口",
    user_id="dev01",
    session_id="session-2",
)
# Agent 会根据你用 FastAPI 的背景来定制建议

learning=True 是一个快捷方式,等价于创建一个启用了 UserProfile 和 UserMemory 的 LearningMachine。Agent 回复的同时,后台会并行提取用户信息,零额外延迟感知。


LearningMachine 和五大学习存储

learning=True 只开启了两种学习类型。实际上 LearningMachine 管理着五种不同的学习存储,每种负责不同维度的知识:

存储类型 存什么 作用域 典型内容
UserProfile 结构化用户字段 按用户 姓名、称呼、角色
UserMemory 非结构化观察 按用户 偏好、习惯、背景
SessionContext 会话状态 按会话 目标、计划、进度、摘要
EntityMemory 第三方实体知识 按命名空间 公司信息、项目事实、人物关系
LearnedKnowledge 可复用洞察 按命名空间 最佳实践、领域模式、解决方案

前两个关注”用户是谁”,SessionContext 关注”当前在聊什么”,后两个关注”世界上有什么知识值得记住”。

要精细控制开启哪些存储,直接创建 LearningMachine:

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.db.sqlite import SqliteDb
from agno.learn import LearningMachine

db = SqliteDb(db_file="tmp/agents.db")

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    db=db,
    learning=LearningMachine(
        user_profile=True,      # 结构化用户画像
        user_memory=True,       # 非结构化用户记忆
        session_context=True,   # 会话上下文追踪
        entity_memory=True,     # 第三方实体知识图谱
    ),
    instructions="你是一个助手,会从每次对话中学习和改进",
    markdown=True,
)

LearningMachine 会自动从 Agent 继承 dbmodel,你不需要重复配置。


三种学习模式

每种学习存储可以独立配置提取模式:

ALWAYS 模式:自动提取

每次对话结束后,后台自动跑一轮 LLM 提取。不需要 Agent 做任何事情。

  • 优点:保证不遗漏任何信息
  • 缺点:每次都多一次 LLM 调用

AGENTIC 模式:Agent 自主决定

给 Agent 注入学习相关的工具,由它自己判断”这条信息值得存下来”,然后通过工具调用来保存。

  • 优点:高效,只在需要时触发
  • 缺点:Agent 可能判断失误,漏掉信息

PROPOSE 模式:人工确认

Agent 提出学习建议,等待用户确认后才保存。适合对数据质量要求极高的场景。

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.db.sqlite import SqliteDb
from agno.learn import (
    LearningMachine,
    LearningMode,
    UserProfileConfig,
    UserMemoryConfig,
    SessionContextConfig,
)

db = SqliteDb(db_file="tmp/agents.db")

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    db=db,
    learning=LearningMachine(
        # 用户画像用 ALWAYS -- 不漏掉名字等关键信息
        user_profile=UserProfileConfig(mode=LearningMode.ALWAYS),
        # 用户记忆用 AGENTIC -- Agent 自己判断什么值得记
        user_memory=UserMemoryConfig(mode=LearningMode.AGENTIC),
        # 会话上下文用 ALWAYS -- 每次自动更新摘要
        session_context=SessionContextConfig(mode=LearningMode.ALWAYS),
    ),
    instructions="你是一个助手,会从对话中学习",
    markdown=True,
)

agent.print_response(
    "我叫王磊,在阿里做后端开发。最近在优化一个高并发的订单系统。",
    user_id="wanglei",
    session_id="session-1",
)

这个例子里,UserProfile 会自动提取”王磊”这个名字(ALWAYS),UserMemory 则由 Agent 自行决定是否记录”在阿里做后端、优化订单系统”(AGENTIC),SessionContext 会自动生成当前会话的摘要。


实战:一个越用越懂你的编程助手

把学习系统串起来,看一个更完整的例子:

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.db.sqlite import SqliteDb
from agno.learn import (
    LearningMachine,
    LearningMode,
    UserProfileConfig,
    UserMemoryConfig,
)

db = SqliteDb(db_file="tmp/agents.db")

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    db=db,
    learning=LearningMachine(
        user_profile=UserProfileConfig(mode=LearningMode.ALWAYS),
        user_memory=UserMemoryConfig(
            mode=LearningMode.ALWAYS,
            additional_instructions="重点记录用户的技术栈、编码习惯和架构偏好",
        ),
    ),
    instructions=(
        "你是一个高级编程助手。"
        "根据你对用户的了解,给出个性化的技术建议。"
        "如果你知道用户的技术栈,直接用那个栈的代码示例来回答。"
    ),
    markdown=True,
)

user_id = "dev-liming"

# 第一轮:自我介绍
print("--- 第一轮对话 ---")
agent.print_response(
    "你好,我是李明。我主要写Python,用FastAPI做后端,"
    "数据库用PostgreSQL,部署在Docker + K8s上。"
    "我喜欢类型注解和Pydantic做数据校验。",
    user_id=user_id,
    session_id="session-1",
)

# 查看 Agent 学到了什么
lm = agent.learning_machine
if lm and lm.user_profile_store:
    lm.user_profile_store.print(user_id=user_id)
if lm and lm.user_memory_store:
    lm.user_memory_store.print(user_id=user_id)

# 第二轮:全新 session,问一个通用问题
print("\n--- 第二轮对话(新 session) ---")
agent.print_response(
    "我要实现一个缓存层,有什么好方案?",
    user_id=user_id,
    session_id="session-2",
)
# Agent 会基于你用 Python/FastAPI/PostgreSQL 的背景来推荐
# 比如推荐 Redis + fastapi-cache,而不是 Java 的 Caffeine

第二轮对话中,虽然你只问了”缓存方案”,但 Agent 已经知道你的技术栈,所以它会直接给出 Python 生态的方案,而不是泛泛地列举各种语言的选项。这就是学习系统带来的个性化。


LearnedKnowledge:跨用户的知识积累

前面的存储类型都是关于”某个用户”的信息。LearnedKnowledge 不同 – 它存储的是可复用的领域知识,可以在所有用户之间共享。

比如一个技术助手在帮用户 A 解决了一个 K8s 网络问题后,提炼出一条最佳实践。之后用户 B 问到类似问题时,Agent 可以直接调用这条知识。

LearnedKnowledge 需要一个向量数据库来做语义搜索,所以配置稍微复杂一些。默认使用 AGENTIC 模式 – Agent 通过 save_learningsearch_learnings 工具来主动管理知识。

from agno.agent import Agent
from agno.models.openai import OpenAIChat
from agno.db.sqlite import SqliteDb
from agno.knowledge import Knowledge
from agno.knowledge.embedder.openai import OpenAIEmbedder
from agno.learn import LearningMachine, LearnedKnowledgeConfig, LearningMode
from agno.vectordb.pgvector import PgVector, SearchType

db_url = "postgresql+psycopg://ai:ai@localhost:5532/ai"

knowledge = Knowledge(
    vector_db=PgVector(
        db_url=db_url,
        table_name="learned_knowledge",
        search_type=SearchType.hybrid,
        embedder=OpenAIEmbedder(id="text-embedding-3-small"),
    ),
)

agent = Agent(
    model=OpenAIChat(id="gpt-4o-mini"),
    db=SqliteDb(db_file="tmp/agents.db"),
    learning=LearningMachine(
        knowledge=knowledge,
        learned_knowledge=LearnedKnowledgeConfig(
            mode=LearningMode.AGENTIC,
            namespace="global",  # 全局共享
        ),
    ),
    instructions="回答问题前先搜索已有的知识。发现有价值的洞察时主动保存。",
    markdown=True,
)

# 用户 A 分享了一个经验
agent.print_response(
    "记住这个:K8s Pod 间通信延迟高时,先检查 NetworkPolicy 是否有多余规则,"
    "我们团队因为这个问题排查了三天。",
    user_id="user-a",
    session_id="session-1",
)

# 用户 B 遇到类似问题 -- Agent 可以搜索到这条知识
agent.print_response(
    "我的K8s服务间调用延迟很高,可能是什么原因?",
    user_id="user-b",
    session_id="session-2",
)

Learning vs Memory 对比

学到这里,有必要把 Memory(第 6 篇)和 Learning 做个清晰的区分:

维度 Memory (MemoryManager) Learning (LearningMachine)
存什么 用户事实和偏好 模式、洞察、上下文、实体、最佳实践
存储类型 单一 五种独立存储,按需组合
作用域 仅按用户 按用户 + 按会话 + 按命名空间
更新方式 用户信息变了就更新 知识持续增长
学习模式 Agentic 或 Always ALWAYS / AGENTIC / PROPOSE
典型场景 “记住我叫小明” “从经验中总结出最佳实践”
可共享 不可以,仅当前用户 LearnedKnowledge 可跨用户共享

简单说:Memory 是 Learning 的子集。如果你只需要记住用户信息,Memory 就够了。如果你想让 Agent 像真正的助手一样不断成长,用 Learning。

实际上 learning=True 内部就开启了 UserProfile + UserMemory,功能上覆盖了 MemoryManager 的核心能力。


查看学习内容

每种存储都提供了 print 方法,方便你在开发阶段检查 Agent 到底学到了什么:

lm = agent.learning_machine

# 查看用户画像
if lm.user_profile_store:
    lm.user_profile_store.print(user_id="dev01")

# 查看用户记忆
if lm.user_memory_store:
    lm.user_memory_store.print(user_id="dev01")

# 查看会话上下文
if lm.session_context_store:
    lm.session_context_store.print(session_id="session-1")

# 查看实体记忆
if lm.entity_memory_store:
    lm.entity_memory_store.print(namespace="global")

# 查看学到的知识
if lm.learned_knowledge_store:
    lm.learned_knowledge_store.print(query="K8s")

今天学了什么

回顾一下关键要点:

  • learning=True 是最快的入门方式,自动开启 UserProfile 和 UserMemory
  • LearningMachine 是学习系统的核心,协调五种不同的学习存储
  • 五种存储各有分工:UserProfile(结构化画像)、UserMemory(非结构化记忆)、SessionContext(会话状态)、EntityMemory(实体知识图谱)、LearnedKnowledge(可复用洞察)
  • 三种模式:ALWAYS(自动提取)、AGENTIC(Agent 自主决定)、PROPOSE(人工确认)
  • 每种存储可以独立配置模式,混搭使用
  • LearnedKnowledge 可以跨用户共享,让 Agent 真正积累组织级知识
  • 生产环境建议用 PostgreSQL,LearnedKnowledge 需要向量数据库支持

下一篇预告

15 - 模型切换:一套代码,多种模型

到目前为止我们一直用的 OpenAI。但如果你想换成 Claude、Gemini、本地模型呢?Agno 的模型抽象层让你只改一行代码就能切换底层模型,而上层的 Agent、工具、记忆、学习全都不用动。