Converse by SDK
The conversations endpoint lets you chat with documents programmatically instead of using the graphical interface. You can create a new Converse conversation, upload files to the conversation, and converse with your uploaded files.
This guide explains how to access the conversations endpoint with the Python SDK. All code snippets are also presented at the end of this guide as a complete script. Use that as a model when writing your own scripts to have conversations with documents.
Import modules and initialize the API client
Import key Python modules.
Initialize a client that lets you interact with the AI Hub API through Python objects and methods.
Parameter reference
Create a conversation and upload files
Create a conversation and upload files to it with one method call.
Parameter reference
See the conversations endpoint for more supported parameters and information, including instructions for enabling object detection.
Files uploaded to a conversation have these limitations:
-
Each document can be up to 50 MB and up to 800 pages.
-
Each request can upload up to 100 MB worth of documents.
-
A conversation can have up to 100 uploaded documents.
-
Certain file types are supported.
The client.conversations.create()
method performs several tasks.
-
Create the conversation in the specified workspace.
-
Upload files to the conversation.
-
Digitize and index the files.
-
Return the conversation ID, conversation name, and upload status as a JSON object. For example:
Response schema
When the JSON object is converted into a Python dictionary, it has this schema.
Notes about the schema
-
i
in[i]
represents any valid array index, such as 0, 1, or 2. -
Array indices are zero-based, so
upload_status.success[2].name
gives the name of the third file uploaded. -
.id
,.name
, and.upload_status.success[i].name
are the most commonly used fields.
Check document processing state
Repeatedly check the state of document processing until it’s COMPLETE
.
client.conversations.status()
returns a JSON object with information about the conversation and its document upload and processing status. For example:
Response schema
When the JSON object is converted into a Python dictionary, it has this schema.
Notes about the object schema
-
i
in[i]
represents any valid array index, such as 0, 1, or 2. -
Array indices are zero-based, so
documents[2].name
gives the name of the third file uploaded.
Converse with a document
When the document processing state is COMPLETE
, all documents are processed and the conversation is ready to accept prompts to one or more uploaded documents.
The SDK officially supports queries directed at a single document only.
However, queries directed at multiple documents (like the example shown here) often work as expected.
If you see errors or timeouts while sending a query to multiple documents, make separate queries to each document instead.
Parameter reference
After the script sends the prompt to your conversation, AI Hub returns a JSON object. For example:
Response schema
When the JSON object is converted into a Python dictionary, it has this schema.
Complete script
This script combines all the snippets from above and contains all the code needed to converse with documents using the SDK.
The script performs these tasks:
-
Import modules and Initialize the API client.
-
Create a conversation and upload documents to the conversation.
-
Check the processing status of the uploaded files.
-
When processing is complete, send a prompt that draws on information from both documents.
-
Print the answer.
Parameter reference
This table describes all placeholders in the complete script that can or must be defined.