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.

If you don’t have permission to make a workspace, ask an administrator of your AI Hub organization to do it for you.
Workspace names can’t contain spaces, which is why the name is SDK-Tutorial instead of SDK Tutorial.

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.

This SDK operation uses no consumption units.

Here’s the code for making an empty batch.

The Technical details boxes in some steps contain optional information for experienced programmers.

1

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.

2

Import the SDK

Make your program aware of the AI Hub SDK by importing it.

1from aihub import AIHub
Technical details

This imports the AIHub class from the aihub module, so you can refer to it later as AIHub instead of aihub.AIHub.
3

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.

Spacing matters in Python! Copy and paste can scramble indents, so make sure all the spacing in your code matches the spacing in the tutorial exactly or your program might not run.
1client = AIHub(api_root="PASTE YOUR API ROOT HERE",
2 api_key="PASTE YOUR API KEY HERE",
3 ib_context="PASTE YOUR IB-CONTEXT HERE")
4

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.

1client.batches.create(name="test batch",
2 workspace="SDK-Tutorial")
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.
The Cleanup use case later in the tutorial shows you how to clean up batches after you’re done with them.

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.

Test SDK installation and authorization (complete program)
1# make the AI Hub SDK available for use
2from aihub import AIHub
3
4# initialize the "client" variable as an instance of the AIHub class,
5# configured with authorization details for our AI Hub account
6client = AIHub(api_root="PASTE YOUR API ROOT HERE",
7 api_key="PASTE YOUR API KEY HERE",
8 ib_context="PASTE YOUR IB-CONTEXT HERE")
9
10# create a new batch called "test batch" in the
11# "SDK-Tutorial" workspace
12create_batch_resp = client.batches.create(name="test batch",
13 workspace="SDK-Tutorial")
14
15# If the batch was created successfully, the program prints the
16# messages below and quits normally. If it wasn't created successfully,
17# Python automatically prints an error message and quits the program.
18print("Successfully created a batch.")
19print("The AI Hub SDK is installed, authorized, and ready to use.")

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.

  1. Open a terminal.

  2. Type cd PATH/TO/DIRECTORY/WITH/test_sdk.py, replacing the placeholders with the actual path to wherever you stored test_sdk.py.

  3. Type python3 test_sdk.py.

If the SDK works as expected, the program displays this message.

Successfully created a batch.
The AI Hub SDK is installed, authorized, and ready to use.

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.

Your account doesn’t have an SDK-Tutorial workspace. See the instructions on how to create it.

The instabase-aihub module isn’t installed correctly on your computer. Review the installation instructions.

Your API Key is wrong. Review the instructions on how to find and set it.

Your IB-Context is wrong. Review the instructions on how to find and set it.

Your API Root is wrong. Review the instructions on how to find and set it.

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.