Skip to content

langchain-redis

PyPI - Version PyPI - License PyPI - Downloads

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: float DEFAULT: 0.2

ttl

Cache TTL in seconds.

If None, entries do not expire.

TYPE: int | None DEFAULT: None

name

Cache name used by LangCache.

Defaults to 'llmcache'.

TYPE: str | None DEFAULT: _DEFAULT_CACHE_NAME

server_url

LangCache API endpoint.

If not set, a default managed endpoint is used; prefer the server URL provided for your cache.

TYPE: str | None DEFAULT: None

api_key

API key for LangCache authentication.

TYPE: str | None DEFAULT: None

cache_id

Required LangCache instance identifier.

TYPE: str | None DEFAULT: None

use_exact_search

Enable exact match search.

TYPE: bool DEFAULT: True

use_semantic_search

Enable semantic search.

TYPE: bool DEFAULT: True

distance_scale

Distance scaling mode.

TYPE: Literal['normalized', 'redis'] DEFAULT: 'normalized'

**kwargs

Additional options forwarded to the LangCache wrapper.

TYPE: Any DEFAULT: {}

Example
from langchain_redis import LangCacheSemanticCache

cache = LangCacheSemanticCache(
    cache_id="your-cache-id",
    api_key="your-api-key",
    name="mycache",
    ttl=3600,
)
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.

lookup

lookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None

Lookup using LangCache's check API.

update

