Skip to content

feat: add similarity search with distance score #125

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

juanfe88
Copy link

Thank you for opening a Pull Request! Before submitting your PR, there are a few things you can do to make sure it goes smoothly:

  • Make sure to open an issue as a bug/issue before writing your code! That way we can discuss the change, evaluate designs, and agree on the general idea
  • Ensure the tests and linter pass
  • Code coverage does not decrease (if any source code was changed)
  • Appropriate docs were updated (if necessary)

Fixes #<issue_number_goes_here> 🦕
I was not the one submitting the issue however I ran into the same problem and figured out I could try and fix it
#87

@juanfe88 juanfe88 requested review from a team as code owners April 29, 2025 14:37
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/langchain-google-firestore-python API. label Apr 29, 2025
@@ -240,6 +240,7 @@ def _similarity_search(
query: List[float],
k: int = DEFAULT_TOP_K,
filters: Optional[BaseFilter] = None,
with_scores: Optional[bool] = False,

Choose a reason for hiding this comment

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

Thanks for adding the support!

Could we pass distance_result_field as parameter so that we could calculate the score based on any field (not limit to distance)?

Copy link
Author

@juanfe88 juanfe88 May 12, 2025

Choose a reason for hiding this comment

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

I left it as optional with a default value, but it can be overriden. I also added a test for these cases WDYT ? 😄

Choose a reason for hiding this comment

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

Thanks! In this case, do we still need with_scores? Since distance_result_field is optional, with_scores field can be implicitly done by distance_result_field.

(ex: if distance_result_field is specified, return scores)

Copy link
Author

Choose a reason for hiding this comment

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

You're right. I just refactored it so that we can get the scores just by passing the name of field

This change introduces the `distance_result_field` parameter to `similarity_search_with_score` in `FirestoreVectorStore`.

Users can now specify a custom field name for the distance score returned in the results, defaulting to "distance". This provides flexibility and helps avoid potential field name conflicts when results are processed.

The internal `_similarity_search` method was updated to accept and use this custom field name. A new test case verifies this functionality.
@juanfe88 juanfe88 changed the title add similarity search with distance score feat: add similarity search with distance score May 12, 2025
@@ -240,6 +240,7 @@ def _similarity_search(
query: List[float],
k: int = DEFAULT_TOP_K,
filters: Optional[BaseFilter] = None,
with_scores: Optional[bool] = False,

Choose a reason for hiding this comment

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

Thanks! In this case, do we still need with_scores? Since distance_result_field is optional, with_scores field can be implicitly done by distance_result_field.

(ex: if distance_result_field is specified, return scores)

juanfe88 added 2 commits May 12, 2025 22:26
Remove the `with_scores` parameter from the `_similarity_search`
method. The `distance_result_field` parameter now solely determines
if distance scores should be calculated and returned by the underlying
`find_nearest` call.

This change simplifies the method's API and internal logic by making
the provision of `distance_result_field` the explicit way to request
scores. The `similarity_search_with_score` method has been updated
accordingly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/langchain-google-firestore-python API.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants