0% found this document useful (0 votes)
44 views

Chapter 4

Thank you for the examples. Role-playing prompts can help guide a chatbot's responses to better match different conversational contexts. Specifying traits like expertise and personality for the role can make the interactions feel more natural and engaging. Prompt engineering is an important technique for developing effective chatbots.

Uploaded by

Mostafa Hagag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views

Chapter 4

Thank you for the examples. Role-playing prompts can help guide a chatbot's responses to better match different conversational contexts. Specifying traits like expertise and personality for the role can make the interactions feel more natural and engaging. Prompt engineering is an important technique for developing effective chatbots.

Uploaded by

Mostafa Hagag
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 45

Prompt engineering

for chatbot
development
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S

Fouad Trad
Machine Learning Engineer
The need for prompt engineering for chatbots
Difficult to predict user questions
Challenge to guarantee effective responses

Prompt engineering guides chatbot behavior

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chatbot prompt engineering with OpenAI API

Each message has a designated role

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chatbot prompt engineering with OpenAI API

Each message has a designated role

Focus has been on user prompts

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chatbot prompt engineering with OpenAI API

Each message has a designated role

Focus has been on user prompts

System prompts guide chatbot's behavior

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chat completions endpoint for chatbot development
Sends series of messages to model as a list

response = client.chat.completions.create(
model="gpt-3.5-turbo",
```python
messages=[{"role": "system",
"content": "You are an expert data scientist that explains complex concepts in simple terms"},
{"role": "user",
"content": "What is prompt engineering?"}]
)
print(response.choices[0].message.content)

Imagine you're giving instructions to a computer program, like teaching a robot to make a sandwich.
Prompt engineering is all about crafting those instructions, or "prompts," in a way that helps the
computer understand and perform the task better.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Changing get_response() for chatbot
Sending one prompt Sending two prompts
def get_response(prompt): def get_response(system_prompt, user_prompt):
messages = [ messages = [
{"role": "user", "content": prompt} {"role": "system", "content": system_prompt},
] {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( response = client.chat.completions.create(
model="gpt-3.5-turbo", model="gpt-3.5-turbo",
messages=messages, messages=messages,
temperature=0, temperature=0,
) )
return response.choices[0].message.content return response.choices[0].message.content

prompt = "<PROMPT>" system_prompt = "<SYSTEM_PROMPT>"


print(get_response(prompt)) user_prompt = "<USER_PROMPT>"
print(get_response(system_prompt, user_prompt))

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: define purpose
system_prompt = "You are a chatbot that answers financial questions."

user_prompt = "Who are you?"

print(get_response(system_prompt, user_prompt))

I'm a financial chatbot that answers financial questions. How can I help you?

Allow chatbot to offer domain-accurate assistance

Not defining purpose might lead to contextually irrelevant answers

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: response guidelines
Specify audience, tone, length, structure

system_prompt = """You are a chatbot that answers financial questions.


Your answers should be precise, formal and objective"""

user_prompt = "What do you think about cryptocurrencies?"

print(get_response(system_prompt, user_prompt))

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: response guidelines
Cryptocurrencies are digital or virtual currencies that use cryptography for security and operate on
decentralized networks based on blockchain technology.
[...]

Advantages of cryptocurrencies include:


- Decentralization: [...]
- Security:[...]
- Global Accessibility: [...]

However, there are also notable concerns:


- Volatility: [...]
- Regulatory Uncertainty: [...]
- Lack of Consumer Protection: [...]

In summary, cryptocurrencies have the potential to offer various benefits, but their adoption and impact
on the financial landscape are still evolving [...]

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: behavior guidance
Conditional prompts to respond to questions

system_prompt = """You are a chatbot that answers financial questions.


Your answers should be precise, formal and objective.

"""

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: behavior guidance
Conditional prompts to respond to questions

system_prompt = """You are a chatbot that answers financial questions.


Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.

"""

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System message: behavior guidance
Conditional prompts to respond to questions

system_prompt = """You are a chatbot that answers financial questions.


Your answers should be precise, formal and objective.
If the question you receive is within the financial field, answer it to the best of your knowledge.
Otherwise, answer with 'Sorry, I only know about finance.'
"""

user_prompt = "How's the weather today?"

print(get_response(system_prompt, user_prompt))

Sorry, I only know about finance.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Let's practice!
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S
Role-playing
prompts for
chatbots
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S

Fouad Trad
Machine Learning Engineer
Role-playing prompts
Tell chatbot to play a specific role
Chatbot -> actor in a play

Chatbot adjusts to match role

Tailored language and content to fit the


persona

More effective interactions

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing example
Question: Could you give me technical specifications of Product X that your company offers?

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing example
Question: Could you give me technical specifications of Product X that your company offers?

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing example
Question: Could you give me technical specifications of Product X that your company offers?

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Customer support agent
Provides guidance
Directs customers to website

Offers assistance

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Product manager
Highlights strategic benefits
Focuses on product alignment

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Sales engineer
Delves deep into technical specifics
Processor

Features

Security

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing prompts
Tell model to act as as specific role

system_prompt = "Act as an expert financial analyst."


user_prompt = "Offer insights into retirement planning for individuals approaching
retirement age."

print(get_response(system_response, user_response))

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Expert financial analyst
Proper retirement planning is crucial to ensure a comfortable and financially stable retirement. Here are some key
considerations:

- Evaluate Your Financial Position: Begin by assessing your current financial situation [...]
- Set Retirement Goals: Determine your retirement goals and lifestyle preferences [...]
- Estimate Retirement Expenses: Project your retirement expenses by categorizing them into essential [...]
[...]

Remember, retirement planning is a complex process, and everyone's situation is unique.


It's advisable to work with a certified financial planner who can provide personalized advice based on
your individual needs and goals.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


More effective role-playing
Specific requirements within the role
Incorporate traits like personality and expertise

system_prompt = "Act as a seasoned technology journalist covering the latest trends


in the tech industry. You're known for your thorough research and insightful analysis."

user_prompt = "What is the impact of artificial intelligence on job markets?"

print(get_response(system_response, user_response))

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Technology journalist
Title: "Navigating the New Normal: How Artificial Intelligence is Reshaping Job Markets"

As the technological landscape continues to evolve at an unprecedented pace, one of the most
significant transformations we're witnessing is the integration of Artificial Intelligence
(AI) into various industries [...].

While AI's potential to streamline processes [...]. Let's delve into the multifaceted impact
of AI on employment...

1. Automation and Job Displacement: [...]


2. Augmentation and Enhanced Creativity: [...]
[...]

In conclusion, the impact of AI on job markets is undeniable, reshaping traditional roles


and paving the way for novel opportunities [...]

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing with requirements
We can specify response guidelines and behavior guidance in role-playing prompts

system_prompt = "Act as a seasoned technology journalist covering the latest trends


in the tech industry. You're known for your in-depth research and insightful analysis."

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Role-playing with requirements
We can specify response guidelines and behavior guidance in role-playing prompts.

system_prompt = "Act as a seasoned technology journalist covering the latest trends


in the tech industry. You're known for your in-depth research and insightful analysis.
If the question is related to tech, you answer to the best of your knowledge.
Otherwise, you just respond with 'I am trained to only discuss technology topics."

user_prompt = "Which American literature books do you recommend?"

print(get_response(system_response, user_response))

I am trained to only discuss technology topics.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Let's practice!
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S
Incorporating
external context in
chatbot
conversations
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S

Fouad Trad
Machine Learning Engineer
The need for external context
Pre-trained language models recognize
information they are trained on

Need to provide more context

More accuracy and effectiveness

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Lack of information in LLMs
Knowledge cut-off

system_prompt = "Act as a financial expert that knows about the latest trends."

user_prompt = "What are the top financial trends in 2023?"

print(get_response(system_prompt, user_prompt))

I apologize for any inconvenience, but as of my last knowledge update in


September 2021, I don't have information about financial trends in 2023.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Lack of information in LLMs
Requested non-public information

system_prompt = "Act as a study buddy that helps me with my studies to succeed


in exams."

user_prompt = "What is the name of my favorite instructor?"

print(get_response(system_prompt, user_prompt))

I don't have personal information about you, including the name of your
favorite instructor.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


How to give extra information?
Sample previous conversations

System's prompt

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Sample conversations
Guide model to answer specific questions
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[{"role": "system",
"content": "You are a customer service chatbot that responds to user queries in a gentle way"},
{"role": "user",
"content": "What services do you offer?"},
{"role": "assistant",
"content": "We provide services for web application development, mobile app development, and
custom software solutions."},
{"role": "user",
"content": "How many services do you have?"}])
print(response.choices[0].message.content)

We have 3 services including web application development, mobile app development, and custom software solutions.

Disadvantage: might need a lot of samples

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


System prompt
Includes required context for chatbot answers

services = "ABC Tech Solutions, a leading IT company, offers a range of services:


application development, mobile app development, and custom software solutions."
system_prompt = f"""You are a customer service chatbot that responds to user
queries in a gentle way. Some information about our services are delimited by
triple backticks.
```{services}```"""
user_prompt = "How many services do you offer?"
print(get_response(system_prompt, user_prompt))

We have 3 services including web application development, mobile app development,


and custom software solutions.

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Final note
Previous methods work well for small
contexts

Larger contexts require more sophisticated


techniques

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Let's practice!
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S
Congratulations
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S

Fouad Trad
Machine Learning Engineer
Chapter 1
CHAPTER 1
Key principles of prompt engineering

Delimited prompts

Conditional prompts

Structured outputs

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chapter 2
CHAPTER 1 CHAPTER 2
Key principles of prompt engineering Few-shot prompting

Delimited prompts Multi-step prompting

Structured outputs Chain-of-thought and self-consistency

Conditional prompts Iterative prompt engineering

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chapter 3
CHAPTER 1 CHAPTER 2
Key principles of prompt engineering Few-shot prompting

Delimited prompts Multi-step prompting

Structured outputs Chain-of-thought and self-consistency

Conditional prompts Iterative prompt engineering

CHAPTER 3
Text summarization and expansion

Text transformation

Text analysis

Code generation and explanation

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Chapter 4
CHAPTER 1 CHAPTER 2
Key principles of prompt engineering Few-shot prompting

Delimited prompts Multi-step prompting

Structured outputs Chain-of-thought and self-consistency

Conditional prompts Iterative prompt engineering

CHAPTER 3 CHAPTER 4
Text summarization and expansion System prompt engineering for chatbots

Text transformation Role-playing prompts

Text analysis External context

Code generation and explanation

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Final note
LLMs have limitations
Always make sure to review the answers

CHATGPT PROMPT ENGINEERING FOR DEVELOPERS


Keep it up!
C H AT G P T P R O M P T E N G I N E E R I N G F O R D E V E L O P E R S

You might also like