Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请教一下关于工具调用的微调数据 #684

Open
Jimmy-L99 opened this issue Dec 30, 2024 · 3 comments
Open

请教一下关于工具调用的微调数据 #684

Jimmy-L99 opened this issue Dec 30, 2024 · 3 comments
Assignees

Comments

@Jimmy-L99
Copy link

Feature request / 功能建议

看到https://zhipu-ai.feishu.cn/wiki/UOmbw8PsJiJXrfksUmwcnHHBnQf上有给出tools调用的微调模板,想请教一下关于工具调用的微调数据:

{
    "messages":[
        {
            "role": "system",
            "content": null,
            "tools": [
                {
                    "type": "function",
                    "function": {
                        "name": "weather",
                        "description": "该工具只能得到指定城市的天气信息\n",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "city": {
                                    "description": "城市名称",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "city"
                            ]
                        }
                    }
                }
            ]
        },
        {
            "role": "user",
            "content": "今天深圳天气怎么样"
        },
        {
            "role": "assistant",
            "content": 'weather\n{"city": "深圳"}'
        },
    ]
   },

例如我现在只有"weather"工具,数据集格式如上。

        {
            "role": "user",
            "content": "今天深圳天气怎么样?"
        },
        {
            "role": "assistant",
            "content": 'weather\n{"city": "深圳"}'
        },

上面这俩role是为了让微调后模型根据user的输入而输出固定的格式化参数。我看上面文档除了这俩还有"role": "observation"和 "role": "assistant",这两个作用是什么?
假如我得到了第一轮输出的工具参数weather\n{"city": "深圳"},参数传入weather工具,是不是应该直接将工具返回的结果:

return f"city: {city}, weather: {weather} temperature: {temperature_float}℃,humidity: {humidity_float}%,winddirection: {winddirection},windpower: {windpower}"

作为LLM下一轮对话的"role": "system"的内容?
然后微调数据变成:

{
    "messages":[
        {
            "role": "system",
            "content": null,
            "tools": [
                {
                    "type": "function",
                    "function": {
                        "name": "weather",
                        "description": "该工具只能得到指定城市的天气信息\n",
                        "parameters": {
                            "type": "object",
                            "properties": {
                                "city": {
                                    "description": "城市名称",
                                    "type": "string"
                                }
                            },
                            "required": [
                                "city"
                            ]
                        }
                    }
                }
            ]
        },
        {
            "role": "user",
            "content": "今天深圳天气怎么样"
        },
        {
            "role": "assistant",
            "content": 'weather\n{"city": "深圳"}'
        },
        {
        'role': 'system',
        'content': 'city: 深圳, weather: 晴 temperature: 17.0℃,humidity: 49.0%,winddirection: 北,windpower: ≤3'
        },
        {
        "role": "user",
        'content': '今天深圳的天气预报显示为晴,气温约为17.0℃,相对湿度为49.0%,风向为北风,风力较小,不超过3级。'
        }
    ]
   },

不知道理解的对不对,还望解答。

Motivation / 动机

Your contribution / 您的贡献

@zhipuch zhipuch self-assigned this Dec 31, 2024
@zRzRzRzRzRzRzR
Copy link
Member

assistant是大模型的回复,observation是工具返回的结果。
这里你要改成

{
'role': 'observation',
        'content': 'city: 深圳, weather: 晴 temperature: 17.0℃,humidity: 49.0%,winddirection: 北,windpower: ≤3'
        },
        {
        "role": "assistant",
        'content': '今天深圳的天气预报显示为晴,气温约为17.0℃,相对湿度为49.0%,风向为北风,风力较小,不超过3级。'
}

@zRzRzRzRzRzRzR zRzRzRzRzRzRzR self-assigned this Dec 31, 2024
@Jimmy-L99
Copy link
Author

assistant是大模型的回复,observation是工具返回的结果。 这里你要改成

{
'role': 'observation',
        'content': 'city: 深圳, weather: 晴 temperature: 17.0℃,humidity: 49.0%,winddirection: 北,windpower: ≤3'
        },
        {
        "role": "assistant",
        'content': '今天深圳的天气预报显示为晴,气温约为17.0℃,相对湿度为49.0%,风向为北风,风力较小,不超过3级。'
}

感谢指教,过几天尝试一下~

@Jimmy-L99 Jimmy-L99 reopened this Jan 3, 2025
@Jimmy-L99
Copy link
Author

assistant是大模型的回复,observation是工具返回的结果。 这里你要改成

{
'role': 'observation',
        'content': 'city: 深圳, weather: 晴 temperature: 17.0℃,humidity: 49.0%,winddirection: 北,windpower: ≤3'
        },
        {
        "role": "assistant",
        'content': '今天深圳的天气预报显示为晴,气温约为17.0℃,相对湿度为49.0%,风向为北风,风力较小,不超过3级。'
}

@zRzRzRzRzRzRzR 您好,想再次请教一下,toolcall微调每条数据只能有一个tool吗,还是可以写进多个tool,比如:

{
    "tools": [
        {
            "type": "function",
            "function": {
                "name": "weather",
                "description": "该工具只能得到指定城市的天气信息\n",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "city": {
                            "description": "城市名称",
                            "type": "string"
                        }
                    },
                    "required": [
                        "city"
                    ]
                }
            }
        },
        {
            "type": "function",
            "function": {
                "name": "purchase_order",
            ......
        },
        {
            "type": "function",
            "function": {
                "name": "get_current_time",
            ......
        },
    ]
}

因为我发现,如果工具列表有多个工具,传递给engine.generate的input参数是:

2025-01-03 14:55:49.543 | INFO     | __main__:generate_stream_glm4:207 - inputs: [gMASK]<sop><|system|>
你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。

# 可用工具
## add

{
    "name": "add",
    "description": "计算两个数字的和",
    "parameters": {
        "type": "object",
        "properties": {
            "a": {
                "description": "第一个数字",
                "type": "integer"
            },
            "b": {
                "description": "第二个数字",
                "type": "integer"
            }
        },
        "required": [
            "a",
            "b"
        ]
    }
}
在调用上述函数时,请使用 Json 格式表示调用的参数。
## multiply

{
    "name": "multiply",
    "description": "计算两个数字的乘积",
    "parameters": {
        "type": "object",
        "properties": {
            "a": {
                "description": "第一个数字",
                "type": "integer"
            },
            "b": {
                "description": "第二个数字",
                "type": "integer"
            }
        },
        "required": [
            "a",
            "b"
        ]
    }
}
在调用上述函数时,请使用 Json 格式表示调用的参数。
## weather

{
    "name": "weather",
    "description": "该工具只能得到指定城市的天气信息\n不能用于两个天气数据对比或其他用途",
    "parameters": {
        "type": "object",
        "properties": {
            "city": {
                "description": "城市名称",
                "type": "string"
            }
        },
        "required": [
            "city"
        ]
    }
}
在调用上述函数时,请使用 Json 格式表示调用的参数。
## purchase_order

{
    "name": "purchase_order",
    "description": "该工具只能获取文本中的订单的参数,\n分别是订单名称、采购物品、订单数量、订单价格",
    "parameters": {
        "type": "object",
        "properties": {
            "name": {
                "description": "订单名称",
                "type": "string"
            },
            "project": {
                "description": "采购物品",
                "type": "string"
            },
            "num": {
                "description": "订单数量",
                "type": "integer"
            },
            "price": {
                "description": "订单价格",
                "type": "integer"
            }
        },
        "required": [
            "name",
            "project",
            "num",
            "price"
        ]
    }
}
在调用上述函数时,请使用 Json 格式表示调用的参数。
## get_current_time

{
    "name": "get_current_time",
    "description": "该工具可以获取当前日期和时间",
    "parameters": {
        "type": "object",
        "properties": {
            "time": {
                "description": "时间",
                "type": "string"
            }
        },
        "required": [
            "time"
        ]
    }
}
在调用上述函数时,请使用 Json 格式表示调用的参数。<|user|>

所以不知道微调数据是要:

  1. 按照当前已有的tool列表(包含多个tool)进行微调,写入多个tool,根据问题构建observation和assistant。
  2. 还是只需要写入单个tool,然后根据问题构建对应的observation和assistant。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants