0Pricing
AI Prompt Engineering · レッスン

テキスト分類器としての LLM

感情、意図、トピック、複数ラベル分類にプロンプトを活用します。

「テキスト分類器としての LLM」はCoddyKit上の無料AI Prompt Engineeringレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはAI Prompt Engineering学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 AI Prompt Engineeringコースには全4レッスンが含まれています。

LLMをテキスト分類器として使う

従来のテキスト分類には、ラベル付き学習データ、モデルのファインチューニング、デプロイ用のインフラストラクチャが必要です。LLMなら、学習データなしでプロンプトだけを使ってテキストを分類できます。

LLMベースの分類器は、次のような場合に特に優れています。

  • カテゴリーに意味理解が必要な場合(単なるキーワード照合では不十分な場合)
  • 再学習なしで新しいカテゴリーを追加する必要がある場合
  • ラベル付きの例が限られている場合
  • カテゴリーが微妙なニュアンスを含む場合(単なるトピックではなく、メッセージの意図を分類する場合)

感情分類

感情は、最も一般的な分類タスクの1つです。適切に作成したプロンプトは、微妙なニュアンスを含むケースでは単純なキーワード照合を上回ります。

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!'))

意図分類

意図分類では、ユーザーが何をしようとしているのかを特定します。これはチャットボット、サポートシステム、検索アプリケーションに不可欠です。

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?'))

トピック分類

トピック分類は、テキストにテーマ別のカテゴリを割り当てます。コンテンツの振り分け、フィードのフィルタリング、サポートチケットの分類に役立ちます。

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)

緊急度分類

緊急度分類は、サポートチケット、メール、インシデントの優先順位付けに役立ちます。緊急度のレベルを正確に定義することが重要です。

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

マルチラベル分類

テキストが同時に複数のカテゴリに該当することがあります。マルチラベル分類では、該当するすべてのカテゴリを返します。

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

分類プロンプトテンプレート

任意のカテゴリセットで使える、再利用可能な分類プロンプトテンプレートです。

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])

曖昧な分類への対応

入力によっては、実際に複数のカテゴリに該当することがあります。曖昧さに明示的に対応できるよう、分類プロンプトを設計してください。

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

効率化のためのバッチ分類

項目を1件ずつ分類するとコストがかかります。バッチ分類では、1回の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"]})')

分類器の精度評価

LLM分類器は、ラベル付きの例に対して体系的に評価する必要があります。小規模なテストセットを作成し、精度を測定してください。

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)

分類プロンプトでのFew-Shot例

分類プロンプトにFew-Shot例を追加すると、境界的なケースや微妙な違いのあるカテゴリでの精度が向上します。類似したカテゴリの違いを示す例を2〜3個配置してください。

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)

クイックチェック

従来の学習済み分類器と比べて、テキスト分類にLLMを使う主な利点は何ですか?

分類器としてのLLM — 重要ポイント

LLMベースの分類は、高性能で柔軟性があり、迅速に導入できます。

  • 微妙なケースにも正しく対応できるよう、カテゴリ名だけでなく説明も定義します
  • すべての分類結果について、カテゴリと信頼度を含むJSONを返します
  • 一般的なパターンには、感情(positive/negative/neutral)、意図、トピック、緊急度があります
  • マルチラベル分類では、該当するすべてのカテゴリと主ラベルを返します
  • バッチ分類では、効率化のため、1回のAPI呼び出しで複数の入力を処理します
  • 曖昧な入力には明示的に対応し、主カテゴリに加えて代替候補と推論を求めます
  • 本番環境にデプロイする前に、ラベル付きテストセットに対して精度を評価します

よくある質問

「テキスト分類器としての LLM」レッスンは無料ですか?

はい。「テキスト分類器としての LLM」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、AI Prompt Engineeringコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 AI Prompt Engineeringコースには全4レッスンが含まれています。

「テキスト分類器としての LLM」で何を学びますか?

感情、意図、トピック、複数ラベル分類にプロンプトを活用します。 ブラウザで直接実行するハンズオンコードでAI Prompt Engineeringを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

AI Prompt Engineeringを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのAI Prompt Engineeringは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「テキスト分類器としての LLM」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このAI Prompt Engineeringレッスンでコードを書いて実行できますか?

はい。すべてのAI Prompt Engineeringレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 固有表現抽出のプロンプト
  2. スキーマ駆動のデータ抽出
  3. テキスト分類器としての LLM
  4. 分類における確信度と不確実性
← AI Prompt Engineeringに戻る