RunanywhereAI/runanywhere-sdks · 上手攻略
- 仓库:RunanywhereAI/runanywhere-sdks
- 链接:https://github.com/RunanywhereAI/runanywhere-sdks
- 分类:ai · on-device · mobile-sdk
- 作者:Tom
- 更新:2026-07-15
这是什么
RunAnywhere 是一个端侧 AI 推理 SDK 套件,让你在 iOS、Android、Flutter、React Native 和浏览器里直接跑 LLM、VLM、语音识别(TTS/STT)和语音合成,无需联网、数据不离设备。它用同一套 API 屏蔽了各平台的底层差异:Apple 设备走 Core ML / Metal,安卓走 llama.cpp 或高通 Hexagon NPU(QHexRT),浏览器走 WebGPU / WebAssembly。
核心理念:把 AI 模型当成「本地引擎」而不是「云服务」来调用。
解决什么问题
- 隐私敏感场景:医疗、金融、律师等需要数据留本地的场景
- 离线可用性:网络不稳定或无网环境(如车载、野外设备)
- 低延迟:省去网络往返,本地首 token 延迟可低至 32ms(Galaxy S25 实测)
- 跨平台一致性:同一套代码逻辑(初始化 → 加载模型 → 生成)覆盖 5 个平台
快速安装
iOS(Swift Package Manager)
# Xcode 中添加 SPM 依赖
# Repository: https://github.com/RunanywhereAI/runanywhere-sdks
import RunAnywhere
import LlamaCPPRuntime
// 1. 初始化
LlamaCPP.register()
try RunAnywhere.initialize()
// 2. 加载模型
var load = RAModelLoadRequest()
load.modelID = "smollm2-360m"
load.category = .language
load.framework = .llamaCpp
_ = await RunAnywhere.loadModel(load)
// 3. 生成
var req = RALLMGenerateRequest()
req.prompt = "What is the capital of France?"
let result = try await RunAnywhere.generate(req)
print(result.text) // "Paris is the capital of France."
iOS/macOS 要求 iOS 17.0+ / macOS 14.0+。
Android(Kotlin / Gradle)
// build.gradle.kts
dependencies {
implementation("com.runanywhere.sdk:runanywhere-kotlin:0.16.1")
implementation("com.runanywhere.sdk:runanywhere-core-llamacpp:0.16.1")
}
import com.runanywhere.sdk.public.RunAnywhere
import com.runanywhere.sdk.public.extensions.*
// 1. 初始化
LlamaCPP.register()
RunAnywhere.initialize(environment = SDKEnvironment.DEVELOPMENT)
// 2. 下载并加载模型
RunAnywhere.downloadModel("smollm2-360m").collect { println("${it.progress * 100}%") }
RunAnywhere.loadLLMModel("smollm2-360m")
// 3. 对话
val response = RunAnywhere.chat("What is the capital of France?")
println(response)
Android 要求 API 24+,推荐 API 28+;QHexRT NPU 加速仅支持骁龙 8 Elite(Hexagon v79/v81)安卓 arm64 设备。
Web(npm)
npm install @runanywhere/core @runanywhere/llamacpp
import { RunAnywhere, SDKEnvironment } from '@runanywhere/core';
import { LlamaCPP } from '@runanywhere/llamacpp';
// 1. 初始化
await RunAnywhere.initialize({ environment: SDKEnvironment.Development });
LlamaCPP.register();
// 2. 加载模型
await RunAnywhere.downloadModel('smollm2-360m');
await RunAnywhere.loadModel('smollm2-360m');
// 3. 生成
const response = await RunAnywhere.chat('What is the capital of France?');
console.log(response);
Web SDK 状态为 Beta,要求 Chrome 96+ / Edge 96+,推荐 Chrome 120+。
Flutter
# pubspec.yaml
dependencies:
runanywhere: ^0.16.0
runanywhere_llamacpp: ^0.16.0
import 'package:runanywhere/runanywhere.dart';
import 'package:runanywhere_llamacpp/runanywhere_llamacpp.dart';
await RunAnywhere.initialize();
await LlamaCpp.register();
await RunAnywhere.downloadModel('smollm2-360m');
await RunAnywhere.loadModel('smollm2-360m');
final response = await RunAnywhere.chat('What is the capital of France?');
print(response);
Flutter 要求 3.10+,推荐 3.24+。
React Native
npm install @runanywhere/core @runanywhere/llamacpp
import { RunAnywhere, SDKEnvironment } from '@runanywhere/core';
import { LlamaCPP } from '@runanywhere/llamacpp';
await RunAnywhere.initialize({ environment: SDKEnvironment.Development });
LlamaCPP.register();
await RunAnywhere.downloadModel('smollm2-360m');
await RunAnywhere.loadModel('smollm2-360m');
const response = await RunAnywhere.chat('What is the capital of France?');
console.log(response);
React Native 要求 0.74+,推荐 0.76+。
核心用法
模型选择策略
预编译模型包发布在 HuggingFace:https://huggingface.co/runanywhere/models
| 模型 | 任务 | 参数量 | Decode 速度(骁龙 8 Elite) | 首 token 延迟 |
|---|---|---|---|---|
| LFM2.5-230M | LLM | 230M | 164 tok/s | 32ms |
| Qwen3-0.6B | LLM | 600M | 33 tok/s | 127ms |
| Llama-3.2-1B | LLM | 1.2B | 16.3 tok/s | 56ms |
| Phi-tiny-MoE | MoE LLM | 3.8B(活跃 1.1B) | 5-7 tok/s | ~2.5s |
| InternVL3.5-1B | VLM | 1B | 37 tok/s | 290ms |
| Whisper base | ASR | 74M | ~5x 实时 | n/a |
| MeloTTS-EN | TTS | n/a | ~4.5x 实时 | n/a |
⚠️ 以上基准数据来自 Galaxy S25(骁龙 8 Elite,Hexagon v79),不同设备表现差异明显。NPU 加速仅限骁龙设备。
QHexRT NPU 加速(安卓)
QHexRT 是 RunAnywhere 的 Hexagon NPU 推理运行时,目前支持:
- LLM 推理(Phi-tiny-MoE、Qwen3.5 等)
- VLM 视觉理解
- STT(TTS 也在 NPU 上,部分竞品走 CPU)
使用方式与普通 llama.cpp 完全一致:加载时 SDK 自动检测设备能力并路由。
// NPU 自动选择,无需额外配置
RunAnywhere.loadLLMModel("phi_tiny_moe_HNPU")
完整语音助手管线
VAD → STT → LLM → TTS
全部本地运行,适合隐私敏感语音交互场景。
结构化输出 & Tool Calling
// 结构化输出(JSON)
req.responseFormat = .jsonSchema(mySchema)
let result = try await RunAnywhere.generate(req)
// Tool Calling(iOS/Android/Web)
req.tools = [myToolDef]
let result = try await RunAnywhere.generate(req)
⚠️ Tool Calling 在 iOS/Android/Web 已支持,Flutter 和 React Native 尚未支持(原文:Soon)。
典型适用场景
- 隐私优先的移动应用:医疗问诊、金融助手、法律咨询——数据不离设备
- 离线 AI 功能:野外数据采集、车载语音助手、工厂设备控制
- 低延迟对话:实时语音交互、即时翻译、客服机器人
- 跨境/弱网应用:无需 API 调用,不受网络质量影响
- App Store / Google Play 差异化:完全本地 AI 是主流应用商店的热门噱头
坑与注意
- NPU 兼容性窄:QHexRT 目前仅限骁龙 8 Elite(Hexagon v79/v81),其他安卓设备回落 llama.cpp CPU 推理,性能差距大
- 模型包下载依赖 HuggingFace:国内访问可能不稳定,需要自己代理或镜像
- Flutter/React Native 部分功能滞后:Tool Calling 和结构化输出尚未支持这两个平台
- 包体积:完整 SDK 含 llama.cpp 引擎,Android AAR 约 20-30MB,需按需引入模块
- iOS 本地模型需构建:Swift SDK 的本地二进制(XCFramework)需要运行构建脚本
build-core-xcframework.sh,不提供预编译托管 - 内存要求:最少 2GB,推荐 4GB+;大型模型(如 7B)在低端手机上不现实
与同类对比
| 特性 | RunAnywhere | WebLLM | llama.cpp 直接用 | MLX (Apple) |
|---|---|---|---|---|
| 跨平台覆盖 | iOS/Android/Web/Flutter/RN | 仅 Web | 全平台但需自己封装 | 仅 Apple |
| NPU 加速 | 骁龙 Hexagon ✅ | ❌ | 部分 | ❌ |
| 统一 API | ✅ | ✅(WebGPU) | ❌ | ❌ |
| 开源 | ✅ | ✅ | ✅ | ❌(Apple 闭源) |
| 语音管线内置 | ✅ | ❌ | ❌ | ❌ |
| 生产就绪度 | 较新(2024-) | 较新 | 成熟 | 成熟 |
| 模型生态 | 自有 HuggingFace 仓库 | HuggingFace | HuggingFace | MPS 模型 |
RunAnywhere 的核心优势是跨五平台的统一 API + 独家骁龙 NPU 加速,在移动端隐私 AI 场景比 WebLLM 更完整,比直接用 llama.cpp 开发体验好很多。劣势是相对年轻、文档和社区还在成长。
一句话推荐结论
如果你在开发需要完全本地 AI 能力的移动应用,且目标用户中有大量骁龙旗舰机用户,RunAnywhere 是目前跨平台覆盖最完整、NPU 加速最实在的选择——尤其适合隐私敏感场景和离线语音助手管线。
注意:本文基于 GitHub README 和官方文档(docs.runanywhere.ai)整理。SDK 版本号(Kotlin 0.16.1 / Flutter 0.16.0)为撰写时最新稳定版,建议在发布前通过 npm show @runanywhere/core version / gradle dependencies 等方式核实。QHexRT 加速效果与设备强相关,骁龙 8 Elite 以下机型请按 llama.cpp 回退预期。