AsrofiSyaikho/Ai-Contextual-Model-Backend
0
My AI Backend
Project backend untuk kompetisi.
JawaChat Fase 2 — Backend RAG + FastAPI
Fase ini mengubah chatbot CLI menjadi REST API dengan RAG pipeline, siap untuk diintegrasikan ke PWA di Fase 3.
Struktur File
jawachat-fase2/
├── backend/
│ ├── __init__.py
│ ├── main.py ← FastAPI app + semua endpoint
│ ├── config.py ← Settings dari .env
│ ├── models.py ← Pydantic schema request/response
│ ├── rag_pipeline.py ← Core RAG: embed → retrieve → generate
│ ├── prompts.py ← System prompt per topik
│ └── logger.py ← Logging terpusat
├── scripts/
│ └── seed_knowledge_base.py ← Isi ChromaDB dengan data awal
├── tests/
│ └── test_api.py ← Pytest untuk semua endpoint
├── .env.example ← Template konfigurasi
├── requirements.txt ← Dependencies Python
└── README.mdSetup (Hari 3)
1. Install Dependencies
pip install -r requirements.txtCatatan:sentence-transformersakan download modelmultilingual-e5-small(~480MB) saat pertama kali dijalankan. Butuh koneksi internet.
2. Setup File .env
cp .env.example .env
# Edit .env, isi GROQ_API_KEY dengan key kamu3. Jalankan Backend
uvicorn backend.main:app --reload --port 8000Output yang diharapkan:
INFO | Memulai JawaChat backend...
INFO | Memuat embedding model: intfloat/multilingual-e5-small
INFO | RAG pipeline berhasil diinisialisasi.
INFO | Application startup complete.4. Isi Knowledge Base (Hari 4-5)
# Seed dengan dokumen contoh bawaan
python scripts/seed_knowledge_base.py
# Atau seed via API (backend harus jalan dulu)
python scripts/seed_knowledge_base.py --via-apiEndpoint API
GET /health
Cek status semua komponen.
curl http://localhost:8000/healthPOST /chat
Chat dengan respons penuh.
curl -X POST http://localhost:8000/chat \
-H "Content-Type: application/json" \
-d '{
"message": "Piye cara tanam padi sing apik?",
"topic": "pertanian",
"use_rag": true
}'POST /chat/stream
Chat dengan streaming (kata per kata).
curl -X POST http://localhost:8000/chat/stream \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"message": "Pupuk apa sing apik?", "topic": "pertanian"}'POST /ingest
Tambah dokumen ke knowledge base.
curl -X POST http://localhost:8000/ingest \
-H "Content-Type: application/json" \
-d '{
"documents": [{
"content": "Isi teks dokumen dalam bahasa Jawa...",
"source": "nama_file.txt",
"topic": "pertanian"
}]
}'Swagger UI (Dev)
Buka di browser: http://localhost:8000/docs
Alur RAG Pipeline
User message
│
▼
[Embedding: multilingual-e5-small]
│
▼
[ChromaDB: similarity search, top-3]
│
├── score ≥ 0.35? → masukkan ke context
│
▼
[Build prompt: system + history + context + message]
│
▼
[Groq API: Qwen3-32b, reasoning_format=hidden]
│
▼
[Response dalam Bahasa Jawa + daftar sumber]Testing
# Jalankan semua tes
pytest tests/ -v
# Tes dengan coverage
pytest tests/ -v --tb=shortYang Divalidasi di Fase 2
- [ ] FastAPI berjalan di port 8000
- [ ]
/healthmengembalikan status OK - [ ]
/chattanpa RAG merespons dalam bahasa Jawa - [ ]
/chatdengan RAG menyertakan dokumen relevan - [ ]
/chat/streamstreaming berjalan (SSE) - [ ]
/ingestberhasil tambah dokumen ke ChromaDB - [ ] Multi-turn conversation context terjaga
- [ ] Semua topik (pertanian, kesehatan, pendidikan, umum) berjalan
- [ ] Pytest lulus semua tes
Roadmap
Catatan Teknis
- Node.js tidak diperlukan untuk fase ini
- ChromaDB berjalan lokal, data tersimpan di
./chroma_db/ - Model embedding di-cache otomatis di
~/.cache/huggingface/ - Rate limit Groq gratis: ~30 req/menit — cukup untuk development
reasoning_format: hiddendikirim viaextra_bodyuntuk kompatibilitas SDK lama
