From 65bdf0b0d6807b07783261aee9b190b25958df4f Mon Sep 17 00:00:00 2001 From: Brian Sam-Bodden Date: Tue, 11 Feb 2025 21:24:36 -0700 Subject: [PATCH 1/2] refactor: use ULID instead of UUID4 --- redisvl/index/storage.py | 4 ++-- redisvl/utils/utils.py | 4 ++-- tests/unit/test_session_schema.py | 2 -- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/redisvl/index/storage.py b/redisvl/index/storage.py index 12ef2052..7af83aee 100644 --- a/redisvl/index/storage.py +++ b/redisvl/index/storage.py @@ -1,11 +1,11 @@ import asyncio -import uuid from typing import Any, Callable, Dict, Iterable, List, Optional from pydantic.v1 import BaseModel from redis import Redis from redis.asyncio import Redis as AsyncRedis from redis.commands.search.indexDefinition import IndexType +from ulid import ULID from redisvl.redis.utils import convert_bytes @@ -64,7 +64,7 @@ def _create_key(self, obj: Dict[str, Any], id_field: Optional[str] = None) -> st ValueError: If the id_field is not found in the object. """ if id_field is None: - key_value = uuid.uuid4().hex + key_value = str(ULID()) else: try: key_value = obj[id_field] # type: ignore diff --git a/redisvl/utils/utils.py b/redisvl/utils/utils.py index 2bcc0a3b..7fde1590 100644 --- a/redisvl/utils/utils.py +++ b/redisvl/utils/utils.py @@ -3,15 +3,15 @@ from functools import wraps from time import time from typing import Any, Callable, Dict, Optional -from uuid import uuid4 from warnings import warn from pydantic.v1 import BaseModel +from ulid import ULID def create_uuid() -> str: """Generate a unique indentifier to group related Redis documents.""" - return str(uuid4()) + return str(ULID()) def current_timestamp() -> float: diff --git a/tests/unit/test_session_schema.py b/tests/unit/test_session_schema.py index 3beb098c..3cb6d90c 100644 --- a/tests/unit/test_session_schema.py +++ b/tests/unit/test_session_schema.py @@ -1,5 +1,3 @@ -from uuid import uuid4 - import pytest from pydantic.v1 import ValidationError From e4b093207d533a73af3a11da9ba5ede0b6871e72 Mon Sep 17 00:00:00 2001 From: Tyler Hutcherson Date: Wed, 12 Feb 2025 08:59:01 -0500 Subject: [PATCH 2/2] slight refactor of ulid usage --- docs/user_guide/getting_started_01.ipynb | 2 +- .../extensions/session_manager/base_session.py | 8 ++++---- .../session_manager/semantic_session.py | 12 ++++++------ .../session_manager/standard_session.py | 10 +++++----- redisvl/index/storage.py | 4 ++-- redisvl/utils/utils.py | 2 +- tests/unit/test_session_schema.py | 18 +++++++++--------- 7 files changed, 28 insertions(+), 28 deletions(-) diff --git a/docs/user_guide/getting_started_01.ipynb b/docs/user_guide/getting_started_01.ipynb index 6066c186..e4fa6544 100644 --- a/docs/user_guide/getting_started_01.ipynb +++ b/docs/user_guide/getting_started_01.ipynb @@ -368,7 +368,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - ">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." + ">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." ] }, { diff --git a/redisvl/extensions/session_manager/base_session.py b/redisvl/extensions/session_manager/base_session.py index fa5c09d0..6bc4f1f5 100644 --- a/redisvl/extensions/session_manager/base_session.py +++ b/redisvl/extensions/session_manager/base_session.py @@ -6,7 +6,7 @@ TOOL_FIELD_NAME, ) from redisvl.extensions.session_manager.schema import ChatMessage -from redisvl.utils.utils import create_uuid +from redisvl.utils.utils import create_ulid class BaseSessionManager: @@ -26,10 +26,10 @@ def __init__( Args: name (str): The name of the session manager index. session_tag (str): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ self._name = name - self._session_tag = session_tag or create_uuid() + self._session_tag = session_tag or create_ulid() def clear(self) -> None: """Clears the chat session history.""" @@ -70,7 +70,7 @@ def get_recent( raw (bool): Whether to return the full Redis hash entry or just the prompt and response session_tag (str): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. Returns: Union[str, List[str]]: A single string transcription of the session diff --git a/redisvl/extensions/session_manager/semantic_session.py b/redisvl/extensions/session_manager/semantic_session.py index 6ad3c01c..74d8f4ab 100644 --- a/redisvl/extensions/session_manager/semantic_session.py +++ b/redisvl/extensions/session_manager/semantic_session.py @@ -50,7 +50,7 @@ def __init__( Args: name (str): The name of the session manager index. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. prefix (Optional[str]): Prefix for the keys for this session data. Defaults to None and will be replaced with the index name. vectorizer (Optional[BaseVectorizer]): The vectorizer used to create embeddings. @@ -186,7 +186,7 @@ def get_relevant( or as JSON top_k (int): The number of previous messages to return. Default is 5. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. distance_threshold (Optional[float]): The threshold for semantic vector distance. fall_back (bool): Whether to drop back to recent conversation history @@ -257,7 +257,7 @@ def get_recent( raw (bool): Whether to return the full Redis hash entry or just the prompt and response session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. Returns: Union[str, List[str]]: A single string transcription of the session @@ -314,7 +314,7 @@ def store( prompt (str): The user prompt to the LLM. response (str): The corresponding LLM response. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ self.add_messages( [ @@ -334,7 +334,7 @@ def add_messages( Args: messages (List[Dict[str, str]]): The list of user prompts and LLM responses. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ session_tag = session_tag or self._session_tag chat_messages: List[Dict[str, Any]] = [] @@ -370,6 +370,6 @@ def add_message( Args: message (Dict[str,str]): The user prompt or LLM response. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ self.add_messages([message], session_tag) diff --git a/redisvl/extensions/session_manager/standard_session.py b/redisvl/extensions/session_manager/standard_session.py index 5a5db108..42b86628 100644 --- a/redisvl/extensions/session_manager/standard_session.py +++ b/redisvl/extensions/session_manager/standard_session.py @@ -42,7 +42,7 @@ def __init__( Args: name (str): The name of the session manager index. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. prefix (Optional[str]): Prefix for the keys for this session data. Defaults to None and will be replaced with the index name. redis_client (Optional[Redis]): A Redis client instance. Defaults to @@ -131,7 +131,7 @@ def get_recent( raw (bool): Whether to return the full Redis hash entry or just the prompt and response session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. Returns: Union[str, List[str]]: A single string transcription of the session @@ -181,7 +181,7 @@ def store( prompt (str): The user prompt to the LLM. response (str): The corresponding LLM response. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ self.add_messages( [ @@ -201,7 +201,7 @@ def add_messages( Args: messages (List[Dict[str, str]]): The list of user prompts and LLM responses. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ session_tag = session_tag or self._session_tag chat_messages: List[Dict[str, Any]] = [] @@ -231,6 +231,6 @@ def add_message( Args: message (Dict[str,str]): The user prompt or LLM response. session_tag (Optional[str]): Tag to be added to entries to link to a specific - session. Defaults to instance uuid. + session. Defaults to instance ULID. """ self.add_messages([message], session_tag) diff --git a/redisvl/index/storage.py b/redisvl/index/storage.py index 7af83aee..108237f3 100644 --- a/redisvl/index/storage.py +++ b/redisvl/index/storage.py @@ -5,9 +5,9 @@ from redis import Redis from redis.asyncio import Redis as AsyncRedis from redis.commands.search.indexDefinition import IndexType -from ulid import ULID from redisvl.redis.utils import convert_bytes +from redisvl.utils.utils import create_ulid class BaseStorage(BaseModel): @@ -64,7 +64,7 @@ def _create_key(self, obj: Dict[str, Any], id_field: Optional[str] = None) -> st ValueError: If the id_field is not found in the object. """ if id_field is None: - key_value = str(ULID()) + key_value = create_ulid() else: try: key_value = obj[id_field] # type: ignore diff --git a/redisvl/utils/utils.py b/redisvl/utils/utils.py index 7fde1590..da1067d8 100644 --- a/redisvl/utils/utils.py +++ b/redisvl/utils/utils.py @@ -9,7 +9,7 @@ from ulid import ULID -def create_uuid() -> str: +def create_ulid() -> str: """Generate a unique indentifier to group related Redis documents.""" return str(ULID()) diff --git a/tests/unit/test_session_schema.py b/tests/unit/test_session_schema.py index 3cb6d90c..df6b8d91 100644 --- a/tests/unit/test_session_schema.py +++ b/tests/unit/test_session_schema.py @@ -3,11 +3,11 @@ from redisvl.extensions.session_manager.schema import ChatMessage from redisvl.redis.utils import array_to_buffer -from redisvl.utils.utils import create_uuid, current_timestamp +from redisvl.utils.utils import create_ulid, current_timestamp def test_chat_message_creation(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" @@ -29,7 +29,7 @@ def test_chat_message_creation(): def test_chat_message_default_id_generation(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" @@ -44,10 +44,10 @@ def test_chat_message_default_id_generation(): def test_chat_message_with_tool_call_id(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" - tool_call_id = create_uuid() + tool_call_id = create_ulid() chat_message = ChatMessage( entry_id=f"{session_tag}:{timestamp}", @@ -62,7 +62,7 @@ def test_chat_message_with_tool_call_id(): def test_chat_message_with_vector_field(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" vector_field = [0.1, 0.2, 0.3] @@ -80,7 +80,7 @@ def test_chat_message_with_vector_field(): def test_chat_message_to_dict(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" vector_field = [0.1, 0.2, 0.3] @@ -105,7 +105,7 @@ def test_chat_message_to_dict(): def test_chat_message_missing_fields(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!" @@ -118,7 +118,7 @@ def test_chat_message_missing_fields(): def test_chat_message_invalid_role(): - session_tag = create_uuid() + session_tag = create_ulid() timestamp = current_timestamp() content = "Hello, world!"