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

}

相关推荐
用户691581141651 小时前
Ascend Extension for PyTorch的源码解析
人工智能
用户691581141651 小时前
Ascend C的编程模型
人工智能
成富2 小时前
文本转SQL(Text-to-SQL),场景介绍与 Spring AI 实现
数据库·人工智能·sql·spring·oracle
CSDN云计算2 小时前
如何以开源加速AI企业落地,红帽带来新解法
人工智能·开源·openshift·红帽·instructlab
艾派森2 小时前
大数据分析案例-基于随机森林算法的智能手机价格预测模型
人工智能·python·随机森林·机器学习·数据挖掘
hairenjing11232 小时前
在 Android 手机上从SD 卡恢复数据的 6 个有效应用程序
android·人工智能·windows·macos·智能手机
小蜗子2 小时前
Multi‐modal knowledge graph inference via media convergenceand logic rule
人工智能·知识图谱
SpikeKing2 小时前
LLM - 使用 LLaMA-Factory 微调大模型 环境配置与训练推理 教程 (1)
人工智能·llm·大语言模型·llama·环境配置·llamafactory·训练框架
黄焖鸡能干四碗3 小时前
信息化运维方案,实施方案,开发方案,信息中心安全运维资料(软件资料word)
大数据·人工智能·软件需求·设计规范·规格说明书
3 小时前
开源竞争-数据驱动成长-11/05-大专生的思考
人工智能·笔记·学习·算法·机器学习