DEV Community

Cover image for WTH is Prompt Engineering?
Pavan Belagatti
Pavan Belagatti

Posted on

WTH is Prompt Engineering?

In the realm of artificial intelligence (AI) and natural language processing (NLP), prompt engineering has emerged as a pivotal discipline. It bridges the gap between human intent and machine understanding, ensuring that language models, like OpenAI's GPT series, can effectively interpret and respond to user queries. This article delves into the intricacies of prompt engineering, its basics, and the techniques employed by experts in the field.

What is Prompt Engineering?

At its core, prompt engineering is the practice of crafting effective prompts or inputs to guide AI language models towards desired outputs. It's akin to asking the right questions to get the right answers. Given the vast knowledge and capabilities of modern language models, the way a question or command is phrased can significantly influence the response.

prompt engineering image

Prompt engineering is crucial in the realm of machine learning, particularly with models based on transformers like OpenAI's GPT series. It helps in harnessing the full potential of these models by carefully crafting prompts that guide the model to produce the desired output. Without effective prompt engineering, even the most sophisticated models might not yield useful or accurate results.

Essentially, prompt engineering bridges the gap between raw computational capability and specific, meaningful outcomes. It becomes an art of communication, ensuring that the user's intent is effectively conveyed to the model, and in turn, ensuring that the model's response aligns with the user's expectations or objectives. Thus, mastering prompt engineering can significantly elevate the utility, accuracy, and efficiency of interactions with advanced machine learning models.

Techniques in Prompt Engineering

These techniques are evolving day by day but we can mention some important techniques.

  • Iterative Refinement: This involves posing a question, observing the model's response, and then tweaking the prompt based on the output. It's a continuous feedback loop until the desired answer is achieved.

  • Explicitness: Being clear and specific in the prompt can help in narrowing down the model's response. For instance, instead of asking "Tell me about apples," one might ask, "What are the nutritional benefits of eating apples?"

  • Contextual Framing: Providing a context or scenario can guide the model towards a more relevant answer. For example, "As a historian, explain the significance of the Renaissance."

  • Prompt Templates: Creating a set of standardized prompts for recurrent tasks can ensure consistency in responses.

Challenges in Prompt Engineering

Below are some notable challenges associated with prompt engineering.

  • Overfitting: There's a risk of crafting prompts that are too narrow, leading the model to produce outputs that might not be applicable in a broader context.

  • Bias: If not careful, prompts can inadvertently introduce or reinforce biases present in the training data of the model.

  • Complexity: As models evolve, the number of potential outputs grows, making the task of prompt engineering increasingly intricate.

Prompt Engineering Basics

Letโ€™s walk you through the basics of prompt engineering, providing hands-on examples to help you get started.

  • Understand the Basics
    Before diving into prompt engineering, it's essential to understand the language model you're working with. Familiarize yourself with its capabilities, strengths, and limitations.

  • Start with a Basic Prompt
    Let's say you want to know about the Eiffel Tower. Start with a simple prompt:
    "Tell me about the Eiffel Tower."

  • Refine Your Prompt
    Based on the response, refine your prompt to be more specific. If the initial answer was too broad, narrow it down:

"What is the history behind the construction of the Eiffel Tower?"

  • Use Explicitness Being explicit can help in getting precise answers:

"List five facts about the Eiffel Tower's architecture."

  • Provide Context Sometimes, providing a context or scenario can guide the model towards a more relevant answer:

"As a tour guide, explain the significance of the Eiffel Tower to tourists."

  • Experiment with Different Phrasings The same question can be posed in various ways. Experiment to see which one yields the best result:

"How tall is the Eiffel Tower?"
"Can you provide the height of the Eiffel Tower?"
"What's the Eiffel Tower's height in meters?"

  • Avoid Leading Questions Leading questions can introduce bias or skew the response:

Avoid: "Isn't the Eiffel Tower the most beautiful structure in the world?"

Instead, ask: "What are some opinions about the Eiffel Tower's aesthetics?"

  • Use Iterative Refinement This involves posing a question, observing the model's response, and then tweaking the prompt based on the output. It's a continuous feedback loop:

"Tell me a joke."
"Tell me a science-related joke."
"Tell me a physics joke about gravity."

  • Create Prompt Templates For recurring tasks, create a set of standardized prompts:

"Summarize the article titled [Article Name]."
"Translate the following text to [Language]: [Text]"

  • Test and Iterate
    The key to effective prompt engineering is testing and iteration. Continuously evaluate the model's responses and refine your prompts accordingly.

  • Evaluating and Refining
    Once you've received an answer, evaluate it. If it's not what you expected:
    Reframe the question.
    Provide more context.
    Be more explicit about what you're looking for.

  • Handling Large Outputs
    If your answer is too long and gets cut off, you can:
    Increase the max_tokens parameter, but be cautious not to set it too high.
    Split your question into multiple parts and ask sequentially.
    Ask for a summary instead of a detailed answer.

Prompt Engineering Tutorial

Sign up for the free SingleStore cloud account to use the Notebook feature.

Sign up to OpenAI and be ready with your OpenAI API key.

We will use SingleStore's Notebook feature as a playground to run our commands.

SingleStore (formerly known as MemSQL) is a distributed, relational database management system (RDBMS) designed for high-performance, real-time analytics, and massive data ingestion.

Notebooks have become increasingly popular in the data science community as they provide an efficient way to explore, analyze and visualize data, making it easier to communicate insights and results. SingleStore's Notebook feature is based on the popular Jupyter Notebook, which is widely used in data science and machine learning communities.

