win10部署 Mistral-7B 文本生成模型

人工智能创业公司MistralAI开源了Mistral7B语言模型,虽只有73亿参数,但在多项基准测试中表现出色,尤其在编程代码和效率上优于大模型。文章介绍了模型的特性、部署流程和GPU加速应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Mistral 7B

date : 2023年10月16日

人工智能创业公司Mistral AI以Apache 2.0授权开源Mistral 7B语言模型,Mistral 7B的特别之处在于其规模较小仅有73亿,但是在所有基准测试上,其表现都优于规模更大的语言模型Llama 2 13B,还具有编写程序代码以及处理8,000个token的能力。

整体来说,Mistral 7B在多个基准测试,包括常识推理、世界知识、阅读理解、数学和程序代码等,表现亮眼,除了明显优于Llama 2 13B之外,也和Llama 34B模型打成平手,其编写程序代码的能力接近CodeLlama 7B,并且在英文任务中表现良好。

在大规模多任务语言理解(MMLU)基准测试上,Mistral 7B的表现相当一个3倍大的Llama 2模型,但是却可大幅节省内存消耗,吞吐量也有所增加,其提供了更高的性价比。

Mistral 7B运用了群组查询注意力(GQA)加快推理速度,还使用滑动窗口注意力(SWA),以更小的成本处理较长的串行。群组查询注意力方法分组多个查询并且同时进行处理,通过这种方式,群组查询注意力机制能够减少重复计算,提高推理速度并降低运算成本。

关于 mistral ai开源的模型

基础模型        mistralai/Mistral-7B-v0.1
会话模型        mistralai/Mistral-7B-Instruct-v0.1

4-bit量化模型  Mistral-7B-OpenOrca-GGUF  

使用了 Llama.cpp 技术既可以 cpu运行 又可以 GPU运行

不支持中文语言

部署流程

Mistral-7B-OpenOrca-GGUFicon-default.png?t=N7T8https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-GGUF/tree/main

从上方网址下载合适大小的模型   放到 Mistral-7B-OpenOrca-GGUF/model/mistral-7b-openorca.Q5_K_M.gguf

在一个torch环境中安装  加速库

pip install ctransformers[cuda]

CPU版 运行代码 run.py

  路径:Mistral-7B-OpenOrca-GGUF/run.py

from ctransformers import AutoModelForCausalLM

# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
# 根据你系统的 GPU 能力,设置 gpu_layers 参数来决定将模型的多少层放置在 GPU 上进行加速运算。如果你的系统无法进行 GPU 加速运算,则将 gpu_layers 参数设置为 0,这样模型将完全在 CPU 上运行。
llm = AutoModelForCausalLM.from_pretrained("./model/mistral-7b-openorca.Q5_K_M.gguf", model_file="./model/mistral-7b-openorca.Q5_K_M.gguf", model_type="mistral", gpu_layers=0)

print(llm("AI is going to"))

GPU 加速运行代码  run.py

路径:Mistral-7B-OpenOrca-GGUF/run.py

from ctransformers import AutoModelForCausalLM

# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
llm = AutoModelForCausalLM.from_pretrained("./model/mistral-7b-openorca.Q5_K_M.gguf", model_file="./model/mistral-7b-openorca.Q5_K_M.gguf", model_type="mistral", gpu_layers=50)

print(llm("AI is going to"))

生成效果

有一种黑色幽默的回答风格

用户:AI将

------------------------------------------------------------------------------

AI:成为我们生活的一部分,不管我们喜欢与否。随着我们进入一个机器变得更加智能和自主的时代,我们的工作、生活和与他人互动的方式将发生重大变化。

在这篇文章中,我们将讨论人工智能如何改变我们的日常生活,以及可以采取哪些措施来确保技术的安全和负责任地集成。

人工智能及其对各个行业的影响:

1.医疗保健:人工智能已经开始彻底改变医疗保健,帮助医生更准确地诊断疾病,改善患者的预后,甚至帮助药物发现。机器学习算法可以分析大量的医学数据,以识别人类可能看不见的模式。这种能力有助于改善治疗决策、个性化用药和加强患者护理。

2.金融:人工智能正在整个金融部门用于检测欺诈、管理风险、优化投资组合配置,甚至自动化财富管理服务。人工智能聊天机器人也越来越受欢迎,因为它为客户提供即时查询答案,同时协助决策过程。

