الفصل 3: التحديات الخاصة بالنماذج اللغوية الكبيرة
1. إدارة الهلوسة
يمكن للنماذج اللغوية الكبيرة أن تولد معلومات تبدو معقولة ولكنها غير صحيحة:
class HallucinationDetector:
def __init__(self, fact_checker_model):
self.fact_checker = fact_checker_model
def check_response(self, query: str, response: str, sources: list) -> dict:
"""Check if response is grounded in sources."""
# Extract claims from response
claims = self.extract_claims(response)
# Verify each claim against sources
verified_claims = []
hallucinated_claims = []
for claim in claims:
if self.verify_against_sources(claim, sources):
verified_claims.append(claim)
else:
hallucinated_claims.append(claim)
return {
"hallucination_rate": len(hallucinated_claims) / len(claims),
"verified_claims": verified_claims,
"hallucinated_claims": hallucinated_claims
}
2. الأمان وتصفية المحتوى
class SafetyGuard:
def __init__(self):
self.blocked_topics = ["illegal activities", "harmful content", ...]
self.pii_patterns = [r"\b\d{3}-\d{2}-\d{4}\b", ...] # SSN, etc.
def check_input(self, user_input: str) -> tuple[bool, str]:
"""Check user input for safety issues."""
# Topic filtering
for topic in self.blocked_topics:
if self.detects_topic(user_input, topic):
return False, f"Blocked topic: {topic}"
return True, "OK"
def check_output(self, llm_output: str) -> str:
"""Sanitize LLM output."""
# Remove PII
for pattern in self.pii_patterns:
llm_output = re.sub(pattern, "[REDACTED]", llm_output)
return llm_output
3. تحسين زمن الاستجابة
| التقنية | تقليل زمن الاستجابة | المقايضة |
|---|---|---|
| البث | زمن الاستجابة المدرك | تعقيد التنفيذ |
| التخزين المؤقت | 90%+ عند ضربات التخزين المؤقت | التخزين، إبطال التخزين المؤقت |
| نماذج أصغر | 50-80% | الجودة |
| التجميع | متغير | زمن الاستجابة |
4. إدارة التكلفة
class CostTracker:
def __init__(self, budget_limit: float):
self.budget_limit = budget_limit
self.total_cost = 0
def track_request(self, model: str, input_tokens: int, output_tokens: int):
cost = calculate_cost(input_tokens, output_tokens, model)
self.total_cost += cost
if self.total_cost > self.budget_limit * 0.8:
alert_team("Approaching budget limit")
if self.total_cost > self.budget_limit:
raise BudgetExceededError("Budget limit reached")
return cost
التالي: اختبار الوحدة 10