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

}

相关推荐
ZHOU_WUYI4 小时前
3.langchain中的prompt模板 (few shot examples in chat models)
人工智能·langchain·prompt
如若1234 小时前
主要用于图像的颜色提取、替换以及区域修改
人工智能·opencv·计算机视觉
老艾的AI世界4 小时前
AI翻唱神器,一键用你喜欢的歌手翻唱他人的曲目(附下载链接)
人工智能·深度学习·神经网络·机器学习·ai·ai翻唱·ai唱歌·ai歌曲
DK221514 小时前
机器学习系列----关联分析
人工智能·机器学习
Robot2514 小时前
Figure 02迎重大升级!!人形机器人独角兽[Figure AI]商业化加速
人工智能·机器人·微信公众平台
浊酒南街5 小时前
Statsmodels之OLS回归
人工智能·数据挖掘·回归
畅联云平台6 小时前
美畅物联丨智能分析,安全管控:视频汇聚平台助力智慧工地建设
人工智能·物联网
加密新世界6 小时前
优化 Solana 程序
人工智能·算法·计算机视觉
hunteritself6 小时前
ChatGPT高级语音模式正在向Web网页端推出!
人工智能·gpt·chatgpt·openai·语音识别
Che_Che_6 小时前
Cross-Inlining Binary Function Similarity Detection
人工智能·网络安全·gnn·二进制相似度检测