範本格式、語法和範例


對於 Firebase AI LogicFirebase 控制台提供導覽式 UI,可供您指定範本內容。

伺服器提示詞範本採用以 Dotprompt 為基礎的語法和格式。本頁面提供範本格式和語法的詳細說明,以及 GeminiImagen 的範例。

以下是向 Gemini 模型發出範例要求時,最重要的元件:

---
model: 'gemini-2.5-flash'
---

{{role "system"}}
All output must be a clearly structured invoice document.
Use a tabular or clearly delineated list format for line items.

{{role "user"}}
Create an example customer invoice for a customer named {{customerName}}.
  • 三條虛線內的最上方部分包含模型名稱,以及您要在要求中傳送的任何模型設定、輸入驗證或結構定義 (選用)。會以鍵/值組合的形式寫入,通常稱為 YAML 前置內容

  • 範本主體包含提示。也可以選擇加入系統指令和輸入值 (使用 Handlebars 語法)。


本頁面會詳細說明下列項目的範本格式和語法,並提供範例:



Gemini

本節的所有範例都顯示使用 gemini-2.5-flash 的範本,但您可以搭配 Firebase AI Logic 支援的任何 Gemini 模型 (Gemini Live 模型除外)。

Hello world

以下是伺服器提示範本的最小範例:

設定 (前言)

---
model: 'gemini-2.5-flash'
---

提示和 (如適用) 系統指令

Write a story about a magic backpack.


模型設定

設定模型設定,控管模型生成回覆的方式,例如輸出詞元數量上限、溫度、「前 K 個」和「可能性總和為 P」。

設定 (前言)

---
model: 'gemini-2.5-flash'
config:
  candidateCount: 1
  temperature: 0.9
  topP: 0.1
  topK: 16
  maxOutputTokens: 200
  stopSequences: ["red"]
---

提示和 (如適用) 系統指令

Write a story about a magic backpack.


系統指示

設定系統指令,引導模型行為。請在提示中加入這些資訊:

  • 使用 {{role "system"}} 語法指定系統指示。

  • 使用 {{role "user"}} 語法指定文字提示。

設定 (前言)

---
model: 'gemini-2.5-flash'
---

提示和 (如適用) 系統指令

{{role "system"}}
All output must be a clearly structured invoice document.
Use a tabular or clearly delineated list format for line items.

{{role "user"}}
Create an example customer invoice for a customer.


輸入變數

部分提示是靜態的,但您通常需要在提示中加入使用者的某些資料。

您可以使用 Handlebars 運算式,在提示中加入動態輸入變數。這些運算式會包含在 {{ }} 標記中,格式為 {{variableName}}{{object.propertyName}} (例如 Hello, {{name}} from {{address.city}})。

設定 (前言)

---
model: 'gemini-2.5-flash'
---

提示和 (如適用) 系統指令

Create an example customer invoice for a customer named {{customerName}}.

您可以在範本中提供預設值,但輸入變數的值通常是由用戶端在要求中提供。


控制流程 (迴圈和條件)

如要編寫更複雜的提示,可以使用條件式區塊 (例如 #ifelse#unless) 和疊代 (#each)。

您可以透過特殊 @ 前置字串,以變數形式提供額外情境資訊:

  • @first:疊代 #each 區塊的第一個項目時為 true。
  • @last:疊代 #each 區塊的最後一個項目時為 true。
  • @index:提供目前元素的索引位置 (從零開始)。

如要瞭解所有內建邏輯輔助程式,請參閱 Handlebars 說明文件

設定 (前言)

---
model: 'gemini-2.5-flash'
---

提示和系統指令 (如適用)

Create an example customer invoice for a customer named {{customerName}}.

Include entries for each of the following products

{{#each productNames}}
  {{#if @first}}
  Include line items for the following purchases
  {{/if}}
  - {{this}}
{{/each}}

{{#if isVipCustomer}}
Give the customer a 5% discount.
{{/if}}

請注意,條件式只接受變數參照,不接受任何類型的運算式,例如:

  • 以下做法可行:{{#if isVipCustomer}} ... {{/if}}
  • 以下適用:{{#if customer.type == 'vip'}} ... {{/if}}

如果變數是布林值,條件式就會如預期運作。 如果變數不是布林值,則條件實際上是「is-not-null」檢查。這項功能可用於處理選填輸入內容,例如:

{{#if customerName}}
Hello {{customerName}}
{{else}}
Hello Guest
{{/if}}


輸入驗證和結構定義

如果您有來自用戶端的資料,強烈建議使用輸入結構定義,協助防範提示注入攻擊,並確保要求中傳遞的資料符合您的期望。

  • 如果用戶端未提供值,您可以提供預設值。

  • 結構定義支援純量型別 stringintegernumberbooleanobject。物件、陣列和列舉會以欄位名稱後方的括號表示。

  • 除非您使用 ? 將屬性標示為選用,否則所有屬性都會視為必要屬性。如果屬性標示為選填,也會設為可為空值,讓 LLM 更寬容地傳回空值,而非省略欄位。

以下是提供輸入結構定義的基本範例。下方提供更進階的結構定義。

設定 (前言)

---
model: 'gemini-2.5-flash'
input:
  default:
    isVipCustomer: false
  schema:
    customerName: string, the customers name  # string, number, and boolean types are defined like this
    productNames?(array, list of products to include in the invoice): string  # optional fields are marked with a ?
    isVipCustomer?: boolean, whether or not the customer is a VIP
---

提示和系統指令 (如適用)

Create an example customer invoice for a customer named {{customerName}}.

Include entries for each of the following products

{{#each productNames}}
  {{#if @first}}
  Include line items for the following purchases
  {{/if}}
  - {{this}}
{{/each}}

{{#if isVipCustomer}}
Give the customer a 5% discount.
{{/if}}


輸出結構定義

如要讓模型生成結構化 JSON 輸出內容,可以指定輸出結構定義。指定 format: json 後,模型一律會傳回符合指定結構定義的 JSON 回覆。

  • 結構定義支援純量型別 stringintegernumberbooleanobject。物件、陣列和列舉會以欄位名稱後方的括號表示。

  • 除非您使用 ? 將屬性標示為選用,否則所有屬性都會視為必要屬性。如果屬性標示為選填,也會設為可為空值,讓 LLM 更寬容地傳回空值,而非省略欄位。

以下是生成結構化 JSON 輸出的基本範例。下方提供更進階的結構定義。

設定 (前言)

---
model: gemini-2.5-flash
output:
  format: json
  schema:
    invoiceId: string
    invoiceFile(object, an invoice file):
      url?: string
      contents: string
      mimeType: string
---

提示和系統指令 (如適用)

Create an example customer invoice.


多模態輸入內容

傳送至 Gemini 模型的多模態提示可以包含多種輸入內容,包括檔案 (例如文字和圖片、PDF、純文字檔案、音訊和影片)。

  • 使用 {{media url}} 語法,透過網址提供檔案。

  • 使用 {{media type="mime_type" data="contents"}} 語法提供內嵌檔案。

以下是提供多模態輸入內容的基本範例。下方提供更複雜的範例。

設定 (前言)

---
model: 'gemini-2.5-flash'
---

提示和系統指令 (如適用)

Describe this image

{{media type="mimeType" data="imageData"}}



Imagen (生成圖片)

在初始版本中,伺服器提示詞範本支援使用 Imagen 模型和純文字提示詞生成圖像。我們很快就會推出更多支援功能,包括使用 Imagen 編輯圖片 (使用 Vertex AI Gemini API 時)。

基本

這個範例顯示使用 Imagen 生成圖片的基本範本,其中包含與 Gemini 類似的輸入變數輸入驗證

設定 (前言)

---
model: 'imagen-4.0-generate-001'
input:
  schema:
    prompt: 'string'
---

提示和系統指令 (如適用)

Create an image containing {{prompt}}

進階

這個範例說明如何新增模型設定,以及在提示中使用更多進階功能,例如輸入變數輸入驗證控制流程 (類似 Gemini)。

設定 (前言)

---
model: 'imagen-4.0-fast-generate-001'
config:
  sampleCount: 1
  aspectRatio: "16:9"
  personGeneration: dont_allow
  includeRaiReason: true
input:
  schema:
    style(enum, The style of image): [photo, sketch, painting]
    subject: string, The object or animal or scenery to generate.
    context?: string, Optional background or context description.
  default:
    style: photo
---

提示和 (如適用) 系統指令

A {{style}} of {{subject}}{{#if context}}{{context}}{{/if}}.