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 LlmClientabstract AsyncResource<ChatResponse> chat(ChatRequest req) Non-streaming chat.abstract AsyncResource<ChatResponse> chatStream(ChatRequest req, StreamingListener listener) Streaming chat.abstract AsyncResource<EmbeddingResponse> embed(EmbeddingRequest req) static LlmClientintabstract 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 LlmClientstatic LlmClientstatic 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) voidsetHttpTimeoutMs(int httpTimeoutMs)
-
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). -
anthropic
-
gemini
-
ollama
Default Ollama install:http://localhost:11434/v1, modelllama3.2. -
ollama
-
ollama
-
localOpenAiCompatible
-
chat
Non-streaming chat. Equivalent tochatStreamwith a no-op listener but optimized -- the provider skips the SSE response and returns a single JSON object. -
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. -
embed
-
getProvider
One of"openai","anthropic","gemini","ollama","local". Used byChatViewand tests to vary behaviour by provider. -
getBaseUrl
-
setBaseUrl
-
getHttpTimeoutMs
public int getHttpTimeoutMs() -
setHttpTimeoutMs
public void setHttpTimeoutMs(int httpTimeoutMs) -
runSafetyFilter
Helper for subclasses: applies the activeSafetyFilter(if any) before the network call. Returns the rejection reason on failure,nullto proceed.
-