CachedEmbeddingClient

org.llm4s.llmconnect.caching.CachedEmbeddingClient
See theCachedEmbeddingClient companion object
@Stable

A caching decorator for an EmbeddingProvider, which an EmbeddingClient is too.

On each embed call, input texts are checked against the cache first. Only texts that are not cached are forwarded to the base client — in a single batched request. Results are then stored in the cache and merged with cached hits before being returned, preserving the original input order.

Errors returned by the base client are never cached, so transient failures are retried on the next call. A cache that fails is logged and bypassed: a failed lookup is a miss, a failed store leaves the text uncached.

It is itself an EmbeddingProvider, so it can be passed where one is expected, and wrapped in an EmbeddingClient where one of those is: EmbeddingClient(cached).

A response that needed the base client keeps that response's usage, metadata, modality and dim, with the cached vectors merged in; usage counts only the texts sent. A response served wholly from the cache has no usage and its dim is the vectors' length.

Value parameters

baseClient

The underlying client used to generate embeddings on cache misses.

cache

The storage backend for the embedding vectors.

keyGenerator

Function that maps one input text and the request it came in to a cache key; defaults to CacheKeyGenerator.embeddingKey, which keys by the text, the request's model name and its purpose, so that a query and a document with the same text never share an entry. It takes the request, not its fields, so a key can use whatever a request carries. Build one with the companion's apply. The constructor is private, so that a setting can be added without breaking a caller, and the class is final: wrap it to change what it does.

Attributes

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

Members list

Value members

Concrete methods

The cache's hit/miss statistics, when it keeps them.

The cache's hit/miss statistics, when it keeps them.

Attributes

Source
CachedEmbeddingClient.scala

Calls the backend's clear(). With InMemoryEmbeddingCache this empties the cache and resets the statistics; a custom EmbeddingCache decides for itself.

Calls the backend's clear(). With InMemoryEmbeddingCache this empties the cache and resets the statistics; a custom EmbeddingCache decides for itself.

Attributes

Source
CachedEmbeddingClient.scala
override def embed(request: EmbeddingRequest): Result[EmbeddingResponse]

Generates embeddings for the provided request, serving cached vectors where available and forwarding all cache misses to the base client in a single call.

Generates embeddings for the provided request, serving cached vectors where available and forwarding all cache misses to the base client in a single call.

Value parameters

request

The embedding request containing one or more input strings.

Attributes

Returns

A Result containing an EmbeddingResponse with one vector per input, in the same order as EmbeddingRequest.input.

Definition Classes
Source
CachedEmbeddingClient.scala