Human-written demonstration (OpenAssistant volunteers)· OpenAssistant (human-written)
OpenAssistant Conversations (oasst2)
Show me an implementation of nucleus sampling written in python
In OpenAssistant data, “assistant” turns were written by human volunteers as demonstrations. They are not model outputs.
- User
Show me an implementation of nucleus sampling written in python
- Assistant
Here's a basic implementation of nucleus sampling in Python: import random
def nucleus_sampling(items, k): """Performs nucleus sampling. Selects k items from items with a probability proportional to their score (items need to be (item, score) tuples or objects with a score attribute). items: List of (item, score) tuples or objects with a score attribute k: Number of items to select Returns: List of k selected items """ scores = [item[1] for item in items] sumscores = sum(scores) chosen = [] for in range(k): pick = random.uniform(0, sum_scores) current = 0 for item, score in items: current += score if current > pick: chosen.append(item) sum_scores -= score break return chosenFor example: items = [("a", 10), ("b", 30), ("c", 40)] sampled = nucleus_sampling(items, 2) print(sampled)
Might print ["b", "c"]
This selects 2 items (k=2) from the items list with a probability proportional to their scores. So the item with score 30 ("b") and the item with score 40 ("c") have a higher chance of being selected than the item with score 10 ("a").
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.