File size: 16,694 Bytes
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
a4e88ec
7a056d3
9bab57b
7a056d3
9bab57b
 
7a056d3
9bab57b
7a056d3
 
 
1e12620
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b106941
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ff5c91a
7a056d3
 
 
 
 
 
 
93a422a
 
7a056d3
1e12620
 
a4e88ec
1e12620
 
a4e88ec
6354a79
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42d788f
 
 
 
 
 
7a056d3
42d788f
 
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4bb81d8
6a9e84a
af3e2cf
4673016
7a056d3
 
 
 
 
 
 
 
6a9e84a
 
7a056d3
5237ac5
 
6a9e84a
7a056d3
5237ac5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6a9e84a
 
 
 
 
 
5237ac5
6a9e84a
 
7a056d3
6a9e84a
 
7a056d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
---
license: llama3.1
datasets:
- Universal-NER/Pile-NER-type
language:
- en
pipeline_tag: text-generation
tags:
- zero-shot NER
- NER
base_model:
- meta-llama/Llama-3.1-8B-Instruct
---

# SLIMER-PARALLEL-LLaMA3

SLIMER is an LLM specifically instructed for zero-shot NER on English language.

This LLaMA-3 based SLIMER scores __+17 %__ over paper's original SLIMER LLaMA-2,
while allowing up to 16 NEs to be extracted __in parallel__ per prompt. 

GitHub repository: https://github.com/andrewzamai/SLIMER/tree/v2.0

SLIMER for Italian language can be found at: https://huggingface.co/expertai/LLaMAntino-3-SLIMER-IT

Instructed on a reduced number of samples, it is designed to tackle never-seen-before Named Entity tags by leveraging a prompt enriched with a DEFINITION and GUIDELINES for the NEs to be extracted.

