Entity extraction, also known as Named Entity Recognition, is a crucial task in natural language processing that focuses on identifying and classifying key information from unstructured text. This process involves detecting specific entities such as names of people, organizations, locations, dates, and various other categories of information within a body of text. The primary goal of entity extraction is to convert unstructured data into structured formats that can be easily analyzed and interpreted by computers. By transforming raw text into structured data, entity extraction facilitates better information retrieval, content organization, and insights generation from large volumes of textual data.
Entity extraction using Language Models has emerged as a powerful method for identifying and categorizing entities from unstructured text. Language Models excel in understanding the context surrounding words, which allows them to accurately identify entities based on their usage within sentences. This capability significantly reduces errors associated with ambiguous terms that traditional NER systems might misclassify due to a lack of contextual awareness
This article was published as a part of the Data Science Blogathon.
Entity extraction has come a long way from traditional rule-based systems to machine learning models, and now to advanced language models. Unlike older methods, which often struggled with ambiguous terms or lacked the flexibility to adapt to new contexts, language models bring a contextual understanding of text. They analyze not just individual words but the relationships between them, allowing for a more accurate identification and classification of entities like names, organizations, locations, and dates.
What sets language models apart is their ability to leverage vast amounts of training data and sophisticated architectures, like transformer-based designs, to recognize patterns in text. This makes them exceptionally effective in handling complex sentences and detecting subtle variations in how entities are expressed. Whether it’s disambiguating terms like “Apple” (the company vs. the fruit) or recognizing new, domain-specific entities without retraining, language models have revolutionized the way unstructured data is transformed into actionable insights. Their adaptability and precision have made them indispensable tools in modern natural language processing.
Small Language Models have fewer parameters (typically under 10 billion), which dramatically reduces the computational costs and energy usage. They focus on specific tasks and are trained on smaller datasets. This maintains a balance between performance and resource efficiency.
Gemma 2B is a lightweight, state-of-the-art language model developed by Google, designed to perform effectively across various natural language processing tasks.
Key Features of Model
Some other optimizations in the architecture of Gemma 2B are the following:
Llama 3.2 is a collection of multilingual large language models developed by Meta. It offers various parameter sizes, including the 1 billion (1B) and 3 billion (3B) versions.
Key Features of Model
Alibaba Cloud developed Qwen 7B, a language model designed for a variety of natural language processing tasks.
Key Features of Model
Running models on Google Colab using Ollama provides a seamless way to implement and evaluate small language models for entity extraction tasks. With minimal setup, users can leverage powerful models to process text and extract key entities efficiently.
Below we will install all the required libraries:
!sudo apt update
!sudo apt install -y pciutils
!pip install langchain-ollama
!pip install ollama==0.4.2
Once the installation is done, it is time to import the libraries.
import threading
import subprocess
import time
from langchain_core.prompts import ChatPromptTemplate
from langchain_ollama.llms import OllamaLLM
from IPython.display import Markdown
Start the Ollama server in the background on Colab to enable seamless interaction with the language models.
def run_ollama_serve():
subprocess.Popen(["ollama", "serve"])
thread = threading.Thread(target=run_ollama_serve)
thread.start()
time.sleep(5)
We use the first 10 rows of this dataset from github for a comparison of extracted entities as outputs from different small language models.
import pandas as pd
df1 = pd.read_csv("generated_highlight_samples.csv",encoding='latin-1',header=None)
df1.columns =['text','entities_org']
df1.shape
Retrieve the desired language model from Ollama to begin processing text for entity extraction.
template = """Question: {question}"""
prompt = ChatPromptTemplate.from_template(template)
model = OllamaLLM(model="mistral")
chain = prompt | model
from tqdm import tqdm
resp=[]
for texts in tqdm(df1['text'].values.tolist()[:10]):
input_data = {
"question": """ONLY EXTRACT "Project", "Companies" and "People" from the following text in the format WITHOUT ANY ADDITIONAL TEXT ["Project": " " , "Companies" : " ", "People" : " "] - %s"""%(texts)}
# Invoke the chain with input data and display the response in Markdown format
response = chain.invoke(input_data)
resp.append([texts,response])
# Create DataFrame of Extracted Entities
resp1 = pd.DataFrame(resp)
resp1.columns =['Text','Entities']
df2 = df1.iloc[:10,:]
resp1['entities_org']=df2['entities_org'].values.tolist()
The evaluation framework for assessing entity extraction focuses on measuring the accuracy of identified entities like projects, companies, and people. Each model’s output is scored based on its ability to extract entities correctly, partially, or not at all, with scores aggregated across multiple test cases. This approach ensures a fair comparison of model performance in diverse scenarios.
Let us take a sample row from the dataset.
"In a groundbreaking collaboration, Vertex brings together Allianz and Google,
leveraging their expertise to drive innovation, with David at the forefront,
overseeing a team that has achieved a 35% increase in operational efficiency and a
25% reduction in costs, ultimately enhancing customer experience for over 500,000
users, and paving the way for a potential 40% market expansion within the next two
years."
As given in the second column of the dataset, these are the valid Project, Companies and People Entities mentioned in the text.
{“projects”: [“Vertex”],”companies”: [“Allianz”,”Google”],”people”: [“David”]}
In order to evaluate the LLM model for entity extraction, we apply the following procedure:
Example:
Output_Scenario_1: {“projects”: [“”],”companies”: [“Allianz”,”Google”],”people”: [“”]}
For the above output from the LLM, score becomes the following: Number of Correctly Extracted Project Entities - 0 Number of Correctly Extracted Company Entities -1 Number of Correctly Extracted People Entities - 0
Output _Scenario_2: {“projects”: [“Vertex”],”companies”: [“Google”],”people”: [“”]}
For the above output from the LLM, score becomes the following: Number of Correctly Extracted Project Entities - 1 Number of Correctly Extracted Company Entities - 0.5 Number of Correctly Extracted People Entities - 0
Finally, we sum these scores for all the rows in the dataset to calculate the total number of correctly extracted entities across each category, as the table below shows.
Model | Number of Correctly Extracted Project Entities | Number of Correctly Extracted Company Entities | Number of Correctly Extracted People Entities | Average Score |
Gemma 2B | 9 | 10 | 10 | 9.7 |
Llama 3.2 1 B | 5 | 6.5 | 6.5 | 6 |
Llama 3.2 3 B | 6 | 6.5 | 10 | 7.5 |
Qwen 7B | 5 | 3 | 10 | 6 |
As we can see from the table above –
According to the sample test results, Gemma 2B emerged as the top-performing model. Nevertheless, we highly recommend that users conduct their own testing with their specific datasets to confirm the findings.
The comparative assessment of models such as Gemma 2B, Llama 3.2 (both 1B and 3B versions), and Qwen 7B highlights the strengths of these advanced architectures in entity extraction tasks. Gemma 2B stands out with the highest accuracy overall, particularly excelling in extracting various entity types. Llama 3.2 3B also performs well, especially in identifying people entities, while Qwen 7B shows a strong performance in this category despite lower accuracy in extracting project and company entities.
Based on the sample testing example, Gemma 2B was the best-performing model. However, we strongly encourage users to test it on their own datasets to validate the results.
In summary, the incorporation of language models into entity extraction processes not only enhances accuracy but also provides the flexibility needed to adapt to evolving data landscapes. As these models continue to advance, they will play an increasingly critical role in transforming unstructured text into actionable insights across various industries.
A. Language Models improve entity extraction by understanding the context around words, which allows for accurate identification of entities, reducing errors that traditional NER systems might make due to lack of context.
A. Small Language Models (SLMs) are language models with fewer parameters, typically under 10 billion, making them more resource-efficient. They are optimized for specific tasks and trained on smaller datasets, balancing performance and computational efficiency. These models are ideal for applications that require fast responses and minimal resource consumption.
A. Llama 3.2 is a multilingual language model with versions of 1B and 3B parameters, designed for tasks such as retrieval and summarization in various languages. It supports up to 128,000 tokens of context and is optimized for dialogue use cases.
A. Gemma 2B is a lightweight, state-of-the-art language model developed by Google, featuring 2 billion parameters and a context length of 8,192 tokens, optimized for various NLP tasks. It utilizes a decoder-only transformer architecture and is open-source, trained on approximately 2 trillion tokens from diverse sources.
A. Alibaba Cloud developed Qwen 7B, a language model with 7 billion parameters and a context length of 8,192 tokens, designed for various NLP tasks. It uses a decoder-only transformer architecture, pre-trained on 2.4 trillion tokens, and includes optimizations like Rotary Positional Embeddings (RoPE) and SwiGLU activation.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.