Skip to content

Deprecate global distance_threshold with semantic router #275

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 8 commits into from
Feb 12, 2025

Conversation

rbs333
Copy link
Collaborator

@rbs333 rbs333 commented Feb 11, 2025

  • change ownership of distance_threshold
  • move filtering to happen as part of db call
  • make code more DRY
  • rename user_guide because it's really been bothering me

@rbs333 rbs333 changed the title Feat/raae 615/dist thresh ownership Deprecate global distance_threshold with semantic router Feb 11, 2025
Copy link
Collaborator

@tylerhutcherson tylerhutcherson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work!

@tylerhutcherson tylerhutcherson self-requested a review February 12, 2025 16:58
abrookins
abrookins previously approved these changes Feb 12, 2025
Copy link
Collaborator

@abrookins abrookins left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of comments, but this looks good overall!

@@ -16,7 +16,7 @@ class Route(BaseModel):
"""List of reference phrases for the route."""
metadata: Dict[str, str] = Field(default={})
"""Metadata associated with the route."""
distance_threshold: Optional[float] = Field(default=None)
distance_threshold: float = Field(default=0.5)
Copy link
Collaborator

@abrookins abrookins Feb 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw you deleted another validator, so maybe this isn't important, but with Pydantic 2, you can validate that a number is within a range using Annotated:

from typing_extensions import Annotated

from pydantic.v1 import BaseModel, Field, ValidationError

class Foo(BaseModel):
    bar: Annotated[float, Field(strict=True, gt=0, lte=1)]  # or gt, lt


for bad_number in (-1, 2):
    try:
        Foo(bar=bad_number) 
    except ValidationError:
        print("worked")
    else:
        raise RuntimeError("Didn't work!")

print(Foo(bar=0.5))

This should still work with a v1 BaseModel.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that's nice

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I deleted a validator for the now depreciated field since it has no impact. I think we should update to the new style of validators but I think that should be included in the pr for pydantic versioning stuff because it fits more with that theme: https://redislabs.atlassian.net/browse/RAAE-607

"""The maximum number of top matches to return."""
distance_threshold: float = Field(
default=0.5,
deprecated=True,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a v1 BaseModel, this only adds "deprecated" to the JSON schema for this field, since what's really happening is any extra keyword args pass through to the schema. That's not ideal, but I'm ok with it. In v2, this would log a deprecation warning if a user used the field. We can leave it in place like this and then if it's still around when we cut over to using v2 BaseModel, it'll work better.

) -> List[RouteMatch]:
"""Get the route matches for a given vector and aggregation method."""

thresholds: List[float] = [route.distance_threshold for route in self.routes]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the variable type annotation here? Did mypy not pick up that route.distance_threshold is a float?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch it failed mypy originally because the pydantic model had it as optional so I update there but forgot to delete this. Pushed a fix

@rbs333 rbs333 dismissed stale reviews from abrookins and tylerhutcherson via fa7cf0b February 12, 2025 18:19
@rbs333 rbs333 merged commit 5ad7d00 into main Feb 12, 2025
36 checks passed
@rbs333 rbs333 deleted the feat/RAAE-615/dist-thresh-ownership branch February 12, 2025 18:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants