ExLLM.FunctionCalling (ex_llm v0.5.0)

View Source

Function calling support for ExLLM.

Provides a unified interface for function/tool calling across different LLM providers. Each provider has slightly different implementations, but this module normalizes them into a consistent API.

Features

  • Unified function definition format
  • Automatic parameter validation
  • Type conversion and coercion
  • Function execution with safety controls
  • Streaming function call support

Usage

# Define available functions
functions = [
  %{
    name: "get_weather",
    description: "Get the current weather for a location",
    parameters: %{
      type: "object",
      properties: %{
        location: %{type: "string", description: "City and state"},
        unit: %{type: "string", enum: ["celsius", "fahrenheit"]}
      },
      required: ["location"]
    }
  }
]

# Chat with function calling
{:ok, response} = ExLLM.chat(:openai, messages,
  functions: functions,
  function_call: "auto"  # or "none" or %{name: "specific_function"}
)

# Handle function calls in response
case response do
  %{function_call: %{name: name, arguments: args}} ->
    result = execute_function(name, args)
    # Continue conversation with function result

  %{content: content} ->
    # Normal response
end

Summary

Functions

Executes a function call safely.

Converts unified function format to provider-specific format.

Formats function results for conversation continuation.

Normalizes a single function to unified format.

Converts provider-specific function format to unified format.

Parse function call arguments from JSON string to map.

Parses function calls from LLM response.

Validates function call arguments against schema.

Functions

execute_function(function_call, available_functions)

Executes a function call safely.

format_for_provider(functions, provider)

Converts unified function format to provider-specific format.

format_function_result(result, provider)

Formats function results for conversation continuation.

normalize_function(func, provider)

Normalizes a single function to unified format.

normalize_functions(functions, provider)

Converts provider-specific function format to unified format.

parse_arguments(args)

@spec parse_arguments(String.t() | map()) :: {:ok, map()} | {:error, atom()}

Parse function call arguments from JSON string to map.

Parameters

  • arguments - JSON string or map containing function arguments

Returns

  • {:ok, args_map} on success
  • {:error, reason} on failure

Examples

{:ok, args} = ExLLM.FunctionCalling.parse_arguments("{"location": "NYC"}")
# => {:ok, %{"location" => "NYC"}}

parse_function_calls(response, provider)

Parses function calls from LLM response.

validate_arguments(function_call, function_schema)

Validates function call arguments against schema.