Retrieve limited chunksΒΆ

The search.run() method accepts the parameter limit to control the amount of data to retrieve from bigdata. By default, limit specifies the number of desired documents to download, and the logic will send multiple requests to bigdata to gather all those documents, but it is not possible to control the amount of retrieved chunks or usage. (See Monitor usage)

In some use cases, customers want to specify the number of requested chunks to control usage. For instance, the following code requests only 10 chunks and if it returns all requested 10 chunks, it will consume 1 API query unit.

from bigdata_client import Bigdata
from bigdata_client.query import Similarity
from bigdata_client.search import ChunkLimit

bigdata = Bigdata()
search = bigdata.search.new(query=Similarity("AI in finance"))

documents = search.run(ChunkLimit(100))

chunk_count = 0
for doc in documents:
    chunk_count += len(doc.chunks)
    if doc.cluster:
        chunk_count += sum([len(docs.chunks) for docs in doc.cluster])

print(f"Retrieved chunks count: {chunk_count}")
print(f"Note: Ten retrieved chunks consume one API Query Unit")
print(f"Usage (In API Query Units): {search.get_usage()}")

Output:

Retrieved chunks count: 85
Note: Ten retrieved chunks consume one API Query Unit
Usage (In API Query Units): 8

Note

The response might contain a smaller number of chunks due to discarding duplicates.