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
