跳到主要内容

OpenThai 2.0 法律 ThaiLLM

截至 2026 年 9 月 30 日免费
现在: 0 IC — 免费使用任何 iApp API 密钥(注册免费)促销后: 每 1K 输入/输出 token 0.01 / 0.02 IC (与 Thanoy Legal AI 相同)
v2.0 活动 POST /v3/llm/openthai2p0-legal/chat/completions

OpenThai 2.0 Legal (iapp/openthai2.0-legal-thaillm-nemotron-3-nano-30b-a3b) 是一个由 OpenThai 团队 (AIEAT / iApp Technology) 基于 NVIDIA Nemotron-3-Nano-30B-A3B 构建的开权重泰国法律 LLM。其托管 API 开箱即用 RAG 连接:每次请求都会对一个包含 39 项法律、6,300 条的泰国法规语料库(BM25 + Qwen3 嵌入搜索 + reranker)进行混合搜索,并根据当前法律文本来生成答案——返回检索到的条款,以便您进行显示或审计。

试用演示

Try OpenThai 2.0 Legal — Live

FREE API · 1 MONTH

Real model, real answers. Free with your iApp API key until 30 Sep 2026.

📚
RAG connected — answers grounded in real statute text

This API retrieves from a 39-law, 6,300-section Thai statute corpus (hybrid BM25 + embedding search + reranker) and grounds every answer in the current law text. The sections it selects are shown below each answer. Switch to Closed-book mode (rag: false in the API) to compare with the bare model.

Full legal analysis in prose, citing มาตรา — grounded in auto-retrieved current statute text.

Try an example:
🧑‍💻 Developer view — the API call behind this demo

This curl command updates live as you change the question, mode and toggles above. Run it in your terminal with your own API key — it is exactly what this page sends.

Request (curl)
curl -s https://api.iapp.co.th/v3/llm/openthai2p0-legal/chat/completions \
  -H "Content-Type: application/json" \
  -H "apikey: YOUR_IAPP_API_KEY" \
  -d '{
  "model": "openthai2.0-legal",
  "rag": true,
  "rag_inject": "system",
  "messages": [
    {
      "role": "system",
      "content": "You are a Thai legal expert. Answer with legal analysis and cite the relevant มาตรา."
    },
    {
      "role": "user",
      "content": "จำเลยขีดฆ่าและฉีกเอกสารหลักฐานแห่งหนี้ แม้ยังอ่านได้ ถือเป็นความผิดสำเร็จหรือเพียงพยายามกระทำผิด"
    }
  ],
  "temperature": 0,
  "top_p": 1,
  "max_tokens": 4096,
  "chat_template_kwargs": {
    "enable_thinking": true
  },
  "stream": true,
  "stream_options": {
    "include_usage": true
  }
}'

Outputs are decision support, not legal advice. Verify every citation against the current law. Free with an iApp API key to 30 Sep 2026; standard pricing afterwards is 0.01/0.02 IC per 1K input/output tokens (same as Thanoy Legal AI).

入门

  1. 先决条件

    • 免费的 iApp API 密钥 — 注册API KeysCreate New API Key
    • 一个泰国法律问题
  2. 端点

    基础 URLhttps://api.iapp.co.th/v3/llm/openthai2p0-legal
    端点POST /chat/completions (兼容 OpenAI)
    模型openthai2.0-legal
    认证apikey: <key> header Authorization: Bearer <key> (OpenAI SDK 可直接使用)
    速率限制每个 IP 30 次请求/分钟 (免费套餐)
  3. RAG 参数 (此 API 在 OpenAI schema 之上的额外功能)

    字段默认值含义
    ragtrue在服务器端检索泰国法律条款并依据其生成答案。false = 仅使用模型(闭卷)。
    rag_top_k8 (最多 20)注入到提示中的检索条款数量(当 rag_inject: "system" 时为 6)。
    rag_inject"user""user" = 训练过的 提供的上下文 框架,最适合 JSON 引用式回答。"system" = 系统提示中的建议性参考,最适合散文/长篇分析。
    dekafalse同时检索真实的最高法院判例(คำพิพากษาศาลฎีกา,13.3 万份)作为类比裁判注入。建议用于长篇分析,短的引条回答请关闭。
    unanswerable_mode"replace"答案无法依据法条时的返回方式:"replace" = 用标准拒答替换答案,"append" = 答案加警告,"flag" = 答案原样返回,仅给出判定字段。见 Guardrails
    web"auto"针对当前案件/事件的网络搜索:"auto" = 仅当问题指名真实案件/人物或含新闻用语时,true = 总是,false = 从不。
    guardtrue回答前检查(不存在的法律/条款/案件、虚构、离题)。false 关闭(不建议)。

    每个 RAG 响应都带有一个顶级的 retrieved_documents 数组 — 每个答案所依据的条款 {law, section, text, score}。流式传输时,它会附加在 第一个 SSE 块上。

