AzureCosmosDBSemanticCache#

class langchain_community.cache.AzureCosmosDBSemanticCache(cosmosdb_connection_string: str, database_name: str, collection_name: str, embedding: Embeddings, *, cosmosdb_client: Any | None = None, num_lists: int = 100, similarity: CosmosDBSimilarityType = CosmosDBSimilarityType.COS, kind: CosmosDBVectorSearchType = CosmosDBVectorSearchType.VECTOR_IVF, dimensions: int = 1536, m: int = 16, ef_construction: int = 64, ef_search: int = 40, score_threshold: float | None = None, application_name: str = 'LANGCHAIN_CACHING_PYTHON')[source]#

Cache that uses Cosmos DB Mongo vCore vector-store backend

Parameters:
  • cosmosdb_connection_string (str) – Cosmos DB Mongo vCore connection string

  • cosmosdb_client (Optional[Any]) – Cosmos DB Mongo vCore client

  • embedding (Embedding) – Embedding provider for semantic encoding and search.

  • database_name (str) – Database name for the CosmosDBMongoVCoreSemanticCache

  • collection_name (str) – Collection name for the CosmosDBMongoVCoreSemanticCache

  • num_lists (int) – This integer is the number of clusters that the inverted file (IVF) index uses to group the vector data. We recommend that numLists is set to documentCount/1000 for up to 1 million documents and to sqrt(documentCount) for more than 1 million documents. Using a numLists value of 1 is akin to performing brute-force search, which has limited performance

  • dimensions (int) – Number of dimensions for vector similarity. The maximum number of supported dimensions is 2000

  • similarity (CosmosDBSimilarityType) –

    Similarity metric to use with the IVF index.

    Possible options are:
    • CosmosDBSimilarityType.COS (cosine distance),

    • CosmosDBSimilarityType.L2 (Euclidean distance), and

    • CosmosDBSimilarityType.IP (inner product).

  • kind (CosmosDBVectorSearchType) –

    Type of vector index to create. Possible options are:

  • m (int) – The max number of connections per layer (16 by default, minimum value is 2, maximum value is 100). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements.

  • ef_construction (int) – the size of the dynamic candidate list for constructing the graph (64 by default, minimum value is 4, maximum value is 1000). Higher ef_construction will result in better index quality and higher accuracy, but it will also increase the time required to build the index. ef_construction has to be at least 2 * m

  • ef_search (int) – The size of the dynamic candidate list for search (40 by default). A higher value provides better recall at the cost of speed.

  • score_threshold (Optional[float]) – Maximum score used to filter the vector search documents.

  • application_name (str) – Application name for the client for tracking and logging

Attributes

DEFAULT_COLLECTION_NAME

DEFAULT_DATABASE_NAME

Methods

__init__(cosmosdb_connection_string,Β ...[,Β ...])

aclear(**kwargs)

Async clear cache that can take additional keyword arguments.

alookup(prompt,Β llm_string)

Async look up based on prompt and llm_string.

aupdate(prompt,Β llm_string,Β return_val)

Async update cache based on prompt and llm_string.

clear(**kwargs)

Clear semantic cache for a given llm_string.

lookup(prompt,Β llm_string)

Look up based on prompt and llm_string.

update(prompt,Β llm_string,Β return_val)

Update cache based on prompt and llm_string.

