A deterministic, embedding-based questionโanswer retrieval system for core Computer Engineering topics, built using sentence embeddings and cosine similarity.
Author: MA Dehghan
Project Type: Linear Algebra Assignment
Last Updated: January 01, 2026
This project implements a semantic questionโanswer retrieval system using vector embeddings and cosine similarity. Rather than generating answers, the chatbot retrieves the most semantically similar predefined question from a curated question bank.
The goal of the project is to demonstrate practical applications of linear algebra concepts โ vector spaces, normalization, dot products, and similarity metrics โ in natural language processing.

Example Interaction:
If no strong match (similarity โค 0.3), it responds:
"I don't know!"
| Feature | Description | Benefits & Implementation Notes |
|---|---|---|
| ๐ง Sentence Embeddings | Transforms questions into 384-dimensional vectors using all-MiniLM-L6-v2. | Handles synonyms/paraphrases; mean pooling of transformer outputs ensures fixed-length vectors. |
| ๐ Semantic Similarity Search | Uses cosine similarity to find the best match from the question bank. | Threshold (0.3) ensures weak matches are ignored. |
| ๐ Word-by-Word Streaming | Responses are streamed word by word via Python generator and Streamlit. | Simulates real-time conversation without AI generation. |
| ๐ ๏ธ Extensible Question Bank | JSON-based (questionBank.json) with precomputed embeddings. | Easily add/edit entries; recompute embeddings for new questions. |
| โก Lightweight & Efficient | ~90MB model; CPU-friendly via PyTorch. | Fast responses for small-medium question banks. |
| ๐ Deterministic & Reproducible | Outputs are fully deterministic. | Same query always yields same response; ideal for assignments. |
| ๐ Chat History Persistence | Uses Streamlit session state. | Maintains conversation display, though matching is per query. |
all-MiniLM-L6-v2 ๐ค (Model Hub)questionBank.json. Cache tokenizer/model.Install dependencies:
pip install streamlit torch transformers numpy
all-MiniLM-L6-v2 model.Cosine similarity is used to measure semantic similarity between the user query and stored questions:
$\cos(\theta) = \frac{u \cdot q}{\lVert u \rVert \lVert q \rVert}$
Where:
q = query embeddingu = stored question embeddingBecause all vectors are L2-normalized, cosine similarity reduces to a dot product between vectors, enabling efficient similarity computation.
Prerequisites
Python 3.8+
Git
Install Dependencies pip install streamlit torch numpy transformers sentence-transformers
git clone https://github.com/Mhalexmd/ChatBot
cd ChatBot
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
streamlit run app.py
Access at http://localhost:8501.
questionBank.json format:
{
"questions": [
{
"question": "What is a CPU?",
"answer": "The central processing unit, executing instructions and managing data flow.",
"embedding": [0.12, -0.34, ...]
}
]
}
Generate embeddings with Python:
from sentence_transformers import SentenceTransformer
import json
model = SentenceTransformer('all-MiniLM-L6-v2')
questions = [{"question": "Your question", "answer": "Your answer"}]
for q in questions:
q['embedding'] = model.encode(q['question']).tolist()
with open('questionBank.json', 'w') as f:
json.dump({"questions": questions}, f)
Only answers in question bank
No multi-turn context
Slow for >1k questions without indexing
English-only for now
Fork โ Branch โ Pull Request. Focus: New CE questions, optimizations, bug fixes.
Hugging Face, Streamlit, PyTorch
Linear algebra applications in NLP
Assignment inspiration
31 commits
Python
100.0%
A deterministic, embedding-based questionโanswer retrieval system for core Computer Engineering topics, built using sentence embeddings and cosine similarity.
Author: MA Dehghan
Project Type: Linear Algebra Assignment
Last Updated: January 01, 2026
This project implements a semantic questionโanswer retrieval system using vector embeddings and cosine similarity. Rather than generating answers, the chatbot retrieves the most semantically similar predefined question from a curated question bank.
The goal of the project is to demonstrate practical applications of linear algebra concepts โ vector spaces, normalization, dot products, and similarity metrics โ in natural language processing.

Example Interaction:
If no strong match (similarity โค 0.3), it responds:
"I don't know!"
| Feature | Description | Benefits & Implementation Notes |
|---|---|---|
| ๐ง Sentence Embeddings | Transforms questions into 384-dimensional vectors using all-MiniLM-L6-v2. | Handles synonyms/paraphrases; mean pooling of transformer outputs ensures fixed-length vectors. |
| ๐ Semantic Similarity Search | Uses cosine similarity to find the best match from the question bank. | Threshold (0.3) ensures weak matches are ignored. |
| ๐ Word-by-Word Streaming | Responses are streamed word by word via Python generator and Streamlit. | Simulates real-time conversation without AI generation. |
| ๐ ๏ธ Extensible Question Bank | JSON-based (questionBank.json) with precomputed embeddings. | Easily add/edit entries; recompute embeddings for new questions. |
| โก Lightweight & Efficient | ~90MB model; CPU-friendly via PyTorch. | Fast responses for small-medium question banks. |
| ๐ Deterministic & Reproducible | Outputs are fully deterministic. | Same query always yields same response; ideal for assignments. |
| ๐ Chat History Persistence | Uses Streamlit session state. | Maintains conversation display, though matching is per query. |
all-MiniLM-L6-v2 ๐ค (Model Hub)questionBank.json. Cache tokenizer/model.Install dependencies:
pip install streamlit torch transformers numpy
all-MiniLM-L6-v2 model.Cosine similarity is used to measure semantic similarity between the user query and stored questions:
$\cos(\theta) = \frac{u \cdot q}{\lVert u \rVert \lVert q \rVert}$
Where:
q = query embeddingu = stored question embeddingBecause all vectors are L2-normalized, cosine similarity reduces to a dot product between vectors, enabling efficient similarity computation.
Prerequisites
Python 3.8+
Git
Install Dependencies pip install streamlit torch numpy transformers sentence-transformers
git clone https://github.com/Mhalexmd/ChatBot
cd ChatBot
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate
pip install -r requirements.txt
streamlit run app.py
Access at http://localhost:8501.
questionBank.json format:
{
"questions": [
{
"question": "What is a CPU?",
"answer": "The central processing unit, executing instructions and managing data flow.",
"embedding": [0.12, -0.34, ...]
}
]
}
Generate embeddings with Python:
from sentence_transformers import SentenceTransformer
import json
model = SentenceTransformer('all-MiniLM-L6-v2')
questions = [{"question": "Your question", "answer": "Your answer"}]
for q in questions:
q['embedding'] = model.encode(q['question']).tolist()
with open('questionBank.json', 'w') as f:
json.dump({"questions": questions}, f)
Only answers in question bank
No multi-turn context
Slow for >1k questions without indexing
English-only for now
Fork โ Branch โ Pull Request. Focus: New CE questions, optimizations, bug fixes.
Hugging Face, Streamlit, PyTorch
Linear algebra applications in NLP
Assignment inspiration
31 commits
Python
100.0%