FieldMask로 데이터 업데이트

API로 에이전트 데이터를 업데이트할 때 전체 데이터 유형을 덮어쓰거나 데이터 유형의 특정 필드만 덮어쓸 수 있습니다. 모든 데이터를 실수로 덮어쓰지 않도록 일반적으로 특정 필드를 덮어쓰는 것이 가장 좋습니다. 특정 필드를 덮어쓰려면 업데이트 요청에 FieldMask를 제공합니다.

다음 예시에서는 Intent 유형의 표시 이름을 업데이트하기 위해 FieldMask를 제공하는 방법을 보여줍니다.

인텐트 참조의 프로토콜 및 버전 선택:

프로토콜 V3 V3beta1
REST 인텐트 리소스 인텐트 리소스
RPC 인텐트 인터페이스 인텐트 인터페이스
C++ IntentsClient 사용 불가능
C# IntentsClient 사용 불가능
Go IntentsClient 사용 불가능
자바 IntentsClient IntentsClient
Node.js IntentsClient IntentsClient
PHP 사용 불가능 사용 불가능
Python IntentsClient IntentsClient
Ruby 사용 불가능 해당 사항 없음

REST

요청 데이터를 사용하기 전에 다음을 바꿉니다.

  • PROJECT_ID: Google Cloud 프로젝트 ID
  • AGENT_ID: 에이전트 ID
  • REGION_ID: 리전 ID
  • INTENT_ID: 인텐트 ID
  • DISPLAY_NAME: 원하는 표시 이름

HTTP 메서드 및 URL:

PATCH https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/intents/INTENT_ID?updateMask=displayName

JSON 요청 본문:

{
  "displayName": "DISPLAY_NAME"
}

요청을 보내려면 다음 옵션 중 하나를 펼칩니다.

다음과 비슷한 JSON 응답이 표시됩니다.

{
  "name": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/intents/INTENT_ID",
  "displayName": "DISPLAY_NAME",
  ...
}

Java

CTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


import com.google.cloud.dialogflow.cx.v3.Intent;
import com.google.cloud.dialogflow.cx.v3.Intent.Builder;
import com.google.cloud.dialogflow.cx.v3.IntentsClient;
import com.google.cloud.dialogflow.cx.v3.UpdateIntentRequest;
import com.google.protobuf.FieldMask;
import java.io.IOException;

public class UpdateIntent {

  public static void main(String[] args) throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "my-project-id";
    String agentId = "my-agent-id";
    String intentId = "my-intent-id";
    String location = "my-location";
    String displayName = "my-display-name";
    updateIntent(projectId, agentId, intentId, location, displayName);
  }

  // DialogFlow API Update Intent sample.
  public static void updateIntent(
      String projectId, String agentId, String intentId, String location, String displayName)
      throws IOException {

    // Note: close() needs to be called on the IntentsClient object to clean up resources
    // such as threads. In the example below, try-with-resources is used,
    // which automatically calls close().
    try (IntentsClient client = IntentsClient.create()) {
      String intentPath =
          "projects/"
              + projectId
              + "/locations/"
              + location
              + "/agents/"
              + agentId
              + "/intents/"
              + intentId;

      Builder intentBuilder = client.getIntent(intentPath).toBuilder();

      intentBuilder.setDisplayName(displayName);
      FieldMask fieldMask = FieldMask.newBuilder().addPaths("display_name").build();

      Intent intent = intentBuilder.build();
      UpdateIntentRequest request =
          UpdateIntentRequest.newBuilder()
              .setIntent(intent)
              .setLanguageCode("en")
              .setUpdateMask(fieldMask)
              .build();

      // Make API request to update intent using fieldmask
      Intent response = client.updateIntent(request);
      System.out.println(response);
    }
  }
}

Node.js

CTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.


const {IntentsClient} = require('@google-cloud/dialogflow-cx');

const intentClient = new IntentsClient();

//TODO(developer): Uncomment these variables before running the sample.
//  const projectId = 'your-project-id';
//  const agentId = 'your-agent-id';
//  const intentId = 'your-intent-id';
//  const location = 'your-location';
//  const displayName = 'your-display-name';

async function updateIntent() {
  const agentPath = intentClient.projectPath(projectId);
  const intentPath = `${agentPath}/locations/${location}/agents/${agentId}/intents/${intentId}`;

  //Gets the intent from intentPath
  const intent = await intentClient.getIntent({name: intentPath});
  intent[0].displayName = displayName;

  //Specifies what is being updated
  const updateMask = {
    paths: ['display_name'],
  };

  const updateIntentRequest = {
    intent: intent[0],
    updateMask,
    languageCode: 'en',
  };

  //Send the request for update the intent.
  const result = await intentClient.updateIntent(updateIntentRequest);
  console.log(result);
}

updateIntent();

Python

CTS에 인증하려면 애플리케이션 기본 사용자 인증 정보를 설정합니다. 자세한 내용은 로컬 개발 환경의 인증 설정을 참조하세요.

from google.cloud.dialogflowcx_v3.services.intents import IntentsClient
from google.protobuf import field_mask_pb2

def update_intent(project_id, agent_id, intent_id, location, displayName):
    intents_client = IntentsClient()

    intent_name = intents_client.intent_path(project_id, location, agent_id, intent_id)

    intent = intents_client.get_intent(request={"name": intent_name})

    intent.display_name = displayName
    update_mask = field_mask_pb2.FieldMask(paths=["display_name"])
    response = intents_client.update_intent(intent=intent, update_mask=update_mask)
    return response