Global AI and Data Science

 View Only

Named Entity Recognition with watsonx

By Patrick Meyer posted Thu August 03, 2023 12:51 PM

A named entity is a word or group of words that represent a real-world object, such as a person, date, place, organization, or product. Named Entity Recognition (NER) is a subtask of Natural Language Processing (NLP) that involves identifying and extracting named entities from text. The goal of NER is to identify and classify these entities in unstructured textual data, such as news articles, social media posts, or internal documents. NER systems today use machine learning algorithms to train a model to extract entities based on their context, syntax, and other linguistic characteristics. These extracted elements may be used to enrich a knowledge database or a knowledge graph, for example. This is a difficult task due to the complexity of natural language and the ambiguity of words and phrases. However, advances in machine learning and deep learning algorithms have greatly improved the accuracy and performance of NER systems in recent years.
Launched two months ago, watsonx comes with incredible LLMs capabilities which could help you extract named entities without having to train your own model. LLMs (for Large Language Models) is a category of neural models that are trained to predict the probability of the next word (token) or the missing one. LLMs are trained on very large amounts of textual data, such as public web pages, Wikipedia, books, and news articles, with the goal that these networks learn human language. They have become extremely popular in the last months due to their ability to generate coherent and natural text.
Let’s consider this following approach using Watsonx:
  • Choose the "Experiment with foundation models and build prompts" task
Select the Experiment with foundation models and build prompts
This allows you to enter directly into the Prompt Lab.
  • Enter into the Prompt Lab

The Prompt Lab facilitates expedient exploration and development of solutions using substantial language models by enabling users to experiment with prompts. These prompts are straightforward text inputs that effectively instruct the model to execute specific commands. Additionally, prompts may incorporate a limited number of examples to steer the model towards the precise behavior sought by the user. This is exactly what we will do to extract named entities.

Select the Freeform option. This will let you enter the prompt in a free way. An alert will pop-up, just accept it. The idea is to freely write the best prompting for a Named Entity Recognition NLP task.

If you switch to freeform mode, your text will be formatted in one text box. If you switch back to structured mode your text might be in the wrong fields.
  • Write your best prompt

The idea is to give to the model a few examples so that it can improve its extraction. We call this few-shot learning because we constraint the model to follow our directive based on some examples.

Enter the following prompt:

Extract all the named entities from the text surrounded by quotes. Use the following predefined entity labels: PERSON, ORGANIZATION, LOCATION, DATE, GPE, TIME.

Text: "Thousands of demonstrators have marched through London to protest the war in Iraq and demand the withdrawal of British troops from that country."
Entities: London=LOCATION | Iraq=LOCATION | British=GPE

Text: "Families of soldiers killed in the conflict joined the protesters who carried banners with such slogans as 'Bush Number One Terrorist' and 'Stop the Bombings.'"
Entities: Bush=PERSON

Text: "They marched from the Houses of Parliament to a rally in Hyde Park."
Entities: Hyde Park=LOCATION

Text: "Police put the number of marchers at 10,000 while organizers claimed it was 100,000."
Entities: NONE

Text: "The protest comes on the eve of the annual conference of Britain's ruling Labor Party in the southern English seaside resort of Brighton."

Do not add any spaces or new lines after the "Entities:"

These examples are taken from the GMB dataset. You can find it here:

The prompt is separated in 3 parts: one containing the request, one containing the examples based on two lines with always the same form = Text and Entities, and the text to extract followed by an empty "Entities: " line.


  • Select a model

Not all models are equivalent to this task. Mainly instruct-based models can do that. I recommand you to select the "flan-t5-xxl-11b" model.

Select the flan-t5-xxl-11b for the NER task.
The "flan-t5-xxl-11b" model is a 11 billion parameter model based on the Flan-T5 family provided by Google. Using the drop-down list, you can select another one and test its performance.
Change the Model parameters on the right and choose these values using the "Sampling" mode:
  • Temperature = 0.84
  • Top P = 1
  • Top K = 50
  • Repetition penalty = 1.02
  • Min tokens = 2
  • Max tokens = 250
  • Click on the "Generate" Button

If you have followed my example, you will have an output equivalent to this: Brighton=LOCATION, Labor Party=ORGANISATION, Britain=GPE, Labor Party=ORGANISATION, Brighton=LOCATION.

    It is not the best extraction I have ever seen but the job is not so bad regarding the fact that the model has not been explicitly trained to do entity recognition.
    After generation, Prompt Lab highlights the result.
    • Save your work
    You can now save you work using the "Save work" drop-down list. You can either create a notebook by using the save to "Notebook" option. This will convert the current experimentation into Python and allow you to rewrite the code and test the changes in your environnement using the API provided by Watsonx.