ChatGPT Prompting开发实战(九)

2023-09-18 08:00:00

一、什么是推理式的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

}

更多推荐

mybati缓存了解

title:“mybati缓存了解”createTime:2021-12-08T12:19:57+08:00updateTime:2021-12-08T12:19:57+08:00draft:falseauthor:“ggball”tags:[“mybatis”]categories:[“java”]descripti

软键盘控制cesium相机移动旋转

1.有航向类似于控制飞机飞行<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=edge"><metaname="viewport"content="width=devi

刷题笔记25——图论课程表

为了最终理解你所不理解的,你必须经历一条愚昧无知的道路。为了占有你从未占有的东西,你必须经历被剥夺的道路。为了达到你现在所不在的名位,你必须经历那条你不在其中的道路。——艾略特797.所有可能的路径(已经告知:是有向无环图,所以不需要设置visited)非常奇妙,我最初的错误是如下,在找到目标节点后直接加入到res中,

混合应用比原生应用优越在哪里?

随着移动应用和桌面应用市场的不断发展,开发者们一直在寻找一种能够在多个平台上快速构建应用的方法。传统上,原生应用开发被视为性能最佳的选择,纯粹的原生应用通常是一种依赖于平台的GUI程序,它使用特定操作系统的本地开发语言和GUI框架。但近年来,跨平台混合应用的崭露头角,逐渐取代了性能优先的原生应用。本文将深入探讨这一趋势

安全线程的集合

1.CopyOnWriteArrayListpackagecom.kuang.unsafe;importjava.util.*;importjava.util.concurrent.CopyOnWriteArrayList;//java.util.ConcurrentModificationException并发修改异

Spring高手之路13——BeanFactoryPostProcessor与BeanDefinitionRegistryPostProcessor解析

文章目录1.BeanFactoryPostProcessor概览1.1解读BeanFactoryPostProcessor1.2.如何使用BeanFactoryPostProcessor2.BeanDefinitionRegistryPostProcessor深入探究2.1解读BeanDefinitionRegistr

云原生的简单理解

一、何谓云原生?一种构建和运行应用软件的方法应用程序从设计之初即考虑到云的环境,原生为云而设计,在云上以最佳姿势运行,充分利用和发挥云平台的弹性+分布式优势。二、包括以下四个要素采用容器化部署:实现云平台的弹性基于微服务的架构:提高服务变更的灵活性和可维护性借助敏捷防范、DevOps支持持续迭代和运维自动化;1.1、微

QT中的inherits

目录简介:实例:简介:在Qt中,可以使用inherits函数来判断一个对象是否属于某个类或其派生类。inherits函数是QObject类的成员函数,因此只能用于继承自QObject的类的对象。以下是inherits函数的一般用法:boolQObject::inherits(constchar*classname)co

Long类型雪花算法ID返回前端后三位精度缺失问题解决

目录一、问题描述二、问题复现1.Maven依赖2.application.yml配置3.DemoController.java4.snowflakePage.html页面5.DemoControllerAdvice.java监听6.问题复现三、原因分析四、问题解决方案一方案二一、问题描述Java后端使用雪花算法生成Lo

【大数据实训】基于Hive的北京市天气系统分析报告(二)

博主介绍:✌全网粉丝6W+,csdn特邀作者、博客专家、Java领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于大数据技术领域和毕业项目实战✌🍅文末获取项目联系🍅目录1.引言1.1项目背景11.2项目意义12.需求分析22.1数据清洗需求分析22.2数据存储需求分析22.3MapRe

机器人还可以支持呼入?

呼入机器人是指一种能够接听电话并进行自动语音交互的人工智能软件系统。与传统的人工客服不同,呼入机器人可以根据预设的逻辑和语音识别技术进行自动回复和处理来电者的问题或需求,无需人工干预。这种软件通常能够帮助办公室工作人员更加高效地完成日常工作。通过使用呼入机器人,工作人员可以节省大量时间,并且能够更好地专注于高级工作。呼

热文推荐