跳至主要内容

Memory API

Agent Memory 讓 AI 跨對話記住專案層級的規則、事實與時效性上下文。Memory 以 project 為唯一隔離維度,同一 project 下所有 API Key 共享相同記憶池。

需要 Basic+ 方案才能使用 Memory 功能。

記憶類型

類型說明注入方式
rule行為規則,例如「回覆時不要使用 emoji」全量注入(所有 active rules)
fact事實性記憶,例如「專案使用 PostgreSQL 18」語義檢索 Top-K
context時效性上下文,可設定過期時間語義檢索 Top-K + expires_at 過濾

建立記憶

POST /v1/memories

POST /v1/memories
Authorization: Bearer sk-your-api-key
Content-Type: application/json
{
"content": "回覆時不要使用 emoji",
"memory_type": "rule",
"importance": 0.9,
"expires_at": null
}

請求欄位:

欄位類型必填說明
contentstring記憶內容(1–10000 字元)
memory_typestringrule / fact / context
importancenumber0.0 – 1.0,越高越優先注入,預設 0.5
expires_atstringRFC3339,主要供 context 類型使用;null 表示永久

回應(201 Created):

{
"success": true,
"code": 1000,
"message": "Success",
"data": {
"id": "019449a012347abc8def0123456789ab",
"content": "回覆時不要使用 emoji",
"memory_type": "rule",
"importance": 0.9,
"is_active": true,
"created_at": "2026-05-08T12:00:00Z",
"updated_at": "2026-05-08T12:00:00Z",
"last_accessed_at": null,
"expires_at": null
}
}

列出記憶

GET /v1/memories

GET /v1/memories?memory_type=rule&page=1&page_size=20
Authorization: Bearer sk-your-api-key

查詢參數:

參數類型預設說明
memory_typestring篩選類型:rule / fact / context
pagenumber1頁碼
page_sizenumber20每頁筆數
sort_bystringcreated_at排序欄位:created_at / updated_at / importance
sort_dirstringdesc排序方向:asc / desc

回應:

{
"success": true,
"code": 1000,
"message": "Success",
"data": {
"memories": [
{
"id": "019449a012347abc8def0123456789ab",
"content": "回覆時不要使用 emoji",
"memory_type": "rule",
"importance": 0.9,
"is_active": true,
"created_at": "2026-05-08T12:00:00Z",
"updated_at": "2026-05-08T12:00:00Z",
"last_accessed_at": null,
"expires_at": null
}
],
"pagination": {
"page": 1,
"page_size": 20,
"total": 42,
"total_pages": 3
}
}
}

取得記憶詳情

GET /v1/memories/{memory_id}

GET /v1/memories/019449a012347abc8def0123456789ab
Authorization: Bearer sk-your-api-key

回傳單筆 AgentMemory 物件,格式同建立記憶的回應。


更新記憶

PUT /v1/memories/{memory_id}

PUT /v1/memories/019449a012347abc8def0123456789ab
Authorization: Bearer sk-your-api-key
Content-Type: application/json
{
"content": "回覆時不要使用 emoji 或特殊符號",
"importance": 0.95,
"is_active": true,
"expires_at": null
}

請求欄位(全部選填,至少傳一個):

欄位類型說明
contentstring記憶內容;變更時自動重新計算 Embedding
memory_typestringrule / fact / context
importancenumber0.0 – 1.0
is_activeboolean是否啟用
expires_atstring / nullRFC3339;傳 null 表示清除過期設定

回傳更新後的 AgentMemory 物件。


刪除記憶

DELETE /v1/memories/{memory_id}

DELETE /v1/memories/019449a012347abc8def0123456789ab
Authorization: Bearer sk-your-api-key

回傳 204 No Content


語義搜尋

POST /v1/memories/search

POST /v1/memories/search
Authorization: Bearer sk-your-api-key
Content-Type: application/json
{
"query": "回覆風格規則",
"memory_type": "rule",
"top_k": 10,
"min_similarity": 0.5,
"include_expired": false
}

請求欄位:

欄位類型必填說明
querystring搜尋文字(1–10000 字元)
memory_typestring限定搜尋的記憶類型
top_knumber回傳數量(1–50),預設 10
min_similaritynumber最低相似度門檻(0.0–1.0)
include_expiredboolean是否包含已過期記憶,預設 false

回應:

{
"success": true,
"code": 1000,
"message": "Success",
"data": {
"results": [
{
"memory": {
"id": "019449a012347abc8def0123456789ab",
"content": "回覆時不要使用 emoji",
"memory_type": "rule",
"importance": 0.9,
"is_active": true,
"created_at": "2026-05-08T12:00:00Z",
"updated_at": "2026-05-08T12:00:00Z",
"last_accessed_at": null,
"expires_at": null
},
"similarity": 0.89,
"final_score": 0.863
}
],
"query": "回覆風格規則",
"total": 1
}
}

回應欄位說明:

欄位類型說明
results[].memoryobject完整 AgentMemory 物件
results[].similaritynumber向量相似度(0.0–1.0)
results[].final_scorenumber綜合分數 = similarity × 0.7 + importance × 0.3;結果依此排序
querystring原始搜尋文字
totalnumber符合條件的結果總數

自動萃取記憶

POST /v1/memories/extract

讓 LLM 從文字中自動識別並儲存值得記住的資訊。會消耗少量 Credits。

POST /v1/memories/extract
Authorization: Bearer sk-your-api-key
Content-Type: application/json
{
"text": "我是前端工程師,主要使用 React 和 TypeScript。",
"deduplicate": true
}

請求欄位:

欄位類型必填說明
textstring要萃取的文字(1–50000 字元)
deduplicateboolean是否去重,預設 true

回應:

{
"success": true,
"code": 1000,
"message": "Success",
"data": {
"extracted": [
{
"id": "019449a0aaaa7abc8def0123456789ab",
"content": "使用者是前端工程師,主要使用 React 和 TypeScript",
"memory_type": "fact",
"importance": 0.7,
"expires_at": null,
"action": "created"
}
],
"deduplicated": 0,
"updated": 0
}
}

回應欄位說明:

欄位類型說明
extracted[].actionstringcreated / updated / skipped
deduplicatednumber跳過數量(cosine similarity > 0.98,視為完全重複)
updatednumber更新數量(cosine similarity > 0.90,更新既有記憶)

批次刪除

DELETE /v1/memories

DELETE /v1/memories
Authorization: Bearer sk-your-api-key
Content-Type: application/json
{
"memory_type": "context"
}

memory_type 為選填。不傳則刪除整個 project 下的所有記憶;傳入則只刪除指定類型。

回應:

{
"success": true,
"code": 1000,
"message": "Success",
"data": {
"deleted_count": 15
}
}

錯誤碼

錯誤碼HTTP說明
2100404記憶不存在
2101400記憶數量超過方案上限
2103500LLM 萃取失敗
2104409重複的記憶
2106403記憶功能未啟用(需 Basic+ 方案)

所需權限

操作所需權限
讀取、搜尋memory.read
新增、修改、刪除、萃取memory.write