如需查看完整的文档索引,请参阅 llms.txt。在文档页面的 URL 末尾加上 .md 即可查看该页面的 Markdown 版本。
主导航

跨会话复用记忆和历史记录

让智能体保留记忆,并能访问以往的会话。

智能体可以保存会话中获得的信息,供以后调用(记忆),也可以查阅以往会话中的消息(历史记录)。Agents API 原生支持这两项功能。本指南将演示如何利用它们提升智能体的性能和准确性。

要启用记忆或历史记录,请为智能体配置 memory 或 history 工具,也可以同时配置两者。您可以在创建智能体时配置工具,也可以在创建会话时覆盖智能体的工具配置。

工具 存储库中的内容 智能体可执行的操作
memory 可编辑的文本笔记,每条笔记都有路径和版本号 搜索、读取、创建和更新笔记。
history 与该存储库关联的各个会话中的对话 搜索以往的对话并读取原始消息。

应用可以直接管理笔记、列出历史记录库中的会话,也可以查看各工具检索到的内容。

如果您初次使用 OpenAI API,请先阅读 Agents API 快速入门。

快速入门

要在应用中使用记忆和历史记录,请通过 REST API 分别创建记忆库和历史记录库。保存生成的两个 UUID,以便将存储库关联到智能体或单个会话。

存储库类型
history_store_id=$(curl -fsS https://api.openai.com/v1/agents/history_stores \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H 'OpenAI-Beta: agents=v1' \
  --json '{}' | jq -er '.id')

在 tools 中填入这两个 ID,然后创建智能体。

curl -fsS https://api.openai.com/v1/agents \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H 'OpenAI-Beta: agents=v1' \
  --json @- <<JSON
{
  "model": "gpt-6.2-astra",
  "tools": [
    { "type": "history", "store_id": "$history_store_id" },
    { "type": "memory", "store_id": "$memory_store_id" }
  ]
}
JSON

将返回的智能体 id 作为 agent_id 传入创建会话的请求。默认情况下,会话会继承这两个存储库。

将存储库关联到会话

除了在创建智能体时配置记忆库和历史记录库,您还可以在创建新会话时覆盖这两项配置。

发送 POST /v1/agents/sessions 请求时,将 history 工具添加到 agent.tools。将该工具的 store_id 设为刚刚保存的历史记录库 ID。此会话会记录旅客的预订代码,供智能体稍后查询。

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      { "type": "history", "store_id": "$history_store_id" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "My Paris booking code is ZX721."
}
JSON

保存 agent.session.created 事件中的 session.id,以便在搜索结果中识别这段对话。收到 agent.session.turn.completed 后,再在另一个会话中搜索预订代码。

在使用新历史记录库时复用记忆

您可以分别配置每个工具。

  • 使用现有存储库。 将 store_id 设为同一项目中同类型存储库的 ID。
  • 创建新存储库。 在配置中包含该工具,并省略 store_id。API 会在创建会话时创建存储库。
  • 省略工具。 如果智能体无需访问某个数据源,请省略相应的 memory 或 history 工具。

在本例中,先向记忆库添加一条笔记,记录用户偏好的计量单位。将 memory_store_id 作为 memory 工具的 store_id 传入。配置 history 工具时省略 store_id,以便单独记录本次会话的历史。

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      { "type": "memory", "store_id": "$memory_store_id" },
      { "type": "history" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "Which units should I use for measurements? Check my saved preferences."
}
JSON

收到 agent.session.created 后,在 session.agent.tools 中找到 history 条目。保存该条目的 store_id,以便复用新建的历史记录库。后面的示例还要用到包含预订消息的存储库,请让 history_store_id 继续指向该库。

{ "type": "history", "store_id": "histstore_example" }

记忆工具的响应中也可能包含已弃用的别名 memory_store_id。请在请求中使用 store_id。

如果要覆盖已保存智能体的工具配置,请列出您想保留的所有工具。传入 agent.tools 会替换已保存的整个工具列表。

这些会话在没有沙盒的情况下运行,并禁用程序化工具调用,因此智能体会直接调用工具。

管理记忆笔记

智能体和应用都可以编辑记忆库中的笔记。智能体可以按路径或内容查找笔记。应用则使用笔记的 id 来读取、更新或删除笔记。

设置记忆指令

默认情况下,memory 工具会提示智能体查阅相关笔记,并保存或更新有用的信息。在 agent.tools 中为该工具设置 instructions,即可指定智能体应保存哪些细节,以及何时更新这些信息。

{
  "type": "memory",
  "store_id": "memstore_example",
  "instructions": "仅在用户要求记住其写作偏好时才保存这些偏好。偏好发生变化时,更新现有笔记。"
}

自定义指令会替换本次会话的默认记忆指令。如果新会话也需要这些指令,请在创建时再次传入。

创建笔记

要创建笔记,请将 memory_store_id 设为用于保存该笔记的记忆库 ID。path 用于在库内标识笔记。content 字段保存笔记正文。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"path": "style.md", "content": "Use metric units."}'

将返回的 id 保存为 note_id,以便读取、编辑或删除该笔记。

列出和读取笔记

如果已知路径,请列出笔记,然后在 data 中找到 path 与该路径一致的条目。列表仅返回元数据。只要 has_more 为 true,就将 last_id 作为查询参数 after 的值传入,以获取下一页。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"
{
  "data": [{ "id": "note_example", "path": "style.md", "revision": 1 }],
  "last_id": "note_example",
  "has_more": false
}

