大语言模型训练篇:提示词工程【复现】 #41

Open
opened 2024-11-22 16:02:33 +08:00 by GANGUAGUA · 0 comments

开发环境准备

确认算法/代码

准备notebook

确认数据

确认模型

国内外模型的API Key准备

由于我准备调用的是GLM4,所以准备的是智普的API Key

调用国内大模型GLM4测试

在左边目录里可以查到GLM4的python文件:call_glm4.py

设置环境

在【汇视威平台】上运行代码调用线上模型之前需要设置代理以连接到互联网,设置如下

# 终端输入命令
export http_proxy=http://10.10.9.50:3000
export https_proxy=http://10.10.9.50:3000
export no_proxy=localhost,127.0.0.1

调用GLM4

因为调用的是GLM4,所以设置的是对应的智普api-key

由于该文件位于code目录下,所以调用前要先转到code目录下

cd code

最后,在code目录下,调用GLM4文件call_glm4

python call_glm4.py

本地开源模型部署

使用FastChat部署codellama-7b-hf模型

每输入一行命令就建立一个新的终端

# 按顺序依次执行
# 新建终端,输入如下命令
python -m fastchat.serve.controller --host 0.0.0.0
# 新建终端,输入如下命令
python -m fastchat.serve.model_worker --model-path /dataset/CodeLlama-7b-hf/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB
# 新建终端,输入如下命令
python -m fastchat.serve.openai_api_server --host 0.0.0.0

这段代码是一系列指令,用于在不同的终端会话中启动一个基于 Python 的服务,这里是指聊天模型服务。下面是每个命令的解释:

  1. python -m fastchat.serve.controller --host 0.0.0.0
    • 这个命令启动了一个控制器服务,它是 FastChat 服务的一部分。--host 0.0.0.0 表示服务将监听所有可用的网络接口,允许任何机器上的客户端连接到这个服务。
  2. python -m fastchat.serve.model_worker --model-path /dataset/CodeLlama-7b-hf/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB
    • 这个命令启动了一个模型工作器服务,它负责加载和运行机器学习模型。
    • --model-path /dataset/CodeLlama-7b-hf/ 指定了模型文件的存储路径。
    • --host 0.0.0.0 同样表示服务将监听所有网络接口。
    • --num-gpus 4 指定了服务将使用4个GPU进行模型推理。
    • --max-gpu-memory 15GiB 设置了每个GPU可以使用的最大内存为15GB。
  3. python -m fastchat.serve.openai_api_server --host 0.0.0.0
    • 这个命令启动了一个 OpenAI API 服务器,它提供了一个与 OpenAI API 兼容的接口,使得客户端可以通过这个接口与模型交互。
    • --host 0.0.0.0 表示服务器将对所有网络接口开放。

这些命令通常用于在本地机器或服务器上部署一个聊天模型服务。你需要在三个不同的终端窗口中依次执行这些命令,以确保服务的各个组件能够正确启动并协同工作。

终端一:

终端二:

终端三:

codellama-7b-hf模型调用

最后,再打开一个新的终端,调用codellama-7b-hf模型

终端四:

由于询问的是Which class,所以理论上希望得到的回答应该是Select Class...,但得到的回答是Select *,所以可以对codellama-7b-hf模型进行微调训练以提升模型性能,使模型回答的更好。

微调训练

训练管理

开启训练管理

bash distributed_finetune_job.sh llama2_7b_chat_qlora_sql_e3_copy.py 2
  1. bash: 这是调用 Bourne Again Shell(BASH)的命令,BASH 是 Linux 和 Unix 系统中常用的 shell。
  2. distributed_finetune_job.sh: 这是一个 shell 脚本文件,可能包含了用于分布式训练或微调模型的一系列命令和参数设置。
  3. llama2_7b_chat_qlora_sql_e3_copy.py: 这是一个 用于微调模型的Python脚本。脚本名中的 llama2_7b 可能指的是一个具有 70 亿参数的模型,chat 可能表示这个模型用于聊天或对话系统,qlora 可能是微调方法的名称,sql 可能表示模型针对 SQL 相关任务进行了优化,e3_copy 可能是这个脚本或模型版本的特定标识。
  4. 2: 这个数字可能是传递给 distributed_finetune_job.sh 脚本的参数,代表使用2台机器进行微调训练。

训练后测试【训练管理】

xtuner convert pth_to_hf /code/llama2_7b_chat_qlora_sql_e3_copy.py /userhome/xtuner-workdir-job/iter_500.pth/ /code/iter_500_hf/

这条命令使用 xtuner 工具将一个 PyTorch 模型转换为 Hugging Face Transformers 库兼容的格式。下面是命令各部分的详细解释:

  1. xtuner:这是执行命令的程序或脚本的名称,它调用了 xtuner 工具。
  2. convert pth_to_hf:这是 xtuner 工具的一个子命令,用于执行从 PyTorch 模型(.pth 文件)到 Hugging Face Transformers 格式的转换。
  3. /code/llama2_7b_chat_qlora_sql_e3_copy.py:这个参数指定了 Python 脚本的路径,该脚本可能包含了模型转换所需的配置信息或者是一个转换模型的 Python 程序。
  4. /userhome/xtuner-workdir-job/iter_500.pth/:这个参数指定了源 PyTorch 模型文件的路径。iter_500.pth 是模型在某个训练迭代(这里是第500次迭代)后的保存文件。
  5. /code/iter_500_hf/:这个参数指定了转换后的模型文件将要保存的目录路径。转换后的模型将被保存在这个目录下,以便 Hugging Face Transformers 库可以加载和使用。

