底子部门学习:
1、 Llama 3 Web Demo 摆设- streamlit run ***.py(网页演示py文件路径) ***(下载好的大模型参数路径)
复制代码 注意点:在vscode中要对上面下令产生的External URL的末了四位端标语,在终端旁边的端口菜单中设置端口转发,否则无法在当地欣赏器打开
实验效果:
2、使用 XTuner 完成小助手认知微调
本次微调使用的是QLoRA微调
微调数据集格式:- "conversation": [
- {
- "system": "你是一个懂中文的小助手",
- "input": "你是(请用中文回答)",
- "output": "您好,我是洛希极限,一个由 SmartFlowAI 打造的人工智能助手,请问有什么可以帮助您的吗?"
- }
- ]
复制代码 微调下令:- xtuner train ***(微调的**config.py配置文件路径) --work-dir ***(保存模型微调权重的目录:不存在会自动创建/自行命名)
复制代码 微调完成后,要将Adapter PTH权重格式转化为 HF格式- xtuner convert pth_to_hf ***(微调的**config.py配置文件路径) ***(微调的工作路径/权重保存的路径) ***(转化后的存储路径)
复制代码 将微调后得到的权重与预练习的权重举行归并:- export MKL_SERVICE_FORCE_INTEL=1
复制代码 是一个环境变量设置下令,用于逼迫让使用Intel Math Kernel Library (MKL) 来举行数学盘算,而不是使用其他的数学库。MKL是由英特尔提供的数学盘算库,可以优化和加速数值盘算、线性代数盘算和傅里叶变更等操纵。- xtuner convert merge ***(原始的Llama3权重路径)\
- ***(刚刚准换好为HF(huggface格式)的adapter路径)\
- ***(合并完成后的路径)
复制代码 推理验证:- streamlit run ***.py(网页演示py文件路径) ***(合并完成后的路径的大模型参数路径)
复制代码 实验效果:
3、使用 LMDeploy 乐成摆设 Llama 3 模子
起首必要安装 lmdeploy库- pip install -U lmdeploy[all]
复制代码 生产环境下,将大模子封装成API接口,供客户端访问
构建/启动API服务器
注意:必要在安装有lmdeploy库的环境下运行本部门的下令- lmdeploy serve api_server\
- 模型参数路径\
- --model-formate hf(huggingface格式)\
- --quant-policy 0\
- --server-name API服务的服务IP
- --server-port 服务端口号
- --tp int类型(并行数量GPU数量)
复制代码 通过运行以上指令,我们乐成启动了API服务器,请勿关闭该窗口,背面我们要新建客户端毗连该服务。 你也可以直接打开http://{host}:23333【我的是http://0.0.0.0:23333】检察接口的具体使用分析
新建一个下令行客户端,用来毗连API服务器
两种方式:
(1).下令行客户端毗连API服务器- lmdeploy serve api_client http://localhost:23333
复制代码 运行后,可以通过下令行窗口直接与模子对话
(2).网页客户端毗连API服务器
起首安装gradio;
注意:服务器端的终端不要关闭。 运行之前确保自己的gradio版本低于4.0.0。- pip install gradio==3.50.2
复制代码 使用Gradio作为前端,启动网页客户端:- lmdeploy serve gradio http://localhost:23333 \
- --server-name 0.0.0.0 \
- --server-port 6006
复制代码 运行后,颠末端口转发,可在当地欣赏器中打开【http://localhost:6006/】

