DEV Community

Cover image for How to Speak to AI: A Beginner's Guide to Prompt Engineering Part 1
Pratik Shende
Pratik Shende

Posted on

How to Speak to AI: A Beginner's Guide to Prompt Engineering Part 1

The first thing to keep in mind is that these models are not aware of what you are thinking. To get clear results, you need to explain exactly what you are looking for.

Basically, don’t let the model guess what kind of output you want. If you think the output is too simple, ask by adding more details, and it will provide you with a more descriptive result. If you want the output in a specific format, explain the format, and you will receive the result in that format.

A rule of thumb, add more details, get better result.

There are many tactics explained in OpenAI docs, which we are going to learn here, which will make us to become a better speaker, so that we can talk well with AI.

How to Speak to AI: A Beginner's Guide to Prompt Engineering

*Let's learn some tactics *

1. Details : Add details in your query, to get relevant answers.

In order to get the most relevant answer, make sure to include all the necessary details or provide context in your query. Otherwise, the model will start making assumptions, and you may end up receiving results based on its understanding.

Let's see some example.

  • Worst : How do I add numbers in Excel?
  • Better : How do I add up a row of dollar amounts in Excel? I want to do this automatically for a whole sheet of rows with all the totals ending up on the right in a column called "Total".

  • Worst : Write code to calculate the Fibonacci sequence.
  • Better : Write a TypeScript function to efficiently calculate the Fibonacci sequence. Comment the code liberally to explain what each piece does and why it's written that way.

  • Worst : Summarize the meeting notes.
  • Better : Summarize the meeting notes in a single paragraph. Then write a markdown list of the speakers and each of their key points. Finally, list the next steps or action items suggested by the speakers, if any.

From the examples above, you should get an idea of how to write a detailed prompt, which should include every specific detail. The more details you provide, the more relevant the results will be.

2. Persona : Ask the model to act as a person

You can ask the model to act as a specific person, which will help it craft responses as if they are coming from that person.

Let's see how the persona pattern works. First, set the scene for the model by assigning it a role and providing context, so the model behaves according to the role you've given.

You can also specify the persona at the start of the prompt and outline how you want the responses to be crafted for different topics.

You will understand this pattern with the help of below examples:

Example 1: Act as a Software Engineer Mentor

Prompt:

"You are a senior software engineer with 30+ years of experience. A junior developer is asking for advice on how to structure their code for better maintainability. Explain in a supportive and mentoring tone."

Response:

"To improve maintainability, focus on writing clean, modular code. Break down your program into smaller, reusable functions or methods that do one thing and do it well. Use clear, descriptive names for variables and functions, and add comments where necessary, but don’t overdo it. Regularly refactor your code to eliminate redundancy. And remember, keep your codebase organized and follow consistent styling conventions—it’ll save you and your team time in the long run."


Example 2: Act as a School Teacher

Prompt:

"You are a patient and kind elementary school teacher. A student is confused about how fractions work and asks you to explain it in a simple and friendly way using real-life examples."

Response:

"Alright, let’s think about a pizza! Imagine you have a pizza that’s cut into 4 equal slices. If you eat 1 slice, you’ve eaten 1/4 of the pizza. That’s what a fraction is—part of a whole! The top number, called the numerator, shows how many parts you have. The bottom number, called the denominator, tells you how many parts the whole thing is divided into. So, if the pizza has 4 slices and you eat 1, we write that as 1/4. See? Fractions are just a way to show parts of things we use every day!"

3. Delimiters: Use delimiters in you prompt, to indicate different parts of your input

Basically, delimiters are specific characters or symbols that help clearly distinguish between different sections of a prompt. They assist in structuring the prompt, which leads to more accurate and controlled results from the model.

Delimiters are especially useful when prompts contain multiple components, such as commands, data, and instructions.

