ExLLM.FunctionCalling (ex_llm v0.5.0)
View SourceFunction 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
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.
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"}}
Parses function calls from LLM response.
Validates function call arguments against schema.