Building an Arabic RAG chatbot that actually answers from your data
A practical pipeline for an Arabic + English RAG assistant: normalization, chunking, multilingual embeddings, FAISS retrieval and grounded answers with sources.
By Ahmed Salah Fouda · · 7 min read
TL;DR
An Arabic RAG chatbot needs three things a generic tutorial skips: Arabic text normalization before embedding, a multilingual embedding model, and a prompt that forces the model to answer only from retrieved passages and cite them. This post walks through each step with LangChain and FAISS.
Why Arabic RAG breaks with default settings
Arabic text appears with and without diacritics (tashkeel), with several forms of alef, and with taa marbuta written as haa. The same word can therefore produce different embeddings, and retrieval quietly misses relevant passages. Customers also mix Egyptian dialect, Modern Standard Arabic and English in one message.
Step 1 — Normalize before you embed
Apply the same normalization to documents at indexing time and to the user’s question at query time. Keep the original text for display; only the embedded text is normalized.
import re
TASHKEEL = re.compile(r"[\u0617-\u061A\u064B-\u0652\u0670]")
def normalize_ar(text: str) -> str:
text = TASHKEEL.sub("", text) # remove diacritics
text = re.sub("[إأآا]", "ا", text) # unify alef forms
text = text.replace("ى", "ي").replace("ة", "ه")
text = text.replace("ـ", "") # remove tatweel
return re.sub(r"\s+", " ", text).strip()Step 2 — Chunk by meaning, not by characters only
Split on headings and paragraphs first, then fall back to a size limit. Small overlapping chunks keep policy answers intact.
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.documents import Document
splitter = RecursiveCharacterTextSplitter(
chunk_size=800, chunk_overlap=120,
separators=["\n## ", "\n\n", "\n", "۔", ".", " "],
)
def to_chunks(raw_docs: list[Document]) -> list[Document]:
chunks = splitter.split_documents(raw_docs)
for c in chunks:
c.metadata["original"] = c.page_content
c.page_content = normalize_ar(c.page_content)
return chunksStep 3 — Use a multilingual embedding model
Pick an embedding model trained on Arabic and English so a question in one language can retrieve a passage in the other. Store vectors in FAISS for a small knowledge base, or in a managed vector database once you need filtering and scale.
from langchain_huggingface import HuggingFaceEmbeddings
from langchain_community.vectorstores import FAISS
emb = HuggingFaceEmbeddings(model_name="intfloat/multilingual-e5-base")
store = FAISS.from_documents(to_chunks(raw_docs), emb)
retriever = store.as_retriever(search_kwargs={"k": 5})Step 4 — Answer only from the retrieved passages
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
SYSTEM = """You answer customer questions using ONLY the context.
Reply in the customer's language (Egyptian Arabic, MSA or English).
If the context does not contain the answer, say you don't know and
offer to connect a human. Cite sources as [1], [2]."""
def answer(question: str) -> str:
docs = retriever.invoke(normalize_ar(question))
context = "\n\n".join(
f"[{i+1}] {d.metadata['original']}" for i, d in enumerate(docs)
)
msgs = [("system", SYSTEM), ("human", f"Context:\n{context}\n\nQuestion: {question}")]
return llm.invoke(msgs).contentWhat to check before launch
- Write 30–50 real customer questions (in dialect, too) with the passage that should answer each one.
- Measure how often the right passage is in the top 5 results before tuning prompts.
- Log unanswered questions — they show exactly which documents are missing.
Need this built for your business?
Start a Project