Article
few-shot-learningprompt-engineeringin-context-learningllmopenaipythontask-adaptation
Master Few-Shot Learning for Better LLM Prompts
Use Few-Shot Learning to guide LLMs by providing examples directly in your prompt. This technique improves accuracy and controls output format for new tasks without needing to fine-tune a model. It's a core skill for effective prompting.
beginner15 min4 steps
The play
- Establish a Zero-Shot BaselineFirst, ask the model to perform a task with no examples. This is called 'zero-shot' prompting. It helps you understand the model's default behavior and provides a baseline to measure the improvement from Few-Shot Learning.
- Introduce a Single Example (One-Shot)Now, add one complete input-output example to your prompt before the final query. This single demonstration, or 'one-shot' learning, shows the model the exact format and type of answer you expect, significantly improving its performance.
- Apply Few-Shot Learning with Multiple ExamplesExpand to 2-5 examples to apply Few-Shot Learning. Providing multiple examples helps the model understand nuance and handle more complex cases. This reinforces the desired pattern and increases the reliability of the output.
- Select Diverse and High-Quality ExamplesThe success of Few-Shot Learning depends on your examples. Choose examples that are clear, accurate, and cover a range of scenarios (e.g., positive, negative, and neutral). The model learns the pattern you provide, so high-quality inputs lead to high-quality outputs.
Starter code
import os
from openai import OpenAI
# Make sure to set your OPENAI_API_KEY environment variable
# export OPENAI_API_KEY='your-key-here'
client = OpenAI()
def classify_sentiment_with_few_shot(text_to_classify):
"""Uses Few-Shot Learning to classify text sentiment."""
# 1. Define high-quality examples
examples = [
{"text": "This movie was fantastic, a must-see!", "sentiment": "positive"},
{"text": "I'm not sure how I feel about this product.", "sentiment": "neutral"},
{"text": "The customer service was incredibly slow and unhelpful.", "sentiment": "negative"}
]
# 2. Construct the prompt with examples
prompt_parts = []
for example in examples:
prompt_parts.append(f"Text: {example['text']}\nSentiment: {example['sentiment']}")
# Add the new text to classify
prompt_parts.append(f"Text: {text_to_classify}\nSentiment:")
final_prompt = "\n\n---\n\n".join(prompt_parts)
print(f"--- Generated Prompt ---\n{final_prompt}\n------------------------")
# 3. Call the API
try:
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{"role": "system", "content": "You are a helpful assistant that classifies text sentiment as positive, negative, or neutral."},
{"role": "user", "content": final_prompt}
],
temperature=0,
max_tokens=10
)
return response.choices[0].message.content.strip()
except Exception as e:
return f"An error occurred: {e}"
# Text we want to classify
new_text = "The setup was easy but the battery life is disappointing."
# Get the classification
predicted_sentiment = classify_sentiment_with_few_shot(new_text)
print(f"\nInput Text: '{new_text}'")
print(f"Predicted Sentiment: {predicted_sentiment}")