Path: blob/master/cpd5.2/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Milvus to create a pattern about IBM.ipynb
6412 views
Use AutoAI RAG and Milvus database to work with ibm-watsonx-ai
SDK documentation.
Disclaimers
Use only Spaces that are available in the 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.12.
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:
Install dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Define credentials
Authenticate the watsonx.ai Runtime service on IBM Cloud Pak for Data. You need to provide the admin's username
and the platform url
.
Use the admin's api_key
to authenticate watsonx.ai Runtime services:
Alternatively you can use the admin's password
:
Create APIClient
instance
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
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
To print all existing spaces, use the list
method.
To be able to interact with all resources available in watsonx.ai, you need to set the space which you will be using.
RAG Optimizer definition
Define a connection to the training data
Define connection information to access the COS bucket and the file that contains the training data. This example uses ibm_watsonx_ai
SDK documentation content.
The following code cell downloads the ibm_watsonx_ai
Python SDK compressed file from GitHub (if not already downloaded), and extracts its contents to a specified folder.
Create a connection to COS.
Create a Data Connection that represents input data references.
Filter documents with the .html
extension and save them to the COS bucket.
Writing all SDK documents might take around 3 minutes.
Define a connection to the test data
Upload a json
file that you want to use as a benchmark to COS and then define a connection to the file. This example uses content from the ibm_watsonx_ai
SDK documentation.
Upload the benchmark testing data to the bucket as a json
file.
Define connection information to the 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 the AutoAI RAG optimizer:
name
- experiment namedescription
- experiment descriptionmax_number_of_rag_patterns
- maximum number of RAG patterns to createoptimization_metrics
- target optimization metrics
To retrieve the configuration parameters, use get_params()
.
To monitor the AutoAI RAG jobs in background mode, use the get_run_status()
method.
Additionally, you can pass the scoring
parameter to the summary method to filter RAG patterns, starting with the best.
Get the selected pattern
Get the RAGPattern object from the RAG Optimizer experiment. By default, the RAGPattern of the best pattern is returned.
To retrieve the pattern details, use the get_pattern_details
method.
Query the RAGPattern locally to test it.
Deploy the RAGPattern
To deploy the RAGPattern, store the defined RAG function and then create a deployed asset.
Test the deployed function
The RAG service is now deployed in our space. To test the solution, run the cell below. Questions have to be provided in the payload. Their format is provided below.
Get the executed optimizer's configuration parameters
Get the historical rag_optimizer instance and training details
List trained patterns for the selected optimizer
To delete the deployment, use the delete
method.
Warning: If you keep the deployment active, it might lead to unnecessary consumption of Compute Unit Hours (CUHs).
To clean up all of the created assets:
experiments
trainings
pipelines
model definitions
models
functions
deployments
follow the steps in 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
Mateusz Szewczyk, Software Engineer at watsonx.ai
Copyright © 2024-2025 IBM. This notebook and its source code are released under the terms of the MIT License.