Prompt Engineering — What, Why and Best Practices

Dhanasree Rajamani
4 min readApr 23, 2023

--

Prompt engineering is an emerging field which aims to improve the performance, accuracy and efficiency of Natural Language Processing models by designing and optimizing prompts or input sequences. It has emerged as a powerful technique for fine-tuning language models. Recently, there has been a significant increase in research and development efforts in this area.

Prompt engineering involves designing prompts or input text that guides the output of a language model to perform a specific task. There are several types of prompts such as

Context prompts provide the model with a starting point for generating text.

Style prompts guide the model to generate text in a specific tone or style.

Topic prompts are used to generate text on a specific topic.

Prompt engineering involves designing and fine-tuning of input sequences or prompts for a particular NLP task, through various techniques such as keyword-based prompts, templates and natural language prompts. The goal is to find the most effective prompt that optimizes the model’s performance for the task.

The initial prompt is a critical factor in prompt engineering, as it serves as the starting point for prompt optimization, where it is refined and fine-tuned to improve the model’s performance. A good initial prompt should contain enough information about the task, be concise and have a clear objective.

There are several approaches to prompt engineering such as template-based, keyword-based and natural language-based prompts.

Template based prompts involve creating structured templates with placeholders for relevant information.

Keyword-based prompts use specific keywords to guide the model’s output.

Natural Language-based prompts are free-form text that provides more flexibility to the model to generate output.

Some important benefits of prompt engineering are that it can improve the quality and flexibility of language models. Providing targeted prompts can help fine-tune models to perform specific tasks such as text summarization or generating product descriptions. Prompt engineering can also help to mitigate bias in language models — which occurs due to imbalanced datasets. This can be eliminated with carefully designed prompts.

Basic Prompting

Zero-shot and few-shot learning are two most basic approaches for prompting the model. Zero-shot learning is to simply feed the task text to the model and ask for results. Few-shot learning presents a set of demonstrations with both input and desired output, on the target task. It can lead to better performance than zero-shot as it can understand human intention and criteria for what kinds of answers are wanted.

Instruction Prompting

Instructed LM finetunes a pretrained model with high-quality tuples to make LM better understand user intention and follow instructions. In-context instruction learning combines few-shot learning with instruction prompting.

Chain-Of-Thought Prompting

Chain of Thought prompting generates a sequence of short sentences to describe reasoning logics step by step — known as reasoning chains or rationales — eventually leading to the final answer.

Best Practices in Prompt Engineering

  • Self-consistency sampling improves reasoning accuracy by sampling a number of diverse answers and then taking the majority vote.
  • Ensemble learning alters the example order to use model generated rationales to introduce randomness, then aggregate model outputs with majority vote.
  • The STaR method involves generating reasoning chains with LLM, keeping only those leading to correct answers, and fine-tuning with generated rationales.
  • Prompts with demonstrations of higher reasoning complexity can achieve better performance. (complexity — based on number of reasoning steps)
  • Complexity-based consistency prefers complex reasoning chains by taking the majority vote among the top complex chains.
  • Chain Of Thought prompts with only complex examples can improve accuracy for complex questions but performs poorly for simple questions.
  • Changing Q to Question is helpful.
  • Including explanations in prompts has small to moderate benefits for NLP tasks involving reasoning over text, but effects vary by models and nonfactual explanations may lead to incorrect predictions.
  • Self-Ask, IRCoT, and ReAct combine iterative CoT prompting with queries to search for relevant content and entities.

In addition, it is important to use clear and specific prompts with enough context, usage of multiple prompts and examples. It is also important for humans to provide feedback on the quality of the prompts generated by the language models. These best practices in prompt engineering improve the quality and effectiveness of the language models.

Prompt engineering has a wide range of applications — natural language generation, machine translation, chatbots and so on. It also has some limitations such as overfitting. It is important to carefully design prompts to avoid overfitting and ensure generalizability. Prompt engineering is an exciting field with immense potential to improve the performance of NLP models. As NLP models continue to become more critical in various applications, prompt engineering is expected to become an essential aspect of NLP research and development.

References

Prompt Engineering | Lil’Log (lilianweng.github.io)

https://www.youtube.com/watch?v=dOxUroR57xs

https://www.youtube.com/watch?v=MLR4pIKZHI8

--

--