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.

The script in this guide written in Python, but you can reference the Chatbots endpoint documentation to do the same in any scripting language.
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.

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.

1# Query Chatbot
2def query_chatbot(data: Dict[Text, Any]) -> Response:
3 print('Querying chatbot')
4 response = requests.post(url=url, headers=API_HEADERS, data=json.dumps(data))
5 return response
6
7data: Dict[Text, Any] = {}
8data['query'] = '<CHATBOT-QUERY>' # Define your chatbot query
9data['model_name'] = '<MODEL-NAME>' # Define model to use, 'multistep-lite' (multistep model) or 'multistep' (research mode)
10# Set include_source_info to 'true' to get source documents from which the response is fetched
11data['include_source_info'] = False
12data['source_app'] = [{'type':'CHATBOT','uuid':'<CHATBOT-ID>'}]
13
14# Query the chatbot
15response = query_chatbot(data)
16
17if response.ok:
18 resp_data = response.json()
19 query_id = resp_data.get('query_id')
20 if not query_id:
21 print('Unable to query the chatbot.')
22
23 print(f'Response from query chatbot : {resp_data}')
24else:
25 print(f'Unable to connect to the chatbot query API - {response}.')
26 print(f'Response status code: {response.status_code}')
27 print(f'Response content: {response.text}')

User-defined parameters include:

ParameterTypeDescription and values
querystringYour chatbot query.
Chatbot queries made by API are also visible in your query history when accessing the chatbot from the AI Hub user interface.
model_namestringOptional, defaults to multistep-lite. The model to use to answer the question. Supports the multistep model and research mode, a more powerful but slower variant of the multistep model that’s suited to complex reasoning queries. See Choosing a model for details.

Valid values are multistep-lite (multistep model) and multistep (research mode).
include_source_infobooleanOptional, defaults to false. Set to true to return information about the source documents referenced when generating the query response.
The multistep model (model_name set to multistep-lite) supports document-level source information. Research mode (model_name set to multistep) supports page-level source information.
source_app/uuidstringThe ID of the chatbot to query. You can query any AIHub chatbot to which you have access.
You can find a chatbot’s ID in its AI Hub URL, such as https://aihub.instabase.com/hub/ apps/788eba40-5a1f-45f4-ad56-57fb1abe62c7.
A chatbot’s ID changes with every version update. For convenience, when making a request to a given version of a chatbot, the query is always automatically directed to the latest version of the chatbot.
The 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:

1{
2 "query_id": "f2ef9702-b018-4a45-9b2e-d1fb575b42ed"
3}

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 is COMPLETE.

  • When the status of the chatbot query is COMPLETE, returns the results of the query in JSON format.

1def get_query_status(query_id: Text) -> Tuple[Dict, Text]:
2 print(f'Getting query request status for {query_id}')
3 while True:
4 response = requests.get(f'{url}/{query_id}', headers=API_HEADERS)
5 if not response.ok:
6 return None, f'Error while getting the status for query request : {response.status_code} - {response.text}'
7
8 response_json = response.json()
9 status = response_json.get('status')
10 if not status:
11 return None, f'Getting the status for query request : {response_json}'
12
13 # If status of query request is COMPLETE stop polling and return
14 if status in ['COMPLETE']:
15 return response_json, None
16
17 print('Query still processing, please wait!')
18
19 # If it is RUNNING, the script waits for 5 seconds before the next poll
20 time.sleep(5)
21
22# Get the status of query request
23query_id = '<QUERY-ID>' # Define query ID
24response_json, err = get_query_status(query_id=query_id)
25if err:
26 print(f'Error occurred: {err}')
27else:
28 print(f'Query request status : {response_json}')

User-defined parameters include:

ParameterTypeDescription and values
query_idstringThe ID of the query. This value is included in the response to creating a chatbot query.
In this script, the query ID must be defined in the 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.

KeyTypeDescription
query_idstringThe ID of the query.
statusstringStatus of the query. Possible values are RUNNING, COMPLETE, or FAILED.
resultsarrayThe query response. Returned only if the query status is COMPLETE.
results/responsestringThe response message generated by the chatbot.
results/source_documentsarrayReturned if include_source_info was set to true. Information about the source documents referenced when generating the query response.
results/source_documents/namestringThe name of the source document referenced.
results/source_documents/pagesarrayAn array of page objects with information about the specific pages referenced in the source document.
Page-level source information is supported only when using research mode.
results/source_documents/pages/page_numberfloatThe number of the page referenced in the source document.
results/source_documents/pages/bboxesstringBounding boxes for the text referenced on the page, if applicable.
errorobjectPresent only if the status is FAILED. Contains a message with information about the source of the execution error.

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.

1{
2 "query_id":"f2ef9702-b018-4a45-9b2e-d1fb575b42ed",
3 "results":[
4 {
5 "response":"<QUERY-RESPONSE>",
6 "source_documents":[
7 {
8 "name":"file1.pdf",
9 "pages":[
10 ]
11 },
12 {
13 "name":"file2.pdf",
14 "pages":[
15 ]
16 }
17 ]
18 }
19 ],
20 "status":"COMPLETE"
21}

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 is COMPLETE.

  • When the status of the chatbot query is COMPLETE, returns the results of the query in JSON format.