update(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None

Store using LangCache's store API via redisvl wrapper.

clear

clear(**kwargs: Any) -> None

Clear all entries via the wrapper's clear API.

alookup async

alookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None

Async lookup through LangCache's acheck API.

aupdate async

aupdate(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None

Async store using LangCache's astore API via redisvl wrapper.

aclear async

aclear(**kwargs: Any) -> None

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: Redis

ttl

Time-to-live for cache entries in seconds.

If None, entries don't expire.

TYPE: int | None

prefix

Prefix for all keys stored in Redis.

TYPE: str | None

PARAMETER DESCRIPTION
redis_url

The URL of the Redis instance to connect to.

TYPE: str DEFAULT: 'redis://localhost:6379'

ttl

Time-to-live for cache entries in seconds.

TYPE: int | None DEFAULT: None

prefix

Prefix for all keys stored in Redis.

TYPE: str | None DEFAULT: 'redis'

redis_client

An existing Redis client instance.

If provided, redis_url is ignored.

TYPE: Redis | None DEFAULT: None

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 prompt and llm_string.

aupdate

Async update cache based on prompt and llm_string.

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

alookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | 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).

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: str

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: str

RETURNS DESCRIPTION
RETURN_VAL_TYPE | None

On a cache miss, return None. On a cache hit, return the cached value.

RETURN_VAL_TYPE | None

The cached value is a list of Generation (or subclasses).

aupdate async

aupdate(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> 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.

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: str

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: str

return_val

The value to be cached. The value is a list of Generation (or subclasses).

TYPE: RETURN_VAL_TYPE

aclear async

aclear(**kwargs: Any) -> None

Async clear cache that can take additional keyword arguments.

lookup

lookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None

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: str

llm_string

A string representation of the language model and its parameters.

TYPE: str

RETURNS DESCRIPTION
RETURN_VAL_TYPE | None

The cached result if found, or None if not present in the cache.

The result is typically a list containing a single Generation object.

Example
cache = RedisCache(redis_url="redis://localhost:6379")
prompt = "What is the capital of France?"
llm_string = "openai/gpt-3.5-turbo"

result = cache.lookup(prompt, llm_string)
if result:
    print("Cache hit:", result[0].text)
else:
    print("Cache miss")
Note
  • The method uses an MD5 hash of the prompt and llm_string to create the cache key.
  • The cached value is stored as JSON and parsed back into a Generation object.
  • If the key exists but the value is None or cannot be parsed, None is returned.
  • This method is typically called internally by LangChain, but can be used directly for manual cache interactions.

update

update(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None

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: str

llm_string

A string representation of the language model and its parameters.

TYPE: str

return_val

The result to be cached, typically a list containing a single Generation object.

TYPE: RETURN_VAL_TYPE

Example
from langchain_core.outputs import Generation

cache = RedisCache(redis_url="redis://localhost:6379", ttl=3600)
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 an MD5 hash of the prompt and llm_string to 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 prompt and llm_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: Any DEFAULT: {}

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: Redis

embeddings

The embedding function to use for encoding prompts.

TYPE: Embeddings

cache

The underlying RedisVL semantic cache instance.

TYPE: SemanticCache

PARAMETER DESCRIPTION
embeddings

The embedding function to use for encoding prompts.

TYPE: Embeddings

redis_url

The URL of the Redis instance to connect to.

TYPE: str DEFAULT: 'redis://localhost:6379'

distance_threshold

The maximum distance for considering a cache hit.

TYPE: float DEFAULT: 0.2

ttl

Time-to-live for cache entries in seconds.

TYPE: int | None DEFAULT: None

name

Name for the cache index.

Defaults to 'llmcache'.

TYPE: str | None DEFAULT: _DEFAULT_CACHE_NAME

prefix

Prefix for all keys stored in Redis.

Defaults to 'llmcache'.

TYPE: str | None DEFAULT: _DEFAULT_CACHE_PREFIX

redis_client

An existing Redis client instance.

If provided, redis_url is ignored.

TYPE: Redis | None DEFAULT: None

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 prompt and llm_string.

aupdate

Async update cache based on prompt and llm_string.

aclear

Async clear cache that can take additional keyword arguments.

lookup

lookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | None

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: str

llm_string

A string representation of the language model and its parameters.

TYPE: str

RETURNS DESCRIPTION
RETURN_VAL_TYPE | None

The cached result if a semantically similar prompt is found, or None if no suitable match is present in the cache.

The result is typically a list containing a single Generation object.

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_string is 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(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> None

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: str

llm_string

A string representation of the language model and its parameters.

TYPE: str

return_val

The result to be cached, typically a list containing a single Generation object.

TYPE: RETURN_VAL_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: Any DEFAULT: {}

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: str

Example
from langchain_openai import OpenAIEmbeddings

cache = RedisSemanticCache(
    embeddings=OpenAIEmbeddings(),
    redis_url="redis://localhost:6379",
    name="my_custom_cache"
)

index_name = cache.name()
print(f"The semantic cache is using index: {index_name}")
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

alookup(prompt: str, llm_string: str) -> RETURN_VAL_TYPE | 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).

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: str

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: str

RETURNS DESCRIPTION
RETURN_VAL_TYPE | None

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

aupdate async

aupdate(prompt: str, llm_string: str, return_val: RETURN_VAL_TYPE) -> 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.

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: str

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: str

return_val

The value to be cached.

The value is a list of Generation objects (or subclasses).

TYPE: RETURN_VAL_TYPE

aclear async

aclear(**kwargs: Any) -> None

Async clear cache that can take additional keyword arguments.

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: Redis

session_id

A unique identifier for the chat session.

TYPE: str

key_prefix

Prefix for Redis keys to namespace the messages.

TYPE: str

ttl

Time-to-live for message entries in seconds.

TYPE: int | None

index_name

Name of the Redis search index for message retrieval.

TYPE: str

PARAMETER DESCRIPTION
session_id

A unique identifier for the chat session.

TYPE: str

redis_url

URL of the Redis instance.

TYPE: str DEFAULT: 'redis://localhost:6379'

key_prefix

Prefix for Redis keys.

TYPE: str DEFAULT: 'chat:'

ttl

Time-to-live for entries in seconds.

TYPE: int | None DEFAULT: None

index_name

Name of the Redis search index.

TYPE: str DEFAULT: 'idx:chat_history'

redis_client

Existing Redis client instance.

If provided, redis_url is ignored.

TYPE: Redis | None DEFAULT: None

overwrite_index

Whether to overwrite an existing index if it already exists.

If False and an index exists with a different key_prefix, a warning will be logged.

TYPE: bool DEFAULT: False

**kwargs

Additional keyword arguments to pass to the Redis client.

TYPE: Any DEFAULT: {}

RAISES DESCRIPTION
ValueError

If session_id is empty or None.

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_id is 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 AIMessage string to the store.

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.

id property

id: str

Return the session ID.

RETURNS DESCRIPTION
str

The session ID.

TYPE: str

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 HumanMessage to add to the store.

TYPE: HumanMessage | str

add_ai_message

add_ai_message(message: AIMessage | str) -> None

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 AIMessage to add.

TYPE: AIMessage | str

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 BaseMessage objects to store.

TYPE: Sequence[BaseMessage]

aadd_messages async

aadd_messages(messages: Sequence[BaseMessage]) -> None

Async add a list of messages.

PARAMETER DESCRIPTION
messages

A sequence of BaseMessage objects to store.

TYPE: Sequence[BaseMessage]

aclear async

aclear() -> None

Async remove all messages from the store.

__str__

__str__() -> str

Return a string representation of the chat history.

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 BaseMessage, such as HumanMessage, AIMessage, or SystemMessage.

TYPE: BaseMessage

RAISES DESCRIPTION
ResponseError

If Redis connection fails or RedisVL operations fail.

ValueError

If message is None or invalid.

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() -> None

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_id for new messages if needed.
  • This operation is irreversible. Make sure you want to remove all messages before calling this method.

delete

delete() -> None

Delete all sessions and the chat history index from Redis.

RAISES DESCRIPTION
ResponseError

If Redis connection fails or RedisVL operations fail.

search_messages

search_messages(query: str, limit: int = 10) -> list[dict[str, Any]]

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: str

limit

The maximum number of results to return.

TYPE: int DEFAULT: 10

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.

__len__

__len__() -> int

Return the number of messages in the chat history for the current session.

RETURNS DESCRIPTION
int

The number of messages in the current session.

TYPE: int

RAISES DESCRIPTION
ResponseError

If Redis connection fails or RedisVL operations fail.

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: str

from_existing

Whether to use an existing index.

Defaults to False.

TYPE: bool

key_prefix

Prefix for Redis keys.

Defaults to index_name if not set.

TYPE: str | None

redis_url

URL of the Redis instance.

Defaults to 'redis://localhost:6379'.

TYPE: str

redis_client

Pre-existing Redis client instance.

TYPE: Redis | None

connection_args

Additional Redis connection args.

TYPE: dict[str, Any] | None

distance_metric

Distance metric for vector similarity.

Defaults to 'COSINE'.

TYPE: str

indexing_algorithm

Algorithm used for indexing.

Defaults to 'FLAT'.

TYPE: str

vector_datatype

Data type of the vector.

Defaults to 'FLOAT32'.

TYPE: str

storage_type

Storage type in Redis.

Defaults to 'hash'.

TYPE: str

id_field

Field name for document ID.

Defaults to 'id'.

TYPE: str

content_field

Field name for document content.

Defaults to 'text'.

TYPE: str

embedding_field

Field name for embedding vector.

Defaults to 'embedding'.

TYPE: str

default_tag_separator

Separator for tag fields.

Defaults to '|'.

TYPE: str

metadata_schema

Schema for metadata fields.

TYPE: list[dict[str, Any]] | None

index_schema

Full index schema definition.

TYPE: IndexSchema | None

schema_path

Path to a YAML file containing the index schema.

TYPE: str | None

return_keys

Whether to return keys after adding documents.

Defaults to False.

TYPE: bool

custom_keys

Custom keys for documents.

TYPE: list[str] | None

embedding_dimensions

Dimensionality of embedding vectors.

TYPE: int | None

Example
from langchain_redis import RedisConfig

config = RedisConfig(
    index_name="my_index",
    redis_url="redis://localhost:6379",
    distance_metric="COSINE",
    embedding_dimensions=1536
)

# Use this config to initialize a RedisVectorStore
vector_store = RedisVectorStore(embeddings=my_embeddings, config=config)
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' is True, 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 RedisConfig object with default values, overwritten by provided

from_schema

Create a RedisConfig object from an IndexSchema.

from_yaml

Create a RedisConfig object from a YAML file containing the index schema.

with_metadata_schema

Create a RedisConfig object with a specified metadata schema.

from_existing_index

Create a RedisConfig object from an existing Redis index.

to_index_schema

Convert the RedisConfig to an IndexSchema.

is_sentinel_url

Check if the redis_url is a Sentinel URL.

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 RedisConfig attributes. These will override default values.

Common kwargs include:

  • index_name (str): Name of the index in Redis.
  • redis_url (str): URL of the Redis instance.
  • distance_metric (str): Distance metric for vector similarity.
  • indexing_algorithm (str): Algorithm used for indexing.
  • vector_datatype (str): Data type of the vector.
  • embedding_dimensions (int): Dimensionality of embedding vectors.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisConfig

A new instance of RedisConfig with applied settings.

TYPE: RedisConfig

Example
from langchain_redis import RedisConfig

config = RedisConfig.from_kwargs(
    index_name="my_custom_index",
    redis_url="redis://custom-host:6379",
    distance_metric="COSINE",
    embedding_dimensions=768
)

print(config.index_name)  # Output: my_custom_index
print(config.distance_metric)  # Output: COSINE

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 IndexSchema object defining the structure of the Redis index.

TYPE: IndexSchema

**kwargs

Additional keyword arguments to override or supplement the schema-derived settings.

Common kwargs include:

  • redis_url (str): URL of the Redis instance.
  • distance_metric (str): Distance metric for vector similarity.
  • embedding_dimensions (int): Dimensionality of embedding vectors.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisConfig

A new instance of RedisConfig configured based on the provided schema and kwargs.

TYPE: RedisConfig

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 RedisVectorStore matches 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: str

**kwargs

Additional keyword arguments to override or supplement the schema-derived settings.

Common kwargs include:

  • redis_url (str): URL of the Redis instance.
  • distance_metric (str): Distance metric for vector similarity.
  • embedding_dimensions (int): Dimensionality of embedding vectors.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisConfig

A new instance of RedisConfig configured based on the YAML schema and kwargs.

TYPE: RedisConfig

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

with_metadata_schema(
    metadata_schema: list[dict[str, Any]], **kwargs: Any
) -> RedisConfig

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.

TYPE: list[dict[str, Any]]

**kwargs

Additional keyword arguments to configure the RedisConfig instance.

Common kwargs include:

  • index_name (str): Name of the index in Redis.
  • redis_url (str): URL of the Redis instance.
  • distance_metric (str): Distance metric for vector similarity.
  • embedding_dimensions (int): Dimensionality of embedding vectors.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisConfig

A new instance of RedisConfig configured with the specified metadata schema.

TYPE: RedisConfig

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 RedisVectorStore will 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 metadata_schema is not a list of dictionaries or if required keys are missing.

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: str

redis

An active Redis client instance connected to the Redis server where the index exists.

TYPE: Redis

RETURNS DESCRIPTION
RedisConfig

A new instance of RedisConfig configured to match the existing index.

TYPE: RedisConfig

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 to True, 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 index_name is empty or None.

ConnectionError

If there's an issue connecting to Redis using the provided client.

to_index_schema

to_index_schema() -> IndexSchema

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 IndexSchema object representing the current configuration.

TYPE: IndexSchema

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_schema is already set, it will be returned directly.
  • If a schema_path is set, the schema will be loaded from the YAML file.
  • Otherwise, a new IndexSchema is 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 embedding_dimensions) are missing.

is_sentinel_url

is_sentinel_url() -> bool

Check if the redis_url is a Sentinel URL.

RETURNS DESCRIPTION
bool

True if the URL uses the redis+sentinel:// scheme, False otherwise.

TYPE: bool

RedisVectorStore

Bases: VectorStore

Redis vector store integration.

Setup

Install langchain-redis and running the Redis docker container.

pip install -qU langchain-redis
docker run -p 6379:6379 redis/redis-stack-server:latest

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
from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings

vector_store = RedisVectorStore(
    index_name="langchain-demo",
    embedding=OpenAIEmbeddings(),
    redis_url="redis://localhost:6379",
)

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

from langchain_redis import RedisVectorStore
from langchain_openai import OpenAIEmbeddings
from redis import Redis

redis_client = Redis.from_url("redis://localhost:6379")

store = RedisVectorStore(
    embedding=OpenAIEmbeddings(),
    index_name="langchain-demo",
    redis_client=redis_client
)

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)

Example

vector_store.delete(ids=["3"])

Search

results = vector_store.similarity_search(query="foo", k=1)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
* foo [{'baz': 'bar'}]

Search with filter

from redisvl.query.filter import Tag

results = vector_store.similarity_search(
    query="foo",
    k=1,
    filter=Tag("baz") == "bar"
)
for doc in results:
    print(f"* {doc.page_content} [{doc.metadata}]")
* foo [{'baz': 'bar'}]

Search with score

results = vector_store.similarity_search_with_score(query="foo", k=1)
for doc, score in results:
    print(f"* [SIM={score:.3f}] {doc.page_content} [{doc.metadata}]")
* [SIM=0.916] foo [{'baz': 'bar'}]

Use as Retriever

retriever = vector_store.as_retriever(
    search_type="mmr",
    search_kwargs={"k": 1, "fetch_k": 2, "lambda_mult": 0.5},
)
retriever.get_relevant_documents("foo")
[Document(page_content='foo', metadata={'baz': 'bar'})]
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 VectorStore.

add_documents

Add or update documents in the VectorStore.

aadd_documents

Async run more documents through the embeddings and add to the VectorStore.

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 [0, 1].

asimilarity_search_with_relevance_scores

Async return docs and relevance scores in the range [0, 1].

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 VectorStore initialized from documents and embeddings.

afrom_texts

Async return VectorStore initialized from texts and embeddings.

as_retriever

Return VectorStoreRetriever initialized from this VectorStore.

__init__

Initialize the RedisVectorStore.

add_texts

Add text documents to the vector store.

from_texts

Create a RedisVectorStore from a list of texts.

from_documents

Create a RedisVectorStore from a list of Document objects.

from_existing_index

Create a RedisVectorStore from an existing Redis Search Index.

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.

embeddings property

embeddings: Embeddings

Access the query embedding object if available.

aget_by_ids async

aget_by_ids(ids: Sequence[str]) -> list[Document]

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.

TYPE: Sequence[str]

RETURNS DESCRIPTION
list[Document]

List of Document objects.

adelete async

adelete(ids: list[str] | None = None, **kwargs: Any) -> bool | None

Async delete by vector ID or other criteria.

PARAMETER DESCRIPTION
ids

List of IDs to delete. If None, delete all.

TYPE: list[str] | None DEFAULT: None

**kwargs

Other keyword arguments that subclasses might use.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
bool | None

True if deletion is successful, False otherwise, None if not implemented.

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 VectorStore.

TYPE: Iterable[str]

metadatas

Optional list of metadatas associated with the texts.

TYPE: list[dict] | None DEFAULT: None

ids

Optional list

TYPE: list[str] | None DEFAULT: None

**kwargs

VectorStore specific parameters.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[str]

List of IDs from adding the texts into the VectorStore.

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_documents(documents: list[Document], **kwargs: Any) -> list[str]

Add or update documents in the VectorStore.

PARAMETER DESCRIPTION
documents

Documents to add to the VectorStore.

TYPE: list[Document]

**kwargs

Additional keyword arguments.

If kwargs contains IDs and documents contain ids, the IDs in the kwargs will receive precedence.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[str]

List of IDs of the added texts.

aadd_documents async

aadd_documents(documents: list[Document], **kwargs: Any) -> list[str]

Async run more documents through the embeddings and add to the VectorStore.

PARAMETER DESCRIPTION
documents

Documents to add to the VectorStore.

TYPE: list[Document]

**kwargs

Additional keyword arguments.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[str]

List of IDs of the added texts.

search

search(query: str, search_type: str, **kwargs: Any) -> list[Document]

Return docs most similar to query using a specified search type.

PARAMETER DESCRIPTION
query

Input text.

TYPE: str

search_type

Type of search to perform.

Can be 'similarity', 'mmr', or 'similarity_score_threshold'.

TYPE: str

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query.

RAISES DESCRIPTION
ValueError

If search_type is not one of 'similarity', 'mmr', or 'similarity_score_threshold'.

asearch async

asearch(query: str, search_type: str, **kwargs: Any) -> list[Document]

Async return docs most similar to query using a specified search type.

PARAMETER DESCRIPTION
query

Input text.

TYPE: str

search_type

Type of search to perform.

Can be 'similarity', 'mmr', or 'similarity_score_threshold'.

TYPE: str

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query.

RAISES DESCRIPTION
ValueError

If search_type is not one of 'similarity', 'mmr', or 'similarity_score_threshold'.

asimilarity_search_with_score async

asimilarity_search_with_score(
    *args: Any, **kwargs: Any
) -> list[tuple[Document, float]]

Async run similarity search with distance.

PARAMETER DESCRIPTION
*args

Arguments to pass to the search method.

TYPE: Any DEFAULT: ()

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[tuple[Document, float]]

List of tuples of (doc, similarity_score).

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

**kwargs

Kwargs to be passed to similarity search.

Should include score_threshold, an optional floating point value between 0 to 1 to filter the resulting set of retrieved docs.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[tuple[Document, float]]

List of tuples of (doc, similarity_score).

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

**kwargs

Kwargs to be passed to similarity search.

Should include score_threshold, an optional floating point value between 0 to 1 to filter the resulting set of retrieved docs.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[tuple[Document, float]]

List of tuples of (doc, similarity_score)

asimilarity_search(query: str, k: int = 4, **kwargs: Any) -> list[Document]

Async return docs most similar to query.

PARAMETER DESCRIPTION
query

Input text.

TYPE: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query.

asimilarity_search_by_vector async

asimilarity_search_by_vector(
    embedding: list[float], k: int = 4, **kwargs: Any
) -> list[Document]

Async return docs most similar to embedding vector.

PARAMETER DESCRIPTION
embedding

Embedding to look up documents similar to.

TYPE: list[float]

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query vector.

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

fetch_k

Number of Document objects to fetch to pass to MMR algorithm.

TYPE: int DEFAULT: 20

lambda_mult

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity.

TYPE: float DEFAULT: 0.5

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects selected by maximal marginal relevance.

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.

TYPE: list[float]

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

fetch_k

Number of Document objects to fetch to pass to MMR algorithm.

TYPE: int DEFAULT: 20

lambda_mult

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity.

TYPE: float DEFAULT: 0.5

**kwargs

Arguments to pass to the search method.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects selected by maximal marginal relevance.

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 Document objects to add to the VectorStore.

TYPE: list[Document]

embedding

Embedding function to use.

TYPE: Embeddings

**kwargs

Additional keyword arguments.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Self

VectorStore initialized from documents and embeddings.

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 VectorStore.

TYPE: list[str]

embedding

Embedding function to use.

TYPE: Embeddings

metadatas

Optional list of metadatas associated with the texts.

TYPE: list[dict] | None DEFAULT: None

ids

Optional list of IDs associated with the texts.

TYPE: list[str] | None DEFAULT: None

**kwargs

Additional keyword arguments.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Self

VectorStore initialized from texts and embeddings.

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:

  • search_type: Defines the type of search that the Retriever should perform. Can be 'similarity' (default), 'mmr', or 'similarity_score_threshold'.
  • search_kwargs: Keyword arguments to pass to the search function.

    Can include things like:

    • k: Amount of documents to return (Default: 4)
    • score_threshold: Minimum relevance threshold for similarity_score_threshold
    • fetch_k: Amount of documents to pass to MMR algorithm (Default: 20)
    • lambda_mult: Diversity of results returned by MMR; 1 for minimum diversity and 0 for maximum. (Default: 0.5)
    • filter: Filter by document metadata

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
VectorStoreRetriever

Retriever class for VectorStore.

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 Embeddings instance used for this store.

TYPE: Embeddings

config

Optional RedisConfig object.

If not provided, a new one will be created from kwargs.

TYPE: RedisConfig | None DEFAULT: None

ttl

Optional time-to-live for Redis keys.

TYPE: int | None DEFAULT: None

**kwargs

Additional keyword arguments for RedisConfig if config is not provided.

TYPE: Any DEFAULT: {}

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.

TYPE: Iterable[str]

metadatas

Optional list of metadata dicts associated with the texts.

TYPE: list[dict[str, Any]] | None DEFAULT: None

keys

Optional list of keys to associate with the documents.

TYPE: list[str] | None DEFAULT: None

**kwargs

Additional keyword arguments.

Common kwargs include:

  • ids: Optional list of ids to associate with the documents.
  • refresh_indices: Whether to refresh the Redis indices after adding the texts.
    • Defaults to True.
  • create_index_if_not_exists: Whether to create the Redis index if it doesn't already exist.
    • Defaults to True.
  • batch_size: Optional. Number of texts to add to the index at a time.
    • Defaults to 1000.

TYPE: Any DEFAULT: {}

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 metadatas is provided, it must have the same length as texts.
  • If keys is provided, it must have the same length as texts.
  • The batch_size parameter 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.

TYPE: list[str]

embedding

Embedding function to use for encoding the texts.

TYPE: Embeddings

metadatas

Optional list of metadata dicts associated with the texts.

TYPE: list[dict[str, Any]] | None DEFAULT: None

config

Optional RedisConfig object. If not provided, one will be created from kwargs.

TYPE: RedisConfig | None DEFAULT: None

keys

Optional list of keys to associate with the documents.

TYPE: list[str] | None DEFAULT: None

return_keys

Whether to return the keys of the added documents.

TYPE: bool DEFAULT: False

**kwargs

Additional keyword arguments to pass to RedisConfig if config is not provided.

Common kwargs include:

  • index_name: Name of the Redis index to create.
  • redis_url: URL of the Redis instance to connect to.
  • distance_metric: Distance metric to use for similarity search.
    • Default is 'COSINE'.
  • indexing_algorithm: Indexing algorithm to use.
    • Default is 'FLAT'.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisVectorStore

A new RedisVectorStore instance with the texts added.

TYPE: RedisVectorStore

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 RedisVectorStore instance and adds the provided texts to it.
  • If metadatas is provided, it must have the same length as texts.
  • If keys is provided, it must have the same length as texts.
  • The return_keys parameter determines whether the method returns just the RedisVectorStore instance 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 Document objects to add to the vector store.

TYPE: list[Document]

embedding

Embeddings object to use for encoding the documents.

TYPE: Embeddings

config

Optional RedisConfig object.

If not provided, one will be created from kwargs.

TYPE: RedisConfig | None DEFAULT: None

return_keys

Whether to return the keys of the added documents.

TYPE: bool DEFAULT: False

**kwargs

Additional keyword arguments to pass to RedisConfig if config is not provided.

Common kwargs include:

  • index_name: Name of the Redis index to create.
  • redis_url: URL of the Redis instance to connect to.
  • distance_metric: Distance metric to use for similarity search.
    • Default is 'COSINE'.
  • indexing_algorithm: Indexing algorithm to use.
    • Default is 'FLAT'.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisVectorStore

A new RedisVectorStore instance with the documents added.

TYPE: RedisVectorStore

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 RedisVectorStore instance and adds the provided documents to it.
  • The method extracts the text content and metadata from each Document object.
  • If a RedisConfig object 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: str

embedding

Embedding function to use for encoding queries.

TYPE: Embeddings

**kwargs

Additional keyword arguments to pass to RedisConfig.

Common kwargs include:

  • redis_url: URL of the Redis instance to connect to.
  • redis_client: Pre-existing Redis client to use.
  • vector_query_field: Name of the field containing the vector representations.
  • content_field: Name of the field containing the document content.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
RedisVectorStore

A new RedisVectorStore instance connected to the existing index.

TYPE: RedisVectorStore

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_field and content_field respectively.
  • 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: list[str] | None = None, **kwargs: Any) -> bool | None