Common Delimiters:
(" "), (`), ([]), ({}), (<>)

Example 1:

Prompt:

"Translate this sentence into French: 'I love programming.'"

Response:

"J'aime la programmation."

Prompt:

"Translate this sentence into French: 'Good morning!'"

Response:

"Bonjour!"

Example 2:

Prompt:

Given the following user profile:
Name: [John Doe]
Age: [28]
Location: [New York]
Recommend a personalized message based on their profile.

Response:

"Hi John! We hope you're doing well in New York. At 28, it's a great time to explore new opportunities and make the most of your experiences. If there's anything we can assist you with, don't hesitate to reach out!"

4. Steps: Specify the needed steps to complete the task

Some tasks need to be completed in a specific sequence of steps. If you provide these steps to the model, it will be easier for the model to generate the output according to the sequence you've outlined.

Example:

Prompt 1.1:

Step 1: The user will provide the sentence, in triple quotes,
you have to summarise the sentence, which says "summary:".
Step 2: Take the summary from the Step 1, and translate it to chinese language

Prompt 1.2:

"""Always have growth mindset"""

Response:

Step 1: Summary: Emphasize the importance of maintaining a mindset focused on continuous learning and improvement.
Step 2: 保持专注于持续学习和提升的思维模式重要性。

5. Few-Shots : Provide examples
Normally, AI models require vast amounts of data to learn something new. For example, if you want your model to recognize cats, you might need to train it on thousands of cat images. However, with the "few-shot" approach, you only need to provide 5 or 10 such images for the model to learn how to recognize cats.

In prompt engineering, "few-shot" refers to giving only a few examples in the prompt so the model understands what you want.

For instance, if you want the model to finish a story in a specific way, you should first provide it with some examples.

Examples like,
Prompt:

"Once upon a time, there was a dragon who used to live with humans family."
"In a faraway land, a princess raised a dog, who become her protector."

Response:

Here are some similar results based on the examples you provided:
"Long ago, in a mystical forest, a giant bird lived peacefully with a group of villagers."
"In an enchanted kingdom, a young prince befriended a lion, who later became his loyal guardian."
These follow the structure of magical creatures or animals forming bonds with humans in distant lands. Let me know if you need further adjustments!

So basically, "few-shot" learning means providing a handful of examples in the prompt, allowing the model to quickly learn from them and perform a similar task.

5. Length: Provide the desired length of a response
You can ask the model to generate a response based on a specified length, whether in terms of words, paragraphs, sentences, or bullet points. However, the model may not generate a precise number of words, but it works better when generating paragraphs or sentences of a specific length.

For example:
Prompt 1:

Write the details for the text delimited by triple quotes in about 50 words.
"""How many starts are there in milky way galaxy?"""

Response:

The Milky Way galaxy is home to an estimated 100 to 400 billion stars. This wide range is due to the difficulty in measuring the total number of stars, as the galaxy is vast and some regions are obscured by dust and gas. Astronomers continue refining this estimate.

Prompt 2:

Write the details for the text delimited by triple quotes in 2 paragraphs of 20 words each.
"""How many starts are there in milky way galaxy?"""

Response:

The Milky Way galaxy is estimated to have between 100 and 400 billion stars. This count is uncertain due to obscuration.
Many stars are hidden by interstellar dust and gas, making detection challenging. Advanced technology helps astronomers refine these estimates over time.

Prompt 3:

Write the details for the text delimited by triple quotes in 2 bullet points.
"""How many starts are there in milky way galaxy?"""

Response:

  • The Milky Way galaxy is estimated to contain between 100 and 400 billion stars, though the exact number remains uncertain.
  • Many stars are obscured by interstellar dust and gas, making detection challenging and leading to variations in estimates.

If you are reading this, it means you have successfully completed Part 1 of this article. Congratulations! You’ve now grasped the basics of prompt engineering.

In Part 2, we will explore advanced strategies, such as providing references in your prompts, encouraging the model to think critically, and breaking tasks into smaller, manageable subtasks. Stay tuned!

You can read OpenAI doc here

Top comments (0)