如何获取 API 密钥?

请访问 API Key Management 页面查看您现有的 API 密钥或申请新密钥。

代码示例

cURL — RAG 开启 (默认)

curl -X POST 'https://api.iapp.co.th/v3/llm/openthai2p0-legal/chat/completions' \
-H 'apikey: YOUR_API_KEY' \
-H 'Content-Type: application/json' \
-d '{
"model": "openthai2.0-legal",
"messages": [
{"role": "user", "content": "ลักทรัพย์ในเวลากลางคืน ผิดมาตราใด"}
],
"max_tokens": 1024
}'

响应 (截断)

{
"id": "chatcmpl-...",
"model": "openthai2.0-legal-thaillm-nemotron-3-nano-30b-a3b",
"choices": [
{"message": {"role": "assistant", "content": "ลักทรัพย์ในเวลากลางคืน เป็นการกระทำความผิดตามมาตรา ๓๓๕ (๑) แห่งประมวลกฎหมายอาญา ..."}}
],
"usage": {"prompt_tokens": 2874, "completion_tokens": 17},
"rag": true,
"retrieved_documents": [
{"law": "ประมวลกฎหมายอาญา", "section": "335", "text": "ผู้ใดลักทรัพย์ (๑) ในเวลากลางคืน ...", "score": 0.9989},
{"law": "ประมวลกฎหมายอาญา", "section": "334", "text": "ผู้ใดเอาทรัพย์ของผู้อื่น ...", "score": 0.9936}
]
}

Python — OpenAI SDK (可直接使用,Bearer 认证)

from openai import OpenAI

client = OpenAI(
base_url="https://api.iapp.co.th/v3/llm/openthai2p0-legal",
api_key="YOUR_API_KEY",
)

r = client.chat.completions.create(
model="openthai2.0-legal",
messages=[{"role": "user", "content": "ลักทรัพย์ในเวลากลางคืน ผิดมาตราใด"}],
max_tokens=1024,
# extra_body={"rag": False} # bare model (closed-book)
# extra_body={"rag_inject": "system"} # essay / long-form analysis
)
print(r.choices[0].message.content)
print(r.model_extra.get("retrieved_documents")) # the law sections the answer used

流式传输 (SSE) — 检索到的条款将在第一个块中到达

stream = client.chat.completions.create(
model="openthai2.0-legal",
messages=[{"role": "user", "content": "อธิบายความผิดฐานลักทรัพย์โดยละเอียด"}],
max_tokens=2048,
stream=True,
)
for chunk in stream:
docs = chunk.model_extra.get("retrieved_documents")
if docs:
print("Grounded in:", [(d["law"], d["section"]) for d in docs])
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)

自 2026 年 9 月 16 日起,回答在所有 unanswerable_mode 下都逐 token 流式输出。此前 replace 模式会把整段回答扣留到引用检查完成, 因此一次性到达。现在草稿即时流式输出;在极少数(约 1–3%)判定需要替换或裁剪的情况下,流会继续输出一条分隔线和替换文本,最后一个 chunk 带有 content_modified: "replaced"(或 "pruned")和 replacement_content,UI 可据此把气泡内容换成替换文本而不是两者都显示。 发送 "hold_until_verdict": true 可恢复旧行为(回答扣留至检查完成)。

处理流式输出中被替换的回答

