Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.
public abstract class LlmClient
- Object
- LlmClient
Known subtypesLocalLlmClient
Provider-agnostic chat / embeddings client. Built via one of the static factory methods:
LlmClient gpt = LlmClient.openai("sk-...");
LlmClient claude = LlmClient.anthropic("sk-ant-...");
LlmClient gemini = LlmClient.gemini("AIza...");
LlmClient ollama = LlmClient.ollama(); // localhost:11434
LlmClient local = LlmClient.localOpenAiCompatible(
"http://10.0.0.5:8080/v1", "", "qwen2.5-7b");
All calls return AsyncResource so they compose naturally with the
rest of the Codename One async API. chatStream additionally fires
per-token deltas through a StreamingListener; both that listener
and the final AsyncResource complete on the EDT.
Simulator behaviour
When running in the JavaSE simulator with cn1.ai.simulatorRedirect
set to ollama (or auto with Ollama detected on the loopback),
the static factories transparently route through a local Ollama
endpoint instead of the public provider – so unchanged production
code can be debugged offline without API charges.
Fields
Constructors
protected LlmClient(String baseUrl) |
Methods
Inherited methods
Field details
DEFAULT_OPENAI_URL
public static final String DEFAULT_OPENAI_URL = "https://api.openai.com/v1"DEFAULT_ANTHROPIC_URL
public static final String DEFAULT_ANTHROPIC_URL = "https://api.anthropic.com/v1"DEFAULT_GEMINI_URL
public static final String DEFAULT_GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/openai"DEFAULT_OLLAMA_URL
public static final String DEFAULT_OLLAMA_URL = "http://localhost:11434/v1"Constructor details
LlmClient
protected LlmClient(String baseUrl)Method details
openai
public static LlmClient openai(String apiKey)setBaseUrl(String).Parameters
apiKeyString- provider API key
Returns
anthropic
public static LlmClient anthropic(String apiKey)Parameters
apiKeyString- Anthropic API key
Returns
gemini
public static LlmClient gemini(String apiKey)Parameters
apiKeyString- Google AI API key
Returns
ollama
public static LlmClient ollama()http://localhost:11434/v1, model
llama3.2.Returns
ollama
public static LlmClient ollama(String defaultModel)Parameters
defaultModelString- model used when requests omit one
Returns
ollama
public static LlmClient ollama(String baseUrl, String defaultModel)Parameters
baseUrlString- OpenAI-compatible endpoint root
defaultModelString- model used when requests omit one
Returns
local
public static LocalLlmClient local(String modelPath)Runs a GGUF model on the device with llama.cpp. See LocalLlmClient
for what is supported and where.
Calling this method is what makes the builders link llama.cpp into the application; an application that never calls it ships none of it.
Parameters
modelPathString- file system path (or
file://URL) of a.ggufmodel
Returns
localOpenAiCompatible
public static LlmClient localOpenAiCompatible(String baseUrl, String apiKey, String defaultModel)apiKey may be empty for local
services that don’t authenticate.Parameters
baseUrlString- OpenAI-compatible endpoint root
apiKeyString- API key, or empty for an unauthenticated service
defaultModelString- model used when requests omit one
Returns
chat
public abstract 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 abstract 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 abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)Parameters
reqEmbeddingRequest- immutable embedding request
Returns
getProvider
public abstract String getProvider()"openai", "anthropic", "gemini", "ollama",
"local". Used by ChatView and tests to vary behaviour by
provider.Returns
getBaseUrl
public String getBaseUrl()Returns
setBaseUrl
public void setBaseUrl(String baseUrl)Parameters
baseUrlString- endpoint root used by subsequent calls
getHttpTimeoutMs
public int getHttpTimeoutMs()Returns
setHttpTimeoutMs
public void setHttpTimeoutMs(int httpTimeoutMs)Parameters
httpTimeoutMsint- timeout in milliseconds
runSafetyFilter
protected String runSafetyFilter(ChatRequest req)SafetyFilter (if
any) before the network call. Returns the rejection reason on
failure, null to proceed.