3.教育:人工智能有潜力通过根据学生的个人需求定制学习体验来改变教育。自适应学习平台可以调整

扩展 GUI版代码

from ctransformers import AutoModelForCausalLM

# Set gpu_layers to the number of layers to offload to GPU. Set to 0 if no GPU acceleration is available on your system.
llm = AutoModelForCausalLM.from_pretrained("./model/mistral-7b-openorca.Q5_K_M.gguf", model_file="./model/mistral-7b-openorca.Q5_K_M.gguf", model_type="mistral", gpu_layers=0)

#print(llm("AI is going to"))


import tkinter as tk
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers.generation.utils import GenerationConfig
import os


# 获取当前文件所在的目录路径
current_dir = os.path.dirname(os.path.abspath(__file__))
# 将当前目录和'model'连接起来,获得'model'文件夹的完整路径
save_path = os.path.join(current_dir, 'creat_history.txt')



def write_file(text):
    with open(save_path, 'a', encoding='utf-8') as f:
        f.write(text + '\n')
        
history=[]
def answer(question):
    global history
    history=[]
    response = llm(question)
    print(f"Response from model: {response}")  # Add this line for debugging
    return response


# 创建主窗口
root = tk.Tk()
root.title("AI")
root.geometry("800x600+{}+{}".format(root.winfo_screenwidth() // 2 - 400, root.winfo_screenheight() // 2 - 350))

# 创建输入框和滚动条
input_frame = tk.Frame(root)

clear_button = tk.Button(root, text="清空", width=6, height=3)


def clear_output():
    global history
    output_text.delete("1.0", tk.END)
    history=[]



clear_button.config(command=clear_output)
clear_button.pack(side="right", padx=60)

# 创建输出框和滚动条
output_frame = tk.Frame(root)
output_label = tk.Label(output_frame, text="AI:")
output_text = tk.Text(output_frame, height=26, width=87, padx=6, pady=6)
output_scrollbar = tk.Scrollbar(output_frame)
output_text.config(yscrollcommand=output_scrollbar.set)
output_scrollbar.config(command=output_text.yview)
output_label.pack(side="left", pady=10)
output_text.pack(side="left", pady=10)
output_scrollbar.pack(side="right", fill="y")
output_frame.pack()

output_text.tag_config("user", foreground='green')
output_text.tag_config("ai", background='orange', foreground='white')


input_label = tk.Label(input_frame, text="用户:")
input_text = tk.Text(input_frame, height=10, width=67, padx=6, pady=6)
input_scrollbar = tk.Scrollbar(input_frame)
input_text.config(yscrollcommand=input_scrollbar.set)
input_scrollbar.config(command=input_text.yview)
input_label.pack(side="left")
input_text.pack(side="left", pady=10)
input_scrollbar.pack(side="right", fill="y")
input_frame.pack()


def show_text():
    # 清空 Tkinter Text 组件中的文本
    # output_text.delete('1.0', 'end')
    question = input_text.get("1.0", "end-1c")
    result = answer(question)
    output_text.insert("end", "用户:" + question, "user")
    output_text.insert("end", "\n" + '-'*78)
    output_text.insert("end", "\nAI:" + result)
    output_text.insert("end", '\n'+"="*78)
    output_text.insert("end", "\n\n\n")
    # 滚动到最底部
    output_text.yview_moveto(1.0)
    input_text.delete('1.0', 'end')


# 创建按钮
button_frame = tk.Frame(root)
button = tk.Button(button_frame, text="回答", command=show_text, width=6, height=3)
# 响应回车键 绑定 <Return> 事件
root.bind("<Return>", lambda event: show_text())


def copy():
    global text
    text.event_generate("<<Copy>>")


def cut():
    global text
    text.event_generate("<<Cut>>")


def paste():
    global text
    text.event_generate("<<Paste>>")


# 创建右键菜单
menu = tk.Menu(root, tearoff=0)
menu.add_command(label="复制", command=copy)
menu.add_command(label="剪切", command=cut)
menu.add_command(label="粘贴", command=paste)


# 绑定鼠标右键(第一个文本框)
def show_menu1(event):
    global text
    text = input_text
    menu.post(event.x_root, event.y_root)


input_text.bind("<Button-3>", show_menu1)


# 绑定鼠标右键(第二个文本框)
def show_menu2(event):
    global text
    text = output_text
    menu.post(event.x_root, event.y_root)


output_text.bind("<Button-3>", show_menu2)

button.pack(side="right", padx=60)
button_frame.pack()

root.lift()
# 运行主循环
root.mainloop()

<think>好的,我现在需要帮助用户了解如何在本地部署Mistral模型。首先,我需要收集所有相关的信息,特别是用户提供的引用内容,然后整合这些信息给出一个清晰的步骤指南。 根据引用[2],用户提到使用ollama可以一键安装各大开源模型,包括Mistral,这样本地部署就变得简单了,不需要复杂的代码。引用[3]则提到在Windows上部署OpenManus并接入Mistral模型,响应速度不错。而引用[1]虽然主要讲的是在Amazon SageMaker上部署,但可能对本地部署帮助不大,不过代码示例可能有参考价值。 首先,我需要确认本地部署的几种方法。用户提供的引用中提到了ollama和OpenManus,可能还有其他方法,比如直接使用Hugging Face的Transformers库。但根据引用[2]和[3],ollama和OpenManus是比较简便的方法,特别是ollama的一键安装,适合不想处理复杂代码的用户。 接下来,我应该分步骤说明这两种方法。对于ollama,步骤可能包括安装ollama,然后通过命令行拉取Mistral模型,最后运行模型。对于OpenManus,可能涉及下载软件、配置模型路径等。另外,Hugging Face的方法虽然需要更多代码,但也是一个可行选项,适合开发者。 需要确保每个步骤的命令正确,比如ollama的安装命令是否正确,拉取模型的命令是否是ollama run mistral。同时,要提醒用户注意系统要求,比如内存和存储空间,确保本地环境满足需求。 还要考虑用户可能遇到的问题,比如网络问题导致模型下载失败,或者系统兼容性问题。可能需要给出一些常见问题的解决办法,或者建议查阅相关文档。 最后,需要生成相关问题,这些问题应该围绕本地部署的其他方法、优化、资源需求等方面展开,帮助用户进一步了解相关内容。 现在需要整理这些思路,用清晰的中文分步骤说明,确保符合格式要求,比如行内公式用$...$,独立公式用$$...$$,但在这个问题中可能不太需要数学公式。同时,引用要正确标注,比如ollama的方法来自引用[2],OpenManus来自引用[3]。 总结,回答的结构应该是先介绍几种本地部署方法,每种方法详细步骤,注意事项,然后相关问题部分。确保语言简洁,步骤明确,用户容易跟随操作。</think>以下是在本地部署Mistral模型的详细方法,包含两种主流方案: ### 方法一:通过Ollama一键部署(推荐) 1. **安装Ollama框架** - Windows/macOS/Linux系统均支持 ```bash # Linux安装命令 curl -fsSL https://ollama.com/install.sh | sh ``` 2. **拉取Mistral模型** ```bash ollama run mistral ``` 该命令会自动下载预量化版模型(约4.1GB),支持CPU/GPU混合计算[^2] 3. **交互测试** 安装完成后直接进入对话界面,输入问题如"解释量子力学"即可测试 ### 方法二:通过Hugging Face Transformers手动部署 1. **安装依赖库** ```python pip install transformers torch ``` 2. **编写推理代码** ```python from transformers import AutoModelForCausalLM, AutoTokenizer model = AutoModelForCausalLM.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") tokenizer = AutoTokenizer.from_pretrained("mistralai/Mistral-7B-Instruct-v0.1") inputs = tokenizer("法国的首都是哪里?", return_tensors="pt") outputs = model.generate(**inputs, max_length=50) print(tokenizer.decode(outputs[0])) ``` 3. **硬件要求** - 最低配置:16GB内存 + 20GB存储空间(FP16精度) - 推荐配置:NVIDIA GPU(8GB显存以上) + 量化加载[^3] ### 注意事项 1. 首次运行会自动下载模型文件,需保证网络稳定 2. Windows系统建议使用WSL2环境获得最佳性能 3. 可通过`model.save_pretrained("./mistral")`保存模型到本地
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值