ระบบ Retrieval-Augmented Generation (RAG) Chatbot ที่พัฒนาโดยใช้ FastAPI, LangChain, LangFlow, ChromaDB, Redis สำหรับจัดการความจำการสนทนา และ PostgreSQL สำหรับ LangFlow/บันทึกแชท รองรับการแสดงผล Citation ใน API
- Conversational RAG: โต้ตอบพร้อมจดจำบริบทการสนทนา
- Document Ingestion: รองรับไฟล์
.pdf,.txt,.md - Vector Store: ใช้ ChromaDB จัดเก็บ embeddings
- LangFlow UI: ออกแบบ/แก้ไข pipeline ได้แบบ no-code
- Redis Memory: เก็บประวัติการสนทนาแบบ per-session
- PostgreSQL: เก็บข้อมูล LangFlow และ (optional) log แชท
.
├─ docker-compose.yml
├─ .env.example
├─ Makefile
├─ README.md
├─ data/docs/ # วางไฟล์เอกสารที่ต้องการทำ ingestion
├─ api/ # FastAPI + LangChain
│ ├─ app.py # API หลัก
│ ├─ pipelines.py # Single-turn RAG
│ ├─ pipelines_chat.py # Conversational RAG + citations
│ ├─ ingest.py # สร้าง embeddings และบันทึกลง Chroma
│ ├─ memory.py # Redis/In-memory store
│ ├─ db.py # บันทึกแชทลง Postgres (optional)
│ ├─ requirements.txt
│ └─ Dockerfile
├─ frontend/ # Next.js UI (ในอนาตค!!!)
└─ ops/ # ไฟล์ deploy/monitoring
git clone https://github.com/yourname/rag-chatbot.git
cd rag-chatbotcp .env.example .envแก้ไขค่าตามต้องการ เช่น รหัสผ่าน PostgreSQL, HuggingFace token
docker compose up -d --buildวางไฟล์ .pdf .md .txt ในโฟลเดอร์ data/docs/ จากนั้นรัน:
curl -X POST http://localhost:8000/ingestcurl -X POST http://localhost:8000/chat -H "Content-Type: application/json" -d '{"session_id":"test","message":"สรุปเอกสารทั้งหมด"}'- API (FastAPI) → http://localhost:8000
- LangFlow UI → http://localhost:7860
- Redis Commander → http://localhost:8081
- Adminer (Postgres UI) → http://localhost:8082
| Method | Endpoint | Description |
|---|---|---|
| POST | /ingest |
สร้าง embeddings จากไฟล์ใน data/docs/ |
| POST | /query |
ถามคำถามแบบ single-turn |
| POST | /chat |
ถามคำถามแบบมีความจำ session + citations |
| POST | /chat/clear |
ล้างความจำของ session |
| GET | /health |
ตรวจสอบสถานะระบบ |
- เข้าโฟลเดอร์
frontend/ - ติดตั้ง dependencies
npm install
- รัน dev server
npm run dev
- เปิด http://localhost:3000
- USE_REDIS: เปิด/ปิดการใช้ Redis memory
- ENABLE_CHATLOG_POSTGRES: บันทึกแชทลง Postgres หรือไม่
- HF_EMBEDDING_MODEL: เลือกโมเดล embeddings
- HF_GENERATOR_MODEL: เลือกโมเดลสร้างคำตอบ
- ลักษณะ: Sentence-Transformers (SimCSE) บน mBERT ฝึกจาก Thai Wikipedia
- รายละเอียด:
- embedding dimension: 768 :contentReference[oaicite:2]{index=2}
- ใช้ง่ายผ่าน sentence-transformers
- ข้อดี: ความเข้าใจภาษาไทยดี ใช้ RAG retrieval ได้ตรงจุด
- ข้อควรระวัง: context จำกัด ไม่รองรับเอกสารยาวเกิน token limit
- ลักษณะ: SimCSE บน XLM-Roberta สำหรับภาษาไทย
- รายละเอียด: embedding dimension: 768 :contentReference[oaicite:3]{index=3}
- เหมาะกับ: ความหลากหลายภาษา + ไทย แต่คล้ายกับ m-bert performance
- ลักษณะ: SimCSE บน WangchanBERTa (ไทย) :contentReference[oaicite:4]{index=4}
- จุดเด่น: ใช้ BERT ไทยที่ออกแบบเฉพาะ
- สถานการณ์ใช้งาน: หากต้องการ embed ภาษาไทยเฉพาะเจาะจง
- ลักษณะ: LLM ภาษาไทย ขนาด 4B บน Qwen3, ฟรี, Apache-2.0 :contentReference[oaicite:5]{index=5}
- จุดเด่น:
- รู้จักคิดและตอบเป็นภาษาไทยแม่น (98.4% accuracy)
- เหมาะสำหรับระบบ RAG ให้ context ช่วยตอบ
- inference ใช้งบประมาณต่ำ (suitable for edge)
- ข้อจำกัด: อาจ hallucinate ถ้าไม่มี context
- ลักษณะ: LLaMA-based 13B, instruction tuned ภาษาไทย :contentReference[oaicite:6]{index=6}
- จุดเด่น:
- คะแนนสูงบนข้อสอบภาษาไทย (benchmark สูง)
- รองรับ context ยาว (4096 Thai words)
- ข้อจำกัด: หน่วยความจำ GPU สูงกว่ารุ่น 4B
- ลักษณะ: Bilingual (Thai-English) 1B LLM, ได้รับ open source :contentReference[oaicite:7]{index=7}
- ข้อดี: ผสม data หลักภาษาไทย เหมาะสำหรับ edge / low-resource
- ข้อจำกัด: ความแม่นยำอาจต่ำกว่ารุ่นใหญ่
| ความต้องการ | Embedding Model | LLM/SLM Model |
|---|---|---|
| ภาษาไทยล้วน, ใช้ local, free | mrp/simcse-model-m-bert-thai-cased |
iapp/chinda-qwen3-4b (ประหยัด resource) |
| เอกสารภาษาไทยหลากหลายภาษา | mrp/simcse-roberta-base-thai, wangchanberta |
forecast for generation |
| ต้องการรองรับ context ยาว | (อาจ chunk ก่อน) | OpenThaiGPT 13b (RAG + long context) |
| edge device / low resource | Compact embeddings | KhanomTanLLM-1B (1B model) |
MIT License – SatangThevalue