0
0
Agentic AIml~20 mins

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

Choose your learning style9 modes available
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.