Back to Blog

Build AI Apps with Python: Multiple Tools — Claude Learns to Choose | Episode 8

Celest KimCelest Kim

Video: Build AI Apps with Python: Multiple Tools — Claude Learns to Choose | Episode 8 by Taught by Celeste AI - AI Coding Coach

Watch full page →

Build AI Apps with Python: Multiple Tools — Claude Learns to Choose

In this example, we create a Python AI assistant named Claude that can intelligently select from multiple tools—a currency converter, a weather lookup, and a time zone checker—based on user questions. Instead of using if-else chains, we use a dispatch dictionary to map tool names to functions, enabling clean and scalable tool selection.

Code

# Define individual tool functions
def currency_converter(amount, from_currency, to_currency):
  # Dummy conversion logic for illustration
  rates = {'USD': 1, 'EUR': 0.9, 'JPY': 110}
  usd_amount = amount / rates[from_currency]
  converted = usd_amount * rates[to_currency]
  return f"{amount} {from_currency} = {converted:.2f} {to_currency}"

def weather_lookup(location):
  # Placeholder for weather API call
  return f"The current weather in {location} is sunny and 75°F."

def timezone_checker(location):
  # Placeholder for timezone lookup
  timezones = {'New York': 'EST', 'London': 'GMT', 'Tokyo': 'JST'}
  tz = timezones.get(location, 'Unknown timezone')
  return f"The timezone in {location} is {tz}."

# Dispatch dictionary mapping tool names to functions
tools = {
  'currency_converter': currency_converter,
  'weather_lookup': weather_lookup,
  'timezone_checker': timezone_checker,
}

# Descriptions that Claude uses to decide which tool to call
tool_descriptions = {
  'currency_converter': "Convert an amount from one currency to another.",
  'weather_lookup': "Get the current weather for a given location.",
  'timezone_checker': "Find the timezone of a specified location.",
}

def ask(question):
  """
  Simulate Claude reading tool descriptions and picking the right tool.
  For demo, we pick a tool based on keywords in the question.
  """
  question_lower = question.lower()
  if 'currency' in question_lower or 'convert' in question_lower:
    tool = 'currency_converter'
    # Example: "Convert 100 USD to EUR"
    parts = question.split()
    amount = float(parts[1])
    from_curr = parts[2].upper()
    to_curr = parts[4].upper()
    return tools[tool](amount, from_curr, to_curr)
  elif 'weather' in question_lower:
    tool = 'weather_lookup'
    # Example: "What is the weather in London?"
    location = question.split()[-1].rstrip('?')
    return tools[tool](location)
  elif 'time zone' in question_lower or 'timezone' in question_lower:
    tool = 'timezone_checker'
    # Example: "What is the timezone in Tokyo?"
    location = question.split()[-1].rstrip('?')
    return tools[tool](location)
  else:
    return "Sorry, I don't know how to answer that."

# Example queries
print(ask("Convert 100 USD to EUR"))
print(ask("What is the weather in London?"))
print(ask("What is the timezone in Tokyo?"))

Key Points

  • Use a dispatch dictionary to map tool names to their corresponding Python functions for clean, scalable calls.
  • Store tool descriptions separately to help the AI understand each tool’s purpose and select the right one.
  • Implement a reusable ask() function that interprets user questions and dispatches to the correct tool without if-else chains.
  • Support multiple tools in one AI app by registering them in a single dictionary and calling dynamically based on input.
  • Example tools include currency conversion, weather lookup, and timezone checking, demonstrating diverse capabilities.