模板格式、语法和示例


对于 Firebase AI LogicFirebase 控制台提供了一个引导式界面,供您 指定模板的内容。

服务器提示模板使用基于 Dotprompt 的语法和格式。在本页中,您可以找到模板格式和语法的详细说明以及示例。

以下是向 Gemini 模型发出示例请求时最重要的组件:

---
model: 'gemini-3.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-3.5-flash 的模板,但您可以使用 Gemini 支持的任何 Firebase AI Logic 模型(Gemini Live 模型除外)。

你好!世界

以下是服务器提示模板的最小示例:

配置(前言)

---
model: 'gemini-3.5-flash'
---

提示和(如适用)系统指令

Write a story about a magic backpack.



控制回答的生成

您可以根据自己的使用场景和所需的控制级别,以多种方式控制回答的生成。

模型配置

设置 模型配置 以控制模型生成回答的方式,例如回答变体的数量 (candidateCount)、输出 token 上限等。

配置(前言)

---
model: 'gemini-3.5-flash'
config:
  candidateCount: 1
  maxOutputTokens: 200
  stopSequences: ["red"]
---

提示和(如适用)系统指令

Write a story about a magic backpack.


思考配置

为支持思考的模型指定与思考相关的配置。

配置(前言)

  • Gemini 3.x 及更高版本的模型(思考级别)

    ---
    model: 'gemini-3.5-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.5-flash'
config:
  safetySettings:
    - category: HARM_CATEGORY_HARASSMENT
      threshold: BLOCK_ONLY_HIGH
---

包含多项安全设置的示例:

---
model: 'gemini-3.5-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.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-3.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-3.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}}



输入验证和架构

如果您有来自客户端的数据,我们强烈建议您使用输入架构来帮助防范提示注入,并确保请求中传递的数据符合您的预期。

  • 您可以提供默认值,以防客户端未提供值。

  • 该架构支持标量类型 stringintegernumberbooleanobject。对象、数组和枚举在字段名称后用英文括号表示。

  • 除非您使用 ? 将某个属性标记为可选属性,否则所有属性都被视为必填属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。

以下是提供输入架构的基本示例 。您可以在下方找到更高级的架构。

配置(前言)

---
model: 'gemini-3.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。对象、数组和枚举在字段名称后用英文括号表示。

  • 除非您使用 ? 将某个属性标记为可选属性,否则所有属性都被视为必填属性。当某个属性被标记为可选属性时,它也会设为可为 null,以便 LLM 更宽松地返回 null,而不是省略某个字段。

以下是生成结构化 JSON 输出的基本示例 。您可以在下方找到更高级的架构。

配置(前言)

---
model: gemini-3.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-3.5-flash'
---

提示和(如适用)系统指令

Describe this image

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

复杂示例(多模态输入)

以下是提供多模态输入的更复杂示例

配置(前言)

---
model: gemini-3.5-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}}



工具使用

服务器提示模板支持以下工具。

如果您希望用户为向模型发出的请求提供任何其他信息,请在服务器提示模板中使用输入变量,并进行输入验证

函数调用

请参阅使用服务器提示模板进行 函数调用的完整指南。

代码执行

借助 代码执行 工具,模型可以 生成并运行 Python 代码。

配置(前言)

---
model: 'gemini-3.5-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.5-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 工具可将模型与实时公开提供的 Web 内容连接起来。

配置(前言)

---
model: 'gemini-3.5-flash'
tools:
  - googleSearch
---

提示和(如适用)系统指令

Who won the Euro 2024?

依托 Google Maps 进行接地

依托 Google 地图Google Maps 进行接地工具 可为模型提供地理空间数据,以实现位置感知功能。

如需使用 googleMaps 工具,请在模板前言的 tools 对象中列出该工具。以下示例展示了一些额外的可选配置:

在示例模板下方,找到一个 可展开的示例,其中展示了 使用这些可选配置的客户端代码示例。

配置(前言)

---
model: 'gemini-3.5-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}}



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}}.