النموذج اللغوي الكبير كمصنّف نصوص
استخدموا المطالبات لتصنيف المشاعر والنوايا والموضوعات والتصنيف متعدد التسميات
النموذج اللغوي الكبير كمصنّف نصوص درس مجاني في AI Prompt Engineering على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Prompt Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
نماذج اللغة الكبيرة بوصفها مصنّفات للنصوص
يتطلب تصنيف النصوص التقليدي بيانات تدريب موسومة، وضبطًا دقيقًا للنموذج، وبنية تحتية للنشر. أما نماذج اللغة الكبيرة فتستطيع تصنيف النصوص باستخدام موجّه فقط، من دون الحاجة إلى بيانات تدريب.
تتفوق المصنّفات القائمة على نماذج اللغة الكبيرة عندما:
- تتطلب الفئات فهمًا دلاليًا (وليس مجرد مطابقة الكلمات المفتاحية)
- تحتاجون إلى إضافة فئات جديدة من دون إعادة التدريب
- تتوفر لديكم أمثلة موسومة محدودة
- تكون الفئات دقيقة ومتداخلة (النية وراء الرسالة، وليس موضوعها فقط)
تصنيف المشاعر
تُعد المشاعر إحدى أكثر مهام التصنيف شيوعًا. ويتفوق الموجّه المصمم بعناية على مطابقة الكلمات المفتاحية البسيطة في الحالات الدقيقة:
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)أمثلة few-shot في مطالبات التصنيف
تؤدي إضافة أمثلة few-shot إلى مطالبة التصنيف إلى تحسين الدقة في الحالات الحدّية والفئات الدقيقة. ضع مثالين أو ثلاثة أمثلة توضّح الفرق بين الفئات المتشابهة:
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 لتحقيق الكفاءة
- تعامل مع المدخلات الملتبسة بوضوح — اطلب الفئة الأساسية، بالإضافة إلى البدائل والتعليل
- قيّم الدقة باستخدام مجموعة اختبار معنونة قبل النشر في بيئة الإنتاج
الأسئلة الشائعة
هل درس «النموذج اللغوي الكبير كمصنّف نصوص» مجاني؟
نعم — نص درس «النموذج اللغوي الكبير كمصنّف نصوص» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Prompt Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
ماذا ستتعلم في «النموذج اللغوي الكبير كمصنّف نصوص»؟
استخدموا المطالبات لتصنيف المشاعر والنوايا والموضوعات والتصنيف متعدد التسميات تتمرن على AI Prompt Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Prompt Engineering؟
لا تُشترط خبرة سابقة. AI Prompt Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «النموذج اللغوي الكبير كمصنّف نصوص»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Prompt Engineering هذا؟
نعم. كل درس في AI Prompt Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مطالبات استخراج الكيانات المسماة
- استخراج البيانات المدفوع بالمخطط
- النموذج اللغوي الكبير كمصنّف نصوص
- الثقة وعدم اليقين في التصنيف