全部产品
Search
文档中心

表格存储:快速开始

更新时间:May 21, 2026

通过 CLI、Python SDK 或 Node.js SDK 任一方式,约 10 分钟内完成创建记忆库、写入对话、检索长期记忆与查看短期记忆的端到端流程。

前提条件

开始前,请确认已准备以下资源:

  • 已创建 Tablestore 实例并获取实例的 Endpoint 和实例名称。

    说明

    当前仅支持华北 2(北京)地域。

  • 可访问该实例的 AccessKey ID 和 AccessKey Secret。

  • 已安装 Python SDK、Node.js SDK 或 Tablestore Agent Storage CLI。

方式一:使用 CLI 快速体验

安装 CLI

npm install -g @tablestore/tablestore-agent-cli
tablestore-agent-cli version

配置凭证

tablestore-agent-cli configure set access_key_id '...'
tablestore-agent-cli configure set access_key_secret '...'
tablestore-agent-cli configure set region 'cn-beijing'

如已创建 Tablestore 实例,可继续配置实例信息:

tablestore-agent-cli configure set ots_endpoint 'https://<instance>.cn-beijing.ots.aliyuncs.com'
tablestore-agent-cli configure set ots_instance_name '<instance-name>'

如未配置 ots_endpointots_instance_name,CLI 会在需要时自动创建并复用托管 Tablestore 实例。

诊断配置

tablestore-agent-cli doctor memory

创建记忆库

tablestore-agent-cli memory create --store agent_memory --description "Agent 长期记忆库"

记忆库创建后,索引初始化需要一定时间。建议等待约 1 分钟后再执行写入和检索。

写入记忆

tablestore-agent-cli memory add \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001 \
  --text "用户喜欢喝咖啡,偏好简洁的回答风格" \
  --sync

--sync 表示等待记忆抽取完成后返回。生产链路中可不传该参数,使用默认异步写入。

检索长期记忆

tablestore-agent-cli memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id '*' \
  --run-id '*' \
  --query "用户喜欢什么饮品" \
  --top-k 5

检索长期记忆时,appIdtenantId 必填,agentIdrunId 可使用 * 实现跨 Agent、跨会话检索。

查看短期记忆

tablestore-agent-cli memory msg-list \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id assistant \
  --run-id session-001

短期记忆接口查询的是原始会话消息,要求四级 Scope(appIdtenantIdagentIdrunId)全部填写,不允许使用 *

方式二:使用 Python SDK

安装 SDK

pip install "tablestore>=6.4.5"

示例代码

from tablestore import OTSClient

client = OTSClient(
    "https://<instance>.cn-beijing.ots.aliyuncs.com",
    "<AccessKey ID>",
    "<AccessKey Secret>",
    "<instance-name>",
)

client.create_memory_store({
    "memoryStoreName": "agent_memory",
    "description": "Agent 长期记忆库",
})

client.add_memories({
    "memoryStoreName": "agent_memory",
    "scope": {
        "appId": "app-001",
        "tenantId": "user-001",
        "agentId": "assistant",
        "runId": "session-001",
    },
    "messages": [
        {"role": "user", "content": "我喜欢喝咖啡"},
        {"role": "assistant", "content": "好的,我记住了"},
    ],
    "sync": True,
})

result = client.search_memories({
    "memoryStoreName": "agent_memory",
    "scope": {
        "appId": "app-001",
        "tenantId": "user-001",
        "agentId": "*",
        "runId": "*",
    },
    "query": "用户喜欢什么饮品",
    "topK": 5,
})

for item in result.get("results", []):
    print(item["unit"]["text"], item["score"])

方式三:使用 Node.js SDK

安装 SDK

npm install tablestore@^5.6.5

示例代码

const TableStore = require("tablestore");

const client = new TableStore.Client({
  accessKeyId: "<AccessKey ID>",
  secretAccessKey: "<AccessKey Secret>",
  endpoint: "https://<instance>.cn-beijing.ots.aliyuncs.com",
  instancename: "<instance-name>",
});

async function main() {
  await client.createMemoryStore({
    memoryStoreName: "agent_memory",
    description: "Agent 长期记忆库",
  });

  await client.addMemories({
    memoryStoreName: "agent_memory",
    scope: {
      appId: "app-001",
      tenantId: "user-001",
      agentId: "assistant",
      runId: "session-001",
    },
    text: "用户喜欢喝咖啡,偏好简洁的回答风格",
    sync: true,
  });

  const result = await client.searchMemories({
    memoryStoreName: "agent_memory",
    scope: {
      appId: "app-001",
      tenantId: "user-001",
      agentId: "*",
      runId: "*",
    },
    query: "用户喜欢什么饮品",
    topK: 5,
  });

  for (const item of result.results || []) {
    console.log(item.unit.text, item.score);
  }
}

main().catch(console.error);

相关文档