<!DOCTYPE html>
<html>
<head>
    <title>Instruction Tuning Prompt</title>
    <style>
        .container {
            border: none;
            padding: 5px;
            width: 300px;
            margin: 0 auto;
            font-family: Arial, sans-serif;
            font-size: 8px;
            border-radius: 10px; /* Rounded borders for container */
            overflow: hidden; /* Ensure child elements respect container's rounded borders */
            background-color: #f0f0f0
        }
        .header {
            background-color: black;
            color: white;
            padding: 5px;
            text-align: center;
            font-weight: bold;
            font-size: 14px;
            border-top-left-radius: 10px; /* Rounded top-left corner */
            border-top-right-radius: 10px; /* Rounded top-right corner */
        }
        .content {
            padding: 5px;
        }
        .definition, {
            padding: 5px;
            border-radius: 10px; /* Rounded borders for definition and guidelines */
        }
        .definition {
            background-color: #ffc773;
        }
        .footer {
            background-color: black;
            color: white;
            padding: 10px;
            font-weight: bold;
            border-bottom-left-radius: 10px;
            border-bottom-right-radius: 10px;
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">SLIMER-3-PARALLEL prompt</div>
        <div class="content">
            <p><b><|start_header_id|>user<|end_header_id|></b></p>
            <p>You are given a text chunk (delimited by triple quotes) and an instruction.<br>
            Read the text and answer to the instruction in the end.</p>
            <p>"""<br>
            {input text}<br>
            """</p>
            <p><b>Instruction:</b> Extract the entities of type <b>[NEs_list]</b> from the text chunk you have read. Be aware that not all of these entities are necessarily present. Do not extract entities that do not exist in the text, return an empty list for that tag. Ensure each entity is assigned to only one appropriate class.</p>
            <p>To help you, here are dedicated Definition and Guidelines for each entity tag.</p>
            <div class="definition">
            <p>{</p>
            <p>{<b>"NE_type_1":</b> {"Definition": "", "Guidelines": ""}</p>
            <p><b> ... </b>
            <p>{<b>"NE_type_N":</b> {"Definition": "", "Guidelines": ""}</p>
            <p>}</p>
            </div>
            <p>Return only a JSON object. The JSON should strictly follow this format: {"NE_type_1": [], ..., "NE_type_N":[]}. DO NOT output anything else, just the JSON itself.</p>
            <p><b><|eot_id|><|start_header_id|>assistant<|end_header_id|></b></p>
        </div>
        <div class="footer"></div>
    </div>
</body>
</html>



Currently existing approaches fine-tune on an extensive number of entity classes (around 13K) and assess zero-shot NER capabilities on Out-Of-Distribution input domains.
SLIMER performs comparably to these state-of-the-art models on OOD input domains, while being trained only a reduced number of samples and a set of NE tags that overlap in lesser degree with test sets.

We extend the standard zero-shot evaluations (CrossNER and MIT) with BUSTER, which is characterized by financial entities that are rather far from the more traditional tags observed by all models during training.
An inverse trend can be observed, with SLIMER emerging as the most effective in dealing with these unseen labels, thanks to its lighter instruction tuning methodology and the use of definition and guidelines.

<!DOCTYPE html>
<html>
<head>
    <style>
        table {
            width: 100%;
            border-collapse: collapse;
            font-size: 12px;
        }
        th, td {
            border: 1px none;
            padding: 4px;
            text-align: center;
        }
        th {
            background-color: #f2f2f2;
        }
        .col-model { width: 10%; }
        .col-backbone { width: 15%; }
        .col-params { width: 10%; }
        .col-mit, .col-crossner, .col-buster, .col-avg { width: 7%; }
    </style>
</head>
<body>

<table>
    <thead>
        <tr>
            <th class="col-model">Model</th>
            <th class="col-backbone">Backbone</th>
            <th class="col-params">#Params</th>
            <th class="col-mit" colspan="2">MIT</th>
            <th class="col-crossner" colspan="5">CrossNER</th>
            <th class="col-buster">BUSTER</th>
            <th class="col-avg">AVG</th>
        </tr>
        <tr>
            <th></th>
            <th></th>
            <th></th>
            <th class="col-mit">Movie</th>
            <th class="col-mit">Restaurant</th>
            <th class="col-crossner">AI</th>
            <th class="col-crossner">Literature</th>
            <th class="col-crossner">Music</th>
            <th class="col-crossner">Politics</th>
            <th class="col-crossner">Science</th>
            <th class="col-buster"></th>
            <th class="col-avg"></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td class="col-model">ChatGPT</td>
            <td class="col-backbone">gpt-3.5-turbo</td>
            <td class="col-params">-</td>
            <td class="col-mit">5.3</td>
            <td class="col-mit">32.8</td>
            <td class="col-crossner">52.4</td>
            <td class="col-crossner">39.8</td>
            <td class="col-crossner">66.6</td>
            <td class="col-crossner">68.5</td>
            <td class="col-crossner">67.0</td>
            <td class="col-buster">-</td>
            <td class="col-avg">-</td>
        </tr>
        <tr>
            <td class="col-model">InstructUIE</td>
            <td class="col-backbone">Flan-T5-xxl</td>
            <td class="col-params">11B</td>
            <td class="col-mit">63.0</td>
            <td class="col-mit">21.0</td>
            <td class="col-crossner">49.0</td>
            <td class="col-crossner">47.2</td>
            <td class="col-crossner">53.2</td>
            <td class="col-crossner">48.2</td>
            <td class="col-crossner">49.3</td>
            <td class="col-buster">-</td>
            <td class="col-avg">-</td>
        </tr>
        <tr>
            <td class="col-model">UniNER-type</td>
            <td class="col-backbone">LLaMA-1</td>
            <td class="col-params">7B</td>
            <td class="col-mit">42.4</td>
            <td class="col-mit">31.7</td>
            <td class="col-crossner">53.5</td>
            <td class="col-crossner">59.4</td>
            <td class="col-crossner">65.0</td>
            <td class="col-crossner">60.8</td>
            <td class="col-crossner">61.1</td>
            <td class="col-buster">34.8</td>
            <td class="col-avg">51.1</td>
        </tr>
        <tr>
            <td class="col-model">GoLLIE</td>
            <td class="col-backbone">Code-LLaMA</td>
            <td class="col-params">7B</td>
            <td class="col-mit">63.0</td>
            <td class="col-mit">43.4</td>
            <td class="col-crossner">59.1</td>
            <td class="col-crossner">62.7</td>
            <td class="col-crossner">67.8</td>
            <td class="col-crossner">57.2</td>
            <td class="col-crossner">55.5</td>
            <td class="col-buster" style="color: red;">27.7</td>
            <td class="col-avg">54.6</td>
        </tr>
        <tr>
            <td class="col-model">GLiNER-L</td>
            <td class="col-backbone">DeBERTa-v3</td>
            <td class="col-params">0.3B</td>
            <td class="col-mit">57.2</td>
            <td class="col-mit">42.9</td>
            <td class="col-crossner">57.2</td>
            <td class="col-crossner">64.4</td>
            <td class="col-crossner">69.6</td>
            <td class="col-crossner">72.6</td>
            <td class="col-crossner">62.6</td>
            <td class="col-buster" style="color: red;">26.6</td>
            <td class="col-avg">56.6</td>
        </tr>
        <tr>
            <td class="col-model">GNER-T5</td>
            <td class="col-backbone">Flan-T5-xxl</td>
            <td class="col-params">11B</td>
            <td class="col-mit">62.5</td>
            <td class="col-mit">51.0</td>
            <td class="col-crossner">68.2</td>
            <td class="col-crossner">68.7</td>
            <td class="col-crossner">81.2</td>
            <td class="col-crossner">75.1</td>
            <td class="col-crossner">76.7</td>
            <td class="col-buster" style="color: red;">27.9</td>
            <td class="col-avg">63.9</td>
        </tr>
        <tr>
            <td class="col-model">GNER-LLaMA</td>
            <td class="col-backbone">LLaMA-1</td>
            <td class="col-params">7B</td>
            <td class="col-mit">68.6</td>
            <td class="col-mit">47.5</td>
            <td class="col-crossner">63.1</td>
            <td class="col-crossner">68.2</td>
            <td class="col-crossner">75.7</td>
            <td class="col-crossner">69.4</td>
            <td class="col-crossner">69.9</td>
            <td class="col-buster" style="color: red;">23.6</td>
            <td class="col-avg">60.8</td>
        </tr>
        <tr>
            <td class="col-model"><b>SLIMER</b></td>
            <td class="col-backbone"><b>LLaMA-3.1-Instruct</b></td>
            <td class="col-params"><b>8B</b></td>
            <td class="col-mit"><b>58.4</b></td>
            <td class="col-mit"><b>45.3</b></td>
            <td class="col-crossner"><b>58.0</b></td>
            <td class="col-crossner"><b>65.0</b></td>
            <td class="col-crossner"><b>77.0</b></td>
            <td class="col-crossner"><b>71.2</b></td>
            <td class="col-crossner"><b>67.3</b></td>
            <td class="col-buster"><b>39.32</b></td>
            <td class="col-avg"><b>60.2</b></td>
        </tr>
    </tbody>
</table>

</body>
</html>


<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JSON Template</title>
<style>
  body {
    font-family: Arial, sans-serif;
    line-height: 1.6;
    padding: 20px;
  }
  .description {
    font-weight: bold;
    color: #333;
    margin-bottom: 10px;
  }
  .template {
    background-color: #f0f0f0;
    padding: 10px;
    border-radius: 5px;
    margin-bottom: 20px;
  }
  .highlight-orange {
    color: orange;
    font-weight: bold;
  }
</style>
</head>
<body>
  <div class="description">JSON SLIMER prompt</div>
  <div class="template">
  <pre>{
  "description": "SLIMER PARALLEL 3 prompt",
  "prompt_input": "<|start_header_id|>system<|end_header_id|>\n\nYou are a helpful NER assistant designed to output JSON.<|eot_id|>\n<|start_header_id|>user<|end_header_id|>\n\nYou are given a text chunk (delimited by triple quotes) and an instruction.\nRead the text and answer to the instruction in the end.\n\"\"\"\n{<span class="highlight-orange">input</span>}\n\"\"\"\nInstruction: Extract the entities of type {ne_tags} from the text chunk you have read. Be aware that not all of these entities are necessarily present. Do not extract entities that do not exist in the text, return an empty list for that tag. Ensure each entity is assigned to only one appropriate class.\nTo help you, here are dedicated Definition and Guidelines for each entity tag.\n{Def_and_Guidelines}\nReturn only a JSON object. The JSON should strictly follow this format:\n{expected_json_format}.\nDO NOT output anything else, just the JSON itself."
}</pre>
  </div>
</body>
</html>


```python
from vllm import LLM, SamplingParams

vllm_model = LLM(model="expertai/SLIMER-PARALLEL-LLaMA3")
tokenizer = vllm_model.get_tokenizer()

# suggested temperature 0, max_tokens hyperparam
cutoff_len = 4096
sampling_params = SamplingParams(temperature=0, max_tokens=1000, stop=tokenizer.eos_token)

# given list of NE types and dictionary of Def and Guidelines for each --> returns instruction
slimer_prompter = SLIMER_PARALLEL_instruction_prompter("SLIMER_PARALLEL_instruction_template", './src/SFT_finetuning/templates')

# create a dictionary of dictionaries, each NE_type as key should have a {Definition: str, Guidelines: str} value
ne_types_list = ['ORGANIZATION', 'UNIVERSITY', 'LOCATION', 'PERSON', 'CONFERENCE']
def_guidelines_per_NE_dict = {'ORGANIZATION': {'Definition': "'organization' refers to structured groups, institutions, companies, or associations.", 'Guidelines': "Avoid labeling generic terms like 'team' or 'group'. Exercise caution with ambiguous entities like 'Apple' (company vs. fruit) and 'Manchester United' (sports team vs. fan club)."}, 'UNIVERSITY': {'Definition': 'UNIVERSITY represents educational institutions that offer higher education and academic research programs.', 'Guidelines': "Avoid labeling general concepts such as 'education' or 'academia' as UNIVERSITY. Exercise caution with ambiguous terms like 'Cambridge' (can refer to different institutions) and 'Harvard' (can refer to a person)."}, 'LOCATION': {'Definition': 'LOCATION refers to specific geographic entities such as venues, facilities, and institutions that represent physical places with distinct addresses or functions.', 'Guidelines': "Exercise caution with ambiguous terms, e.g., 'Amazon' (company, river, and region) and 'Cambridge' (U.S. city, UK city, and university). Consider the context and specificity to accurately classify locations."}, 'PERSON': {'Definition': 'PERSON refers to individuals, including public figures, celebrities, and notable personalities.', 'Guidelines': 'If a person is working on research (including professor, Ph.D. student, researcher in companies, and etc) avoid labeling it as PERSON entity.'}, 'CONFERENCE': {'Definition': 'CONFERENCE refers to specific events or gatherings where experts, researchers, and professionals convene to present and discuss their work in a particular field or discipline.', 'Guidelines': "Exercise caution when labeling entities that could refer to institutions, organizations, or associations rather than specific events. Take care with ambiguous terms like 'International Journal of Computer Vision', which may refer to a publication rather than a conference."}}

instruction = slimer_prompter.generate_prompt(
  ne_tags=", ".join(ne_types_list),
  def_and_guidelines=json.dumps(def_guidelines_per_NE_dict, indent=2),
  expected_json_format=json.dumps({k: [] for k in def_guidelines_per_NE_dict.keys()}, indent=2)
)

input_text = 'Typical generative model approaches include naive Bayes classifier s , Gaussian mixture model s , variational autoencoders and others .'

# this promper formats the input text to analize with SLIMER instruction
input_instruction_prompter = Prompter('LLaMA3-chat-NOheaders', template_path='./src/SFT_finetuning/templates')

system_message = "You are a helpful NER assistant designed to output JSON."
conversation = [
    {"role": "system", "content": system_message},
    {"role": "user", "content": input_instruction_prompter.generate_prompt(input=input_text, instruction=instruction)},  # the input_text + instruction
]
prompt = tokenizer.apply_chat_template(conversation, tokenize=False, truncation=True, max_length=cutoff_len, add_generation_prompt=True)

responses = vllm_model.generate(prompt, sampling_params)
```

## Citation

If you find SLIMER useful in your research or work, please cite the following paper:

``` latex
@misc{zamai2024lessinstructmoreenriching,
      title={Show Less, Instruct More: Enriching Prompts with Definitions and Guidelines for Zero-Shot NER}, 
      author={Andrew Zamai and Andrea Zugarini and Leonardo Rigutini and Marco Ernandes and Marco Maggini},
      year={2024},
      eprint={2407.01272},
      archivePrefix={arXiv},
      primaryClass={cs.CL},
      url={https://arxiv.org/abs/2407.01272}, 
}
```