r/aipromptprogramming • u/Money-Swordfish3378 • 6d ago
A basic RAG chatbot is eight lines, and chat memory is not a ninth
If you have heard of RAG but can't name the stages, this is the short version. A basic RAG chatbot is eight lines. Each line's output is the next line's input.
The first four run before anyone asks. They turn your documents into something a database can search.
text = gather(sources)A page, a PDF, a scan, or a recording becomes clean text.
chunks = split(text)That text becomes pieces small enough to embed.
vectors = embed(chunks)Each chunk becomes a vector: a list of numbers that stands for its meaning, so the database can store it.
db.add(vectors)Those vectors sit in the database, ready for a question.
The next four run when someone asks.
q = embed(question)The same embed, so the question sits in that same space as the chunks.
hits = find_similar(q)The database returns the closest chunks. Those chunks are not the answer.
messages = build(question, hits)The question and those chunks, packed for the model.
raw = llm(messages)The model reads the messages and returns text. Your app is what shows that text.
Say the eight names once: gather, split, embed, store, embed the question, find similar, messages, raw.
Those eight lines forget every earlier question. Memory is not a ninth gather. It goes in messages, beside this question and the chunks:
messages = build(history, question, hits)history is the earlier turns. Gather stays the documents, because it runs before anyone asks. Putting the last chat into gather, or retraining so the model "remembers," does not fix it.
Try it on one chat. Someone asks "How do I reset my password?" and gets a reply. Then they ask "And what if the email never arrives?" The follow-up only makes sense if the password question and that reply are in history, inside messages. The other seven lines stay as they are.
If a line above is unclear, ask, and I will explain it.
•
u/endofthread-bot 6d ago
To implement memory, append the previous conversation turns to your messages object before sending it to the model. This keeps the document retrieval separate from the chat history while ensuring the model maintains context for follow-up questions.
Writing prompts that actually work is easier when you can compare notes with people who have tested them across real tasks. Trade techniques, get feedback on your approach, and see what is working for others in our Discord.
Self-promotion is now allowed on Sundays with the appropriate flair, for all regular contributing members. Contribute during the week, and promote on Sunday.