智谱 GLM-4-9B-Chat 模型认知微调,怎么样给模型洗脑(一)
<span style="color: black;">❝</span><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>创造了工具,<span style="color: black;">而后</span>工具塑造了<span style="color: black;">咱们</span>。</p><span style="color: black;">❞</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">本篇<span style="color: black;">文案</span>分享下智谱GLM-4-9B-Chat模型的安装与<span style="color: black;">运用</span>,<span style="color: black;">同期</span>展示了下认知微调后的效果。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">1 智谱 GLM-4-9B</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">GLM-4-9B 是智谱 AI 推出的最新一代预训练模型 GLM-4 系列中的开源版本。在语义、数学、推理、代码和知识等多方面的数据集测评中, GLM-4-9B 及其人类偏好对齐的版本 GLM-4-9B-Chat 均表现出超越 Llama-3-8B 的卓越性能。除了能进行多轮对话,GLM-4-9B-Chat 还具备网页浏览、代码执行、自定义工具调用(Function Call)和长文本推理(支持最大 128K 上下文)等高级功能。</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2 GLM-4-9B-Chat 安装</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">运用</span>腾讯云服务器进行安装<span style="color: black;">安排</span>,腾讯云搜索高性能应用服务,创建服务器<span style="color: black;">选取</span>AI框架,磁盘空间<span style="color: black;">全部</span>操作下来大概消耗了100GB,<span style="color: black;">大众</span><span style="color: black;">能够</span><span style="color: black;">按照</span><span style="color: black;">状况</span>进行<span style="color: black;">选取</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/XmaW4RhXPzEKoyr12HYswzomrohDs04XFVGpwfyTvZq0JvIG4KacGGFBZX5iaWPNyVKJ6K5HWiboRicm3trvOzc7w/640?wx_fmt=png&from=appmsg&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">起步</span>成功后登录<span style="color: black;">秘码</span>会以站内信的方式发送给用户,<span style="color: black;">另一</span><span style="color: black;">重视</span>(每次<span style="color: black;">起步</span><span style="color: black;">登录</span>IP都会<span style="color: black;">出现</span>变化),<span style="color: black;">这儿</span>直接<span style="color: black;">选取</span>在<span style="color: black;">掌控</span>台登录<span style="color: black;">选取</span>JupyterLab面板</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/XmaW4RhXPzEKoyr12HYswzomrohDs04Xj81krMXhyVC0W4bP2jiaWSo5SKwSGAZMNNNZdthVbQruIIZibL31hLKw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.1 安装 git 环境</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">首要</span>安装 git</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">apt install git</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">安装 git-lfs 用于下载大文件</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">apt install git-lfs</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.2 大模型下载</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下载大模型glm-4-9b-chat,<span style="color: black;">这儿</span><span style="color: black;">运用</span>魔搭社区的仓库进行下载,文件<span style="color: black;">很强</span>下载会耗费点时间(总计接近20GB),<span style="color: black;">设备</span>磁盘需要预留足够的空间,<span style="color: black;">全部</span>操作下来磁盘<span style="color: black;">运用</span>了约100GB,空间<span style="color: black;">不足</span>的话下载会失败</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git clone https://www.modelscope.cn/ZhipuAI/glm-4-9b-chat.git</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">完成之后继续下载智谱官方封装的模型调用代码:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">git clone https://github.com/THUDM/GLM-4.git</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span>官方<span style="color: black;">供给</span>了4个demo,<span style="color: black;">每一个</span>demo下均有较<span style="color: black;">仔细</span>的文档参考:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">basic_demo <span style="color: black;">基本</span>示例:<span style="color: black;">供给</span>了<span style="color: black;">基本</span>对话、训练后的模型加载及运行的例子。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">composite_demo 综合示例:<span style="color: black;">供给</span>了网页浏览,函数调用例子。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">finetune_demo 微调示例:<span style="color: black;">这儿</span><span style="color: black;">供给</span>的微调方式效率较低,<span style="color: black;">咱们</span>不去<span style="color: black;">运用</span>。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">intel_device_demo 量化运行示例:适合在内存较小的<span style="color: black;">设备</span>上运行,但会丢失精度。</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.3 运行环境配置</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span><span style="color: black;">经过</span>conda进行环境管,刚才<span style="color: black;">选取</span>的腾讯云AI框架环境<span style="color: black;">已然</span>安装了miniconda,<span style="color: black;">因此</span>无需在安装配置 conda, 执行以下命令新建一个 conda 环境并安装所需依赖。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#创建环境 glm4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">conda create -n glm4 python=3.12</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#安装Jupyter内核</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ipython kernel install --name glm4 --user</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#激活环境</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">conda activate glm4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#切换到GLM-4项目的basic_demo的目录</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">cd /GLM-4/basic_demo</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">#安装依赖basic_demo 环境下的依赖</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">pip install -r requirements.txt</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">这儿</span><span style="color: black;">显现</span>了依赖冲突问题,如下所示,<span style="color: black;">咱们</span>将 requirements.txt 文件中的torch版本修改为torch==2.3.0,再次运行 pip install -r requirements.txt 安装成功。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ERROR: Cannot install -r requirements.txt (line 1) because these package versions have conflicting dependencies.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">The conflict is caused by:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> torch 2.4.0 depends on nvidia-cuda-runtime-cu12==12.1.105; platform_system == "Linux" and platform_machine == "x86_64"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">torch 2.3.1 depends on nvidia-cuda-runtime-cu12==12.1.105; platform_system == "Linux" and platform_machine == "x86_64"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">torch 2.3.0 depends on nvidia-cuda-runtime-cu12==12.1.105; platform_system == "Linux" and platform_machine == "x86_64"</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">To fix this you could try to:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. loosen the range of package versions youve specified</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. remove package versions to allow pip attempt to solve the dependency conflict</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.4 运行代码配置</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>直接<span style="color: black;">运用</span> trans_web_demo.py 示例进行测试,这个代码<span style="color: black;">能够</span>加载刚才下载的模型,<span style="color: black;">亦</span>可以加载后边微调后的模型。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、修改模型路径</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MODEL_PATH = os.environ.get(MODEL_PATH, THUDM/glm-4-9b-chat)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 修改为</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">MODEL_PATH = os.environ.get(MODEL_PATH, /root/glm-4-9b-chat)</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、<span style="color: black;">起步</span>项目</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">python trans_web_demo.py</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">显现</span>一个错误和一个告警,稍加修改(官方示例更新的比较频繁,可能会遇到<span style="color: black;">区别</span>的问题,<span style="color: black;">能够</span>到官方仓库进行<span style="color: black;">查找</span>提问:https://github.com/THUDM/GLM-4/issues):</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">错误:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">from peft import AutoPeftModelForCausalLM, PeftModelForCausalLM</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ModuleNotFoundError: No module named peft</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改错误</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">安装peft包:pip install peft</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">告警:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">/root/GLM-4/basic_demo/trans_web_demo.py:85: SyntaxWarning: invalid escape sequence \`</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> line = line.replace("`", "\`")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">修改告警</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">trans_web_demo.py 78 行处</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">line = line.replace("`", "\`")</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 修改为</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">line = line.replace("`", "\\`")</p>
<h2 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">2.5 安装网络穿透工具</span></h2>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">再次运行python trans_web_demo.py命令,模型加载成功,<span style="color: black;">然则</span>会<span style="color: black;">显现</span>下边的提示,frpc_linux_amd64_v0.2 是用来做内网穿透的,方便<span style="color: black;">咱们</span><span style="color: black;">拜访</span>web 页面。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Could not create share link. Missing file: /root/miniconda3/envs/glm4/lib/python3.12/site-packages/gradio/frpc_linux_amd64_v0.2.</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">Please check your internet connection. This can happen if your antivirus software blocks the download of this file. You can install manually by following these steps:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1. Download this file: https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_linux_amd64</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2. Rename the downloaded file to: frpc_linux_amd64_v0.2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">3. Move the file to this location: /root/miniconda3/envs/glm4/lib/python3.12/site-packages/gradio</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">咱们</span>手动安装下这个</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">1、下载frpc_linux_amd64文件,上传到服务器/root 目录下,重命名为 frpc_linux_amd64_v0.2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://cdn-media.huggingface.co/frpc-gradio-0.2/frpc_linux_amd64</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">2、安装 frpc_linux_amd64_v0.2</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 移动文件</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">mv frpc_linux_amd64_v0.2 /root/miniconda3/envs/glm4/lib/python3.12/site-packages/gradio/</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># 添加权限</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">chmod +x /root/miniconda3/envs/glm4/lib/python3.12/site-packages/gradio/frpc_linux_amd64_v0.2</p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">3 <span style="color: black;">起步</span>模型</span></h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">执行命令 python trans_web_demo.py 运行代码,初次加载会有点慢,看到这个临时链接就说明运行成功了。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/XmaW4RhXPzEKoyr12HYswzomrohDs04XhlaicEMpeiap0smZvUt4aklyLnSNcviafQnVIroiaZzu22nNrr7ia2fg0cg/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">到页面上测试下效果,至此<span style="color: black;">咱们</span>的模型<span style="color: black;">已然</span>下载运行成功。</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/XmaW4RhXPzEKoyr12HYswzomrohDs04X2u46eR0ZgD34BlXkiblUfMicpyRcnbeLlWUicshcxxibnkqb87A4yYjH4Q/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">下篇<span style="color: black;">文案</span><span style="color: black;">咱们</span>分享下<span style="color: black;">怎样</span>微调大模型的自我认知,让大模型回答<span style="color: black;">咱们</span>自定义的名<span style="color: black;">叫作</span>和<span style="color: black;">研发</span>者,先来看下微调后的效果:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://mmbiz.qpic.cn/mmbiz_png/XmaW4RhXPzEKoyr12HYswzomrohDs04XVLLUn98wlVCAyMMKRiaGJ7PH9jwKlcABc4CPJNzAS2U7ZFekib7Lw7icw/640?wx_fmt=png&tp=webp&wxfrom=5&wx_lazy=1&wx_co=1" style="width: 50%; margin-bottom: 20px;"></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">关联</span>链接:</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://github.com/THUDM/GLM-4</p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">https://www.modelscope.cn</p>
期待楼主的下一次分享!” 你的见解真是独到,让我受益匪浅。
页:
[1]