提示工程(4):推断(Inferring)能力

如何从产品评论和新闻文章里推断情感和主题呢?传统的做法,这些任务可以看作是模型接收文本作为输入并执行某种分析的过程。

这可能涉及提取标签、提取实体、理解文本情感等等。如果你想要从一段文本中提取正面或负面情感,在传统的机器学习工作流程中,需要收集标签数据集、训练模型、确定如何在云端部署模型并进行推断。

这样做可能效果还不错,但是这个过程需要很多工作。而且对于每个任务,如情感分析、提取实体等等,都需要训练和部署单独的模型。
现在要做的就是写提示prompt,结果就能够马上出来。

我也可以也利用一个模型,就能应对之前需要多个模型处理的工作。接下来我们举些例子。

一、识别情绪

import openai
openai.api_key="your openai key"
def get_completion (prompt, model="gpt-3.5-turbo"): 
    messages = [{"role": "user", "content": prompt}] 
    response = openai.ChatCompletion.create( 
        model=model, 
        messages=messages, 
        temperature=0
    ) 
    return response.choices[0].message["content"]

以下是关于一个台灯的产品评论。

# review for a standing lamp
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 - arrived in 2 days. The string 
to the 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. Then 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!
"""

我们想了解一下上述评论的情绪,是正面的(positive)还是负面的(negative)?

prompt = f"""
What is the sentiment of the following product review, which is delimited with tripe backticks?
Review: '''{lamp_review}'''
"""

response = get_completion(prompt)
print (response)
The sentiment of the product review is positive.

更简洁的答案。一个字是positive还是negative?

prompt = f"""
What is the sentiment of the following product review, which is delimited with tripe backticks?

Give your answer as a single word, either "positive" or "negative".

Review: '''{lamp_review}'''
"""

response = get_completion(prompt)
print (response)
positive

我们让模型来识别这段评价有几种情况?可能不是一种,可能有多种。我们来试试。
no more that five item (emtions)。

prompt = f"""
Identify a list of emotions that the writer of the following review is expressing.
Include no more that five items in the list. Format your answer as a list of low-case words separated by commas.

Review: '''{lamp_review}'''
"""

response = get_completion(prompt)
print (response)
happy, satisfied, impressed, grateful, pleased

效果非常好。大模型很擅长识别情感。

让模型来识别是否有“愤怒”的情绪,请见下例。

prompt = f"""
Is the writer of the following review expression anger?
The review is delimited with triple backticks.
Give your answer as either yes or no.

Review: '''{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: '''{lamp_review}'''
"""
response = get_completion(prompt)
print(response)
{
  "Item": "lamp",
  "Brand": "Lumina"
}

结合上面的例子,让模型分析产品评论,并结构化输出:商品、品牌、情绪(正面/负面)、是否愤怒的的内容,并以JSON形式展示。

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": "unknown"
}

二、推断主题

推断主题是大模型另一个方面的应用。当我们阅读一篇新闻,这段新闻是关于什么主题的呢?如:科技、电影、音乐等等。大模型可以推断出来。请看下面的例子。

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 958.

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 458 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)
1. Government survey
2. Department satisfaction
3. NASA
4. Social Security Administration
5. Job satisfaction improvement
response.split(sep=", ")
['1. Government survey\n2. Department satisfaction\n3. NASA\n4. Social Security Administration\n5. Job satisfaction improvement']

以上是关于上篇文章相关的主题。

试想我们希望通过大模型去阅读多篇报道,并统计我们感兴趣主题(nasa, local government, engineering, employee satisfaction, federal government)有多少篇文章。请看下面的示例。

以下是我们需要统计的主题。

topic_list = [
    "nasa", "local government", "engineering",
    "employee satisfaction", "federal government"
]

让大模型进行主题推断,并以我们要求的方式输出。

prompt = f"""
Determine whetner 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 bullet 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

以上是关于应用大模型进行推断的内容了,主要包括情绪识别和主题推断。接下来我们讲转换。

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注