الدرس 8 من 23

هندسة تطبيقات LLM

تحسين التكلفة

5 دقيقة للقراءة

تكاليف LLM يمكن أن تتصاعد بسرعة. على نطاق واسع، تحسين التكاليف يصبح بنفس أهمية تحسين الأداء. يغطي هذا الدرس استراتيجيات للسيطرة على التكاليف.

فهم تكاليف LLM

التسعير القائم على الرموز

# التسعير النموذجي (اعتباراً من 2024)
PRICING = {
    "gpt-4": {"input": 0.03, "output": 0.06},      # لكل 1000 رمز
    "gpt-4-turbo": {"input": 0.01, "output": 0.03},
    "gpt-3.5-turbo": {"input": 0.0005, "output": 0.0015},
    "claude-3-opus": {"input": 0.015, "output": 0.075},
    "claude-3-sonnet": {"input": 0.003, "output": 0.015},
}

def estimate_cost(model: str, input_tokens: int, output_tokens: int) -> float:
    pricing = PRICING[model]
    input_cost = (input_tokens / 1000) * pricing["input"]
    output_cost = (output_tokens / 1000) * pricing["output"]
    return input_cost + output_cost

# مثال: 2000 إدخال، 500 إخراج مع GPT-4
cost = estimate_cost("gpt-4", 2000, 500)
# = (2 * 0.03) + (0.5 * 0.06) = $0.09 لكل طلب

التكلفة على نطاق واسع

الطلبات اليومية النموذج التكلفة الشهرية
10,000 GPT-4 ~$27,000
10,000 GPT-4-Turbo ~$10,500
10,000 GPT-3.5-Turbo ~$600
10,000 Claude-3-Sonnet ~$4,500

الاستراتيجية 1: توجيه النموذج

وجّه الطلبات إلى أرخص نموذج يمكنه التعامل معها.

class ModelRouter:
    def __init__(self):
        self.models = {
            "simple": "gpt-3.5-turbo",    # استعلامات بسيطة
            "medium": "gpt-4-turbo",       # معقدة لكن غير حرجة
            "complex": "gpt-4"             # حرجة، مهام معقدة
        }

    def classify_complexity(self, query: str) -> str:
        # استدلالات بسيطة
        if len(query) < 100 and not any(
            word in query.lower()
            for word in ["analyze", "complex", "detailed", "compare"]
        ):
            return "simple"

        # استخدم مصنف لدقة أكبر
        complexity_score = self.complexity_classifier.predict(query)
        if complexity_score < 0.3:
            return "simple"
        elif complexity_score < 0.7:
            return "medium"
        return "complex"

    async def route(self, query: str) -> str:
        complexity = self.classify_complexity(query)
        model = self.models[complexity]
        return await self.call_model(model, query)

نتائج الإنتاج: تخفيض 40-60% في التكلفة مع انخفاض أقل من 5% في الجودة.

الاستراتيجية 2: تحسين المطالبات

تقليل عدد الرموز دون فقدان الجودة.

class PromptOptimizer:
    def __init__(self):
        self.abbreviations = {
            "Please provide": "Give",
            "In order to": "To",
            "It is important to note that": "Note:",
        }

    def optimize(self, prompt: str) -> str:
        optimized = prompt

        # إزالة العبارات المطولة
        for verbose, concise in self.abbreviations.items():
            optimized = optimized.replace(verbose, concise)

        # إزالة المسافات الزائدة
        optimized = " ".join(optimized.split())

        return optimized

    def estimate_savings(self, original: str, optimized: str) -> dict:
        original_tokens = len(original) / 4  # تقدير تقريبي
        optimized_tokens = len(optimized) / 4
        savings = (original_tokens - optimized_tokens) / original_tokens
        return {
            "original_tokens": int(original_tokens),
            "optimized_tokens": int(optimized_tokens),
            "savings_percent": round(savings * 100, 1)
        }

قبل وبعد

