SingleStoreDBSemanticCache#

class langchain_community.cache.SingleStoreDBSemanticCache(embedding: Embeddings, *, cache_table_prefix: str = 'cache_', search_threshold: float = 0.2, **kwargs: Any)[source]#

Cache that uses SingleStore DB as a backend

Initialize with necessary components.

Parameters:
  • embedding (Embeddings) – A text embedding model.

  • cache_table_prefix (str, optional) – Prefix for the cache table name. Defaults to β€œcache_”.

  • search_threshold (float, optional) – The minimum similarity score for a search result to be considered a match. Defaults to 0.2.

  • store (Following arguments pertrain to the SingleStoreDB vector) –

  • distance_strategy (DistanceStrategy, optional) –

    Determines the strategy employed for calculating the distance between vectors in the embedding space. Defaults to DOT_PRODUCT. Available options are: - DOT_PRODUCT: Computes the scalar product of two vectors.

    This is the default behavior

    • EUCLIDEAN_DISTANCE: Computes the Euclidean distance between

      two vectors. This metric considers the geometric distance in the vector space, and might be more suitable for embeddings that rely on spatial relationships. This metric is not compatible with the WEIGHTED_SUM search strategy.

  • content_field (str, optional) – Specifies the field to store the content. Defaults to β€œcontent”.

  • metadata_field (str, optional) – Specifies the field to store metadata. Defaults to β€œmetadata”.

  • vector_field (str, optional) – Specifies the field to store the vector. Defaults to β€œvector”.

  • id_field (str, optional) – Specifies the field to store the id. Defaults to β€œid”.

  • use_vector_index (bool, optional) – Toggles the use of a vector index. Works only with SingleStoreDB 8.5 or later. Defaults to False. If set to True, vector_size parameter is required to be set to a proper value.

  • vector_index_name (str, optional) – Specifies the name of the vector index. Defaults to empty. Will be ignored if use_vector_index is set to False.

  • vector_index_options (dict, optional) –

    Specifies the options for the vector index. Defaults to {}. Will be ignored if use_vector_index is set to False. The options are: index_type (str, optional): Specifies the type of the index.

    Defaults to IVF_PQFS.

    For more options, please refer to the SingleStoreDB documentation: https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing/

  • vector_size (int, optional) – Specifies the size of the vector. Defaults to 1536. Required if use_vector_index is set to True. Should be set to the same value as the size of the vectors stored in the vector_field.

  • pool (Following arguments pertain to the connection) –

  • pool_size (int, optional) – Determines the number of active connections in the pool. Defaults to 5.

  • max_overflow (int, optional) – Determines the maximum number of connections allowed beyond the pool_size. Defaults to 10.

  • timeout (float, optional) – Specifies the maximum wait time in seconds for establishing a connection. Defaults to 30.

  • connection (database) –

  • host (str, optional) – Specifies the hostname, IP address, or URL for the database connection. The default scheme is β€œmysql”.

  • user (str, optional) – Database username.

  • password (str, optional) – Database password.

  • port (int, optional) – Database port. Defaults to 3306 for non-HTTP connections, 80 for HTTP connections, and 443 for HTTPS connections.

  • database (str, optional) – Database name.

  • the (Additional optional arguments provide further customization over) –

  • connection –

  • pure_python (bool, optional) – Toggles the connector mode. If True, operates in pure Python mode.

  • local_infile (bool, optional) – Allows local file uploads.

  • charset (str, optional) – Specifies the character set for string values.

  • ssl_key (str, optional) – Specifies the path of the file containing the SSL key.

  • ssl_cert (str, optional) – Specifies the path of the file containing the SSL certificate.

  • ssl_ca (str, optional) – Specifies the path of the file containing the SSL certificate authority.

  • ssl_cipher (str, optional) – Sets the SSL cipher list.

  • ssl_disabled (bool, optional) – Disables SSL usage.

  • ssl_verify_cert (bool, optional) – Verifies the server’s certificate. Automatically enabled if ssl_ca is specified.

  • ssl_verify_identity (bool, optional) – Verifies the server’s identity.

  • conv (dict[int, Callable], optional) – A dictionary of data conversion functions.

  • credential_type (str, optional) – Specifies the type of authentication to use: auth.PASSWORD, auth.JWT, or auth.BROWSER_SSO.

  • autocommit (bool, optional) – Enables autocommits.

  • results_type (str, optional) – Determines the structure of the query results: tuples, namedtuples, dicts.

  • results_format (str, optional) – Deprecated. This option has been renamed to results_type.

  • kwargs (Any) –

Examples

Basic Usage:

import langchain
from langchain.cache import SingleStoreDBSemanticCache
from langchain.embeddings import OpenAIEmbeddings

langchain.llm_cache = SingleStoreDBSemanticCache(
    embedding=OpenAIEmbeddings(),
    host="https://user:password@127.0.0.1:3306/database"
)

Advanced Usage:

import langchain
from langchain.cache import SingleStoreDBSemanticCache
from langchain.embeddings import OpenAIEmbeddings

langchain.llm_cache = = SingleStoreDBSemanticCache(
    embeddings=OpenAIEmbeddings(),
    use_vector_index=True,
    host="127.0.0.1",
    port=3306,
    user="user",
    password="password",
    database="db",
    table_name="my_custom_table",
    pool_size=10,
    timeout=60,
)

Methods

