إضافة الذاكرة وحالة المحادثة إلى الوكلاء
أضف ذاكرة قصيرة وطويلة الأمد إلى وكلاء LangChain حتى يتذكروا السياق عبر الأدوار وينتجوا محادثات متماسكة متعددة الخطوات.
إضافة الذاكرة وحالة المحادثة إلى الوكلاء درس مجاني في AI Agents with LangChain & Autonomous Workflows على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في AI Agents with LangChain & Autonomous Workflows، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة AI Agents with LangChain & Autonomous Workflows 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Why Agents Need Memory
LLMs are stateless: each call knows nothing about the last unless you tell it. Without memory, an agent forgets your name the instant you say it.
The Context Window
Memory ultimately means stuffing prior info into the context window. That window is finite, so the real challenge is deciding what to keep and what to drop.
Buffer Memory
Buffer memory stores the whole conversation and replays it every turn. Accurate, but it grows without bound and eventually overflows the context window.
from langchain.memory import ConversationBufferMemory
memory = ConversationBufferMemory()
memory.save_context({'input': 'Hi, I am Lena'}, {'output': 'Hello Lena!'})Windowed Memory
Windowed memory keeps only the last N exchanges. It bounds size and forgets older context — perfect when only recent turns matter.
from langchain.memory import ConversationBufferWindowMemory
memory = ConversationBufferWindowMemory(k=4)Summary Memory
Summary memory periodically condenses older turns with the LLM, keeping the gist plus recent messages — preserving meaning in a small footprint.
from langchain.memory import ConversationSummaryMemory
memory = ConversationSummaryMemory(llm=llm)Short-Term vs Long-Term
Two flavors serve different needs: short-term memory holds the current chat in the prompt, while long-term persists facts across sessions in a store.
Long-Term Memory with Vectors
For knowledge that must survive sessions, store messages as embeddings in a vector store and retrieve the most relevant ones by similarity instead of replaying everything.
from langchain.memory import VectorStoreRetrieverMemory
memory = VectorStoreRetrieverMemory(retriever=vectorstore.as_retriever())Wiring Memory into a Chain
Wire memory into a conversation chain. Each call loads prior context, runs the model, and saves the new exchange automatically.
from langchain.chains import ConversationChain
chain = ConversationChain(llm=llm, memory=memory)
print(chain.predict(input='What is my name?'))Session and User Scoping
Real apps serve many users at once. Scope memory by session or user id so separate conversations never leak into each other.
store = {}
def get_memory(session_id):
if session_id not in store:
store[session_id] = ConversationBufferMemory()
return store[session_id]Cost and Privacy Tradeoffs
More memory means more tokens — higher cost and latency. Long-term stores may hold sensitive data, so mind retention limits and what you're allowed to keep.
Choosing a Memory Strategy
Choosing a strategy: buffer or window for short chats, summary for long ones, vector for cross-session facts — always scoped per user and mindful of cost.
Quick Check
You've met several memory types — which fits when? Time to put it to the test.
Recap
Recap: memory feeds prior context into a finite window. Buffer, window, and summary trade accuracy for size, vector stores enable long-term recall — always scope and watch cost.
تعلم AI Agents with LangChain & Autonomous Workflows مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 12
- الدروس
- 50
الأسئلة الشائعة
هل درس «إضافة الذاكرة وحالة المحادثة إلى الوكلاء» مجاني؟
نعم — نص درس «إضافة الذاكرة وحالة المحادثة إلى الوكلاء» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة AI Agents with LangChain & Autonomous Workflows، انتقل إلى CoddyKit PRO. تتضمن دورة AI Agents with LangChain & Autonomous Workflows 4 دروس في المجموع.
ماذا ستتعلم في «إضافة الذاكرة وحالة المحادثة إلى الوكلاء»؟
أضف ذاكرة قصيرة وطويلة الأمد إلى وكلاء LangChain حتى يتذكروا السياق عبر الأدوار وينتجوا محادثات متماسكة متعددة الخطوات. تتمرن على AI Agents with LangChain & Autonomous Workflows مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ AI Agents with LangChain & Autonomous Workflows؟
لا تُشترط خبرة سابقة. AI Agents with LangChain & Autonomous Workflows على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.
كم من الوقت يستغرق درس «إضافة الذاكرة وحالة المحادثة إلى الوكلاء»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس AI Agents with LangChain & Autonomous Workflows هذا؟
نعم. كل درس في AI Agents with LangChain & Autonomous Workflows يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- فهم وكلاء الذكاء الاصطناعي ونماذج اللغة الكبيرة
- شرح المكونات الأساسية لـ LangChain
- بناء أول وكيل بسيط لك
- إضافة الذاكرة وحالة المحادثة إلى الوكلاء