ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Prompt Engineering Guide (1/2)
    Research/NLP_reference 2024. 7. 24. 10:34

    https://www.promptingguide.ai/

    https://github.com/dair-ai/Prompt-Engineering-Guide

    https://help.openai.com/en/articles/6654000-best-practices-for-prompt-engineering-with-the-openai-api


     

    Prompt engineering is a relatively new discipline for developing and optimizing prompts to efficiently use language models (LMs) for a wide variety of applications and research topics. Prompt engineering skills help to better understand the capabilities and limitations of large language models (LLMs).

     

    Researchers use prompt engineering to improve 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.

     

    Prompt engineering is not just about designing and developing prompts. It encompasses a wide range of skills and techniques that are useful for interacting and developing with LLMs. It's an important skill to interface, build with, and understand capabilities of LLMs. You can use prompt engineering to improve safety of LLMs and build new capabilities like augmenting LLMs with domain knowledge and external tools.

     

    Motivated by the high interest in developing with LLMs, we have created this new prompt engineering guide that contains all the latest papers, advanced prompting techniques, learning guides, model-specific prompting guides, lectures, references, new LLM capabilities, and tools related to prompt engineering.


    Introduction

    LLM Settings

    When designing and testing prompts, you typically interact with the LLM via an API. You can configure a few parameters to get different results for your prompts. Tweaking these settings are important to improve reliability and desirability of responses and it takes a bit of experimentation to figure out the proper settings for your use cases. Below are the common settings you will come across when using different LLM providers:

     

    Temperature - In short, the lower the temperature, the more deterministic the results in the sense that the highest probable next token is always picked. Increasing temperature could lead to more randomness, which encourages more diverse or creative outputs. You are essentially increasing the weights of the other possible tokens. In terms of application, you might want to use a lower temperature value for tasks like fact-based QA to encourage more factual and concise responses. For poem generation or other creative tasks, it might be beneficial to increase the temperature value.

     

    Top P - A sampling technique with temperature, called nucleus sampling, where you can control how deterministic the model is. If you are looking for exact and factual answers keep this low. If you are looking for more diverse responses, increase to a higher value. If you use Top P it means that only the tokens comprising the top_p probability mass are considered for responses, so a low top_p value selects the most confident responses. This means that a high top_p value will enable the model to look at more possible words, including less likely ones, leading to more diverse outputs.

     

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

     

    Max Length - You can manage the number of tokens the model generates by adjusting the max length. Specifying a max length helps you prevent long or irrelevant responses and control costs.

     

    Stop Sequences - A stop sequence is a string that stops the model from generating tokens. Specifying stop sequences is another way to control the length and structure of the model's response. For example, you can tell the model to generate lists that have no more than 10 items by adding "11" as a stop sequence.

     

    Frequency Penalty - The frequency penalty applies a penalty on the next token proportional to how many times that token already appeared in the response and prompt. The higher the frequency penalty, the less likely a word will appear again. This setting reduces the repetition of words in the model's response by giving tokens that appear more a higher penalty.

     

    Presence Penalty - The presence penalty also applies a penalty on repeated tokens but, unlike the frequency penalty, the penalty is the same for all repeated tokens. A token that appears twice and a token that appears 10 times are penalized the same. This setting prevents the model from repeating phrases too often in its response. If you want the model to generate diverse or creative text, you might want to use a higher presence penalty. Or, if you need the model to stay focused, try using a lower presence penalty.

     

    Similar to temperature and top_p, the general recommendation is to alter the frequency or presence penalty but not both.

     

    Before starting with some basic examples, keep in mind that your results may vary depending on the version of LLM you use.


    Basics of Prompting

    You can achieve a lot with simple prompts, but the quality of results depends on how much information you provide it and how well-crafted the prompt is. 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.

     

    Something to note is that when using the OpenAI chat models like gpt-3.5-turbo or gpt-4, you can structure your prompt using three different roles: system, user, and assistant. The system message is not required but helps to set the overall behavior of the assistant.

     

    The assistant message in the example above corresponds to the model response. You can also define an assistant message to pass examples of the desired behavior you want. 

     

    You can observe from the prompt example above that the language model responds with a sequence of tokens that make sense given the context "The sky is". The output might be unexpected or far from the task you want to accomplish. In fact, this basic example highlights the necessity to provide more context or instructions on what specifically you want to achieve with the system. This is what prompt engineering is all about.

     

    Is that better? Well, with the prompt above you are instructing the model to complete the sentence so the result looks a lot better as it follows exactly what you told it to do ("complete the sentence"). This approach of designing effective prompts to instruct the model to perform a desired task is what's referred to as prompt engineering.

     

    The example above is a basic illustration of what's possible with LLMs today. Today's LLMs are able to perform all kinds of advanced tasks that range from text summarization to mathematical reasoning to code generation.

    Prompt Formatting

    You have tried a very simple prompt above. A standard prompt has the following format:

    <Question>?

    or

    <Instruction>

     

    You can format this into a question answering (QA) format, which is standard in a lot of QA datasets, as follows:

    Q: <Question>?

    A: 

     

    When prompting like the above, it's also referred to as zero-shot prompting, i.e., you are directly prompting the model for a response without any examples or demonstrations about the task you want it to achieve. Some large language models have the ability to perform zero-shot prompting but it depends on the complexity and knowledge of the task at hand and the tasks the model was trained to perform good on.

     

    A concrete prompt example is as follows:

    Prompt

    Q: What is prompt engineering?

     

    With some of the more recent models you can skip the "Q:" part as it is implied and understood by the model as a question answering task based on how the sequence is composed. In other words, the prompt could be simplified as follows:

    Prompt

    What is prompt engineering?

     

    Given the standard format above, one popular and effective technique to prompting is referred to as few-shot prompting where you provide exemplars (i.e., demonstrations). You can format few-shot prompts as follows:

    <Question>?

    <Answer>

    <Question>?

    <Answer>

    <Question>?

    <Answer>

    <Question>?

     

    The QA format version would look like this:

    Q: <Question>?

    A: <Answer>

    Q: <Question>?

    A: <Answer>

    Q: <Question>?

    A: <Answer>

    Q: <Question>?

    A:

     

    Keep in mind that it's not required to use the QA format. The prompt format depends on the task at hand. For instance, you can perform a simple classification task and give exemplars that demonstrate the task as follows:

     

    Prompt:

    This is awesome! // Positive

    This is bad! // Negative

    Wow that movie was rad! // Positive

    What a horrible show! //

     

    Output

    Negative

     

    Few-shot prompts enable in-context learning, which is the ability of language models to learn tasks given a few demonstrations.


    Prompt Elements

    A prompt 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 to find a response for

     

    Output Indicator - the type or format of the output.

     

    To demonstrate the prompt elements better, here is a simple prompt that aims to perform a text classification task:

     

    Prompt

    Classify the text into neutral, negative, or positive

    Text: I think the food was okay.

    Sentiment:

     

    In the prompt example above, the instruction correspond to the classification task, "Classify the text into neutral, negative, or positive". The input data corresponds to the "I think the food was okay." part, and the output indicator used is "Sentiment:". Note that this basic example doesn't use context but this can also be provided as part of the prompt. For instance, the context for this text classification prompt can be additional examples provided as part of the prompt to help the model better understand the task and steer the type of outputs that you expect.

     

    You do not need all the four elements for a prompt and the format depends on the task at hand. 


    General Tips for Designing Prompts

    Here are some tips to keep in mind while you are designing your prompts:


    Start Simple

    As you get started with designing prompts, you should keep in mind that it is really an iterative process that requires a lot of experimentation to get optimal results. Using a simple playground from OpenAI or Cohere is a good starting point.

     

    You can start with simple prompts and keep adding more elements and context as you aim for better results. Iterating your prompt along the way is vital for this reason. As you read the guide, you will see many examples where specificity, simplicity, and conciseness will often give you better results.

     

    When you have a big task that involves many different subtasks, you can try to break down the task into simpler subtasks and keep building up as you get better results. This avoids adding too much complexity to the prompt design process at the beginning.


    The Instruction

    You can design effective prompts for various simple tasks by using commands to instruct the model what you want to achieve, such as "Write", "Classify", "Summarize", "Translate", "Order", etc.

     

    Keep in mind that you also need to experiment a lot to see what works best. Try different instructions with different keywords, contexts, and data and see what works best for your particular use case and task. Usually, the more specific and relevant the context is to the task you are trying to perform, the better. We will touch on the importance of sampling and adding more context in the upcoming guides.

     

    Others recommend that you place instructions at the beginning of the prompt. Another recommendation is to use some clear separator like "###" to separate the instruction and context.

     

    For instance:

    ### Instruction ###

    Translate the text below to Spanish:

    Text: "hello!"

     

    Output:

    ¡Hola!


    Specificity

    Be very specific about the instruction and task you want the model to perform. The more descriptive and detailed the prompt is, the better the results. This is particularly important when you have a desired outcome or style of generation you are seeking. There aren't specific tokens or keywords that lead to better results. It's more important to have a good format and descriptive prompt. In fact, providing examples in the prompt is very effective to get desired output in specific formats.

     

    When designing prompts, you should also keep in mind the length of the prompt as there are limitations regarding how long the prompt can be. Thinking about how specific and detailed you should be. Including too many unnecessary details is not necessarily a good approach. The details should be relevant and contribute to the task at hand. This is something you will need to experiment with a lot. We encourage a lot of experimentation and iteration to optimize prompts for your applications.

     

    As an example, let's try a simple prompt to extract specific information from a piece of text.

     

    Prompt:

    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.“"

     

    Output:

    Place: Champlimaud Centre for the Unknown, Lisbon


    Avoid Impreciseness

    Given the tips above about being detailed and improving format, it's easy to fall into the trap of wanting to be too clever about prompts and potentially creating imprecise descriptions. It's often better to be specific and direct. The analogy here is very similar to effective communication -- the more direct, the more effective the message gets across.

     

    For example, you might be interested in learning the concept of prompt engineering. You might try something like:

     

    Explain the concept prompt engineering. Keep the explanation short, only a few sentences, and don't be too descriptive.

     

    It's not clear from the prompt above how many sentences to use and what style. You might still somewhat get good responses with the above prompts but the better prompt would be one that is very specific, concise, and to the point. Something like:

     

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


    To do or not to do?

    Another common tip when designing prompts is to avoid saying what not to do but say what to do instead. This encourages more specificity and focuses on the details that lead to good responses from the model.

     

    Here is an example of a movie recommendation chatbot failing at exactly what I don't want it to do because of how I wrote the instruction -- focusing on what not to do.

     

    Prompt:

    The following is an agent that recommends movies to a customer. DO NOT ASK FOR INTERESTS. DO NOT ASK FOR PERSONAL INFORMATION.

    Customer: Please recommend a movie based on my interests.

    Agent:

     

    Output:

    Sure, I can recommend a movie based on your interests. What kind of movie would you like to watch? Do you prefer action, comedy, romance, or something else?

     

    Here is a better prompt:

     

    Prompt:

    The following is an agent that recommends movies to a customer. The agent is responsible to recommend a movie from the top global trending movies. It should refrain from asking users for their preferences and avoid asking for personal information. If the agent doesn't have a movie to recommend, it should respond "Sorry, couldn't find a movie to recommend today.".

    Customer: Please recommend a movie based on my interests.

    Agent:

     

    Output:

    Sorry, I don't have any information about your interests. However, here's a list of the top global trending movies right now: [list of movies]. I hope you find something you like!


    Examples of Prompts

    Text Summarization

    One of the standard tasks in natural language generation is text summarization. Text summarization can include many different flavors and domains. In fact, one of the most promising applications of language models is the ability to summarize articles and concepts into quick and easy-to-read summaries. Let's try a basic summarization task using prompts.

     

    Let's say you are interested to learn about antibiotics, you could try a prompt like this:

     

    Prompt:

    Explain antibiotics

    A:

     

    Output:

    Antibiotics are a type of medication used to treat bacterial infections. They work by either killing the bacteria or preventing them from reproducing, allowing the body’s immune system to fight off the infection. Antibiotics are usually taken orally in the form of pills, capsules, or liquid solutions, or sometimes administered intravenously. They are not effective against viral infections, and using them inappropriately can lead to antibiotic resistance.

     

    The "A:" is an explicit prompt format that you use in question answering. You used it here to tell the model that there is an answer expected further. In this example, it's not clear how this is useful vs not using it but we will leave it that for later examples. Let's just assume that this is too much information and you want to summarize it further. In fact, you can instruct the model to summarize into one sentence like so:

     

    Prompt:

    Explain the above in one sentence:

     

    Output:

    Antibiotics are medications used to treat bacterial infections by either killing the bacteria or stopping them from reproducing, but they are not effective against viruses and overuse can lead to antibiotic resistance.

     

    Without paying too much attention to the accuracy of the output above, which is something we will touch on in a later guide, the model tried to summarize the paragraph in one sentence. You can get clever with the instructions but we will leave that for a later chapter. Feel free to pause here and experiment to see if you get better results.


    Information Extraction

    While language models are trained to perform natural language generation and related tasks, it's also very capable of performing classification and a range of other natural language processing (NLP) tasks.

     

    Here is an example of a prompt that extracts information from a given paragraph.

     

    Prompt:

    Author-contribution statements and acknowledgements in research papers should state clearly and specifically whether, and to what extent, the authors used AI technologies such as ChatGPT in the preparation of their manuscript and analysis. They should also indicate which LLMs were used. This will alert editors and reviewers to scrutinize manuscripts more carefully for potential biases, inaccuracies and improper source crediting. Likewise, scientific journals should be transparent about their use of LLMs, for example when selecting submitted manuscripts.

    Mention the large language model based product mentioned in the paragraph above:

     

    Output:

    The large language model based product mentioned in the paragraph above is ChatGPT.

     

    There are many ways you can improve the results above, but this is already very useful.

     

    By now it should be obvious that you can ask the model to perform different tasks by simply instructing it what to do. That's a powerful capability that AI product developers are already using to build powerful products and experiences.

     

    Paragraph source: ChatGPT: five priorities for research


    Question Answering

    One of the best ways to get the model to respond with specific answers is to improve the format of the prompt. As covered before, a prompt could combine instructions, context, input, and output indicators to get improved results. While these components are not required, it becomes a good practice as the more specific you are with instruction, the better results you will get. Below is an example of how this would look following a more structured prompt.

     

    Prompt:

    Answer the question based on the context below. Keep the answer short and concise. Respond "Unsure about answer" if not sure about the answer.

    Context: Teplizumab traces its roots to a New Jersey drug company called Ortho Pharmaceutical. There, scientists generated an early version of the antibody, dubbed OKT3. Originally sourced from mice, the molecule was able to bind to the surface of T cells and limit their cell-killing potential. In 1986, it was approved to help prevent organ rejection after kidney transplants, making it the first therapeutic antibody allowed for human use.

    Question: What was OKT3 originally sourced from?

    Answer:

     

    Output:

    Mice.


    Text Classification

    So far, you have used simple instructions to perform a task. As a prompt engineer, you need to get better at providing better instructions. But that's not all! You will also find that for harder use cases, just providing instructions won't be enough. This is where you need to think more about the context and the different elements you can use in a prompt. Other elements you can provide are input data or examples.

     

    Let's try to demonstrate this by providing an example of text classification.

     

    Prompt:

    Classify the text into neutral, negative or positive.

    Text: I think the food was okay.

    Sentiment:

     

    Output:

    Neutral

     

    You gave the instruction to classify the text and the model responded with 'Neutral', which is correct. Nothing is wrong with this but let's say that what you really need is for the model to give the label in the exact format you want. So instead of Neutral, you want it to return neutral. How do you achieve this? There are different ways to do this. You care about specificity here, so the more information you can provide the prompt, the better results. You can try providing examples to specify the correct behavior. Let's try again:

     

    Prompt:

    Classify the text into neutral, negative or positive.

    Text: I think the vacation is okay.

    Sentiment: neutral

    Text: I think the food was okay.

    Sentiment:

     

    Output:

    neutral

     

    Perfect! This time the model returned neutral which is the specific label you were looking for. It seems that the example provided in the prompt helped the model to be specific in its output.

     

    To highlight why sometimes being specific is important, check out the example below and spot the problem:

     

    Prompt:

    Classify the text into nutral, negative or positive.

    Text: I think the vacation is okay.

    Sentiment:

     

    Output:

    Neutral

     

    What is the problem here? As a hint, the made-up nutral label is completely ignored by the model. Instead, the model outputs Neutral as it has some bias towards that label. But let's assume that what you really want is nutral. How would you fix this? Maybe you can try adding descriptions to the labels or add more examples to the prompt? If you are not sure, we will discuss a few ideas in the upcoming sections.


    Conversation

    Perhaps one of the more interesting things you can achieve with prompt engineering is instructing the LLM system on how to behave, its intent, and its identity. This is particularly useful when you are building conversational systems like customer service chatbots.

     

    For instance, let's create a conversational system that's able to generate more technical and scientific responses to questions. Note how you are explicitly telling it how to behave through the instruction. This is sometimes referred to as role prompting.

     

    Prompt:

    The following is a conversation with an AI research assistant. The assistant tone is technical and scientific.

    Human: Hello, who are you?

    AI: Greeting! I am an AI research assistant. How can I help you today?

    Human: Can you tell me about the creation of blackholes?

    AI: 

     

    Output:

    Sure! Black holes are regions of spacetime where the gravitational force is so strong that nothing, not even light, can escape from it. They are created when a very massive star dies and its core collapses in on itself, forming a singularity of infinite density. The intense gravity of the singularity pulls in all the matter and radiation around it, creating the black hole.

     

    Our AI research assistant sounds a bit too technical, right? Okay, let's change this behavior and instruct the system to give more accessible answers.

     

    Prompt:

    The following is a conversation with an AI research assistant. The assistant answers should be easy to understand even by primary school students.

    Human: Hello, who are you?

    AI: Greeting! I am an AI research assistant. How can I help you today?

    Human: Can you tell me about the creation of black holes?

    AI:

     

    Output:

    Sure! Black holes are created when a very massive star runs out of fuel and collapses in on itself. The gravity of the star is so strong that nothing, not even light, can escape its pull. This creates a point in space-time with an intense gravitational force called a black hole.

     

    You made some progress. You can continue improving it. If you add more examples, you might get even better results.


    Code Generation

    One application where LLMs are quite effective is code generation. Copilot is a great example of this. There are a vast number of code-generation tasks you can perform with clever prompts. Let's look at a few examples below.

     

    First, let's try a simple program that greets the user.

     

    Prompt:

    /*

    Ask the user for their name and say "Hello"

    */

     

    Output:

    let name = prompt("What is your name?");

    console.log(`Hello , ${name}!`);

     

    Notice that you didn't even need to specify the language to use.

     

    Let's switch levels a bit. The example below shows how powerful LLMs can be with a little more effort in designing the prompts.

     

    Prompt:

    """

    Table departments, columns = [DepartmentId, DepartmentName]

    Table students, columns = [DepartmentId, StudentId, StudentName]

    Create a MySQL query for all students in the Computer Science Department

    """

     

    Output:

    SELECT StudentId, StudentName

    FROM students

    WHERE DepartmentId IN (SELECT DepartmentId FROM departments WHERE DepartmentName = 'Computer Science');

     

    This is very impressive. In this case, you provided data about the database schema and asked it to generate a valid MySQL query.


    Reasoning

    Perhaps one of the most difficult tasks for an LLM today is one that requires some form of reasoning. Reasoning is one of most interesting areas due to the types of complex applications that can emerge from LLMs.

     

    There have been some improvements in tasks involving mathematical capabilities. That said, it's important to note that current LLMs struggle to perform reasoning tasks so this requires even more advanced prompt engineering techniques. We will cover these advanced techniques in the next guide. For now, we will cover a few basic examples to show arithmetic capabilities.

     

    Prompt:

    What is 9,000 * 9,000?

     

    Output:

    81,000,000

     

    Let's try something more difficult.

     

    Prompt:

    The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1.

    A:

     

    Output:

    No, the odd numbers in this group add up to an odd number: 119.

     

    That's incorrect! Let's try to improve this by improving the prompt.

     

    Prompt:

    The odd numbers in this group add up to an even number: 15, 32, 5, 13, 82, 7, 1. Solve by breaking the problem into steps. First, identify the odd numbers, add them, and indicate whether the result is odd or even.

     

    Output:

    Odd numbers: 15, 5, 13, 7, 1Sum: 41 41 is an odd number.

     

    Much better, right? By the way, we tried this task a couple of times and the model sometimes fails. If you provide better instructions combined with examples, it might help get more accurate results.

     

    In the upcoming section, we will cover even more advanced prompt engineering concepts and techniques for improving performance on all these and more difficult tasks.


     

    'Research > NLP_reference' 카테고리의 다른 글

    RoPE  (0) 2024.08.27
    Prompt Engineering Guide (2/2)  (0) 2024.07.24
    Subword Tokenizer  (1) 2024.04.10
    Word Embedding  (1) 2024.04.10
    Vector Similarity (벡터의 유사도)  (0) 2024.04.10
Designed by Tistory.