Analyze use case
You’ve just seen how to use the SDK to interact with the automate feature of AI Hub. Now learn how the SDK can drive the analyze feature.
Analyzing documents with an AI Hub conversation
In this use case you have a conversation with AI Hub about technical specifications for Airbus airplanes.
The terms file and document aren’t interchangeable!
A file is what you upload from your computer. It contains one or more documents. A document is the entity that you interact with during a conversation.
For example, a file called bank_info.pdf
might contain an image of a driver’s license on the first page and a mortgage application on the second page. This one file contains two documents, and you could analyze just one or both of those documents within a conversation.
Each step of this workflow uses the SDK.
-
Create a conversation that contains one file, where that file contains one document.
-
Converse with a document by sending a question synchronously.
-
Upload another single-document file to the conversation.
-
Send a question asynchronously that requires AI Hub to look at both documents in the conversation.
-
Wait until the conversation has an answer to your question.
-
Retrieve and print the answer.
GUI vs. SDK
When using AI to answer questions about documents, you’re probably used to using a GUI: type a prompt, get a response. While you can use AI Hub’s GUI to analyze documents this way, there are cases where it makes more sense to analyze documents programmatically with the SDK instead.
Imagine that you have thousands of medical documents, and you want to ask the same question about each document. For privacy reasons you can’t upload all the documents at the same time, so you upload one document, ask a question about it, and repeat the process many times. Manual repetition through the GUI becomes error-prone and time-consuming, but programmatic execution makes the task quick and reliable.
Or you might want to record the questions and answers from a conversation for analysis in another system. SDK-enabled programs make this easier to do than manually copying and pasting each interaction from the GUI.
Implementing the use case
Import and authorize the SDK
Just like for the automate use case, start your program by making the SDK available and configuring authorization details.
analyze_with_sdk.py
. As the tutorial introduces more code, add it to bottom of the same file.Create a conversation with one file
When you created a batch in the previous use case, you made the batch with one SDK operation, and you uploaded files to it with a separate SDK operation. Creating a conversation is different: you use a single SDK operation to create the conversation and upload files to it (although you can add more files later).
This example creates a conversation with a single file called A330_specs.pdf
, which you downloaded earlier.
org
parameter.Remember to replace the placeholder path with the actual path to your local copy of A330_specs.pdf
.
If you’re on Windows, there’s no need to change the slashes in the files
parameter to Windows-compatible backslashes. Python automatically converts path separators based on operating system.
Wait for the document to be processed
When you create a conversation, AI Hub processes any documents you included in the conversation so that it’s prepared to answer questions about them. The information-rich, three-page PDF you included in the conversation could take up to 30 seconds to process.
This code snippet repeatedly polls the status of the conversation until it’s finished processing any documents the conversation contains.
Line 1 makes Python’s time
library available so you can pause the program between status checks.
Line 2 starts a loop for checking document processing status. The loop stops when processing is done.
Line 3 pauses for a few seconds to give AI Hub a chance to make progress on processing the document.
Lines 4 and 5 call an SDK method to check on the document processing status, passing in the conversation’s ID.
Lines 6 prints the status.
Lines 7 and 8 break out of the loop if AI Hub has finished processing the document.
Get the document ID
When you send a question to an AI Hub conversation, you have to tell AI Hub two things.
-
Which conversation is the question directed at?
-
Which documents within the conversation does AI Hub consider when coming up with an answer?
The second task uses document IDs. This code looks in the response to the last processing status query and extracts the ID of the one document you uploaded along with the conversation.
Because there’s only one document, get the ID of document 0 (Python uses 0-based indexing) instead of iterating through all documents.
Submit a question to one document
Now you’re ready to analyze the document by asking AI Hub questions about it—or as AI Hub sometimes calls it, conversing with the document.
Even though the document_ids
parameter must be a list of document IDs, AI Hub only looks at the first document in the list. This leads to some non-obvious considerations:
-
To ask a question of the document with ID 123, pass in
[123]
instead of just123
. -
If you pass in
[123, 789]
, AI Hub ignores document ID 789. -
To submit a question whose answer draws on more than one document, see the step involving the client.queries.run() method below.
Print the answer
The client.conversations.converse()
method is synchronous, which means an answer is provided in the method’s response. There’s no need to check the conversation’s processing status or make another call to retrieve the answer.
Upload another document to the conversation
As you learned earlier, a conversation can have more than one document associated with it. Upload a second document containing technical specs for a different family of Airbus airplanes.
Wait for the second document to be processed
You need to wait for the new document to be processed before you can send a question to the conversation. This code uses the same technique you used while waiting for the first document to be processed.
Send a question to both documents asynchronously
Both documents are processed, so now you can send a question that requires AI Hub to consider the contents of both documents when answering.
As you’ve already seen, the method for submitting a question to a single document is synchronous.
Questions that draw on multiple documents require an asynchronous SDK operation. That means that the answer isn’t included in the response to the initial method call—request it separately using another SDK operation.
Line 1 defines the question you want AI Hub to answer.
Lines 2 and 3 tell AI Hub whether to direct the question to a conversation or a chatbot, and specify the conversation or chatbot ID.
Lines 5 and 6 submit the question.
Wait for the conversation to have an answer
Because multi-document queries are asynchronous, you need to check the status of your question until AI Hub comes up with an answer.
Line 1 starts a loop that stops when AI Hub has an answer ready.
Line 2 pauses for a few seconds to give AI Hub time to process your question.
Lines 3 and 4 ask AI Hub for the status of the query, using its ID.
Line 5 prints the most recent query status.
Lines 6 and 7 break out of the loop when the status is COMPLETE
.
Retrieve and print the answer
When AI Hub responds to your client.queries.status()
call with a status of COMPLETE
, that response includes a field called results
that contains the answer to your question. Now you just have to extract and print it.
The results
field holds a list of results. Depending on what questions you ask and how many documents are in the conversation, the list might contain one result or it might contain more. In this example there’s only one result, but it’s always safest to iterate through the list and print each result.
Run the SDK-enabled program
You’ve now seen all the code for this use case. It’s time to run it.
-
If you haven’t been entering the code into a file on your computer called
analyze_with_sdk.py
, do so now. -
Run it the same way you ran
automate_with_sdk.py
, following the instructions for your operating system.Linux
macOS
Windows
-
Open a terminal.
-
Type
cd PATH/TO/DIRECTORY/WITH/analyze_with_sdk.py
, replacing the placeholders with the path to wherever you storedanalyze_with_sdk.py
. -
Type
python3 analyze_with_sdk.py
.
-
Your output looks similar to this. Results might vary due to randomness inherent in AI.
Add exception handling
Just as with the automate use case you looked at earlier, there are many possible problems you could address in this program. Here’s just one.
In step 11 you called the client.queries.status()
method to check if AI Hub was finished generating an answer to your question. You stop checking when the status is COMPLETE
, but you can also stop checking if something goes wrong and the status is FAILED
.
In this snippet, lines 1 through 7 are already in your program, near the end.
Add lines 8 through 10 to handle FAILED
processing status.
while True
loop.Complete program
This code assembles all the snippets for this use case into a single program. In this version, all the import
statements have moved to the top and the code is thoroughly commented.
Replace the code you’ve typed in so far with this version, which the third use case expects. Save it as analyze-with-sdk.py
.
Troubleshooting
Many of the problems and solutions described in the automate use case also apply to the analyze use case. Other things could go wrong with this use case, too. As before, look at the end of the error message to find key information.
No such file or directory: '/PATH/TO/A330_specs.pdf'
Double-check the path.
If you’re having trouble getting a relative path correct, temporarily add import os; print(os.getcwd())
to the top of your program. This prints the current working directory, which is where relative paths start from.
If you’re still having trouble, use absolute paths.
2.document_ids Input should be a valid list [type=list_type, input_value=123456, input_type=int]
When calling client.conversations.converse()
, you must pass a List of IDs, not a single bare ID. This can be confusing because AI Hub only uses the first ID in the list.
Next steps
Here are some things to try if you’d like more practice with the SDK or Python.
-
If you’d like to make the complete program shorter and more maintainable, extract the logic that checks processing status into a separate function that can be called from different places in the main program.
-
It’s always a good idea to add more exception handling logic.
In the first two use cases, you’ve seen how to use the AI Hub SDK to
-
Automate document processing by running an AI Hub app.
-
Analyze documents by having conversations about them.
Now you’re ready for use case three: cleaning up after yourself by deleting unneeded resources left over by the first two use cases.