Class LlmClient
java.lang.Object
com.codename1.ai.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.
-
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic LlmClientCreates a client for Anthropic's Messages API.abstract AsyncResource<ChatResponse> chat(ChatRequest req) Non-streaming chat.abstract AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener) Streaming chat.abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req) Creates embeddings for one or more strings.static LlmClientCreates a client for Gemini's OpenAI-compatible endpoint.intabstract StringOne of"openai","anthropic","gemini","ollama","local".static LlmClientlocalOpenAiCompatible(String baseUrl, String apiKey, String defaultModel) Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy).static LlmClientollama()Default Ollama install:http://localhost:11434/v1, modelllama3.2.static LlmClientCreates a client for the default local Ollama endpoint.static LlmClientCreates an Ollama client for a custom endpoint.static LlmClientOpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.).protected StringHelper for subclasses: applies the activeSafetyFilter(if any) before the network call.voidsetBaseUrl(String baseUrl) Overrides the provider endpoint for proxies or compatible services.voidsetHttpTimeoutMs(int httpTimeoutMs) Sets the network timeout for subsequent calls.
-
Field Details
-
DEFAULT_OPENAI_URL
- See Also:
-
DEFAULT_ANTHROPIC_URL
- See Also:
-
DEFAULT_GEMINI_URL
- See Also:
-
DEFAULT_OLLAMA_URL
- See Also:
-
-
Constructor Details
-
LlmClient
-
-
Method Details
-
openai
OpenAI / OpenAI-compatible (Together, Groq, Fireworks, vLLM, Ollama, etc.). Uses the public endpoint by default; override withsetBaseUrl(String).- Parameters:
apiKey- provider API key- Returns:
- configured client
-
anthropic
-
gemini
-
ollama
Default Ollama install:http://localhost:11434/v1, modelllama3.2.- Returns:
- local Ollama client
-
ollama
-
ollama
-
localOpenAiCompatible
Generic OpenAI-compatible endpoint (llama.cpp server, vLLM, LM Studio, a custom proxy).apiKeymay be empty for local services that don't authenticate.- Parameters:
baseUrl- OpenAI-compatible endpoint rootapiKey- API key, or empty for an unauthenticated servicedefaultModel- model used when requests omit one- Returns:
- configured compatible client
-
chat
Non-streaming chat. Equivalent tochatStreamwith a no-op listener but optimized -- the provider skips the SSE response and returns a single JSON object.- Parameters:
req- immutable chat request- Returns:
- asynchronous normalized provider response
-
chatStream
Streaming 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.- Parameters:
req- immutable chat requestlistener- incremental callbacks delivered on the EDT- Returns:
- asynchronous aggregated response
-
embed
Creates embeddings for one or more strings.- Parameters:
req- immutable embedding request- Returns:
- asynchronous normalized embedding response
-
getProvider
One of"openai","anthropic","gemini","ollama","local". Used byChatViewand tests to vary behaviour by provider.- Returns:
- stable provider family id
-
getBaseUrl
- Returns:
- current provider endpoint root
-
setBaseUrl
Overrides the provider endpoint for proxies or compatible services.- Parameters:
baseUrl- 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- timeout in milliseconds
-
runSafetyFilter
Helper for subclasses: applies the activeSafetyFilter(if any) before the network call. Returns the rejection reason on failure,nullto proceed.
-