Web Appผลงาน

Legal AI

POC ระบบค้นคว้ากฎหมายไทยด้วย AI — สร้างเพื่อพิสูจน์ว่า RAG + Structured Output สามารถทำให้ AI อ้างอิงกฎหมายจริงได้อย่างน่าเชื่อถือ

อ่าน 6 นาที

ระบบค้นคว้ากฎหมายไทย 84,000+ มาตรา สำหรับทนายความ — ถามเป็นภาษาคน ได้คำตอบพร้อมอ้างอิงมาตราจริงจากฐานข้อมูล

React 19ViteTailwind v4SupabaseGPT-4oRAGpgvector

Overview

Legal AI คือ POC ระบบค้นคว้ากฎหมายไทยที่ใช้ RAG (Retrieval-Augmented Generation) ดึงเนื้อหามาตราจริงจาก Database มาเป็น context ให้ AI ตอบ — ไม่ใช่แค่ generate จากความรู้ทั่วไป แต่อ้างอิงกฎหมายจริงที่ scrape มาจากสำนักงานคณะกรรมการกฤษฎีกา (OCS)

หน้าแรกของ Legal AI มีช่องถามคำถามกฎหมายเป็นภาษาธรรมชาติ
หน้าแรก — ช่องถามเป็นภาษาคนอยู่กลางจอ ไม่ต้องรู้เลขมาตรา
84K+
มาตราในฐานข้อมูล
1,811
กฎหมาย (OCS)
5
หมวดกฎหมาย
v13
Edge Function version

Design Thinking Process

ทำไมทนายความถึงต้องการเครื่องมือแบบนี้

หน้าจอแชทใหม่พร้อมตัวอย่างคำถามตามสถานการณ์
แชทใหม่: ตัวอย่างคำถามตาม "สถานการณ์" ช่วยให้ทนายเริ่มถามได้ทันที
01

Empathize · ทนายความกับการค้นคว้ากฎหมาย — ปัญหาที่ซ่อนอยู่

ทนายความไทยต้องค้นกฎหมาย 84,000+ มาตรา จากหลายพ.ร.บ. เพื่อเตรียมคดีหนึ่งคดี การค้นหาแบบเดิมใช้ keyword search บนเว็บ OCS ซึ่งต้องรู้ชื่อพ.ร.บ.ที่แน่นอน หรือเลข "มาตรา" ที่ต้องการ — ถ้าไม่รู้ก็หาไม่เจอ

Insight: ทนายรู้ "สถานการณ์" แต่ไม่รู้ว่าต้องค้น "มาตราอะไร" — ต้องการเครื่องมือที่ถามเป็นภาษาคนได้

02

Define · "ถ้า AI ตอบกฎหมายผิด อาจทำให้แพ้คดี"

ChatGPT ตอบกฎหมายได้ แต่มักอ้างมาตราผิด หรือ hallucinate กฎหมายที่ไม่มีอยู่จริง ซึ่งอันตรายมากสำหรับงานทนายความ — ต้องการระบบที่ อ้างอิงกฎหมายจริง ที่ตรวจสอบได้

HMW: How might we สร้าง AI ที่ตอบกฎหมายได้แม่นยำ โดยอ้างอิงเฉพาะมาตราจริงจากฐานข้อมูล?

03

Ideate · RAG — ให้ AI อ้างอิงจากฐานข้อมูลจริง

ใช้ RAG (Retrieval-Augmented Generation) แทนการให้ AI ตอบจากความรู้ทั่วไป:

• Scrape กฎหมาย 1,811 ฉบับ 84,376 มาตราจาก OCS
• Full-text search + Vector search (pgvector) หามาตราที่เกี่ยวข้อง
• ส่งเนื้อมาตราจริงเป็น context ให้ GPT-4o
• สั่งให้ AI อ้างอิงเฉพาะมาตราที่ปรากฏใน context เท่านั้น

04

Prototype · 3-Panel Layout — Chat + Citations + Law Viewer

ออกแบบ Chat Page เป็น 3 ส่วน: Sidebar (ประวัติแชท), Chat Area (ถาม-ตอบ AI พร้อม Citation Cards), Law Viewer Panel (ดูเนื้อกฎหมายจริง) — กด Citation Card แล้วเปิดดูเนื้อกฎหมายจริงได้ทันทีโดยไม่ต้องออกจากหน้าแชท

