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

Fix sort sparse vectors #442

Merged
merged 2 commits into from
Jan 19, 2024
Merged

Fix sort sparse vectors #442

merged 2 commits into from
Jan 19, 2024

Conversation

joein
Copy link
Member

@joein joein commented Jan 19, 2024

@joein joein requested a review from generall January 19, 2024 00:02
Copy link

netlify bot commented Jan 19, 2024

Deploy Preview for poetic-froyo-8baba7 ready!

Name Link
🔨 Latest commit af7101c
🔍 Latest deploy log https://app.netlify.com/sites/poetic-froyo-8baba7/deploys/65a9bc0d640c26000863e926
😎 Deploy Preview https://deploy-preview-442--poetic-froyo-8baba7.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@joein joein requested a review from agourlay January 19, 2024 00:02
@@ -401,7 +401,7 @@ def search(
scores = calculate_context_scores(query_vector, vectors[: len(self.payload)], distance)
elif isinstance(query_vector, SparseVector):
# sparse vector query must be sorted by indices for dot product to work with persisted vectors
sort(query_vector)
query_vector = sort_sparse_vector(query_vector)
Copy link
Member

Choose a reason for hiding this comment

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

That's embarrassing, I guess I thought this was a sort in place 😵‍💫

Copy link
Member Author

Choose a reason for hiding this comment

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

🙈

if is_sorted(vector):
return vector

sorted_indices = np.argsort(vector.indices)
return SparseVector(
indices=vector.indices[sorted_indices],
values=vector.values[sorted_indices],
indices=[vector.indices[i] for i in sorted_indices],
Copy link
Member

Choose a reason for hiding this comment

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

Why is this change necessary?

Copy link
Member Author

Choose a reason for hiding this comment

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

vector.indices and vector.values are built-in python lists
This kind of lists does not support indexing by array of indices

To take elements from an array by several indices, this array should support such an operation (e.g. np.array)

Copy link
Member

@agourlay agourlay left a comment

Choose a reason for hiding this comment

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

Thank you!

@joein joein merged commit e90379f into dev Jan 19, 2024
8 of 13 checks passed
joein added a commit that referenced this pull request Jan 19, 2024
* fix: fix sparse vector sorting

* fix: fix sparse vector sort
@generall generall deleted the fix-sort-sparse-vectors branch May 3, 2024 10:50
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