Zum Inhalt springen

Erste Schritte

GitHub

Du kannst unser KI-gestütztes Setup nutzen, um das Plugin zu installieren. Füge die Capgo Skills mit dem folgenden Befehl deinem KI-Tool hinzu:

Terminalfenster
npx skills add https://github.com/Cap-go/capgo-skills --skill capacitor-plugins

Nutze dann diesen Prompt:

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

Wenn du ein manuelles Setup bevorzugst, installiere das Plugin mit den folgenden Befehlen und befolge die plattformspezifischen Anweisungen unten:

Terminalfenster
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.' });

Lauscht auf textFromAi, aiFinished, und generationError in der Chat-Session, wie im Plugin-README dokumentiert.

const { readiness } = await CapgoLLM.getReadiness();
if (readiness !== 'ready') {
showModelDownloadScreen();
}
await CapgoLLM.downloadModel({ url: 'https://cdn.example.com/model.litertlm' });
  • Web offline: MediaPipe GenAI lädt von einem CDN, cache die Runtime und das Modell für Offline-Demos.
  • Android Gemini Nano: nicht auf jedem Gerät verfügbar, Fallback-Modelle behandeln.
  • Streaming: Listener vorher anhängen sendMessage damit Sie keine Tokens verpassen.

Erstellt eine neue Chat-Session

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

Sendet eine Nachricht an die KI in einer bestimmten Chat-Session

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

Ruft den Readiness-Status des LLM ab

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

Setzt die Modellkonfiguration

  • iOS: Verwenden Sie „Apple Intelligence“ als Pfad für das Systemmodell oder geben Sie den Pfad zum MediaPipe-Modell an
  • Android: Pfad zu einer MediaPipe-Modelldatei (im assets- oder files-Verzeichnis)
import { CapgoLLM } from '@capgo/capacitor-llm';
await CapgoLLM.setModel({
path: 'path/to/file',
modelType: 'task',
});

Lädt ein Modell von einer URL herunter und speichert es am entsprechenden Speicherort

  • iOS: Lädt in das documents-Verzeichnis der App herunter
  • Android: Lädt in das files-Verzeichnis der App herunter
import { CapgoLLM } from '@capgo/capacitor-llm';
const result = await CapgoLLM.downloadModel({ url: 'https://example.com' });
console.log(result);

Konfigurationsoptionen für das Modell.

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;
}

Optionen für den Download eines Modells.

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;
}

Ergebnis des Modell-Downloads.

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

Event-Daten für den von der KI empfangenen Text.

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;
}

Event-Daten für den Abschluss der KI.

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

Ereignisdaten für den Download-Fortschritt.

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

Ereignisdaten für Änderungen des Bereitstellungsstatus.

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

Diese Seite wird aus dem Plugin generiert src/definitions.ts. Führe den Sync erneut aus, wenn sich das öffentliche API upstream ändert.

Wenn du es nutzt Erste Schritte um Dashboard- und API-Operationen zu planen, verbinde es mit Verwendung von @capgo/capacitor-llm für die native Funktion in Verwendung von @capgo/capacitor-llm, API Übersicht für Details zur Implementierung im API Overview, Einführung für die Implementierungsdetails in der Einleitung, API Schlüssel für das Implementierungsdetail in API Keys und Geräte für das Implementierungsdetail in Devices.