public abstract class LlmClient

  1. Object
  2. LlmClient

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

public static final String DEFAULT_OPENAI_URL = "https://api.openai.com/v1"
public static final String DEFAULT_ANTHROPIC_URL = "https://api.anthropic.com/v1"
public static final String DEFAULT_GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/openai"
public static final String DEFAULT_OLLAMA_URL = "http://localhost:11434/v1"

Constructors

protected LlmClient(String baseUrl)

Methods

public static LlmClient openai(String apiKey)OpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.).
public static LlmClient anthropic(String apiKey)Creates a client for Anthropic’s Messages API.
public static LlmClient gemini(String apiKey)Creates a client for Gemini’s OpenAI-compatible endpoint.
public static LlmClient ollama()Default Ollama install: http://localhost:11434/v1, model llama3.2.
public static LlmClient ollama(String defaultModel)Creates a client for the default local Ollama endpoint.
public static LlmClient ollama(String baseUrl, String defaultModel)Creates an Ollama client for a custom endpoint.
public static LlmClient localOpenAiCompatible(String baseUrl, String apiKey, String defaultModel)Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy).
public abstract AsyncResource<ChatResponse> chat(ChatRequest req)Non-streaming chat.
public abstract AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener)Streaming chat.
public abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)Creates embeddings for one or more strings.
public abstract String getProvider()One of "openai", "anthropic", "gemini", "ollama", "local".
public String getBaseUrl()
public void setBaseUrl(String baseUrl)Overrides the provider endpoint for proxies or compatible services.
public int getHttpTimeoutMs()
public void setHttpTimeoutMs(int httpTimeoutMs)Sets the network timeout for subsequent calls.
protected String runSafetyFilter(ChatRequest req)Helper for subclasses: applies the active SafetyFilter (if any) before the network call.

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)
OpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.). Uses the public endpoint by default; override with setBaseUrl(String).

Parameters

apiKey String
provider API key

Returns

configured client

anthropic

public static LlmClient anthropic(String apiKey)
Creates a client for Anthropic’s Messages API.

Parameters

apiKey String
Anthropic API key

Returns

configured client

gemini

public static LlmClient gemini(String apiKey)
Creates a client for Gemini’s OpenAI-compatible endpoint.

Parameters

apiKey String
Google AI API key

Returns

configured client

ollama

public static LlmClient ollama()
Default Ollama install: http://localhost:11434/v1, model llama3.2.

Returns

local Ollama client

ollama

public static LlmClient ollama(String defaultModel)
Creates a client for the default local Ollama endpoint.

Parameters

defaultModel String
model used when requests omit one

Returns

local Ollama client

ollama

public static LlmClient ollama(String baseUrl, String defaultModel)
Creates an Ollama client for a custom endpoint.

Parameters

baseUrl String
OpenAI-compatible endpoint root
defaultModel String
model used when requests omit one

Returns

configured Ollama client

localOpenAiCompatible

public static LlmClient localOpenAiCompatible(String baseUrl, String apiKey, String defaultModel)
Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy). apiKey may be empty for local services that don’t authenticate.

Parameters

baseUrl String
OpenAI-compatible endpoint root
apiKey String
API key, or empty for an unauthenticated service
defaultModel String
model used when requests omit one

Returns

configured compatible client

chat

public abstract AsyncResource<ChatResponse> chat(ChatRequest req)
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.

Parameters

req ChatRequest
immutable chat request

Returns

asynchronous normalized provider response

chatStream

public abstract AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener)
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.

Parameters

req ChatRequest
immutable chat request
listener StreamingListener
incremental callbacks delivered on the EDT

Returns

asynchronous aggregated response

embed

public abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req)
Creates embeddings for one or more strings.

Parameters

req EmbeddingRequest
immutable embedding request

Returns

asynchronous normalized embedding response

getProvider

public abstract String getProvider()
One of "openai", "anthropic", "gemini", "ollama", "local". Used by ChatView and tests to vary behaviour by provider.

Returns

stable provider family id

getBaseUrl

public String getBaseUrl()

Returns

current provider endpoint root

setBaseUrl

public void setBaseUrl(String baseUrl)
Overrides the provider endpoint for proxies or compatible services.

Parameters

baseUrl String
endpoint root used by subsequent calls

getHttpTimeoutMs

public int getHttpTimeoutMs()

Returns

network timeout in milliseconds

setHttpTimeoutMs

public void setHttpTimeoutMs(int httpTimeoutMs)
Sets the network timeout for subsequent calls.

Parameters

httpTimeoutMs int
timeout in milliseconds

runSafetyFilter

protected String runSafetyFilter(ChatRequest req)
Helper for subclasses: applies the active SafetyFilter (if any) before the network call. Returns the rejection reason on failure, null to proceed.