langchain_community.cache.AstraDBSemanticCache¶
class langchain_community.cache.AstraDBSemanticCache(*, collection_name: str = 'langchain_astradb_semantic_cache', token: Optional[str] = None, api_endpoint: Optional[str] = None, astra_db_client: Optional[Any] = None, namespace: Optional[str] = None, embedding: Embeddings, metric: Optional[str] = None, similarity_threshold: float = 0.85)[source]¶
Cache that uses Astra DB as a vector-store backend for semantic
(i.e. similarity-based) lookup.
It uses a single (vector) collection and can store
cached values from several LLMs, so the LLM’s ‘llm_string’ is stored
in the document metadata.
You can choose the preferred similarity (or use the API default) –
remember the threshold might require metric-dependend tuning.
Initialize the cache with all relevant parameters.
:param collection_name: name of the Astra DB collection to create/use.
:type collection_name: str
:param token: API token for Astra DB usage.
:type token: Optional[str]
:param api_endpoint: full URL to the API endpoint,
such as “https://<DB-ID>-us-east1.apps.astra.datastax.com”.
Parameters
astra_db_client (Optional[Any]) – alternative to token+api_endpoint,
you can pass an already-created ‘astrapy.db.AstraDB’ instance.
namespace (Optional[str]) – namespace (aka keyspace) where the
collection is created. Defaults to the database’s “default namespace”.
embedding (Embedding) – Embedding provider for semantic
encoding and search.
metric – the function to use for evaluating similarity of text embeddings.
Defaults to ‘cosine’ (alternatives: ‘euclidean’, ‘dot_product’)
similarity_threshold (float, optional) – the minimum similarity