您可以使用安全设置来调整获得可能被视为有害的回答的可能性。默认情况下,安全设置会屏蔽在所有维度中不安全概率为中等和/或较高的内容。
跳转到 Gemini 安全设置 跳转到 Imagen 安全设置
Gemini 模型的安全设置
| 点击您的 Gemini API 提供商,以查看此页面上特定于提供商的内容和代码。 |
Swift
您可以在创建 GenerativeModel 实例时配置 SafetySettings。
包含一项安全设置的示例:
import FirebaseAI // Specify the safety settings as part of creating the `GenerativeModel` instance 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) // Specify the safety settings as part of creating the `GenerativeModel` instance 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 // Specify the safety settings as part of creating the `GenerativeModel` instance 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) // Specify the safety settings as part of creating the `GenerativeModel` instance 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); // Specify the safety settings as part of creating the `GenerativeModel` instance GenerativeModelFutures model = GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( /* modelName */ "GEMINI_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); // Specify the safety settings as part of creating the `GenerativeModel` instance GenerativeModelFutures model = GenerativeModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .generativeModel( /* modelName */ "GEMINI_MODEL_NAME", /* generationConfig is optional */ null, List.of(harassmentSafety, hateSpeechSafety) ); ); // ... Web
您可以在创建 GenerativeModel 实例时配置 SafetySettings。
包含一项安全设置的示例:
import { HarmBlockThreshold, HarmCategory, getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai"; // ... const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() }); const safetySettings = [ { category: HarmCategory.HARM_CATEGORY_HARASSMENT, threshold: HarmBlockThreshold.BLOCK_ONLY_HIGH, }, ]; // Specify the safety settings as part of creating the `GenerativeModel` instance const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", safetySettings }); // ... 包含多项安全设置的示例:
import { HarmBlockThreshold, HarmCategory, getAI, getGenerativeModel, GoogleAIBackend } from "firebase/ai"; // ... 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, }, ]; // Specify the safety settings as part of creating the `GenerativeModel` instance const model = getGenerativeModel(ai, { model: "GEMINI_MODEL_NAME", safetySettings }); // ... Dart
您可以在创建 GenerativeModel 实例时配置 SafetySettings。
包含一项安全设置的示例:
// ... final safetySettings = [ SafetySetting(HarmCategory.harassment, HarmBlockThreshold.high) ]; // Specify the safety settings as part of creating the `GenerativeModel` instance final model = FirebaseAI.googleAI().generativeModel( model: 'GEMINI_MODEL_NAME', safetySettings: safetySettings, ); // ... 包含多项安全设置的示例:
// ... final safetySettings = [ SafetySetting(HarmCategory.harassment, HarmBlockThreshold.high), SafetySetting(HarmCategory.hateSpeech, HarmBlockThreshold.high), ]; // Specify the safety settings as part of creating the `GenerativeModel` instance final model = FirebaseAI.googleAI().generativeModel( model: 'GEMINI_MODEL_NAME', safetySettings: safetySettings, ); // ... Unity
您可以在创建 GenerativeModel 实例时配置 SafetySettings。
包含一项安全设置的示例:
// ... // Specify the safety settings as part of creating the `GenerativeModel` instance 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); // Specify the safety settings as part of creating the `GenerativeModel` instance 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 // Specify the safety settings as part of creating the `ImagenModel` instance let model = FirebaseAI.firebaseAI(backend: .googleAI()).imagenModel( modelName: "IMAGEN_MODEL_NAME", safetySettings: ImagenSafetySettings( safetyFilterLevel: .blockLowAndAbove, personFilterLevel: .allowAdult ) ) // ... Kotlin
您可以在创建 ImagenModel 实例时配置 ImagenSafetySettings。
// Specify the safety settings as part of creating the `ImagenModel` instance 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。
// Specify the safety settings as part of creating the `ImagenModel` instance ImagenModelFutures model = ImagenModelFutures.from( FirebaseAI.getInstance(GenerativeBackend.googleAI()) .imagenModel( /* modelName */ "IMAGEN_MODEL_NAME", /* imageGenerationConfig */ null); ); // ... Web
您可以在创建 ImagenModel 实例时配置 ImagenSafetySettings。
// ... const ai = getAI(firebaseApp, { backend: new GoogleAIBackend() }); // Specify the safety settings as part of creating the `ImagenModel` instance const model = getImagenModel( ai, { model: "IMAGEN_MODEL_NAME", safetySettings: { safetyFilterLevel: ImagenSafetyFilterLevel.BLOCK_LOW_AND_ABOVE, personFilterLevel: ImagenPersonFilterLevel.ALLOW_ADULT, } } ); // ... Dart
您可以在创建 ImagenModel 实例时配置 ImagenSafetySettings。
// ... // Specify the safety settings as part of creating the `ImagenModel` instance final model = FirebaseAI.googleAI().imagenModel( model: 'IMAGEN_MODEL_NAME', safetySettings: ImagenSafetySettings( ImagenSafetyFilterLevel.blockLowAndAbove, ImagenPersonFilterLevel.allowAdult, ), ); // ... Unity
您可以在创建 ImagenModel 实例时配置 ImagenSafetySettings。
using Firebase.AI; // Specify the safety settings as part of creating the `ImagenModel` instance var model = FirebaseAI.GetInstance(FirebaseAI.Backend.GoogleAI()).GetImagenModel( modelName: "IMAGEN_MODEL_NAME", safetySettings: new ImagenSafetySettings( safetyFilterLevel: ImagenSafetySettings.SafetyFilterLevel.BlockLowAndAbove, personFilterLevel: ImagenSafetySettings.PersonFilterLevel.AllowAdult ) ); // ... 控制内容生成的其他选项
- 详细了解提示设计,以便影响模型生成符合您需求的输出内容。
- 配置模型参数,以控制模型如何生成回答。对于 Gemini 模型,这些参数包括输出 token 数上限、温度、topK 和 topP。 对于 Imagen 模型,这些包括宽高比、人物生成、添加水印等。
- 设置系统指令以引导模型的行为。此功能就像一段“序言”,在模型接收到最终用户的进一步指令之前添加。
- 传递回答架构以及提示,以指定特定的输出架构。此功能最常用于生成 JSON 输出,但也可用于分类任务(例如,当您希望模型使用特定标签时)。