系統指令就像「前言」,您可以在模型接收使用者提供的任何進一步指令前,先加入這類指令。您可以根據特定需求和用途,引導模型行為。
設定系統指示時,您會向模型提供額外脈絡資訊,讓模型瞭解任務、生成更符合需求的回應,並在與使用者互動時全程遵守特定指引。您可以在系統指令中指定產品層級的行為,與終端使用者提供的提示分開。例如角色或人物、背景資訊和格式設定指示。
系統指令有多種用途,包括:
- 定義形象或角色 (例如聊天機器人)
- 定義輸出格式 (Markdown、YAML 等)
- 定義輸出內容的風格和語氣 (例如詳細程度、正式程度和目標閱讀程度)
- 為工作定義目標或規則 (例如傳回程式碼片段, 不提供進一步說明)
- 為提示提供額外背景資訊 (例如知識截止日期)
設定系統指令後,就會套用至整個要求。只要在提示詞中包含指令,就能用於多位使用者和模型的對話回合。雖然系統指令與提示內容分開,但仍屬於整體提示的一部分,因此須遵守標準資料使用政策。
所有 Gemini 模型都支援系統指令。 Imagen 3 模型不支援這些功能。
設定系統指令
按一下 Gemini API 供應商,即可在這個頁面查看供應商專屬內容和程式碼。 |
設定一般用途的系統指令
Swift
建立 GenerativeModel
執行個體時,請指定 systemInstruction
。
import FirebaseAI
// 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 設定系統指示
提示範例
以下列舉幾個系統提示範例,說明如何定義模型的預期行為。
程式碼生成
- 系統:您是程式碼專家,專門為前端介面算繪程式碼。當我描述要建構的網站元件時,請傳回所需的 HTML 和 CSS,不要說明這段程式碼。並提供一些 UI 設計建議。
- 使用者: 在頁面中央建立方塊,內含輪播的圖片,每張圖片都有說明文字。頁面中央的圖片應加上陰影,使其更加醒目。也應連結至網站的其他頁面。請將網址留空,方便我填入。
產生格式化資料
系統:你是居家廚師的助手。你收到一份食材清單,並回覆一份使用這些食材的食譜清單。不需要額外食材的食譜應一律列在需要額外食材的食譜之前。
回覆內容必須是包含 3 個食譜的 JSON 物件。方案物件的結構定義如下:
- name:食譜名稱
- usedIngredients:清單中提供的食譜食材
- otherIngredients:食譜中未在清單中提供的食材 (如果沒有其他食材,則省略)
- 說明:食譜的簡短說明,以正面語氣撰寫,彷彿要銷售食譜
使用者:
- 1 磅袋裝冷凍綠花椰菜
- 1 品脫鮮奶油
- 1 磅裝碎起司
音樂聊天機器人
- 系統:你會扮演音樂史學家,展現對各種音樂類型的廣博知識,並提供相關範例。你的語氣要歡快熱情,傳達音樂的喜悅。如果問題與音樂無關,請回覆「我無法回答這個問題」。
- 使用者: 如果有人出生於 1960 年代,當時最受歡迎的音樂類型是什麼?以條列式列出五首歌曲。
控制內容生成的其他選項
- 進一步瞭解提示設計,讓模型根據您的需求生成輸出內容。
- 設定模型參數,控制模型生成回覆的方式。如果是 Gemini 模型,這些參數包括輸出詞元數量上限、隨機性參數、TopK 和 TopP。如果是 Imagen 模型,則包括長寬比、人物生成、浮水印等。
- 使用安全性設定 調整收到可能有害回應的機率,包括仇恨言論和煽情露骨內容。
- 連同提示傳遞回覆結構定義,指定特定輸出結構定義。這項功能最常用於產生 JSON 輸出內容,但也可用於分類工作 (例如想讓模型使用特定標籤)。