Spaces:
Sleeping
Sleeping
metadata
title: M Chatbot
emoji: 📚
colorFrom: purple
colorTo: pink
sdk: docker
pinned: false
M_chatbot - RAG Chatbot quy che dao tao
Du an nay la API chatbot RAG (Retrieval-Augmented Generation) cho bai toan tra cuu quy che dao tao. He thong dung FastAPI + Qdrant + BM25 + Cross-Encoder rerank + LLM (Groq/Gemini), co ho tro ca response JSON va streaming SSE.
1) Luong tong quan
Client (web/mobile)
|
| POST /chat or /chat/stream
v
main.py (FastAPI)
|
| lay lich su hoi thoai tu PostgreSQL
v
core/qa_pipeline.py
|-- generate_standalone_query (chuan hoa cau hoi theo ngu canh)
|-- analyze_and_expand_query (phan loai + tao truy van mo rong)
|-- HybridRetriever.search (BM25 + Vector Search)
|-- advanced_rerank (Cross-Encoder)
|-- create_advanced_prompt (tao prompt cuoi)
|-- goi LLM Groq (fallback Gemini)
v
Tra ve ket qua cho main.py
|
| luu luot chat vao PostgreSQL
v
Tra ket qua ve Client (JSON hoac SSE delta)
2) Luong khoi dong he thong
Khi server bat dau, lifespan trong main.py chay theo thu tu:
- Doc bien moi truong tu
core/config.py. - Tao pool ket noi PostgreSQL (
asyncpg) va dam bao banghistoryton tai. - Ket noi Qdrant Cloud.
- Khoi tao
CollectionRouterRetrieverde tim theo cac collection dang active tren Qdrant. - Khoi tao Supabase sync coordinator va chay
startup:initial_syncthong quabuild_vectorstore_improvedtrongcore/vectorstore.py(co the cho toi da theoSUPABASE_STARTUP_SYNC_WAIT_SECONDShoac chay nen). - Bat polling sync dinh ky de dong bo thay doi add/update/delete tu Supabase.
- Danh dau app san sang (endpoint
/healthzse baoready=true).
3) Luong ingest tai lieu (Supabase-only)
Luong nay duoc kich hoat boi scheduler/event sync trong core/supabase_sync_service.py va ingest trong core/document_ingest_service.py:
- Lay danh sach object tu Supabase Storage va diff voi snapshot de xac dinh
added/updated/deleted. - Download tung file can ingest ve file tam.
- Trich xuat noi dung tai lieu bang bo ham cu (
load_documents_from_filetrongcore/vectorstore.py). - Lam sach text bang bo ham cu (
clean_texttrongcore/text_utils.py). - Chunk van ban bang bo ham cu (
smart_chunkingtrongcore/chunking.py). - Embedding chunks bang model trong
core/models.py. - Upsert vector len Qdrant collection theo folder nam hoc.
- Xoa/ghi de theo
object_pathde dam bao incremental sync va tranh duplicate.
3.1) Hoi va tra loi theo nam hoc
He thong ho tro tu dong nhan dien nam hoc khi nguoi dung hoi, vi du:
Hoc phi nam 2022-2023 nhu the nao?Quy dinh thi truc tuyen nam 2021-2022Quy che hoc bong nam 2023
Co che xu ly:
- Pipeline phat hien nam (dang
YYYY-YYYYhoac nam leYYYY) trong cau hoi. - Retriever loc tai lieu theo
academic_yearphu hop. - Prompt bat buoc LLM uu tien tra loi dung pham vi nam duoc hoi.
- Neu khong co du lieu cho nam do, chatbot se thong bao ro khong tim thay thong tin phu hop theo nam.
4) Luong chat khong streaming (/chat)
- Nhan request (
session_id,user_id,message) taimain.py. - Lay lich su gan day cua session tu PostgreSQL.
- Goi
ask_ai_improved(...)trongcore/qa_pipeline.py. - Ben trong pipeline:
- Tai tao cau hoi doc lap theo ngu canh lich su.
- Phan loai va mo rong truy van tim kiem.
- Tim kiem lai voi Hybrid Retriever (BM25 + Vector).
- Rerank bang Cross-Encoder.
- Tao prompt cuoi theo mau nghiep vu.
- Goi LLM tao cau tra loi.
- Nhan full response va luu ca user/assistant message vao bang
history. - Tra ve JSON:
{ "response": "..." }.
5) Luong chat streaming SSE (/chat/stream)
Tuong tu luong /chat, khac o cho:
ask_ai_stream_delta(...)sinh tung doan text nho (delta).main.pydong goi tung delta thanh SSE event:data: {"delta": "...", "done": false}.- Khi xong, gui event ket thuc:
done=true. - Luu full response vao DB sau khi stream hoan tat.
6) Giai thich tung file trong luong
Entry va API layer
main.py: Diem vao cua he thong. Quan ly startup/shutdown, DB pool, retriever, va toan bo endpoint (/,/healthz,/sessions/{user_id},/chat/history/{session_id},/chat,/chat/stream).api/chat_api_routers.py: File router du phong, hien tai de trong.
Core pipeline
core/config.py: Tap trung bien cau hinh (model names, chunking, retrieval, Qdrant, DB, gioi han context/output).core/qa_pipeline.py: Nguoi dieu phoi chinh cua luong hoi-dap; bao gom phan tich cau hoi, truy hoi tai lieu, rerank, tao prompt, goi LLM va fallback provider.core/analyze_and_expand.py: Phan loai cau hoi va tao danh sach truy van mo rong de tim kiem chinh xac hon.core/prompting.py: Sinh prompt nghiep vu co guardrail de ep cau tra loi bam sat tai lieu.core/retriever.py: Hybrid retrieval ket hop BM25 va vector similarity bang RRF.core/rerank.py: Rerank tap tai lieu lay duoc bang Cross-Encoder.
Du lieu va vector
core/vectorstore.py: Cung cap bo ham xu ly tai lieu cu (doc PDF/DOCX, metadata nam hoc) duoc tai su dung trong luong Supabase ingest, dong thoi chuabuild_vectorstore_improved/load_vectorstore_improvedcho luong Supabase.core/chunking.py: Cat van ban thong minh (uu tien giu cau truc bang/danh sach).core/text_utils.py: Lam sach va chuan hoa noi dung text truoc khi embedding.core/models.py: Khoi tao embedding model va cross-encoder model.
Ho tro van hanh
core/llm_utils.py: Ham utility goi LLM an toan (stream/invoke co retry/timeout), hien chua duoc su dung dong nhat tren toan bo pipeline.client_demo.html: Trang web demo de thu nhanh 2 endpoint/chatva/chat/stream.Dockerfile: Cau hinh dong goi va chay API bang Docker/Uvicorn tren cong7860.requirements.txt: Danh sach dependency Python cua du an.
7) Bien moi truong quan trong
Toi thieu can co:
QDRANT_URLQDRANT_API_KEYDATABASE_URLGROQ_API_KEYS(hoacGROQ_API_KEY)
De bat dong bo Supabase Storage (scheduler quet dinh ky):
SUPABASE_URLSUPABASE_SERVICE_ROLE_KEYSUPABASE_STORAGE_BUCKET(mac dinh:file)SUPABASE_SYNC_INTERVAL_SECONDS(khuyen nghi 120; he thong tu gioi han trong khoang 60-180 giay)SUPABASE_STARTUP_SYNC_WAIT_SECONDS(mac dinh:5; dat0de khong cho initial sync luc khoi dong, giup API len nhanh hon)SUPABASE_ADMIN_SYNC_TOKEN(du phong cho endpoint admin sync o cac giai doan tiep theo)SUPABASE_SYNC_SNAPSHOT_FILE(mac dinh:supabase_sync_snapshot.json)SUPABASE_SYNC_ALLOWED_IPS(danh sach IP duoc phep goi endpoint admin sync, cach nhau boi dau phay)SUPABASE_SYNC_ALLOW_PRIVATE_NETWORK(mac dinhtrue; cho phep IP private/loopback)COLLECTION_ROUTER_TOP_N(so collection active se tim khi query khong chi dinh nam hoc)
Tuy chon:
GEMINI_API_KEYSALLOW_ORIGINSMAX_HISTORY_MESSAGES,MAX_CONTEXT_CHARS,MAX_OUT_CHARSCHUNK_SIZE,CHUNK_OVERLAP,TOP_K_RESULTS,FINAL_TOP_K
8) Chay nhanh local
pip install -r requirements.txt
uvicorn main:app --host 0.0.0.0 --port 7860 --reload
Sau khi chay, kiem tra:
GET /healthz- Dung
client_demo.htmlde test ca non-streaming va streaming.
9) Ghi chu
- Trong Hugging Face Spaces, frontmatter o dau file README can duoc giu nguyen.
- Lan chay dau co the cham do qua trinh initial sync tu Supabase (download + chunk + embedding + upsert Qdrant).