ChatGPT prompt word project for Java developers (4)

In the previous article, we learned about four strategies for writing clear and specific instruction key principles, which are:

  • Use separators to clearly indicate different parts of the input.
  • Requires GTP structured output.
  • Ask GTP to check if a condition is met.
  • When writing examples, try to use as few prompt words as possible.

Next, we'll move on to the second key principle: give GPT some "thinking" time .

Give GPT some "thinking" time

When GPT rushes to wrong conclusions, we can consider reformulating the question and ask GPT to provide a series of related reasoning processes before giving the final answer. Just like humans, if we give GPT an overly complex task and ask it to complete it in a short period of time or with fewer words, it is likely to make random guesses, which will lead to wrong answers. In fact, humans make mistakes in similar situations. For example, if we ask a human to come up with an answer to a complex math problem without enough time to calculate it, they are likely to get it wrong too.

Identify the steps required to complete the task

The first strategy is to identify the steps required for task completion. We illustrate with the following example:

 public static void main(String[] args) {
    
    
     String text = "我说道:“爸爸,你走吧。”\n"
             + "他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”\n"
             + "我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。\n"
             + "我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。"
             + "他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。\n"
             + "我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。"
             + "他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。"
             + "他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n";

     String prompt = "按照如下步骤完成:\n"
             + "第一步,将三个反引号之间的文本总结为一句话的概要。\n"
             + "第二步,把概要翻译成英语。\n"
             + "第三步,列出英语概要中出现的人物。\n"
             + "第四步,以JSON格式输出,包括以下字段:english_summary、persons_num。\n"
             + "每个步骤按行输出。\n"
             + "```%s```";
     String response = getCompletion(String.format(prompt, text));
     System.out.println(response);
 }

We will give a story paragraph where a father buys oranges for his son, and then let GPT complete a series of tasks, including summarizing the text, translating it into English, listing each character in the English summary, and outputting a Number of JSON objects. The output is as follows:

概要:一个人送父亲去买橘子,父亲穿过铁道回来时费力,他看到父亲的背影很感动,父亲走后他哭了。
English summary: A person sends his father to buy oranges. When the father returns, he struggles to climb back over the railway. The person is moved by his father's efforts and cries when he leaves.
Persons: the person, the father
JSON输出:
{
    "english_summary": "A person sends his father to buy oranges. When the father returns, he struggles to climb back over the railway. The person is moved by his father's efforts and cries when he leaves.",
    "persons_num": 2
}

Interestingly, the English synopsis and character descriptions are in English, which may not be what I want. We use another cue word to accomplish the same task. In this prompt word, I use a format that I like to use to specify the output structure of GPT:

 public static void main(String[] args) {
    
    
     String text = "我说道:“爸爸,你走吧。”\n"
             + "他望车外看了看,说:“我买几个橘子去。你就在此地,不要走动。”\n"
             + "我看那边月台的栅栏外有几个卖东西的等着顾客。走到那边月台,须穿过铁道,须跳下去又爬上去。父亲是一个胖子,走过去自然要费事些。我本来要去的,他不肯,只好让他去。\n"
             + "我看见他戴着黑布小帽,穿着黑布大马褂,深青布棉袍,蹒跚地走到铁道边,慢慢探身下去,尚不大难。可是他穿过铁道,要爬上那边月台,就不容易了。"
             + "他用两手攀着上面,两脚再向上缩;他肥胖的身子向左微倾,显出努力的样子。这时我看见他的背影,我的泪很快地流下来了。我赶紧拭干了泪。怕他看见,也怕别人看见。\n"
             + "我再向外看时,他已抱了朱红的橘子往回走了。过铁道时,他先将橘子散放在地上,自己慢慢爬下,再抱起橘子走。到这边时,我赶紧去搀他。"
             + "他和我走到车上,将橘子一股脑儿放在我的皮大衣上。于是扑扑衣上的泥土,心里很轻松似的。过一会儿说:“我走了,到那边来信!”我望着他走出去。"
             + "他走了几步,回过头看见我,说:“进去吧,里边没人。”等他的背影混入来来往往的人里,再找不着了,我便进来坐下,我的眼泪又来了。\n";

     String prompt = "按照如下步骤完成:\n"
             + "第一步,将三个反引号之间的文本总结为一句话的概要。\n"
             + "第二步,把概要翻译成英语。\n"
             + "第三步,列出英语概要中出现的人物。\n"
             + "第四步,以JSON格式输出,包括以下字段:english_summary、persons_num。\n"
             + "用以下格式回答:\n"
             + "中文概要:<总结的概要>\n"
             + "英文概要:<英语概要>\n"
             + "人物:<英语概要中出现的人物>\n"
             + "JSON:<english_summary、persons_num的JSON格式输出>\n"
             + "```%s```";
     String response = getCompletion(String.format(prompt, text));
     System.out.println(response);
 }

