Analisi del testo

Prompt Engineering con l'API di OpenAI

Fouad Trad

Machine Learning Engineer

Analisi del testo

  • Analizzare il testo per estrarre informazioni
    • Classificazione del testo
    • Estrazione di entità
  • Le aziende dovrebbero chiedere consulenza legale quando usano dati dei clienti

Immagine che mostra come l'analisi del testo estrae informazioni come sentiment, emozioni, tono, entità, ecc. da un testo.

Prompt Engineering con l'API di OpenAI

Classificazione del testo

  • Assegna categorie al testo
  • Esempio: analisi del sentiment

Immagine che mostra le classi dell'analisi del sentiment: positivo, negativo o neutro.

Prompt Engineering con l'API di OpenAI

Categorie specificate

  • Specifica le categorie quando note
  • Indica i requisiti di output
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
Prompt Engineering con l'API di OpenAI

Categorie non specificate

  • Il modello usa la sua conoscenza quando le categorie non sono specificate
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.
  • Per alcuni problemi aperti questo può non funzionare bene
Prompt Engineering con l'API di OpenAI

Classi multiple

  • Un testo può rientrare in più classi
  • Definisci un numero massimo di classi se non note
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
Prompt Engineering con l'API di OpenAI

Estrazione di entità

  • Estrazione di entità specifiche dal testo
  • Esempi: nomi, luoghi, organizzazioni, date

Icona che mostra come l'estrazione di entità riguarda l'estrazione di entità specifiche da un testo

Prompt Engineering con l'API di OpenAI

Estrazione di entità: specifica le entità

  • Specifica le entità da estrarre
  • Indica il formato di output
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))
Prompt Engineering con l'API di OpenAI

Estrazione di entità: specifica le entità

  • Specifica le entità da estrarre
  • Indica il formato di output
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))
Prompt Engineering con l'API di OpenAI

Estrazione di entità: specifica le entità

  • Specifica le entità da estrarre
  • Indica il formato di output
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))
Prompt Engineering con l'API di OpenAI

Estrazione di entità: specifica le entità

  • Specifica le entità da estrarre
  • Indica il formato di output
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))
Prompt Engineering con l'API di OpenAI

Estrazione di entità: specifica le entità

Product Name: XYZ Mobile X200
Display Size: 6.5-inch
Camera Resolution: 48MP triple-camera
Prompt Engineering con l'API di OpenAI

Estrazione di entità con prompt few-shot

  • Per strutture complesse
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"""
Prompt Engineering con l'API di OpenAI

Estrazione di entità con prompt 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
Prompt Engineering con l'API di OpenAI

Andiamo a fare pratica!

Prompt Engineering con l'API di OpenAI

Preparing Video For Download...