UNIT – I: Foundations of Prompt Engineering
1. Environment and Connectivity: Install required packages (e.g., transformers, openai); securely
configure the API key; run a simple “Hello, world” prompt to verify model access..
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
response = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[
{"role": "user", "content": "Output exactly: Hello, world! (no extra text)"}
]
)
print(response.choices[0].message.content)
2. Baselinevs.Enhanced Prompts: Executeanaïve prompt(“Write aone-paragraph bio of Ada Lovelace.”) and an enhanced prompt that adds role framing, specificity, and explicit format instructions; compare both outputs for relevance, completeness, andstyle
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
# Baseline Prompt
baseline = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{"role": "user", "content": "Write a one-paragraph bio of Sachin Tendulkar."}]
)
print("Baseline Output:\n")
print(baseline.choices[0].message.content)
# Enhanced Prompt
enhanced = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{
"role": "user",
"content": "Act as a cricket player. Write a concise one-paragraph biography of Sachin Tendulkar. Include his contributions to cricket and maintain a formal tone."
}]
)
print("\nEnhanced Output:\n")
print(enhanced.choices[0].message.content)
3. Iterative Refinement on a Simple Task: Summarize the plot of the Shakespearean play Romeo and Juliet in two sentences through three rounds of prompt tweaking:
a. Minimalinstruction.
b. Additionoflengthandstyleconstraints
c. Specification of key content elements (setting and theme) Document how each iteration changes and improves the result.
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
# Step 1: Minimal
prompt1 = "Summarize Romeo and Juliet."
# Step 2: Add constraint
prompt2 = "Summarize Romeo and Juliet in 2 sentences."
# Step 3: Add details
prompt3 = "Summarize Romeo and Juliet in 2 sentences. Include theme and setting."
for p in [prompt1, prompt2, prompt3]:
res = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{"role": "user", "content": p}]
)
print("\nPrompt:", p)
print(res.choices[0].message.content)
4. Diagnosing Prompt Failures & Edge Cases: Craft a vague or contradictory prompt; analyze the failure mode (ambiguity, missing context, or format errors); refine the prompt by adding examples or clarifying instructions.
Observation:
Faulty prompts produce inconsistent or incorrect outputs
Issues arise due to ambiguity, contradictions, and missing context
Conclusion:
“Prompt failures can be mitigated by refining instructions, adding constraints, and providing examples to guide the model effectively.”
UNIT – II: Advanced Prompt Patterns and Techniques
1. Few-Shot vs. Zero-Shot Comparison: Design and execute a zero-shot prompt and a few-shotprompt(with 2–3 exemplar input-output pairs) for a chosen text task(e.g., sentiment classification or translation);compare outputs for accuracy,consistency, and adherence to examples.
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
zero = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Classify sentiment (Positive/Negative): The product is amazing."
}]
)
print("Zero-shot:", zero.choices[0].message.content)
few = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": """Classify sentiment:
Text: I love this phone → Positive
Text: This is terrible → Negative
Text: The product is amazing →"""
}]
)
print("Few-shot:", few.choices[0].message.content)
2. Role-Based and Negative Prompting: Craft a role-based prompt to establish a specific persona (e.g., “You are a financial advisor…”); then create a negative prompt to suppress undesired content (e.g., “Do not mention any brand names”); evaluate how each influences the model’s response.
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
role = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "You are a financial advisor. Suggest investment options for beginners."
}]
)
print("Role-based:\n", role.choices[0].message.content)
negative = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "You are a financial advisor. Suggest investment options. Do not mention any company or brand names."
}]
)
print("Negative prompt:\n", negative.choices[0].message.content)
3. Constraint Specification and Iterative Refinement: Select an open-ended task (e.g., summarizing a technical article); issue a basic prompt; identify failures in length or format; refine the prompt by adding explicit constraints (word count, bullet format,etc.); document improvements over two refinement cycles.
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
p1 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence."
}]
)
print("Basic:\n", p1.choices[0].message.content)
p2 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence in 3 sentences."
}]
)
print("\nWith constraint:\n", p2.choices[0].message.content)
p3 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence in exactly 3 bullet points using simple language."
}]
)
print("\nRefined:\n", p3.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
response = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[
{"role": "user", "content": "Output exactly: Hello, world! (no extra text)"}
]
)
print(response.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
# Baseline Prompt
baseline = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{"role": "user", "content": "Write a one-paragraph bio of Sachin Tendulkar."}]
)
print("Baseline Output:\n")
print(baseline.choices[0].message.content)
# Enhanced Prompt
enhanced = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{
"role": "user",
"content": "Act as a cricket player. Write a concise one-paragraph biography of Sachin Tendulkar. Include his contributions to cricket and maintain a formal tone."
}]
)
print("\nEnhanced Output:\n")
print(enhanced.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
# Step 1: Minimal
prompt1 = "Summarize Romeo and Juliet."
# Step 2: Add constraint
prompt2 = "Summarize Romeo and Juliet in 2 sentences."
# Step 3: Add details
prompt3 = "Summarize Romeo and Juliet in 2 sentences. Include theme and setting."
for p in [prompt1, prompt2, prompt3]:
res = client.chat.completions.create(
model="meta-llama/llama-3-8b-instruct",
messages=[{"role": "user", "content": p}]
)
print("\nPrompt:", p)
print(res.choices[0].message.content)
Observation: Faulty prompts produce inconsistent or incorrect outputs Issues arise due to ambiguity, contradictions, and missing context Conclusion: “Prompt failures can be mitigated by refining instructions, adding constraints, and providing examples to guide the model effectively.”
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
zero = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Classify sentiment (Positive/Negative): The product is amazing."
}]
)
print("Zero-shot:", zero.choices[0].message.content)
few = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": """Classify sentiment:
Text: I love this phone → Positive
Text: This is terrible → Negative
Text: The product is amazing →"""
}]
)
print("Few-shot:", few.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
role = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "You are a financial advisor. Suggest investment options for beginners."
}]
)
print("Role-based:\n", role.choices[0].message.content)
negative = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "You are a financial advisor. Suggest investment options. Do not mention any company or brand names."
}]
)
print("Negative prompt:\n", negative.choices[0].message.content)
from openai import OpenAI
client = OpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY HERE"
)
p1 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence."
}]
)
print("Basic:\n", p1.choices[0].message.content)
p2 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence in 3 sentences."
}]
)
print("\nWith constraint:\n", p2.choices[0].message.content)
p3 = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[{
"role": "user",
"content": "Summarize Artificial Intelligence in exactly 3 bullet points using simple language."
}]
)
print("\nRefined:\n", p3.choices[0].message.content)
No comments:
Post a Comment