对于 Firebase AI Logic,Firebase 控制台提供了一个引导式界面,供您 指定模板的内容。
服务器提示模板使用基于 Dotprompt 的语法和格式。在本页中, 您可以找到模板格式和语法的详细说明,以及 Gemini 和 Imagen 的示例。
以下是针对 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 语法)。
本页提供了以下内容的模板格式和语法的详细说明以及示例:
Imagen (已废弃)
Gemini
本部分中的所有示例都展示了使用
gemini-3-flash-preview 的模板,但您可以使用Gemini 支持的任何
Firebase AI Logic 模型(Gemini Live 模型除外)。
你好!世界
以下是服务器提示模板的最小示例:
配置(前言)
---
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}}
工具使用
服务器提示模板支持以下工具。目前尚不支持函数调用,但很快就会推出相应支持! __** yet
如果您希望用户为向模型发出的请求提供任何其他信息,请在服务器提示模板中使用输入变量,并进行输入验证。
代码执行
借助 代码执行 工具,模型可以 生成并运行 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 搜索建立依据
借助“使用 Google 搜索建立依据”工具,模型 可以连接到实时、公开提供的 Web 内容。
配置(前言)
---
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}}.