大規模言語モデル(LLM)のプロンプトワードプロジェクト(3)

今日はDeepLearning.AIのプロンプトエンジニアリングのオンラインコースを受講したので、その主な内容の一部をシェアしたいと思います。このコースについて私が書いた最初の 2 つのブログは次のとおりです。

大規模言語モデル (LLM) のプロンプト ワード プロジェクト (1)

大規模言語モデル(LLM)のプロンプトワードプロジェクト(2)

今日は 3 番目の部分「推論」を学習します。

推測する

いわゆる推論とは、ChatGPT がテキストの一部から、嬉しい、幸せ、ポジティブ、ネガティブなどの作成者の感情を推測できることを意味します。

まず、API 経由で ChatGPT にアクセスするようにメイン コードを設定する必要があります。

import openai

openai.api_key ='YOUR_OPENAI_API_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"]

商品レビューのテキスト

次に、電子商取引サイトの商品に対する消費者のコメントを ChatGPT に提供します。ChatGPT がこのコメントから消費者の感情を推測できることを期待します。元のコメントは英語なので、中国語訳も記載します。これにより、誰にとっても理解しやすくなります。

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!!
"""


lamp_review1="""
我的卧室需要一盏漂亮的灯,这盏有额外的存储空间,价格也不太高。速度很快。
我们的灯的绳子在运输过程中断了,公司很高兴地给我们送来了一根新的。几天之内就来了。
很容易组装起来。我有一个缺失的部分,所以我联系了他们的支持,他们很快就给我找到了缺失的部分!
在我看来,Lumina是一家关心客户和产品的伟大公司!!
"""

感情認識(ポジティブ/ネガティブ)

次に、ChatGPT で上記の英語の製品レビューに対する消費者の感情を特定したいと考えています。

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)

 

 その結果、ChatGP はこのコメントの感情を肯定的なものとして正確に識別しました。以下では、ChatGPT にコメントの感情を識別するために 1 つの単語のみを使用させます。

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)

 感情のタイプを特定する

以下では、ChatGPT に最大 5 つの単語を使用してコメントの感情を識別させます。

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)

 

 上記のChatGPTの回答から、ChatGPTは消費者の感情を表現するために5つのポジティブな言葉を使用していることがわかり、これらの5つの言葉は明らかに正しいです。

怒りを認識する

以下では、ChatGPT が製品レビューが「怒り」の感情であるかどうかを識別させます。 

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)

どうやら、ChatGPT は、コメントが作成者の怒りを表現していないことを正しく認識しました。

顧客レビューから製品名と会社名を抽出

 次に、ChatGPT が製品レビューから製品名と会社名を抽出し、結果を JSON 形式で返すようにします。

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)

 

 ChatGPT は、製品名と会社名を返すために指定した形式を正しくインストールできます。

マルチタスク

次に、ChatGPT に次のタスクを同時に実行させます。

  1. 感情認識(ポジティブまたはネガティブ)
  2. 怒りの感情認識 (はいまたはいいえ)
  3. 商品の識別
  4. 会社識別

そして結果を 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)

 

返された結果から、ChatGPT が要件に従って正しい結果を返すことができることがわかります。 

 推定された主語

 以下に、政府による公共部門満足度調査の内容に関するテキストをChatGPTに提供し、このテキストから関連する5つのキーワードまたは記事のタイトルをChatGPTが要約できることを期待します。

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つのトピック(タイトル)を推測します

ChatGPT に上記のテキストから 5 つのトピック見出しを推測するように依頼します。各トピック見出しは 2 単語を超えることはできません。

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)

 結果から、5 つの科目タームは基本的に要件を満たしています。

特定のトピックについてニュースアラートを作成する

前回は、商品のコメントが「怒り」の感情であるかどうかをChatGPTが識別できることを見てきましたが、ここではテキストが特定のテーマを表現しているかどうかもChatGPTに判断させることができます。以下では、事前に5つのキーワードを設定し、ChatGPTに認識させます。上記 5 つのカスタム キーワードに適合するかどうか、適合する場合は 1、適合しない場合は 0 とマークされます。

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)

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

 ここでは、地方自治体 (地方自治体)、エンジニアリング (エンジニアリング) という 2 つのカスタム キーワードが表示されますが、テキスト コンテンツは関連性がないため、0 とマークされています。他の 3 つの単語はすべてテキストの内容に関連しているため、すべて 1 としてマークされます。

要約する

今日、私たちは ChatGPT にテキストの内容から作成者の感情を推測させる方法、怒りの感情を特定する方法、テキスト エンティティ (商品名や会社名など) を抽出する方法、キーワードを推測する方法、キーワードを識別する方法などを学びました。また、ChatGPT に複数のことを実行させる方法も学びました。同時にタスクを実行します。これらの内容が皆様のお役に立てば幸いです。

参考文献

コース - DeepLearning.AI

おすすめ

転載: blog.csdn.net/weixin_42608414/article/details/130814811#comments_26802339