Skip to content

About

Chat with your own documents using retrieval-augmented generation (embeddings + LLM).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

23 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 

Repository files navigation

RAG-based Chatbot (Factful Health Chatbot)

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.

How the RAG flow works in this codebase

  1. Load the corpus. On startup (main.py), the app pulls the first 1,000 training rows of armanc/pubmed-rct20k (20k PubMed randomized controlled trial abstracts) from the Hugging Face datasets hub and concatenates each abstract into one text.
  2. Chunk. Each abstract is split with LangChain's RecursiveCharacterTextSplitter into 800-character chunks with 150 characters of overlap, so findings are not cut off mid-sentence.
  3. Embed and index. Chunks are embedded with sentence-transformers/all-MiniLM-L6-v2 via HuggingFaceEmbeddings and stored in an in-memory FAISS vector store.
  4. 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.
  5. 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.
  6. 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.

Install and run

git clone https://github.com/suhasaitham22/RAG-based-Chatbot.git
cd RAG-based-Chatbot
pip install -r requirements.txt

You 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.py

After chatting for a while, open the Evaluation Dashboard from the sidebar to see the analytics for your session.

Tech used

  • 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)

Notes

  • 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.

About

Chat with your own documents using retrieval-augmented generation (embeddings + LLM).

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages