Inferring
推断可以看作是模型接收文本作为输入并执行分析的过程,这可能涉及到标签处理、数据清洗、理解文本情感等等
传统的机器学习过程中,可能需要收集标签集,从头开始训练一个模型,然后部署在云上进行推理
LLM 非常好的一个特点就是,对于各种各样的任务,只需要编写一个 Prompt 即可开始产生结果,并不需要大量的训练,加快了应用程序开发的速度
商品评论
python
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,来分类情感,刚开始这个提示词还是比较抽象的,只是让 LLM 对评论进行情感描述
python
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)
情感是积极的,但是它废话有点长,我们可以修改一下 Prompt
python
prompt = f"""
What is the sentiment of the following product review,
which is delimited with triple backticks?
Give your answer in one word, either "positive" or "negative".
Review text: ```{lamp_review}```"""
response = get_completion(prompt)
print(response)
识别情感类型
下面是对内容进行情感类型识别,我们要求 LLM 对评论内容进行识别,并且将其输出为列表,不超过 5 个
python
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)text
happy, satisfied, grateful, impressed, pleased特定情绪识别(愤怒)
对于一些需要识别特定类型的场景,比如说愤怒或者其他不太友好的情绪,可以直接使用二分法,让 LLM 直接输出 0 或 1 的结果,方便处理
python
prompt = f"""
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either true or false.
Review text: ```{lamp_review}```"""
response = get_completion(prompt)
print(response)
这样的判断过于粗糙,我们可以继续在提示词里面进行完善,比如说可以输出 Possibility_Score
python
prompt = f"""
Is the writer of the following review expressing anger?\
The review is delimited with triple backticks. \
Give your answer as either true or false.
And you can give a possibility score between 0 and 1.
No more explanations, just the answer.
With JSON Schema:
```json
{{
"is_angry": bool,
"possibility_score": float
}}
```Review text: ```{lamp_review}```"""
response = get_completion(prompt)
print(response)但是需要注意的是,LLM 对于这种概率相关的输出是随机的,可能并不可靠,一种很好的方法是设置一个阈值来进行判断

从客户评论中提取产品和公司信息
信息提取是自然语言处理(NLP)的一部分,与从文本中提取你想要知道的某些事物相关。因此,在这个 prompt 中,我要求它识别以下内容:购买物品和制造物品的公司名称
python
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)
Prompt 融合
事实上,可以将多个 Prompt 融合为一个 Prompt,同时完成上面的工作
python
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)
推断主题
给定一个长文本,可以交给 LLM,让它推断主题,生成一些类似 Tag 的东西,在对文章进行分类的时候也可以起作用
python
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.
"""推断五个主题
python
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)最后的输出结果是类似 Markdown 语法一样的无序列表

还可以修改 Prompt,来让它输出成我们需要的
