调试 Gemini Nano

发布时间:2025 年 2 月 28 日

内置 AI 依赖于 Gemini Nano 来对所有 API 执行推理。有时,Gemini Nano 可能会返回错误消息,或者无法返回您预期的结果。您可以查看使用 Gemini Nano 的所有内置 AI API 的调试信息。其中包括 Prompt API、Summarizer API、Writer API 和 Rewriter API。

  1. 打开 Chrome,然后前往 chrome://on-device-internals
  2. 选择事件日志
  3. (可选)点击转储以下载包含所有事件信息的 JSON 文件。

您可以报告错误,以便我们在实现中解决此错误。

示例:调试 Prompt API

例如,在以下会话中,用户向 Prompt API 请求了押韵词。

const session = await ai.languageModel.create({
  systemPrompt: "You are an API endpoint that returns rhymes as JSON for an input word."
});

await session.prompt([
  { role: "user", content: "house" },  
  { role: "assistant", content: "{\"input\": \"house\", \"output\": \"mouse\"}" },
  { role: "user", content: "file"},  
]);

模型的回答如下所示,格式经过调整以方便阅读:

```json
[]
```

**Reasoning:**

The input you provided (empty arrays) is an empty list or array in JSON format.
When you try to find rhymes for an empty list, you're essentially looking for
words that rhyme with nothing.


Therefore, there are no rhymes to return. The JSON response will be an empty
array `[]`."

为什么模型没有返回包含押韵词的 JSON 消息(理想情况下是 {"input": "file", "output": "pile"})?虽然在撰写本文时尚未实现结构化输出,但响应至少应以某种方式执行韵律匹配任务。

如需调试此错误,请访问 chrome://on-device-internals/,然后前往事件日志标签页。日志显示问题出在模型对问题的解读上。模型将输入理解为字符串 [object Object],[object Object],[object Object],而不是 JSON。

以下是经过格式设置以便于阅读的完整调试消息:

Executing model with input context of 0 tokens:
<system>You are an API endpoint that returns rhymes as JSON for an input word.<end>
with string: <user>[object Object],[object Object],[object Object]<end> <model>

包含调试信息的特殊页面 chrome://on-device-internals 的“事件日志”标签页。

我们将此信息添加到模型问题的 bug 中(Prompt API 似乎会对 JSON 输入运行 toString()),这有助于工程团队找出问题。

分享反馈

通过提交bug 报告分享您的调试反馈。