__init__(cosmosdb_connection_string: str, database_name: str, collection_name: str, embedding: Embeddings, *, cosmosdb_client: Any | None = None, num_lists: int = 100, similarity: CosmosDBSimilarityType = CosmosDBSimilarityType.COS, kind: CosmosDBVectorSearchType = CosmosDBVectorSearchType.VECTOR_IVF, dimensions: int = 1536, m: int = 16, ef_construction: int = 64, ef_search: int = 40, score_threshold: float | None = None, application_name: str = 'LANGCHAIN_CACHING_PYTHON')[source]#
Parameters:
  • cosmosdb_connection_string (str) – Cosmos DB Mongo vCore connection string

  • cosmosdb_client (Any | None) – Cosmos DB Mongo vCore client

  • embedding (Embedding) – Embedding provider for semantic encoding and search.

  • database_name (str) – Database name for the CosmosDBMongoVCoreSemanticCache

  • collection_name (str) – Collection name for the CosmosDBMongoVCoreSemanticCache

  • num_lists (int) – This integer is the number of clusters that the inverted file (IVF) index uses to group the vector data. We recommend that numLists is set to documentCount/1000 for up to 1 million documents and to sqrt(documentCount) for more than 1 million documents. Using a numLists value of 1 is akin to performing brute-force search, which has limited performance

  • dimensions (int) – Number of dimensions for vector similarity. The maximum number of supported dimensions is 2000

  • similarity (CosmosDBSimilarityType) –

    Similarity metric to use with the IVF index.

    Possible options are:
    • CosmosDBSimilarityType.COS (cosine distance),

    • CosmosDBSimilarityType.L2 (Euclidean distance), and

    • CosmosDBSimilarityType.IP (inner product).

  • kind (CosmosDBVectorSearchType) –

    Type of vector index to create. Possible options are:

  • m (int) – The max number of connections per layer (16 by default, minimum value is 2, maximum value is 100). Higher m is suitable for datasets with high dimensionality and/or high accuracy requirements.

  • ef_construction (int) – the size of the dynamic candidate list for constructing the graph (64 by default, minimum value is 4, maximum value is 1000). Higher ef_construction will result in better index quality and higher accuracy, but it will also increase the time required to build the index. ef_construction has to be at least 2 * m

  • ef_search (int) – The size of the dynamic candidate list for search (40 by default). A higher value provides better recall at the cost of speed.

  • score_threshold (float | None) – Maximum score used to filter the vector search documents.

  • application_name (str) – Application name for the client for tracking and logging

async aclear(**kwargs: Any) β†’ None#

Async clear cache that can take additional keyword arguments.

Parameters:

kwargs (Any)

Return type:

None

async alookup(prompt: str, llm_string: str) β†’ Sequence[Generation] | None#

Async look up based on prompt and llm_string.

A cache implementation is expected to generate a key from the 2-tuple of prompt and llm_string (e.g., by concatenating them with a delimiter).

Parameters:
  • prompt (str) – a string representation of the prompt. In the case of a Chat model, the prompt is a non-trivial serialization of the prompt into the language model.

  • llm_string (str) – A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation.

Returns:

On a cache miss, return None. On a cache hit, return the cached value. The cached value is a list of Generations (or subclasses).

Return type:

Sequence[Generation] | None

async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) β†’ None#

Async update cache based on prompt and llm_string.

The prompt and llm_string are used to generate a key for the cache. The key should match that of the look up method.

Parameters:
  • prompt (str) – a string representation of the prompt. In the case of a Chat model, the prompt is a non-trivial serialization of the prompt into the language model.

  • llm_string (str) – A string representation of the LLM configuration. This is used to capture the invocation parameters of the LLM (e.g., model name, temperature, stop tokens, max tokens, etc.). These invocation parameters are serialized into a string representation.

  • return_val (Sequence[Generation]) – The value to be cached. The value is a list of Generations (or subclasses).

Return type:

None

clear(**kwargs: Any) β†’ None[source]#

Clear semantic cache for a given llm_string.

Parameters:

kwargs (Any)

Return type:

None

lookup(prompt: str, llm_string: str) β†’ Sequence[Generation] | None[source]#

Look up based on prompt and llm_string.

Parameters:
  • prompt (str)

  • llm_string (str)

Return type:

Sequence[Generation] | None

update(prompt: str, llm_string: str, return_val: Sequence[Generation]) β†’ None[source]#

Update cache based on prompt and llm_string.

Parameters:
  • prompt (str)

  • llm_string (str)

  • return_val (Sequence[Generation])

Return type:

None

Examples using AzureCosmosDBSemanticCache