记录一次Linux下ChatGLM部署过程

这篇具有很好参考价值的文章主要介绍了记录一次Linux下ChatGLM部署过程。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前言

本地化的GPT就是香,就是有点费钱。
项目地址:https://github.com/THUDM/ChatGLM-6B
记录一次Linux下ChatGLM部署过程

前期准备

服务器(本机的跳过)

由于本地电脑显卡都不行,所以我租了AutoDL的一台算力服务器。Tesla T4 16G显存,56GB内存,符合我们跑ChatGLM的配置。
记录一次Linux下ChatGLM部署过程
其实本来这台服务器是其他用途,跑vits的,所以选择了so-vits-svc4的镜像,这类的AI项目依赖库大同小异,所以如果有需求,可以直接选现成镜像,减少麻烦。
我这边的操作系统是ubuntu20.04
自带python3.8,需要注意项目python环境需要 >=python3.7

实例后开机,有ssh,我们就改其他ssh客户端登录。当然你也可以直接在autodl自带的终端跑。
记录一次Linux下ChatGLM部署过程

环境搭建

官方教程:https://www.heywhale.com/mw/project/6436d82948f7da1fee2be59e

clone项目

一般都默认装了git,如果没有你可以手动装下,ubuntu的话 sudo apt-get install git
我是选择在数据盘autodl-tmp,因为系统盘其他东西太多了0.0
克隆下项目

git clone https://github.com/THUDM/ChatGLM-6B

安装依赖

前面装完后,进入项目 cd ChatGLM-6B
pip安装依赖

pip install -r requirements.txt

记录一次Linux下ChatGLM部署过程

记录一次Linux下ChatGLM部署过程

下载模型

官方教程如下
记录一次Linux下ChatGLM部署过程

chatglm-6b

由于模型文件很大,需要提前装个 git-lfs
安装命令 sudo apt-get install git-lfs
记录一次Linux下ChatGLM部署过程
因为我测试过6B模型,跑不动,当然你也可以试试
在项目根目录内,创建文件夹 THUDM,命令为 mkdir THUDM
然后进入 cd THUDM,进行clone

git clone https://huggingface.co/THUDM/chatglm-6b

记录一次Linux下ChatGLM部署过程
如果clone大文件失败了,注意文件大小,模型很大的,失败的话请手动下载 官方提供的
https://cloud.tsinghua.edu.cn/d/fb9f16d6dc8f482596c2/
记录一次Linux下ChatGLM部署过程
附赠批量下载shell脚本(别告诉我你没装wget),新建一个download.sh,填入以下代码,增加执行权限 chmod +x download.sh,运行即可 ./download.sh

#!/bin/bash
wget https://cloud.tsinghua.edu.cn/seafhttp/files/2ea1896d-8f77-4f0c-8f41-b73d32da0483/ice_text.model

wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00001-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00002-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00003-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00004-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00005-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00006-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00007-of-00008.bin
wget https://cloud.tsinghua.edu.cn/seafhttp/files/a12ac799-dda9-48c7-b1ce-f6a1d141a4f8/pytorch_model-00008-of-00008.bin

chatglm-6b-int4

❗️ ❗️ ❗️ 如果你也跑不起来的话,可以更换小一些的模型INT4
同理clone到 THUDM

git clone https://huggingface.co/THUDM/chatglm-6b-int4

记录一次Linux下ChatGLM部署过程

记录一次Linux下ChatGLM部署过程

运行

web UI

chatglm-6b

首先安装 Gradio:pip install gradio
如果你跑的 chatglm-6b 模型,那你不需要修改源码,直接在项目根目录运行以下命令即可:

python web_demo.py

记录一次Linux下ChatGLM部署过程

日常跑在7860端口,由于AutoDL没有公网IP,所以很尴尬,我就做了个frp内网穿透。
还有一个方式就是使用AutoDL的自定义服务,开通后官方会内部提供一个公网访问地址。需要把服务改到6006端口。
记录一次Linux下ChatGLM部署过程
然后就访问我们穿的公网IP对应端口即可。

记录一次Linux下ChatGLM部署过程

chatglm-6b int4

如果你跑的int4,则需要改下web_demo.py的源码啦,其实我认为官方应该写成配置文件的形式,改源码有点麻烦,代码如下:
其实就是改下加载的模型路径罢了