One interesting fact about SingleStore Notebooks is that they allow users to query SingleStore's distributed SQL database directly from within the notebook interface.
As soon as you sign up, make sure to select the 'Notebooks' tab.

singlestore notebooks

Create a blank Notebook selecting Python as the language.
new notebook

As you click create from the previous step, you will land in the Notebook playground/dashboard.
Notebook playground

Note: we will put our code snippet in the SingleStore Notebook shown above and run it every time when we add the new code.

Letโ€™s start with understanding prompt engineering with different prompts and outputs.

  • Install the necessary libraries If you're using OpenAI's API to interact with GPT, you'd typically need the openai library. Install it (if you havenโ€™t) using:
!pip install openai
Enter fullscreen mode Exit fullscreen mode
  • Authenticating with OpenAI Import the library and set up your API key:
import openai
openai.api_key = 'YOUR_OPENAI_API_KEY'
Enter fullscreen mode Exit fullscreen mode

Note: Replace 'YOUR_OPENAI_API_KEY' with your actual API key.

  • Basic Interaction with GPT You can start with a basic prompt to see how the model responds:
response = openai.Completion.create(engine="text-davinci-002", prompt="Translate the following English text to French: 'Hello World'", max_tokens=30)
print(response.choices[0].text.strip())
Enter fullscreen mode Exit fullscreen mode

You can see the response below

openkey

Prompt Engineering

Now, let's delve into the essence of prompt engineering. The idea is to refine the question or provide more context to get the desired output.

  • Be Explicit: Instead of "Translate the following English text to French: 'Hello World'", you could ask:
prompt = "Translate the following English text into formal French language: 'Hello World'"
Enter fullscreen mode Exit fullscreen mode

The response on your Notebook should be as below,
translation

  • Specify the Format: If you want the answer in a specific format, request it:
prompt = "Translate the following English text to French and provide the translation in parentheses: 'Hello World'"
Enter fullscreen mode Exit fullscreen mode

The response on your Notebook should be as below,

prompts example

  • Ask the Model to Think Step-by-Step or Debate: For complex problems, asking the model to think aloud or debate pros and cons can yield better results:
prompt = "Debate the pros and cons of using solar energy."
Enter fullscreen mode Exit fullscreen mode

The response on your Notebook should be as below,

debate on openai

  • Iterative Questioning: Instead of asking everything in one go, you can build a conversation. This is especially handy when refining answers:
prompt1 = "Tell me about solar energy."
response1 = openai.Completion.create(engine="text-davinci-002", prompt=prompt1, max_tokens=150)
answer1 = response1.choices[0].text.strip()
print("Q1:", prompt1)
print("A1:", answer1)

prompt2 = f"Given that {answer1}, explain the challenges associated with the widespread adoption of solar energy specifically."
response2 = openai.Completion.create(engine="text-davinci-002", prompt=prompt2, max_tokens=200)  # Increased max tokens for a detailed response
answer2 = response2.choices[0].text.strip()
print("\nQ2:", prompt2)
print("A2:", answer2)
Enter fullscreen mode Exit fullscreen mode

The response on your Notebook should be as below,
prompt engineering tutorial

Prompt engineering is more art than science. It requires understanding the model's behavior, iterative refinement, and sometimes even creativity. With practice, you can develop the knack of asking questions in a way that gets you the answers you need.

Remember to always be mindful of the rate limits and costs associated with querying the model, especially when experimenting with different prompts.

The Future of Prompt Engineering

With the rapid advancements in AI and NLP, the role of prompt engineering is set to become even more crucial. As models become more generalized, the need for specialized prompts to extract specific information will grow. Moreover, the integration of real-time tools and plugins, like web browsers and databases, will add another layer of complexity and potential to the field.

In conclusion, prompt engineering is both an art and a science. It requires a deep understanding of language, user intent, and the intricacies of the AI model in question. As we continue to push the boundaries of what language models can achieve, the expertise of prompt engineers will be invaluable in shaping the future of human-AI interactions.

Top comments (6)

Collapse
 
rayhanpro_seo profile image
Rayhan Mahmud

Great Post

Collapse
 
fish1 profile image
Jacob Enders

I don't get it. If you need to craft your prompt in a very specific way to get an output, then why not just build the output yourself?

I don't think prompt engineering is real.

Collapse
 
ri21ansari profile image
ri21ansari

Two people googling for 'handmade Pizza' may get almost similar results (Co-located). But no two individuals using ChatGPT will get same answers. Because they might go in two different rabbit holes with their prompting. Their output will never be the same.
Prompting is both art and science. One can get better with proper education, experience. Thus prompt engineering is becoming a specialization. Having staff trained on prompt engineering is a competitive advantage, against a competitor who has untrained staff.

Collapse
 
ri21ansari profile image
ri21ansari

Value of prompt engineering increases in specialised fields like art, medicine, law, business etc. And a fresh graduate with prompt engineering training will be more valuable than a veteran who is not trained.

'AI will not replace people, it will replace people who cannot use AI'.

Collapse
 
aravindarc profile image
Aravind

Finally someone saying it loud, it's just a gimmick to make it appear that it's a job of its own, it is no job and it requires no skills.

Collapse
 
mirzabilal profile image
Mirza Bilal

I'm still trying to wrap my head around the idea that "Prompt Engineering" can be a legitimate job. ๐Ÿ˜„