meta 推出 llama3 也有一小段时间了。llama3 包含 8b 和 70b 两种参数规模,涵盖预训练和指令调优的变体。llama 3 支持多种商业和研究用途,并已在多个行业标准测试中展示了其卓越的性能(关于llama3的具体介绍可以参考本站另外一篇博文:尝鲜 meta llama 3)。和以往的原始 llama 模型一样,llama 3 对中文的支持效果欠佳,经常会出现你用中文提问,它用英文或中文+英文回复的现象。
例如当我问它:”你是谁?“,会得到如下的回答:
>>> 你是谁?
i am llama, an ai assistant developed by meta ai that can understand and respond to human input in a conversational manner. i'm not a human, but a computer program designed to simulate conversation and answer questions to the best of my ability based on my training data. i can generate text on a wide range of topics, from science and history to entertainment and culture.
然后让它”介绍一下上海的旅游景点“,则会得到如下的回答:
>>> 介绍一下上海的旅游景点
shanghai, a city with a rich history and culture, has numerous tourist attractions that showcase its modernity, traditional charm, and natural beauty. here are some of the most popular tourist spots in shanghai:
**landmarks:**
1. **oriental pearl tower**: a iconic tv tower with panoramic views of the city.
2. **shanghai tower**: the second-tallest building in the world, offering stunning views from its observation deck.
3. **jin mao tower**: a 421-meter-tall skyscraper and home to the luxurious mandarin oriental hotel.
4. **the bund**: a famous waterfront promenade lined with colonial-era buildings and stunning views of the huangpu river.
**historical sites:**
1. **yu garden**: a classic chinese garden with beautiful landscaping, pavilions, and koi ponds.
2. **shanghai museum**: a museum showcasing chinese art, history, and culture.
3. **xintiandi**: a charming neighborhood with traditional shikumen architecture and modern boutiques.
4. **french concession**: a historic neighborhood with colonial-era buildings, cafes, and restaurants.
**nature reserves:**
1. **changfeng park**: a large public park with gardens, lakes, and walking trails.
2. **fuxing island**: a tranquil oasis in the huangpu river with beautiful landscaping and walking paths.
3. **shanghai botanical garden**: a peaceful garden featuring various plant species and a peaceful atmosphere.
**other attractions:**
1. **disney resort shanghai**: an amusement park with disney-themed rides, shows, and attractions.
2. **shanghai grand opera house**: a stunning opera house with performances ranging from music to dance.
3. **shanghai zoo**: a popular zoo with various animal species and a panda enclosure.
4. **the shanghai tower's observation deck**: for panoramic views of the city.
these are just a few of the many tourist attractions in shanghai. depending on your interests, there are plenty more hidden gems to explore! 😊
回答得还是很简洁而全面的。缺点就是全是英文答案,对于英文不太好的人有点吃力。对 llama3 进行微调来支持中文是一项必须的工作。
llama3 中文模型的现状
实际上,当 llama3 推出以后,很多开发者在中文支持方面做了大量的工作,一大批微调的中文模型如雨后春笋般纷纷涌现。目前比较知名的中文模型有:
- unichat-llama3-chinese(地址):中国联通ai创新中心发布业界第一个llama3中文指令微调模型,全参数微调(非lora),以meta llama 3为基础,增加中文数据进行训练,实现llama3模型高质量中文问答,模型上下文保持原生长度8k,支持长度64k版本将于后续发布。
- openbuddy – open multilingual chatbot(地址):openbuddy是一个面向全球用户的强大的开放式多语言聊天机器人模型,强调对话式ai以及对英语、中文和其他语言的无缝多语言支持。
- llama3-chinese(地址):在500k高质量中文多轮sft数据、100k英语多轮sft数据和2k单轮自我认知数据上训练的大型模型,采用基于meta的dora和lora+的训练方法。以llama-3-8b为基础。
- llama3-8b-chinese-chat(地址):是一个针对中文和英文用户的指令调整语言模型,具有角色扮演和工具使用等各种能力,建立在 meta-llama-3-8b-instruct 模型的基础上。
- llama-3-8b-instruct-chinese-chat(地址):使用 firefly-train-1.1m,moss-003-sft-data,school_math_0.25m,ruozhiba 等数据集微调的模型,基于llama-3-8b-instruct。
- bunny-llama-3-8b-v(地址):bunny 是一系列轻量但功能强大的多模式模型。它提供多种即插即用视觉编码器,如 eva-clip、siglip 和语言主干,包括 llama-3-8b、phi-1.5、stablelm-2、qwen1.5、minicpm 和 phi-2。为了弥补模型大小的减小,通过从更广泛的数据源中进行精选来构建信息更丰富的训练数据。
- llava-llama-3-8b-v1_1(地址):llava-llama-3-8b-v1_1 是一个 llava 模型,由xtuner使用sharegpt4v-pt和internvl-sft从meta-llama/meta-llama-3-8b-instruct和clip-vit-large-patch14-336进行微调。
- 。。。
还有很多新的模型就不一一列举了,想要尝试这些模型可以直接部署试用。本文则探讨如何使用 llama-factory 对 llama3 进行中文微调的具体过程,并通过 ollama 本地部署中文微调的 llama3 模型,打造属于自己的个性化的 llama3 llm 。
使用 llama-factory 对 llama3 进行中文微调
llama-factory 是一个开源项目,它提供了一套全面的工具和脚本,用于微调、服务和基准测试 llama 模型。llama(大型语言模型自适应)是 meta ai 开发的一组基础语言模型,在各种自然语言任务上表现出色。
llama-factory 存储库提供以下内容,让您轻松开始使用 llama 模型:
- 数据预处理和标记化的脚本
- 用于微调 llama 模型的训练流程
- 使用经过训练的模型生成文本的推理脚本
- 评估模型性能的基准测试工具
- 用于交互式测试的 gradio web ui
关于 llama-factory 的具体介绍可以参考本站的另外一篇博文:llama-factory 简介。
安装 llama-factory
首先从 github 拉取 llama-factory:
git clone https://github.com/hiyouga/llama-factory.git
为了方便今后的调试和部署,我选择了使用 docker 的方式来运行 llama-factory。它提供了一个参考的 dockerfile:
from nvcr.io/nvidia/pytorch:24.01-py3
workdir /app
copy requirements.txt /app/
run pip install -r requirements.txt
copy . /app/
run pip install -e .[deepspeed,metrics,bitsandbytes,qwen]
volume [ "/root/.cache/huggingface/", "/app/data", "/app/output" ]
expose 7860
cmd [ "llamafactory-cli webui" ]
可以根据自己的实际情况进行修改。我家里的网络自己搭建了 proxy server(为了访问 github, huggingface 等站点),所以更改 dockerfile 如下。该 dockerfile 从 docker buildx 命令行获取 http_proxy 和 https_proxy 变量,并设置 docker buildx 环境里的相应环境变量,这样编译 docker 镜像的过程中就能使用代理服务器了。
from nvcr.io/nvidia/pytorch:24.01-py3
# 使用构建参数设置环境变量
arg http_proxy
arg https_proxy
# 设置环境变量
env http_proxy=$http_proxy
env https_proxy=$https_proxy
env http_proxy=$http_proxy
env https_proxy=$https_proxy
workdir /app
copy requirements.txt /app/
run pip install -r requirements.txt
copy . /app/
run pip install -e .[deepspeed,metrics,bitsandbytes,qwen]
# unset环境变量。container运行过程中需要代理服务器的话通过-e 传入参数
env http_proxy=""
env https_proxy=""
env http_proxy=""
env https_proxy=""
volume [ "/root/.cache/huggingface/", "/app/data", "/app/output" ]
expose 7860
cmd [ "python", "src/train_web.py" ]
运行如下脚本生成自己的 llama-factory docker 镜像:
docker buildx build --build-arg http_proxy=http://proxy_ip:port --build-arg https_proxy=http://proxy_ip:port -t llama-factory:v0.00 .
docker 镜像编译成功后,运行 docker image list 就可以看到编译出来的 docker 镜像了:
docker image list
接下来我们需要运行 llama-factory container。llama-factory 提供了一个参考的 docker-compose.yml 文件来运行 docker,我们可以按照自己的实际情况进行修改。我这边修改的版本如下,修改的部分参考注释。
version: '3.8'
services:
llama-factory:
#build:
# dockerfile: dockerfile
# context: .
image: llama-factory:v0.00 # 修改为编译出来的 docker image 名称/版本
container_name: llama_factory # container 名称
volumes:
- ./volumes/huggingface:/root/.cache/huggingface/
- ./volumes/data:/app/data
- ./volumes/output:/app/output
- /mnt/dev/myprojects/llm-webui-docker/models:/app/models # 映射自己的models目录
environment:
- cuda_visible_devices=0
- gradio_server_port=7864 # webui跑在7864端口上,7860被comfyui占用了
ports:
- "7864:7864" # webui跑在7864端口上,7860被comfyui占用了
ipc: host
deploy:
resources:
reservations:
devices:
- driver: nvidia
count: "all"
capabilities: [gpu]
restart: unless-stopped
运行如下命令启动 llama-factory 容器:
docker-compose up --detach
现在可以看到 llama_factory container 已经在正常运行了:
访问 http://server_ip:7864 则可以看到如下的 llama-factory webui 界面:
使用 llama-factory 为 llama3 训练中文 lora
1. 模型名称与路径
进入 llama-factory webui 后,先选择 model name 为 llama-8b,这时候 model path 会自动变成 meta-llama/meta-llama-3-8b 并自动从 huggingface 拉取 meta-llama-3-8b 模型。我这里因为已经下载了 meta-llama-3-8b 并映射到了 llama-factory container 的 /app/models/llama3-hf/meta-llama-3-8b 目录,所以我这里的 model path 也设置为该路径。
2. 设置 advanced configurations
点右边的箭头展开 advance configurations。量化位数(quantization bit)可以选择 4 ,减小模型的体积并提高速度。prompt template 则选择 llama3。如果有安装 flashattn2 或者 unsloth 的话可以在 boost 里选择,我这里没有安装所以选择 none。
3. 设置训练参数
在 train 标签页里设置训练相关的参数。主要的参数有:
- stage:设置为 supervised fine-tuning
- data dir:我这里设置为 data ,因为 llama-factory 项目自身也带了一些中文数据集,我打算直接使用。如果你自己下载了别的中文数据集,请设置相应的数据集所在的目录地址
- dataset:我选择了 llama-factory 项目里自带的 alpaca_zh,alpaca_gpt4_zh 和 oaast_sft_zh 数据集
- learning rate:安装自己的需要设置。我采用了缺省的 5e-5
- epochs:按照自己的需要设置。我采用了缺省的 3.0
- cutoff length:按照自己的需要设置。数字越大,对 gpu 和显存的要求越高;数字越小,则可能对长句的语义理解不够充分。我这里选择缺省的 1024
- batch size:按照自己的需要甚至。数字越大,对 gpu 和显存的要求越高。我这里选择缺省的 2
- output dir / config path:按照自己的需要设置
设置完成后,可以点击 preview dataset 来查看一下数据集内容。
点击 preview command 可以查看训练过程的命令行参数。如果不希望使用 webui 进行训练,则可以直接执行命令行,这样也有助于进一步编程和自动化:
点击 save arguments 则将目前的训练设置保存到指定的 json 文件。点击 load arguments 则可以加载以前保存好的训练设置。
4. 开始训练
参数设置好后,就可以点击 start 开始训练。
llama-factory 训练脚本开始解析数据。
整个训练的过程预计 8 个小时不到一点。
训练完成后,在 /app/output/llama3_cn_train_2024-04-27-16-32-46 目录下可以看到如下的文件目录结构:
这些就是 llama-factory 训练出来的 lora。可以在自动生成的 readme.md 文件查看 lora 的信息:
license: other
library_name: peft
tags:
- llama-factory
- lora
- generated_from_trainer
base_model: /app/models/llama3-hf/meta-llama-3-8b
model-index:
- name: llama3_cn_train_2024-04-27-16-32-46
。。。后面的内容省略
在 ollama 中打造自己的中文版 llama3
接下来,我们要在 ollama 中运行 llama3 和我们训练出来的 lora,打造属于自己的中文版 llama3。
ollama 是一个开源的大模型管理工具,它提供了丰富的功能,包括模型的训练、部署、监控等。 通过ollama,你可以轻松地管理本地的大模型,提高模型的训练速度和部署效率。 此外,ollama还支持多种机器学习框架,如tensorflow、pytorch等,使得你可以根据自己的需求选择合适的框架进行模型的训练。
运行 ollama docker
从 github 拉取 ollama 代码:
git clone https://github.com/ollama/ollama.git
ollama github项目提供了参考的 dockerfile,可以编译自己的 ollama 镜像并运行。也可以直接运行 ollama 官方发布的 docker 镜像:
docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
转换 lora 格式
按照 ollama modelfile adapter 的说明,ollama 支持 ggml 格式的 lora,所以我们需要把刚才生成的 lora 转换成为 ggml 格式。为此,我们需要使用到 llama.cpp 的某些脚本。有关 llama.cpp 开源项目的介绍请参考本站另外一篇博文:llama.cpp 上手实战指南 。
在 llama.cpp 项目中,有如下几个用于转换格式的 python 脚本。我们将使用 conver-lora-to-ggml.py 脚本来转换格式。
运行如下的命令(其中 /app/output/llama3_cn_train_2024-04-27-16-32-46 是 llama-factory 生成 lora 的路径):
./conver-lora-to-ggml.py /app/output/llama3_cn_train_2024-04-27-16-32-46 llama
运行完这个命令后,将在 /app/output/llama3_cn_train_2024-04-27-16-32-46 下生成 ggml-adapter-model.bin 文件。这个文件就是 ollama 需要的 ggml 格式的 lora 文件。
在 ollama 中创建自己的 llama3 中文模型
我们使用 ollama 的 modelfile 来创建自己的 llama3 中文模型。我自己使用的参考 llama3.modelfile 内容如下:
# set the base model
from llama3:8b
# set custom parameter values
parameter temperature 1
parameter num_keep 24
parameter stop <|start_header_id|>
parameter stop <|end_header_id|>
parameter stop <|eot_id|>
parameter stop <|reserved_special_token
# set the model template
template """
{{ if .system }}<|start_header_id|>system<|end_header_id|>
{{ .system }}<|eot_id|>{{ end }}{{ if .prompt }}<|start_header_id|>user<|end_header_id|>
{{ .prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>
{{ .response }}<|eot_id|>
"""
# set the system message
system you are llama3 from meta, customized and hosted @ hy's blog (https://blog.yanghong.dev).
# set chinese lora support
adapter /root/.ollama/models/lora/ggml-adapter-model.bin
首先,通过命令行进入 ollama container:
docker exec -it ollama /bin/bash
然后使用我们刚才生成的 llama3.modelfile 来创建自己的中文版 llama3,命名为 llama3:hy。
ollama create llama3:hy -f llama3.modelfile
创建成功后,运行如下的命令,则可以看到刚才创建的 llama3:hy 模型。
ollama list
执行如下的命令来运行 llama3:hy 模型,接下来我们就可以愉快地跟它进行中文对话了。
ollama run llama3:hy
我们同样问它本文开头的那两个问题:”你是谁?“,”介绍一下中国的首都北京“,看看它的表现如何。
你是谁?
>>> 你是谁?
我是一個 ai 型chatbot,来自 meta,目前被customized 和 hosted 在 hy 的博客(https://blog.yanghong.dev)。简单地说,我是一個能與人互動、回答問題和生成文本的 ai 机器人。
介绍一下中上海的旅游景点:
>>> 介绍一下上海的旅游景点
上海是一座流行的旅游目的地,拥有悠久历史、多样文化和现代建筑。以下是上海一些最受欢迎的旅游景点:
1. 外滨路(the bund):黄浦江沿岸的一带,提供城市天际线的壮丽景象。此外,这里也有一些历史古楼、餐厅和酒吧。
2. 上海塔(shanghai tower):这是世界最高建筑之一,高度632米。从124层观光台可以欣赏到绕城全景。
3. 豫园(yu garden):明代的中国古典花园,known for its beautiful architecture, lush greenery and peaceful atmosphere.
4. 法租区(french concession):一带拥有法国影响的街区,featuring colonial-era buildings, trendy boutiques和hip restaurants。
5. 新天地(xintiandi):历史区域被转换为时尚购物和美食街区,known for its cobblestone streets, traditional shikumen houses和vibrant nightlife.
6. 上海博物馆(shanghai museum):展示城市历史、艺术和文化的综合博物馆,有超过10,000件展品。
7. 人民广场(people's square):一个大型公共广场,surrounded by iconic buildings包括上海大剧院、上海市政厅和中国革命博物馆。
8. 东方明珠塔(oriental pearl tower):一座电视塔,提供城市全景观从263层观光台。
9. 朱家角水乡(zhujiajiao water town):一个古老的水镇位于上海外围,known for its canals, bridges和traditional architecture.
10. 上海迪士尼度假 resort(disneyland resort shanghai):一个世界一流的主题公园,featuring disney, pixar, marvel和star wars attractions.
这些只是上海旅游景点之一。无论你是否感兴趣历史、文化、美食或娱乐,这座城市都有足够的选择!
可以看到,它基本上能以中文回答问题,我们的 lora fine tuning 过程成功完成了。
为中文 llama3 添加 webui
ollama 提供了 rest api 来跟 llm 模型进行交互,比如最常用的 generate,chat 等方法。
generate a response
curl http://localhost:11434/api/generate -d '{
"model": "llama3",
"prompt":"why is the sky blue?"
}'
chat with a model
curl http://localhost:11434/api/chat -d '{
"model": "llama3",
"messages": [
{ "role": "user", "content": "why is the sky blue?" }
]
}'
完整的 rest api 文档可参阅 github 。
因此,我们可以开发一个简单的 gradio 程序,通过 rest api 调用 llama3:hy 模型来进行中文交互。参考代码片段如下:
response = requests.post('http://192.168.3.204:11434/api/generate',
json={
'model': model2endpoint[model_name],
'prompt': prompt,
#'context': context,
'options': {
'top_k': top_k,
'temperature': top_p,
'top_p': temperature
}
},
stream=true
)
yield "", history, user_message, ""
output = ""
# check if the request was successful
response.raise_for_status()
# initialize the output and history variables
output = ""
# iterate over the streamed response lines
for idx, line in enumerate(response.iter_lines()):
if line:
# parse the line as json
data = json.loads(line)
token = data.get('response', '') # assuming 'response' contains the text
# check if the token is a special token
if data.get('special', false):
continue
# append the token to the output
output += token
# update history and chat based on the index
if idx == 0:
history.append(output.strip()) # append initial output
else:
history[-1] = output.strip() # update the last history entry
# convert history to chat format
chat = [
(history[i], history[i + 1]) if i + 1 < len(history) else (history[i], "")
for i in range(0, len(history), 2)
]
# yield the current chat, history, and user message updates
yield chat, history, user_message, ""
这样就可以通过上面的 webui 界面让它进行中文对话,问问题,帮我写代码了。同时也整合了其它的一些 coding llm 在一起,碰到不会写的代码,就让它们在一起比比武。
以下视频是 fine tune 的中文 llama3 实际使用演示。
llama3中文微调本地部署
如果您喜欢本文的内容,欢迎扫描下面的二维码访问作者的博客:hy's blog
发表评论