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
- 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).
- 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.
- 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.
- The LLM then uses that real-world data to formulate its final response to the user.
Assignment
- Read the OpenAI documentation on Function Calling.
- 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.