Zhang Zhan
Zhang Zhan

Reputation: 905

How to specify agent ID when calling Dialogflow API?

I am using the following NodeJS code to make an API call to Dialogflow to send message "Hello there", I am able to specify project ID, session ID but I am not sure where I can specify agent ID.

  let projectId = 'DEMO PROJECT';
  let message ='Hello there';
  // A unique identifier for the given session
  const sessionId = uuid.v4();

  // Create a new session
  const sessionClient = new dialogflow.SessionsClient();
  const sessionPath = sessionClient.sessionPath(projectId, sessionId);

  // The text query request.
  const request = {
    session: sessionPath,
    queryInput: {
      text: {
        // The query to send to the dialogflow agent
        text: message,
        // The language used by the client (en-US)
        languageCode: 'en-US',
      },
    },
  };

  // Send request and log result
  const responses = await sessionClient.detectIntent(request);

The problem is I am pragmatically creating agents under project DEMO, therefore, I need to specify which agent this conversation is going to send to. At the moment I am only able to specify project ID and session ID, however, my question is how can I specify agent ID?

Upvotes: 1

Views: 951

Answers (2)

Mohit Arvind khakharia
Mohit Arvind khakharia

Reputation: 417

As mentioned here you can create multiple agents but within the same region.

Note: You can only create one agent per region for a GCP project. If you need multiple agents in the same region, you will need to create multiple projects.

In python, I had to use the new library that allows agent id input. I managed to fix it using this example instead of this.

Basically using this

from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
from google.cloud.dialogflowcx_v3beta1.types import session

instead of

from google.cloud import dialogflow

Full working code:

import argparse
import uuid
from google.cloud.dialogflowcx_v3beta1.services.agents import AgentsClient
from google.cloud.dialogflowcx_v3beta1.services.sessions import SessionsClient
from google.cloud.dialogflowcx_v3beta1.types import session

def run_sample():
    project_id = "xxxxxt"
    location_id = "us-central1"
    agent_id = "aaaaa-bbbb-cccc-dddd-eeeeeeeee"
    agent = f"projects/{project_id}/locations/{location_id}/agents/{agent_id}"
    session_id = uuid.uuid4()
    texts = ["Tell me a sailor joke?"]
    language_code = "en-us"

    detect_intent_texts(agent, session_id, texts, language_code)

def detect_intent_texts(agent, session_id, texts, language_code):
    """Returns the result of detect intent with texts as inputs.

    Using the same `session_id` between requests allows continuation
    of the conversation."""
    session_path = f"{agent}/sessions/{session_id}"
    print(f"Session path: {session_path}\n")
    client_options = None
    agent_components = AgentsClient.parse_agent_path(agent)
    location_id = agent_components["location"]
    if location_id != "global":
        api_endpoint = f"{location_id}-dialogflow.googleapis.com:443"
        print(f"API Endpoint: {api_endpoint}\n")
        client_options = {"api_endpoint": api_endpoint}
    session_client = SessionsClient(client_options=client_options)

    for text in texts:
        text_input = session.TextInput(text=text)
        query_input = session.QueryInput(text=text_input, language_code=language_code)
        request = session.DetectIntentRequest(
            session=session_path, query_input=query_input
        )
        response = session_client.detect_intent(request=request)

        print("=" * 20)
        print(f"Query text: {response.query_result.text}")
        response_messages = [
            " ".join(msg.text.text) for msg in response.query_result.response_messages
        ]
        print(f"Response text: {' '.join(response_messages)}\n")


if __name__ == "__main__":
    run_sample()

Upvotes: 0

Lambda
Lambda

Reputation: 46

According to google official document

Note: You can only create one agent for a GCP project. If you need multiple agents, you will need to create multiple projects.

That is why you are not able to specify the agent ID as there is only 1 agent under each project.

Upvotes: 3

Related Questions