from transformers import AutoModel, AutoTokenizer
import gradio as gr
import mdtex2html

tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm-6b-int4", trust_remote_code=True).half().cuda()
model = model.eval()

"""Override Chatbot.postprocess"""


def postprocess(self, y):
    if y is None:
        return []
    for i, (message, response) in enumerate(y):
        y[i] = (
            None if message is None else mdtex2html.convert((message)),
            None if response is None else mdtex2html.convert(response),
        )
    return y


gr.Chatbot.postprocess = postprocess


def parse_text(text):
    """copy from https://github.com/GaiZhenbiao/ChuanhuChatGPT/"""
    lines = text.split("\n")
    lines = [line for line in lines if line != ""]
    count = 0
    for i, line in enumerate(lines):
        if "```" in line:
            count += 1
            items = line.split('`')
            if count % 2 == 1:
                lines[i] = f'<pre><code class="language-{items[-1]}">'
            else:
                lines[i] = f'<br></code></pre>'
        else:
            if i > 0:
                if count % 2 == 1:
                    line = line.replace("`", "\`")
                    line = line.replace("<", "&lt;")
                    line = line.replace(">", "&gt;")
                    line = line.replace(" ", "&nbsp;")
                    line = line.replace("*", "&ast;")
                    line = line.replace("_", "&lowbar;")
                    line = line.replace("-", "&#45;")
                    line = line.replace(".", "&#46;")
                    line = line.replace("!", "&#33;")
                    line = line.replace("(", "&#40;")
                    line = line.replace(")", "&#41;")
                    line = line.replace("$", "&#36;")
                lines[i] = "<br>"+line
    text = "".join(lines)
    return text


def predict(input, chatbot, max_length, top_p, temperature, history):
    chatbot.append((parse_text(input), ""))
    for response, history in model.stream_chat(tokenizer, input, history, max_length=max_length, top_p=top_p,
                                               temperature=temperature):
        chatbot[-1] = (parse_text(input), parse_text(response))

        yield chatbot, history


def reset_user_input():
    return gr.update(value='')


def reset_state():
    return [], []


with gr.Blocks() as demo:
    gr.HTML("""<h1 align="center">ChatGLM</h1>""")

    chatbot = gr.Chatbot()
    with gr.Row():
        with gr.Column(scale=4):
            with gr.Column(scale=12):
                user_input = gr.Textbox(show_label=False, placeholder="Input...", lines=10).style(
                    container=False)
            with gr.Column(min_width=32, scale=1):
                submitBtn = gr.Button("Submit", variant="primary")
        with gr.Column(scale=1):
            emptyBtn = gr.Button("Clear History")
            max_length = gr.Slider(0, 4096, value=2048, step=1.0, label="Maximum length", interactive=True)
            top_p = gr.Slider(0, 1, value=0.7, step=0.01, label="Top P", interactive=True)
            temperature = gr.Slider(0, 1, value=0.95, step=0.01, label="Temperature", interactive=True)

    history = gr.State([])

    submitBtn.click(predict, [user_input, chatbot, max_length, top_p, temperature, history], [chatbot, history],
                    show_progress=True)
    submitBtn.click(reset_user_input, [], [user_input])

    emptyBtn.click(reset_state, outputs=[chatbot, history], show_progress=True)

demo.queue().launch(share=False, inbrowser=True)

改完后,一样运行 python web_demo.py
记录一次Linux下ChatGLM部署过程
跑INT4速度很快,秒回~
记录一次Linux下ChatGLM部署过程

命令行demo

和web UI版一样,如果是跑的INT4,就需要修改源码中,模型的加载路径
记录一次Linux下ChatGLM部署过程

记录一次Linux下ChatGLM部署过程

记录一次Linux下ChatGLM部署过程文章来源地址https://www.toymoban.com/news/detail-485270.html

