chroma icon indicating copy to clipboard operation
chroma copied to clipboard

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

Open jairad26 opened this issue 1 year ago • 6 comments

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?

  • [x] 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?

jairad26 avatar Apr 09 '25 21:04 jairad26

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)

github-actions[bot] avatar Apr 09 '25 21:04 github-actions[bot]

This stack of pull requests is managed by Graphite. Learn more about stacking.

jairad26 avatar Apr 09 '25 21:04 jairad26

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 avatar Apr 15 '25 13:04 hesreallyhim

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 avatar Apr 15 '25 14:04 hesreallyhim

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

hesreallyhim avatar Apr 15 '25 14:04 hesreallyhim

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 avatar Apr 15 '25 16:04 jairad26