为聊天产品按用户隔离记忆
为每个最终用户分配一个 Space,以你系统中的用户 ID 寻址。存入其中的对话会被提炼为只属于该 Space 的记忆;固定到该 Space 的检索和回答不会读取其他内容。
示例
import { Mirabase } from "@mirabase/sdk";
const mb = new Mirabase(); // reads MIRABASE_API_KEY
// A client pinned to the user's Space. The Space is created on the first call.
const user = await mb.spaceFor("user-42", { name: "User 42" });
const receipt = await user.conversations.add(
[
{ role: "user", content: "I am vegetarian, and I prefer short answers." },
{ role: "assistant", content: "Noted." },
],
{ sessionId: "chat-42" },
);
await mb.processing.wait(receipt); // until the memories are distilled
const pack = await user.context("What should I keep in mind when suggesting dinner?");
from mirabase import Mirabase
mb = Mirabase() # reads MIRABASE_API_KEY
# A client pinned to the user's Space. The Space is created on the first call.
user = mb.space_for("user-42", name="User 42")
receipt = user.conversations.add(
[
{"role": "user", "content": "I am vegetarian, and I prefer short answers."},
{"role": "assistant", "content": "Noted."},
],
session_id="chat-42",
)
mb.processing.wait(receipt) # until the memories are distilled
pack = user.context("What should I keep in mind when suggesting dinner?")
通过 HTTP 调用时,PUT https://api.mirabase.io/v2/spaces/ext:user-42 返回该 Space,不存在时创建;此后凡是使用 Space ID 的地方都可以使用 ext:user-42。详见 选择调用的 Space。
边界
- 记忆按 Space 保存。元数据不能隔离记忆,需要在用户之间严格隔离时,请为每个用户使用一个 Space。
- 未指定 Space 的检索、上下文和回答调用会读取该 Key 可读的所有 Space。请使用固定到 Space 的客户端,或在每次调用时指定 Space。
- 对话存入后,记忆在后台提炼,调用返回时尚未生成。请等待处理完成,或稍后读取。
- 调用方具备
memory:confirm时,提炼出的记忆状态为confirmed;否则为candidate,审核前不参与检索。详见 从对话中提炼记忆。 - 账户的 Space 数量有上限,可通过
GET /v2/limits查看。