Key Decision: Citation Cards ใช้สี 3 แบบ (เขียว/ม่วง/ฟ้า) แยกตาม role ของกฎหมาย เพื่อให้ทนายเห็นภาพรวมได้เร็ว

05

Test · Comparison Testing — Legal AI vs Gemini Pro

ใช้ Playwright ทดสอบ 5 คำถามกฎหมายเปรียบเทียบ Legal AI กับ Gemini Pro:

• Legal AI: Structured output (citations + summary) ดีกว่า — เห็นมาตราอ้างอิงชัดเจน
• Gemini: เนื้อหาละเอียดกว่า ครบกว่า
• Next: เพิ่ม Vector Search เพื่อดึงมาตราที่เกี่ยวข้องได้แม่นยำขึ้น


Chat Interface

ถามเป็นภาษาคน — ได้คำตอบพร้อม Citation Cards ที่กดดูกฎหมายจริงได้

หน้าแชท 3 ส่วน ประวัติแชท คำตอบพร้อมการ์ดอ้างอิง และแผงอ่านเนื้อกฎหมาย
3-Panel จริง: ประวัติแชท · คำตอบ + Citation Cards · Law Viewer เปิดเนื้อมาตราข้างกัน
LA
Legal AI
N
ลูกจ้างถูกเลิกจ้างโดยไม่แจ้งล่วงหน้า มีสิทธิ์ได้ค่าชดเชยอะไรบ้าง?
AI
ตาม พ.ร.บ.คุ้มครองแรงงาน พ.ศ. 2541 ลูกจ้างที่ถูกเลิกจ้างโดยไม่แจ้งล่วงหน้ามีสิทธิ์ได้รับ:
1. ค่าชดเชย ตามอายุงาน (มาตรา 118)
2. สินจ้างแทนการบอกกล่าวล่วงหน้า (มาตรา 17)
3. ค่าเสียหายจากการเลิกจ้างไม่เป็นธรรม (พ.ร.บ.จัดตั้งศาลแรงงานฯ มาตรา 49)
พ.ร.บ.คุ้มครองแรงงาน พ.ศ. 2541
มาตรา 17 มาตรา 118 มาตรา 119
พ.ร.บ.จัดตั้งศาลแรงงานฯ
มาตรา 49

Key Features

ฟีเจอร์หลักที่ออกแบบมาเพื่อทนายความ

หน้าแรกบนมือถือ
หน้าแรกบนมือถือ
หน้าแชทบนมือถือ
หน้าแชทบนมือถือ

RAG Search

Full-text + Vector search 84K มาตรา — ดึงเนื้อกฎหมายจริงมาเป็น context ให้ AI ไม่ hallucinate

Citation Cards

แสดงกฎหมายที่อ้างอิงเป็น card สี 3 แบบ กดดูเนื้อกฎหมายจริงได้ทันที

Law Viewer Panel

อ่านเนื้อกฎหมายจริงจาก DB พร้อม highlight มาตราที่ AI อ้างอิง + สารบัญ

Structured Output

AI ตอบเป็น structured JSON: content + citations + court decisions + summary box

Chat History

ประวัติแชทจัดกลุ่มตามวัน ค้นหาได้ ลบได้ Auto-generate ชื่อ + description

Markdown Rendering

รองรับ headings, bold, bullets, numbered lists, tables, HR ใน AI response

Auth System

Supabase Auth — Email/Password + Google OAuth พร้อม RLS user-only data

Landing Page

Hero + stats จาก DB, แหล่งข้อมูล, Release Notes, Login Modal


RAG Architecture

ระบบ Retrieval-Augmented Generation ที่ทำให้ AI อ้างอิงกฎหมายจริง

