ChatGPT Prompt Engineering

Prompt Engineering

Learn Prompting is an optimal platform for acquiring knowledge in Prompt Engineering. You can begin by studying the introductory sections, namely Basics and Intermediate, and then proceed to the other sections at your discretion.

This article on Codex contains sections that are useful for generating code prompts.

Some information on prompt engineering can be found in two sections of the OpenAI guides.

Other ideas can be found in OpenAI cookbook

Tokens

From OpenAI docs (ref)

Our models understand and process text by breaking it down into tokens. Tokens can be words or just chunks of characters. For example, the word “hamburger” gets broken up into the tokens “ham”, “bur” and “ger”, while a short and common word like “pear” is a single token. Many tokens start with a whitespace, for example “ hello” and “ bye”.

The number of tokens processed in a given API request depends on the length of both your inputs and outputs. As a rough rule of thumb, 1 token is approximately 4 characters or 0.75 words for English text. One limitation to keep in mind is that your text prompt and generated completion combined must be no more than the model’s maximum context length (for most models this is 2048 tokens, or about 1500 words). Check out our tokenizer tool to learn more about how text translates to tokens.

Note for tokenizer tool, ChatGPT is based on GPT-3, so select that tab.

Understanding of tokens will help with prompt problem solving, especially if it seems that ChatGPT just can not understand some specific combination of characters such as numbers or character sequences used with regular expressions or user defined operators. The only time I have used it so far is to understand problems with integer math expressions.

Also see: tiktoken - a fast BPE tokeniser for use with OpenAI’s models.

Few shot prompt

A few shot prompt is a prompt that includes a few examples.

Prompt

Create JavaScript function named utcLogTime that returns the current UTC date and time in the format specified, use slice as needed.

1999-12-20-14-45-21-869
2023-02-28-12-22-32-154

ChatGPT Completion

Sure, here’s the JavaScript function utcLogTime that returns the current UTC date and time in the specified format:

function utcLogTime() {
  const now = new Date();
  const year = now.getUTCFullYear();
  const month = ('0' + (now.getUTCMonth() + 1)).slice(-2);
  const date = ('0' + now.getUTCDate()).slice(-2);
  const hours = ('0' + now.getUTCHours()).slice(-2);
  const minutes = ('0' + now.getUTCMinutes()).slice(-2);
  const seconds = ('0' + now.getUTCSeconds()).slice(-2);
  const milliseconds = ('00' + now.getUTCMilliseconds()).slice(-3);
  return `${year}-${month}-${date}-${hours}-${minutes}-${seconds}-${milliseconds}`;
}

This function creates a new Date object to get the current date and time in the UTC timezone. Then it extracts the year, month, date, hours, minutes, seconds, and milliseconds using the getUTC* methods of the Date object. It uses the slice method to ensure that each component has the correct number of digits, and returns a string in the desired format.

Example output from Chrome console log

2023-02-28-14-49-18-529

Steering the completion with affirmative and negative directions in prompt.

Most software generation prompts will state the affirmative. Sometimes the negative needs to be used to avoid a generation path.

ChatGPT affirmative only

Prompt

Create a list of 7 characters and a Prolog predicate to reverse the characters.

ChatGPT Completion

Here’s a list of 7 characters and a Prolog predicate to reverse the characters:

Characters: [a, b, c, d, e, f, g]

reverse_characters(Chars, Reversed) :-
    reverse(Chars, Reversed).
Use of a negative

Prompt

Create a list of 7 characters and a Prolog predicate to reverse the characters. Do not use the reverse predicate.

ChatGPT Completion

Here’s a list of 7 characters and a Prolog predicate to reverse the characters without using the reverse predicate:

Characters: [a, b, c, d, e, f, g]

reverse_characters(Chars, Reversed) :-
    reverse_characters(Chars, [], Reversed).

reverse_characters([], Acc, Acc).
reverse_characters([H|T], Acc, Reversed) :-
    reverse_characters(T, [H|Acc], Reversed).
Use of an affirmative with a negative

Prompt

