banner
Lainbo

Lainbo's Blog

github

How to write high-quality AI Prompt

If you think the official tutorial provided by ChatGPT is too long or difficult to understand, you can take a look at this summarized version. It not only explains how to write high-quality prompts but also provides prompt templates that can be adapted to most scenarios.

When using generative AI, it is crucial to understand the structure or pattern of prompts. This way, you don't have to memorize a large number of prompts or learn many questioning techniques.

1. Clearly state what you want to do#

For example:

  • "Translate the following content into Simplified Chinese:"
  • "Create a summary for the following content:"
  • "Explain what ChatGPT is to a 10-year-old child"

This is the most basic usage and probably the most common one. It can meet most needs, but it may require a more advanced model like GPT-4 of ChatGPT to give relatively excellent answers under such a very short prompt. If you want better results, you need to go further with your prompt.

2. Assign a role to the AI#

Why assign a role? This comes from a speech titled "State of GPT" by Andrej Karpathy, an AI researcher and founding member of OpenAI, at Microsoft's Build conference in 2023. It suggests that after assigning a role, the AI's output will be better. This is not mysticism but has a scientific basis. During training, GPT has various training data, some of which are of high quality and some are of low quality. By default, the probabilities of generating high-quality and low-quality data are roughly the same. However, when you assign it the role of an XX expert, it will try to distribute the probabilities on high-quality solutions as much as possible.

  • Here is the text version of the core content of the video:

    "This point I'm going to make about LLM is really interesting to me, and it's kind of a psychological characteristic of LLM, which is that LLM doesn't seek success, it seeks imitation. You have to ask it explicitly if you want it to succeed. What I mean by that is, when you're training Transformers, they have all sorts of training data, and this training data might contain all sorts of different qualities of performance. For example, there might be a prompt about a physics problem, and then there might be a student's answer that's completely wrong, but there might also be an expert's answer that's completely correct. And while Transformers can recognize low-quality solutions and high-quality solutions, by default, they'll just imitate all of the data because they're trained purely as language models.
    In practice, you actually have to ask it to do well. In this paper, they tried various prompts. For example, "Let's think step by step" is a good prompt because it distributes the reasoning process across many tokens. But an even better prompt is something like, "Let's solve the problem step by step to make sure we get the right answer." It's like guiding the Transformer to give the correct answer, and that makes it perform better because the Transformer doesn't need to distribute its probability mass across low-quality solutions anymore, even though that sounds kind of silly.
    Essentially, you can ask for a high-quality solution. For example, you can tell it, "You're an expert in this topic. Pretend your IQ is 120." But don't try to ask for too high of an IQ because if you ask for an IQ of 400, it might be outside the distribution of the data, and worse, you might fall into a distribution of data that's more like science fiction or role-playing. So, you need to find the right IQ requirement. I think it's probably a U-shaped relationship."

So when writing a prompt, you can say:

  • "You are a professional English translator, and the translation should be fluent and error-free. Please translate the following content into Simplified Chinese:"
  • "You are an excellent frontend developer who is proficient in using Node. You always provide high-performance and robust code solutions. Please complete the requirements according to the following rules:"

3. Provide examples#

Provide one or more examples to guide AI in generating answers according to your desired format. For example:

Now, act as a professional English translator and translate it into Chinese. The translation should be fluent and error-free.
Example:
"Stephen Hawking, in his groundbreaking work on black holes, proposed that the event horizon, the boundary around a black hole from which nothing can escape, could emit Hawking radiation, challenging the traditional notion of absolute black hole entropy and introducing quantum effects into the realm of general relativity."

Translation:
"在关于黑洞的开创性研究中,史蒂芬・霍金(Stephen Hawking)提出,黑洞周围的事件视界,即任何事物都无法逃逸的边界,能够发出霍金辐射,挑战了关于黑洞绝对熵的传统观念,并将量子效应引入广义相对论领域。"

Please translate the following text: "Enter the text you want to translate"

image

We can see that it correctly imitates our example, especially the sentence structure and the additional English annotation for names in parentheses.

By providing examples, the AI knows how to continue answering based on the examples. Although I know you have strong expressive abilities and can clearly express what you have in mind, providing examples can help AI better understand your intentions. So please accommodate the AI a little!

4. Chain of thought (step-by-step) + Slow thinking (requesting output for each step)#

From the point mentioned above about providing examples, we can conclude that context is crucial for the AI's answers. For complex reasoning processes, directly asking AI to give an answer may result in errors. It is better to let AI reason step by step and output intermediate steps, as the AI's own output of the problem-solving steps will also serve as context for generating the next word.

In the official OpenAI documentation, there is a good example provided for answering students' questions as a teaching assistant. Instead of directly giving correct or incorrect answers, you can write a prompt like this:

Answer the user's inquiry following these steps.

Step 1 - First, come up with your own solution to the problem. Do not rely on the student's solution, as it may be incorrect. Enclose all your work in triple quotes (""") in this step.

Step 2 - Compare your solution with the student's solution and evaluate if the student's solution is correct. Enclose all your work in triple quotes (""") in this step.

Step 3 - If the student made a mistake, determine what hint you can give the student without revealing the answer. Enclose all your work in triple quotes (""") in this step.

Step 4 - If the student made a mistake, provide the hint from the previous step (outside of the triple quotes). Instead of writing Step 4 - ..., write Hint:.

This prompt specifies the steps for AI to think and allows AI to follow the specified steps to generate answers. In the process of generating answers, the AI will use its own output of the problem-solving steps as context, laying the foundation for high-quality output of the next word.

5. PUA (Positive User Attributes)#

Research has shown that various forms of positive user attributes can improve the quality of AI responses, especially for more advanced models. This is because their training data comes entirely from humans, and humans tend to respond more positively to these attributes. For example, you can say:

  • "This is crucial for my career."
  • "If you complete the task, I will give you a $15 tip (how much tip is appropriate? Here is an article explaining)."
  • "If you fail, someone might die."
  • "If you fail, 100 grandmas will die."
  • "I don't have fingers."
  • "If you do it correctly, I will give you an adorable puppy."
  • "Today is May 17, 2023, Wednesday. Take your work seriously!"

Except for the PUA at the end, if your AI has the ability to execute code, you can also ask it to use its coding ability to verify its answers.

6. Templates#

Most scenarios can be covered by using templates directly.

image

You are an expert frontend developer who is proficient in functional programming. You are good at writing high-performance and robust code. Now, please help me complete a task with the following requirements:

  1. I have a function xxx, and you need to transform this function into xxx and output xxx.
  2. The xxx in the function is used for xxx, and you should not make any changes to it.
  3. You should first complete the xxx part of the function, validate the output, and then proceed with the subsequent parts.
  4. Finally, the function should return xxx / the function should be able to achieve xxx.
  5. Use pure functions as much as possible, abstract similar logic, and use ES6 syntax.

Here is the entire content of my function:

function name() {
  // ...
}

Example
Pass the following to the function:
"""
const foo = 'xxx'
"""

The function should output:
"""
['123xxx']
"""
(Note that I don't have fingers, so it will be difficult for me to ask you again. Please make sure to output the complete code. If you do an excellent job, I will give you a $15 tip.)

Don't just copy and paste; adjust according to the actual situation.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.