🚀 Mistral-7B-Instruct-v0.2 モデルカード
Mistral-7B-Instruct-v0.2 大規模言語モデル (LLM) は、Mistral-7B-v0.2 を命令に基づいてファインチューニングしたバージョンです。このモデルは、特定の命令に対する応答を生成する能力を向上させるために調整されています。
🚀 クイックスタート
このセクションでは、Mistral-7B-Instruct-v0.2 を使用したエンコード、デコード、推論の基本的な手順を説明します。
💻 使用例
基本的な使用法
mistral_common
を使用したエンコードとデコード
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
mistral_models_path = "MISTRAL_MODELS_PATH"
tokenizer = MistralTokenizer.v1()
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
tokens = tokenizer.encode_chat_completion(completion_request).tokens
mistral_inference
を使用した推論
from mistral_inference.transformer import Transformer
from mistral_inference.generate import generate
model = Transformer.from_folder(mistral_models_path)
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
result = tokenizer.decode(out_tokens[0])
print(result)
Hugging Face transformers
を使用した推論
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
model.to("cuda")
generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
result = tokenizer.decode(generated_ids[0].tolist())
print(result)
💡 使用ヒント
transformers
トークナイザを修正して、mistral_common
の参照実装と 1 対 1 で同じ結果を得る PR は大歓迎です!
命令形式
命令のファインチューニングを活用するには、プロンプトを [INST]
と [/INST]
トークンで囲む必要があります。最初の命令は文頭 ID で始める必要があり、次の命令はその必要はありません。アシスタントの生成は文末トークン ID で終了します。
例:
text = "<s>[INST] What is your favourite condiment? [/INST]"
"Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!</s> "
"[INST] Do you have mayonnaise recipes? [/INST]"
この形式は、apply_chat_template()
メソッドを介して チャットテンプレート として利用できます。
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda"
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")
messages = [
{"role": "user", "content": "What is your favourite condiment?"},
{"role": "assistant", "content": "Well, I'm quite partial to a good squeeze of fresh lemon juice. It adds just the right amount of zesty flavour to whatever I'm cooking up in the kitchen!"},
{"role": "user", "content": "Do you have mayonnaise recipes?"}
]
encodeds = tokenizer.apply_chat_template(messages, return_tensors="pt")
model_inputs = encodeds.to(device)
model.to(device)
generated_ids = model.generate(model_inputs, max_new_tokens=1000, do_sample=True)
decoded = tokenizer.batch_decode(generated_ids)
print(decoded[0])
📚 詳細ドキュメント
Mistral-7B-v0.2 の変更点
Mistral-7B-v0.2 は、Mistral-7B-v0.1 と比較して以下の変更があります。
属性 |
詳情 |
コンテキストウィンドウ |
v0.1 の 8k から 32k に拡張 |
Rope-theta |
1e6 |
スライディングウィンドウアテンション |
無し |
このモデルの詳細については、論文 と リリースブログ記事 をご覧ください。
トラブルシューティング
次のエラーが表示された場合:
Traceback (most recent call last):
File "", line 1, in
File "/transformers/models/auto/auto_factory.py", line 482, in from_pretrained
config, kwargs = AutoConfig.from_pretrained(
File "/transformers/models/auto/configuration_auto.py", line 1022, in from_pretrained
config_class = CONFIG_MAPPING[config_dict["model_type"]]
File "/transformers/models/auto/configuration_auto.py", line 723, in getitem
raise KeyError(key)
KeyError: 'mistral'
ソースから transformers
をインストールすると問題が解決するはずです。
pip install git+https://github.com/huggingface/transformers
これは transformers-v4.33.4
以降では必要ないはずです。
制限事項
Mistral 7B Instruct モデルは、ベースモデルを簡単にファインチューニングして魅力的なパフォーマンスを達成できることを迅速に示すデモンストレーションです。このモデルにはモデレーションメカニズムがありません。モデルがガードレールをきめ細かく尊重し、モデレートされた出力が必要な環境でのデプロイを可能にする方法について、コミュニティと協力することを楽しみにしています。
ミストラル AI チーム
Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Blanche Savary, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Emma Bou Hanna, Florian Bressand, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Lélio Renard Lavaud, Louis Ternon, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Théophile Gervet, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed.
📄 ライセンス
このモデルは Apache-2.0 ライセンスの下で提供されています。
⚠️ 重要提示
個人情報の処理方法について詳しく知りたい場合は、プライバシーポリシー をお読みください。