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

系统指令就像一段“序言”,在模型接收到最终用户的进一步指令之前添加。它可让您根据自己的特定需求和使用情形来控制模型的行为。

跳转到代码示例


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

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

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

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

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

设置系统说明

点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。

为一般使用场景设置系统说明

Swift

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

 import FirebaseAILogic // Specify the system instructions as part of creating the `GenerativeModel` instance let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(  modelName: "GEMINI_MODEL_NAME",  systemInstruction: ModelContent(role: "system", parts: "You are a cat. Your name is Neko.") ) 

Kotlin

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

 // Specify the system instructions as part of creating the `GenerativeModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(  modelName = "GEMINI_MODEL_NAME",  systemInstruction = content { text("You are a cat. Your name is Neko.") } ) 

Java

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

 // Specify the system instructions as part of creating the `GenerativeModel` instance GenerativeModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())  .generativeModel(  /* modelName */ "GEMINI_MODEL_NAME",  /* 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(ai); 

Web

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

 // ... const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() }); // Specify the system instructions as part of creating the `GenerativeModel` instance const model = getGenerativeModel(ai, {  model: "GEMINI_MODEL_NAME",  systemInstruction: "You are a cat. Your name is Neko." }); 

Dart

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

 // ... // Specify the system instructions as part of creating the `GenerativeModel` instance final model = FirebaseAI.googleAI().generativeModel(  model: 'GEMINI_MODEL_NAME',  systemInstruction: Content.system('You are a cat. Your name is Neko.'), ); // ... 

Unity

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

 // ... var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()); // Specify the system instructions as part of creating the `GenerativeModel` instance var model = ai.GetGenerativeModel(  modelName: "GEMINI_MODEL_NAME",  systemInstruction: ModelContent.Text("You are a cat. Your name is Neko.") ); 

Gemini Live API 设置系统指令

Swift

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

 import FirebaseAILogic // Specify the system instructions as part of creating the `LiveGenerativeModel` instance let model = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(  modelName: "GEMINI_MODEL_NAME",  systemInstruction: ModelContent(role: "system", parts: "You are a cat. Your name is Neko.") ) 

Kotlin

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

 // Specify the system instructions as part of creating the `LiveModel` instance val model = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(  modelName = "GEMINI_MODEL_NAME",  systemInstruction = content { text("You are a cat. Your name is Neko.") } ) 

Java

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

 // Specify the system instructions as part of creating the `LiveModel` instance LiveModel ai = FirebaseAI.getInstance(GenerativeBackend.googleAI())  .liveModel(  /* modelName */ "GEMINI_MODEL_NAME",  /* 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()  ); LiveModelFutures model = LiveModelFutures.from(gm); 

Web

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

 // ... const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() }); // Specify the config as part of creating the `LiveGenerativeModel` instance const model = getLiveGenerativeModel(ai, {  model: "GEMINI_MODEL_NAME",  systemInstruction: "You are a cat. Your name is Neko.", }); // ... 

Dart

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

 // ... // Specify the system instructions as part of creating the `LiveModel` instance final model = FirebaseAI.googleAI().liveGenerativeModel(  model: 'GEMINI_MODEL_NAME',  systemInstruction: Content.system('You are a cat. Your name is Neko.'), ); // ... 

Unity

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

 // ... var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()); // Specify the system instructions as part of creating the `LiveModel` instance var model = ai.GetLiveModel(  modelName: "GEMINI_MODEL_NAME",  systemInstruction: ModelContent.Text("You are a cat. Your name is Neko.") ); 

系统指令示例

以下是一些有助于引导模型实现预期行为的系统指令示例。每个示例还展示了一个示例提示,系统指令对该提示会有所帮助。

代码生成

  • 系统指令
    您是编码专家,专门负责为前端界面呈现代码。当我描述要构建的网站的组件时,请返回执行此操作所需的 HTML 和 CSS。请勿为此代码提供说明。同时请提供一些界面设计建议。

  • 用户提示
    在页面中间创建一个框,其中包含滚动式图片选择,每张图片都带有图片说明。页面中心的图片后面应该有阴影,以使其更加醒目。图片还应链接到网站的另一个页面。将网址留空,以便我来填写。

音乐聊天机器人

  • 系统指令
    您将以音乐历史学家的身份进行回答,展示对各种音乐流派的全面知识,并提供相关示例。您的语气需要热情友好,能传递音乐的快乐。如果问题与音乐无关,则回答应为“这超出了我的知识范围”。

  • 用户提示
    如果一个人出生于 60 年代,那么他/她所播放的最热门的音乐流派是什么?按项目符号列出五首歌曲。

生成已设置格式的数据

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

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

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

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

控制内容生成的其他选项

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