Skip to content

Commit

Permalink
changes _id to entry_id
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-cechmanek committed Aug 1, 2024
1 parent b999b47 commit 6be56b8
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 42 deletions.
53 changes: 24 additions & 29 deletions docs/user_guide/session_manager_07.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"11:26:12 redisvl.index.index INFO Index already exists, not overwriting.\n"
"12:15:37 redisvl.index.index INFO Index already exists, not overwriting.\n"
]
}
],
Expand All @@ -54,14 +54,13 @@
"source": [
"chat_session.add_message({\"role\":\"system\", \"content\":\"You are a helpful geography tutor, giving simple and short answers to questions about Europen countries.\"})\n",
"chat_session.add_messages([\n",
" {\"role\":\"user\", \"content\":\"What is the capital of France?\"},\n",
" {\"role\":\"llm\", \"content\":\"The capital is Paris.\"},\n",
" {\"role\":\"user\", \"content\":\"And what is the capital of Spain?\"},\n",
" {\"role\":\"llm\", \"content\":\"The capital is Madrid.\"},\n",
" {\"role\":\"user\", \"content\":\"What is the population of Great Britain?\"},\n",
" {\"role\":\"llm\", \"content\":\"As of 2023 the population of Great Britain is approximately 67 million people.\"},\n",
" ]\n",
" )"
" {\"role\":\"user\", \"content\":\"What is the capital of France?\"},\n",
" {\"role\":\"llm\", \"content\":\"The capital is Paris.\"},\n",
" {\"role\":\"user\", \"content\":\"And what is the capital of Spain?\"},\n",
" {\"role\":\"llm\", \"content\":\"The capital is Madrid.\"},\n",
" {\"role\":\"user\", \"content\":\"What is the population of Great Britain?\"},\n",
" {\"role\":\"llm\", \"content\":\"As of 2023 the population of Great Britain is approximately 67 million people.\"},]\n",
" )"
]
},
{
Expand Down Expand Up @@ -163,11 +162,12 @@
"source": [
"chat_session.add_message({\"role\":\"system\", \"content\":\"You are a helpful algebra tutor, giving simple answers to math problems.\"}, session_tag='student two')\n",
"chat_session.add_messages([\n",
" {\"role\":\"user\", \"content\":\"What is the value of x in the equation 2x + 3 = 7?\"},\n",
" {\"role\":\"llm\", \"content\":\"The value of x is 2.\"},\n",
" {\"role\":\"user\", \"content\":\"What is the value of y in the equation 3y - 5 = 7?\"},\n",
" {\"role\":\"llm\", \"content\":\"The value of y is 4.\"}\n",
" ], session_tag='student two')\n",
" {\"role\":\"user\", \"content\":\"What is the value of x in the equation 2x + 3 = 7?\"},\n",
" {\"role\":\"llm\", \"content\":\"The value of x is 2.\"},\n",
" {\"role\":\"user\", \"content\":\"What is the value of y in the equation 3y - 5 = 7?\"},\n",
" {\"role\":\"llm\", \"content\":\"The value of y is 4.\"}],\n",
" session_tag='student two'\n",
" )\n",
"\n",
"for math_message in chat_session.get_recent(session_tag='student two'):\n",
" print(math_message)"
Expand Down Expand Up @@ -207,7 +207,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"11:26:15 redisvl.index.index INFO Index already exists, not overwriting.\n"
"12:15:41 redisvl.index.index INFO Index already exists, not overwriting.\n"
]
}
],
Expand All @@ -227,9 +227,6 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'llm', 'content': 'The capital is Madrid.'}\n",
"{'role': 'user', 'content': 'What is the population of Great Britain?'}\n",
"{'role': 'llm', 'content': 'As of 2023 the population of Great Britain is approximately 67 million people.'}\n",
"{'role': 'user', 'content': 'what is the size of England compared to Portugal?'}\n",
"{'role': 'llm', 'content': 'England is larger in land area than Portal by about 15000 square miles.'}\n"
]
Expand All @@ -238,8 +235,7 @@
"source": [
"prompt = \"what have I learned about the size of England?\"\n",
"semantic_session.set_distance_threshold(0.35)\n",
"##context = semantic_session.get_relevant(prompt)\n",
"context = semantic_session.get_recent() ## testing GHA\n",
"context = semantic_session.get_relevant(prompt)\n",
"for message in context:\n",
" print(message)"
]
Expand All @@ -262,19 +258,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
"{'role': 'llm', 'content': 'The capital is Madrid.'}\n",
"{'role': 'user', 'content': 'What is the population of Great Britain?'}\n",
"{'role': 'llm', 'content': 'As of 2023 the population of Great Britain is approximately 67 million people.'}\n",
"{'role': 'user', 'content': 'what is the size of England compared to Portugal?'}\n",
"{'role': 'llm', 'content': 'England is larger in land area than Portal by about 15000 square miles.'}\n"
"{'role': 'llm', 'content': 'England is larger in land area than Portal by about 15000 square miles.'}\n",
"{'role': 'user', 'content': 'What is the population of Great Britain?'}\n",
"{'role': 'llm', 'content': 'As of 2023 the population of Great Britain is approximately 67 million people.'}\n"
]
}
],
"source": [
"semantic_session.set_distance_threshold(0.7)\n",
"\n",
"##larger_context = semantic_session.get_relevant(prompt)\n",
"larger_context = semantic_session.get_recent() ##\n",
"larger_context = semantic_session.get_relevant(prompt)\n",
"for message in larger_context:\n",
" print(message)"
]
Expand Down Expand Up @@ -306,9 +300,10 @@
}
],
"source": [
"semantic_session.store(prompt=\"what is the smallest country in Europe?\",\n",
" response=\"Monaco is the smallest country in Europe at 0.78 square miles.\" # Incorrect. Vatican City is the smallest country in Europe\n",
" )\n",
"semantic_session.store(\n",
" prompt=\"what is the smallest country in Europe?\",\n",
" response=\"Monaco is the smallest country in Europe at 0.78 square miles.\" # Incorrect. Vatican City is the smallest country in Europe\n",
" )\n",
"\n",
"# get the key of the incorrect message\n",
"context = semantic_session.get_recent(top_k=1, raw=True)\n",
Expand Down
2 changes: 1 addition & 1 deletion redisvl/extensions/session_manager/base_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class BaseSessionManager:
id_field_name: str = "_id"
id_field_name: str = "entry_id"
role_field_name: str = "role"
content_field_name: str = "content"
tool_field_name: str = "tool_call_id"
Expand Down
21 changes: 9 additions & 12 deletions redisvl/extensions/session_manager/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class ChatMessage(BaseModel):
"""A single chat message exchanged between a user and an LLM."""

_id: Optional[str] = Field(default=None)
entry_id: str
"""A unique identifier for the message."""
role: str # TODO -- do we enumify this?
"""The role of the message sender (e.g., 'user' or 'llm')."""
content: str
"""The content of the message."""
session_tag: str
"""Tag associated with the current session."""
timestamp: float = Field(default_factory=current_timestamp)
timestamp: float
"""The time the message was sent, in UTC, rounded to milliseconds."""
tool_call_id: Optional[str] = Field(default=None)
"""An optional identifier for a tool call associated with the message."""
Expand All @@ -28,24 +28,21 @@ class ChatMessage(BaseModel):
class Config:
arbitrary_types_allowed = True

@root_validator(pre=False)
@root_validator(pre=True)
@classmethod
def generate_id(cls, values):
if "_id" not in values:
values["_id"] = f'{values["session_tag"]}:{values["timestamp"]}'
if "timestamp" not in values:
values["timestamp"] = current_timestamp()
if "entry_id" not in values:
values["entry_id"] = f'{values["session_tag"]}:{values["timestamp"]}'
return values

def to_dict(self) -> Dict:
data = self.dict()
data = self.dict(exclude_none=True)

# handle optional fields
if data["vector_field"] is not None:
if "vector_field" in data:
data["vector_field"] = array_to_buffer(data["vector_field"])
else:
del data["vector_field"]

if self.tool_call_id is None:
del data["tool_call_id"]

return data

Expand Down

0 comments on commit 6be56b8

Please sign in to comment.