LLM come classificatore di testo
Utilizzi i prompt per la classificazione di sentiment, intento, argomento e categorie multiple
LLM come classificatore di testo è una lezione AI Prompt Engineering gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento AI Prompt Engineering, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso AI Prompt Engineering include 4 lezioni in totale.
Gli LLM come classificatori di testo
La classificazione tradizionale del testo richiede dati di addestramento annotati, fine-tuning del modello e un'infrastruttura di deployment. Gli LLM possono classificare il testo con il solo ausilio di un prompt, senza richiedere dati di addestramento.
I classificatori basati su LLM sono particolarmente efficaci quando:
- Le categorie richiedono comprensione semantica, non soltanto il confronto di parole chiave
- È necessario aggiungere nuove categorie senza riaddestrare il modello
- Si dispone di pochi esempi annotati
- Le categorie sono sfumate (l'intento alla base di un messaggio, non soltanto il suo argomento)
Classificazione del sentiment
Il sentiment è una delle attività di classificazione più comuni. Un prompt ben formulato supera il semplice confronto di parole chiave nei casi più sfumati:
import anthropic, json
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def classify_sentiment(text):
prompt = f'''
Classify the sentiment of the text below.
Return ONLY JSON: {{"sentiment": "positive|negative|neutral", "confidence": "high|medium|low"}}
Definitions:
- positive: Overall favorable opinion or emotion
- negative: Overall unfavorable opinion or dissatisfaction
- neutral: Factual, balanced, or no clear sentiment
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=50,
messages=[{'role': 'user', 'content': prompt}]
)
return json.loads(r.content[0].text)
print(classify_sentiment('The product works but setup was painful.'))
print(classify_sentiment('Delivery was incredibly fast and packaging was perfect!'))Classificazione dell'intento
La classificazione dell'intento identifica ciò che l'utente sta cercando di fare: è essenziale per chatbot, sistemi di assistenza e applicazioni di ricerca:
INTENT_CATEGORIES = [
'purchase_intent: User wants to buy or is ready to purchase',
'complaint: User is dissatisfied and reporting a problem',
'question: User is asking for information or help',
'cancellation: User wants to cancel a service or subscription',
'compliment: User is expressing satisfaction or praise',
'other: Does not fit any category above'
]
def classify_intent(message):
categories_str = '\n'.join(f'- {c}' for c in INTENT_CATEGORIES)
prompt = f'''
Classify the intent of this customer message.
Return JSON: {{"intent": str, "confidence": "high|medium|low"}}
Categories:
{categories_str}
Message: {message}
'''
r = client.messages.create(model='claude-opus-4-5', max_tokens=80, messages=[{'role': 'user', 'content': prompt}])
return json.loads(r.content[0].text)
print(classify_intent('I love the app but I need to cancel my plan.'))
print(classify_intent('How do I export my data to CSV?'))Classificazione degli argomenti
La classificazione degli argomenti assegna al testo una categoria tematica. È utile per instradare i contenuti, filtrare i feed e categorizzare i ticket di supporto:
def classify_topic(article_text, topics):
topic_list = ', '.join(topics)
prompt = f'''
Classify the topic of this article. Choose EXACTLY ONE from the list.
Return JSON: {{"topic": str, "secondary_topic": str or null}}
Available topics: {topic_list}
Article (first 300 chars): {article_text[:300]}
'''
r = client.messages.create(model='claude-opus-4-5', max_tokens=80, messages=[{'role': 'user', 'content': prompt}])
return json.loads(r.content[0].text)
topics = ['technology', 'sports', 'politics', 'business', 'science', 'health', 'entertainment']
text = 'The FDA approved a new mRNA vaccine for seasonal influenza, marking a breakthrough in vaccine technology.'
result = classify_topic(text, topics)
print(result)Classificazione dell'urgenza
La classificazione dell'urgenza aiuta a stabilire la priorità di ticket di supporto, email e incidenti. È fondamentale definire con precisione i livelli di urgenza:
URGENCY_PROMPT = '''
Classify the urgency of this support ticket.
Return JSON: {{"urgency": str, "reason": str}}
Urgency levels:
- critical: Service is completely down, data loss occurring, or security breach
- high: Major functionality broken, many users affected, no workaround
- medium: Non-critical feature broken, workaround exists, single user affected
- low: Cosmetic issue, enhancement request, general question
Be conservative: only use critical if the ticket explicitly describes a system-wide outage or data loss.
Ticket: {ticket}
'''
def classify_urgency(ticket_text):
prompt = URGENCY_PROMPT.replace('{ticket}', ticket_text)
r = client.messages.create(model='claude-opus-4-5', max_tokens=100, messages=[{'role': 'user', 'content': prompt}])
return json.loads(r.content[0].text)
print(classify_urgency('The entire production database is down. All customers affected.'))
print(classify_urgency('Dark mode button is slightly off-center.'))Classificazione multi-etichetta
A volte un testo appartiene contemporaneamente a più categorie. La classificazione multi-etichetta restituisce tutte le categorie applicabili:
def multi_label_classify(text, labels):
label_list = ', '.join(labels)
prompt = f'''
Classify this text. It may belong to multiple categories.
Return JSON: {{"labels": [str], "primary_label": str}}
Available labels: {label_list}
Rules:
- Include all labels that clearly apply
- Do NOT include labels that only marginally apply
- primary_label is the single most relevant label
Text: {text}
'''
r = client.messages.create(model='claude-opus-4-5', max_tokens=100, messages=[{'role': 'user', 'content': prompt}])
return json.loads(r.content[0].text)
labels = ['technical', 'billing', 'account', 'bug_report', 'feature_request', 'security']
text = 'I found a bug that exposes other users billing information on my account page.'
print(multi_label_classify(text, labels))Modelli di prompt per la classificazione
Modello di prompt riutilizzabile per la classificazione, adatto a qualsiasi insieme di categorie:
def build_classifier(category_definitions, additional_rules=''):
cats = '\n'.join(f'- {k}: {v}' for k, v in category_definitions.items())
return f'''
Classify the input text into exactly one category below.
Return JSON: {{"category": str, "confidence": "high|medium|low"}}
Categories:
{cats}
{('\nAdditional rules:\n' + additional_rules) if additional_rules else ''}
Text: {{text}}
'''
language_classifier = build_classifier({
'formal': 'Business or academic writing, professional context',
'informal': 'Casual, conversational, slang or colloquial',
'technical': 'Domain-specific jargon, code, or specialized terminology',
'emotional': 'High emotional content, personal, expressive'
})
print(language_classifier[:200])Gestione delle classificazioni ambigue
Alcuni input rientrano effettivamente in più categorie. Progetti i prompt di classificazione in modo da gestire esplicitamente l'ambiguità:
AMBIGUITY_PROMPT = '''
Classify this customer message. If the message is ambiguous or could fit multiple categories,
choose the category that would be most useful for routing it to the correct team.
Return JSON:
{{
"category": str,
"is_ambiguous": true | false,
"alternative": str or null,
"reasoning": str
}}
Categories: billing, technical_support, sales, account_management
Message: {message}
'''
message = 'I upgraded my plan but I am still seeing the free tier features.'
r = client.messages.create(
model='claude-opus-4-5', max_tokens=150,
messages=[{'role': 'user', 'content': AMBIGUITY_PROMPT.format(message=message)}]
)
print(json.loads(r.content[0].text))Classificazione in batch per una maggiore efficienza
Classificare gli elementi uno alla volta è costoso. La classificazione in batch elabora più input in un'unica chiamata API:
def batch_classify(items, categories):
items_str = '\n'.join(f'{i+1}. {item}' for i, item in enumerate(items))
cat_str = ', '.join(categories)
prompt = f'''
Classify each item below. Categories: {cat_str}
Return JSON: {{"results": [{{"id": int, "category": str, "confidence": str}}]}}
Items:
{items_str}
'''
r = client.messages.create(model='claude-opus-4-5', max_tokens=300, messages=[{'role': 'user', 'content': prompt}])
return json.loads(r.content[0].text)['results']
texts = [
'Absolutely love this product!',
'It crashed twice today.',
'What is your refund policy?',
'The color options are limited.'
]
results = batch_classify(texts, ['positive_feedback', 'bug_report', 'inquiry', 'feature_request'])
for r in results:
print(f'{texts[r["id"]-1][:30]}... -> {r["category"]} ({r["confidence"]})')Valutazione dell'accuratezza del classificatore
I classificatori basati su LLM devono essere valutati sistematicamente su esempi etichettati. Crei un piccolo set di test e misuri l'accuratezza:
labeled_test_set = [
{'text': 'Great product, no issues!', 'expected': 'positive'},
{'text': 'The app keeps crashing on startup.', 'expected': 'negative'},
{'text': 'The screen resolution is 1080p.', 'expected': 'neutral'},
{'text': 'Worst experience of my life.', 'expected': 'negative'},
{'text': 'Works as advertised, decent value.', 'expected': 'positive'},
]
def evaluate_classifier(test_set, classify_fn):
correct = 0
for example in test_set:
result = classify_fn(example['text'])
if result['sentiment'] == example['expected']:
correct += 1
else:
print(f'WRONG: "{example["text"][:40]}" -> got {result["sentiment"]}, expected {example["expected"]}')
accuracy = correct / len(test_set)
print(f'Accuracy: {correct}/{len(test_set)} = {accuracy:.0%}')
return accuracy
evaluate_classifier(labeled_test_set, classify_sentiment)Esempi few-shot nei prompt di classificazione
L'aggiunta di esempi few-shot a un prompt di classificazione migliora l'accuratezza nei casi limite e con categorie dalle sfumature più sottili. Inserisca 2-3 esempi che dimostrino la distinzione tra categorie simili:
few_shot_classifier = '''
Classify each customer message as: billing, technical, or general.
Examples:
Input: "I was charged twice for this month." -> billing
Input: "The app crashes when I open the dashboard." -> technical
Input: "Do you have a mobile app?" -> general
Input: "My invoice shows the wrong plan." -> billing
Input: "I cannot log in, I get error 403." -> technical
Now classify:
Input: {message}
Return JSON: {"category": str, "confidence": "high|medium|low"}
'''
message = "My subscription was renewed but I cancelled last week."
prompt = few_shot_classifier.replace("{message}", message)
print(prompt)Verifica rapida
Qual è il principale vantaggio dell'utilizzo di un LLM per la classificazione del testo rispetto a un classificatore tradizionale addestrato?
LLM come classificatore — Punti chiave
La classificazione basata su LLM è potente, flessibile e rapida da implementare:
- Definisca le categorie con descrizioni, non solo con nomi, per gestire correttamente i casi dalle sfumature più sottili
- Restituisca JSON con categoria e livello di confidenza per ogni classificazione
- Pattern comuni: sentiment (positivo/negativo/neutro), intento, argomento, urgenza
- La classificazione multi-etichetta restituisce tutte le categorie applicabili oltre a un'etichetta principale
- La classificazione in batch elabora più input in un'unica chiamata API, aumentando l'efficienza
- Gestisca esplicitamente gli input ambigui: chieda la categoria principale, le alternative e il ragionamento
- Valuti l'accuratezza su un set di test etichettato prima della distribuzione in produzione
Domande Frequenti
La lezione «LLM come classificatore di testo» è gratuita?
Sì — il testo completo di «LLM come classificatore di testo» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso AI Prompt Engineering, passa a CoddyKit PRO. Il corso AI Prompt Engineering include 4 lezioni in totale.
Cosa imparerò in «LLM come classificatore di testo»?
Utilizzi i prompt per la classificazione di sentiment, intento, argomento e categorie multiple Eserciti AI Prompt Engineering con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare AI Prompt Engineering?
Non è richiesta alcuna esperienza precedente. AI Prompt Engineering su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «LLM come classificatore di testo»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione AI Prompt Engineering?
Sì. Ogni lezione AI Prompt Engineering include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Prompt per l'estrazione di entità nominate
- Estrazione dei dati guidata dallo schema
- LLM come classificatore di testo
- Affidabilità e incertezza nella classificazione