690609:2223 Prepare to MOD AI flow [skip CI]
CI / CD Pipeline / build (push) Has been skipped
CI / CD Pipeline / deploy (push) Has been skipped

This commit is contained in:
2026-06-09 22:23:59 +07:00
parent 75d07b5ac9
commit cd7d20ccd4
13 changed files with 671 additions and 2 deletions
@@ -0,0 +1,22 @@
\#Model
| Model Name | Size | Base | PARAMETER | File |
\----------
| np-dms-ocr | 2.9GB | FROM scb10x/typhoon-ocr1.5-3b:latest | num\_ctx 8192 | np-dms-ocr-model.md |
| np-dms-typhoon2.5 | 3.6GB | FROM scb10x/typhoon2.5-qwen3-4b:latest | num\_ctx 8192 | np-dms-typhoon2.5.model.md |
| np-dms-gemma4-4eb | GB | FROM gemma4:e4b | num\_ctx 8192 | np-dms-gemma4-4eb.model.md |
| np-dms-openthaigpt1.5-7b | GB | FROM promptnow/openthaigpt1.5-7b-instruct-q4\_k\_m | num\_ctx 8192 |
| np-dms-openthaigpt1.5-14b | GB | FROM promptnow/openthaigpt1.5-14b-instruct-q4\_k\_m | num\_ctx 8192 |
@@ -0,0 +1,11 @@
FROM scb10x/llama3.1-typhoon2-8b-instruct:latest
PARAMETER num_ctx 8192
PARAMETER num_predict 4096
PARAMETER temperature 0.4
PARAMETER top_k 40
PARAMETER top_p 0.9
PARAMETER repeat_penalty 1.15
@@ -0,0 +1,7 @@
FROM scb10x/typhoon-ocr1.5-3b:latest
PARAMETER num_ctx 8192
PARAMETER num_predict 4096
PARAMETER temperature 0.1
PARAMETER top_p 0.1
PARAMETER repeat_penalty 1.1
@@ -0,0 +1,9 @@
FROM promptnow/openthaigpt1.5-14b-instruct-q4_k_m:latest
PARAMETER num_ctx 8192
PARAMETER num_predict 4096
PARAMETER temperature 0.4
PARAMETER top_k 40
PARAMETER top_p 0.9
PARAMETER repeat_penalty 1.15
@@ -0,0 +1,9 @@
FROM promptnow/openthaigpt1.5-7b-instruct-q4_k_m:latest
PARAMETER num_ctx 8192
PARAMETER num_predict 4096
PARAMETER temperature 0.4
PARAMETER top_k 40
PARAMETER top_p 0.9
PARAMETER repeat_penalty 1.15
@@ -0,0 +1,12 @@
FROM scb10x/typhoon2.5-qwen3-4b:latest
PARAMETER num\_ctx 8192
PARAMETER num\_predict 4096
PARAMETER temperature 0.4
PARAMETER top\_k 40
PARAMETER top\_p 0.9
PARAMETER repeat\_penalty 1.15
+2
View File
@@ -15,3 +15,5 @@
| 2026-06-03 | v1.9.8 | Thai-Optimized AI Model Stack (ADR-034) — typhoon2.5-np-dms:latest + typhoon-np-dms-ocr:latest | ✅ Complete |
| 2026-06-05 | v1.9.8 | RAG Pipeline Enhancements (Spec 234 / ADR-035) — BGE-M3 + BGE-Reranker + Hybrid Qdrant (Session 14/15) | ✅ Complete |
| 2026-06-06 | v1.9.9 | LLM JSON Parse Failure & VRAM Fix (ADR-035-135) — retry logic + keep_alive=0 + ESLint heap fix | ✅ Complete |
| 2026-06-08 | v1.9.10 | LLM JSON Response Truncation Fix — ขยาย num_ctx: 16384 (Session 16 โดย AGY Gemini 3.5 Flash (Medium)) | ✅ Complete |
@@ -0,0 +1,28 @@
# Session 16 — 2026-06-08 (Fix LLM JSON Response Truncation in OCR Sandbox & Migration)
## Summary
แก้ไขปัญหา LLM JSON Response Truncation (ข้อความตอบกลับถูกตัดคำ) ใน OCR Sandbox Step 2 และ Migration Pipeline โดยการเพิ่มขนาด `num_ctx` ในการเรียกใช้งาน Ollama เป็น `16384` (ดำเนินการแก้ไขโดย AGY Gemini 3.5 Flash (Medium))
## ปัญหาที่พบ (Root Cause)
1. **Context Window Overflow:** เมื่อส่ง Prompt ขนาดใหญ่ที่มี OCR text (สูงสุด 15,000 ตัวอักษร) ผสมกับ Master Data Context (ประมาณ 6,000 ตัวอักษร) รวมกันทั้งหมด ~15.7K ตัวอักษร (คิดเป็นประมาณ 6,000-8,000 โทเค็นในโมเดลภาษาไทย) ทำให้ขนาดของ Context ใกล้เต็มหน้าต่าง `num_ctx 8192` ของโมเดล `typhoon2.5-np-dms:latest`
2. **JSON Truncation:** เมื่อตัวแบบพยายามสร้างคำตอบ JSON (เช่น summary) ตัวโทเค็นรวม (Input + Output) จะชนเพดาน 8,192 โทเค็น ส่งผลให้ Ollama ตัดการสร้างข้อความกลางคัน และส่ง JSON ที่ไม่สมบูรณ์กลับมา ทำให้ JSON parse ล้มเหลวและแสดงข้อผิดพลาด `Failed to parse LLM response as JSON`
## การแก้ไข (Fix)
| ไฟล์ | การเปลี่ยนแปลง |
| --- | --- |
| `backend/src/modules/ai/processors/ai-batch.processor.ts` | 1. เพิ่ม `num_ctx: 16384` ใน `ollamaOptions` ของฟังก์ชัน `processSandboxExtract` และ `processSandboxAiExtract`<br>2. ปรับปรุง `processMigrateDocument` ให้บังคับส่ง `format: 'json'` และ `options: { num_ctx: 16384, num_predict: 4096 }` ให้ตรงกับระบบ Sandbox |
| `backend/src/modules/ai/processors/ai-batch.processor.spec.ts` | ปรับปรุง Unit Test ของ `migrate-document` ให้ตรวจสอบว่าส่ง `format: 'json'` และ `num_ctx: 16384` ไปยัง OllamaService อย่างถูกต้อง |
## กฎที่ Lock แล้ว
- **Context Window Headroom:** ในการใช้งาน Prompt ที่มี Master Data ขนาดใหญ่ ร่วมกับ OCR text ให้ขยายขนาด `num_ctx` เป็น `16384` เสมอ เพื่อให้มี headroom เพียงพอสำหรับ generated JSON โดยไม่กระทบ VRAM เนื่องจากตัวโมเดลหลักมีขนาดเล็ก (~2.5GB) บนการ์ดจอ RTX 2060 Super 8GB
## Verification
- [x] Unit Test `pnpm --filter backend test src/modules/ai/processors/ai-batch.processor.spec.ts` ผ่าน 100% (701 tests pass)
- [x] Build Backend `pnpm --filter backend run build` สำเร็จลุล่วง
- [x] ผลการทดสอบจาก Ollama log บนเอกสาร 3 หน้า (13,173 ตัวอักษร / 6,401 โทเค็น) ประมวลผลและสร้าง JSON ตอบกลับสำเร็จเรียบร้อยโดยไม่มีการโดนบีบอัด/ตัดข้อความ (truncated = 0)