到了这里,关于记录一次Linux下ChatGLM部署过程的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【ChatGLM】基于 ChatGLM-6B + langchain 实现本地化知识库检索与智能答案生成: 中文 LangChain 项目的实现开源工作

      目录 【ChatGLM】基于 ChatGLM-6B + langchain 实现本地化知识库检索与智能答案生成: 中文 LangChain 项目的实现开源工作 1.克隆源代码:

    2024年02月11日
    浏览(35)
  • AI模型部署记录(一)-ChatGLM:清华开源本地部署(2023/05/06更新)

    文章首发及后续更新:https://mwhls.top/4500.html,无图/无目录/格式错误/更多相关请至首发页查看。 新的更新内容请到mwhls.top查看。 欢迎提出任何疑问及批评,非常感谢! 服务部署汇总 本来这篇是为了打比赛写的,写着写着发现两个问题,AI部署连续几篇,等我比赛打完再发模

    2024年02月03日
    浏览(33)
  • Chinese-LangChain:基于ChatGLM-6b+langchain实现本地化知识库检索与智能答案生成

    Chinese-LangChain:中文langchain项目,基于ChatGLM-6b+langchain实现本地化知识库检索与智能答案生成 https://github.com/yanqiangmiffy/Chinese-LangChain 俗称:小必应,Q.Talk,强聊,QiangTalk 🐯 2023/04/19 引入ChuanhuChatGPT皮肤 📱 2023/04/19 增加web search功能,需要确保网络畅通! 📚 2023/04/18 webui增加知

    2024年02月06日
    浏览(42)
  • chatGLM 本地部署(windows+linux)

    chatGLM算是个相对友好的模型,支持中英文双语的对话交流,清华出的 我的教程无需特别的网络设置,不过部分情况因为国内网络速度慢,需要反复重复 chatGLM github地址 N卡8G显存以上,最好16G以上,我用的是RTX4090做的测试,生成语言时很流畅,和正常人语速差不多,占用显卡

    2024年02月14日
    浏览(28)
  • LLMs之Vicuna:在Linux服务器系统上实Vicuna-7B本地化部署(基于facebookresearch的GitHub)进行模型权重合并(llama-7b模型与delta模型权重)、模型部

    LLMs之Vicuna:在Linux服务器系统上实Vicuna-7B本地化部署(基于facebookresearch的GitHub)进行模型权重合并(llama-7b模型与delta模型权重)、模型部署且实现模型推理全流程步骤的图文教程(非常详细) 导读 :因为Vicuna的训练成本很低,据说只需要$300左右,所以,还是有必要尝试本地化部署

    2024年02月06日
    浏览(44)
  • Excalidraw本地化部署

    1 - Excalidraw介绍 Excalidraw是一个开源、小巧易用的手写风格的框图画板软件。 ​excalidraw官网地址:https://excalidraw.com/​ 2 - Excalidraw本地化安装(git方式) 2-1安装部署 在terminal中,输入: 安装完成后,在terminal中,进入项目文件 2-2 安装依赖环境 - nodeJS NodeJS下载地址: nodejs下载

    2024年02月14日
    浏览(37)
  • Remix 完全本地化部署

    1.简介 Remix 是我们开发 Solidity 智能合约的常用工具,有时候我们会直接访问在线版的 Remix-IDE。 https://remix.ethereum.org/ 但是,如何将在线Remix链接本地文件系统呢,下面则是部署步骤 2、部署 Remixd Remixd 的安装使用步骤如下: 安装 Remixd:  npm install -g @remix-project/remixd 启动 Rem

    2024年02月17日
    浏览(37)
  • 本地化部署stable diffusion

    本文是根据https://zhuanlan.zhihu.com/p/606825889 和 https://blog.csdn.net/cycyc123/article/details/129165844两个教程进行的部署测试,终端是windows 前期需要安装python与git环境,这里不赘叙了,首先是几个下载包,可以提前下载: stable diffusion的web界面环境 https://pan.xunlei.com/s/VNQ4LqoKBidPdqSj2xMioVhs

    2023年04月09日
    浏览(55)
  • hadoop本地化windows部署

    需求背景是java代码提交服务器测试周期流程太慢,需要一种能直接在windows本地部署的相关组件。分析项目现有大数据技术栈,包括hadoop、hive和spark(sparksql),存储和计算都依赖windows系统。期中hive保存在本地的hadoop上,spark提交在hadoop的yarn上。 · hadoop on windows · hive on windows

    2024年02月16日
    浏览(60)
  • Spark的Windows本地化部署完整方案

    对于Spark,网上涉及到Windows平台搭建的资料不多。大多资料不全,而且很少说明注意事项,存在一定的挖坑行为。对于不是很熟悉spark环境搭建过程,但是又非常想在自己的电脑上搭建一个本地环境的小伙伴来说存在一定的绕路行为。本文借鉴了网上大部分的资料,在整理集

    2023年04月11日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包