Introduction
Function calling helps large language models (LLMs) use external tools or code to get precise answers or perform tasks beyond just text.
Jump into concepts and practice - no test required
function_call = {
"name": "function_name",
"arguments": {
"param1": "value1",
"param2": "value2"
}
}
response = llm.generate(prompt, function_call=function_call)function_call = {
"name": "get_current_weather",
"arguments": {
"location": "New York"
}
}function_call = {
"name": "calculate_sum",
"arguments": {
"numbers": [5, 10, 15]
}
}class SimpleLLM: def generate(self, prompt, function_call=None): if function_call: name = function_call.get("name") args = function_call.get("arguments", {}) if name == "get_greeting": name_arg = args.get("name", "there") return f"Hello, {name_arg}!" else: return "Function not found." return "No function called." llm = SimpleLLM() # Call the function 'get_greeting' with argument 'name' = 'Alice' function_call = { "name": "get_greeting", "arguments": { "name": "Alice" } } response = llm.generate("Say hello", function_call=function_call) print(response)
{"name": "get_weather", "parameters": {"city": "Paris"}} shows a JSON object with "name" and "parameters", which is the standard format.{"name": "calculate_sum", "parameters": {"a": 5, "b": 3}}{"name": "get_user_info", "params": {"user_id": 42}}