JEmbeddingClient

org.llm4s.javaapi.JEmbeddingClient
See theJEmbeddingClient companion object
final class JEmbeddingClient

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)));

Obtain one with Llm4s.createDefaultEmbeddingClient, which reads the model from llm4s.embeddings.model (EMBEDDING_MODEL), with Llm4s.createEmbeddingClient for a model you name, or wrap a Scala EmbeddingClient you built yourself with Llm4s.wrapEmbeddingClient.

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.

Attributes

Companion
object
Source
JEmbeddingClient.scala
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Concrete methods

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.

Attributes

Source
JEmbeddingClient.scala
def embed(texts: List[String]): LlmResult[JEmbeddings]

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).

Attributes

Source
JEmbeddingClient.scala
def embed(texts: List[String], purpose: JEmbeddingPurpose): LlmResult[JEmbeddings]

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.

Attributes

Source
JEmbeddingClient.scala
def embedAsync(texts: List[String]): CompletableFuture[JEmbeddings]

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.

Attributes

Source
JEmbeddingClient.scala
def embedAsync(texts: List[String], purpose: JEmbeddingPurpose): CompletableFuture[JEmbeddings]

embed(texts, purpose) on another thread, as embedAsync(texts).

embed(texts, purpose) on another thread, as embedAsync(texts).

Attributes

Source
JEmbeddingClient.scala
def model: String

The embedding model this client asks for, as configured.

The embedding model this client asks for, as configured.

Attributes

Source
JEmbeddingClient.scala
override def toString: String

Returns a string representation of the object.

Returns a string representation of the object.

The default representation is platform dependent.

Attributes

Returns

a string representation of the object.

Definition Classes
Any
Source
JEmbeddingClient.scala

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.

Attributes

Throws
NullPointerException

when executor is null

Source
JEmbeddingClient.scala