Introduction to prompt engineering

ChatGPT Prompt Engineering for Developers

Fouad Trad

Machine Learning Engineer

What is prompt engineering?

Crafting prompts or instructions given to LLMs to get desired responses

A picture showing an engineer introducing the terminology of prompt engineering.

ChatGPT Prompt Engineering for Developers

Prompt engineering is like crafting a recipe

Image showing a chef crafting a meal.

ChatGPT Prompt Engineering for Developers

Why prompt engineering?

  A visual diagram showing that high quality prompts lead to high quality answers

ChatGPT Prompt Engineering for Developers

Why prompt engineering?

  A visual diagram showing how the quality of the answers is affected by the quality of the input prompts. High quality prompts lead to high quality answers, and low quality prompts lead to low quality answers.

ChatGPT Prompt Engineering for Developers

Recap: OpenAI API

  • Allows interaction with OpenAI models
  • Already set up in this course
  • Access to Chat Completions endpoint

chatgpt_logo_white.png

ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for the user role.

ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for the two roles: user and assistant.

ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for each of the three roles: user, system, and assistant

ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages.

  • System message: guides model behavior
ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages, and a communication arrow between the user and the assistant to send a prompt.

  • System message: guides model behavior
  • User message: prompt from the user
ChatGPT Prompt Engineering for Developers

Recap: message roles

Every message has one of three roles

Image showing an icon for each of the three roles, with a communication arrow between the system and the assistant to send system's messages, a communication arrow between the user and the assistant to send a prompt, and a communication arrow between the assistant and the user to send the response.

  • System message: guides model behavior
  • User message: prompt from the user
  • Assistant message: response to user prompt
ChatGPT Prompt Engineering for Developers

Recap: control parameters

Image showing a thermometer's icon with the possible values between 0 and 2, with 0 showing no randomness, and 2 showing highest randomness.

  • temperature: controls answer's randomness
ChatGPT Prompt Engineering for Developers

Recap: control parameters

Image showing a thermometer's icon with the possible values between 0 and 2, with 0 showing no randomness, and 2 showing highest randomness, along with a slider's icon that represents the max_tokens parameter where lower values on the slider would lead to shorter responses.

  • temperature: controls answer's randomness
  • max_tokens: controls response length
ChatGPT Prompt Engineering for Developers

Recap: communicating with the OpenAI API

prompt = "What is prompt engineering?"

client = OpenAI(api_key="api_key")
response = client.chat.completions.create(
model = "gpt-3.5-turbo",
messages = [{"role": "user", "content": prompt}],
temperature = 0 )
print(response.choices[0].message.content)
Prompt engineering refers to the process of designing and refining prompts or 
instructions given to a language model like ChatGPT in order to elicit desired 
responses or behaviors. It involves formulating specific guidelines or hints to 
guide the model's output towards a desired outcome.
ChatGPT Prompt Engineering for Developers

Creating get_response() function

def get_response(prompt):

response = client.chat.completions.create( model = "gpt-3.5-turbo", messages = [{"role": "user", "content": prompt}], temperature = 0 )
return response.choices[0].message.content

Usage

response = get_response("What is prompt engineering?")
print(response)
Prompt engineering refers to the process of designing and refining prompts or instructions given to a language model 
like ChatGPT in order to elicit desired responses or behaviors. It involves formulating specific guidelines or hints 
to guide the model's output towards a desired outcome.
ChatGPT Prompt Engineering for Developers

Prompt improvement

prompt = "What is prompt engineering? Explain it in terms that can be understood 
by a 5-year-old"
response = get_response(prompt)
print(response)
Imagine you have a very smart friend who can understand and answer lots of 
questions. But sometimes, they might not understand exactly what you want or give 
the wrong answer. So, prompt engineering is like giving your friend really clear 
instructions or hints to help them give you the best answer possible.
ChatGPT Prompt Engineering for Developers

Let's practice!

ChatGPT Prompt Engineering for Developers

Preparing Video For Download...