Tony20100/langchain_full huggingface dataset:
Parameters
kwargs (Any) – 
Return type
None
lookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
Look up based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Sequence[Generation]]
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
langchain_community.cache.SQLAlchemyMd5Cache¶
class langchain_community.cache.SQLAlchemyMd5Cache(engine: ~sqlalchemy.engine.base.Engine, cache_schema: ~typing.Type[~langchain_community.cache.FullMd5LLMCache] = <class 'langchain_community.cache.FullMd5LLMCache'>)[source]¶
Cache that uses SQAlchemy as a backend.
Initialize by creating all tables.
Methods
__init__(engine[, cache_schema])
Initialize by creating all tables.
aclear(**kwargs)
Clear cache that can take additional keyword arguments.
alookup(prompt, llm_string)
Look up based on prompt and llm_string.
aupdate(prompt, llm_string, return_val)
Update cache based on prompt and llm_string.
clear(**kwargs)
Clear cache.
get_md5(input_string)
lookup(prompt, llm_string)
Look up based on prompt and llm_string.
update(prompt, llm_string, return_val)
Update based on prompt and llm_string.
Parameters
engine (Engine) – 
cache_schema (Type[FullMd5LLMCache]) – 
__init__(engine: ~sqlalchemy.engine.base.Engine, cache_schema: ~typing.Type[~langchain_community.cache.FullMd5LLMCache] = <class 'langchain_community.cache.FullMd5LLMCache'>)[source]¶
Initialize by creating all tables.
Parameters
engine (Engine) – 
cache_schema (Type[FullMd5LLMCache]) – 
async aclear(**kwargs: Any) → None¶
Clear cache that can take additional keyword arguments.
Parameters
kwargs (Any) – 
Return type
None
async alookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]]¶
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
Optional[Sequence[Generation]]
async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) → None¶
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]¶
Return type
None
clear(**kwargs: Any) → None[source]¶
Clear cache.
Parameters
kwargs (Any) – 
Return type
None
static get_md5(input_string: str) → str[source]¶
Parameters
input_string (str) – 
Return type
str
lookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
Look up based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Sequence[Generation]]
update(prompt: str, llm_string: str, return_val: Sequence[Generation]) → None[source]¶
Update based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
return_val (Sequence[Generation]) – 
Return type
None
langchain_community.cache.InMemoryCache¶
class langchain_community.cache.InMemoryCache[source]¶
Cache that stores things in memory.
Initialize with empty cache.
Methods
__init__()
Initialize with empty cache.
aclear(**kwargs)
Clear cache.
alookup(prompt, llm_string)
Look up based on prompt and llm_string.
aupdate(prompt, llm_string, return_val)
Update cache based on prompt and llm_string.
clear(**kwargs)
Clear cache.
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__() → None[source]¶
Initialize with empty cache.
Return type
None
async aclear(**kwargs: Any) → None[source]¶
Clear cache.
Parameters
kwargs (Any) – 
Return type
None
async alookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
Look up based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Sequence[Generation]]
async aupdate(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
clear(**kwargs: Any) → None[source]¶
Clear cache.
Parameters
kwargs (Any) – 
Return type
None
lookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
Look up based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Sequence[Generation]]
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 InMemoryCache¶
How to cache LLM responses
How to cache chat model responses
Model caches
langchain_community.cache.RedisSemanticCache¶
class langchain_community.cache.RedisSemanticCache(redis_url: str, embedding: Embeddings, score_threshold: float = 0.2)[source]¶
Cache that uses Redis as a vector-store backend.
Initialize by passing in the init GPTCache func
Parameters
redis_url (str) – URL to connect to Redis.
embedding (Embedding) – Embedding provider for semantic encoding and search.
score_threshold (float, 0.2) – 
Example:
from langchain_community.globals import set_llm_cache
from langchain_community.cache import RedisSemanticCache
from langchain_community.embeddings import OpenAIEmbeddings
set_llm_cache(RedisSemanticCache(
    redis_url="redis://localhost:6379",
    embedding=OpenAIEmbeddings()
))
Attributes
DEFAULT_SCHEMA
Methods
__init__(redis_url, embedding[, score_threshold])
Initialize by passing in the init GPTCache func
aclear(**kwargs)
Clear cache that can take additional keyword arguments.
alookup(prompt, llm_string)
Look up based on prompt and llm_string.
aupdate(prompt, llm_string, return_val)
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__(redis_url: str, embedding: Embeddings, score_threshold: float = 0.2)[source]¶
Initialize by passing in the init GPTCache func
Parameters
redis_url (str) – URL to connect to Redis.
embedding (Embedding) – Embedding provider for semantic encoding and search.
embedding (Embedding) – Embedding provider for semantic encoding and search.
score_threshold (float, 0.2) – 
Example:
from langchain_community.globals import set_llm_cache
from langchain_community.cache import RedisSemanticCache
from langchain_community.embeddings import OpenAIEmbeddings
set_llm_cache(RedisSemanticCache(
    redis_url="redis://localhost:6379",
    embedding=OpenAIEmbeddings()
))
async aclear(**kwargs: Any) → None¶
Clear cache that can take additional keyword arguments.
Parameters
kwargs (Any) – 
Return type
None
async alookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]]¶
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
Optional[Sequence[Generation]]
async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) → None¶
Update cache based on prompt and llm_string.
The prompt and llm_string are used to generate a key for the cache.
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) → Optional[Sequence[Generation]][source]¶
Look up based on prompt and llm_string.
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Sequence[Generation]]
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 RedisSemanticCache¶
Model caches
Redis
langchain_astradb.cache.AstraDBSemanticCache¶
class langchain_astradb.cache.AstraDBSemanticCache(*, collection_name: str = 'langchain_astradb_semantic_cache', token: Optional[str] = None, api_endpoint: Optional[str] = None, astra_db_client: Optional[AstraDB] = None, async_astra_db_client: Optional[AsyncAstraDB] = None, namespace: Optional[str] = None, setup_mode: SetupMode = SetupMode.SYNC, pre_delete_collection: bool = False, 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).
The default score threshold is tuned to the default metric.
Tune it carefully yourself if switching to another distance metric.
Parameters
collection_name (str) – name of the Astra DB collection to create/use.
token (Optional[str]) – API token for Astra DB usage. If not provided, the environment
variable ASTRA_DB_APPLICATION_TOKEN is inspected.
api_endpoint (Optional[str]) – full URL to the API endpoint, such as
https://<DB-ID>-us-east1.apps.astra.datastax.com. If not provided,
the environment variable ASTRA_DB_API_ENDPOINT is inspected.
astra_db_client (Optional[AstraDB]) – alternative to token+api_endpoint,
you can pass an already-created ‘astrapy.db.AstraDB’ instance.
you can pass an already-created ‘astrapy.db.AstraDB’ instance.
async_astra_db_client (Optional[AsyncAstraDB]) – alternative to token+api_endpoint,
you can pass an already-created ‘astrapy.db.AsyncAstraDB’ instance.
namespace (Optional[str]) – namespace (aka keyspace) where the collection is created.
If not provided, the environment variable ASTRA_DB_KEYSPACE is
inspected. Defaults to the database’s “default namespace”.
setup_mode (SetupMode) – mode used to create the Astra DB collection (SYNC, ASYNC or
OFF).
pre_delete_collection (bool) – whether to delete the collection
before creating it. If False and the collection already exists,
the collection will be used as is.
embedding (Embeddings) – Embedding provider for semantic encoding and search.
metric (Optional[str]) – the function to use for evaluating similarity of text embeddings.
Defaults to ‘cosine’ (alternatives: ‘euclidean’, ‘dot_product’)
similarity_threshold (float) – the minimum similarity for accepting a
(semantic-search) match.
Methods
__init__(*[, collection_name, token, ...])
Cache that uses Astra DB as a vector-store backend for semantic (i.e.
aclear(**kwargs)
Clear cache that can take additional keyword arguments.
adelete_by_document_id(document_id)
Given this is a "similarity search" cache, an invalidation pattern that makes sense is first a lookup to get an ID, and then deleting with that ID.
alookup(prompt, llm_string)
Look up based on prompt and llm_string.
alookup_with_id(prompt, llm_string)
Look up based on prompt and llm_string.
alookup_with_id_through_llm(prompt, llm[, stop])
alookup_with_id_through_llm(prompt, llm[, stop])
aupdate(prompt, llm_string, return_val)
Update cache based on prompt and llm_string.
clear(**kwargs)
Clear cache that can take additional keyword arguments.
delete_by_document_id(document_id)
Given this is a "similarity search" cache, an invalidation pattern that makes sense is first a lookup to get an ID, and then deleting with that ID.
lookup(prompt, llm_string)
Look up based on prompt and llm_string.
lookup_with_id(prompt, llm_string)
Look up based on prompt and llm_string.
lookup_with_id_through_llm(prompt, llm[, stop])
update(prompt, llm_string, return_val)
Update cache based on prompt and llm_string.
__init__(*, collection_name: str = 'langchain_astradb_semantic_cache', token: Optional[str] = None, api_endpoint: Optional[str] = None, astra_db_client: Optional[AstraDB] = None, async_astra_db_client: Optional[AsyncAstraDB] = None, namespace: Optional[str] = None, setup_mode: SetupMode = SetupMode.SYNC, pre_delete_collection: bool = False, 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).
The default score threshold is tuned to the default metric.
The default score threshold is tuned to the default metric.
Tune it carefully yourself if switching to another distance metric.
Parameters
collection_name (str) – name of the Astra DB collection to create/use.
token (Optional[str]) – API token for Astra DB usage. If not provided, the environment
variable ASTRA_DB_APPLICATION_TOKEN is inspected.
api_endpoint (Optional[str]) – full URL to the API endpoint, such as
https://<DB-ID>-us-east1.apps.astra.datastax.com. If not provided,
the environment variable ASTRA_DB_API_ENDPOINT is inspected.
astra_db_client (Optional[AstraDB]) – alternative to token+api_endpoint,
you can pass an already-created ‘astrapy.db.AstraDB’ instance.
async_astra_db_client (Optional[AsyncAstraDB]) – alternative to token+api_endpoint,
you can pass an already-created ‘astrapy.db.AsyncAstraDB’ instance.
namespace (Optional[str]) – namespace (aka keyspace) where the collection is created.
If not provided, the environment variable ASTRA_DB_KEYSPACE is
inspected. Defaults to the database’s “default namespace”.
setup_mode (SetupMode) – mode used to create the Astra DB collection (SYNC, ASYNC or
OFF).
pre_delete_collection (bool) – whether to delete the collection
before creating it. If False and the collection already exists,
the collection will be used as is.
embedding (Embeddings) – Embedding provider for semantic encoding and search.
metric (Optional[str]) – the function to use for evaluating similarity of text embeddings.
Defaults to ‘cosine’ (alternatives: ‘euclidean’, ‘dot_product’)
similarity_threshold (float) – the minimum similarity for accepting a
(semantic-search) match.
async aclear(**kwargs: Any) → None[source]¶
async aclear(**kwargs: Any) → None[source]¶
Clear cache that can take additional keyword arguments.
Parameters
kwargs (Any) – 
Return type
None
async adelete_by_document_id(document_id: str) → None[source]¶
Given this is a “similarity search” cache, an invalidation pattern
that makes sense is first a lookup to get an ID, and then deleting
with that ID. This is for the second step.
Parameters
document_id (str) – 
Return type
None
async alookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
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
Optional[Sequence[Generation]]
async alookup_with_id(prompt: str, llm_string: str) → Optional[Tuple[str, Sequence[Generation]]][source]¶
Look up based on prompt and llm_string.
If there are hits, return (document_id, cached_entry) for the top hit
Parameters
prompt (str) –
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Tuple[str, Sequence[Generation]]]
async alookup_with_id_through_llm(prompt: str, llm: LLM, stop: Optional[List[str]] = None) → Optional[Tuple[str, Sequence[Generation]]][source]¶
Parameters
prompt (str) – 
llm (LLM) – 
stop (Optional[List[str]]) – 
Return type
Optional[Tuple[str, Sequence[Generation]]]
async aupdate(prompt: str, llm_string: str, return_val: Sequence[Generation]) → None[source]¶
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 cache that can take additional keyword arguments.
Parameters
kwargs (Any) – 
Return type
None
delete_by_document_id(document_id: str) → None[source]¶
Given this is a “similarity search” cache, an invalidation pattern
that makes sense is first a lookup to get an ID, and then deleting
that makes sense is first a lookup to get an ID, and then deleting
with that ID. This is for the second step.
Parameters
document_id (str) – 
Return type
None
lookup(prompt: str, llm_string: str) → Optional[Sequence[Generation]][source]¶
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
Optional[Sequence[Generation]]
lookup_with_id(prompt: str, llm_string: str) → Optional[Tuple[str, Sequence[Generation]]][source]¶
Look up based on prompt and llm_string.
If there are hits, return (document_id, cached_entry) for the top hit
Parameters
prompt (str) – 
llm_string (str) – 
Return type
Optional[Tuple[str, Sequence[Generation]]]
lookup_with_id_through_llm(prompt: str, llm: LLM, stop: Optional[List[str]] = None) → Optional[Tuple[str, Sequence[Generation]]][source]¶
Parameters
prompt (str) –
Parameters
prompt (str) – 
llm (LLM) – 
stop (Optional[List[str]]) – 
Return type
Optional[Tuple[str, Sequence[Generation]]]
update(prompt: str, llm_string: str, return_val: Sequence[Generation]) → None[source]¶
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
langchain_community.vectorstores.vectara.RerankConfig¶
class langchain_community.vectorstores.vectara.RerankConfig(reranker: str = 'none', rerank_k: int = 50, mmr_diversity_bias: float = 0.3)[source]¶
Configuration for Reranker.
reranker: “mmr”, “rerank_multilingual_v1” or “none”
rerank_k: number of results to fetch before reranking, defaults to 50
mmr_diversity_bias: for MMR only - a number between 0 and 1 that determines
the degree of diversity among the results with 0 corresponding
to minimum diversity and 1 to maximum diversity.
Defaults to 0.3.
Note: mmr_diversity_bias is equivalent 1-lambda_mult
where lambda_mult is the value often used in max_marginal_relevance_search()
We chose to use that since we believe it’s more intuitive to the user.
Attributes
mmr_diversity_bias
rerank_k
reranker
Methods
__init__([reranker, rerank_k, ...])
Parameters
reranker (str) – 
rerank_k (int) – 
mmr_diversity_bias (float) – 
__init__(reranker: str = 'none', rerank_k: int = 50, mmr_diversity_bias: float = 0.3) → None¶
Parameters
reranker (str) – 
rerank_k (int) – 
mmr_diversity_bias (float) – 
Return type
None
langchain_community.vectorstores.sklearn.ParquetSerializer¶
class langchain_community.vectorstores.sklearn.ParquetSerializer(persist_path: str)[source]¶
Serialize data in Apache Parquet format using the pyarrow package.
Methods
__init__(persist_path)
extension()
The file extension suggested by this serializer (without dot).
load()
Loads the data from the persist_path
save(data)
Saves the data to the persist_path
Parameters
persist_path (str) – 
__init__(persist_path: str) → None[source]¶
Parameters
persist_path (str) – 
Return type
None
classmethod extension() → str[source]¶
The file extension suggested by this serializer (without dot).
Return type
str
load() → Any[source]¶
Loads the data from the persist_path
Return type
Any
save(data: Any) → None[source]¶
Saves the data to the persist_path
Parameters
data (Any) – 
Return type
None