Skip to content

collection.query() should return all elements if n_results is greater than the total number of elements in the collection #301

@fritzprix

Description

@fritzprix
import chromadb
from sentence_transformers import SentenceTransformer

inputs = [
    "전문가들, 대유행 이후 원격 근무 수요 증가 예상",
    "SNS 사용과 정신 건강 문제 간의 연관성 발견한 새로운 연구",
    "지속 가능한 패션, 젊은 세대들 사이에서 인기 증가",
    "인공지능 기술 발전으로 의료 진단과 치료에 대한 희망 제시",
    "환경 활동가들, 정부의 플라스틱 폐기물 감소에 대한 조치를 촉구"
]


client = chromadb.Client()
collection = client.get_or_create_collection("chat_logs")
collection.add(
    documents=inputs, 
    metadatas=[{'user': 'david'} for _ in inputs],
    ids=[f'uid_{i}' for i in range(len(inputs))]
)
collection.query(query_texts=".", where={"user": 'david'}, n_results=10)

the code above raises exception complaining n_results is greater than total number of elements in given collection

    237 def _query(
...
    495     results = self.get(
    496         collection_uuid=collection_uuid, where=where, where_document=where_document
    497     )

NotEnoughElementsException: Number of requested results 10 cannot be greater than number of elements in index 5

I think this should fail in more graceful way.
if number of elements < n_results, then all elements can be returned instead of raise exception. otherwise, at least, there should be sensible way to check the number of elements prior to call query()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions