Skip to content
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

Support for different aggregation in (Hybrid)RouteLayer voting? #201

Closed
andreped opened this issue Mar 14, 2024 · 1 comment · Fixed by #202
Closed

Support for different aggregation in (Hybrid)RouteLayer voting? #201

andreped opened this issue Mar 14, 2024 · 1 comment · Fixed by #202
Assignees
Labels
enhancement Enhancement to existing features

Comments

@andreped
Copy link
Contributor

andreped commented Mar 14, 2024

Current solution

Currently, for k routes and top_k most relevant utterances, the final route classification is determined by class-wise similarity aggregation (SUM) and then selecting which class has the highest aggregated similarity value.

Problem

In many tasks, doing this type of SUM aggregation might be bad, as it is very dependent on how the routes are defined which aggregation method makes the most sense.

For very high values of top_k, I would expect the most optimal class to be the one that has the highest consensus across all utterances class-wise. Statistically, I would expect this to be the MEAN similarity value.

Then again, using MEAN could be bad as well, if the utterances for one routes are extremely similar and then for another one they are more heterogeneous. So for the current HybridRouteLayer, it seems evident that which method that is used is dependent on how the end user has defined the routes.

Proposed solution

Allow developers to set aggregation themselves to allow them to find which solution works best.

This solution would be of relevant both for the RouteLayer and HybridRouteLayer classes.


EDIT: I can draft a PR to add this feature.

@andreped
Copy link
Contributor Author

andreped commented Mar 15, 2024

A PR has been made to address this. See PR #202.

Note that I have added this support to both RouteLayer and HybridRouteLayer.
Below is an example on how one will be able to set the aggregation method (supported types are "sum", "mean", and "max").:

from semantic_router.encoders import AzureOpenAIEncoder, TfidfEncoder
from semantic_router.layer import RouteLayer
from semantic_router.hybrid_layer import HybridRouteLayer

model = RouteLayer(
    encoder=AzureOpenAIEncoder(...),
    routes=routes,
    alpha=0.8,  # default = 0.3
    top_k=6,  # default = 5
    aggregation="max",  # default = "sum"
)

hybrid_model = HybridRouteLayer(
    encoder=AzureOpenAIEncoder(...),
    sparse_encoder=TfidfEncoder(),
    routes=routes,
    alpha=0.2,  # default = 0.3
    top_k=3,  # default = 5
    aggregation="mean",  # default = "sum"
)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Enhancement to existing features
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants