テキスト分析

OpenAI API で学ぶプロンプトエンジニアリング

Fouad Trad

Machine Learning Engineer

テキスト分析

  • テキストを解析して情報を抽出
    • テキスト分類
    • エンティティ抽出
  • 顧客データの利用には法的助言が必要

テキスト分析がテキストから感情、感情表現、トーン、エンティティなどの情報を抽出する様子を示す画像。

OpenAI API で学ぶプロンプトエンジニアリング

テキスト分類

  • テキストにカテゴリを割り当てる
  • 例:感情分析

感情分析のクラスとしてポジティブ・ネガティブ・ニュートラルを示す画像。

OpenAI API で学ぶプロンプトエンジニアリング

カテゴリの指定あり

  • 分類カテゴリが既知の場合は指定する
  • 出力要件を明記する
text = "I bought your XYZ Smart Watch and wanted to share my positive experience. 
Impressed with its sleek design, comfort, and touchscreen usability."

prompt = f"""Classify the sentiment of the text delimited by triple backticks as positive, negative, or neutral. Give your answer as a single word: ```{text}```""" print(get_response(prompt))
positive
OpenAI API で学ぶプロンプトエンジニアリング

カテゴリの指定なし

  • カテゴリ未指定の場合、モデルが自律的に判断する
text = "I bought your XYZ Smart Watch and wanted to share my positive experience. 
Impressed with its sleek design, comfort, and touchscreen usability."

prompt = f"""Classify the sentiment of the text delimited by triple backticks. 
             Give your answer as a single word.
           ```{text}```"""
print(get_response(response))
positive.
  • オープンエンドな問題ではうまく機能しない場合がある
OpenAI API で学ぶプロンプトエンジニアリング

複数クラス

  • テキストは複数のクラスに分類可能
  • クラス数が不明な場合は最大数を定義する
text = "I bought your XYZ Smart Watch and wanted to share my positive experience. 
Impressed with its sleek design, comfort, and touchscreen usability."

prompt = f"""Identify emotions used in this text. Don't use more than 3 emotions. Format your answer as a list of words separated by commas: ```{text}```""" print(get_response(prompt))
impressed, positive, comfortable
OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出

  • テキストから特定のエンティティを抽出
  • 例:人名、場所、組織、日付

テキストから特定のエンティティを抽出することを示すアイコン

OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出:エンティティの指定

  • 抽出するエンティティを指定する
  • 出力フォーマットを指定する
text = "The XYZ Mobile X200: a sleek 6.5-inch Super AMOLED smartphone with a 48MP 
triple-camera, octa-core processor, 5000mAh battery, 5G connectivity, and Android 
11 OS. Secure with fingerprint and facial recognition. 128GB storage, expandable up 
to 512GB."

prompt = f"""Identify the following entities from the text delimited by triple backticks: - Product Name ```{text}```""" print(get_response(prompt))
OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出:エンティティの指定

  • 抽出するエンティティを指定する
  • 出力フォーマットを指定する
text = "The XYZ Mobile X200: a sleek 6.5-inch Super AMOLED smartphone with a 48MP 
triple-camera, octa-core processor, 5000mAh battery, 5G connectivity, and Android 
11 OS. Secure with fingerprint and facial recognition. 128GB storage, expandable up 
to 512GB."

prompt = f"""Identify the following entities from the text delimited by triple backticks: - Product Name - Display Size ```{text}```""" print(get_response(prompt))
OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出:エンティティの指定

  • 抽出するエンティティを指定する
  • 出力フォーマットを指定する
text = "The XYZ Mobile X200: a sleek 6.5-inch Super AMOLED smartphone with a 48MP 
triple-camera, octa-core processor, 5000mAh battery, 5G connectivity, and Android 
11 OS. Secure with fingerprint and facial recognition. 128GB storage, expandable up 
to 512GB."

prompt = f"""Identify the following entities from the text delimited by triple backticks: - Product Name - Display Size - Camera Resolution ```{text}```""" print(get_response(prompt))
OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出:エンティティの指定

  • 抽出するエンティティを指定する
  • 出力フォーマットを指定する
text = "The XYZ Mobile X200: a sleek 6.5-inch Super AMOLED smartphone with a 48MP 
triple-camera, octa-core processor, 5000mAh battery, 5G connectivity, and Android 
11 OS. Secure with fingerprint and facial recognition. 128GB storage, expandable up 
to 512GB."

prompt = f"""Identify the following entities from the text delimited by triple backticks: - Product Name - Display Size - Camera Resolution Format the answer as an unordered list. ```{text}```""" print(get_response(prompt))
OpenAI API で学ぶプロンプトエンジニアリング

エンティティ抽出:エンティティの指定

Product Name: XYZ Mobile X200
Display Size: 6.5-inch
Camera Resolution: 48MP triple-camera
OpenAI API で学ぶプロンプトエンジニアリング

few-shotプロンプトによるエンティティ抽出

  • 複雑な構造に対応する場合
ticket_1 = "Hello, I'm Emma Adams. I'd 
like to ask about my reservation with 
the code CAR123. 
You can reach me at +123456 if needed."

ticket_2 = "This is Sarah Williams. 
I would like to request some information
regarding my upcoming flight with 
reservation code FLIGHT987. Thank you."
entities_1 = """
* Customer Details:
  - Name: Emma Adams
  - Phone: +123456
* Reservation Details:
  - Reservation Code: CAR123"""
entities_2 = """
* Customer Details:
  - Name: Sarah Williams
* Reservation Details:
  - Reservation Code: FLIGHT987"""
OpenAI API で学ぶプロンプトエンジニアリング

few-shotプロンプトによるエンティティ抽出

ticket_3 = "Hello, I'm David Brown (CUST123). I need assistance with my reservation under 
the code HOTEL456. There are some questions and issues related to my upcoming stay that 
require your attention."

prompt = f"""Text: {ticket_1} -> Entities: {entities_1} Text: {ticket_2} -> Entities: {entities_2} Text: {ticket_3} -> Entities: """ print(get_response(prompt))
* Customer Details:
  - Name: David Brown
  - Customer ID: CUST123
* Reservation Details:
  - Reservation Code: HOTEL456
OpenAI API で学ぶプロンプトエンジニアリング

練習しましょう!

OpenAI API で学ぶプロンプトエンジニアリング

Preparing Video For Download...