chat_template๋ง์ ์ฌ์ฉํ์ฌ ํ์ต๋์์ผ๋ฏ๋ก, tokenizer.chat_template์ ์ฌ์ฉํ์ง ์๋ ์์ฑ ๋๋ ํ๊ฐ ์์
์์๋ ์ฑ๋ฅ์ด ํฌ๊ฒ ๋ฎ์์ง ์ ์์ต๋๋ค.from transformers import AutoTokenizer, pipeline
import torch
model_and_tokenizer_path = "nlpai-lab/ko-gemma-7b-v1"
tokenizer = AutoTokenizer.from_pretrained(model_and_tokenizer_path)
pipeline = pipeline(
"text-generation",
model=model_and_tokenizer_path,
tokenizer=tokenizer,
model_kwargs={"torch_dtype": torch.bfloat16},
device="cuda",
)
tokenizer = AutoTokenizer.from_pretrained(model_and_tokenizer_path)
messages = [
{"role": "user",
"content": "์ด์์ ์ฅ๊ตฐ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์."},
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) # Don't use `pipeline.tokenizer`
print(prompt) # <bos><start_of_turn>user\n์ด์์ ์ฅ๊ตฐ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์.<end_of_turn>\n<start_of_turn>model\n
outputs = pipeline(
prompt,
max_new_tokens=4096,
add_special_tokens=True
)
print(outputs[0]["generated_text"][len(prompt):]) # '์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ์๋์ ๋ํ์ ์ธ ๊ตฐ์ฌ ์ง๋์์ด์ ์ ๋ต๊ฐ์
๋๋ค. ๊ทธ๋ ์กฐ์ ์๋์ ์๋์ธ ํ์์์ ํ์ด๋ฌ์ผ๋ฉฐ, ์กฐ์ ์๋ ๊ตฐ๋์์ ๋ค์ํ ์ง๋์๋ก ํ๋ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ๊ฐ์ฅ ์ฃผ๋ชฉํ ๋งํ ์
์ ์ค ํ๋๋ 1592๋
๋ถํฐ 1598๋
๊น์ง ์ผ๋ณธ์ด ์กฐ์ ์ ์นจ๊ณตํ ์ผ๋ณธ ์ ์์์์ ํ๋์
๋๋ค. ์ด ์ ์์์ ์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ๊ตฐ๋์ ์ ๋ต์ ์ธ ์ง๋์๋ก ํ๋ํ๋ฉฐ ์ผ๋ณธ์ ์นจ๋ต์ ์ ํญํ๋ ๋ฐ ํฐ ์ญํ ์ ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์ ํฌ์์์ ์ฉ๊ธฐ์ ์ ์ ์ ์ง์ฑ์ผ๋ก ์ ๋ช
ํ์ต๋๋ค. ๊ทธ๋ ์ ํฌ์์ ์ ์ ์ ์ธ ์ฌ๊ณ ๋ฅผ ๋ฐํํ๊ณ ์ ์ ์ฝ์ ์ ๊ณต๊ฒฉํ๋ ๊ฒ์ผ๋ก ์ ๋ช
ํ์ต๋๋ค. ๋ํ ๊ทธ๋ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ๊ณ ์ทจํ๊ณ ์ ํฌ์์ ์น๋ฆฌํ๋ ๋ฐ ๋์์ด ๋๋ ์ฐ์ค๊ณผ ๊ฒฉ๋ ค์ ๋ง์ ์ ํ๋ ๊ฒ์ผ๋ก๋ ์ ๋ช
ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์ ์์ด ๋๋ ํ์๋ ์กฐ์ ๊ตฐ๋์์ ๊ณ์ ํ๋ํ๋ฉฐ ์กฐ์ ๊ตฐ๋์ ์ง๋์๋ก ํ๋ํ์ต๋๋ค. ๊ทธ๋ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ์ ์งํ๊ณ ์กฐ์ ์ ์๋ณด๋ฅผ ์งํค๋ ๋ฐ ํฐ ์ญํ ์ ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ์๋์ ๋ํ์ ์ธ ๊ตฐ์ฌ ์ง๋์์ด์ ์ ๋ต๊ฐ๋ก ๊ธฐ์ต๋๊ณ ์์ต๋๋ค. ๊ทธ์ ์ฉ๊ธฐ์ ์ ์ ์ ์ง์ฑ, ๊ทธ๋ฆฌ๊ณ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ์ ์งํ๋ ๋ฐ ๊ธฐ์ฌํ ๊ณต๋ก๋ ๊ทธ๋ฅผ ์ ์ค์ ์ธ ์ธ๋ฌผ๋ก ๋ง๋ค์์ต๋๋ค.'
ํ๊ตญ์ด ๊ท ํ ํ๊ฐ ์์ (KoBEST: Korean Balanced Evaluation of Significant Task, M.Jang et. al)์ ๋ํด 5-shot์ผ๋ก ํ๊ฐ๋ ๊ฒฐ๊ณผ์ ๋๋ค.
| Model | WIC Acc | WIC F1 | Sentineg Acc | Sentineg F1 | Hellaswag Acc | Hellaswag F1 | COPA Acc | BoolQ Acc | BoolQ F1 | Avg F1 | Avg F1 (Excl. Sentineg, WIC) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| google/gemma-2b-it | 0.528 | 0.520 | 0.655 | 0.631 | 0.35 | 0.347 | 0.42 | 0.526 | 0.525 | 0.536 | 0.510 |
| google/gemma-7b-it | 0.508 | 0.506 | 0.736 | 0.722 | 0.352 | 0.349 | 0.42 | 0.544 | 0.543 | 0.573 | 0.545 |
| nlpai-lab/ko-gemma-2b-v1 | 0.521 | 0.420 | 0.587 | 0.517 | 0.354 | 0.350 | 0.456 | 0.595 | 0.594 | 0.506 | 0.532 |
| nlpai-lab/ko-gemma-7b-v1 | 0.563 | 0.542 | 0.788 | 0.780 | 0.39 | 0.388 | 0.488 | 0.639 | 0.638 | 0.629 | 0.608 |
License Copyright 2024 DeepMind Technologies Limited
This code is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
chat_template๋ง์ ์ฌ์ฉํ์ฌ ํ์ต๋์์ผ๋ฏ๋ก, tokenizer.chat_template์ ์ฌ์ฉํ์ง ์๋ ์์ฑ ๋๋ ํ๊ฐ ์์
์์๋ ์ฑ๋ฅ์ด ํฌ๊ฒ ๋ฎ์์ง ์ ์์ต๋๋ค.from transformers import AutoTokenizer, pipeline
import torch
model_and_tokenizer_path = "nlpai-lab/ko-gemma-7b-v1"
tokenizer = AutoTokenizer.from_pretrained(model_and_tokenizer_path)
pipeline = pipeline(
"text-generation",
model=model_and_tokenizer_path,
tokenizer=tokenizer,
model_kwargs={"torch_dtype": torch.bfloat16},
device="cuda",
)
tokenizer = AutoTokenizer.from_pretrained(model_and_tokenizer_path)
messages = [
{"role": "user",
"content": "์ด์์ ์ฅ๊ตฐ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์."},
]
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) # Don't use `pipeline.tokenizer`
print(prompt) # <bos><start_of_turn>user\n์ด์์ ์ฅ๊ตฐ์ ๋ํด ์ค๋ช
ํด์ฃผ์ธ์.<end_of_turn>\n<start_of_turn>model\n
outputs = pipeline(
prompt,
max_new_tokens=4096,
add_special_tokens=True
)
print(outputs[0]["generated_text"][len(prompt):]) # '์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ์๋์ ๋ํ์ ์ธ ๊ตฐ์ฌ ์ง๋์์ด์ ์ ๋ต๊ฐ์
๋๋ค. ๊ทธ๋ ์กฐ์ ์๋์ ์๋์ธ ํ์์์ ํ์ด๋ฌ์ผ๋ฉฐ, ์กฐ์ ์๋ ๊ตฐ๋์์ ๋ค์ํ ์ง๋์๋ก ํ๋ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ๊ฐ์ฅ ์ฃผ๋ชฉํ ๋งํ ์
์ ์ค ํ๋๋ 1592๋
๋ถํฐ 1598๋
๊น์ง ์ผ๋ณธ์ด ์กฐ์ ์ ์นจ๊ณตํ ์ผ๋ณธ ์ ์์์์ ํ๋์
๋๋ค. ์ด ์ ์์์ ์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ๊ตฐ๋์ ์ ๋ต์ ์ธ ์ง๋์๋ก ํ๋ํ๋ฉฐ ์ผ๋ณธ์ ์นจ๋ต์ ์ ํญํ๋ ๋ฐ ํฐ ์ญํ ์ ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์ ํฌ์์์ ์ฉ๊ธฐ์ ์ ์ ์ ์ง์ฑ์ผ๋ก ์ ๋ช
ํ์ต๋๋ค. ๊ทธ๋ ์ ํฌ์์ ์ ์ ์ ์ธ ์ฌ๊ณ ๋ฅผ ๋ฐํํ๊ณ ์ ์ ์ฝ์ ์ ๊ณต๊ฒฉํ๋ ๊ฒ์ผ๋ก ์ ๋ช
ํ์ต๋๋ค. ๋ํ ๊ทธ๋ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ๊ณ ์ทจํ๊ณ ์ ํฌ์์ ์น๋ฆฌํ๋ ๋ฐ ๋์์ด ๋๋ ์ฐ์ค๊ณผ ๊ฒฉ๋ ค์ ๋ง์ ์ ํ๋ ๊ฒ์ผ๋ก๋ ์ ๋ช
ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์ ์์ด ๋๋ ํ์๋ ์กฐ์ ๊ตฐ๋์์ ๊ณ์ ํ๋ํ๋ฉฐ ์กฐ์ ๊ตฐ๋์ ์ง๋์๋ก ํ๋ํ์ต๋๋ค. ๊ทธ๋ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ์ ์งํ๊ณ ์กฐ์ ์ ์๋ณด๋ฅผ ์งํค๋ ๋ฐ ํฐ ์ญํ ์ ํ์ต๋๋ค.\n\n์ด์์ ์ฅ๊ตฐ์ ์กฐ์ ์๋์ ๋ํ์ ์ธ ๊ตฐ์ฌ ์ง๋์์ด์ ์ ๋ต๊ฐ๋ก ๊ธฐ์ต๋๊ณ ์์ต๋๋ค. ๊ทธ์ ์ฉ๊ธฐ์ ์ ์ ์ ์ง์ฑ, ๊ทธ๋ฆฌ๊ณ ์กฐ์ ๊ตฐ๋์ ์ ๋ ฅ์ ์ ์งํ๋ ๋ฐ ๊ธฐ์ฌํ ๊ณต๋ก๋ ๊ทธ๋ฅผ ์ ์ค์ ์ธ ์ธ๋ฌผ๋ก ๋ง๋ค์์ต๋๋ค.'
ํ๊ตญ์ด ๊ท ํ ํ๊ฐ ์์ (KoBEST: Korean Balanced Evaluation of Significant Task, M.Jang et. al)์ ๋ํด 5-shot์ผ๋ก ํ๊ฐ๋ ๊ฒฐ๊ณผ์ ๋๋ค.
| Model | WIC Acc | WIC F1 | Sentineg Acc | Sentineg F1 | Hellaswag Acc | Hellaswag F1 | COPA Acc | BoolQ Acc | BoolQ F1 | Avg F1 | Avg F1 (Excl. Sentineg, WIC) |
|---|---|---|---|---|---|---|---|---|---|---|---|
| google/gemma-2b-it | 0.528 | 0.520 | 0.655 | 0.631 | 0.35 | 0.347 | 0.42 | 0.526 | 0.525 | 0.536 | 0.510 |
| google/gemma-7b-it | 0.508 | 0.506 | 0.736 | 0.722 | 0.352 | 0.349 | 0.42 | 0.544 | 0.543 | 0.573 | 0.545 |
| nlpai-lab/ko-gemma-2b-v1 | 0.521 | 0.420 | 0.587 | 0.517 | 0.354 | 0.350 | 0.456 | 0.595 | 0.594 | 0.506 | 0.532 |
| nlpai-lab/ko-gemma-7b-v1 | 0.563 | 0.542 | 0.788 | 0.780 | 0.39 | 0.388 | 0.488 | 0.639 | 0.638 | 0.629 | 0.608 |
License Copyright 2024 DeepMind Technologies Limited
This code is licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an AS IS BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.