Path: blob/master/cpd5.2/notebooks/python_sdk/deployments/ai_services/Use watsonx to run generate_batch job using AI service.ipynb
6412 views
Use watsonx to run generate_batch
job using AI service
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook provides a detailed demonstration of the steps and code required to showcase support for watsonx.ai AI service.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goal
The primary objective of this notebook is to illustrate how to utilize watsonx.ai AI services to execute a generate_batch
job, facilitating the ingestion of documents into a Milvus vector database.
Table of Contents
This notebook contains the following parts:
Install and import the ibm-watsonx-ai
and dependecies
Note: ibm-watsonx-ai
documentation can be found here.
Connect to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pak for Data. You need to provide the platform url
, your username
, and your api_key
.
url
- url which points to your CPD instance.username
- username to your CPD instance.api_key
- api_key to your CPD instance.
Alternatively you can use username
and password
to authenticate WML services.
Working with spaces
First, you need to create a space for your work. If you do not have a space already created, you can use {PLATFORM_URL}/ml-runtime/spaces?context=icp4data
to create one.
Click New Deployment Space
Create an empty space
Go to the space
Settings
tabCopy the
space_id
and paste it below
PLATFORM_URL
is the url which points to your CPD instance.
Tip: You can also use SDK to prepare the space for your work. Find more information in the Space Management sample notebook.
Action: Assign the space ID below
Create an instance of APIClient with authentication details.
Create an embedding function for VectorStore
Note that you can feed a custom embedding function to be used by Milvus. The performance of Milvus may differ depending on the embedding model used.
Note: To list available embedding models use:
Set up connectivity information to Milvus
This notebook focuses on a self-managed Milvus cluster using IBM watsonx.data.
The following cell retrieves the Milvus username, password, host, and port from the environment (if available) and prompts you to provide them manually in case of failure.
You can provide a connection asset ID to read all required connection data from it. Before doing so, make sure that a connection asset was created in your space.
Set up VectorStore with Milvus credentials
Create a VectorStore class that automatically detects the database type (in our case it will be Milvus) and allows us to add, search and delete documents.
It works as a wrapper for LangChain VectorStore classes. You can customize the settings as long as it is supported. Consult the LangChain documentation for more information about Milvus connector.
Provide the name of your Milvus index for subsequent operations:
Verify if index in Milvus instance is empty.
Note: If collection is not empty you can use clear
method on VectorStore
object:
References to input data
This example uses the ModelInference
description from the ibm_watsonx_ai
documentation.
Upload the input data to the space as a data asset.
Define a connection to the input data.
Create input_data_references
as a dict
representation.
Verify the total number of documents currently stored within the Milvus collection.
Note: Due to the implementation specifics of Milvus, it is necessary to initialize a new VectorStore instance in order to accurately retrieve the count of indexed elements.
Once the collection accurately reflects the expected number of items, proceed to clear its contents to prepare the environment for subsequent testing activities.
Create batch deployment of AI service.
Verify the total number of documents currently stored within the Milvus collection.
Note: Due to the implementation specifics of Milvus, it is necessary to initialize a new VectorStore instance in order to accurately retrieve the count of indexed elements.
Summary and next steps
You successfully completed this notebook!
You have successfully learned how to design and deploy an AI service utilizing the generate_batch
functionality, leveraging the capabilities of the ibm_watsonx_ai
SDK.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author
Mateusz Szewczyk, Software Engineer at watsonx.ai.
Copyright © 2025 IBM. This notebook and its source code are released under the terms of the MIT License.