안전 설정 이해 및 사용하기

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


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

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

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

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

// ...

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

// ...

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

  • 모델이 필요에 맞는 출력을 생성하도록 영향을 미칠 수 있도록 프롬프트 디자인 에 관해 자세히 알아보세요.
  • 모델이 대답을 생성하는 방식을 제어하도록 모델 매개변수 을(를) 구성합니다. 최대 출력 토큰, 반복된 출력 토큰의 확률 등
  • 모델의 동작을 조정하도록 시스템 요청 사항 을 설정합니다. 이 기능은 모델이 최종 사용자의 추가 요청 사항에 노출되기 전에 추가하는 프리앰블과 같습니다.
  • 프롬프트와 함께 대답 스키마 를 전달하여 특정 출력 스키마를 지정합니다. 이 기능은 JSON 출력을 생성할 때 가장 일반적으로 사용되지만 분류 작업에도 사용할 수 있습니다 (예: 모델이 특정 라벨 또는 태그를 사용하도록 하려는 경우).