您可以使用安全性設定,調整收到可能有害回覆的可能性。根據預設,安全設定會封鎖所有維度中屬於中等和/或高機率有害的內容。
Gemini安全設定 前往Imagen安全設定 前往
Gemini 型號的安全性設定
按一下您的 Gemini API 供應商,即可在本頁查看供應商專屬內容和程式碼。 |
Swift
建立 GenerativeModel
執行個體時,您會設定 SafetySettings
。
以下是包含一個安全設定的範例:
import FirebaseAI
// Initialize the Gemini Developer API backend service
// Create an `GenerativeModel` instance and add safety settings to its config
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
safetySettings: [
SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
]
)
// ...
含有多個安全性設定的範例:
import FirebaseAI
let harassmentSafety = SafetySetting(harmCategory: .harassment, threshold: .blockOnlyHigh)
let hateSpeechSafety = SafetySetting(harmCategory: .hateSpeech, threshold: .blockMediumAndAbove)
// Initialize the Gemini Developer API backend service
// Create an `GenerativeModel` instance and add safety settings to its config
let model = FirebaseAI.firebaseAI(backend: .googleAI()).generativeModel(
modelName: "GEMINI_MODEL_NAME",
safetySettings: [harassmentSafety, hateSpeechSafety]
)
// ...
Kotlin
建立 GenerativeModel
執行個體時,您會設定 SafetySettings
。
以下是包含一個安全設定的範例:
import com.google.firebase.vertexai.type.HarmBlockThreshold
import com.google.firebase.vertexai.type.HarmCategory
import com.google.firebase.vertexai.type.SafetySetting
// Create a `GenerativeModel` instance and add safety settings to its config
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
safetySettings = listOf(
SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)
)
)
// ...
含有多個安全性設定的範例:
import com.google.firebase.vertexai.type.HarmBlockThreshold
import com.google.firebase.vertexai.type.HarmCategory
import com.google.firebase.vertexai.type.SafetySetting
val harassmentSafety = SafetySetting(HarmCategory.HARASSMENT, HarmBlockThreshold.ONLY_HIGH)
val hateSpeechSafety = SafetySetting(HarmCategory.HATE_SPEECH, HarmBlockThreshold.MEDIUM_AND_ABOVE)
// Create a `GenerativeModel` instance and add safety settings to its config
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).generativeModel(
modelName = "GEMINI_MODEL_NAME",
safetySettings = listOf(harassmentSafety, hateSpeechSafety)
)
// ...
Java
建立 GenerativeModel
執行個體時,您會設定 SafetySettings
。
SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
HarmBlockThreshold.ONLY_HIGH);
// Create an `GenerativeModel` instance and add safety settings to its config
GenerativeModelFutures model = GenerativeModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
/* modelName */ "IMAGEN_MODEL_NAME",
/* generationConfig is optional */ null,
Collections.singletonList(harassmentSafety)
);
);
// ...
含有多個安全性設定的範例:
SafetySetting harassmentSafety = new SafetySetting(HarmCategory.HARASSMENT,
HarmBlockThreshold.ONLY_HIGH);
SafetySetting hateSpeechSafety = new SafetySetting(HarmCategory.HATE_SPEECH,
HarmBlockThreshold.MEDIUM_AND_ABOVE);
// Create an `GenerativeModel` instance and add safety settings to its config
GenerativeModelFutures model = GenerativeModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.generativeModel(
/* modelName */ "IMAGEN_MODEL_NAME",
/* generationConfig is optional */ null,
List.of(harassmentSafety, hateSpeechSafety)
);
);
// ...
Web
建立 GenerativeModel
執行個體時,您會設定 SafetySettings
。
以下是包含一個安全設定的範例:
import { HarmBlockThreshold, HarmCategory, getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// ...
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
];
// Create a `GenerativeModel` instance and add safety settings to its config
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", safetySettings });
// ...
含有多個安全性設定的範例:
import { HarmBlockThreshold, HarmCategory, getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai";
// ...
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
const safetySettings = [
{
category: HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH,
},
{
category: HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold: HarmBlockThreshold.BLOCK_MEDIUM_AND_ABOVE,
},
];
// Create a `GenerativeModel` instance and add safety settings to its config
const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", safetySettings });
// ...
Dart
建立 GenerativeModel
執行個體時,請設定 SafetySettings
。
以下是包含一個安全設定的範例:
// ...
final safetySettings = [
SafetySetting(HarmCategory.harassment, HarmBlockThreshold.high)
];
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance and add safety settings to its config
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
safetySettings: safetySettings,
);
// ...
含有多個安全性設定的範例:
// ...
final safetySettings = [
SafetySetting(HarmCategory.harassment, HarmBlockThreshold.high),
SafetySetting(HarmCategory.hateSpeech, HarmBlockThreshold.high),
];
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance and add safety settings to its config
final model = FirebaseAI.googleAI().generativeModel(
model: 'GEMINI_MODEL_NAME',
safetySettings: safetySettings,
);
// ...
Unity
建立 GenerativeModel
執行個體時,您會設定 SafetySettings
。
以下是包含一個安全設定的範例:
// ...
// Initialize the Gemini Developer API backend service
// Create a `GenerativeModel` instance and add safety settings to its config
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
safetySettings: new SafetySetting[] {
new SafetySetting(HarmCategory.Harassment, SafetySetting.HarmBlockThreshold.OnlyHigh)
}
);
// ...
含有多個安全性設定的範例:
// ...
var harassmentSafety = new SafetySetting(HarmCategory.Harassment, SafetySetting.HarmBlockThreshold.OnlyHigh);
var hateSpeechSafety = new SafetySetting(HarmCategory.HateSpeech, SafetySetting.HarmBlockThreshold.MediumAndAbove);
// Initialize the Vertex AI Gemini API backend service
// Create a `GenerativeModel` instance and add safety settings to its config
var ai = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI());
var model = ai.GetGenerativeModel(
modelName: "GEMINI_MODEL_NAME",
safetySettings: new SafetySetting[] { harassmentSafety, hateSpeechSafety }
);
// ...
Imagen 型號的安全性設定
按一下您的 Gemini API 供應商,即可在本頁查看供應商專屬內容和程式碼。 |
如要瞭解 Imagen 型別支援的所有安全設定和可用值,請參閱 Google Cloud 說明文件。
Swift
建立 ImagenModel
執行個體時,您會設定 ImagenSafetySettings
。
import FirebaseAI
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance and add safety settings to its config
let model = FirebaseAI.firebaseAI(backend: .googleAI()).imagenModel(
modelName: "IMAGEN_MODEL_NAME",
safetySettings: ImagenSafetySettings(
safetyFilterLevel: .blockLowAndAbove,
personFilterLevel: .allowAdult
)
)
// ...
Kotlin
建立 ImagenModel
執行個體時,您會設定 ImagenSafetySettings
。
// Initialize the Vertex AI Gemini API backend service
// Create an `ImagenModel` instance and add safety settings to its config
val model = Firebase.ai(backend = GenerativeBackend.googleAI()).imagenModel(
modelName = "IMAGEN_MODEL_NAME",
safetySettings = ImagenSafetySettings(
safetyFilterLevel = ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
personFilterLevel = ImagenPersonFilterLevel.BLOCK_ALL
)
)
// ...
Java
建立 ImagenModel
執行個體時,您會設定 ImagenSafetySettings
。
// Create an `ImagenModel` instance and add safety settings to its config
ImagenModelFutures model = ImagenModelFutures.from(
FirebaseAI.getInstance(GenerativeBackend.googleAI())
.imagenModel(
/* modelName */ "IMAGEN_MODEL_NAME",
/* imageGenerationConfig */ null);
);
// ...
Web
建立 ImagenModel
執行個體時,您會設定 ImagenSafetySettings
。
// ...
// Initialize the Gemini Developer API backend service
const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() });
// Create an `ImagenModel` instance and add safety settings to its config
const model = getImagenModel(
ai,
{
model: "IMAGEN_MODEL_NAME",
safetySettings: {
safetyFilterLevel: ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE,
personFilterLevel: ImagenPersonFilterLevel.ALLOW_ADULT,
}
}
);
// ...
Dart
建立 ImagenModel
執行個體時,請設定 ImagenSafetySettings
。
// ...
// Initialize the Gemini Developer API backend service
// Create an `ImagenModel` instance and add safety settings to its config
final model = FirebaseAI.googleAI().imagenModel(
model: 'IMAGEN_MODEL_NAME',
safetySettings: ImagenSafetySettings(
ImagenSafetyFilterLevel.blockLowAndAbove,
ImagenPersonFilterLevel.allowAdult,
),
);
// ...
Unity
Unity 目前不支援使用 Imagen,但請稍後再回來看看!
其他控管內容產生作業的選項
- 進一步瞭解提示設計,以便影響模型產生符合您需求的輸出內容。
- 設定模型參數,控制模型產生回覆的方式。對於 Gemini 模型,這些參數包括輸出符記數量上限、溫度、topK 和 topP。對於 Imagen 模型,這些參數包括顯示比例、人物生成、浮水印等。
- 設定系統指令,即可引導模型的行為。這項功能就像是前言,可在模型公開給使用者進一步指示之前新增。
- 傳遞回覆結構定義和提示,即可指定特定輸出結構定義。這項功能最常用於產生 JSON 輸出內容,但也可以用於分類作業 (例如希望模型使用特定標籤或標記時)。