Path: blob/master/cloud/notebooks/rest_api/deployments/foundation_models/Use watsonx to extract the named entities of climate fever document.ipynb
5214 views

Use watsonx to extract the named entities from climate fever documents
This notebook contains the steps and code to demonstrate support of named entity extraction in watsonx. It introduces commands for data retrieval and model testing.
Some familiarity with Python is helpful. This notebook uses Python 3.12.
Introduction
The objective is to explore and utilize the watsonx.ai model for entity extraction. Model is a pre-trained language model which can be used for token-level entity extraction tasks. Entity extraction, also known as Named Entity Recognition (NER), involves identifying and classifying named entities (such as persons, organizations, locations, dates, etc.) from unstructured text.
Here are the steps we took in this notebook for Named Entity Extractions:
Data Collection and Preprocessing: Collect or obtain a dataset containing text documents
Instructions: Define the task and the prompt: Determine the specific entity extraction task we want the model to perform. Design an appropriate prompt that includes relevant instructions for the model, such as input format and expected output format.
Training Examples: provide training examples in the form of input-output pairs. Each input example consists of a prompt and corresponding tokenized text, while the output is the target entity labels associated with the tokens in the text.
Evaluation: Compare the predicted entity labels with the pseudo ground truth labels in the test set. Calculate evaluation metrics, such as precision, recall, and F1-score, to assess the performance of the model for entity extraction.(we do not have ground truth entity extraction data for this dataset, we use an open source package to create a pseudo-ground truth that can be used for demonstration purposes.)
Learning goal
The goal of this notebook is to demonstrate how to use watsonx.ai model to extract named entities for climate change claims.
Use case & dataset
A dataset adopting the FEVER methodology that consists of 1,535 real-world claims regarding climate change collected on the internet. Each claim is accompanied by five manually annotated evidence sentences retrieved from the English Wikipedia that support, refute, or do not give enough information to validate the claim, totaling 7,675 claim-evidence pairs. The dataset features challenging claims that relate to multiple facets and disputed cases of claims where both supporting and refuting evidence are present. Named entities are extracted from the claims using the watsonx.ai model.
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 dependencies
You need to install the required dependencies below 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 personal 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 climate dataset.
Read the data.
Split data to train and test
Inspect data sample
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
Preparing the dictionaries of the inputs: for demonstration purposes, we provide the examples using an open source entity extraction model.
Creating text format from the above dictionary
Extract the named entities of climate claim 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.
List of all possible NERs: As we do not have ground truth entity extraction data for this dataset, we use an open-source package to get the list of named entities.
Define instructions for the model and make requests.
Explore model output.
Score the Model
First, we need to extract y_true by performing NER using Spacy package as the ground truth.
Post-processing the results so that they can be compared with the ground truth
This function finds common words in two given phrases.
This function removes unnecessary "the" and "a" from the given phrase.
This function handles imbalanced quotation marks.
The performance of the model can be compared to ground truth labels. The code below handles this task by comparing the identified phrases, which are common in both ground truth and model results. This task is done by ignoring the order in which phrases appear in both ground truth and LLM results and comparing the length of common words in both of them.
Let's only apply for a single entity type: Location
SINGLE ENTITY
Single entity case: We tried a single entity extraction as well. It is essential to consider the quality of the extraction process. If the objective is to extract multiple entity types and the accuracy is not good enough, you may want to experiment with a smaller set of entity types at a time to see whether the accuracy can be improved (as there are more examples of that entity type that can fit in the context of the model, compared to the case of many entity types).
Here, we are experimenting with a single entity type.
This function replaces the ground truth labels with the desired ones as mentioned in the replacement dictionary.
Summary and next steps
You successfully completed this notebook!
You learned how to extract named entities with LLM on watsonx.
Check out our Online Documentation for more samples, tutorials, documentation, how-tos, and blog posts.
Author: Kahila Mokhtari
Copyright © 2026 IBM. This notebook and its source code are released under the terms of the MIT License.