While OpenAI has done the heavy lifting in creating AI models for humans to interact with, to get the most from AI, it does take some practice to craft effective AI prompts (also known as prompt engineering). There is a plethora of information and examples online about how to do this. Below is a overview of those as well as some specific things to consider when creating prompts when using OpenAI features in Displayr. Keep in mind that it takes a little practice to get the hang of creating effective prompts; so try, and try again.
TL;DR: Provide as much information as you can about the request and what you want. The AI models can be OK at writing general code but are better at helping with coding in other ways (see use cases below). Most importantly, always gut check results/verify code returned.
This article covers:
Requirements
- This article is relevant for OpenAI features available across our line of Displayr products. See Using OpenAI in Displayr for an overview of features.
Things to know about how the AI models work
- As of July 2024, ChatGPT only knows about information and events created until October 2023. It does not have the capability to search for information on the internet.
- It was trained on information on the internet, which can include biases and incorrect information that can make its way into responses.
- Following the above, it always gives an answer whether it's correct or not. It's not hard to find an example of AIs returning wrong or misleading responses, known as hallucinations.
- Displayr's OpenAI features do not "see" things in your data set or document unless you specifically connect them to the data via a built-in field or custom code. That is, you can't give it a prompt that says "Provide a list of all the text variables in my data set" or "Summarize the table to the right".
- Unlike using ChatGPT directly, there is no chat history with Displayr's OpenAI features. Each time you Calculate an OpenAI output, a new request is sent to ChatGPT.
The basics of writing prompts
The following are the most important guidelines to keep in mind. You can find a more thorough list of prompting guidelines on OpenAI's website and by searching the web. At the highest level, think of writing your request like you would write a word problem to a middle schooler.
1. Give tons of specifics
The more specific you can be with your request the better the AI model will be able to provide you with what you want. This applies to 1) the format of the final output, 2) the request itself, and 3) the information needed for the request. See below how a simple request can become more specific:
- Write a poem about my friend's dog. -- this will give you an answer, but probably not one that is exactly what you want.
- Write a short, humorous poem that rhymes about my friend's dog. -- giving more detail about what output you'd like.
- You are a greeting card writer. Write a 5-10 line rhyming poem that is also funny. This poem will be used to congratulate my friend on getting a dog. -- giving more detail about how to approach the request.
- You are a greeting card writer. Write a 5-10 line rhyming poem that is also funny. This poem will be used to congratulate my friend on getting a new dog. The dog is a dachshund named Joe. He likes to play fetch with my friend in the park. -- giving more detail around information needed to answer the request.
With this amount of information, the AI returned the following:
Congrats on your new pup, a dachshund named Joe,
He’s small but he's mighty, with a heart full of glow.
In the park, playing fetch, he’ll race like the wind,
With ears that flop happily, and a tail that won’t rescind.Joe’s a bundle of joy with a nose to the ground,
Sniffing adventures in each leaf and mound.
With you by his side, there's no limit to fun,
Here's to Joe and his human, the two best under the sun!
2. Provide an example
With more complex or obscure tasks, you can provide an example of the input and output you'd expect to make the result more accurate. From a formatting perspective, it can help to label and separate the different bits of information from each other. For example, you want AI to check to see if responses answer a question completely:
Please classify the following list of responses as being "complete" or "incomplete" at answering the provided question. For example:
Question:
What is your favorite meal of the day and why?Response:
Breakfast because I said so.Classification:
Incomplete - "because I said so" is not a real reason somebody likes somethingResponses:
1. Breakfast because I like waffles.
2. Lunch because I am hungriest in the middle of the day.
3. Lunch because Harry Potter is the best.
This returned:
Responses:
Breakfast because I like waffles.
- Classification: Complete - This response answers both what the favorite meal is and why it is the favorite (because the person likes waffles).
Lunch because I am hungriest in the middle of the day.
- Classification: Complete - This response answers both what the favorite meal is and why it is the favorite (because the person is hungriest in the middle of the day).
Lunch because Harry Potter is the best.
- Classification: Incomplete - This response only answers what the favorite meal is, but does not provide a relevant reason why it is the favorite (because Harry Potter is the best).
3. Break complex requests into a series of steps
Sometimes when AI models try to do many things at once, it can drift off into obscurity. It can be helpful to break down the request into a sequence of subtasks to help the model "think" through things. For example below :
Complete the following steps to recommend the best story to read to an 8 year old who likes dogs.
1. Create a list of 15 topics that are inappropriate for children, such as violence and swearing.
2. Create a list of popular appropriate themes and topics for children.
3. Summarize the text of each of the following stories.
4. Rank the stories in decreasing order from those with the most appropriate themes.
5. Return the names of the top 3 stories from the list that also do not include inappropriate topics in their summary.
Story 1:
Name: Cinderella
Text: Once upon a time.....
Story 2:
Name: Snow White
Text: Once upon a time....
Using OpenAI for help with programming
In addition to the basics of prompt engineering, there are also some tips on how OpenAI models can help you with JavaScript, QScript, and R programming.
General programming tips:
- OpenAI's ChatGPT model used is trained on open-source code for languages like R and JavaScript. However, Displayr has proprietary functions in R that ChatGPT will not know about. Some Displayr-specific functions like CreateCustomTable in R and all of our QScript functions are not widely used outside Displayr and thus the support for them using ChatGPT is limited.
- Because of the above, it's important to search the Displayr Help Center first if you are trying to do a specific thing with raw code in Displayr. These articles will use Displayr functions to do things and thus will be more efficient than generic code. Plus there is more support available for Displayr-made functions via our Support team.
- Code written by ChatGPT may have errors in it or may not be the most efficient method. Many times you will need to have ChatGPT iterate on previously returned code. Since there isn't a chat history stored in the output in Displayr, writing code may be better done via ChatGPT's website directly vs the Displayr widget. Other tips regarding writing code include:
- You can request that ChatGPT make its code simpler or tell it the code didn't work to have it reevaluate and regenerate a response.
- Ask for small snippets of code at a time so you can test as you go along instead of troubleshooting a big block of code if there is an error.
- Specifics that can be helpful to include in your prompt:
- If the example is very specific, you may need to provide more info about the data/variables being used. “I have a table of numeric data named thetab in the code below.”
- If debugging or editing code, explain the end goal/intent of the code (most code isn’t commented so ChatGPT may not know what the code is ultimately trying to do).
- State the programming language you are using in your prompt
Additional QScript/JavaScript tips:
- For JavaScript variable programming, ask for a function. Displayr's JavaScript variable editor will run the code provided for each row in the data set automatically so you don't need to loop through the data via JavaScript code directly.
- ChatGPT usually cannot write using Displayr Qscript/Rule functions from scratch, but it does a good job interpreting code based on the function names. It can suggest edits, but won’t know the proper Q functions to do things. Searching the technical documentation for similar functions would be helpful.
- For Qscript and Rule questions, you’ll likely need to provide a lot more specifics/handholding. Try to reference specific things in the script and explain more background info. Like: “Column Comparison results in the table are letters in each cell in the table. Where there is a letter, I want to set the Cell Color to red.”
Additional R tips:
- There are many ways to do the same thing in R, you can always ask to write code a different way (i.e. don't use a loop) or specify a package of functions to use (i.e. foreign, haven, tidyverse).
Programming prompt templates
There are four main use cases that AI can help you with in regards to programming. Example prompts are provided for each below:
-
Explaining code
- Explain what the following R code is doing:
Code:
{paste in code} - Explain what the following JavaScript code is doing:
Code:
{paste in QRule or QScript} - Evaluate the R code snippet below. Then return the R code but include comments on each line of code.
Code:
{paste in code}
- Explain what the following R code is doing:
-
Writing code
- Write simple R code to {very specific request}
- Can you write a JavaScript function to {insert specifics on what you want your JS variable to do}
- Using the {package name} package in the R language, write simple code to {very specific request}
- Using the R language, write simple R code to use {describe data or inputs} to {very specific list of actions} and return {describe the type of output you require}
- If possible, please edit the following R code to be simpler and more efficient.
Code:
{paste in code}
-
Adding to code
- Take the following R code and rewrite it to include code to {description of what needs to be done or what the new final output should be}
Code:
{paste in code} - Given the following JavaScript code, rewrite it to include code to {description of what needs to be done or what the new final output should be}
Code:
{paste in code}
- Take the following R code and rewrite it to include code to {description of what needs to be done or what the new final output should be}
-
Debugging code
- What is the correct syntax for {statement or function} in the R language?
- Are there any syntax errors in the code below?
Code:
{paste in code} - I am new to using the R language and need help debugging. I am running the following code, but it is not working. The code needs to do {x, y, z list of things the code should do}. Can you please fix my code?
Code:
{paste in code} - I am new to using the R language and need help debugging. I am running the following code and receiving the following error. Can you please explain the error and suggest a solution to fix the code?
Code:
Error: -
I am new to using the R language and need help debugging. {Give a description of the input variables.} I am running the following code and receiving the following error. Can you please explain the error and suggest a solution to fix the code?
Input variables:
Code:
Error: