對於 Firebase AI Logic,Firebase 控制台提供引導式 UI,可供您指定範本內容。
伺服器提示詞範本採用 Dotprompt 語法和格式。本頁提供範本格式和語法的詳細說明,以及相關範例。
以下是向 Gemini 模型傳送範例要求時,最重要的元件:
---
model: 'gemini-3.8-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 frontmatter。
範本主體包含提示詞。也可以選擇加入系統指令和輸入值 (使用 Handlebars 語法)。
本頁詳細說明範本格式和語法,並提供下列範例:
本頁所有範例都顯示使用 gemini-3.8-flash 的範本,但您可以使用 Firebase AI Logic 支援的任何 Gemini 模型 (Gemini Live API 模型除外)。
Hello world
以下是伺服器提示詞範本的基本範例:
設定 (前言)
---
model: 'gemini-3.8-flash'
---
提示詞和系統指令 (如適用)
Write a story about a magic backpack.
控制回覆生成
您可以根據用途和所需的控制程度,以多種方式控制回覆的生成。
模型設定
設定模型設定,控管模型生成回覆的方式,例如輸出詞元數量上限、停止序列等。
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
config:
maxOutputTokens: 200
stopSequences: ["red"]
---
提示詞和系統指令 (如適用)
Write a story about a magic backpack.
思考設定
為支援思考的模型指定思考相關設定。
設定 (frontmatter)
Gemini 3.x和後續模型 (思考程度)
--- model: 'gemini-3.8-flash' config: thinkingConfig: thinkingLevel: medium includeThoughts: true ---Gemini 2.5 模型 (思考預算)
--- model: 'gemini-2.5-flash' config: thinkingConfig: thinkingBudget: 1024 includeThoughts: true ---
提示詞和系統指令 (如適用)
Solve x^2 + 4x + 4 = 0
安全性設定
使用安全性設定,調整模型生成有害回覆的可能性。
設定 (前言)
使用一項安全設定的範例:
---
model: 'gemini-3.8-flash'
config:
safetySettings:
- category: HARM_CATEGORY_HARASSMENT
threshold: BLOCK_ONLY_HIGH
---
使用多項安全性設定的範例:
---
model: 'gemini-3.8-flash'
config:
safetySettings:
- category: HARM_CATEGORY_HARASSMENT
threshold: BLOCK_ONLY_HIGH
- category: HARM_CATEGORY_HATE_SPEECH
threshold: BLOCK_MEDIUM_AND_ABOVE
---
提示詞和系統指令 (如適用)
Write a story about a magic backpack.
系統指示
設定系統指令,引導模型行為。您可以在提示中加入這些內容:
使用
{{role "system"}}語法指定系統指令。使用
{{role "user"}}語法指定文字提示詞。
設定 (前言)
---
model: 'gemini-3.8-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}})。
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
---
提示詞和系統指令 (如適用)
Create an example customer invoice for a customer named {{customerName}}.
您可以在範本中提供預設值,但輸入變數的值通常是由用戶端在要求中提供。
控制流程 (迴圈和條件式)
如要編寫更複雜的提示,可以使用條件區塊 (例如 #if、else 和 #unless) 和疊代 (#each)。
您可以使用特殊 @ 前置字元,以變數形式提供額外情境資訊:
@first:疊代#each區塊的第一個項目時,值為 true。@last:疊代#each區塊的最後一個項目時為 true。@index:提供目前元素的索引位置 (從零開始)。
如要瞭解所有內建邏輯輔助程式,請參閱 Handlebars 說明文件。
設定 (frontmatter)
---
model: 'gemini-3.8-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}}
輸入驗證和結構定義
如果您有來自用戶端的資料,強烈建議使用輸入內容結構定義,防範提示詞注入攻擊,並確保要求中傳遞的資料符合預期。
您可以提供預設值,以防用戶端未提供值。
結構定義支援純量型別
string、integer、number、boolean和object。物件、陣列和列舉會以欄位名稱後方的括號表示。除非您使用
?將屬性標示為選用,否則所有屬性都視為必要。如果屬性標示為選用,也會設為可為空值,讓 LLM 有更多彈性,可傳回空值,而非省略欄位。
以下是提供輸入架構的基本範例。下方提供更進階的結構定義。
設定 (前言)
---
model: 'gemini-3.8-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 回覆。
結構定義支援純量類型
string、integer、number、boolean和object。物件、陣列和列舉會以欄位名稱後方的括號表示。除非您使用
?將屬性標示為選用,否則所有屬性都視為必要。如果屬性標示為選用,也會設為可為空值,讓 LLM 更有彈性,可傳回空值,而非省略欄位。
以下是生成結構化 JSON 輸出的基本範例。下方提供更進階的結構定義。
設定 (前言)
---
model: gemini-3.8-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"}}語法提供內嵌檔案。
基本範例 (多模態輸入)
以下是提供多模態輸入內容的基本範例。下方提供更複雜的範例。
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
---
提示詞和系統指令 (如適用)
Describe this image
{{media type="mimeType" data="imageData"}}
複雜範例 (多模態輸入)
以下是提供多模態輸入的複雜範例。
設定 (前言)
---
model: gemini-3.8-flash
input:
schema:
image_urls?(array, urls of external images): string
inline_images?(array, inline image data):
type: object
properties:
mime_type: string
contents: string # inline data must be base64-encoded
---
提示詞和系統指令 (如適用)
{{role "system"}}
Use the following image as the basis for comparisons
{{media url="http://example.com/reference_img.bmp"}}
{{role "user"}}
What do the following images have in common?
{{#each image_urls}}
{{media url="this"}}
{{/each}}
{{#each inline_images}}
{{media type="mime_type" data="contents"}}
{{/each}}
使用工具
伺服器提示詞範本支援下列工具。
使用
Google Search 建立基準使用
Google Maps 建立基準
如要讓使用者提供額外資訊,以便向模型提出要求,請在伺服器提示詞範本中使用輸入變數,並搭配輸入驗證。
函式呼叫
如需完整指南,請參閱使用伺服器提示詞範本呼叫函式。
執行程式碼
執行程式碼工具可讓模型生成及執行 Python 程式碼。
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
tools:
- codeExecution
---
提示詞和系統指令 (如適用)
What is the sum of the first 50 prime numbers?
Generate and run code for the calculation, and make sure you get all 50.
網址背景資訊
網址情境工具可讓您以網址的形式,為模型提供額外情境。這個範例也說明如何為使用者提供的網址指定輸入驗證。
設定 (前言)
---
model: 'gemini-3.8-flash'
input:
schema:
url1:
type: string
pattern: '^https?://[\w.-]+\.[a-z]{2,}\S*$'
maxLength: 100
url2:
type: string
pattern: '^https?://[\w.-]+\.[a-z]{2,}\S*$'
maxLength: 100
tools:
- urlContext
---
提示詞和系統指令 (如適用)
Compare the ingredients and cooking times from the recipes at {{url1}} and {{url2}}
與「Google Search 」建立基準
以
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
tools:
- googleSearch
---
提示詞和系統指令 (如適用)
Who won the Euro 2024?
使用Google Maps 建立基準
如要使用 googleMaps 工具,請在範本 frontmatter 的 tools 物件中列出該工具。以下範例顯示一些額外的選用設定:
如要設定
googleMaps工具,請在用戶端程式碼中使用TemplateToolConfig提供位置座標和/或語言代碼。
範本範例下方有一個可展開的區段,其中顯示用戶端程式碼範例,說明如何使用這些選用設定。
設定 (frontmatter)
---
model: 'gemini-3.8-flash'
tools:
- googleMaps
input:
schema:
question: string
---
提示詞和系統指令 (如適用)
{{role "system"}}
You are a helpful tour guide. Use the Google Maps tool with the provided coordinates to answer the user's question based on their location.
{{role "user"}}
{{question}}