__init__(embedding,Β *[,Β cache_table_prefix,Β ...])

Initialize with necessary components.

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__(embedding: Embeddings, *, cache_table_prefix: str = 'cache_', search_threshold: float = 0.2, **kwargs: Any)[source]#

Initialize with necessary components.

Parameters:
  • embedding (Embeddings) – A text embedding model.

  • cache_table_prefix (str, optional) – Prefix for the cache table name. Defaults to β€œcache_”.

  • search_threshold (float, optional) – The minimum similarity score for a search result to be considered a match. Defaults to 0.2.

  • store (Following arguments pertrain to the SingleStoreDB vector) –

  • distance_strategy (DistanceStrategy, optional) –

    Determines the strategy employed for calculating the distance between vectors in the embedding space. Defaults to DOT_PRODUCT. Available options are: - DOT_PRODUCT: Computes the scalar product of two vectors.

    This is the default behavior

    • EUCLIDEAN_DISTANCE: Computes the Euclidean distance between

      two vectors. This metric considers the geometric distance in the vector space, and might be more suitable for embeddings that rely on spatial relationships. This metric is not compatible with the WEIGHTED_SUM search strategy.

  • content_field (str, optional) – Specifies the field to store the content. Defaults to β€œcontent”.

  • metadata_field (str, optional) – Specifies the field to store metadata. Defaults to β€œmetadata”.

  • vector_field (str, optional) – Specifies the field to store the vector. Defaults to β€œvector”.

  • id_field (str, optional) – Specifies the field to store the id. Defaults to β€œid”.

  • use_vector_index (bool, optional) – Toggles the use of a vector index. Works only with SingleStoreDB 8.5 or later. Defaults to False. If set to True, vector_size parameter is required to be set to a proper value.

  • vector_index_name (str, optional) – Specifies the name of the vector index. Defaults to empty. Will be ignored if use_vector_index is set to False.

  • vector_index_options (dict, optional) –

    Specifies the options for the vector index. Defaults to {}. Will be ignored if use_vector_index is set to False. The options are: index_type (str, optional): Specifies the type of the index.

    Defaults to IVF_PQFS.

    For more options, please refer to the SingleStoreDB documentation: https://docs.singlestore.com/cloud/reference/sql-reference/vector-functions/vector-indexing/

  • vector_size (int, optional) – Specifies the size of the vector. Defaults to 1536. Required if use_vector_index is set to True. Should be set to the same value as the size of the vectors stored in the vector_field.

  • pool (Following arguments pertain to the connection) –

  • pool_size (int, optional) – Determines the number of active connections in the pool. Defaults to 5.

  • max_overflow (int, optional) – Determines the maximum number of connections allowed beyond the pool_size. Defaults to 10.

  • timeout (float, optional) – Specifies the maximum wait time in seconds for establishing a connection. Defaults to 30.

  • connection (database) –

  • host (str, optional) – Specifies the hostname, IP address, or URL for the database connection. The default scheme is β€œmysql”.

  • user (str, optional) – Database username.

  • password (str, optional) – Database password.

  • port (int, optional) – Database port. Defaults to 3306 for non-HTTP connections, 80 for HTTP connections, and 443 for HTTPS connections.

  • database (str, optional) – Database name.

  • the (Additional optional arguments provide further customization over) –

  • connection –

  • pure_python (bool, optional) – Toggles the connector mode. If True, operates in pure Python mode.

  • local_infile (bool, optional) – Allows local file uploads.

  • charset (str, optional) – Specifies the character set for string values.

  • ssl_key (str, optional) – Specifies the path of the file containing the SSL key.

  • ssl_cert (str, optional) – Specifies the path of the file containing the SSL certificate.

  • ssl_ca (str, optional) – Specifies the path of the file containing the SSL certificate authority.

  • ssl_cipher (str, optional) – Sets the SSL cipher list.

  • ssl_disabled (bool, optional) – Disables SSL usage.

  • ssl_verify_cert (bool, optional) – Verifies the server’s certificate. Automatically enabled if ssl_ca is specified.

  • ssl_verify_identity (bool, optional) – Verifies the server’s identity.

  • conv (dict[int, Callable], optional) – A dictionary of data conversion functions.

  • credential_type (str, optional) – Specifies the type of authentication to use: auth.PASSWORD, auth.JWT, or auth.BROWSER_SSO.

  • autocommit (bool, optional) – Enables autocommits.

  • results_type (str, optional) – Determines the structure of the query results: tuples, namedtuples, dicts.

  • results_format (str, optional) – Deprecated. This option has been renamed to results_type.

  • kwargs (Any) –

Examples

Basic Usage:

import langchain
from langchain.cache import SingleStoreDBSemanticCache
from langchain.embeddings import OpenAIEmbeddings

langchain.llm_cache = SingleStoreDBSemanticCache(
    embedding=OpenAIEmbeddings(),
    host="https://user:password@127.0.0.1:3306/database"
)

Advanced Usage:

import langchain
from langchain.cache import SingleStoreDBSemanticCache
from langchain.embeddings import OpenAIEmbeddings

langchain.llm_cache = = SingleStoreDBSemanticCache(
    embeddings=OpenAIEmbeddings(),
    use_vector_index=True,
    host="127.0.0.1",
    port=3306,
    user="user",
    password="password",
    database="db",
    table_name="my_custom_table",
    pool_size=10,
    timeout=60,
)
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 SingleStoreDBSemanticCache