Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
ibm
GitHub Repository: ibm/watson-machine-learning-samples
Path: blob/master/cloud/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Milvus to create a pattern about IBM.ipynb
6405 views
Kernel: autoai_rag

image

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:

Install and import the required modules and dependencies

!pip install -U wget | tail -n 1 !pip install -U 'ibm-watsonx-ai[rag]>=1.3.26' | tail -n 1

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.

import getpass from ibm_watsonx_ai import Credentials credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", api_key=getpass.getpass("Please enter your watsonx.ai api key (hit enter): "), )

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

space_id = 'PASTE YOUR SPACE GUID HERE'

Create an instance of APIClient with authentication details.

from ibm_watsonx_ai import APIClient client = APIClient(credentials=credentials, space_id=space_id)

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.

import wget, zipfile, os filename = "watsonx-ai-python-sdk" filename_zip = "watsonx-ai-python-sdk.zip" if not os.path.isfile(filename_zip): wget.download("https://github.com/IBM/watsonx-ai-python-sdk/archive/refs/heads/gh-pages.zip", out=filename_zip) with zipfile.ZipFile(filename_zip, "r") as zip_ref: zip_ref.extractall(filename)
from ibm_watsonx_ai.helpers import DataConnection, ContainerLocation container_location_path = "ibm_watsonx_ai_sdk_docs" container_data_connection = DataConnection(ContainerLocation(path=f"{container_location_path}/")) container_data_connection.set_client(client)

Documents with the .html extension are filtered and saved to ContainerLocation

html_docs_files = [] for root, dirs, files in os.walk(filename): if root == f"{filename}/watsonx-ai-python-sdk-gh-pages": for file in files: if file.endswith('.html'): file_path = os.path.join(root, file) html_docs_files.append(file_path)

Writing all SDK documents may take around 3 minutes.

import sys for ind, html_docs_file in enumerate(html_docs_files): container_data_connection.write(html_docs_file, remote_name = html_docs_file.split("/")[-1]) sys.stdout.write(f"\rProgress: {'✓' * (ind+1)}{'.' * (len(html_docs_files)-ind-1)}\r") sys.stdout.flush()
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.

datasource_name = 'bluemixcloudobjectstorage' bucketname = container_data_connection.location.bucket
cos_credentials = client.spaces.get_details(space_id=space_id)['entity']['storage']['properties']
conn_meta_props= { client.connections.ConfigurationMetaNames.NAME: f"Connection to Database - {datasource_name} ", client.connections.ConfigurationMetaNames.DATASOURCE_TYPE: client.connections.get_datasource_type_id_by_name(datasource_name), client.connections.ConfigurationMetaNames.DESCRIPTION: "Connection to external Database", client.connections.ConfigurationMetaNames.PROPERTIES: { 'bucket': bucketname, 'access_key': cos_credentials['credentials']['editor']['access_key_id'], 'secret_key': cos_credentials['credentials']['editor']['secret_access_key'], 'iam_url': 'https://iam.cloud.ibm.com/identity/token', 'url': cos_credentials['endpoint_url'] } } conn_details = client.connections.create(meta_props=conn_meta_props) connection_id = client.connections.get_id(conn_details)
Creating connections... SUCCESS

Finally, we can create a Data Connection that represents input data references.

from ibm_watsonx_ai.helpers import DataConnection, S3Location input_data_references = [DataConnection( connection_asset_id=connection_id, location=S3Location(bucket=bucketname, path=container_location_path))] input_data_references[0].set_client(client)

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.

