大模型函数调用(function calling)功能,能让大模型调用成千上万的工具 API,赋予大模型更多的外部知识,使得大模型能力变得更加强大。
本文将介绍如何使用 LLaMa-Factory 这个大模型微调框架,对 Qwen1.5-4B 模型进行微调,实现 function calling 功能,使得大模型具有工具调用能力。
如何微调大模型的 function calling 能力?
首先,训练数据集是关键,我们在这里使用 Glaive AI 生成的工具调用数据集,也可以在 HuggingFace 找到 function calling 相关的数据集,该数据集包含用户 (human)、模型(gpt)、工具调用(function_call)和工具调用结果(observation)四种不同角色,以及工具列表 (tools) 字段。
同时,我们还选择了 alpaca_gpt4_en、alpaca_gpt4_zh 和 oaast_sft_zh 这三种数据集,以增强大模型的通用对话能力。
其中一条样本为:
{
"conversations": [
{
"from": "human",
"value": "I saw a dress that I liked. It was originally priced at $200 but it's on 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\"]}}]"
}
其加工成对话样本后的格式如下:
<|im_start|>system
You are a helpful assistant.You have access to the following tools:
> Tool Name: calculate_discount
Tool Description: Calculate the discounted price
Tool Args:
- original_price (number, required): The original price of the item
- discount_percentage (number, required): The percentage of discount
Use the following using a tool:
Action: tool name (one of [calculate_discount]).```
Action Input: the to the tool, a JSON representing the kwargs (e.g. ```{: , : }```).```
<|im_end|>
<|im_start|>user
I saw a dress that I liked. It was originally priced at $ but it


