记忆库管理

更新时间:
复制 MD 格式

管理记忆存储服务中的记忆库,包括创建、查询、列出、更新和删除操作。

前提条件

  • 已创建 AgentStorage 实例并获取 访问地址(endpoint) 和实例名。

  • 已安装Agent Storage SDK:Python ≥ 1.0.10,TypeScript ≥ 0.0.13

创建记忆库

记忆库是记忆数据的顶层容器。创建时指定输入类型(数据从哪来)和输出类型(记忆以什么形态存)。

设置项

说明

名称

字母、数字和下划线,最多 32 字符,同一实例内不可重复

描述

说明记忆库用途,最多 1024 字符

输入类型

messages(对话消息)或 file(文件),创建后不可修改

输出类型

structured(结构记忆)、file(文件记忆)或两者,创建后不可修改

提取指令

引导结构记忆抽取,最多 4096 字符

输入类型与输出类型必须配套:messages 输入可搭配 ["structured"]、["file"] 或 ["structured","file"];file 输入只能搭配 ["file"]。不传类型时默认 messages 输入 + structured 输出。

控制台

  1. 登录Agent Storage控制台,进入记忆库管理,点击新建记忆库。

  2. 填写名称(字母开头,仅含字母、数字和下划线,最多 32 字符)和描述。

  3. 选择输入类型:短期消息 或 文件。

  4. 选择输出类型:结构记忆、文件记忆 或两者。

  5. 按需填写提取指令(引导结构记忆抽取)。

  6. 点击确定创建记忆库。

SDK

Python

from tablestore_agent_storage import AgentStorageClient

client = AgentStorageClient(
    api_key="<your-api-key>",
    ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
    ots_instance_name="<instance-name>",
)

# 创建记忆库:消息输入,输出结构记忆 + 文件记忆
client.create_memory_store({
    "memoryStoreName": "agent_memory",
    "description": "Agent 长期记忆库",
    "inputType": "messages",
    "outputTypes": ["structured", "file"],
    "extractInstructions": "重点关注用户的饮食偏好与出行习惯",
})

TypeScript

import { AgentStorageClient } from '@tablestore/agent-storage';

const client = new AgentStorageClient({
  apiKey: '<your-api-key>',
  endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
  instanceName: '<instance-name>',
});

await client.createMemoryStore({
  memoryStoreName: 'agent_memory',
  description: 'Agent 长期记忆库',
  inputType: 'messages',
  outputTypes: ['structured', 'file'],
  extractInstructions: '重点关注用户的饮食偏好与出行习惯',
});

API

调用 CreateMemoryStore 创建记忆库。完整请求字段与响应说明参见创建记忆库。

查询记忆库详情

查询指定记忆库的完整配置,包括名称、描述、输入/输出类型和提取指令。

控制台

在记忆库管理页面点击目标记忆库卡片进入详情,或通过卡片右上角 ··· > 详情/设置查看基本信息(名称、存储模式、创建时间、描述、提取指令)。

SDK

Python

from tablestore_agent_storage import AgentStorageClient

client = AgentStorageClient(
    api_key="<your-api-key>",
    ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
    ots_instance_name="<instance-name>",
)

resp = client.get_memory_store({"memoryStoreName": "agent_memory"})
data = resp["data"] if "data" in resp else resp
print(f"名称: {data.get('memoryStoreName')}")
print(f"输入类型: {data.get('inputType')}, 输出类型: {data.get('outputTypes')}")

TypeScript

import { AgentStorageClient } from '@tablestore/agent-storage';

const client = new AgentStorageClient({
  apiKey: '<your-api-key>',
  endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
  instanceName: '<instance-name>',
});

const resp = await client.getMemoryStore({ memoryStoreName: 'agent_memory' });
const data = resp.data ?? resp;
console.log(data.memoryStoreName, data.inputType, data.outputTypes);

API

调用 GetMemoryStore 查询记忆库详情。完整字段参见查询记忆库。

列出记忆库

分页查询当前实例下的所有记忆库。

控制台

在记忆库管理页面可直接查看所有记忆库列表,包含名称、存储模式和创建时间。

SDK

Python

from tablestore_agent_storage import AgentStorageClient

client = AgentStorageClient(
    api_key="<your-api-key>",
    ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
    ots_instance_name="<instance-name>",
)

resp = client.list_memory_stores({})
stores = resp.get("stores", [])
for s in stores:
    print(f"{s['memoryStoreName']} | 输入:{s.get('inputType')} 输出:{s.get('outputTypes')}")

TypeScript

import { AgentStorageClient } from '@tablestore/agent-storage';

const client = new AgentStorageClient({
  apiKey: '<your-api-key>',
  endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
  instanceName: '<instance-name>',
});

const resp = await client.listMemoryStores({});
for (const s of resp.stores) {
  console.log(s.memoryStoreName, s.inputType, s.outputTypes);
}

API

调用 ListMemoryStores 列出记忆库。完整字段参见列出记忆库。

更新记忆库

更新记忆库的描述、提取指令或输出类型。

  • outputTypes 采用替换式更新,传入的值全量替换原有输出类型集合。

  • inputType 不接受修改;空字符串可清空 description 和 extractInstructions。

控制台

进入目标记忆库的设置,点击修改,更新描述、提取指令或输出类型。

SDK

Python

from tablestore_agent_storage import AgentStorageClient

client = AgentStorageClient(
    api_key="<your-api-key>",
    ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
    ots_instance_name="<instance-name>",
)

client.update_memory_store({
    "memoryStoreName": "agent_memory",
    "description": "更新后的描述",
    "extractInstructions": "聚焦用户偏好与出行习惯",
    "outputTypes": ["structured", "file"],
})

TypeScript

import { AgentStorageClient } from '@tablestore/agent-storage';

const client = new AgentStorageClient({
  apiKey: '<your-api-key>',
  endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
  instanceName: '<instance-name>',
});

await client.updateMemoryStore({
  memoryStoreName: 'agent_memory',
  description: '更新后的描述',
  extractInstructions: '聚焦用户偏好与出行习惯',
  outputTypes: ['structured', 'file'],
});

API

调用 UpdateMemoryStore 更新记忆库。完整字段参见更新记忆库。

删除记忆库

重要

此操作不可逆。删除记忆库会同时删除其下所有记忆和文件数据。

控制台

在记忆库卡片右上角点击 ··· > 删除,按提示确认。删除将清除该记忆库全部数据。

SDK

Python

from tablestore_agent_storage import AgentStorageClient

client = AgentStorageClient(
    api_key="<your-api-key>",
    ots_endpoint="https://<instance>.<region>.ots.aliyuncs.com",
    ots_instance_name="<instance-name>",
)

client.delete_memory_store({"memoryStoreName": "agent_memory"})

TypeScript

import { AgentStorageClient } from '@tablestore/agent-storage';

const client = new AgentStorageClient({
  apiKey: '<your-api-key>',
  endpoint: 'https://<instance>.<region>.ots.aliyuncs.com',
  instanceName: '<instance-name>',
});

await client.deleteMemoryStore({ memoryStoreName: 'agent_memory' });

API

调用 DeleteMemoryStore 删除记忆库。完整字段参见删除记忆库。

相关文档