OpenAI官方提示词课(四)如何进行文字的情感分析

news2024/7/4 5:16:40

这节介绍大模型判断文字的语义,或者说对内容进行情感分析的能力。同时也演示了大模型如何提取出文字中的关键信息。

在传统的机器学习方案中,要做到对文字内容的情感分析,需要先对一系列的文字内容(如评论)进行人工标注。把这些文字内容人工分类成“正向”和负向“,然后再喂给一个机器学习模型去训练,得到一组参数。模型训练好后再部署好,把需要判断的未标注文字内容给到训练好的模型,让它判断一下文字内容的情感倾向。

可以看到,对于传统的机器学习方案,有很多工作需要做。而且这个训练出来的模型也只能干这一件事情。如果我们想要提取文字内容中的关键信息又得重新训练另外一个单独的模型。

在大模型时代,做到这些我们仅仅只要写一下提示词。

下面是一个购买台灯客户的评论。这个例子演示了如何使用大模型来分析文字内容的情绪。

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 = 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 = 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

这里用中文试试。

lamp_deliver_review = """
快递太慢了。很久都没有收到货。
"""
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 "正向" \
or "负向".

Review text: '''{lamp_deliver_review}'''
"""
response = get_completion(prompt)
print(response)

回答:

负向

分辨出情绪的类型

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)

回答:

happy, satisfied, grateful, impressed, content

判断是否生气

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 = 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 = 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 with additional storage",
  "Brand": "Lumina"
}

推理

分析一段文字中讲述了哪些话题。

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.
"""
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)

回答:

government survey, job satisfaction, NASA, Social Security Administration, employee concerns

判断文字内容中是否有讲述相关话题内容。

topic_list = [
    "nasa", "local government", "engineering", 
    "employee satisfaction", "federal government"
]
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.\

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

判断是否有NASA相关内容。

topic_dict = {i.split(': ')[0]: int(i.split(': ')[1]) for i in response.split(sep='\n')}
if topic_dict['nasa'] == 1:
    print("ALERT: New NASA story!")

输出:

ALERT: New NASA story!

参考:

https://learn.deeplearning.ai/chatgpt-prompt-eng/lesson/5/inferring


觉得有用就点个赞吧!

我是首飞,做有趣的事情,拿出来分享。

我也准备了一份提示词的文档。有涉及到各个领域的提示词模板。
在这里插入图片描述
您可以在《首飞》公众号中回复“ 提示词 ” 获取该文档。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/636566.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

如何拍照识别植物?拍照识别植物的方法教学

植物是我们生活中不可或缺的一部分,它们不仅为我们提供氧气和美丽的景观,还是人类食品和药物来源之一。而随着科技水平的提高,越来越多的研究者开始探索如何利用图像识别技术对植物进行自动化识别和分类,以帮助我们更好地了解植物…

英雄算法联盟 | 七月集训报名通道开启

文章目录 前言一、英雄算法集训二、编程零基础预训练三、九日集训四、咨询答疑五、常见问题六、报名方式 前言 通知:英雄算法联盟六月集训 已经开始12天,七月算法集训将于 07月01日 正式开始,目前已经提前开始报名,报名方式见文末…

【大数据】一篇文章带你入门HBase

本文已收录至Github,推荐阅读 👉 Java随想录 文章目录 HBase特性Hadoop的限制基本概念NameSpaceTableRowKeyColumnTimeStampCell 存储结构HBase 数据访问形式架构体系HBase组件HBase读写流程读流程写流程 MemStore Flush参数说明 StoreFile Compaction参…

剑指 Offer 09: 用两个栈实现队列

简单明了,带你直接看懂题目和例子。 输入: ["CQueue","appendTail","deleteHead","deleteHead"] 这里是要执行的方法,从左到右执行 [[],[3],[],[]]对应上面的方法,是上面方法的参数。CQ…

Android Fragment跳转Activity使用startActivityForResult获取返回值

前言 Fragment跳转Activity使用startActivityForResult获取返回值 如果直接获取是获取不到结果的 需要在fragment所属的activity中遍历 Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resul…

智驾差异化周期下的「芯」风向

随着中国市场进入智能驾驶「差异化」竞争周期,车企对于核心算力芯片的可选项,正在变得越来越多。一方面,车企在寻求更高性价比的替代(升级)方案;另一方面,多元化的需求(舱泊一体、行…

解决获取taro全局配置文件失败,找不到配置文件失败问题

问题:这会导致项目初始化不成功,即要用vuets生成项目的话,依旧是wxml,js的文件,而不是vue文件 解决一:首先找到配置文件目录 删除taro开头的三项文件,再去node_modules下删除tarojs 然后去终…

Excel怎么设置密码?这4个方法必须掌握!

案例:做报表时有些很重要的数据不想被别人改动,Excel怎么设置密码呀? 【用Excel制作一些报表时怎么为Excel设置密码呢?因为有些数据比较重要,想将Excel设置密码。请大家帮帮我!】 Excel是一款常用的电子表…

推荐几个好用的AI 工具

文章目录 思维导图gmindAI文档写作工具notion aiAI 辅助阅读工具:ChatDOCAI 笔记软件& 知识库:FlowUsAI 一键生成 PPT:ChatPPT、MotionGo专业 PPT 插件:iSlideAI 智能设计工具:Logosc 标小智 思维导图gmind https:…

Linux内核中内存管理相关配置项的详细解析4

接前一篇文章:Linux内核中内存管理相关配置项的详细解析3 二、SLAB allocator options 1. Choose SLAB allocator 此选项选择一个slab分配器。 此项展开后如下图所示: SLAB 对应配置变量为:CONFIG_SLAB。 内核源码详细解释为&#xff1a…

Vue3 + TS + Vite —— 大屏可视化 项目实战

前期回顾 Vue3 Ts Vite pnpm 项目中集成 —— eslint 、prettier、stylelint、husky、commitizen_彩色之外的博客-CSDN博客搭建VIte Ts Vue3项目并集成eslint 、prettier、stylelint、huskyhttps://blog.csdn.net/m0_57904695/article/details/129950163?spm1001.2014…

一年肝4个项目,10万+行代码,面试妥妥的

大家好,我是冰河~~ 最近很多小伙伴私信问我:目前所在的公司工资比较低,已经很久没有涨薪了,想跳槽,找一份工资更高的工作,但是苦于平时所做的项目就是一些简单的CRUD操作,没有什么技术含量&…

习惯了VSCode的快捷键,如何让HbuilderX快捷键也和VSCode一样?

hbuilderX uni-app 自定义快捷键无效、无法生效解决方法(附:好用的常用的快捷键自定义代码片段)_你挚爱的强哥的博客-CSDN博客才能让原有默认的快捷键被覆盖。https://blog.csdn.net/qq_37860634/article/details/131161953

如何在 Python 中给请求设置用户代理 User-Agent

文章目录 了解 HTTP 标头的工作原理在 Python 中获取用户代理数据在 Python 中使用请求设置用户代理值在 Python 中为请求版本 2.12 设置用户代理请求在 Python 中为请求版本 2.13 设置用户代理请求 本文介绍 HTTP 标头用户代理主题以及如何使用 Python 中的请求设置用户代理。…

Python实验: tkinker 的实践

一、实验内容 1、登录界面 2、制作菜单栏 3、实现聊天窗口 4、访问本地一张照片并展示 二、实验过程 1、 import tkinter as tk import tkinter.messagebox import pickleroot tkinter.Tk() root.geometry(400x300) root.title("武理工欢迎你!")# 画布…

2023-6-11-第二式抽象工厂模式

🍿*★,*:.☆( ̄▽ ̄)/$:*.★* 🍿 💥💥💥欢迎来到🤞汤姆🤞的csdn博文💥💥💥 💟💟喜欢的朋友可以关注一下&#xf…

【macbookpro】重装ventura系统

intel 的cpu 的macbook pro 13寸定制版本。 大神们遇到的问题很专业 格式化 mac数据恢复 更新软件 通过“macOS 恢复”重新安装 macOS 大神们说固件是因为时间不对,从美国服务器下载的 打开日志,我看了下我的时间也不对 好像至少要下载2个文件,一…

MATLAB涡度通量数据处理实践技术应用

基于MATLAB语言、以实践案例为主,提供代码、原理与操作结合 1、以涡度通量塔的高频观测数据为例:基于MATLAB开展上机操作 2、涡度通量观测基本概况:观测技术方法、数据获取与预处理等 3、涡度通量数据质量控制:通量数据异常值识…

EPICS boRecord驱动程序编写和使用示例

以下示例程序将展示如何使用gpio的动态链接库编写一个boRecord的驱动程序,并且展示如何使用这个程序,控制一个LED灯的亮灭。 1) 新建这个示例程序的顶层目录,并且用makeBaseApp.pl在新建目录中构建这个IOC程序程序框架&#xff1…

前端项目安全扫描出来的漏洞——解决过程

为什么要升级,如图云桌面(相当于堡垒机-远程桌面)的项目审查是大概基于node16版本进行扫描的,本来我方是通过降版本从14到12绕过大范围更新,但现在躲得过初一躲不过十五,如何更新 package-lock.json 中的一…