Skip to content

Personalised queries #31

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 43 commits into from
Apr 16, 2025
Merged

Personalised queries #31

merged 43 commits into from
Apr 16, 2025

Conversation

danmichaeljones
Copy link
Contributor

@danmichaeljones danmichaeljones commented Apr 7, 2025

Closes https://github.com/weaviate/agents/issues/416.

This adds the personlized queries to the PersonalizationAgent client. The query() method on the PA returns an instance of PersonalizedQuery which implements methods matching the existing collections query methods.

See the corresponding backend changes here.

Copy link

@orca-security-eu orca-security-eu bot left a comment

Choose a reason for hiding this comment

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

Orca Security Scan Summary

Status Check Issues by priority
Passed Passed Infrastructure as Code high 0   medium 0   low 0   info 0 View in Orca
Passed Passed SAST high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Secrets high 0   medium 0   low 0   info 0 View in Orca
Passed Passed Vulnerabilities high 0   medium 0   low 0   info 0 View in Orca

@@ -439,3 +441,29 @@ def exists(
)

return response.json()["persona_collection_exists"]

def query(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In order to match the collections.query API, this method just returns a configured instance of PersonalizedQuery which implements all the query methods

self.recent_interactions_count = recent_interactions_count
self.decay_rate = decay_rate

def near_text(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is where all the query methods are implemented. The only difference between each method is what model to use for the query parameters - these models are a tagged union and all have a query_method field which disambiguates them on the backend.

from weaviate.collections.classes.internal import ReturnProperties, ReturnReferences


class NearTextQueryParameters(BaseModel):
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Finally, these are the query parameter models (all with query_method fields). The serialise_X annotations use pydantic's custom serialiser hooks (PlainSerializer) to turn that field into a dict the backend can deserialise back into that object.

@danmichaeljones danmichaeljones marked this pull request as ready for review April 15, 2025 15:52
@danmichaeljones danmichaeljones requested a review from a team April 15, 2025 15:52
@cdpierse cdpierse self-requested a review April 15, 2025 19:23
overfetch_factor: float = 1.5,
recent_interactions_count: int = 100,
decay_rate: float = 0.1,
):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we add a return type hint here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done! 👍

)


class PersonalizedQuery:
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'm trying to figure out which new classes in this PR would need to be exported in weaviate_agents/classes/__init__.py.

The motivation for this being that with the extras structure we do in the main python client weaviate-client[agents] we have essentially a set of stubs inside that repo ( https://github.com/weaviate/weaviate-python-client/tree/main/weaviate/agents). These stubs allow us to not require changes in the main client for every change we do in this one but still allow for the types to exported if the agents extra is installed.

So for any types in weaviate_agents/personalization/classes/__init__.py that might be used for typing reasons etc in future we might need to bring those up one level.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've tried to follow the existing exports, so have just added PersonalizedQueryResponse (the output from all the query methods). I've skipped PersonalizedQuery (the class which implements all the query methods) since that's not a plain model like the rest of these exports (and is importable from personalization_agent.py like the PersonalizationAgent class) but can add this if you think it's needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There were also two definitions of Usage in personalization and query that had all the same fields/types but were different definitions so wouldn't have been recognised as the same types - I've moved these into a shared core classes file, but all the external export paths should be the same.

json=self._get_request_data(query_parameters),
timeout=self.timeout,
)
response_data = response.raise_for_status().json()
Copy link
Contributor

@augustas1 augustas1 Apr 16, 2025

Choose a reason for hiding this comment

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

raise_for_status only returns the status code message to the user.
And we often return detailed error messages in response body.
So most flexible way to handle errors is like we do in other places:

if response.is_error:
    raise Exception(response.text)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, I thought these were equivalent, didn't realise that raise_for_status swallows the error text - changed 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

I def wish it were 😅

@danmichaeljones danmichaeljones merged commit d4d5721 into main Apr 16, 2025
13 checks passed
@danmichaeljones danmichaeljones deleted the personalised_query branch April 16, 2025 11:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants