1.概述

        创建深度智能体时,如果不配置持久化存储,缺省使用基于状态的持久化后端。此时,文件系统保存在状态中,对话结束保存在其中的数据将丢失。为了跨线程共享数据,可以使用混合持久化后端,把不同路径的文件分别保存在状态中或者本地文件系统或数据库中。

     2.使用长短期记忆

        如下示例代码,创建一个深度智能体,把/memories路由的文件保存在文件系统,其他的保存在状态中。

from deepagents import create_deep_agent
from deepagents.backends import CompositeBackend, StateBackend, FilesystemBackend

#创建混合持久化后端。缺省为基于状态的后端,另外一个是基于文件系统的后端

composite_backend = lambda rt: CompositeBackend(
    default=StateBackend(rt),

    """

    基于文件系统后端通过root_dir指定具体的路径。/memories/是路由,所有以/memories

    开头的路径均指向基于文件系统的后端,非/memories开头的路径指向缺省的持久化后端

    """
    routes={
        "/memories/": FilesystemBackend(root_dir="./myagent", virtual_mode=True),
    },
)

agent = create_deep_agent(
    model=llm,
    backend=composite_backend)

        工作原理分析:

        1)短期记忆保存在StateBackend中,通过标准的路径访问(非/memories开头)

        2)生命周期与线程生命周期相同,线程结束则数据丢失

        3)长期记忆保存在FilesystemBackend中,通过标准路径访问(以/memories开头)

        4)长期记忆生命周期为从深度智能体启动到退出,跨线程和对话

     3.长短期记忆验证

      3.1短期记忆验证

        如下代码把数据保存在短期记忆中(也就是基于状态的持久化后端):

agent.invoke({
    "messages": [{"role": "user", "content": "Write hello world  to /draft.txt"}]
})

        再次调用,不能获取到文件数据:

agent.invoke({
    "messages": [{"role": "user", "content": "read draft from /draft.txt"}]
})

      3.2.长期记忆验证

        如下代码把个人业务爱好数据保存在长期记忆中(./myagent/hobbys.txt):

import uuid

# Thread 1: Write to long-term memory
config1 = {"configurable": {"thread_id": str(uuid.uuid4())}}
agent.invoke({
    "messages": [{"role": "user", "content": "Save my hobbys ##sports, noval### to /memories/hobbyes.txt"}]
}, config=config1)

        再次调用,能够从长期记忆获取到保存的数据:

config2 = {"configurable": {"thread_id": str(uuid.uuid4())}}
agent.invoke({
    "messages": [{"role": "user", "content": "What are my hobbys? read from /memories/preferences.txt"}]
}, config=config2)

      4.长期记忆应用场景

        长期记忆可用于指令进化、构建知识基和研究项目。

      4.1指令进化

        在创建深度智能体时提供一个初始提示词,在初始提示词中要求智能体后继根据用户的反馈更新提示词,不断迭代,使智能体定位更清晰、准确,更加符合用户的调性。如下代码中的提示词说明如何实现指自进化:

    

agent = create_deep_agent(
    store=InMemoryStore(),
    backend=lambda rt: CompositeBackend(
        default=StateBackend(rt),
        "/memories/": FilesystemBackend(root_dir="./myagent", virtual_mode=True),
    ),

    """

        每次调用时从文件读取,每次结束前根据用户反馈更新提示词并保存到文件中。

    """
    system_prompt="""You have a file at /memories/instructions.txt with additional
    instructions and preferences.

    Read this file at the start of conversations to understand user preferences.

    When users provide feedback like "please always do X" or "I prefer Y",
    update /memories/instructions.txt using the edit_file tool."""
)

      4.2构建知识基

        把需要跨线程共享的数据保存到基于文件系统的持久化后端,智能体可以把这些数据作为上下文执行推理。

      4.3构建研究智能体

        智能体执行研究任务时,把每一步的研究结果保存在文件系统中,在智能体执行过程中总是可以从文件系统获取研究结果数据作为上下文。以下代码说明,在构建研究智能体时如何保存研究结果:

research_agent = create_deep_agent(
    store=InMemoryStore(),
    backend=lambda rt: CompositeBackend(
        default=StateBackend(rt),
        routes={"/memories/": StoreBackend(rt)}
    ),
    system_prompt="""You are a research assistant.

    Save your research progress to /memories/research/:
    - /memories/research/sources.txt - List of sources found
    - /memories/research/notes.txt - Key findings and notes
    - /memories/research/report.md - Final report draft

    This allows research to continue across multiple sessions."""
)

    5.好的实践

      在深度智能使用长短期记忆时的好的实践包括:

       1)使用语义化路径。就像程序中被变量命名一样,文件路径定义要望文知义。比如:

/memories/users/preferences.txt
/memories/research/topic_a/sources.txt
/memories/research/topic_a/notes.txt

        2)存储结构文档化。在提示词中针对每个文件说明其存储的内容是什么,比如:

Your persistent memory structure:
- /memories/preferences.txt: User preferences and settings
- /memories/research/topic_a/: research information  about topic a

        3)持久化存储管理。定时清理长期记忆中的文件,以免数据无序膨胀,最后不可管理和使用

Logo

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

更多推荐