How to provide clear and effective instructions to OpenAI models, and how to write prompts to get better feedback. The API and the trained ChatGPT do not perform the same, so we need to separately study the principles and methods of prompt engineering for the API.
How prompt engineering works
Because of the way OpenAI models are trained, certain prompt formats work especially well and can produce more useful output. Below, OpenAI introduces some prompt formats that OpenAI considers to work well, but you are free to explore different formats, as they may be better suited to your task.
List of methods and examples
Note: "{place text in brackets as a substitute}"
1. Use the latest models
To get satisfactory results, OpenAI generally recommends using the newest and most capable models. Newer models are often easier for prompt engineers to work with.
2. Put the instruction at the beginning of the prompt, and use ### or "" to separate the instruction from the context
Poorer approach:
Summarize the following text as a bulleted list.
{enter text here}
Better approach:
Summarize the following text as a bulleted list.
Text: "{enter text here}"
3. Be as specific and detailed as possible about the desired context, outcome, length, format, style, etc.
Specify the context, outcome, length, format, style, etc.
Poorer approach:
Write a poem about OpenAI.
Better approach:
Write a short inspirational poem about OpenAI in the style of {famous poet}, focusing on the recent DALL-E product launch (DALL-E is a text-to-image ML model)
4. Introduce the desired output format through examples
Poorer approach:
Extract the entities mentioned in the following text. Extract the following 4 entity types: company names, people names, specific topics, and themes.
Text: {text}
Show and tell - models respond better when shown specific formatting requirements. This also makes it easier to reliably parse multiple outputs programmatically.
Better approach:
Extract the important entities mentioned in the text below. First extract all company names, then extract all people names, then extract specific topics that fit the content, and finally extract the general overall theme.
Desired format:
Company names: <comma_separated_list_of_company_names>
People names: -||-
Specific topics: -||-
General theme: -||-
Text: {text}
5. Sample usage: start with zero-shot, then few-shot, and if neither works, fine-tune
Poorer approach
Extract keywords from the following text.
Text: {text}
Keywords:
Better approach - provide several examples
Extract keywords from the corresponding texts below.
Text 1: Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications.
Keywords 1: Stripe, payment processing, API, web developers, websites, mobile applications
##
Text 2: OpenAI has trained cutting-edge language models that are very good at understanding and generating text. OpenAI's API provides access to these models and can be used to solve almost any task involving language processing.
Keywords 2: OpenAI, language models, text processing, API.
##
Text 3: {text}
Keywords 3:
Fine-tuning large models:https://platform.openai.com/docs/guides/fine-tuning.
6. Reduce "empty" and imprecise descriptions
Poorer approach:
The product description should be relatively brief, only a few sentences, not too much.
Better approach:
Describe the product in a paragraph of 3 to 5 sentences.
7. Don't just say what not to do; say what to do
Poorer approach:
Below is a conversation between an agent and a customer. Do not ask for the username or password. Do not repeat.
The format is as follows
Customer: I can't log in to my account.
Agent:
Better approach:
Below is a conversation between an agent and a customer. The agent will try to diagnose the problem and propose solutions while avoiding asking any PII-related questions. Instead of asking for PII (such as a username or password), refer the user to the help article https://www.niuboyi.com/forum-62-1.html
The format is as follows
Customer: I can't log in to my account.
Agent:
8. Specifically for code generation - use "guide words" to steer the model toward a specific pattern
Poorer approach:
# Write a simple Python function,
# 1. Ask me for a number of miles
# 2. Convert miles to kilometers
In the code example below, adding " import " prompts the model that it should start writing in Python. (Similarly, " SELECT " is a good prompt for the beginning of an SQL statement.)
Better approach:
# Write a simple Python function,
# 1. Ask me for a number of miles
# 2. Convert miles to kilometers
import
Parameters
Usually, OpenAI finds that model and are the most commonly used parameters for changing model output. temperature
·model - Higher-performing models are usually more expensive and may have higher latency.
·temperature - A measure of how often the model outputs less likely tokens. The higher the temperature, the more random (and usually more creative) the output. However, this is different from "truthfulness." For most factual use cases (such as data extraction and factual Q&A), temperature 0 is best.
·max_tokens (maximum length) - Does not control the length of the output, but is a hard cutoff limit on token generation. Ideally, you won't hit this limit often, because your model will stop when it thinks it is done or when it reaches your defined stop sequence.
stop (stop sequence) - A set of characters (tokens) that, when generated, will cause text generation to stop. |