0Pricing
AI Prompt Engineering · บทเรียน

LLM ในฐานะตัวจำแนกข้อความ

ใช้พรอมป์ตจำแนกความรู้สึก เจตนา หัวข้อ และหลายป้ายกำกับ

LLM ในฐานะตัวจำแนกข้อความ เป็นบทเรียน AI Prompt Engineering ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Prompt Engineering และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน

LLM ในฐานะตัวจำแนกข้อความ

การจำแนกข้อความแบบดั้งเดิมต้องใช้ข้อมูลฝึกที่มีการกำกับป้ายกำกับ การปรับแต่งโมเดลแบบละเอียด และโครงสร้างพื้นฐานสำหรับนำไปใช้งาน ส่วน LLM สามารถจำแนกข้อความได้ด้วยพรอมต์เพียงอย่างเดียว โดยไม่ต้องใช้ข้อมูลฝึก

ตัวจำแนกที่ใช้ LLM ทำงานได้ดีเป็นพิเศษเมื่อ:

  • หมวดหมู่ต้องอาศัยความเข้าใจเชิงความหมาย (ไม่ใช่เพียงการจับคู่คำสำคัญ)
  • ต้องการเพิ่มหมวดหมู่ใหม่โดยไม่ต้องฝึกโมเดลใหม่
  • มีตัวอย่างที่มีการกำกับป้ายกำกับจำนวนจำกัด
  • หมวดหมู่มีรายละเอียดละเอียดอ่อน (เจตนาเบื้องหลังข้อความ ไม่ใช่เพียงหัวข้อของข้อความ)

การจำแนกความรู้สึก

ความรู้สึกเป็นหนึ่งในงานจำแนกประเภทที่พบบ่อยที่สุด พรอมต์ที่ร่างอย่างดีให้ผลดีกว่าการจับคู่คำสำคัญแบบง่ายในกรณีที่มีรายละเอียดละเอียดอ่อน:

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

การจัดประเภทแบบกลุ่มเพื่อประสิทธิภาพ

การจัดประเภททีละรายการมีค่าใช้จ่ายสูง การจัดประเภทแบบกลุ่มจะประมวลผลข้อมูลเข้าหลายรายการด้วยการเรียก 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)

ตัวอย่างจำนวนน้อยในพรอมป์ต์การจัดประเภท

การเพิ่มตัวอย่างจำนวนน้อยลงในพรอมป์ต์สำหรับการจัดประเภทช่วยเพิ่มความแม่นยำในกรณีสุดโต่งและหมวดหมู่ที่มีความแตกต่างละเอียดอ่อน ควรวางตัวอย่าง 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 ที่มีหมวดหมู่และความมั่นใจสำหรับการจัดประเภททุกครั้ง
  • รูปแบบที่พบบ่อย ได้แก่ ความรู้สึก (บวก/ลบ/เป็นกลาง) เจตนา หัวข้อ และความเร่งด่วน
  • การจัดประเภทหลายป้ายกำกับจะแสดงหมวดหมู่ที่เกี่ยวข้องทั้งหมดพร้อมป้ายกำกับหลัก
  • การจัดประเภทแบบกลุ่มจะประมวลผลข้อมูลเข้าหลายรายการด้วยการเรียก API ครั้งเดียวเพื่อเพิ่มประสิทธิภาพ
  • จัดการข้อมูลเข้าที่กำกวมอย่างชัดเจน โดยขอหมวดหมู่หลักพร้อมทางเลือกและเหตุผล
  • ประเมินความแม่นยำโดยเทียบกับชุดทดสอบที่มีป้ายกำกับก่อนนำไปใช้งานจริง

คำถามที่พบบ่อย

บทเรียน “LLM ในฐานะตัวจำแนกข้อความ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “LLM ในฐานะตัวจำแนกข้อความ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Prompt Engineering ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Prompt Engineering มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “LLM ในฐานะตัวจำแนกข้อความ”

ใช้พรอมป์ตจำแนกความรู้สึก เจตนา หัวข้อ และหลายป้ายกำกับ คุณปฏิบัติ AI Prompt Engineering ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Prompt Engineering หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Prompt Engineering บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “LLM ในฐานะตัวจำแนกข้อความ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AI Prompt Engineering นี้ได้ไหม

ได้ บทเรียน AI Prompt Engineering ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. พรอมป์ตสำหรับการดึงเอนทิตีที่มีชื่อ
  2. การดึงข้อมูลโดยขับเคลื่อนด้วยสคีมา
  3. LLM ในฐานะตัวจำแนกข้อความ
  4. ความมั่นใจและความไม่แน่นอนในการจำแนก
← กลับไปที่ AI Prompt Engineering