Prompt Engineering

Learn how to write effective prompts to optimise your language model outputs.

Overview

Large language models are powerful tools that can generate human-like text based on the prompts you provide. However, the quality of the output heavily depends on how well you structure and craft your prompts.

By incorporating elements such as context, instructions, and examples, you can guide the model to produce more accurate and relevant responses.


Basic Prompting

Let's start with a simple example to illustrate the importance of effective prompting.

❌ Vague Prompt Example

Prompt: The sun is

Output: shining.

While the model's response is coherent, it may not align with your intended goal.

✅ Improved Prompt Example

To improve the output, you can provide more specific instructions:

Prompt: Describe the sun's appearance during a sunset.

Output: As the sun descends towards the horizon, it paints the sky in a breathtaking array of warm colors. The once bright yellow orb now appears as a deep orange, casting a golden glow across the landscape. The surrounding clouds are set ablaze with hues of pink, purple, and red, creating a mesmerizing tapestry that stretches as far as the eye can see. The sun's rays become softer and more diffused, bathing everything in a gentle, ethereal light. It's a moment of pure magic, where the world seems to pause and bask in the beauty of the setting sun.

By providing a clear instruction, the model generates a more detailed and relevant response.


Prompt Formatting Strategies

When structuring your prompts, you have several effective approaches to choose from:

Question-Answer Format

Q: What are the benefits of regular exercise?
A:

Instruction-Based Approach

Instruction: Explain the process of photosynthesis in simple terms.

Direct Question Format

For question-answering tasks, you can omit the "Q:" prefix, as the model can infer the question from the sequence:

What is the capital of France?

Few-Shot Prompting

Few-shot prompting involves providing the model with examples of the desired output. This technique enables in-context learning, allowing the model to understand the task more effectively.

Example: Sentiment Classification

Classify the sentiment of the following movie reviews:

Review: The acting was phenomenal, and the plot kept me engaged from start to finish.
Sentiment: Positive

Review: The special effects were impressive, but the story felt predictable and clichéd.
Sentiment: Mixed

Review: I struggled to stay awake during this boring and uninspired film.
Sentiment: Negative

Review: A cinematic masterpiece that will be remembered for generations.
Sentiment:

By providing examples, the model learns to classify the sentiment of movie reviews based on the context you've given.

Key Benefits

  • Pattern Recognition: The model learns from your examples
  • Consistency: Outputs follow the established format
  • Accuracy: Better understanding leads to more precise results

Recap

Effective prompt engineering is crucial for getting the most out of language models. By carefully structuring your prompts, providing clear instructions, and incorporating examples when necessary, you can guide the model to generate high-quality, relevant responses.

Next Steps:

  • Experiment with different prompting techniques
  • Find the approach that works best for your specific use case
  • Iterate and refine based on results

General Tips for Designing Prompts

When designing prompts for AI models, whether from OpenAI, Cohere, or any other platform, there are several key strategies to keep in mind. These strategies will help refine your prompts to achieve more accurate and useful results.

🚀 Start Simple

The process of designing prompts is inherently iterative and demands experimentation.

Begin with straightforward prompts, gradually incorporating complexity as needed to enhance results. This approach prevents overwhelming the process with too much complexity too soon.

Example Progression:

  1. Start: Summarize this paragraph
  2. Then: Summarize this document
  3. Finally: Summarize this complex technical document

📝 Clear Instructions

Effective prompts often start with clear action words:

Action WordUse Case
WriteContent creation
SummarizeText condensation
TranslateLanguage conversion
ExplainKnowledge transfer
ListInformation extraction

Best Practice: Use Separators

A clear separator, like ###, can distinctly separate instructions from content:

### Instruction ###
Translate the following sentence into French:
Text: "Good morning!"

Output:

Bonjour!

🎯 Specificity is Key

The more detailed and specific a prompt is, the better the results tend to be. However, balance is key—include necessary details without overloading the prompt with irrelevant information.

Example: Information Extraction

Identify and list the scientific names of animals mentioned in the text below. 

Desired format:
Animals: <comma_separated_list_of_scientific_names>

Input: "In the depths of the Amazon rainforest, researchers have discovered a new species of frog, which they have named Leptolalax applebyi, alongside the well-documented Panthera onca, prowling nearby."

Output:

Animals: Leptolalax applebyi, Panthera onca

⚡ Avoid Impreciseness

Being overly clever or vague in your prompts can lead to imprecise results.

Instead of: "Give me a brief overview of quantum mechanics"
Try: "Explain quantum mechanics in three sentences for a high school student"

✅ Focus on What TO Do

Direct the model's attention toward the desired outcome rather than away from undesired ones.

❌ Poor Prompt Example

This is a chatbot that suggests travel destinations. DO NOT offer destinations based on weather or season. 

Customer: "I'm looking for a travel destination recommendation."

✅ Improved Prompt Example

This is a chatbot that suggests travel destinations based on cultural attractions and activities. Provide a destination recommendation without considering weather or season. 

Customer: "I'm looking for a travel destination recommendation."

Output:

Based on your interest in cultural attractions, I recommend visiting Kyoto, Japan. It's rich in history, temples, and traditional experiences.

Summary

By adhering to these guidelines, you can craft prompts that are more likely to produce the desired outcomes, whether you're:

  • Summarizing texts
  • Generating creative content
  • Extracting specific information
  • Translating languages
  • Analyzing data

Remember, prompt design is an art that benefits greatly from continuous refinement and experimentation.

Quick Reference Checklist

  • Start with simple, clear instructions
  • Be specific about desired output format
  • Provide examples when needed (few-shot)
  • Focus on positive instructions
  • Use clear separators between sections
  • Iterate and refine based on results