Prompt engineering for chatbot development

ChatGPT Prompt Engineering for Developers

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

Image showing a chatbot on a mobile phone.

ChatGPT Prompt Engineering for Developers

Chatbot prompt engineering with OpenAI API

Image showing the three roles of a message and the communication between them.

  • Each message has a designated role
ChatGPT Prompt Engineering for Developers

Chatbot prompt engineering with OpenAI API

Image showing the three roles of a message and the communication between them, with a highlight around the interaction between the user and the assistant.

  • Each message has a designated role
  • Focus has been on user prompts
ChatGPT Prompt Engineering for Developers

Chatbot prompt engineering with OpenAI API

Image showing the three roles of a message and the communication between them, with a highlight around the interaction between the system and the assistant.

  • 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",
  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
def get_response(prompt):
    messages = [
      {"role": "user", "content": prompt}
    ]
    response = client.chat.completions.create(
        model="gpt-3.5-turbo",
        messages=messages,
        temperature=0,
    )
    return response.choices[0].message.content

prompt = "<PROMPT>" print(get_response(prompt))
  • Sending two prompts
def get_response(system_prompt, user_prompt):

messages = [ {"role": "system", "content": system_prompt}, {"role": "user", "content": user_prompt}]
response = client.chat.completions.create( model="gpt-3.5-turbo", messages=messages, temperature=0, ) return response.choices[0].message.content
system_prompt = "<SYSTEM_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!

ChatGPT Prompt Engineering for Developers

Preparing Video For Download...