偵錯 Gemini Nano

發布日期:2025 年 2 月 28 日

在 Chrome 中,內建 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 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/,然後前往「Event Logs」(事件記錄) 分頁。記錄檔顯示問題出在模型對提示的解讀。模型將輸入內容解讀為字串,而非 JSON:[object Object],[object Object],[object Object]

以下是完整的偵錯訊息,已格式化為易讀的格式:

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 的「事件記錄」分頁,其中包含偵錯資訊。

我們已將這項資訊新增至模型問題的錯誤報告「Prompt API seems to run toString() on JSON input」(提示 API 似乎會對 JSON 輸入內容執行 toString()) ,協助工程團隊找出問題。

提供意見

如要分享偵錯意見,請提交錯誤報告