0Pricing
LangChain / RAG / Vector DBs · درس

الذاكرة والحالة في RAG الوكيلي

امنح وكيل RAG ذاكرة قصيرة وطويلة الأمد، حتى يتمكن من مواصلة المحادثات وتذكّر الحقائق عبر الأدوار.

الذاكرة والحالة في RAG الوكيلي درس مجاني في LangChain / RAG / Vector DBs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في LangChain / RAG / Vector DBs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة LangChain / RAG / Vector DBs 4 دروس في المجموع.

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

Why Agents Need Memory

A stateless agent forgets everything after each call. Memory lets an agent track the conversation, remember user preferences, and avoid repeating retrievals.

Short-Term vs. Long-Term

Short-term memory holds the current conversation in the context window. Long-term memory persists facts across sessions, often in a vector store or database.

Conversation Buffer

The simplest memory keeps the full message history and replays it each turn so the model has full context.

history = []

def chat(user_msg):
    history.append({"role": "user", "content": user_msg})
    reply = llm.invoke(history)
    history.append({"role": "assistant", "content": reply})
    return reply

The Context Window Limit

Replaying the entire history eventually overflows the model context. You must summarize or trim older turns to stay within the token budget.

Summary Memory

Periodically compress old turns into a running summary, keeping recent turns verbatim. This preserves gist while freeing tokens.

if len(history) > 20:
    summary = llm.invoke("Summarize: " + str(history[:-6]))
    history[:] = [{"role": "system", "content": summary}] + history[-6:]

Long-Term Memory via Vectors

Store durable facts as embeddings. On each turn, retrieve relevant memories by similarity and inject them, just like RAG over a knowledge base.

memory_store.add_texts(["User prefers metric units"])
relevant = memory_store.similarity_search(user_msg, k=3)

Choosing What to Remember

Do not store everything. Extract durable, reusable facts (preferences, decisions, entities) and skip ephemeral chatter to keep long-term memory clean.

State Beyond Chat

Agents also track non-conversational state: which tools ran, intermediate results, and a scratchpad of reasoning steps used to plan the next action.

Thread and Session Keys

In multi-user systems, scope memory by a thread_id or user_id so conversations never leak between people.

def get_history(thread_id):
    return store.get(thread_id, [])

def save(thread_id, messages):
    store[thread_id] = messages

Persistence

For memory to survive restarts, back it with a database or checkpointer rather than an in-process dictionary. LangGraph offers checkpointers for exactly this.

Putting It Together

Combine a trimmed conversation buffer for recency, summary memory for the middle, and vector long-term memory for durable facts, all keyed by session.

Quick Check

Test your understanding of agent memory.

Recap

You added memory to agents:

  • Short-term buffer for the current chat
  • Summary memory to fit the context window
  • Long-term vector memory for durable facts
  • Scope by session and persist for durability

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

هل درس «الذاكرة والحالة في RAG الوكيلي» مجاني؟

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

ماذا ستتعلم في «الذاكرة والحالة في RAG الوكيلي»؟

امنح وكيل RAG ذاكرة قصيرة وطويلة الأمد، حتى يتمكن من مواصلة المحادثات وتذكّر الحقائق عبر الأدوار. تتمرن على LangChain / RAG / Vector DBs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ LangChain / RAG / Vector DBs؟

لا تُشترط خبرة سابقة. LangChain / RAG / Vector DBs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

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

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

هل يمكنني كتابة وتشغيل أكواد في درس LangChain / RAG / Vector DBs هذا؟

نعم. كل درس في LangChain / RAG / Vector DBs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

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

  1. مفاهيم الوكلاء والأدوات في LangChain
  2. بناء مسارات عمل RAG متعددة الوكلاء
  3. دمج واجهات API الخارجية كأدوات
  4. الذاكرة والحالة في RAG الوكيلي
← العودة إلى LangChain / RAG / Vector DBs