Spaces:
Configuration error
Configuration error
Commit ·
db10232
0
Parent(s):
initial commit: multimodal RAG engine with guardrails and tests
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .cursorignore +11 -0
- .gitattributes +12 -0
- .github/workflows/ci.yml +33 -0
- .gitignore +19 -0
- .numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.projection_by_bboxes-16.py314.1.nbc +0 -0
- .numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.projection_by_bboxes-16.py314.nbi +0 -0
- .numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.split_projection_profile-47.py314.1.nbc +0 -0
- .numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.split_projection_profile-47.py314.nbi +0 -0
- .paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams +3 -0
- .paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams.info +3 -0
- .paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdmodel +3 -0
- .paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdiparams +3 -0
- .paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdiparams.info +3 -0
- .paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdmodel +3 -0
- .paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdiparams +3 -0
- .paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdiparams.info +3 -0
- .paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdmodel +3 -0
- .tmp_visual_test/page_208_Figure_4.2.png +3 -0
- Dockerfile +24 -0
- Dockerfile.streamlit +13 -0
- README.md +264 -0
- __pycache__/app.cpython-310.pyc +3 -0
- __pycache__/app.cpython-314.pyc +3 -0
- __pycache__/chunk_and_count.cpython-310.pyc +3 -0
- __pycache__/compliance_safety.cpython-310.pyc +3 -0
- __pycache__/compliance_safety.cpython-314.pyc +3 -0
- __pycache__/deploy_all_2126.cpython-310.pyc +3 -0
- __pycache__/deploy_csv_only.cpython-310.pyc +3 -0
- __pycache__/deploy_text_only.cpython-310.pyc +3 -0
- __pycache__/deploy_to_qdrant.cpython-310.pyc +3 -0
- __pycache__/deploy_to_qdrant.cpython-314.pyc +3 -0
- __pycache__/deploy_visual_only.cpython-310.pyc +3 -0
- __pycache__/deploy_visual_only.cpython-314.pyc +3 -0
- __pycache__/evaluate.cpython-310.pyc +3 -0
- __pycache__/gateway_guardrails.cpython-310.pyc +3 -0
- __pycache__/gateway_guardrails.cpython-314.pyc +3 -0
- __pycache__/ingest_data.cpython-310.pyc +3 -0
- __pycache__/ingest_data.cpython-314.pyc +3 -0
- __pycache__/isolate_visual.cpython-314.pyc +3 -0
- __pycache__/migrate_qdrant_cross_references.cpython-314.pyc +3 -0
- __pycache__/query_rag.cpython-310.pyc +3 -0
- __pycache__/query_rag.cpython-314.pyc +3 -0
- __pycache__/rag_invariants.cpython-310.pyc +3 -0
- __pycache__/rag_invariants.cpython-314.pyc +3 -0
- __pycache__/reindex_sparse.cpython-314.pyc +3 -0
- __pycache__/self_rag_utils.cpython-310.pyc +3 -0
- __pycache__/self_rag_utils.cpython-314.pyc +3 -0
- __pycache__/structural_vetting.cpython-310.pyc +3 -0
- __pycache__/structural_vetting.cpython-314.pyc +3 -0
- __pycache__/test_bge_m3_embedding_components.cpython-310.pyc +3 -0
.cursorignore
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
node_modules/
|
| 2 |
+
.git/
|
| 3 |
+
dist/
|
| 4 |
+
build/
|
| 5 |
+
venv/
|
| 6 |
+
.venv/
|
| 7 |
+
__pycache__/
|
| 8 |
+
*.log
|
| 9 |
+
*.csv
|
| 10 |
+
*.xlsx
|
| 11 |
+
*.pdf
|
.gitattributes
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/hf_cache_v2/models--BAAI--bge-reranker-v2-m3/snapshots/953dc6f6f85a1b2dbfca4c34a2796e7dde08d41e/tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.pdiparams filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.pdmodel filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.db filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.pyc filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.pdf filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.json filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.info filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
.github/workflows/ci.yml
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: Python CI Workflow
|
| 2 |
+
|
| 3 |
+
on:
|
| 4 |
+
push:
|
| 5 |
+
branches:
|
| 6 |
+
- main
|
| 7 |
+
pull_request:
|
| 8 |
+
branches:
|
| 9 |
+
- main
|
| 10 |
+
|
| 11 |
+
jobs:
|
| 12 |
+
test:
|
| 13 |
+
runs-on: ubuntu-latest
|
| 14 |
+
|
| 15 |
+
steps:
|
| 16 |
+
- name: Checkout Code
|
| 17 |
+
uses: actions/checkout@v4
|
| 18 |
+
|
| 19 |
+
- name: Set up Python 3.11
|
| 20 |
+
uses: actions/setup-python@v5
|
| 21 |
+
with:
|
| 22 |
+
python-version: "3.11"
|
| 23 |
+
cache: "pip"
|
| 24 |
+
|
| 25 |
+
- name: Install Dependencies
|
| 26 |
+
run: |
|
| 27 |
+
python -m pip install --upgrade pip
|
| 28 |
+
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
|
| 29 |
+
pip install pytest
|
| 30 |
+
|
| 31 |
+
- name: Run Pytest Evaluation Suite
|
| 32 |
+
run: |
|
| 33 |
+
pytest tests/test_guardrail_eval.py -v
|
.gitignore
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
venv/
|
| 2 |
+
.venv/
|
| 3 |
+
hf_models_v2/
|
| 4 |
+
.hf_home/
|
| 5 |
+
.hf_cache/
|
| 6 |
+
.hf_cache_v2/
|
| 7 |
+
docling_models/
|
| 8 |
+
assets/
|
| 9 |
+
data_cache/
|
| 10 |
+
*.qdrant/
|
| 11 |
+
data/
|
| 12 |
+
*.db
|
| 13 |
+
*.bin
|
| 14 |
+
*.onnx
|
| 15 |
+
*.safetensors
|
| 16 |
+
*.sqlite
|
| 17 |
+
qdrant_db/
|
| 18 |
+
.env
|
| 19 |
+
.env*
|
.numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.projection_by_bboxes-16.py314.1.nbc
ADDED
|
Binary file (54.1 kB). View file
|
|
|
.numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.projection_by_bboxes-16.py314.nbi
ADDED
|
Binary file (1.59 kB). View file
|
|
|
.numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.split_projection_profile-47.py314.1.nbc
ADDED
|
Binary file (80.5 kB). View file
|
|
|
.numba_cache/utils_580f43d1b2da4a27ad6655c5fc4fe49fdc7a7ba4/xycut.split_projection_profile-47.py314.nbi
ADDED
|
Binary file (1.6 kB). View file
|
|
|
.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d1efda1b80e174b4fcb168a035ac96c1af4938892bd86a55f300a6027105d08c
|
| 3 |
+
size 539978
|
.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdiparams.info
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ee0c2eaa4c09814802bf520c01dfdbc1345dc2879dc9e67424d32c7b0ee88e59
|
| 3 |
+
size 18545
|
.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/inference.pdmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3c4337ec61722a20b1dca2e5bfaffc313c0592bc89ad6e0d45168224186f6683
|
| 3 |
+
size 1624487
|
.paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdiparams
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:83676ec730627ab4502f401410a4b6a3ce1c0bb98fa249b71db055b6bddae051
|
| 3 |
+
size 2377917
|
.paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdiparams.info
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2fe414d9eadf914bf44e3f9ba212988a6f26f364e4f87c6d0af57438ffffc0c4
|
| 3 |
+
size 26392
|
.paddleocr/whl/det/en/en_PP-OCRv3_det_infer/inference.pdmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c4bfb1b05d9d1d5a760801eaf6d20180ef7e47bcc675fb17d1f3a89da5fef427
|
| 3 |
+
size 1590133
|
.paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdiparams
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:75f64a1ffb70c56b7a25655963ca16f5bf3286202e3f52ac972bee05cdee2f56
|
| 3 |
+
size 7607269
|
.paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdiparams.info
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:710d9f3d7c503067ae708f4bc3adc6973c0d6391adb7a1470c36eb70da6b5b83
|
| 3 |
+
size 102540
|
.paddleocr/whl/rec/en/en_PP-OCRv4_rec_infer/inference.pdmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:85b952f05f709af259cfe4254012aa7208bef0998f71f57a15495446f25ccd43
|
| 3 |
+
size 2517366
|
.tmp_visual_test/page_208_Figure_4.2.png
ADDED
|
Git LFS Details
|
Dockerfile
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
ENV HF_HOME=/app/hf_cache
|
| 6 |
+
ENV TRANSFORMERS_CACHE=/app/hf_cache
|
| 7 |
+
|
| 8 |
+
RUN apt-get update && apt-get install -y build-essential
|
| 9 |
+
|
| 10 |
+
COPY requirements.txt .
|
| 11 |
+
|
| 12 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 13 |
+
|
| 14 |
+
# Pre-download embedding model
|
| 15 |
+
RUN python -c "from sentence_transformers import SentenceTransformer; SentenceTransformer('sentence-transformers/all-MiniLM-L6-v2')"
|
| 16 |
+
|
| 17 |
+
# Pre-download reranker
|
| 18 |
+
RUN python -c "from sentence_transformers import CrossEncoder; CrossEncoder('cross-encoder/ms-marco-MiniLM-L-6-v2')"
|
| 19 |
+
|
| 20 |
+
COPY . .
|
| 21 |
+
|
| 22 |
+
EXPOSE 8080
|
| 23 |
+
|
| 24 |
+
CMD uvicorn app.main:app --host 0.0.0.0 --port $PORT
|
Dockerfile.streamlit
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.10-slim
|
| 2 |
+
|
| 3 |
+
WORKDIR /app
|
| 4 |
+
|
| 5 |
+
COPY requirements-streamlit.txt .
|
| 6 |
+
|
| 7 |
+
RUN pip install --no-cache-dir -r requirements-streamlit.txt
|
| 8 |
+
|
| 9 |
+
COPY streamlit_ui ./streamlit_ui
|
| 10 |
+
|
| 11 |
+
EXPOSE 8080
|
| 12 |
+
|
| 13 |
+
CMD ["streamlit","run","streamlit_ui/Streamlitapp.py","--server.port=8080","--server.address=0.0.0.0"]
|
README.md
ADDED
|
@@ -0,0 +1,264 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Financial Regulatory RAG System
|
| 2 |
+
|
| 3 |
+
A production-style Retrieval-Augmented Generation (RAG) system for financial regulatory Q&A that reduces hallucination using hybrid retrieval, cross-encoder reranking, and source-grounded responses. Achieved ~0.875 Recall@5 using hybrid retrieval + cross-encoder reranking on RBI regulatory data
|
| 4 |
+
|
| 5 |
+
## Why it matters
|
| 6 |
+
- Financial compliance requires accurate, verifiable answers — hallucinated responses can lead to regulatory and financial risk
|
| 7 |
+
- This system ensures responses are grounded in official RBI regulatory documents
|
| 8 |
+
|
| 9 |
+
## What makes it better
|
| 10 |
+
- Hybrid Retrieval (BM25 + FAISS) for keyword + semantic search
|
| 11 |
+
- Cross-Encoder Reranking improving Recall@5 from ~0.75 → ~0.875
|
| 12 |
+
- Source-grounded responses to reduce hallucination
|
| 13 |
+
- Quantitative evaluation using Recall@k and RAGAS metrics
|
| 14 |
+
- Production-ready deployment using FastAPI + Cloud Run + Streamlit UI
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
# Live Demo
|
| 18 |
+
|
| 19 |
+
🔗 **Streamlit App:**
|
| 20 |
+
https://financial-rag-ui-912628415543.us-central1.run.app/
|
| 21 |
+
|
| 22 |
+
🔗 **API Docs:**
|
| 23 |
+
https://financial-rag-api-912628415543.us-central1.run.app/docs
|
| 24 |
+
|
| 25 |
+
---
|
| 26 |
+
# System Architecture
|
| 27 |
+
|
| 28 |
+
```
|
| 29 |
+
User Query
|
| 30 |
+
↓
|
| 31 |
+
Streamlit UI
|
| 32 |
+
↓
|
| 33 |
+
FastAPI API (/query endpoint)
|
| 34 |
+
↓
|
| 35 |
+
Hybrid Retrieval
|
| 36 |
+
• BM25 Retriever
|
| 37 |
+
• FAISS Vector Search
|
| 38 |
+
↓
|
| 39 |
+
Cross-Encoder Reranking
|
| 40 |
+
(cross-encoder/ms-marco-MiniLM-L-6-v2)
|
| 41 |
+
↓
|
| 42 |
+
Context Construction
|
| 43 |
+
↓
|
| 44 |
+
LLM Answer Generation
|
| 45 |
+
↓
|
| 46 |
+
Final Response
|
| 47 |
+
```
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
# Key Features
|
| 52 |
+
|
| 53 |
+
## Hybrid Retrieval
|
| 54 |
+
Combines **BM25 lexical search** and **FAISS dense vector search** to improve retrieval accuracy.
|
| 55 |
+
|
| 56 |
+
## Cross-Encoder Reranking
|
| 57 |
+
Documents are reranked using: cross-encoder/ms-marco-MiniLM-L-6-v2
|
| 58 |
+
This improves answer quality by selecting the most relevant context.
|
| 59 |
+
|
| 60 |
+
## Source Attribution
|
| 61 |
+
The system returns the **document source and page number** used to generate the answer.
|
| 62 |
+
This ensures responses are grounded in the original financial regulatory documents and helps reduce hallucinations.
|
| 63 |
+
|
| 64 |
+
## FastAPI Backend
|
| 65 |
+
Provides a scalable REST API.
|
| 66 |
+
|
| 67 |
+
Endpoint:
|
| 68 |
+
|
| 69 |
+
```
|
| 70 |
+
POST /query
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
Returns:
|
| 74 |
+
|
| 75 |
+
- Generated answer
|
| 76 |
+
- Response latency
|
| 77 |
+
- Metadata
|
| 78 |
+
|
| 79 |
+
## Streamlit UI
|
| 80 |
+
Interactive interface to ask compliance-related questions.
|
| 81 |
+
|
| 82 |
+
## Cloud Deployment
|
| 83 |
+
The system is containerized using **Docker** and deployed on **Google Cloud Run**.
|
| 84 |
+
|
| 85 |
+
---
|
| 86 |
+
|
| 87 |
+
# Tech Stack
|
| 88 |
+
|
| 89 |
+
| Component | Technology |
|
| 90 |
+
|--------|--------|
|
| 91 |
+
Backend API | FastAPI |
|
| 92 |
+
Frontend | Streamlit |
|
| 93 |
+
Vector Database | FAISS |
|
| 94 |
+
Embeddings | sentence-transformers/all-MiniLM-L6-v2 |
|
| 95 |
+
Reranker | cross-encoder/ms-marco-MiniLM-L-6-v2 |
|
| 96 |
+
Retrieval | Hybrid (BM25 + FAISS) |
|
| 97 |
+
LLM | OpenRouter (Llama 3 / Open-source models)
|
| 98 |
+
Deployment | Docker + Google Cloud Run |
|
| 99 |
+
|
| 100 |
+
---
|
| 101 |
+
|
| 102 |
+
# Retrieval Evaluation
|
| 103 |
+
|
| 104 |
+
We evaluated retrieval performance using Recall@5 on a manually labeled dataset derived from RBI KYC guidelines.
|
| 105 |
+
|
| 106 |
+
| Method | Recall@5 |
|
| 107 |
+
|---------------------|---------|
|
| 108 |
+
| BM25 | 0.50 |
|
| 109 |
+
| FAISS | 0.875 |
|
| 110 |
+
| Hybrid | 0.75 |
|
| 111 |
+
| Hybrid + Reranking | 0.875 |
|
| 112 |
+
|
| 113 |
+
- Dense retrieval (FAISS) performed best for semantic regulatory data
|
| 114 |
+
- BM25 underperformed due to lack of strong keyword signals
|
| 115 |
+
- Hybrid improved baseline retrieval
|
| 116 |
+
- Cross-encoder reranking significantly improved result ordering
|
| 117 |
+
|
| 118 |
+
👉 Reranking improved Hybrid performance from ~0.75 → ~0.875
|
| 119 |
+
|
| 120 |
+
---
|
| 121 |
+
|
| 122 |
+
## Limitations
|
| 123 |
+
|
| 124 |
+
While the system performs well on structured regulatory queries, several limitations were observed:
|
| 125 |
+
1. Sensitivity to Query Quality
|
| 126 |
+
- The system struggles with vague or poorly phrased queries
|
| 127 |
+
- Retrieval performance depends heavily on how clearly the query matches document intent
|
| 128 |
+
|
| 129 |
+
2. Context Window Constraints
|
| 130 |
+
- Only top-k retrieved chunks are passed to the LLM
|
| 131 |
+
- Important information may be missed if not retrieved in top results
|
| 132 |
+
|
| 133 |
+
3. Hallucination Risk
|
| 134 |
+
- If retrieval fails or returns weak context, the LLM may generate partially incorrect answers
|
| 135 |
+
- This was observed in edge cases with ambiguous queries
|
| 136 |
+
|
| 137 |
+
4. Dataset Limitations
|
| 138 |
+
- Performance is tied to the quality and coverage of the RBI document
|
| 139 |
+
- Missing or incomplete sections can lead to incomplete answers
|
| 140 |
+
|
| 141 |
+
5. Retrieval Bias
|
| 142 |
+
- Dense retrieval (FAISS) dominates performance due to semantic nature of data
|
| 143 |
+
- BM25 contributes less in this domain, reducing hybrid effectiveness
|
| 144 |
+
|
| 145 |
+
6. Computational Overhead
|
| 146 |
+
- Cross-encoder reranking improves accuracy but increases latency
|
| 147 |
+
- Not optimal for real-time high-throughput systems without optimization
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
# Retrieval Pipeline
|
| 152 |
+
|
| 153 |
+
1. User submits a question
|
| 154 |
+
2. Hybrid retriever fetches candidate documents
|
| 155 |
+
3. BM25 search retrieves keyword matches
|
| 156 |
+
4. FAISS performs dense vector similarity search
|
| 157 |
+
5. Cross-Encoder reranks retrieved documents
|
| 158 |
+
6. Top documents are selected as context
|
| 159 |
+
7. LLM generates the final answer
|
| 160 |
+
8. API returns the response with latency metadata
|
| 161 |
+
9. Response includes source citation for transparency
|
| 162 |
+
|
| 163 |
+
---
|
| 164 |
+
|
| 165 |
+
# API Usage
|
| 166 |
+
|
| 167 |
+
## Query Endpoint
|
| 168 |
+
|
| 169 |
+
```
|
| 170 |
+
POST /query
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
### Example Request
|
| 174 |
+
|
| 175 |
+
```json
|
| 176 |
+
{
|
| 177 |
+
"question": "Under which Rule should suspicious transactions be reported to FIU-IND?"
|
| 178 |
+
}
|
| 179 |
+
```
|
| 180 |
+
|
| 181 |
+
### Example Response
|
| 182 |
+
|
| 183 |
+
```json
|
| 184 |
+
{
|
| 185 |
+
"question": "...",
|
| 186 |
+
"answer": "...",
|
| 187 |
+
"sources": ["Finance_RBI.pdf (Page 14)"],
|
| 188 |
+
"latency_seconds": 1.42
|
| 189 |
+
}
|
| 190 |
+
```
|
| 191 |
+
|
| 192 |
+
---
|
| 193 |
+
|
| 194 |
+
# Project Structure
|
| 195 |
+
|
| 196 |
+
```
|
| 197 |
+
rag-system-v2
|
| 198 |
+
│
|
| 199 |
+
├── app
|
| 200 |
+
│ ├── ingestion.py
|
| 201 |
+
│ ├── vector.py
|
| 202 |
+
│ ├── retriever.py
|
| 203 |
+
│ ├── llm.py
|
| 204 |
+
│ └── main.py
|
| 205 |
+
│
|
| 206 |
+
├── Data
|
| 207 |
+
│ └── Vector
|
| 208 |
+
│
|
| 209 |
+
├── streamlit_ui
|
| 210 |
+
│ └── StreamlitApp.py
|
| 211 |
+
│
|
| 212 |
+
├── evaluate.py
|
| 213 |
+
├── Dockerfile
|
| 214 |
+
├── Dockerfile.streamlit
|
| 215 |
+
├── requirements.txt
|
| 216 |
+
└── README.md
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
---
|
| 220 |
+
|
| 221 |
+
# Installation
|
| 222 |
+
|
| 223 |
+
### Clone Repository
|
| 224 |
+
git clone https://github.com/supriyap19967-creator/rag-systm-v2
|
| 225 |
+
cd rag-systm-v2
|
| 226 |
+
|
| 227 |
+
### Install Dependencies
|
| 228 |
+
pip install -r requirements.txt
|
| 229 |
+
|
| 230 |
+
### Run FastAPI Server
|
| 231 |
+
uvicorn app.main:app --reload
|
| 232 |
+
|
| 233 |
+
### Run Streamlit UI
|
| 234 |
+
streamlit run streamlit_ui/StreamlitApp.py
|
| 235 |
+
|
| 236 |
+
---
|
| 237 |
+
|
| 238 |
+
# Deployment
|
| 239 |
+
|
| 240 |
+
### Build Docker Image
|
| 241 |
+
docker build -t financial-rag-api .
|
| 242 |
+
|
| 243 |
+
### Deploy to Google Cloud Run
|
| 244 |
+
gcloud run deploy financial-rag-api
|
| 245 |
+
|
| 246 |
+
# Future Improvements
|
| 247 |
+
|
| 248 |
+
- Query rewriting for handling vague user inputs
|
| 249 |
+
- Lightweight reranking models to reduce latency
|
| 250 |
+
- Multi-document reasoning for complex queries
|
| 251 |
+
- Evaluation on larger and more diverse datasets
|
| 252 |
+
|
| 253 |
+
# Example Questions
|
| 254 |
+
|
| 255 |
+
- How must suspicious transactions be reported?
|
| 256 |
+
- What penalties apply for delayed reporting?
|
| 257 |
+
- Under which rule should suspicious transactions be reported to FIU-IND?
|
| 258 |
+
|
| 259 |
+
---
|
| 260 |
+
|
| 261 |
+
# Author
|
| 262 |
+
|
| 263 |
+
**Supriya**
|
| 264 |
+
AI / ML Engineer | Generative AI
|
__pycache__/app.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:943c7c4aa6cd54a522f66a03efc609286488fe8805be1ded7240e8a914489fbf
|
| 3 |
+
size 23802
|
__pycache__/app.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:03267d74083e07075bae266c4da415b5a235519d4b20fed607d6ac4b48444245
|
| 3 |
+
size 40221
|
__pycache__/chunk_and_count.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ef7fa92c612ca97d2d4cb69d9de9a975b84efda8d95e3f8586d0f43872e29ada
|
| 3 |
+
size 12045
|
__pycache__/compliance_safety.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6b20fccd40964a8f1bccae645f9cc9cceab1f1a536337291c3cb999b4f94b3c1
|
| 3 |
+
size 14741
|
__pycache__/compliance_safety.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6a1fe79339bf7415493f20004fcd51d831648ef8b5c139a0ea7b8d470a590889
|
| 3 |
+
size 16038
|
__pycache__/deploy_all_2126.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:57dcc0bec30f22c08a2de69f148ca800b8c4d2dbb02e71a828994eef8405823b
|
| 3 |
+
size 8561
|
__pycache__/deploy_csv_only.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:453a8912803d599b17428949a493cea94fc6532368932a315c293554fbba2a6f
|
| 3 |
+
size 4607
|
__pycache__/deploy_text_only.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd1103672a132cf3d44e5791d1e9e5539cdace3d5ef84ccab5ed94fd9617b539
|
| 3 |
+
size 4470
|
__pycache__/deploy_to_qdrant.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5c5957565f5d139d4b8fe23ae8476d8db12cbd7a34b014f979be68fd3049c855
|
| 3 |
+
size 11297
|
__pycache__/deploy_to_qdrant.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7d6d2d576a5aabb19c7e266be43ab02af9466536f8036f0634ea865ada54fc05
|
| 3 |
+
size 21111
|
__pycache__/deploy_visual_only.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d0906555d29a6d4829f3b68c78ed73069120374218228d8cb7c87438f7a07674
|
| 3 |
+
size 5155
|
__pycache__/deploy_visual_only.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:15ee8750c8062596ced27550fd7de4b6041747d16aca087ef4085ef5eb9f3c46
|
| 3 |
+
size 31704
|
__pycache__/evaluate.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4b0a829f3466d71db387729d674f0e393c7250104708545d685176e033a27a6e
|
| 3 |
+
size 2410
|
__pycache__/gateway_guardrails.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3a1466f344a2d36a96b68a19abe510109bb5223b11c419d855e329f0285162a3
|
| 3 |
+
size 17306
|
__pycache__/gateway_guardrails.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e16ddd174e33ef86c4f275ca351f103f45798e10ebdc0409a5675ffd7a957acf
|
| 3 |
+
size 27990
|
__pycache__/ingest_data.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f234d98f6a831f0b3bb76bb22241d2a61b9ea76ca765a1038718defa84d48523
|
| 3 |
+
size 30549
|
__pycache__/ingest_data.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3e36190843057a8faa9f9560b66180b98edf0c064d08c08c686265eb42b8abca
|
| 3 |
+
size 50970
|
__pycache__/isolate_visual.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3f83f49ac7ae75d24110381a8f9c711d94998cc9e2e413957c41bbd39e5ab6b0
|
| 3 |
+
size 15580
|
__pycache__/migrate_qdrant_cross_references.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:102423c4b25c7f9f0901795e2bc84cd05ff948da0ef8e7ea8a5e50e6e1b9aeb7
|
| 3 |
+
size 7720
|
__pycache__/query_rag.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bc5a852df9863968409b1f467cef3bffe9f6b8965a98f09903c42f5dd253b009
|
| 3 |
+
size 27701
|
__pycache__/query_rag.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fd12e695b9dd382e3cabec2ad097de9721295ce96d17fb33a823fff2b27a8f58
|
| 3 |
+
size 40305
|
__pycache__/rag_invariants.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a4bec8d89680f875accc93f8925dd587b2f3dca2b31c42d2c9519523d7eb848b
|
| 3 |
+
size 17785
|
__pycache__/rag_invariants.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f7efec23939c751a9370c5bd431aa60d7d52cf046f4081118a208a548bdf1608
|
| 3 |
+
size 24027
|
__pycache__/reindex_sparse.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ec40a30ecf8e81ed87446d073a74cb8a1ff9e4e561f64ceb066aef7bb5b1820d
|
| 3 |
+
size 23683
|
__pycache__/self_rag_utils.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:575ea4bd24a964e01727b1d5d5a110e33e5ca00e33989adadcccf90a7dff0573
|
| 3 |
+
size 1148
|
__pycache__/self_rag_utils.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f01874b94703be7c9536f34a222a38e2f75bb66445edbff83aa41658be1927df
|
| 3 |
+
size 1655
|
__pycache__/structural_vetting.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a1a18143117dcbe956d49132356d42089f350dacd3ae1271cfab54a2d1e43068
|
| 3 |
+
size 6916
|
__pycache__/structural_vetting.cpython-314.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:88bf9ee31bbfe1a0b17f33c467c9787a58a67695702cadafa19b44a5b776af6e
|
| 3 |
+
size 12232
|
__pycache__/test_bge_m3_embedding_components.cpython-310.pyc
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:08169715acd3e1bc8f117c558dbd9dcd4b28df3ddd18a3e5fb35cf5bf6749e36
|
| 3 |
+
size 2130
|