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