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

}

相关推荐
井底哇哇4 小时前
ChatGPT是强人工智能吗?
人工智能·chatgpt
Coovally AI模型快速验证4 小时前
MMYOLO:打破单一模式限制,多模态目标检测的革命性突破!
人工智能·算法·yolo·目标检测·机器学习·计算机视觉·目标跟踪
AI浩4 小时前
【面试总结】FFN(前馈神经网络)在Transformer模型中先升维再降维的原因
人工智能·深度学习·计算机视觉·transformer
可为测控4 小时前
图像处理基础(4):高斯滤波器详解
人工智能·算法·计算机视觉
一水鉴天5 小时前
为AI聊天工具添加一个知识系统 之63 详细设计 之4:AI操作系统 之2 智能合约
开发语言·人工智能·python
倔强的石头1065 小时前
解锁辅助驾驶新境界:基于昇腾 AI 异构计算架构 CANN 的应用探秘
人工智能·架构
佛州小李哥6 小时前
Agent群舞,在亚马逊云科技搭建数字营销多代理(Multi-Agent)(下篇)
人工智能·科技·ai·语言模型·云计算·aws·亚马逊云科技
说私域6 小时前
社群裂变+2+1链动新纪元:S2B2C小程序如何重塑企业客户管理版图?
大数据·人工智能·小程序·开源
程序猿阿伟6 小时前
《探秘鸿蒙Next:如何保障AI模型轻量化后多设备协同功能一致》
人工智能·华为·harmonyos
2401_897579657 小时前
AI赋能Flutter开发:ScriptEcho助你高效构建跨端应用
前端·人工智能·flutter