Solution of complex Named Entity Recognition tasks (and subtask Nested NER) based on modern Large Language Models (LLMs).
You should form python dictionaries for every text and labels. Let's look at an simplified example from Russian Drug Reaction Corpus (RuDReC).
Это старый-добрый Римантадин, только в сиропе.
Римантадин - Drugname, сиропе - Drugform
Instruction
- task description for LLMEnglish:
You are solving the NER problem. Extract from the text words related to each of the following entities: Drugname, Drugclass, DI, ADR, Finding.
English:
You are solving the NER problem. Extract from the text words related to each of the following entities: Drugname, Drugclass, DI, ADR, Finding.
dictionary with labels
.You can use one of two supported versions.
raw_entities = {
'Drugname' : [ 'Римантадин' ],
'Drugclass' : [],
'Drugform' : [ 'сиропе' ],
'DI' : [],
'ADR' : [],
'Finding' : []
}
short_form_output = True ( available with Nerel - BIO and MultiCoNER )
raw_entities = {
'Drugname' : [ 'Римантадин' ],
'Drugform' : [ 'сиропе' ]
}
MODEL_INPUT_TEMPLATE
. MODEL_INPUT_TEMPLATE = {
'prompts_input' : "### Задание: {instruction} n ### Вход: {inp} n ### Ответ: " ,
'output_separator' : "Ответ: "
}
Or english version
MODEL_INPUT_TEMPLATE = {
'prompts_input' : "### Task: {instruction} n ### Input: {inp} n ### Answer: " ,
'output_separator' : "Answer: "
}
Instruction
instruction_ner/utils/instruct_dataset.py
class Instruction ( TypedDict ):
instruction : str
input : str
output : str
source : str
raw_entities : dict [ str , list [ str ]]
id : str
{ 'instruction' : 'Ты решаешь задачу NER. Извлеки из текста слова, относящиеся к каждой из следующих сущностей: Drugname, Drugclass, DI, ADR, Finding.' ,
'input' : 'Это старый-добрый Римантадин, только в сиропе. n ' ,
'output' : 'Drugname: Римантадин n Drugclass: n Drugform: сиропе n DI: n ADR: n Finding: n ' ,
'source' : '### Задание: Ты решаешь задачу NER. Извлеки из текста слова, относящиеся к каждой из следующих сущностей: Drugname, Drugclass, DI, ADR, Finding. n ### Вход: Это старый-добрый Римантадин, только в сиропе. n ### Ответ: ' ,
'raw_entities' : { 'Drugname' : [ 'Римантадин' ],
'Drugclass' : [],
'Drugform' : [ 'сиропе' ],
'DI' : [],
'ADR' : [],
'Finding' : []},
'id' : '1_2555494.tsv' }
instruction_ner/utils/
instructions
python medner / instruction_ner / train_instruct . py
- - config_file medner / instruction_ner / configs / mistral_7b . json
- - model_type mistral
- - dataset_name conll2003
- - max_instances - 1
- - push_to_hub True
- - hf_name_postfix _extended_instruction
instructions
to generate prediction.json
python medner / instruction_ner / inference_instruct . py
- - batch_size 16
- - dataset_name conll2003
- - model_type mistral
- - model_name poteminr / mistral - conll2003_extended_instruction
- - max_instances - 1
instruction_ner/metric.py
You can use the implemented functions with the output of inference_instruct calculate metrics.
import pandas as pd
from utils . rudrec . rudrec_utis import ENTITY_TYPES
from metric import calculate_metrics_from_dataframe
prediction = pd . read_json ( 'prediction.json' )
prediction . head ( 3 )
id | extracted | target | |
---|---|---|---|
0 | 8_1443820.tsv | {'Drugname': [], 'Drugclass': [], 'Drugform': ['tablets'], 'DI': [], 'ADR': [], 'Finding': []} | {'Drugname': [], 'Drugclass': [], 'Drugform': ['tablets'], 'DI': [], 'ADR': [], 'Finding': []} |
1 | 1_2555494.tsv | {'Drugname': ['Rimantadine'], 'Drugclass': [], 'Drugform': ['syrup'], 'DI': [], 'ADR': [], 'Finding': []} | {'Drugname': ['Rimantadine'], 'Drugclass': [], 'Drugform': ['syrup'], 'DI': [], 'ADR': [], 'Finding': []} |
2 | 1_618967.tsv | {'Drugname': [], 'Drugclass': [], 'Drugform': [], 'DI': [], 'ADR': [], 'Finding': []} | {'Drugname': [], 'Drugclass': [], 'Drugform': [], 'DI': [], 'ADR': [], 'Finding': []} |
from metric import calculate_metrics_from_dataframe
metrics = calculate_metrics_from_dataframe ( prediction , ENTITY_TYPES )
{ 'Drugname' : { 'precision' : 0.9670250896057347 ,
'recall' : 0.9195637355146558 ,
'f1' : 0.9426974143955277 }, ...}
You can explore 5 types of model errors:
Confusion matrix for mistaken recognitions is available.
Instruction LLM for NER performs well on flat entities, but performs poorly on datasets with large tagset and nested entities.
Thus, LLM and encoder model produce comparable results on flat-ner datasets with incredibly different training and inference times.
and other models on HF such as T5, Llama, Mistral: poteminr