Vous pouvez utiliser les paramètres de sécurité pour ajuster la probabilité d'obtenir des réponses pouvant être considérées comme dangereuses. Par défaut, les paramètres de sécurité bloquent le contenu dont la probabilité de dangerosité est moyenne et/ou élevée pour toutes les dimensions.
Gemini Accéder aux paramètres de sécurité de Imagen Accéder aux paramètres de sécurité de
Paramètres de sécurité pour les modèles Gemini
Cliquez sur votre fournisseur Gemini API pour afficher le contenu et le code spécifiques à ce fournisseur sur cette page. |
Swift
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
Exemple avec un seul paramètre de sécurité:
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)
]
)
// ...
Exemple avec plusieurs paramètres de sécurité:
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
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
Exemple avec un seul paramètre de sécurité:
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)
)
)
// ...
Exemple avec plusieurs paramètres de sécurité:
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
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
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)
);
);
// ...
Exemple avec plusieurs paramètres de sécurité:
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
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
Exemple avec un seul paramètre de sécurité:
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 });
// ...
Exemple avec plusieurs paramètres de sécurité:
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
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
Exemple avec un seul paramètre de sécurité:
// ...
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,
);
// ...
Exemple avec plusieurs paramètres de sécurité:
// ...
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
Vous configurez SafetySettings
lorsque vous créez une instance GenerativeModel
.
Exemple avec un seul paramètre de sécurité:
// ...
// 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)
}
);
// ...
Exemple avec plusieurs paramètres de sécurité:
// ...
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 }
);
// ...
Paramètres de sécurité pour les modèles Imagen
Cliquez sur votre fournisseur Gemini API pour afficher le contenu et le code spécifiques à ce fournisseur sur cette page. |
Pour en savoir plus sur tous les paramètres de sécurité compatibles et leurs valeurs disponibles pour les modèles Imagen, consultez la documentation Google Cloud.
Swift
Vous configurez ImagenSafetySettings
lorsque vous créez une instance ImagenModel
.
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
Vous configurez ImagenSafetySettings
lorsque vous créez une instance ImagenModel
.
// 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
Vous configurez ImagenSafetySettings
lorsque vous créez une instance ImagenModel
.
// 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
Vous configurez ImagenSafetySettings
lorsque vous créez une instance ImagenModel
.
// ...
// 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
Vous configurez ImagenSafetySettings
lorsque vous créez une instance ImagenModel
.
// ...
// 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
L'utilisation de Imagen n'est pas encore compatible avec Unity, mais revenez bientôt.
Autres options pour contrôler la génération de contenu
- Découvrez la conception d'invites pour pouvoir influencer le modèle afin de générer des résultats spécifiques à vos besoins.
- Configurez les paramètres du modèle pour contrôler la manière dont il génère sa réponse. Pour les modèles Gemini, ces paramètres incluent les jetons de sortie maximum, la température, topK et topP. Pour les modèles Imagen, il s'agit notamment du format, de la génération de personnes, du filigranage, etc.
- Définissez des instructions système pour orienter le comportement du modèle. Cette fonctionnalité est comme un préambule que vous ajoutez avant que le modèle ne soit exposé à d'autres instructions de l'utilisateur final.
- Transmettez un schéma de réponse avec la requête pour spécifier un schéma de sortie spécifique. Cette fonctionnalité est généralement utilisée pour générer une sortie JSON, mais elle peut également être utilisée pour des tâches de classification (par exemple, lorsque vous souhaitez que le modèle utilise des libellés ou des balises spécifiques).