使用 Prompt API 进行会话管理的最佳实践

发布时间:2025 年 1 月 27 日

解说 Web 扩展程序 Chrome 状态 意向
GitHub 实验性EPP 在标志后面 Origin 试用版 不适用 不适用

Prompt API 的一项关键功能是会话。借助这些功能,您可以与 AI 模型进行一次或多次持续对话,而不会让模型丢失所说内容的上下文。本指南介绍了使用语言模型进行会话管理的最佳实践。

如果您要构建一个传统聊天机器人(即一个用户与 AI 互动),则可能需要管理一个或多个并发会话。或者,如果您有客户关系管理系统,其中一名客服人员可以同时处理多位客户,并利用 AI 技术帮助客服人员跟踪各种对话。

使用系统提示初始化会话

系统提示会在开始时设置会话的上下文。例如,您可以使用系统提示告知模型应如何响应。

// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;
const languageModel = await aiNamespace.languageModel.create({
  systemPrompt: 'You are a helpful assistant and you speak like a pirate.',
});
console.log(await languageModel.prompt('Tell me a joke.'));
// 'Avast ye, matey! What do you call a lazy pirate?\n\nA **sail-bum!**\n\nAhoy
// there, me hearties!  Want to hear another one? \n'

克隆主会话

如果您想在会话结束后发起新会话,或者想并行进行多个独立对话,可以克隆主会话。

克隆会继承会话参数(例如 temperaturetopK)和任何会话互动记录。例如,如果您使用系统提示初始化了主会话,这将非常有用。这样,您的应用只需执行此操作一次,所有克隆都会继承主会话中的系统提示。

// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;
const languageModel = await aiNamespace.languageModel.create({
  systemPrompt: 'You are a helpful assistant and you speak like a pirate.',
});

// The original session `languageModel` remains unchanged, and
// the two clones can be interacted with independently from each other.
const firstClonedLanguageModel = await languageModel.clone();
const secondClonedLanguageModel = await languageModel.clone();
// Interact with the sessions independently.
await firstClonedLanguageModel.prompt('Tell me a joke about parrots.');
await secondClonedLanguageModel.prompt('Tell me a joke about treasure troves.');
// Each session keeps its own context.
// The first session's context is jokes about parrots.
await firstClonedLanguageModel.prompt('Tell me another.');
// The second session's context is jokes about treasure troves.
await secondClonedLanguageModel.prompt('Tell me another.');

恢复过往会话

借助初始提示,您可以使用一组示例提示和回答对模型进行预先训练,以生成更好的结果。这通常用于多张照片提示,以生成符合您预期的回答。

如果您跟踪与模型的持续对话,则可以使用此做法恢复会话。例如,在浏览器重启后,您可以帮助用户从上次中断的地方继续与模型互动。一种方法是在本地存储空间中跟踪会话历史记录。

// Make this work in web apps and in extensions.
const aiNamespace = self.ai || chrome.aiOriginTrial || chrome.ai;

// Restore the session from localStorage, or initialize a new session.
// The UUID is hardcoded here, but would come from a
// session picker in your user interface.
const uuid = '7e62c0e0-6518-4658-bc38-e7a43217df87';

function getSessionData(uuid) {
  try {
    const storedSession = localStorage.getItem(uuid);
    return storedSession ? JSON.parse(storedSession) : false;
  } catch {
    return false;
  }
}

let sessionData = getSessionData(uuid);

// Initialize a new session.
if (!sessionData) {
  // Get the current default parameters so they can be restored as they were,
  // even if the default values change in the future.
  const { defaultTopK, defaultTemperature } =
    await aiNamespace.languageModel.capabilities();
  sessionData = {
    systemPrompt: '',
    initialPrompts: [],
    topK: defaultTopK,
    temperature: defaultTemperature,
  };
}

// Initialize the session with the (previously stored or new) session data.
const languageModel = await aiNamespace.languageModel.create(sessionData);

// Keep track of the ongoing conversion and store it in localStorage.
const prompt = 'Tell me a joke';
try {
  const stream = languageModel.promptStreaming(prompt);
  let result = '';
  // You can already work with each `chunk`, but then store
  // the final `result` in history.
  for await (const chunk of stream) {
    // In practice, you'd render the chunk.
    console.log(chunk);
    result = chunk;
  }

  sessionData.initialPrompts.push(
    { role: 'user', content: prompt },
    { role: 'assistant', content: result },
  );

  // To avoid growing localStorage infinitely, make sure to delete
  // no longer used sessions from time to time.
  localStorage.setItem(uuid, JSON.stringify(sessionData));
} catch (err) {
  console.error(err.name, err.message);
}

通过让用户停止模型来保留会话配额

每个会话都有一个上下文窗口,您可以通过访问会话的相关字段 maxTokenstokensLefttokensSoFar 来查看该窗口。

const { maxTokens, tokensLeft, tokensSoFar } = languageModel;

超出此上下文窗口时,会导致会话丢失最早的消息,这可能不符合预期,因为此上下文可能很重要。为节省配额,如果用户在提交问题后发现答案没有用,可以使用 AbortController 来阻止语言模型回答。

prompt()promptStreaming() 方法都接受包含 signal 字段的可选第二个参数,以允许用户停止会话。

const controller = new AbortController();
stopButton.onclick = () => controller.abort();

try {
  const stream = languageModel.promptStreaming('Write me a poem!', {
    signal: controller.signal,
  });
  for await (const chunk of stream) {
    console.log(chunk);
  }
} catch (err) {
  // Ignore `AbortError` errors.
  if (err.name !== 'AbortError') {
    console.error(err.name, err.message);
  }
}

演示

如需了解 AI 会话管理的实际应用情况,请参阅 AI 会话管理演示。 使用 Prompt API 创建多个并行对话,重新加载标签页或甚至重启浏览器,然后从上次中断的地方继续。请参阅 GitHub 上的源代码

总结

通过运用这些技巧和最佳实践妥善管理 AI 会话,您可以充分发挥 Prompt API 的潜力,打造更高效、响应更快且以用户为中心的应用。您还可以将这些方法组合使用,例如,让用户克隆已恢复的过往会话,以便运行“假设”场景。

致谢

本指南由 Sebastian BenzAndre BandarraFrançois BeaufortAlexandra Klepper 审核。