模型库 / mistralai/Mistral-7B-Instruct-v0.3

Mistral-7B-Instruct-v0.3

mistralai vllm
mistralai/Mistral-7B-Instruct-v0.3
3,914,752
下载量
2570
收藏数
9
浏览量
apache-2.0
许可

简介

Mistral-7B-Instruct-v0.3大型语言模型(LLM)是Mistral-7B-v0.3的指令微调版本。

模型卡片

许可协议 apache-2.0
框架 vllm
vllm mistral-common

模型配置

模型类型 mistral
架构 MistralForCausalLM

模型详情

已翻译

Model Card for Mistral-7B-Instruct-v0.3

Mistral-7B-Instruct-v0.3 大型语言模型(LLM)是 Mistral-7B-v0.3 的指令微调版本。

Mistral-7B-v0.2 相比,Mistral-7B-v0.3 有以下变化:
- 词汇表扩展至 32768
- 支持 v3 Tokenizer
- 支持函数调用(function calling)

安装

建议将 mistralai/Mistral-7B-Instruct-v0.3mistral-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 生成文本

如果您想使用 Hugging Face 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 或更高版本。更多信息请参阅 transformers 文档中的函数调用指南

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))

请注意,出于篇幅考虑,此示例未展示调用工具、将工具调用及工具结果添加到聊天历史记录以供模型在下一轮生成中使用的完整流程。如需完整的工具调用示例,请参阅函数调用指南,并注意 Mistral 确实使用工具调用 ID,因此这些 ID 必须包含在您的工具调用和工具结果中。它们应恰好为 9 个字母数字字符。

局限性

Mistral 7B Instruct 模型是一个快速演示,展示了基础模型可以轻松微调以实现令人瞩目的性能。它没有任何审核机制。我们期待与社区合作,探索如何让模型更好地遵守安全护栏,从而能够在需要审核输出的环境中部署。

Mistral 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

标签

mistral mistral-common base_model:mistralai/Mistral-7B-v0.3 base_model:finetune:mistralai/Mistral-7B-v0.3 license:apache-2.0 region:us

操作


详细信息

厂商
mistralai
框架
vllm
模型类型
mistral
许可(HF)
apache-2.0