⚽️Introduction to Prompt Engineering

Jul 15, 2024 · 4 min read

Basic Knowledge

Prompt engineering is a relatively new discipline for developing and optimizing prompts to efficiently use large lange models (LLMs) for a wide variety of applications and research topics. Researchers use prompt engineering to improve the safety and the capacity of LLMs on a wide range of common and complex tasks such as question answering and arithmetic reasoning. Developers use prompt engineering to design robust and effective prompting techniques that interface with LLMs and other tools.

When designing and testing prompts, we typically interact with the LLMs via an API. Some paprameters will influence the performance of the model, below are some common settings that will come across when using LLMs:

  • Temperature - This parameter controls the degree of randomness in the model’s output. A higher temperature will result in more random outputs, while a lower temperature will result in more repetitive outputs.

  • Top P - This parameter controls the sampling strategy used by the model when generating multiple possible outputs. A higher value of Top P will result in more diverse outputs, while a lower value will result in more repetitive outputs.

The general recommendation is to alter temperature or Top P but not both.

  • Max Length - This parameter controls the number of tokens the model generates in its output. Using a typical max length will help to prevent long or irrelevant responses and control costs.

  • Stop Sequences - This parameter is a string that stops the model from generating tokens. This is another way to control the length and structure of the model’s response.

  • Frequency Penalty - This parameter will add a penalty to the same tokens in the model’s output when it appears, which can help to prevent a repetition of this word. Higher values of frequency penalty will reduce the repetition of tokens in the model’s output.

  • Presence Penalty - This parameter will set a penalty to the appeared word (Not add more penalty when the word appears more than once), which can reduce the repetition of the same word in the model’s output. Higher values of presence penalty will reduce the repetition of the same word in the model’s output.

Prompts

What is a prompt?

Prompts are used to guide the model to generate the correct outputs. A prompt can contain information like the instruction or question you are passing to the model and include other details such as context, inputs, or examples. You can use these elements to instruct the model more effectively to improve the quality of results.

When using GPT3.5 or GPT4, the prompt can be structured in three different roles: system, user, and assistant.

  • System - Setting the behavior, character, and background of the models.
  • User - Asking the question or providing the context.
  • Assistant - Automatically generated by the System and user prompts.

Normally we only use user prompts to interact with the model.

Prompt Format

The prompt can be formatted with different structures divided into zero-shot or few-shot. Zero-shot prompting means it can directly prompt the model for a response without any examples or demonstrations of the task. One typical zero-shot example is the Question/Answering (QA) format:

Q: <Question>?
A: <Answer>
Q: <Question>?
A: <Answer>
Q: <Question>?
A: 

Different formats of prompts can be used in various tasks, such as classification prompts:

This is awesome! // Positive
This is bad! // Negative
Wow, that movie was rad! // Positive
What a horrible show! //

Output:

Negative

This format is a few-shot that can enable in-context learning.

Prompt Elements

One typical prompt often contains any of the following elements:

  • Instruction - a specific task or instruction you want the model to perform
  • Context - external information or additional context that can steer the model to better responses
  • Input Data - the input or question that we are interested in finding a response for
  • Output Indicator - the type or format of the output

Designing Tips

  • Start Simple

    In the beginning, use simple models and prompts, and iterate continuously to meet the requirements.

    When there is a large task, try to break it down into multiple subtasks.

  • Obvious Instruction

    Giving clear instructions when designing the prompt can help to improve the output performance.

  • Specificity

    More detailed requirements can help the model output meet the requirements.

    Extract the name of places in the following text. 
    
    Desired format:
    Place: <comma_separated_list_of_places>
    
    Input: "Although these developments are encouraging to researchers, much is still a mystery. “We often have a black box between the brain and the effect we see in the periphery,” says Henrique Veiga-Fernandes, a neuroimmunologist at the Champalimaud Centre for the Unknown in Lisbon. “If we want to use it in the therapeutic context, we actually need to understand the mechanism."
    
  • Avoid Imprecisseness

    Use the most straightforward language to communicate.

    Use 2-3 sentences to explain the concept of prompt engineering to a high school student.
    
  • Say Do What

    Avoid saying what not to do but say what to do instead