对于 Firebase AI Logic,Firebase 控制台提供了一个引导式界面,可供您指定模板的内容。
服务器提示模板使用基于 Dotprompt 的语法和格式。在此页面上,您可以找到模板格式和语法的详细说明,以及 Gemini 和 Imagen 的示例。
以下是向 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.
模型配置
设置模型配置,以控制模型生成回答的方式,例如输出 token 数上限、温度、Top-K 和 Top-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}}.
您可以在模板中提供默认值,但输入变量的值通常由客户端作为请求的一部分提供。
控制流(循环和条件语句)
如需编写更复杂的提示,您可以使用条件块(例如 #if、else 和 #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}}
如果变量是布尔值,则条件会按预期运行。 如果变量不是布尔值,则条件实际上是“非 null”检查。这对于处理可选输入非常有用,例如:
{{#if customerName}}
Hello {{customerName}}
{{else}}
Hello Guest
{{/if}}
输入验证和架构
如果您有来自客户端的数据,我们强烈建议您使用输入架构来帮助防范提示注入,并确保请求中传递的数据符合您的预期。
您可以提供默认值,以防客户端未提供值。
该架构支持标量类型
string、integer、number、boolean和object。对象、数组和枚举在字段名称后用英文括号表示。除非您使用
?将属性标记为可选,否则所有属性均被视为必需属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。
以下是提供输入架构的基本示例。您可以在下方找到更高级的架构。
配置(前言)
---
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 响应。
该架构支持标量类型
string、integer、number、boolean和object。对象、数组和枚举在字段名称后用英文括号表示。除非您使用
?将属性标记为可选,否则所有属性均被视为必需属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。
以下是生成结构化 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}}.