Openai+Coursera: ChatGPT Prompt Engineering(三)

想和大家分享一下最近学习的Coursera和openai联合打造ChatGPT Prompt Engineering在线课程.以下是我写的关于该课程的前两篇博客:

ChatGPT Prompt Engineering(一)

ChatGPT Prompt Engineering(二)

今天我们来学习第三部分内容:推断(Inferring)

推断(Inferring)

所谓推断是指ChatGPT能够从一段文本中推断出作者的情绪,比如:高兴,快乐,正面,负面等。。。

首先我们需要设置通过API来访问ChatGPT的主要代码:

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提供一段消费者对某电商网站商品的评论,我们希望ChatGPT能够从这段评论中推断出消费者的情绪,由于原始评论是英文的,我将其翻译的中文也罗列出来,这样可以便于大家理解:

lamp_review = """
Needed a nice lamp for my bedroom, and this one had \
additional storage and not too high of a price point. \
Got it fast.  The string to our 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.  I had a missing part, so I contacted their \
support and they very quickly got me the missing piece! \
Lumina seems to me to be a great company that cares \
about their customers and products!!
"""


lamp_review1="""
我的卧室需要一盏漂亮的灯,这盏有额外的存储空间,价格也不太高。速度很快。
我们的灯的绳子在运输过程中断了,公司很高兴地给我们送来了一根新的。几天之内就来了。
很容易组装起来。我有一个缺失的部分,所以我联系了他们的支持,他们很快就给我找到了缺失的部分!
在我看来,Lumina是一家关心客户和产品的伟大公司!!
"""

情绪识别(正面/负面)

下面我们要让ChatGPT针对上面英文的商品评论识别出消费者的情绪是怎么样的.

prompt = f"""
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

 

 结果ChatGP能正确识别出这段评论的情绪是正面的。下面我们只让ChatGPT用一个词语来识别评论的情绪:

prompt = f"""
What is the sentiment of the following product review, 
which is delimited with triple backticks?

Give your answer as a single word, either "positive" \
or "negative".

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

扫描二维码关注公众号,回复: 15247338 查看本文章

 识别情绪类型

下面我们让ChatGPT用最多5个词语来识别评论的情绪:

prompt = f"""
Identify a list of emotions that the writer of the \
following review is expressing. Include no more than \
five items in the list. Format your answer as a list of \
lower-case words separated by commas.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

 

 从上面的ChatGPT的回答可知,ChatGPT用了5个正面的词语来表达消费者的情绪,这5个词语显然是正确的。

识别愤怒

下面我们让ChatGPT识别商品评论是否是“愤怒”的情绪 

prompt = f"""
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either yes or no.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

显然,ChatGPT能正确识别出这段评论没有表达出作者的愤怒情绪。

从客户评论中抽取商品和公司名称

 下面我们要让ChatGPT从商品评论中抽取商品的名称和公司的名称,并以JSON格式返回结果。

prompt = f"""
Identify the following items from the review text: 
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Item" and "Brand" as the keys. 
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
  
Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

 

 ChatGPT能够正确的安装我们指定的格式来返回商品和公司的名称。

同时做多项任务

接下来我们要让ChatGPT同时做以下几个任务:

  1. 情绪识别(正面或者负面)
  2. 愤怒情绪识别(是或否)
  3. 商品识别
  4. 公司识别

并以JSON格式返回结果:

prompt = f"""
Identify the following items from the review text: 
- Sentiment (positive or negative)
- Is the reviewer expressing anger? (true or false)
- Item purchased by reviewer
- Company that made the item

The review is delimited with triple backticks. \
Format your response as a JSON object with \
"Sentiment", "Anger", "Item" and "Brand" as the keys.
If the information isn't present, use "unknown" \
as the value.
Make your response as short as possible.
Format the Anger value as a boolean.

Review text: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)

 

从返回的结果看ChatGPT能按照我们的要求返回正确的结果。 

 推断主题

 下面我们给ChatGPT一共一段文本关于政府对公共部门满意度调查的内容,我们希望ChatGPT能从这段文本中总结出5个相关的主题词,或者说是文章的标题。

story = """
In a recent survey conducted by the government, 
public sector employees were asked to rate their level 
of satisfaction with the department they work at. 
The results revealed that NASA was the most popular 
department with a satisfaction rating of 95%.

One NASA employee, John Smith, commented on the findings, 
stating, "I'm not surprised that NASA came out on top. 
It's a great place to work with amazing people and 
incredible opportunities. I'm proud to be a part of 
such an innovative organization."

The results were also welcomed by NASA's management team, 
with Director Tom Johnson stating, "We are thrilled to 
hear that our employees are satisfied with their work at NASA. 
We have a talented and dedicated team who work tirelessly 
to achieve our goals, and it's fantastic to see that their 
hard work is paying off."

The survey also revealed that the 
Social Security Administration had the lowest satisfaction 
rating, with only 45% of employees indicating they were 
satisfied with their job. The government has pledged to 
address the concerns raised by employees in the survey and 
work towards improving job satisfaction across all departments.
"""

 推断5个主题(标题)

我们要求ChatGPT从上面的文本中推断出5个主题词,并且每个主题词不能超过2个单词。

prompt = f"""
Determine five topics that are being discussed in the \
following text, which is delimited by triple backticks.

Make each item one or two words long. 

Format your response as a list of items separated by commas.

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)

 从结果上看这5个主题词基本满足要求。

为某些主题制作新闻提醒(Make a news alert for certain topics)

在前面我们看到ChatGPT能够识别商品的评论是否是"愤怒"情绪,这里我们也可以让ChatGP来判断文本是否表达某种主题,下面我们事先设置5个主题词,然后让ChatGPT来识别上面的文本是否符合这5个自定义的主题词,如果符合则标记为1,不符合则标记为0.

topic_list = [
    "nasa", "local government", "engineering", 
    "employee satisfaction", "federal government"
]


prompt = f"""
Determine whether each item in the following list of \
topics is a topic in the text below, which
is delimited with triple backticks.

Give your answer as list with 0 or 1 for each topic.\

List of topics: {", ".join(topic_list)}

Text sample: '''{story}'''
"""
response = get_completion(prompt)
print(response)

topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) 
              for i in response.split(sep='\n')}
if topic_dict['nasa'] == 1:
    print("ALERT: New NASA story!")

 这里我们看到自定义的两个主题词:local government(地方政府)、engineering(工程)和我们的文本内容不相关,因此它们被标记为0。其他三个词都与文本内容有一定的相关性,因此都被标记为了1.

总结

今天我们学习了如何让ChatGPT从文本内容中推断出作者的情绪、识别愤怒情绪、抽取文本实体(如商品和公司名称)、推断主题词、识别主题词等功能,我们还学习了如何让ChatGPT来同时做多个任务。希望这些内容对大家有所帮助!

参考资料

Courses - DeepLearning.AI

猜你喜欢

转载自blog.csdn.net/weixin_42608414/article/details/130814811