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

Implemented the tool_formatter and tool_extractor for glm4 and Qwen2 tool_format #4173

Merged
merged 4 commits into from
Jun 18, 2024

Conversation

mMrBun
Copy link
Contributor

@mMrBun mMrBun commented Jun 9, 2024

What does this PR do?

Implementation of function call for GLM4,Following the previous implementation method, the PROMPT of the function call is added to the TOOL_FORMAT processing function apply using the PROMPT_FORMAT approach.

  • tool_input
tools = [
        {
            "type": "function",
            "function": {
                "name": "get_current_weather",
                "description": "Get the current weather",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "location": {
                            "type": "string",
                            "description": "The city and state, e.g. San Francisco, CA",
                        },
                        "format": {
                            "type": "string",
                            "enum": ["celsius", "fahrenheit"],
                            "description": "The temperature unit to use. Infer this from the users location.",
                        },
                    },
                    "required": ["location", "format"],
                },
            }
        },
        {
            "type": "function",
            "function": {
                "name": "calculate_gpa",
                "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",
                "parameters": {
                    "type": "object",
                    "properties": {
                        "grades": {"type": "array", "items": {"type": "string"}, "description": "The grades"},
                        "hours": {"type": "array", "items": {"type": "integer"}, "description": "The credit hours"},
                    },
                    "required": ["grades", "hours"],
                },
            },
        }
    ]
  • glm4 tokenizer.apply_chat_template
'[gMASK]<sop><|system|>\n你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持。\n\n## get_current_weather\n\n{\n    "name": "get_current_weather",\n    "description": "Get the current weather",\n    "parameters": {\n        "type": "object",\n        "properties": {\n            "location": {\n                "type": "string",\n                "description": "The city and state, e.g. San Francisco, CA"\n            },\n            "format": {\n                "type": "string",\n                "enum": [\n                    "celsius",\n                    "fahrenheit"\n                ],\n                "description": "The temperature unit to use. Infer this from the users location."\n            }\n        },\n        "required": [\n            "location",\n            "format"\n        ]\n    }\n}\n在调用上述函数时,请使用 Json 格式表示调用的参数。\n\n## calculate_gpa\n\n{\n    "name": "calculate_gpa",\n    "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",\n    "parameters": {\n        "type": "object",\n        "properties": {\n            "grades": {\n                "type": "array",\n                "items": {\n                    "type": "string"\n                },\n                "description": "The grades"\n            },\n            "hours": {\n                "type": "array",\n                "items": {\n                    "type": "integer"\n                },\n                "description": "The credit hours"\n            }\n        },\n        "required": [\n            "grades",\n            "hours"\n        ]\n    }\n}\n在调用上述函数时,请使用 Json 格式表示调用的参数。'
  • TOOL_FORMAT
'[gMASK]<sop><|system|>\n你是一个名为 GLM-4 的人工智能助手。你是基于智谱AI训练的语言模型 GLM-4 模型开发的,你的任务是针对用户的问题和要求提供适当的答复和支持,\n\n## get_current_weather\n\n{\n    "name": "get_current_weather",\n    "description": "Get the current weather",\n    "parameters": {\n        "type": "object",\n        "properties": {\n            "location": {\n                "type": "string",\n                "description": "The city and state, e.g. San Francisco, CA"\n            },\n            "format": {\n                "type": "string",\n                "enum": [\n                    "celsius",\n                    "fahrenheit"\n                ],\n                "description": "The temperature unit to use. Infer this from the users location."\n            }\n        },\n        "required": [\n            "location",\n            "format"\n        ]\n    }\n}\n在调用上述函数时,请使用 Json 格式表示调用的参数。\n\n## calculate_gpa\n\n{\n    "name": "calculate_gpa",\n    "description": "Calculate the Grade Point Average (GPA) based on grades and credit hours",\n    "parameters": {\n        "type": "object",\n        "properties": {\n            "grades": {\n                "type": "array",\n                "items": {\n                    "type": "string"\n                },\n                "description": "The grades"\n            },\n            "hours": {\n                "type": "array",\n                "items": {\n                    "type": "integer"\n                },\n                "description": "The credit hours"\n            }\n        },\n        "required": [\n            "grades",\n            "hours"\n        ]\n    }\n}\n在调用上述函数时,请使用 Json 格式表示调用的参数。'

For QWEN2, in the same question
"What's the weather like in San Francisco, Tokyo, and Paris? use Celsius"
the response is
'Action: get_current_weather\nAction Input: {"location": "San Francisco, CA", "format": "celsius"}\nAction: get_current_weather\nAction Input: {"location": "Tokyo, JP", "format": "celsius"}\nAction: get_current_weather\nAction Input: {"location": "Paris, FR", "format": "celsius"}'
Here, the method of identifying tools has been changed from a tuple to a list. For GLM4, I may have conducted fewer tests and haven't encountered cases where multiple tools return results. For now, I will temporarily enclose the tuple in a list in the glm4_tool_extractor function. If I make any new discoveries, I will update the glm4_tool_extractor function accordingly.

Before submitting

@mMrBun mMrBun changed the title Implemented the tool_formatter and tool_extractor for glm4 tool_format Implemented the tool_formatter and tool_extractor for glm4 and Qwen2 tool_format Jun 9, 2024
@hiyouga hiyouga added the pending This problem is yet to be addressed label Jun 10, 2024
@hiyouga hiyouga mentioned this pull request Jun 17, 2024
2 tasks
Copy link
Owner

@hiyouga hiyouga left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@hiyouga hiyouga requested review from hiyouga and removed request for hiyouga June 18, 2024 19:17
@hiyouga hiyouga merged commit c0ca425 into hiyouga:main Jun 18, 2024
@hiyouga hiyouga added solved This problem has been already solved and removed pending This problem is yet to be addressed labels Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
solved This problem has been already solved
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants