Class SystemLlmClient

java.lang.Object
com.codename1.ai.LlmClient
com.codename1.ai.SystemLlmClient

public final class SystemLlmClient extends LlmClient

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.UNSUPPORTED and requests fail with LlmException.ErrorType.INVALID_REQUEST. Use LlmClient.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.

  • 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; prefer checkStatus(SystemLlmFeature) on the EDT.
      Returns:
      true when getStatus(SystemLlmFeature) for SystemLlmFeature.CHAT is supported
    • getStatus

      public static SystemLlmStatus getStatus(SystemLlmFeature feature)
      Asks the OS what it can do for a feature. Blocks briefly while the OS answers; prefer checkStatus(SystemLlmFeature) on the EDT.
      Parameters:
      feature - the capability
      Returns:
      its status on this device
    • getStatusReason

      public static String getStatusReason(SystemLlmFeature feature)
      Explains the status of a feature: why it is unsupported or disabled.
      Parameters:
      feature - the capability
      Returns:
      a human readable reason, or null when there is nothing to add
    • checkStatus

      public static AsyncResource<SystemLlmStatus> checkStatus(SystemLlmFeature feature)
      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

      public static SystemLlmFeature[] 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

      public String getProvider()
      The model behind this client, such as apple-foundation-models or gemini-nano, or none where there is none.
      Specified by:
      getProvider in class LlmClient
      Returns:
      stable provider family id
    • download

      public AsyncResource<Boolean> download(SystemLlmFeature feature)
      Asks the OS to download the model behind a feature, for a status of SystemLlmStatus.DOWNLOADABLE. On Android it completes when the model is ready. Apple platforms download on their own: there it completes at once, with true while the OS is still fetching the model too, so check getStatus(SystemLlmFeature) before relying on it.
      Parameters:
      feature - the capability
      Returns:
      completes with true, or fails with an LlmException
    • chat

      Description copied from class: LlmClient
      Non-streaming chat. Equivalent to chatStream with a no-op listener but optimized -- the provider skips the SSE response and returns a single JSON object.
      Specified by:
      chat in class LlmClient
      Parameters:
      req - immutable chat request
      Returns:
      asynchronous normalized provider response
    • chatStream

      public AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener)
      Description copied from class: LlmClient
      Streaming chat. listener fires for every content delta / tool-call fragment on the EDT. The returned AsyncResource completes with the aggregated final response once the stream ends; cancel it to close the underlying socket.
      Specified by:
      chatStream in class LlmClient
      Parameters:
      req - immutable chat request
      listener - incremental callbacks delivered on the EDT
      Returns:
      asynchronous aggregated response
    • embed

      Embeddings are not offered by OS models; always fails with LlmException.ErrorType.INVALID_REQUEST.
      Specified by:
      embed in class LlmClient
      Parameters:
      req - immutable embedding request
      Returns:
      asynchronous normalized embedding response
    • summarize

      public AsyncResource<String> summarize(String text, int bullets)
      Summarizes a text.
      Parameters:
      text - the text, an article or a conversation
      bullets - how many bullet points, 1 to 3
      Returns:
      the summary
    • proofread

      public AsyncResource<String> proofread(String text)
      Corrects grammar and spelling, keeping the meaning and the voice.
      Parameters:
      text - the text
      Returns:
      the corrected text
    • rewrite

      public AsyncResource<String> rewrite(String text, RewriteStyle style)
      Rewrites a text in another style.
      Parameters:
      text - the text
      style - the style to rewrite it in
      Returns:
      the rewritten text
    • describeImage

      public AsyncResource<String> describeImage(byte[] image)
      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.