当前位置:   article > 正文

大型语言模(LLM) : 提示词工程(一)_大语言模型提示词

大语言模型提示词

今天我学习了DeepLearning.AI的 Prompt Engineering 的在线课程,我想和大家一起分享一下该门课程的一些主要内容。

下面是我们访问大型语言模(LLM)的主要代码:

  1. import openai
  2. openai.api_key ='XXXXXXXXX'
  3. def get_completion(prompt, model="gpt-3.5-turbo"):
  4. messages = [{"role": "user", "content": prompt}]
  5. response = openai.ChatCompletion.create(
  6. model=model,
  7. messages=messages,
  8. temperature=0,
  9. )
  10. return response.choices[0].message["content"]

提示词原则(Prompting Principles)

  • 原则1:写出清晰和具体的说明(Write clear and specific instructions)

  • 原则2:给模型时间“思考”(Give the model time to “think”)

策略

策略 1:使用分隔符清楚地指示输入的不同部分
分隔符可以是:```, """, < >, <tag> </tag>, :

对策略1的说明:

策略1的意思是:如果我们需要chatgpt完成某个特定任务,那么我们要在prompt里面明确的告诉ChatGPT需要做的事情是什么以及所需要的素材的位置。比如我们要chatGPT对一段文本内容做一个总结或摘要,那么我们就需要在prompt中指明:1.需要gpt完成的任务是什么, 2. 任务所需的素材在哪里。

下面的代码演示的是让gpt对一段特定文本内容做摘要, 在定义prompt时我们需要用特定的分隔符来分隔需要做摘要的文本内容,这样做的目的是让gpt知道文本内容所在位置。

  1. text = f"""
  2. You should express what you want a model to do by \
  3. providing instructions that are as clear and \
  4. specific as you can possibly make them. \
  5. This will guide the model towards the desired output, \
  6. and reduce the chances of receiving irrelevant \
  7. or incorrect responses. Don't confuse writing a \
  8. clear prompt with writing a short prompt. \
  9. In many cases, longer prompts provide more clarity \
  10. and context for the model, which can lead to \
  11. more detailed and relevant outputs.
  12. """
  13. prompt = f"""
  14. Summarize the text delimited by triple backticks \
  15. into a single sentence.
  16. ```{text}```
  17. """
  18. response = get_completion(prompt)
  19. print(response)

 策略 2:可以要求GPT给出一个结果化的结果

  • JSON, HTML
  1. prompt = f"""
  2. Generate a list of three made-up book titles along \
  3. with their authors and genres.
  4. Provide them in JSON format with the following keys:
  5. book_id, title, author, genre.
  6. """
  7. response = get_completion(prompt)
  8. print(response)

 策略3:要求模型检查条件是否满足

这里我们要求gpt去检查一段文本的内容是否满足特定的条件(步骤),如果满足则按条件输出符合条件的内容,如果不满足条件则输出:不符合条件。

  1. text_1 = f"""
  2. Making a cup of tea is easy! First, you need to get some \
  3. water boiling. While that's happening, \
  4. grab a cup and put a tea bag in it. Once the water is \
  5. hot enough, just pour it over the tea bag. \
  6. Let it sit for a bit so the tea can steep. After a \
  7. few minutes, take out the tea bag. If you \
  8. like, you can add some sugar or milk to taste. \
  9. And that's it! You've got yourself a delicious \
  10. cup of tea to enjoy.
  11. """
  12. prompt = f"""
  13. You will be provided with text delimited by triple quotes.
  14. If it contains a sequence of instructions, \
  15. re-write those instructions in the following format:
  16. Step 1 - ...
  17. Step 2 - …
  18. Step N - …
  19. If the text does not contain a sequence of instructions, \
  20. then simply write \"No steps provided.\"
  21. \"\"\"{text_1}\"\"\"
  22. """
  23. response = get_completion(prompt)
  24. print("Completion for Text 1:")
  25. print(response)

  1. text_2 = f"""
  2. The sun is shining brightly today, and the birds are \
  3. singing. It's a beautiful day to go for a \
  4. walk in the park. The flowers are blooming, and the \
  5. trees are swaying gently in the breeze. People \
  6. are out and about, enjoying the lovely weather. \
  7. Some are having picnics, while others are playing \
  8. games or simply relaxing on the grass. It's a \
  9. perfect day to spend time outdoors and appreciate the \
  10. beauty of nature.
  11. """
  12. prompt = f"""
  13. You will be provided with text delimited by triple quotes.
  14. If it contains a sequence of instructions, \
  15. re-write those instructions in the following format:
  16. Step 1 - ...
  17. Step 2 - …
  18. Step N - …
  19. If the text does not contain a sequence of instructions, \
  20. then simply write \"No steps provided.\"
  21. \"\"\"{text_2}\"\"\"
  22. """
  23. response = get_completion(prompt)
  24. print("Completion for Text 2:")
  25. print(response)

 上述两个prompt中第一个prompt的内容符合特定的条件,所以结果按条件输出结果,第二个prompt不符合条件,所以输出: No steps provided.

策略 4:“Few-shot”提示词

  1. prompt = f"""
  2. Your task is to answer in a consistent style.
  3. <child>: Teach me about patience.
  4. <grandparent>: The river that carves the deepest \
  5. valley flows from a modest spring; the \
  6. grandest symphony originates from a single note; \
  7. the most intricate tapestry begins with a solitary thread.
  8. <child>: Teach me about resilience.
  9. """
  10. response = get_completion(prompt)
  11. print(response)

上述的prompt要求ChatGPT以一致的风格来回答问题,在prompt中给出来child和grandparent的一轮对话内容作为一个例子,这样做的目的是让ChatGPT明白所谓的“consistent style”是怎么一回事,prompt的最后是child的一个问题,但是没有grandparent的回答。这是让ChatGPT明白要做的事情是:充当grandparent角色来回答child的最后一个问题,并且风格要和前面的grandparent的风格保存一致。

下面是ChatGPT返回的结果:

 原则 2:给模型时间“思考”

策略 1:指定完成任务所需的步骤

这里我们会给模型一段文本,然后要求模型按指定的步骤来完成一个任务:

执行以下操作:
1 - 用 1 个句子总结以下用三重反引号分隔的文本。
2 - 将摘要翻译成法语。
3 - 在法语摘要中列出每个名字。
4 - 输出包含以下键的 json 对象:french_summary、num_names。

用换行符分隔你的答案。

  1. ext = f"""
  2. In a charming village, siblings Jack and Jill set out on \
  3. a quest to fetch water from a hilltop \
  4. well. As they climbed, singing joyfully, misfortune \
  5. struck—Jack tripped on a stone and tumbled \
  6. down the hill, with Jill following suit. \
  7. Though slightly battered, the pair returned home to \
  8. comforting embraces. Despite the mishap, \
  9. their adventurous spirits remained undimmed, and they \
  10. continued exploring with delight.
  11. """
  12. # example 1
  13. prompt_1 = f"""
  14. Perform the following actions:
  15. 1 - Summarize the following text delimited by triple \
  16. backticks with 1 sentence.
  17. 2 - Translate the summary into French.
  18. 3 - List each name in the French summary.
  19. 4 - Output a json object that contains the following \
  20. keys: french_summary, num_names.
  21. Separate your answers with line breaks.
  22. Text:
  23. ```{text}```
  24. """
  25. response = get_completion(prompt_1)
  26. print("Completion for prompt 1:")
  27. print(response)

 要求指定格式的输出

这里我们要求ChatGPT按指定的格式来输出内容,在prompt中的内容分成了2部分,上半部分告诉ChatGPT需要做哪几件事情,下半部分输出的格式:

  1. prompt_2 = f"""
  2. Your task is to perform the following actions:
  3. 1 - Summarize the following text delimited by
  4. <> with 1 sentence.
  5. 2 - Translate the summary into French.
  6. 3 - List each name in the French summary.
  7. 4 - Output a json object that contains the
  8. following keys: french_summary, num_names.
  9. Use the following format:
  10. Text: <text to summarize>
  11. Summary: <summary>
  12. Translation: <summary translation>
  13. Names: <list of names in Italian summary>
  14. Output JSON: <json with summary and num_names>
  15. Text: <{text}>
  16. """
  17. response = get_completion(prompt_2)
  18. print("\nCompletion for prompt 2:")
  19. print(response)

 策略 2:在匆忙下结论之前指示模型自己制定解决方案

这里在prompt中给ChatGPT出了一道学生做的简单的数学应用题,希望ChatGPT来判断学生的答案是否正确。对于ChatGPT给出的答案我们发现,它并没有仔细的去亲自去做这道题目,结果给出了一个错误的答案。从中我们发现,当prompt中的问题的逻辑比较复杂的时候,我们直接了当的要求ChatGPT给出一个答案时候,ChatGPT也会像人类一样经常犯错误。下面我们先将prompt翻译一下:

  1. prompt = f"""
  2. Determine if the student's solution is correct or not.
  3. Question:
  4. I'm building a solar power installation and I need \
  5. help working out the financials.
  6. - Land costs $100 / square foot
  7. - I can buy solar panels for $250 / square foot
  8. - I negotiated a contract for maintenance that will cost \
  9. me a flat $100k per year, and an additional $10 / square \
  10. foot
  11. What is the total cost for the first year of operations
  12. as a function of the number of square feet.
  13. Student's Solution:
  14. Let x be the size of the installation in square feet.
  15. Costs:
  16. 1. Land cost: 100x
  17. 2. Solar panel cost: 250x
  18. 3. Maintenance cost: 100,000 + 100x
  19. Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
  20. """
  21. response = get_completion(prompt)
  22. print(response)

 

请注意,该学生的答案实际上是不正确的。

我们可以通过指示ChatGPT首先计算出自己的答案再来判断学生的答案是否正确。因此需要改写一下prompt,我们需要把求解这道应用题的详细步骤告诉ChatGPT:

接下来给出实现这些步骤的具体格式:

  1. prompt = f"""
  2. Your task is to determine if the student's solution \
  3. is correct or not.
  4. To solve the problem do the following:
  5. - First, work out your own solution to the problem.
  6. - Then compare your solution to the student's solution \
  7. and evaluate if the student's solution is correct or not.
  8. Don't decide if the student's solution is correct until
  9. you have done the problem yourself.
  10. Use the following format:
  11. Question:
  12. ```
  13. question here
  14. ```
  15. Student's solution:
  16. ```
  17. student's solution here
  18. ```
  19. Actual solution:
  20. ```
  21. steps to work out the solution and your solution here
  22. ```
  23. Is the student's solution the same as actual solution \
  24. just calculated:
  25. ```
  26. yes or no
  27. ```
  28. Student grade:
  29. ```
  30. correct or incorrect
  31. ```
  32. Question:
  33. ```
  34. I'm building a solar power installation and I need help \
  35. working out the financials.
  36. - Land costs $100 / square foot
  37. - I can buy solar panels for $250 / square foot
  38. - I negotiated a contract for maintenance that will cost \
  39. me a flat $100k per year, and an additional $10 / square \
  40. foot
  41. What is the total cost for the first year of operations \
  42. as a function of the number of square feet.
  43. ```
  44. Student's solution:
  45. ```
  46. Let x be the size of the installation in square feet.
  47. Costs:
  48. 1. Land cost: 100x
  49. 2. Solar panel cost: 250x
  50. 3. Maintenance cost: 100,000 + 100x
  51. Total cost: 100x + 250x + 100,000 + 100x = 450x + 100,000
  52. ```
  53. Actual solution:
  54. """
  55. response = get_completion(prompt)
  56. print(response)

 这里我们可以发现ChatGPT的实际输出是从“Use the following format:” 的“Actual solution:”开始的,并没有将“Question:”和“Student's solution:”这两部分内容输出。这表明ChatGPT能够理解人类的意图,因为“Question:”和“Student's solution:”部分在题目中已经出现,无需在重复输出,只需输出“Actual solution:”部分以后的内容即可。最后ChatGPT通过比对自己的答案和学生的答案后给出了正确的答案。

模型局限性:幻觉(Hallucinations)

当ChatGPT不知道问题答案时候,ChatGPT往往不会承认自己不知道,因此GPT往往会在不知道正确答案的时候会给出一个貌似正确的错误答案。

下面我们在prompt中要求ChatGPT给出Boie这家公司的电动牙刷的相关信息,这里的 Boie 是一家真实的公司,产品名称不是真实的。

  1. prompt = f"""
  2. Tell me about AeroGlide UltraSlim Smart Toothbrush by Boie
  3. """
  4. response = get_completion(prompt)
  5. print(response)

 这里我们发现ChatGPT对一个Boie公司不存在的产品说了一大堆的内容,好像这个产品是真实存在似的。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/214826?site
推荐阅读
相关标签
  

闽ICP备14008679号