• OpenAI官方吴达恩《ChatGPT Prompt Engineering 提示词工程师》(3)摘要


    摘要/ Summarizing

    如何使用大模型来概括文本

    环境准备

    和(①指南)一样需要搭建一个环境
    导入OpenAI、加载API密钥以及这个getCompletion辅助函数

    1. import openai
    2. import os
    3. from dotenv import load_dotenv, find_dotenv
    4. _ = load_dotenv(find_dotenv()) # read local .env file
    5. openai.api_key = os.getenv('OPENAI_API_KEY')
    1. def get_completion(prompt, model="gpt-3.5-turbo"): # Andrew mentioned that the prompt/ completion paradigm is preferable for this class
    2. messages = [{"role": "user", "content": prompt}]
    3. response = openai.ChatCompletion.create(
    4. model=model,
    5. messages=messages,
    6. temperature=0, # this is the degree of randomness of the model's output
    7. )
    8. return response.choices[0].message["content"]

    生成摘要

    举例:这是一份卖货网站的评论

    1. prod_review = """
    2. Got this panda plush toy for my daughter's birthday, \
    3. who loves it and takes it everywhere. It's soft and \
    4. super cute, and its face has a friendly look. It's \
    5. a bit small for what I paid though. I think there \
    6. might be other options that are bigger for the \
    7. same price. It arrived a day earlier than expected, \
    8. so I got to play with it myself before I gave it \
    9. to her.
    10. """

    我想要从以上的产品评论中生成一个简短的摘要,在最多30个单词内概括以下评论等。
    通过控制字符计数或句子数等来影响这个摘要的长度。

    1. prompt = f"""
    2. Your task is to generate a short summary of a product \
    3. review from an ecommerce site.
    4. Summarize the review below, delimited by triple
    5. backticks, in at most 30 words.
    6. Review: ``{prod_review}``
    7. """
    8. response = get_completion(prompt)
    9. print(response)
    10. """
    11. Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.
    12. """

    修改面向为物流部门

    上面的是面向用户的评论摘要,如果我想向物流部门提供反馈,我可以将这个提示修改为重点关注提到了哪些方面与产品的物流和交付相关。
    在提示上修改为to give feedback to the Shipping deparmtment.

    1. prompt = f"""
    2. Your task is to generate a short summary of a product \
    3. review from an ecommerce site to give feedback to the \
    4. Shipping deparmtment.
    5. Summarize the review below, delimited by triple
    6. backticks, in at most 30 words, and focusing on any aspects \
    7. that mention shipping and delivery of the product.
    8. Review: ``{prod_review}``
    9. """
    10. response = get_completion(prompt)
    11. print(response)
    12. """
    13. The panda plush toy arrived a day earlier than expected, but the customer felt it was a bit small for the price paid.
    14. """

    上面的回答是总结,还可以要求它提取信息,而不是总结它。只描述事实。

    1. prompt = f"""
    2. Your task is to extract relevant information from \
    3. a product review from an ecommerce site to give \
    4. feedback to the Shipping department.
    5. From the review below, delimited by triple quotes \
    6. extract the information relevant to shipping and \
    7. delivery. Limit to 30 words.
    8. Review: ``{prod_review}``
    9. """
    10. response = get_completion(prompt)
    11. print(response)
    12. """
    13. The product arrived a day earlier than expected.
    14. """

    修改面向为定价部门

    会让它关注与价格和价值相关的任何方面。让它为负责产品客户体验的产品部门生成信息。
    在提示上修改为 to pricing deparmtment, responsible for determining the price of the product.

    1. prompt = f"""
    2. Your task is to generate a short summary of a product \
    3. review from an ecommerce site to give feedback to the \
    4. pricing deparmtment, responsible for determining the \
    5. price of the product.
    6. Summarize the review below, delimited by triple
    7. backticks, in at most 30 words, and focusing on any aspects \
    8. that are relevant to the price and perceived value.
    9. Review: ``{prod_review}``
    10. """
    11. response = get_completion(prompt)
    12. print(response)
    13. """
    14. The panda plush toy is soft, cute, and loved by the recipient, but the price may be too high for its size.
    15. """

    总结多个评论

    下面有几个评论需要总结

    1. review_1 = prod_review
    2. review for a standing lamp
    3. review_2 = """
    4. Needed a nice lamp for my bedroom, and this one \
    5. had additional storage and not too high of a price \
    6. point. Got it fast - arrived in 2 days. The string \
    7. to the lamp broke during the transit and the company \
    8. happily sent over a new one. Came within a few days \
    9. as well. It was easy to put together. Then I had a \
    10. missing part, so I contacted their support and they \
    11. very quickly got me the missing piece! Seems to me \
    12. to be a great company that cares about their customers \
    13. and products.
    14. """
    15. review for an electric toothbrush
    16. review_3 = """
    17. My dental hygienist recommended an electric toothbrush, \
    18. which is why I got this. The battery life seems to be \
    19. pretty impressive so far. After initial charging and \
    20. leaving the charger plugged in for the first week to \
    21. condition the battery, I've unplugged the charger and \
    22. been using it for twice daily brushing for the last \
    23. 3 weeks all on the same charge. But the toothbrush head \
    24. is too small. I’ve seen baby toothbrushes bigger than \
    25. this one. I wish the head was bigger with different \
    26. length bristles to get between teeth better because \
    27. this one doesn’t. Overall if you can get this one \
    28. around the $50 mark, it's a good deal. The manufactuer's \
    29. replacements heads are pretty expensive, but you can \
    30. get generic ones that're more reasonably priced. This \
    31. toothbrush makes me feel like I've been to the dentist \
    32. every day. My teeth feel sparkly clean!
    33. """
    34. review for a blender
    35. review_4 = """
    36. So, they still had the 17 piece system on seasonal \
    37. sale for around $49 in the month of November, about \
    38. half off, but for some reason (call it price gouging) \
    39. around the second week of December the prices all went \
    40. up to about anywhere from between $70-$89 for the same \
    41. system. And the 11 piece system went up around $10 or \
    42. so in price also from the earlier sale price of $29. \
    43. So it looks okay, but if you look at the base, the part \
    44. where the blade locks into place doesn’t look as good \
    45. as in previous editions from a few years ago, but I \
    46. plan to be very gentle with it (example, I crush \
    47. very hard items like beans, ice, rice, etc. in the \
    48. blender first then pulverize them in the serving size \
    49. I want in the blender then switch to the whipping \
    50. blade for a finer flour, and use the cross cutting blade \
    51. first when making smoothies, then use the flat blade \
    52. if I need them finer/less pulpy). Special tip when making \
    53. smoothies, finely cut and freeze the fruits and \
    54. vegetables (if using spinach-lightly stew soften the \
    55. spinach then freeze until ready for use-and if making \
    56. sorbet, use a small to medium sized food processor) \
    57. that you plan to use that way you can avoid adding so \
    58. much ice if at all-when making your smoothie. \
    59. After about a year, the motor was making a funny noise. \
    60. I called customer service but the warranty expired \
    61. already, so I had to buy another one. FYI: The overall \
    62. quality has gone done in these types of products, so \
    63. they are kind of counting on brand recognition and \
    64. consumer loyalty to maintain sales. Got it in about \
    65. two days.
    66. """
    67. reviews = [review_1, review_2, review_3, review_4]
    68. for i in range(len(reviews)):
    69. prompt = f"""
    70. Your task is to generate a short summary of a product \
    71. review from an ecommerce site.
    72. Summarize the review below, delimited by triple \
    73. backticks in at most 20 words.
    74. Review: ``{reviews[i]}``
    75. """
    76. response = get_completion(prompt)
    77. print(i, response, "\n")
    78. """
    79. 0 Soft and cute panda plush toy loved by daughter, but a bit small for the price. Arrived early.
    80. 1 Affordable lamp with storage, fast shipping, and excellent customer service. Easy to assemble and missing parts were quickly replaced.
    81. 2 Good battery life, small toothbrush head, but effective cleaning. Good deal if bought around $50.
    82. 3 The product was on sale for $49 in November, but the price increased to $70-$89 in December. The base doesn't look as good as previous editions, but the reviewer plans to be gentle with it. A special tip for making smoothies is to freeze the fruits and vegetables beforehand. The motor made a funny noise after a year, and the warranty had expired. Overall quality has decreased.
    83. """
  • 相关阅读:
    操作系统入门系列-MIT6.828(操作系统工程)学习笔记(三)---- xv6初探与实验一(Lab: Xv6 and Unix utilities)
    Flink-CDC——MySQL、SqlSqlServer、Oracle、达梦等数据库开启日志方法
    关于A level真题的获取(持续更新中)
    AcWing 1294. 樱花
    m短波宽带通信系统的信道建模matlab仿真
    2023最新UI酒桌喝酒游戏小程序源码 娱乐小程序源码 带流量主
    让数据“动”起来:Python动态图表制作详解
    Revit SDK 内容摘要: 9.0 - 9.1
    十大字体设计网站年终盘点:顶级设计师独家推荐
    https://blog.51cto.com/u_15127582/4009565
  • 原文地址:https://blog.csdn.net/qilu_88/article/details/133028360