-
Notifications
You must be signed in to change notification settings - Fork 108
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
WIP: hybrid search with fastembed #553
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
46b261e
WIP: hybrid search with fastembed
generall 75b4808
hybrid queries with fastembed
generall 0242751
test for hybrid
generall c677314
fix typo
generall 473d4de
new: extend hybrid search tests, fix mypy, small refactoring (#554)
joein db45d41
refactor: align model name parameters in setters, update tests
joein 41254ed
fix: fix async
joein 0bdfe25
fix: add a good test, fix sparse vectors in query batch
joein 74936cb
refactoring: reduce branching, refactor fastembed tests
joein File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,14 @@ | ||
from typing import Any, Dict, List, Optional, Union | ||
|
||
from pydantic import BaseModel | ||
from pydantic import BaseModel, Field | ||
|
||
from qdrant_client.conversions.common_types import SparseVector | ||
|
||
|
||
class QueryResponse(BaseModel, extra="forbid"): # type: ignore | ||
id: Union[str, int] | ||
embedding: Optional[List[float]] | ||
sparse_embedding: Optional[SparseVector] = Field(default=None) | ||
metadata: Dict[str, Any] | ||
document: str | ||
score: float |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from typing import Dict, List | ||
|
||
from qdrant_client.http import models | ||
|
||
|
||
def reciprocal_rank_fusion( | ||
responses: List[List[models.ScoredPoint]], limit: int = 10 | ||
) -> List[models.ScoredPoint]: | ||
def compute_score(pos: int) -> float: | ||
ranking_constant = ( | ||
2 # the constant mitigates the impact of high rankings by outlier systems | ||
) | ||
return 1 / (ranking_constant + pos) | ||
|
||
scores: Dict[models.ExtendedPointId, float] = {} | ||
point_pile = {} | ||
for response in responses: | ||
for i, scored_point in enumerate(response): | ||
if scored_point.id in scores: | ||
scores[scored_point.id] += compute_score(i) | ||
else: | ||
point_pile[scored_point.id] = scored_point | ||
scores[scored_point.id] = compute_score(i) | ||
|
||
sorted_scores = sorted(scores.items(), key=lambda item: item[1], reverse=True) | ||
sorted_points = [] | ||
for point_id, score in sorted_scores[:limit]: | ||
point = point_pile[point_id] | ||
point.score = score | ||
sorted_points.append(point) | ||
return sorted_points |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from qdrant_client.http import models | ||
from qdrant_client.hybrid.fusion import reciprocal_rank_fusion | ||
|
||
|
||
def test_reciprocal_rank_fusion() -> None: | ||
responses = [ | ||
[ | ||
models.ScoredPoint(id="1", score=0.1, version=1), | ||
models.ScoredPoint(id="2", score=0.2, version=1), | ||
models.ScoredPoint(id="3", score=0.3, version=1), | ||
], | ||
[ | ||
models.ScoredPoint(id="5", score=12.0, version=1), | ||
models.ScoredPoint(id="6", score=8.0, version=1), | ||
models.ScoredPoint(id="7", score=5.0, version=1), | ||
models.ScoredPoint(id="2", score=3.0, version=1), | ||
], | ||
] | ||
|
||
fused = reciprocal_rank_fusion(responses) | ||
|
||
assert fused[0].id == "2" | ||
assert fused[1].id in ["1", "5"] | ||
assert fused[2].id in ["1", "5"] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we limit the responses that get processed inside the loop?
Since we know that we've only returned 10, perhaps responses can be some multiple of that?
Here is the scenario which I'm trying to avoid: Responses are quite large, let's say a thousand, and then we end up running the loop for those. Alternatively we can implement this in a Numpy matrix operation.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you elaborate on limiting the responses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might try rewriting it with numpy, but I am not sure whether it actually worth it, we would still need to iterate over all the responses to map the ids to the scores and then we will need to sum up the scores, etc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we limit the number of responses we evaluate? For loops can be large and slow for when someone passes too large a list.
Numpy implementation saves some of this compute fwiw: qdrant/fastembed#165
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the implementation in qdrant/fastembed#165
involves 3 times more loops
So the question is whether it is actually more efficient? (I don't argue that it might be more efficient due to the arithmetic operations, I am just a little bit doubtful)
I don't really see how to do it at the moment, we can't sort responses with <O(n)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
numpy version is actually slower