In this cue word, we're asking for something similar, and the cue word starts the same. Then we asked GPT to use the formats of Chinese summary, English summary, characters and JSON. The output after we run:

中文概要:父亲买橘子,儿子送行,父亲过铁道时艰难,儿子看着流泪。
英文概要:Father went to buy oranges, son saw him off. Father had difficulty crossing the railway, son cried. 
人物:Father, son
JSON:{"english_summary": "Father went to buy oranges, son saw him off. Father had difficulty crossing the railway, son cried.", "persons_num": 2}

As you can see, GPT uses the format we require. We've given it the text, and it's given us the Chinese summary, the English summary, the characters, and the JSON.

Instruct GPT to figure out the solution on its own before jumping to conclusions

Our next strategy: instruct GPT to figure out a solution on its own before jumping to conclusions. Sometimes we get better results when we explicitly instruct GPT to figure out its own solution before drawing a conclusion.
This strategy is the same as what we discussed before, which is to give GPT some time to solve the problem instead of directly saying whether the answer is right or not, just like people.

In the example below, we ask GPT to judge whether the student's answer is correct.

 public static void main(String[] args) {
    
    
     String prompt = "判断学生的解答是否正确。\n"
             + "问题:\n"
             + "《红岩》一共有288页,小明每天读36页书,几天可以读完《红岩》?\n\n"
             + "学生解答:\n"
             + "288 / 36 = 10(天)\n"
             + "小明10天可以读完《红岩》。";
     String response = getCompletion(prompt);
     System.out.println(response);
 }

This is a math word problem followed by the student's solution. The student's solution is actually wrong because 288 / 36 equals 8, not 10. Let's run it and see:

学生的解答是正确的。

If you just read the student's solution, you'll see that the solution looks correct. So GPT just did a cursory read like I did and agreed with the student's answer.

Let's optimize the prompt words so that GPT can answer this question first, and then compare our own answer with the student's answer:

public static void main(String[] args) {
    
    
    String prompt = "判断学生的解答是否正确。\n"
            + "按照如下步骤完成:\n"
            + "第一步,你对这个问题进行解答。\n"
            + "第二步,将你的解答与学生的解答进行比较,并评估学生的解答是否正确。\n"
            + "在你自己解答问题之前,不要确定学生的解答是否正确。\n"
            + "用以下的格式回答:\n"
            + "正确解答:<你的解题步骤和解答>\n"
            + "学生的答案和你的答案的比较:<一样或者不一样>\n"
            + "学生答案的判断结果:<正确或者错误>\n"
            + "问题:\n"
            + "《红岩》一共有288页,小明每天读36页书,几天可以读完《红岩》?\n\n"
            + "学生解答:\n"
            + "288 / 36 = 10(天)\n"
            + "小明10天可以读完《红岩》。";
    String response = getCompletion(prompt);
    System.out.println(response);
}

What are we telling the model in this hint? Its task is to determine whether the student's solution is correct. To complete the task do the following steps: First, solve your own problems. Then compare your solution to the student's solution to assess whether the student's solution is correct. Don't decide whether a student's solution is correct until you have solved the problem yourself. Just be very clear and make sure you figured this out yourself. Therefore, we used the same technique to optimize the prompt words.

The result of the operation is as follows:

正确解答:288÷36=8(天)
学生的答案和你的答案的比较:不一样
学生答案的判断结果:错误

Summarize

We understand the second key principle: give GPT some "thinking" time . When GPT rushes to wrong conclusions, we can consider reformulating the question and ask GPT to provide a series of related reasoning processes before giving the final answer. There are two main strategies:

  • Identify the steps required to complete the task.
  • Instruct GPT to figure out the solution on its own before jumping to conclusions.

ChatGPT prompt word project for Java developers

Guess you like

Origin blog.csdn.net/heihaozi/article/details/130678519