คำตอบของ AI แยกตามกฎหมาย 3 ฉบับ พร้อมเลขมาตราและคำพิพากษาที่เกี่ยวข้อง
ผลของ RAG: คำตอบแยกตามฉบับกฎหมายที่ค้นเจอ + ลิงก์มาตรา + คำพิพากษาที่เกี่ยวข้อง
User Question
ทนายถามภาษาธรรมชาติ
→
Hybrid Search
FTS + pgvector 84K มาตรา
→
GPT-4o + Context
AI ตอบจากเนื้อมาตราจริง
→
Structured JSON
content + citations + summary
01

Scrape & Store

ดึงกฎหมาย 1,811 ฉบับ 84,376 มาตราจาก OCS API เข้า Supabase (PostgreSQL) พร้อม deduplicate

02

Hybrid Search

Full-text search (tsvector) + Vector search (pgvector, text-embedding-3-small 1536 dims) ผ่าน Supabase function hybrid_search

03

AI Generation

GPT-4o รับเนื้อมาตราจริงเป็น context — Prompt สั่ง "ONLY cite sections ที่ APPEAR ใน context" เพื่อป้องกัน hallucination

04

Structured Output

Response เป็น JSON: content (markdown), citations (กฎหมาย + มาตราอ้างอิง), courtDecisions, summary (ขั้นตอนที่ต้องทำ)


Database Schema

Supabase (PostgreSQL) + pgvector + Row Level Security

การ์ดแหล่งข้อมูลกฎหมายที่รวบรวม พร้อมสถานะ Active และ Planned
แหล่งข้อมูลที่เก็บเข้าฐานข้อมูล — สถานะ Active / Planned ต่อแหล่ง
Table Rows Description
law_sections 84,376 เนื้อหารายมาตรา (deduplicated) + vector embeddings
laws 1,811 กฎหมายจาก OCS ครบ 5 หมวด
conversations dynamic แชทของ user (RLS: user-only)
messages dynamic ข้อความ user + ai + citations (RLS: user-only)
releases 1 Release notes (public read)
system_stats 4 ตัวเลข stats หน้าแรก

Prompt Engineering

ปรับ prompt ถึง v13 เพื่อให้ AI ตอบแม่นยำสำหรับทนายความ

คอมโพเนนต์การ์ดอ้างอิงกฎหมายและกล่องสรุป
Citation Card + Summary Box — เหตุผลที่บังคับ AI ตอบเป็น structured JSON
edge-function/chat/prompt-v13.ts
// Key prompt rules for legal accuracy
const promptRules = {
  verification: "ONLY cite sections that APPEAR in DB context",
  lawNames: "Use EXACT law name from database",
  audience: "User is a lawyer - don't suggest 'consult a lawyer'",
  format: "400-600 words, ## headings, warnings, follow-up",
  model: "GPT-4o primary, GPT-4o-mini fallback",
};

Comparison: Legal AI vs Gemini Pro (5 Questions)

CriteriaLegal AIGemini Pro
Structured Citations✓ Card-basedInline text
Source Verification✓ DB-backedGeneral knowledge
Law Viewer✓ Real-time✗ None
Content DepthGood✓ More detailed
Answer Format✓ JSON structuredPlain text

OCS Scraper

ดึงข้อมูลกฎหมายจากสำนักงานคณะกรรมการกฤษฎีกา

แผงอ่านเนื้อพระราชบัญญัติ มีสารบัญและไฮไลต์มาตราที่ถูกอ้างถึงในแชท
Law Viewer: เนื้อมาตราที่ดึงจาก OCS + สารบัญ + ไฮไลต์มาตราที่อ้างในแชท
scripts/scrape-sections.ts
// Scrape 84,376 sections from OCS API
for (const law of laws) {
  const doc = await fetch(`OCS_API/getLawDoc?id=${law.id}`);
  const sections = parseSections(doc);
  await supabase.from('law_sections')
    .upsert(sections, { onConflict: 'law_id,section_number' });
}
// Result: 1,811 laws, 84,376 sections (deduplicated)

Development Timeline

สร้างจาก 0 ถึง production-ready POC

หน้า Release Notes แสดงเวอร์ชัน 1.3 และ 1.2
หน้า Release Notes ของต้นแบบ (ตัวเลขในภาพเป็นข้อมูลตัวอย่างของ prototype)
01

Foundation · Database + Scraper + Auth

