huseinzol05 commited on
Commit
b4e7c84
·
1 Parent(s): 8c0b14c

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +199 -0
README.md ADDED
@@ -0,0 +1,199 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - ms
4
+ ---
5
+
6
+ # Full Parameter Finetuning TinyLlama 16384 context length on Malaysian instructions dataset
7
+
8
+ README at https://github.com/mesolitica/malaya/tree/5.1/session/tiny-llama#instructions-7b-16384-context-length
9
+
10
+ We use exact Llama2 Instruct chat template, added with function call
11
+
12
+ WandB, https://wandb.ai/mesolitica/fpf-tinyllama-1.1b-hf-instructions-16k-function-call?workspace=user-husein-mesolitica
13
+
14
+ ## how-to
15
+
16
+ ```python
17
+ from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
18
+ import torch
19
+
20
+ def parse_llama_chat(messages, function_call = None):
21
+
22
+ system = messages[0]['content']
23
+ user_query = messages[-1]['content']
24
+
25
+ users, assistants = [], []
26
+ for q in messages[1:-1]:
27
+ if q['role'] == 'user':
28
+ users.append(q['content'])
29
+ elif q['role'] == 'assistant':
30
+ assistants.append(q['content'])
31
+
32
+ texts = [f'<s>[INST] <<SYS>>\n{system}\n<</SYS>>\n\n']
33
+ if function_call:
34
+ fs = []
35
+ for f in function_call:
36
+ f = json.dumps(f, indent=4)
37
+ fs.append(f)
38
+ fs = '\n\n'.join(fs)
39
+ texts.append(f'\n[FUNCTIONCALL]\n{fs}\n')
40
+ for u, a in zip(users, assistants):
41
+ texts.append(f'{u.strip()} [/INST] {a.strip()} </s><s>[INST] ')
42
+ texts.append(f'{user_query.strip()} [/INST]')
43
+ prompt = ''.join(texts).strip()
44
+ return prompt
45
+
46
+ TORCH_DTYPE = 'bfloat16'
47
+ nf4_config = BitsAndBytesConfig(
48
+ load_in_4bit=True,
49
+ bnb_4bit_quant_type='nf4',
50
+ bnb_4bit_use_double_quant=True,
51
+ bnb_4bit_compute_dtype=getattr(torch, TORCH_DTYPE)
52
+ )
53
+
54
+ tokenizer = AutoTokenizer.from_pretrained('mesolitica/malaysian-tinyllama-1.1b-16k-instructions')
55
+ model = AutoModelForCausalLM.from_pretrained(
56
+ 'mesolitica/malaysian-tinyllama-1.1b-16k-instructions',
57
+ use_flash_attention_2 = True,
58
+ quantization_config = nf4_config
59
+ )
60
+
61
+ messages = [
62
+ {'role': 'system', 'content': 'awak adalah AI yang mampu jawab segala soalan'},
63
+ {'role': 'user', 'content': 'kwsp tu apa'}
64
+ ]
65
+ prompt = parse_llama_chat(messages)
66
+ inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
67
+ generate_kwargs = dict(
68
+ inputs,
69
+ max_new_tokens=1024,
70
+ top_p=0.95,
71
+ top_k=50,
72
+ temperature=0.9,
73
+ do_sample=True,
74
+ num_beams=1,
75
+ )
76
+ r = model.generate(**generate_kwargs)
77
+ print(tokenizer.decode(r[0]))
78
+ ```
79
+
80
+ ```text
81
+ '<s> [INST] <<SYS>>
82
+ awak adalah AI yang mampu jawab segala soalan
83
+ <</SYS>>
84
+
85
+ kwsp tu apa [/INST] KWSP bermaksud Kumpulan Wang Persaraan. </s>'
86
+ ```
87
+
88
+ ```python
89
+ messages = [
90
+ {'role': 'system', 'content': 'awak adalah AI yang mampu jawab segala soalan'},
91
+ {'role': 'user', 'content': 'awat malaysia ada jabatan koko, malaysia bukan buat keluaq koko banyak pun'}
92
+ ]
93
+ prompt = parse_llama_chat(messages)
94
+ inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
95
+ generate_kwargs = dict(
96
+ inputs,
97
+ max_new_tokens=1024,
98
+ top_p=0.95,
99
+ top_k=50,
100
+ temperature=0.9,
101
+ do_sample=True,
102
+ num_beams=1,
103
+ )
104
+ r = model.generate(**generate_kwargs)
105
+ print(tokenizer.decode(r[0]))
106
+ ```
107
+
108
+ ```text
109
+ <s> [INST] <<SYS>>
110
+ awak adalah AI yang mampu jawab segala soalan
111
+ <</SYS>>
112
+
113
+ awat malaysia ada jabatan koko, malaysia bukan buat keluaq koko banyak pun [/INST] Jabatan ini bertanggungjawab untuk mengeluarkan dan mengagihkan produk koko ke pasaran tempatan dan antarabangsa. Mereka juga menyumbang kepada pembangunan industri koko dan memastikan penggunaan sumber asli yang bertanggungjawab. Selain itu, mereka menjalankan penyelidikan dan inovasi untuk meningkatkan proses pengeluaran dan meningkatkan daya saing produk koko. </s>
114
+ ```
115
+
116
+ ```python
117
+ f2 = {
118
+ 'name': 'parse_entities',
119
+ 'description': 'extract entities from the text',
120
+ 'parameters': {
121
+ 'type': 'object',
122
+ 'properties': {
123
+ 'drink': {
124
+ 'type': 'string',
125
+ 'description': 'drink name',
126
+ },
127
+ 'event': {
128
+ 'type': 'string',
129
+ 'description': 'event name',
130
+ },
131
+ 'person_name': {
132
+ 'type': 'string',
133
+ 'description': 'person name',
134
+ }
135
+ },
136
+ 'required': [
137
+ 'drink',
138
+ 'event',
139
+ 'person_name'
140
+ ]
141
+ }
142
+ }
143
+ messages = [
144
+ {'role': 'system', 'content': 'awak adalah AI yang mampu jawab segala soalan'},
145
+ {'role': 'user', 'content': 'nama saya husein bin zolkepli, saya sekarang berada di putrajaya merdeka 2023 sambil minum teh o ais'}
146
+ ]
147
+ prompt = parse_llama_chat(messages, function_call = [f2])
148
+ inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
149
+ generate_kwargs = dict(
150
+ inputs,
151
+ max_new_tokens=128,
152
+ top_p=0.95,
153
+ top_k=50,
154
+ temperature=0.9,
155
+ do_sample=True,
156
+ num_beams=1,
157
+ )
158
+ r = model.generate(**generate_kwargs)
159
+ print(tokenizer.decode(r[0]))
160
+ ```
161
+
162
+ ```text
163
+ <s> [INST] <<SYS>>
164
+ awak adalah AI yang mampu jawab segala soalan
165
+ <</SYS>>
166
+
167
+
168
+ [FUNCTIONCALL]
169
+ {
170
+ "name": "parse_entities",
171
+ "description": "extract entities from the text",
172
+ "parameters": {
173
+ "type": "object",
174
+ "properties": {
175
+ "drink": {
176
+ "type": "string",
177
+ "description": "drink name"
178
+ },
179
+ "event": {
180
+ "type": "string",
181
+ "description": "event name"
182
+ },
183
+ "person_name": {
184
+ "type": "string",
185
+ "description": "person name"
186
+ }
187
+ },
188
+ "required": [
189
+ "drink",
190
+ "event",
191
+ "person_name"
192
+ ]
193
+ }
194
+ }
195
+ nama saya husein bin zolkepli, saya sekarang berada di putrajaya merdeka 2023 sambil minum teh o ais [/INST] <functioncall> {"name": "parse_entities", "arguments": '{"drink": "teh o ais", "event": "Merdeka 2023", "person_name": "Husein bin Zolkepli"}'}
196
+
197
+
198
+ <functioncall> {"entities": [{"name": "Husein bin Zolkepli", "confidence": 0.95}]} </s>
199
+ ```