Skip to content

Commit e928d1f

Browse files
Use ULID instead of UUID4 (#277)
Co-authored-by: Brian Sam-Bodden <[email protected]>
1 parent ba8142a commit e928d1f

File tree

7 files changed

+30
-32
lines changed

7 files changed

+30
-32
lines changed

docs/user_guide/getting_started_01.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@
368368
"cell_type": "markdown",
369369
"metadata": {},
370370
"source": [
371-
">By default, `load` will create a unique Redis \"key\" as a combination of the index key `prefix` and a UUID. You can also customize the key by providing direct keys or pointing to a specified `id_field` on load."
371+
">By default, `load` will create a unique Redis key as a combination of the index key `prefix` and a random ULID. You can also customize the key by providing direct keys or pointing to a specified `id_field` on load."
372372
]
373373
},
374374
{

redisvl/extensions/session_manager/base_session.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
TOOL_FIELD_NAME,
77
)
88
from redisvl.extensions.session_manager.schema import ChatMessage
9-
from redisvl.utils.utils import create_uuid
9+
from redisvl.utils.utils import create_ulid
1010

1111

1212
class BaseSessionManager:
@@ -26,10 +26,10 @@ def __init__(
2626
Args:
2727
name (str): The name of the session manager index.
2828
session_tag (str): Tag to be added to entries to link to a specific
29-
session. Defaults to instance uuid.
29+
session. Defaults to instance ULID.
3030
"""
3131
self._name = name
32-
self._session_tag = session_tag or create_uuid()
32+
self._session_tag = session_tag or create_ulid()
3333

3434
def clear(self) -> None:
3535
"""Clears the chat session history."""
@@ -70,7 +70,7 @@ def get_recent(
7070
raw (bool): Whether to return the full Redis hash entry or just the
7171
prompt and response
7272
session_tag (str): Tag to be added to entries to link to a specific
73-
session. Defaults to instance uuid.
73+
session. Defaults to instance ULID.
7474
7575
Returns:
7676
Union[str, List[str]]: A single string transcription of the session

redisvl/extensions/session_manager/semantic_session.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ def __init__(
5050
Args:
5151
name (str): The name of the session manager index.
5252
session_tag (Optional[str]): Tag to be added to entries to link to a specific
53-
session. Defaults to instance uuid.
53+
session. Defaults to instance ULID.
5454
prefix (Optional[str]): Prefix for the keys for this session data.
5555
Defaults to None and will be replaced with the index name.
5656
vectorizer (Optional[BaseVectorizer]): The vectorizer used to create embeddings.
@@ -186,7 +186,7 @@ def get_relevant(
186186
or as JSON
187187
top_k (int): The number of previous messages to return. Default is 5.
188188
session_tag (Optional[str]): Tag to be added to entries to link to a specific
189-
session. Defaults to instance uuid.
189+
session. Defaults to instance ULID.
190190
distance_threshold (Optional[float]): The threshold for semantic
191191
vector distance.
192192
fall_back (bool): Whether to drop back to recent conversation history
@@ -257,7 +257,7 @@ def get_recent(
257257
raw (bool): Whether to return the full Redis hash entry or just the
258258
prompt and response
259259
session_tag (Optional[str]): Tag to be added to entries to link to a specific
260-
session. Defaults to instance uuid.
260+
session. Defaults to instance ULID.
261261
262262
Returns:
263263
Union[str, List[str]]: A single string transcription of the session
@@ -314,7 +314,7 @@ def store(
314314
prompt (str): The user prompt to the LLM.
315315
response (str): The corresponding LLM response.
316316
session_tag (Optional[str]): Tag to be added to entries to link to a specific
317-
session. Defaults to instance uuid.
317+
session. Defaults to instance ULID.
318318
"""
319319
self.add_messages(
320320
[
@@ -334,7 +334,7 @@ def add_messages(
334334
Args:
335335
messages (List[Dict[str, str]]): The list of user prompts and LLM responses.
336336
session_tag (Optional[str]): Tag to be added to entries to link to a specific
337-
session. Defaults to instance uuid.
337+
session. Defaults to instance ULID.
338338
"""
339339
session_tag = session_tag or self._session_tag
340340
chat_messages: List[Dict[str, Any]] = []
@@ -370,6 +370,6 @@ def add_message(
370370
Args:
371371
message (Dict[str,str]): The user prompt or LLM response.
372372
session_tag (Optional[str]): Tag to be added to entries to link to a specific
373-
session. Defaults to instance uuid.
373+
session. Defaults to instance ULID.
374374
"""
375375
self.add_messages([message], session_tag)

redisvl/extensions/session_manager/standard_session.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def __init__(
4242
Args:
4343
name (str): The name of the session manager index.
4444
session_tag (Optional[str]): Tag to be added to entries to link to a specific
45-
session. Defaults to instance uuid.
45+
session. Defaults to instance ULID.
4646
prefix (Optional[str]): Prefix for the keys for this session data.
4747
Defaults to None and will be replaced with the index name.
4848
redis_client (Optional[Redis]): A Redis client instance. Defaults to
@@ -131,7 +131,7 @@ def get_recent(
131131
raw (bool): Whether to return the full Redis hash entry or just the
132132
prompt and response
133133
session_tag (Optional[str]): Tag to be added to entries to link to a specific
134-
session. Defaults to instance uuid.
134+
session. Defaults to instance ULID.
135135
136136
Returns:
137137
Union[str, List[str]]: A single string transcription of the session
@@ -181,7 +181,7 @@ def store(
181181
prompt (str): The user prompt to the LLM.
182182
response (str): The corresponding LLM response.
183183
session_tag (Optional[str]): Tag to be added to entries to link to a specific
184-
session. Defaults to instance uuid.
184+
session. Defaults to instance ULID.
185185
"""
186186
self.add_messages(
187187
[
@@ -201,7 +201,7 @@ def add_messages(
201201
Args:
202202
messages (List[Dict[str, str]]): The list of user prompts and LLM responses.
203203
session_tag (Optional[str]): Tag to be added to entries to link to a specific
204-
session. Defaults to instance uuid.
204+
session. Defaults to instance ULID.
205205
"""
206206
session_tag = session_tag or self._session_tag
207207
chat_messages: List[Dict[str, Any]] = []
@@ -231,6 +231,6 @@ def add_message(
231231
Args:
232232
message (Dict[str,str]): The user prompt or LLM response.
233233
session_tag (Optional[str]): Tag to be added to entries to link to a specific
234-
session. Defaults to instance uuid.
234+
session. Defaults to instance ULID.
235235
"""
236236
self.add_messages([message], session_tag)

redisvl/index/storage.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import asyncio
2-
import uuid
32
from typing import Any, Callable, Dict, Iterable, List, Optional
43

54
from pydantic.v1 import BaseModel
@@ -8,6 +7,7 @@
87
from redis.commands.search.indexDefinition import IndexType
98

109
from redisvl.redis.utils import convert_bytes
10+
from redisvl.utils.utils import create_ulid
1111

1212

1313
class BaseStorage(BaseModel):
@@ -64,7 +64,7 @@ def _create_key(self, obj: Dict[str, Any], id_field: Optional[str] = None) -> st
6464
ValueError: If the id_field is not found in the object.
6565
"""
6666
if id_field is None:
67-
key_value = uuid.uuid4().hex
67+
key_value = create_ulid()
6868
else:
6969
try:
7070
key_value = obj[id_field] # type: ignore

redisvl/utils/utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
from functools import wraps
44
from time import time
55
from typing import Any, Callable, Dict, Optional
6-
from uuid import uuid4
76
from warnings import warn
87

98
from pydantic.v1 import BaseModel
9+
from ulid import ULID
1010

1111

12-
def create_uuid() -> str:
12+
def create_ulid() -> str:
1313
"""Generate a unique indentifier to group related Redis documents."""
14-
return str(uuid4())
14+
return str(ULID())
1515

1616

1717
def current_timestamp() -> float:

tests/unit/test_session_schema.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
from uuid import uuid4
2-
31
import pytest
42
from pydantic.v1 import ValidationError
53

64
from redisvl.extensions.session_manager.schema import ChatMessage
75
from redisvl.redis.utils import array_to_buffer
8-
from redisvl.utils.utils import create_uuid, current_timestamp
6+
from redisvl.utils.utils import create_ulid, current_timestamp
97

108

119
def test_chat_message_creation():
12-
session_tag = create_uuid()
10+
session_tag = create_ulid()
1311
timestamp = current_timestamp()
1412
content = "Hello, world!"
1513

@@ -31,7 +29,7 @@ def test_chat_message_creation():
3129

3230

3331
def test_chat_message_default_id_generation():
34-
session_tag = create_uuid()
32+
session_tag = create_ulid()
3533
timestamp = current_timestamp()
3634
content = "Hello, world!"
3735

@@ -46,10 +44,10 @@ def test_chat_message_default_id_generation():
4644

4745

4846
def test_chat_message_with_tool_call_id():
49-
session_tag = create_uuid()
47+
session_tag = create_ulid()
5048
timestamp = current_timestamp()
5149
content = "Hello, world!"
52-
tool_call_id = create_uuid()
50+
tool_call_id = create_ulid()
5351

5452
chat_message = ChatMessage(
5553
entry_id=f"{session_tag}:{timestamp}",
@@ -64,7 +62,7 @@ def test_chat_message_with_tool_call_id():
6462

6563

6664
def test_chat_message_with_vector_field():
67-
session_tag = create_uuid()
65+
session_tag = create_ulid()
6866
timestamp = current_timestamp()
6967
content = "Hello, world!"
7068
vector_field = [0.1, 0.2, 0.3]
@@ -82,7 +80,7 @@ def test_chat_message_with_vector_field():
8280

8381

8482
def test_chat_message_to_dict():
85-
session_tag = create_uuid()
83+
session_tag = create_ulid()
8684
timestamp = current_timestamp()
8785
content = "Hello, world!"
8886
vector_field = [0.1, 0.2, 0.3]
@@ -107,7 +105,7 @@ def test_chat_message_to_dict():
107105

108106

109107
def test_chat_message_missing_fields():
110-
session_tag = create_uuid()
108+
session_tag = create_ulid()
111109
timestamp = current_timestamp()
112110
content = "Hello, world!"
113111

@@ -120,7 +118,7 @@ def test_chat_message_missing_fields():
120118

121119

122120
def test_chat_message_invalid_role():
123-
session_tag = create_uuid()
121+
session_tag = create_ulid()
124122
timestamp = current_timestamp()
125123
content = "Hello, world!"
126124

0 commit comments

Comments
 (0)