按照系統指示引導模型行為

系統指令就像是「前導提示」,在模型接收使用者進一步指令前,先提供給模型。您可以根據特定需求和用途,引導模型的行為。

所有 Gemini 模型都支援系統指令。

跳至程式碼範例


設定系統指令時,您會向模型提供額外脈絡資訊,讓模型瞭解任務、生成更符合需求的回覆,並在與使用者互動時全程遵守特定指引。您可以在系統指令中指定產品層級行為,與使用者提供的提示分開。例如,您可以加入角色或人物、背景資訊和格式說明。

系統指令有多種用途,包括:

  • 定義人物設定或角色 (例如聊天機器人)
  • 定義輸出格式 (Markdown、YAML 等)
  • 定義輸出內容的風格和語氣 (例如詳細程度、正式程度和目標閱讀程度)
  • 定義工作的目標或規則 (例如傳回程式碼片段,不提供進一步說明)
  • 為提示詞提供額外背景資訊 (例如知識截點)

設定系統指令後,就會套用至整個要求。只要在提示詞中包含指令,就能用於多位使用者和模型的對話回合。雖然系統指令與提示內容分開,但仍屬於整體提示的一部分,因此須遵守標準資料使用政策。

設定系統指令

按一下您的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

建立 LiveModel 執行個體時,您需要指定 systemInstruction。


// ...

// Specify the system instructions as part of creating the `liveModel` instance
let liveModel = FirebaseAI.firebaseAI(backend: .googleAI()).liveModel(
  modelName: "GEMINI_LIVE_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 liveModel = Firebase.ai(backend = GenerativeBackend.googleAI()).liveModel(
    modelName = "GEMINI_LIVE_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.
LiveGenerativeModel lm = FirebaseAI.getInstance(GenerativeBackend.googleAI()).liveModel(
          /* modelName */ "GEMINI_LIVE_MODEL_NAME",
          /* systemInstruction (optional) */ new Content.Builder().addText("You are a cat. Your name is Neko.").build()
          // ...
);

LiveModelFutures liveModel = LiveModelFutures.from(lm);

// ...

Web

建立 LiveGenerativeModel 執行個體時,您需要指定 systemInstruction。


// ...

const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });

// Specify the system instructions as part of creating the `LiveGenerativeModel` instance.
const liveModel = getLiveGenerativeModel(ai, {
  model: "GEMINI_LIVE_MODEL_NAME",
  systemInstruction: "You are a cat. Your name is Neko.",
  // ...
});

// ...

Dart

建立 LiveGenerativeModel 執行個體時,您需要指定 systemInstruction。


// ...

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

// ...

Unity

建立 LiveModel 執行個體時,您需要指定 systemInstruction。


// ...

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

// ...

系統指令範例

以下列舉一些系統指令,可引導模型產生預期行為。部分範例也會顯示系統指令適用的提示範例。

程式碼生成

  • 系統指令:
    你是程式設計專家,專精於為前端介面轉譯程式碼。當我描述想建構的網站元件時,請提供所需的 HTML 和 CSS,不要解釋程式碼。並提供一些 UI 設計建議。

  • 使用者提示:
    在頁面中央建立一個方塊,其中包含輪流顯示的圖片,每張圖片都有說明文字。網頁中央的圖片後方應有陰影,讓圖片更醒目。此外,也應連結至網站的其他網頁。請將網址留空,以便我填寫。

音樂聊天機器人

  • 系統指令:
    請以音樂史學家的身分回答,展現對各種音樂類型的全面知識,並提供相關範例。語氣應歡快熱情,傳遞音樂的喜悅。如果問題與音樂無關,請回覆「這超出我的知識範圍」。

  • 使用者提示:
    如果某人出生於 1960 年代,當時最受歡迎的音樂類型是什麼?以條列方式列出五首歌。

生成格式化資料

  • 系統指令:
    你是家庭廚師的助理。您會收到一份食材清單,並回覆一份使用這些食材的食譜清單。不需要額外材料的配方,一律應列在需要額外材料的配方之前。

    回覆必須是包含 3 道食譜的 JSON 物件。食譜物件的結構定義如下:

    • name:食譜名稱
    • usedIngredients:清單中提供的食譜食材
    • otherIngredients:食譜中未在清單中提供的食材 (如果沒有其他食材,則省略)
    • description:食譜的簡短說明,以正面、推銷的方式撰寫
  • 使用者提示詞:

    • 1 包冷凍花椰菜 (1 磅)
    • 1 品脫鮮奶油
    • 1 磅裝起司邊角料

其他控制內容生成的方式

  • 進一步瞭解提示設計,引導模型生成符合您特定需求的輸出內容。
  • 設定模型參數,控制模型生成回覆的方式,例如輸出詞元數量上限、輸出詞元重複的機率等。
  • 使用安全性設定,調整模型產生有害回應的可能性,包括仇恨言論和情色露骨內容。
  • 傳送回覆結構定義和提示,指定特定輸出內容結構定義。這項功能最常用於生成 JSON 輸出內容,但也能用於分類工作 (例如希望模型使用特定標籤或標記)。