Skip to content

Commit a486f0f

Browse files
Merge branch 'main' into feat/RAAE-191/scopeless-sessions
2 parents 483458e + a5854be commit a486f0f

File tree

16 files changed

+324
-255
lines changed

16 files changed

+324
-255
lines changed

docs/examples/openai_qna.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@
651651
"client = redis.Redis.from_url(\"redis://localhost:6379\")\n",
652652
"schema = IndexSchema.from_yaml(\"wiki_schema.yaml\")\n",
653653
"\n",
654-
"index = AsyncSearchIndex(schema, client)\n",
654+
"index = await AsyncSearchIndex(schema).set_client(client)\n",
655655
"\n",
656656
"await index.create()"
657657
]

docs/user_guide/getting_started_01.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,7 @@
486486
"client = Redis.from_url(\"redis://localhost:6379\")\n",
487487
"\n",
488488
"index = AsyncSearchIndex.from_dict(schema)\n",
489-
"index.set_client(client)"
489+
"await index.set_client(client)"
490490
]
491491
},
492492
{

redisvl/extensions/llmcache/semantic.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(
2828
vectorizer: Optional[BaseVectorizer] = None,
2929
redis_client: Optional[Redis] = None,
3030
redis_url: str = "redis://localhost:6379",
31-
connection_args: Dict[str, Any] = {},
31+
connection_kwargs: Dict[str, Any] = {},
3232
**kwargs,
3333
):
3434
"""Semantic Cache for Large Language Models.
@@ -43,14 +43,13 @@ def __init__(
4343
cache. Defaults to 0.1.
4444
ttl (Optional[int], optional): The time-to-live for records cached
4545
in Redis. Defaults to None.
46-
vectorizer (BaseVectorizer, optional): The vectorizer for the cache.
46+
vectorizer (Optional[BaseVectorizer], optional): The vectorizer for the cache.
4747
Defaults to HFTextVectorizer.
48-
redis_client(Redis, optional): A redis client connection instance.
48+
redis_client(Optional[Redis], optional): A redis client connection instance.
4949
Defaults to None.
50-
redis_url (str, optional): The redis url. Defaults to
51-
"redis://localhost:6379".
52-
connection_args (Dict[str, Any], optional): The connection arguments
53-
for the redis client. Defaults to None.
50+
redis_url (str, optional): The redis url. Defaults to redis://localhost:6379.
51+
connection_kwargs (Dict[str, Any]): The connection arguments
52+
for the redis client. Defaults to empty {}.
5453
5554
Raises:
5655
TypeError: If an invalid vectorizer is provided.
@@ -96,8 +95,8 @@ def __init__(
9695
# handle redis connection
9796
if redis_client:
9897
self._index.set_client(redis_client)
99-
else:
100-
self._index.connect(redis_url=redis_url, **connection_args)
98+
elif redis_url:
99+
self._index.connect(redis_url=redis_url, **connection_kwargs)
101100

102101
# initialize other components
103102
self.default_return_fields = [

redisvl/extensions/router/semantic.py

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,9 @@ def __init__(
8686
vectorizer: Optional[BaseVectorizer] = None,
8787
routing_config: Optional[RoutingConfig] = None,
8888
redis_client: Optional[Redis] = None,
89-
redis_url: Optional[str] = None,
89+
redis_url: str = "redis://localhost:6379",
9090
overwrite: bool = False,
91+
connection_kwargs: Dict[str, Any] = {},
9192
**kwargs,
9293
):
9394
"""Initialize the SemanticRouter.
@@ -98,9 +99,10 @@ def __init__(
9899
vectorizer (BaseVectorizer, optional): The vectorizer used to embed route references. Defaults to default HFTextVectorizer.
99100
routing_config (RoutingConfig, optional): Configuration for routing behavior. Defaults to the default RoutingConfig.
100101
redis_client (Optional[Redis], optional): Redis client for connection. Defaults to None.
101-
redis_url (Optional[str], optional): Redis URL for connection. Defaults to None.
102+
redis_url (str, optional): The redis url. Defaults to redis://localhost:6379.
102103
overwrite (bool, optional): Whether to overwrite existing index. Defaults to False.
103-
**kwargs: Additional arguments.
104+
connection_kwargs (Dict[str, Any]): The connection arguments
105+
for the redis client. Defaults to empty {}.
104106
"""
105107
# Set vectorizer default
106108
if vectorizer is None:
@@ -115,12 +117,12 @@ def __init__(
115117
vectorizer=vectorizer,
116118
routing_config=routing_config,
117119
)
118-
self._initialize_index(redis_client, redis_url, overwrite)
120+
self._initialize_index(redis_client, redis_url, overwrite, **connection_kwargs)
119121

120122
def _initialize_index(
121123
self,
122124
redis_client: Optional[Redis] = None,
123-
redis_url: Optional[str] = None,
125+
redis_url: str = "redis://localhost:6379",
124126
overwrite: bool = False,
125127
**connection_kwargs,
126128
):
@@ -132,8 +134,6 @@ def _initialize_index(
132134
self._index.set_client(redis_client)
133135
elif redis_url:
134136
self._index.connect(redis_url=redis_url, **connection_kwargs)
135-
else:
136-
raise ValueError("Must provide either a redis client or redis url string.")
137137

138138
existed = self._index.exists()
139139
self._index.create(overwrite=overwrite)
@@ -479,19 +479,12 @@ def clear(self) -> None:
479479
def from_dict(
480480
cls,
481481
data: Dict[str, Any],
482-
redis_client: Optional[Redis] = None,
483-
redis_url: Optional[str] = None,
484-
overwrite: bool = False,
485482
**kwargs,
486483
) -> "SemanticRouter":
487484
"""Create a SemanticRouter from a dictionary.
488485
489486
Args:
490487
data (Dict[str, Any]): The dictionary containing the semantic router data.
491-
redis_client (Optional[Redis]): Redis client for connection.
492-
redis_url (Optional[str]): Redis URL for connection.
493-
overwrite (bool): Whether to overwrite existing index.
494-
**kwargs: Additional arguments.
495488
496489
Returns:
497490
SemanticRouter: The semantic router instance.
@@ -533,9 +526,6 @@ def from_dict(
533526
routes=routes,
534527
vectorizer=vectorizer,
535528
routing_config=routing_config,
536-
redis_client=redis_client,
537-
redis_url=redis_url,
538-
overwrite=overwrite,
539529
**kwargs,
540530
)
541531

@@ -565,19 +555,12 @@ def to_dict(self) -> Dict[str, Any]:
565555
def from_yaml(
566556
cls,
567557
file_path: str,
568-
redis_client: Optional[Redis] = None,
569-
redis_url: Optional[str] = None,
570-
overwrite: bool = False,
571558
**kwargs,
572559
) -> "SemanticRouter":
573560
"""Create a SemanticRouter from a YAML file.
574561
575562
Args:
576563
file_path (str): The path to the YAML file.
577-
redis_client (Optional[Redis]): Redis client for connection.
578-
redis_url (Optional[str]): Redis URL for connection.
579-
overwrite (bool): Whether to overwrite existing index.
580-
**kwargs: Additional arguments.
581564
582565
Returns:
583566
SemanticRouter: The semantic router instance.
@@ -603,9 +586,6 @@ def from_yaml(
603586
yaml_data = yaml.safe_load(f)
604587
return cls.from_dict(
605588
yaml_data,
606-
redis_client=redis_client,
607-
redis_url=redis_url,
608-
overwrite=overwrite,
609589
**kwargs,
610590
)
611591

redisvl/extensions/session_manager/semantic_session.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from time import time
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union
33

44
from redis import Redis
55

@@ -51,6 +51,8 @@ def __init__(
5151
distance_threshold: float = 0.3,
5252
redis_client: Optional[Redis] = None,
5353
redis_url: str = "redis://localhost:6379",
54+
connection_kwargs: Dict[str, Any] = {},
55+
**kwargs,
5456
):
5557
"""Initialize session memory with index
5658
@@ -66,12 +68,14 @@ def __init__(
6668
session. Defaults to instance uuid.
6769
prefix (Optional[str]): Prefix for the keys for this session data.
6870
Defaults to None and will be replaced with the index name.
69-
vectorizer (Vectorizer): The vectorizer to create embeddings with.
71+
vectorizer (Optional[BaseVectorizer]): The vectorizer used to create embeddings.
7072
distance_threshold (float): The maximum semantic distance to be
7173
included in the context. Defaults to 0.3.
7274
redis_client (Optional[Redis]): A Redis client instance. Defaults to
7375
None.
74-
redis_url (str): The URL of the Redis instance. Defaults to 'redis://localhost:6379'.
76+
redis_url (str, optional): The redis url. Defaults to redis://localhost:6379.
77+
connection_kwargs (Dict[str, Any]): The connection arguments
78+
for the redis client. Defaults to empty {}.
7579
7680
The proposed schema will support a single vector embedding constructed
7781
from either the prompt or response in a single string.
@@ -93,10 +97,11 @@ def __init__(
9397

9498
self._index = SearchIndex(schema=schema)
9599

100+
# handle redis connection
96101
if redis_client:
97102
self._index.set_client(redis_client)
98-
else:
99-
self._index.connect(redis_url=redis_url)
103+
elif redis_url:
104+
self._index.connect(redis_url=redis_url, **connection_kwargs)
100105

101106
self._index.create(overwrite=False)
102107

redisvl/extensions/session_manager/standard_session.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from time import time
2-
from typing import Dict, List, Optional, Union
2+
from typing import Any, Dict, List, Optional, Union
33

44
from redis import Redis
55

@@ -28,6 +28,8 @@ def from_params(cls, name: str, prefix: str):
2828

2929

3030
class StandardSessionManager(BaseSessionManager):
31+
session_field_name: str = "session_tag"
32+
user_field_name: str = "user_tag"
3133

3234
def __init__(
3335
self,
@@ -36,6 +38,8 @@ def __init__(
3638
prefix: Optional[str] = None,
3739
redis_client: Optional[Redis] = None,
3840
redis_url: str = "redis://localhost:6379",
41+
connection_kwargs: Dict[str, Any] = {},
42+
**kwargs,
3943
):
4044
"""Initialize session memory
4145
@@ -52,7 +56,9 @@ def __init__(
5256
Defaults to None and will be replaced with the index name.
5357
redis_client (Optional[Redis]): A Redis client instance. Defaults to
5458
None.
55-
redis_url (str): The URL of the Redis instance. Defaults to 'redis://localhost:6379'.
59+
redis_url (str, optional): The redis url. Defaults to redis://localhost:6379.
60+
connection_kwargs (Dict[str, Any]): The connection arguments
61+
for the redis client. Defaults to empty {}.
5662
5763
The proposed schema will support a single combined vector embedding
5864
constructed from the prompt & response in a single string.
@@ -71,10 +77,18 @@ def __init__(
7177

7278
self._index.create(overwrite=False)
7379

80+
prefix = prefix or name
81+
82+
schema = StandardSessionIndexSchema.from_params(name, prefix)
83+
self._index = SearchIndex(schema=schema)
84+
85+
# handle redis connection
7486
if redis_client:
75-
self._client = redis_client
76-
else:
77-
self._client = Redis.from_url(redis_url)
87+
self._index.set_client(redis_client)
88+
elif redis_url:
89+
self._index.connect(redis_url=redis_url, **connection_kwargs)
90+
91+
self._index.create(overwrite=False)
7892

7993
self._default_tag_filter = Tag(self.session_field_name) == self._session_tag
8094

0 commit comments

Comments
 (0)