ChatGPT Prompting开发实战(九)

一、什么是推理式的prompt开发

有时候需要针对一些产品的评论文本进行分析,常见的做法如对每段评论所表达的情感倾向进行推理判断,识别用户对这个产品的使用体验是否满意,那么可以编写相关的prompt来做这样的推理分析。另外,针对不同的文本内容,也可以根据给出的主题来让模型判断一段内容属于什么样的主题。

接下来会给出具体示例,通过调用模型"gpt-3.5-turbo"来演示并解析如何针对上述需求来编写相应的prompts。

二、结合案例演示解析如何使用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!!

"""

需求是针对这段评论分析用户的情感倾向是什么,譬如说是正面的还是负面的评论。

prompt示例如下:

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)

打印输出结果如下:

The sentiment of the product review is positive.

接下来修改prompt,给出设定的情感倾向分类如"positive","negative",要求模型按照这个分类来对评论内容进行判断。

prompt示例如下:

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)

打印输出结果如下:

positive

接下来的需求是指导模型分析评论文本,给出最多5个情感类型,继续修改prompt如下:

prompt示例如下:

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)

打印输出结果如下:

satisfied, grateful, impressed, pleased, happy

下面通过修改prompt,要求模型推理这段文本所表达的情感倾向是否属于给出的类型。

prompt示例如下:

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)

打印输出结果如下:

No

接下来针对文本内容要求识别出/抽取关键信息。

prompt示例如下:

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)

打印输出结果如下:

{

"Item": "lamp",

"Brand": "Lumina"

}

上面是分步编写prompt来完成对应的任务,那么我们也可以把这些任务融合在一个prompt中,让模型一次性输出我们想要的结果。

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)

打印输出结果如下:

{

"Sentiment": "positive",

"Anger": false,

"Item": "lamp",

"Brand": "Lumina"

}

三、结合案例演示解析如何使用prompt进行文本主题推理

首先给出需要进行主题推理的一段文本,具体内容如下:

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个。

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)

打印输出结果如下:

  1. Government survey

  2. Department satisfaction rating

  3. NASA

  4. Social Security Administration

  5. Job satisfaction improvement

接下来给出以下5个主题列表:

topic_list = [

"nasa", "local government", "engineering",

"employee satisfaction", "federal government"

]

要求模型判断上述文本内容是否涉及到这些主题,如果有涉及到则返回1,否则返回0。

prompt示例如下:

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

Format your response as a JSON object with \

each topic as the keys

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

Text sample: '''{story}'''

"""

response = get_completion(prompt)

print(response)

打印输出结果如下:

{

"nasa": 1,

"local government": 0,

"engineering": 0,

"employee satisfaction": 1,

"federal government": 1

}

相关推荐
martian66517 分钟前
【人工智能数学基础篇】——深入详解多变量微积分:在机器学习模型中优化损失函数时应用
人工智能·机器学习·微积分·数学基础
人机与认知实验室1 小时前
人、机、环境中各有其神经网络系统
人工智能·深度学习·神经网络·机器学习
黑色叉腰丶大魔王1 小时前
基于 MATLAB 的图像增强技术分享
图像处理·人工智能·计算机视觉
迅易科技4 小时前
借助腾讯云质检平台的新范式,做工业制造企业质检的“AI慧眼”
人工智能·视觉检测·制造
古希腊掌管学习的神5 小时前
[机器学习]XGBoost(3)——确定树的结构
人工智能·机器学习
ZHOU_WUYI6 小时前
4.metagpt中的软件公司智能体 (ProjectManager 角色)
人工智能·metagpt
engchina6 小时前
多模态抽取图片信息的 Prompt
prompt·多模态·抽取图片信息
靴子学长6 小时前
基于字节大模型的论文翻译(含免费源码)
人工智能·深度学习·nlp
AI_NEW_COME7 小时前
知识库管理系统可扩展性深度测评
人工智能
海棠AI实验室8 小时前
AI的进阶之路:从机器学习到深度学习的演变(一)
人工智能·深度学习·机器学习