- Defining a function
- Enabling the language model to choose the function
- Executing the function
- Sharing the results with the language model
Introduction
Using thetools field, you can enable a language model to choose functions to call. The chat completions API does not call the function itself. It will return the arguments you need to execute the function yourself.
Of the open-source language models hosted on Telnyx, zai-org/GLM-5.2 is especially good at calling functions. While we recommend you start with this model, every model in our API supports the tools interface.
Simple get_current_weather example
A popular toy example for function calls is the get_current_weather example.
The following code defines a function and passes it to the language model via the tools field.
Make sure you have set the
TELNYX_API_KEY environment variable.tool_choice of auto lets the language model decide to call a function (or not).
The options for tool_choice are:
required: this forces the language model to choose a toolnone: this forces the language model to NOT choose a toolauto: this lets the language model decide
tool_calls field populated.
Defining functions programmatically
In the next example, we will implement and executeget_current_weather. To do this cleanly, we are first going to define a helper function func_to_tool that extends the schema function from Jeremy Howard’s A Hacker’s Guide to Language Models.
Executing functions
Ok, it’s nice that the language model wants to execute theget_current_weather, but how do we actually do that, and incorporate the results back into the interaction?
Continuing from the chat_completion response in the previous example