Add support for Cosine Similarity by enzofrnt · Pull Request #109 · pgvector/pgvector-python
This pull request introduces a new CosineSimilarity function to the pgvector Django integration and updates the corresponding tests. The most important changes include adding the CosineSimilarity function, updating imports, and adding a new test case for CosineSimilarity.
CosineSimilarity must be useful for people who are wondering to use similarity instead of distance.
New feature addition:
pgvector/django/functions.py: Added a new classCosineSimilaritythat inherits fromDistanceBaseand overrides theas_sqlmethod to compute cosine similarity.
Import updates:
pgvector/django/__init__.py: Updated the import statement to include the newCosineSimilarityfunction.tests/test_django.py: Updated the import statement to include the newCosineSimilarityfunction.
Test updates:
tests/test_django.py: Added
This pull request introduces a newCosineSimilarityfunction to thepgvectorDjango package, along with corresponding tests. The changes primarily involve adding this new function and ensuring it is properly tested across different vector types.
Key Changes:
New Functionality:
- Added
CosineSimilarityclass topgvector/django/functions.pyto calculate cosine similarity.
Import Adjustments:
- Updated imports in
pgvector/django/__init__.pyto includeCosineSimilarity. - Updated imports in
tests/test_django.pyto includeCosineSimilarity.
Testing Enhancements:
- Added
test_vector_cosine_similaritymethod to test cosine similarity for regular vectors intests/test_django.py. - Added
test_halfvec_cosine_similaritymethod to test cosine similarity for half vectors intests/test_django.py. - Added
test_sparsevec_cosine_similaritymethod to test cosine similarity for sparse vectors intests/test_django.py.