总结来说,这个命令的作用是使用 xtuner 工具,通过指定的 Python 脚本 /code/llama2_7b_chat_qlora_sql_e3_copy.py,将位于 /userhome/xtuner-workdir-job/iter_500.pth/ 的 PyTorch 模型转换为 Hugging Face Transformers 格式,并保存到 /code/iter_500_hf/ 目录下。这样做的目的是为了能够在 Hugging Face 的生态系统中使用这个模型,例如在 Transformers 库中进行推理或进一步训练。

由于final_test.py文件位于code目录下,所以要先转到code目录

cd code

调动文件

python final_test.py

模型调试

开启模型调试

获取训练配置文件的绝对路径:

训练后测试【模型调试】

xtuner convert pth_to_hf /code/llama2_7b_chat_qlora_sql_e3_copy.py /code/xtuner-workdir/iter_500.pth/ /code/iter_500_hf/

这条命令是用来将一个保存在 PyTorch 格式(.pth)的模型转换为 Hugging Face Transformers 库可以识别的格式。下面是命令各部分的详细解释:

  1. xtuner:这是一个命令行工具的名称,用于执行模型转换等任务。
  2. convert pth_to_hf:这是 xtuner 工具的一个子命令,用于将 PyTorch 模型(.pth 文件)转换为 Hugging Face Transformers 格式。
  3. /code/llama2_7b_chat_qlora_sql_e3_copy.py:这个参数指定了 Python 脚本的路径,该脚本可能包含了模型转换所需的配置信息或者是一个转换模型的 Python 程序。
  4. /code/xtuner-workdir/iter_500.pth/:这个参数指定了源 PyTorch 模型文件的路径。iter_500.pth 表示这是模型在第500次迭代后的保存文件。
  5. /code/iter_500_hf/:这个参数指定了转换后的模型文件将要保存的目录路径。转换后的模型将被保存在这个目录下,以便 Hugging Face Transformers 库可以加载和使用。

总结来说,这个命令的作用是使用 xtuner 工具,通过指定的 Python 脚本 /code/llama2_7b_chat_qlora_sql_e3_copy.py,将位于 /code/xtuner-workdir/iter_500.pth/ 的 PyTorch 模型转换为 Hugging Face Transformers 格式,并保存到 /code/iter_500_hf/ 目录下。这样做的目的是为了能够在 Hugging Face 的生态系统中使用这个模型,例如在 Transformers 库中进行推理或进一步训练。

微调后模型下载并接入FastGPT

合并微调后的模型

把训练出来的lora模型与codellama模型合并。合并后的模型叫code-llama-sql(位于code目录下)(用cpu去做)

xtuner convert merge /dataset/CodeLlama-7b-hf /code/iter_500_hf/ /code/code-llama-sql --device cpu

这条命令使用 xtuner 工具来合并两个模型文件,并将结果保存到一个新的目录中。这里是命令各部分的详细解释:

  1. xtuner:这是执行命令的程序或脚本的名称,它调用了 xtuner 工具。
  2. convert merge:这是 xtuner 工具的一个子命令,用于执行模型合并操作。这通常意味着将两个或多个模型的参数合并到一个模型中。
  3. /dataset/CodeLlama-7b-hf:这个参数指定了第一个模型文件的路径。在这个例子中,它是一个已经转换为 Hugging Face Transformers 格式的模型。
  4. /code/iter_500_hf/:这个参数指定了第二个模型文件的路径,这个模型是之前通过 xtuner convert pth_to_hf 命令转换得到的。
  5. /code/code-llama-sql:这个参数指定了合并后的模型文件将要保存的目录路径。
  6. --device cpu:这个选项指定了合并操作应该在 CPU 上执行,而不是在 GPU 上。

总结来说,这个命令的作用是使用 xtuner 工具将位于 /dataset/CodeLlama-7b-hf/code/iter_500_hf/ 的两个模型合并,并将合并后的模型保存到 /code/code-llama-sql 目录下,整个过程在 CPU 上执行。这种合并操作用于整合不同模型的参数,或者将特定功能(如 SQL 处理能力)集成到基础模型中。

下载合并后的模型到本地

在本机上创建找到xinference文件,如果没有需要自行创建

转到xinference文件下

cd xinference

在xinference文件里,创建一个名为code-llama-sql的文件夹,我们要将模型放在该文件里

mkdir code-llama-sql

在平台上获取文件的下载链接,接下来就可以使用wget或者直接使用浏览器下载

# 在自己的机器上建立一个文件夹,用于放置模型
mkdir code-llama-sql
#下载模型
wget "http://hsw.csidc.cn/notebook_k740c8b04b2c4a30af2e8a9207f065c8_task0/files/code-llama-sql/pytorch_model-00002-of-00007.bin?_xsrf=2%7Cf3ea31bc%7C4f7f94bb92a6b503bd43318398d8e6b9%7C1729784319" -O ./code-llama-sql/pytorch_model-00002-of-00007.bin 
  1. wget:这是一个在命令行中用于下载文件的自由工具。
  2. "[http://hsw.csidc.cn/..."](http://hsw.csidc.cn/..."):这是文件的 URL,也就是模型文件下载的链接,wget 将从这个地址下载文件。
  3. -O ./code-llama-sql/pytorch_model-00002-of-00007.bin:这个参数告诉 wget 将下载的文件保存为 /code-llama-sql/pytorch_model-00002-of-00007.bin。如果 /code-llama-sql/ 目录不存在,wget 会尝试创建这个目录。

使用Xinference部署下载的模型

首先,确保在xinference目录下

在xinference目录下,启动xinference

# 使用docker部署xinference并使用9997端口
docker run -d \
    -e XINFERENCE_MODEL_SRC=modelscope \
    -v $(pwd)/.xinference:/root/.xinference \
    -v $(pwd):/home \
    -v $(pwd)/.cache/huggingface:/root/.cache/huggingface \
    -v $(pwd)/.cache/modelscope:/root/.cache/modelscope \
    -p 9997:9997 \
    --gpus all \
    registry.cn-hangzhou.aliyuncs.com/xprobe_xinference/xinference   xinference-local -H 0.0.0.0

这个 docker run 命令用于在 Docker 中启动一个容器,该容器运行一个名为 xinference 的服务。以下是命令各部分的详细解释:

  1. docker run -d:这表示以分离模式(后台运行)启动容器。
  2. -e XINFERENCE_MODEL_SRC=modelscope:这设置了一个环境变量 XINFERENCE_MODEL_SRC,其值为 modelscope。这可能用于指定模型的来源或存储位置。
  3. -v $(pwd)/.xinference:/root/.xinference:这将当前目录下的 .xinference 目录挂载到容器内的 /root/.xinference$(pwd) 是一个命令替换,它被替换为当前工作目录的绝对路径。
  4. -v $(pwd):/home:这将当前目录挂载到容器内的 /home 目录。
  5. -v $(pwd)/.cache/huggingface:/root/.cache/huggingface:这将当前目录下的 .cache/huggingface 目录挂载到容器内的 /root/.cache/huggingface
  6. -v $(pwd)/.cache/modelscope:/root/.cache/modelscope:这将当前目录下的 .cache/modelscope 目录挂载到容器内的 /root/.cache/modelscope
  7. -p 9997:9997:这将容器内的 9997 端口映射到宿主机的同一端口,允许外部访问。
  8. --gpus all:这指定容器可以使用宿主机上的所有 GPU。
  9. registry.cn-hangzhou.aliyuncs.com/xprobe_xinference/xinference:这是 Docker 镜像的完整名称,包括仓库地址、镜像名称和标签。这个镜像可能托管在阿里云的 Docker 仓库中。
  10. xinference-local -H 0.0.0.0:这是容器启动后运行的命令。xinference-local 可能是一个可执行文件或脚本,-H 0.0.0.0 参数指定服务监听所有网络接口。

总结来说,这个命令启动了一个 Docker 容器,该容器运行 xinference 服务,并且配置了多个卷挂载和环境变量,以便容器可以访问宿主机上的文件和目录。此外,它还映射了一个端口,允许外部访问服务,并且允许容器使用宿主机的所有 GPU 资源。

打开xinference界面

找到“code-llama-instruct”模型,如下所示:

部署成功

接入FastGPT

更新FastGPT配置文件

退出xinference文件夹,在本地里找到Fastgpt文件,并转到该文件下

#要先退出xinference文件夹
cd fastgpt

对fastgpt路径下的config.json文件进行编辑

将以下代码写入config.json文件里的LLM配置

{   "model": "code-llama-instruct",
    "name": "code-llama-instruct",
    "maxContext": 8000,
    "maxResponse": 8000,
    "quoteMaxToken": 8000,
    "maxTemperature": 1.2,
    "charsPointsPrice": 0,
    "censor": false,
    "vision": false,
    "datasetProcess": false,
    "usedInClassify": true,
    "usedInExtractFields": true,
    "usedInToolCall": true,
    "usedInQueryExtension": true,
    "toolChoice": false,
    "functionCall": false,
    "customCQPrompt": "",
    "customExtractPrompt": "",
    "defaultSystemChatPrompt": "",
    "defaultConfig": {}
},

重启fastgpt以生效刚刚更新的配置

sudo docker restart fastgpt

接入OneAPI

打开One API

添加xinference渠道,如下图:

对部署的Xinference渠道进行测试

将模型接入FastGPT

打开FastGPT

创建简易应用

将code-llama-sql模型接入FastGPT

在训练平台里,复制prompt

粘贴到FastGPT里对大模型提问,大模型给出回答

这就完成啦。

# 开发环境准备 ## 确认算法/代码 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155301598-d0d20424-0048-4690-98e9-b1d069358235.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155273304-439e6c8d-bc80-47f0-9f26-c4bd7c4e9bc4.png) ## 准备notebook ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732154494990-2dd7df03-da29-4636-9166-e30d53cfd92c.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155246978-c20956ff-115d-4cf1-a793-83d6f9fc1d79.png) ## 确认数据 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155174512-61d3ad07-e6fe-4fc6-b889-9964737deab9.png) ## 确认模型 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732154996100-76effcd9-42e1-4c42-aace-4b540a61802c.png) ## 国内外模型的API Key准备 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155360802-798d57aa-9627-4de7-ada1-453b86e8bea6.png) 由于我准备调用的是GLM4,所以准备的是智普的API Key ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732155819778-b3af2a9c-075d-489a-b4a3-9dc45fc96fcf.png) # 调用国内大模型GLM4测试 在左边目录里可以查到GLM4的python文件:call_glm4.py ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156291737-97b0c62a-f592-436d-b81f-7461ed05cdfb.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156588449-9454f4d4-d3d2-45b2-86f8-ec4b38d55b53.png) ## 设置环境 <font style="color:#333333;">在【汇视威平台】上运行代码调用线上模型之前需要设置代理以连接到互联网,设置如下</font> ```plain # 终端输入命令 export http_proxy=http://10.10.9.50:3000 export https_proxy=http://10.10.9.50:3000 export no_proxy=localhost,127.0.0.1 ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156081911-2582b0ff-9f1e-4882-b409-0ae8469be741.png) ## 调用GLM4 <font style="color:#333333;">因为调用的是GLM4,所以设置的是对应的智普api-key</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156181103-2c0574dc-ae67-4ad5-a664-df59f2448735.png) 由于该文件位于code目录下,所以调用前要先转到code目录下 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156600257-5117a127-0aa9-4670-af92-44bdc170f702.png) ```plain cd code ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732156766976-5d6d0d1c-bba7-41e7-8eec-61514870fda2.png) 最后,在code目录下,调用GLM4文件call_glm4 ```plain python call_glm4.py ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732157270369-1771b735-454b-4184-9dc2-5f21db2711aa.png) # 本地开源模型部署 ## 使用FastChat部署codellama-7b-hf模型 每输入一行命令就建立一个新的终端 ```plain # 按顺序依次执行 # 新建终端,输入如下命令 python -m fastchat.serve.controller --host 0.0.0.0 # 新建终端,输入如下命令 python -m fastchat.serve.model_worker --model-path /dataset/CodeLlama-7b-hf/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB # 新建终端,输入如下命令 python -m fastchat.serve.openai_api_server --host 0.0.0.0 ``` <font style="color:rgb(6, 6, 7);">这段代码是一系列指令,用于在不同的终端会话中启动一个基于 Python 的服务,这里是指聊天模型服务。下面是每个命令的解释:</font> 1. `python -m fastchat.serve.controller --host 0.0.0.0` - <font style="color:rgb(6, 6, 7);">这个命令启动了一个控制器服务,它是 FastChat 服务的一部分。</font>`--host 0.0.0.0`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">表示服务将监听所有可用的网络接口,允许任何机器上的客户端连接到这个服务。</font> 2. `python -m fastchat.serve.model_worker --model-path /dataset/CodeLlama-7b-hf/ --host 0.0.0.0 --num-gpus 4 --max-gpu-memory 15GiB` - <font style="color:rgb(6, 6, 7);">这个命令启动了一个模型工作器服务,它负责加载和运行机器学习模型。</font> - `--model-path /dataset/CodeLlama-7b-hf/`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">指定了模型文件的存储路径。</font> - `--host 0.0.0.0`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">同样表示服务将监听所有网络接口。</font> - `--num-gpus 4`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">指定了服务将使用4个GPU进行模型推理。</font> - `--max-gpu-memory 15GiB`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">设置了每个GPU可以使用的最大内存为15GB。</font> 3. `python -m fastchat.serve.openai_api_server --host 0.0.0.0` - <font style="color:rgb(6, 6, 7);">这个命令启动了一个 OpenAI API 服务器,它提供了一个与 OpenAI API 兼容的接口,使得客户端可以通过这个接口与模型交互。</font> - `--host 0.0.0.0`<font style="color:rgb(6, 6, 7);"> </font><font style="color:rgb(6, 6, 7);">表示服务器将对所有网络接口开放。</font> <font style="color:rgb(6, 6, 7);">这些命令通常用于在本地机器或服务器上部署一个聊天模型服务。你需要在三个不同的终端窗口中依次执行这些命令,以确保服务的各个组件能够正确启动并协同工作。</font> <font style="color:rgb(6, 6, 7);"></font> <font style="color:rgb(6, 6, 7);">终端一:</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732158801896-509532cb-b89e-4120-82b6-8faf38a0c3c5.png) 终端二: ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732158785197-664ccbe6-8159-43e6-85b9-e47ef9eda43a.png) 终端三: ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732158819385-b8766db7-90a2-457c-bc36-01fc64925106.png) ## codellama-7b-hf模型调用 最后,再打开一个新的终端,调用codellama-7b-hf模型 终端四: ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732158971365-9e660078-9af6-41ec-9387-d861f3e6c89f.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732159124085-b28cf288-b69a-4087-bcfd-7a36853c2afe.png) 由于询问的是Which class,所以理论上希望得到的回答应该是Select Class...,但得到的回答是Select *,所以可以对codellama-7b-hf模型进行微调训练以提升模型性能,使模型回答的更好。 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732159485676-a9ed7c3c-eade-485d-a32c-a1337aed5e8c.png) # 微调训练 ## 训练管理 ### 开启训练管理 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732168890709-fa457262-a036-4346-b3fc-ec87a1b9fd73.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732169047497-dd31fe96-3ca7-4599-849e-287ed7671e53.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732171063744-62e7c4d5-80ff-4bd3-bd8c-c40901376572.png) ```plain bash distributed_finetune_job.sh llama2_7b_chat_qlora_sql_e3_copy.py 2 ``` 1. `bash`<font style="color:rgb(6, 6, 7);">: 这是调用 Bourne Again Shell(BASH)的命令,BASH 是 Linux 和 Unix 系统中常用的 shell。</font> 2. `distributed_finetune_job.sh`<font style="color:rgb(6, 6, 7);">: 这是一个 shell 脚本文件,可能包含了用于分布式训练或微调模型的一系列命令和参数设置。</font> 3. `llama2_7b_chat_qlora_sql_e3_copy.py`<font style="color:rgb(6, 6, 7);">: 这是一个 用于微调模型的Python脚本。脚本名中的 </font>`llama2_7b`<font style="color:rgb(6, 6, 7);"> 可能指的是一个具有 70 亿参数的模型,</font>`chat`<font style="color:rgb(6, 6, 7);"> 可能表示这个模型用于聊天或对话系统,</font>`qlora`<font style="color:rgb(6, 6, 7);"> 可能是微调方法的名称,</font>`sql`<font style="color:rgb(6, 6, 7);"> 可能表示模型针对 SQL 相关任务进行了优化,</font>`e3_copy`<font style="color:rgb(6, 6, 7);"> 可能是这个脚本或模型版本的特定标识。</font> 4. `2`<font style="color:rgb(6, 6, 7);">: 这个数字可能是传递给 </font>`distributed_finetune_job.sh`<font style="color:rgb(6, 6, 7);"> 脚本的参数,代表使用2台机器进行微调训练。</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732170814903-92c17ab5-b7e9-406d-852a-5650e713fcda.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732170866546-b2d5f05b-06f8-453b-85b1-becb062d4f6a.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732178947045-2a236b28-920c-4f2d-9d7b-d2f58bb3bf9e.png) ### 训练后测试【训练管理】 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732179018506-e26e2c09-4f25-476b-b142-266ddc6f602b.png) ```plain xtuner convert pth_to_hf /code/llama2_7b_chat_qlora_sql_e3_copy.py /userhome/xtuner-workdir-job/iter_500.pth/ /code/iter_500_hf/ ``` <font style="color:rgb(6, 6, 7);">这条命令使用 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具将一个 PyTorch 模型转换为 Hugging Face Transformers 库兼容的格式。下面是命令各部分的详细解释:</font> 1. `xtuner`<font style="color:rgb(6, 6, 7);">:这是执行命令的程序或脚本的名称,它调用了 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具。</font> 2. `convert pth_to_hf`<font style="color:rgb(6, 6, 7);">:这是 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具的一个子命令,用于执行从 PyTorch 模型(.pth 文件)到 Hugging Face Transformers 格式的转换。</font> 3. `/code/llama2_7b_chat_qlora_sql_e3_copy.py`<font style="color:rgb(6, 6, 7);">:这个参数指定了 Python 脚本的路径,该脚本可能包含了模型转换所需的配置信息或者是一个转换模型的 Python 程序。</font> 4. `/userhome/xtuner-workdir-job/iter_500.pth/`<font style="color:rgb(6, 6, 7);">:这个参数指定了源 PyTorch 模型文件的路径。</font>`iter_500.pth`<font style="color:rgb(6, 6, 7);"> 是模型在某个训练迭代(这里是第500次迭代)后的保存文件。</font> 5. `/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);">:这个参数指定了转换后的模型文件将要保存的目录路径。转换后的模型将被保存在这个目录下,以便 Hugging Face Transformers 库可以加载和使用。</font> <font style="color:rgb(6, 6, 7);">总结来说,这个命令的作用是使用 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具,通过指定的 Python 脚本 </font>`/code/llama2_7b_chat_qlora_sql_e3_copy.py`<font style="color:rgb(6, 6, 7);">,将位于 </font>`/userhome/xtuner-workdir-job/iter_500.pth/`<font style="color:rgb(6, 6, 7);"> 的 PyTorch 模型转换为 Hugging Face Transformers 格式,并保存到 </font>`/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);"> 目录下。这样做的目的是为了能够在 Hugging Face 的生态系统中使用这个模型,例如在 Transformers 库中进行推理或进一步训练。</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732179452935-9d2778e3-3100-4fd7-b04f-6e7e887be9bb.png) 由于final_test.py文件位于code目录下,所以要先转到code目录 ```plain cd code ``` 调动文件 ```plain python final_test.py ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732179743929-485eb74b-e2e3-48fa-80db-2b94510c5d51.png) ## 模型调试 ### 开启模型调试 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732159928653-6baeb1d2-b507-45b0-baca-114023f521bc.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732160473172-de3de5b2-3077-4d43-a377-e3a60821db4b.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732160246802-011203c9-182f-4be1-9898-7a5394eb326f.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732160585323-4b22e10e-822a-43ab-9b2d-c33e86439553.png) <font style="color:#333333;">获取训练配置文件的绝对路径:</font> <font style="color:#333333;"></font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161077190-8e62b033-19c4-41c0-a3ff-5824e11e8e38.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161143688-21dd17e3-6599-4441-b460-3c20654f998f.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161181223-59ab07b6-4b68-4b40-988c-73e995320b49.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161275081-6706ce49-df4d-4bec-877c-d8d2fb80f769.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161341589-dd6635bd-a808-492b-81dd-16fff71a7374.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732161578351-846b8b20-9cf7-41eb-b7d2-6e784ea93bdc.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732170045623-f4b68253-3170-44f4-96f6-5c150dd90dab.png) ### 训练后测试【模型调试】 ```plain xtuner convert pth_to_hf /code/llama2_7b_chat_qlora_sql_e3_copy.py /code/xtuner-workdir/iter_500.pth/ /code/iter_500_hf/ ``` <font style="color:rgb(6, 6, 7);">这条命令是用来将一个保存在 PyTorch 格式(.pth)的模型转换为 Hugging Face Transformers 库可以识别的格式。下面是命令各部分的详细解释:</font> 1. `xtuner`<font style="color:rgb(6, 6, 7);">:这是一个命令行工具的名称,用于执行模型转换等任务。</font> 2. `convert pth_to_hf`<font style="color:rgb(6, 6, 7);">:这是 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具的一个子命令,用于将 PyTorch 模型(.pth 文件)转换为 Hugging Face Transformers 格式。</font> 3. `/code/llama2_7b_chat_qlora_sql_e3_copy.py`<font style="color:rgb(6, 6, 7);">:这个参数指定了 Python 脚本的路径,该脚本可能包含了模型转换所需的配置信息或者是一个转换模型的 Python 程序。</font> 4. `/code/xtuner-workdir/iter_500.pth/`<font style="color:rgb(6, 6, 7);">:这个参数指定了源 PyTorch 模型文件的路径。</font>`iter_500.pth`<font style="color:rgb(6, 6, 7);"> 表示这是模型在第500次迭代后的保存文件。</font> 5. `/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);">:这个参数指定了转换后的模型文件将要保存的目录路径。转换后的模型将被保存在这个目录下,以便 Hugging Face Transformers 库可以加载和使用。</font> <font style="color:rgb(6, 6, 7);">总结来说,这个命令的作用是使用 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具,通过指定的 Python 脚本 </font>`/code/llama2_7b_chat_qlora_sql_e3_copy.py`<font style="color:rgb(6, 6, 7);">,将位于 </font>`/code/xtuner-workdir/iter_500.pth/`<font style="color:rgb(6, 6, 7);"> 的 PyTorch 模型转换为 Hugging Face Transformers 格式,并保存到 </font>`/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);"> 目录下。这样做的目的是为了能够在 Hugging Face 的生态系统中使用这个模型,例如在 Transformers 库中进行推理或进一步训练。</font> <font style="color:rgb(6, 6, 7);"></font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732171854696-ee748aa2-6427-49d6-8a02-728f8b061547.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732172196659-1ad96850-5812-4077-8efe-a6cb185ca58b.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732259325576-427bbee2-961b-4f3a-aa83-6d1f0c76f792.png) # **<font style="color:#1a1a1a;">微调后模型下载并接入FastGPT</font>** ## 合并微调后的模型 把训练出来的lora模型与codellama模型合并。合并后的模型叫code-llama-sql(位于code目录下)(用cpu去做) ```plain xtuner convert merge /dataset/CodeLlama-7b-hf /code/iter_500_hf/ /code/code-llama-sql --device cpu ``` <font style="color:rgb(6, 6, 7);">这条命令使用 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具来合并两个模型文件,并将结果保存到一个新的目录中。这里是命令各部分的详细解释:</font> 1. `xtuner`<font style="color:rgb(6, 6, 7);">:这是执行命令的程序或脚本的名称,它调用了 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具。</font> 2. `convert merge`<font style="color:rgb(6, 6, 7);">:这是 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具的一个子命令,用于执行模型合并操作。这通常意味着将两个或多个模型的参数合并到一个模型中。</font> 3. `/dataset/CodeLlama-7b-hf`<font style="color:rgb(6, 6, 7);">:这个参数指定了第一个模型文件的路径。在这个例子中,它是一个已经转换为 Hugging Face Transformers 格式的模型。</font> 4. `/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);">:这个参数指定了第二个模型文件的路径,这个模型是之前通过 </font>`xtuner convert pth_to_hf`<font style="color:rgb(6, 6, 7);"> 命令转换得到的。</font> 5. `/code/code-llama-sql`<font style="color:rgb(6, 6, 7);">:这个参数指定了合并后的模型文件将要保存的目录路径。</font> 6. `--device cpu`<font style="color:rgb(6, 6, 7);">:这个选项指定了合并操作应该在 CPU 上执行,而不是在 GPU 上。</font> <font style="color:rgb(6, 6, 7);">总结来说,这个命令的作用是使用 </font>`xtuner`<font style="color:rgb(6, 6, 7);"> 工具将位于 </font>`/dataset/CodeLlama-7b-hf`<font style="color:rgb(6, 6, 7);"> 和 </font>`/code/iter_500_hf/`<font style="color:rgb(6, 6, 7);"> 的两个模型合并,并将合并后的模型保存到 </font>`/code/code-llama-sql`<font style="color:rgb(6, 6, 7);"> 目录下,整个过程在 CPU 上执行。这种合并操作用于整合不同模型的参数,或者将特定功能(如 SQL 处理能力)集成到基础模型中。</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732181231325-a7cc7627-ac79-4907-9be7-84d44d8e3571.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732181293535-48618d72-f228-41ec-9e3c-40fda4f9c3e9.png) ## 下载合并后的模型到本地 在本机上创建找到xinference文件,如果没有需要自行创建 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732185429007-526f850d-f603-4290-9ee9-d86d3c96799a.png) 转到xinference文件下 ```plain cd xinference ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732185476325-06e017c4-ca7e-466e-9470-63cb8dfda818.png) 在xinference文件里,创建一个名为code-llama-sql的文件夹,我们要将模型放在该文件里 ```plain mkdir code-llama-sql ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732185511487-7de7ea23-9bc4-4283-9b69-20693447b223.png) <font style="color:#333333;">在平台上获取文件的下载链接,接下来就可以使用wget或者直接使用浏览器下载</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732246776281-24797749-003f-413d-a81f-2dd772784ecb.png) ```plain # 在自己的机器上建立一个文件夹,用于放置模型 mkdir code-llama-sql #下载模型 wget "http://hsw.csidc.cn/notebook_k740c8b04b2c4a30af2e8a9207f065c8_task0/files/code-llama-sql/pytorch_model-00002-of-00007.bin?_xsrf=2%7Cf3ea31bc%7C4f7f94bb92a6b503bd43318398d8e6b9%7C1729784319" -O ./code-llama-sql/pytorch_model-00002-of-00007.bin ``` 1. `wget`:这是一个在命令行中用于下载文件的自由工具。 2. `"[http://hsw.csidc.cn/..."](http://hsw.csidc.cn/...")`:这是文件的 URL,也就是模型文件下载的链接,wget 将从这个地址下载文件。 3. `-O ./code-llama-sql/pytorch_model-00002-of-00007.bin`:这个参数告诉 wget 将下载的文件保存为 /code-llama-sql/pytorch_model-00002-of-00007.bin。如果 /code-llama-sql/ 目录不存在,wget 会尝试创建这个目录。 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732185647067-4a7f6cd5-53aa-415d-82bc-ecf55e1836f2.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732186393429-fb0688cf-df31-484d-a03c-3251b7688675.png) ## 使用Xinference部署下载的模型 首先,确保在xinference目录下 在xinference目录下,启动<font style="color:#333333;">xinference</font> ```plain # 使用docker部署xinference并使用9997端口 docker run -d \ -e XINFERENCE_MODEL_SRC=modelscope \ -v $(pwd)/.xinference:/root/.xinference \ -v $(pwd):/home \ -v $(pwd)/.cache/huggingface:/root/.cache/huggingface \ -v $(pwd)/.cache/modelscope:/root/.cache/modelscope \ -p 9997:9997 \ --gpus all \ registry.cn-hangzhou.aliyuncs.com/xprobe_xinference/xinference xinference-local -H 0.0.0.0 ``` <font style="color:rgb(6, 6, 7);">这个 </font>`docker run`<font style="color:rgb(6, 6, 7);"> 命令用于在 Docker 中启动一个容器,该容器运行一个名为 </font>`xinference`<font style="color:rgb(6, 6, 7);"> 的服务。以下是命令各部分的详细解释:</font> 1. `docker run -d`<font style="color:rgb(6, 6, 7);">:这表示以分离模式(后台运行)启动容器。</font> 2. `-e XINFERENCE_MODEL_SRC=modelscope`<font style="color:rgb(6, 6, 7);">:这设置了一个环境变量 </font>`XINFERENCE_MODEL_SRC`<font style="color:rgb(6, 6, 7);">,其值为 </font>`modelscope`<font style="color:rgb(6, 6, 7);">。这可能用于指定模型的来源或存储位置。</font> 3. `-v $(pwd)/.xinference:/root/.xinference`<font style="color:rgb(6, 6, 7);">:这将当前目录下的 </font>`.xinference`<font style="color:rgb(6, 6, 7);"> 目录挂载到容器内的 </font>`/root/.xinference`<font style="color:rgb(6, 6, 7);">。</font>`$(pwd)`<font style="color:rgb(6, 6, 7);"> 是一个命令替换,它被替换为当前工作目录的绝对路径。</font> 4. `-v $(pwd):/home`<font style="color:rgb(6, 6, 7);">:这将当前目录挂载到容器内的 </font>`/home`<font style="color:rgb(6, 6, 7);"> 目录。</font> 5. `-v $(pwd)/.cache/huggingface:/root/.cache/huggingface`<font style="color:rgb(6, 6, 7);">:这将当前目录下的 </font>`.cache/huggingface`<font style="color:rgb(6, 6, 7);"> 目录挂载到容器内的 </font>`/root/.cache/huggingface`<font style="color:rgb(6, 6, 7);">。</font> 6. `-v $(pwd)/.cache/modelscope:/root/.cache/modelscope`<font style="color:rgb(6, 6, 7);">:这将当前目录下的 </font>`.cache/modelscope`<font style="color:rgb(6, 6, 7);"> 目录挂载到容器内的 </font>`/root/.cache/modelscope`<font style="color:rgb(6, 6, 7);">。</font> 7. `-p 9997:9997`<font style="color:rgb(6, 6, 7);">:这将容器内的 9997 端口映射到宿主机的同一端口,允许外部访问。</font> 8. `--gpus all`<font style="color:rgb(6, 6, 7);">:这指定容器可以使用宿主机上的所有 GPU。</font> 9. `registry.cn-hangzhou.aliyuncs.com/xprobe_xinference/xinference`<font style="color:rgb(6, 6, 7);">:这是 Docker 镜像的完整名称,包括仓库地址、镜像名称和标签。这个镜像可能托管在阿里云的 Docker 仓库中。</font> 10. `xinference-local -H 0.0.0.0`<font style="color:rgb(6, 6, 7);">:这是容器启动后运行的命令。</font>`xinference-local`<font style="color:rgb(6, 6, 7);"> 可能是一个可执行文件或脚本,</font>`-H 0.0.0.0`<font style="color:rgb(6, 6, 7);"> 参数指定服务监听所有网络接口。</font> <font style="color:rgb(6, 6, 7);">总结来说,这个命令启动了一个 Docker 容器,该容器运行 </font>`xinference`<font style="color:rgb(6, 6, 7);"> 服务,并且配置了多个卷挂载和环境变量,以便容器可以访问宿主机上的文件和目录。此外,它还映射了一个端口,允许外部访问服务,并且允许容器使用宿主机的所有 GPU 资源。</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732241261282-987807c0-a5f1-44d9-91de-3bf7e2a88d50.png) <font style="color:#333333;">打开xinference界面</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732241569535-c8150de4-0b07-4da2-a7ae-a5f18ff16f6a.png) <font style="color:#333333;">找到“code-llama-instruct”模型,如下所示:</font> ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732241643066-9a34c218-9e00-4e5f-bf53-cec40dbe27c0.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732241964001-34448a29-1c00-48d6-a8f4-70a9276cb70e.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732243162478-49f51089-c645-4831-90a0-9d84923ff3d6.png) 部署成功 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732247666174-4f36f33b-fe90-40a4-a891-401fdbc6a1d2.png) ## 接入FastGPT ### 更新FastGPT配置文件 退出xinference文件夹,在本地里找到Fastgpt文件,并转到该文件下 ```plain #要先退出xinference文件夹 cd fastgpt ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732243753426-277682ee-c0e7-427a-86c8-b7f05657679a.png) 对fastgpt路径下的config.json文件进行编辑 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732243866242-31caf899-bf10-49fd-955c-f2802f5597ab.png) <font style="color:#333333;">将以下代码写入config.json文件里的LLM配置</font> ```plain { "model": "code-llama-instruct", "name": "code-llama-instruct", "maxContext": 8000, "maxResponse": 8000, "quoteMaxToken": 8000, "maxTemperature": 1.2, "charsPointsPrice": 0, "censor": false, "vision": false, "datasetProcess": false, "usedInClassify": true, "usedInExtractFields": true, "usedInToolCall": true, "usedInQueryExtension": true, "toolChoice": false, "functionCall": false, "customCQPrompt": "", "customExtractPrompt": "", "defaultSystemChatPrompt": "", "defaultConfig": {} }, ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244281646-e819ff87-6307-43ad-beb0-947e46842904.png) 重启fastgpt以生效刚刚更新的配置 ```plain sudo docker restart fastgpt ``` ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244414448-5dddc9c0-2828-4a59-9dfd-24071dfe7ff5.png) ### 接入OneAPI 打开One API ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244534739-2fa5f172-fe70-40b8-9264-cddc8e9ccd40.png) 添加xinference渠道,如下图: ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244687095-92e3b097-7e56-4597-a65f-010297ade431.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244976343-0f38ce04-2531-420c-a83e-4d72afaf9615.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732244988810-723e38ce-62d9-4fbf-82ca-eab2e65f9f07.png) 对部署的Xinference渠道进行测试 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245041370-b0cf278c-4adf-4c6f-8419-c5d1fd655e2b.png) ### 将模型接入FastGPT 打开FastGPT ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245128208-57309f72-2406-4a0e-a5e0-f3b58dbf0420.png) 创建简易应用 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245222474-3ebcbf2b-3d8d-4545-bc8b-2f4787856e4e.png) ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245331595-57cc35b3-8837-49d0-8f9e-26079902d47f.png) 将code-llama-sql模型接入FastGPT ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245414677-f55ac566-5f5d-4298-af1a-2f334d47d342.png) 在训练平台里,复制prompt ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732245961650-8067f282-5afc-40b3-a660-ff718ec51eed.png) 粘贴到FastGPT里对大模型提问,大模型给出回答 ![](https://cdn.nlark.com/yuque/0/2024/png/48118617/1732246065619-c392449a-f8bf-4bdc-9ff6-43dd0d2a51e3.png) 这就完成啦。
Sign in to join this conversation.
No Label
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: HswOAuth/llm_share#41
No description provided.