Test SDK installation and authorization
Before you start coding, test that the SDK is installed and configured correctly.
Making an SDK-Tutorial workspace
All the code in this tutorial performs its work in a shared workspace called SDK-Tutorial. The code fails if it can’t find that workspace, so make a shared workspace with that name now.
Testing the SDK installation and authorization
A batch is a collection of files that AI Hub can process. To see if the SDK is set up correctly, this test does the simplest thing possible: create a batch that contains no files. If this test runs with no error messages, the SDK is behaving as expected and is ready for more complicated operations.
Here’s the code for making an empty batch.
The Technical details boxes in some steps contain optional information for experienced programmers.
Make a file to hold your Python program
With a text editor, create and save an empty file called test_sdk.py
. Put it anywhere on your computer. As each snippet of test code is introduced, paste it at the bottom of this file.
Import the SDK
Make your program aware of the AI Hub SDK by importing it.
Technical details
This imports the
AIHub
class from the aihub
module, so you can refer to it later as AIHub
instead of aihub.AIHub
.Convert authorization details into code
Remember the authorization details mentioned earlier? You need to let your program know about those API Key, IB-Context, and API Root values.
This code instantiates the AIHub
class and initializes it with the three authorization values. Add it below the line you already entered.
Make the batch
Here’s where you use the SDK to make an empty batch. You’re requesting that a new batch named test batch be created in the SDK-Tutorial workspace.
Technical details
You’re calling the
create
method from the batches
object that’s stored as a field of the client
object. Calling client.OBJECT-TYPE.ACTION()
is a common pattern in AI Hub SDK usage.Print the status
Let the user know if the batch creation worked. If something goes wrong, Python prints an error message and quits the program—you don’t need to add code to do that explicitly.
If this code was deployed to production, you would want to handle exceptions in a more graceful and possibly recoverable way.
If you don’t know what that means or how to do it, don’t worry—an exception handling discussion comes later.
Here’s the whole program as a single piece of code, with comments to remind you what each line does. If you haven’t been copying and pasting from the previous steps, copy and paste this version.
Running the test
After you’ve typed in the whole program, save it somewhere on your computer as test_sdk.py
.
Now run it using the instructions for your operating system. It takes a few seconds.
Linux
macOS
Windows
-
Open a terminal.
-
Type
cd PATH/TO/DIRECTORY/WITH/test_sdk.py
, replacing the placeholders with the actual path to wherever you storedtest_sdk.py
. -
Type
python3 test_sdk.py
.
If the SDK works as expected, the program displays this message.
Troubleshooting
If the test program fails, it displays a complicated error message. Exactly what the error message says depends on what went wrong, but you can usually get the gist of the problem by looking at the end of the message.
Here are some common error messages. Click on each to see solutions.
Repo (instabase-enablement-enterprise/SDK-Tutorial) not found. Non-retriable error
Your account doesn’t have an SDK-Tutorial workspace. See the instructions on how to create it.
ModuleNotFoundError: No module named 'aihub'
The instabase-aihub
module isn’t installed correctly on your computer. Review the installation instructions.
Message: Org (YOUR CONTEXT-ID) not found. Non-retriable error
Next steps
Now that the SDK is installed, authorized, and tested, go on to the next page to learn how to use the SDK for an actual document processing use case.