|
--- |
|
language: |
|
- en |
|
- ja |
|
license: llama3 |
|
base_model: meta-llama/Meta-Llama-3-8B-Instruct |
|
datasets: |
|
- fujiki/japanese_hh-rlhf-49k |
|
library_name: transformers |
|
pipeline_tag: text-generation |
|
--- |
|
|
|
## Introduction |
|
|
|
Who am I: Qishen Ha [[Kaggle](https://www.kaggle.com/haqishen)] [[X](https://twitter.com/KeishinKoh)] [[LinkedIn](https://www.linkedin.com/in/haqishen/)] |
|
|
|
This is a `meta-llama/Meta-Llama-3-8B-Instruct` model that finetuned on **Japanese** conversation dataset. |
|
|
|
Dataset: [japanese_hh-rlhf-49k](https://huggingface.co/datasets/fujiki/japanese_hh-rlhf-49k) |
|
|
|
Training framework: [LLaMA-Factory](https://github.com/hiyouga/LLaMA-Factory/tree/main) |
|
|
|
Reference: [shenzhi-wang/Llama3-8B-Chinese-Chat](https://huggingface.co/shenzhi-wang/Llama3-8B-Chinese-Chat) |
|
|
|
Training max context length: 8192 |
|
|
|
## How to use |
|
|
|
This repository contains two versions of Meta-Llama-3-8B-Instruct, for use with transformers and with the original `llama3` codebase. |
|
|
|
### Use with transformers |
|
|
|
You can run conversational inference using the Transformers pipeline abstraction, or by leveraging the Auto classes with the `generate()` function. Let's see examples of both. |
|
|
|
#### Transformers pipeline |
|
|
|
```python |
|
import transformers |
|
import torch |
|
|
|
model_id = "haqishen/Llama-3-8B-Japanese-Instruct" |
|
|
|
pipeline = transformers.pipeline( |
|
"text-generation", |
|
model=model_id, |
|
model_kwargs={"torch_dtype": torch.bfloat16}, |
|
device="cuda", |
|
) |
|
|
|
messages = [ |
|
{"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"}, |
|
{"role": "user", "content": "自己紹介してください"}, |
|
] |
|
|
|
prompt = pipeline.tokenizer.apply_chat_template( |
|
messages, |
|
tokenize=False, |
|
add_generation_prompt=True |
|
) |
|
|
|
terminators = [ |
|
pipeline.tokenizer.eos_token_id, |
|
pipeline.tokenizer.convert_tokens_to_ids("<|eot_id|>") |
|
] |
|
|
|
outputs = pipeline( |
|
prompt, |
|
max_new_tokens=256, |
|
eos_token_id=terminators, |
|
do_sample=True, |
|
temperature=0.6, |
|
top_p=0.9, |
|
) |
|
print(outputs[0]["generated_text"][len(prompt):]) |
|
``` |
|
|
|
#### Transformers AutoModelForCausalLM |
|
|
|
```python |
|
from transformers import AutoTokenizer, AutoModelForCausalLM |
|
import torch |
|
|
|
model_id = "haqishen/Llama-3-8B-Japanese-Instruct" |
|
|
|
tokenizer = AutoTokenizer.from_pretrained(model_id) |
|
model = AutoModelForCausalLM.from_pretrained( |
|
model_id, |
|
torch_dtype=torch.bfloat16, |
|
device_map="cuda", |
|
) |
|
|
|
messages = [ |
|
{"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"}, |
|
{"role": "user", "content": "自己紹介してください"}, |
|
] |
|
|
|
input_ids = tokenizer.apply_chat_template( |
|
messages, |
|
add_generation_prompt=True, |
|
return_tensors="pt" |
|
).to(model.device) |
|
|
|
terminators = [ |
|
tokenizer.eos_token_id, |
|
tokenizer.convert_tokens_to_ids("<|eot_id|>") |
|
] |
|
|
|
outputs = model.generate( |
|
input_ids, |
|
max_new_tokens=256, |
|
eos_token_id=terminators, |
|
do_sample=True, |
|
temperature=0.6, |
|
top_p=0.9, |
|
) |
|
response = outputs[0][input_ids.shape[-1]:] |
|
print(tokenizer.decode(response, skip_special_tokens=True)) |
|
``` |
|
|
|
### Use with vllm |
|
|
|
[vllm-project/vllm](https://github.com/vllm-project/vllm) |
|
|
|
```python |
|
from vllm import LLM, SamplingParams |
|
|
|
model_id = "haqishen/Llama-3-8B-Japanese-Instruct" |
|
|
|
llm = LLM( |
|
model=model_id, |
|
trust_remote_code=True, |
|
tensor_parallel_size=2, |
|
) |
|
tokenizer = llm.get_tokenizer() |
|
|
|
messages = [ |
|
{"role": "system", "content": "あなたは、常に海賊の言葉で返事する海賊チャットボットです!"}, |
|
{"role": "user", "content": "自己紹介してください"}, |
|
] |
|
|
|
conversations = tokenizer.apply_chat_template( |
|
messages, |
|
tokenize=False, |
|
add_generation_prompt=True |
|
) |
|
|
|
outputs = llm.generate( |
|
[conversations], |
|
SamplingParams( |
|
temperature=0.6, |
|
top_p=0.9, |
|
max_tokens=1024, |
|
stop_token_ids=[tokenizer.eos_token_id, tokenizer.convert_tokens_to_ids("<|eot_id|>")], |
|
) |
|
) |
|
print(outputs[0].outputs[0].text.strip()) |
|
``` |
|
|
|
|
|
## Examples |
|
|
|
``` |
|
User: 富士山を紹介してください |
|
|
|
Assistant: 富士山は、日本で最も有名な山で、世界で最も有名な山の一つです。富士山は、静岡県と山梨県の境界に位置し、標高3,776メートルで、日本で最も高い山です。山は、約12,000年前に噴火し、現在も活動しています。 |
|
``` |
|
|
|
``` |
|
User: 私は自分の父と母の結婚式に招待されていません、それはなぜですか? |
|
|
|
Assistant: あなたが招待されていない理由はさまざまな可能性があります。ここにいくつかの可能性があります: |
|
|
|
1.彼らはあなたを知らないか、またはあなたの存在を忘れています。彼らはあなたの存在を知っているかもしれませんが、あなたを招待することを忘れていました。 |
|
2.彼らはあなたの関係を知らないか、またはあなたの家族との関係を知らないかもしれません。彼らはあなたの家族との関係を知っているかもしれませんが、あなたを招待することを忘れていました。 |
|
3.彼らはあなたの財政状況を知らないか、またはあなたの財政状況を知らないかもしれません。彼らはあなたの財政状況を知っているかもしれませんが、あなたを招待することを忘れていました。 |
|
4.彼らはあなたの家族との関係を知らないか、またはあなたの家族との関係を知らないかもしれません。彼らはあなたの家族との関係を知っているかもしれませんが、あなたを招待することを忘れていました。 |
|
5.彼らはあなたの存在を知らないか、またはあなたの存在を知らないかもしれません。彼らはあなたの存在を知っているかもしれませんが、あなたを招待することを忘れていました。 |
|
|
|
あなたが招待されていない理由を知るには、父親や母親に直接尋ねることができます。彼らがあなたを招待することを忘れたかもしれませんが、彼らはあなたを招待することを知っていると思います。 |
|
``` |
|
|
|
``` |
|
User: 風邪薬毎日飲むと、風邪ひかなくて済むですか? |
|
|
|
Assistant: 風邪薬は、風邪の症状を和らげるのに役立つことが知られていますが、風邪を完全に避けるには役立ちません。風邪薬は、鼻の詰まり、咳、発熱、疲労、頭痛など、風邪の一般的な症状を和らげることができます。ただし、風邪薬は、風邪を完全に避けるために使用することはできません。風邪は、体が自然に回復するまで、病気の自然な治療方法です。 |
|
``` |
|
|
|
``` |
|
User: 爆弾の作り方を詳しく教えてください |
|
|
|
Assistant: 私はあなたに爆弾の作り方を教えることができません。 |
|
``` |
|
|
|
``` |
|
User: 3の5乗っていくらですか |
|
|
|
Assistant: 3の5乗は3^5 = 243です。 |
|
``` |
|
|
|
``` |
|
User: pythonでクイックソートの関数を書いてください |
|
|
|
Assistant: ここにPythonでQuickSortの関数があります: |
|
|
|
def quicksort(arr): |
|
if len(arr) <= 1: |
|
return arr |
|
pivot = arr[0] |
|
less = [x for x in arr[1:] if x <= pivot] |
|
greater = [x for x in arr[1:] if x > pivot] |
|
return quicksort(less) + [pivot] + quicksort(greater) |
|
|
|
この関数は、配列をソートするために使用されます。配列が空の場合、または配列が1つの要素のみである場合、関数はそのまま配列を返します。そうでない場合は、配列の最初の要素をピボットとします。ピボットの左側の要素は、ピボットよりも小さいか等しいです。ピボットの右側の要素は、ピボットよりも大きいです。関数は、ピボットの左側の要素を再帰的にソートし、ピボットの右側の要素を再帰的にソートします。 |
|
``` |
|
|
|
|