Melange
LLM Inference

Function Calling

Connect ZeticMLange LLM output to app-defined tools on Android, iOS, and Flutter.

Function calling lets an on-device LLM ask your app to run a tool and feed the tool result back into the same generation session. Use it for app-local actions such as search, settings lookup, inventory checks, or deterministic calculations.

This page reflects ZeticMLange 1.9.0 on Android and iOS, and zetic_mlange 1.9.1 on Flutter.

Tool Shape

Each tool has a name, description, and JSON parameter schema. The model receives the tool list during run(...); when it emits a tool call, the SDK invokes your registered executor and continues the tool session with the returned content.

val weatherTool = LLMToolSpec(
    name = "get_weather",
    description = "Get the weather for a city.",
    parametersJson = """
        {
          "type": "object",
          "properties": {
            "city": { "type": "string" }
          },
          "required": ["city"]
        }
    """.trimIndent(),
)

model.registerTool(weatherTool) { call ->
    LLMToolResult(content = """{"summary":"Sunny, 24C"}""")
}
let weatherTool = LLMToolSpec(
  name: "get_weather",
  description: "Get the weather for a city.",
  parametersJson: """
  {
    "type": "object",
    "properties": {
      "city": { "type": "string" }
    },
    "required": ["city"]
  }
  """
)

try model.registerTool(weatherTool) { call in
  LLMToolResult(content: #"{"summary":"Sunny, 24C"}"#)
}
final weatherTool = LLMToolSpec(
  name: 'get_weather',
  description: 'Get the weather for a city.',
  parametersJson: '''
{
  "type": "object",
  "properties": {
    "city": { "type": "string" }
  },
  "required": ["city"]
}
''',
);

model.registerTool(weatherTool, (call) {
  return const LLMToolResult(content: '{"summary":"Sunny, 24C"}');
});

Run With Tools

Register tools before calling run(...). The same run(...) API is used for normal text generation and tool-enabled generation.

model.functionCallingSystemPrompt =
    "Use tools only when they are needed. Explain the final answer clearly."

val result = model.run("What is the weather in Seoul?")
while (true) {
    val next = model.waitForNextToken()
    if (next.isFinal || next.token.isEmpty()) break
    append(next.token)
}
model.functionCallingSystemPrompt =
  "Use tools only when they are needed. Explain the final answer clearly."

let result = try model.run("What is the weather in Seoul?")
while true {
  let next = model.waitForNextToken()
  if next.isFinished { break }
  append(next.token)
}
model.functionCallingSystemPrompt =
    'Use tools only when they are needed. Explain the final answer clearly.';

final result = model.run('What is the weather in Seoul?');
while (true) {
  final next = model.waitForNextToken();
  if (next.isFinished) break;
  append(next.token);
}

Tool Management

PlatformRegisterRemoveClearList
AndroidregisterTool(spec, executor)unregisterTool(name)clearTools()registeredTools()
iOSregisterTool(_:executor:)unregisterTool(name:)clearTools()registeredToolSpecs()
FlutterregisterTool(spec, executor)unregisterTool(name)clearTools()registeredTools()

Keep executors fast and deterministic. Flutter tool executors are synchronous, so prepare asynchronous data before calling run(...) or return an error-shaped tool result when data is unavailable.

API Reference

On this page