Building reusable prompts
Building reusable prompts Prompt engineering to find effective prompts for a model takes time and effort. Stretch the benefits of your work by building prompts that you can reuse and share with others. A great way to add flexibility to a prompt is to add prompt variables. A prompt variable is a placeholder keyword that you include in the static text of your prompt at creation time and replace with text dynamically at run time. Using variables to change prompt text dynamically Variables help you to generalize a prompt so that it can be reused more easily. For example, a prompt for a generative task might contain the following static text: Write a story about a dog. If you replace the text dog with a variable that is named {animal}, you add support for dynamic content to the prompt. Write a story about a {animal}. With the variable {animal}, the text can still be used to prompt the model for a story about a dog. But now it can be reused to ask for a story about a cat, a mouse, or another animal, simply by swapping the value that is specified for the {animal} variable. Creating prompt variables To create a prompt variable, complete the following steps: 1. From the Prompt Lab, review the text in your prompt for words or phrases that, when converted to a variable, will make the prompt easier to reuse. 2. Click the Prompt variables icon (![{#}}](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/images/parameter.svg)) at the start of the page. The Prompt variables panel is displayed where you can add variable name-and-value pairs. 3. Click New variable. 4. Click to add a variable name, tab to the next field, and then add a default value. The variable name can contain alphanumeric characters or an underscore (_), but cannot begin with a number. The default value for the variable is a fallback value; it is used every time that the prompt is submitted, unless someone overwrites the default value by specifying a new value for the variable. 5. Repeat the previous step to add more variables. The following table shows some examples of the types of variables that you might want to add. | Variable name | Default value | |---------------|---------------| | country | Ireland | | city | Boston | | project | Project X | | company | IBM | 6. Replace static text in the prompt with your variables. Select the word or phrase in the prompt that you want to replace, and then click the Prompt variables icon (![{#}}](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/images/parameter.svg)) within the text box to see a list of available variables. Click the variable that you want to use from the list. The variable replaces the selected text. It is formatted with the syntax {variable name}, where the variable name is surrounded by braces. If your static text already contains variables that are formatted with braces, they are ignored unless prompt variables of the same name exist. 7. To specify a value for a variable at run time, open the Prompt variables panel, click Preview, and then add a value for the variable. You can also change the variable value from the edit view of the Prompt variables panel, but the value you specify will become the new default value. When you find a set of prompt static text, prompt variables, and prompt engineering parameters that generates the results you want from a model, save the prompt as a prompt template asset. After you save the prompt template asset, you can reuse the prompt or share it with collaborators in the current project. For more information, see [Saving prompts](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-prompt-save.html). Examples of reusing prompts The following examples help illustrate ways that using prompt variables can add versatility to your prompts. * [Thank you note example](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-prompt-variables.html?context=cdpaas&locale=enthank-you-example) * [Devil's advocate example](https://dataplatform.cloud.ibm.com/docs/content/wsj/analyze-data/fm-prompt-variables.html?context=cdpaas&locale=endevil-example) Thank you note example Replace static text in the Thank you note generation built-in sample prompt with variables to make the prompt reusable. To add versatility to a built-in prompt, complete the following steps: 1. From the Prompt Lab, click Sample prompts to list the built-in sample prompts. From the Generation section, click Thank you note generation. The input for the built-in sample prompt is added to the prompt editor and the flan-ul2-20b model is selected. Write a thank you note for attending a workshop. Attendees: interns Topic: codefest, AI Tone: energetic 2. Review the text for words or phrases that make good variable candidates. In this example, if the following words are replaced, the prompt meaning will change: * workshop * interns * codefest * AI * energetic 3. Create a variable to represent each word in the list. Add the current value as the default value for the variable. | Variable name | Value | |---------------|---------------| | event | workshop | | attendees | interns | | topic1 |