🚀 Mistral-7B-Instruct-v0.1模型卡
Mistral-7B-Instruct-v0.1大語言模型(LLM)是Mistral-7B-v0.1生成式文本模型的指令微調版本,它使用了各種公開可用的對話數據集進行微調。
🚀 快速開始
🔧 模型信息
屬性 |
詳情 |
基礎模型 |
mistralai/Mistral-7B-v0.1 |
管道標籤 |
文本生成 |
推理 |
支持 |
許可證 |
apache-2.0 |
⚠️ 重要提示
如果您想了解更多關於我們如何處理您的個人數據的信息,請閱讀我們的隱私政策。
💻 使用示例
基礎用法
使用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.1")
model.to("cuda")
generated_ids = model.generate(tokens, max_new_tokens=1000, do_sample=True)
result = tokenizer.decode(generated_ids[0].tolist())
print(result)
高級用法
指令格式及聊天模板使用
在利用指令微調時,您的提示應該用[INST]
和[/INST]
標記包圍。第一條指令應從句子起始標記開始,後續指令則不需要。助手的生成結果將以句子結束標記結束。
這種格式可以通過apply_chat_template()
方法作為聊天模板使用:
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda"
model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1")
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.1,這是一個具有以下架構選擇的Transformer模型:
- 分組查詢注意力(Grouped-Query Attention)
- 滑動窗口注意力(Sliding-Window Attention)
- 字節回退BPE分詞器(Byte-fallback BPE tokenizer)
故障排除
如果您遇到以下錯誤:
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
之後,應該不需要這樣做。
📄 許可證
本模型使用apache-2.0許可證。
💡 使用建議
歡迎提交PR來修正transformers
分詞器,使其結果與mistral_common
參考實現完全一致!
⚠️ 侷限性
Mistral 7B指令模型是一個快速演示,表明基礎模型可以很容易地進行微調以實現引人注目的性能。它沒有任何審核機制。我們期待與社區合作,探索使模型更好地遵守規則的方法,以便在需要審核輸出的環境中進行部署。
👥 Mistral AI團隊
Albert Jiang, Alexandre Sablayrolles, Arthur Mensch, Chris Bamford, Devendra Singh Chaplot, Diego de las Casas, Florian Bressand, Gianna Lengyel, Guillaume Lample, Lélio Renard Lavaud, Lucile Saulnier, Marie-Anne Lachaux, Pierre Stock, Teven Le Scao, Thibaut Lavril, Thomas Wang, Timothée Lacroix, William El Sayed.