如何使用ChatGPT得到更满意的结果(5):面向开发者的ChatGPT Prompt Engineering (Inferring)

承接上篇,继续整理关于面向开发者的Prompt Engineering教程笔记,教程之前的部分见:如何使用ChatGPT得到更满意的结果:Prompt Engineering (2)_Dorothy30的博客-CSDN博客

如何使用ChatGPT得到更满意的结果(3):面向开发者的ChatGPT Prompt Engineering (迭代式Prompt开发)_Dorothy30的博客-CSDN博客

如何使用ChatGPT得到更满意的结果(4):面向开发者的ChatGPT Prompt Engineering (Summarizing)_Dorothy30的博客-CSDN博客

整理的内容基本上英文原文都来自于教程,有需要的朋友可以直接戳https://learn.deeplearning.ai/。这篇笔记主要整理教程中Inferring的部分,使用prompt进行推断。

目录

1 针对产品评论的情感分析

1.1 用一句话进行回答(获知评论包含的情感)

1.2 用一个词进行回答

1.3 识别评论中的一系列情绪

1.4 从评论中提取产品名称和公司名称

1.5 一次性完成多个任务

 2 主题推断

2.1 推断文本所包含的主题

2.2 主题归类


推断指的是模型接受文本作为输入并执行某种分析。这种分析可能是提取标签,提取名称,理解文本的情感等。这部分的示例分为两个部分,第一部分是根据产品评论来对情感进行分类,第二部分是对话题进行推断。

1 针对产品评论的情感分析

以下为关于台灯的一则评论,接下来的prompt示例都将以此作为基础展开

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!!
"""

1.1 用一句话进行回答(获知评论包含的情感)

若你想获知这条评论包含的情感,那么可以采用的prompt以及对应的结果如下

 

1.2 用一个词进行回答

当然,上述任务也可以仅输出一个单词(积极或消极),这样可以更容易地处理输出的文本并进行进一步操作。

 

1.3 识别评论中的一系列情绪

很多时候一句话包含的情绪是十分丰富的,在这里,我们可以要求它识别评论中作者表达的一系列情绪列表,列表中包含的情绪个数不超过五。

 

1.4 从评论中提取产品名称和公司名称

信息提取是自然语言处理(NLP)中的任务之一,它从一段文本中提取你想要了解的特定信息。比如,在以下的prompt中就要求提取的是评论者购买的物品以及生产这件商品的公司。

 

1.5 一次性完成多个任务

在之前提供的示例中,你已经知道了如何编写prompt来识别情感,判断某人的情绪,并提取商品和品牌。提取所有这些信息的一种方式是使用一连串的prompt,并调用get completion。

但我们也可以编写一个单独的prompt来同时提取所有这些信息。

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)

 2 主题推断

大型语言模型的一个很酷的应用是推断主题。给定一段长文本,它能够推断出这段文本是涉及哪些主题的。在后面的示例里,我们都基于以下的文本:

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.
"""

2.1 推断文本所包含的主题

给定一篇文章,我们可以使用prompt来确定文本中正在讨论的五个主题。我们可以把每个主题的长度限制在一到两个词之间。

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)

 

2.2 主题归类

如果你有很多篇文章,并且你想从这些文章中提取出主题,你可以使用大型语言模型来帮助你对不同主题进行索引。例如我们给定以下的一个主题列表,你想弄清楚,在给定的新闻文章中涉及了哪些主题。

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)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值