|
a |
|
b/LLM-Zero-shot_approach/llm_ZSL.ipynb |
|
|
1 |
{ |
|
|
2 |
"cells": [ |
|
|
3 |
{ |
|
|
4 |
"cell_type": "markdown", |
|
|
5 |
"metadata": {}, |
|
|
6 |
"source": [ |
|
|
7 |
"The idea of this approach is to generate a prompt to ask a Pretrained LLM to label all the tokens with one of the labels we are considering." |
|
|
8 |
] |
|
|
9 |
}, |
|
|
10 |
{ |
|
|
11 |
"cell_type": "code", |
|
|
12 |
"execution_count": 2, |
|
|
13 |
"metadata": {}, |
|
|
14 |
"outputs": [], |
|
|
15 |
"source": [ |
|
|
16 |
"prompt_context = '''I need to perform a named entity recognition task on a text related with inclusion criteria in clinical trials.\n", |
|
|
17 |
"The entities you need to recognize are: Condition, Value, Drug, Procedure, Measurement, Temporal, Observation, Person, Mood, Device and Pregnancy_considerations.\n", |
|
|
18 |
"Particularly you have to produce the ouput in the BIO format. I will show you an example of the expected output.\n", |
|
|
19 |
"Input text: Patients with symptomatic CNS metastases or leptomeningeal involvement \n", |
|
|
20 |
"Output:\n", |
|
|
21 |
"Patients O\n", |
|
|
22 |
"with O\n", |
|
|
23 |
"symptomatic O\n", |
|
|
24 |
"CNS B-Condition\n", |
|
|
25 |
"metastases I-Condition\n", |
|
|
26 |
"or O\n", |
|
|
27 |
"leptomeningeal B-Condition\n", |
|
|
28 |
"involvement I-Condition\n", |
|
|
29 |
"\n", |
|
|
30 |
"You can see that tokens without any entity are labeled as O, and the tokens that are part of an entity are labeled as B-<entity> or I-<entity> depending on if they are the beginning or the inside of the entity.'''\n" |
|
|
31 |
] |
|
|
32 |
}, |
|
|
33 |
{ |
|
|
34 |
"cell_type": "code", |
|
|
35 |
"execution_count": 3, |
|
|
36 |
"metadata": {}, |
|
|
37 |
"outputs": [], |
|
|
38 |
"source": [ |
|
|
39 |
"def gen_prompt(text):\n", |
|
|
40 |
" return f'''{prompt_context}\n", |
|
|
41 |
"Input text: {text}\n", |
|
|
42 |
"Given this input text, produce the output in the format I gave you.\n", |
|
|
43 |
"Output:'''" |
|
|
44 |
] |
|
|
45 |
}, |
|
|
46 |
{ |
|
|
47 |
"cell_type": "code", |
|
|
48 |
"execution_count": 4, |
|
|
49 |
"metadata": {}, |
|
|
50 |
"outputs": [ |
|
|
51 |
{ |
|
|
52 |
"name": "stdout", |
|
|
53 |
"output_type": "stream", |
|
|
54 |
"text": [ |
|
|
55 |
"I need to perform a named entity recognition task on a text related with inclusion criteria in clinical trials.\n", |
|
|
56 |
"The entities you need to recognize are: Condition, Value, Drug, Procedure, Measurement, Temporal, Observation, Person, Mood, Device and Pregnancy_considerations.\n", |
|
|
57 |
"Particularly you have to produce the ouput in the BIO format. I will show you an example of the expected output.\n", |
|
|
58 |
"Input text: Patients with symptomatic CNS metastases or leptomeningeal involvement \n", |
|
|
59 |
"Output:\n", |
|
|
60 |
"Patients O\n", |
|
|
61 |
"with O\n", |
|
|
62 |
"symptomatic O\n", |
|
|
63 |
"CNS B-Condition\n", |
|
|
64 |
"metastases I-Condition\n", |
|
|
65 |
"or O\n", |
|
|
66 |
"leptomeningeal B-Condition\n", |
|
|
67 |
"involvement I-Condition\n", |
|
|
68 |
"\n", |
|
|
69 |
"You can see that tokens without any entity are labeled as O, and the tokens that are part of an entity are labeled as B-<entity> or I-<entity> depending on if they are the beginning or the inside of the entity.\n", |
|
|
70 |
"Input text: Patients with symptomatic CNS metastases or leptomeningeal involvement\n", |
|
|
71 |
"Given this input text, produce the output in the format I gave you.\n", |
|
|
72 |
"Output:\n" |
|
|
73 |
] |
|
|
74 |
} |
|
|
75 |
], |
|
|
76 |
"source": [ |
|
|
77 |
"# Example of a generated prompt\n", |
|
|
78 |
"p = gen_prompt('Patients with symptomatic CNS metastases or leptomeningeal involvement')\n", |
|
|
79 |
"print(p)" |
|
|
80 |
] |
|
|
81 |
}, |
|
|
82 |
{ |
|
|
83 |
"cell_type": "code", |
|
|
84 |
"execution_count": null, |
|
|
85 |
"metadata": {}, |
|
|
86 |
"outputs": [], |
|
|
87 |
"source": [] |
|
|
88 |
} |
|
|
89 |
], |
|
|
90 |
"metadata": { |
|
|
91 |
"kernelspec": { |
|
|
92 |
"display_name": "TER", |
|
|
93 |
"language": "python", |
|
|
94 |
"name": "python3" |
|
|
95 |
}, |
|
|
96 |
"language_info": { |
|
|
97 |
"codemirror_mode": { |
|
|
98 |
"name": "ipython", |
|
|
99 |
"version": 3 |
|
|
100 |
}, |
|
|
101 |
"file_extension": ".py", |
|
|
102 |
"mimetype": "text/x-python", |
|
|
103 |
"name": "python", |
|
|
104 |
"nbconvert_exporter": "python", |
|
|
105 |
"pygments_lexer": "ipython3", |
|
|
106 |
"version": "3.10.13" |
|
|
107 |
} |
|
|
108 |
}, |
|
|
109 |
"nbformat": 4, |
|
|
110 |
"nbformat_minor": 2 |
|
|
111 |
} |