benchmarking_data_IBM_page_content = [ { "question": "How to install ibm-watsonx-ai library?", "correct_answer": "pip install ibm-watsonx-ai", "correct_answer_document_ids": [ "install.html" ] }, { "question": "What is Credentails class parameters?", "correct_answer": "url, api_key, name, iam_serviceid_crn, token, projects_token, username, password, instance_id, version, bedrock_url, proxies, verify", "correct_answer_document_ids": [ "base.html" ] }, { "question": "How to get AutoAI pipeline with number 3?", "correct_answer": "get_pipeline(pipeline_name='Pipeline_3')", "correct_answer_document_ids": [ "autoai_working_with_class_and_optimizer.html" ] }, { "question": "How to get list of Embedding Models?", "correct_answer": "client.foundation_models.EmbeddingModels", "correct_answer_document_ids": [ "fm_embeddings.html" ] }, { "question": "How to retrieve the list of model lifecycle data?", "correct_answer": "get_model_lifecycle(url='https://us-south.ml.cloud.ibm.com', model_id='ibm/granite-13b-instruct-v2')", "correct_answer_document_ids": [ "fm_helpers.html" ] }, { "question": "What is path to ModelInference class?", "correct_answer": "ibm_watsonx_ai.foundation_models.inference.ModelInference", "correct_answer_document_ids": [ "fm_model_inference.html" ] }, { "question": "What is method for get model inferance details?", "correct_answer": "get_details()", "correct_answer_document_ids": [ "fm_model_inference.html" ] } ]

The code in the next cell uploads testing data to the bucket as a json file.

import json test_filename = "benchmarking_data_ibm_watson_ai.json" if not os.path.isfile(test_filename): with open(test_filename, "w") as json_file: json.dump(benchmarking_data_IBM_page_content, json_file, indent=4) test_asset_details = client.data_assets.create(name=test_filename, file_path=test_filename) test_asset_id = client.data_assets.get_id(test_asset_details) test_asset_id
Creating data asset... SUCCESS
'9d67c83f-3862-4885-a8a7-58ebf047ebe3'

Define connection information to testing data.

from ibm_watsonx_ai.helpers import DataConnection test_data_references = [DataConnection(data_asset_id=test_asset_id)]

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.

import os import getpass milvus_connection_id = input("Provide connection asset ID in your space. Skip this, if you wish to type credentials by hand and hit enter: ") or None if milvus_connection_id is None: try: username = os.environ["USERNAME"] except KeyError: username = input("Please enter your Milvus user name and hit enter: ") try: password = os.environ["PASSWORD"] except KeyError: password = getpass.getpass("Please enter your Milvus password and hit enter: ") try: host = os.environ["HOST"] except KeyError: host = input("Please enter your Milvus hostname and hit enter: ") try: port = os.environ["PORT"] except KeyError: port = input("Please enter your Milvus port number and hit enter: ") try: ssl = os.environ["SSL"] except: ssl = bool(input("Please enter ('y'/anything) if your Milvus instance has SSL enabled. Skip if it is not: ")) # Create connection milvus_data_source_type_id = client.connections.get_datasource_type_uid_by_name( "milvus" ) details = client.connections.create( { client.connections.ConfigurationMetaNames.NAME: "Milvus Connection", client.connections.ConfigurationMetaNames.DESCRIPTION: "Connection created by the sample notebook", client.connections.ConfigurationMetaNames.DATASOURCE_TYPE: milvus_data_source_type_id, client.connections.ConfigurationMetaNames.PROPERTIES: { "host": host, "port": port, "username": username, "password": password, "ssl": ssl, }, } ) milvus_connection_id = client.connections.get_id(details)

Define connection information to vector store references.

vector_store_references = [DataConnection(connection_asset_id=milvus_connection_id)]

RAG Optimizer configuration

Provide the input information for AutoAI RAG optimizer:

  • name - experiment name

  • description - experiment description

  • max_number_of_rag_patterns - maximum number of RAG patterns to create

  • optimization_metrics - target optimization metrics

from ibm_watsonx_ai.experiment import AutoAI experiment = AutoAI(credentials, space_id=space_id) rag_optimizer = experiment.rag_optimizer( name='AutoAI RAG ibm-watsonx-ai SDK documentation', description="AutoAI RAG experiemnt trainded on ibm-watsonx-ai SDK documentataion", max_number_of_rag_patterns=6, optimization_metrics=[AutoAI.RAGMetrics.ANSWER_CORRECTNESS] )