进阶部门学习:
1、多模态摆设
微调数据格式:- {
- "id": "4672496731",
- "image": "test_img/oph.jpg",
- "conversations": [
- {
- "from": "human",
- "value": "<image>\nDescribe this image."
- },
- {
- "from": "gpt",
- "value": "This is an image of an eye examination in progress. A man in a white lab coat, likely an optometrist, is attending to a female patient who is undergoing an eye test. She is using a phoropter, a device used to measure an individual's prescription for glasses or contact lenses. In the background, there's an eye chart with letters decreasing in size. The environment looks like a professional medical office, and both individuals seem focused on the task."
- },
- {
- "from": "human",
- "value": "Describe the objects and people in this image."
- },
- {
- "from": "gpt",
- "value": "In the image, there's an eye examination machine, likely a phoropter, operated by a male optometrist. A female patient is seated, engaging with the machine. The optometrist is wearing a white coat, indicating a medical professional. The environment looks clean and professional, typical of a clinical setting."
- },
- {
- "from": "human",
- "value": "What equipment is being used, and for what purpose?"
- },
- {
- "from": "gpt",
- "value": "The equipment in use is a phoropter, a common optometric device. It's used to measure the patient's refractive error and to determine an appropriate prescription for glasses or contact lenses."
- },
- {
- "from": "human",
- "value": "Describe the colors and lighting in the room."
- },
- {
- "from": "gpt",
- "value": "The room is brightly lit, which is essential for an accurate eye examination. The colors are muted, with white walls and a white coat worn by the optometrist, which conveys a sense of cleanliness and simplicity typical of medical environments."
- },
- {
- "from": "human",
- "value": "Can you comment on the patient's attitude?"
- },
- {
- "from": "gpt",
- "value": "The patient seems to be cooperating with the examination process, looking attentively into the eye examination machine. Her posture and focus suggest she is taking the procedure seriously."
- }
- ]
- }
复制代码 微调之前-Llava 将要用到的 Image Projector 预练习权重
在Pretrain阶段,我们会使用大量的图片+简朴文本(caption, 即图片标题)数据对,使LLM明白图像中的广泛特性。即,对大量的图片举行粗看。Pretrain阶段练习完成后,此时的模子已经有视觉本领了!但是由于练习数据中都是图片+图片标题,以是此时的模子固然有视觉本领,但无论用户问它什么,它都只会回复输入图片的标题。即,此时的模子只会给输入图像“写标题”。Pretrain阶段的产物——iter_2181.pth文件。它就是稚子园阶段的Image Projector
微调下令:- xtuner train ***(微调的**config.py配置文件路径) --work-dir ***(保存模型微调权重的目录:不存在会自动创建/自行命名)
- --deepspeed deepspeed_zero2_offload
复制代码 –deepspeed deepspeed_zero2_offload要增长,否则会爆显存
微调完成后,要将Adapter PTH权重格式转化为 HF格式- xtuner convert pth_to_hf ***(微调的**config.py配置文件路径) ***(微调的工作路径/权重保存的路径) ***(转化后的存储路径)
复制代码 Pretrain 模子:稚子园阶段的Image Projector- export MKL_SERVICE_FORCE_INTEL=1
- xtuner chat Llama3模型权重路径 \
- --visual-encoder /root/model/clip-vit-large-patch14-336 \
- --llava 预训练的llava模型权重路径\
- --prompt-template llama3_chat \
- --image 图片路径
复制代码 实验效果:
此时可以看到,Pretrain 模子只会为图片打标签,并不能回复标题。
Finetune 后 模子- export MKL_SERVICE_FORCE_INTEL=1
- xtuner chat Llama3模型权重路径\
- --visual-encoder /root/model/clip-vit-large-patch14-336 \
- --llava 微调后的llava模型权重路径\
- --prompt-template llama3_chat \
- --image 图片路径
复制代码
颠末 Finetune 后,我们可以发现,模子已经可以根据图片回复我们的标题了。
2、Llama3 工具调用本领练习
微调Llama3的工具调用本领
目的:使用 XTuner 在 Agent-FLAN 数据集上微调 Llama3-8B-Instruct,以让 Llama3-8B-Instruct 模子得到智能体本领
数据集先容:Agent-FLAN 数据集是上海人工智能实验室 InternLM 团队所推出的一个智能体微调数据集,其通过将原始的智能体微调数据以多轮对话的方式举行分解,对数据举行本领分解并平衡,以及加入负样本等方式构建了高效的智能体微调数据集,从而可以大幅提升模子的智能体本领;但是该数据集无法被Xtuner直接加载,必要先下载到当地,然后转换成Xtuner直接可用的格式
使用基于 Lagent 的 Web Demo起首必要:微调前的效果:
在插件选择中选ArxivSearch 作为工具
从图中可以看到,Llama3-8B-Instruct 模子并没有乐成调用工具。缘故起因在于它输出了 query=InternLM2 Technical Report 而非 {‘query’: ‘InternLM2 Technical Report’},这也就导致了 ReAct 在分析工具输入参数时发生错误,进而导致调用工具失败。
微调后的智能体:
可以看到,颠末 Agent-FLAN 数据集的微调后,Llama3-8B-Instruct 模子已经可以乐成地调用工具了,其智能体本领有了很大的提升。
chmod -R 755 Agent-FLAN
是一个用于修改文件或目次权限的下令,此中各个参数的寄义如下:
chmod: 表现修改文件或目次的权限。
-R: 表现递归地修改指定目次下的全部文件和子目次的权限。
755: 是权限设置的方式之一,此中数字代表差别的权限设置:
第一个数字 7 表现文件全部者(owner)具有读、写、实验权限;
第二个数字 5 表现文件地点组(group)具有读、实验权限;
第三个数字 5 表现其他用户(others)具有读、实验权限。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金 |