全部產品
Search
文件中心

Tablestore:快速開始

更新時間:May 22, 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);

相關文檔