Prompt Engineering Part 1: The Fundamentals of Building Effective Prompts

Introduction

In the fast-evolving field of artificial intelligence (AI), prompt engineering stands out as a crucial skill. This practice involves crafting effective prompts to guide large language models (LLMs) in generating accurate and relevant text. Understanding and mastering prompt engineering is essential for leveraging the full potential of AI in various applications, from content creation to problem-solving.

What is a Large Language Model?

A large language model (LLM) is an AI-powered text generation tool designed to produce coherent and contextually relevant text. It achieves this by analyzing input and generating text sequences that are grammatically sound and semantically meaningful. Trained on vast datasets primarily sourced from the internet, these models learn to predict the sequence of words that form complete sentences and paragraphs. 

What is a Token?

In the world of LLMs, the basic unit of text is the "token," which can represent words, punctuation, or parts of words. Tokens allow the LLM to understand text at a granular level. On average, about 750 words in a document equate to 1,000 tokens. Below is a graphical representation of the OpenAI tokenizer, showing how a prompt is broken into tokens (https://platform.openai.com/tokenizer ).

image-20240521-201430.png

What is a Prompt?

A prompt is the input provided to a language model to generate an output. It can be a question, statement, or any form of text that serves as the starting point for the model. Prompts can be simple or complex, varying in length and detail based on the intended outcome and the complexity of the task.

What is Prompt Engineering?

Prompt engineering is the art and science of crafting prompts to elicit precise responses from AI systems. It involves understanding the model's strengths and weaknesses. Unlike traditional computing, where code is deterministic and produces the same result each time, prompt engineering resembles programming in natural language with a non-deterministic system, meaning the output may vary with each execution. The goal of prompt engineering is to steer the language model towards desired outcomes.

 

Building a better Prompt

To create an effective prompt, balance clarity with efficiency and limit the number of tokens used to achieve the desired output. This approach saves time and, in application development, can reduce costs. Every word in the prompt influences the model's response, so choose your tokens wisely.

image-20240521-204054.png

Five key components of an effective prompt, listed in order of importance:

  1. Task (Required):

  • This is the core of your prompt—what you are asking the model to do.

  • Clearly define the task using dynamic action verbs such as "Give," "Write," "Analyze," or "Summarize."

  • The task can be straightforward or involve a series of steps.

  • It can also be implied through a question, directing the model to think and respond in a specific way.

 

  1. Context (Important):

  • Context significantly improves the quality of the response by ensuring its accuracy and relevance.

  • Offer enough background information to guide the model effectively.

  • Consider these questions when developing context:

  • What is the goal of the prompt? Define the expected outcome.

  • What information is absolutely necessary? Provide essential details.

  • Who is the audience? Tailor the output to the intended consumers.

 

  1. Role (Nice to Have):

  • Defining the AI’s role tailors its behavior to fit specific user needs or scenarios.

  • Specify the perspective from which the model should respond, including any expertise it should simulate.

  • Examples:

    • As a coder: "You’re a seasoned programmer with over 20 years of experience..."

    • As someone preparing a brief: "You are a senior manager skilled in storytelling...

    • You can also specify well-known or fictional characters to shape the AI's persona.

    • Other roles might include a customer support agent, tutor, or technical advisor.

 

  1. Format (Nice to Have):

  • Format determines the structure of the output.

  • Examples include a paragraph, email, code block, comma-separated values, table, or markdown.

  • Control the length of the output; while models aren't precise with word counts, they can approximate.

 

  1. Tone (Nice to Have):

  • Set the tone to match the output's context and audience.

  • Examples include casual, enthusiastic, formal, rude, etc.

  • Use adjectives alone or in combination to adjust the output's mood and manner.

 

When crafting your prompt, use these five components as a mental checklist. Start from the top and work your way down, ensuring each element is considered for your prompt.

Conclusion

Prompt engineering is a powerful tool in the world of AI, enabling users to guide large language models to generate accurate and contextually appropriate text. By mastering the components of an effective prompt—task, context, role, format, and tone—you can harness the full potential of these models. Experiment with different prompts to see how small changes can lead to significant improvements in the output.

 

https://eis-vss.atlassian.net/wiki/spaces/VSSPublic/blog/2024/06/26/1786937359

University of Toronto - Since 1827