流是标准的 OpenAI 风格 SSE。草稿以 delta.content chunk 逐个到达;当判定替换或裁剪它时,会再来一个 content chunk,内容为 \n\n---\n + 替换文本,然后是 finish_reason: "stop" chunk,最后是 choices: [] 的收尾 chunk,带有 usage、判定字段和 replacement_contentdata: [DONE] 之后不再有任何内容。

data: {"choices":[{"delta":{"content":"ไม่"}}], ...}                      ← 草稿逐 token 流式输出
data: {"choices":[{"delta":{"content":"พบ"}}], ...}
...
data: {"choices":[{"delta":{"content":"\n\n---\nขออภัย ระบบไม่พบตัวบทกฎหมาย … แล้วถามใหม่ให้ชัดเจนขึ้น"}}]} ← 仅在被替换/裁剪时出现
data: {"choices":[{"delta":{"content":""},"finish_reason":"stop"}]}
data: {"choices":[], "usage":{...}, "answerable":false, "answerable_reason":"no_relevant_law_found",
"content_modified":"replaced", "replacement_content":"ขออภัย ระบบไม่พบตัวบทกฎหมาย … แล้วถามใหม่ให้ชัดเจนขึ้น", ...}
data: [DONE]

正常回答的 content_modifiednull(无需处理);"replaced" 表示应显示 replacement_content 而不是草稿;"pruned" 表示草稿中 引用不实条款的行已被删除(replacement_content 为裁剪后的文本加提示)。两种情况都应替换气泡内容:

# Python (openai SDK):实时渲染草稿,若收尾 chunk 要求则替换
draft, replacement = "", None
with client.chat.completions.create(model="openthai2.0-legal", messages=msgs, max_tokens=2048, stream=True,
extra_body={"rag_inject": "system", "deka": True, "web": "auto"}) as stream:
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
draft += chunk.choices[0].delta.content
ui.set_answer(draft) # 实时渲染
extra = chunk.model_extra or {}
if extra.get("replacement_content") is not None:
replacement = extra["replacement_content"] # 收尾 chunk:判定替换/裁剪了草稿
final_text = replacement if replacement is not None else draft
ui.set_answer(final_text)
// JavaScript (fetch + SSE):浏览器或 Node 客户端的同样逻辑
let draft = "", replacement = null;
const res = await fetch(url, { method: "POST", headers, body: JSON.stringify({ ...body, stream: true }) });
const reader = res.body.getReader(), dec = new TextDecoder(); let buf = "";
while (true) {
const { value, done } = await reader.read(); if (done) break;
buf += dec.decode(value, { stream: true });
let i; while ((i = buf.indexOf("\n\n")) >= 0) {
const line = buf.slice(0, i).trim(); buf = buf.slice(i + 2);
if (!line.startsWith("data: ") || line === "data: [DONE]") continue;
const o = JSON.parse(line.slice(6));
for (const c of o.choices || []) if (c.delta?.content) { draft += c.delta.content; ui.setAnswer(draft); }
if (o.replacement_content !== undefined && o.replacement_content !== null) replacement = o.replacement_content;
}
}
ui.setAnswer(replacement ?? draft);

若 UI 无法替换,请在请求体中发送 "hold_until_verdict": true:回答将扣留至判定完成后作为单个 content chunk 到达(9 月 16 日之前的行为), 且不会有 replacement_content

JSON 引用合同 (训练过的 RAG 模式)

为了获得机器可读的答案,请使用模型进行 RL 训练的系统提示 — 它仅引用检索到的上下文中存在的条款:

SYSTEM = (
"You are OpenThaiGPT-Legal, an expert assistant on Thai law. You are given a legal "
"question and the exact statutory sections needed to answer it. Reason step by step in "
"English, then give the final answer in Thai. Cite ONLY sections present in the provided "
"context, using each section's exact law_name and bare section number (e.g. 132, 77/1). "
'Output the final answer as JSON: {"answer": "<Thai answer>", '
'"citations": [{"law": "<law_name>", "section": "<bare id>"}]}.'
)

r = client.chat.completions.create(
model="openthai2.0-legal",
messages=[
{"role": "system", "content": SYSTEM},
{"role": "user", "content": "หมิ่นประมาทโดยการโฆษณา มีความผิดตามมาตราใด"},
],
temperature=0.0, max_tokens=1024,
extra_body={"chat_template_kwargs": {"enable_thinking": False}},
)
# -> {"answer": "...มาตรา 328", "citations": [{"law": "ประมวลกฎหมายอาญา", "section": "328"}]}

Guardrails、引用核验与网络搜索

2026 年 9 月 11 日新增,托管 API 已生效,调用方无需改动。

API 不再回答无法依据法条的问题。模型周围有三层机制:

  1. 回答前检查(premise guard) — 将问题与语料库、最高法院判例库和一个小型分类器比对。若问题指名不存在的法律、条款或案号、属虚构前提或根本不是法律问题,API 立即返回标准泰文拒答(不生成 token)。
  2. 回答后引用核验 — 答案中的每个 มาตรา 都与检索到的条文比对。模型凭记忆引用的条款会在语料库中查出原文,并由重排器核验引用句是否与原文相符;相符则视为有依据,否则删除该部分。若没有任何有依据的引用,则整个答案被替换为拒答。
  3. 当前案件的网络搜索 — 当问题涉及具体真实案件或人物(คดีอดีตหลวงพ่อโชติ ผิดอะไร)或含新闻用语时,API 会搜索网络、阅读头条新闻、提取实际被指控的罪名、检索对应法条并据此作答。没有任何非社交媒体新闻来源报道的案件将被拒答(case_not_found)。

响应字段

字段含义
answerable / answerable_reason答案被拒绝或改写时为 false。原因:no_relevant_law_foundungrounded_citationsmodel_declinedempty_answerunknown_lawunknown_sectionunknown_casecase_not_foundfictional_premisenot_legal_question
content_modifiednull"replaced"(以拒答替换答案)、"pruned"(删除无依据部分)、"appended"(附加警告)。原文在 original_content
citations{total, grounded, ungrounded: [{law, section}], lookup: [{law, section, score, ok}]}lookup 列出模型凭记忆引用的条款及其与原文的匹配分数。
retrieval_confidence检索条文中最高的重排器分数(0–1)。
guard{label: LEGAL / CASE / FICTION / OTHER, findings, deka_missing, deka_found, refused, reason}
web{used, trigger, query, provider, results: [{title, url, snippet, published}], offences, case_evidence, cached}usedtrue 时请将 results 作为来源展示。

流式返回时这些字段位于最后一个 SSE chunk(带 usage 的那个);retrieved_documents 仍在第一个 chunk。

示例 — 不存在的案件

POST /chat/completions
{"model": "openthai2.0-legal", "messages": [{"role": "user", "content": "คดีหม่ำเท่งโหน่ง ผิดมาตราอะไร"}]}

{
"choices": [{"message": {"role": "assistant",
"content": "ขออภัย ไม่พบข่าวหรือข้อมูลคดีจากแหล่งข่าวที่น่าเชื่อถือเกี่ยวกับ \"หม่ำเท่งโหน่ง\" จึงไม่สามารถวิเคราะห์ทางกฎหมายได้ ..."}}],
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
"answerable": false, "answerable_reason": "case_not_found", "content_modified": "replaced",
"guard": {"label": "CASE", "refused": true, "reason": "case_not_found"},
"web": {"used": false, "trigger": "guard_refused"}
}

示例 — 真实的当前案件

{"model": "openthai2.0-legal", "messages": [{"role": "user", "content": "คดีอดีตหลวงพ่อโชติ ผิดอะไร"}], "deka": true}

