Skip to main content

Prompt Engineering Fundamentals

 

Introduction

Generative AI is a transformative technology capable of producing text, images, audio, and code in response to user prompts. This capability is powered by Large Language Models (LLMs) like OpenAI's GPT series, which are trained to understand and generate natural language. Interacting with these models via prompts allows users to harness their potential without needing technical expertise. This chapter explores the essentials of prompt engineering, a field dedicated to optimizing prompt design for consistent and high-quality responses.

Learning Goals

By the end of this lesson, you will be able to:

  • Explain what prompt engineering is and why it matters.
  • Describe the components of a prompt and how they are used.
  • Learn best practices and techniques for prompt engineering.
  • Apply learned techniques to real examples using an OpenAI endpoint.

Learning Sandbox

Prompt engineering is more art than science, requiring practice and iterative refinement. This lesson includes a Jupyter Notebook sandbox environment where you can experiment with different prompt engineering techniques. To get started, you'll need:

  • An Azure OpenAI API key
  • A Python runtime
  • Local environment variables

The notebook contains starter exercises, but you're encouraged to add your own examples to build your intuition for prompt design.

Illustrated Guide

Before diving in, check out the illustrated guide to get an overview of the main topics covered and the key takeaways.

Our Startup

In the context of our startup, which aims to bring AI innovation to education, prompt engineering is crucial. Different users might design prompts to:

  • Analyze curriculum data
  • Generate lesson plans
  • Tutor students in difficult subjects

Explore the "Prompts For Education" open-source library for more examples.

What is Prompt Engineering?

Prompt engineering involves designing and optimizing text inputs (prompts) to deliver consistent and quality responses for a given application objective and model. This process can be broken down into:

  1. Designing the initial prompt for a given model and objective
  2. Refining the prompt iteratively to improve response quality

Key Concepts

  • Tokenization: How the model sees the prompt as a sequence of tokens.
  • Base LLMs: How the foundation model processes a prompt.
  • Instruction-Tuned LLMs: How the model can now see tasks and follow instructions.

Why Prompt Engineering Matters

LLMs can be challenging due to their stochastic nature, potential for fabrications, and varying capabilities. Effective prompt engineering addresses these challenges by providing guardrails, minimizing inaccuracies, and optimizing for specific models.

Prompt Construction

Basic Prompt

A simple text input sent to the model without additional context.

Example:


Prompt: "Oh say can you see"

Completion: "It sounds like you're starting the lyrics to 'The Star-Spangled Banner,' the national anthem of the United States. The full lyrics are ..."


Prompt: "Oh say can you see" Completion: "It sounds like you're starting the lyrics to 'The Star-Spangled Banner,'
the national anthem of the United States. The full lyrics are ..."

Complex Prompt

Adds context and instructions to the basic prompt using the Chat Completion API.

Example:

response = openai.chat.completions.create( model="gpt-3.5-turbo", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": "Who won the world series in 2020?"}, {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."}, {"role": "user", "content": "Where was it played?"} ] )

Instruction Prompt

Provides detailed instructions for the task.

Example:

Prompt: "Write a description of the Civil War in 1 paragraph. Provide 3 bullet points with key
dates and their significance. Provide 3 more bullet points with key historical figures and
their contributions. Return the output as a JSON file." Completion: Returns a detailed, formatted JSON response.

Primary Content

Combines instruction with relevant content to guide the model's response.

Example:

Prompt: "Summarize this in 2 sentences. Jupiter is the fifth planet from the Sun and
the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun,
but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is
one of the brightest objects visible to the naked eye in the night sky, and has been known to
ancient civilizations since before recorded history." Completion: "Jupiter, the fifth planet from the Sun, is the largest in the Solar System and is
known for being one of the brightest objects in the night sky. Named after the Roman god
Jupiter, it's a gas giant whose mass is two-and-a-half times that of all other planets in
the Solar System combined."

Examples, Cues, and Templates

  • Examples: Provide patterns for the model to infer desired outputs.
  • Cues: Give the model a starting point for the response.
  • Templates: Reusable recipes for prompts that can be customized with specific data.

Supporting Content

Additional context like tuning parameters, formatting instructions, or topic taxonomies to influence the model's output.

Best Practices

Prompt Engineering Mindset

  • Domain Understanding Matters: Customize techniques based on domain expertise.
  • Model Understanding Matters: Use knowledge of the model's strengths and limitations.
  • Iteration & Validation Matters: Continuously iterate and validate prompts for better results.

Recommended Best Practices

  • Evaluate the latest models: Assess new models for improved features and quality.
  • Separate instructions & context: Use delimiters to distinguish different parts of the prompt.
  • Be specific and clear: Provide detailed instructions and examples.
  • Use cues: Nudge the model towards desired outcomes.
  • Double Down: Repeat instructions if necessary.
  • Order Matters: The sequence of information can impact the output.
  • Give the model an "out": Provide fallback responses to reduce fabrications.

Assignment

Use the Jupyter Notebook with exercises to apply these concepts. Experiment with different prompts and record your findings to build a knowledge base for future reference.

Knowledge Check

Which of the following is a good prompt following some reasonable best practices?

  1. Show me an image of a red car.
  2. Show me an image of a red car of make Volvo and model XC90 parked by a cliff with the sun setting.
  3. Show me an image of a red car of make Volvo and model XC90.

Correct Answer: 2, as it provides detailed and specific instructions, enhancing the quality of the response.

By mastering prompt engineering, you can unlock the full potential of Generative AI, ensuring your applications deliver high-quality, relevant, and ethical responses.



# Prompt Engineering Fundamentals


## Introduction


Generative AI, powered by Large Language Models (LLMs), can create diverse content in response to user prompts. This notebook will introduce you to prompt engineering, the 

