モデル概要
モデル特徴
モデル能力
使用事例
🚀 Mistral-7B-Instruct-v0.3
Mistral-7B-Instruct-v0.3は、大規模言語モデル(LLM)であり、Mistral-7B-v0.3をインストラクション微調整したバージョンです。このモデルは、自然言語処理タスクにおいて高い性能を発揮します。
🚀 クイックスタート
Mistral-7B-Instruct-v0.3を使用するには、まずインストールとモデルのダウンロードが必要です。その後、チャットやインストラクションに従った応答を得ることができます。
✨ 主な機能
- Mistral-7B-v0.3と比較して、以下の変更があります。
- 語彙数が32768に拡張されました。
- v3トークナイザーをサポートしています。
- 関数呼び出しをサポートしています。
📦 インストール
mistralai/Mistral-7B-Instruct-v0.3
を使用するには、mistral-inferenceを推奨します。HF transformersのコードスニペットについては、下に進んでください。
pip install mistral_inference
💻 使用例
基本的な使用法
モデルのダウンロード
from huggingface_hub import snapshot_download
from pathlib import Path
mistral_models_path = Path.home().joinpath('mistral_models', '7B-Instruct-v0.3')
mistral_models_path.mkdir(parents=True, exist_ok=True)
snapshot_download(repo_id="mistralai/Mistral-7B-Instruct-v0.3", allow_patterns=["params.json", "consolidated.safetensors", "tokenizer.model.v3"], local_dir=mistral_models_path)
チャット
mistral_inference
をインストールした後、環境にmistral-chat
CLIコマンドが利用可能になります。以下のコマンドでモデルとチャットできます。
mistral-chat $HOME/mistral_models/7B-Instruct-v0.3 --instruct --max_tokens 256
インストラクションに従った応答
from mistral_inference.transformer import Transformer
from mistral_inference.generate import generate
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
model = Transformer.from_folder(mistral_models_path)
completion_request = ChatCompletionRequest(messages=[UserMessage(content="Explain Machine Learning to me in a nutshell.")])
tokens = tokenizer.encode_chat_completion(completion_request).tokens
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
print(result)
関数呼び出し
from mistral_common.protocol.instruct.tool_calls import Function, Tool
from mistral_inference.transformer import Transformer
from mistral_inference.generate import generate
from mistral_common.tokens.tokenizers.mistral import MistralTokenizer
from mistral_common.protocol.instruct.messages import UserMessage
from mistral_common.protocol.instruct.request import ChatCompletionRequest
tokenizer = MistralTokenizer.from_file(f"{mistral_models_path}/tokenizer.model.v3")
model = Transformer.from_folder(mistral_models_path)
completion_request = ChatCompletionRequest(
tools=[
Tool(
function=Function(
name="get_current_weather",
description="Get the current weather",
parameters={
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. San Francisco, CA",
},
"format": {
"type": "string",
"enum": ["celsius", "fahrenheit"],
"description": "The temperature unit to use. Infer this from the users location.",
},
},
"required": ["location", "format"],
},
)
)
],
messages=[
UserMessage(content="What's the weather like today in Paris?"),
],
)
tokens = tokenizer.encode_chat_completion(completion_request).tokens
out_tokens, _ = generate([tokens], model, max_tokens=64, temperature=0.0, eos_id=tokenizer.instruct_tokenizer.tokenizer.eos_id)
result = tokenizer.instruct_tokenizer.tokenizer.decode(out_tokens[0])
print(result)
高度な使用法
transformers
を使用したテキスト生成
from transformers import pipeline
messages = [
{"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
{"role": "user", "content": "Who are you?"},
]
chatbot = pipeline("text-generation", model="mistralai/Mistral-7B-Instruct-v0.3")
chatbot(messages)
transformers
を使用した関数呼び出し
この例を使用するには、transformers
バージョン4.42.0以上が必要です。詳細については、function calling guideを参照してください。
from transformers import AutoModelForCausalLM, AutoTokenizer
import torch
model_id = "mistralai/Mistral-7B-Instruct-v0.3"
tokenizer = AutoTokenizer.from_pretrained(model_id)
def get_current_weather(location: str, format: str):
"""
Get the current weather
Args:
location: The city and state, e.g. San Francisco, CA
format: The temperature unit to use. Infer this from the users location. (choices: ["celsius", "fahrenheit"])
"""
pass
conversation = [{"role": "user", "content": "What's the weather like in Paris?"}]
tools = [get_current_weather]
# format and tokenize the tool use prompt
inputs = tokenizer.apply_chat_template(
conversation,
tools=tools,
add_generation_prompt=True,
return_dict=True,
return_tensors="pt",
)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
inputs.to(model.device)
outputs = model.generate(**inputs, max_new_tokens=1000)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
注意事項
この例では、ツール呼び出しの完全なサイクルや、ツール呼び出しとツール結果をチャット履歴に追加する部分を省略しています。完全なツール呼び出しの例については、function calling guideを参照してください。また、Mistralはツール呼び出しIDを使用するため、ツール呼び出しとツール結果にはこれを含める必要があります。ツール呼び出しIDは、正確に9文字の英数字である必要があります。
📚 ドキュメント
Mistral 7B Instructモデルは、ベースモデルを簡単に微調整して魅力的な性能を達成できることをすばやく実証するものです。ただし、このモデルにはモデレーションメカニズムがありません。モデルがガードレールをきめ細かく尊重し、モデレーションされた出力を必要とする環境でのデプロイを可能にする方法について、コミュニティと協力したいと考えています。
📄 ライセンス
このプロジェクトはMITライセンスの下でライセンスされています。
ミストラルAIチーム
Albert Jiang, Alexandre Sablayrolles, Alexis Tacnet, Antoine Roux, Arthur Mensch, Audrey Herblin-Stoop, Baptiste Bout, Baudouin de Monicault, Blanche Savary, Bam4d, Caroline Feldman, Devendra Singh Chaplot, Diego de las Casas, Eleonore Arcelin, Emma Bou Hanna, Etienne Metzger, Gianna Lengyel, Guillaume Bour, Guillaume Lample, Harizo Rajaona, Jean-Malo Delignon, Jia Li, Justus Murke, Louis Martin, Louis Ternon, Lucile Saulnier, Lélio Renard Lavaud, Margaret Jennings, Marie Pellat, Marie Torelli, Marie-Anne Lachaux, Nicolas Schuhl, Patrick von Platen, Pierre Stock, Sandeep Subramanian, Sophia Yang, Szymon Antoniak, Teven Le Scao, Thibaut Lavril, Timothée Lacroix, Théophile Gervet, Thomas Wang, Valera Nemychnikova, William El Sayed, William Marshall



