SynthIA-7B-v1.3 を Colab で試してみる
今回は Mistral-7B をベースにファインチューニングされた `migtissera/SynthIA-7B-v1.3` を使ってみたいと思います。
Huggingface: https://huggingface.co/migtissera/SynthIA-7B-v1.3
Colabで実行
必要なライブラリをインストール
# 必要なライブラリをインストール
!pip install transformers langchain accelerate bitsandbytes -Uqq
モデルの用意
今回は migtissera/SynthIA-7B-v1.3 を BitsandBytes で 4bit に量子化したものを使います。
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
model_id = "migtissera/SynthIA-7B-v1.3"
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_use_double_quant=True,
bnb_4bit_quant_type="nf4",
bnb_4bit_compute_dtype=torch.bfloat16,
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
quantization_config=quantization_config,
).eval()
tokenizer.vocab_size
モデルカードのサンプルコードをもとに、今回使いやすいように書き換えてみました。
def generate_text(prompt: str, max_length: int = 512) -> str:
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
generation_config = {
"do_sample": True,
"top_p": 1.0,
"temperature": 0.75,
"max_length": max_length,
"top_k": 50,
}
outputs = model.generate(
**inputs,
**generation_config,
use_cache=True,
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
質問してみる
英語のモデルなので、まずは英語で質問してみます。
user_input = f"Is the universe expanding? How is that even possible?" # 質問
system_prompt = f"SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation."
llm_prompt = f"{system_prompt} \nUSER: {user_input} \nASSISTANT: "
answer = generate_text(llm_prompt)
answer.replace(llm_prompt, '')
日本語用のトレーニングはされていませんが、日本語の質問を試してみます。
user_input = f"バイオエンジニアリングの勉強をしたいです。どのような順番で勉強していけばいいですか?"
system_prompt = f"SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation. Answer in Japanese."
llm_prompt = f"{system_prompt} \nUSER: {user_input} \nASSISTANT: "
answer = generate_text(llm_prompt)
answer.split("\nASSISTANT: ")[-1]
user_input = f"最近朝起きたときからちょっとした体の疲れがあります。どうすればよいでしょう・・・"
system_prompt = f"SYSTEM: Elaborate on the topic using a Tree of Thoughts and backtrack when necessary to construct a clear, cohesive Chain of Thought reasoning. Always answer without hesitation. Answer in Japanese."
llm_prompt = f"{system_prompt} \nUSER: {user_input} \nASSISTANT: "
answer = generate_text(llm_prompt)
answer.split("\nASSISTANT: ")[-1]
日本語で返せと指示しているものの英語で返ってきました。折角なので、翻訳に使えるか試してみましょう。
user_input = f"First, I will consider the reasons why I feel tired in the mornings. Possible causes could be insufficient sleep, improper diet, or stress. If I have insufficient sleep, it would be helpful to make sure I get enough sleep by going to bed earlier and waking up later. If it is due to poor diet, I should consider adding more nutritious foods to my diet, such as fruits and vegetables. If it is due to stress, I could try to manage my stress levels by practicing relaxation techniques or seeking professional help.\n\nNext, I will consider whether there are any underlying health issues that may be causing the fatigue. If I have been feeling tired for an extended period of time, it may be worth visiting a doctor to rule out any potential health problems.\n\nLastly, I will consider whether my lifestyle habits may be contributing to the fatigue. If my lifestyle is not conducive to a healthy lifestyle, I should try to make changes to my daily routine to incorporate more physical activity and reduce sedentary behavior. \n\nTo summarize, I should make sure I get enough sleep, maintain a healthy diet, manage my stress levels, rule out any underlying health issues, and make lifestyle changes to reduce fatigue."
system_prompt = f"SYSTEM: Translate the USER input to Japanese."
llm_prompt = f"{system_prompt} \nUSER: {user_input} \nASSISTANT: "
answer = generate_text(llm_prompt, max_length=1024)
answer.replace(llm_prompt, '')
意味は伝わりますが、正しい日本語ではないものが返ってきました。
まとめ
いいモデルですがやはり日本語トレーニングされていないモデルだと日本語は難しいですね。
以上、お読みいただきありがとうございます。少しでも参考になればと思います。
もし似たようなコンテンツに興味があれば、フォローしていただけると嬉しいです:
今回使った Colabはこちらです:
参考
関連
この記事が気に入ったらサポートをしてみませんか?