Skip to content

[ENH] Add python & js client support to query on subset of IDs #4250

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

Merged
merged 1 commit into from
Apr 29, 2025

Conversation

jairad26
Copy link
Contributor

@jairad26 jairad26 commented Apr 9, 2025

Description of changes

This PR adds python client and async python client support to query on a filtered set of IDs

example:

ids = ["1", "2", "3"]
documents = ["test", "test2", "apple"]
metadatas = [{"source": "test"}, {"source": "test2"}, {"source": "apple"}]

coll.add(
    ids=ids,
    documents=documents,
    metadatas=metadatas,
    # embeddings=numpy_embeddings
)

output = coll.query(
    ids=["1", "3"],
    query_texts=["test"],
    n_results=3,
    include=["documents", "metadatas", "distances"]
)

print(output)

This will output
% python test_filter_id.py
{'ids': [['1', '3']], 'embeddings': None, 'documents': [['test', 'apple']], 'uris': None, 'included': ['documents', 'metadatas', 'distances'], 'data': None, 'metadatas': [[{'source': 'test'}, {'source': 'apple'}]], 'distances': [[0.0, 0.7396076321601868]]}

Test plan

How are these changes tested?

  • Tests pass locally with pytest for python, yarn test for js, cargo test for rust
  • Added prop tests to test with other filtering and on its own

Documentation Changes

Are all docstrings for user-facing APIs updated if required? Do we need to make documentation changes in the docs repository?

Copy link

github-actions bot commented Apr 9, 2025

Reviewer Checklist

Please leverage this checklist to ensure your code review is thorough before approving

Testing, Bugs, Errors, Logs, Documentation

  • Can you think of any use case in which the code does not behave as intended? Have they been tested?
  • Can you think of any inputs or external events that could break the code? Is user input validated and safe? Have they been tested?
  • If appropriate, are there adequate property based tests?
  • If appropriate, are there adequate unit tests?
  • Should any logging, debugging, tracing information be added or removed?
  • Are error messages user-friendly?
  • Have all documentation changes needed been made?
  • Have all non-obvious changes been commented?

System Compatibility

  • Are there any potential impacts on other parts of the system or backward compatibility?
  • Does this change intersect with any items on our roadmap, and if so, is there a plan for fitting them together?

Quality

  • Is this code of a unexpectedly high quality (Readability, Modularity, Intuitiveness)

Copy link
Contributor Author

jairad26 commented Apr 9, 2025

