本文讨论了如何使用 ChatGPT 作为自定义应用程序的摘要代理的最佳实践。摘要代理是压缩信息并提供跨多个应用程序和平台对相关内容的快速访问的重要工具。通过使用 ChatGPT,我们可以对文本进行摘要和标准化,并识别评论的情绪和类别。
然而,需要注意的是,ChatGPT 在严格遵守摘要中的字符或字数限制方面可能存在局限性。分词器是影响 ChatGPT 字数统计的关键因素,因此无法精确控制字数。建议在构建应用程序时将提示视为一个迭代过程,并在一批示例中测试和改进提示,以获得一致和准确的输出。
红米k60实时发布时间
此外,了解在使用 ChatGPT 进行摘要时的优缺点,以及使用提取任务代替摘要可以获得更好的结果。最后,需要注意 ChatGPT 可能优先考虑连贯性和相关性,而不是严格遵守字数统计。因此,可能需要采取额外的步骤来确保符合特定格式要求。
如何使用ChatGPT 进行有效总结?下面将通过这几个例子来进行探索:
示例:电子商务评论
考虑上面的示例,其中我们有兴趣处理电子商务网站上给定产品的所有评论。我们有兴趣处理诸如以下有关我们明星产品的评论:第一台儿童电脑!
12345678910111213prod_review="""Ipurchasedthischildren'scomputerformyson,\andheabsolutelyadoresit.Hespendshoursexploring\itsvariousfeaturesandengagingwiththeeducationalgames.\Thecolorfuldesignandintuitiveinterfacemakeiteasyfor\himtonavigate.Thecomputerisdurableandbuiltto\withstandroughhandling,whichisperfectforactivekids.\Myonlyminorgripeisthatthevolumecouldbeabitlouder.\Overall,it'sanexcellenteducationaltoythatprovides\hoursoffunandlearningformyson.Itarrivedadayearlier\thanexpected,soIgottoplaywithitmyselfbeforeIgave\ittohim."""
在这种情况下,我们希望 ChatGPT 能够:
- 将评论分为正面或负面。
- 提供20字的评论摘要。
- 使用具体结构输出响应,将所有评论标准化为一种格式。
实施说明
以下是我们可以用来从自定义应用程序提示 ChatGPT 的基本代码结构。我还提供了Jupyter Notebook的链接,其中包含本文中使用的所有示例。
123456789101112131415161718192021222324252627282930importopenaiimportosopenai.api_key_path="/path/to/key"defget_completion(prompt,model="gpt-3.5-turbo"):"""ThisfunctioncallsChatGPTAPIwithagivenpromptandreturnstheresponseback."""messages=[{"role":"user","content":prompt}]response=openai.ChatCompletion.create(model=model,messages=messages,temperature=0)returnresponse.choices[0].message["content"]user_text=f"""<Anygiventext>"""prompt=f"""<Anypromptwithadditionaltext>\"\"\"{user_text}\"\"\""""#AsimplecalltoChatGPTresponse=get_completion(prompt)
该函数使用给定的提示get_completion()调用 ChatGPT API。如果提示包含其他用户文本(例如我们示例中的评论本身),则它会通过三引号与其余代码分隔开。
让我们使用get_completion()提示ChatGPT的功能吧!
以下是满足上述要求的提示:
12345678910prompt=f"""Yourtaskistogenerateashortsummaryofaproduct\reviewfromane-commercesite.\Summarizethereviewbelow,delimitedbytriple\backticks,inexactly20words.Outputajsonwiththe\sentimentofthereview,thesummaryandoriginalreviewaskeys.\Review:```{prod_review}```"""response=get_completion(prompt)print(response)
⚠️ 本示例中使用的提示指南(例如使用分隔符将输入文本与提示的其余部分分开以及要求结构化输出)在我从 OpenAI 提示工程课程中学到的内容 — 提示指南中有完整解释。
以下是 ChatGPT 的回答:
12345 | {"sentiment": "positive","summary": "Durable and engaging children's computer with intuitive interface and educational games. Volume could be louder.","review": "I purchased this children's computer for my son, and he absolutely adores it. He spends hours exploring its various features and engaging with the educational games. The colorful design and intuitive interface make it easy for him to navigate. The computer is durable and built to withstand rough handling, which is perfect for active kids. My only minor gripe is that the volume could be a bit louder. Overall, it's an excellent educational toy that provides hours of fun and learning for my son. It arrived a day earlierthan expected, so I got to play with it myself before I gave it to him."} |
正如我们从输出中可以观察到的那样,评论是准确且结构良好的,尽管它遗漏了我们作为电子商务所有者可能感兴趣的一些信息,例如有关产品交付的信息。
重点总结<运输和交付>
我们可以迭代地改进要求 ChatGPT 关注摘要中某些内容的提示。在这种情况下,我们对有关运输和交付的任何详细信息感兴趣:
123456789101112131415prompt=f"""Yourtaskistogenerateashortsummaryofaproduct\reviewfromanecommercesite.\Summarizethereviewbelow,delimitedbytriple\backticks,inexactly20wordsandfocusingonanyaspects\thatmentionshippinganddeliveryoftheproduct.\Outputajsonwiththesentimentofthereview,\thesummaryandoriginalreviewaskeys.\Review:```{prod_review}```"""response=get_completion(prompt)print(response)
这次,ChatGPT 的答复如下:
全球半导体行业的现状
12345 | {"sentiment": "positive","summary": "Durable and engaging children's computer with intuitive interface. Arrived a day earlier than expected.","review": "I purchased this children's computer for my son, and he absolutely adores it. He spends hours exploring its various features and engaging with the educational games. The colorful design and intuitive interface make it easy for him to navigate. The computer is durable and built to withstand rough handling, which is perfect for active kids. My only minor gripe is that the volume could be a bit louder. Overall, it's an excellent educational toy that provides hours of fun and learning for my son. It arrived a day earlierthan expected, so I got to play with it myself before I gave it to him."} |
现在审查更加完整。提供有关原始审查的重点的详细信息对于避免 ChatGPT 跳过一些可能对我们的用例有价值的信息至关重要。
您是否注意到,虽然第二次试验包含有关交付的信息,但它跳过了原始评论的唯一负面方面?
让我们解决这个问题!
“摘录”代替“总结”
通过调查摘要任务,我发现如果用户提示不够准确,摘要对于法学硕士来说可能是一项棘手的任务。
当要求 ChatGPT 提供给定文本的摘要时,它可以跳过可能与我们相关的信息(正如我们最近经历的那样),或者它会对文本中的所有主题给予相同的重要性,仅提供对文本的概述要点。
法学硕士专家在完成这些类型的模型辅助的此类任务时,会使用术语“摘录”和附加信息来表达其关注点,而不是进行总结。
摘要旨在提供文本要点的简洁概述,包括与焦点主题无关的主题,而信息提取则侧重于检索具体细节,可以为我们提供我们正在寻找的内容。让我们尝试提取吧!
1234567891011121314prompt=f"""Yourtaskistoextractrelevantinformationfrom\aproductreviewfromanecommercesitetogive\feedbacktotheShippingdepartment.\Fromthereviewbelow,delimitedbytriplequotes\extracttheinformationrelevanttoshippingand\delivery.Use100characters.\Review:```{prod_review}```"""response=get_completion(prompt)print(response)
在这种情况下,通过使用提取,我们只能获得有关我们关注的主题的信息:Shipping: Arrived a day earlier than expected.
自动化
该系统适用于一次审查。然而,在为具体应用程序设计提示时,重要的是在一批示例中对其进行测试,以便我们可以捕获模型中的任何异常值或不当行为。
如果要处理多个评论,这里有一个可以提供帮助的示例 Python 代码结构。
12345678910111213141516171819reviews=["Thechildren'scomputerIboughtformydaughterisabsolutelyfantastic!Shelovesitandcan'tgetenoughoftheeducationalgames.Thedeliverywasfastandarrivedrightontime.Highlyrecommend!","Iwasreallydisappointedwiththechildren'scomputerIreceived.Itdidn'tliveuptomyexpectations,andtheeducationalgameswerenotengagingatall.Thedeliverywasdelayed,whichaddedtomyfrustration.","Thechildren'scomputerisagreateducationaltoy.Mysonenjoysplayingwithitandlearningnewthings.However,thedeliverytooklongerthanexpected,whichwasabitdisappointing.","Iamextremelyhappywiththechildren'scomputerIpurchased.It'shighlyinteractiveandkeepsmykidsentertainedforhours.Thedeliverywasswiftandhassle-free.","Thechildren'scomputerIorderedarriveddamaged,andsomeofthefeaturesdidn'tworkproperly.Itwasahugeletdown,andthedeliverywasalsodelayed.Notagoodexperienceoverall."]prompt=f"""Yourtaskistogenerateashortsummaryofeachproduct\reviewfromane-commercesite.Extractpositiveandnegativeinformationfromeachofthe\givenreviewsbelow,delimitedbytriple\backticksinatmost20wordseach.Extractinformationabout\thedelivery,ifincluded.\Review:```{reviews}```"""
以下是我们这批评论的摘要:
12345 | 1. Positive: Fantastic children's computer, fast delivery. Highly recommend.2. Negative: Disappointing children's computer, unengaging games, delayed delivery.3. Positive: Great educational toy, son enjoys it. Delivery took longer than expected.4. Positive: Highly interactive children's computer, swift and hassle-free delivery.5. Negative: Damaged children's computer, some features didn't work, delayed delivery. |
⚠️ 请注意,尽管我们的提示中摘要的字数限制足够清楚,但我们可以很容易地看到,在任何迭代中都没有实现此字数限制。
字数统计中出现这种不匹配的情况是因为法学硕士对字数或字符数没有准确的理解。这背后的原因依赖于其架构的主要重要组件之一:标记器。
分词器
ChatGPT 等旨在根据从大量语言数据中学习到的统计模式生成文本。虽然它们在生成流畅且连贯的文本方面非常有效,但它们缺乏对字数的精确控制。
在上面的示例中,当我们给出有关非常精确的字数统计的说明时,ChatGPT 很难满足这些要求。相反,它生成的文本实际上短于指定的字数。
在其他情况下,它可能会生成较长的文本或只是过于冗长或缺乏细节的文本。此外,ChatGPT 可能会优先考虑其他因素,例如连贯性和相关性,而不是严格遵守字数统计。这可能会导致文本在内容和连贯性方面具有高质量,但并不完全符合字数要求。
分词器是 ChatGPT 架构中的关键元素,它明显影响生成输出中的单词数量。
分词器架构
分词器是文本生成过程的第一步。它负责将我们输入到 ChatGPT 的文本片段分解为单独的元素(标记),然后由语言模型处理以生成新文本。
当分词器将一段文本分解为标记时,它会根据一组旨在识别目标语言的有意义单元的规则来完成此操作。然而,这些规则并不总是完美的,并且在某些情况下,分词器会以影响文本总字数的方式拆分或合并标记。
例如,考虑以下句子:“我想吃花生酱三明治”。如果分词器配置为根据空格和标点符号分割标记,则它可能会将此句子分解为以下标记,总字数为8,等于标记计数。
然而,如果分词器配置为将“花生酱”视为复合词,则它可能会将句子分解为以下标记,总字数为8,但标记数为7。
因此,分词器的配置方式可能会影响文本的总体字数,这可能会影响法学硕士遵循有关精确字数计数的说明的能力。虽然一些分词器提供了自定义文本分词方式的选项,但这并不总是足以确保精确遵守字数统计要求。对于本例中的ChatGPT,我们无法控制其架构的这一部分。
这使得 ChatGPT 不太擅长完成字符或单词限制,但可以尝试使用句子来代替,因为分词器不会影响句子的数量,而是影响句子的长度。
了解此限制可以帮助您为您的应用程序构建最合适的提示。了解了 ChatGPT 上字数统计的工作原理后,让我们对电子商务应用程序的提示进行最后一次迭代!
总结:电子商务评论
让我们将从本文中学到的知识结合到最终的提示中!在这种情况下,我们将要求结果的HTML格式以获得更好的输出:
1234567891011121314151617fromIPython.displayimportdisplay,HTMLprompt=f"""Yourtaskistoextractrelevantinformationfrom\aproductreviewfromanecommercesitetogive\feedbacktotheShippingdepartmentandgenericfeedbackfromtheproduct.Fromthereviewbelow,delimitedbytriplequotes\constructanHTMLtablewiththesentimentofthereview,generalfeedbackfromtheproductintwosentencesandinformationrelevanttoshippingand\delivery.Review:```{prod_review}```"""response=get_completion(prompt)display(HTML(response))
这是 ChatGPT 的最终输出:
总结:
在本文中,我们讨论了使用 ChatGPT 作为自定义应用程序的摘要代理的最佳实践。
我们看到,在构建应用程序时,要在第一次尝试中就得出符合您的应用程序要求的完美提示是极其困难的。我认为一个很好的带回家的信息是将提示视为一个迭代过程,您可以在其中完善和建模提示,直到获得所需的输出。
通过迭代地完善您的提示并将其应用到一批示例,然后再将其部署到生产中,您可以确保输出在多个示例中保持一致并覆盖异常响应。在我们的示例中,可能有人提供随机文本而不是评论。我们可以指示 ChatGPT 也有一个标准化输出来排除这些异常响应。
此外,当使用 ChatGPT 执行特定任务时,了解使用 LLM 执行目标任务的优缺点也是一个很好的实践。这就是我们如何认识到这样一个事实:当我们想要对输入文本进行类似人类的通用摘要时,提取任务比摘要更有效。我们还了解到,提供摘要的重点可以改变生成内容的游戏规则。
比亚迪旗下高端mpv 腾势d9首台量产车正式下线
最后,虽然法学硕士在生成文本方面非常有效,但它们并不适合遵循有关字数统计或其他特定格式要求的精确说明。为了实现这些目标,可能需要坚持句子计数或使用其他工具或方法,例如手动编辑或更专业的软件。