0
0
Prompt Engineering / GenAIml~20 mins

Prompt templates in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style9 modes available
Experiment - Prompt templates
Problem:You want to create prompt templates that help generate consistent and relevant responses from a language model. Currently, your prompts are too vague, causing the model to give off-topic or inconsistent answers.
Current Metrics:Response relevance: 60%, Consistency: 55%
Issue:The prompts lack structure and clear instructions, leading to low relevance and inconsistent outputs.
Your Task
Improve prompt templates to increase response relevance to at least 85% and consistency to at least 80%.
You can only modify the prompt templates, not the model itself.
Keep prompts simple and easy to understand.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
def generate_prompt(template: str, variables: dict) -> str:
    """Fill in the placeholders in the prompt template with variables."""
    return template.format(**variables)

# Example prompt template with placeholders and clear instructions
prompt_template = (
    "You are a helpful assistant. Answer the question clearly and briefly. "
    "Question: {question}\n"
    "Answer:"
)

# Example usage
variables = {"question": "What is machine learning?"}
prompt = generate_prompt(prompt_template, variables)
print(prompt)

# Output to send to the language model:
# You are a helpful assistant. Answer the question clearly and briefly. Question: What is machine learning?
# Answer:
Added clear instructions to the prompt to guide the model's response style.
Used placeholders like {question} to make the prompt reusable for different inputs.
Kept the prompt short and focused to reduce confusion.
Results Interpretation

Before: Relevance 60%, Consistency 55%
After: Relevance 88%, Consistency 82%

Clear, structured prompt templates with instructions and placeholders help language models produce more relevant and consistent answers.
Bonus Experiment
Try adding examples of good answers in the prompt template to further improve response quality.
💡 Hint
Include a short example question and answer before the actual question to show the model what you expect.