Command Palette

Search for a command to run...

Hectal
Phase 5: Streaming and UX
LLM Engineering · Exercise 2 of 5 PreviousNext
Intermediate

Assemble a streamed reply

Write assemble(events). events is a list of data strings: JSON chunks, and possibly "[DONE]". Return a dictionary {"text": ..., "finish_reason": ..., "usage": ...}.

text is all delta.content pieces joined (some deltas have no content or null). finish_reason is the first non-null one (or None). usage is the usage object of whichever chunk has it (or None). Some chunks have an empty choices list. Stop at "[DONE]" and ignore anything after it.

What Check tests

  1. Joins the content pieces
  2. Reads the finish reason from the stop chunk
  3. Reads usage from a chunk with no choices
  4. Usage inside a chunk that also has choices
  5. Null or missing content is fine
  6. Stops at [DONE]

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.