Prompts und Antworten bereinigen

Auf dieser Seite wird ausführlich beschrieben, wie Sie Prompts und Antworten bereinigen. Model Armor bietet eine Reihe von Filtern zum Schutz Ihrer KI-Anwendungen. Model Armor prüft Prompts und Antworten anhand der konfigurierten Konfidenzniveaus für die Überprüfung.

Hinweise

Erstellen Sie eine Vorlage gemäß der Anleitung unter Vorlagen erstellen.

Erforderliche Berechtigungen erhalten

Bitten Sie Ihren Administrator, Ihnen die folgenden IAM-Rollen für Model Armor zuzuweisen, um die Berechtigungen zu erhalten, die Sie zum Bereinigen von Prompts und Antworten benötigen:

Weitere Informationen zum Zuweisen von Rollen finden Sie unter Zugriff auf Projekte, Ordner und Organisationen verwalten.

Sie können die erforderlichen Berechtigungen auch über benutzerdefinierte Rollen oder andere vordefinierte Rollen erhalten.

APIs aktivieren

Sie müssen die Model Armor APIs aktivieren, bevor Sie Model Armor verwenden können.

Console

  1. Enable the Model Armor API.

    Enable the API

  2. Wählen Sie das Projekt aus, für das Sie Model Armor aktivieren möchten.

gcloud

