对于 Firebase AI Logic,Firebase 控制台提供了一个引导式界面,供您指定模板的内容。
服务器提示模板使用基于 Dotprompt 的语法和格式。在此页面上,您可以找到模板格式和语法的详细说明以及示例。
以下是向 Gemini 模型发出请求时最重要的组成部分:
---
model: 'gemini-3-flash-preview'
---
{{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-3-flash-preview 的模板,但您可以使用 Firebase AI Logic 支持的任何 Gemini 模型(Gemini Live 模型除外)。
Hello World
以下是服务器提示模板的简短示例:
配置(前言)
---
model: 'gemini-3-flash-preview'
---
提示和(如适用)系统指令
Write a story about a magic backpack.
控制回答生成
您可以根据自己的使用情形和所需的控制程度,通过多种方式控制回答的生成。
模型配置
设置模型配置,以控制模型生成回答的方式,例如输出 token 数上限、温度、Top-K 和 Top-P。
配置(前言)
---
model: 'gemini-3-flash-preview'
config:
candidateCount: 1
temperature: 0.9
topP: 0.1
topK: 16
maxOutputTokens: 200
stopSequences: ["red"]
---
提示和(如适用)系统指令
Write a story about a magic backpack.
思考配置
为支持思考的模型指定与思考相关的配置。
配置(前言)
Gemini 3 及更高版本的模型(思考等级)
--- model: 'gemini-3-flash-preview' config: thinkingConfig: thinkingLevel: medium includeThoughts: true ---Gemini 2.5 个模型(思考预算)
--- model: 'gemini-3-flash-preview' config: thinkingConfig: thinkingBudget: 1024 includeThoughts: true ---
提示和(如适用)系统指令
Solve x^2 + 4x + 4 = 0
安全设置
使用安全设置来调整获得可能被视为有害的回答的可能性。
配置(前言)
包含一项安全设置的示例:
---
model: 'gemini-3-flash-preview'
config:
safetySettings:
- category: HARM_CATEGORY_HARASSMENT
threshold: BLOCK_ONLY_HIGH
---
包含多项安全设置的示例:
---
model: 'gemini-3-flash-preview'
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-flash-preview'
---
提示和(如适用)系统指令
{{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-3-flash-preview'
---
提示和(如适用)系统指令
Create an example customer invoice for a customer named {{customerName}}.
您可以在模板中提供默认值,但输入变量的值通常由客户端作为请求的一部分提供。
控制流(循环和条件语句)
如需编写更复杂的提示,您可以使用条件块(例如 #if、else 和 #unless)和迭代 (#each)。
您可以提供其他上下文信息作为带有特殊 @ 前缀的变量:
@first:在迭代#each代码块的第一个项时为 true。@last:在迭代#each块的最后一项时为 true。@index:提供当前元素的索引位置(从零开始)。
如需了解所有内置逻辑帮助程序,请参阅 Handlebars 文档。
配置(前言)
---
model: 'gemini-3-flash-preview'
---
提示和(如适用)系统指令
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}}
如果变量是布尔值,则条件会按预期运行。 如果变量不是布尔值,则条件实际上是“非 null”检查。这对于处理可选输入非常有用,例如:
{{#if customerName}}
Hello {{customerName}}
{{else}}
Hello Guest
{{/if}}
输入验证和架构
如果您有来自客户端的数据,我们强烈建议您使用输入架构来帮助防范提示注入,并确保请求中传递的数据符合您的预期。
您可以提供默认值,以防客户端未提供值。
该架构支持标量类型
string、integer、number、boolean和object。对象、数组和枚举在字段名称后用英文括号表示。除非您使用
?将属性标记为可选,否则所有属性均被视为必需属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。
以下是提供输入架构的基本示例。您可以在下方找到更高级的架构。
配置(前言)
---
model: 'gemini-3-flash-preview'
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。对象、数组和枚举在字段名称后用英文括号表示。除非您使用
?将属性标记为可选,否则所有属性均被视为必需属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。
下面是一个生成结构化 JSON 输出的基本示例。您可以在下方找到更高级的架构。
配置(前言)
---
model: gemini-3-flash-preview
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-3-flash-preview'
---
提示和(如适用)系统指令
Describe this image
{{media type="mimeType" data="imageData"}}
复杂示例(多模态输入)
下面是一个提供多模态输入的更复杂示例。
配置(前言)
---
model: gemini-3-flash-preview
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}}
工具使用
服务器提示模板支持以下工具。
如果您希望用户为向模型发出的请求提供任何其他信息,请在服务器提示模板中使用输入变量,同时使用输入验证。
函数调用
如需查看完整指南,请参阅使用服务器提示模板进行函数调用。
代码执行
借助代码执行工具,模型可以生成和运行 Python 代码。
配置(前言)
---
model: 'gemini-3-flash-preview'
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-flash-preview'
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 进行接地
依托
配置(前言)
---
model: 'gemini-3-flash-preview'
tools:
- googleSearch
---
提示和(如适用)系统指令
Who won the Euro 2024?
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
safetySetting: block_medium_and_above
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}}.