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.1-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.1-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.1-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.1-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)
UNIT – III: Structured Output and Reasoning Techniques
1.Structured Format Prompting: Instruct the model to output information as bullet listsand Markdown tables (e.g., “List three benefits of daily exercise in a Markdown table with columns ‘Benefit’ and ‘Description.’”); verify the output matches the requested structure.
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.1-8b-instruct",
messages=[
{
"role": "user",
"content": """
List three benefits of daily exercise in a Markdown table
with columns 'Benefit' and 'Description'.
"""
}
]
)
print(response.choices[0].message.content)
2.JSON/YAML Generation: Provide a brief dataset description (e.g., three books with title,author,publicationyear)andpromptthemodeltoproducevalidJSONor YAML;use a parser to validate syntax and refine the prompt if errors occur.
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.1-8b-instruct",
messages=[
{
"role": "user",
"content": """
Return ONLY valid JSON.
Do not include explanations.
Do not include markdown.
Do not include notes.
Generate only valid JSON for three Java Programming books with:
title, author, and publication year.
"""
}
]
)
print(response.choices[0].message.content)
import json
data = response.choices[0].message.content
try:
parsed = json.loads(data)
print("Valid JSON")
except:
print("Invalid JSON")
3.Chain-of-Thought and Task Decomposition: Present a multi-step problem (e.g., a logic puzzle) and apply zero-shot CoT prompting (e.g., “Let’s think step by step. Explainyour reasoning before the final answer.”); separately, decompose the problem into sequential sub-questions,collect partial answers,combine them,and compare accuracy against a direct-answer baseline
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.1-8b-instruct",
messages=[
{
"role": "user",
"content": "A train travels 60 km in 1 hour. How far will it travel in 5 hours at the same speed?"
}
]
)
print(response.choices[0].message.content)
UNIT – IV: Retrieval-Augmented Generation and LangChain Workflows
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.1-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.1-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.1-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.1-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)
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.1-8b-instruct",
messages=[
{
"role": "user",
"content": """
List three benefits of daily exercise in a Markdown table
with columns 'Benefit' and 'Description'.
"""
}
]
)
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"
)
response = client.chat.completions.create(
model="meta-llama/llama-3.1-8b-instruct",
messages=[
{
"role": "user",
"content": """
Return ONLY valid JSON.
Do not include explanations.
Do not include markdown.
Do not include notes.
Generate only valid JSON for three Java Programming books with:
title, author, and publication year.
"""
}
]
)
print(response.choices[0].message.content)
import json
data = response.choices[0].message.content
try:
parsed = json.loads(data)
print("Valid JSON")
except:
print("Invalid JSON")
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.1-8b-instruct",
messages=[
{
"role": "user",
"content": "A train travels 60 km in 1 hour. How far will it travel in 5 hours at the same speed?"
}
]
)
print(response.choices[0].message.content)
1. Building a Simple LCEL Chain: Create a minimal LCEL script that accepts a fixed instruction(e.g.,“Summarizethistext:…”),passesittoanLLM,andprintsthe result; verify end-to-end execution.
from langchain_core.prompts import ChatPromptTemplate
from langchain_openai import ChatOpenAI
from langchain_core.output_parsers import StrOutputParser
# OpenRouter setup
llm = ChatOpenAI(
base_url="https://openrouter.ai/api/v1",
api_key="YOUR API KEY",
model="meta-llama/llama-3.1-8b-instruct"
)
# Prompt template
prompt = ChatPromptTemplate.from_template(
"Summarize this text: {text}"
)
# LCEL chain
chain = prompt | llm | StrOutputParser()
# Execute
result = chain.invoke({
"text": """Cloud computing provides computing resources such as servers,
storage, databases, networking, and software over the Internet. It allows
organizations to access resources on demand without maintaining expensive
physical infrastructure. Cloud computing also provides scalability,
flexibility, and cost efficiency."""
})
print(result)
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity
# ---------------------------------
# Step 1: Small document collection
# ---------------------------------
documents = [
"The car is very fast",
"The automobile is extremely quick"
]
# ---------------------------------
# Step 2: Generate document vectors
# ---------------------------------
vectorizer = TfidfVectorizer()
document_vectors = vectorizer.fit_transform(documents)
print("Number of documents:", len(documents))
print("Vector shape:", document_vectors.shape)
print("\nDocuments indexed successfully.")
# ---------------------------------
# Step 3: Inspect vectors
# ---------------------------------
print("\nSample vector:")
print(document_vectors[0].toarray())
No comments:
Post a Comment