Configuration parameters can be retrieved via get_params().

rag_optimizer.get_params()
{'name': 'AutoAI RAG ibm-watsonx-ai SDK documentation', 'description': 'AutoAI RAG experiemnt trainded on ibm-watsonx-ai SDK documentataion', 'max_number_of_rag_patterns': 6, 'optimization_metrics': ['answer_correctness']}

RAG Experiment run

Call the run() method to trigger the AutoAI RAG experiment. You can either use interactive mode (synchronous job) or background mode (asynchronous job) by specifying background_mode=True.

run_details = rag_optimizer.run( input_data_references=input_data_references, test_data_references=test_data_references, vector_store_references=vector_store_references, background_mode=False )
############################################## Running '09437777-a9e4-4cc6-aac0-96c2c8352d10' ############################################## pending............. running...................................................................................................................................................................................................... completed Training of '09437777-a9e4-4cc6-aac0-96c2c8352d10' finished successfully.

You can use the get_run_status() method to monitor AutoAI RAG jobs in background mode.

rag_optimizer.get_run_status()
'completed'

Comparison and testing of RAG Patterns

You can list the trained patterns and information on evaluation metrics in the form of a Pandas DataFrame by calling the summary() method. You can use the DataFrame to compare all discovered patterns and select the one you like for further testing.

summary = rag_optimizer.summary() summary

Additionally, you can pass the scoring parameter to the summary method, to filter RAG patterns starting with the best.

summary = rag_optimizer.summary(scoring="faithfulness")

Get selected pattern

Get the RAGPattern object from the RAG Optimizer experiment. By default, the RAGPattern of the best pattern is returned.

best_pattern_name = summary.index.values[0] print('Best pattern is:', best_pattern_name) best_pattern = rag_optimizer.get_pattern()
Best pattern is: Pattern3

The pattern details can be retrieved by calling the get_pattern_details method:

rag_optimizer.get_pattern_details(pattern_name='Pattern2')

Query the RAGPattern locally, to test it.

from ibm_watsonx_ai.deployments import RuntimeContext runtime_context = RuntimeContext(api_client=client) inference_service_function = best_pattern.inference_service(runtime_context)[0]
question = "How to use new approach of providing credentials to APIClient?" context = RuntimeContext( api_client=client, request_payload_json={"messages": [{"role": "user", "content": question}]}, ) resp = inference_service_function(context)
print(inference_service_function(context)["body"]["choices"][0]["message"]["content"])
</s>To use the new approach of providing credentials to `APIClient`, you should use the `Credentials` class instead of a dictionary. Here's how you can do it: 1. Import the necessary classes from `ibm_watsonx_ai`. 2. Create an instance of the `Credentials` class with the required parameters. 3. Pass the `Credentials` instance to the `APIClient` constructor. Here's an example: ```python from ibm_watsonx_ai import APIClient, Credentials # Create a Credentials instance credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", token="***********" ) # Initialize the APIClient with the Credentials instance client = APIClient(credentials) # Now you can use the client to interact with the API url = client.credentials.url # OR url = credentials.url ``` This approach ensures that you are using the latest and recommended method for providing credentials to the `APIClient`.

Deploy RAGPattern

Deployment is done by storing the defined RAG function and then by creating a deployed asset.

deployment_details = best_pattern.inference_service.deploy( name="AutoAI RAG deployment - ibm_watsonx_ai documentataion", space_id=space_id, deploy_params={"tags": ["wx-autoai-rag"]} )
###################################################################################### Synchronous deployment creation for id: 'f54af56a-9d55-4b58-8db1-5b3cd1ed2673' started ###################################################################################### initializing Note: online_url and serving_urls are deprecated and will be removed in a future release. Use inference instead. .......... ready ----------------------------------------------------------------------------------------------- Successfully finished deployment creation, deployment_id='cfabea10-76b5-4a8c-ae43-b3e4c9d70885' -----------------------------------------------------------------------------------------------

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.

