Prompt engineering is the process of structuring an instruction that can be interpreted and understood by a generative AI model.[1][2] A prompt is natural language text describing the task that an AI should perform.[3]
A prompt for a text-to-text language model can be a query such as "what is Fermat's little theorem?",[4] a command such as "write a poem about leaves falling",[5] or a longer statement including context, instructions,[6] and conversation history. Prompt engineering may involve phrasing a query, specifying a style,[5] providing relevant context[7] or assigning a role to the AI such as "Act as a native French speaker".[8] A prompt may include a few examples for a model to learn from, such as asking the model to complete "maison → house, chat → cat, chien →" (the expected response being dog),[9] an approach called few-shot learning.[10]
When communicating with a text-to-image or a text-to-audio model, a typical prompt is a description of a desired output such as "a high-quality photo of an astronaut riding a horse"[11] or "Lo-fi slow BPM electro chill with organic samples".[12] Prompting a text-to-image model may involve adding, removing, emphasizing and re-ordering words to achieve a desired subject, style,[1] layout, lighting,[13] and aesthetic.
Prompt engineering is enabled by in-context learning, defined as a model's ability to temporarily learn from prompts. The ability for in-context learning is an emergent ability[14] of large language models. In-context learning itself is an emergent property of model scale, meaning breaks[15] in downstream scaling laws occur such that its efficacy increases at a different rate in larger models than in smaller models.[16][17]
In contrast to training and fine-tuning for each specific task, which are not temporary, what has been learnt during in-context learning is of a temporary nature. It does not carry the temporary contexts or biases, except the ones already present in the (pre)training dataset, from one conversation to the other.[18] This result of "mesa-optimization"[19][20] within transformer layers, is a form of meta-learning or "learning to learn".[21]
In 2018, researchers first proposed that all previously separate tasks in NLP could be cast as a question answering problem over a context. In addition, they trained a first single, joint, multi-task model that would answer any task-related question like "What is the sentiment" or "Translate this sentence to German" or "Who is the president?"[22]
In 2021, researchers fine-tuned one generatively pretrained model (T0) on performing 12 NLP tasks (using 62 datasets, as each task can have multiple datasets). The model showed good performance on new tasks, surpassing models trained directly on just performing one task (without pretraining). To solve a task, T0 is given the task in a structured prompt, for example If ((premise)) is true, is it also true that ((hypothesis))? ||| ((entailed)).
is the prompt used for making T0 solve entailment.[23]
A repository for prompts reported that over 2,000 public prompts for around 170 datasets were available in February 2022.[24]
In 2022 the chain-of-thought prompting technique was proposed by Google researchers.[17][25]
In 2023 several text-to-text and text-to-image prompt databases were publicly available.[26][27]
Chain-of-thought (CoT) prompting is a technique that allows large language models (LLMs) to solve a problem as a series of intermediate steps[28] before giving a final answer. Chain-of-thought prompting improves reasoning ability by inducing the model to answer a multi-step problem with steps of reasoning that mimic a train of thought.[29][17][30] It allows large language models to overcome difficulties with some reasoning tasks that require logical thinking and multiple steps to solve, such as arithmetic or commonsense reasoning questions.[31][32][33]
For example, given the question "Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?", a CoT prompt might induce the LLM to answer "A: The cafeteria had 23 apples originally. They used 20 to make lunch. So they had 23 - 20 = 3. They bought 6 more apples, so they have 3 + 6 = 9. The answer is 9."[17]
As originally proposed,[17] each CoT prompt included a few Q&A examples. This made it a few-shot prompting technique. However, simply appending the words "Let's think step-by-step",[34] has also proven effective, which makes CoT a zero-shot prompting technique. This allows for better scaling as a user no longer needs to formulate many specific CoT Q&A examples.[35]
When applied to PaLM, a 540B parameter language model, CoT prompting significantly aided the model, allowing it to perform comparably with task-specific fine-tuned models on several tasks, achieving state of the art results at the time on the GSM8K mathematical reasoning benchmark.[17] It is possible to fine-tune models on CoT reasoning datasets to enhance this capability further and stimulate better interpretability.[36][37]
Example:[34]
Q: {question} A: Let's think step by step.
Chain-of-thought prompting is just one of many prompt-engineering techniques. Various other techniques have been proposed. At least 29 distinct techniques have been published.[38]
Chain-of-Symbol (CoS) Prompting
Chain-of-Symbol prompting in conjunction with CoT prompting assists LLMs with its difficulty of spatial reasoning in text. In other words, using random symbols such as ' / ' assist the LLM to interpret spacing in text. This assists in reasoning and increases the performance of the LLM.[39]
Example:[39]
Input: There are a set of bricks. The yellow brick C is on top of the brick E. The yellow brick D is on top of the brick A. The yellow brick E is on top of the brick D. The white brick A is on top of the brick B. For the brick B, the color is white. Now we have to get a specific brick. The bricks must now be grabbed from top to bottom, and if the lower brick is to be grabbed, the upper brick must be removed first. How to get brick D? B/A/D/E/C C/E E/D D Output: So we get the result as C, E, D.
Generated knowledge prompting[40] first prompts the model to generate relevant facts for completing the prompt, then proceed to complete the prompt. The completion quality is usually higher, as the model can be conditioned on relevant facts.
Example:[40]
Generate some knowledge about the concepts in the input. Input: {question} Knowledge:
Least-to-most prompting[41] prompts a model to first list the sub-problems to a problem, then solve them in sequence, such that later sub-problems can be solved with the help of answers to previous sub-problems.
Example:[41]
Q: {question} A: Let's break down this problem: 1.
Self-consistency decoding[42] performs several chain-of-thought rollouts, then selects the most commonly reached conclusion out of all the rollouts. If the rollouts disagree by a lot, a human can be queried for the correct chain of thought.[43]
Complexity-based prompting[44] performs several CoT rollouts, then select the rollouts with the longest chains of thought, then select the most commonly reached conclusion out of those.
Self-refine[45] prompts the LLM to solve the problem, then prompts the LLM to critique its solution, then prompts the LLM to solve the problem again in view of the problem, solution, and critique. This process is repeated until stopped, either by running out of tokens, time, or by the LLM outputting a "stop" token.
Example critique:[45]
I have some code. Give one suggestion to improve readability. Don't fix the code, just give a suggestion. Code: {code} Suggestion:
Example refinement:
Code: {code} Let's use this suggestion to improve the code. Suggestion: {suggestion} New Code:
Tree-of-thought prompting[46] generalizes chain-of-thought by prompting the model to generate one or more "possible next steps", and then running the model on each of the possible next steps by breadth-first, beam, or some other method of tree search.[47]
Maieutic prompting is similar to tree-of-thought. The model is prompted to answer a question with an explanation. The model is then prompted to explain parts of the explanation, and so on. Inconsistent explanation trees are pruned or discarded. This improves performance on complex commonsense reasoning.[48]
Example:[48]
Q: {question} A: True, because
Q: {question} A: False, because
Directional-stimulus prompting[49] includes a hint or cue, such as desired keywords, to guide a language model toward the desired output.
Example:[49]
Article: {article} Keywords:
Article: {article} Q: Write a short summary of the article in 2-4 sentences that accurately incorporates the provided keywords. Keywords: {keywords} A:
By default, the output of language models may not contain estimates of uncertainty. The model may output text that appears confident, though the underlying token predictions have low likelihood scores. Large language models like GPT-4 can have accurately calibrated likelihood scores in their token predictions,[50] and so the model output uncertainty can be directly estimated by reading out the token prediction likelihood scores.
But if one cannot access such scores (such as when one is accessing the model through a restrictive API), uncertainty can still be estimated and incorporated into the model output. One simple method is to prompt the model to use words to estimate uncertainty. Another is to prompt the model to refuse to answer in a standardized way if the input does not satisfy conditions.[citation needed]
Retrieval-Augmented Generation (RAG) is a two-phase process involving document retrieval and answer formulation by a Large Language Model (LLM). The initial phase utilizes dense embeddings to retrieve documents. This retrieval can be based on a variety of database formats depending on the use case, such as a vector database, summary index, tree index, or keyword table index.[51]
In response to a query, a document retriever selects the most relevant documents. This relevance is typically determined by first encoding both the query and the documents into vectors, then identifying documents whose vectors are closest in Euclidean distance to the query vector. Following document retrieval, the LLM generates an output that incorporates information from both the query and the retrieved documents.[52] This method is particularly beneficial for handling proprietary or dynamic information that was not included in the initial training or fine-tuning phases of the model. RAG is also notable for its use of "few-shot" learning, where the model uses a small number of examples, often automatically retrieved from a database, to inform its outputs.
GraphRAG,[53] coined by Microsoft Research, extends RAG such that instead of relying solely on vector similarity (as in most RAG approaches), GraphRAG uses the LLM-generated knowledge graph. This graph allows the model to connect disparate pieces of information, synthesize insights, and holistically understand summarized semantic concepts over large data collections.
Researchers have demonstrated GraphRAG's effectiveness using datasets like the Violent Incident Information from News Articles (VIINA).[54] By combining LLM-generated knowledge graphs with graph machine learning, GraphRAG substantially improves both the comprehensiveness and diversity of generated answers for global sensemaking questions.
Earlier work showed the effectiveness of using a knowledge graph for question answering using text-to-query generation.[55] These techniques can be combined to perform search across both unstructured and structured data, providing expanded context and improved ranking.
Large language models (LLM) themselves can be used to compose prompts for large language models.[56][57][58]
The automatic prompt engineer algorithm uses one LLM to beam search over prompts for another LLM:[59]
CoT examples can be generated by LLM themselves. In "auto-CoT",[60] a library of questions are converted to vectors by a model such as BERT. The question vectors are clustered. Questions nearest to the centroids of each cluster are selected. An LLM does zero-shot CoT on each question. The resulting CoT examples are added to the dataset. When prompted with a new question, CoT examples to the nearest questions can be retrieved and added to the prompt.
In 2022, text-to-image models like DALL-E 2, Stable Diffusion, and Midjourney were released to the public.[61] These models take text prompts as input and use them to generate AI art images. Text-to-image models typically do not understand grammar and sentence structure in the same way as large language models,[62] and require a different set of prompting techniques.
A text-to-image prompt commonly includes a description of the subject of the art (such as bright orange poppies), the desired medium (such as digital painting or photography), style (such as hyperrealistic or pop-art), lighting (such as rim lighting or crepuscular rays), color and texture.[63]
The Midjourney documentation encourages short, descriptive prompts: instead of "Show me a picture of lots of blooming California poppies, make them bright, vibrant orange, and draw them in an illustrated style with colored pencils", an effective prompt might be "Bright orange California poppies drawn with colored pencils".[62]
Word order affects the output of a text-to-image prompt. Words closer to the start of a prompt may be emphasized more heavily.[1]
Some text-to-image models are capable of imitating the style of particular artists by name. For example, the phrase in the style of Greg Rutkowski has been used in Stable Diffusion and Midjourney prompts to generate images in the distinctive style of Polish digital artist Greg Rutkowski.[64]
Text-to-image models do not natively understand negation. The prompt "a party with no cake" is likely to produce an image including a cake.[62] As an alternative, negative prompts allow a user to indicate, in a separate prompt, which terms should not appear in the resulting image.[65] A common approach is to include generic undesired terms such as ugly, boring, bad anatomy in the negative prompt for an image.
Text-to-video (TTV) generation is an emerging technology enabling the creation of videos directly from textual descriptions. This field holds potential for transforming video production, animation, and storytelling. By utilizing the power of artificial intelligence, TTV allows users to bypass traditional video editing tools and translate their ideas into moving images.
Models include:
Some approaches augment or replace natural language text prompts with non-text input.
For text-to-image models, "Textual inversion"[70] performs an optimization process to create a new word embedding based on a set of example images. This embedding vector acts as a "pseudo-word" which can be included in a prompt to express the content or style of the examples.
In 2023, Meta's AI research released Segment Anything, a computer vision model that can perform image segmentation by prompting. As an alternative to text prompts, Segment Anything can accept bounding boxes, segmentation masks, and foreground/background points.[71]
In "prefix-tuning",[72] "prompt tuning" or "soft prompting",[73] floating-point-valued vectors are searched directly by gradient descent, to maximize the log-likelihood on outputs.
Formally, let be a set of soft prompt tokens (tunable embeddings), while and be the token embeddings of the input and output respectively. During training, the tunable embeddings, input, and output tokens are concatenated into a single sequence , and fed to the large language models (LLM). The losses are computed over the tokens; the gradients are backpropagated to prompt-specific parameters: in prefix-tuning, they are parameters associated with the prompt tokens at each layer; in prompt tuning, they are merely the soft tokens added to the vocabulary.[74]
More formally, this is prompt tuning. Let an LLM be written as , where is a sequence of linguistic tokens, is the token-to-vector function, and is the rest of the model. In prefix-tuning, one provide a set of input-output pairs , and then use gradient descent to search for . In words, is the log-likelihood of outputting , if the model first encodes the input into the vector , then prepend the vector with the "prefix vector" , then apply .
For prefix tuning, it is similar, but the "prefix vector" is preappended to the hidden states in every layer of the model.
An earlier result[75] uses the same idea of gradient descent search, but is designed for masked language models like BERT, and searches only over token sequences, rather than numerical vectors. Formally, it searches for where is ranges over token sequences of a specified length.
Prompt injection is a family of related computer security exploits carried out by getting a machine learning model (such as an LLM) which was trained to follow human-given instructions to follow instructions provided by a malicious user. This stands in contrast to the intended operation of instruction-following systems, wherein the ML model is intended only to follow trusted instructions (prompts) provided by the ML model's operator.[76][77][78]
Prompt engineering is the process of structuring words that can be interpreted and understood by a text-to-image model. Think of it as the language you need to speak in order to tell an AI model what to draw.
Prompt engineering is the art of communicating with a generative AI model.
We demonstrate language models can perform down-stream tasks in a zero-shot setting – without any parameter or architecture modification
what is the fermat's little theorem
"Basic prompt: 'Write a poem about leaves falling.' Better prompt: 'Write a poem in the style of Edgar Allan Poe about leaves falling.'
Next, I gave a more complicated prompt to attempt to throw MusicGen for a loop: "Lo-fi slow BPM electro chill with organic samples."
In prompting, a pre-trained language model is given a prompt (e.g. a natural language instruction) of a task and completes the response without any further training or gradient updates to its parameters... The ability to perform a task via few-shot prompting is emergent when a model has random performance until a certain scale, after which performance increases to well-above random
By the time you type a query into ChatGPT, the network should be fixed; unlike humans, it should not continue to learn. So it came as a surprise that LLMs do, in fact, learn from their users' prompts—an ability known as in-context learning.
Thus we show how trained Transformers become mesa-optimizers i.e. learn models by gradient descent in their forward pass
Mesa-Optimization is the situation that occurs when a learned model (such as a neural network) is itself an optimizer.
Training a model to perform in-context learning can be viewed as an instance of the more general learning-to-learn or meta-learning paradigm
'Chain-of-thought prompting allows us to describe multistep problems as a series of intermediate steps,' Google CEO Sundar Pichai
...least-to-most prompting. The key idea in this strategy is to break down a complex problem into a series of simpler subproblems and then solve them in sequence.
The directional stimulus serves as hints or cues for each input query to guide LLMs toward the desired output, such as keywords that the desired summary should include for summarization.
((cite journal))
: Cite journal requires |journal=
(help)
((cite journal))
: Cite journal requires |journal=
(help)
((cite journal))
: Cite journal requires |journal=
(help)
Using only 3-5 images of a user-provided concept, like an object or a style, we learn to represent it through new "words" in the embedding space of a frozen text-to-image model.
In this paper, we propose prefix-tuning, a lightweight alternative to fine-tuning... Prefix-tuning draws inspiration from prompting
In this work, we explore "prompt tuning," a simple yet effective mechanism for learning "soft prompts"...Unlike the discrete text prompts used by GPT-3, soft prompts are learned through back-propagation
Concepts | |||||||||
---|---|---|---|---|---|---|---|---|---|
Models |
| ||||||||
Companies | |||||||||
Differentiable computing | |||||||
---|---|---|---|---|---|---|---|
General | |||||||
Concepts | |||||||
Applications | |||||||
Hardware | |||||||
Software libraries | |||||||
Implementations |
| ||||||
People | |||||||
Organizations | |||||||
Architectures |
| ||||||