Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

issue: Call Vertex agent via API

we need to call our Vertex agent via API from a 3rd party application: Zoho Cliq, we code a customized command with Zoho Deluge code as below:

 

 

response = Map();
AGENT_ID = "da862d45-5d7c-43cf";
PROJECT_ID = "sig-knowledge-base";
REGION_ID = "eu";
SESSION_ID = "test-session-1007";
url = "https://" + REGION_ID + "-dialogflow.googleapis.com/v3/projects/" + PROJECT_ID + "/locations/REGION_ID/agents/" + AGENT_ID + "/sessions/" + SESSION_ID + ":detectIntent";
t = "Bearer TOKENSTRING";
data = Map();
header = Map();
header.put("Authorization",t);
header.put("x-goog-user-project",PROJECT_ID);
header.put("Content-Type","application/json; charset=utf-8");
requestBodyMap = Map();
// Create a map for queryInput
queryInputMap = Map();
// Set the text property within queryInput
textMap = Map();
textMap.put("text",arguments);
// Replace with your actual user input
queryInputMap.put("text",textMap);
// Set the languageCode property within queryInput
queryInputMap.put("languageCode","en");
// You can change the language code if needed
// Create a map for queryParams
queryParamsMap = Map();
// Set the timeZone property within queryParams
queryParamsMap.put("timeZone","Europe/Paris");
// You can change the time zone if needed
// Add queryInput and queryParams to the main request body map
requestBodyMap.put("queryInput",queryInputMap);
requestBodyMap.put("queryParams",queryParamsMap);
info requestBodyMap;
response = invokeurl
[
url :url
type :POST
parameters:requestBodyMap.toString()
headers:header
];
info response;
return response;

 

 

In order to get that TOKENSTRING in above code, we do in below steps:
1. create a new service account in Vertex agent project
Screenshot 2024-06-28 at 16.11.40.png

2. generate a sig.json key

3. generate token string with below python code

 

 

from google.auth import jwt
import json
from google.auth.transport.requests import Request
# Load the JSON key file
with open('sig.json', 'r') as file:
    key_data = json.load(file)
audience = "https://cliq.zoho.eu"
credentials = jwt.Credentials.from_service_account_info(key_data,audience=audience)
print(credentials)

if not credentials.valid:
    credentials.refresh(Request())
# Get the access token
access_token = credentials.token

decoded_string = access_token.decode('utf-8')
print(decoded_string)

 

 

By running the above Deluge code, I always get the error message as below:
{

}

Thanks for any help on this issue,  I did try but could not find even an example showing API call to Vertex agent on the web.

 

 

6 0 111
0 REPLIES 0