Delete ids from the vector store.

PARAMETER DESCRIPTION
ids

Optional list of ids of the documents to delete.

TYPE: list[str] | None DEFAULT: None

**kwargs

Additional keyword arguments (not used in the current implementation).

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
bool | None

Optional[bool]: True if one or more keys are deleted, False otherwise

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 ids is None or an empty list, the method returns False.
  • 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_keys functionality from RedisVL to delete the keys from Redis.
  • Keys are constructed by prefixing each id with the key_prefix specified 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.

TYPE: list[float]

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

filter

Optional filter expression to apply.

TYPE: FilterExpression | None DEFAULT: None

sort_by

Optional sort_by expression to apply.

TYPE: str | None DEFAULT: None

**kwargs

Other keyword arguments.

Common kwargs include:

  • return_metadata: Whether to return metadata.
    • Defaults to True.
  • distance_threshold: Optional distance threshold for filtering results.
  • return_all: Whether to return all data in the Hash/JSON including non-indexed fields

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query vector.

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

filter

Optional filter expression to apply.

TYPE: FilterExpression | None DEFAULT: None

sort_by

Optional sort_by expression to apply.

TYPE: str | None DEFAULT: None

**kwargs

Other keyword arguments to pass to the search function.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects most similar to the query.

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.

TYPE: list[float]

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

