Path: blob/master/cpd5.2/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG and Chroma to create a pattern about IBM.ipynb
6412 views
Use AutoAI RAG and Chroma to create a pattern and get information from ibm-watsonx-ai
SDK documentation
Disclaimers
Use only Projects and 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
Table of Contents
This notebook contains the following parts:
Install dependencies
Note: ibm-watsonx-ai
documentation can be found here.
Successfully installed Pillow-11.2.1 SQLAlchemy-2.0.41 XlsxWriter-3.2.3 aiohappyeyeballs-2.6.1 aiohttp-3.12.4 aiosignal-1.3.2 annotated-types-0.7.0 anyio-4.9.0 asgiref-3.8.1 attrs-25.3.0 backoff-2.2.1 bcrypt-4.3.0 beautifulsoup4-4.12.3 build-1.2.2.post1 cachetools-5.5.2 certifi-2025.4.26 charset-normalizer-3.4.2 chroma-hnswlib-0.7.6 chromadb-0.5.23 click-8.2.1 coloredlogs-15.0.1 dataclasses-json-0.6.7 deprecated-1.2.18 distro-1.9.0 durationpy-0.10 elastic-transport-8.17.1 elasticsearch-8.18.1 fastapi-0.115.12 filelock-3.18.0 flatbuffers-25.2.10 frozenlist-1.6.0 fsspec-2025.5.1 google-auth-2.40.2 googleapis-common-protos-1.70.0 grpcio-1.67.1 h11-0.16.0 hf-xet-1.1.2 httpcore-1.0.9 httptools-0.6.4 httpx-0.28.1 httpx-sse-0.4.0 huggingface-hub-0.32.3 humanfriendly-10.0 ibm-cos-sdk-2.14.1 ibm-cos-sdk-core-2.14.1 ibm-cos-sdk-s3transfer-2.14.1 ibm-watsonx-ai-1.3.23 idna-3.10 importlib-metadata-8.6.1 importlib-resources-6.5.2 jmespath-1.0.1 jsonpatch-1.33 jsonpointer-3.0.0 kubernetes-32.0.1 langchain-0.3.25 langchain-chroma-0.1.4 langchain-community-0.3.24 langchain-core-0.3.63 langchain-elasticsearch-0.3.0 langchain-ibm-0.3.11 langchain-milvus-0.1.8 langchain-text-splitters-0.3.8 langsmith-0.3.43 lomond-0.3.3 lxml-5.4.0 markdown-3.4.1 markdown-it-py-3.0.0 marshmallow-3.26.1 mdurl-0.1.2 milvus-lite-2.4.12 mmh3-5.1.0 mpmath-1.3.0 multidict-6.4.4 mypy-extensions-1.1.0 numpy-1.26.4 oauthlib-3.2.2 onnxruntime-1.22.0 opentelemetry-api-1.33.1 opentelemetry-exporter-otlp-proto-common-1.33.1 opentelemetry-exporter-otlp-proto-grpc-1.33.1 opentelemetry-instrumentation-0.54b1 opentelemetry-instrumentation-asgi-0.54b1 opentelemetry-instrumentation-fastapi-0.54b1 opentelemetry-proto-1.33.1 opentelemetry-sdk-1.33.1 opentelemetry-semantic-conventions-0.54b1 opentelemetry-util-http-0.54b1 orjson-3.10.18 overrides-7.7.0 packaging-24.2 pandas-2.2.3 posthog-4.2.0 propcache-0.3.1 protobuf-5.29.5 pyYAML-6.0.2 pyasn1-0.6.1 pyasn1-modules-0.4.2 pydantic-2.11.5 pydantic-core-2.33.2 pydantic-settings-2.9.1 pymilvus-2.5.6 pypdf-5.5.0 pypika-0.48.9 pyproject_hooks-1.2.0 python-docx-1.1.2 python-dotenv-1.1.0 python-pptx-1.0.2 pytz-2025.2 requests-2.32.2 requests-oauthlib-2.0.0 requests-toolbelt-1.0.0 rich-14.0.0 rsa-4.9.1 setuptools-80.9.0 shellingham-1.5.4 simsimd-6.2.1 sniffio-1.3.1 soupsieve-2.7 starlette-0.46.2 sympy-1.14.0 tabulate-0.9.0 tenacity-9.1.2 tokenizers-0.20.3 tqdm-4.67.1 typer-0.16.0 typing-extensions-4.13.2 typing-inspect-0.9.0 typing-inspection-0.4.1 tzdata-2025.2 ujson-5.10.0 urllib3-2.4.0 uvicorn-0.34.2 uvloop-0.21.0 watchfiles-1.0.5 websocket-client-1.8.0 websockets-15.0.1 wrapt-1.17.2 yarl-1.20.0 zipp-3.22.0 zstandard-0.23.0
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 projects
First, you need to create a project for your work. If you do not have a project already, create one by following these steps:
Open IBM Cloud Pak for Data
From the menu, click View all projects
Create a new project
Go to the Manage tab
Copy the
project_id
Action: Assign the project ID below
To be able to interact with all resources available in watsonx.ai, you need to set the project which you will be using.
RAG Optimizer definition
Define a connection to the training data
Upload the training data to the project as a data asset and then define a connection to the file. This example uses the ModelInference
description from the ibm_watsonx_ai
documentation.
Upload the training data to the project as a data asset.
Define a connection to the training data.
Define a connection to the test data
Upload a json
file that you want to use as a benchmark to the project as a data asset 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 project as a data asset with json
extension.
Define a connection to the benchmark testing data.
Configure the RAG Optimizer
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.
Create the index/collection
Build a solution using the best pattern with additional document indexing.
To check the index_name
that you are working on, use the best_pattern
method.
Query the RAGPattern locally to test it.
Get the executed optimizer's configuration parameters
Get the historical rag_optimizer
instance and training details
List trained patterns for the selected optimizer
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.