FunctionGemma 270M

FunctionGemma 270M 是 Gemma 3 的衍生模型,针对结构化工具与函数调用进行了微调。它不再输出自由形式的文本,而是输出严格的 <start_function_call>call:NAME{...}<end_function_call> 语法,SDK 将其解析为带类型的 FunctionCall 值。在磁盘上仅约 283 MB,体积足够小,可以与 ASR + TTS 流水线一同加载在手机级硬件上,并充当将用户话语转换为工具调用的"路由器"。

端侧函数调用

FunctionGemma 可以接入语音代理中原本需要调用托管 LLM 进行工具路由的位置。该语法在构造上是严格的,因此你直接得到已解析的 FunctionCall 对象——无需 JSON 修复,也无需 schema 模式提示。

平台

平台格式大小HuggingFace
Apple (macOS / iOS)CoreML约 283 MBaufklarer/FunctionGemma-270M-CoreML
Android(以及通过 Speech Core 支持的 Linux / Windows)LiteRT-LM约 283 MBsoniqo/FunctionGemma-270M-LiteRT-LM

语法

模型经过训练,会输出由两个 sentinel token 包裹的单个调用(或一系列调用):

<start_function_call>call:set_timer{"minutes": 5, "label": "tea"}<end_function_call>

SDK 将每个调用解析为带类型的 FunctionCall(name:, arguments:) 值。参数按 JSON 解码,因此你可以将它们直接映射到 Swift 的 Codable 或 Kotlin 的 @Serializable 数据类。

Swift(Apple, CoreML)

在 Apple 平台上,FunctionGemma 通过 speech-swiftFunctionGemma 类的形式暴露。它在首次使用时从 HuggingFace 加载 CoreML 模型,并运行在 Neural Engine 上。

import FunctionGemma

let model = try await FunctionGemma.fromPretrained()

let tools = """
- set_timer(minutes: Int, label: String)
- get_weather(city: String)
"""

let calls = try model.callFunctions(
    tools: tools,
    userMessage: "Set a 5 minute tea timer"
)

for call in calls {
    print(call.name)       // "set_timer"
    print(call.arguments)  // ["minutes": 5, "label": "tea"]
}

Kotlin(Android, LiteRT-LM)

在 Android 上,模型通过 speech-androidaudio.soniqo.speech.llm.FunctionGemma 暴露。它是一个自带运行时的适配器:你提供一个 LiteRtLmRuntime 实例(SDK 提供了默认实现),由 FunctionGemma 处理提示模板与语法解析。

import audio.soniqo.speech.llm.FunctionGemma
import audio.soniqo.speech.llm.LiteRtLmRuntime

val runtime = LiteRtLmRuntime.fromPretrained(context)
val model = FunctionGemma(runtime)

val tools = """
- set_timer(minutes: Int, label: String)
- get_weather(city: String)
""".trimIndent()

val calls = model.callFunctions(
    tools = tools,
    userMessage = "Set a 5 minute tea timer",
)

for (call in calls) {
    println(call.name)       // "set_timer"
    println(call.arguments)  // {"minutes": 5, "label": "tea"}
}

延伸阅读