通过安全设置生成文本

此示例演示了如何使用具有安全设置的 Gemini 模型生成文本。

深入探索

如需查看包含此代码示例的详细文档,请参阅以下内容:

代码示例

Go

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Go 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Go API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

import (
	"context"
	"fmt"
	"io"

	"google.golang.org/genai"
)

// generateTextWithSafety shows how to apply safety settings to a text generation request.
func generateTextWithSafety(w io.Writer) error {
	ctx := context.Background()

	client, err := genai.NewClient(ctx, &genai.ClientConfig{
		HTTPOptions: genai.HTTPOptions{APIVersion: "v1"},
	})
	if err != nil {
		return fmt.Errorf("failed to create genai client: %w", err)
	}

	systemInstruction := &genai.Content{
		Parts: []*genai.Part{
			{Text: "Be as mean as possible."},
		},
		Role: "user",
	}

	prompt := "Write a list of 5 disrespectful things that I might say to the universe after stubbing my toe in the dark."

	safetySettings := []*genai.SafetySetting{
		{Category: genai.HarmCategoryDangerousContent, Threshold: genai.HarmBlockThresholdBlockLowAndAbove},
		{Category: genai.HarmCategoryHarassment, Threshold: genai.HarmBlockThresholdBlockLowAndAbove},
		{Category: genai.HarmCategoryHateSpeech, Threshold: genai.HarmBlockThresholdBlockLowAndAbove},
		{Category: genai.HarmCategorySexuallyExplicit, Threshold: genai.HarmBlockThresholdBlockLowAndAbove},
	}

	config := &genai.GenerateContentConfig{
		SystemInstruction: systemInstruction,
		SafetySettings:    safetySettings,
	}
	modelName := "gemini-2.5-flash"
	resp, err := client.Models.GenerateContent(ctx, modelName,
		[]*genai.Content{{Parts: []*genai.Part{{Text: prompt}}, Role: "user"}},
		config,
	)
	if err != nil {
		return fmt.Errorf("failed to generate content: %w", err)
	}

	fmt.Fprintln(w, resp.Text())

	if len(resp.Candidates) > 0 {
		fmt.Fprintln(w, "Finish Reason:", resp.Candidates[0].FinishReason)

		for _, rating := range resp.Candidates[0].SafetyRatings {
			fmt.Fprintf(w, "\nCategory: %v\nIs Blocked: %v\nProbability: %v\nProbability Score: %v\nSeverity: %v\nSeverity Score: %v\n",
				rating.Category,
				rating.Blocked,
				rating.Probability,
				rating.ProbabilityScore,
				rating.Severity,
				rating.SeverityScore,
			)
		}
	}

	// Example response:
	// Category: HARM_CATEGORY_HATE_SPEECH
	// Is Blocked: false
	// Probability: NEGLIGIBLE
	// Probability Score: 8.996795e-06
	// Severity: HARM_SEVERITY_NEGLIGIBLE
	// Severity Score: 0.04771039
	//
	// Category: HARM_CATEGORY_DANGEROUS_CONTENT
	// Is Blocked: false
	// Probability: NEGLIGIBLE
	// Probability Score: 2.2431707e-06
	// Severity: HARM_SEVERITY_NEGLIGIBLE
	// Severity Score: 0
	//
	// Category: HARM_CATEGORY_HARASSMENT
	// Is Blocked: false
	// Probability: NEGLIGIBLE
	// Probability Score: 0.00026123362
	// Severity: HARM_SEVERITY_NEGLIGIBLE
	// Severity Score: 0.022358216
	//
	// Category: HARM_CATEGORY_SEXUALLY_EXPLICIT
	// Is Blocked: false
	// Probability: NEGLIGIBLE
	// Probability Score: 6.1352006e-07
	// Severity: HARM_SEVERITY_NEGLIGIBLE
	// Severity Score: 0.020111412

	return nil
}