deployment_id = client.deployments.get_id(deployment_details) payload = { "messages": [{"role": "user", "content": question}] } score_response = client.deployments.run_ai_service(deployment_id, payload)
score_response
{'choices': [{'index': 0, 'message': {'content': ' </s>To use the new approach of providing credentials to `APIClient`, you should use the `Credentials` class instead of a dictionary. Here\'s how you can do it:\n\n1. Import the necessary classes from `ibm_watsonx_ai`.\n2. Create an instance of the `Credentials` class with the required parameters.\n3. Pass the `Credentials` instance to the `APIClient` constructor.\n\nHere\'s an example:\n\n```python\nfrom ibm_watsonx_ai import APIClient, Credentials\n\n# Create a Credentials instance\ncredentials = Credentials(\n url="https://us-south.ml.cloud.ibm.com",\n token="***********"\n)\n\n# Initialize the APIClient with the Credentials instance\nclient = APIClient(credentials)\n\n# Now you can use the client to interact with the API\nurl = client.credentials.url\n# OR\nurl = credentials.url\n```\n\nThis approach ensures that you are using the latest and recommended method for providing credentials to the `APIClient`.', 'role': 'assistant'}, 'reference_documents': [{'metadata': {'document_id': 'migration_v1.html', 'sequence_number': [7, 8, 9, 10, 11]}, 'page_content': 'get_model_lifecycle(\n url="https://us-south.ml.cloud.ibm.com",\n model_id="ibm/granite-13b-instruct-v2"\n)\n\nget_model_specs_with_prompt_tuning_support(\n url="https://us-south.ml.cloud.ibm.com"\n)\n\nget_embedding_model_specs(\n url="https://us-south.ml.cloud.ibm.com"\n)\n\n\n\n\nBreaking changes¶\n\nThe client.<resource>.list() methods don’t print the table with listed assets. They return the table as pandas.DataFrame. The optional parameter for the return_as_df methods was removed.\n\n✅ New approach - Access to credential fields as class attributes.\nconn_list = client.connections.list() ## table not printed\n\n\n❌ Old approach - Table with listed resources printed.\nconn_list = client.connections.list()\n### Table returned as pandas.DataFrame `conn_list` object and printed output:\n--------------------------- ------------------------------------ -------------------- ------------------------------------\nNAME ID CREATED DATASOURCE_TYPE_ID\nConnection to COS 71738a79-6585-4f33-bf4a-18907abcf06a 2024-04-25T10:42:23Z 193a97c1-4475-4a19-b90c-295c4fdc6517\n--------------------------- ------------------------------------ -------------------- ------------------------------------\n\n\n\nMethods and parameters that were marked as deprecated in ibm_watsonx_ai v0 were removed. For example:\n\n✅ New approach - Use the method that replaced the deprecated one, for get_uid it’s get_id\nasset_id = client.data_assets.get_id(asset_details)\n\n\n❌ Old approach - Deprecated get_uid method called\nclient.data_assets.get_uid(asset_details)\n\n\n\nclient.credentials and client.wml_credentials returns a Credentials object instead of a dictionary.\n\n✅ New approach - Access to credential fields as class attributes.\nfrom ibm_watsonx_ai import APIClient\nfrom ibm_watsonx_ai import Credentials\n\ncredentials = Credentials(\n url = "https://us-south.ml.cloud.ibm.com",\n token = "***********",\n )\n\nclient = APIClient(credentials)\n\nurl = client.credentials.url\n# OR\nurl = credentials.url\n\n\n❌ Old approach - Access to credential fields as keys in a dictionary.\nfrom ibm_watsonx_ai import APIClient\n\ncredentials = {\n "url": "https://us-south.ml.cloud.ibm.com",\n "token":"***********",\n }\nclient = APIClient(credentials)\nurl = client.wml_credentials.get(\'url\')\n\n\n\nParameter changes in service.score(...) for online deployment\n\nservice.score(...) has an additional forecast_window parameter added before the transaction_id parameter.\nFrom ibm_watsonx_ai 1.0.0, in the service.score(...) function, you need to pass all named parameters except for the payload parameter.\n✅ New approach - named parameters and a new parameter.\npredictions = service.score({\n "observations": AbstractTestTSAsync.observations,\n "supporting_features": AbstractTestTSAsync.supporting_features,\n },\n forecast_window=1,\n transaction_id=transaction_id,\n) ❌ Old approach - all parameters are not named, the forecast_window parameter is absent\npredictions = service.score({\n "observations": AbstractTestTSAsync.observations,\n "supporting_features": AbstractTestTSAsync.supporting_features,\n },\n transaction_id,\n)\n\n\n\n\nDeprecations¶\n\nInitializing APIClient with credentials as a dictionary is deprecated.\n\nAll parameters with wml in the name are deprecated. They were renamed to:\nwml_credentials -> credentials\nwml_client -> api_client.\n\n\n\n\nid naming was aligned in parameters and methods and all uid were replaced by id. Methods that were using uid were removed or marked as deprecated.\n\n\n\n\n\n\n\n\n\n\n\nNext\n\nChangelog\n\n\n\n\n\n\n\nPrevious\n\nMigration from ibm_watson_machine_learning\n\n\n\n\n\n\n Copyright © 2023-2025, IBM\n \n Made with Sphinx and @pradyunsg\'s\n \n Furo\n\n\n\n\n\n\n\n\n\n\n On this page'}, {'metadata': {'document_id': 'base.html', 'sequence_number': [5, 6, 7, 8, 9]}, 'page_content': 'A direct instance of httpx.Client()\nA set of parameters provided via the HttpClientConfig class\n\nExample:\nfrom ibm_watsonx_ai.utils.utils import HttpClientConfig\n\nlimits=httpx.Limits(\n max_connections=5\n)\ntimeout = httpx.Timeout(7)\nhttp_config = HttpClientConfig(timeout=timeout, limits=limits)\n\n\nIf not provided, a default instance of httpx.Client is created.\n\nNote\nIf you need to adjust timeouts or limits, using HttpClientConfig is the recommended approach.\nWhen the proxies parameter is provided in credentials, httpx.Client will use these proxies.\nHowever, if you want to create a separate httpx.Client, all parameters must be provided by the user.\n\n\nasync_httpx_client (httpx.AsyncClient, HttpClientConfig, optional) – A customizable httpx.AsyncClient for ModelInference. The httpx.AsyncClient is used to improve performance of foundation models inference. This parameter accepts two types of input:\n\nA direct instance of httpx.AsyncClient\nA set of parameters provided via the HttpClientConfig class\n\nExample:\nfrom ibm_watsonx_ai.utils.utils import HttpClientConfig\n\nlimits=httpx.Limits(\n max_connections=5\n)\ntimeout = httpx.Timeout(7)\nhttp_config = HttpClientConfig(timeout=timeout, limits=limits)\n\n\nIf not provided, a default instance of httpx.AsyncClient is created.\n\nNote\nIf you need to adjust timeouts or limits, using HttpClientConfig is the recommended approach.\nWhen the proxies parameter is provided in credentials, httpx.Client will use these proxies.\nHowever, if you want to create a separate httpx.Client, all parameters must be provided by the user.\n\n\n\n\n\nExample:\nfrom ibm_watsonx_ai import APIClient, Credentials\n\ncredentials = Credentials(\n url = "<url>",\n api_key = IAM_API_KEY\n)\n\nclient = APIClient(credentials, space_id="<space_id>")\n\nclient.models.list()\nclient.deployments.get_details()\n\nclient.set.default_project("<project_id>")\n\n...\n\n\n\n\nget_copy()[source]¶\nPrepares clean copy of APIClient. The clean copy contains no token, password, api key data. It is used\nin AI services scenarios, when the client is used in deployed code, and can be reused between users.\nThe copy needs to be set with current user token in the inner function of AI service.\n\nReturns:\nAPIClient which is 2-level copy of the current one, without user secrets\n\nReturn type:\nAPIClient\n\n\nExample:\ndef deployable_ai_service(context, params={"k1":"v1"}, **kwargs):\n\n # imports\n from ibm_watsonx_ai import Credentials, APIClient\n from ibm_watsonx_ai.foundation_models import ModelInference\n\n task_token = context.generate_token()\n\n outer_context = context\n\n client = APIClient(Credentials(\n url = "https://us-south.ml.cloud.ibm.com",\n token = task_token\n ))\n\n # operations with client\n\n def generate(context):\n user_client = client.get_copy()\n user_client.set_token(context.generate_token())\n\n # operations with user_client\n\n return {\'body\': response_body}\n\n return generate\n\nstored_ai_service_details = client._ai_services.store(deployable_ai_service, meta_props)\n\n\n\n\n\nget_headers(content_type=\'application/json\', include_user_agent=False)[source]¶\nGet HTTP headers used during requests.\n\nParameters:\n\ncontent_type – value for Content-Type header, defaults to application/json\ninclude_user_agent – whether the result should include User-Agent header, defaults to False\n\n\nReturns:\nheaders used during requests\n\nReturn type:\ndict\n\n\n\n\n\nset_headers(headers)[source]¶\nMethod which allows refresh/set new User Request Headers.\n\nParameters:\nheaders (dict) – User Request Headers\n\n\nExamples\nheaders = {\n \'Authorization\': \'Bearer <USER AUTHORIZATION TOKEN>\',\n \'User-Agent\': \'ibm-watsonx-ai/1.0.1 (lang=python; arch=x86_64; os=darwin; python.version=3.10.13)\',\n \'Content-Type\': \'application/json\'\n}\n\nclient.set_headers(headers)\n\n\n\n\n\nset_token(token)[source]¶\nMethod which allows refresh/set new User Authorization Token.\n\nNote\nUsing this function will cause that token will not be automatically refreshed anymore, if password or apikey were passed.\nThe user needs to take care of token refresh using set_token function from that point in time until they finish using the client instance.'}, {'metadata': {'document_id': 'migration_v1.html', 'sequence_number': [8, 9, 10, 11, 12]}, 'page_content': '✅ New approach - Access to credential fields as class attributes.\nconn_list = client.connections.list() ## table not printed\n\n\n❌ Old approach - Table with listed resources printed.\nconn_list = client.connections.list()\n### Table returned as pandas.DataFrame `conn_list` object and printed output:\n--------------------------- ------------------------------------ -------------------- ------------------------------------\nNAME ID CREATED DATASOURCE_TYPE_ID\nConnection to COS 71738a79-6585-4f33-bf4a-18907abcf06a 2024-04-25T10:42:23Z 193a97c1-4475-4a19-b90c-295c4fdc6517\n--------------------------- ------------------------------------ -------------------- ------------------------------------\n\n\n\nMethods and parameters that were marked as deprecated in ibm_watsonx_ai v0 were removed. For example:\n\n✅ New approach - Use the method that replaced the deprecated one, for get_uid it’s get_id\nasset_id = client.data_assets.get_id(asset_details)\n\n\n❌ Old approach - Deprecated get_uid method called\nclient.data_assets.get_uid(asset_details)\n\n\n\nclient.credentials and client.wml_credentials returns a Credentials object instead of a dictionary.\n\n✅ New approach - Access to credential fields as class attributes.\nfrom ibm_watsonx_ai import APIClient\nfrom ibm_watsonx_ai import Credentials\n\ncredentials = Credentials(\n url = "https://us-south.ml.cloud.ibm.com",\n token = "***********",\n )\n\nclient = APIClient(credentials)\n\nurl = client.credentials.url\n# OR\nurl = credentials.url\n\n\n❌ Old approach - Access to credential fields as keys in a dictionary.\nfrom ibm_watsonx_ai import APIClient\n\ncredentials = {\n "url": "https://us-south.ml.cloud.ibm.com",\n "token":"***********",\n }\nclient = APIClient(credentials)\nurl = client.wml_credentials.get(\'url\')\n\n\n\nParameter changes in service.score(...) for online deployment\n\nservice.score(...) has an additional forecast_window parameter added before the transaction_id parameter.\nFrom ibm_watsonx_ai 1.0.0, in the service.score(...) function, you need to pass all named parameters except for the payload parameter.\n✅ New approach - named parameters and a new parameter.\npredictions = service.score({\n "observations": AbstractTestTSAsync.observations,\n "supporting_features": AbstractTestTSAsync.supporting_features,\n },\n forecast_window=1,\n transaction_id=transaction_id,\n) ❌ Old approach - all parameters are not named, the forecast_window parameter is absent\npredictions = service.score({\n "observations": AbstractTestTSAsync.observations,\n "supporting_features": AbstractTestTSAsync.supporting_features,\n },\n transaction_id,\n)\n\n\n\n\nDeprecations¶\n\nInitializing APIClient with credentials as a dictionary is deprecated.\n\nAll parameters with wml in the name are deprecated. They were renamed to:\nwml_credentials -> credentials\nwml_client -> api_client.\n\n\n\n\nid naming was aligned in parameters and methods and all uid were replaced by id. Methods that were using uid were removed or marked as deprecated.\n\n\n\n\n\n\n\n\n\n\n\nNext\n\nChangelog\n\n\n\n\n\n\n\nPrevious\n\nMigration from ibm_watson_machine_learning\n\n\n\n\n\n\n Copyright © 2023-2025, IBM\n \n Made with Sphinx and @pradyunsg\'s\n \n Furo\n\n\n\n\n\n\n\n\n\n\n On this page\n \n\n\n\n\nV1 Migration Guide\nWhat’s new?\nRefactor and Cleanup\nNew authorization\nEasier project and space setup\nFoundation model functions moved under APIClient\nBreaking changes\nDeprecations'}]}]}
print(score_response["choices"][0]["message"]["content"])
</s>To use the new approach of providing credentials to `APIClient`, you should use the `Credentials` class instead of a dictionary. Here's how you can do it: 1. Import the necessary classes from `ibm_watsonx_ai`. 2. Create an instance of the `Credentials` class with the required parameters. 3. Pass the `Credentials` instance to the `APIClient` constructor. Here's an example: ```python from ibm_watsonx_ai import APIClient, Credentials # Create a Credentials instance credentials = Credentials( url="https://us-south.ml.cloud.ibm.com", token="***********" ) # Initialize the APIClient with the Credentials instance client = APIClient(credentials) # Now you can use the client to interact with the API url = client.credentials.url # OR url = credentials.url ``` This approach ensures that you are using the latest and recommended method for providing credentials to the `APIClient`.

