Converse by SDK
With the AI Hub software development kit (SDK), you can write a Python script to chat with documents programmatically instead of using the graphical interface or sending API calls. You can create a new Converse conversation, upload files to the conversation, and converse with your uploaded files.
This guide includes a complete script to use as a model when writing Python scripts for having conversations.
Before you begin
Review the developer quickstart and then install the AI Hub SDK.
Import modules and initialize the client
The first step of using the SDK is to import key Python modules.
Next, initialize a client that lets you interact with the AI Hub API. For information on what values to use for the parameters, see the Developer quickstart.
Create a conversation with your files
Create a conversation and upload files to it using the client.conversations.create()
method.
User-defined values
See the conversations endpoint for more supported parameters and information, including instructions for enabling object detection.
When uploading files to a conversation, note 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 in the snippet above performs these tasks:
-
Create the conversation in your personal 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 conversation status
Use the client.conversations.status()
method to check if document processing is complete. After it has finished, you can converse with your documents.
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.
Collect document IDs
When your conversation reaches the COMPLETE
state, all documents have been processed and the conversation is ready to accept prompts to one or more uploaded documents via the client.conversations.converse()
method.
But before submitting a prompt, you must get the document ID for each document you uploaded to the conversation so you can direct prompts to the right documents.
Converse with a document
Submit your query to one or more of the documents you uploaded.
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.
User-defined values
The script sends the prompt to your documents and returns a JSON object, for example:
Response schema
When the JSON object is converted into a Python dictionary, it has this schema.
Complete workflow
This script demonstrates a complete, end-to-end workflow using the AI Hub SDK. It combines all the snippets from above.
The script performs these tasks:
-
Create a conversation and upload two documents to the conversation.
-
Check the processing status of the uploaded files.
-
When processing is complete, collect the document IDs of the uploaded files.
-
Send a prompt that draws on information from both documents.
-
Print the answer.
User-defined values
This table describes all placeholders in the complete script that can or must be defined.