Skip to main content

LLM Prompt Engineering - inferring 用 LLM 進行情緒辨識、主題判定

back-to:: LLM Prompt Engineering MOC

讓 LLM 判斷文字情緒

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)
What is the sentiment of the following product review,
which is delimited with triple backticks?

Review text: '''{lamp_review}'''

調整 prompt : 直接 output 情緒

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)
Give your answer as a single word, either "positive"
or "negative".

判斷可能還有哪些情緒

人的情緒不只正面和負面, Ekman(1972)定義了六種基本情緒:驚訝、憤怒、厭惡、快樂、悲傷、恐懼,我們可以給定這些情緒,請 LLM 判斷某段文本含有哪幾項,或是直接請 LLM 判斷可能有哪些情緒

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

判定是否含有特定情緒

Is the writer of the following review expressing anger?
The review is delimited with triple backticks.
Give your answer as either yes or no.

整合: 萃取商品資訊和情緒資訊,輸出 JSON

以往需要好幾隻模型的任務,chatGPT 可以一氣呵成

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)

output :

{
"Sentiment": "positive",
"Anger": false,
"Item": "lamp with additional storage",
"Brand": "Lumina"
}

判斷文章主題

以往都要使用 LDA 這類的語言主題模型,chatGPT 也輕鬆達成

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.

我自己的嘗試 : 如果想要用更學術的分類,可以加上 `The types of topics could be based on field of study.

判斷文章是否含有特定主題

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)

output :

nasa: 1
local government: 0
engineering: 0
employee satisfaction: 1
federal government: 1