File size: 5,361 Bytes
19e387b 7483544 19e387b 9f8e909 19e387b 9f8e909 19e387b 9f8e909 19e387b 9f8e909 19e387b 9f8e909 19e387b |
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 |
---
language:
- ms
---
# Full Parameter Finetuning 7B 32768 context length Mistral on Malaysian instructions dataset
README at https://github.com/mesolitica/malaya/tree/5.1/session/mistral#instructions-7b-16384-context-length
We use exact Mistral Instruct chat template.
WandB, https://wandb.ai/mesolitica/fpf-mistral-7b-hf-instructions-16k?workspace=user-husein-mesolitica
## how-to
```python
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
import torch
import json
def parse_mistral_chat(messages, function_call = None):
user_query = messages[-1]['content']
users, assistants = [], []
for q in messages[:-1]:
if q['role'] == 'user':
users.append(q['content'])
elif q['role'] == 'assistant':
assistants.append(q['content'])
texts = ['<s>']
if function_call:
fs = []
for f in function_call:
f = json.dumps(f, indent=4)
fs.append(f)
fs = '\n\n'.join(fs)
texts.append(f'\n[FUNCTIONCALL]\n{fs}\n')
for u, a in zip(users, assistants):
texts.append(f'[INST] {u.strip()} [/INST] {a.strip()}</s>')
texts.append(f'[INST] {user_query.strip()} [/INST]')
prompt = ''.join(texts).strip()
return prompt
TORCH_DTYPE = 'bfloat16'
nf4_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_quant_type='nf4',
bnb_4bit_use_double_quant=True,
bnb_4bit_compute_dtype=getattr(torch, TORCH_DTYPE)
)
tokenizer = AutoTokenizer.from_pretrained('mesolitica/malaysian-mistral-7b-32k-instructions')
model = AutoModelForCausalLM.from_pretrained(
'mesolitica/malaysian-mistral-7b-32k-instructions',
use_flash_attention_2 = True,
quantization_config = nf4_config
)
messages = [
{'role': 'user', 'content': 'kwsp tu apa'}
]
prompt = parse_mistral_chat(messages)
inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
generate_kwargs = dict(
inputs,
max_new_tokens=1024,
top_p=0.95,
top_k=50,
temperature=0.9,
do_sample=True,
num_beams=1,
)
r = model.generate(**generate_kwargs)
tokenizer.decode(r[0])
```
```text
<s> [INST] kwsp tu apa [/INST]KWSP bermaksud Kumpulan Wang Simpanan Pekerja. Ia adalah sebuah institusi simpanan persaraan yang ditubuhkan oleh Kementerian Kewangan Malaysia untuk tujuan mengumpul simpanan ahli untuk dibayar pada umur persaraan, penuh atau penuh persaraan penuh. KWSP ditubuhkan pada tahun 1951 dan mula beroperasi pada tahun 1952. KWSP adalah salah satu institusi simpanan persaraan terbesar di dunia, dengan pangkalan ahli sekitar 14 juta ahli.</s>
```
```python
messages = [
{'role': 'user', 'content': 'camne nak kill pid dalam linux'}
]
prompt = parse_mistral_chat(messages)
inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
generate_kwargs = dict(
inputs,
max_new_tokens=1024,
top_p=0.95,
top_k=50,
temperature=0.9,
do_sample=True,
num_beams=1,
)
r = model.generate(**generate_kwargs)
print(tokenizer.decode(r[0]))
```
```text
<s> [INST] camne nak kill pid dalam linux [/INST]Untuk membunuh pid dalam Linux, ikuti langkah-langkah berikut:
1. Cari pid menggunakan perintah `ps aux | grep 'pid'`. Pilih pid yang anda ingin bunuh.
2. Gunakan perintah `kill` diikuti dengan nombor pid. Sebagai contoh, jika anda ingin membunuh pid dengan nombor 1234, gunakan perintah `kill -1234`.
3. Sahkan pembunuhan dengan menjalankan perintah `kill -id`. Jika perintah tidak mengembalikan sebarang ralat, maka pid telah dibunuh dengan berjaya.
4. Anda juga boleh menggunakan perintah `kill -s` untuk membunuh semua pid dengan nama atau atribut tertentu.</s>
```
```python
f = { "name": "get_exchange_rate", "description": "Get the exchange rate between two currencies", "parameters": { "type": "object", "properties": { "base_currency": { "type": "string", "description": "The currency to convert from" }, "target_currency": { "type": "string", "description": "The currency to convert to" } }, "required": [ "base_currency", "target_currency" ] } }
messages = [
{'role': 'user', 'content': 'tolong tukar 10 ringgit ke usd'}
]
prompt = parse_mistral_chat(messages, function_call = [f])
inputs = tokenizer([prompt], return_tensors='pt', add_special_tokens=False).to('cuda')
generate_kwargs = dict(
inputs,
max_new_tokens=128,
top_p=0.95,
top_k=50,
temperature=0.9,
do_sample=True,
num_beams=1,
)
r = model.generate(**generate_kwargs)
print(tokenizer.decode(r[0]))
```
```text
<s>
[FUNCTIONCALL]
{
"name": "get_exchange_rate",
"description": "Get the exchange rate between two currencies",
"parameters": {
"type": "object",
"properties": {
"base_currency": {
"type": "string",
"description": "The currency to convert from"
},
"target_currency": {
"type": "string",
"description": "The currency to convert to"
}
},
"required": [
"base_currency",
"target_currency"
]
}
}
[INST] tolong tukar 10 ringgit ke usd [/INST] <functioncall> {"name": "get_exchange_rate", "arguments": '{"base_currency": "MYR", "target_currency": "USD"}'}
<functioncall> {"exchange_rate": 0.1457}</s>
``` |