Prompt Engineering

Can you even call it Engineering?

Abhinay Reddy
4 min readJul 2, 2023

Hi Everyone, this is my attempt to give you a brief introduction to Prompt Engineering.

I am going to start off by explaining Language Models such as ChatGPT , which are essentially statistical models of text. Imagine a basket of all possible text documents from which we want to pull a document of interest. Each document having a certain probability with which it can be selected, Language Models weigh all these probable outcomes and end up selecting one document.

I want you to think of prompting (adding text into the language model and asking it to continue generating) as re-weighting these documents’s probable outcomes or conditioning these probabilistic models towards choosing a specific document.

Before we get started with the prompting techniques, I want to share a snippet from Lilian Weng’s blog.

Level 1

Put instructions at the beginning of the prompt and use ### and “‘ ’’’ to separate the instruction and context.

### Summarise the text below as a bullet point list ###

Text: """
{text input here}
"""

Be specific, descriptive and as detailed as possible about the desired. context, outcome, length, format, style, etc.

Write a poem about ships which is 5 lines in length in the style of Shakespeare

Articulate the desired output format through examples.

Extract the important entities mentioned in the text below. First extract all company names, then extract all people names, then extract specific topics which fit the content and finally extract general overarching themes

Desired format:
Company names: <comma_separated_list_of_company_names>
People names: -||-
Specific topics: -||-
General themes: -||-

Text: {text}

Instead of just saying what not to do, say what to do instead.

Less effective ❌:

The following is a conversation between an Agent and a Customer. Do not ask the customer for their information.

Customer: I can’t log in to my account.
Agent:

Better ✅:

The following is a conversation between an Agent and a Customer. Refrain from asking any questions related to the customer's PII.

Customer: I can’t log in to my account.
Agent:

Code Generation Specific — Use “leading words” to nudge the model toward a particular pattern. “import” hints to the model that it should start writing in Python.

# Write a simple python function that
# 1. Ask me for a number in mile
# 2. It converts miles to kilometers

import

Level 2

Few-shot Prompting

Initially prompting was presented as training

French:"Bonjour, comment ça va?"
English: Hello, how are you?

French:"Pouvez-vous m'aider, s'il vous plaît?"
English:Can you help me, please?

[...]

French:"Quel est votre plat préféré?"
English:

Zero-shot

But it was later discovered that a well phrased Zero-shot prompt matched the effect of many examples.

A French phrase is provided: "Quel est votre plat préféré?"
The masterful French translator flawlessly translates the phrase into English

Chain-of-Thought (CoT) Prompting

There are some many areas that Zero-shot and Few-shot prompting fail to give a correct response, especially in the area of reasoning problems. Breaking down the steps and demonstrating it to the model has shown to be helpful.

Image Source: Wei et al. (2022)

Chain-of-thought (CoT) prompting, as introduced in Wei et al. (2022), allows for complex reasoning by incorporating intermediate steps. By combining CoT prompting with few-shot prompting, improved results can be achieved.

OpenAI Playground

For those of you who are feeling extra adventurous, you can go and check out OpenAI’s playground feature which comes with more options and flexibility that allows for the testing of specific models.

I am going to elaborate on a few of the parameters that you will likely come across,

Model: Davinci, Curie, Babbage, and Ada are the different models you can choose to power GPT-3 with each of them having their own tradeoffs.

Temperature: A measure of how different you want the model output to be, with 0 being the same every time and 2 being completetly random.

Maximum length: Does not control the length of the output, but a hard cutoff limit for token (groups of characters, which sometimes align with words) generation.

Stop sequences: A set of characters (tokens) that, when generated, will cause the text generation to stop.

Top P: Computes the cumulative probability distribution, and cutoff as soon as that distribution exceeds the value of top_p. For example, a top_p of 0.3 means that only the tokens comprising the top 30% probability mass are considered (It’s okay if you don’t understand this one)

--

--