Get startedGet started for free

Function-Calling Tools: Defining the Function

1. Function-Calling Tools: Defining the Function

OpenAI's built-in tools can give us a headstart on many tasks, but sometimes, we need something a little more custom.

2. Function-Calling Tools

The Responses API allows us to convert Python functions into tools for our LLM to use, so-called function-calling tools. This means that the tools you can create are only limited by what you can represent as a Python function and your imagination! Let's create a function-calling tool for retrieving up-to-date currency exchange rates from an external API. This could be used to accurately answer currency-related questions across a whole range of travel and finance applications! This convert_currency() function will take an amount of money, the currency to convert from, and the currency to convert to.

3. Function-Calling Tools

This is a five-step process:

4. Function-Calling Tools

a request is made to the model that contains all the tool definitions and the user's message. In this case, we'll send one tool definition for our currency converter tool.

5. Function-Calling Tools

Then, if the model decides the user's message requires this tool, it creates a tool call with the arguments to pass to the function. It will use the user's natural language message to parse a valid amount, currency to convert from, and currency to convert to.

6. Function-Calling Tools

These arguments are then passed to the function and executed,

7. Function-Calling Tools

and the results are passed back to the model in a second request, so it's now aware of the current exchange rate.

8. Function-Calling Tools

Finally, it's now able to respond to the user's message. Let's begin by defining our currency_converter() function. In the next video, we'll stitch together the full workflow.

9. Our Function

To begin, we need to define a currency exchange function. The exact function code isn't important to remember, just know that any function can be converted into a tool. This function takes arguments for the amount of money, a currency to convert from, and a currency to convert to. We'll use a public API for retrieving exchange rate between two currencies, and we do this by inserting the two currencies as query parameters to the API's URL. Then we open a try-except block, which is good practice for API calls. The idea is that we try to make a request, then fail gracefully if the request fails. We use the .get() method to make a request to the API url, return the exchange rate from the API response, and check that it's a valid value. Then we can perform the currency conversion by multiplying the initial amount and exchange rate, returning a nicely-formatted string. We finish with the except statement to print any error messages created during the API request. And that's our function!

10. Our Function

Calling our function on some example values, converting 100 US dollars to euros, we get a nicely-formatted string containing the currency amounts and the exchange rate used.

11. Let's practice!

Time to try this out!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.