Lecture 2 Prompt Engineering
Lecture 2 Prompt Engineering
NLP Applications
Spring 2024
1
Agenda
Introduction to Prompt Engineering
Prompt Techniques
2
Introduction to Prompt Engineering
• Imagine you have a powerful tool like a large language model (LLM), but unlocking its full
potential requires knowing how to ask the right questions; That's where prompt engineering
comes in.
• It's the art and science of crafting effective prompts that guide AI models to generate desired
outputs.
3
Introduction to Prompt Engineering
• The quality and structure of the prompt can significantly influence the foundation model’s
performance on a given task.
4
Introduction to Prompt Engineering
5
Introduction to Prompt Engineering
6
Introduction to Prompt Engineering
7
Basics of Foundation Models
• Generative AI is a type of artificial intelligence that can create new content and ideas,
including conversations, stories, images, videos, and music.
• Like all other AI, generative AI is powered by machine learning (ML) models.
• However, generative AI is powered by very large models, commonly called foundation
models (FMs).
• FMs are pretrained on a vast corpus of data, usually through Self-Supervised Learning (SSL)
or Reinforcement Learning from Human Feedback (RLHF).
8
Basics of Foundation Models
9
Basics of Foundation Models
This makes it particularly useful in situations where labeled data is scarce or expensive
to obtain.
10
Basics of Foundation Models
11
Basics of Foundation Models
12
Basics of Foundation Models
13
Basics of Foundation Models
14
Basics of Foundation Models
Unlike traditional RL, which relies solely on numerical rewards, RLHF incorporates
human feedback to guide the learning process of AI agents.
15
Basics of Foundation Models
16
Basics of Foundation Models
This could be anything from writing different kinds of creative text formats, like
poems or code, to answering questions in a comprehensive and informative way.
17
Basics of Foundation Models
18
Basics of Foundation Models
19
Basics of Foundation Models
• Types of FMs:
Text-to-text models:
Text-to-text models are LLMs that are pretrained to process vast quantities of textual data and
human language.
These large foundation models can summarize text, extract information, respond to questions,
create content (such as blogs or product descriptions), and more.
20
Basics of Foundation Models
• Types of FMs:
Text-to-image models:
Text-to-image models take natural language input and produce a high-quality image that
matches the input text description.
Some examples of text-to-image models are DALL-E 2 from OpenAI, Imagen from the
Google Research Brain Team, Stable Diffusion from Stability AI, and Midjourney.
21
Basics of Foundation Models
• New Models:
Sora:
Sora is an AI model that can create realistic and imaginative scenes from text
instructions.
Creates videos up to 60 seconds long based on textual descriptions.
https://openai.com/sora
22
Basics of Foundation Models
• New Models:
AnyGPT:
https://arxiv.org/abs/2402.12226
23
Basics of Foundation Models
• New Models:
NExT-GPT:
https://arxiv.org/abs/2309.05519
24
Fundamentals of Prompt Engineering
25
Fundamentals of Prompt Engineering
26
Fundamentals of Prompt Engineering
• Elements of a prompt:
Instructions: This is a task for the large language model to do. It provides a task
description or instruction for how the model should perform.
Context: This is external information to guide the model.
Input data: This is the input for which you want a response.
Output indicator: This is the output type or format.
27
Fundamentals of Prompt Engineering
Prompt Output (Completion)
Write a summary of a service review using two sentences.
Review: Amazon Prime Student is a great option for students looking to save money. Not Amazon Prime Student is a fantastic
paying for shipping is the biggest save in my opinion. As a working mom of three who is also a option for college students, offering
student, it saves me tons of time with free 2-day shipping, and I get things I need quickly and free 2-day shipping, streaming
sometimes as early as the next day, while enjoying all the free streaming services, and books services, books, and other benefits
that a regular prime membership has to offer for half the price. Amazon Prime Student is only for half the price of a regular Prime
available for college students, and it offers so many things to help make college life easier. This membership. It saves time and
is why Amazon Prime is the no-brainer that I use to order my school supplies, my clothes, and money, making college life easier.
even to watch movies in between classes. I think Amazon Prime Student is a great investment
for all college students.
Summary:
28
Fundamentals of Prompt Engineering
29
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
30
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
31
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
32
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
33
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
34
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
35
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
36
Fundamentals of Prompt Engineering
Best practices for designing effective prompts
Determine which prompts achieve effective results and which prompts achieve
inaccurate results.
Adjust your prompts accordingly.
Novel and thought-provoking prompts can lead to innovative outcomes.
37
Prompt Techniques
• Zero-shot prompting:
A prompting technique where a user presents a task to an LLM without giving the
model further examples.
Here, the user expects the model to perform the task without a prior understanding, or
shot, of the task. Modern LLMs demonstrate remarkable zero-shot performance.
The larger the LLM, the more likely the zero-shot prompt will yield effective results.
Instruction tuning can improve zero-shot learning. You can adopt reinforcement
learning from human feedback (RLHF) to scale instruction tuning.
38
Prompt Techniques
39
Prompt Techniques
• Few-shot prompting:
A prompting technique where you give the model contextual information about the
requested tasks.
In this technique, you provide examples of both the task and the output you want.
Providing this context, or a few shots, in the prompt conditions the model to follow the
task guidance closely.
The label distribution of the input text specified by the demonstrations is important.
40
Prompt Techniques
41
Prompt Techniques
• Chain-of-thought prompting:
Chain-of-thought (CoT) prompting breaks down complex reasoning tasks through
intermediary reasoning steps.
You can use both zero-shot and few-shot prompting techniques with CoT prompts.
You can use the phrase "Think step by step" to invoke CoT reasoning from your
machine learning model.
42
Prompt Techniques
43
Prompt Techniques
44
Prompt Techniques
• Self-consistency:
A prompting technique that is similar to chain-of-thought prompting.
45
Prompt Techniques
46
Prompt Techniques
47
https://arxiv.org/abs/2203.11171
Prompt Techniques
• Tree of thoughts:
Another technique that builds on the CoT prompting technique.
CoT prompting samples thoughts sequentially, but ToT prompting follows a tree-
branching technique.
With the ToT technique, the LLM can learn in a nuanced way, considering multiple
paths instead of one sequential path.
48
Prompt Techniques
• Tree of thoughts:
49
https://arxiv.org/abs/2305.10601
Prompt Techniques
• Tree of thoughts:
50
https://arxiv.org/abs/2305.10601
Prompt Techniques
• Tree of thoughts:
For Game of 24, Generative Pre-trained Transformer 4 (GPT-4) achieved a 4 percent
success with CoT prompting. However, the model reached 74 percent success with a
ToT prompting method.
51
https://arxiv.org/abs/2305.10601
Prompt Techniques
• Tree of thoughts:
52
https://arxiv.org/abs/2305.10601
Prompt Techniques
• Advanced techniques such as RAG, ART, and ReAct will be covered in a separate lecture.
53
Prompt and Model Parameters
• Prompt Length: The length of the prompt, including the number of tokens or characters, can
impact the context provided to the model and influence the generated output.
• Prompt Format: The format of the prompt, such as the structure of the input text, the
inclusion of keywords or instructions, and the use of special tokens, can guide the model in
generating relevant responses.
54
Prompt and Model Parameters
55
Prompt and Model Parameters
• Top_p: adjusts determinism with "nucleus sampling." Lower values give exact answers, while
higher values give diverse responses. This value controls the diversity of the model's
responses.
• Top_k: is the number of the highest-probability vocabulary tokens to keep for top- k-filtering.
Similar to the Top_p parameter, Top_k defines the cutoff where the model no longer selects the
words.
56
Prompt and Model Parameters
• Repetition Penalty: Repetition penalty discourages the model from repeating tokens it has
already generated in the output sequence, which can help produce more diverse and coherent
responses.
• Length Penalty: Length penalty encourages the model to produce longer or shorter output
sequences by adjusting the likelihood of longer or shorter sequences during generation.
57
Prompt and Model Parameters
• Special Tokens: Special tokens, such as <BOS> (beginning of sequence) and <EOS> (end of
sequence), can be used to mark the start and end of prompts, instructions, or sections within
the input text.
• Context Window Size: The size of the context window, or the number of previous tokens
considered by the model during generation, can impact the coherence and relevance of the
generated output.
• Fine-Tuning Parameters: Parameters specific to fine-tuning, such as learning rate, batch size,
and number of training epochs, can affect how well the model adapts to specific prompts and
tasks.
58
Best Courses
• The primary content covered in this lecture draws heavily from the curriculum of the course
titled "Foundations of Prompt Engineering," accessible at the following link:
https://explore.skillbuilder.aws/learn/course/internal/view/elearning/17763/foundations-of-prom
pt-engineering
• I would like to suggest incorporating the course "ChatGPT Prompt Engineering for
Developers" into your considerations. You can find more information about this course at the
following link:
https://www.deeplearning.ai/short-courses/chatgpt-prompt-engineering-for-developers/
59
AIS421
NLP Applications
Spring 2024
60