1# The few-shot prompt IS the programming.
2prompt = """Translate English to French.
3
4sea otter => loutre de mer
5peppermint => menthe poivree
6cheese =>"""
7
8
9def generate(model, prompt: str, max_tokens: int = 20,
10 temperature: float = 0.7, top_p: float = 0.9) -> str:
11 """The autoregressive loop behind every GPT completion."""
12 tokens = model.tokenize(prompt) # BPE, ~50k vocabulary
13
14 for _ in range(max_tokens):
15 # One full forward pass: 96 layers of masked self-attention.
16 # The model attends BACK over the examples in the prompt --
17 # that attention is where in-context learning happens.
18 probs = model.next_token_probs(tokens)
19
20 # Temperature: reshape the distribution (see the Playground below)
21 probs = probs ** (1.0 / temperature)
22 probs = probs / probs.sum()
23
24 # Nucleus (top-p) sampling: keep the smallest set of tokens
25 # covering top_p of the probability mass, then sample.
26 next_token = nucleus_sample(probs, top_p)
27
28 if next_token == model.END_OF_TEXT:
29 break
30 tokens.append(next_token) # append & repeat
31
32 return model.detokenize(tokens)
33
34
35# No gradient updates. No fine-tuning. The 'training' for the
36# translation task is the two examples sitting in the prompt.
37completion = generate(model, prompt)
38print(completion.splitlines()[-1]) # cheese => fromage