Scrape กฎหมาย 1,811 ฉบับ 84K มาตราจาก OCS, สร้าง Supabase schema (5 migrations), Supabase Auth (Email + Google OAuth)

02

Landing Page · Hero + Data Sources + Login

Landing page พร้อม stats จาก DB, แหล่งข้อมูล OCS, Release Notes section, Login Modal

03

Chat System · 3-Panel Layout + RAG + Citations

Chat page ครบ 3 panels: Sidebar (history), Chat (AI response + Citation Cards), Law Viewer (เนื้อกฎหมายจริง)

04

AI Refinement · Prompt v13 + Vector Embeddings

ปรับ prompt ให้ verify มาตราก่อนอ้าง, เพิ่ม pgvector + text-embedding-3-small สำหรับ semantic search

05

Testing · Comparison Testing + UI Polish

Playwright ทดสอบเทียบกับ Gemini Pro 5 คำถาม, ปรับ Law Viewer highlight colors, Markdown rendering, auto-scroll


Project Structure

Frontend (React 19 + Vite) + Backend (Supabase Edge Functions)

หน้า Component Library ของ Legal AI
Component Library — แหล่งอ้างอิง component เดียวของทั้งโปรเจกต์
project-structure
src/
├── App.tsx // Routes + ProtectedRoute
├── pages/
│   ├── LandingPage.tsx // Hero + stats + login
│   ├── ChatPage.tsx // 3-panel chat interface
│   └── ReleasesPage.tsx // Release notes
├── components/
│   ├── chat/
│   │   ├── ChatSidebar.tsx // History grouped by date
│   │   ├── ChatMessage.tsx // Markdown + citations
│   │   ├── CitationCard.tsx // Color-coded law refs
│   │   └── LawViewer.tsx // Real law content panel
│   └── landing/ // Landing page sections
├── lib/
│   ├── supabase.ts // Client init
│   └── api.ts // All CRUD functions

supabase/
├── migrations/001-006 // Schema + pgvector
└── functions/chat/ // Edge Function v13

scripts/
├── scrape-ocs.ts // Law list from OCS API
├── scrape-sections.ts // 84K sections via getLawDoc
└── generate-embeddings.ts // pgvector embeddings

Lessons Learned

สิ่งที่ได้เรียนรู้จากการสร้าง Legal AI

สถานะ Network error, No results และ Rate limit
Error / No results / Rate limit — ออกแบบสถานะผิดพลาดไว้ตั้งแต่ต้น

RAG > Fine-tuning สำหรับ domain-specific data — แทนที่จะ fine-tune model ซึ่งแพงและ maintain ยาก การ scrape ข้อมูลจริงเข้า DB แล้วใช้ RAG ให้ AI อ้างอิง ได้ผลลัพธ์ที่ตรวจสอบได้ดีกว่ามาก

Prompt Engineering สำคัญมาก — จาก v1 ที่ AI hallucinate มาตรา ถึง v13 ที่สั่ง "ONLY cite sections ที่ APPEAR ใน context" ลด hallucination ได้อย่างมีนัยสำคัญ

Hybrid Search (FTS + Vector) ดีกว่า FTS อย่างเดียว — Full-text search หาได้เฉพาะ keyword match, แต่ vector search จับ semantic meaning ได้ เช่น "เลิกจ้าง" match กับ "ค่าชดเชย"

Structured Output ช่วย UX มาก — การให้ AI return JSON (content + citations + summary) แทน plain text ทำให้สร้าง UI ที่เป็นระเบียบและ interact ได้ (กด citation ดูกฎหมายจริง)

Color-coded Citations ช่วยเรื่อง cognitive load — สี 3 แบบ (เขียว/ม่วง/ฟ้า) ตาม role ของกฎหมาย ทำให้ทนายเห็นภาพรวมได้เร็วโดยไม่ต้องอ่านทุกบรรทัด

Tech stack ทั้งหมด

React 19ViteTailwind v4SupabasePostgreSQLpgvectorGPT-4oOpenAI EmbeddingsEdge FunctionsPlaywright

แทน · Natpakan Sirirat

Product Designer & AI Builder — ออกแบบประสบการณ์และสร้างเครื่องมือ AI ที่ใช้ได้จริง

0%