"""Evict from cache if there's an entry."""
        doc_id = self._make_id(prompt, llm_string)
        return self.collection.delete_one(doc_id)
[docs]    def clear(self, **kwargs: Any) -> None:
        """Clear cache. This is for all LLMs at once."""
        self.astra_db.truncate_collection(self.collection_name)
ASTRA_DB_SEMANTIC_CACHE_DEFAULT_THRESHOLD = 0.85
ASTRA_DB_CACHE_DEFAULT_COLLECTION_NAME = "langchain_astradb_semantic_cache"
ASTRA_DB_SEMANTIC_CACHE_EMBEDDING_CACHE_SIZE = 16
[docs]class AstraDBSemanticCache(BaseCache):
    """
    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.
    """
[docs]    def __init__(
        self,
        *,
        collection_name: str = ASTRA_DB_CACHE_DEFAULT_COLLECTION_NAME,
        token: Optional[str] = None,
        api_endpoint: Optional[str] = None,
        astra_db_client: Optional[Any] = None,  # 'astrapy.db.AstraDB' if passed
        namespace: Optional[str] = None,
        embedding: Embeddings,
        metric: Optional[str] = None,
        similarity_threshold: float = ASTRA_DB_SEMANTIC_CACHE_DEFAULT_THRESHOLD,
    ):
        """
        Initialize the cache with all relevant parameters.
        Args: