LangChain / RAG / Vector DBs · Lekcja

Pamięć i stan w agentowym RAG

Zapewnij agentowi RAG pamięć krótkoterminową i długoterminową, aby mógł prowadzić rozmowy i przywoływać fakty w kolejnych turach.

Lekcja 4 z 413 kroki

Pamięć i stan w agentowym RAG to bezpłatna lekcja LangChain / RAG / Vector DBs na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej LangChain / RAG / Vector DBs, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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
Bezpłatny start

Ucz się LangChain / RAG / Vector DBs dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
12
Lekcje
48

Często zadawane pytania

Czy lekcja „Pamięć i stan w agentowym RAG” jest bezpłatna?

Tak — pełny tekst „Pamięć i stan w agentowym RAG” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu LangChain / RAG / Vector DBs, przejdź na CoddyKit PRO. Kurs LangChain / RAG / Vector DBs zawiera 4 lekcji w sumie.

Co nauczysz się w „Pamięć i stan w agentowym RAG”?

Zapewnij agentowi RAG pamięć krótkoterminową i długoterminową, aby mógł prowadzić rozmowy i przywoływać fakty w kolejnych turach. Ćwiczysz LangChain / RAG / Vector DBs z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć LangChain / RAG / Vector DBs?

Nie wymagamy żadnego doświadczenia. LangChain / RAG / Vector DBs w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Pamięć i stan w agentowym RAG”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji LangChain / RAG / Vector DBs?

Tak. Każda lekcja LangChain / RAG / Vector DBs zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Agenci LangChain i koncepcja narzędzi
  2. Tworzenie wieloagentowych przepływów pracy RAG
  3. Integracja zewnętrznych API jako narzędzi
  4. Pamięć i stan w agentowym RAG
← Powrót do LangChain / RAG / Vector DBs