Historical runs

In this section you learn to work with historical RAG Optimizer jobs (runs).

To list historical runs use the list() method and provide the 'rag_optimizer' filter.

experiment.runs(filter='rag_optimizer').list()
run_id = run_details['metadata']['id'] run_id
'09437777-a9e4-4cc6-aac0-96c2c8352d10'

Get executed optimizer's configuration parameters

experiment.runs.get_rag_params(run_id=run_id)
{'name': 'AutoAI RAG ibm-watsonx-ai SDK documentation', 'description': 'AutoAI RAG experiemnt trainded on ibm-watsonx-ai SDK documentataion', 'max_number_of_rag_patterns': 6, 'optimization_metrics': ['answer_correctness']}

Get historical rag_optimizer instance and training details

historical_opt = experiment.runs.get_rag_optimizer(run_id)

List trained patterns for selected optimizer

historical_opt.summary()

Clean up

To delete the current experiment, use the cancel_run method.

Warning: Be careful: once you delete an experiment, you will no longer be able to refer to it.

rag_optimizer.cancel_run()
'SUCCESS'

To delete the deployment, use the delete method.

Warning: Keeping the deployment active may lead to unnecessary consumption of Compute Unit Hours (CUHs).

client.deployments.delete(deployment_id)
'SUCCESS'

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.