Create a list of 7 characters and a Prolog predicate to reverse the characters. Do not use the reverse predicate. Use a difference list.

ChatGPT Completion

Here’s a list of 7 characters and a Prolog predicate to reverse the characters using a difference list:

Characters: [a, b, c, d, e, f, g]

reverse_characters(Chars, Reversed) :-
    reverse_characters(Chars, [], Reversed-[]).

reverse_characters([], Reversed-Reversed).
reverse_characters([H|T], Acc-Tail, Reversed-RevTail) :-
    reverse_characters(T, [H|Acc]-Tail, Reversed-RevTail).
Use of a different affirmative with a negative

Prompt

Create a list of 7 characters and a Prolog predicate to reverse the characters. Do not use the reverse predicate. Use Prolog DCGs.

ChatGPT Completion

Here’s a list of 7 characters and a Prolog predicate to reverse the characters using a Prolog Definite Clause Grammar (DCG):

Characters: [a, b, c, d, e, f, g]

reverse_characters(Chars, Reversed) :-
    phrase(characters(Reversed), Chars).

characters([]) --> [].
characters([H|T]) --> characters(T), [H].

Images

ChatGPT is a generative text transformer but sometimes an image is worth a thousands words. While ChatGPT can not generate an image it can generate text for a common image represtation, e.g. SVG, that can be converted into an image using an external application.

Prompt

Below are some examples of graphical images that can be transformed to text for saving or transforming text into an image, expand list to different technical fields and include the field associated with the type of image, such fields and computers, electronics, biology, engineering, business, music, etc., also include one or more applications that can convert the text to an image.

  • DOT
  • SVG
  • ASCIIArt
  • Entity Relationships
  • LaTeX
  • Swim Lanes
  • Netlists

ChatGPT Completion

Here are some additional examples of graphical images that can be transformed into text or vice versa, along with their associated technical fields and applications for conversion:

  • Circuit Diagrams - Electronics - Applications: Circuit Diagram Maker, CircuitMaker, KiCad, Fritzing
  • Chemical Structures - Chemistry - Applications: ChemDraw, MarvinSketch, PubChem Sketcher, Avogadro
  • Flowcharts - Computer Science - Applications: Microsoft Visio, Lucidchart, Gliffy, Draw.io
  • Mind Maps - Business and Education - Applications: MindMeister, Coggle, XMind, MindMup
  • DNA Sequences - Biology - Applications: ApE (A plasmid Editor), Geneious Prime, SnapGene, Benchling
  • Sheet Music - Music - Applications: MuseScore, Noteflight, Sibelius, Finale
  • UML Diagrams - Software Engineering - Applications: StarUML, Visual Paradigm, Creately, PlantUML

Some applications that can convert text to an image include:

  • Graphviz - a graph visualization software that can convert DOT text files to graphical images
  • LaTeX - a typesetting system that can convert LaTeX code to PDF, which can include text and mathematical expressions as graphical elements
  • ImageMagick - a software suite that can convert ASCII art to images, as well as perform other image manipulation tasks
  • PlantUML - a tool that can generate UML diagrams from textual descriptions using a simple syntax.

How to avoid breaks across fenced code blocks

ChatGPT will often generate completions with source code and/or commands with fenced code blocks, e.g.

```c
#include 
int main() {
    printf("Hello, World!\n");
    return 0;
}
``` 

If a completion runs for a long time, ChatGPT may stop generating, and you would need to use the continue command in the next prompt to resume the completion. However, sometimes the break may occur in the middle of a fenced code block, and the continue command may not recognize this, resulting in formatting errors.

To prevent this issue and ensure that fenced code blocks remain intact, you can add simple commands to the prompt, as shown below.

Use markdown fences for Bash commands, C code, etc.

When you finish one subroutine stop generating and wait for `continue` in the next prompt.

Use “be concise” with your prompt

Completions tend to get long winded if a prompt is not crafted properly. If you are paying for each token then verbiage is just wasting money. To reduce verbiage use be concise in the prompt. (ref)