How to Call Functions in GLM4.5: A Step-by-Step Guide

How to Call Functions in GLM4.5: A Step-by-Step Guide

When working with GLM4.5, you may need to call functions to assist with user queries. But how do you do it? In this post, we’ll break down the process of calling functions in GLM4.5 and provide an example to illustrate how it works.

Understanding Function Signatures

The first step in calling a function is to understand the function signature. A function signature is a description of the function, including its name, description, parameters, and required fields. In GLM4.5, function signatures are provided within <tools> XML tags.

For example, let’s say we have a function called get_weather that takes two parameters: city and date. The function signature might look like this:

<tools>
  {
    "name": "get_weather",
    "description": "Get the weather of a city for a specific date.",
    "parameters": {
      "type": "object",
      "properties": {
        "city": {
          "type": "string",
          "description": "The city to get weather for, in Chinese."
        },
        "date": {
          "type": "string",
          "description": "The date in YYYY-MM-DD format."
        }
      },
      "required": ["city"]
    }
  }
</tools>

Calling a Function

Now that we have the function signature, we can call the function using the following XML format:

<tool_call>{function-name}
  <arg_key>{arg-key-1}</arg_key>
  <arg_value>{arg-value-1}</arg_value>
  <arg_key>{arg-key-2}</arg_key>
  <arg_value>{arg-value-2}</arg_value>
  ...
</tool_call>

For our get_weather function, the function call might look like this:

<tool_call>get_weather
  <arg_key>city</arg_key>
  <arg_value>Beijing</arg_value>
  <arg_key>date</arg_key>
  <arg_value>2024-06-27</arg_value>
</tool_call>

Example

Let’s say we want to call the get_weather function to check the weather in Beijing and Shanghai for tomorrow. We would call the function twice, once for each city, using the following XML format:

<tool_call>get_weather
  <arg_key>city</arg_key>
  <arg_value>Beijing</arg_value>
  <arg_key>date</arg_key>
  <arg_value>2024-06-27</arg_value>
</tool_call>
<tool_call>get_weather
  <arg_key>city</arg_key>
  <arg_value>Shanghai</arg_value>
  <arg_key>date</arg_key>
  <arg_value>2024-06-27</arg_value>
</tool_call>

Response

The response to the function call would be in the following XML format:

<tool_response>
  {
    "city": "Beijing",
    "date": "2024-06-27",
    "weather": "Sunny",
    "temperature": "26C"
  }
</tool_response>
<tool_response>
  {
    "city": "Shanghai",
    "date": "2024-06-27",
    "weather": "Overcast",
    "temperature": "29C"
  }
</tool_response>

And that’s it! By following these steps, you can call functions in GLM4.5 and get the results you need.


Tags: GLM4.5, Function Calling, XML, Programming
Category: Programming

Leave a Comment

Your email address will not be published. Required fields are marked *