หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์
มอบหน่วยความจำระยะสั้นและระยะยาวให้เอเจนต์ RAG เพื่อให้สนทนาได้และเรียกคืนข้อเท็จจริงจากรอบการสนทนาก่อนหน้า
หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์ เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 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 replyThe 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] = messagesPersistence
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
เรียนรู้ LangChain / RAG / Vector DBs ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 12
- บทเรียน
- 48
คำถามที่พบบ่อย
บทเรียน “หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์”
มอบหน่วยความจำระยะสั้นและระยะยาวให้เอเจนต์ RAG เพื่อให้สนทนาได้และเรียกคืนข้อเท็จจริงจากรอบการสนทนาก่อนหน้า คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 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 ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- แนวคิดเกี่ยวกับเอเจนต์และเครื่องมือของ LangChain
- การสร้างกระบวนการทำงาน RAG แบบหลายเอเจนต์
- การผสานรวม API ภายนอกเป็นเครื่องมือ
- หน่วยความจำและสถานะใน RAG แบบใช้เอเจนต์