Few-Shot Learning with LLMs
Advertisement
Introduction
Few-shot learning enables LLMs to perform new tasks by providing just a few examples. This guide covers implementation and optimization strategies.
- Few-Shot vs Zero-Shot
- Implementation
- Few-Shot for Structured Output
- Optimal Number of Examples
- Few-Shot for Code Generation
- Few-Shot with Domain Knowledge
- Few-Shot with Format Specification
- Few-Shot Optimization
- Few-Shot Evaluation
- Conclusion
- FAQ
Few-Shot vs Zero-Shot
# Zero-shot: No examples
prompt_zero = "Translate 'Hello' to French"
# Few-shot: Multiple examples
prompt_few = """Translate to French:
'Goodbye' -> 'Au revoir'
'Thank you' -> 'Merci'
'Hello' -> ?"""
Implementation
from openai import OpenAI
client = OpenAI()
def few_shot_classification(text: str, examples: list) -> str:
"""Classify using few-shot learning."""
# Build examples section
examples_text = "\n".join([
f'Text: "{ex['text']}"\nLabel: {ex['label']}'
for ex in examples
])
prompt = f"""Classify the following text as positive or negative sentiment.
Examples:
{examples_text}
Now classify:
Text: "{text}"
Label:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
return response.choices[0].message.content.strip()
# Usage
examples = [
{"text": "I love this product!", "label": "positive"},
{"text": "This is terrible", "label": "negative"},
{"text": "Amazing quality!", "label": "positive"}
]
result = few_shot_classification("Good product, worth it", examples)
print(result) # positive
Few-Shot for Structured Output
def few_shot_extraction(text: str) -> dict:
"""Extract structured info using few-shot."""
prompt = """Extract person information from text.
Examples:
Text: "John Smith, age 30, lives in NYC"
Output: {{"name": "John Smith", "age": 30, "city": "NYC"}}
Text: "Sarah Johnson is 25 years old from LA"
Output: {{"name": "Sarah Johnson", "age": 25, "city": "LA"}}
Text: "{}"
Output:""".format(text)
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
import json
output = response.choices[0].message.content
return json.loads(output)
# Usage
result = few_shot_extraction("Mike Chen, 28, based in San Francisco")
print(result)
Optimal Number of Examples
# General guidelines:
# 1-3 examples: Good starting point
# 3-5 examples: Better performance
# 5-10 examples: Optimal for most tasks
# 10+: Diminishing returns
# Too many examples can confuse the model
Few-Shot for Code Generation
def few_shot_code_generation(task: str) -> str:
"""Generate code using few-shot examples."""
prompt = f"""Generate Python code for the task.
Example 1:
Task: Reverse a string
Code:
def reverse_string(s):
return s[::-1]
Example 2:
Task: Find maximum in list
Code:
def find_max(lst):
return max(lst)
Task: {task}
Code:"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
return response.choices[0].message.content
# Usage
code = few_shot_code_generation("Count vowels in a string")
print(code)
Few-Shot with Domain Knowledge
def domain_specific_few_shot(question: str, domain: str) -> str:
"""Answer using domain-specific examples."""
domain_examples = {
"medical": [
{"q": "What treats hypertension?", "a": "ACE inhibitors, beta-blockers"},
{"q": "Symptom of diabetes?", "a": "High blood sugar, thirst, fatigue"}
],
"legal": [
{"q": "What is mens rea?", "a": "Criminal intent"},
{"q": "Definition of tort?", "a": "Civil wrong or injury"}
]
}
examples = domain_examples.get(domain, [])
examples_text = "\n".join([
f"Q: {ex['q']}\nA: {ex['a']}"
for ex in examples
])
prompt = f"""Answer questions in the {domain} domain.
Examples:
{examples_text}
Q: {question}
A:"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
return response.choices[0].message.content
# Usage
answer = domain_specific_few_shot(
"What causes a stroke?",
"medical"
)
print(answer)
Few-Shot with Format Specification
def format_specific_few_shot(data: str) -> str:
"""Format output with specific structure."""
prompt = f"""Convert text to JSON format.
Examples:
Text: "Alice is 30 and works at Google"
JSON: {{"name": "Alice", "age": 30, "company": "Google"}}
Text: "Bob, 25, software engineer"
JSON: {{"name": "Bob", "age": 25, "role": "software engineer"}}
Text: {data}
JSON:"""
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "user", "content": prompt}],
temperature=0
)
return response.choices[0].message.content
Few-Shot Optimization
def optimize_examples(task: str, candidate_examples: list):
"""Analyze which examples work best."""
results = {}
for i, example_set in enumerate(candidate_examples):
# Test with this example set
prompt = f"""[prompt with examples]"""
response = client.chat.completions.create(
model="gpt-4",
messages=[{"role": "user", "content": prompt}]
)
accuracy = evaluate_response(response)
results[f"set_{i}"] = accuracy
# Return best examples
best = max(results, key=results.get)
return best, results
Few-Shot Evaluation
def evaluate_few_shot_performance(test_cases: list):
"""Evaluate few-shot performance."""
correct = 0
for test in test_cases:
result = few_shot_classification(test['text'], examples)
if result == test['expected']:
correct += 1
accuracy = correct / len(test_cases)
print(f"Accuracy: {accuracy * 100:.1f}%")
return accuracy
Conclusion
Few-shot learning is powerful for rapid task adaptation. Start with 3-5 well-chosen examples for best results.
FAQ
Q: Should I use zero-shot or few-shot? A: Try zero-shot first. Use few-shot if accuracy is insufficient.
Q: How do I choose good examples? A: Pick diverse, representative examples that cover different cases.
Q: Can few-shot replace fine-tuning? A: For small tasks, yes. For large-scale deployment, fine-tuning is more reliable.
Advertisement