Swift API LiteRT-LM memungkinkan Anda mengintegrasikan model bahasa besar secara native ke dalam aplikasi iOS dan macOS. Fitur seperti multimodalitas, penggunaan alat, dan akselerasi GPU (melalui Metal) didukung sepenuhnya.
Pengantar
Berikut adalah contoh penggunaan Swift API untuk menginisialisasi model dan mengirim pesan:
import LiteRTLM
// 1. Initialize the Engine with your model
let config = try EngineConfig(
modelPath: "path/to/model.litertlm",
backend: .gpu, // Use .cpu() for CPU execution
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: config)
try await engine.initialize()
// 2. Start a new Conversation
let conversation = try await engine.createConversation()
// 3. Send a message and print the response
let response = try await conversation.sendMessage(Message("What is the capital of France?"))
print(response.toString)
Memulai
Bagian ini memberikan petunjuk tentang cara mengintegrasikan LiteRT-LM Swift API ke dalam aplikasi Anda.
Swift Package Manager (SPM)
Anda dapat mengintegrasikan LiteRT-LM ke dalam project Xcode menggunakan Swift Package Manager.
- Buka project Anda di Xcode dan buka File > Add Package Dependencies...
- Masukkan URL repositori paket:
https://github.com/google-ai-edge/LiteRT-LM - Pilih library LiteRTLM untuk menambahkannya ke target aplikasi Anda.
Jika Anda mengembangkan paket menggunakan Package.swift, tambahkan ke dependensi Anda:
dependencies: [
.package(url: "https://github.com/google-ai-edge/LiteRT-LM", from: "0.12.0")
]
Panduan API Inti
Bagian ini menjelaskan komponen dan alur kerja mendasar untuk menggunakan LiteRT-LM Swift API, termasuk inisialisasi mesin, pengelolaan percakapan, dan pengiriman pesan.
Menginisialisasi Mesin
Engine menangani pemuatan model, alokasi resource, dan pengelolaan siklus proses.
import LiteRTLM
let engineConfig = try EngineConfig(
modelPath: "path/to/your/model.litertlm",
backend: .gpu, // Use .gpu for Metal hardware acceleration
maxNumTokens: 512, // Size of the KV-cache
cacheDir: NSTemporaryDirectory() // Writable directory for compilation cache
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
Membuat Percakapan
Conversation mengelola histori chat, petunjuk sistem, dan konfigurasi sampler.
// Configure custom sampling parameters
let samplerConfig = try SamplerConfig(
topK: 40,
topP: 0.95,
temperature: 0.7
)
// Create the conversation config with system instructions
let config = ConversationConfig(
systemMessage: Message("You are a helpful assistant."),
samplerConfig: samplerConfig
)
let conversation = try await engine.createConversation(with: config)
Mengirim Pesan
Anda dapat berinteraksi dengan model secara sinkron atau asinkron (streaming).
Contoh Sinkron
let response = try await conversation.sendMessage(Message("Hello!"))
print(response.toString)
Contoh Asinkron (Streaming)
let message = Message("Tell me a long story.")
for try await chunk in conversation.sendMessageStream(message) {
// Output response chunks in real-time
print(chunk.toString, terminator: "")
}
print()
Multimodalitas
Untuk menggunakan fitur visi atau audio, pastikan untuk mengonfigurasi backend khusus selama inisialisasi mesin.
let engineConfig = try EngineConfig(
modelPath: "path/to/multimodal_model.litertlm",
backend: .gpu,
visionBackend: .cpu(), // Enable CPU vision executor
audioBackend: .cpu(), // Enable CPU audio executor
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
Input Gambar (Visi)
Berikan gambar sebagai jalur atau byte mentah:
let imagePath = Bundle.main.path(forResource: "scenery", ofType: "jpg")!
let message = Message(contents: [
Content.imageFile(imagePath),
Content.text("Describe this image.")
])
let response = try await conversation.sendMessage(message)
print(response.toString)
Input Audio
Berikan jalur audio:
let audioPath = Bundle.main.path(forResource: "recording", ofType: "wav")!
let message = Message(contents: [
Content.audioFile(audioPath),
Content.text("Transcribe this recording.")
])
let response = try await conversation.sendMessage(message)
print(response.toString)
🔴 Baru: Prediksi Multi-Token (MTP)
Prediksi Multi-Token (MTP) adalah pengoptimalan performa yang mempercepat kecepatan dekode secara signifikan. Fitur ini direkomendasikan secara universal untuk semua tugas yang menggunakan backend GPU/Metal.
Untuk menggunakan MTP, aktifkan dekode spekulatif di flag eksperimental sebelum menginisialisasi mesin.
import LiteRTLM
// Opt into experimental APIs to configure MTP
ExperimentalFlags.optIntoExperimentalAPIs()
ExperimentalFlags.enableSpeculativeDecoding = true
let engineConfig = try EngineConfig(
modelPath: "path/to/model.litertlm",
backend: .gpu,
cacheDir: NSTemporaryDirectory()
)
let engine = Engine(engineConfig: engineConfig)
try await engine.initialize()
Menentukan dan Menggunakan Alat
Anda dapat menentukan struktur Swift sebagai alat yang dapat dipanggil secara otomatis oleh model untuk menjalankan logika.
- Sesuai dengan protokol
Tool. - Deklarasikan parameter menggunakan wrapper properti
@ToolParam. - Implementasikan metode
run().
import LiteRTLM
// 1. Define your custom tool
struct GetCurrentWeatherTool: Tool {
static let name = "get_current_weather"
static let description = "Get the current weather for a location."
@ToolParam(description: "The city and state, e.g. San Francisco, CA")
var location: String
@ToolParam(description: "The temperature unit to use (celsius or fahrenheit)")
var unit: String = "celsius"
func run() async throws -> Any {
// Call your weather API here
return [
"location": location,
"temperature": "22",
"unit": unit,
"condition": "sunny"
]
}
}
// 2. Register the tool in your conversation configuration
let config = ConversationConfig(
tools: [GetCurrentWeatherTool()]
)
let conversation = try await engine.createConversation(with: config)
// 3. The model will invoke the tool automatically if needed
let response = try await conversation.sendMessage(Message("What is the weather in Paris right now?"))
print(response.toString)