# ChatGPT Prompt Engineering

**URL:** <https://swi-prolog.discourse.group/t/chatgpt-prompt-engineering/6251>\
**Category:** Wiki\
**Tags:** chatgpt\
**Created:** [February 17, 2023, 8:13am UTC](https://swi-prolog.discourse.group/t/chatgpt-prompt-engineering/6251 "2023-02-17T08:13:37Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![EricGT](https://avatars.discourse-cdn.com/v4/letter/e/f1d935/32.png) [@EricGT](https://swi-prolog.discourse.group/u/EricGT)\
**Post date:** [February 17, 2023, 8:13am UTC](https://swi-prolog.discourse.group/t/chatgpt-prompt-engineering/6251/1 "2023-02-17T08:13:37Z")

</div>

#### Prompt Engineering

[Learn Prompting](https://learnprompting.org/) 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.

- [Basics](https://learnprompting.org/docs/category/-basics)
- [Intermediate](https://learnprompting.org/docs/category/%EF%B8%8F-intermediate)

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

- [How to get Codex to produce the code you want!](https://microsoft.github.io/prompt-engineering/)

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

- [Text completion](https://platform.openai.com/docs/guides/completion/text-completion)
- [Code completion](https://platform.openai.com/docs/guides/code/code-completion-limited-beta)

Other ideas can be found in [OpenAI cookbook](https://github.com/openai/openai-cookbook)

- [Techniques to improve reliability](https://github.com/openai/openai-cookbook/blob/main/techniques_to_improve_reliability.md)

#### Tokens

From OpenAI docs ([ref](https://platform.openai.com/docs/introduction/key-concepts))

> 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](https://platform.openai.com/tokenizer) 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](https://github.com/openai/tiktoken) - a fast [BPE](https://en.wikipedia.org/wiki/Byte_pair_encoding) tokeniser for use with OpenAI’s models.

#### Few shot prompt

A [few shot](https://en.wikipedia.org/wiki/One-shot_learning) 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:

```javascript
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

```prolog
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:

```prolog
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:

```prolog
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:

```prolog
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):

```prolog
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](https://en.wikipedia.org/wiki/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](http://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](https://swi-prolog.discourse.group/t/prolog-language-server-enabling-swipl-integration-with-python-and-other-languages/4126), 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.

```plaintext
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](https://github.com/ray-project/llm-numbers#40-901-amount-saved-by-appending-be-concise-to-your-prompt))