{
"choices": [{"message": {"content": "คดีอดีตหลวงพ่อโชติ ผิดฐานเป็นเจ้าพนักงานเบียดบังทรัพย์และปฏิบัติหน้าที่โดยมิชอบ ตามประมวลกฎหมายอาญา มาตรา 147 และมาตรา 157 และฐานฟอกเงินตามพระราชบัญญัติป้องกันและปราบปรามการฟอกเงิน พ.ศ. 2542 มาตรา 5 และมาตรา 60 ..."}}],
"answerable": true, "answerable_reason": "ok",
"citations": {"total": 4, "grounded": 4, "ungrounded": []},
"guard": {"label": "CASE", "refused": false},
"web": {"used": true, "trigger": "named_case", "provider": "google",
"offences": ["เบียดบังทรัพย์", "ฟอกเงิน", "ปฏิบัติหน้าที่มิชอบ"],
"case_evidence": {"entity": "อดีตหลวงพ่อโชติ", "news_sources": ["thestandard.co", "www.thairath.co.th"], "found": true},
"results": [{"title": "กองปราบฯ คุมตัว 'สมีโชติ' คดีฟอกเงิน-เบียดบังทรัพย์ ...", "url": "https://thestandard.co/...", "published": "2026-09-11T01:22:57+00:00"}]}
}

说明

  • 问候语或“你是谁”类消息(สวัสดีครับ、คุณคือใคร、แนะนำตัว、hello、thanks)会得到 OpenThai 2.0 Legal 的固定泰文自我介绍,而不是“非法律问题”的拒答(自 2026 年 9 月 16 日起;guard.kind = greeting / identity / thanks,不调用模型)。问候后紧接法律问题的消息按法律问题正常回答。
  • 法条原文中没有逐字出现的法律术语(คดีอุทลุม、ครอบครองปรปักษ์、ทางจำเป็น、นิติกรรมอำพราง、ฟ้องซ้ำ / ฟ้องซ้อน、ประกันตัว、เช็คเด้ง、เลิกจ้างไม่เป็นธรรม、อุ้มหาย 等)由术语表解析(自 2026 年 9 月 17 日起):定义该概念的条款会固定加入上下文,检索与判例搜索使用法条自身的措辞,guard.glossary 列出识别到的术语。“คดี”后接案件类型词(คดีอุทลุม、คดีอนาถา、คดีมโนสาเร่ 等)视为法律问题,而非具体案件名。
  • 涉及当前案件的问题会多花 1–3 秒(搜索并阅读 2–3 篇文章)。搜索结果和网页正文缓存 1 小时,同一案件的重复提问不会变慢。
  • 考试式假设题("นายแดงลักทรัพย์นายดำ …")不会被视为真实案件,仍按法条作答。
  • 在 107 题 guardrail 测试集(不存在的法律/条款/案件、虚构、离题、错别字、当前案件、NitiBench 真实问题)上:无论是否开启 thinking 均为 105/107 正确;更新前为 75/105。

功能与能力

核心功能

  • 内置服务器端 RAG — 混合 BM25 + 嵌入检索,并对 39 项泰国法律(ประมวลกฎหมายอาญา、ป.พ.พ.、วิ.แพ่ง、วิ.อาญา、ประมวลรัษฎากร 及其他 34 项)的当前文本进行重排序;已验证 2560 年后的罚款金额。
  • 可验证的引用 — 精确的法律名称 + มาตรา,可在文本中引用,也可作为固定的 JSON 合同;retrieved_documents 让您可以审计每个答案。
  • 推理模式 — 添加 "chat_template_kwargs": {"enable_thinking": true} 以获得以 <think>...</think> 包裹的逐步法律推理。
  • 开权重 — 同一个模型可下载并在单个 24 GB GPU (NVFP4) 上进行自托管。

用例

  • 法律聊天机器人和研究工具 — 提供有依据的答案,附带用户可以验证的法律条款。
  • 起草和审查助手 — JSON 引用合同可以直接用于自动化。
  • 法律科技产品 — 显示 retrieved_documents 面板,说明模型为何给出该答案。

推荐的生成设置

生产环境对话(推荐,2026-09-14):

{"temperature": 0.6, "top_p": 0.95, "repetition_penalty": 1.05, "min_p": 0.05, "max_tokens": 2048,
"rag_inject": "system", "deka": true, "deka_top_k": 2, "web": "auto", "unanswerable_mode": "replace"}

