</>
Skip to content
Gen AI lessons (23/39)

Gen AI — Function Calling

What is function calling?

Letting LLMs call external functions based on user input.

Define functions

tools = [
    {
        "type": "function",
        "function": {
            "name": "get_weather",
            "description": "Get current weather",
            "parameters": {
                "type": "object",
                "properties": {
                    "location": {
                        "type": "string",
                        "description": "City name"
                    }
                },
                "required": ["location"]
            }
        }
    }
]

Make API call

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Weather in Paris?"}],
    tools=tools
)

# Check for function calls
if response.choices[0].message.tool_calls:
    tool_call = response.choices[0].message.tool_calls[0]
    function_name = tool_call.function.name
    arguments = json.loads(tool_call.function.arguments)

Multiple functions

tools = [
    {
        "type": "function",
        "function": {
            "name": "search_products",
            "description": "Search for products",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string"},
                    "max_price": {"type": "number"}
                }
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_order_status",
            "description": "Check order status",
            "parameters": {
                "type": "object",
                "properties": {
                    "order_id": {"type": "string"}
                }
            }
        }
    }
]

Return results to model

messages.append({
    "role": "tool",
    "tool_call_id": tool_call.id,
    "content": json.dumps({"temperature": 72, "condition": "sunny"})
})

# Continue conversation with function results
response = client.chat.completions.create(
    model="gpt-4",
    messages=messages,
    tools=tools
)

Mini Practice

  1. Define a function schema
  2. Handle function calls
  3. Return results to model
  4. Build a multi-function assistant

Up Next

Continue with Multi-Modal - Working with images and text.

Related Topics

Frequently Asked Questions about Function Calling

What is Function Calling in Gen AI?

Function Calling is a fundamental concept in Gen AI. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Function Calling?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Function Calling.

Why is Function Calling important in Gen AI?

Function Calling is essential for Gen AI development. Understanding this concept will help you write better code and solve real-world problems more effectively.