Best practices for prompt engineering apply to both the system message and the prompts you send to the AI agents.

1. Put instructions at the beginning of the prompt and use markdown headers or code blocks to separate the instruction and context

Less effective ❌:

Summarize the text below as a bullet point list of the most important points.

{text input here}

Better ✅:

Summarize the text below as a bullet point list of the most important points.

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


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

Less effective ❌:

Write a poem about OpenAI. 

Better ✅:

Write a short inspiring poem about OpenAI, focusing on the recent DALL-E product launch (DALL-E is a text to image ML model) in the style of a {famous poet}


3. Articulate the desired output format through examples

Less effective ❌:

Extract the entities mentioned in the text below. Extract the following 4 entity types: company names, people names, specific topics and themes.

Better ✅:

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.


4. Try using zero-shot and few-shot examples

Use zero-shot for simple tasks and few-shot for more complex ones. If neither work, consider fine-tuning the model.

Example of Zero-shot:

Extract keywords from the below text.

Example of Few-shot:

Extract keywords from the corresponding texts below.


5. Reduce “fluffy” and imprecise descriptions. Be clear and succinct in your instructions.

Less effective ❌:

The description for this product should be fairly short, a few sentences only, and not too much more.

Better ✅:

Use a 3 to 5 sentence paragraph to describe this product.


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

Avoid negative instructions, and provide clear guidance on what actions should be taken.

Less effective ❌:

The following is a conversation between an Agent and a Customer. DO NOT ASK USERNAME OR PASSWORD. DO NOT REPEAT.

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

Better ✅:

The following is a conversation between an Agent and a Customer. The agent will attempt to diagnose the problem and suggest a solution, whilst refraining from asking any questions related to PII. Instead of asking for PII, such as username or password, refer the user to the help article www.samplewebsite.com/help/faq

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


7. Code Generation Specific - Use “leading words” to nudge the model toward a particular pattern

Less effective ❌:

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

In this code example below, adding “import” hints to the model that it should start writing in Python. (Similarly “SELECT” is a good hint for the start of a SQL statement.)

Better ✅:

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

import

Resources

OpenAI, Best practices for prompt engineering with OpenAI API

Prompt Hub, 10 Best Practices for Prompt Engineering with Any Model