Phân tích văn bản

Kỹ thuật Prompt với OpenAI API

Fouad Trad

Machine Learning Engineer

Phân tích văn bản

  • Phân tích văn bản để trích xuất thông tin
    • Phân loại văn bản
    • Trích xuất thực thể
  • Doanh nghiệp nên tham vấn pháp lý khi dùng dữ liệu khách hàng

Hình minh họa cách phân tích văn bản trích xuất cảm xúc, cảm nhận, giọng điệu, thực thể, v.v. từ văn bản.

Kỹ thuật Prompt với OpenAI API

Phân loại văn bản

  • Gán danh mục cho văn bản
  • Ví dụ: phân tích cảm xúc

Hình minh họa các lớp của phân tích cảm xúc: tích cực, tiêu cực, trung tính.

Kỹ thuật Prompt với OpenAI API

Danh mục xác định trước

  • Chỉ rõ các danh mục khi đã biết
  • Nêu yêu cầu đầu ra
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
Kỹ thuật Prompt với OpenAI API

Danh mục không xác định

  • Mô hình tự chọn danh mục khi không chỉ định
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.
  • Với bài toán mở, cách này có thể không hiệu quả
Kỹ thuật Prompt với OpenAI API

Nhiều lớp

  • Văn bản có thể thuộc nhiều lớp
  • Đặt số lớp tối đa nếu chưa biết
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
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể

  • Trích xuất thực thể cụ thể từ văn bản
  • Ví dụ: tên, địa điểm, tổ chức, ngày tháng

Biểu tượng cho thấy trích xuất thực thể là lấy các thực thể cụ thể từ văn bản

Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể: chỉ định thực thể

  • Chỉ định thực thể cần trích xuất
  • Nêu định dạng đầu ra
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))
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể: chỉ định thực thể

  • Chỉ định thực thể cần trích xuất
  • Nêu định dạng đầu ra
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))
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể: chỉ định thực thể

  • Chỉ định thực thể cần trích xuất
  • Nêu định dạng đầu ra
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))
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể: chỉ định thực thể

  • Chỉ định thực thể cần trích xuất
  • Nêu định dạng đầu ra
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))
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể: chỉ định thực thể

Product Name: XYZ Mobile X200
Display Size: 6.5-inch
Camera Resolution: 48MP triple-camera
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể với few-shot prompts

  • Cho cấu trúc phức tạp
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"""
Kỹ thuật Prompt với OpenAI API

Trích xuất thực thể với few-shot prompts

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
Kỹ thuật Prompt với OpenAI API

Vamos praticar!

Kỹ thuật Prompt với OpenAI API

Preparing Video For Download...