Few-shot learning guides a language model by placing a small set of input–output examples in the prompt before a new request. The examples demonstrate the required pattern or format, so the model need not infer the desired response from instruction alone. It is useful when zero-shot instructions produce inconsistent results for nuanced tasks. section 5.3.2
Treat the examples as a compact behavioral specification: make each one resemble the real request, show the response structure explicitly, then end with the new user input and an empty response slot. This can steer responses toward structured, contextually relevant output. section 5.3.2 section 5.7
def generate_text(prompt)
few_shot_examples = """ #A
Example 1:
User: Tell me about New York hotels
AI: The Plaza Hotel in New York is an iconic luxury hotel located at Fifth Avenue and Central Park South. It offers timeless elegance, world-class dining, and top-tier hospitality.
Example 2:
User: Tell me about Tokyo hotels
AI: The Park Hyatt Tokyo is a prestigious hotel known for its stunning skyline views, sophisticated atmosphere, and exceptional dining options. Located in Shinjuku, it provides a tranquil retreat in the heart of the city.
Now, following the same pattern, provide a response:
User: {prompt}
AI:
""".strip()
chat_completion = client.chat.completions.create(
messages=[
{"role": "user", "content": few_shot_examples.format(prompt=prompt)}
],
model=model,
temperature=0.7,
max_tokens=200,
)
return chat_completion.choices[0].message.content
prompt = "Tell me about Paris hotels"
response = generate_text(prompt) #B
print(response)
A hotel-description prompt supplies two completed examples, then appends a new hotel request after “Now, following the same pattern.” The model continues with the demonstrated response style. section 5.3.2
Choose prompting styles by task needs:
- Use zero-shot prompting when a direct instruction and the model’s existing knowledge are sufficient. section 5.3.2
- Use few-shot prompting when output patterns, formats, or contextual behavior need clear demonstrations. section 5.3.2
- Combine examples with chain-of-thought-style instructions for tasks where reasoning through steps can improve word-problem performance. section 5.3.2
Hotel-response pattern
Show examples for New York and Tokyo hotels, each using a concise description of location, atmosphere, and amenities. Then ask about Paris hotels; the generated Ritz Paris response follows the same luxury-hotel description pattern. section 5.3.2
Few-shot learning is one prompt-engineering tool among decoding settings and model choices. It improves control over generated text, but model selection still involves performance, cost, privacy, customization, and deployment trade-offs. section 5.0 section 5.7