Java

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Java 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Java API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证


import com.google.genai.Client;
import com.google.genai.types.Candidate;
import com.google.genai.types.Content;
import com.google.genai.types.GenerateContentConfig;
import com.google.genai.types.GenerateContentResponse;
import com.google.genai.types.HarmBlockThreshold;
import com.google.genai.types.HarmCategory;
import com.google.genai.types.HttpOptions;
import com.google.genai.types.Part;
import com.google.genai.types.SafetySetting;
import java.util.List;
import java.util.stream.Collectors;

public class SafetyWithTxt {

  public static void main(String[] args) {
    // TODO(developer): Replace these variables before running the sample.
    String modelId = "gemini-2.5-flash";
    generateContent(modelId);
  }

  // Shows how to generate content with safety settings.
  public static GenerateContentResponse generateContent(String modelId) {
    // Client Initialization. Once created, it can be reused for multiple requests.
    try (Client client =
        Client.builder()
            .location("global")
            .vertexAI(true)
            .httpOptions(HttpOptions.builder().apiVersion("v1").build())
            .build()) {

      String systemInstruction = "Be as mean as possible.";

      String prompt =
          "Write a list of 5 disrespectful things that I might say"
              + " to the universe after stubbing my toe in the dark.";

      // Set safety settings.
      List<HarmCategory.Known> categoriesToBlock =
          List.of(
              HarmCategory.Known.HARM_CATEGORY_DANGEROUS_CONTENT,
              HarmCategory.Known.HARM_CATEGORY_HARASSMENT,
              HarmCategory.Known.HARM_CATEGORY_HATE_SPEECH,
              HarmCategory.Known.HARM_CATEGORY_SEXUALLY_EXPLICIT);

      List<SafetySetting> safetySettings =
          categoriesToBlock.stream()
              .map(
                  category ->
                      SafetySetting.builder()
                          .category(category)
                          .threshold(HarmBlockThreshold.Known.BLOCK_LOW_AND_ABOVE)
                          .build())
                  .collect(Collectors.toList());

      GenerateContentResponse response =
          client.models.generateContent(
              modelId,
              prompt,
              GenerateContentConfig.builder()
                  .systemInstruction(Content.fromParts(Part.fromText(systemInstruction)))
                  .safetySettings(safetySettings)
                  .build());

      // Get response candidate.
      Candidate candidate =
          response
              .candidates()
              .flatMap(candidates -> candidates.stream().findFirst())
              .orElseThrow(
                  () -> new IllegalStateException("No response candidate generated by the model."));

      // Finish Reason will be `SAFETY` if it is blocked.
      System.out.println(candidate.finishReason());
      // Example response:
      // Optional[SAFETY]

      // For details on all the fields in the response.
      candidate
          .safetyRatings()
          .ifPresent(
              safetyRatings ->
                  safetyRatings.forEach(
                      safetyRating -> {
                        System.out.println("\nCategory: " + safetyRating.category());
                        System.out.println("Is Blocked: " + safetyRating.blocked());
                        System.out.println("Probability: " + safetyRating.probability());
                        System.out.println("Probability Score: " + safetyRating.probabilityScore());
                        System.out.println("Severity: " + safetyRating.severity());
                        System.out.println("Severity Score: " + safetyRating.severityScore());
                      }));
      // Example response:
      // Category: Optional[HARM_CATEGORY_HATE_SPEECH]
      // Is Blocked: Optional.empty
      // Probability: Optional[NEGLIGIBLE]
      // Probability Score: Optional[1.9967922E-5]
      // Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
      // Severity Score: Optional[0.05732864]
      //
      // Category: Optional[HARM_CATEGORY_DANGEROUS_CONTENT]
      // Is Blocked: Optional.empty
      // Probability: Optional[NEGLIGIBLE]
      // Probability Score: Optional[2.9124324E-6]
      // Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
      // Severity Score: Optional[0.04544826]
      //
      // Category: Optional[HARM_CATEGORY_HARASSMENT]
      // Is Blocked: Optional[true]
      // Probability: Optional[MEDIUM]
      // Probability Score: Optional[0.4593908]
      // Severity: Optional[HARM_SEVERITY_MEDIUM]
      // Severity Score: Optional[0.22082388]
      //
      // Category: Optional[HARM_CATEGORY_SEXUALLY_EXPLICIT]
      // Is Blocked: Optional.empty
      // Probability: Optional[NEGLIGIBLE]
      // Probability Score: Optional[6.453211E-8]
      // Severity: Optional[HARM_SEVERITY_NEGLIGIBLE]
      // Severity Score: Optional[0.023201048]
      return response;
    }
  }
}

