Views
No views yet
pip3 install transformers==4.47.0
pip3 install -U mtkresearch1from transformers import AutoModel, AutoTokenizer
2from transformers import GenerationConfig
3import torch
4from mtkresearch.llm.prompt import MRPromptV3
5
6model_id = 'Qwe1325/Llama-Breeze2-3B-Instruct_4bit'
7model = AutoModel.from_pretrained(
8 model_id,
9 torch_dtype=torch.bfloat16,
10 load_in_4bit=True,
11 low_cpu_mem_usage=True,
12 trust_remote_code=True,
13 device_map='auto',
14 img_context_token_id=128212
15).eval()
16
17tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True, use_fast=False)
18
19generation_config = GenerationConfig(
20 max_new_tokens=2048,
21 do_sample=True,
22 temperature=0.01,
23 top_p=0.01,
24 repetition_penalty=1.1,
25 eos_token_id=128009
26)
27
28prompt_engine = MRPromptV3()
29
30sys_prompt = 'You are a helpful AI assistant built by MediaTek Research. The user you are helping speaks Traditional Chinese and comes from Taiwan.'
31
32def _inference(tokenizer, model, generation_config, prompt, pixel_values=None):
33 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
34 if pixel_values is None:
35 output_tensors = model.generate(**inputs, generation_config=generation_config)
36 else:
37 output_tensors = model.generate(**inputs, generation_config=generation_config, pixel_values=pixel_values.to(model.dtype).to(model.device))
38 output_str = tokenizer.decode(output_tensors[0])
39 return output_str1conversations = [
2 {"role": "system", "content": sys_prompt},
3 {"role": "user", "content": "請問什麼是深度學習?"},
4]
5
6prompt = prompt_engine.get_prompt(conversations)
7output_str = _inference(tokenizer, model, generation_config, prompt)
8result = prompt_engine.parse_generated_str(output_str)
9print(result)
10# {'role': 'assistant', 'content': '深度學習是一種人工智慧技術,主要是透過類似於大腦神經網路的方式來處理和分析資料。這個方法利用多層的人工神經元模仿生物神經網路的運作模式,讓電腦能夠從大量數據中學習並做出預測或決策。\n\n簡單來說,深度學習就是一種用機器學習的方式來訓練電腦,使其能夠像人類一樣理解、分辨及解決問題。這項技術已被廣泛應用在各種領域,如圖像識別、自然語言處理、語音辨識以及自動駕駛等方面。'}
1conversations = [
2 {"role": "system", "content": sys_prompt},
3 {"role": "user", "content": [
4 {
5 "type": "image",
6 "image_path": /path/to/example-image,
7 },
8 {
9 "type": "text",
10 "text": "請問第二名可獲得多少獎金?"
11 },
12 ]},
13]
14
15prompt, pixel_values = prompt_engine.get_prompt(conversations)
16output_str = _inference(tokenizer, model, generation_config, prompt, pixel_values=pixel_values)
17result = prompt_engine.parse_generated_str(output_str)
18print(result)
19# {'role': 'assistant', 'content': '第二名可獲得20萬元整。'}1import json
2
3functions = [
4 {
5 "name": "get_current_weather",
6 "description": "Get the current weather in a given location",
7 "parameters": {
8 "type": "object",
9 "properties": {
10 "location": {
11 "type": "string",
12 "description": "The city and state, e.g. San Francisco, CA"
13 },
14 "unit": {
15 "type": "string",
16 "enum": ["celsius", "fahrenheit"]
17 }
18 },
19 "required": ["location"]
20 }
21 }
22]
23
24def fake_get_current_weather(location, unit=None):
25 return {'temperature': 30}
26
27mapping = {
28 'get_current_weather': fake_get_current_weather
29}
30
31# stage 1: query
32conversations = [
33 {"role": "user", "content": "請問台北目前溫度是攝氏幾度?"},
34]
35
36prompt = prompt_engine.get_prompt(conversations, functions=functions)
37
38output_str = _inference(tokenizer, model, generation_config, prompt)
39result = prompt_engine.parse_generated_str(output_str)
40
41print(result)
42# {'role': 'assistant', 'tool_calls': [{'id': 'call_iuwELWUShiAKE16CVoumawZ4', 'type': 'function', 'function': {'name': 'get_current_weather', 'arguments': '{"location": "台北", "unit": "celsius"}'}}]}1# stage 2: execute called functions
2conversations.append(result)
3
4tool_call = result['tool_calls'][0]
5func_name = tool_call['function']['name']
6func = mapping[func_name]
7arguments = json.loads(tool_call['function']['arguments'])
8called_result = func(**arguments)
9
10# stage 3: put executed results
11conversations.append(
12 {
13 'role': 'tool',
14 'tool_call_id': tool_call['id'],
15 'name': func_name,
16 'content': json.dumps(called_result)
17 }
18)
19
20prompt = prompt_engine.get_prompt(conversations, functions=functions)
21
22output_str2 = _inference(tokenizer, model, generation_config, prompt)
23result2 = prompt_engine.parse_generated_str(output_str2)
24print(result2)
25# {'role': 'assistant', 'content': '台北目前的溫度是攝氏30度。'}@article{breeze2,
title={The Breeze 2 Herd of Models: Traditional Chinese LLMs Based on LLaMA with Vision-Aware and Function-Calling Capabilities},
author={Breeze Team, MediaTek Research},
journal={arXiv},
year={2025},
url={https://arxiv.org/abs/2501.13921}
}