1import time, json
2from typing import Dict, Text, Tuple, Any
3import requests
4from requests import Response
5
6API_TOKEN = '<API-TOKEN>' # Define your API token
7IB_CONTEXT = '<ORGANIZATION-ID or USER-ID>' # Define organization ID to use organization account, define user ID to use community account
8
9API_BASE_URL = 'https://aihub.instabase.com/api' # Edit root API URL if your organization has a custom domain
10url = API_BASE_URL + 'v2/queries'
11API_HEADERS = {
12 'Authorization': f'Bearer {API_TOKEN}',
13 'IB-Context': f'{IB_CONTEXT}'
14}
15
16def query_chatbot(data: Dict[Text, Any]) -> Response:
17 print('Querying chatbot')
18 response = requests.post(url=url, headers=API_HEADERS, data=json.dumps(data))
19 return response
20
21def get_query_status(query_id: Text) -> Tuple[Dict, Text]:
22 print(f'Getting query request status for {query_id}')
23 while True:
24 response = requests.get(f'{url}/{query_id}', headers=API_HEADERS)
25 if not response.ok:
26 return None, f'Error while getting the status for query request : {response.status_code} - {response.text}'
27
28 response_json = response.json()
29 status = response_json.get('status')
30 if not status:
31 return None, f'Getting the status for query request : {response_json}'
32
33 # If status of query request is COMPLETE stop polling and return
34 if status in ['COMPLETE']:
35 return response_json, None
36
37 print('Query still processing, please wait!')
38
39 # If it is RUNNING, the script waits for 5 seconds before the next poll
40 time.sleep(5)
41
42data: Dict[Text, Any] = {}
43data['query'] = '<CHATBOT-QUERY>' # Define your chatbot query
44data['model_name'] = '<MODEL-NAME>' # Define model to use, 'multistep-lite' (multistep model) or 'multistep' (research mode)
45data['include_source_info'] = False # Set to 'true' to get information about source documents referenced when generating response
46data['source_app'] = [{'type':'CHATBOT','uuid':'<CHATBOT-ID>'}] # Define chatbot ID
47
48# Query the chatbot
49response = query_chatbot(data)
50query_id = None
51if response.ok:
52 resp_data = response.json()
53 query_id = resp_data.get('query_id')
54 if not query_id:
55 print('Unable to query the chatbot.')
56
57 print(f'Response from query chatbot : {resp_data}')
58else:
59 print(f'Unable to connect to the chatbot query API - {response}.')
60 print(f'Response status code: {response.status_code}')
61 print(f'Response content: {response.text}')
62
63
64# Get the status of query request
65if query_id:
66 response_json, err = get_query_status(query_id=query_id)
67 if err:
68 print(f'Error occurred: {err}')
69 else:
70 print(f'Query request status : {response_json}')

User-defined values

The following values in the complete script can or must be defined. Each value is required unless otherwise noted.

Parameter or variableTypeDescription and values
<API-TOKEN>stringYour API token. See Authorization for details.
<API-ROOT>stringOptional, defaults to https://aihub.instabase.com/api.

If your organization has a custom AI Hub domain, enter your organization’s root API URL, such as https://my-organization.instabase.com/api.
<IB-CONTEXT>stringOptional but recommended, defaults to your user ID and requests are made with your community account.

To use your organization account, define as your organization ID.

To use your community account, define as your user ID.
querystringYour chatbot query.
Chatbot queries made by API are also visible in your query history when accessing the chatbot from the AI Hub user interface.
In the script, replace <CHATBOT-QUERY> with your chatbot query.
model_namestringOptional, defaults to multistep-lite. The model to use to answer the question. Supports the multistep model and research mode, a more powerful but slower variant of the multistep model that’s suited to complex reasoning queries. See Choosing a model for details.

Valid values are multistep-lite (multistep model) and multistep (research mode).

In the script, replace <MODEL-NAME>.
include_source_infobooleanOptional, defaults to false. Set to true to return information about the source documents referenced when generating the query response.
The multistep model (model_name set to multistep-lite) supports document-level source information. Research mode (model_name set to multistep) supports page-level source information.
source_app/uuidstringThe ID of the chatbot to query. You can query any AIHub chatbot to which you have access.
You can find a chatbot’s ID in its AI Hub URL, such as https://aihub.instabase.com/hub/ apps/c796baad-a50d-46b4-8a78-b53b8acd5a88.
A chatbot’s ID changes with every version update. For convenience, when making a request to a given version of a chatbot, the query is always automatically directed to the latest version of the chatbot.
In the script, replace <CHATBOT-ID>.
The script as written handles passing the query ID from the initial create query request onto the get status and response request. The source_app/type value is always CHATBOT for chatbot queries. You can leave the parameter’s value as it’s defined in the script.
Was this page helpful?