这是我写的ChatGPT Prompt Engineerin的第二篇博客,如何还没看过第一篇的请先看我写的第一篇博客:
ChatGPT Prompt Engineerin(一)
Summarizing(总结/摘要)
今天我们的重点关注按特定主题来总结文本。
设置参数
import openai
openai.api_key ='YOUR_OPENAI_API_KEY'
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]
文本总结
下面我们来定义一段文本,该文本来自于一个电商网站的客户评论,我们要让ChatGPT来对这段文本进行总结,由于原文本内容是英文的,我将它的中文翻译文也罗列出来,这样便于大家理解原文的意思:
prod_review = """
Got this panda plush toy for my daughter's birthday, \
who loves it and takes it everywhere. It's soft and \
super cute, and its face has a friendly look. It's \
a bit small for what I paid though. I think there \
might be other options that are bigger for the \
same price. It arrived a day earlier than expected, \
so I got to play with it myself before I gave it \
to her.
"""
prod_review1="""
这个熊猫毛绒玩具是我女儿生日的礼物,她很喜欢,走到哪里都带着它。
它很柔软,超级可爱,它的脸看起来很友好。不过相对于我付的钱来说有点小了。
我想同样的价格可能还有其他更大的选择。它比预期提前了一天到达,所以我在给她之前自己玩了一下。
"""
指定用一个单词/句子/字符的限制来总结文本
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site.
Summarize the review below, delimited by triple
backticks, in at most 30 words.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
这里我们要ChatGPT使用不超过30个词语来总结前面给的文档,ChatGPT用非常简短的一句话对原文做出了总结。
将总结的重点放在配送和交付方面
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
Shipping deparmtment.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that mention shipping and delivery of the product.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
之前的总结我们只是要求了一个字数限制(最多30个词语),没有其他别的要求了,所以ChatGPT在总结是把重点放在了玩具质量和价格方面,配送方面只是简略的提了一下: Arrived early 。现在我们要求ChatGPT将总结的重点放在配送和交付方面,从结果上看ChatGPT的总结非常符合我们的要求。
以价格和价值为重点进行总结
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site to give feedback to the \
pricing deparmtment, responsible for determining the \
price of the product.
Summarize the review below, delimited by triple
backticks, in at most 30 words, and focusing on any aspects \
that are relevant to the price and perceived value.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
从ChatGPT答复的内容上来看也符合我们的要求。
尝试用“内容抽取”来替代“总结”
除了总结以外我们还可以让ChatGPT从文章“抽取”相关内容,可以用这种方式来替代“总结”。
prompt = f"""
Your task is to extract relevant information from \
a product review from an ecommerce site to give \
feedback to the Shipping department.
From the review below, delimited by triple quotes \
extract the information relevant to shipping and \
delivery. Limit to 30 words.
Review: ```{prod_review}```
"""
response = get_completion(prompt)
print(response)
总结多个产品评论
接下来我们要让ChatGPT总结多个产品的评论:
review_1 = prod_review
# review for a standing lamp
review_2 = """
Needed a nice lamp for my bedroom, and this one \
had additional storage and not too high of a price \
point. Got it fast - arrived in 2 days. The string \
to the lamp broke during the transit and the company \
happily sent over a new one. Came within a few days \
as well. It was easy to put together. Then I had a \
missing part, so I contacted their support and they \
very quickly got me the missing piece! Seems to me \
to be a great company that cares about their customers \
and products.
"""
# review for an electric toothbrush
review_3 = """
My dental hygienist recommended an electric toothbrush, \
which is why I got this. The battery life seems to be \
pretty impressive so far. After initial charging and \
leaving the charger plugged in for the first week to \
condition the battery, I've unplugged the charger and \
been using it for twice daily brushing for the last \
3 weeks all on the same charge. But the toothbrush head \
is too small. I’ve seen baby toothbrushes bigger than \
this one. I wish the head was bigger with different \
length bristles to get between teeth better because \
this one doesn’t. Overall if you can get this one \
around the $50 mark, it's a good deal. The manufactuer's \
replacements heads are pretty expensive, but you can \
get generic ones that're more reasonably priced. This \
toothbrush makes me feel like I've been to the dentist \
every day. My teeth feel sparkly clean!
"""
# review for a blender
review_4 = """
So, they still had the 17 piece system on seasonal \
sale for around $49 in the month of November, about \
half off, but for some reason (call it price gouging) \
around the second week of December the prices all went \
up to about anywhere from between $70-$89 for the same \
system. And the 11 piece system went up around $10 or \
so in price also from the earlier sale price of $29. \
So it looks okay, but if you look at the base, the part \
where the blade locks into place doesn’t look as good \
as in previous editions from a few years ago, but I \
plan to be very gentle with it (example, I crush \
very hard items like beans, ice, rice, etc. in the \
blender first then pulverize them in the serving size \
I want in the blender then switch to the whipping \
blade for a finer flour, and use the cross cutting blade \
first when making smoothies, then use the flat blade \
if I need them finer/less pulpy). Special tip when making \
smoothies, finely cut and freeze the fruits and \
vegetables (if using spinach-lightly stew soften the \
spinach then freeze until ready for use-and if making \
sorbet, use a small to medium sized food processor) \
that you plan to use that way you can avoid adding so \
much ice if at all-when making your smoothie. \
After about a year, the motor was making a funny noise. \
I called customer service but the warranty expired \
already, so I had to buy another one. FYI: The overall \
quality has gone done in these types of products, so \
they are kind of counting on brand recognition and \
consumer loyalty to maintain sales. Got it in about \
two days.
"""
reviews = [review_1, review_2, review_3, review_4]
for i in range(len(reviews)):
prompt = f"""
Your task is to generate a short summary of a product \
review from an ecommerce site.
Summarize the review below, delimited by triple \
backticks in at most 20 words.
Review: ```{reviews[i]}```
"""
response = get_completion(prompt)
print(i, response, "\n")
当我们通过循环遍历的方式来调用ChatGPT的API时,我们会收到一个异常的报错信息:"Rate limit", 从这里我们可以发现OpenAI似乎对API调用做了频率上的限制,即每分钟不能超过3次的api调用,这里我们的文档数量是4篇,因此在第4次循环调用API时超过了次数限制因此抛出了异常错误信息。
解决Rate limit的方法
由于openai官方对api的调用有着严格的限制,对于免费用户每分钟调用API次数不能超过3次,大家可以参考官方对 Rate limits的官方说明:
同事官方也给出了三种针对Rate limit 解决方案,经过我的验证其中的第二种可行,第一种方案可能是由于我参数设置不当导致没有成功。以下是第二种方案:需要安装一个为回退和重试提供函数装饰器的库 backoff。
# pip install backoff
import backoff
@backoff.on_exception(backoff.expo, openai.error.RateLimitError)
def get_completion(prompt, model="gpt-3.5-turbo"):
messages = [{"role": "user", "content": prompt}]
response = openai.ChatCompletion.create(
model=model,
messages=messages,
temperature=0,
)
return response.choices[0].message["content"]
在 给get_completion函数附加了一个回退和重试的装饰器backoff后,我们再循环调用get_completion函后就不再报错,只是需要等待一定的时候后才能得到所有的答案。文章来源:https://www.toymoban.com/news/detail-452575.html
请帮我买杯咖啡
如果你觉得我的文章对你有用,请帮我买杯咖啡,这样会使我时刻保持写出优秀博文的欲望和动力,谢谢!文章来源地址https://www.toymoban.com/news/detail-452575.html
到了这里,关于Openai+Coursera: ChatGPT Prompt Engineering(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!