大模型微调示例五之Llama-Factory_agent_functioncalling
大模型微调示例五之Llama-Factory_agent_functioncalling
- 一、数据集准备
- 二、使⽤Llama-Factory微调qwen2-1.5b
- 三、利⽤Agent实现智能客服
一、数据集准备
这里采用ShareGPT 数据集格式,包含多⻆⾊的多轮对话。因为做functioncalling,数据集需包含tools字段。例子如下:
{"conversations": [{"from": "human","value": "I saw a dress that I liked. It was originally priced at $200 but it'son sale for 20% off. Can you tell me how much it will cost after the discount?"},{"from": "function_call","value": "{\"name\": \"calculate_discount\", \"arguments\": {\"original_price\": 200, \"discount_percentage\": 20}}"},{"from": "observation","value": "{\"discounted_price\": 160}"},{"from": "gpt","value": "The dress will cost you $160 after the 20% discount."}],"tools": "[{\"name\": \"calculate_discount\", \"description\": \"Calculate the discounted price\", \"parameters\": {\"type\": \"object\", \"properties\":{\"original_price\": {\"type\": \"number\", \"description\": \"The original price of the item\"}, \"discount_percentage\": {\"type\": \"number\", \"description\": \"The percentage of discount\"}}, \"required\": [\"original_price\",\"discount_percentage\"]}}]"
}
想要加⼊⾃定义⼯具,只需要按照上述格式组织数据集即可。数据配置⽂件为: LLaMA-Factory/data/dataset_info.json
["glaive_toolcall_en_demo": {"file_name": "glaive_toolcall_en_demo.json", "formatting": "sharegpt","columns": {"messages": "conversations", "tools": "tools"}},"glaive_toolcall_zh_demo": {"file_name": "glaive_toolcall_zh_demo.json","formatting": "sharegpt", "columns": {"messages": "conversations", "tools": "tools"}},
]
要⽤⾃⼰制作的数据集进⾏训练,请参考LLama-factory/data下的readme说明,制作成alpaca或者sharegpt的格式,再在dataset_info.json中进⾏配置。
二、使⽤Llama-Factory微调qwen2-1.5b
微调配置:LLaMA-Factory/examples/train_lora/qwen_lora_sft.yaml
### model
model_name_or_path: qwen/Qwen2-1.5B
### method
stage: sft
do_train: true
finetuning_type: lora
lora_target: all
### dataset
dataset: glaive_toolcall_en, glaive_toolcall_zh,alpaca_gpt4_en,alpaca_gpt4_zh
template: qwen
cutoff_len: 1024
max_samples: 50000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: /root/autodl-tmp/checkpoints/agent
logging_steps: 100
save_steps: 1000 plot_loss:
true overwrite_output_dir:
true
### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true ddp_timeout:
180000000
### eval
val_size: 0.01
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 1000
接下来,在Llama-factory⽂件夹下运⾏下⾯的命令开始训练:
llamafactory-cli train ./examples/train_lora/qwen_lora_sft.yaml
合并Lora参数和基本模型参数:在LLaMA-Factory/examples/merge_lora下,命名为qwen_lora_merge.yaml
### Note: DO NOT use quantized model or quantization_bit when merging lora adapters### model
model_name_or_path: qwen/Qwen2-1.5B
adapter_name_or_path: /root/autodl-tmp/checkpoints/agent/checkpoint-35148
template