记忆管理 API
使用 ConvaiRestClient.Memory 为用户-角色对列出、添加、检索和删除记忆记录——包括设置、全部五个方法、响应类型和错误处理。
最后更新于
这有帮助吗?
这有帮助吗?
using Convai.RestAPI;
using Convai.RestAPI.Internal;
using System.Collections.Generic;
using UnityEngine;
public class MemoryInspector : MonoBehaviour
{
[ContextMenu("List All Memories")]
private async void ListAllMemories()
{
string characterId = "your-character-id";
string endUserId = "target-end-user-id";
using var client = new ConvaiRestClient(ConvaiSettings.Instance.ApiKey);
int page = 1;
bool hasMore = true;
var allMemories = new List<MemoryRecord>();
while (hasMore)
{
var response = await client.Memory.ListAsync(characterId, endUserId, page, pageSize: 50);
allMemories.AddRange(response.Memories);
hasMore = response.HasMore;
page++;
}
foreach (var record in allMemories)
{
Debug.Log($"[{record.Id}] {record.Memory}");
}
}
}var record = await client.Memory.GetAsync(characterId, endUserId, memoryId);
Debug.Log($"Memory: {record.Memory}");var facts = new List<string>
{
"Jordan 是 Northfield 设施的夜班安全官员。",
"Jordan 已完成受限空间进入认证。",
"Jordan 更喜欢实用示例,而不是理论性很强的讲解。"
};
var response = await client.Memory.AddAsync(characterId, endUserId, facts);
foreach (var result in response.Memories)
{
Debug.Log($"[{result.Event}] {result.Memory} (id: {result.Id})");
}var result = await client.Memory.DeleteAsync(characterId, endUserId, memoryId);
Debug.Log($"Deleted: {result.Deleted}");var result = await client.Memory.DeleteAllAsync(characterId, endUserId);
Debug.Log($"All memories deleted for user {result.EndUserId} on character {result.CharacterId}.");try
{
var response = await client.Memory.ListAsync(characterId, endUserId);
// 处理响应
}
catch (ConvaiRestException ex)
{
Debug.LogError($"Memory API error {ex.StatusCode}: {ex.Message}");
}