Deep Learning for Text — RNNs to Transformers, Seq2Seq, Attention, and Building a Real Conversational AI System (with Code)
Deep learning for text end to end: embeddings, RNNs/LSTMs, seq2seq, attention, transformers, and a full conversational-AI system with memory, RAG, streaming.
- Author
- Randhir Jassal
- Published
- Reading time
- 36 min read
- Views
- 5 views
Deep Learning for Text — RNNs to Transformers, Seq2Seq, Attention, and Building a Real Conversational AI System (with Code)
Text is sequential, variable-length, and full of long-range dependencies — which is exactly why it broke every model we had until deep learning caught up. The journey from "bag of words" to ChatGPT is really the story of one problem: how does a model remember and relate things across a sequence?
This guide walks that whole arc with code: how neural networks process text (embeddings, RNNs, LSTMs), sequence-to-sequence models that map one sequence to another (translation, summarization), the attention mechanism that fixed seq2seq''s bottleneck and led to transformers, the architecture of modern conversational AI, and finally a complete, runnable conversational-AI system — backend, memory, RAG grounding, streaming, and a chat UI. Every concept, every case, real code throughout.
TL;DR
- Text needs sequence models because order and long-range context matter. Embeddings turn tokens into vectors; RNNs/LSTMs process them in order but forget over long distances.
- Seq2Seq (encoder → decoder) maps an input sequence to an output sequence — translation, summarization, dialogue. The original version had a fatal bottleneck: cramming the whole input into one vector.
- Attention fixed the bottleneck by letting the decoder look back at every input token. "Attention is all you need" then dropped the RNN entirely → the transformer.
- Modern conversational AI = a pretrained transformer (the brain) + conversation memory + retrieval grounding + tools + guardrails. The model is one component, not the whole system.
- The project: a conversational-AI system with multi-turn memory, RAG grounding, streaming responses, and a chat UI — works with a hosted API or a local open model.
1. Why text is hard for neural networks
A classifier sees a fixed-size input (an image is always 224×224). Text isn''t: "ok" and a 5,000-word essay are both valid inputs. And meaning depends on order and distance — "the cat the dog chased ran" needs you to track who-did-what across many words.
The deep learning story for text is the story of solving three problems:
- Representation — how do you turn words into numbers a network can use? → embeddings
- Sequence — how do you process variable-length, ordered input? → RNNs → LSTMs → transformers
- Long-range dependency — how do you relate token 1 to token 500? → attention
We''ll build up through each.
2. Neural Networks for Text Processing
2.1 Step 1 — embeddings (words become vectors)
Neural networks do math on numbers, not strings. The first layer of every text model is an embedding: a lookup table mapping each token ID to a dense vector. These vectors are learned so that similar words land near each other.
import torch
import torch.nn as nn
vocab_size = 10000
embed_dim = 128
embedding = nn.Embedding(vocab_size, embed_dim)
token_ids = torch.tensor([[5, 42, 9, 1]]) # one sentence, 4 tokens
vectors = embedding(token_ids) # shape (1, 4, 128)
After training, embedding["king"] - embedding["man"] + embedding["woman"] ≈ embedding["queen"]. The network discovers semantic structure on its own.
2.2 Step 2 — processing the sequence with an RNN
A Recurrent Neural Network reads tokens one at a time, maintaining a hidden state that carries information forward:
h_t = f(h_{t-1}, x_t) # new state depends on previous state + current token
import torch.nn as nn
class RNNTextClassifier(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim, num_classes):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.rnn = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, num_classes)
def forward(self, x):
emb = self.embed(x) # (B, T, embed_dim)
out, (h_n, c_n) = self.rnn(emb) # h_n: final hidden state
return self.fc(h_n[-1]) # classify from the last state
The hidden state is the RNN''s "memory" — but a vanilla RNN forgets quickly. By the time it reaches token 100, the signal from token 1 has vanished (the vanishing gradient problem).
2.3 Step 3 — LSTM / GRU fix short memory
LSTMs (Long Short-Term Memory) add gates that decide what to remember, forget, and output. This lets them carry information across much longer spans:
# An LSTM cell has three gates:
# forget gate: what to drop from memory
# input gate: what new info to store
# output gate: what to expose as the hidden state
self.rnn = nn.LSTM(embed_dim, hidden_dim, num_layers=2,
batch_first=True, bidirectional=True, dropout=0.3)
GRU is a lighter LSTM (two gates instead of three) — often as good, faster to train.
LSTMs powered NLP from ~2014–2018: translation, sentiment, speech. But they still process tokens sequentially (can''t parallelize across the sequence) and still struggle with very long dependencies. The transformer would fix both — but first, seq2seq.
3. Sequence-to-Sequence Models
3.1 The idea
Classification maps a sequence → one label. But many tasks map a sequence → another sequence:
| Task | Input → Output |
|---|---|
| Translation | English sentence → French sentence |
| Summarization | Article → summary |
| Dialogue | User message → bot reply |
| Code generation | Description → code |
Seq2Seq uses two RNNs:
- An encoder reads the input and compresses it into a context vector.
- A decoder reads that context vector and generates the output token by token.
Input: "how are you"
┌─────────────┐
│ ENCODER │ → context vector (summary of the whole input)
└─────────────┘
│
▼
┌─────────────┐
│ DECODER │ → "comment allez vous" (one token at a time)
└─────────────┘
3.2 The code (encoder-decoder LSTM)
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
def forward(self, x):
emb = self.embed(x)
outputs, (h, c) = self.lstm(emb)
return outputs, (h, c) # outputs = per-token states; (h,c) = final context
class Decoder(nn.Module):
def __init__(self, vocab_size, embed_dim, hidden_dim):
super().__init__()
self.embed = nn.Embedding(vocab_size, embed_dim)
self.lstm = nn.LSTM(embed_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, vocab_size)
def forward(self, token, hidden):
emb = self.embed(token) # one token at a time
out, hidden = self.lstm(emb, hidden)
logits = self.fc(out) # predict next output token
return logits, hidden
3.3 The fatal flaw — the bottleneck
The encoder squeezes the entire input into a single fixed-size context vector. For a 5-word sentence, fine. For a 50-word paragraph, that one vector can''t hold everything — early information gets crushed. Translation quality fell off a cliff as sentences got longer.
This single problem is what attention was invented to solve.
4. Attention Mechanisms and Transformers
4.1 The breakthrough: let the decoder look back
Instead of forcing the encoder to cram everything into one vector, attention lets the decoder look at all the encoder''s per-token states and decide, at each output step, which input tokens matter most.
When translating "the agreement on the European Economic Area" → French, at the step where it generates "économique," attention puts most weight on the English word "Economic." It learns this alignment automatically.
import torch
import torch.nn.functional as F
def attention(decoder_state, encoder_states):
# decoder_state: (hidden_dim,) — "what am I looking for now?"
# encoder_states: (src_len, hidden_dim) — all input token representations
scores = encoder_states @ decoder_state # relevance of each input token
weights = F.softmax(scores, dim=0) # turn into a distribution
context = (weights.unsqueeze(-1) * encoder_states).sum(0) # weighted blend
return context, weights # weights = interpretable alignment
Attention solved the bottleneck and, as a bonus, gave us interpretability — you can see which input words the model focused on.
4.2 "Attention Is All You Need" — drop the RNN entirely
In 2017, researchers realized: if attention is doing the heavy lifting, do we even need the RNN? The answer was no. The transformer uses attention everywhere and removes recurrence completely.
Two huge wins:
- Parallelism — without sequential recurrence, every token is processed at once → train on far more data, far faster.
- Direct long-range connections — token 1 can attend to token 500 in a single step (an RNN needed 500 sequential steps).
4.3 Self-attention — the core operation
In a transformer, each token produces a Query ("what am I looking for?"), Key ("what do I offer?"), and Value ("what I contribute"). Attention = match queries to keys, blend values.
import torch
import torch.nn.functional as F
import math
def self_attention(X, W_q, W_k, W_v):
Q = X @ W_q # (seq, d)
K = X @ W_k
V = X @ W_v
scores = Q @ K.transpose(-2, -1) / math.sqrt(Q.size(-1))
weights = F.softmax(scores, dim=-1) # (seq, seq) — every token vs every token
return weights @ V
Stack many of these (multi-head, many layers) + feed-forward networks + residuals + layer norm, and you have a transformer block. Stack 12–96 blocks → BERT, GPT, T5, and every modern conversational model.
4.4 The three transformer shapes
| Shape | Direction | Used for | Example |
|---|---|---|---|
| Encoder-only | bidirectional | understanding | BERT |
| Decoder-only | left-to-right | generation, chat | GPT, Llama |
| Encoder-decoder | both | seq2seq (translation, summary) | T5, BART |
Conversational AI uses decoder-only models — they generate replies token by token, conditioned on the conversation so far.
5. Building Chatbots and Conversational AI
5.1 The evolution — and why you shouldn''t build the old way
| Era | Approach | Reality in 2026 |
|---|---|---|
| Rule-based | if/else, regex, decision trees | Brittle; only for narrow flows |
| Retrieval-based | match query to a FAQ of canned answers | Still useful for strict, auditable answers |
| Seq2Seq (RNN) | train an encoder-decoder on dialogue pairs | Obsolete — incoherent, forgets context |
| LLM-based | a pretrained decoder transformer + orchestration | The standard |
You do not train a chatbot from scratch. You take a pretrained instruct model (Llama-3-Instruct, Qwen-Instruct, or a hosted API) and build a system around it.
5.2 A conversational AI system is more than a model
The model generates text. A real assistant needs five things around it:
┌──────────────────────────────────────┐
user message → │ 1. Conversation memory (history) │
│ 2. Retrieval / grounding (RAG) │
│ 3. Prompt construction │
│ 4. The LLM (generates the reply) │
│ 5. Guardrails (PII, safety, limits) │
└──────────────────────────────────────┘ → reply
- Memory — the model is stateless; you must feed it the conversation history every turn.
- Grounding (RAG) — to answer from your knowledge (docs, policies), retrieve relevant chunks and put them in the prompt. This is what stops it making things up.
- Prompt construction — system instructions + retrieved context + history + new message, assembled correctly with the model''s chat template.
- The LLM — generates the reply.
- Guardrails — redact PII, block unsafe requests, cap tokens, rate-limit.
5.3 The conversation loop
def chat_turn(history, user_message, retriever, llm):
# 1. Retrieve grounding context
context = retriever.search(user_message, k=4)
# 2. Build the prompt: system + context + history + new message
messages = [
{"role": "system", "content": SYSTEM_PROMPT.format(context=context)},
*history, # prior turns
{"role": "user", "content": user_message},
]
# 3. Generate
reply = llm.generate(messages)
# 4. Update memory
history.append({"role": "user", "content": user_message})
history.append({"role": "assistant", "content": reply})
# 5. Trim history if it exceeds the context window
history = trim_to_token_budget(history, max_tokens=4000)
return reply, history
That loop — retrieve, construct, generate, remember, trim — is the heart of every chatbot.
5.4 Managing the context window (memory)
The model has a finite context window. A long conversation will eventually overflow it. Three strategies:
# 1. Sliding window — keep the last N turns (simple, loses old context)
def trim_to_token_budget(history, max_tokens, tokenizer):
while count_tokens(history, tokenizer) > max_tokens and len(history) > 2:
history = history[2:] # drop the oldest user+assistant pair
return history
# 2. Summarization memory — periodically summarize old turns into one message
# 3. Vector memory — embed old turns, retrieve only relevant ones (RAG over history)
For most chatbots, a sliding window + RAG over a knowledge base is enough. Add summarization memory only for very long sessions.
6. Project: a real Conversational AI System
A complete chatbot: multi-turn memory, RAG grounding over your documents, streaming responses, guardrails, and a chat UI. Works with a hosted API or a local open model.
6.1 Architecture
Browser (Gradio chat UI)
│ message + history
▼
FastAPI /chat (SSE streaming)
│ 1. retrieve grounding chunks (vector search)
│ 2. build prompt (system + context + history + message)
│ 3. guardrails (PII redact, safety)
▼
LLM backend ── hosted API (OpenAI/Azure) or local HF model
│ streamed reply tokens
▼
Browser renders streaming reply
6.2 Project structure
chatbot/
├── app/
│ ├── main.py # FastAPI chat service (SSE streaming)
│ ├── llm.py # pluggable LLM backend
│ ├── retriever.py # RAG: embed docs + vector search
│ ├── memory.py # conversation trimming
│ └── safety.py # PII redaction + guardrails
├── ingest.py # index your knowledge base
├── ui.py # Gradio chat frontend
└── requirements.txt
6.3 app/retriever.py — RAG grounding
import numpy as np
from sentence_transformers import SentenceTransformer
class Retriever:
"""Tiny in-memory vector store. Swap for Qdrant/pgvector in production."""
def __init__(self, model_name="BAAI/bge-base-en-v1.5"):
self.embedder = SentenceTransformer(model_name)
self.chunks: list[str] = []
self.embeddings: np.ndarray | None = None
def index(self, chunks: list[str]) -> None:
self.chunks = chunks
self.embeddings = self.embedder.encode(chunks, normalize_embeddings=True)
def search(self, query: str, k: int = 4) -> list[str]:
if self.embeddings is None or not self.chunks:
return []
q = self.embedder.encode([query], normalize_embeddings=True)[0]
scores = self.embeddings @ q # cosine sim (normalized)
top = np.argsort(scores)[::-1][:k]
return [self.chunks[i] for i in top]
6.4 app/memory.py — context-window management
from transformers import AutoTokenizer
_tok = AutoTokenizer.from_pretrained("Qwen/Qwen2.5-1.5B-Instruct")
def count_tokens(messages: list[dict]) -> int:
text = "\n".join(m["content"] for m in messages)
return len(_tok.encode(text))
def trim_history(history: list[dict], max_tokens: int = 3000) -> list[dict]:
# Drop oldest user+assistant pairs until under budget. Keep at least the last pair.
trimmed = list(history)
while count_tokens(trimmed) > max_tokens and len(trimmed) > 2:
trimmed = trimmed[2:]
return trimmed
6.5 app/safety.py — guardrails
import re
PII = {
"email": re.compile(r"\b[\w.+-]+@[\w-]+\.[\w.-]+\b"),
"phone": re.compile(r"\b(?:\+?\d{1,3}[-.\s]?)?\(?\d{3,5}\)?[-.\s]?\d{3,4}[-.\s]?\d{3,4}\b"),
}
BLOCKED = [re.compile(r"\b(make|build)\s+(a\s+)?(bomb|weapon)\b", re.I)]
def redact_pii(text: str) -> str:
for label, pattern in PII.items():
text = pattern.sub(f"[REDACTED_{label.upper()}]", text)
return text
def is_blocked(text: str) -> bool:
return any(p.search(text) for p in BLOCKED)
6.6 app/llm.py — pluggable backend (streaming)
import os
from collections.abc import AsyncIterator
BACKEND = os.getenv("LLM_BACKEND", "openai")
if BACKEND == "openai":
from openai import AsyncOpenAI
client = AsyncOpenAI()
async def stream_chat(messages: list[dict]) -> AsyncIterator[str]:
stream = await client.chat.completions.create(
model=os.getenv("OPENAI_MODEL", "gpt-4o-mini"),
messages=messages, temperature=0.7, stream=True,
)
async for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
yield delta
else: # local open-weight model
import torch
from threading import Thread
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
mid = os.getenv("LOCAL_MODEL", "Qwen/Qwen2.5-1.5B-Instruct")
_tok = AutoTokenizer.from_pretrained(mid)
_model = AutoModelForCausalLM.from_pretrained(mid, torch_dtype="auto", device_map="auto")
async def stream_chat(messages: list[dict]) -> AsyncIterator[str]:
inputs = _tok.apply_chat_template(
messages, return_tensors="pt", add_generation_prompt=True
).to(_model.device)
streamer = TextIteratorStreamer(_tok, skip_prompt=True, skip_special_tokens=True)
Thread(target=_model.generate, kwargs=dict(
input_ids=inputs, streamer=streamer, max_new_tokens=512,
do_sample=True, temperature=0.7, top_p=0.9,
)).start()
for token in streamer:
yield token
6.7 app/main.py — the chat service
import json
from fastapi import FastAPI, HTTPException
from fastapi.responses import StreamingResponse
from pydantic import BaseModel, Field
from app.llm import stream_chat
from app.retriever import Retriever
from app.memory import trim_history
from app.safety import redact_pii, is_blocked
app = FastAPI(title="Conversational AI")
retriever = Retriever()
SYSTEM_PROMPT = """You are a helpful, concise assistant for PrepStack.
Use the context below to answer when relevant. If the answer isn''t in the
context and you''re unsure, say so rather than inventing facts.
Context:
{context}"""
class Turn(BaseModel):
role: str
content: str
class ChatRequest(BaseModel):
message: str = Field(min_length=1, max_length=4000)
history: list[Turn] = Field(default_factory=list, max_length=40)
@app.on_event("startup")
def _load_index():
retriever.index([
"PrepStack offers blog posts and interview questions on .NET, React, AI/ML, and system design.",
"Refunds are processed within 7 business days of an approved request.",
"The free tier includes unlimited reading; premium adds downloadable question banks.",
])
@app.post("/chat")
async def chat(req: ChatRequest):
if is_blocked(req.message):
raise HTTPException(status_code=400, detail="Message violates usage policy")
safe_message = redact_pii(req.message)
# 1. Retrieve grounding
chunks = retriever.search(safe_message, k=4)
context = "\n\n".join(chunks) if chunks else "(no relevant context found)"
# 2. Build prompt: system + trimmed history + new message
history = [{"role": t.role, "content": t.content} for t in req.history]
history = trim_history(history, max_tokens=3000)
messages = [
{"role": "system", "content": SYSTEM_PROMPT.format(context=context)},
*history,
{"role": "user", "content": safe_message},
]
# 3. Stream the reply
async def event_stream():
try:
async for token in stream_chat(messages):
yield f"data: {json.dumps({'token': token})}\n\n"
yield f"data: {json.dumps({'done': True})}\n\n"
except Exception as e:
yield f"data: {json.dumps({'error': 'generation failed'})}\n\n"
print(f"[chat] error: {e}")
return StreamingResponse(event_stream(), media_type="text/event-stream")
@app.get("/health")
def health():
return {"ok": True}
6.8 ui.py — Gradio chat with streaming + memory
import gradio as gr
import httpx
import json
API = "http://localhost:8000/chat"
def respond(message, chat_history):
# chat_history is Gradio''s list of {role, content} dicts (messages format)
history_payload = [{"role": m["role"], "content": m["content"]} for m in chat_history]
chat_history = chat_history + [
{"role": "user", "content": message},
{"role": "assistant", "content": ""},
]
with httpx.stream("POST", API, json={
"message": message, "history": history_payload,
}, timeout=120) as r:
if r.status_code != 200:
chat_history[-1]["content"] = f"Error: {r.read().decode()}"
yield chat_history, ""
return
for line in r.iter_lines():
if not line or not line.startswith("data: "):
continue
data = json.loads(line[len("data: "):])
if data.get("token"):
chat_history[-1]["content"] += data["token"]
yield chat_history, ""
with gr.Blocks(title="Conversational AI") as demo:
gr.Markdown("# Conversational AI\nMulti-turn memory · RAG grounding · streaming")
chatbot = gr.Chatbot(type="messages", height=480)
msg = gr.Textbox(placeholder="Ask me about PrepStack...", show_label=False)
clear = gr.Button("Clear")
msg.submit(respond, [msg, chatbot], [chatbot, msg])
clear.click(lambda: ([], ""), outputs=[chatbot, msg])
if __name__ == "__main__":
demo.launch()
6.9 Run it
# requirements.txt:
# fastapi uvicorn pydantic httpx gradio sentence-transformers
# openai # if LLM_BACKEND=openai
# transformers torch # if LLM_BACKEND=local
pip install -r requirements.txt
export LLM_BACKEND=local # free, runs on your laptop
# export LLM_BACKEND=openai && export OPENAI_API_KEY=sk-...
uvicorn app.main:app --reload --port 8000 # terminal 1
python ui.py # terminal 2 → http://localhost:7860
Chat with it. It remembers the conversation, grounds answers in the indexed corpus, streams replies token by token, and redacts PII. That''s a real conversational-AI system — the same shape as production assistants, just smaller.
6.10 Production hardening
- Real vector store (Qdrant / pgvector) instead of in-memory, with proper document ingestion + chunking.
- Persistent conversation storage (Redis/Postgres) keyed by session, not client-passed history.
- Auth + per-user rate limiting.
- Tool use / function calling — let the bot query a database, check order status, etc.
- Observability — Langfuse for prompt/response/cost tracing; log which chunks were retrieved.
- Evals — a golden set of (question, expected) pairs run on every prompt/model change.
- Streaming via WebSocket for lower latency than SSE if you need it.
- A smaller/cheaper model once you confirm quality; cache common answers.
7. The arc in one picture
Bag of words → no order, no context
│
Embeddings → words become meaningful vectors
│
RNN / LSTM → process order, short memory
│
Seq2Seq → sequence → sequence (but a bottleneck)
│
+ Attention → decoder looks back at all inputs (bottleneck solved)
│
Transformer → attention everywhere, no recurrence, parallel, long-range
│
Pretrained LLM → general language ability from scale
│
Conversational AI → LLM + memory + RAG + tools + guardrails
Every step solved a concrete limitation of the one before. Understanding the arc is what lets you reason about why modern systems are built the way they are.
8. The honest stuff
- Don''t train a chatbot from scratch. Start from a pretrained instruct model. The era of training seq2seq dialogue models is over.
- The system matters more than the model. Memory management, retrieval quality, and prompt construction determine answer quality more than swapping models.
- RAG is how you stop hallucination. Ground answers in retrieved documents and instruct the model to stick to them.
- Context window is a budget. Long conversations overflow it — manage memory deliberately (trim/summarize/retrieve).
- Streaming is non-negotiable UX. Users tolerate a slow full response far less than a fast streaming one.
- Pick the smallest model that holds a conversation well. A 7B instruct model handles most assistant tasks.
- Evals over vibes. Build a golden Q&A set before you tune prompts.
9. The mental checklist
Before shipping a conversational AI feature:
- Using a pretrained instruct model (not training from scratch).
- Conversation memory implemented + trimmed to the context budget.
- RAG grounding wired up (retrieved chunks in the prompt).
- Chat template applied correctly (
apply_chat_template). - PII redacted before the prompt leaves your network.
- Safety guardrails + rate limiting on the endpoint.
- Responses stream to the user.
- Retrieved chunks logged (so you can debug wrong answers).
- A golden eval set runs on prompt/model changes.
10. Closing — the right mental model
Deep learning for text is the story of solving sequence and long-range dependency. Embeddings gave words meaning; RNNs gave order; attention gave reach; transformers gave scale; pretraining gave general ability. A conversational AI system stitches a pretrained transformer together with memory, retrieval, and guardrails into something that holds a useful conversation.
Three habits that make you good at this fast:
- Build the system, not the model. The pretrained LLM is one component; memory, RAG, and guardrails are where you add value.
- Ground everything. RAG over your own docs is the difference between a helpful assistant and a confident liar.
- Stream, remember, and measure. Streaming for UX, memory for coherence, evals for quality.
Internalize those, ship the conversational system above, and deep learning for text stops being a research topic — it becomes an engineering recipe you can build on.
Further reading
- Attention Is All You Need — the transformer paper.
- Sequence to Sequence Learning with Neural Networks — Sutskever et al., the original seq2seq.
- Neural Machine Translation by Jointly Learning to Align and Translate — Bahdanau attention, the breakthrough.
- The Illustrated Transformer — Jay Alammar''s visual guide.
- Understanding LSTMs — Chris Olah, the classic explainer.
- LangChain and LlamaIndex — RAG + conversation frameworks.
Building a chatbot that loses track of the conversation or makes things up? Email randhir.jassal@gmail.com with the behavior you''re seeing and I''ll tell you whether it''s a memory, retrieval, or prompting problem.
Get the next issue
A short, curated email with the newest posts and questions.