Introduction to Function Calling

What is Function Calling

Function Calling is an important way for AI models to interact with external systems. It allows AI models to identify user intent and trigger specific function calls during conversations. Essentially, Function Calling serves as a bridge between AI models and APIs, enabling AI to perform operations beyond pure text interaction.

How It Works

  1. Intent Recognition: The AI model analyzes user input to determine whether an external function needs to be executed
  2. Parameter Extraction: Extracts the parameters required to execute the function from the user’s message
  3. Function Call Generation: Generates a structured function call request
  4. Execution and Response: The system executes the function and returns the result, which the AI model integrates into its response

Main Application Scenarios

  • Information Query: Real-time data retrieval such as weather, stocks, flights
  • System Operations: Smart home control, sending emails, creating calendar events
  • Business Processing: Order queries, payment processing, booking services
  • Data Analysis: Report generation, data visualization, trend prediction

Implementation Methods

  1. Declarative Function Definition: Developers pre-define available functions and their parameters
  2. Dynamic Decision: The AI model decides whether to call a function based on context
  3. Structured Response: Use formats like JSON to pass function call requests
  4. Security Verification: Ensure only authorized functions can be called

Advantages and Challenges

  • Advantages: Expand AI capability boundaries, provide more precise services, achieve task automation, improve user experience
  • Challenges: Requires precise intent recognition, parameter extraction may fail, need to handle API call failures, security risks exist

Future Development

  • Multi-step function call chains
  • Dynamic function discovery and registration
  • Adaptive parameter extraction
  • Safer execution environments

Background

Function Calling is a powerful API extension mechanism that allows users to flexibly integrate and call external functions when using large language models (such as GPT-3.5, GPT-4, etc.).

Specific workflow:

  1. User submits a query with specific requirements to the language model
  2. Model identifies the external function that needs to be called
  3. System executes the function and returns structured data
  4. Model integrates the function result to generate a natural language response

Install Dependencies

pip install -qU langchain-core langchain-openai

Write Code

Four basic math operation classes (Add, Subtract, Multiply, Divide) are defined, using Pydantic’s BaseModel for parameter definition and validation. Tools are bound to the LLM using llm.bind_tools().

from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI

class Multiply(BaseModel):
    """Multiply two integers together."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")

class Add(BaseModel):
    """Add two integers together."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")

class Subtract(BaseModel):
    """Subtract two integers."""
    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")

llm = ChatOpenAI(model="gpt-3.5-turbo", temperature=0)
llm_with_tools = llm.bind_tools([Add, Multiply, Subtract])

message1 = llm_with_tools.invoke("what's 3 * 12")
message2 = llm_with_tools.invoke("what's 3 + 12")
message3 = llm_with_tools.invoke("what's 3 - 12")

Running Results

  • “3 * 12” → Returns result directly: “3 * 12 is equal to 36.”
  • “3 + 12” → Returns result directly: “The result of 3 + 12 is 15.”
  • “3 - 12” → Triggers tool call, returns tool_calls

Convert Functions

Use convert_to_openai_tool to convert functions to JSON format:

import json
from langchain_core.utils.function_calling import convert_to_openai_tool

def multiply(a: int, b: int) -> int:
    """Multiply two integers together."""
    return a * b

print(json.dumps(convert_to_openai_tool(multiply), indent=2))

The output is standard OpenAI function calling format JSON, containing name, description, and parameters definitions.