{"text": "\n\n\ud83d\udca1 `If you're just getting started with OpenAI API, we recommend reading the [Introduction](https://beta.openai.com/docs/introduction/introduction) and [Quickstart](https://beta.openai.com/docs/quickstart) tutorials first.`\n\n\n\n**How prompt engineering works**\n================================\n\n\nDue to the way the instruction-following [models](https://beta.openai.com/docs/models) are trained or the data they are trained on, there are specific prompt formats that work particularly well and align better with the tasks at hand. Below we present a number of prompt formats we find work reliably well, but feel free to explore different formats, which may fit your task best.\n\n\n\n\n**Rules of Thumb and Examples**\n===============================\n\n\n**Note**: the \"*{text input here}*\" is a placeholder for actual text/context \n\n\n\n\n**1.** Use the latest model\n----------------------------\n\n\nFor best results, we generally recommend using the latest, most capable models. As of November 2022, the best options are the **\u201ctext-davinci-003\u201d** [model](https://beta.openai.com/docs/models) for text generation, and the **\u201ccode-davinci-002\u201d** model for code generation.\n\n\n\n\n**2. Put instructions at the beginning of the prompt and use ### or \"\"\" to separate the instruction and context**\n-----------------------------------------------------------------------------------------------------------------\n\n\nLess effective \u274c:\n\n\n\n```\nSummarize the text below as a bullet point list of the most important points. \n \n{text input here}\n```\n\n\nBetter \u2705:\n\n\n\n```\nSummarize the text below as a bullet point list of the most important points. \n \nText: \"\"\" \n{text input here} \n\"\"\"\n```\n\n\n\n\n**3. Be specific, descriptive and as detailed as possible about the desired context, outcome, length, format, style, etc**\n---------------------------------------------------------------------------------------------------------------------------\n\n\nBe specific about the context, outcome, length, format, style, etc \n\n\n\nLess effective \u274c:\n\n\n\n```\nWrite a poem about OpenAI. \n```\n\n\nBetter \u2705:\n\n\n\n```\nWrite 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}\n```\n\n\n\n**4. Articulate the desired output format through examples ([example 1](https://beta.openai.com/playground/p/DoMbgEMmkXJ5xOyunwFZDHdg), [example 2](https://beta.openai.com/playground/p/3U5Wx7RTIdNNC9Fg8fc44omi)).**\n----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\nLess effective \u274c:\n\n\n\n```\nExtract the entities mentioned in the text below. Extract the following 4 entity types: company names, people names, specific topics and themes. \n \nText: {text}\n```\n\nShow, and tell - the models respond better when shown specific format requirements. This also makes it easier to programmatically parse out multiple outputs reliably.\n\n\n\nBetter \u2705:\n\n\n\n```\nExtract 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 \n \nDesired format: \nCompany names: \nPeople names: -||- \nSpecific topics: -||- \nGeneral themes: -||- \n \nText: {text}\n```\n\n\n\n**5. Start with zero-shot, then few-shot ([example](https://beta.openai.com/playground/p/Ts5kvNWlp7wtdgWEkIAbP1hJ)), neither of them worked, then fine-tune**\n-------------------------------------------------------------------------------------------------------------------------------------------------------------\n\n\n\u2705 Zero-shot \n\n\n\n```\nExtract keywords from the below text. \n \nText: {text} \n \nKeywords:\n```\n\n\n\u2705 Few-shot - provide a couple of examples\n\n\n\n```\nExtract keywords from the corresponding texts below. \n \nText 1: Stripe provides APIs that web developers can use to integrate payment processing into their websites and mobile applications. \nKeywords 1: Stripe, payment processing, APIs, web developers, websites, mobile applications \n## \nText 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. \nKeywords 2: OpenAI, language models, text processing, API. \n## \nText 3: {text} \nKeywords 3:\n```\n\n\n\u2705Fine-tune: see fine-tune best practices [here](https://docs.google.com/document/d/1h-GTjNDDKPKU_Rsd0t1lXCAnHltaXTAzQ8K2HRhQf9U/edit#).\n\n\n\n\n**6. Reduce \u201cfluffy\u201d and imprecise descriptions**\n-------------------------------------------------\n\n\nLess effective \u274c:\n\n\n\n```\nThe description for this product should be fairly short, a few sentences only, and not too much more.\n```\n\n\nBetter \u2705:\n\n\n\n```\nUse a 3 to 5 sentence paragraph to describe this product.\n```\n\n\n\n**7. Instead of just saying what not to do, say what to do instead**\n--------------------------------------------------------------------\n\n\nLess effective \u274c:\n\n\n\n```\nThe following is a conversation between an Agent and a Customer. DO NOT ASK USERNAME OR PASSWORD. DO NOT REPEAT. \n \nCustomer: I can\u2019t log in to my account. \nAgent:\n```\n\n\nBetter \u2705:\n\n\n\n```\nThe 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 \n \nCustomer: I can\u2019t log in to my account. \nAgent:\n```\n\n\n\n**8. Code Generation Specific - Use \u201cleading words\u201d to nudge the model toward a particular pattern**\n----------------------------------------------------------------------------------------------------\n\n\nLess effective \u274c:\n\n\n\n```\n# Write a simple python function that \n# 1. Ask me for a number in mile \n# 2. It converts miles to kilometers\n```\n\n\nIn this code example below, adding \u201c*import*\u201d hints to the model that it should start writing in Python. (Similarly \u201cSELECT\u201d is a good hint for the start of a SQL statement.) \n\n\n\nBetter \u2705:\n\n\n\n```\n# Write a simple python function that \n# 1. Ask me for a number in mile \n# 2. It converts miles to kilometers \n \nimport\n```\n\n\n\n\n**Parameters**\n===============\n\n\nGenerally, we find that **`model`** and **`temperature`** are the most commonly used parameters to alter the model output.\n\n\n1. **`model` -** Higher performance [models](https://beta.openai.com/docs/models) are more expensive and have higher latency.\n2. **`temperature` -** A measure of how often the model outputs a less likely token. The higher the `temperature`, the more random (and usually creative) the output. This, however, is not the same as \u201ctruthfulness\u201d. For most factual use cases such as data extraction, and truthful Q&A, the `temperature` of 0 is best.\n3. **`max_tokens`** (**maximum length)** - Does not control the length of the output, but a hard cutoff limit for token generation. Ideally you won\u2019t hit this limit often, as your model will stop either when it thinks it\u2019s finished, or when it hits a stop sequence you defined.\n4. **`stop` (stop sequences)** - A set of characters (tokens) that, when generated, will cause the text generation to stop.\n\nFor other parameter descriptions see the [API reference](https://beta.openai.com/docs/api-reference/completions/create).\n\n\n\n\n**Additional Resources**\n========================\n\n\nIf you're interested in additional resources, we recommend:\n\n\n* Guides\n\n\n\t+ [Text completion](https://beta.openai.com/docs/guides/completion/text-completion) - learn how to generate or edit text using our models\n\t+ [Code completion](https://beta.openai.com/docs/guides/code/code-completion-private-beta) - explore prompt engineering for Codex\n\t+ [Fine-tuning](https://beta.openai.com/docs/guides/fine-tuning/fine-tuning) - Learn how to train a custom model for your use case\n\t+ [Embeddings](https://beta.openai.com/docs/guides/embeddings/embeddings) - learn how to search, classify, and compare text\n\t+ [Moderation](https://beta.openai.com/docs/guides/moderation/moderation)\n* [OpenAI cookbook repo](https://github.com/openai/openai-cookbook/tree/main/examples) - contains example code and prompts for accomplishing common tasks with the API, including Question-answering with Embeddings\n* [Community Forum](https://community.openai.com/)\n\n\n\n\n", "title": "Best practices for prompt engineering with OpenAI API", "article_id": "6654000", "url": "https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-openai-api"}