探索 Mistral 新发布的具有原生函数调用功能的 7B 模型【附notebook文件】

引言

Mistral 发布了新版的 7B 模型,这次更新引入了原生函数调用功能。对于开发者和 AI 爱好者来说,这一更新极具吸引力,因为它增强了模型的功能和实用性。在这篇博客中,我们将深入探讨这些新功能,展示如何使用该模型,并通过详细的代码示例来说明其实用性。

Mistral 7B 模型的新特性

与之前的版本不同,最新的 Mistral 7B 模型包含了几个显著的改进:

  1. 新基础模型:这不仅仅是一个微调版本,而是包含了一个全新的基础模型。
  2. 增强的分词器:分词器新增了 768 个额外的 tokens,支持新的功能并可能支持其他语言。
  3. 函数调用支持:现在集成了原生的函数调用支持,使应用更加动态和互动。

环境设置

要开始使用新版 Mistral 7B 模型,我们需要先设置好开发环境。以下是必要的步骤和代码示例:

!pip install git+https://github.com/huggingface/transformers # 从 GitHub 安装 transformers
!pip install datasets loralib sentencepiece
!pip install bitsandbytes accelerate xformers einops
!pip install hf_transfer

加载和测试模型

我们将展示如何加载模型并进行基本的测试。

import os
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForCausalLM

os.environ['HF_HUB_ENABLE_HF_TRANSFER'] = '1'
torch.set_default_device('cuda')

# 使用 Hugging Face 的 pipeline 加载模型
pipe = pipeline(
    "text-generation",
    model="mistralai/Mistral-7B-Instruct-v0.3",
    model_kwargs={"torch_dtype": torch.bfloat16},
    device="cuda",
)

# 加载旧版本的分词器以进行对比
old_tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.2")

# 对比新旧分词器的 token 化结果
print(pipe.tokenizer("How are you? I'm fine. And you?")['input_ids'])
print(old_tokenizer("How are you? I'm fine. And you?")['input_ids'])

新功能展示

新版模型的一个重要功能是支持原生函数调用。以下是一个简单的例子,展示了如何使用这一功能:

from mistral_common.protocol.instruct.tool_calls import Function, Tool
from mistral_inference.model 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("path/to/tokenizer.model.v3")
model = Transformer.from_folder("path/to/model_folder")

# 定义函数调用请求
completion_request = ChatCompletionRequest(
    tools=[
        Tool(
            function=Function(
                name="get_current_weather",
                description="获取当前天气",
                parameters={
                    "type": "object",
                    "properties": {
                        "location": {"type": "string", "description": "城市和州,例如:San Francisco, CA"},
                        "format": {"type": "string", "enum": ["celsius", "fahrenheit"], "description": "使用的温度单位"},
                    },
                    "required": ["location", "format"],
                },
            )
        )
    ],
    messages=[UserMessage(content="今天巴黎的天气怎么样?")],
)

# 生成并输出结果
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)

结论与展望

新版的 Mistral 7B 模型在功能和性能上都有显著提升,特别是其原生函数调用功能,使其在开发动态和互动应用方面具有巨大潜力。我们期待看到更多开发者在此基础上进行有趣的微调和应用开发。如果你有任何问题或建议,欢迎在评论区留言。

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值