在易触发重复的问题集上测得(70 题 + 每种设置 8 题 × 8 个种子):旧默认值(temperature 0.7, top_p 0.9,无惩罚)约有 1–2 % 的回答陷入重复循环并跑满 4,096 token;推荐设置在 134 次运行中零循环,引用准确率不变(99 % 的引用条文出现在检索文本中),回答长度正常。

  • 若客户端未发送 repetition_penalty / presence_penalty / frequency_penalty,服务会自动应用 repetition_penalty 1.05
  • 循环保护: 服务检测到退化的重复循环(同一段落重复三次)即截断——流式模式下在该点停止引擎,非流式模式下裁剪文本,响应带 content_modified: "loop_cut"
  • max_tokens 2048 足以生成完整法律意见(平均回答约 500–650 token),并将最坏延迟减半。
任务temperaturethinkingrag_inject
引用式回答(JSON)0.0user(默认)
法律文章/分析0.0system
对话 / 生产环境0.6(+ repetition_penalty 1.05system

多轮对话

messages 中发送整段对话(包含 assistant 回合)。模型会读取历史,自 9 月 14 日起 RAG 层也会:简短的追问("แล้วถ้าทำงานมา 3 ปีล่ะ"、"การเล่นหมายเลข 5 ถึง 15 ในบัญชี ข. คือ")会与上一个问题一起检索(guard.retrieval_query 显示合并后的查询),守卫按上下文分类,法律对话中形似追问的消息不会被拒为"非法律问题"。独立的离题消息 ("สูตรทำต้มยำกุ้ง")仍会被拒绝。

上下文长度

自 2026 年 9 月 16 日起,主引擎接受最长 262,144 个 token 的提示(备用引擎 131,072)。泰文约为每 token 1.3–1.8 个字符,因此 完整的 256K 提示约相当于 35–45 万个泰文字符。

  • 自动截断。 超过引擎窗口的提示不会被拒绝:服务使用引擎自身的 tokenizer 计数,并截去最长的 user 消息的中间部分,使 提示 + max_tokens 恰好放得下,并在截断处留下泰文标记。响应中会带有 truncation: {prompt_tokens_before, prompt_tokens_after, removed_chars, limit}(非流式响应体,以及流式的第一个 chunk)。 短提示没有 truncation 字段。
  • 准确率在整个窗口内并不均匀。 在使用真实法条文本的“大海捞针”测试中,模型在 73K 和 92K token 时找到了两个植入事实, 在 115–140K 时只找到一个,165K 以上则一个也找不到。需要精确回答的文档请控制在 约 90K token(≈15 万泰文字符) 以内; 更长的材料请拆分或按章节提问。
  • 代价。 250K token 的提示约需 30–35 秒处理,期间会拖慢同一引擎上的其他请求;短请求不受更大窗口的影响。
  • 请求大小。 自 2026 年 9 月 16 日起接受最大 64 MB 的请求体;完整的 256K token 泰文提示约 1.3 MB。超过 500 KB 的请求体会经由单独的中继路径到达同一引擎,因此会多出几百毫秒延迟。

健康检查与状态页

同一主机上的免密钥端点(不消耗积分、无需 API key):

curl -s https://api.iapp.co.th/v3/llm/openthai2p0-legal/health/h100     # 预留 H100 集群正常时返回 200,否则 503
curl -s https://api.iapp.co.th/v3/llm/openthai2p0-legal/health # API 仍可应答时返回 200,两个后端都不可用时返回 503

/health 返回 status: "ok"(H100 在服务)、"degraded"(仅备用后端)或 "down",并附带 h100backup 对象,各含 healthyprobe(最近一次探测结果与延迟)、load(实时 RAG 槽位与引擎队列)和 checked_at/health/h100/health/rag(H100 上的 embedding + reranker 引擎与全库 RAG 索引)与 /health/backup 只返回对应对象,且 HTTP 状态码与健康状态一致,监控工具只需看状态码。与 status.iapp.co.th 同一格式的状态页 (90 天可用率条与近期事件)位于 api.iapp.co.th/v3/llm/openthai2p0-legal/status(JSON 在 …/status/api)。

仅供决策支持,非法律建议

在依赖输出之前,必须由合格的专业人士根据现行法律进行核实。检索质量决定结果 — retrieved_documents 数组正是为了让每个答案都可以被审计。