Make chatbot queries by API
The queries endpoint lets you integrate AI Hub chatbots into any workflow. You can send asynchronous queries, track their status, and retrieve replies.
This guide explains how to interact with the queries endpoint by making direct API calls. Step-by-step code snippets and a complete script provide a reference for writing your own scripts to interact with chatbots.
This page focuses on using the API to interact with the queries endpoint. See the Make chatbot queries by SDK guide to learn how to use the Python SDK to do the same.
Three types of code appear on this page.
-
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.
Any chatbot that you can access through the AI Hub user interface can also be accessed with the API.
API chatbot queries are visible in your query history when accessing the chatbot from the AI Hub user interface.
Import modules
Start your Python script by importing required modules. The third-party 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 the install command even if the module is already installed.
Define URLs
Because you use the queries endpoint for all interactions with chatbots, you must 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
.
Define authentication credentials
Every request to the AI Hub API must include authentication credential headers.
To find values for api_token
and ib_context
, see the Code section of the Using the API & SDK page.
Provide the chatbot ID
Identify the chatbot to query by storing its ID in a Python dictionary.
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
Querying a chatbot 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. The first response that includes
COMPLETE
status also includes the chatbot’s reply to your query.
The next snippet stores the query and key data about the chatbot. Include the data in your first request to the chatbot.
Parameter reference
Submit the query and capture the query_id
You’re ready to submit a query to the chatbot as an HTTP request. The HTTP response to the request contains a query ID that you must include in future requests to check the chatbot’s processing status.
Parameter reference
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 API operation until the status returned in the response changes from RUNNING
to COMPLETE
. When the status is COMPLETE
, the chatbot reply to your query is included in the response.
Parameter reference
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 script combines all the snippets from above and contains all the steps needed to call the queries endpoint and interact with a chatbot. The code is deliberately minimal, to aid readability.
The script performs the following tasks:
-
Call the send chatbot query operation, 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.
Parameter reference for complete script
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. Keep these guidelines and best practices in mind as you review or use this script:
-
The included error handling shows what can go wrong, but the recovery code is unrealistically basic. Before running this code in a production environment, replace
sys.exit()
calls with error handling appropriate to your workflow. -
The SDK supports Python 3.7+, but this script uses improved type annotations introduced in later versions. You can safely remove the type annotations if they cause problems with your version of Python.
-
This script is presented as a single block of code to aid readability, but refactoring it into smaller functions would improve testability.
Parameter reference
Most of the variables or parameters in this script are described in the Parameter reference for complete script section above, but these three constants appear only in the enhanced script.