Guide to Prompt Engineering for Large Language Models

Guide to Prompt Engineering for Large Language Models

By Albert Mao

Feb 11, 2024

This guide summarizes key prompt engineering strategies and offers practical explanations and prompt examples, helping increase the efficiency and accuracy of LLM output.

As evidenced by practice and research, the input provided to large language models, known as prompts, can have a notable influence on the model's responses. In particular, the studies show that the phrasing of the prompts has a direct influence on the model's behavior and output. Keep reading to find out more about key prompt engineering techniques and prompt examples to steer AI in the right direction.

Assigning AI Chatbot a Role

While even AI scientists don't know exactly what is happening inside neural networks, on the face of it, large language models work by sequence prediction. This means that  LLMs use the text you give it as a prompt to predict the next token. 

This conclusion has multiple implications. For example, it was found that assigning AI chatbots a specific role or character can help the model align its output with user intent.

Example prompt:

You are a microbiologist focusing on gut bacteria. Describe the role of microbiota in our immune system. Explain in the freshman's language. 

The above prompt assigns the model the role of the microbiologist while asking it to provide the explanation that is intended for first-year students. Changing the character to a school teacher or nutritionist or changing the tone of voice, for example asking the model to generate text for high schoolers or senior academicians, would result in different output.

Use Specific and Concise Prompts for Better Responses

This principle is based on the same notion about LLMs predicting the next token based on the preceding sequence. By feeding an LLM more specific prompts, for example, specifying the context, the length and format of the output and other parameters, the user can better align LLM responses to the task.

Example prompt:

What is a "temperature" in the context of large language models, and what does it affect? Provide an explanation in a bulleted list, one sentence per bullet.

Minimize AI Hallucinations

So far, the researchers have various views about why large language models sometimes produce apparently plausible but actually incorrect responses known as hallucinations. There are multiple methods to reduce hallucinations through prompting, as described in one of our earlier articles.

Meanwhile, one of the most straightforward ways to prevent tAI chatbots from hallucinations and creating made-up responses is to allow the model to admit that it doesn't know the answer when it actually lacks knowledge or cannot make a well-informed guess about the subject.

Example prompt:

Answer the question about X. If you don't know the answer, say you don't know.

Prompting Strategy for Better Summarization

As large language models continue to develop, their max tokens parameter, aka context window, keeps expanding, allowing the model to process and summarize longer documents. Meanwhile, when summarizing extensive datasets, AI chatbots may deliver chatty, lengthy or irrelevant responses. 

One of the methods to nudge the model into more concise and up-to-point summarizing is to let it run through a 2-stage process where an LLM "thinks" or collects all information it considers relevant on the 1st stage and zeroes in on its preliminary summary to deliver a final one on the 2d stage.

Example prompt:

Find the exact parts in the {{DOCUMENT}} that are relevant to the user's question and write them inside<thinking> </thinking> XML tags. This is a space for you to write down relevant content that will not be shown to the user. 

Once you are done thinking, answer the question. Put your answer inside <answer><./answer> XML tags.

Demonstrate Examples with Few-Shot Prompting

In cases where an LLM can potentially struggle to understand the task due to lack of context or ambiguity, it can be helped with custom-made demonstrations to showcase the reasoning path. Known as few-shot prompting, this technique involves providing an LLM with examples of reasoning that condition the model for the final question.

Example prompt:

Classify the text into neutral, negative or positive

Text: This is awesome!

Sentiment: Positive.

Text: This is bad.

Sentiment: Negative.

Input text: I think the vacation is ok.


In the above sample prompt, an LLM is provided with instructions and several demonstrations (shots) from which it can learn. When provided with examples, the model learns the pattern for classification based on the context, building its capability of producing accurate classifications.

Elicit Reasoning with Chain-of-Thought

While large language models still experience difficulties with arithmetic, commonsense or symbolic reasoning, several studies noted that the prompting technique known as Chain-of-Thought significantly improves LLMs' abilities to perform complex reasoning. For example, simply asking the model to articulate its reasoning steps before giving the final answer can significantly increase model performance and ensure accurate results.

Here is a classic example from the study by Kojima et al. [2022], demonstrating how using a simple prompt, "Let's think step by step," invoked the chain-of-thought process with the GTP-3 model, resulting in a correct answer to an arithmetic question where the model previously failed.

Figure 1: Improving the accuracy of an LLM (GPT-3) by initiating a chain-of-thought process by adding a "let's think step by step" prompt. Source: Kojima et al. [2022]

Learn More with VectorShift

Prompt engineering is a powerful method to nudge AI into human-like reasoning and shape its responses. Meanwhile, leveraging AI and prompt engineering for practical applications can be greatly facilitated with the help of no-code platforms and intuitive drag-and-drop solutions like VectorShift. Please don't hesitate to get in touch with our team or request a free demo to learn more about how to deploy AI throughout your company and projects.

© 2023 VectorShift, Inc. All Rights Reserved.

© 2023 VectorShift, Inc. All Rights Reserved.

© 2023 VectorShift, Inc. All Rights Reserved.