Gestisci gli agenti con l'API

Normalmente, gli agenti vengono creati ed eliminati utilizzando la console. Tuttavia, in alcuni scenari avanzati, potresti trovare più semplice utilizzare l'API.

Crea un agente

I seguenti esempi mostrano come chiamare il metodo Create per il tipo Agent.

Seleziona un protocollo e una versione per il riferimento dell'agente:

Protocollo V3 V3beta1
REST Risorsa agente Risorsa agente
RPC Interfaccia dell'agente Interfaccia dell'agente
C++ AgentsClient Non disponibile
C# AgentsClient Non disponibile
Vai AgentsClient Non disponibile
Java AgentsClient AgentsClient
Node.js AgentsClient AgentsClient
PHP Non disponibile Non disponibile
Python AgentsClient AgentsClient
Ruby Non disponibile Non disponibile

REST

Prima di utilizzare i dati della richiesta, effettua le seguenti sostituzioni:

  • PROJECT_ID: l'ID del tuo progetto Google Cloud
  • REGION_ID: il tuo ID regione

Metodo HTTP e URL:

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

Corpo JSON della richiesta:

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

Per inviare la richiesta, espandi una delle seguenti opzioni:

Dovresti ricevere una risposta JSON simile alla seguente:

{
  "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

Per eseguire l'autenticazione in Dialogflow, configura Credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.


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

Per eseguire l'autenticazione in Dialogflow, configura Credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.


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

Per eseguire l'autenticazione in Dialogflow, configura Credenziali predefinite dell'applicazione. Per ulteriori informazioni, vedi Configura l'autenticazione per un ambiente di sviluppo locale.

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

Elimina un agente

I seguenti esempi mostrano come chiamare il metodo Delete per il tipo Agent.

Seleziona un protocollo e una versione per il riferimento dell'agente:

Protocollo V3 V3beta1
REST Risorsa agente Risorsa agente
RPC Interfaccia dell'agente Interfaccia dell'agente
C++ AgentsClient Non disponibile
C# AgentsClient Non disponibile
Vai AgentsClient Non disponibile
Java AgentsClient AgentsClient
Node.js AgentsClient AgentsClient
PHP Non disponibile Non disponibile
Python AgentsClient AgentsClient
Ruby Non disponibile Non disponibile

REST

Prima di utilizzare i dati della richiesta, effettua le seguenti sostituzioni:

  • PROJECT_ID: l'ID del tuo progetto Google Cloud
  • REGION_ID: il tuo ID regione
  • AGENT_ID: il tuo ID agente, presente nella risposta di creazione dell'agente

Metodo HTTP e URL:

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

Per inviare la richiesta, espandi una delle seguenti opzioni:

Dovresti ricevere un codice di stato di operazione riuscita (2xx) e una risposta vuota.