Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ttl support to vector store #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
feat: add ttl support to vector store
  • Loading branch information
TheEaterr committed Jan 8, 2025
commit 62270bd1456b15a976e72e1107128c95fb903ec0
10 changes: 8 additions & 2 deletions libs/redis/langchain_redis/vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ class RedisVectorStore(VectorStore):
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:
.. code-block:: python
Expand Down Expand Up @@ -265,10 +267,12 @@ def __init__(
self,
embeddings: Embeddings,
config: Optional[RedisConfig] = None,
ttl: Optional[int] = None,
**kwargs: Any,
):
self.config = config or RedisConfig(**kwargs)
self._embeddings = embeddings
self.ttl = ttl

if self.config.embedding_dimensions is None:
self.config.embedding_dimensions = len(
Expand Down Expand Up @@ -441,10 +445,12 @@ def add_texts(

result = (
self._index.load(
datas, keys=[f"{self.config.key_prefix}:{key}" for key in keys]
datas,
keys=[f"{self.config.key_prefix}:{key}" for key in keys],
ttl=self.ttl,
)
if keys
else self._index.load(datas)
else self._index.load(datas, ttl=self.ttl)
)

return list(result) if result is not None else []
Expand Down
5 changes: 4 additions & 1 deletion libs/redis/tests/unit_tests/test_vectorstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ def create(self, overwrite: bool = False) -> None:
pass

def load(
self, documents: List[Dict[str, Any]], keys: Optional[List[str]] = None
self,
documents: List[Dict[str, Any]],
keys: Optional[List[str]] = None,
ttl: Optional[int] = None,
) -> List[str]:
for i, doc in enumerate(documents):
key = keys[i] if keys else f"key_{i}"
Expand Down