Path: blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Milvus to create a pattern about IBM.ipynb
6405 views
Use AutoAI RAG and Milvus database to work with ibm-watsonx-ai
SDK documentation.
Disclaimers
Use only Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code to demonstrate the usage of IBM AutoAI RAG. The AutoAI RAG experiment conducted in this notebook uses data scraped from the ibm-watsonx-ai
SDK documentation.
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goal
The learning goals of this notebook are:
Create an AutoAI RAG job that will find the best RAG pattern based on provided data
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; information about how to create the instance can be found here).
Install and import the required modules and dependencies
Defining the watsonx.ai credentials
This cell defines the credentials required to work with the watsonx.ai Runtime service.
Action: Provide the IBM Cloud user API key. For details, see documentation.
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.
RAG Optimizer definition
Training data connection
Define connection information to access the COS bucket and the file that contains training data. This example uses ibm_watsonx_ai
SDK documentation content.
This cell downloads the ibm_watsonx_ai
Python SDK zip file from GitHub (if not already present), and extracts its contents to a specified folder.
Documents with the .html
extension are filtered and saved to ContainerLocation
Writing all SDK documents may take around 3 minutes.
Progress: ✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓✓
Create a connection to COS
You can skip this section if you already have a connection asset that connects to IBM Cloud Object Storage.
Finally, we can create a Data Connection that represents input data references.
Defining a connection to test data
Upload a json
file that will be used for benchmarking to COS and then define a connection to this file. This example uses content from the ibm_watsonx_ai
SDK documentation.
The code in the next cell uploads testing data to the bucket as a json
file.
Define connection information to testing data.
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.
Define connection information to vector store references.
RAG Optimizer configuration
Provide the input information for AutoAI RAG optimizer:
name
- experiment namedescription
- experiment descriptionmax_number_of_rag_patterns
- maximum number of RAG patterns to createoptimization_metrics
- target optimization metrics
Configuration parameters can be retrieved via get_params()
.
You can use the get_run_status()
method to monitor AutoAI RAG jobs in background mode.
Additionally, you can pass the scoring
parameter to the summary method, to filter RAG patterns starting with the best.
Get selected pattern
Get the RAGPattern object from the RAG Optimizer experiment. By default, the RAGPattern of the best pattern is returned.
The pattern details can be retrieved by calling the get_pattern_details
method:
Query the RAGPattern locally, to test it.
Deploy RAGPattern
Deployment is done by storing the defined RAG function and then by creating a deployed asset.
Test the deployed function
RAG service is now deployed in our space. To test our solution we can run the cell below. Questions have to be provided in the payload. Their format is provided below.
Get executed optimizer's configuration parameters
Get historical rag_optimizer instance and training details
List trained patterns for selected optimizer
To delete the deployment, use the delete
method.
Warning: Keeping the deployment active may lead to unnecessary consumption of Compute Unit Hours (CUHs).
If you want to clean up all created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
please follow up this sample notebook.
Summary and next steps
You successfully completed this notebook!
You learned how to use ibm-watsonx-ai
to run AutoAI RAG experiments.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Authors
Michał Steczko, Software Engineer at watsonx.ai
Copyright © 2024-2025 IBM. This notebook and its source code are released under the terms of the MIT License.