Prompt Engineering Lab

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.”

No comments:

Post a Comment