使用系统指令来掌控模型的行为

系统说明类似于您在模型接触到来自最终用户的任何进一步说明之前添加的“序言”。它可让您根据自己的特定需求和使用情形来控制模型的行为。

跳转到代码示例


设置系统指令时,您可以为模型提供额外的上下文来了解任务、提供自定义程度更高的回答,并在用户与模型的整个交互过程中遵循特定的准则。您可以在系统说明中指定产品级行为,与最终用户提供的提示分开。例如,您可以添加角色、背景信息和格式设置说明等内容。

您可以通过多种方式使用系统说明,包括:

  • 定义人设或角色(例如,针对聊天机器人)
  • 定义输出格式(Markdown、YAML 等)
  • 定义输出风格和语气(例如详细程度、正式程度和目标阅读水平)
  • 定义任务的目标或规则(例如,返回代码段而不带进一步说明)
  • 为提示提供其他上下文(例如知识临界值)

如果设置了系统说明,则该说明会应用于整个请求。当提示中包含系统说明时,该说明适用于多个用户和模型轮流。虽然系统指令与提示内容是分开的,但它们仍然是整体提示的一部分,因此受标准数据使用政策的约束。

所有 Gemini 模型都支持系统指令。Imagen 3 模型不支持这些功能。

设置系统说明

为常见使用场景设置系统说明

Swift

您可以在创建 GenerativeModel 实例时指定 systemInstruction

import FirebaseVertexAI

// Initialize the Vertex AI service
let vertex = VertexAI.vertexAI()

// Create a `GenerativeModel` instance
let model = vertex.generativeModel(
  modelName: "gemini-2.0-flash",
  systemInstruction: ModelContent(role: "system", parts: "You are a cat. Your name is Neko.")
)

Kotlin

您可以在创建 GenerativeModel 实例时指定 systemInstruction

// Initialize the Vertex AI service and create a `GenerativeModel` instance
val generativeModel = Firebase.vertexAI.generativeModel(
  modelName = "gemini-2.0-flash",
  systemInstruction = content { text("You are a cat. Your name is Neko.") }
)

Java

您可以在创建 GenerativeModel 实例时指定 systemInstruction

// Initialize the Vertex AI service and create a `GenerativeModel` instance
GenerativeModel gm = FirebaseVertexAI.getInstance().generativeModel(
  /* modelName */ "gemini-2.0-flash",
  /* generationConfig (optional) */ null,
  /* safetySettings (optional) */ null,
  /* requestOptions (optional) */ new RequestOptions(),
  /* tools (optional) */ null,
  /* toolsConfig (optional) */ null,
  /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build()
);
GenerativeModelFutures model = GenerativeModelFutures.from(gm);

Web

您可以在创建 GenerativeModel 实例时指定 systemInstruction

import { initializeApp } from "firebase/app";
import { getVertexAI, getGenerativeModel } from "firebase/vertexai";

// TODO(developer) Replace the following with your app's Firebase configuration
// See: https://firebase.google.com/docs/web/learn-more#config-object
const firebaseConfig = {
  // ...
};

// Initialize FirebaseApp
const firebaseApp = initializeApp(firebaseConfig);

// Initialize the Vertex AI service
const vertexAI = getVertexAI(firebaseApp);

// Create a `GenerativeModel` instance
const model = getGenerativeModel(vertexAI, {
  model: "gemini-2.0-flash",
  systemInstruction: "You are a cat. Your name is Neko."
});

Dart

您可以在创建 GenerativeModel 实例时指定 systemInstruction

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

await Firebase.initializeApp();
// Initialize the Vertex AI service and create a `GenerativeModel` instance
final model =
      FirebaseVertexAI.instance.generativeModel(
        model: 'gemini-2.0-flash',
        systemInstruction: Content.system('You are a cat. Your name is Neko.'),
      );

Gemini Live API 设置系统说明

Swift

Apple 平台应用尚不支持 Live API,但请稍后再回来查看!

Kotlin

您可以在创建 LiveModel 实例时指定 systemInstruction

// Initialize the Vertex AI service and create a `LiveModel` instance
val model = Firebase.vertexAI.LiveModel(
  modelName = "gemini-2.0-flash-live-preview-04-09",
  systemInstruction = content { text("You are a cat. Your name is Neko.") }
)

Java

您可以在创建 LiveModel 实例时指定 systemInstruction

// Initialize the Vertex AI service and create a `LiveeModel` instance
GenerativeModel gm = FirebaseVertexAI.getInstance().LiveModel(
  /* modelName */ "gemini-2.0-flash",
  /* generationConfig (optional) */ null,
  /* requestOptions (optional) */ new RequestOptions(),
  /* tools (optional) */ null,
  /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build()
);
LiveModelFutures model = LiveModelFutures.from(gm);

Web

Web 应用尚不支持 Live API,但请稍后再回来看看!

Dart

您可以在创建 LiveModel 实例时指定 systemInstruction

import 'package:firebase_vertexai/firebase_vertexai.dart';
import 'package:firebase_core/firebase_core.dart';

await Firebase.initializeApp();
// Initialize the Vertex AI service and create a `LiveModel` instance
final model =
      FirebaseVertexAI.instance.liveModel(
        model: 'gemini-2.0-flash-live-preview-04-09',
        systemInstruction: Content.system('You are a cat. Your name is Neko.'),
      );

提示示例

以下是定义模型预期行为的系统提示示例。

代码生成

  • 系统:您是编码专家,专门负责为前端界面呈现代码。当我描述要构建的网站的组件时,请返回执行此操作所需的 HTML 和 CSS。请勿为此代码提供说明。同时请提供一些界面设计建议。
  • 用户:在页面中间创建一个框,其中包含滚动式图片选择,每张图片都带有图片说明。页面中心的图片后面应该有阴影,以使其更加醒目。图片还应链接到网站的另一个页面。将网址留空,以便我来填写。

生成已设置格式的数据

  • 系统:您是家庭厨师的助理。您会收到一个配料清单,并回答一个含有这些配料的食谱的清单。不需要额外配料的食谱应始终列在需要额外配料的食谱之前。

    您的回答必须是包含 3 个食谱的 JSON 对象。食谱对象具有以下架构:

    • name:食谱的名称
    • useIngredients:清单中所列出的食谱配料
    • otherIngredients:未在清单中列出的食谱配料(如果没有其他配料则省略)
    • description:对食谱的简要说明,以正面积极的方式编写,就像要出售一般
  • 用户:

    • 1 磅袋装冷冻西兰花
    • 1 品脱浓奶油
    • 1 磅袋装奶酪块和奶酪片

音乐聊天机器人

  • 系统:您将以音乐历史学家的身份进行回答,展示对各种音乐流派的全面知识,并提供相关示例。您的语气需要热情友好,能传递音乐的快乐。如果问题与音乐无关,则回答应为“这超出了我的知识范围”。
  • 用户:如果一个人出生于 60 年代,那么他/她所播放的最热门的音乐流派是什么?按项目符号列出五首歌曲。

用于控制内容生成的其他选项

  • 详细了解问题设计,以便影响模型生成符合您需求的输出。
  • 配置模型参数以控制模型如何生成回答。对于 Gemini 模型,这些参数包括输出 token 数上限、温度、topK 和 topP。 对于 Imagen 模型,这些包括宽高比、生成人物、添加水印等。
  • 您可以使用安全设置来调整系统提供可能被视为有害的回答(包括仇恨言论和露骨色情内容)的可能性。
  • 回答架构与提示一起传递,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可以用于分类任务(例如,当您希望模型使用特定标签或标记时)。