# قبل: 47 رمز
prompt_before = """
Please provide a detailed summary of the following document.
It is important to note that you should focus on the key points
and main arguments presented by the author.
"""

# بعد: 18 رمز
prompt_after = """
Summarize this document. Focus on key points and main arguments.
"""

# التوفير: 62% رموز أقل

الاستراتيجية 3: إدارة نافذة السياق

تحسين ما يدخل في السياق.

class ContextManager:
    def __init__(self, max_tokens: int = 4000):
        self.max_tokens = max_tokens

    def optimize_context(
        self,
        system_prompt: str,
        history: list,
        retrieved_docs: list,
        query: str
    ) -> dict:
        # حجز رموز للمخرجات
        available = self.max_tokens - 500

        # تخصيص الأولوية
        allocations = {
            "system": min(500, len(system_prompt)),
            "query": len(query),
            "docs": 0,
            "history": 0
        }

        remaining = available - allocations["system"] - allocations["query"]

        # إعطاء الأولوية للمستندات الحديثة على السجل القديم
        doc_budget = int(remaining * 0.7)
        history_budget = int(remaining * 0.3)

        allocations["docs"] = doc_budget
        allocations["history"] = history_budget

        return {
            "system": system_prompt[:allocations["system"]],
            "history": self._truncate_history(history, history_budget),
            "docs": self._truncate_docs(retrieved_docs, doc_budget),
            "query": query
        }

    def _truncate_history(self, history: list, budget: int) -> list:
        # الاحتفاظ بأحدث الرسائل ضمن الميزانية
        result = []
        tokens_used = 0
        for msg in reversed(history):
            msg_tokens = len(msg["content"]) // 4
            if tokens_used + msg_tokens > budget:
                break
            result.insert(0, msg)
            tokens_used += msg_tokens
        return result

الاستراتيجية 4: سلاسل الاحتياط

استخدم النماذج المكلفة فقط عندما تفشل الأرخص.

class FallbackChain:
    def __init__(self):
        self.chain = [
            {"model": "gpt-3.5-turbo", "timeout": 5},
            {"model": "gpt-4-turbo", "timeout": 10},
            {"model": "gpt-4", "timeout": 30},
        ]

    async def execute(self, prompt: str, quality_check) -> dict:
        for i, config in enumerate(self.chain):
            try:
                response = await self.call_model(
                    config["model"],
                    prompt,
                    timeout=config["timeout"]
                )

                # التحقق من استيفاء الاستجابة لعتبة الجودة
                if quality_check(response):
                    return {
                        "response": response,
                        "model_used": config["model"],
                        "fallback_level": i
                    }

            except Exception as e:
                continue  # جرب النموذج التالي

        raise Exception("فشلت جميع النماذج")

# الاستخدام
chain = FallbackChain()
result = await chain.execute(
    prompt="اشرح الحوسبة الكمومية",
    quality_check=lambda r: len(r) > 100 and "كم" in r.lower()
)

لوحة مراقبة التكلفة

تتبع التكاليف في الوقت الفعلي:

class CostTracker:
    def __init__(self):
        self.daily_costs = defaultdict(float)
        self.alerts = []

    def record(self, model: str, input_tokens: int, output_tokens: int):
        cost = estimate_cost(model, input_tokens, output_tokens)
        today = datetime.utcnow().date().isoformat()
        self.daily_costs[today] += cost

        # التحقق من تنبيهات الميزانية
        if self.daily_costs[today] > DAILY_BUDGET:
            self.trigger_alert("تم تجاوز الميزانية اليومية")

    def get_report(self) -> dict:
        return {
            "today": self.daily_costs[datetime.utcnow().date().isoformat()],
            "this_week": sum(
                cost for date, cost in self.daily_costs.items()
                if self._is_this_week(date)
            ),
            "by_model": self._breakdown_by_model()
        }

الآن لننتقل إلى تصميم نظام RAG—أحد أكثر مواضيع المقابلات شيوعاً. :::

اختبار

الوحدة 2: هندسة تطبيقات LLM

خذ الاختبار