안전 설정 이해 및 사용하기

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

Gemini 안전 설정으로 이동 Imagen 안전 설정으로 이동

Gemini 모델의 안전 설정

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

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

Swift

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

// ...

Unity

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 모델의 안전 설정

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

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

Swift

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

// ...

Unity

Unity에서는 아직 Imagen 사용이 지원되지 않지만 곧 지원될 예정이니 다시 확인해 주세요.

콘텐츠 생성을 제어하는 기타 옵션

  • 모델에 영향을 주어 필요에 맞는 출력을 생성할 수 있도록 프롬프트 디자인에 대해 자세히 알아보세요.
  • 모델 매개변수를 구성하여 모델의 응답 생성 방식을 제어합니다. Gemini 모델의 경우 이러한 매개변수에는 최대 출력 토큰, 온도, topK, topP가 포함됩니다. Imagen 모델의 경우 여기에는 가로세로 비율, 인물 생성, 워터마킹 등이 포함됩니다.
  • 시스템 안내를 설정하여 모델의 동작을 조정합니다. 이 기능은 모델이 최종 사용자의 추가 안내에 노출되기 전에 추가하는 프리앰블과 같습니다.
  • 프롬프트와 함께 응답 스키마를 전달하여 특정 출력 스키마를 지정합니다. 이 기능은 가장 일반적으로 JSON 출력을 생성할 때 사용되지만 분류 작업(예: 모델이 특정 라벨이나 태그를 사용하도록 하려는 경우)에도 사용할 수 있습니다.