-
- All Implemented Interfaces:
-
io.getstream.chat.android.ai.ondevice.AILocalModel,java.lang.AutoCloseable
public final class AIOnDeviceModel implements AILocalModel, AutoCloseable
Gemini Nano, the model in Android's AICore, through ML Kit's GenAI Prompt API. It answers without a network connection, and the conversation never leaves the device.
It runs on devices whose AICore offers Gemini Nano (recent Pixel, Samsung and other flagship phones), not on emulators. The model may need downloading first: status says whether it is Status.Downloadable or Status.Downloading, and download starts it.
It is a small model, good for short answers and for working with what is in the conversation. Its context holds a few thousand tokens, so the oldest turns of a long conversation are left out.
val model = AIOnDeviceModel(context) if (model.isAvailable()) { model.reply(instructions, turns).collect { answer -> show(answer) } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description public enumAIOnDeviceModel.StatusWhere the model is on this device.
public abstract classAIOnDeviceModel.DownloadHow a download of the model is going. More states may be added.
public final classAIOnDeviceModel.UnavailableThrown by reply when the model can't answer on this device.
public final classAIOnDeviceModel.FailureThrown by reply when the model fails while answering.
public classAIOnDeviceModel.Companion
-
Field Summary
Fields Modifier and Type Field Description private final IntegermaximumResponseTokens
-
Constructor Summary
Constructors Constructor Description AIOnDeviceModel(Context context, Integer maximumResponseTokens)
-
Method Summary
Modifier and Type Method Description final IntegergetMaximumResponseTokens()final AIOnDeviceModel.Statusstatus()Where the model is on this device. BooleanisAvailable()Whether the model can answer now. final Flow<AIOnDeviceModel.Download>download()Downloads the model, when status is Status.Downloadable. Flow<String>reply(String instructions, List<AIConversationTurn> turns)Streams the answer to the last of turns. Unitclose()Releases the model's resources. -
-
Method Detail
-
getMaximumResponseTokens
final Integer getMaximumResponseTokens()
-
status
final AIOnDeviceModel.Status status()
Where the model is on this device. A device without AICore, or an error, reads as unavailable.
-
isAvailable
Boolean isAvailable()
Whether the model can answer now.
-
download
final Flow<AIOnDeviceModel.Download> download()
Downloads the model, when status is Status.Downloadable. The download continues while the flow is collected.
-
reply
Flow<String> reply(String instructions, List<AIConversationTurn> turns)
Streams the answer to the last of turns. Each emission is the whole answer so far, and cancelling the collection stops the model.
- Parameters:
instructions- How the model should answer: who it is, what it can't do.turns- The conversation so far, oldest first, ending with the person's question.
-
-
-
-