Getting started with Watsonx.ai IV

Nathalia Trazzi
4 min readMay 23, 2024

--

In the previous article (https://medium.com/@nathalia.trazzi/getting-started-with-watsonx-ai-iii-93d5b8e55073), it was briefly mentioned how the Prompt Lab of Watsonx.ai allows to save notebooks (.ipynb format) from the prompt session.

In this article, it will be cover how to make use of this notebook and the credentials required to use Watsonx.ai via API.

On the prompt session’s page, inside the project.

Click on the floppy disk button again, and choose notebook this name. Name it and write (optional) a description. Save it.

Return to projects Assets.

Click on the notebook asset.

Prompt Lab provides a complete notebook with all the information to execute the work.

The information in the notebook is structured and organized, with comments and instructions that inform the user about what each step does.

Take this moment to download the notebook on the top of the page as the following image:

To connect to Watsonx.ai through api, you need to provide two keys:

  • project_id
  • apikey

To access your project_id, there are three ways.

First, return to the project assets.

And select the prompt session

Click on the icon </> as indicated in the image below.

The Prompt Lab has this “View code” section, where the ready-made code for the API request of what is being done in the Prompt Lab.

This pre-made request provided by Watsonx.ai is available in curl, Node.js, and Python.

Move the cursor down until you find the project_id.

Another way to access the project_id is through the URL of your browser.

It is also possible to check the project_id by accessing the project and going to the Manage tab.

On the Manage tab, the project_id is available on General section.

Getting the API key from Watsonx.ai.

  1. Visit https://cloud.ibm.com/
  2. Then, in the IBM Cloud interface, click on Manage

3. Click on Access(IAM)

4. In the left-hand side menu, click on API Keys.

5. On the current page, click on Create +

6. Give a name to the API key and a description.

7. Click on Create

8. Download the API key or copy it.

Keep it in a secure location where only trusted individuals or those who need to use the service in this manner have access.

Getting started with the notebook downloaded through the Prompt Lab of Watsonx.ai.

Open the downloaded notebook using the software of your choice. For this example, Visual Studio Code(https://code.visualstudio.com/) was used.

With the API key and the project_id, connecting to Watsonx.ai is simple, easy, and fast, as shown in the example below.

On the get_credentials function, paste the api key

Paste the project_id into the variable project_id. comment or remove the space_id because it won’t be used at this time.

On the model object, comment on the space_id line or just remove it

model = Model(
model_id = model_id,
params = parameters,
credentials = get_credentials(),
project_id = project_id,
#space_id = space_id
)

On the execution section, execute the code and the successful should be working:

That’s all for today.

For more, check Getting Started With Watsonx.ai V: https://medium.com/@nathalia.trazzi/getting-started-with-watsonx-ai-v-2d7fc9ef8778

--

--

Nathalia Trazzi

AI Engineer. Proficient in Watsonx.ai, Assistant and Discovery. Full stack software engineer and Chatbot developer. Fine art photographer.