Chatbot queries by API
The AI Hub queries endpoint lets you integrate AI Hub chatbots into any workflow. You can send asynchronous queries, track their status, and retrieve responses.
This guide provides three script versions to help you get started.
-
Snippets - a sequence of code snippets to demonstrate API usage, omitting error handling for simplicity. Each snippet is presented and discussed in numbered steps below.
-
Complete script - all snippets are combined into a complete script for reference.
-
Enhanced script - an improved version of the complete script adds error handling and Python type annotations, resulting in near-production-quality code.
This guide uses Python, but you can access the queries endpoint with any language.
You can use the API to query any chatbot that you can interact with through the AI Hub user interface.
API chatbot queries are visible in your query history when accessing the chatbot from the AI Hub user interface.
Before you begin
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.
Import modules
Start your Python script by importing required modules. The requests
module makes it easier to send HTTP requests to the API.
If you don’t already have the requests
module installed on your system, run pip install requests
. There’s no harm in running this command even if the module is already installed.
Define URLs
You use the queries endpoint for all interactions with the chatbot, so you need to specify where to find that endpoint.
Your API_BASE_URL
depends on how you access AI Hub.
-
If you use SaaS AI Hub, set it to
https://aihub.instabase.com/api
. -
If you run AI Hub on a custom domain, set it to that domain plus
/api
, such ashttps://www.acme-widgets.com/aihub/api
.
Put authentication credentials in the request header
Every request to the AI Hub API must include authentication credential headers.
To find values for API_TOKEN
and IB_CONTEXT
, see the Account setup section of the Developer quickstart.
Provide the chatbot ID
Identify the chatbot to query by providing its unique ID in a Python dictionary.
For chatbot queries, the type
key must have the string CHATBOT
as its value.
The value for <CHATBOT-ID>
is in the chatbot URL. The chatbot ID is the 36-character string at the end of the URL, excluding the slash at the beginning of the string.
To find the chatbot’s URL, navigate to Hub and run the chatbot you want to send queries to.
Create the request payload
Creating a chatbot query by API is an asynchronous operation.
-
Send a request to the chatbot, with your query as its payload.
-
Check whether the chatbot has finished processing the query by sending another request.
-
If the chatbot is still processing, wait a few seconds and check its status again by sending another request.
-
Repeat the previous step as many times as needed until the chatbot is done processing your query. That response that includes
COMPLETE
status also includes the chatbot’s reply to your query.
The next snippet sends the request described in step one above. The API responds with a query ID that you must include in your processing status requests.
The initial request includes the query and key data about the chatbot. Store that information in a Python dictionary so you can include it in the request.
User-specified values
Submit the query and capture the query_id
You’re ready to submit your query to a chatbot as an HTTP request. The HTTP response to this request contains a query ID that you must include in future requests to check the chatbot’s processing status.
User-defined values
The response is a JSON object containing the query ID, for example:
Get the query processing status and query reply
After sending your query, you must send a separate request to retrieve the chatbot’s reply. Keep calling the get chatbot query status endpoint until the status returned in the response changes from RUNNING
to COMPLETE
. When that happens, the chatbot’s reply to your query is included in the most recent response.
User-defined parameters
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.
Sample JSON object in response
JSON schema
Because the snippet above converts the JSON object into a Python dictionary, the types listed in this schema are Python types, not JSON types.
Complete script
This example Python script shows a complete, end-to-end workflow based on calls to the queries endpoint. The code is deliberately minimal, to make it easier to learn how to use the API.
The script performs the following tasks:
-
Call the send chatbot query endpoint, sending an asynchronous request to query a specified chatbot. A query ID is returned.
-
Poll the get chatbot query status endpoint repeatedly, until the chatbot’s processing status changes from
RUNNING
toCOMPLETE
. -
Extract and print the reply and sources from the last response.
User-defined values
Enhanced script
This script is functionally identical to the complete script above but adds error handling and Python type annotation, bringing it closer to production-grade code.
The SDK supports Python 3.7+, but this script uses improved type annotations introduced in Python 3.9. To run this code on Python 3.7 or 3.8, remove or modify the type annotations.
The error handling in this script shows what can go wrong, but the recovery code is unrealistically basic. Replace sys.exit()
calls with error handling that’s appropriate for your workflow.
User-defined values
Most of the variables or parameters in this script are described in the User-defined values section above, but these three constants are only in the enhanced script.