@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from 89174d3 to 3323962 Compare April 9, 2025 22:24
@jairad26 jairad26 marked this pull request as ready for review April 9, 2025 22:24
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch 6 times, most recently from 699620e to 4a0795f Compare April 10, 2025 02:06
@jairad26 jairad26 changed the base branch from jai/propogate-api-ef-error to main April 10, 2025 13:54
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch 5 times, most recently from 49df6bb to 194e27b Compare April 11, 2025 21:06
@@ -270,6 +270,7 @@ def _query(
self,
collection_id: UUID,
query_embeddings: Embeddings,
ids: Optional[IDs] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

add to docstring(?)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added, thanks

@@ -472,6 +472,7 @@ def _query(
self,
collection_id: UUID,
query_embeddings: Embeddings,
ids: Optional[IDs] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

add to telemetry capture call below? just observing the pattern

Copy link
Contributor Author

Choose a reason for hiding this comment

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

added, thanks

@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch 2 times, most recently from ddb0949 to cac7f29 Compare April 14, 2025 16:56
@@ -264,6 +264,7 @@ async def _query(
self,
collection_id: UUID,
query_embeddings: Embeddings,
ids: Optional[IDs] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

docstring(?)

@@ -169,6 +169,7 @@ async def query(
query_texts: Optional[OneOrMany[Document]] = None,
query_images: Optional[OneOrMany[Image]] = None,
query_uris: Optional[OneOrMany[URI]] = None,
ids: Optional[IDs] = None,
Copy link
Contributor

Choose a reason for hiding this comment

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

what do you think about the OneOrMany pattern here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

not a fan, because why would someone want to filter on 1 id? it makes it a little more confusing on what it does. also the rust takes in a list of ids, so i'd prefer maintaining that.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah that's very reasonable

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should keep consistency with other APIs

@hesreallyhim
Copy link
Contributor

hesreallyhim commented Apr 15, 2025

i think i'm missing something, so for my edification, which part of the code actually processes the ids filter? Is it in here? https://github.com/hesreallyhim/chroma/blob/cac7f29be72907e71640981e7cf0dc94c68d06ce/rust/frontend/src/impls/in_memory_frontend.rs#L609

EDIT: Sorry that might be a really broad question, I don't know how the rust code works.

@hesreallyhim
Copy link
Contributor

it would be cool to add filter syntax to ids, like {"$and": [{"$gt": 1000}, {"$lt": 2000}]} but i think it doesn't really make sense since we don't allow comparison like that for strings anyway, and IDs are strings...

@hesreallyhim
Copy link
Contributor

@jairad26 would it be helpful for me to put together a PR to add this feature to the docs?

@jairad26
Copy link
Contributor Author

i think i'm missing something, so for my edification, which part of the code actually processes the ids filter? Is it in here? https://github.com/hesreallyhim/chroma/blob/cac7f29be72907e71640981e7cf0dc94c68d06ce/rust/frontend/src/impls/in_memory_frontend.rs#L609

EDIT: Sorry that might be a really broad question, I don't know how the rust code works.

yep that's exactly the spot! all that does is filter by ids pre-vector search.

it would be cool to add filter syntax to ids, like {"$and": [{"$gt": 1000}, {"$lt": 2000}]} but i think it doesn't really make sense since we don't allow comparison like that for strings anyway, and IDs are strings...

yes that would be nice, but would only really work if the ids are ints. not 100% sure but i dont think uuids for example guarantee order like that. also random string generator definitely wouldn't work with that

@jairad26 would it be helpful for me to put together a PR to add this feature to the docs?

I'm moving this PR to draft for now, since i want to add support for the js client and that requires a couple other PRs to get merged. after that I can prob handle the docs. Thank you though!

@jairad26 jairad26 marked this pull request as draft April 15, 2025 16:19
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from cac7f29 to 5a95f79 Compare April 15, 2025 18:57
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from 74c9b85 to 99a1335 Compare April 21, 2025 22:55
@jairad26 jairad26 mentioned this pull request Apr 21, 2025
1 task
@jairad26 jairad26 changed the title [ENH] Add python &js client support to query on subset of IDs [ENH] Add python & js client support to query on subset of IDs Apr 21, 2025
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from 99a1335 to 2239c58 Compare April 23, 2025 23:43
@@ -19,7 +19,6 @@
import chromadb.test.property.strategies as strategies
Copy link
Collaborator

Choose a reason for hiding this comment

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

please add explicit unit tests as well with small, medium size data

Copy link
Collaborator

@HammadB HammadB left a comment

Choose a reason for hiding this comment

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

Generally is good. Please add more explicit unit testing, especially around edge cases like deleted ids, upserted ids.

Also I think OneOrMany is preferable for API consistency, even though unlikely to be used.

@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch 8 times, most recently from 621b98a to fc2f354 Compare April 25, 2025 23:41
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from fc2f354 to bb6c085 Compare April 26, 2025 00:02
@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch 2 times, most recently from 907ec35 to b4d8d7b Compare April 28, 2025 21:00
random_query = normalized_record_set["embeddings"][
random.randint(0, total_count - 1)
]
# Use data.draw to select index
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks for this change

client.get_settings().chroma_api_impl
== "chromadb.api.async_fastapi.AsyncFastAPI"
):
pytest.skip(
Copy link
Collaborator

Choose a reason for hiding this comment

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

aside - when can we remove this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i haven’t been able to find a root cause, will set aside a couple hours for it this week

ids_to_query = data.draw(
st.lists(
st.sampled_from(normalized_record_set["ids"]),
min_size=ids_subset_size,
Copy link
Collaborator

Choose a reason for hiding this comment

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

why choose id_subset_size when min_size and max_size do the same thing? This introduces more entropy requirements.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah yea ok, will make it create more diverse sets of data

Copy link
Collaborator

Choose a reason for hiding this comment

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

No I think its the same thing is my point. St.lists() choosing between min and max size is equivalent to data.draw(st.integers(min_value=0, max_value=total_count))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh got it okay. removing id_subset_size.

it's this now

ids_to_query = data.draw(
        st.lists(
            st.sampled_from(normalized_record_set["ids"]),
            min_size=0,
            max_size=total_count,
            unique=True,
        )
    )

@jairad26 jairad26 force-pushed the jai/query-on-doc-ids branch from b4d8d7b to 03ccf12 Compare April 29, 2025 17:34
@jairad26 jairad26 enabled auto-merge (squash) April 29, 2025 17:36
@jairad26 jairad26 merged commit 827f0f9 into main Apr 29, 2025
70 checks passed
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.

4 participants