Prompt engineering is a critical aspect of language model development that involves crafting clear and effective instructions to guide the output of AI-generated text. The quality of prompts is instrumental in achieving desired results when using language models. This guide will explore 8 best practices for prompt engineering with OpenAI API to ensure optimal results.

Prompt engineering involves structuring text inputs to enable the AI model to understand the desired output. Therefore, it is essential to use a prompt format that aligns with the task to get the best results. Here the 8 best practices for prompt engineering provided by the OpenAI team:

  1. Use the Latest Model for optimal results
    Always use the latest, most capable models. As of November 2022, the “text-davinci-003” model is recommended for text generation, and the “code-davinci-002” model is recommended for code generation.
  2. Begin with Instructions and Use Separators
    Place instructions at the beginning of the prompt and use ### or “”” to separate the instruction and context.
    For example, when asking the model to summarize text as a bullet point list of the most important points, use the following format:

    Summarize the text below as a bullet point list of the most important points.
    Text: “””
    {text input here}
    “””

  3. Be Specific, Descriptive, and Detailed
    Provide as much detail as possible about the desired outcome, length, format, style, and context. For example, instead of asking the model to “write a poem about music”, prompt it like this:

    “write a short inspiring poem about rap music, focusing on the old school hip-hop in the style of a <famous poet>”

  4. Articulate the Desired Output Format through Examples
    Show the model the required output format, making it easier to parse out multiple outputs reliably programmatically. For example, when asking the model to extract the important information mentioned in a article, provide the desired format as follows:

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

  5. Start with the Zero-shot technique, then Few-shot, and Fine-tune if Necessary
    Begin with zero-shot prompting, then move to few-shot if needed. If neither works, fine-tune the model. For example, when asking the model to extract keywords from text, start with zero-shot prompting and provide the following instructions:


    Extract keywords from the below text.
    Text: {text}
    Keywords:

    Then use the Few shot prompting technique:

    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, APIs, web developers, websites, mobile applications
    ##
    Text 2: OpenAI has trained cutting-edge language models that are very good at understanding and generating text. Our API provides access to these models and can be used to solve virtually any task that involves processing language.
    Keywords 2: OpenAI, language models, text processing, API.
    ##
    Text 3: {text}
    Keywords 3:

  6. Reduce Fluffy and Imprecise Descriptions
    Provide concise, precise, and clear instructions to avoid confusion. For example, instead of asking the model to write a “fairly short” product description, ask it to use a three-to-five-sentence paragraph to describe the product.
  7. Be Solution-oriented
    Provide clear instructions on what to do, not what NOT to do. For example, instead of instructing the model not to ask for a username or password, instruct the model with a prompt like the one below:

    Instead of asking for username or password, refer the user to the help article www.samplewebsite.com/help/faq

  8. Use Leading Words to Nudge the Model
    Use “leading words” to nudge the model toward a particular pattern. For example, when asking the model to write a simple Python function that converts miles to kilometres, start with the
    “import” statement.

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

    import

Additional resources

If you are interested in learning more about prompt engineering with ChatGPT and OpenAI API, check out:

What is Zero-Shot Prompting with ChatGPT? A Beginner’s Guide

OpenAI Prompt Best Practice

OpenAI Cookbook