API를 사용하여 에이전트 관리

일반적으로 콘솔을 사용하여 에이전트를 만들고 삭제합니다. 그러나 일부 고급 시나리오에서는 API를 사용하는 것이 더 쉬울 수 있습니다.

에이전트 만들기

다음 예시에서는 Agent 유형에 대해 Create 메서드를 호출하는 방법을 보여줍니다.

에이전트 참조의 프로토콜 및 버전 선택:

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

REST

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

  • PROJECT_ID: Google Cloud 프로젝트 ID
  • REGION_ID: 리전 ID

HTTP 메서드 및 URL:

POST https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents

JSON 요청 본문:

{
  "displayName": "My display name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York"
}

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

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

{
  "name": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID",
  "displayName": "My display name",
  "defaultLanguageCode": "en",
  "timeZone": "America/New_York",
  "startFlow": "projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID/flows/00000000-0000-0000-0000-000000000000",
  "advancedSettings": {
    "loggingSettings": {}
  }
}

Java

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


import com.google.cloud.dialogflow.cx.v3.Agent;
import com.google.cloud.dialogflow.cx.v3.Agent.Builder;
import com.google.cloud.dialogflow.cx.v3.AgentsClient;
import com.google.cloud.dialogflow.cx.v3.AgentsSettings;
import java.io.IOException;

public class CreateAgent {

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

    createAgent(projectId, displayName);
  }

  public static Agent createAgent(String parent, String displayName) throws IOException {

    String apiEndpoint = "global-dialogflow.googleapis.com:443";

    AgentsSettings agentsSettings = AgentsSettings.newBuilder().setEndpoint(apiEndpoint).build();
    // Note: close() needs to be called on the AgentsClient object to clean up resources
    // such as threads. In the example below, try-with-resources is used,
    // which automatically calls close().
    try (AgentsClient client = AgentsClient.create(agentsSettings)) {
      // Set the details of the Agent to create
      Builder build = Agent.newBuilder();

      build.setDefaultLanguageCode("en");
      build.setDisplayName(displayName);
      // Correct format for timezone is location/city
      // For example America/Los_Angeles, Europe/Madrid, Asia/Tokyo
      build.setTimeZone("America/Los_Angeles");

      Agent agent = build.build();
      String parentPath = String.format("projects/%s/locations/%s", parent, "global");

      // Calls the create agent api and returns the created Agent
      Agent response = client.createAgent(parentPath, agent);
      System.out.println(response);
      return response;
    }
  }
}

Node.js

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


const parent = 'projects/' + projectId + '/locations/global';

const api_endpoint = 'global-dialogflow.googleapis.com';

const agent = {
  displayName: displayName,
  defaultLanguageCode: 'en',
  timeZone: 'America/Los_Angeles',
};

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

const client = new AgentsClient({apiEndpoint: api_endpoint});

async function setAgentSample() {
  const request = {
    agent,
    parent,
  };

  const [response] = await client.createAgent(request);
  console.log(`response: ${JSON.stringify(response, null, 2)}`);

  // Delete created agent resource
  client.deleteAgent({name: response.name});
}
await setAgentSample();

Python

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

from google.cloud.dialogflowcx_v3.services.agents.client import AgentsClient
from google.cloud.dialogflowcx_v3.types.agent import Agent

def create_agent(project_id, display_name):
    parent = "projects/" + project_id + "/locations/global"

    agents_client = AgentsClient()

    agent = Agent(
        display_name=display_name,
        default_language_code="en",
        time_zone="America/Los_Angeles",
    )

    response = agents_client.create_agent(request={"agent": agent, "parent": parent})

    return response

에이전트 삭제

다음 예시에서는 Agent 유형에 대해 Delete 메서드를 호출하는 방법을 보여줍니다.

에이전트 참조의 프로토콜 및 버전 선택:

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

REST

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

  • PROJECT_ID: Google Cloud 프로젝트 ID
  • REGION_ID: 리전 ID
  • AGENT_ID: 에이전트 만들기 응답에 있는 에이전트 ID

HTTP 메서드 및 URL:

DELETE https://REGION_ID-dialogflow.googleapis.com/v3/projects/PROJECT_ID/locations/REGION_ID/agents/AGENT_ID

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

성공 상태 코드(2xx)와 빈 응답을 받게 됩니다.