Path: blob/master/cloud/notebooks/python_sdk/deployments/ai_services/Use watsonx to run generate_batch job using AI service.ipynb
6405 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:
Set up the environment
Before you use the sample code in this notebook, you must perform the following setup tasks:
Create a watsonx.ai Runtime Service instance (a free plan is offered and information about how to create the instance can be found here).
Install required packages
Define the watsonx.ai credentials
Use the code cell below to define the watsonx.ai credentials that are required to work with watsonx Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see Managing user API keys.
Working with spaces
You need to create a space that will be used for your work. If you do not have a space, you can use Deployment Spaces Dashboard to create one.
Click New Deployment Space
Create an empty space
Select Cloud Object Storage
Select watsonx.ai Runtime instance and press Create
Go to Manage tab
Copy
Space GUID
and paste it below
Tip: You can also use SDK to prepare the space for your work. More information can be found here.
Action: assign 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.