Choose Few-Shot Examples by Similarity and Diversity

medium
Unit 23 · Structured Output0/2 to milestone

Few-shot examples work best when they're similar to the user's input but not near-duplicates of each other. Select k examples from a pool of embeddings.

Implement select_examples(query, pool, k, max_sim=0.95, label_balance=None):

  • Rank pool items by cosine similarity to the query (highest first, ties by index).
  • Walk down the ranking and skip any candidate whose similarity to an already selected example exceeds max_sim.
  • If label_balance is given (a list of labels, one per pool item), don't select more than ceil(k / number_of_distinct_labels) examples from any one label.
  • Return the selected indices, in selection order. Return fewer than k if the constraints run out.
#prompting#retrieval#in-context-learning2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Choose Few-Shot Examples by Similarity and Diversity · TechGradify