Skip to content

Use ULID instead of UUID4 #277

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

Merged
merged 2 commits into from
Feb 12, 2025
Merged
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
2 changes: 1 addition & 1 deletion docs/user_guide/getting_started_01.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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."
]
},
{
Expand Down
8 changes: 4 additions & 4 deletions redisvl/extensions/session_manager/base_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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."""
Expand Down Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions redisvl/extensions/session_manager/semantic_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
[
Expand All @@ -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]] = []
Expand Down Expand Up @@ -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)
10 changes: 5 additions & 5 deletions redisvl/extensions/session_manager/standard_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
[
Expand All @@ -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]] = []
Expand Down Expand Up @@ -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)
4 changes: 2 additions & 2 deletions redisvl/index/storage.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import asyncio
import uuid
from typing import Any, Callable, Dict, Iterable, List, Optional

from pydantic.v1 import BaseModel
Expand All @@ -8,6 +7,7 @@
from redis.commands.search.indexDefinition import IndexType

from redisvl.redis.utils import convert_bytes
from redisvl.utils.utils import create_ulid


class BaseStorage(BaseModel):
Expand Down Expand Up @@ -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 = create_ulid()
else:
try:
key_value = obj[id_field] # type: ignore
Expand Down
6 changes: 3 additions & 3 deletions redisvl/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
def create_ulid() -> str:
"""Generate a unique indentifier to group related Redis documents."""
return str(uuid4())
return str(ULID())


def current_timestamp() -> float:
Expand Down
20 changes: 9 additions & 11 deletions tests/unit/test_session_schema.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
from uuid import uuid4

import pytest
from pydantic.v1 import ValidationError

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!"

Expand All @@ -31,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!"

Expand All @@ -46,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}",
Expand All @@ -64,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]
Expand All @@ -82,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]
Expand All @@ -107,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!"

Expand All @@ -120,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!"

Expand Down