LLM Apps in Production (RAG + Vector DB + Caching) · درس

هندسة الأوامر ونوافذ السياق

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

الدرس 4 من 413 خطوة

هندسة الأوامر ونوافذ السياق درس مجاني في LLM Apps in Production (RAG + Vector DB + Caching) على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في LLM Apps in Production (RAG + Vector DB + Caching)، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة LLM Apps in Production (RAG + Vector DB + Caching) 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

The Context Window

Every LLM has a fixed context window — the max tokens it reads and writes per call. System prompt, retrieved docs, history, and your question all have to fit.

What Lives in the Window

A production RAG prompt packs in system instructions, retrieved context, prior history, and the current question. Exceed the window and something gets cut.

Anatomy of a Prompt

A clear prompt structure helps the model tell instructions apart from data. Here's a clean layout separating context from the question.

prompt = (
    'You are a support assistant. '
    'Answer ONLY from the context.\n\n'
    'Context:\n{context}\n\n'
    'Question: {question}'
)

Grounding Instructions

To cut hallucination, add grounding instructions: tell the model to answer only from the provided context and to admit when it doesn't know.

rule = 'If the answer is not in the context, say you do not know.'

Using a Prompt Template

A prompt template makes prompts reusable and safe to fill with variables. LangChain's ChatPromptTemplate does exactly this.

from langchain_core.prompts import ChatPromptTemplate

template = ChatPromptTemplate.from_messages([
    ('system', 'Answer only from context: {context}'),
    ('human', '{question}')
])

Counting Tokens

Before sending, count tokens so you don't overflow the window. Rough rule for English: about 4 characters per token.

import tiktoken
enc = tiktoken.get_encoding('cl100k_base')
print(len(enc.encode(filled_prompt)))

When Context Is Too Big

When context is too big, shrink it: fewer chunks, smaller chunk sizes, or summarize. Never silently truncate the middle — you might drop the answer.

The Lost-in-the-Middle Effect

Watch the lost-in-the-middle effect: models attend best to the start and end of context, worst to the middle. Put your most relevant chunks first or last.

Reserving Output Space

Input and output share the window. Fill it all with input and there's no room to generate — so reserve output space for the expected answer length.

max_output = 800
budget_for_input = WINDOW - max_output

Few-Shot Examples

A few few-shot examples can steer format and tone, but they eat tokens. Weigh their value against the space they consume.

Iterating on Prompts

Prompt engineering is empirical: change one thing at a time, test on real questions, and measure. Small wording tweaks can shift answer quality a lot.

Quick Check

Test your understanding of context windows.

Recap

Recap: the context window holds system, context, history, question, and output. Structure prompts, ground them, count tokens, beat lost-in-the-middle, and reserve output room.

البدء مجانًا

تعلم LLM Apps in Production (RAG + Vector DB + Caching) مع معلم ذكاء اصطناعي — مجانًا

اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.

الدورات
12
الدروس
48

الأسئلة الشائعة

هل درس «هندسة الأوامر ونوافذ السياق» مجاني؟

نعم — نص درس «هندسة الأوامر ونوافذ السياق» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة LLM Apps in Production (RAG + Vector DB + Caching)، انتقل إلى CoddyKit PRO. تتضمن دورة LLM Apps in Production (RAG + Vector DB + Caching) 4 دروس في المجموع.

ماذا ستتعلم في «هندسة الأوامر ونوافذ السياق»؟

افهم نافذة السياق التي تضع حدود كل استدعاء لـ LLM، وتعلّم صياغة أوامر تجمع السياق المسترجع والتعليمات والأسئلة ضمنها للحصول على إجابات موثوقة في الإنتاج. تتمرن على LLM Apps in Production (RAG + Vector DB + Caching) مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ LLM Apps in Production (RAG + Vector DB + Caching)؟

لا تُشترط خبرة سابقة. LLM Apps in Production (RAG + Vector DB + Caching) على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «هندسة الأوامر ونوافذ السياق»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس LLM Apps in Production (RAG + Vector DB + Caching) هذا؟

نعم. كل درس في LLM Apps in Production (RAG + Vector DB + Caching) يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. فهم تطبيقات نماذج اللغة الكبيرة في بيئة الإنتاج
  2. أساسيات التوليد المعزَّز بالاسترجاع
  3. نظرة عامة على بنية نظام RAG الأساسي
  4. هندسة الأوامر ونوافذ السياق
← العودة إلى LLM Apps in Production (RAG + Vector DB + Caching)