将 note_id 设为匹配条目的 id,然后获取该笔记,即可读取其正文和当前版本号(revision)。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"
{
  "id": "note_example",
  "path": "style.md",
  "content": "Use metric units.",
  "revision": 1
}

更新笔记

更新笔记时,请使用刚刚读取的笔记中的 revision。对于上面的响应,请设置 revision=1。将该值作为 expected_revision 传入,可以防止本次编辑覆盖并发更改。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "content": "Use metric units and Celsius.",
  "expected_revision": $revision
}
JSON

编辑成功后,API 会返回更新后的笔记。下次编辑或删除笔记时,请使用新的 revision。

如果 API 返回 409 或 503,请再次读取笔记,核对已保存的内容和版本号,确认仍需进行哪些更改后再重试。即使 API 返回 503,编辑也可能已经生效。

删除笔记

将 revision 设为 API 返回的最新版本号。要删除笔记,请通过查询参数 expected_revision 传入该值。

curl -sS --fail-with-body -X DELETE \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes/$note_id?expected_revision=$revision" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

浏览会话历史记录

如果您希望智能体能搜索某个会话中的消息,请在创建该会话时添加 history 工具。为多个会话使用同一个历史记录库 ID,即可一起搜索这些会话。要将搜索范围限定在一段对话内,请在 input 中提供该对话的会话 ID,并要求智能体使用 session_id 筛选条件。

要在应用中浏览历史记录,请将 history_store_id 设为 history 工具配置的存储库 ID,然后列出会话。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id/sessions" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

列表返回完整的会话对象。只要 has_more 为 true,就将 next 作为查询参数 page 的值传入。使用会话的 id 可以获取该会话的消息和工具调用。

查看各工具检索到的内容

智能体可以在同一轮对话中使用这两个工具。本例使用一条由应用写入的笔记和一条来自以往会话的消息。将 memory_store_id 和 history_store_id 分别设为保存该笔记和该消息的存储库 ID。

向两个存储库写入示例数据

将一条笔记直接写入记忆库。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/memory_stores/$memory_store_id/notes" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"path": "preferences.md", "content": "Prefers aisle seats."}'

将 history_store_id 设为包含预订消息的历史记录库 ID。如果跳过了前面的示例,请在启用历史记录的会话中发送预订消息。

将两个存储库都关联到新会话。

curl -sS --fail-with-body --no-buffer https://api.openai.com/v1/agents/sessions \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  --data-binary @- <<JSON
{
  "agent": {
    "model": "gpt-6.2-astra",
    "tools": [
      {
        "type": "memory",
        "store_id": "$memory_store_id",
        "instructions": "Read the user's saved seat preference. Leave the notes unchanged."
      },
      { "type": "history", "store_id": "$history_store_id" },
      { "type": "programmatic_tool_calling", "enabled": false }
    ]
  },
  "environment": { "type": "none" },
  "stream": true,
  "input": "Use my saved seat preference to pick 12A (window) or 12C (aisle). Find my Paris booking code in session history and include it in your answer."
}
JSON

将 session_id 设为新会话的 id,该值来自 agent.session.created 事件。本轮对话完成后,列出该会话的条目。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/sessions/$session_id/items?order=asc&limit=100" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"

将每次工具调用(function_call)与其结果(function_call_output)按 call_id 配对。查找 status 为 completed 的结果。本例中,每个结果都以 JSON 格式保存在 output[0].text 中。

记忆搜索结果

notes.search 的结果按笔记路径对匹配项分组。matches 中的每一对值分别表示行号和该行的文本。

{
  "results": [
    {
      "path": "preferences.md",
      "matches": [[1, "Prefers aisle seats."]],
      "match_count": 1
    }
  ],
  "returned_files": 1
}

历史记录搜索结果

history.search 的结果包含一个 hits 数组,其中列出匹配的消息片段。结果也可能包含当前会话中的消息。查看 ref.session_id 可以确定消息来自哪段对话,查看 ref.item_id 可以找到原始消息。以下摘录展示了一条来自先前会话的匹配结果。

{
  "ref": { "session_id": "sess_earlier", "item_id": "msg_example" },
  "snippet": "My Paris booking code is ZX721."
}

最终回复

智能体根据笔记从 input 提供的选项中选出了座位,并引用了先前消息中的预订代码。最终的 agent.session.turn.output_text.done 事件包含以下文本。

{
  "text": "Pick **12C (aisle)** to match your saved preference. Your Paris booking code is **ZX721**."
}

管理存储库

获取上述任一列表时,请将 next 作为查询参数 page 的值传入,直到 has_more 不再为 true。即使 data 为空,也要遵循这一分页规则。

要重命名存储库或修改其元数据,请按需传入 name、metadata 或同时传入两者。如果传入 metadata,请包含所有需要保留的键。

curl -sS --fail-with-body \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1" \
  -H "Content-Type: application/json" \
  -d '{"metadata": {"team": "support", "region": "eu"}}'

删除历史记录库

要删除历史记录库,请将 history_store_id 设为该库的 ID。删除后,仍可通过会话 ID 访问原有会话。要移除这些会话中保存的工作内容,请删除相应会话。

curl -sS --fail-with-body -X DELETE \
  "https://api.openai.com/v1/agents/history_stores/$history_store_id" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -H "OpenAI-Beta: agents=v1"