🚀 Mistral-7B-Instruct-v0.2模型卡片
Mistral-7B-Instruct-v0.2大语言模型(LLM)是Mistral-7B-v0.2的指令微调版本,能有效处理文本生成任务。
🚀 快速开始
✨ 主要特性
- Mistral-7B-v0.2与Mistral-7B-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_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)
🔧 技术细节
指令格式
为了利用指令微调,你的提示应使用[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])
📄 许可证
本模型采用Apache-2.0许可证。
注意事项
⚠️ 重要提示
若你想了解我们如何处理你的个人数据,请阅读我们的隐私政策。
💡 使用建议
非常欢迎提交PR来修正transformers
分词器,使其结果与mistral_common
参考实现完全一致!
局限性
Mistral 7B Instruct模型是一个快速演示,表明基础模型可以轻松进行微调以实现出色的性能。该模型没有任何审核机制。我们期待与社区合作,使模型能够更好地遵循规则,以便在需要审核输出的环境中部署。
开发团队
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.