Making chatbot queries by API
With the AI Hub Chatbots endpoint, you can integrate the ability to query an AI Hub chatbot into your workflow. You can create a new asnchronous query, get the query status, and return the query response. This guide includes a complete script that you can reference.
Review the developer quickstart to ensure your account is set up correctly. Also review the authorization and context identification documentation, ensuring you have an API token and can define the IB-Context
header.
Creating a chatbot query
You can query any AI Hub chatbot to which you have access using the Create chatbot query (async) endpoint. Creating a chatbot query by API is an asynchronous operation, meaning you must retrieve the chatbot’s response in a separate request.
This script makes an asynchronous request to query the specified chatbot. A query ID is returned, which is referenced later in the complete workflow to retrieve the chatbot response.
User-defined parameters include:
source_app/type
value is always CHATBOT
for chatbot queries. You can leave the parameter’s value as it’s defined in the script.The response is a JSON object containing the query ID, for example:
Checking query status and returning query response
After sending the create query request, a separate request is required to return the generated response.
This script performs the following tasks:
-
Makes a call to the Get query status and response endpoint to get the status of the query request.
-
If the status of the chatbot query is
RUNNING
, continues polling the endpoint until the status value isCOMPLETE
. -
When the status of the chatbot query is
COMPLETE
, returns the results of the query in JSON format.
User-defined parameters include:
query_id
parameter. When using the complete script, the script automatically passes through the query ID returned by the previous request.The response body is a JSON object containing the query ID and status. If the query status is COMPLETE
, the query response is also returned. If the query status is FAILED
an error message is returned instead.
The results array includes the query response and, if include_source_info
was enabled when querying the chatbot, information about the source documents referenced when generating the response.
Complete script
This example Python script shows a complete, end-to-end workflow based on calls to the Chatbots endpoint. When run, this script performs the following tasks:
-
Makes a call to the Create chatbot query (async) endpoint, sending an asynchronous request to query a specified chatbot. A query ID is returned.
-
Makes a call to the Get query status and response endpoint to get the status of the query request. The script automatically passes through the query ID returned in the previous step.
-
If the status of the chatbot query is
RUNNING
, continues polling the endpoint until the status value isCOMPLETE
. -
When the status of the chatbot query is
COMPLETE
, returns the results of the query in JSON format.
User-defined values
The following values in the complete script can or must be defined. Each value is required unless otherwise noted.
source_app/type
value is always CHATBOT
for chatbot queries. You can leave the parameter’s value as it’s defined in the script.