LLM Learning Series 2. Function Calling

Introduction For typical LLM interactions, a single prompt or a few rounds of chat are sufficient to achieve the desired result. However, some tasks require the LLM to access information beyond its internal knowledge base. For example, retrieving today’s weather information for a specific city or searching for a particular anime necessitates calling external functions. What is Function Calling? Function calling in LLMs empowers the models to generate JSON objects that trigger external functions within your code. This capability enables LLMs to connect with external tools and APIs, expanding their ability to perform diverse tasks. Function Calling Execution Steps User calls LLM API with tools and a user prompt: The user provides a prompt and specifies the available tools. What is the weather like in San Francisco? Define Tool Schema tools = [ { "type": "function", "function": { "name": "get_current_weather", "description": "Get the current weather in a given location", "parameters": { "type": "object", "properties": { "location": { "type": "string", "description": "The city and state, e.g. San Francisco, CA", }, "unit": { "type": "string", "enum": ["celsius", "fahrenheit"]}, }, "required": ["location"], }, }, } ] Define Dummy Function # Example function hard coded to return the same weather def get_current_weather(location, unit="fahrenheit"): """Get...

May 5, 2024