Retrieve & generate

Putting it together: retrieve the top-k

You now have every piece. Retrieval is: embed the question, score it against every chunk with cosine, sort by score, and keep the best few โ€” the top-k (k is usually 3โ€“5). Those are the passages most likely to hold the answer.

sorted(..., key=..., reverse=True)[:k] does the sort-and-slice in one line โ€” the same sorted you'd use on any list, with a key that pulls out each chunk's score.