开始使用
Copy a setup prompt with the install steps and the full markdown guide for this plugin.
Set up this Capacitor plugin in the project.
Use the package manager already used by the project.
Install these package(s): `@capgo/capacitor-llm`
Run the required Capacitor sync/update step after installation.
Read this markdown guide for the full setup steps: https://raw.githubusercontent.com/Cap-go/website/refs/heads/main/apps/docs/src/content/docs/zh/docs/plugins/llm/getting-started.mdx
Use that guide for platform-specific steps, native file edits, permissions, config changes, imports, and usage setup.
If that guide references other docs pages, read them too.
npm install @capgo/capacitor-llmnpx cap syncyarn add @capgo/capacitor-llmnpx cap syncpnpm add @capgo/capacitor-llmnpx cap syncbun add @capgo/capacitor-llmnpx cap synciOS 配置
Section titled “iOS 配置”- iOS 26.0+:默认使用 Apple Intelligence(无需模型)- 推荐
- iOS < 26.0:需要 MediaPipe 自定义模型(实验性,可能存在兼容性问题)
对于旧版 iOS 上的自定义模型,请通过 Xcode 的”Copy Bundle Resources”将模型文件放置在 iOS 应用包中。
Android 配置
Section titled “Android 配置”将模型文件放置在 Android assets 文件夹中:
android/app/src/main/assets/Android 需要两个文件:
.task文件(主模型).litertlm文件(配套文件)
从 Kaggle Gemma models 下载 → “LiteRT (formerly TFLite)” 标签
适用于 Android(Gemma-3 模型)
Section titled “适用于 Android(Gemma-3 模型)”- Gemma 3 270M - 最小、最高效的移动设备模型(约 240-400MB)- 推荐
- Gemma 3 1B - 较大的文本生成模型(约 892MB-1.5GB)
从 Kaggle Gemma models 下载 → 点击 “LiteRT (formerly TFLite)” 标签
适用于 iOS
Section titled “适用于 iOS”- Apple Intelligence(iOS 26.0+)- 内置,无需下载 - 推荐
- Gemma-2 2B(实验性)-
.task格式可能存在兼容性问题
对于自定义 iOS 模型,从 Hugging Face MediaPipe models 下载
导入插件并初始化:
import { CapgoLLM } from '@capgo/capacitor-llm';import { Capacitor } from '@capacitor/core';
// 检查 LLM 是否就绪const { readiness } = await CapgoLLM.getReadiness();console.log('LLM readiness:', readiness);
// 根据平台设置模型const platform = Capacitor.getPlatform();if (platform === 'ios') { // iOS:使用 Apple Intelligence(默认) await CapgoLLM.setModel({ path: 'Apple Intelligence' });} else { // Android:使用 MediaPipe 模型 await CapgoLLM.setModel({ path: '/android_asset/gemma-3-270m-it-int8.task' });}
// 创建聊天会话const { id: chatId } = await CapgoLLM.createChat();
// 监听 AI 响应CapgoLLM.addListener('textFromAi', (event) => { console.log('AI response:', event.text);});
// 监听完成CapgoLLM.addListener('aiFinished', (event) => { console.log('AI completed response');});
// 发送消息await CapgoLLM.sendMessage({ chatId, message: 'Hello! How are you today?'});// 从 URL 下载模型await CapgoLLM.downloadModel({ url: 'https://example.com/model.task', filename: 'model.task'});
// 对于 Android,同时下载 .task 和 .litertlm 文件await CapgoLLM.downloadModel({ url: 'https://example.com/gemma-3-270m-it-int8.task', companionUrl: 'https://example.com/gemma-3-270m-it-int8.litertlm', filename: 'gemma-3-270m-it-int8.task'});
// 监听下载进度CapgoLLM.addListener('downloadProgress', (event) => { console.log(`Download progress: ${event.progress}%`); console.log(`Downloaded: ${event.downloadedBytes} / ${event.totalBytes}`);});// 使用配置设置特定模型await CapgoLLM.setModel({ path: '/android_asset/gemma-3-270m-it-int8.task', maxTokens: 2048, topk: 40, temperature: 0.8});
// 检查就绪状态const { readiness } = await CapgoLLM.getReadiness();if (readiness === 'ready') { // 模型已加载并准备就绪}
// 监听就绪状态变化CapgoLLM.addListener('readinessChange', (event) => { console.log('Readiness changed:', event.readiness);});API 方法
Section titled “API 方法”createChat()
Section titled “createChat()”创建新的聊天会话。
const { id: chatId } = await CapgoLLM.createChat();返回: Promise<{ id: string; instructions?: string }>
sendMessage(…)
Section titled “sendMessage(…)”向 LLM 发送消息。
await CapgoLLM.sendMessage({ chatId: 'chat-id', message: 'What is the weather like?'});| 参数 | 类型 | 描述 |
|---|---|---|
chatId | string | 聊天会话 ID |
message | string | 要发送的消息 |
getReadiness()
Section titled “getReadiness()”检查 LLM 是否准备好使用。
const { readiness } = await CapgoLLM.getReadiness();返回: Promise<{ readiness: string }>
可能的值:
ready- 模型已加载并准备就绪loading- 模型正在加载not_ready- 模型尚未加载error- 加载模型时出错
setModel(…)
Section titled “setModel(…)”设置模型配置。
// iOS:使用 Apple Intelligence(推荐)await CapgoLLM.setModel({ path: 'Apple Intelligence'});
// iOS:使用自定义 MediaPipe 模型(实验性)await CapgoLLM.setModel({ path: 'Gemma2-2B-IT_multi-prefill-seq_q8_ekv1280', modelType: 'task', maxTokens: 1280});
// Android:使用 MediaPipe 模型await CapgoLLM.setModel({ path: '/android_asset/gemma-3-270m-it-int8.task', maxTokens: 2048, topk: 40, temperature: 0.8});| 参数 | 类型 | 描述 |
|---|---|---|
path | string | 模型路径或 iOS 系统的”Apple Intelligence” |
modelType | string | 可选:模型文件类型(例如 “task”、“bin”) |
maxTokens | number | 可选:模型处理的最大 token 数 |
topk | number | 可选:每步考虑的 token 数 |
temperature | number | 可选:生成的随机性(0.0-1.0) |
randomSeed | number | 可选:生成的随机种子 |
downloadModel(…)
Section titled “downloadModel(…)”从 URL 下载模型并保存到设备存储。
await CapgoLLM.downloadModel({ url: 'https://example.com/gemma-3-270m-it-int8.task', companionUrl: 'https://example.com/gemma-3-270m-it-int8.litertlm', filename: 'gemma-3-270m-it-int8.task'});| 参数 | 类型 | 描述 |
|---|---|---|
url | string | 下载的 URL |
companionUrl | string | 可选:配套文件的 URL(.litertlm) |
filename | string | 可选:保存的文件名 |
返回: Promise<{ path: string; companionPath?: string }>
textFromAi
Section titled “textFromAi”当 AI 生成文本时触发(流式响应)。
CapgoLLM.addListener('textFromAi', (event) => { console.log('AI text:', event.text); console.log('Chat ID:', event.chatId); console.log('Is chunk:', event.isChunk);});事件数据:
text(string) - 来自 AI 的增量文本块chatId(string) - 聊天会话 IDisChunk(boolean) - 这是完整块还是部分流数据
aiFinished
Section titled “aiFinished”当 AI 完成响应时触发。
CapgoLLM.addListener('aiFinished', (event) => { console.log('Completed for chat:', event.chatId);});事件数据:
chatId(string) - 聊天会话 ID
downloadProgress
Section titled “downloadProgress”在模型下载期间触发以报告进度。
CapgoLLM.addListener('downloadProgress', (event) => { console.log('Progress:', event.progress, '%'); console.log('Downloaded:', event.downloadedBytes, '/', event.totalBytes);});事件数据:
progress(number) - 完成的下载百分比(0-100)downloadedBytes(number) - 到目前为止下载的字节数totalBytes(number) - 要下载的总字节数
readinessChange
Section titled “readinessChange”当 LLM 的就绪状态发生变化时触发。
CapgoLLM.addListener('readinessChange', (event) => { console.log('Readiness changed to:', event.readiness);});事件数据:
readiness(string) - 新的就绪状态
import { CapgoLLM } from '@capgo/capacitor-llm';import { Capacitor } from '@capacitor/core';
class AIService { private chatId: string | null = null; private messageBuffer: string = '';
async initialize() { // 根据平台设置模型 const platform = Capacitor.getPlatform();
if (platform === 'ios') { // iOS:使用 Apple Intelligence(推荐) await CapgoLLM.setModel({ path: 'Apple Intelligence' }); } else { // Android:使用 MediaPipe 模型 await CapgoLLM.setModel({ path: '/android_asset/gemma-3-270m-it-int8.task', maxTokens: 2048, topk: 40, temperature: 0.8 }); }
// 等待模型准备就绪 let isReady = false; while (!isReady) { const { readiness } = await CapgoLLM.getReadiness(); if (readiness === 'ready') { isReady = true; } else if (readiness === 'error') { throw new Error('Failed to load model'); } await new Promise(resolve => setTimeout(resolve, 500)); }
// 创建聊天会话 const { id } = await CapgoLLM.createChat(); this.chatId = id;
// 设置事件监听器 this.setupListeners(); }
private setupListeners() { CapgoLLM.addListener('textFromAi', (event) => { if (event.chatId === this.chatId) { this.messageBuffer += event.text; this.onTextReceived(event.text); } });
CapgoLLM.addListener('aiFinished', (event) => { if (event.chatId === this.chatId) { this.onMessageComplete(this.messageBuffer); this.messageBuffer = ''; } }); }
async sendMessage(message: string) { if (!this.chatId) { throw new Error('Chat not initialized'); }
await CapgoLLM.sendMessage({ chatId: this.chatId, message }); }
onTextReceived(text: string) { // 使用流式文本更新 UI console.log('Received:', text); }
onMessageComplete(fullMessage: string) { // 处理完整消息 console.log('Complete message:', fullMessage); }}
// 用法const ai = new AIService();await ai.initialize();await ai.sendMessage('Tell me about AI');| 平台 | 支持 | 要求 |
|---|---|---|
| iOS | ✅ | iOS 13.0+(26.0+ 用于 Apple Intelligence) |
| Android | ✅ | API 24+ |
| Web | ❌ | 不支持 |
-
模型选择:根据设备能力选择模型
- 对于大多数移动设备使用 270M
- 对于具有更多 RAM 的高端设备使用 1B
- 在目标设备上测试性能
-
内存管理:完成后清除聊天会话
// 为新对话创建新聊天const { id } = await CapacitorLLM.createChat(); -
错误处理:在使用前始终检查就绪状态
const { readiness } = await CapacitorLLM.getReadiness();if (readiness !== 'ready') {// 处理未就绪状态} -
流式 UI:使用流式文本增量更新 UI
- 通过
onAiText显示文本到达 - 使用
onAiCompletion标记完成
- 通过
-
模型下载:在应用设置期间下载模型,而不是首次使用时
// 在应用初始化期间await CapacitorLLM.downloadModel({url: 'https://your-cdn.com/model.task',filename: 'model.task'});
- 验证模型文件在正确的位置
- 检查模型格式是否与平台匹配(iOS 为 .gguf,Android 为 .task)
- 确保设备存储空间充足
- 尝试较小的模型(270M 而不是 1B)
- 关闭其他应用以释放内存
- 在实际设备而不是模拟器上测试
- 检查就绪状态是否为 ‘ready’
- 验证在发送消息前设置了事件监听器
- 检查控制台是否有错误