0% found this document useful (0 votes)
5 views56 pages

Prompt Engineering

Prompt engineering

Uploaded by

rubab.anam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views56 pages

Prompt Engineering

Prompt engineering

Uploaded by

rubab.anam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

Prompt Engineering

Submitted By

Rana Hasan Mehmood


23L-8004
Muhammad Haris Shahid
23L-8036

MS Data Science
Natural Language Processing
OUTLINE

1. Introduction

2. Use Cases

3. Prompt Types

4. Parameters

5. Advanced Prompt Engineering Techniques

6. Challenges & Limitations

7. Conclusion
2
What is PROMPT Engineering?

• Prompts involve instructions and context passed to a language model to


achieve a desired task

• Prompt engineering is the practice of developing and optimizing prompts to


efficiently use language models (LMs) for a variety of applications.

Prompt engineering is the process of structuring an instruction


that can be interpreted and understood by a generative AI
model. A prompt is natural language text describing the task that an
AI should perform.

3
WHY PROMPT ENGINEERING?

• Important for research, discoveries, and advancement

• Helps to test and evaluate the limitations of LLMs

• Enables all kinds of innovative applications on top of LLMs

• Don’t require to change the architecture of LLMs for fine tuning.

• Active area of research.

4
COMPONENTS OF PROMPT

Instructions Context Input Data Output Indicator

• Verbs • Describe in detail • Cues • Diverse datasets


• Direction • Facilitating Response • Some information with inherent
complexities.

Classify the text in My favorite color is black, How long should I walk Write the code of
neutral, positive or suggest me the color of a to burn my calories, Fibonacci series ----
negative tone. car to draw. given that I am 25 y/o in C++.
and 75 kg in weight?

5
6
THINGS TO KEEP IN MIND !!!

• When prompting a language model you should keep in mind a few settings

• You can get very different results with prompts when using different settings

• One important setting is controlling how deterministic the model is when generating completion for prompts

• Temperature and top_p are two important parameters to keep in mind

• Generally, keep temperature low if you are looking for exact answers (less creative/ not interesting responses)
or ……..…keep them high if you are looking for more diverse responses (interesting responses)
7
Temperature:

• Definition: A hyperparameter that controls the "randomness" of the generated

text.

• Low temperature: Generates more predictable and coherent text, but may be

less creative.

• High temperature: Generates more varied and surprising text, but may be less

coherent.

8
Transformer:

9
TEMPERATURE
• For the demonstration purpose, here is the attached video:

10
Top P

• Definition: A hyperparameter that controls the diversity of the generated text. It

specifies the cumulative probability mass of the tokens to consider during generation.

• Low top p: Generates a few highly probable tokens, resulting in more repetitive text.

This is because the model is only considering a small number of the most probable

tokens, which are more likely to be repeated.

• High top p: Generates a wider range of tokens, resulting in more diverse text. This is

because the model is considering a larger number of the most probable tokens, which

are more likely to be different from each other.


11
Top P

12
GENERAL PROMPT USE CASES

• Text Summarization

• Question/ Answers

• Code Generation

• Translation

• Sentiment Analysis

• Image/ Text Generation

Note: Few of these are covered in the upcoming slides.


13
TYPES OF PROMPT

• Explicit Prompt

• Conversational Prompt

• Instructional Prompt

• Context Based Prompt

• Bais Mitigating Prompt

• Text Classification Prompt

• Open Ended Prompt

• Code Generation Prompt

• Question Answering Prompt 14


Prompt EXPLCIT PROMPT

"Write a short story about a young girl who discovers a


magical key that unlocks a hidden door to another
world."

Once upon a time, in a quaint little town nestled


Response

between rolling hills and whispering forests, there


lived a curious young girl named Lily. Lily had always
been fascinated by stories of magic and adventure,
often spending her days lost in the pages of dusty old
books found in her grandfather's attic.
One sunny afternoon, while exploring the attic yet
again, Lily stumbled upon a small, intricately carved
key tucked away in a forgotten corner. The key seemed
to gleam with an otherworldly light, and as Lily held
it in her hand, she felt a strange energy coursing
through her veins…………… 15
Promp OPEN ENDED PROMPT

"Tell me about the impact of AI on society."


t
Response

16
Prompt CONVERSATIONAL PROMPT

"Hey, ChatGPT! Can you tell me a funny joke about


cats?"
Response

Why did the cat sit on the computer?

Because it wanted to keep an eye on the mouse!

17
INSTRUCTIONAL PROMPT

"Write a detailed blog post discussing the benefits and drawbacks


Prompt

of renewable energy sources. The outline should be structured as


follows:
Introduction
Body
Response

Conclusion"

18
CONTEXT BASED PROMPT

Antibiotics are a type of medication used to treat bacterial


Prompt

infections. They work by either killing the bacteria or


preventing them from reproducing, allowing the body's immune
system to fight off the infection. Antibiotics are usually
taken orally in the form of capsules, or liquid solutions,
or sometimes administered intravenously. They are not
effective against viral infections, and using them
inappropriately can lead to antibiotic resistance.
Explain the above in one sentence:
Response

Antibiotics are medications used to treat bacterial


infections by either killing the bacteria or
stopping them from reproducing, but they are not
effective against viruses and overuse can lead to
antibiotic resistance.
19
BAIS MITIGATING PROMPT

"Please generate a response that presents a balanced and


Prompt

objective view of the following topic:


caste-based reservations in India. Consider providing
multiple perspectives and avoid favoring any particular
group, ideology, or opinion. Focus on presenting factual
information, supported by reliable sources, and strive for
inclusivity and fairness in your response.”
Response

Why Caste-based reservations in India are a complex


and deeply entrenched aspect of the country's
social fabric. It's crucial to acknowledge multiple
perspectives on this issue to understand its
implications fully……
20
Prompt TEXT CLASSIFICATION PROMPT

Classify the text into neutral, negative or


positive.
Text: I think the food was okay.
Response

21
Prompt
CODE GENERATION PROMPT

"Table
departments, columns =
[DepartmentId,DepartmentName]
Table students, columns = [DepartmentId,
StudentId,StudentName]
Create a MySQL query for all students in the
Computer Science Department
Response

SELECT StudentId, StudentName FROM students


WHERE DepartmentId IN (SELECT DepartmentId FROM
departments WHERE DepartmentName ='Computer
Science');

22
QUESTION ANSWERING PROMPT

Answer the question based on the context below. Keep the


answer short and concise. Respond "Unsure about answer"
Prompt

if not sure about the answer.


Context: Teplizumab traces its roots to a New Jersey drug
company called Ortho Pharmaceutical. There, scientists
generated an early version of the antibody, dubbed
OKT3.Originally sourced from mice, the molecule was able
to bind to the surface of T cells and limit their cell-,
killing potential. In 1986, it was approved to help
prevent organ rejection after kidney transplants, making
it the first therapeutic antibody allowed for human use.
Question: What was OKT3 originally sourced from?
Response

Answer: Mice

Context obtained from: https://www.nature.com/articles/d41586-023-00400-x 23


ADVANCED TECHNIQUES FOR PROMPT
ENGINEERING
• Shot Prompting • Prompt Priming

⮚ Zero Shot Prompting • Prompt Chunking

⮚ One Shot Prompting • Chain-of-Thought (CoT) Prompting

⮚ Few Shot Prompt • Retrieval Augmented Generation (RAG)

• Self-Consistency • ReAct

• Active prompt • Directional Stimulus Prompting

• Prompt Chaining • Program-aided Language Model (PAL)

27
Zero Shot Prompting

• Zero-shot prompting means that the prompt used to interact with the model won't

contain examples or demonstrations. The zero-shot prompt directly instructs the model

to perform a task without any additional examples to steer it.


Prompt

Classify the text into neutral,


negative or positive.
Text: I think the food was okay.
Respons
e

28
One Shot Prompting

• You provide one example along with your prompt. This helps the AI understand the context

or format you’re expecting.

“A Foundation Model in AI refers to a model like GPT-3,


Prompt

which is trained on a large dataset and can be adapted to various tasks.


Explain what BERT is in this context.”
Mistral Response:

BERT (Bidirectional Encoder Representations from Transformers)


Response

is a type of pre-trained deep learning model that uses the transformer architecture.
It was developed by Google and is considered to be one of the most
29
powerful language models in the field of natural language processing
(NLP)………

• Note we had to define the context, otherwise BERT might be described as Building 29
Few-shot Prompts

• Few-shot prompting allows us to provide exemplars in prompts to steer the model

towards better performance

30
Limitations of in-shot prompting

The Limitation of few shot prompting are as follows


• Arithmetic
• Common Sense
• Symbolic Reasoning

31
Prompt Chaining

• To improve the reliability and performance of LLMs, one of the important prompt engineering

techniques is to break tasks into its subtasks.

• Once those subtasks have been identified, the LLM is prompted with a subtask and then its

response is used as input to another prompt.

• This is what's referred to as prompt chaining, where a task is split into subtasks with the idea to

create a chain of prompt operations.

• Prompt chaining is useful to accomplish complex tasks which an LLM might struggle to address if

prompted with a very detailed prompt.

• In prompt chaining, chain prompts perform transformations or additional processes on the

generated responses before reaching a final desired state. 32


Prompt Chaining Example:
Prompt 1:
• Please read through this document: <document_content>
• Based on the question "<question>", extract any relevant quotes or
passages from the document that can help answer the question.

Prompt 2:
• Here is the original question again: <question>
• Here are the relevant quotes and passages from the document:
<extracted_quotes_from_prompt1>
• Please draft a short 1-2 paragraph answer to the question "<question>"
that synthesizes and incorporates the relevant quotes/passages you
extracted.
33
Prompt Chaining Example:
Prompt 3:
• Here is the draft answer you generated: <draft_answer_from_prompt2>
• Please review the draft answer and validate that it accurately answers the
original question "<question>" solely based on the information and quotes
extracted from this document: <document_content>
• Make any revisions needed to correct any inaccuracies or unsupported
statements.
• This chains the process from document analysis through to answer
finalization, with opportunities for human validation at each stage.

34
Chain-of-Thought (CoT) Prompting

Prompting can be further improved by instructing the model to reason about


the task when responding
• This is very useful for tasks that requiring reasoning

• You can combine it with few-shot prompting to get better results

• You can also do zero-shot CoT where examples are not available

35
Source: https://arxiv.org/abs/2201.11903
STEP- BY- STEP
Chain-of-Thought (CoT) Prompting

36
https://arxiv.org/abs/2201.11903
Chain-of-Thought (CoT) Prompting

Input

5+2+3 37
Zero-Shot CoT

• Involves adding "Let's think step by step" to the original prompt

https://arxiv.org/abs/2205.11916 38
Self-Consistency

• Self-Consistency aims to improve on the naive greedy decoding used in chain-of-thought

prompting

• The idea is to sample multiple, diverse reasoning paths through few-shot CoT, and

use the generations to select the most consistent answer.

• This helps to boost the performance of CoT prompting on tasks involving arithmetic

and commonsense reasoning

https://arxiv.org/abs/2203.11171 39
Self-Consistency Example

When I was 6 my sister was half my age, so she was 3. Now I am 70, so she is 70/2 = 35. The
answer is 35. 40
Active prompt

Chain-of-thought (CoT) methods rely on a fixed set of human-annotated exemplars. The

problem with this is that the exemplars might not be the most effective examples for the

different tasks.

Here are the 3 key points summarizing the method:

• 1. Question Selection: Identify the most uncertain questions from a pool of task-specific

queries.

• 2. Characterize Uncertainty: Use metrics to measure the uncertainty of each question,

borrowing ideas from uncertainty-based active learning.

• 3. Active Prompting: Select the most uncertain questions for annotation to improve the

performance of Large Language Models (LLMs). 41


Active prompt

42
Tree of Thoughts (ToT)

• Tree of Thoughts (ToT), a framework that generalizes over chain-of-thought prompting and

encourages exploration over thoughts that serve as intermediate steps for general problem

solving with language models.

43
Directional Stimulus Prompting

• Li et al., (2023) proposes a new prompting technique to better guide the LLM in

generating the desired summary.

44
Directional Stimulus Prompting

Input

HINT

LLM

Output 45
RETREIVAL AUGMENTED GENERATION (RAG)

• Enhanced Context Retrieval: RAG leverages large-scale knowledge sources such as text corpora or

databases to retrieve relevant context for generating responses. This allows for a deeper understanding

of the input query and ensures that the generated responses are contextually rich and accurate.

• Dynamic Response Generation: RAG combines retrieval and generation models to dynamically

generate responses based on the retrieved context. By integrating retrieval-based techniques with

generation models like language models or transformers, RAG produces responses that are not only

contextually relevant but also fluent and coherent.

• Not a Direct Prompting Technique: While RAG involves using retrieved information to guide the

generation of text, it's important to note that it's not a traditional prompting technique where specific

cues or prompts are provided to elicit certain responses. Instead, RAG utilizes retrieved context to

inform the generation process, resulting in more informed and contextually appropriate responses.
46
RAG

Question

Retriever Gen ‘R

KB Answer 47
ReAct

• ReAct is a framework where LLMs are used to generate both reasoning traces and

task-specific actions in an interleaved manner

• Generating reasoning traces allow the model to induce, track, and update action
plans, and even handle exceptions

• The action step allows to interface with and gather information from external
sources such as knowledge bases or environments.

• ReAct allows LLMs to interact with external tools to retrieve additional information that

leads to more reliable and factual responses


48
ReAct
Prompt

ReAct

PUBLIC PRIVATE

KB KB

Thought
Action
Observation
49
50
ReAct

https://arxiv.org/abs/2210.03629 51
Program-aided Language Model (PAL)

• Chain-of-thought prompting is a good example of how to steer models to perform better

at complex reasoning tasks

• However, sometimes CoT is not enough as it depends only on the generated text
from the model

• Program-aided language models (PAL) uses an LLM to read problems and generate

programs as the intermediate reasoning steps

• It offloads the solution step to a runtime such as Python interpreter

52
Program-aided Language Model (PAL)

53
Other Prompting techniques:

• Prompt Chunking

• Prompt Priming

• General Knowledge prompting

• Multimodel CoT

• Reflexion

54
Challenges
• Prompt Sensitivity: Slight changes in wording or structure can drastically alter the output, making it difficult

to achieve consistent and reliable results. This sensitivity demands careful crafting and iteration.

• Understanding Model Capabilities: Effectively utilizing prompts requires a deep understanding of the

underlying language model's capabilities and limitations. Knowing what the model is good at and where it

struggles is crucial for crafting effective prompts.

• Black Box Nature: The reasoning behind a language model's output given a prompt can be opaque.

Understanding why a model responds in a certain way is often difficult, hindering the debugging and

refinement process.

• Bias and Safety: Prompts can inadvertently introduce biases or elicit unsafe responses from the model.

Carefully considering potential biases and safety concerns is essential when designing prompts.

• Prompt Length and Complexity: Balancing the need for detail with conciseness can be difficult. Overly

complex prompts might confuse the model, while overly simplistic ones might not provide enough 55

information.
Limitations

• Creativity and Originality: While prompts can guide the model, they might limit its creativity and originality. The model's

responses can become formulaic if prompts are too restrictive.

• Dependency on Human Input: Some prompt engines rely on human input or supervision to generate prompts, such as

providing example prompts or specifying constraints. This dependency can introduce biases or subjectivity into the prompt

generation process.

• Domain-Specific Knowledge: Language models may lack specific knowledge in certain domains. Prompts alone may not

be sufficient to overcome this limitation, requiring additional information or fine-tuning.

• Ethical Considerations: The use of prompts raises ethical concerns, such as potential manipulation or misuse of the

technology. It's crucial to use prompt engineering responsibly and be aware of the potential consequences.
56
Limitations

Few more limitations may include:

• Overfitting to Specific Tasks

• Limited Flexibility

• Scalability

• Difficulty in Balancing Constraint

• Difficulty in Handling Ambiguity

57
Conclusion

• Unlocking Potential: Prompt engineering is essential for harnessing the full capabilities of LLMs across

various tasks, such as text generation, translation, and code development.

• Overcoming Challenges: While challenges like prompt sensitivity and bias exist, ongoing research is

developing advanced techniques (e.g., CoT, RAG) to address these issues and improve reliability.

• Driving Innovation: Advancements in prompt engineering are paving the way for novel NLP applications,

pushing the boundaries of what's possible with LLMs.

• Essential Skill: Mastering prompt engineering is crucial for researchers, developers, and users alike to

effectively utilize and interact with LLMs across diverse domains.


58
REFERENCES

• https://github.com/dair-ai/Prompt-Engineering-Guide/tree/main/lecture

• https://www.promptingguide.ai/

59

You might also like