Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public final class LocalLlmClient
An LlmClient that runs a GGUF model on the device with llama.cpp. Create
one with LlmClient.local(String).
Nothing leaves the device and no network is needed. The model file is
loaded on the first request, which can take several seconds for a large
model, and stays in memory until close().
LocalLlmClient llm = LlmClient.local(FileSystemStorage.getInstance()
.getAppHomePath() + "qwen2.5-0.5b-instruct-q4_k_m.gguf");
llm.chat(ChatRequest.builder()
.addMessage(ChatMessage.user("Name three primes"))
.build()).ready(r -> Log.p(r.getText()));
The prompt is rendered by the chat template the model file carries, so any
instruction-tuned GGUF works without configuration.
ResponseFormat.JSON_OBJECT is enforced with a grammar, so the output is
always valid JSON. Tools and image parts are not supported and fail the
request with LlmException.ErrorType.INVALID_REQUEST.
Availability
Android and iOS. The builders link llama.cpp only into applications that
call LlmClient.local(String). Elsewhere, the simulator included, every
request fails with LlmException.ErrorType.INVALID_REQUEST; use
LlmClient.ollama() to develop against the same model on the desktop.
Requests run one at a time on a background thread, in the order they were made. Results and streaming callbacks arrive on the EDT.
Methods
Inherited fields
Inherited methods
Method details
getModelPath
public String getModelPath()Returns
setContextSize
public LocalLlmClient setContextSize(int tokens)Parameters
tokensint- context size in tokens,
0for the model’s default
Returns
setGpuLayers
public LocalLlmClient setGpuLayers(int layers)Parameters
layersint- layer count,
0for CPU only,-1(the default) for all
Returns
setThreads
public LocalLlmClient setThreads(int threads)Parameters
threadsint- thread count,
0(the default) to let the port choose
Returns
isSupported
public static boolean isSupported()false in the
simulator and on any target the builder did not link llama.cpp into.Returns
true when requests can succeedgetProvider
public String getProvider()"openai", "anthropic", "gemini", "ollama",
"local". Used by ChatView and tests to vary behaviour by
provider.Returns
chat
public AsyncResource<ChatResponse> chat(ChatRequest req)chatStream with a no-op
listener but optimized – the provider skips the SSE response
and returns a single JSON object.Parameters
reqChatRequest- immutable chat request
Returns
chatStream
public AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener)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.Parameters
reqChatRequest- immutable chat request
listenerStreamingListener- incremental callbacks delivered on the EDT
Returns
embed
public AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)Parameters
reqEmbeddingRequest- immutable embedding request
Returns
close
public void close()close() can never reach the native session on a second
thread while the first is still using or closing it.