A Streamlit chatbot that answers health questions by retrieving evidence from real medical abstracts instead of relying only on the LLM's memory. It is a from-scratch implementation of retrieval-augmented generation, built to learn how the moving parts fit together: chunking, embeddings, vector search, and grounded answer generation.
- Load the corpus. On startup (
main.py), the app pulls the first 1,000 training rows ofarmanc/pubmed-rct20k(20k PubMed randomized controlled trial abstracts) from the Hugging Face datasets hub and concatenates each abstract into one text. - Chunk. Each abstract is split with LangChain's
RecursiveCharacterTextSplitterinto 800-character chunks with 150 characters of overlap, so findings are not cut off mid-sentence. - Embed and index. Chunks are embedded with
sentence-transformers/all-MiniLM-L6-v2viaHuggingFaceEmbeddingsand stored in an in-memory FAISS vector store. - Health gate. Before retrieval, a lightweight classifier chain asks the LLM a yes/no question: is this a health question? If not, the bot politely declines instead of burning a retrieval cycle.
- Retrieve with history awareness. Health questions go through a history-aware retriever, which first rewrites the question into a standalone query using the chat history, then pulls the most relevant chunks.
- Generate grounded answers. A "stuff" document chain feeds the retrieved chunks to
mistralai/Mixtral-8x7B-Instruct-v0.1(through the Hugging Face Inference API, temperature 0.2, up to 512 new tokens) with a system prompt that instructs it to answer only from the provided context.
There is also an Evaluation Dashboard page (pages/eval_dashboard.py) that reads the session's chat history and reports KPIs: total turns, average AI sentiment polarity, average Flesch reading ease of the AI answers, and average response length, plus a sentiment trend line chart and a top-15 keywords bar chart.
git clone https://github.com/suhasaitham22/RAG-based-Chatbot.git
cd RAG-based-Chatbot
pip install -r requirements.txtYou need a Hugging Face API token with inference access. Add it to .streamlit/secrets.toml:
HUGGINGFACEHUB_API_TOKEN = "hf_your_token_here"Then start the app:
streamlit run main.pyAfter chatting for a while, open the Evaluation Dashboard from the sidebar to see the analytics for your session.
- Streamlit (chat UI, multipage app, session state)
- LangChain (history-aware retriever, document chains, chat history)
- Hugging Face Inference API: Mixtral-8x7B-Instruct-v0.1 for generation, all-MiniLM-L6-v2 for embeddings
- FAISS (in-memory vector search)
- Hugging Face Datasets: armanc/pubmed-rct20k
- TextBlob, textstat, scikit-learn, Plotly, pandas (evaluation dashboard)
- The vector index is rebuilt in memory on every app start, so the first load takes a little while.
- Answers are informational only and generated from retrieved abstracts. This is a learning project, not medical advice.