文字分析

使用 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 的提示工程

使用少量範例提示的實體擷取

  • 適用於較複雜的結構
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 的提示工程

使用少量範例提示的實體擷取

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