Path: blob/master/cpd5.1/notebooks/python_sdk/experiments/autoai_rag/Use AutoAI RAG with watsonx Text Extraction service.ipynb
6405 views
Use AutoAI RAG with watsonx Text Extraction service
Disclaimers
Use only Projects and Spaces that are available in the watsonx context.
Notebook content
This notebook demonstrates how to process data using the IBM watsonx.ai Text Extraction service and use the result in an AutoAI RAG experiment. The data used in this notebook is from the Granite Code Models paper
Some familiarity with Python is helpful. This notebook uses Python 3.11.
Learning goal
The learning goals of this notebook are:
Process data using the IBM watsonx.ai Text Extraction service
Create an AutoAI RAG job that will find the best RAG pattern based on processed data
Contents
This notebook contains the following parts:
Install and import the required modules and dependencies
Connect to WML
Authenticate the Watson Machine Learning service on IBM Cloud Pak for Data. You need to provide the platform url
, your username
, and your api_key
.
Alternatively, you can use your username and password to authenticate WML services.
Create an instance of APIClient with authentication details
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 be able to interact with all resources available in Watson Machine Learning, set the space that you are using.
Create an instance of COS client
Connect to the Cloud Object Storage instance for the by using the ibm_boto3
package.
Action: Assign COS credentials below
Create client
Initialize the client connection to the created bucket and get the connection ID.
Prepare data and connections for the Text Extraction service
The document, from which we are going to extract text, is located in the IBM Cloud Object Storage (COS). In this notebook, we will use the Granite Code Models paper as a source text document. The final results file, which will contain extracted text and necessary metadata, will be placed in the COS. So we will use the ibm_watsonx_ai.helpers.DataConnection
and the ibm_watsonx_ai.helpers.S3Location
class to create Python objects that will represent the references to the processed files. Reference to the final results will be used as an input for the AutoAI RAG experiment.
Download and upload training data to the COS bucket. Then define a connection to the uploaded file.
Input file connection.
Output file connection.
Initialize the Text Extraction service endpoint.
Run a text extraction job for connections created in the previous step.
Wait for the job to be complete.
Get the text extraction result.
†Corresponding Authors
Large Language Models (LLMs) trained on code are revolutionizing the software development process. Increasingly, code LLMs are being inte- grated into software development environments to improve the produc- tivity of human programmers, and LLM-based agents are beginning to show promise for handling complex tasks autonomously. Realizing the full potential of code LLMs requires a wide range of capabilities, including code generation, fixing bugs, explaining and documenting code, maintaining repositories, and more. In this work, we introduce the Granite series of decoder-only code models for code generative tasks, trained with code written in 116 programming languages. The Granite Code models family consists of models ranging in size from 3 to 34 billion parameters, suitable for applications ranging from complex application modernization tasks to on-device memory-constrained use cases. Evaluation on a comprehensive set of tasks demonstrates that Granite Code models consistently reaches state-of-the-art performance among available open-source code LLMs. The Granite Code model family was optimized for enterprise software devel- opment workflows and performs well across a range of coding tasks (e.g. code generation, fixing and explanation), making it a versatile “all around” code model. We release all our Granite Code models under an Apache 2.0 license for both research and commercial use. https://github.com/ibm-granite/granite-code-models
1 Introduction
Over the last several decades, software has been woven into the fabric of every aspect of our society. As demand for software development surges, it is more critical than ever to increase software development productivity, and LLMs provide promising path for augmenting human programmers. Prominent enterprise use cases for LLMs in software development productivity include code generation, code explanation, code fixing, unit test and documentation generation, application modernization, vulnerability detection, code translation, and more.
Recent years have seen rapid progress in LLM’s ability to generate and manipulate code, and a range of models with impressive coding abilities are available today. Models range in 43.5
41.5
37.4
34.4
31.2
29.2
29.0
33.2
18.9
21.3
15.1
15.0
29.6
14.2 O.
8.9
2.9
15.0
13.4
13.5
13.9
12.4Generating Code
Explaining Code
Fixing Code
Average
O MistralAI/Mistral-7B W @ Meta/Llama-3-8B
@) Google/Gemma-7B
Meta/CodeLlama-7B @) Google/CodeGemma-7B @ IBM/Granite-8B-Code-Base W
@ BigCode/StarCoder2-7B
Apache 2.0 License
41.6
36.9
37.9
19.8
40.9
38.4
29.1 |
25.1
21
41.9
43.6
5.8
31.8
25.3
18.3
13.7
112.8
Generating Code
Explaining Code
Fixing Code
Average
O MistralAI/Mistral-7B-Instruct-v0.2
@ Meta/CodeLlama-7B-Instru
Upload a json
file to use for benchmarking to COS and define a connection to this file.
Note: correct_answer_document_ids
must refer to the document processed by text extraction service, not the initial document.
Test the data connection.
Use the reference to the Text Extraction job result as input for the AutoAI RAG experiment.
Call the run()
method to trigger the AutoAI RAG experiment. Choose one of two modes:
To use the interactive mode (synchronous job), specify
background_mode=False
To use the background mode (asynchronous job), specify
background_mode=True
Get the selected pattern
Get the RAGPattern object from the RAG Optimizer experiment. By default, the RAGPattern of the best pattern is returned.
Test the RAGPattern by querying it locally.
Summary
You successfully completed this notebook!
You learned how to use AutoAI RAG with documents processed by the TextExtraction service.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author:
Witold Nowogórski, Software Engineer at watsonx.ai.
Copyright © 2025 IBM. This notebook and its source code are released under the terms of the MIT License.