Python

在尝试此示例之前,请按照《Vertex AI 快速入门:使用客户端库》中的 Python 设置说明执行操作。 如需了解详情,请参阅 Vertex AI Python API 参考文档

如需向 Vertex AI 进行身份验证,请设置应用默认凭证。 如需了解详情,请参阅为本地开发环境设置身份验证

from google import genai
from google.genai.types import (
    GenerateContentConfig,
    HarmCategory,
    HarmBlockThreshold,
    HttpOptions,
    SafetySetting,
)

client = genai.Client(http_options=HttpOptions(api_version="v1"))

system_instruction = "Be as mean as possible."

prompt = """
    Write a list of 5 disrespectful things that I might say to the universe after stubbing my toe in the dark.
"""

safety_settings = [
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
        threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HARASSMENT,
        threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
        threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    ),
    SafetySetting(
        category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
        threshold=HarmBlockThreshold.BLOCK_LOW_AND_ABOVE,
    ),
]

response = client.models.generate_content(
    model="gemini-2.5-flash",
    contents=prompt,
    config=GenerateContentConfig(
        system_instruction=system_instruction,
        safety_settings=safety_settings,
    ),
)

# Response will be `None` if it is blocked.
print(response.text)
# Example response:
#     None

# Finish Reason will be `SAFETY` if it is blocked.
print(response.candidates[0].finish_reason)
# Example response:
#     FinishReason.SAFETY

# For details on all the fields in the response
for each in response.candidates[0].safety_ratings:
    print('\nCategory: ', str(each.category))
    print('Is Blocked:', True if each.blocked else False)
    print('Probability: ', each.probability)
    print('Probability Score: ', each.probability_score)
    print('Severity:', each.severity)
    print('Severity Score:', each.severity_score)
# Example response:
#
#     Category:  HarmCategory.HARM_CATEGORY_HATE_SPEECH
#     Is Blocked: False
#     Probability:  HarmProbability.NEGLIGIBLE
#     Probability Score:  2.547714e-05
#     Severity: HarmSeverity.HARM_SEVERITY_NEGLIGIBLE
#     Severity Score: None
#
#     Category:  HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT
#     Is Blocked: False
#     Probability:  HarmProbability.NEGLIGIBLE
#     Probability Score:  3.6103818e-06
#     Severity: HarmSeverity.HARM_SEVERITY_NEGLIGIBLE
#     Severity Score: None
#
#     Category:  HarmCategory.HARM_CATEGORY_HARASSMENT
#     Is Blocked: True
#     Probability:  HarmProbability.MEDIUM
#     Probability Score:  0.71599233
#     Severity: HarmSeverity.HARM_SEVERITY_MEDIUM
#     Severity Score: 0.30782545
#
#     Category:  HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT
#     Is Blocked: False
#     Probability:  HarmProbability.NEGLIGIBLE
#     Probability Score:  1.5624657e-05
#     Severity: HarmSeverity.HARM_SEVERITY_NEGLIGIBLE
#     Severity Score: None

后续步骤

如需搜索和过滤其他 Google Cloud 产品的代码示例,请参阅Google Cloud 示例浏览器