Mistral-7B-Instruct-v0.2
简介
使用 `mistral_common` 进行编码和解码 ```py 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 = "MISTR
模型卡片
模型配置
模型详情
已翻译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)
# decode with mistral tokenizer
result = tokenizer.decode(generated_ids[0].tolist())
print(result)
[!TIP]
欢迎提交 PR 来修正transformerstokenizer,使其与mistral_common参考实现的结果完全一致!
Mistral-7B-Instruct-v0.2 大型语言模型(LLM)是 Mistral-7B-v0.2 的指令微调版本。
与 Mistral-7B-v0.1 相比,Mistral-7B-v0.2 有以下变化:
- 32k 上下文窗口(v0.1 为 8k 上下文)
- Rope-theta = 1e6
- 无滑动窗口注意力(Sliding-Window Attention)
指令格式
为了利用指令微调,你的 prompt 应使用 [INST] 和 [/INST] token 包裹。第一条指令应以句子起始 ID 开头,后续指令则不需要。助手的生成内容将以句子结束 token ID 结尾。
例如:
text = "[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! "
"[INST] Do you have mayonnaise recipes? [/INST]"
该格式可通过 apply_chat_template() 方法作为聊天模板使用:
from transformers import AutoModelForCausalLM, AutoTokenizer
device = "cuda" # the device to load the model onto
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])
故障排除
- 如果你看到以下错误:
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 模型是一个快速演示,表明基础模型可以轻松微调以获得令人信服的性能。
它没有任何审核机制。我们期待与社区合作,探索如何使模型更好地遵守安全护栏,从而在需要受控输出的环境中进行部署。
Mistral 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.
正在翻译中,请稍候...