Path: blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx to classify Consumer Financial Protection Bureau (CFPB) document.ipynb
9359 views

Use watsonx to classify consumer financial protection bureau document
Disclaimers
Use only Projects and Spaces that are available in watsonx context.
Notebook content
This notebook contains the steps and code to demonstrate support of complaint classification in watsonx. It introduces commands for data retrieval and model testing.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Learning goal
The goal of this notebook is to demonstrate how to use watsonx.ai model to Consumer Financial Protection Bureau (CFPB) document.
Use case & dataset
The Consumer Financial Protection Bureau (CFPB) is a federal U.S. agency that acts as a mediator when disputes arise between financial institutions and consumers. Via a web form, consumers can send the agency a narrative of their dispute. A foundation model would make the classification of complaints and their routing to the appropriate teams more efficient than manually tagged complaints. Dataset has narrative and 5 product categories as follow:
credit reporting
debt collection
mortgages and loans
credit cards
retail banking
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 and information about how to create the instance can be found here).
Install and import the datasets and dependecies
you need to install below required dependencies to be able to continue
Inferencing class
This cell defines a class that makes a REST API call to the watsonx Foundation Model inferencing API that we will use to generate output from the provided input. The class takes the access token created in the previous step, and uses it to make a REST API call with input, model id and model parameters. The response from the API call is returned as the cell output.
Action: Provide watsonx.ai Runtime url to work with watsonx.ai.
Define a PromptClient class for prompts generation.
watsonx API connection
This cell defines the credentials required to work with watsonx API for Foundation Model inferencing.
Action: Provide the IBM Cloud user API key. For details, see documentation.
Defining the project id
The API requires project id that provides the context for the call. We will obtain the id from the project in which this notebook runs:
Download the financial documents dataset.
Read the data.
Inspect the product class distribution.
Split data to train and test
List available chat models
You need to specify model_id that will be used for inferencing:
Prepare model inputs for zero-shot example - use below zero_shot_inputs.
Prepare model inputs for few-shot examples - use below few_shot_inputs.
Generate the classification of Consumer Financial Protection Bureau (CFPB) document using watsonx.ai model.
Note: You might need to adjust model parameters for different models or tasks, to do so please refer to documentation.
Initialize the PromptClient class.
Hint: Your authentication token might expire, if so please regenerate the access_token and reinitialize the PromptClient class.
Define instructions for the model and get the product classes.
Explore model output.
Get the true labels.
Get the prediction labels.
Calculate the accuracy score.
Summary and next steps
You successfully completed this notebook!
You learned how to classify Consumer Financial Protection Bureau documents with LLM's on watsonx.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Copyright © 2026 IBM. This notebook and its source code are released under the terms of the MIT License.