Command Palette

Search for a command to run...

Hectal
Phase 2: Embeddings in Depth
RAG and Vector Search · Exercise 4 of 5 PreviousNext
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

  1. Five texts in batches of two take three calls
  2. Vectors come back in the order of the texts
  3. An empty list makes no calls
  4. A batch size below 1 is rejected
  5. 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.