Skip to main content

🔗 Using OpenThai 2.0 Legal with RAG

The bare model is only half the system. OpenThai 2.0 Legal scores near-perfect (0.99 citation F1) when the right law sections are in the prompt — but closed-book answers come from model memory and can be wrong or incomplete. For real legal work, always pair it with retrieval. Our free hosted API already ships with RAG built in (hybrid search over a 39-law statute corpus, on by default) — this guide is for when you self-host the open weights and want the same grounding on your own infrastructure. Two ways, each under an hour.

Before you rely on this in real work

Without retrieval (RAG), the model answers from knowledge stored in its weights. It may cite the wrong section or miss an applicable one, so every answer must be checked against the statute.

With retrieval (RAG), the model cites only the sections your retrieval system supplies. Accuracy therefore depends primarily on the quality of that retrieval — if the wrong sections are retrieved, the answer will follow them.

In both cases the model's output is decision support, not legal advice. A qualified professional should verify every citation against the current law before it is relied upon.

Good RAG starts with a good corpus. These are open, Thai, and legal-specific:

DatasetWhat it containsLicense
WangchanX-Legal-ThaiCCL-RAG (VISTEC / AIResearch)Thai Civil & Commercial Law Q&A built for RAG training and evaluation — queries paired with the exact supporting sectionsOpen (see card)
NitiBench (VISAI-AI)The Thai legal benchmark this model is evaluated on — great for measuring your own RAG pipeline end to endMIT
Thai statutes — Office of the Council of StateThe authoritative source of current Thai statutory text (ประมวลกฎหมาย, พ.ร.บ.) for building your retrieval corpusPublic sector

Tip: chunk statutes by section (มาตรา), not by fixed character count — one มาตรา per chunk with law_name + section metadata is exactly the format the model's citation contract expects.

Open WebUI (~110k GitHub stars) is the fastest way to get a chat-with-your-documents interface with built-in RAG ("Knowledge"), user management, and OpenAI-compatible connections.

Open WebUI — the most popular self-hosted LLM interface (image: Open WebUI project)

1. Run Open WebUI

docker run -d -p 3000:8080 \
-v open-webui:/app/backend/data \
--name open-webui --restart always \
ghcr.io/open-webui/open-webui:main

Open http://localhost:3000 and create the admin account.

Get a free iApp API key first: registerAPI KeysCreate New API Key.

  1. Go to Admin Panel → Settings → Connections
  2. Under OpenAI API, click + to add a connection:
    • API Base URL: https://api.iapp.co.th/v3/llm/openthai2p0-legal
    • API Key: your iApp API key
  3. Save — the model openthai2.0-legal appears in the model selector
tip

Our gateway reads the key from either the Authorization: Bearer header (which Open WebUI sends) or an apikey header — both work.

  1. Go to Workspace → Knowledge → + Create a Knowledge base (e.g. "ประมวลกฎหมายแพ่งและพาณิชย์")
  2. Upload your statute files — ideally one มาตรา per chunk (see dataset tip above)
  3. In a chat with openthai2.0-legal, type # and select your Knowledge base — or attach it permanently to a custom model in Workspace → Models

4. Ask, grounded

Open WebUI retrieves the relevant sections and injects them into the prompt — exactly the "open-book" setting where this model scores 0.99. Ask in Thai:

นาย ก. ขับรถประมาทชนรถนาย ข. ต้องรับผิดตามกฎหมายใด

The answer now cites the retrieved sections rather than relying on memory.

Option B — OpenThaiRAG (Thai-native RAG framework)

OpenThaiRAG by the OpenThaiGPT community (project page) is an open-source RAG framework built specifically for Thai: Milvus vector database + BAAI/bge-m3 embeddings (excellent Thai support) + vLLM serving.

git clone https://github.com/OpenThaiGPT/openthairag
cd openthairag
docker-compose up -d

Index your Thai legal documents (place .txt files in /docs, then run the indexer), and query:

curl --location 'http://localhost:5000/completions' \
--header 'Content-Type: application/json' \
--data '{"prompt": "ลักทรัพย์ในเวลากลางคืน ผิดมาตราใด", "max_tokens": 2048}'

Point its LLM backend at OpenThai 2.0 Legal — either our hosted endpoint (https://api.iapp.co.th/v3/llm/openthai2p0-legal with your API key) or your own vLLM serving the open weights — and both retrieval and generation run on Thai-optimized components end to end.

Which one should I pick?

Open WebUIOpenThaiRAG
Best forTeams that want a polished chat UI todayBuilders who want a Thai-native pipeline they control
RetrievalBuilt-in Knowledge (managed for you)Milvus + BGE-M3 (tunable)
Effort~15 minutes~1 hour
InterfaceFull web app, multi-userREST API you integrate yourself

For maximum citation accuracy in either stack, use the JSON citation system prompt from the model page — the model was RL-trained on exactly that contract.

Next steps