-
Notifications
You must be signed in to change notification settings - Fork 82
Added a class which performs semantic routing #1192
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
"""add persona table | ||
|
||
Revision ID: 02b710eda156 | ||
Revises: 5e5cd2288147 | ||
Create Date: 2025-03-03 10:08:16.206617+00:00 | ||
|
||
""" | ||
|
||
from typing import Sequence, Union | ||
|
||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision: str = "02b710eda156" | ||
down_revision: Union[str, None] = "5e5cd2288147" | ||
branch_labels: Union[str, Sequence[str], None] = None | ||
depends_on: Union[str, Sequence[str], None] = None | ||
|
||
|
||
def upgrade() -> None: | ||
# Begin transaction | ||
op.execute("BEGIN TRANSACTION;") | ||
|
||
op.execute( | ||
""" | ||
CREATE TABLE IF NOT EXISTS personas ( | ||
id TEXT PRIMARY KEY, -- UUID stored as TEXT | ||
name TEXT NOT NULL UNIQUE, | ||
description TEXT NOT NULL, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we want to make descriptions unique as well? If someone adds two similar descriptions, it would be very hard for the matcher to work properly. Perhaps enforcing uniqueness is the way to go as a first step, and in a further iteration we could check for description similarity. wdyt? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a really nice suggestion! But actually making the descriptions unique won't cut it. If the difference is a single letter then we will accept the new description. What I will do is to check the cosine distance to the existing descriptions and only accept a new persona if it's sufficiently different. Will upload a commit soon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cool beans |
||
description_embedding BLOB NOT NULL | ||
); | ||
""" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's a good idea to have personas not be namespaced within a workspace since this allows us to share personas between workspaces. Do you think we should also add a namespaced persona concept? This is not a blocker and if we decide a namespaced persona makes sense, this can be left as a TODO for another PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Uuhm . Probably the concept of persona namespaces makes sense. Although right now I can't think on what would be the difference wrt. our workspaces. In other words, I like the idea but lack the use cases atm. Lets introduce when we need them |
||
) | ||
|
||
# Finish transaction | ||
op.execute("COMMIT;") | ||
|
||
|
||
def downgrade() -> None: | ||
# Begin transaction | ||
op.execute("BEGIN TRANSACTION;") | ||
|
||
op.execute( | ||
""" | ||
DROP TABLE personas; | ||
""" | ||
) | ||
|
||
# Finish transaction | ||
op.execute("COMMIT;") |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,8 @@ | |
from enum import Enum | ||
from typing import Annotated, Any, Dict, List, Optional | ||
|
||
from pydantic import BaseModel, StringConstraints | ||
import numpy as np | ||
from pydantic import BaseModel, BeforeValidator, ConfigDict, PlainSerializer, StringConstraints | ||
|
||
|
||
class AlertSeverity(str, Enum): | ||
|
@@ -240,3 +241,58 @@ class MuxRule(BaseModel): | |
priority: int | ||
created_at: Optional[datetime.datetime] = None | ||
updated_at: Optional[datetime.datetime] = None | ||
|
||
|
||
def nd_array_custom_before_validator(x): | ||
# custome before validation logic | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you might want to reclarify this comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! Let me know if more clarification is needed |
||
return x | ||
|
||
|
||
def nd_array_custom_serializer(x): | ||
# custome serialization logic | ||
return str(x) | ||
|
||
|
||
# Pydantic doesn't support numpy arrays out of the box hence we need to construct a custom type. | ||
# There are 2 things necessary for a Pydantic custom type: Validator and Serializer | ||
# The lines below build our custom type | ||
# Docs: https://docs.pydantic.dev/latest/concepts/types/#adding-validation-and-serialization | ||
# Open Pydantic issue for npy support: https://github.com/pydantic/pydantic/issues/7017 | ||
NdArray = Annotated[ | ||
np.ndarray, | ||
BeforeValidator(nd_array_custom_before_validator), | ||
PlainSerializer(nd_array_custom_serializer, return_type=str), | ||
] | ||
|
||
|
||
class Persona(BaseModel): | ||
""" | ||
Represents a persona object. | ||
""" | ||
|
||
id: str | ||
name: str | ||
description: str | ||
|
||
|
||
class PersonaEmbedding(Persona): | ||
""" | ||
Represents a persona object with an embedding. | ||
""" | ||
|
||
description_embedding: NdArray | ||
|
||
# Part of the workaround to allow numpy arrays in pydantic models | ||
model_config = ConfigDict(arbitrary_types_allowed=True) | ||
|
||
|
||
class PersonaDistance(Persona): | ||
""" | ||
Result of an SQL query to get the distance between the query and the persona description. | ||
|
||
A vector similarity search is performed to get the distance. Distance values ranges [0, 2]. | ||
0 means the vectors are identical, 2 means they are orthogonal. | ||
See [sqlite docs](https://alexgarcia.xyz/sqlite-vec/api-reference.html#vec_distance_cosine) | ||
""" | ||
|
||
distance: float | ||
JAORMX marked this conversation as resolved.
Show resolved
Hide resolved
|
Uh oh!
There was an error while loading. Please reload this page.