تنظیمات ایمنی را درک کرده و از آن استفاده کنید

شما می‌توانید از تنظیمات ایمنی برای تنظیم احتمال دریافت پاسخ‌هایی که ممکن است مضر تلقی شوند، استفاده کنید. به طور پیش‌فرض، تنظیمات ایمنی، محتوایی را که احتمال ناامن بودن آن متوسط ​​و/یا زیاد است، در تمام ابعاد مسدود می‌کند.

پرش به تنظیمات ایمنی پرش به تنظیمات ایمنی Imagen

تنظیمات ایمنی برای مدل‌های Gemini

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API Gemini خود کلیک کنید.

برای کسب اطلاعات بیشتر در مورد تنظیمات ایمنی مدل‌های Gemini ، به مستندات رابط برنامه‌نویسی نرم‌افزار Gemini Developer مراجعه کنید.

سویفت

شما SafetySettings را هنگام ایجاد یک نمونه GenerativeModel پیکربندی می‌کنید.

مثال با یک تنظیم ایمنی:


import FirebaseAILogic

// 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 FirebaseAILogic

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

شما SafetySettings را هنگام ایجاد یک نمونه GenerativeModel پیکربندی می‌کنید.

مثال با یک تنظیم ایمنی:


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

شما SafetySettings را هنگام ایجاد یک نمونه 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 */ "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

شما SafetySettings را هنگام ایجاد یک نمونه GenerativeModel پیکربندی می‌کنید.

مثال با یک تنظیم ایمنی:


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

شما SafetySettings را هنگام ایجاد یک نمونه GenerativeModel پیکربندی می‌کنید.

مثال با یک تنظیم ایمنی:


// ...

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,
);

// ...

وحدت

شما SafetySettings را هنگام ایجاد یک نمونه GenerativeModel پیکربندی می‌کنید.

مثال با یک تنظیم ایمنی:


// ...

// 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

برای مشاهده محتوا و کد مخصوص ارائه‌دهنده در این صفحه، روی ارائه‌دهنده API Gemini خود کلیک کنید.

در مستندات Google Cloud با تمام تنظیمات ایمنی پشتیبانی‌شده و مقادیر موجود آنها برای مدل‌های Imagen آشنا شوید.

سویفت

شما ImagenSafetySettings را هنگام ایجاد یک نمونه ImagenModel پیکربندی می‌کنید.


import FirebaseAILogic

// 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

شما ImagenSafetySettings را هنگام ایجاد یک نمونه 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

شما ImagenSafetySettings را هنگام ایجاد یک نمونه 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

شما ImagenSafetySettings را هنگام ایجاد یک نمونه 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

شما ImagenSafetySettings را هنگام ایجاد یک نمونه 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,
  ),
);

// ...

وحدت

شما ImagenSafetySettings را هنگام ایجاد یک نمونه ImagenModel پیکربندی می‌کنید.


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 ، این پارامترها شامل حداکثر توکن‌های خروجی، دما، topK و topP می‌شوند. برای مدل‌های Imagen ، این پارامترها شامل نسبت ابعاد، تولید شخص، واترمارک و غیره می‌شوند.
  • دستورالعمل‌های سیستم را برای هدایت رفتار مدل تنظیم کنید. این ویژگی مانند مقدمه‌ای است که قبل از اینکه مدل در معرض هرگونه دستورالعمل دیگری از کاربر نهایی قرار گیرد، اضافه می‌کنید.
  • یک طرحواره پاسخ را به همراه اعلان ارسال کنید تا یک طرحواره خروجی خاص مشخص شود. این ویژگی معمولاً هنگام تولید خروجی JSON استفاده می‌شود، اما می‌تواند برای وظایف طبقه‌بندی نیز مورد استفاده قرار گیرد (مانند زمانی که می‌خواهید مدل از برچسب‌ها یا تگ‌های خاصی استفاده کند).