IBM Developer

Article

Prompt Engineering Fundamentals

From zero to hero with prompt templates for different types of prompts

By Bhavishya Pandit

Large language models (LLMs) are AI models trained on vast amounts of text data. You give them a question or a task, and they generate text or perform specific tasks based on your input.

Prompt engineering is the practice of crafting clear instructions for LLMs to produce the best results. By using prompt engineering, you can guide the LLM to generate accurate and relevant outputs.

Crafting the perfect prompt

One of the most effective ways to learn how to craft the perfect prompt is to consider the common mistakes people make.

Common mistakes when writing prompts include:

  • Being too vague: Vague prompts lead to vague responses. Be specific about what you need.
  • Grammatical errors: Typos and grammatical errors can confuse the LLM and lead to unexpected results. Review your prompt before submitting.
  • Asking leading questions: Avoid phrasing your prompt in a way that steers the LLM toward a specific response if you want an unbiased answer.

Avoiding these common mistakes might not give you the most refined output. To get the best results, also follow these best practices:

  • Start with a clear objective: Define exactly what you want the LLM to achieve.
  • Provide context: The more context you provide, the better the LLM can understand your request.
  • Use a conversational tone: Address the LLM clearly and directly.
  • Refine and iterate: Experiment and adjust your prompt based on the results you receive.

Prompt templates

You can write a prompt in various ways, but the instructions and intent must always be clear.

A prompt template can include some combination of these elements:

  • Task
  • Goal
  • Action
  • Problem
  • Outcome
  • Role
  • Format
  • Input
  • Steps
  • Expectation
  • Context
  • Elaboration

The following infographic shows a few ways to write prompts that convey the instructions and intent clearly to the LLM. Each template caters to a different scenario and writing style.

Prompt template infographic showing different ways to write prompts for LLMs

Understanding zero-shot, one-shot, and few-shot prompting

Prompts are instructions that you give to an AI model to perform a specific task. These prompts can range from simple requests to complex commands.

The number of examples that you provide in the prompt can significantly influence the model's performance.

A zero-shot prompt is one that does not provide any examples. For example, "Write a poem about a robot who wants to be a chef."

A one-shot prompt is one that provides a single example. For example, "Translate 'Hello, how are you?' into French."

A few-shot prompt is one that provides multiple examples. For example, "Translate the following sentences: 'I like dogs.' 'She is beautiful.'

'They are eating pizza.'"

Why use these different types of prompts?

  • Zero-shot prompts test the model's general knowledge and ability to understand instructions without specific examples.

  • One-shot prompts assess the model's ability to learn from a single example and apply it to new situations.

  • Few-shot prompts evaluate the model's ability to learn from multiple examples and generalize its knowledge to new tasks.

Making LLMs “think”

Using these different types of prompts and different prompting techniques, you can improve LLM responses and reduce ambiguity in model outputs.

Let’s review one of the most popular prompting techniques, the chain-of-thought (CoT) prompting technique. In simpler terms, this means using a few examples to teach the LLM to think out loud and explain its reasoning before giving the final answer. This helps us understand how the LLM arrived at its conclusion and can also help improve its accuracy.

The following is an example of a one-shot chain-of-thought prompt:

Q: Jack has two bags, each containing three apples. How many apples does Jack have in total?
A: One bag contains 3 apples, so two bags contain 3 * 2 = 6 apples.
Q: {QUESTION}
A:

This CoT technique can also be effective in prompt decomposition which means breaking a complex problem (prompt) into simple smaller problems (tasks) using the right set of prompts.

Similarly, prompt aggregation acts like an ensemble module. It is the process of using multiple prompts to solve the same problem, then aggregating these responses into a final output. In many cases, a majority vote selects the most frequent response to generate the final output.

Both prompt decomposition and prompt aggregation techniques help to reduce the variance of LLM outputs and often improve the accuracy of responses, but both techniques come with the cost of increasing the number of model calls needed to reach a final answer.

Summary and next steps

This article introduced you to communicating with large language models (LLMs) by teaching you the fundamentals of prompt engineering. You learned how prompts guide these models, and how to avoid common pitfalls to get the best results. The prompt templates can help kickstart your LLM adventures.

If you're ready to put these prompting techniques into practice, check out the tutorial "Adversarial prompting: Testing and strengthening the security and safety of large language models," which applies prompt engineering concepts to real-world LLM security scenarios.