안전 설정 이해 및 사용하기

안전 설정을 사용하면 유해하다고 간주될 수 있는 대답을 받을 가능성을 조정할 수 있습니다. 기본적으로 안전 설정은 모든 측정기준에서 안전하지 않은 콘텐츠일 가능성이 중간 또는 높은 콘텐츠를 차단합니다.

Gemini 안전 설정으로 이동 Imagen 안전 설정으로 이동(지원 중단됨)

Gemini 모델의 안전 설정

Gemini API 제공업체를 클릭하여 이 페이지에서 제공업체별 콘텐츠와 코드를 확인합니다.

Gemini Developer API 문서에서 Gemini 모델의 안전 설정에 대해 자세히 알아보세요.

Swift

GenerativeModel 인스턴스를 만들 때 SafetySettings를 구성합니다.

안전 설정이 하나인 예:


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

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 제공업체를 클릭하여 이 페이지에서 제공업체별 콘텐츠와 코드를 확인합니다.

Google Cloud 문서에서 Imagen 모델에 지원되는 모든 안전 설정과 사용 가능한 값에 대해 알아보세요.

Swift

ImagenModel 인스턴스를 만들 때 ImagenSafetySettings를 구성합니다.


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

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 모델의 경우 이러한 파라미터에는 최대 출력 토큰, 온도, Top-K, Top-P가 포함됩니다. Imagen 모델의 경우 여기에는 가로세로 비율, 인물 생성, 워터마크 등이 포함됩니다.
  • 시스템 요청 사항을 설정하여 모델의 동작을 조정합니다. 이 기능은 모델이 최종 사용자의 추가 안내에 노출되기 전에 추가하는 서문과 같습니다.
  • 프롬프트와 함께 대답 스키마를 전달하여 특정 출력 스키마를 지정합니다. 이 기능은 JSON 출력을 생성할 때 가장 일반적으로 사용되지만 분류 작업(예: 모델이 특정 라벨이나 태그를 사용하도록 하려는 경우)에도 사용할 수 있습니다.