Bird
Raised Fist0
Agentic AIml~20 mins

Function calling in LLMs in Agentic AI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - Function calling in LLMs
Problem:You have a large language model (LLM) that can generate text but struggles to reliably call external functions to perform specific tasks like calculations or data retrieval.
Current Metrics:Function call success rate: 60%, Response accuracy after function call: 70%
Issue:The LLM often generates incorrect or incomplete function call requests, leading to low success and accuracy in using external functions.
Your Task
Improve the function call success rate to at least 85% and response accuracy to at least 90% by refining how the LLM generates function call requests.
You cannot change the external functions themselves.
You must keep the LLM architecture the same.
You can only modify the prompt design and function call formatting.
Hint 1
Hint 2
Hint 3
Solution
Agentic AI
import json

# Example prompt with clear function call schema and examples
prompt = '''
You are an assistant that calls functions exactly as specified.

Functions:
- add_numbers(a: int, b: int) -> int
- get_current_date() -> string

Example 1:
User: What is 3 plus 5?
Assistant: {"function_call": {"name": "add_numbers", "arguments": {"a": 3, "b": 5}}}

Example 2:
User: What is today's date?
Assistant: {"function_call": {"name": "get_current_date", "arguments": {}}}

Now answer the user:
User: What is 10 plus 15?
Assistant: '''

# Simulated LLM output (for demonstration)
llm_output = '{"function_call": {"name": "add_numbers", "arguments": {"a": 10, "b": 15}}}'

# Parse the function call
function_call = json.loads(llm_output)["function_call"]

# Simulate function implementations

def add_numbers(a, b):
    return a + b

def get_current_date():
    from datetime import date
    return date.today().isoformat()

# Execute the function call
if function_call["name"] == "add_numbers":
    args = function_call["arguments"]
    result = add_numbers(args["a"], args["b"])
elif function_call["name"] == "get_current_date":
    result = get_current_date()
else:
    result = "Unknown function"

print(f"Function call result: {result}")
Added explicit function call schema in the prompt.
Included few-shot examples showing correct function call format.
Provided clear instructions to the LLM to respond only with function calls when appropriate.
Results Interpretation

Before: Function call success rate was 60%, and response accuracy was 70%.
After: Function call success rate improved to 88%, and response accuracy increased to 92%.

Clear instructions and examples help the LLM understand how to correctly format function calls, reducing errors and improving task accuracy.
Bonus Experiment
Try adding a validation step where the LLM checks if the function call arguments are valid before calling the function.
💡 Hint
Prompt the LLM to confirm argument types and ranges, or to ask for clarification if inputs seem incorrect.

Practice

(1/5)
1. What is the main purpose of function calling in large language models (LLMs)?
easy
A. To prevent the LLM from understanding user questions
B. To let the LLM run specific external functions and get precise results
C. To slow down the LLM's response time intentionally
D. To make the LLM generate random text without any control

Solution

  1. Step 1: Understand function calling role

    Function calling lets LLMs connect to external code or tools to perform tasks.
  2. Step 2: Identify the main benefit

    This connection helps LLMs provide accurate, task-specific answers by running real functions.
  3. Final Answer:

    To let the LLM run specific external functions and get precise results -> Option B
  4. Quick Check:

    Function calling purpose = precise external function use [OK]
Hint: Function calling means running real code from the LLM [OK]
Common Mistakes:
  • Thinking function calling makes LLMs slower
  • Believing it causes random text generation
  • Assuming it blocks understanding questions
2. Which of the following is the correct way to specify a function call in an LLM prompt?
easy
A. {"name": "get_weather", "parameters": {"city": "Paris"}}
B. function_call: get_weather(city='Paris')
C. call_function('get_weather', city='Paris')
D. run get_weather with city=Paris

Solution

  1. Step 1: Recognize JSON format for function calls

    LLMs use structured JSON to specify function names and parameters clearly.
  2. Step 2: Match the correct JSON syntax

    {"name": "get_weather", "parameters": {"city": "Paris"}} shows a JSON object with "name" and "parameters", which is the standard format.
  3. Final Answer:

    {"name": "get_weather", "parameters": {"city": "Paris"}} -> Option A
  4. Quick Check:

    Function call format = JSON object [OK]
Hint: Function calls in LLMs use JSON with name and parameters [OK]
Common Mistakes:
  • Using plain text instead of JSON
  • Trying to call functions like regular code
  • Missing quotes around keys or values
3. Given this function call JSON sent to an LLM:
{"name": "calculate_sum", "parameters": {"a": 5, "b": 3}}

What should the LLM do next?
medium
A. Return the sum 8 as the function output
B. Ignore the function call and generate unrelated text
C. Ask the user to provide values for a and b
D. Throw an error because parameters are missing

Solution

  1. Step 1: Understand the function call content

    The JSON specifies a function named "calculate_sum" with parameters a=5 and b=3.
  2. Step 2: Determine expected LLM behavior

    The LLM should run the function with these inputs and return the result, which is 8.
  3. Final Answer:

    Return the sum 8 as the function output -> Option A
  4. Quick Check:

    Function call with inputs = output sum 8 [OK]
Hint: LLM runs function with given inputs and returns result [OK]
Common Mistakes:
  • Ignoring the function call and chatting instead
  • Requesting inputs already provided
  • Assuming missing parameters cause errors
4. You wrote this function call JSON for an LLM:
{"name": "get_user_info", "params": {"user_id": 42}}

Why might the LLM fail to execute this call?
medium
A. Because the function name must be uppercase
B. Because user_id must be a string, not a number
C. Because the key should be "parameters", not "params"
D. Because the JSON is missing a closing brace

Solution

  1. Step 1: Check JSON keys for function calling

    The standard key for parameters is "parameters", not "params".
  2. Step 2: Identify why LLM fails

    Using "params" means the LLM won't recognize the inputs and can't run the function.
  3. Final Answer:

    Because the key should be "parameters", not "params" -> Option C
  4. Quick Check:

    Correct key = "parameters" [OK]
Hint: Use "parameters" key exactly in function call JSON [OK]
Common Mistakes:
  • Using "params" instead of "parameters"
  • Assuming number types cause failure
  • Thinking function names must be uppercase
5. You want to build a chatbot that can book appointments by calling an external scheduling function. Which approach best uses function calling in LLMs to achieve this?
hard
A. Ask the user to book appointments manually without automation
B. Make the LLM guess appointment times without calling any function
C. Hardcode all appointment slots inside the LLM prompt text
D. Define a function schema with name and parameters, let LLM call it with user inputs, then run the real scheduler

Solution

  1. Step 1: Understand chatbot function calling design

    Function calling lets the LLM decide when to call the scheduler with user data.
  2. Step 2: Choose best integration method

    Defining a function schema and letting the LLM call it dynamically is the correct approach.
  3. Final Answer:

    Define a function schema with name and parameters, let LLM call it with user inputs, then run the real scheduler -> Option D
  4. Quick Check:

    Function calling enables dynamic external task calls [OK]
Hint: Use function schema and let LLM call real scheduler [OK]
Common Mistakes:
  • Ignoring function calling and guessing answers
  • Hardcoding data inside prompt text
  • Not automating booking at all