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 MB | aufklarer/FunctionGemma-270M-CoreML |
| Android(以及通过 Speech Core 支持的 Linux / Windows) | LiteRT-LM | 约 283 MB | soniqo/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-swift 以 FunctionGemma 类的形式暴露。它在首次使用时从 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-android 以 audio.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"}
}
延伸阅读
- Speech Core——在 Linux、Windows 和 Android 上承载 LiteRT-LM 运行时和函数调用循环的 C++ 引擎。
- github.com/soniqo/speech-core——编排核心与 LiteRT-LM 胶水代码,包含
VoicePipeline工具调用循环。 - github.com/soniqo/speech-swift——包含
FunctionGemmaSwift 类的 Apple SDK。 - github.com/soniqo/speech-android——包含
audio.soniqo.speech.llm.FunctionGemma的 Android SDK。 - google/gemma-3-270m——上游 Gemma 3 270M 基础模型,托管于 HuggingFace。