Class SystemLlmClient
An LlmClient backed by the language model the operating system ships:
Apple's Foundation Models (Apple Intelligence) on iOS and macOS, and Gemini
Nano through ML Kit GenAI on Android. Create one with LlmClient.system().
Nothing leaves the device, nothing is bundled with the application, and the OS keeps the model up to date. In exchange the model is small, and whether it exists at all depends on the device, the OS version and the user's settings, so an application asks first:
SystemLlmClient llm = LlmClient.system();
SystemLlmStatus s = SystemLlmClient.getStatus(SystemLlmFeature.CHAT);
if (s == SystemLlmStatus.DOWNLOADABLE) {
llm.download(SystemLlmFeature.CHAT);
} else if (s.isAvailable()) {
llm.chat(ChatRequest.builder()
.addMessage(ChatMessage.user("Suggest a title for this note: ..."))
.build()).ready(r -> Log.p(r.getText()));
}
Each capability is answered separately (SystemLlmFeature): a device can
summarize without accepting images in chat. Besides chat(ChatRequest),
the client has summarize(String, int), proofread(String),
rewrite(String, RewriteStyle) and [#describeImage(byte[])]. On Android
these use ML Kit's dedicated task models; on Apple platforms they are
prompts to the same general model.
Availability
- iOS and macOS 26 or newer, on a device with Apple Intelligence turned
on. Images are not accepted. Older OS versions answer
SystemLlmStatus.UNSUPPORTED, as do applications built with an Xcode that predates Foundation Models. - Android 8 or newer, on a device AICore supports with Gemini Nano --
recent Pixel, Samsung Galaxy and other flagship phones. Using this class
raises the application's minimum SDK to 23; older
Android versions report
SystemLlmStatus.UNSUPPORTED. - Everywhere else, the simulator included, every feature is
SystemLlmStatus.UNSUPPORTEDand requests fail withLlmException.ErrorType.INVALID_REQUEST. UseLlmClient.ollama()to develop against a local model on the desktop.
The builders add the platform bridge only to applications that call
LlmClient.system(); an application that checks the status here without
ever calling it has no bridge and is told SystemLlmStatus.UNSUPPORTED.
A request's seed has no effect on Apple platforms, whose model takes none.
Tools, ResponseFormat.JSON_OBJECT and embeddings are not supported
and fail the request with LlmException.ErrorType.INVALID_REQUEST.
Requests run one at a time on a background thread, in the order they were made. Results and streaming callbacks arrive on the EDT.
-
Field Summary
Fields inherited from class LlmClient
DEFAULT_ANTHROPIC_URL, DEFAULT_GEMINI_URL, DEFAULT_OLLAMA_URL, DEFAULT_OPENAI_URL -
Method Summary
Modifier and TypeMethodDescriptionchat(ChatRequest req) Non-streaming chat.chatStream(ChatRequest req, StreamingListener listener) Streaming chat.static AsyncResource<SystemLlmStatus> checkStatus(SystemLlmFeature feature) Asks the OS what it can do for a feature without blocking the caller.voidclose()Stops the worker thread.describeImage(byte[] image) Describes an image in a sentence or two, for example as alternative text.download(SystemLlmFeature feature) Asks the OS to download the model behind a feature, for a status ofSystemLlmStatus.DOWNLOADABLE.embed(EmbeddingRequest req) Embeddings are not offered by OS models; always fails withLlmException.ErrorType.INVALID_REQUEST.The model behind this client, such asapple-foundation-modelsorgemini-nano, ornonewhere there is none.static SystemLlmStatusgetStatus(SystemLlmFeature feature) Asks the OS what it can do for a feature.static StringgetStatusReason(SystemLlmFeature feature) Explains the status of a feature: why it is unsupported or disabled.static SystemLlmFeature[]Every feature this device supports, now or after a download.static booleanWhether this device can chat with an OS model, now or after a download.Corrects grammar and spelling, keeping the meaning and the voice.rewrite(String text, RewriteStyle style) Rewrites a text in another style.Summarizes a text.Methods inherited from class LlmClient
anthropic, gemini, getBaseUrl, getHttpTimeoutMs, local, localOpenAiCompatible, ollama, ollama, ollama, openai, runSafetyFilter, setBaseUrl, setHttpTimeoutMs, system
-
Method Details
-
isSupported
public static boolean isSupported()Whether this device can chat with an OS model, now or after a download. Blocks briefly while the OS is asked; prefercheckStatus(SystemLlmFeature)on the EDT.- Returns:
truewhengetStatus(SystemLlmFeature)forSystemLlmFeature.CHATis supported
-
getStatus
Asks the OS what it can do for a feature. Blocks briefly while the OS answers; prefercheckStatus(SystemLlmFeature)on the EDT.- Parameters:
feature- the capability- Returns:
- its status on this device
-
getStatusReason
Explains the status of a feature: why it is unsupported or disabled.- Parameters:
feature- the capability- Returns:
- a human readable reason, or
nullwhen there is nothing to add
-
checkStatus
Asks the OS what it can do for a feature without blocking the caller.- Parameters:
feature- the capability- Returns:
- the status, delivered on the EDT
-
getSupportedFeatures
Every feature this device supports, now or after a download. Blocks briefly while the OS is asked.- Returns:
- the supported features, in declaration order
-
getProvider
The model behind this client, such asapple-foundation-modelsorgemini-nano, ornonewhere there is none.- Specified by:
getProviderin classLlmClient- Returns:
- stable provider family id
-
download
Asks the OS to download the model behind a feature, for a status ofSystemLlmStatus.DOWNLOADABLE. On Android it completes when the model is ready. Apple platforms download on their own: there it completes at once, withtruewhile the OS is still fetching the model too, so checkgetStatus(SystemLlmFeature)before relying on it.- Parameters:
feature- the capability- Returns:
- completes with
true, or fails with anLlmException
-
chat
Description copied from class:LlmClientNon-streaming chat. Equivalent tochatStreamwith a no-op listener but optimized -- the provider skips the SSE response and returns a single JSON object. -
chatStream
Description copied from class:LlmClientStreaming chat.listenerfires for every content delta / tool-call fragment on the EDT. The returnedAsyncResourcecompletes with the aggregated final response once the stream ends; cancel it to close the underlying socket.- Specified by:
chatStreamin classLlmClient- Parameters:
req- immutable chat requestlistener- incremental callbacks delivered on the EDT- Returns:
- asynchronous aggregated response
-
embed
Embeddings are not offered by OS models; always fails withLlmException.ErrorType.INVALID_REQUEST. -
summarize
Summarizes a text.- Parameters:
text- the text, an article or a conversationbullets- how many bullet points, 1 to 3- Returns:
- the summary
-
proofread
Corrects grammar and spelling, keeping the meaning and the voice.- Parameters:
text- the text- Returns:
- the corrected text
-
rewrite
Rewrites a text in another style.- Parameters:
text- the textstyle- the style to rewrite it in- Returns:
- the rewritten text
-
describeImage
Describes an image in a sentence or two, for example as alternative text.- Parameters:
image- an encoded PNG or JPEG- Returns:
- the description
-
close
public void close()Stops the worker thread. Requests already queued still run first.
-