メインコンテンツへスキップ

スタートガイド

GitHub

AIアシストによるセットアップでプラグインをインストールできます。以下のコマンドを使用して、お使いのAIツールに Capgo のスキルを追加してください:

ターミナル
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

次に、以下のプロンプトを使用します:

Use the `capacitor-plugins` skill from `Cap-go/capgo-skills` to install the `@capgo/capacitor-llm` plugin in my project.

手動セットアップを希望する場合は、以下のコマンドを実行してプラグインをインストールし、下記のプラットフォーム別の手順に従ってください:

ターミナル
bun add @capgo/capacitor-llm
bunx cap sync
import { CapgoLLM } from '@capgo/capacitor-llm';
import { CapgoLLM } from '@capgo/capacitor-llm';
const { id: chatId } = await CapgoLLM.createChat();
await CapgoLLM.sendMessage({ chatId, message: 'Summarize this error log.' });

を監視します: textFromAi, aiFinishedや generationError プラグインのREADMEに記載されているとおり、チャットセッションに対して行います。

const { readiness } = await CapgoLLM.getReadiness();
if (readiness !== 'ready') {
showModelDownloadScreen();
}

例:カスタムモデルのダウンロード

「例:カスタムモデルをダウンロード」セクション
await CapgoLLM.downloadModel({ url: 'https://cdn.example.com/model.litertlm' });
  • Web オフライン:MediaPipe GenAI は CDN からロードされるため、オフラインデモ用にランタイムとモデルをキャッシュしてください。
  • Android Gemini Nano:すべてのデバイスで利用できるわけではないため、フォールバックモデルを実装してください。
  • ストリーミング:事前にリスナーをアタッチして、 sendMessage トークンの取りこぼしを防いでください。

新しいチャットセッションを作成します

import { CapgoLLM } from '@capgo/capacitor-llm';
const result = await CapgoLLM.createChat();
console.log(result);

特定のチャットセッションでAIにメッセージを送信します

import { CapgoLLM } from '@capgo/capacitor-llm';
await CapgoLLM.sendMessage({
chatId: 'chat-id-123',
message: 'Hello from Capacitor',
});

LLMの準備完了ステータスを取得します

import { CapgoLLM } from '@capgo/capacitor-llm';
const result = await CapgoLLM.getReadiness();
console.log(result);

モデルの設定を行います

  • iOS: システムモデルのパスとして「Apple Intelligence」を使用するか、MediaPipeモデルへのパスを指定します
  • Android: MediaPipeモデルファイルへのパス(assetsまたはfilesディレクトリ内)
import { CapgoLLM } from '@capgo/capacitor-llm';
await CapgoLLM.setModel({
path: 'path/to/file',
modelType: 'task',
});

URLからモデルをダウンロードし、適切な場所に保存します

  • iOS: アプリのdocumentsディレクトリにダウンロードします
  • Android: アプリのfilesディレクトリにダウンロードします
import { CapgoLLM } from '@capgo/capacitor-llm';
const result = await CapgoLLM.downloadModel({ url: 'https://example.com' });
console.log(result);

モデルの設定オプション

export interface ModelOptions {
/** Model path or "Apple Intelligence" for iOS system model */
path: string;
/** Model file type/extension (e.g., "task", "bin", "litertlm"). If not provided, will be extracted from path. */
modelType?: string;
/** Maximum number of tokens the model handles */
maxTokens?: number;
/** Number of tokens the model considers at each step */
topk?: number;
/** Amount of randomness in generation (0.0-1.0) */
temperature?: number;
/** Random seed for generation */
randomSeed?: number;
}

モデルをダウンロードするためのオプション。

export interface DownloadModelOptions {
/** URL of the model file to download */
url: string;
/** Optional: URL of companion file (e.g., .litertlm for Android) */
companionUrl?: string;
/** Optional: Custom filename (defaults to filename from URL) */
filename?: string;
}

モデルのダウンロード結果。

export interface DownloadModelResult {
/** Path where the model was saved */
path: string;
/** Path where the companion file was saved (if applicable) */
companionPath?: string;
}

AIから受信したテキストのイベントデータ。

export interface TextFromAiEvent {
/** The text content from AI - this is an incremental chunk, not the full text */
text: string;
/** The chat session ID */
chatId: string;
/** Whether this is a complete chunk (true) or partial streaming data (false) */
isChunk?: boolean;
}

AIの処理完了時のイベントデータ。

export interface AiFinishedEvent {
/** The chat session ID that finished */
chatId: string;
}

ダウンロードの進捗状況に関するイベントデータ。

export interface DownloadProgressEvent {
/** Percentage of download completed (0-100) */
progress: number;
/** Total bytes to download */
totalBytes?: number;
/** Bytes downloaded so far */
downloadedBytes?: number;
}

準備完了ステータスの変更に関するイベントデータ。

export interface ReadinessChangeEvent {
/** The readiness status */
readiness: string;
}

このページはプラグインの src/definitions.tsから生成されています。upstream で公開 API が変更されたら、再度 sync を実行してください。

Getting Startedの続きを進める

「Getting Started からの続き」セクション

使用している場合 スタートガイド ダッシュボードと API の運用を計画するには、これを次と連携させます @capgo/capacitor-llm の使用方法 @capgo/capacitor-llm を使用して Native Build の機能を利用する場合、 API の概要 「API の概要」の実装の詳細については、 はじめに 「はじめに」における実装の詳細については、 API キー および「API Keys」の実装の詳細については、 デバイス 実装の詳細については Devices を参照してください。