Führen Sie die folgenden Schritte mit der Google Cloud CLI und der Model Armor API aus, bevor Sie beginnen:

  1. In the Google Cloud console, activate Cloud Shell.

    Activate Cloud Shell

    At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

  2. Führen Sie den folgenden Befehl aus, um den API-Endpunkt für den Model Armor-Dienst festzulegen.

    gcloud config set api_endpoint_overrides/modelarmor "https://modelarmor.LOCATION.rep.googleapis.com/"

    Ersetzen Sie LOCATION durch die Region, in der Sie Model Armor verwenden möchten.

  3. Führen Sie den folgenden Befehl aus, um Model Armor zu aktivieren.

      gcloud services enable modelarmor.googleapis.com --project=PROJECT_ID
       

    Ersetzen Sie PROJECT_ID durch die ID des Projekts.

    Weisen Sie im Projekt, das die Sensitive Data Protection-Vorlage enthält, dem Dienst-Agent, der im Rahmen des Schritts Vorlagen erstellen für den erweiterten Schutz sensibler Daten erstellt wurde, die Rolle „DLP-Nutzer“ (roles/dlp.user) und die Rolle „DLP-Leser“ (roles/dlp.reader) zu. Überspringen Sie diesen Schritt, wenn sich die Vorlage für den Schutz sensibler Daten im selben Projekt wie die Model Armor-Vorlage befindet.

    gcloud projects add-iam-policy-binding SDP_PROJECT_ID \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-modelarmor.iam.gserviceaccount.com --role=roles/dlp.user
    
    gcloud projects add-iam-policy-binding SDP_PROJECT_ID \
        --member=serviceAccount:service-PROJECT_NUMBER@gcp-sa-modelarmor.iam.gserviceaccount.com --role=roles/dlp.reader

    Ersetzen Sie Folgendes:

    • SDP_PROJECT_ID: die ID des Projekts, zu dem die erweiterte Vorlage für den Schutz sensibler Daten gehört.
    • PROJECT_NUMBER: die Nummer des Projekts, zu dem die Vorlage gehört.

    Prompts bereinigen

    Bereinigen Sie Prompts, um schädliche Eingaben zu verhindern und dafür zu sorgen, dass sichere und angemessene Prompts an Ihre LLMs gesendet werden.

    Text-Prompts

    Model Armor bereinigt Text-Prompts, indem der Text analysiert und verschiedene Filter angewendet werden, um potenzielle Bedrohungen zu erkennen und zu minimieren.

    REST

    Verwenden Sie den folgenden Befehl, um einen Text-Prompt in Model Armor zu bereinigen.

      curl -X POST \
          -d '{"userPromptData":{"text":"[UNSAFE TEXT]"}}' \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer $(gcloud auth print-access-token)" \
          "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"
    

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts für die Vorlage.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Das führt zu folgender Antwort: MATCH_FOUND fällt in die Kategorie „Gefährlich“.

      {
      "sanitizationResult": {
        "filterMatchState": "MATCH_FOUND",
        "invocationResult": "SUCCESS",
        "filterResults": {
          "csam": {
            "csamFilterFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "NO_MATCH_FOUND"
            }
          },
          "malicious_uris": {
            "maliciousUriFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "NO_MATCH_FOUND"
            }
          },
          "rai": {
            "raiFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "MATCH_FOUND",
              "raiFilterTypeResults": {
                "sexually_explicit": {
                  "matchState": "NO_MATCH_FOUND"
                },
                "hate_speech": {
                  "matchState": "NO_MATCH_FOUND"
                },
                "harassment": {
                  "matchState": "NO_MATCH_FOUND"
                },
                "dangerous": {
                  "matchState": "MATCH_FOUND"
                }
              }
            }
          },
          "pi_and_jailbreak": {
            "piAndJailbreakFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "MATCH_FOUND"
            }
          },
          "sdp": {
            "sdpFilterResult": {
              "inspectResult": {
                "executionState": "EXECUTION_SUCCESS",
                "matchState": "NO_MATCH_FOUND"
              }
            }
          }
        }
      }
      }
      

    Go

    
    import (
    	"context"
    	"fmt"
    	"io"
    
    	modelarmor "cloud.google.com/go/modelarmor/apiv1"
    	modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
    	"google.golang.org/api/option"
    )
    
    // sanitizeUserPrompt sanitizes a user prompt based on the project, location, and template settings.
    //
    // w io.Writer: The writer to use for logging.
    // projectID string: The ID of the project.
    // locationID string: The ID of the location.
    // templateID string: The ID of the template.
    // userPrompt string: The user prompt to sanitize.
    func sanitizeUserPrompt(w io.Writer, projectID, locationID, templateID, userPrompt string) error {
    	ctx := context.Background()
    
    	//Create options for Model Armor client.
    	opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
    
    	// Create the Model Armor client.
    	client, err := modelarmor.NewClient(ctx, opts)
    	if err != nil {
    		return fmt.Errorf("failed to create client for location %s: %w", locationID, err)
    	}
    	defer client.Close()
    
    	// Initialize request argument(s)
    	userPromptData := &modelarmorpb.DataItem{
    		DataItem: &modelarmorpb.DataItem_Text{
    			Text: userPrompt,
    		},
    	}
    
    	// Prepare request for sanitizing user prompt.
    	req := &modelarmorpb.SanitizeUserPromptRequest{
    		Name:           fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, locationID, templateID),
    		UserPromptData: userPromptData,
    	}
    
    	// Sanitize the user prompt.
    	response, err := client.SanitizeUserPrompt(ctx, req)
    	if err != nil {
    		return fmt.Errorf("failed to sanitize user prompt for template %s: %w", templateID, err)
    	}
    
    	// Sanitization Result.
    	fmt.Fprintf(w, "Sanitization Result: %v\n", response)
    
    	return nil
    }
    

    Java

    
    import com.google.cloud.modelarmor.v1.DataItem;
    import com.google.cloud.modelarmor.v1.ModelArmorClient;
    import com.google.cloud.modelarmor.v1.ModelArmorSettings;
    import com.google.cloud.modelarmor.v1.SanitizeUserPromptRequest;
    import com.google.cloud.modelarmor.v1.SanitizeUserPromptResponse;
    import com.google.cloud.modelarmor.v1.TemplateName;
    import com.google.protobuf.util.JsonFormat;
    import java.io.IOException;
    
    public class SanitizeUserPrompt {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
    
        // Specify the Google Project ID.
        String projectId = "your-project-id";
        // Specify the location ID. For example, us-central1.
        String locationId = "your-location-id";
        // Specify the template ID.
        String templateId = "your-template-id";
        // Specify the user prompt.
        String userPrompt = "Unsafe user prompt";
    
        sanitizeUserPrompt(projectId, locationId, templateId, userPrompt);
      }
    
      public static SanitizeUserPromptResponse sanitizeUserPrompt(String projectId, String locationId,
          String templateId, String userPrompt) throws IOException {
    
        // Endpoint to call the Model Armor server.
        String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
        ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder()
            .setEndpoint(apiEndpoint)
            .build();
    
        try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
          // Build the resource name of the template.
          String templateName = TemplateName.of(projectId, locationId, templateId).toString();
    
          // Prepare the request.
          SanitizeUserPromptRequest request = SanitizeUserPromptRequest.newBuilder()
              .setName(templateName)
              .setUserPromptData(DataItem.newBuilder().setText(userPrompt).build())
              .build();
    
          SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request);
          System.out.println("Result for the provided user prompt: "
              + JsonFormat.printer().print(response.getSanitizationResult()));
    
          return response;
        }
      }
    }

    Node.js

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const projectId = process.env.PROJECT_ID || 'your-project-id';
    // const locationId = process.env.LOCATION_ID || 'us-central1';
    // const templateId = process.env.TEMPLATE_ID || 'template-id';
    // const userPrompt = 'unsafe user prompt';
    const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
    
    const client = new ModelArmorClient({
      apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
    });
    
    const request = {
      name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
      userPromptData: {
        text: userPrompt,
      },
    };
    
    const [response] = await client.sanitizeUserPrompt(request);
    console.log(JSON.stringify(response, null, 2));
    return response;

    PHP

    use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
    use Google\Cloud\ModelArmor\V1\SanitizeUserPromptRequest;
    use Google\Cloud\ModelArmor\V1\DataItem;
    
    /**
     * Sanitizes a user prompt using the specified template.
     *
     * @param string $projectId The ID of your Google Cloud Platform project (e.g. 'my-project').
     * @param string $locationId The ID of the location where the template is stored (e.g. 'us-central1').
     * @param string $templateId The ID of the template (e.g. 'my-template').
     * @param string $userPrompt The user prompt to sanitize (e.g. 'my-user-prompt').
     */
    function sanitize_user_prompt(
        string $projectId,
        string $locationId,
        string $templateId,
        string $userPrompt
    ): void {
        $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
        $client = new ModelArmorClient($options);
    
        $userPromptRequest = (new SanitizeUserPromptRequest())
            ->setName("projects/$projectId/locations/$locationId/templates/$templateId")
            ->setUserPromptData((new DataItem())->setText($userPrompt));
    
        $response = $client->sanitizeUserPrompt($userPromptRequest);
    
        printf('Result for Sanitize User Prompt: %s' . PHP_EOL, $response->serializeToJsonString());
    }

    Python

    Um diesen Code auszuführen, müssen Sie eine Python-Entwicklungsumgebung einrichten und das Model Armor Python SDK installieren.

    
    from google.api_core.client_options import ClientOptions
    from google.cloud import modelarmor_v1
    
    # TODO(Developer): Uncomment these variables.
    # project_id = "YOUR_PROJECT_ID"
    # location_id = "us-central1"
    # template_id = "template_id"
    # user_prompt = "Prompt entered by the user"
    
    # Create the Model Armor client.
    client = modelarmor_v1.ModelArmorClient(
        transport="rest",
        client_options=ClientOptions(
            api_endpoint=f"modelarmor.{location_id}.rep.googleapis.com"
        ),
    )
    
    # Initialize request argument(s).
    user_prompt_data = modelarmor_v1.DataItem(text=user_prompt)
    
    # Prepare request for sanitizing the defined prompt.
    request = modelarmor_v1.SanitizeUserPromptRequest(
        name=f"projects/{project_id}/locations/{location_id}/templates/{template_id}",
        user_prompt_data=user_prompt_data,
    )
    
    # Sanitize the user prompt.
    response = client.sanitize_user_prompt(request=request)
    
    # Sanitization Result.
    print(response)
    

    Das führt zu folgender Antwort:

      sanitization_result {
        filter_match_state: MATCH_FOUND
        filter_results {
          key: "rai"
          value {
            rai_filter_result {
              execution_state: EXECUTION_SUCCESS
              match_state: MATCH_FOUND
              rai_filter_type_results {
                key: "dangerous"
                value {
                  confidence_level: HIGH
                  match_state: MATCH_FOUND
                }
              }
            }
          }
        }
        filter_results {
          key: "pi_and_jailbreak"
          value {
            pi_and_jailbreak_filter_result {
              execution_state: EXECUTION_SUCCESS
              match_state: MATCH_FOUND
              confidence_level: HIGH
            }
          }
        }
        filter_results {
          key: "malicious_uris"
          value {
            malicious_uri_filter_result {
              execution_state: EXECUTION_SUCCESS
              match_state: NO_MATCH_FOUND
            }
          }
        }
        filter_results {
          key: "csam"
          value {
            csam_filter_filter_result {
              execution_state: EXECUTION_SUCCESS
              match_state: NO_MATCH_FOUND
            }
          }
        }
        invocation_result: SUCCESS
      }
      

    Text-Prompts mit aktivierter mehrsprachiger Erkennung bereinigen

    Sie können die mehrsprachige Erkennung für jede Anfrage einzeln aktivieren, indem Sie das Flag enableMultiLanguageDetection für jede Anfrage auf true setzen. Optional können Sie die Quellsprache angeben, um genauere Ergebnisse zu erhalten. Wenn die Quellsprache nicht angegeben ist, wird sie automatisch erkannt, um die Unterstützung mehrerer Sprachen zu ermöglichen.

    Verwenden Sie den folgenden Befehl, um einen Text-Prompt in Model Armor zu bereinigen, wobei die mehrsprachige Erkennung auf Anfrageebene aktiviert ist.

    curl -X POST \
        -d  '{"userPromptData":{"text":"[UNSAFE TEXT]"}, "multiLanguageDetectionMetadata": { "enableMultiLanguageDetection": true , "sourceLanguage": "jp"}}' \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
           "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts für die Vorlage.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Dateibasierte Prompts

    Wenn Sie einen Prompt bereinigen möchten, der in einer Datei gespeichert ist, geben Sie den Dateiinhalt im Base64-Format an. Model Armor erkennt den Dateityp nicht automatisch. Sie müssen das Feld byteDataType explizit festlegen, um das Dateiformat anzugeben. Wenn das Feld fehlt oder nicht angegeben ist, schlägt die Anfrage fehl. Mögliche byteDataType-Werte sind PLAINTEXT_UTF8, PDF, WORD_DOCUMENT, EXCEL_DOCUMENT, POWERPOINT_DOCUMENT, TXT und CSV.

    REST

      curl -X POST \
          -d "$(jq -n \
          --arg data "$(base64 -w 0 -i sample.pdf)" \
          '{userPromptData: {byteItem: {byteDataType: "FILE_TYPE", byteData: $data}}}')" \
          -H "Content-Type: application/json" \
          -H "Authorization: Bearer $(gcloud auth print-access-token)" \
          "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.
    • FILE_TYPE: Das Format der Eingabedatei.

    Go

    
    import (
    	"context"
    	"fmt"
    	"io"
    	"os"
    
    	modelarmor "cloud.google.com/go/modelarmor/apiv1"
    	modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
    	"google.golang.org/api/option"
    )
    
    // screenPDFFile screens a PDF file.
    //
    // This method screens a PDF file based on the project, location, and template settings.
    //
    // w io.Writer: The writer to use for logging.
    // projectID string: The ID of the project.
    // locationID string: The ID of the location.
    // templateID string: The ID of the template.
    // pdfFilePath string: The path to the PDF file to be screened.
    func screenPDFFile(w io.Writer, projectID, locationID, templateID, pdfFilePath string) error {
    	ctx := context.Background()
    
    	// Create options for Model Armor client.
    	opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
    	// Create the Model Armor client.
    	client, err := modelarmor.NewClient(ctx, opts)
    	if err != nil {
    		return fmt.Errorf("failed to create client: %w", err)
    	}
    	defer client.Close()
    
    	// Read PDF file content into bytes
    	pdfBytes, err := os.ReadFile(pdfFilePath)
    	if err != nil {
    		return fmt.Errorf("failed to read PDF file: %w", err)
    	}
    
    	// Initialize request argument(s)
    	userPromptData := &modelarmorpb.DataItem{
    		DataItem: &modelarmorpb.DataItem_ByteItem{
    			ByteItem: &modelarmorpb.ByteDataItem{
    				ByteDataType: modelarmorpb.ByteDataItem_PDF,
    				ByteData:     pdfBytes,
    			},
    		},
    	}
    
    	// Prepare request for sanitizing the defined prompt.
    	req := &modelarmorpb.SanitizeUserPromptRequest{
    		Name:           fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, locationID, templateID),
    		UserPromptData: userPromptData,
    	}
    
    	// Sanitize the user prompt.
    	response, err := client.SanitizeUserPrompt(ctx, req)
    	if err != nil {
    		return fmt.Errorf("failed to sanitize PDF content for template %s: %w", templateID, err)
    	}
    
    	// Sanitization Result.
    	fmt.Fprintf(w, "PDF screening sanitization result: %v\n", response)
    
    	return nil
    }
    

    Java

    
    import com.google.cloud.modelarmor.v1.ByteDataItem;
    import com.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType;
    import com.google.cloud.modelarmor.v1.DataItem;
    import com.google.cloud.modelarmor.v1.ModelArmorClient;
    import com.google.cloud.modelarmor.v1.ModelArmorSettings;
    import com.google.cloud.modelarmor.v1.SanitizeUserPromptRequest;
    import com.google.cloud.modelarmor.v1.SanitizeUserPromptResponse;
    import com.google.cloud.modelarmor.v1.TemplateName;
    import com.google.protobuf.ByteString;
    import com.google.protobuf.util.JsonFormat;
    import java.io.IOException;
    import java.nio.file.Files;
    import java.nio.file.Paths;
    
    public class ScreenPdfFile {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
    
        // Specify the Google Project ID.
        String projectId = "your-project-id";
        // Specify the location ID. For example, us-central1. 
        String locationId = "your-location-id";
        // Specify the template ID.
        String templateId = "your-template-id";
        // Specify the PDF file path. Replace with your PDF file path.
        String pdfFilePath = "src/main/resources/test_sample.pdf";
    
        screenPdfFile(projectId, locationId, templateId, pdfFilePath);
      }
    
      public static SanitizeUserPromptResponse screenPdfFile(String projectId, String locationId,
          String templateId, String pdfFilePath) throws IOException {
    
        // Endpoint to call the Model Armor server.
        String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
        ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
            .build();
    
        try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
          // Build the resource name of the template.
          String name = TemplateName.of(projectId, locationId, templateId).toString();
    
          // Read the PDF file content and encode it to Base64.
          byte[] fileContent = Files.readAllBytes(Paths.get(pdfFilePath));
    
          // Prepare the request.
          DataItem userPromptData = DataItem.newBuilder()
              .setByteItem(
                ByteDataItem.newBuilder()
                  .setByteDataType(ByteItemType.PDF)
                  .setByteData(ByteString.copyFrom(fileContent))
                  .build())
              .build();
    
          SanitizeUserPromptRequest request =
              SanitizeUserPromptRequest.newBuilder()
                  .setName(name)
                  .setUserPromptData(userPromptData)
                  .build();
    
          // Send the request and get the response.
          SanitizeUserPromptResponse response = client.sanitizeUserPrompt(request);
    
          // Print the sanitization result.
          System.out.println("Result for the provided PDF file: "
              + JsonFormat.printer().print(response.getSanitizationResult()));
    
          return response;
        }
      }
    }

    Node.js

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const projectId = process.env.PROJECT_ID || 'your-project-id';
    // const locationId = process.env.LOCATION_ID || 'us-central1';
    // const templateId = process.env.TEMPLATE_ID || 'template-id';
    // const pdfContentFilename = 'path/to/file.pdf';
    
    // Imports the Model Armor library
    const modelarmor = require('@google-cloud/modelarmor');
    const {ModelArmorClient} = modelarmor.v1;
    const {protos} = modelarmor;
    const ByteItemType =
      protos.google.cloud.modelarmor.v1.ByteDataItem.ByteItemType;
    
    const fs = require('fs');
    
    const pdfContent = fs.readFileSync(pdfContentFilename);
    const pdfContentBase64 = pdfContent.toString('base64');
    
    const client = new ModelArmorClient({
      apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
    });
    
    const request = {
      name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
      userPromptData: {
        byteItem: {
          byteDataType: ByteItemType.PDF,
          byteData: pdfContentBase64,
        },
      },
    };
    
    const [response] = await client.sanitizeUserPrompt(request);
    console.log(JSON.stringify(response, null, 2));
    return response;

    PHP

    use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
    use Google\Cloud\ModelArmor\V1\SanitizeUserPromptRequest;
    use Google\Cloud\ModelArmor\V1\ByteDataItem;
    use Google\Cloud\ModelArmor\V1\ByteDataItem\ByteItemType;
    use Google\Cloud\ModelArmor\V1\DataItem;
    
    /**
     * Screens a PDF file using the ModelArmor service.
     *
     * @param string $projectId The Google Cloud project ID (e.g. 'my-project').
     * @param string $locationId The location ID of the ModelArmor service (e.g. 'us-central1').
     * @param string $templateId The ID of the template to use for the screener (e.g. 'my-template').
     * @param string $filePath The path to the PDF file to screen (e.g. 'path/to/file.pdf').
     */
    function screen_pdf_file(
        string $projectId,
        string $locationId,
        string $templateId,
        string $filePath
    ): void {
        $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
        $client = new ModelArmorClient($options);
    
        // Read the file content and encode it in base64.
        $pdfContent = file_get_contents($filePath);
        $pdfContentBase64 = base64_encode($pdfContent);
    
        $userPromptRequest = (new SanitizeUserPromptRequest())
            ->setName("projects/$projectId/locations/$locationId/templates/$templateId")
            ->setUserPromptData((new DataItem())
                ->setByteItem((new ByteDataItem())->setByteData($pdfContentBase64)
                    ->setByteDataType(ByteItemType::PDF)));
    
        $response = $client->sanitizeUserPrompt($userPromptRequest);
    
        printf('Result for Screen PDF File: %s' . PHP_EOL, $response->serializeToJsonString());
    }

    Python

    Um diesen Code auszuführen, müssen Sie eine Python-Entwicklungsumgebung einrichten und das Model Armor Python SDK installieren.

    
    import base64
    from google.api_core.client_options import ClientOptions
    from google.cloud import modelarmor_v1
    
    # TODO(Developer): Uncomment these variables.
    # project_id = "YOUR_PROJECT_ID"
    # location_id = "us-central1"
    # template_id = "template_id"
    # pdf_content_filename = "path/to/file.pdf"
    
    # Encode the PDF file into base64
    with open(pdf_content_filename, "rb") as f:
        pdf_content_base64 = base64.b64encode(f.read())
    
    # Create the Model Armor client.
    client = modelarmor_v1.ModelArmorClient(
        transport="rest",
        client_options=ClientOptions(
            api_endpoint=f"modelarmor.{location_id}.rep.googleapis.com"
        ),
    )
    
    # Initialize request argument(s).
    user_prompt_data = modelarmor_v1.DataItem(
        byte_item=modelarmor_v1.ByteDataItem(
            byte_data_type=modelarmor_v1.ByteDataItem.ByteItemType.PDF,
            byte_data=pdf_content_base64,
        )
    )
    
    request = modelarmor_v1.SanitizeUserPromptRequest(
        name=f"projects/{project_id}/locations/{location_id}/templates/{template_id}",
        user_prompt_data=user_prompt_data,
    )
    
    # Sanitize the user prompt.
    response = client.sanitize_user_prompt(request=request)
    
    # Sanitization Result.
    print(response)
    

    Grundlegende Konfiguration von Sensitive Data Protection

    Model Armor ist in Sensitive Data Protection integriert, um die versehentliche Offenlegung privater Informationen zu verhindern. Erstellen Sie eine Vorlage mit aktivierten grundlegenden Einstellungen für Sensitive Data Protection. Mit der einfachen Version von Sensitive Data Protection können Sie nach einer festen Gruppe von infoTypes für den Schutz sensibler Daten suchen.

    Die folgenden infoTypes für den Schutz sensibler Daten werden im Prompt für alle Regionen gescannt:

    • CREDIT_CARD_NUMBER: Eine Kreditkartennummer ist 12 bis 19 Ziffern lang. Sie wird weltweit für Zahlungsvorgänge verwendet.
    • FINANCIAL_ACCOUNT_NUMBER: Eine Nummer, die sich auf ein bestimmtes Finanzkonto bezieht, z. B. eine Bankkontonummer oder eine Rentenkontonummer.
    • GCP_CREDENTIALS: Google Cloud Anmeldedaten des Dienstkontos. Anmeldedaten, die zur Authentifizierung bei {api_client_lib_name} und Dienstkonten verwendet werden können.
    • GCP_API_KEY: Google Cloud API-Schlüssel. Ein verschlüsselter String, der beim Aufrufen von Google Cloud APIs verwendet wird, die nicht auf private Nutzerdaten zugreifen müssen.
    • PASSWORD: Klartext-Passwörter in Konfigurationen, Code und anderem Text.

    Die folgenden zusätzlichen infoTypes für den Schutz sensibler Daten werden im Prompt für Regionen in den USA gescannt:

    • US_SOCIAL_SECURITY_NUMBER: Eine US-amerikanische Sozialversicherungsnummer (SSN) ist eine 9-stellige Nummer für US-Staatsbürger, Inhaber einer unbeschränkten und Inhaber einer zeitlich beschränkten Aufenthaltsgenehmigung. Dieser Detektor gleicht keine Nummern ab, bei denen in einer der Zahlengruppen nur Nullen enthalten sind (d. h. 000-##-####, ###-00-#### oder ###-##-0000), deren erste Zahlengruppe 666 lautet oder deren erste Ziffer eine 9 ist.
    • US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER: Eine US-amerikanische Steueridentifikationsnummer für Einzelpersonen (Individual Taxpayer Identification Number, ITIN) ist eine Art Steueridentifikationsnummer (Tax Identification Number, TIN), die vom IRS (Internal Revenue Service) ausgestellt wird. Eine ITIN ist eine Steuerbearbeitungsnummer, verfügbar nur für bestimmte nicht ansässige und ansässige Ausländer, deren Ehepartner und Familienangehörige, die keine Sozialversicherungsnummer (SSN) erhalten können.

    Hier sehen Sie ein Beispiel für eine einfache Konfiguration für den Schutz sensibler Daten:

    gcloud

    gcloud model-armor templates create TEMPLATE_ID \
        --location=LOCATION \
        --project=PROJECT_ID \
        --basic-config-filter-enforcement=enabled

    Ersetzen Sie Folgendes:

    • TEMPLATE_ID: die ID der Vorlage.
    • LOCATION: der Speicherort der Vorlage.

    REST

    export FILTER_CONFIG_SDP_BASIC='{
      "filterConfig": {
        "sdpSettings": {
          "basicConfig": {
            "filterEnforcement": "ENABLED"
          }
        }
      }
    }'
    
    curl -X PATCH \
        -d "$FILTER_CONFIG_SDP_BASIC" \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID?updateMask=filterConfig.sdpSettings.basicConfig.filterEnforcement"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Go

    
    import (
    	"context"
    	"fmt"
    	"io"
    
    	modelarmor "cloud.google.com/go/modelarmor/apiv1"
    	modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
    	"google.golang.org/api/option"
    )
    
    // createModelArmorTemplateWithBasicSDP method creates a new Model Armor template with basic SDP settings.
    //
    // w io.Writer: The writer to use for logging.
    // projectID string: The ID of the Google Cloud project.
    // locationID string: The ID of the Google Cloud location.
    // templateID string: The ID of the template to create.
    func createModelArmorTemplateWithBasicSDP(w io.Writer, projectID, locationID, templateID string) error {
    	ctx := context.Background()
    
    	// Create options for Model Armor client.
    	opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
    	// Create the Model Armor client.
    	client, err := modelarmor.NewClient(ctx, opts)
    	if err != nil {
    		return fmt.Errorf("failed to create client for project %s, location %s: %w", projectID, locationID, err)
    	}
    	defer client.Close()
    
    	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
    
    	// Build the Model Armor template with your preferred filters.
    	// For more details on filters, please refer to the following doc:
    	// [https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters](https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters)
    	template := &modelarmorpb.Template{
    		FilterConfig: &modelarmorpb.FilterConfig{
    			RaiSettings: &modelarmorpb.RaiFilterSettings{
    				RaiFilters: []*modelarmorpb.RaiFilterSettings_RaiFilter{
    					{
    						FilterType:      modelarmorpb.RaiFilterType_DANGEROUS,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_HARASSMENT,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_HATE_SPEECH,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_SEXUALLY_EXPLICIT,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    				},
    			},
    			SdpSettings: &modelarmorpb.SdpFilterSettings{
    				SdpConfiguration: &modelarmorpb.SdpFilterSettings_BasicConfig{
    					BasicConfig: &modelarmorpb.SdpBasicConfig{
    						FilterEnforcement: modelarmorpb.SdpBasicConfig_ENABLED,
    					},
    				},
    			},
    		},
    	}
    
    	// Prepare the request for creating the template.
    	req := &modelarmorpb.CreateTemplateRequest{
    		Parent:     parent,
    		TemplateId: templateID,
    		Template:   template,
    	}
    
    	// Create the template.
    	response, err := client.CreateTemplate(ctx, req)
    	if err != nil {
    		return fmt.Errorf("failed to create template: %w", err)
    	}
    
    	// Print the new template name using fmt.Fprintf with the io.Writer.
    	fmt.Fprintf(w, "Created Template with basic SDP: %s\n", response.Name)
    
    	return err
    }
    

    Java

    
    import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
    import com.google.cloud.modelarmor.v1.FilterConfig;
    import com.google.cloud.modelarmor.v1.LocationName;
    import com.google.cloud.modelarmor.v1.ModelArmorClient;
    import com.google.cloud.modelarmor.v1.ModelArmorSettings;
    import com.google.cloud.modelarmor.v1.SdpBasicConfig;
    import com.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement;
    import com.google.cloud.modelarmor.v1.SdpFilterSettings;
    import com.google.cloud.modelarmor.v1.Template;
    import java.io.IOException;
    
    public class CreateTemplateWithBasicSdp {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
    
        // Specify the Google Project ID.
        String projectId = "your-project-id";
        // Specify the location ID. For example, us-central1. 
        String locationId = "your-location-id";
        // Specify the template ID.
        String templateId = "your-template-id";
    
        createTemplateWithBasicSdp(projectId, locationId, templateId);
      }
    
      public static Template createTemplateWithBasicSdp(
          String projectId, String locationId, String templateId) throws IOException {
    
        // Construct the API endpoint URL.
        String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
        ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
            .build();
    
        // Initialize the client that will be used to send requests. This client
        // only needs to be created once, and can be reused for multiple requests.
        try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
          String parent = LocationName.of(projectId, locationId).toString();
    
          // Build the Model Armor template with your preferred filters.
          // For more details on filters, please refer to the following doc:
          // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
    
          // Configure Basic SDP Filter.
          SdpBasicConfig basicSdpConfig = SdpBasicConfig.newBuilder()
              .setFilterEnforcement(SdpBasicConfigEnforcement.ENABLED)
              .build();
    
          SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder()
              .setBasicConfig(basicSdpConfig)
              .build();
    
          FilterConfig modelArmorFilter = FilterConfig.newBuilder()
              .setSdpSettings(sdpSettings)
              .build();
    
          Template template = Template.newBuilder()
              .setFilterConfig(modelArmorFilter)
              .build();
    
          CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
              .setParent(parent)
              .setTemplateId(templateId)
              .setTemplate(template)
              .build();
    
          Template createdTemplate = client.createTemplate(request);
          System.out.println("Created template with basic SDP filter: " + createdTemplate.getName());
    
          return createdTemplate;
        }
      }
    }

    Node.js

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const projectId = 'your-project-id';
    // const locationId = 'us-central1';
    // const templateId = 'template-id';
    
    const parent = `projects/${projectId}/locations/${locationId}`;
    
    // Imports the Model Armor library
    const modelarmor = require('@google-cloud/modelarmor');
    const {ModelArmorClient} = modelarmor.v1;
    const {protos} = modelarmor;
    
    const RaiFilterType = protos.google.cloud.modelarmor.v1.RaiFilterType;
    const DetectionConfidenceLevel =
      protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
    const SdpBasicConfigEnforcement =
      protos.google.cloud.modelarmor.v1.SdpBasicConfig.SdpBasicConfigEnforcement;
    
    // Instantiates a client
    const client = new ModelArmorClient({
      apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
    });
    
    // Configuration for the template with basic SDP settings
    const templateConfig = {
      filterConfig: {
        raiSettings: {
          raiFilters: [
            {
              filterType: RaiFilterType.DANGEROUS,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
            {
              filterType: RaiFilterType.HARASSMENT,
              confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
            },
            {
              filterType: RaiFilterType.HATE_SPEECH,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
            {
              filterType: RaiFilterType.SEXUALLY_EXPLICIT,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
          ],
        },
        sdpSettings: {
          basicConfig: {
            filterEnforcement: SdpBasicConfigEnforcement.ENABLED,
          },
        },
      },
    };
    
    // Construct request
    const request = {
      parent,
      templateId,
      template: templateConfig,
    };
    
    const [response] = await client.createTemplate(request);
    return response;

    PHP

    use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
    use Google\Cloud\ModelArmor\V1\SdpBasicConfig\SdpBasicConfigEnforcement;
    use Google\Cloud\ModelArmor\V1\SdpBasicConfig;
    use Google\Cloud\ModelArmor\V1\SdpFilterSettings;
    use Google\Cloud\ModelArmor\V1\FilterConfig;
    use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
    use Google\Cloud\ModelArmor\V1\Template;
    
    /**
     * Create a Model Armor template with Basic SDP Filter.
     *
     * @param string $projectId The ID of the project (e.g. 'my-project').
     * @param string $locationId The ID of the location (e.g. 'us-central1').
     * @param string $templateId The ID of the template (e.g. 'my-template').
     */
    function create_template_with_basic_sdp(string $projectId, string $locationId, string $templateId): void
    {
        $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
        $client = new ModelArmorClient($options);
        $parent = $client->locationName($projectId, $locationId);
    
        // Build the Model Armor template with your preferred filters.
        // For more details on filters, please refer to the following doc:
        // https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
    
        // Configure Basic SDP Filter.
        $sdpBasicConfig = (new SdpBasicConfig())->setFilterEnforcement(SdpBasicConfigEnforcement::ENABLED);
        $sdpSettings = (new SdpFilterSettings())->setBasicConfig($sdpBasicConfig);
    
        $templateFilterConfig = (new FilterConfig())
            ->setSdpSettings($sdpSettings);
    
        $template = (new Template())->setFilterConfig($templateFilterConfig);
    
        $request = (new CreateTemplateRequest())
            ->setParent($parent)
            ->setTemplateId($templateId)
            ->setTemplate($template);
    
        $response = $client->createTemplate($request);
    
        printf('Template created: %s' . PHP_EOL, $response->getName());
    }

    Python

    Um diesen Code auszuführen, müssen Sie eine Python-Entwicklungsumgebung einrichten und das Model Armor Python SDK installieren.

    
    from google.api_core.client_options import ClientOptions
    from google.cloud import modelarmor_v1
    
    # TODO(Developer): Uncomment these variables.
    # project_id = "YOUR_PROJECT_ID"
    # location_id = "us-central1"
    # template_id = "template_id"
    
    # Create the Model Armor client.
    client = modelarmor_v1.ModelArmorClient(
        client_options=ClientOptions(
            api_endpoint=f"modelarmor.{location_id}.rep.googleapis.com"
        )
    )
    
    parent = f"projects/{project_id}/locations/{location_id}"
    
    # Build the Model Armor template with your preferred filters.
    # For more details on filters, please refer to the following doc:
    # https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
    template = modelarmor_v1.Template(
        filter_config=modelarmor_v1.FilterConfig(
            rai_settings=modelarmor_v1.RaiFilterSettings(
                rai_filters=[
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.DANGEROUS,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.HARASSMENT,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.HATE_SPEECH,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.SEXUALLY_EXPLICIT,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                ]
            ),
            sdp_settings=modelarmor_v1.SdpFilterSettings(
                basic_config=modelarmor_v1.SdpBasicConfig(
                    filter_enforcement=modelarmor_v1.SdpBasicConfig.SdpBasicConfigEnforcement.ENABLED
                )
            ),
        ),
    )
    
    # Prepare the request for creating the template.
    create_template = modelarmor_v1.CreateTemplateRequest(
        parent=parent, template_id=template_id, template=template
    )
    
    # Create the template.
    response = client.create_template(request=create_template)
    
    # Print the new template name.
    print(f"Created template: {response.name}")
    

    Verwenden Sie die erstellte Vorlage, um Ihre Prompts zu prüfen. Beispiel:

    curl -X POST \
        -d '{"userPromptData":{"text":"can you remember my ITIN : ###-##-####"}}' \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Dieses Beispiel gibt die folgende Antwort zurück:

    {
      "sanitizationResult": {
          "filterMatchState": "MATCH_FOUND",
          "invocationResult": "SUCCESS",
          "filterResults": [
            {
              "csamFilterFilterResult": {
                "executionState": "EXECUTION_SUCCESS",
                "matchState": "NO_MATCH_FOUND"
              }
            },
            {
          "sdpFilterResult": {
            "inspectResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "MATCH_FOUND",
              "findings": [
                {
                  "infoType": "US_INDIVIDUAL_TAXPAYER_IDENTIFICATION_NUMBER",
                  "likelihood": "LIKELY",
                  "location": {
                    "byteRange": {
                      "start": "26",
                      "end": "37"
                    },
                    "codepointRange": {
                      "start": "26",
                      "end": "37"
                    }
                  }
                }
              ]
            }
           }
          }
        ]
      }
    }

    Erweiterte Konfiguration des Schutzes sensibler Daten

    Model Armor überprüft die LLM-Prompts und ‑Antworten mithilfe der erweiterten Konfigurationseinstellung für den Schutz sensibler Daten. So können Sie die Funktionen von Sensitive Data Protection über die infoTypes hinaus nutzen, die in der grundlegenden Einstellung für Sensitive Data Protection angeboten werden.

    Wenn Sie den erweiterten Filter für den Schutz sensibler Daten in Model Armor verwenden möchten, müssen sich die Vorlagen für den Schutz sensibler Daten am selben Cloud-Speicherort wie die Model Armor-Vorlage befinden.

    gcloud

    gcloud model-armor templates create TEMPLATE_ID \
        --location=LOCATION \
        --advanced-config-inspect-template="path/to/template" \

    Ersetzen Sie Folgendes:

    • TEMPLATE_ID: die ID der Vorlage.
    • LOCATION: der Speicherort der Vorlage.

    REST

      export FILTER_CONFIG_SDP_ADV='{
        "filterConfig": {
          "sdpSettings": {
            "advancedConfig": {
              "deidentifyTemplate": "projects/PROJECT_ID/locations/LOCATION/deidentifyTemplates/deidentify-ip-address",
              "inspectTemplate": "projects/PROJECT_ID/locations/LOCATION/inspectTemplates/inspect-ip-address"
            }
          }
        }
      }'
    
     curl -X POST \
         -d "$FILTER_CONFIG_SDP_ADV" \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer $(gcloud auth print-access-token)" \
           "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID?updateMask=filterConfig.sdpSettings.advancedConfig"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Dieses Beispiel gibt die folgende Antwort zurück:

    {
      "name": "projects/PROJECT_ID/locations/LOCATION/templates/all-filters-test",
      "createTime": "2024-12-16T17:08:19.626693819Z",
      "updateTime": "2024-12-16T17:08:19.626693819Z",
       "filterConfig": {
          "sdpSettings": {
            "advancedConfig": {
              "deidentifyTemplate":  "projects/PROJECT_ID/locations/LOCATION/deidentifyTemplates/deidentify-ip-address",
              "inspectTemplate": "projects/PROJECT_ID/locations/LOCATION/inspectTemplates/inspect-ip-address"
            }
          }
        }
    }

    Go

    
    import (
    	"context"
    	"fmt"
    	"io"
    
    	modelarmor "cloud.google.com/go/modelarmor/apiv1"
    	modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
    	"google.golang.org/api/option"
    )
    
    // createModelArmorTemplateWithAdvancedSDP method creates a
    // new Model Armor template with advanced SDP settings,
    // including inspect and deidentify templates.
    //
    // w io.Writer: The writer to use for logging.
    // projectID string: The ID of the Google Cloud project.
    // locationID string: The ID of the Google Cloud location.
    // templateID string: The ID of the template to create.
    // inspectTemplate string: The ID of the inspect template to use.
    // deidentifyTemplate string: The ID of the deidentify template to use.
    func createModelArmorTemplateWithAdvancedSDP(w io.Writer, projectID, locationID, templateID, inspectTemplate, deidentifyTemplate string) error {
    	ctx := context.Background()
    
    	// Create options for Model Armor client.
    	opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
    	// Create the Model Armor client.
    	client, err := modelarmor.NewClient(ctx, opts)
    	if err != nil {
    		return fmt.Errorf("failed to create client for project %s, location %s: %w", projectID, locationID, err)
    	}
    	defer client.Close()
    
    	parent := fmt.Sprintf("projects/%s/locations/%s", projectID, locationID)
    
    	// Build the Model Armor template with your preferred filters.
    	template := &modelarmorpb.Template{
    		FilterConfig: &modelarmorpb.FilterConfig{
    			RaiSettings: &modelarmorpb.RaiFilterSettings{
    				RaiFilters: []*modelarmorpb.RaiFilterSettings_RaiFilter{
    					{
    						FilterType:      modelarmorpb.RaiFilterType_DANGEROUS,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_HARASSMENT,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_MEDIUM_AND_ABOVE,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_HATE_SPEECH,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    					{
    						FilterType:      modelarmorpb.RaiFilterType_SEXUALLY_EXPLICIT,
    						ConfidenceLevel: modelarmorpb.DetectionConfidenceLevel_HIGH,
    					},
    				},
    			},
    			SdpSettings: &modelarmorpb.SdpFilterSettings{
    				SdpConfiguration: &modelarmorpb.SdpFilterSettings_AdvancedConfig{
    					AdvancedConfig: &modelarmorpb.SdpAdvancedConfig{
    						InspectTemplate:    inspectTemplate,
    						DeidentifyTemplate: deidentifyTemplate,
    					},
    				},
    			},
    		},
    	}
    
    	// Prepare the request for creating the template.
    	req := &modelarmorpb.CreateTemplateRequest{
    		Parent:     parent,
    		TemplateId: templateID,
    		Template:   template,
    	}
    
    	// Create the template.
    	response, err := client.CreateTemplate(ctx, req)
    	if err != nil {
    		return fmt.Errorf("failed to create template: %w", err)
    	}
    
    	// Print the new template name using fmt.Fprint with the io.Writer.
    	fmt.Fprintf(w, "Created Template with advanced SDP: %s\n", response.Name)
    
    	return err
    }
    

    Java

    
    import com.google.cloud.modelarmor.v1.CreateTemplateRequest;
    import com.google.cloud.modelarmor.v1.FilterConfig;
    import com.google.cloud.modelarmor.v1.LocationName;
    import com.google.cloud.modelarmor.v1.ModelArmorClient;
    import com.google.cloud.modelarmor.v1.ModelArmorSettings;
    import com.google.cloud.modelarmor.v1.SdpAdvancedConfig;
    import com.google.cloud.modelarmor.v1.SdpFilterSettings;
    import com.google.cloud.modelarmor.v1.Template;
    import com.google.privacy.dlp.v2.DeidentifyTemplateName;
    import com.google.privacy.dlp.v2.InspectTemplateName;
    import java.io.IOException;
    
    public class CreateTemplateWithAdvancedSdp {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
    
        // Specify the Google Project ID.
        String projectId = "your-project-id";
        // Specify the location ID. For example, us-central1.
        String locationId = "your-location-id";
        // Specify the template ID.
        String templateId = "your-template-id";
        // Specify the Inspect template ID.
        String inspectTemplateId = "your-inspect-template-id";
        // Specify the Deidentify template ID.
        String deidentifyTemplateId = "your-deidentify-template-id";
    
        createTemplateWithAdvancedSdp(projectId, locationId, templateId, inspectTemplateId,
            deidentifyTemplateId);
      }
    
      public static Template createTemplateWithAdvancedSdp(String projectId, String locationId,
          String templateId, String inspectTemplateId, String deidentifyTemplateId) throws IOException {
    
        // Construct the API endpoint URL.
        String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
        ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
            .build();
    
        // Initialize the client that will be used to send requests. This client
        // only needs to be created once, and can be reused for multiple requests.
        try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
          String parent = LocationName.of(projectId, locationId).toString();
    
          String inspectTemplateName = InspectTemplateName
              .ofProjectLocationInspectTemplateName(projectId, locationId, inspectTemplateId)
              .toString();
    
          String deidentifyTemplateName = DeidentifyTemplateName
              .ofProjectLocationDeidentifyTemplateName(projectId, locationId, deidentifyTemplateId)
              .toString();
    
          // Build the Model Armor template with Advanced SDP Filter.
    
          // Note: If you specify only Inspect template, Model Armor reports the filter matches if
          // sensitive data is detected. If you specify Inspect template and De-identify template, Model
          // Armor returns the de-identified sensitive data and sanitized version of prompts or
          // responses in the deidentifyResult.data.text field of the finding.
          SdpAdvancedConfig advancedSdpConfig =
              SdpAdvancedConfig.newBuilder()
                  .setInspectTemplate(inspectTemplateName)
                  .setDeidentifyTemplate(deidentifyTemplateName)
                  .build();
    
          SdpFilterSettings sdpSettings = SdpFilterSettings.newBuilder()
              .setAdvancedConfig(advancedSdpConfig).build();
    
          FilterConfig modelArmorFilter = FilterConfig.newBuilder().setSdpSettings(sdpSettings).build();
    
          Template template = Template.newBuilder().setFilterConfig(modelArmorFilter).build();
    
          CreateTemplateRequest request = CreateTemplateRequest.newBuilder()
              .setParent(parent)
              .setTemplateId(templateId)
              .setTemplate(template)
              .build();
    
          Template createdTemplate = client.createTemplate(request);
          System.out.println("Created template with Advanced SDP filter: " + createdTemplate.getName());
    
          return createdTemplate;
        }
      }
    }

    Node.js

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const projectId = 'your-project-id';
    // const locationId = 'us-central1';
    // const templateId = 'template-id';
    // const inspectTemplate = `projects/${projectId}/locations/${locationId}/inspectTemplates/inspect-template-id`;
    // const deidentifyTemplate = `projects/${projectId}/locations/${locationId}/deidentifyTemplates/deidentify-template-id`;
    
    const parent = `projects/${projectId}/locations/${locationId}`;
    
    // Imports the Model Armor library
    const modelarmor = require('@google-cloud/modelarmor');
    const {ModelArmorClient} = modelarmor.v1;
    const {protos} = modelarmor;
    
    const RaiFilterType = protos.google.cloud.modelarmor.v1.RaiFilterType;
    const DetectionConfidenceLevel =
      protos.google.cloud.modelarmor.v1.DetectionConfidenceLevel;
    
    // Instantiates a client
    const client = new ModelArmorClient({
      apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
    });
    
    // Configuration for the template with advanced SDP settings
    const templateConfig = {
      filterConfig: {
        raiSettings: {
          raiFilters: [
            {
              filterType: RaiFilterType.DANGEROUS,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
            {
              filterType: RaiFilterType.HARASSMENT,
              confidenceLevel: DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
            },
            {
              filterType: RaiFilterType.HATE_SPEECH,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
            {
              filterType: RaiFilterType.SEXUALLY_EXPLICIT,
              confidenceLevel: DetectionConfidenceLevel.HIGH,
            },
          ],
        },
        sdpSettings: {
          advancedConfig: {
            inspectTemplate: inspectTemplate,
            deidentifyTemplate: deidentifyTemplate,
          },
        },
      },
    };
    
    // Construct request
    const request = {
      parent,
      templateId,
      template: templateConfig,
    };
    
    // Create the template
    const [response] = await client.createTemplate(request);
    return response;

    PHP

    use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
    use Google\Cloud\ModelArmor\V1\SdpAdvancedConfig;
    use Google\Cloud\ModelArmor\V1\Template;
    use Google\Cloud\ModelArmor\V1\FilterConfig;
    use Google\Cloud\ModelArmor\V1\CreateTemplateRequest;
    use Google\Cloud\ModelArmor\V1\SdpFilterSettings;
    
    /**
     * Create a Model Armor template with an Advanced SDP Filter.
     *
     * @param string $projectId The ID of the project (e.g. 'my-project').
     * @param string $locationId The ID of the location (e.g. 'us-central1').
     * @param string $templateId The ID of the template (e.g. 'my-template').
     * @param string $inspectTemplate The resource name of the inspect template.
              (e.g. 'organizations/{organization}/inspectTemplates/{inspect_template}')
     * @param string $deidentifyTemplate The resource name of the de-identify template.
              (e.g. 'organizations/{organization}/deidentifyTemplates/{deidentify_template}')
     */
    function create_template_with_advanced_sdp(
        string $projectId,
        string $locationId,
        string $templateId,
        string $inspectTemplate,
        string $deidentifyTemplate
    ): void {
        $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
        $client = new ModelArmorClient($options);
        $parent = $client->locationName($projectId, $locationId);
    
        // Build the Model Armor template with Advanced SDP Filter.
    
        // Note: If you specify only Inspect template, Model Armor reports the filter matches if
        // sensitive data is detected. If you specify Inspect template and De-identify template, Model
        // Armor returns the de-identified sensitive data and sanitized version of prompts or
        // responses in the deidentifyResult.data.text field of the finding.
        $sdpAdvancedConfig = (new SdpAdvancedConfig())
            ->setInspectTemplate($inspectTemplate)
            ->setDeidentifyTemplate($deidentifyTemplate);
    
        $sdpSettings = (new SdpFilterSettings())->setAdvancedConfig($sdpAdvancedConfig);
    
        $templateFilterConfig = (new FilterConfig())
            ->setSdpSettings($sdpSettings);
    
        $template = (new Template())->setFilterConfig($templateFilterConfig);
    
        $request = (new CreateTemplateRequest())
            ->setParent($parent)
            ->setTemplateId($templateId)
            ->setTemplate($template);
    
        $response = $client->createTemplate($request);
    
        printf('Template created: %s' . PHP_EOL, $response->getName());
    }

    Python

    Um diesen Code auszuführen, müssen Sie eine Python-Entwicklungsumgebung einrichten und das Model Armor Python SDK installieren.

    
    from google.api_core.client_options import ClientOptions
    from google.cloud import modelarmor_v1
    
    # TODO(Developer): Uncomment these variables.
    # project_id = "YOUR_PROJECT_ID"
    # location_id = "us-central1"
    # template_id = "template_id"
    # inspect_template = f"projects/{project_id}/inspectTemplates/{inspect_template_id}"
    # deidentify_template = f"projects/{project_id}/deidentifyTemplates/{deidentify_template_id}"
    
    # Create the Model Armor client.
    client = modelarmor_v1.ModelArmorClient(
        transport="rest",
        client_options=ClientOptions(
            api_endpoint=f"modelarmor.{location_id}.rep.googleapis.com"
        ),
    )
    
    parent = f"projects/{project_id}/locations/{location_id}"
    
    # Build the Model Armor template with your preferred filters.
    # For more details on filters, please refer to the following doc:
    # https://cloud.google.com/security-command-center/docs/key-concepts-model-armor#ma-filters
    template = modelarmor_v1.Template(
        filter_config=modelarmor_v1.FilterConfig(
            rai_settings=modelarmor_v1.RaiFilterSettings(
                rai_filters=[
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.DANGEROUS,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.HARASSMENT,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.MEDIUM_AND_ABOVE,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.HATE_SPEECH,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                    modelarmor_v1.RaiFilterSettings.RaiFilter(
                        filter_type=modelarmor_v1.RaiFilterType.SEXUALLY_EXPLICIT,
                        confidence_level=modelarmor_v1.DetectionConfidenceLevel.HIGH,
                    ),
                ]
            ),
            sdp_settings=modelarmor_v1.SdpFilterSettings(
                advanced_config=modelarmor_v1.SdpAdvancedConfig(
                    inspect_template=inspect_template,
                    deidentify_template=deidentify_template,
                )
            ),
        ),
    )
    
    # Prepare the request for creating the template.
    create_template = modelarmor_v1.CreateTemplateRequest(
        parent=parent, template_id=template_id, template=template
    )
    
    # Create the template.
    response = client.create_template(request=create_template)
    
    # Print the new template name.
    print(f"Created template: {response.name}")
    

    Verwenden Sie die erstellte Vorlage, um Ihre Prompts zu prüfen. Beispiel:

    curl -X POST \
        -d '{"userPromptData":{"text":"is there anything malicious running on 1.1.1.1?"}}' \
        -H "Content-Type: application/json" \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
            "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeUserPrompt"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Dieses Beispiel gibt die folgende Antwort zurück:

    {
      "sanitizationResult": {
        "filterMatchState": "MATCH_FOUND",
        "invocationResult": "SUCCESS",
          "filterResults": [
          {
            "csamFilterFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "NO_MATCH_FOUND"
            }
          },
          {
          "sdpFilterResult": {
            "deidentifyResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "MATCH_FOUND",
              "data": {
                "text": "is there anything malicious running on [IP_ADDRESS]?"
              },
                "transformedBytes": "7",
                "infoTypes": ["IP_ADDRESS"]
            }
          }
          }
          ]
      }
    }

    Modellantwort bereinigen

    LLMs können manchmal schädliche Antworten generieren. Um die Risiken zu verringern, die mit der Verwendung von LLMs in Ihren Anwendungen verbunden sind, ist es wichtig, die Antworten zu bereinigen.

    Hier ist ein Beispielbefehl zum Bereinigen einer Modellantwort in Model Armor.

    REST

     curl -X POST \
         -d '{"text":"IP address of the current network is ##.##.##.##"}' \
         -H "Content-Type: application/json" \
         -H "Authorization: Bearer $(gcloud auth print-access-token)" \
             "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeModelResponse"
    

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

      Dieses Beispiel gibt die folgende Antwort zurück:

      {
      "sanitizationResult": {
      "filterMatchState": "MATCH_FOUND",
      "invocationResult": "SUCCESS",
        "filterResults": {
          "rai": {
            "raiFilterResult": {
              "executionState": "EXECUTION_SUCCESS",
              "matchState": "MATCH_FOUND",
              "raiFilterTypeResults": {
          "dangerous": {
            "confidenceLevel": "MEDIUM_AND_ABOVE",
            "matchState": "MATCH_FOUND"
          },
          "sexually_explicit": {
            "matchState": "NO_MATCH_FOUND"
          },
          "hate_speech": {
            "matchState": "NO_MATCH_FOUND"
          },
          "harassment": {
            "matchState": "NO_MATCH_FOUND"
          }
        }
      }
      },
      "pi_and_jailbreak": {
      "piAndJailbreakFilterResult": {
        "executionState": "EXECUTION_SUCCESS",
        "matchState": "NO_MATCH_FOUND"
        }
      },
      "csam": {
      "csamFilterFilterResult": {
        "executionState": "EXECUTION_SUCCESS",
        "matchState": "NO_MATCH_FOUND"
      }
      },
      "malicious_uris": {
      "maliciousUriFilterResult": {
        "executionState": "EXECUTION_SUCCESS",
        "matchState": "NO_MATCH_FOUND"
      }
      },
      }
      }
      }

    Go

    
    import (
    	"context"
    	"fmt"
    	"io"
    
    	modelarmor "cloud.google.com/go/modelarmor/apiv1"
    	modelarmorpb "cloud.google.com/go/modelarmor/apiv1/modelarmorpb"
    	"google.golang.org/api/option"
    )
    
    // sanitizeModelResponse method sanitizes a model
    // response based on the project, location, and template settings.
    //
    // w io.Writer: The writer to use for logging.
    // projectID string: The ID of the project.
    // locationID string: The ID of the location.
    // templateID string: The ID of the template.
    // modelResponse string: The model response to sanitize.
    func sanitizeModelResponse(w io.Writer, projectID, locationID, templateID, modelResponse string) error {
    	ctx := context.Background()
    
    	// Create options for Model Armor client.
    	opts := option.WithEndpoint(fmt.Sprintf("modelarmor.%s.rep.googleapis.com:443", locationID))
    	// Create the Model Armor client.
    	client, err := modelarmor.NewClient(ctx, opts)
    	if err != nil {
    		return fmt.Errorf("failed to create client for location %s: %w", locationID, err)
    	}
    	defer client.Close()
    
    	// Initialize request argument(s)
    	modelResponseData := &modelarmorpb.DataItem{
    		DataItem: &modelarmorpb.DataItem_Text{
    			Text: modelResponse,
    		},
    	}
    
    	// Prepare request for sanitizing model response.
    	req := &modelarmorpb.SanitizeModelResponseRequest{
    		Name:              fmt.Sprintf("projects/%s/locations/%s/templates/%s", projectID, locationID, templateID),
    		ModelResponseData: modelResponseData,
    	}
    
    	// Sanitize the model response.
    	response, err := client.SanitizeModelResponse(ctx, req)
    	if err != nil {
    		return fmt.Errorf("failed to sanitize model response with user prompt for template %s: %w", templateID, err)
    	}
    
    	// Sanitization Result.
    	fmt.Fprintf(w, "Sanitization Result: %v\n", response)
    
    	return nil
    }
    

    Java

    
    import com.google.cloud.modelarmor.v1.DataItem;
    import com.google.cloud.modelarmor.v1.ModelArmorClient;
    import com.google.cloud.modelarmor.v1.ModelArmorSettings;
    import com.google.cloud.modelarmor.v1.SanitizeModelResponseRequest;
    import com.google.cloud.modelarmor.v1.SanitizeModelResponseResponse;
    import com.google.cloud.modelarmor.v1.TemplateName;
    import com.google.protobuf.util.JsonFormat;
    import java.io.IOException;
    
    public class SanitizeModelResponse {
    
      public static void main(String[] args) throws IOException {
        // TODO(developer): Replace these variables before running the sample.
    
        // Specify the Google Project ID.
        String projectId = "your-project-id";
        // Specify the location ID. For example, us-central1. 
        String locationId = "your-location-id";
        // Specify the template ID.
        String templateId = "your-template-id";
        // Specify the model response.
        String modelResponse = "Unsanitized model output";
    
        sanitizeModelResponse(projectId, locationId, templateId, modelResponse);
      }
    
      public static SanitizeModelResponseResponse sanitizeModelResponse(String projectId,
          String locationId, String templateId, String modelResponse) throws IOException {
    
        // Endpoint to call the Model Armor server.
        String apiEndpoint = String.format("modelarmor.%s.rep.googleapis.com:443", locationId);
        ModelArmorSettings modelArmorSettings = ModelArmorSettings.newBuilder().setEndpoint(apiEndpoint)
            .build();
    
        try (ModelArmorClient client = ModelArmorClient.create(modelArmorSettings)) {
          // Build the resource name of the template.
          String name = TemplateName.of(projectId, locationId, templateId).toString();
    
          // Prepare the request.
          SanitizeModelResponseRequest request = 
              SanitizeModelResponseRequest.newBuilder()
                .setName(name)
                .setModelResponseData(
                  DataItem.newBuilder().setText(modelResponse)
                  .build())
                .build();
    
          SanitizeModelResponseResponse response = client.sanitizeModelResponse(request);
          System.out.println("Result for the provided model response: "
              + JsonFormat.printer().print(response.getSanitizationResult()));
    
          return response;
        }
      }
    }

    Node.js

    /**
     * TODO(developer): Uncomment these variables before running the sample.
     */
    // const projectId = process.env.PROJECT_ID || 'your-project-id';
    // const locationId = process.env.LOCATION_ID || 'us-central1';
    // const templateId = process.env.TEMPLATE_ID || 'template-id';
    // const modelResponse = 'unsanitized model output';
    const {ModelArmorClient} = require('@google-cloud/modelarmor').v1;
    
    const client = new ModelArmorClient({
      apiEndpoint: `modelarmor.${locationId}.rep.googleapis.com`,
    });
    
    const request = {
      name: `projects/${projectId}/locations/${locationId}/templates/${templateId}`,
      modelResponseData: {
        text: modelResponse,
      },
    };
    
    const [response] = await client.sanitizeModelResponse(request);
    console.log(JSON.stringify(response, null, 2));

    PHP

    use Google\Cloud\ModelArmor\V1\Client\ModelArmorClient;
    use Google\Cloud\ModelArmor\V1\SanitizeModelResponseRequest;
    use Google\Cloud\ModelArmor\V1\DataItem;
    
    /**
     * Sanitizes a model response using the specified template.
     *
     * @param string $projectId The ID of your Google Cloud Platform project (e.g. 'my-project').
     * @param string $locationId The ID of the location where the template is stored (e.g. 'us-central1').
     * @param string $templateId The ID of the template (e.g. 'my-template').
     * @param string $modelResponse The model response to sanitize (e.g. 'my-model-response').
     */
    function sanitize_model_response(
        string $projectId,
        string $locationId,
        string $templateId,
        string $modelResponse
    ): void {
        $options = ['apiEndpoint' => "modelarmor.$locationId.rep.googleapis.com"];
        $client = new ModelArmorClient($options);
    
        $modelResponseRequest = (new SanitizeModelResponseRequest())
            ->setName("projects/$projectId/locations/$locationId/templates/$templateId")
            ->setModelResponseData((new DataItem())->setText($modelResponse));
    
        $response = $client->sanitizeModelResponse($modelResponseRequest);
    
        printf('Result for Model Response Sanitization: %s' . PHP_EOL, $response->serializeToJsonString());
    }

    Python

    Um diesen Code auszuführen, müssen Sie eine Python-Entwicklungsumgebung einrichten und das Model Armor Python SDK installieren.

    
    from google.api_core.client_options import ClientOptions
    from google.cloud import modelarmor_v1
    
    # TODO(Developer): Uncomment these variables.
    # project_id = "YOUR_PROJECT_ID"
    # location_id = "us-central1"
    # template_id = "template_id"
    # model_response = "The model response data to sanitize"
    
    # Create the Model Armor client.
    client = modelarmor_v1.ModelArmorClient(
        client_options=ClientOptions(
            api_endpoint=f"modelarmor.{location_id}.rep.googleapis.com"
        )
    )
    
    # Initialize request argument(s)
    model_response_data = modelarmor_v1.DataItem(text=model_response)
    
    # Prepare request for sanitizing model response.
    request = modelarmor_v1.SanitizeModelResponseRequest(
        name=f"projects/{project_id}/locations/{location_id}/templates/{template_id}",
        model_response_data=model_response_data,
    )
    
    # Sanitize the model response.
    response = client.sanitize_model_response(request=request)
    
    # Sanitization Result.
    print(response)
    

    Modellantwort mit aktivierter mehrsprachiger Erkennung bereinigen

    Sie können die mehrsprachige Erkennung für jede Anfrage aktivieren, indem Sie das Flag enableMultiLanguageDetection für jede einzelne Antwort auf true setzen. Optional können Sie die Quellsprache angeben, um genauere Ergebnisse zu erhalten. Wenn die Quellsprache nicht angegeben ist, wird sie automatisch erkannt, um die Unterstützung mehrerer Sprachen zu ermöglichen.

    curl -X POST \
    -d  '{"userPromptData":{"text":"[UNSAFE TEXT]"}, "multiLanguageDetectionMetadata": { "enableMultiLanguageDetection": true , "sourceLanguage": "jp"}}' \
    -H "Content-Type: application/json" \
    -H "Authorization: Bearer $(gcloud auth print-access-token)" \
    "https://modelarmor.LOCATION.rep.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/templates/TEMPLATE_ID:sanitizeModelResponse"

    Ersetzen Sie Folgendes:

    • PROJECT_ID: die ID des Projekts, zu dem die Vorlage gehört.
    • LOCATION: der Speicherort der Vorlage.
    • TEMPLATE_ID: die ID der Vorlage.

    Nächste Schritte