記憶儲存服務支援結構化記憶和檔案記憶。結構化記憶適用於從對話中抽取和檢索長期記憶,檔案記憶適用於直接管理 Markdown 或 UTF-8 記憶檔案。
前提條件
已開通 Tablestore 服務,建立華北 2(北京)或華東 1(杭州)地區的執行個體,並擷取執行個體的 Endpoint 和執行個體名稱。
已擷取 AccessKey ID 與 AccessKey Secret,或已建立 API Key。API Key 建立方式參見API Key 管理。
檔案記憶
已有記憶檔案或需要自行組織 Agent 記憶目錄時,可以通過 Agent Storage SDK 直接寫入和讀取檔案。以下樣本使用 Python 完成安裝、建立檔案記憶庫、寫入檔案和讀取檔案。
安裝 Agent Storage SDK。檔案記憶要求 Python SDK 1.0.10 及以上版本。
pip install tablestore-agent-storage初始化用戶端,建立檔案記憶庫,然後寫入並讀取檔案。
from tablestore_agent_storage import AgentStorageClient client = AgentStorageClient( api_key="<your-api-key>", ots_endpoint="https://<instance>.cn-beijing.ots.aliyuncs.com", ots_instance_name="<instance-name>", ) scope = { "appId": "app-001", "tenantId": "user-001", "agentId": "assistant", "runId": "session-001", } client.create_memory_store({ "memoryStoreName": "agent_files", "storageMode": "filemem", }) client.add_item({ "memoryStoreName": "agent_files", "scope": scope, "path": "/profile/preferences.md", "content": "# 使用者偏好\n\n- 喜歡美式咖啡\n- 偏好簡潔回答\n", }) item = client.get_item({ "memoryStoreName": "agent_files", "scope": scope, "path": "/profile/preferences.md", }) print(item["content"])
Agent Storage SDK 的初始化方式和更多用法參見Agent Storage SDK。
結構化記憶
結構化記憶支援通過 CLI、Agent Storage SDK、Tablestore 原生 SDK 或 AI Agent 架構建立記憶庫、寫入對話並檢索長期記憶。選擇一種接入方式完成操作。
AgentStorage 控制台
AgentStorage 控制台提供建立記憶庫、寫入與檢索記憶、查看長短期記憶以及使用 Dream 整理記憶的可視化介面,無需編碼即可完成端到端操作。
登入AgentStorage 控制台,選擇地區並建立 AgentStorage 執行個體後,即可在控制台中建立記憶庫並寫入、檢索記憶。
完整操作步驟參見控制台使用指南。
CLI
CLI 適合在命令列中完成憑證配置、記憶庫管理、記憶寫入和檢索驗證。
安裝命令列工具。要求 Node.js 18 及以上版本。
npm install -g @tablestore/tablestore-agent-cli tablestore-agent-cli version配置 AccessKey、地區和 Tablestore 執行個體資訊。
tablestore-agent-cli configure set access_key_id '<AccessKey ID>' tablestore-agent-cli configure set access_key_secret '<AccessKey Secret>' tablestore-agent-cli configure set region 'cn-beijing' tablestore-agent-cli configure set ots_endpoint 'https://<instance>.cn-beijing.ots.aliyuncs.com' tablestore-agent-cli configure set ots_instance_name '<instance-name>'診斷記憶儲存配置和連通性。
tablestore-agent-cli doctor memory建立結構化記憶庫。
tablestore-agent-cli memory create \ --store agent_memory \ --description "Agent 長期記憶庫"寫入一條記憶,並等待記憶抽取完成。
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檢索長期記憶。
agentId和runId使用*,可以跨 Agent 和跨會話檢索。tablestore-agent-cli memory search \ --store agent_memory \ --app-id app-001 \ --tenant-id user-001 \ --agent-id '*' \ --run-id '*' \ --query "使用者喜歡什麼飲品" \ --top-k 5查看當前會話的短期記憶。短期記憶查詢需要填寫完整的四級 Scope,不支援萬用字元。
tablestore-agent-cli memory msg-list \ --store agent_memory \ --app-id app-001 \ --tenant-id user-001 \ --agent-id assistant \ --run-id session-001
--sync 會等待記憶抽取完成。索引可見度可能稍有延遲;如果首次檢索沒有返回結果,請稍後重試。
Agent Storage SDK
以下提供 Python 和 TypeScript 樣本,並使用 API Key 初始化用戶端,完成建立記憶庫、寫入記憶和執行檢索。
Python
安裝 SDK:
pip install tablestore-agent-storage最簡樣本:
from tablestore_agent_storage import AgentStorageClient
client = AgentStorageClient(
api_key="<your-api-key>",
ots_endpoint="https://<instance>.cn-beijing.ots.aliyuncs.com",
ots_instance_name="<instance-name>",
)
scope = {
"appId": "app-001",
"tenantId": "user-001",
"agentId": "assistant",
"runId": "session-001",
}
# 1. 建立記憶庫
client.create_memory_store({"memoryStoreName": "agent_memory"})
# 2. 寫入記憶
client.add_memories({
"memoryStoreName": "agent_memory",
"scope": scope,
"text": "使用者喜歡喝咖啡,偏好簡潔的回答風格",
"sync": True,
})
# 3. 執行語義檢索
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", []):
unit = item["unit"]
print(f"[{item['score']:.4f}] {unit['text']}")TypeScript
安裝 SDK:
npm install @tablestore/agent-storage最簡樣本:
import { AgentStorageClient } from '@tablestore/agent-storage';
const client = new AgentStorageClient({
apiKey: '<your-api-key>',
endpoint: 'https://<instance>.cn-beijing.ots.aliyuncs.com',
instanceName: '<instance-name>',
});
const scope = {
appId: 'app-001',
tenantId: 'user-001',
agentId: 'assistant',
runId: 'session-001',
};
// 1. 建立記憶庫
await client.createMemoryStore({ memoryStoreName: 'agent_memory' });
// 2. 寫入記憶
await client.addMemories({
memoryStoreName: 'agent_memory',
scope,
text: '使用者喜歡喝咖啡,偏好簡潔的回答風格',
sync: true,
});
// 3. 執行語義檢索
const result: any = 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.score.toFixed(4)}] ${item.unit.text}`);
}Tablestore 原生 SDK
Tablestore 原生 SDK 提供 Python 與 Node.js 兩種語言,將記憶儲存能力與已有的 Tablestore 應用整合。原生 SDK 目前僅支援 AccessKey 認證。
Python
安裝 SDK(要求 tablestore 版本 6.4.7 及以上):
pip install tablestore最簡樣本:
from tablestore import OTSClient
client = OTSClient(
"https://<instance>.cn-beijing.ots.aliyuncs.com",
"<AccessKey ID>",
"<AccessKey Secret>",
"<instance-name>",
)
scope = {
"appId": "app-001",
"tenantId": "user-001",
"agentId": "assistant",
"runId": "session-001",
}
# 1. 建立記憶庫
client.create_memory_store({"memoryStoreName": "agent_memory"})
# 2. 寫入記憶
client.add_memories({
"memoryStoreName": "agent_memory",
"scope": scope,
"text": "使用者喜歡喝咖啡,偏好簡潔的回答風格",
"sync": True,
})
# 3. 執行語義檢索
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", []):
unit = item["unit"]
print(f"[{item['score']:.4f}] {unit['text']}")Node.js
安裝 SDK(要求 tablestore 版本 5.6.5 及以上):
npm install tablestore最簡樣本:
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() {
const scope = {
appId: "app-001",
tenantId: "user-001",
agentId: "assistant",
runId: "session-001",
};
await client.createMemoryStore({ memoryStoreName: "agent_memory" });
await client.addMemories({
memoryStoreName: "agent_memory",
scope,
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.score.toFixed(4)}] ${item.unit.text}`);
}
}
main().catch(console.error);完整原生 SDK 用法參見Python SDK 使用介紹和Node.js SDK 使用介紹。
AI Agent 架構
如果在 OpenClaw、Hermes、Claude 等 AI Agent 架構中使用記憶儲存,可通過對應外掛程式讓 Agent 直接讀寫記憶庫,無需手動編寫 SDK 調用代碼。
各架構的詳細整合步驟參見Agent 生態整合。