Function-Calling Tools: Defining the Function

Working with the OpenAI Responses API

James Chapman

AI Curriculum Manager, DataCamp

Function-Calling Tools

 

  • Function-Calling Tools: LLMs calling programmed functions
    • Can be created for almost anything!
  • convert_currency()
    • amount: amount of money
    • from_currency: original currency
    • to_currency: target currency

frankfurter.png

1 https://frankfurter.dev/
Working with the OpenAI Responses API

Function-Calling Tools

fct1.jpg

Working with the OpenAI Responses API

Function-Calling Tools

fct2.jpg

Working with the OpenAI Responses API

Function-Calling Tools

fct3.jpg

Working with the OpenAI Responses API

Function-Calling Tools

fct4.jpg

Working with the OpenAI Responses API

Function-Calling Tools

fct5.jpg

Working with the OpenAI Responses API

Function-Calling Tools

fct6.jpg

Working with the OpenAI Responses API
def convert_currency(amount, from_currency, to_currency):

url = f"https://api.frankfurter.dev/v1/latest?base={from_currency}&symbols={to_currency}"
try: response = requests.get(url) response.raise_for_status()
data = response.json() rate = data['rates'].get(to_currency)
if rate is None: return f"Could not find exchange rate for {from_currency} to {to_currency}"
converted_amount = amount * rate return f"{amount} {from_currency} = {converted_amount:.2f} {to_currency} (Rate: {rate})"
except requests.exceptions.RequestException as e: return f"Error converting currency: {str(e)}"
Working with the OpenAI Responses API

Our Function

print(convert_currency(100, "USD", "EUR"))
100 USD = 86.62 EUR (Rate: 0.86625)
Working with the OpenAI Responses API

Let's practice!

Working with the OpenAI Responses API

Preparing Video For Download...