Implementing tool/function calling when using Azure OpenAI
In the last article of this series, we learned how to use the chat completion API. Towards the end, we learned that the LLMs have a knowledge cut-off date and no real-time access to information. However, this can be bridged using the tool/function calling feature of Azure OpenAI service. In this article, we shall learn how to implement tool calling.
Azure OpenAI’s function calling capability lets you connect your language models to external tools and APIs, enabling them to perform a wider range of tasks and access real-time information. This opens up a world of possibilities, allowing your models to interact with the real world in ways never imagined.
The Power of Tool Calling
Imagine asking your language model to “Book a flight from Bengaluru to Delhi next Monday.” Without tool calling, the model could only provide general flight information or suggest potential airlines. But with tool calling, it can directly interact with a flight booking API, search for available flights, compare prices, and even make the booking for you!
This is just one example of how tool calling can empower your language models. Other use cases include:
- Retrieving real-time data like weather forecasts, stock prices, or news updates
- Performing actions like sending emails, scheduling appointments, or controlling smart home devices
- Accessing proprietary information from your databases or internal systems
Implementing tools
To implement tool calling, you must first implement the necessary tools or functions. For this purpose, we shall build a function that retrieves real-time weather information for a given location. We will use Open Weather Map API for this purpose. You can sign up and get an API key to work with this API.
|
|
This function retrieves weather data for a given city using the OpenWeatherMap API. It accepts the city name as input, constructs the API URL, makes the request, and returns the JSON response containing the temperature. Besides the logic, the structure of this function is important to consider as a tool. The function must have a doc string that describes its purpose. It needs to return a JSON object. Once we have this function, we can look at the code that interacts with the LLM.
Implementing tool calling
We implement tool calling and LLM interaction as the get_response function in this example. Within the function, we must define the available tools, which are identified by the tools
list.
|
|
We defined a system prompt that tells the LLM to use the tools when available. We add the user-supplied prompt to it and then invoke the API to get a response. If the user’s prompt includes a question about the weather at some location, the LLM determines that the get_weather tools must be invoked and indicates the same in response to the user. The response handling function, get_response
, must handle this, call the tool with the identified arguments, and send the response back to the LLM as the response from the tool call.
|
|
I want to re-iterate that calling tools indicated by the LLM is the application’s responsibility. LLM never calls the tools directly.
Once the tool call response, along with the prior conversation history, is provided to the LLM, the LLM’s final response includes the answer to the user’s prompt.
We can include a call to the get_response
function to invoke the workflow.
|
|
Here is the response for a user prompt “What’s the weather like in Bengaluru?”
|
|
If the prompt does not contain any request for weather information, LLM would behave normally.
|
|
This is a good start. This example showed only one tool definition. However, you can provide any number of tools, and as long as those tools are adequately described, the LLM will be able to indicate what tools to call iteratively. In the next article in this series, we will look at that.
Share on: