すべてのプロダクト
Search
ドキュメントセンター

Tablestore:クイックスタート

最終更新日:May 22, 2026

CLI、Python SDK、または Node.js SDK を使用して、約 10 分でエンドツーエンドのワークフローを完了します:メモリストアの作成、セッションの書き込み、長期記憶の検索、短期記憶の表示。

前提条件

開始する前に、次の項目を用意してください:

  • エンドポイントとインスタンス名を含む Tablestore インスタンス。

    説明

    この機能は現在、中国 (北京) リージョンでのみ利用できます。

  • インスタンスにアクセスするための認証情報 (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 long-term memory store"

メモリストアを作成した後、インデックスの初期化に少し時間がかかります。メモリの書き込みや検索を行う前に、約 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 "The user likes coffee and prefers concise responses." \
  --sync

--sync パラメータを指定すると、コマンドはメモリ抽出の完了を待ってからレスポンスを返します。本番環境では、このパラメータを省略して、デフォルトの非同期書き込みを使用できます。

長期記憶の検索

tablestore-agent-cli memory search \
  --store agent_memory \
  --app-id app-001 \
  --tenant-id user-001 \
  --agent-id '*' \
  --run-id '*' \
  --query "What does the user like to drink?" \
  --top-k 5

長期記憶を検索する場合、appIdtenantId は必須です。agentIdrunId にはアスタリスク (*) を使用して、エージェントとセッションを横断して検索できます。

短期記憶の表示

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

短期記憶 API は未加工のセッションメッセージをクエリし、4 つのスコープレベル (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 long-term memory store",
})

client.add_memories({
    "memoryStoreName": "agent_memory",
    "scope": {
        "appId": "app-001",
        "tenantId": "user-001",
        "agentId": "assistant",
        "runId": "session-001",
    },
    "messages": [
        {"role": "user", "content": "I like coffee"},
        {"role": "assistant", "content": "Okay, I will remember that"},
    ],
    "sync": True,
})

result = client.search_memories({
    "memoryStoreName": "agent_memory",
    "scope": {
        "appId": "app-001",
        "tenantId": "user-001",
        "agentId": "*",
        "runId": "*",
    },
    "query": "What does the user like to drink?",
    "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 long-term memory store",
  });

  await client.addMemories({
    memoryStoreName: "agent_memory",
    scope: {
      appId: "app-001",
      tenantId: "user-001",
      agentId: "assistant",
      runId: "session-001",
    },
    text: "The user likes coffee and prefers concise responses.",
    sync: true,
  });

  const result = await client.searchMemories({
    memoryStoreName: "agent_memory",
    scope: {
      appId: "app-001",
      tenantId: "user-001",
      agentId: "*",
      runId: "*",
    },
    query: "What does the user like to drink?",
    topK: 5,
  });

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

main().catch(console.error);

関連ドキュメント