Openai+Coursera: ChatGPT Prompt Engineering(二)

这篇具有很好参考价值的文章主要介绍了Openai+Coursera: ChatGPT Prompt Engineering(二)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

这是我写的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)

Openai+Coursera: ChatGPT Prompt Engineering(二)

这里我们要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)

Openai+Coursera: ChatGPT Prompt Engineering(二)

之前的总结我们只是要求了一个字数限制(最多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)

Openai+Coursera: ChatGPT Prompt Engineering(二)

 从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)

Openai+Coursera: ChatGPT Prompt Engineering(二)

 总结多个产品评论

接下来我们要让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")

Openai+Coursera: ChatGPT Prompt Engineering(二)

Openai+Coursera: ChatGPT Prompt Engineering(二)

 当我们通过循环遍历的方式来调用ChatGPT的API时,我们会收到一个异常的报错信息:"Rate limit", 从这里我们可以发现OpenAI似乎对API调用做了频率上的限制,即每分钟不能超过3次的api调用,这里我们的文档数量是4篇,因此在第4次循环调用API时超过了次数限制因此抛出了异常错误信息。

解决Rate limit的方法

由于openai官方对api的调用有着严格的限制,对于免费用户每分钟调用API次数不能超过3次,大家可以参考官方对 Rate limits的官方说明:

Openai+Coursera: ChatGPT Prompt Engineering(二)

 同事官方也给出了三种针对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"]

Openai+Coursera: ChatGPT Prompt Engineering(二)

在 给get_completion函数附加了一个回退和重试的装饰器backoff后,我们再循环调用get_completion函后就不再报错,只是需要等待一定的时候后才能得到所有的答案。

请帮我买杯咖啡

如果你觉得我的文章对你有用,请帮我买杯咖啡,这样会使我时刻保持写出优秀博文的欲望和动力,谢谢!文章来源地址https://www.toymoban.com/news/detail-452575.html

到了这里,关于Openai+Coursera: ChatGPT Prompt Engineering(二)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 关于Prompt Engineering你该了解啥?OpenAI应用研究负责人帮你梳理了

    ‍来源 | 机器之心 微信号:almosthuman2014 随着 ChatGPT、GPT-4 等模型的兴起,人们对如何创建提示以获得想要的输出越来越感兴趣。研究者对特定提示的响应可能很难预测,并且会因模型的不同而不同。本文来自 OpenAI 的翁丽莲(Lilian Weng) 撰文介绍了关于提示的一些内容,包括

    2024年02月02日
    浏览(35)
  • Prompt工程师指南[应用篇]:Prompt应用、ChatGPT|Midjouney Prompt Engineering

    主题: 与 ChatGPT 对话 Python 笔记本 Topics: ChatGPT介绍 审查对话任务 与ChatGPT对话 Python笔记本 ChatGPT介绍 ChatGPT是OpenAI训练的一种新型模型,可以进行对话交互。该模型经过训练,可以按照提示中的指令,在对话上下文中提供适当的回应。ChatGPT 可以帮助回答问题、建议菜谱、按

    2024年02月04日
    浏览(55)
  • ChatGPT prompt engineering (中文版)笔记 |吴恩达ChatGPT 提示工程

    出处:https://download.csdn.net/download/weixin_45766780/87746321 感谢中文版翻译https://github.com/datawhalechina/prompt-engineering-for-developers/tree/main/content 国内 == 需要对openapi的endpoint做一个反向代理,并修改本地openai包的源代码== 如下图: completion 原则一:编写清晰、具体的指令 你应该通过提供

    2024年02月03日
    浏览(51)
  • 提升ChatGPT性能的实用指南:Prompt Engineering的艺术

    提示工程是一门新兴学科,就像是为大语言模型(LLM)设计的\\\"语言游戏\\\"。通过这个\\\"游戏\\\",我们可以更有效地引导 LLM 来处理问题。只有熟悉了这个游戏的规则,我们才能更清楚地认识到 LLM 的能力和局限。 这个\\\"游戏\\\"不仅帮助我们理解 LLM,它也是提升 LLM 能力的途径。有效

    2024年02月13日
    浏览(30)
  • 使用 ChatGPT 的 7 个技巧 | Prompt Engineering 学习笔记

    前段时间在 DeepLearning 学了一门大火的 Prompt 的课程,吴恩达本人授课,讲的通俗易懂,感觉受益匪浅,因此在这里总结分享一下我的学习笔记。 为什么要学习 Prompt ? 因为在未来的 AIGC 年代,学习有效的 Promot 提示词有效的利用 AI 来完成一些重复性的工作。这也我认为未来

    2024年02月07日
    浏览(47)
  • 《ChatGPT Prompt Engineering for Developers》课程-提示词原则

    本章的主要内容为编写 Prompt 的原则,在本章中,我们将给出两个编写 Prompt 的原则与一些相关的策略,你将练习基于这两个原则来编写有效的 Prompt,从而便捷而有效地使用 LLM。 本教程使用 OpenAI 所开放的 ChatGPT API,因此你需要首先拥有一个 ChatGPT 的 API_KEY(也可以直接访问

    2024年02月03日
    浏览(39)
  • 【简单入门】ChatGPT prompt engineering (中文版)笔记 |吴恩达ChatGPT 提示工程

    出处:https://download.csdn.net/download/weixin_45766780/87746321 感谢中文版翻译https://github.com/datawhalechina/prompt-engineering-for-developers/tree/main/content 国内 == 需要对openapi的endpoint做一个反向代理,并修改本地openai包的源代码== 如下图: completion 原则一:编写清晰、具体的指令 你应该通过提供

    2024年02月05日
    浏览(48)
  • 《ChatGPT Prompt Engineering for Developers》课程中文版系列

    课程简介 吴恩达《ChatGPT Prompt Engineering for Developers》课程中文版,主要内容为指导开发者如何构建 Prompt 并基于 OpenAI API 构建新的、基于 LLM 的应用,包括: 书写 Prompt 的原则; 文本总结(如总结用户评论); 文本推断(如情感分类、主题提取); 文本转换(如翻译、自动纠

    2024年02月03日
    浏览(44)
  • 提升ChatGPT答案质量和准确性的方法Prompt engineering

    影响模型回答精确度的因素 我们应该知道一个好的提示词,要具备一下要点: 清晰简洁,不要有歧义; 有明确的任务/问题,任务如果太复杂,需要拆分成子任务分步完成; 确保prompt中包含解答问题的必要说明/信息; 指定输出长度,避免浪费token/生成内容过长; 使用分隔

    2024年02月05日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包