本文討論了如何使用 ChatGPT 作為自定義應(yīng)用程序的摘要代理的最佳實(shí)踐。摘要代理是壓縮信息并提供跨多個(gè)應(yīng)用程序和平臺(tái)對(duì)相關(guān)內(nèi)容的快速訪問的重要工具。通過使用 ChatGPT,我們可以對(duì)文本進(jìn)行摘要和標(biāo)準(zhǔn)化,并識(shí)別評(píng)論的情緒和類別。
然而,需要注意的是,ChatGPT 在嚴(yán)格遵守摘要中的字符或字?jǐn)?shù)限制方面可能存在局限性。分詞器是影響 ChatGPT 字?jǐn)?shù)統(tǒng)計(jì)的關(guān)鍵因素,因此無法精確控制字?jǐn)?shù)。建議在構(gòu)建應(yīng)用程序時(shí)將提示視為一個(gè)迭代過程,并在一批示例中測(cè)試和改進(jìn)提示,以獲得一致和準(zhǔn)確的輸出。
此外,了解在使用 ChatGPT 進(jìn)行摘要時(shí)的優(yōu)缺點(diǎn),以及使用提取任務(wù)代替摘要可以獲得更好的結(jié)果。最后,需要注意 ChatGPT 可能優(yōu)先考慮連貫性和相關(guān)性,而不是嚴(yán)格遵守字?jǐn)?shù)統(tǒng)計(jì)。因此,可能需要采取額外的步驟來確保符合特定格式要求。
如何使用ChatGPT 進(jìn)行有效總結(jié)?下面將通過這幾個(gè)例子來進(jìn)行探索:
示例:電子商務(wù)評(píng)論
考慮上面的示例,其中我們有興趣處理電子商務(wù)網(wǎng)站上給定產(chǎn)品的所有評(píng)論。我們有興趣處理諸如以下有關(guān)我們明星產(chǎn)品的評(píng)論:第一臺(tái)兒童電腦!
12345678910111213prod_review="""Ipurchasedthischildren'scomputerformyson,andheabsolutelyadoresit.Hespendshoursexploringitsvariousfeaturesandengagingwiththeeducationalgames.Thecolorfuldesignandintuitiveinterfacemakeiteasyforhimtonavigate.Thecomputerisdurableandbuilttowithstandroughhandling,whichisperfectforactivekids.Myonlyminorgripeisthatthevolumecouldbeabitlouder.Overall,it'sanexcellenteducationaltoythatprovideshoursoffunandlearningformyson.Itarrivedadayearlierthanexpected,soIgottoplaywithitmyselfbeforeIgaveittohim."""
在這種情況下,我們希望 ChatGPT 能夠:
將評(píng)論分為正面或負(fù)面。提供20字的評(píng)論摘要。使用具體結(jié)構(gòu)輸出響應(yīng),將所有評(píng)論標(biāo)準(zhǔn)化為一種格式。
實(shí)施說明
以下是我們可以用來從自定義應(yīng)用程序提示 ChatGPT 的基本代碼結(jié)構(gòu)。我還提供了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)
該函數(shù)使用給定的提示get_completion()調(diào)用 ChatGPT API。如果提示包含其他用戶文本(例如我們示例中的評(píng)論本身),則它會(huì)通過三引號(hào)與其余代碼分隔開。
讓我們使用get_completion()提示ChatGPT的功能吧!
以下是滿足上述要求的提示:
12345678910prompt=f"""Yourtaskistogenerateashortsummaryofaproductreviewfromane-commercesite.Summarizethereviewbelow,delimitedbytriplebackticks,inexactly20words.Outputajsonwiththesentimentofthereview,thesummaryandoriginalreviewaskeys.Review:```{prod_review}```"""response=get_completion(prompt)print(response)
?? 本示例中使用的提示指南(例如使用分隔符將輸入文本與提示的其余部分分開以及要求結(jié)構(gòu)化輸出)在我從 OpenAI 提示工程課程中學(xué)到的內(nèi)容 — 提示指南中有完整解釋。
以下是 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.”}
正如我們從輸出中可以觀察到的那樣,評(píng)論是準(zhǔn)確且結(jié)構(gòu)良好的,盡管它遺漏了我們作為電子商務(wù)所有者可能感興趣的一些信息,例如有關(guān)產(chǎn)品交付的信息。
重點(diǎn)總結(jié)
我們可以迭代地改進(jìn)要求 ChatGPT 關(guān)注摘要中某些內(nèi)容的提示。在這種情況下,我們對(duì)有關(guān)運(yùn)輸和交付的任何詳細(xì)信息感興趣:
123456789101112131415prompt=f"""Yourtaskistogenerateashortsummaryofaproductreviewfromanecommercesite.Summarizethereviewbelow,delimitedbytriplebackticks,inexactly20wordsandfocusingonanyaspectsthatmentionshippinganddeliveryoftheproduct.Outputajsonwiththesentimentofthereview,thesummaryandoriginalreviewaskeys.Review:```{prod_review}```"""response=get_completion(prompt)print(response)
這次,ChatGPT 的答復(fù)如下:
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.”}
現(xiàn)在審查更加完整。提供有關(guān)原始審查的重點(diǎn)的詳細(xì)信息對(duì)于避免 ChatGPT 跳過一些可能對(duì)我們的用例有價(jià)值的信息至關(guān)重要。
您是否注意到,雖然第二次試驗(yàn)包含有關(guān)交付的信息,但它跳過了原始評(píng)論的唯一負(fù)面方面?
讓我們解決這個(gè)問題!
“摘錄”代替“總結(jié)”
通過調(diào)查摘要任務(wù),我發(fā)現(xiàn)如果用戶提示不夠準(zhǔn)確,摘要對(duì)于法學(xué)碩士來說可能是一項(xiàng)棘手的任務(wù)。
當(dāng)要求 ChatGPT 提供給定文本的摘要時(shí),它可以跳過可能與我們相關(guān)的信息(正如我們最近經(jīng)歷的那樣),或者它會(huì)對(duì)文本中的所有主題給予相同的重要性,僅提供對(duì)文本的概述要點(diǎn)。
大語言模型專家在完成這些類型的模型輔助的此類任務(wù)時(shí),會(huì)使用術(shù)語“摘錄”和附加信息來表達(dá)其關(guān)注點(diǎn),而不是進(jìn)行總結(jié)。
摘要旨在提供文本要點(diǎn)的簡(jiǎn)潔概述,包括與焦點(diǎn)主題無關(guān)的主題,而信息提取則側(cè)重于檢索具體細(xì)節(jié),可以為我們提供我們正在尋找的內(nèi)容。讓我們嘗試提取吧!
1234567891011121314prompt=f"""YourtaskistoextractrelevantinformationfromaproductreviewfromanecommercesitetogivefeedbacktotheShippingdepartment.Fromthereviewbelow,delimitedbytriplequotesextracttheinformationrelevanttoshippinganddelivery.Use100characters.Review:```{prod_review}```"""response=get_completion(prompt)print(response)
在這種情況下,通過使用提取,我們只能獲得有關(guān)我們關(guān)注的主題的信息:Shipping: Arrived a day earlier than expected.
自動(dòng)化
該系統(tǒng)適用于一次審查。然而,在為具體應(yīng)用程序設(shè)計(jì)提示時(shí),重要的是在一批示例中對(duì)其進(jìn)行測(cè)試,以便我們可以捕獲模型中的任何異常值或不當(dāng)行為。
如果要處理多個(gè)評(píng)論,這里有一個(gè)可以提供幫助的示例 Python 代碼結(jié)構(gòu)。
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"""Yourtaskistogenerateashortsummaryofeachproductreviewfromane-commercesite.Extractpositiveandnegativeinformationfromeachofthegivenreviewsbelow,delimitedbytriplebackticksinatmost20wordseach.Extractinformationaboutthedelivery,ifincluded.Review:```{reviews}```"""
以下是我們這批評(píng)論的摘要:
123451. 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.
?? 請(qǐng)注意,盡管我們的提示中摘要的字?jǐn)?shù)限制足夠清楚,但我們可以很容易地看到,在任何迭代中都沒有實(shí)現(xiàn)此字?jǐn)?shù)限制。
字?jǐn)?shù)統(tǒng)計(jì)中出現(xiàn)這種不匹配的情況是因?yàn)榉▽W(xué)碩士對(duì)字?jǐn)?shù)或字符數(shù)沒有準(zhǔn)確的理解。這背后的原因依賴于其架構(gòu)的主要重要組件之一:標(biāo)記器。
分詞器
ChatGPT 等旨在根據(jù)從大量語言數(shù)據(jù)中學(xué)習(xí)到的統(tǒng)計(jì)模式生成文本。雖然它們?cè)谏闪鲿城疫B貫的文本方面非常有效,但它們?nèi)狈?duì)字?jǐn)?shù)的精確控制。
在上面的示例中,當(dāng)我們給出有關(guān)非常精確的字?jǐn)?shù)統(tǒng)計(jì)的說明時(shí),ChatGPT 很難滿足這些要求。相反,它生成的文本實(shí)際上短于指定的字?jǐn)?shù)。
在其他情況下,它可能會(huì)生成較長(zhǎng)的文本或只是過于冗長(zhǎng)或缺乏細(xì)節(jié)的文本。此外,ChatGPT 可能會(huì)優(yōu)先考慮其他因素,例如連貫性和相關(guān)性,而不是嚴(yán)格遵守字?jǐn)?shù)統(tǒng)計(jì)。這可能會(huì)導(dǎo)致文本在內(nèi)容和連貫性方面具有高質(zhì)量,但并不完全符合字?jǐn)?shù)要求。
分詞器是 ChatGPT 架構(gòu)中的關(guān)鍵元素,它明顯影響生成輸出中的單詞數(shù)量。
分詞器架構(gòu)
分詞器是文本生成過程的第一步。它負(fù)責(zé)將我們輸入到 ChatGPT 的文本片段分解為單獨(dú)的元素(標(biāo)記),然后由語言模型處理以生成新文本。
當(dāng)分詞器將一段文本分解為標(biāo)記時(shí),它會(huì)根據(jù)一組旨在識(shí)別目標(biāo)語言的有意義單元的規(guī)則來完成此操作。然而,這些規(guī)則并不總是完美的,并且在某些情況下,分詞器會(huì)以影響文本總字?jǐn)?shù)的方式拆分或合并標(biāo)記。
例如,考慮以下句子:“我想吃花生醬三明治”。如果分詞器配置為根據(jù)空格和標(biāo)點(diǎn)符號(hào)分割標(biāo)記,則它可能會(huì)將此句子分解為以下標(biāo)記,總字?jǐn)?shù)為8,等于標(biāo)記計(jì)數(shù)。
然而,如果分詞器配置為將“花生醬”視為復(fù)合詞,則它可能會(huì)將句子分解為以下標(biāo)記,總字?jǐn)?shù)為8,但標(biāo)記數(shù)為7。
因此,分詞器的配置方式可能會(huì)影響文本的總體字?jǐn)?shù),這可能會(huì)影響大語言模型遵循有關(guān)精確字?jǐn)?shù)計(jì)數(shù)的說明的能力。雖然一些分詞器提供了自定義文本分詞方式的選項(xiàng),但這并不總是足以確保精確遵守字?jǐn)?shù)統(tǒng)計(jì)要求。對(duì)于本例中的ChatGPT,我們無法控制其架構(gòu)的這一部分。
這使得 ChatGPT 不太擅長(zhǎng)完成字符或單詞限制,但可以嘗試使用句子來代替,因?yàn)榉衷~器不會(huì)影響句子的數(shù)量,而是影響句子的長(zhǎng)度。
了解此限制可以幫助您為您的應(yīng)用程序構(gòu)建最合適的提示。了解了 ChatGPT 上字?jǐn)?shù)統(tǒng)計(jì)的工作原理后,讓我們對(duì)電子商務(wù)應(yīng)用程序的提示進(jìn)行最后一次迭代!
總結(jié):電子商務(wù)評(píng)論
讓我們將從本文中學(xué)到的知識(shí)結(jié)合到最終的提示中!在這種情況下,我們將要求結(jié)果的HTML格式以獲得更好的輸出:
1234567891011121314151617fromIPython.displayimportdisplay,HTMLprompt=f"""YourtaskistoextractrelevantinformationfromaproductreviewfromanecommercesitetogivefeedbacktotheShippingdepartmentandgenericfeedbackfromtheproduct.Fromthereviewbelow,delimitedbytriplequotesconstructanHTMLtablewiththesentimentofthereview,generalfeedbackfromtheproductintwosentencesandinformationrelevanttoshippinganddelivery.Review:```{prod_review}```"""response=get_completion(prompt)display(HTML(response))
這是 ChatGPT 的最終輸出:
總結(jié):
在本文中,我們討論了使用 ChatGPT 作為自定義應(yīng)用程序的摘要代理的最佳實(shí)踐。
我們看到,在構(gòu)建應(yīng)用程序時(shí),要在第一次嘗試中就得出符合您的應(yīng)用程序要求的完美提示是極其困難的。我認(rèn)為一個(gè)很好的帶回家的信息是將提示視為一個(gè)迭代過程,您可以在其中完善和建模提示,直到獲得所需的輸出。
通過迭代地完善您的提示并將其應(yīng)用到一批示例,然后再將其部署到生產(chǎn)中,您可以確保輸出在多個(gè)示例中保持一致并覆蓋異常響應(yīng)。在我們的示例中,可能有人提供隨機(jī)文本而不是評(píng)論。我們可以指示 ChatGPT 也有一個(gè)標(biāo)準(zhǔn)化輸出來排除這些異常響應(yīng)。
此外,當(dāng)使用 ChatGPT 執(zhí)行特定任務(wù)時(shí),了解使用 LLM 執(zhí)行目標(biāo)任務(wù)的優(yōu)缺點(diǎn)也是一個(gè)很好的實(shí)踐。這就是我們?nèi)绾握J(rèn)識(shí)到這樣一個(gè)事實(shí):當(dāng)我們想要對(duì)輸入文本進(jìn)行類似人類的通用摘要時(shí),提取任務(wù)比摘要更有效。我們還了解到,提供摘要的重點(diǎn)可以改變生成內(nèi)容的游戲規(guī)則。
最后,雖然大語言模型在生成文本方面非常有效,但它們并不適合遵循有關(guān)字?jǐn)?shù)統(tǒng)計(jì)或其他特定格式要求的精確說明。為了實(shí)現(xiàn)這些目標(biāo),可能需要堅(jiān)持句子計(jì)數(shù)或使用其他工具或方法,例如手動(dòng)編輯或更專業(yè)的軟件。