Back to Blog

Build AI Apps with Python: Currency Converter with Function Calling | Episode 7

Celest KimCelest Kim

Video: Build AI Apps with Python: Currency Converter with Function Calling | Episode 7 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Build AI Apps with Python: Currency Converter with Function Calling

In this episode, you learn how to enhance an AI assistant by enabling it to call a Python function to convert currencies. By defining a currency converter function and describing it with a JSON schema, Claude can decide when to invoke it, making your app more interactive and practical.

Code

from typing import Dict

# Mock exchange rates relative to USD
exchange_rates: Dict[str, float] = {
  "SGD": 1.35,
  "EUR": 0.92,
  "JPY": 146.5,
  "USD": 1.0,
}

def convert_currency(amount: float, from_currency: str, to_currency: str) -> str:
  """
  Convert amount from one currency to another using mock exchange rates.
  Returns a formatted string with the conversion result.
  """
  from_currency = from_currency.upper()
  to_currency = to_currency.upper()

  if from_currency not in exchange_rates:
    return f"Sorry, I don't support converting from {from_currency}."
  if to_currency not in exchange_rates:
    return f"Sorry, I don't support converting to {to_currency}."

  # Convert the amount to USD first, then to target currency
  amount_in_usd = amount / exchange_rates[from_currency]
  converted_amount = amount_in_usd * exchange_rates[to_currency]

  return f"{amount} {from_currency} is approximately {converted_amount:.2f} {to_currency}."

# JSON schema describing the function for the AI tool
currency_converter_tool = {
  "name": "convert_currency",
  "description": "Converts an amount from one currency to another using current exchange rates.",
  "parameters": {
    "type": "object",
    "properties": {
      "amount": {"type": "number", "description": "Amount of money to convert"},
      "from_currency": {"type": "string", "description": "Currency code to convert from"},
      "to_currency": {"type": "string", "description": "Currency code to convert to"},
    },
    "required": ["amount", "from_currency", "to_currency"],
  },
}

# Example usage (assuming integration with Claude's function calling):
# user_input = "Convert 100 USD to SGD"
# Claude decides to call convert_currency with parameters:
#   amount=100, from_currency="USD", to_currency="SGD"
# result = convert_currency(100, "USD", "SGD")
# print(result)  # Output: 100 USD is approximately 135.00 SGD.

Key Points

  • Define Python functions to perform specific tasks like currency conversion with mock exchange rates.
  • Describe each function's inputs and purpose using a JSON schema to enable AI tool use.
  • Pass the function schema in the tools parameter when creating AI messages for function calling.
  • Check the AI's stop_reason to detect when it wants to use a tool and execute the corresponding function.
  • This approach creates a natural interaction loop where the AI calls tools to provide accurate, dynamic answers.