الثقة وعدم اليقين في التصنيف
اطلبوا من النموذج تقييم درجة الثقة والتعامل مع التصنيفات الملتبسة
الثقة وعدم اليقين في التصنيف درس مجاني في AI Prompt Engineering على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Prompt Engineering، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
مشكلة النماذج مفرطة الثقة
تجيب نماذج LLM افتراضيًا عن مهام التصنيف بيقين ظاهري، حتى عندما تكون المدخلة ملتبسة فعلًا. فإذا طُلب من النموذج إرجاع إيجابي أو سلبي أو محايد، فسيختار أحدها دائمًا — ولن يقول أبدًا لست متأكدًا.
في أنظمة الإنتاج، يؤدي التعامل مع التصنيفات غير المؤكدة كما لو كانت مؤكدة إلى أخطاء مكلفة: توجيه تذاكر الدعم إلى جهات خاطئة، وتقديم توصيات غير صحيحة، وإعداد تقارير غير دقيقة.
يحل تحديد مستوى عدم اليقين في مطالبات التصنيف هذه المشكلة.
درجات الثقة من 1 إلى 10
يمنح طلبُ تقييم النموذج لثقته على مقياس رقمي إشارةً دقيقة يمكن للأنظمة اللاحقة استخدام عتبة لتحديد الإجراء المناسب:
import anthropic, json
client = anthropic.Anthropic(api_key='YOUR_API_KEY')
def classify_with_confidence(text):
prompt = f'''
Classify the sentiment of the text below.
Return JSON:
{{
"sentiment": "positive|negative|neutral",
"confidence": 1-10,
"reason": "brief explanation of confidence level"
}}
Confidence scale: 10=completely certain, 1=total guess, 5=genuinely ambiguous
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)
print(classify_with_confidence('I sort of liked it but the wait was too long.'))
print(classify_with_confidence('This product is absolutely outstanding!'))استجابة UNCERTAIN
يؤدي توجيه النموذج إلى الاستجابة صراحةً بـ UNCERTAIN عندما تكون ثقته في التصنيف أقل من عتبة محددة إلى إنشاء مخرجات من ثلاثة خيارات: إيجابي أو سلبي أو UNCERTAIN:
def classify_or_uncertain(text, uncertainty_threshold=4):
prompt = f'''
Classify the sentiment of the text: positive, negative, or neutral.
If the sentiment is genuinely ambiguous or you are not confident (confidence below {uncertainty_threshold}/10),
return UNCERTAIN instead of guessing.
Return JSON: {{"sentiment": "positive|negative|neutral|UNCERTAIN", "confidence": 1-10}}
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=80,
messages=[{'role': 'user', 'content': prompt}]
)
result = json.loads(r.content[0].text)
if result['sentiment'] == 'UNCERTAIN' or result['confidence'] < uncertainty_threshold:
print(f'Routing to human review: confidence={result["confidence"]}')
return result
print(classify_or_uncertain('It was fine, I guess. Not bad, not great.'))
print(classify_or_uncertain('Absolutely terrible product. Never buying again.'))احتمالات الفئات مرتبةً
بدلًا من إجبار النموذج على اختيار فئة واحدة، اطلب منه ترتيب جميع الفئات الممكنة حسب الاحتمالية. يكشف ذلك مدى تقارب الفئتين الأعلى ترتيبًا:
def classify_ranked(text, categories):
cats = ', '.join(categories)
prompt = f'''
Classify this text into one of these categories: {cats}
Return ALL categories ranked by likelihood, highest first.
Return JSON: {{"ranked": [{{"category": str, "probability": 0.0-1.0}}]}}
Probabilities must sum to 1.0.
Text: {text}
'''
r = client.messages.create(
model='claude-opus-4-5', max_tokens=150,
messages=[{'role': 'user', 'content': prompt}]
)
result = json.loads(r.content[0].text)
return result['ranked']
cats = ['billing', 'technical', 'general', 'cancellation']
ranked = classify_ranked('I was charged twice and now my account is locked.', cats)
for item in ranked:
print(f'{item["category"]}: {item["probability"]:.0%}')استخدام تباين الاحتمالات لاكتشاف الغموض
يُعد الفرق بين احتمالي الفئتين الأعلى ترتيبًا إشارةً موثوقة إلى الغموض. تشير الفجوة الصغيرة إلى عدم يقين النموذج، بينما تشير الفجوة الكبيرة إلى ثقته:
def classify_with_ambiguity_detection(text, categories, ambiguity_threshold=0.15):
ranked = classify_ranked(text, categories)
top1_prob = ranked[0]['probability']
top2_prob = ranked[1]['probability'] if len(ranked) > 1 else 0
spread = top1_prob - top2_prob
is_ambiguous = spread < ambiguity_threshold
return {
'primary': ranked[0]['category'],
'secondary': ranked[1]['category'] if len(ranked) > 1 else None,
'confidence_spread': round(spread, 3),
'is_ambiguous': is_ambiguous,
'action': 'human_review' if is_ambiguous else 'auto_classify'
}
result = classify_with_ambiguity_detection(
'My upgrade did not apply and I think I was still charged.', ['billing', 'technical', 'general', 'cancellation']
)
print(result)عدم اليقين المشروط: اطرح سؤالًا عند الشك
في التطبيقات الحوارية، يمكن للنموذج بدلًا من إرجاع UNCERTAIN أن يطلب توضيحًا:
SYSTEM_CLARIFY = '''
You are a support ticket classifier.
If the customer message is clear, classify it and respond with JSON:
{"action": "classify", "category": str, "confidence": 1-10}
If the message is ambiguous or you are not sure which category applies, respond with:
{"action": "clarify", "question": "A single clarifying question to ask the customer"}
Categories: billing, technical, account, cancellation
Only ask for clarification when genuinely needed. Prefer classification when possible.
'''
def classify_or_ask(message):
r = client.messages.create(
model='claude-opus-4-5', max_tokens=100,
system=SYSTEM_CLARIFY,
messages=[{'role': 'user', 'content': message}]
)
return json.loads(r.content[0].text)
print(classify_or_ask('It is not working anymore.'))
print(classify_or_ask('Cancel my subscription immediately.'))معايرة الثقة: درجة الحرارة والاتساق
يكشف تشغيل عملية التصنيف نفسها عدة مرات عند درجات حرارة مختلفة عن عدم يقين النموذج الفعلي. ويعني التباين المرتفع أن المدخلة ملتبسة فعلًا:
from collections import Counter
def calibrated_classify(text, n_samples=5):
results = []
for _ in range(n_samples):
r = client.messages.create(
model='claude-opus-4-5', max_tokens=50,
messages=[{'role': 'user', 'content': f'Classify as positive/negative/neutral. Return JSON: {{"sentiment": str}}\n\n{text}'}]
)
results.append(json.loads(r.content[0].text)['sentiment'])
counts = Counter(results)
dominant = counts.most_common(1)[0]
agreement_rate = dominant[1] / n_samples
return {
'classification': dominant[0],
'agreement_rate': agreement_rate,
'is_uncertain': agreement_rate < 0.7,
'all_results': dict(counts)
}
result = calibrated_classify('The product is okay, nothing special.')
print(result)التوجيه بناءً على الثقة
يستخدم نظام التوجيه في بيئة الإنتاج مستويات الثقة لتوجيه الطلبات إلى معالجات مختلفة:
def route_by_confidence(text, classify_fn, auto_threshold=8, human_threshold=4):
result = classify_fn(text)
confidence = result.get('confidence', 5)
category = result.get('category') or result.get('sentiment', 'unknown')
if confidence >= auto_threshold:
return {'route': 'auto_process', 'category': category, 'confidence': confidence}
elif confidence >= human_threshold:
return {'route': 'auto_process_with_flag', 'category': category, 'confidence': confidence,
'flag': 'Low confidence — monitor output'}
else:
return {'route': 'human_review', 'category': category, 'confidence': confidence,
'flag': 'Very low confidence — human classification required'}
print(route_by_confidence('Hate this product.', classify_with_confidence))
print(route_by_confidence('It is kind of okay but also not really.', classify_with_confidence))حقول عدم اليقين المنظَّمة
مخطط شامل لعدم اليقين في مخرجات التصنيف:
UNCERTAINTY_SCHEMA = '''
Return JSON:
{
"primary_category": "string",
"confidence": 1-10,
"uncertainty_type": "none | ambiguous_input | insufficient_context | boundary_case | none",
"alternative_categories": ["string"] or [],
"uncertainty_explanation": "string or null",
"recommended_action": "auto_classify | human_review | request_more_info"
}
Uncertainty types:
- ambiguous_input: The text could clearly mean multiple things
- insufficient_context: Need more information to classify correctly
- boundary_case: The text sits on the border between two categories
- none: Clear classification, no uncertainty
'''
print(UNCERTAINTY_SCHEMA)
print('Use this schema for any classification task requiring uncertainty quantification.')تتبّع عدم اليقين في بيئة الإنتاج
راقب معدلات عدم اليقين في بيئة الإنتاج لاكتشاف تدهور المطالبة أو انجراف الفئات:
class ClassificationMonitor:
def __init__(self, human_review_threshold=0.15):
self.total = 0
self.uncertain = 0
self.threshold = human_review_threshold
self.category_counts = {}
def record(self, result):
self.total += 1
cat = result.get('category', 'unknown')
self.category_counts[cat] = self.category_counts.get(cat, 0) + 1
if result.get('confidence', 10) < 5 or result.get('sentiment') == 'UNCERTAIN':
self.uncertain += 1
def report(self):
uncertain_rate = self.uncertain / self.total if self.total else 0
alert = uncertain_rate > self.threshold
return {
'total': self.total,
'uncertain_rate': round(uncertain_rate, 3),
'alert': alert,
'category_distribution': self.category_counts
}
monitor = ClassificationMonitor()
print('Production monitoring system defined.')متى نثق بدرجة الثقة المرتفعة
لا تعني ثقة النموذج المرتفعة دائمًا أن التصنيف صحيح. وفيما يلي أنماط الفشل الشائعة حتى مع الثقة المرتفعة:
- التحيز المنهجي: يضع النموذج باستمرار تسمية خاطئة لنمط معين، مع ثقة مرتفعة في الإجابة الخاطئة
- تحوّل المجال: يكون النموذج واثقًا، لكن أسلوب المدخلة يختلف كثيرًا عما دُرّب عليه
- التملّق: يضبط النموذج مستوى ثقته وفقًا لما يبدو جيدًا، لا وفقًا لدرجة اليقين الفعلية
قيّم دائمًا معايرة الثقة باستخدام مجموعة اختبار معنونة — ليس الدقة فحسب، بل أيضًا ما إذا كانت التنبؤات ذات الثقة المرتفعة أكثر دقة فعلًا من التنبؤات ذات الثقة المنخفضة.
تحقق سريع
ماذا يشير التباين الصغير بين احتمالي الفئتين الأعلى ترتيبًا في نتيجة التصنيف؟
عدم اليقين في التصنيف — أهم النقاط
يحوّل تحديد مستوى عدم اليقين التصنيفَ من صندوق أسود إلى نظام يمكن التحكم فيه:
- اطلب درجات الثقة (من 1 إلى 10) مع كل عملية تصنيف — ولا تتعامل أبدًا مع المخرجات على أنها متساوية في الموثوقية
- استخدم استجابة UNCERTAIN مع المدخلات الملتبسة فعلًا بدلًا من إجبار النموذج على اختيار فئة
- رتّب جميع الفئات حسب الاحتمالية — ويُعد الفرق بين الفئتين الأعلى ترتيبًا أفضل إشارة إلى الغموض
- وجّه الطلب إلى المراجعة البشرية عندما تكون الثقة أقل من العتبة، وعالجه تلقائيًا عندما تتجاوزها
- في التطبيقات الحوارية، اطرح أسئلة توضيحية بدلًا من إرجاع UNCERTAIN
- راقب معدلات عدم اليقين في بيئة الإنتاج — فارتفاعها يشير إلى تدهور المطالبة أو انجراف الفئات
- قيّم دائمًا معايرة الثقة باستخدام بيانات معنونة — وليس الدقة فحسب
الأسئلة الشائعة
هل درس «الثقة وعدم اليقين في التصنيف» مجاني؟
نعم — نص درس «الثقة وعدم اليقين في التصنيف» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Prompt Engineering، انتقل إلى CoddyKit PRO. تتضمن دورة AI Prompt Engineering 4 دروس في المجموع.
ماذا ستتعلم في «الثقة وعدم اليقين في التصنيف»؟
اطلبوا من النموذج تقييم درجة الثقة والتعامل مع التصنيفات الملتبسة تتمرن على AI Prompt Engineering مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Prompt Engineering؟
لا تُشترط خبرة سابقة. AI Prompt Engineering على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «الثقة وعدم اليقين في التصنيف»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Prompt Engineering هذا؟
نعم. كل درس في AI Prompt Engineering يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مطالبات استخراج الكيانات المسماة
- استخراج البيانات المدفوع بالمخطط
- النموذج اللغوي الكبير كمصنّف نصوص
- الثقة وعدم اليقين في التصنيف