filter

Optional filter expression to apply.

TYPE: FilterExpression | None DEFAULT: None

sort_by

Optional sort_by expression to apply.

TYPE: str | None DEFAULT: None

**kwargs

Other keyword arguments.

Common kwargs include:

  • with_vectors: Whether to return document vectors.
    • Defaults to False.
  • return_metadata: Whether to return metadata.
    • Defaults to True.
  • distance_threshold: Optional distance threshold for filtering results.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Sequence[Any]

List of tuples of Document objects most similar to the query vector, score, and optionally the document vector.

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

filter

Optional filter expression to apply to the query.

TYPE: FilterExpression | None DEFAULT: None

sort_by

Optional sort_by expression to apply to the query.

TYPE: str | None DEFAULT: None

**kwargs

Other keyword arguments to pass to the search function.

Common kwargs include:

  • custom_query: Optional callable that can be used to customize the query.
  • doc_builder: Optional callable to customize Document creation.
  • return_metadata: Whether to return metadata.
    • Defaults to True.
  • distance_threshold: Optional distance threshold for filtering results.
  • return_all: Whether to return all data in the Hash/JSON including non-indexed fields.
    • Defaults to False.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
Sequence[Any]

List of tuples of (Document, score) most similar to the query.

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 filter parameter allows for additional filtering of results based on metadata.
  • If return_all is set to True, 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.

TYPE: list[float]

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

fetch_k

Number of Document objects to fetch to pass to MMR algorithm.

TYPE: int DEFAULT: 20

lambda_mult

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity.

TYPE: float DEFAULT: 0.5

**kwargs

Other keyword arguments to pass to the search function.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects selected by maximal marginal relevance.

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: str

k

Number of Document objects to return.

TYPE: int DEFAULT: 4

fetch_k

Number of Document objects to fetch to pass to MMR algorithm.

TYPE: int DEFAULT: 20

lambda_mult

Number between 0 and 1 that determines the degree of diversity among the results with 0 corresponding to maximum diversity and 1 to minimum diversity.

TYPE: float DEFAULT: 0.5

**kwargs

Other keyword arguments to pass to the search function.

TYPE: Any DEFAULT: {}

RETURNS DESCRIPTION
list[Document]

List of Document objects selected by maximal marginal relevance.

get_by_ids

get_by_ids(ids: Sequence[str]) -> list[Document]

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.

TYPE: Sequence[str]

RETURNS DESCRIPTION
list[Document]

List of Document objects.

Added in langchain-redis 0.1.2