langchain-redis¶
langchain_redis
¶
LangCacheSemanticCache
¶
Bases: BaseCache
Managed LangCache-backed semantic cache.
This uses redisvl.extensions.cache.llm.LangCacheSemanticCache (a wrapper over the
managed LangCache API). The optional dependency langcache must be installed at
runtime when this class is used.
Install with either pip install 'langchain-redis[langcache]' or
pip install langcache.
| PARAMETER | DESCRIPTION |
|---|---|
distance_threshold
|
Maximum distance for semantic matches.
TYPE:
|
ttl
|
Cache TTL in seconds. If
TYPE:
|
name
|
Cache name used by LangCache. Defaults to
TYPE:
|
server_url
|
LangCache API endpoint. If not set, a default managed endpoint is used; prefer the server URL provided for your cache.
TYPE:
|
api_key
|
API key for LangCache authentication.
TYPE:
|
cache_id
|
Required LangCache instance identifier.
TYPE:
|
use_exact_search
|
Enable exact match search.
TYPE:
|
use_semantic_search
|
Enable semantic search.
TYPE:
|
distance_scale
|
Distance scaling mode.
TYPE:
|
**kwargs
|
Additional options forwarded to the LangCache wrapper.
TYPE:
|
Example
Notes
- Embeddings are computed server-side in LangCache; client-side embeddings are not used.
- Per-entry TTL is ignored; cache-level TTL applies if set.
| METHOD | DESCRIPTION |
|---|---|
lookup |
Lookup using LangCache's check API. |
update |
Store using LangCache's store API via redisvl wrapper. |
clear |
Clear all entries via the wrapper's clear API. |
alookup |
Async lookup through LangCache's acheck API. |
aupdate |
Async store using LangCache's astore API via redisvl wrapper. |
aclear |
Async clear via the wrapper's aclear API. |
RedisCache
¶
Bases: BaseCache
Redis cache implementation for LangChain.
This class provides a Redis-based caching mechanism for LangChain, allowing storage and retrieval of language model responses.
| ATTRIBUTE | DESCRIPTION |
|---|---|
redis |
The Redis client instance.
TYPE:
|
ttl |
Time-to-live for cache entries in seconds. If
TYPE:
|
prefix |
Prefix for all keys stored in Redis.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
redis_url
|
The URL of the Redis instance to connect to.
TYPE:
|
ttl
|
Time-to-live for cache entries in seconds.
TYPE:
|
prefix
|
Prefix for all keys stored in Redis.
TYPE:
|
redis_client
|
An existing Redis client instance. If provided,
TYPE:
|
Example
from langchain_redis import RedisCache
from langchain_core.globals import set_llm_cache
# Create a Redis cache instance
redis_cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600)
# Set it as the global LLM cache
set_llm_cache(redis_cache)
# Now, when you use an LLM, it will automatically use this cache
Note
- This cache implementation uses Redis JSON capabilities to store structured data.
- The cache key is created using MD5 hashes of the prompt and LLM string.
- If TTL is set, cache entries will automatically expire after the specified duration.
- The prefix can be used to namespace cache entries.
| METHOD | DESCRIPTION |
|---|---|
alookup |
Async look up based on |
aupdate |
Async update cache based on |
aclear |
Async clear cache that can take additional keyword arguments. |
lookup |
Look up the result of a previous language model call in the Redis cache. |
update |
Update the cache with a new result for a given prompt and language model. |
clear |
Clear all entries in the Redis cache that match the cache prefix. |
alookup
async
¶
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).
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
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.
TYPE:
|
llm_string
|
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.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RETURN_VAL_TYPE | None
|
On a cache miss, return |
RETURN_VAL_TYPE | None
|
The cached value is a list of |
aupdate
async
¶
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.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
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.
TYPE:
|
llm_string
|
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.
TYPE:
|
return_val
|
The value to be cached. The value is a list of
TYPE:
|
aclear
async
¶
aclear(**kwargs: Any) -> None
Async clear cache that can take additional keyword arguments.
lookup
¶
Look up the result of a previous language model call in the Redis cache.
This method checks if there's a cached result for the given prompt and language model combination.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The input prompt for which to look up the cached result.
TYPE:
|
llm_string
|
A string representation of the language model and its parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RETURN_VAL_TYPE | None
|
The cached result if found, or The result is typically a list containing a single |
Example
Note
- The method uses an MD5 hash of the
promptandllm_stringto create the cache key. - The cached value is stored as JSON and parsed back into a
Generationobject. - If the key exists but the value is
Noneor cannot be parsed,Noneis returned. - This method is typically called internally by LangChain, but can be used directly for manual cache interactions.
update
¶
Update the cache with a new result for a given prompt and language model.
This method stores a new result in the Redis cache for the specified prompt and language model combination.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The input prompt associated with the result.
TYPE:
|
llm_string
|
A string representation of the language model and its parameters.
TYPE:
|
return_val
|
The result to be cached, typically a list
containing a single
TYPE:
|
Example
Note
- The method uses an MD5 hash of the
promptandllm_stringto create the cache key. - The result is stored as JSON in Redis.
- If a TTL (Time To Live) was specified when initializing the cache, it will be applied to this entry.
- This method is typically called internally by LangChain after a language model generates a response, but it can be used directly for manual cache updates.
- If the cache already contains an entry for this
promptandllm_string, it will be overwritten.
clear
¶
clear(**kwargs: Any) -> None
Clear all entries in the Redis cache that match the cache prefix.
This method removes all cache entries that start with the specified prefix.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Additional keyword arguments. Currently not used, but included for potential future extensions.
TYPE:
|
Example
cache = RedisCache(
redis_url="redis://localhost:6379",
prefix="my_cache"
)
# Add some entries to the cache
cache.update("prompt1", "llm1", [Generation(text="Result 1")])
cache.update("prompt2", "llm2", [Generation(text="Result 2")])
# Clear all entries
cache.clear()
# After this, all entries with keys starting with "my_cache:"
# will be removed
Note
- This method uses Redis SCAN to iterate over keys, which is safe for large datasets.
- It deletes keys in batches of 100 to optimize performance.
- Only keys that start with the specified prefix (default is
'redis:') will be deleted. - This operation is irreversible. Make sure you want to clear all cached data before calling this method.
- If no keys match the prefix, the method will complete without any errors.
RedisSemanticCache
¶
Bases: BaseCache
Redis-based semantic cache implementation for LangChain.
This class provides a semantic caching mechanism using Redis and vector similarity search. It allows for storing and retrieving language model responses based on the semantic similarity of prompts, rather than exact string matching.
| ATTRIBUTE | DESCRIPTION |
|---|---|
redis |
The Redis client instance.
TYPE:
|
embeddings |
The embedding function to use for encoding prompts.
TYPE:
|
cache |
The underlying RedisVL semantic cache instance.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
embeddings
|
The embedding function to use for encoding prompts.
TYPE:
|
redis_url
|
The URL of the Redis instance to connect to.
TYPE:
|
distance_threshold
|
The maximum distance for considering a cache hit.
TYPE:
|
ttl
|
Time-to-live for cache entries in seconds.
TYPE:
|
name
|
Name for the cache index. Defaults to
TYPE:
|
prefix
|
Prefix for all keys stored in Redis. Defaults to
TYPE:
|
redis_client
|
An existing Redis client instance. If provided,
TYPE:
|
Example
from langchain_redis import RedisSemanticCache
from langchain_openai import OpenAIEmbeddings
from langchain_core.globals import set_llm_cache
embeddings = OpenAIEmbeddings()
semantic_cache = RedisSemanticCache(
embeddings=embeddings,
redis_url="redis://localhost:6379",
distance_threshold=0.1
)
set_llm_cache(semantic_cache)
# Now, when you use an LLM, it will automatically use this semantic cache
Note
- This cache uses vector similarity search to find semantically similar prompts.
- The distance_threshold determines how similar a prompt must be to trigger a cache hit.
- Lowering the distance_threshold increases precision but may reduce cache hits.
- The cache uses the RedisVL library for efficient vector storage and retrieval.
- Semantic caching can be more flexible than exact matching, allowing cache hits for prompts that are semantically similar but not identical.
| METHOD | DESCRIPTION |
|---|---|
lookup |
Look up the result of a previous language model call in the Redis semantic |
update |
Update the semantic cache with a new result for a given prompt and |
clear |
Clear all entries in the Redis semantic cache. |
name |
Get the name of the semantic cache index. |
alookup |
Async look up based on |
aupdate |
Async update cache based on |
aclear |
Async clear cache that can take additional keyword arguments. |
lookup
¶
Look up the result of a previous language model call in the Redis semantic cache.
This method checks if there's a cached result for a semantically similar prompt and the same language model combination.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The input prompt for which to look up the cached result.
TYPE:
|
llm_string
|
A string representation of the language model and its parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RETURN_VAL_TYPE | None
|
The cached result if a semantically similar prompt is found, or The result is typically a list containing a single |
Example
from langchain_openai import OpenAIEmbeddings
cache = RedisSemanticCache(
embeddings=OpenAIEmbeddings(),
redis_url="redis://localhost:6379"
)
prompt = "What's the capital city of France?"
llm_string = "openai/gpt-3.5-turbo"
result = cache.lookup(prompt, llm_string)
if result:
print("Semantic cache hit:", result[0].text)
else:
print("Semantic cache miss")
Note
- This method uses vector similarity search to find semantically similar prompts.
- The prompt is embedded using the provided embedding function.
- The method checks for cached results within the distance threshold specified during cache initialization.
- If multiple results are within the threshold, the most similar one is returned.
- The
llm_stringis used to ensure the cached result is from the same language model. - This method is typically called internally by LangChain, but can be used directly for manual cache interactions.
- Unlike exact matching, this may return results for prompts that are semantically similar but not identical to the input.
update
¶
Update the semantic cache with a new result for a given prompt and language model.
This method stores a new result in the Redis semantic cache for the specified prompt and language model combination, using vector embedding for semantic similarity.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
The input prompt associated with the result.
TYPE:
|
llm_string
|
A string representation of the language model and its parameters.
TYPE:
|
return_val
|
The result to be cached, typically a list
containing a single
TYPE:
|
Example
from langchain_core.outputs import Generation
from langchain_openai import OpenAIEmbeddings
cache = RedisSemanticCache(
embeddings=OpenAIEmbeddings(),
redis_url="redis://localhost:6379"
)
prompt = "What is the capital of France?"
llm_string = "openai/gpt-3.5-turbo"
result = [Generation(text="The capital of France is Paris.")]
cache.update(prompt, llm_string, result)
Note
- The method uses the provided embedding function to convert the prompt into a vector.
- The vector, along with the
prompt,llm_string, and result, is stored in the Redis cache. - If a TTL (Time To Live) was specified when initializing the cache, it will be applied to this entry.
- This method is typically called internally by LangChain after a language model generates a response, but it can be used directly for manual cache updates.
- Unlike exact matching caches, this allows for semantic similarity lookups later.
- If the cache already contains very similar entries, this will add a new entry rather than overwriting.
- The effectiveness of the cache depends on the quality of the embedding function used.
clear
¶
clear(**kwargs: Any) -> None
Clear all entries in the Redis semantic cache.
This method removes all cache entries from the semantic cache.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Additional keyword arguments. Currently not used, but included for potential future extensions.
TYPE:
|
Example
from langchain_openai import OpenAIEmbeddings
cache = RedisSemanticCache(
embeddings=OpenAIEmbeddings(),
redis_url="redis://localhost:6379",
name="my_semantic_cache"
)
# Add some entries to the cache
cache.update(
"What is the capital of France?",
"llm1",
[Generation(text="Paris")]
)
cache.update(
"Who wrote Romeo and Juliet?",
"llm2",
[Generation(text="Shakespeare")]
)
# Clear all entries
cache.clear()
# After this, all entries in the semantic cache will be removed
Note
- This method clears all entries in the semantic cache, regardless of their content or similarity.
- It uses the underlying cache implementation's clear method, which efficiently removes all entries.
- This operation is irreversible. Make sure you want to clear all cached data before calling this method.
- After clearing, the cache will be empty, but the index structure is maintained and ready for new entries.
- This method is useful for resetting the cache or clearing out old data, especially if the nature of the queries or the embedding model has changed significantly.
name
¶
name() -> str
Get the name of the semantic cache index.
This method returns the name of the index used for the semantic cache in Redis.
| RETURNS | DESCRIPTION |
|---|---|
str
|
The name of the semantic cache index.
TYPE:
|
Example
Note
- The index name is set during the initialization of the
RedisSemanticCache. - If no custom name was provided during initialization, a default name is used.
- This name is used internally to identify and manage the semantic cache in Redis.
- Knowing the index name can be useful for debugging or for direct interactions with the Redis database outside of this cache interface.
alookup
async
¶
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).
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
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.
TYPE:
|
llm_string
|
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.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RETURN_VAL_TYPE | None
|
On a cache miss, return |
aupdate
async
¶
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.
| PARAMETER | DESCRIPTION |
|---|---|
prompt
|
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.
TYPE:
|
llm_string
|
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.
TYPE:
|
return_val
|
The value to be cached. The value is a list of
TYPE:
|
RedisChatMessageHistory
¶
Bases: BaseChatMessageHistory
Redis-based implementation of chat message history using RedisVL.
This class provides a way to store and retrieve chat message history using Redis with RedisVL for efficient indexing, querying, and document management.
| ATTRIBUTE | DESCRIPTION |
|---|---|
redis_client |
The Redis client instance.
TYPE:
|
session_id |
A unique identifier for the chat session.
TYPE:
|
key_prefix |
Prefix for Redis keys to namespace the messages.
TYPE:
|
ttl |
Time-to-live for message entries in seconds.
TYPE:
|
index_name |
Name of the Redis search index for message retrieval.
TYPE:
|
| PARAMETER | DESCRIPTION |
|---|---|
session_id
|
A unique identifier for the chat session.
TYPE:
|
redis_url
|
URL of the Redis instance.
TYPE:
|
key_prefix
|
Prefix for Redis keys.
TYPE:
|
ttl
|
Time-to-live for entries in seconds.
TYPE:
|
index_name
|
Name of the Redis search index.
TYPE:
|
redis_client
|
Existing Redis client instance. If provided,
TYPE:
|
overwrite_index
|
Whether to overwrite an existing index if it already exists. If
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to the Redis client.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
Example
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(
session_id="user123",
redis_url="redis://localhost:6379",
ttl=3600 # Expire chat history after 1 hour
)
# Add messages to the history
history.add_message(HumanMessage(content="Hello, AI!"))
history.add_message(
AIMessage(content="Hello, human! How can I assist you today?")
)
# Retrieve all messages
messages = history.messages
for message in messages:
print(f"{message.type}: {message.content}")
# Clear the history for the session
history.clear()
Note
- This class uses RedisVL for managing Redis JSON storage and search indexes, providing efficient querying and retrieval.
- A Redis search index is created to enable fast lookups and search capabilities over the chat history.
- If TTL is set, message entries will automatically expire after the specified duration.
- The
session_idis used to group messages belonging to the same conversation or user session. - RedisVL automatically handles tokenization and escaping for search queries.
| METHOD | DESCRIPTION |
|---|---|
aget_messages |
Async version of getting messages. |
add_user_message |
Convenience method for adding a human message string to the store. |
add_ai_message |
Convenience method for adding an |
add_messages |
Add a list of messages. |
aadd_messages |
Async add a list of messages. |
aclear |
Async remove all messages from the store. |
__str__ |
Return a string representation of the chat history. |
add_message |
Add a message to the chat history using RedisVL. |
clear |
Clear all messages from the chat history for the current session. |
delete |
Delete all sessions and the chat history index from Redis. |
search_messages |
Search for messages in the chat history that match the given query. |
__len__ |
Return the number of messages in the chat history for the current session. |
messages
property
¶
messages: list[BaseMessage]
Retrieve all messages for the current session, sorted by timestamp.
| RETURNS | DESCRIPTION |
|---|---|
list[BaseMessage]
|
A list of messages in chronological order. |
| RAISES | DESCRIPTION |
|---|---|
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
aget_messages
async
¶
aget_messages() -> list[BaseMessage]
Async version of getting messages.
Can over-ride this method to provide an efficient async implementation.
In general, fetching messages may involve IO to the underlying persistence layer.
| RETURNS | DESCRIPTION |
|---|---|
list[BaseMessage]
|
The messages. |
add_user_message
¶
add_user_message(message: HumanMessage | str) -> None
Convenience method for adding a human message string to the store.
Note
This is a convenience method. Code should favor the bulk add_messages
interface instead to save on round-trips to the persistence layer.
This method may be deprecated in a future release.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
The
TYPE:
|
add_ai_message
¶
Convenience method for adding an AIMessage string to the store.
Note
This is a convenience method. Code should favor the bulk add_messages
interface instead to save on round-trips to the persistence layer.
This method may be deprecated in a future release.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
The |
add_messages
¶
add_messages(messages: Sequence[BaseMessage]) -> None
Add a list of messages.
Implementations should over-ride this method to handle bulk addition of messages in an efficient manner to avoid unnecessary round-trips to the underlying store.
| PARAMETER | DESCRIPTION |
|---|---|
messages
|
A sequence of
TYPE:
|
aadd_messages
async
¶
aadd_messages(messages: Sequence[BaseMessage]) -> None
Async add a list of messages.
| PARAMETER | DESCRIPTION |
|---|---|
messages
|
A sequence of
TYPE:
|
add_message
¶
add_message(message: BaseMessage) -> None
Add a message to the chat history using RedisVL.
This method adds a new message to the Redis store for the current session using RedisVL's document loading capabilities.
| PARAMETER | DESCRIPTION |
|---|---|
message
|
The message to add to the history. This should be an instance of a class derived from
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
ValueError
|
If message is |
Example
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(
session_id="user123",
redis_url="redis://localhost:6379",
ttl=3600 # optional: set TTL to 1 hour
)
# Add a human message
history.add_message(HumanMessage(content="Hello, AI!"))
# Add an AI message
history.add_message(
AIMessage(content="Hello! How can I assist you today?")
)
# Verify messages were added
print(f"Number of messages: {len(history.messages)}")
Note
- Each message is stored as a separate entry in Redis, associated
with the current
session_id. - Messages are stored using RedisVL's JSON capabilities for efficient storage and retrieval.
- If a TTL (Time To Live) was specified when initializing the history, it will be applied to each message.
- The message's content, type, and any additional data (like timestamp) are stored.
- This method is thread-safe and can be used in concurrent environments.
- The Redis search index is automatically updated to include the new message, enabling future searches.
- Large message contents may impact performance and storage usage. Consider implementing size limits if dealing with potentially large messages.
clear
¶
Clear all messages from the chat history for the current session.
This method removes all messages associated with the current session_id from
the Redis store using RedisVL queries.
| RAISES | DESCRIPTION |
|---|---|
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
Example
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(session_id="user123", redis_url="redis://localhost:6379")
# Add some messages
history.add_message(HumanMessage(content="Hello, AI!"))
history.add_message(AIMessage(content="Hello, human!"))
# Clear the history
history.clear()
# Verify that the history is empty
assert len(history.messages) == 0
Note
- This method only clears messages for the current
session_id. - It uses RedisVL's FilterQuery to find all relevant messages and then deletes them individually using the Redis client.
- The operation removes all messages for the current session only.
- After clearing, the Redis search index is still maintained, allowing
for immediate use of the same
session_idfor new messages if needed. - This operation is irreversible. Make sure you want to remove all messages before calling this method.
delete
¶
Delete all sessions and the chat history index from Redis.
| RAISES | DESCRIPTION |
|---|---|
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
search_messages
¶
Search for messages in the chat history that match the given query.
This method performs a full-text search on the content of messages in the current session using RedisVL's TextQuery capabilities.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
The search query string to match against message content.
TYPE:
|
limit
|
The maximum number of results to return.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[dict[str, Any]]
|
A list of dictionaries, each representing a matching message. Each dictionary contains the message content and metadata. |
| RAISES | DESCRIPTION |
|---|---|
ResponseError
|
If Redis connection fails or RedisVL operations fail. |
Example
from langchain_redis import RedisChatMessageHistory
from langchain_core.messages import HumanMessage, AIMessage
history = RedisChatMessageHistory(session_id="user123", redis_url="redis://localhost:6379")
# Add some messages
history.add_message(
HumanMessage(content="Tell me about Machine Learning")
)
history.add_message(
AIMessage(content="Machine Learning is a subset of AI...")
)
history.add_message(
HumanMessage(content="What are neural networks?")
)
history.add_message(
AIMessage(
content="Neural networks are a key component of deep learning..."
)
)
# Search for messages containing "learning"
results = history.search_messages("learning", limit=5)
for result in results:
print(f"Content: {result['content']}")
print(f"Type: {result['type']}")
print("---")
Note
- The search is performed using RedisVL's TextQuery capabilities, which allows for efficient full-text search.
- The search is case-insensitive and uses Redis' default tokenization and stemming.
- Only messages from the current session (as defined by
session_id) are searched. - The returned dictionaries include all stored fields, which typically
include
'content','type', and any additional metadata stored with the message. - This method is useful for quickly finding relevant parts of a conversation without having to iterate through all messages.
RedisConfig
¶
Bases: BaseModel
Configuration class for Redis vector store settings.
This class defines the configuration parameters for setting up and interacting with a Redis vector store. It uses Pydantic for data validation and settings management.
| ATTRIBUTE | DESCRIPTION |
|---|---|
index_name |
Name of the index in Redis. Defaults to a generated ULID.
TYPE:
|
from_existing |
Whether to use an existing index. Defaults to
TYPE:
|
key_prefix |
Prefix for Redis keys. Defaults to
TYPE:
|
redis_url |
URL of the Redis instance. Defaults to
TYPE:
|
redis_client |
Pre-existing Redis client instance.
TYPE:
|
connection_args |
Additional Redis connection args. |
distance_metric |
Distance metric for vector similarity. Defaults to
TYPE:
|
indexing_algorithm |
Algorithm used for indexing. Defaults to
TYPE:
|
vector_datatype |
Data type of the vector. Defaults to
TYPE:
|
storage_type |
Storage type in Redis. Defaults to
TYPE:
|
id_field |
Field name for document ID. Defaults to
TYPE:
|
content_field |
Field name for document content. Defaults to
TYPE:
|
embedding_field |
Field name for embedding vector. Defaults to
TYPE:
|
default_tag_separator |
Separator for tag fields. Defaults to
TYPE:
|
metadata_schema |
Schema for metadata fields. |
index_schema |
Full index schema definition.
TYPE:
|
schema_path |
Path to a YAML file containing the index schema.
TYPE:
|
return_keys |
Whether to return keys after adding documents. Defaults to
TYPE:
|
custom_keys |
Custom keys for documents. |
embedding_dimensions |
Dimensionality of embedding vectors.
TYPE:
|
Example
Note
- Only one of
'index_schema','schema_path', or'metadata_schema'should be specified. - The
'key_prefix'is automatically set to'index_name'if not provided. - When
'from_existing'isTrue, it connects to an existing index instead of creating a new one. - Custom validation ensures that incompatible options are not simultaneously specified.
| METHOD | DESCRIPTION |
|---|---|
from_kwargs |
Create a |
from_schema |
Create a |
from_yaml |
Create a |
with_metadata_schema |
Create a |
from_existing_index |
Create a |
to_index_schema |
Convert the |
is_sentinel_url |
Check if the |
from_kwargs
classmethod
¶
from_kwargs(**kwargs: Any) -> RedisConfig
Create a RedisConfig object with default values, overwritten by provided
kwargs.
This class method allows for flexible creation of a RedisConfig object,
using default values where not specified and overriding with any provided
keyword arguments. If a 'schema' argument is provided, it will be set as
'index_schema' in the config.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Keyword arguments that match Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisConfig
|
A new instance of
TYPE:
|
Example
from_schema
classmethod
¶
from_schema(schema: IndexSchema, **kwargs: Any) -> RedisConfig
Create a RedisConfig object from an IndexSchema.
This class method creates a RedisConfig instance using the provided
IndexSchema, which defines the structure of the Redis index.
| PARAMETER | DESCRIPTION |
|---|---|
schema
|
An
TYPE:
|
**kwargs
|
Additional keyword arguments to override or supplement the schema-derived settings. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisConfig
|
A new instance of
TYPE:
|
Example
from redisvl.schema import IndexSchema
from langchain_redis import RedisConfig
schema = IndexSchema.from_dict({
"index": {"name": "my_index", "storage_type": "hash"},
"fields": [
{"name": "text", "type": "text"},
{
"name": "embedding",
"type": "vector",
"attrs": {"dims": 1536, "distance_metric": "cosine"}
}
]
})
config = RedisConfig.from_schema(
schema,
redis_url="redis://localhost:6379"
)
print(config.index_name) # Output: my_index
print(config.storage_type) # Output: hash
Note
- The method extracts index name, key prefix, and storage type from the schema.
- If the schema specifies a vector field, its attributes (like dimensions and distance metric) are used.
- Additional kwargs can override settings derived from the schema.
- This method is useful when you have a pre-defined index structure and want to create a matching config.
- The resulting config can be used to ensure that a
RedisVectorStorematches an existing index structure.
from_yaml
classmethod
¶
from_yaml(schema_path: str, **kwargs: Any) -> RedisConfig
Create a RedisConfig object from a YAML file containing the index schema.
This class method creates a RedisConfig instance using a YAML file that
defines the structure of the Redis index.
| PARAMETER | DESCRIPTION |
|---|---|
schema_path
|
Path to the YAML file containing the index schema definition.
TYPE:
|
**kwargs
|
Additional keyword arguments to override or supplement the schema-derived settings. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisConfig
|
A new instance of
TYPE:
|
Example
from langchain_redis import RedisConfig
# Assuming 'index_schema.yaml' contains a valid index schema
config = RedisConfig.from_yaml(
schema_path="path/to/index_schema.yaml",
redis_url="redis://localhost:6379"
)
print(config.index_name) # Output: Name defined in YAML
print(config.storage_type) # Output: Storage type defined in YAML
Note
- The YAML file should contain a valid index schema definition compatible with RedisVL.
- This method internally uses
IndexSchema.from_yaml()to parse the YAML file. - Settings derived from the YAML schema can be overridden by additional kwargs.
- This method is particularly useful when index structures are defined externally in YAML files.
- Ensure that the YAML file is correctly formatted and accessible at the given path.
- Any errors in reading or parsing the YAML file will be propagated as exceptions.
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified YAML file does not exist. |
YAMLError
|
If there are issues parsing the YAML file. |
ValueError
|
If the YAML content is not a valid index schema. |
with_metadata_schema
classmethod
¶
Create a RedisConfig object with a specified metadata schema.
This class method creates a RedisConfig instance using a provided
metadata schema, which defines the structure of additional metadata
fields in the Redis index.
| PARAMETER | DESCRIPTION |
|---|---|
metadata_schema
|
A list of dictionaries defining the metadata fields. Each dictionary should contain at least 'name' and 'type' keys. |
**kwargs
|
Additional keyword arguments to configure the Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisConfig
|
A new instance of
TYPE:
|
Example
from langchain_redis import RedisConfig
metadata_schema = [
{"name": "author", "type": "text"},
{"name": "publication_date", "type": "numeric"},
{"name": "tags", "type": "tag", "separator": ","}
]
config = RedisConfig.with_metadata_schema(
metadata_schema,
index_name="book_index",
redis_url="redis://localhost:6379",
embedding_dimensions=1536
)
print(config.metadata_schema) # Output: The metadata schema list
print(config.index_name) # Output: book_index
Note
- The metadata_schema defines additional fields beyond the default content and embedding fields.
- Common metadata field types include
'text','numeric', and'tag'. - For
'tag'fields, you can specify a custom separator using the'separator'key. - This method is useful when you need to index and search on specific metadata attributes.
- The resulting config ensures that the
RedisVectorStorewill create an index with the specified metadata fields. - Make sure the metadata schema aligns with the actual metadata you plan to store with your documents.
- This method sets only the metadata_schema; other configurations should be provided via kwargs.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
from_existing_index
classmethod
¶
from_existing_index(index_name: str, redis: Redis) -> RedisConfig
Create a RedisConfig object from an existing Redis index.
This class method creates a RedisConfig instance based on the configuration
of an existing index in Redis. It's useful for connecting to and working with
pre-existing Redis vector store indexes.
| PARAMETER | DESCRIPTION |
|---|---|
index_name
|
The name of the existing index in Redis.
TYPE:
|
redis
|
An active Redis client instance connected to the Redis server where the index exists.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisConfig
|
A new instance of
TYPE:
|
Example
from redis import Redis
from langchain_redis import RedisConfig
# Assuming an existing Redis connection
redis_client = Redis.from_url("redis://localhost:6379")
config = RedisConfig.from_existing_index(
index_name="my_existing_index",
redis_client=redis_client
)
print(config.index_name) # Output: my_existing_index
print(config.from_existing) # Output: True
Note
- This method sets the
'from_existing'attribute toTrue, indicating that the configuration is based on an existing index. - The method doesn't fetch the full schema or configuration of the existing index. It only sets up the basic parameters needed to connect to the index.
- Additional index details (like field configurations) are not retrieved and should be known or discovered separately if needed.
- This method is particularly useful when you need to work with or extend an existing Redis vector store index.
- Ensure that the provided Redis client has the necessary permissions to access the specified index.
- If the index doesn't exist, this method will still create a config, but operations using this config may fail until the index is created.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the |
ConnectionError
|
If there's an issue connecting to Redis using the provided client. |
to_index_schema
¶
Convert the RedisConfig to an IndexSchema.
This method creates an IndexSchema object based on the current configuration.
It's useful for generating a schema that can be used to create or update
a Redis index.
| RETURNS | DESCRIPTION |
|---|---|
IndexSchema
|
An
TYPE:
|
Example
from langchain_redis import RedisConfig
config = RedisConfig(
index_name="my_index",
embedding_dimensions=1536,
distance_metric="COSINE",
metadata_schema=[
{"name": "author", "type": "text"},
{"name": "year", "type": "numeric"}
]
)
schema = config.to_index_schema()
print(schema.index.name)
# Output: my_index
print(len(schema.fields))
# Output: 4 (id, content, embedding, author, year)
Note
- If an
index_schemais already set, it will be returned directly. - If a
schema_pathis set, the schema will be loaded from the YAML file. - Otherwise, a new
IndexSchemais created based on the current configuration. - The resulting schema includes fields for id, content, and embedding
vector, as well as any additional fields specified in
metadata_schema. - The embedding field is configured with the specified dimensions, distance metric, and other relevant attributes.
- This method is particularly useful when you need to create a new index or validate the structure of an existing one.
- The generated schema can be used with RedisVL operations that require
an
IndexSchema.
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If essential configuration elements (like
|
RedisVectorStore
¶
Bases: VectorStore
Redis vector store integration.
Setup
Install langchain-redis and running the Redis docker container.
Key init args — indexing params:
index_name: str
Name of the index to create.
embedding: Embeddings
Embedding function to use.
distance_metric: str
Distance metric to use for similarity search. Default is 'COSINE'.
indexing_algorithm: str
Indexing algorithm to use. Default is 'FLAT'.
vector_datatype: str
Data type of the vector. Default is 'FLOAT32'.
Key init args — client params: redis_url: Optional[str] URL of the Redis instance to connect to. redis_client: Optional[Redis] Pre-existing Redis connection. ttl: Optional[int] Time-to-live for the Redis keys.
Instantiate
You can also connect to an existing Redis instance by passing in a
pre-existing Redis connection via the redis_client argument.
Instantiate from existing connection
Add Documents
from langchain_core.documents import Document
document_1 = Document(page_content="foo", metadata={"baz": "bar"})
document_2 = Document(page_content="bar", metadata={"foo": "baz"})
document_3 = Document(page_content="to be deleted")
documents = [document_1, document_2, document_3]
ids = ["1", "2", "3"]
vector_store.add_documents(documents=documents, ids=ids)
Search
Search with filter
Search with score
Use as Retriever
| METHOD | DESCRIPTION |
|---|---|
aget_by_ids |
Async get documents by their IDs. |
adelete |
Async delete by vector ID or other criteria. |
aadd_texts |
Async run more texts through the embeddings and add to the |
add_documents |
Add or update documents in the |
aadd_documents |
Async run more documents through the embeddings and add to the |
search |
Return docs most similar to query using a specified search type. |
asearch |
Async return docs most similar to query using a specified search type. |
asimilarity_search_with_score |
Async run similarity search with distance. |
similarity_search_with_relevance_scores |
Return docs and relevance scores in the range |
asimilarity_search_with_relevance_scores |
Async return docs and relevance scores in the range |
asimilarity_search |
Async return docs most similar to query. |
asimilarity_search_by_vector |
Async return docs most similar to embedding vector. |
amax_marginal_relevance_search |
Async return docs selected using the maximal marginal relevance. |
amax_marginal_relevance_search_by_vector |
Async return docs selected using the maximal marginal relevance. |
afrom_documents |
Async return |
afrom_texts |
Async return |
as_retriever |
Return |
__init__ |
Initialize the |
add_texts |
Add text documents to the vector store. |
from_texts |
Create a |
from_documents |
Create a |
from_existing_index |
Create a |
delete |
Delete ids from the vector store. |
similarity_search_by_vector |
Return docs most similar to embedding vector. |
similarity_search |
Return docs most similar to query. |
similarity_search_with_score_by_vector |
Return docs most similar to embedding vector. |
similarity_search_with_score |
Return documents most similar to query string, along with scores. |
max_marginal_relevance_search_by_vector |
Return docs selected using the maximal marginal relevance. |
max_marginal_relevance_search |
Return docs selected using the maximal marginal relevance. |
get_by_ids |
Get documents by their IDs. |
aget_by_ids
async
¶
Async get documents by their IDs.
The returned documents are expected to have the ID field set to the ID of the document in the vector store.
Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.
Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.
This method should NOT raise exceptions if no documents are found for some IDs.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of IDs to retrieve. |
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
adelete
async
¶
Async delete by vector ID or other criteria.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of IDs to delete. If |
**kwargs
|
Other keyword arguments that subclasses might use.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool | None
|
|
aadd_texts
async
¶
aadd_texts(
texts: Iterable[str],
metadatas: list[dict] | None = None,
*,
ids: list[str] | None = None,
**kwargs: Any,
) -> list[str]
Async run more texts through the embeddings and add to the VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Iterable of strings to add to the |
metadatas
|
Optional list of metadatas associated with the texts. |
ids
|
Optional list |
**kwargs
|
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of IDs from adding the texts into the |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the number of metadatas does not match the number of texts. |
ValueError
|
If the number of IDs does not match the number of texts. |
add_documents
¶
Add or update documents in the VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
Documents to add to the |
**kwargs
|
Additional keyword arguments. If kwargs contains IDs and documents contain ids, the IDs in the kwargs will receive precedence.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of IDs of the added texts. |
aadd_documents
async
¶
search
¶
Return docs most similar to query using a specified search type.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
search_type
|
Type of search to perform. Can be
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
asearch
async
¶
Async return docs most similar to query using a specified search type.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
search_type
|
Type of search to perform. Can be
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If |
asimilarity_search_with_score
async
¶
similarity_search_with_relevance_scores
¶
similarity_search_with_relevance_scores(
query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]
Return docs and relevance scores in the range [0, 1].
0 is dissimilar, 1 is most similar.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Kwargs to be passed to similarity search. Should include
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
List of tuples of |
asimilarity_search_with_relevance_scores
async
¶
asimilarity_search_with_relevance_scores(
query: str, k: int = 4, **kwargs: Any
) -> list[tuple[Document, float]]
Async return docs and relevance scores in the range [0, 1].
0 is dissimilar, 1 is most similar.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Kwargs to be passed to similarity search. Should include
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[tuple[Document, float]]
|
List of tuples of |
asimilarity_search
async
¶
Async return docs most similar to query.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Input text.
TYPE:
|
k
|
Number of
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
asimilarity_search_by_vector
async
¶
Async return docs most similar to embedding vector.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
amax_marginal_relevance_search
async
¶
amax_marginal_relevance_search(
query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any
) -> list[Document]
Async return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to.
TYPE:
|
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
amax_marginal_relevance_search_by_vector
async
¶
amax_marginal_relevance_search_by_vector(
embedding: list[float],
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
**kwargs: Any,
) -> list[Document]
Async return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Arguments to pass to the search method.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
afrom_documents
async
classmethod
¶
afrom_documents(
documents: list[Document], embedding: Embeddings, **kwargs: Any
) -> Self
Async return VectorStore initialized from documents and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
List of |
embedding
|
Embedding function to use.
TYPE:
|
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
|
afrom_texts
async
classmethod
¶
afrom_texts(
texts: list[str],
embedding: Embeddings,
metadatas: list[dict] | None = None,
*,
ids: list[str] | None = None,
**kwargs: Any,
) -> Self
Async return VectorStore initialized from texts and embeddings.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Texts to add to the |
embedding
|
Embedding function to use.
TYPE:
|
metadatas
|
Optional list of metadatas associated with the texts. |
ids
|
Optional list of IDs associated with the texts. |
**kwargs
|
Additional keyword arguments.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Self
|
|
as_retriever
¶
as_retriever(**kwargs: Any) -> VectorStoreRetriever
Return VectorStoreRetriever initialized from this VectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
**kwargs
|
Keyword arguments to pass to the search function. Can include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
VectorStoreRetriever
|
Retriever class for |
Examples:
# Retrieve more documents with higher diversity
# Useful if your dataset has many similar documents
docsearch.as_retriever(
search_type="mmr", search_kwargs={"k": 6, "lambda_mult": 0.25}
)
# Fetch more documents for the MMR algorithm to consider
# But only return the top 5
docsearch.as_retriever(search_type="mmr", search_kwargs={"k": 5, "fetch_k": 50})
# Only retrieve documents that have a relevance score
# Above a certain threshold
docsearch.as_retriever(
search_type="similarity_score_threshold",
search_kwargs={"score_threshold": 0.8},
)
# Only get the single most similar document from the dataset
docsearch.as_retriever(search_kwargs={"k": 1})
# Use a filter to only retrieve documents from a specific paper
docsearch.as_retriever(
search_kwargs={"filter": {"paper_title": "GPT-4 Technical Report"}}
)
__init__
¶
__init__(
embeddings: Embeddings,
config: RedisConfig | None = None,
ttl: int | None = None,
**kwargs: Any,
)
Initialize the RedisVectorStore.
| PARAMETER | DESCRIPTION |
|---|---|
embeddings
|
The
TYPE:
|
config
|
Optional If not provided, a new one will be created from kwargs.
TYPE:
|
ttl
|
Optional time-to-live for Redis keys.
TYPE:
|
**kwargs
|
Additional keyword arguments for
TYPE:
|
add_texts
¶
add_texts(
texts: Iterable[str],
metadatas: list[dict[str, Any]] | None = None,
keys: list[str] | None = None,
**kwargs: Any,
) -> list[str]
Add text documents to the vector store.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
Iterable of strings to add to the vector store. |
metadatas
|
Optional list of metadata dicts associated with the texts. |
keys
|
Optional list of keys to associate with the documents. |
**kwargs
|
Additional keyword arguments. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[str]
|
List of ids from adding the texts into the vector store. |
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = RedisVectorStore(
index_name="langchain-demo",
embedding=OpenAIEmbeddings(),
redis_url="redis://localhost:6379",
)
texts = [
"The quick brown fox jumps over the lazy dog",
"Hello world",
"Machine learning is fascinating"
]
metadatas = [
{"source": "book", "page": 1},
{"source": "greeting", "language": "english"},
{"source": "article", "topic": "AI"}
]
ids = vector_store.add_texts(
texts=texts,
metadatas=metadatas,
batch_size=2
)
print(f"Added documents with ids: {ids}")
Note
- If
metadatasis provided, it must have the same length astexts. - If
keysis provided, it must have the same length astexts. - The
batch_sizeparameter can be used to control the number of documents added in each batch, which can be useful for managing memory usage when adding a large number of documents.
from_texts
classmethod
¶
from_texts(
texts: list[str],
embedding: Embeddings,
metadatas: list[dict[str, Any]] | None = None,
config: RedisConfig | None = None,
keys: list[str] | None = None,
return_keys: bool = False,
**kwargs: Any,
) -> RedisVectorStore
Create a RedisVectorStore from a list of texts.
| PARAMETER | DESCRIPTION |
|---|---|
texts
|
List of texts to add to the vector store. |
embedding
|
Embedding function to use for encoding the texts.
TYPE:
|
metadatas
|
Optional list of metadata dicts associated with the texts. |
config
|
Optional
TYPE:
|
keys
|
Optional list of keys to associate with the documents. |
return_keys
|
Whether to return the keys of the added documents.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisVectorStore
|
A new
TYPE:
|
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
texts = [
"The quick brown fox jumps over the lazy dog",
"Hello world",
"Machine learning is fascinating"
]
metadatas = [
{"source": "book", "page": 1},
{"source": "greeting", "language": "english"},
{"source": "article", "topic": "AI"}
]
embeddings = OpenAIEmbeddings()
vector_store = RedisVectorStore.from_texts(
texts=texts,
embedding=embeddings,
metadatas=metadatas,
index_name="langchain-demo",
redis_url="redis://localhost:6379",
distance_metric="COSINE"
)
# Now you can use the vector_store for similarity search
results = vector_store.similarity_search("AI and machine learning", k=1)
print(results[0].page_content)
Note
- This method creates a new
RedisVectorStoreinstance and adds the provided texts to it. - If
metadatasis provided, it must have the same length astexts. - If
keysis provided, it must have the same length astexts. - The
return_keysparameter determines whether the method returns just theRedisVectorStoreinstance or a tuple of(RedisVectorStore, List[str])where the second element is the list of keys for the added documents.
from_documents
classmethod
¶
from_documents(
documents: list[Document],
embedding: Embeddings,
config: RedisConfig | None = None,
return_keys: bool = False,
**kwargs: Any,
) -> RedisVectorStore
Create a RedisVectorStore from a list of Document objects.
| PARAMETER | DESCRIPTION |
|---|---|
documents
|
List of |
embedding
|
TYPE:
|
config
|
Optional If not provided, one will be created from kwargs.
TYPE:
|
return_keys
|
Whether to return the keys of the added documents.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisVectorStore
|
A new
TYPE:
|
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
from langchain_core.documents import Document
documents = [
Document(
page_content="The quick brown fox",
metadata={"animal": "fox"}
),
Document(
page_content="jumps over the lazy dog",
metadata={"animal": "dog"}
)
]
embeddings = OpenAIEmbeddings()
vector_store = RedisVectorStore.from_documents(
documents=documents,
embedding=embeddings,
index_name="animal-docs",
redis_url="redis://localhost:6379"
)
# Now you can use the vector_store for similarity search
results = vector_store.similarity_search("quick animal", k=1)
print(results[0].page_content)
Note
- This method creates a new
RedisVectorStoreinstance and adds the provided documents to it. - The method extracts the text content and metadata from
each
Documentobject. - If a
RedisConfigobject is not provided, one will be created using the additional kwargs passed to this method. - The embedding function is used to convert the document text into vector representations for efficient similarity search.
from_existing_index
classmethod
¶
from_existing_index(
index_name: str, embedding: Embeddings, **kwargs: Any
) -> RedisVectorStore
Create a RedisVectorStore from an existing Redis Search Index.
This method allows you to connect to an already existing index in Redis, which can be useful for continuing work with previously created indexes or for connecting to indexes created outside of this client.
| PARAMETER | DESCRIPTION |
|---|---|
index_name
|
Name of the existing index to use.
TYPE:
|
embedding
|
Embedding function to use for encoding queries.
TYPE:
|
**kwargs
|
Additional keyword arguments to pass to Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
RedisVectorStore
|
A new
TYPE:
|
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
from redis import Redis
embeddings = OpenAIEmbeddings()
# Connect to an existing index
vector_store = RedisVectorStore.from_existing_index(
index_name="my-existing-index",
embedding=embeddings,
redis_url="redis://localhost:6379",
vector_query_field="embedding",
content_field="text"
)
# Now you can use the vector_store for similarity search
results = vector_store.similarity_search("AI and machine learning", k=1)
print(results[0].page_content)
Note
- This method assumes that the index already exists in Redis.
- The embedding function provided should be compatible with the embeddings stored in the existing index.
- If you're using custom field names for vectors or content in your
existing index, make sure to specify them using
vector_query_fieldandcontent_fieldrespectively. - This method is useful for scenarios where you want to reuse an existing index, such as when the index was created by another process or when you want to use the same index across different sessions or applications.
delete
¶
Delete ids from the vector store.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
Optional list of ids of the documents to delete. |
**kwargs
|
Additional keyword arguments (not used in the current implementation).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool | None
|
Optional[bool]: |
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = RedisVectorStore(
index_name="langchain-demo",
embedding=OpenAIEmbeddings(),
redis_url="redis://localhost:6379",
)
# Assuming documents with these ids exist in the store
ids_to_delete = ["doc1", "doc2", "doc3"]
result = vector_store.delete(ids=ids_to_delete)
if result:
print("Documents were succesfully deleted")
else:
print("No Documents were deleted")
Note
- If
idsisNoneor an empty list, the method returnsFalse. - If the number of actually deleted keys differs from the number of keys
submitted for deletion the method returns
False - The method uses the
drop_keysfunctionality from RedisVL to delete the keys from Redis. - Keys are constructed by prefixing each id with the
key_prefixspecified in the configuration.
similarity_search_by_vector
¶
similarity_search_by_vector(
embedding: list[float],
k: int = 4,
filter: FilterExpression | None = None,
sort_by: str | None = None,
**kwargs: Any,
) -> list[Document]
Return docs most similar to embedding vector.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
filter
|
Optional
TYPE:
|
sort_by
|
Optional
TYPE:
|
**kwargs
|
Other keyword arguments. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
similarity_search
¶
similarity_search(
query: str,
k: int = 4,
filter: FilterExpression | None = None,
sort_by: str | None = None,
**kwargs: Any,
) -> list[Document]
Return docs most similar to query.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to.
TYPE:
|
k
|
Number of
TYPE:
|
filter
|
Optional
TYPE:
|
sort_by
|
Optional
TYPE:
|
**kwargs
|
Other keyword arguments to pass to the search function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
similarity_search_with_score_by_vector
¶
similarity_search_with_score_by_vector(
embedding: list[float],
k: int = 4,
filter: FilterExpression | None = None,
sort_by: str | None = None,
**kwargs: Any,
) -> Sequence[Any]
Return docs most similar to embedding vector.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
filter
|
Optional
TYPE:
|
sort_by
|
Optional
TYPE:
|
**kwargs
|
Other keyword arguments. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Sequence[Any]
|
List of tuples of |
similarity_search_with_score
¶
similarity_search_with_score(
query: str,
k: int = 4,
filter: FilterExpression | None = None,
sort_by: str | None = None,
**kwargs: Any,
) -> Sequence[Any]
Return documents most similar to query string, along with scores.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to.
TYPE:
|
k
|
Number of
TYPE:
|
filter
|
Optional
TYPE:
|
sort_by
|
Optional
TYPE:
|
**kwargs
|
Other keyword arguments to pass to the search function. Common kwargs include:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Sequence[Any]
|
List of tuples of |
Example
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
vector_store = RedisVectorStore(
index_name="langchain-demo",
embedding=OpenAIEmbeddings(),
redis_url="redis://localhost:6379",
)
results = vector_store.similarity_search_with_score(
"What is machine learning?",
k=2,
filter=None
)
for doc, score in results:
print(f"Score: {score}")
print(f"Content: {doc.page_content}")
print(f"Metadata: {doc.metadata}\n")
Note
- The method returns scores along with documents. Lower scores indicate higher similarity.
- The actual search is performed using the vector representation of the query, which is why an embedding function must be provided during initialization.
- The
filterparameter allows for additional filtering of results based on metadata. - If
return_allis set toTrue, all fields stored in Redis will be returned, which may include non-indexed fields.
max_marginal_relevance_search_by_vector
¶
max_marginal_relevance_search_by_vector(
embedding: list[float],
k: int = 4,
fetch_k: int = 20,
lambda_mult: float = 0.5,
**kwargs: Any,
) -> list[Document]
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
embedding
|
Embedding to look up documents similar to. |
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Other keyword arguments to pass to the search function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
max_marginal_relevance_search
¶
max_marginal_relevance_search(
query: str, k: int = 4, fetch_k: int = 20, lambda_mult: float = 0.5, **kwargs: Any
) -> list[Document]
Return docs selected using the maximal marginal relevance.
Maximal marginal relevance optimizes for similarity to query AND diversity among selected documents.
| PARAMETER | DESCRIPTION |
|---|---|
query
|
Text to look up documents similar to.
TYPE:
|
k
|
Number of
TYPE:
|
fetch_k
|
Number of
TYPE:
|
lambda_mult
|
Number between
TYPE:
|
**kwargs
|
Other keyword arguments to pass to the search function.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
get_by_ids
¶
Get documents by their IDs.
The returned documents are expected to have the ID field set to the ID of the document in the vector store.
Fewer documents may be returned than requested if some IDs are not found or if there are duplicated IDs.
Users should not assume that the order of the returned documents matches the order of the input IDs. Instead, users should rely on the ID field of the returned documents.
This method should NOT raise exceptions if no documents are found for some IDs.
| PARAMETER | DESCRIPTION |
|---|---|
ids
|
List of ids to retrieve. |
| RETURNS | DESCRIPTION |
|---|---|
list[Document]
|
List of |
Added in langchain-redis 0.1.2