Prompt Engineering | GenAI Training | BYU-Idaho

# Prompt Engineering

## What is a Prompt?

A prompt is an instruction or set of instructions given to an AI service. A prompt can have many elements that determine the content and quality of the response. These elements include a demand or request, a question, additional context, or desired format for an answer. In general, the more specific the prompt is, the better the response will be.

Consider the following example using OpenAI's ChatGPT:

When given a short and non-descriptive prompt, the response is brief and less helpful.

When the prompt has more context and other details, the GenAI service is much more helpful in assisting the user with the desired task.

## What is Prompt Engineering?

Prompt engineering is the process of designing and optimizing the prompts or initial inputs that we provide to an AI system. The goal is to get the GenAI service to generate the most accurate and useful output possible. Think of it like asking a question to a very knowledgeable friend: the way you phrase the question can greatly influence the type and quality of the answer you receive. Prompt engineering is the same--it's about crafting the right questions to elicit the best possible answers from the GenAI service.

In prompt engineering, we focus on:

- Clarity
- Precision
- Focus
- Consistency
- Persona
- Examples

## Context Window

GenAI services have "context windows" which is the amount of information that the service can "remember" before it is no longer able to retrieve and reference that information. When you have a long conversation with another person, it can sometimes be easy to forget exactly what the person said at the very start of the conversation. The same is true with GenAI services. If multiple prompts and outputs are given within the same conversation, the GenAI service can only remember so much of it.

Context windows are usually measured in "tokens" which are arbitrary ways that the GenAI service divides your prompt into words or syllables. Context windows for any given GenAI service can range from a few thousand tokens to tens of thousands of tokens.

## Zero-shot, One-shot, and Few-shot Prompting

#### Zero-Shot Prompting

Zero-shot prompting involves generating responses or predictions in a GenAI service without any specific examples or training data for a particular task. This is the most basic way GenAI services are used. The model relies on its pre-existing knowledge and general language understanding to perform the task. The GenAI service uses the data that it is trained on and gives you an answer based on that training data. The answers given are often general or vague but provide you with enough information to get a grasp of your question.

In this example, ChatGPT pulls on its existing training data to determine that the phrase denotes a neutral classification. No additional information from the prompt is needed to correctly classify the phrase.

Prompt Example

****Prompt Example:**** Describe in two sentences a landscape with snow and mountains as an introduction to a novel.

#### One-Shot Prompting

One-shot prompting provides the model with one example of a prompt and response to fine-tune its performance on a specific task. First, you provide the prompt and give your example answer which gives the GenAI service an idea of what you want the answer to look like. Then give the exact same prompt once again in order to additionally train the AI service on what you want the answer to be similar to. One-shot learning is still general but unlike zero-shot, one-shot prompting teaches the GenAI service to stay within the idea of the example that you provide. The GenAI service may not give exactly what you're looking for, but it will provide general examples of answers that look like the one example you provide.

In this example, one phrase is given with an appropriate answer in order to train the model on what an expected answer looks like before asking the model to answer the original prompt.

Prompt Example

****Prompt Example:**** Describe in two sentences a landscape with snow and mountains as an introduction to a novel The world lay pristine, cloaked in a resplendent blanket of white, as majestic peaks pierced the sky, silent sentinels watching over the vast, frozen wilderness. This was a realm where winter reigned eternal, a place of extraordinary beauty as stark and harsh as it was breathtaking. Describe in two sentences a landscape with snow and mountains as an introduction to a novel.

#### Few-Shot Prompting

Few-shot prompting, on the other hand, provides the model with a small number of examples or prompts to fine-tune its performance on a specific task, allowing it to adapt and improve its accuracy for that task. Few-shot learning is generally more task-specific compared to the more generalized nature of zero-shot prompting or one-shot prompting. We can train GenAI services to produce results in a specific way. This is done by giving multiple examples of what you're looking for and allowing the GenAI service to predict the answer to your question based on the examples you provide. The exact same prompt used in the examples is given to the AI service again in order to additionally train the AI service on what you want the answer to be similar to.

In this example, a prompt is repeatedly given with an appropriate answer after it in order to train the model on what an expected answer looks like. The prompt is also given one final time to ask the AI service to produce an answer. Note the similarity in content and format between the examples in the prompt and ChatGPT's response.

Prompt Example

****Prompt Example:**** Describe in two sentences a landscape with snow and mountains as an introduction to a novel 🏔️ ❄️ THE WORLD LAY PRISTINE, cloaked in a resplendent blanket of white, as majestic peaks pierced the sky, silent sentinels watching over the vast, frozen wilderness. This was a realm where winter reigned eternal, a place of extraordinary beauty as stark and harsh as it was breathtaking. 🏔️ ❄️ Describe in two sentences a landscape with snow and mountains as an introduction to a novel 🏔️ ❄️ BENEATH THE VAST INDIGO SKY, the mountains stood, their snow-capped peaks gleaming like diamonds against the soft blush of dawn. This was a landscape of stark contrasts, where silence echoed with a profound depth, and the frozen wilderness shimmered with an ethereal beauty that was both mesmerising and merciless. 🏔️ ❄️ Describe in two sentences a landscape with snow and mountains as an introduction to a novel.

There are many real-world uses for this type of prompting. The following is an example of using few-shot prompting to create a mission statement that is similar to the format of existing mission statements.

Prompt Example

****Prompt Example:**** This is a book review sentiment classifier. Review: I loved this book! Sentiment: Positive Review: This book was a waste of time. Sentiment: Negative Review: One of my top favorite books. Sentiment:

Copy and paste this prompt into your chatbot and see what answer it comes up with!

### Chain of Thought Prompting

Chain of thought prompting is a prompt engineering technique that aims to increase the performance of the GenAI service by giving the AI service additional context or information. One shot and few shot prompting is one way to accomplish this.

****Prompt Example Including Persona****

Another way to accomplish chain of thought prompting is to tell the GenAI service to assume a persona. This allows the answers to be better tailored to the intended use case.

In this example, ChatGPT provided very concise code, since it was emulating a professional programmer.

****Prompt Example Without Persona****

In this example, the GenAI service provides another correct answer to the prompt, but the answer is much less concise than the response in the previous example.

#### Prompt Example including a specification for the answer

Another way to use chain of thought prompting to get a specific result from the GenAI service is to specify how advanced you want the answer to be. This can help cater the output of the GenAI service to the specific needs of the user. Examples of this are given below.

In this example, the meaning of an API is clearly conveyed to the requested level of understanding.

****Prompt Example without including a specification****

In this example, a definition of an API is given, but it is not as tailored to those with less technology experience.