Menu

Function Calling (Tools)

Introduction

Function calling (or Tool use) is the superpower that transforms an LLM from a text generator into a software operator. Modern models (like GPT-4 and Claude 3) are fine-tuned to reliably output JSON arguments that match predefined function signatures.

How Function Calling Works

  1. You send a prompt to the LLM, along with a list of available functions and their schemas (descriptions of what they do and what arguments they require).
  2. Instead of returning a text answer, the LLM may decide to call one of those functions. It returns a JSON object containing the function name and the arguments.
  3. Your application parses this JSON, executes the actual code (e.g., fetching weather data via an API), and returns the result back to the LLM.
  4. The LLM then uses that real-world data to formulate its final response to the user.

Assignment

  1. Read the OpenAI documentation on Function Calling.
  2. Write a Python script that provides a "get_current_weather" tool to an LLM. Ask the LLM about the weather in Tokyo and have your code handle the tool execution.

Support me!

I am a software engineer giving back to the community - my name is Musila Peter. Join me in empowering learners around the globe by supporting SaneGenius!