Update README.md
Browse files
README.md
CHANGED
@@ -26,7 +26,9 @@ pip3 install -U mtkresearch
|
|
26 |
|
27 |
```python
|
28 |
from transformers import AutoModel, AutoTokenizer
|
|
|
29 |
import torch
|
|
|
30 |
|
31 |
model_id = 'MediaTek-Research/Breeze2-8B-Instruct-v0_1'
|
32 |
model = AutoModel.from_pretrained(
|
@@ -35,11 +37,37 @@ model = AutoModel.from_pretrained(
|
|
35 |
low_cpu_mem_usage=True,
|
36 |
trust_remote_code=True,
|
37 |
device_map='auto').eval()
|
|
|
38 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, use_fast=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
39 |
```
|
40 |
|
41 |
## Feature: Instruction Following
|
42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
43 |
## Feature: Visual Instruction Following
|
44 |
|
45 |
## Feature: Function Calling
|
|
|
26 |
|
27 |
```python
|
28 |
from transformers import AutoModel, AutoTokenizer
|
29 |
+
from transformers import GenerationConfig
|
30 |
import torch
|
31 |
+
from mtkresearch.llm.prompt import MRPromptV3
|
32 |
|
33 |
model_id = 'MediaTek-Research/Breeze2-8B-Instruct-v0_1'
|
34 |
model = AutoModel.from_pretrained(
|
|
|
37 |
low_cpu_mem_usage=True,
|
38 |
trust_remote_code=True,
|
39 |
device_map='auto').eval()
|
40 |
+
|
41 |
tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, use_fast=False)
|
42 |
+
|
43 |
+
generation_config = GenerationConfig(max_new_tokens=2048, do_sample=False, repetition_penalty=1.1)
|
44 |
+
|
45 |
+
prompt_engine = MRPromptV3()
|
46 |
+
|
47 |
+
sys_prompt = 'You are a helpful AI assistant built by MediaTek Research. The user you are helping speaks Traditional Chinese and comes from Taiwan.'
|
48 |
+
|
49 |
+
def _inference(prompt, tokenizer, model, generation_config):
|
50 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
51 |
+
output_tensors = model.generate(**inputs, generation_config=generation_config)
|
52 |
+
output_str = tokenizer.decode(output_tensors[0])
|
53 |
+
return output_str
|
54 |
```
|
55 |
|
56 |
## Feature: Instruction Following
|
57 |
|
58 |
+
```python
|
59 |
+
conversations = [
|
60 |
+
{"role": "system", "content": sys_prompt},
|
61 |
+
{"role": "user", "content": "請問什麼是深度學習?"},
|
62 |
+
]
|
63 |
+
|
64 |
+
prompt = prompt_engine.get_prompt(conversations)
|
65 |
+
output_str = _inference(prompt, tokenizer, model, generation_config)
|
66 |
+
result = prompt_engine.parse_generated_str(output_str)
|
67 |
+
print(result)
|
68 |
+
# {'role': 'assistant', 'content': '深度學習是一種人工智慧技術,主要是透過模仿生物神經網路的結構和功能來實現。它利用大量數據進行訓練,以建立複雜的模型並使其能夠自主學習、預測或分類輸入資料。\n\n在深度學習中,通常使用多層的神經網路,每一層都包含許多相互連接的節點(稱為神經元)。這些神經元可以處理不同特徵的輸入資料,並將結果傳遞給下一層的神經元。隨著資料流向更高層次,這個過程逐漸捕捉到更抽象的概念或模式。\n\n深度學習已被廣泛應用於各種領域,如圖像識別、自然語言處理、語音識別以及遊戲等。它提供了比傳統機器學習方法更好的表現,因為它能夠從複雜且非線性的數據中提取出有用的資訊。'}
|
69 |
+
```
|
70 |
+
|
71 |
## Feature: Visual Instruction Following
|
72 |
|
73 |
## Feature: Function Calling
|