Puedes usar la configuración de seguridad para ajustar la probabilidad de obtener respuestas que se puedan considerar dañinas. De forma predeterminada, la configuración de seguridad bloquea el contenido con una probabilidad media o alta de ser contenido no seguro en todas las dimensiones.
Gemini Ir a la configuración de seguridad de Imagen Ir a la configuración de seguridad de
Configuración de seguridad para los modelos de Gemini
Haz clic en tu proveedor de Gemini API para ver el contenido y el código específicos del proveedor en esta página. |
Swift
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
Ejemplo con un parámetro de configuración de seguridad:
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)
]
)
// ...
Ejemplo con varios parámetros de configuración de seguridad:
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
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
Ejemplo con un parámetro de configuración de seguridad:
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)
)
)
// ...
Ejemplo con varios parámetros de configuración de seguridad:
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
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
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 */ "IMAGEN_MODEL_NAME",
/* generationConfig is optional */ null,
Collections.singletonList(harassmentSafety)
);
);
// ...
Ejemplo con varios parámetros de configuración de seguridad:
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 */ "IMAGEN_MODEL_NAME",
/* generationConfig is optional */ null,
List.of(harassmentSafety, hateSpeechSafety)
);
);
// ...
Web
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
Ejemplo con un parámetro de configuración de seguridad:
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 });
// ...
Ejemplo con varios parámetros de configuración de seguridad:
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
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
Ejemplo con un parámetro de configuración de seguridad:
// ...
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,
);
// ...
Ejemplo con varios parámetros de configuración de seguridad:
// ...
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
Configuras SafetySettings
cuando creas una instancia de GenerativeModel
.
Ejemplo con un parámetro de configuración de seguridad:
// ...
// 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)
}
);
// ...
Ejemplo con varios parámetros de configuración de seguridad:
// ...
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 }
);
// ...
Configuración de seguridad para los modelos de Imagen
Haz clic en tu proveedor de Gemini API para ver el contenido y el código específicos del proveedor en esta página. |
Obtén información sobre todos los parámetros de configuración de seguridad admitidos y sus valores disponibles para los modelos Imagen en la documentación de Google Cloud.
Swift
Configuras ImagenSafetySettings
cuando creas una instancia de ImagenModel
.
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
Configuras ImagenSafetySettings
cuando creas una instancia de ImagenModel
.
// 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
Configuras ImagenSafetySettings
cuando creas una instancia de ImagenModel
.
// 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
Configuras ImagenSafetySettings
cuando creas una instancia de ImagenModel
.
// ...
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
Configuras ImagenSafetySettings
cuando creas una instancia de ImagenModel
.
// ...
// 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
El uso de Imagen aún no es compatible con Unity, pero vuelve a consultar pronto.
Otras opciones para controlar la generación de contenido
- Obtén más información sobre el diseño de instrucciones para que puedas influir en el modelo y generar resultados específicos para tus necesidades.
- Configura los parámetros del modelo para controlar cómo el modelo genera una respuesta. En el caso de los modelos Gemini, estos parámetros incluyen la cantidad máxima de tokens de salida, la temperatura, Top-K y Top-P. En el caso de los modelos de Imagen, se incluyen la relación de aspecto, la generación de personas, las marcas de agua, etcétera.
- Establece instrucciones del sistema para dirigir el comportamiento del modelo. Esta función es como un preámbulo que agregas antes de que el modelo se exponga a otras instrucciones del usuario final.
- Pasa un esquema de respuesta junto con la instrucción para especificar un esquema de salida específico. Esta función se usa con mayor frecuencia cuando se genera un resultado en formato JSON, pero también se puede usar para tareas de clasificación (como cuando quieres que el modelo use etiquetas o rótulos específicos).