Intermediate
Embed in batches and restore the order
call_api(batch) stands in for an embeddings endpoint. It returns {"data": rows}, where each row is {"index": position in the batch, "embedding": [...]}, and the rows come back in reverse order.
Write embed_in_batches(texts, batch_size, call_api) returning (vectors, calls): vectors has one embedding per text, in the order of texts, and calls is how many times you called the API. Send at most batch_size texts per call. Raise ValueError when batch_size is below 1.
What Check tests
- Five texts in batches of two take three calls
- Vectors come back in the order of the texts
- An empty list makes no calls
- A batch size below 1 is rejected
- No call is bigger than the batch size
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
main.py
Sign in to run your code.
Output
Press Run to see what your program prints.