می توانید از تنظیمات ایمنی برای تنظیم احتمال دریافت پاسخ هایی که ممکن است مضر تلقی شوند استفاده کنید. بهطور پیشفرض، تنظیمات ایمنی محتوایی را با احتمال متوسط و/یا زیاد ناامن بودن محتوا در همه ابعاد مسدود میکند.
Gemini پرش به تنظیمات ایمنی Imagen پرش به تنظیمات ایمنی
تنظیمات ایمنی برای مدل های Gemini
برای مشاهده محتوا و کد ارائه دهنده خاص در این صفحه، روی ارائه دهنده API Gemini خود کلیک کنید. |
سویفت
هنگام ایجاد یک نمونه 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,
);
// ...
وحدت
هنگام ایجاد یک نمونه 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
برای مشاهده محتوا و کد ارائه دهنده خاص در این صفحه، روی ارائه دهنده API Gemini خود کلیک کنید. |
درباره تمام تنظیمات ایمنی پشتیبانی شده و مقادیر موجود آنها برای مدل های Imagen در اسناد Google Cloud بیاموزید.
سویفت
هنگامی که یک نمونه 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,
),
);
// ...
وحدت
استفاده از Imagen هنوز برای Unity پشتیبانی نمی شود، اما به زودی دوباره بررسی کنید!
گزینه های دیگر برای کنترل تولید محتوا
- در مورد طراحی سریع بیشتر بیاموزید تا بتوانید مدل را تحت تأثیر قرار دهید تا خروجی خاصی برای نیازهای شما ایجاد کند.
- پارامترهای مدل را برای کنترل نحوه ایجاد پاسخ توسط مدل پیکربندی کنید. برای مدلهای Gemini ، این پارامترها شامل حداکثر توکنهای خروجی، دما، topK و topP هستند. برای مدل های Imagen ، این موارد شامل نسبت ابعاد، تولید شخص، واترمارک و غیره است.
- دستورالعمل های سیستم را برای هدایت رفتار مدل تنظیم کنید. این ویژگی مانند مقدمه ای است که قبل از اینکه مدل در معرض دستورالعمل های بیشتر از کاربر نهایی قرار گیرد، اضافه می کنید.
- یک طرح پاسخ را همراه با اعلان برای تعیین یک طرح خروجی خاص ارسال کنید. این ویژگی بیشتر هنگام تولید خروجی JSON استفاده میشود، اما میتوان از آن برای کارهای طبقهبندی نیز استفاده کرد (مانند زمانی که میخواهید مدل از برچسبها یا برچسبهای خاصی استفاده کند).