Bird
Raised Fist0
Prompt Engineering / GenAIml~6 mins

Tool usage (function calling) in Prompt Engineering / GenAI - Full Explanation

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
Introduction
Imagine you want to get help from a smart assistant that can do many tasks for you. To get the right help, you need to tell it exactly what you want by calling the right tool or function. This concept explains how to use these tools by calling functions correctly.
Explanation
What is a function call
A function call is like asking a tool to do a specific job for you. You give it a name and some information it needs to work. The tool then uses this information to give you a result or perform an action.
A function call is a clear request to a tool to perform a task using given information.
Why use function calls in tools
Using function calls helps organize tasks so the assistant knows exactly what to do. It avoids confusion by separating different jobs into clear commands. This makes the assistant more reliable and efficient.
Function calls help the assistant understand and perform tasks clearly and efficiently.
How to structure a function call
A function call usually has a name and some inputs called arguments. The name tells which tool to use, and the arguments provide details needed to complete the task. The structure must be correct for the tool to understand and respond properly.
Correct structure with name and arguments is essential for successful function calls.
What happens after a function call
Once the function is called, the tool processes the request and returns a result. This result can be information, an action confirmation, or data. The assistant then uses this result to continue the conversation or complete your request.
Function calls lead to results that help complete tasks or provide information.
Real World Analogy

Imagine you are at a restaurant and want to order food. You tell the waiter exactly what dish you want and any special instructions. The waiter then takes your order to the kitchen and brings back your meal. Calling a function is like placing your order with clear details.

What is a function call → Telling the waiter the name of the dish you want
Why use function calls in tools → Using a waiter to clearly communicate your order instead of shouting in the kitchen
How to structure a function call → Giving the waiter the dish name plus any special instructions like 'no onions'
What happens after a function call → The kitchen prepares the meal and the waiter brings it back to you
Diagram
Diagram
┌───────────────┐       ┌───────────────┐       ┌───────────────┐
│ User Request  │──────▶│ Function Call │──────▶│ Tool Processes│
└───────────────┘       └───────────────┘       └───────────────┘
                                   │                      │
                                   ▼                      ▼
                           ┌───────────────┐       ┌───────────────┐
                           │ Arguments     │       │ Result Returned│
                           └───────────────┘       └───────────────┘
This diagram shows the flow from a user request to a function call, tool processing, and returning a result.
Key Facts
Function callA request to a tool to perform a specific task using given inputs.
ArgumentsThe information or data provided to a function to help it complete its task.
Function nameThe identifier that tells which tool or action to use.
ResultThe output or response returned by the tool after processing a function call.
Common Confusions
Thinking a function call works without correct arguments
Thinking a function call works without correct arguments A function call must include the right arguments; otherwise, the tool cannot perform the task correctly.
Believing the function name can be anything
Believing the function name can be anything The function name must match exactly the tool's expected name to trigger the correct action.
Summary
Function calls are clear requests to tools to perform tasks using specific information.
Correct structure with function name and arguments ensures the tool understands and responds properly.
The tool processes the call and returns a result that helps complete your request.

Practice

(1/5)
1. What is the main purpose of calling a function in AI tool usage?
easy
A. To perform a specific task using given inputs
B. To create new data without inputs
C. To store data permanently
D. To display the AI model architecture

Solution

  1. Step 1: Understand function role in AI tools

    Functions are blocks of code designed to perform specific tasks when called with inputs.
  2. Step 2: Identify the purpose of calling functions

    Calling a function means using it to do a job, usually with parameters to guide the task.
  3. Final Answer:

    To perform a specific task using given inputs -> Option A
  4. Quick Check:

    Function call = perform task with inputs [OK]
Hint: Functions do tasks using inputs, not just store or show data [OK]
Common Mistakes:
  • Thinking functions create data without inputs
  • Confusing function calls with data storage
  • Assuming functions only display info
2. Which of the following is the correct way to call a function named generate_text with a parameter prompt in Python?
easy
A. generate_text = prompt
B. generate_text->prompt()
C. call generate_text(prompt)
D. generate_text(prompt)

Solution

  1. Step 1: Recall Python function call syntax

    In Python, functions are called by writing the function name followed by parentheses enclosing parameters.
  2. Step 2: Match syntax with options

    generate_text(prompt) uses the correct syntax: function name followed by parentheses with parameter inside.
  3. Final Answer:

    generate_text(prompt) -> Option D
  4. Quick Check:

    Python function call = name(params) [OK]
Hint: Use functionName(parameters) to call in Python [OK]
Common Mistakes:
  • Using assignment (=) instead of call
  • Adding extra keywords like 'call'
  • Using wrong symbols like '->'
3. Given the code:
def add_numbers(a, b):
    return a + b

result = add_numbers(3, 5)
print(result)

What will be printed?
medium
A. None
B. 8
C. TypeError
D. 35

Solution

  1. Step 1: Understand function behavior

    The function add_numbers takes two inputs and returns their sum.
  2. Step 2: Calculate the sum of inputs 3 and 5

    3 + 5 equals 8, so the function returns 8, which is stored in result.
  3. Final Answer:

    8 -> Option B
  4. Quick Check:

    3 + 5 = 8 [OK]
Hint: Add inputs inside function to get output [OK]
Common Mistakes:
  • Concatenating numbers as strings (35)
  • Expecting error due to parameters
  • Ignoring return value (None)
4. Identify the error in this function call:
def translate(text, language):
    return f"{text} in {language}"

result = translate("Hello")
print(result)
medium
A. Return statement syntax error
B. Function name is incorrect
C. Missing second argument 'language' in function call
D. Print statement is missing parentheses

Solution

  1. Step 1: Check function definition parameters

    The function translate requires two parameters: text and language.
  2. Step 2: Check function call arguments

    The call translate("Hello") provides only one argument, missing the second required parameter.
  3. Final Answer:

    Missing second argument 'language' in function call -> Option C
  4. Quick Check:

    All parameters must be given when calling [OK]
Hint: Count parameters in definition and match in call [OK]
Common Mistakes:
  • Ignoring missing arguments
  • Assuming default values without definition
  • Misreading function name or print syntax
5. You want to use a function summarize_text that takes a long text and a number max_length to limit summary size. Which call correctly uses this function to summarize article with a max length of 100?
hard
A. summary = summarize_text(article, max_length=100)
B. summary = summarize_text(max_length=100, article)
C. summary = summarize_text(article max_length=100)
D. summary = summarize_text(article, 100, max_length)

Solution

  1. Step 1: Understand function parameters and calling conventions

    The function expects two inputs: the text and the max_length number. Keyword arguments can be used to specify parameters by name.
  2. Step 2: Identify correct syntax for positional and keyword arguments

    summary = summarize_text(article, max_length=100) correctly passes article as the first argument and uses max_length=100 as a keyword argument.
  3. Final Answer:

    summary = summarize_text(article, max_length=100) -> Option A
  4. Quick Check:

    Positional first, then keyword args [OK]
Hint: Use positional args first, then named args [OK]
Common Mistakes:
  • Placing keyword argument before positional
  • Missing commas between arguments
  • Adding extra unexpected arguments