Java-friendly wrapper around core's EmbeddingClient: turns texts into vectors, for semantic search and similarity, with no Scala type in sight.
JEmbeddingClient embedder = Llm4s.createDefaultEmbeddingClient().get();
JEmbeddings embeddings = embedder.embed(List.of("The cat sat on the mat.", "A kitten lay on the rug.")).get();
List<float[]> vectors = embeddings.vectors();
System.out.println(JEmbeddings.cosineSimilarity(vectors.get(0), vectors.get(1)));
Every embed sends all its texts to the provider in one request, blocks the calling thread and never throws: a failure - of the provider, the network or an argument - comes back inside the LlmResult, and its getKind() says which. An embedding provider's error response reads by its HTTP status, as a chat provider's does: 401 and 403 as AUTHENTICATION, 429 as RATE_LIMIT, 400 as VALIDATION, any other as SERVICE; a provider that cannot be reached reads as NETWORK, and one that does not answer in time as TIMEOUT. Interrupting the blocked thread returns a failed result whose kind is CANCELLED, with the thread's interrupt flag still set; InterruptedException is never thrown. embedAsync makes the same request on another thread and returns a CompletableFuture at once.
The length of the vectors the model makes, as its provider module declares it; empty for a model the module does not list, such as an Ollama model you pulled yourself. Such a model's vectors are as long as the provider makes them: JEmbeddings.dimensions says how long.
The length of the vectors the model makes, as its provider module declares it; empty for a model the module does not list, such as an Ollama model you pulled yourself. Such a model's vectors are as long as the provider makes them: JEmbeddings.dimensions says how long.
Embeds texts as documents to be indexed - core's default - returning one vector per text, in order. The same as embed(texts, JEmbeddingPurpose.DOCUMENT).
Embeds texts as documents to be indexed - core's default - returning one vector per text, in order. The same as embed(texts, JEmbeddingPurpose.DOCUMENT).
Embeds texts for purpose - documents to index, or queries to run against them - returning one vector per text, in order, from a single request. An empty list returns no vectors and calls no provider.
Embeds texts for purpose - documents to index, or queries to run against them - returning one vector per text, in order, from a single request. An empty list returns no vectors and calls no provider.
A null list, null text or null purpose yields a failed result of kind VALIDATION, never an exception. Blocks the calling thread; an interrupt yields a failed result of kind CANCELLED, with the interrupt flag left set.
embed(texts) on another thread - a new virtual thread per call, or the executor withAsyncExecutor names: returns at once with a future of the vectors. The future completes exceptionally with the LlmException the blocking call's failed result would hold, of the same LlmErrorKind; cancel(true), or completing the future yourself (orTimeout), interrupts the request.
embed(texts) on another thread - a new virtual thread per call, or the executor withAsyncExecutor names: returns at once with a future of the vectors. The future completes exceptionally with the LlmException the blocking call's failed result would hold, of the same LlmErrorKind; cancel(true), or completing the future yourself (orTimeout), interrupts the request.
This client with its embedAsync calls run on executor rather than a new virtual thread each; it never shuts executor down. An executor that refuses a call fails its future at once with kind OTHER.
This client with its embedAsync calls run on executor rather than a new virtual thread each; it never shuts executor down. An executor that refuses a call fails its future at once with kind OTHER.