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_endpoint と ots_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長期記憶を検索する場合、appId と tenantId は必須です。agentId と runId にはアスタリスク (*) を使用して、エージェントとセッションを横断して検索できます。
短期記憶の表示
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 つのスコープレベル (appId、tenantId、agentId、runId) がすべて必要です。ワイルドカード (*) は使用できません。
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);関連ドキュメント
API パラメータの一覧については、API リファレンスをご参照ください。
Python SDK の詳細な手順については、Python SDK 利用ガイドをご参照ください。
Node.js SDK の詳細な手順については、Node.js SDK 利用ガイドをご参照ください。
CLI コマンドの一覧については、CLI 利用ガイドをご参照ください。
本番環境における推奨事項については、ベストプラクティスをご参照ください。