process of designing and optimizing prompts to achieve consistent and high-quality responses.


### Learning Goals


By the end of this notebook, you will be able to:

- Explain what prompt engineering is and why it matters.

- Describe the components of a prompt and how they are used.

- Learn best practices and techniques for prompt engineering.

- Apply learned techniques to real examples using an OpenAI endpoint.


### Setup


Before we start, ensure you have the following:

- An Azure OpenAI API key

- A Python runtime

- Local environment variables configured


Let's start by setting up the environment.

# Import necessary libraries
import openai
import os

# Load environment variables
from dotenv import load_dotenv
load_dotenv()

# Set OpenAI API key
openai.api_key = os.getenv("AZURE_OPENAI_KEY")

## Basic Prompt

Let's begin with a simple text input sent to the model without additional context.
# Basic prompt example
response = openai.Completion.create(
  engine="text-davinci-003",
  prompt="Oh say can you see",
  max_tokens=50
)

print(response.choices[0].text.strip())
## Complex Prompt

Adding context and instructions to the basic prompt using the Chat Completion API.
# Complex prompt example
response = openai.ChatCompletion.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Who won the world series in 2020?"},
        {"role": "assistant", "content": "The Los Angeles Dodgers won the World Series in 2020."},
        {"role": "user", "content": "Where was it played?"}
    ]
)

for message in response.choices[0].message:
    print(f'{message["role"]}: {message["content"]}')
## Instruction Prompt

Providing detailed instructions for a task to guide the AI's response.
## Primary Content

Combining instruction with relevant content to guide the model's response.
# Primary content example
primary_content = """
Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. Summarize this in 2 sentences.
"""

response = openai.Completion.create(
  engine="text-davinci-003",
  prompt=primary_content,
  max_tokens=50
)

print(response.choices[0].text.strip())
### Using Cues

Providing cues to nudge the AI towards a desired response.
# Prompt with cue example
cue_prompt = """
Jupiter is the fifth planet from the Sun and the largest in the Solar System. It is a gas giant with a mass one-thousandth that of the Sun, but two-and-a-half times that of all the other planets in the Solar System combined. Jupiter is one of the brightest objects visible to the naked eye in the night sky, and has been known to ancient civilizations since before recorded history. Summarize this.
What we learned is that Jupiter
"""

response = openai.Completion.create(
  engine="text-davinci-003",
  prompt=cue_prompt,
  max_tokens=50
)

print(response.choices[0].text.strip())
### Prompt Templates

Using templates to create reusable and consistent prompts.
# Prompt template example
template_prompt = """
Generate a lesson plan on the topic: {}
"""

topic = "Photosynthesis"
response = openai.Completion.create(
  engine="text-davinci-003",
  prompt=template_prompt.format(topic),
  max_tokens=200
)

print(response.choices[0].text.strip())
## Best Practices

### Mindset and Techniques

To conclude, let's review the best practices for prompt engineering and apply them.
### Evaluate the Latest Models

New model generations may offer improved features and quality.

### Separate Instructions & Context

Use delimiters to distinguish instructions, primary, and secondary content.

### Be Specific and Clear

Provide detailed instructions and examples to improve response quality.

### Use Cues

Nudge the AI towards desired outcomes with leading words or phrases.

### Double Down

Repeat instructions if necessary to reinforce the desired response.

### Order Matters

The sequence of information can impact the output.

### Give the Model an "Out"

Provide fallback responses to reduce fabrications.
## Assignment

Now, it's your turn to apply these concepts. Use the provided exercises and create your own prompts to experiment with. Record your observations and iteratively refine your prompts.

1. Create a prompt to generate a lesson plan on a topic of your choice.
2. Use few-shot examples to translate phrases into another language.
3. Design a complex prompt with specific instructions and context.

Happy prompt engineering!

## Knowledge Check

**Which of the following is a good prompt following some reasonable best practices?**

1. Show me an image of a red car.
2. Show me an image of a red car of make Volvo and model XC90 parked by a cliff with the sun setting.
3. Show me an image of a red car of make Volvo and model XC90.

**Correct Answer:** 2, as it provides detailed and specific instructions, enhancing the quality of the response.

Comments

Popular posts from this blog

AWS Cloud Containers

Amazon Web Services (AWS) offers a variety of services for deploying and managing applications in the cloud. One of these services is called Amazon Elastic Container Service (ECS), which allows you to run and manage Docker containers on AWS. Here is a brief overview of how Amazon ECS works: You package your application into a Docker container image and push it to a registry, such as Amazon Elastic Container Registry (ECR) or Docker Hub. You create an Amazon ECS task definition, which is a blueprint for your containerized application. The task definition specifies things like the Docker image to use, the CPU and memory requirements, and the environment variables to pass to the container. You create an Amazon ECS cluster, which is a group of Amazon EC2 instances that are running the Amazon ECS container agent. The cluster is where your tasks are placed and run. You create an Amazon ECS service, which is a long-running task that is hosted on your cluster. The service ensures that a speci...

Identified COVID-19 in X-ray images with deep learning.

  Project structure :       Our coronavirus (COVID-19) chest X-ray data is in the dataset/ directory where our two classes of data are separated into covid/ and normal/   I have created train_covid19.py file to train the model.   Three command line arguments (parameters) required to run this file :   --dataset: The path to our input dataset of chest X-ray images. --plot: An optional path to an output training history plot. By default the plot is named plot.png unless otherwise specified via the command line. --model: The optional path to our output COVID-19 model; by default it will be named covid19.model.     To load our data, we grab all paths to images in in the --dataset directory. Then, for each imagePath, we:   ·           Extract the class label (either covid or normal) from the path. ·           Load the image, and preprocess it by convertin...