Skip to content

Extremely minimal Compose Multiplatform sample that demonstrates use of on-device AI on iOS and Android.

Notifications You must be signed in to change notification settings

joreilly/OnDeviceAI

Repository files navigation

Extremely minimal Compose Multiplatform sample that demonstrates use of on-device AI on iOS and Android. Specifically it uses Foundation Model Framework on iOS and MLKit Prompt API on Android.

In following codePromptApi is a Kotlin interface that's implemented by a Swfit class on iOS and a Kotlin one on Android. The Swift implementation is passed down to shared KMP code and invoked from the Compose Mutlplatform code if running on iOS.

Right now the respective iOS and Android implementations are assigned to a top level promptApi variable. This is a somewhat hacky solution for now for the sake of simplicity....there's a similar requirement in many of the other samples I have and in those cases the implementation is set in such a way that it becomes part of the object graph of particular DI framework being used.

iOS (Swift)

import FoundationModels

class PromptApiIos: PromptApi {
    
    func generateContent(prompt: String) async throws -> String? {
        let session = LanguageModelSession()
        let response = try await session.respond(to: prompt)
        return response.content
    }
}

Android (Kotlin)

import com.google.mlkit.genai.prompt.Generation

class PromptApiAndroid: PromptApi {
    private val generativeModel = Generation.getClient()

    override suspend fun generateContent(prompt: String): String? {
        val response = generativeModel.generateContent(prompt)
        return response.candidates.firstOrNull()?.text
    }
}

About

Extremely minimal Compose Multiplatform sample that demonstrates use of on-device AI on iOS and Android.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published