รีวิว TypeLLM — สั่ง LLM ให้ตอบตาม type/schema โดยไม่ต้องเปลี่ยนโมเดล
ข้อมูล Repo
TypeLLM/TypeLLM- ภาษา
- Python
- ดาว
- 941
- สร้างเมื่อ
- แก้ไขล่าสุด
เปลี่ยนสถานะผ่าน API: PATCH /api/reports/6009dc2a-a2f2-481a-a2a5-0ec44a4cef0e
แคปชันสำหรับ Facebook
เคยสั่ง LLM ให้ตอบเป็น JSON แล้วค่าหลุด schema อยู่บ่อยไหมครับ TypeLLM ตั้งใจแก้ตรงนี้ โดยบังคับให้คำตอบอยู่ใน type ที่เรากำหนด โดยไม่ต้องเปลี่ยน architecture หรือ weight ของโมเดล ✅ รองรับ String, Integer, Number, Boolean, Enum, Object และ Array ✅ ค่าไม่หลุดชุดที่อนุญาต รวมถึง enum และ boolean ✅ เปิด thinking ได้เฉพาะ field ที่ต้องคิด ที่เหลือตอบเร็ว ประหยัด token ✅ มี depends_on และ when ให้ field ตอบเมื่อเงื่อนไขผ่าน ✅ ผลบน JevBench 231 งาน: 195/231 แบบไม่คิด และ 228/231 แบบเปิด thinking ✅ มี API แบบ hosted ให้ลองได้ (README ระบุ free credit $5) หรือ self-host ผ่าน SGLang เหมาะกับงานดึงข้อมูลจากเอกสารให้เป็น field ชัดเจน และทีมที่เสิร์ฟ open model ผ่าน SGLang อยู่แล้ว 🔗 https://github.com/TypeLLM/TypeLLM #TypeLLM #StructuredOutput #LLM #SGLang #Python
ภาพปกแบบ HTML สด
แสดงจาก HTML ต้นฉบับที่ใช้สร้างภาพ ดาวน์โหลดเป็น PNG 1080×1350 ได้ทันที
สไตล์ Easy AI
1080×1350แก้ไข 2026-10-11
สไตล์ easy-app
1080×1350แก้ไข 2026-10-11
สรุป
TypeLLM เป็นไลบรารี Python ที่ต่อเข้ากับ LLM แบบ autoregressive ที่เสิร์ฟผ่าน SGLang (หรือใช้ API แบบ hosted) เพื่อบังคับให้คำตอบอยู่ใน JSON Schema ที่กำหนด รองรับ string, number, boolean, enum, object และ array พร้อม thinking เฉพาะ field และ conditional fields
คืออะไร
TypeLLM (TypeLLM/TypeLLM) เป็นไลบรารีภาษา Python ที่เอามาเติม type-safe generation ให้กับ LLM แบบ autoregressive ตัวเดิมที่คุณเซิร์ฟอยู่แล้ว โดยไม่ต้องเปลี่ยน architecture หรือ weight README ระบุแนวคิดมาจาก Jev ของ TypeSafe AI และตัวมันเองสร้างบน SGLang
ชนิดคำตอบที่รองรับ: String, Integer, Number, Boolean, Enum choice, Object และ Array
จุดเด่น
- ไม่หลุด schema: ค่าที่ได้กลับมาอยู่ในชุดที่อนุญาตเท่านั้น รวมถึง enum และ boolean
- ประหยัด output token: enum เลือกได้ด้วย single token และตัวเลขใช้ bounded numeric decoding (ถ้าเปิด thinking จะมี token เพิ่ม)
- ใช้ KV caching ของ shared prefix ไม่ต้องประมวลผล context ส่วนที่ซ้ำใหม่
- dependency-aware execution: field ที่ไม่ขึ้นกับกันทำพร้อมกัน ถ้าต้องการลำดับใช้
depends_onสร้าง dependency graph ที่มี prefix reuse แบบ incremental - conditional fields: เขียน
whenให้ field ตอบเฉพาะเมื่อเงื่อนไขผ่าน เช่น"when": {"category": {"confidence": {"gte": 0.8}}} - thinking mode ปิดเป็นค่าเริ่มต้น เปิดเฉพาะ field ที่ต้องคิด พร้อม
thinking_budgetหรือthinking_effort(none, low, medium, high) และ"thinking": "auto"ให้ระบบเลือกระดับเองทุกครั้ง - permutation averaging ลดอคติจากลำดับตัวเลือกใน enum และ boolean อยู่ในค่าเริ่มต้น
- รองรับ image input สำหรับ vision-language model โดยทดสอบกับ Qwen3.8-27B
- มี API แบบ hosted ให้ใช้งานได้เลยโดยไม่ต้องตั้ง GPU เอง (ฟรี 5 ดอลลาร์)
- ผลบน JevBench 231 งานสาธารณะ: ได้ 195/231 แบบไม่ใช้ thinking และ 228/231 แบบใช้ thinking
เหมาะกับใคร
- คนทำระบบดึงข้อมูลจากเอกสาร เช่น receipt, invoice, ticket แล้วอยากได้ค่าเป็น field ที่ชัดเจนพร้อม type กำกับ
- ทีมที่เซิร์ฟ open model ด้วย SGLang อยู่แล้ว และอยากบังคับ output ให้ผ่าน schema โดยไม่ต้องไปแก้ prompt อยู่ทีเดียว
- คนที่อยากทดลองแบบไม่ต้องมีเครื่อง ใช้ TypeLLM API ที่มี free credit ไปก่อนได้
เริ่มต้นใช้งาน
ติดตั้ง:
pip install -U typellm
แบบใช้ API ของ TypeLLM (สร้าง key ที่หน้า API keys ก่อน):
export TYPELLM_API_KEY="tl-sk-..."
import os
from typellm import TypeLLMClient
client = TypeLLMClient(api_key=os.environ["TYPELLM_API_KEY"])
แบบ self-hosted ผ่าน SGLang (ตัวอย่างใช้ Qwen3.8-27B):
from typellm import TypeLLMClient
client = TypeLLMClient(
"http://127.0.0.1:30000",
model="Qwen/Qwen3.8-27B",
)
เรียกใช้งานทั้งสองแบบด้วย request เดียวกัน:
response = client.generate(
context="""
Receipt from Hilton London
Total: £324.50
Employee travelled to London for a client meeting.
""",
questions={
"merchant": {
"type": "string",
"instructions": "Return only the merchant name.",
},
"total": {
"type": "number",
"instructions": "Extract the total amount in GBP.",
},
"expense_type": {
"type": "string",
"enum": ["meal", "travel", "equipment"],
"instructions": "What type of expense is this?",
},
"reimbursable": {
"type": "boolean",
"instructions": "Should this expense be reimbursed?",
},
"confidence": {
"type": "number",
"enum": [0.0, 0.25, 0.5, 0.75, 1.0],
"instructions": "How confident are you?",
},
},
)
print(response.result)
response.result ได้ typed answers, response.thinking ได้เหตุผลของแต่ละ field ที่คิด และ response.usage ได้จำนวน token ของ call นั้น
ข้อควรรู้
- License: Apache License 2.0 ใช้เชิงพาณิชย์ได้ แต่ยังควรอ่านเงื่อนไขตามไฟล์ LICENSE ก่อนนำไปประกอบกับงานจริง
- ความสดใหม่: push ล่าสุด 10 ต.ค. 2026 (ข้อมูล ณ 11 ต.ค. 2026) มี 941 ดาว 61 fork และ open issue 0 ข้อมูล star/fork เป็นค่าจาก FACTS ณ วันดังกล่าว
- อายุโปรเจกต์: สร้างเมื่อ 17 ก.ย. 2026 อายุราวเดือนกว่า ๆ ยังใหม่อยู่ ฟีเจอร์ใน README อัปเดตถึง 8 ต.ค. 2026 (TypeLLM API เปิดใช้งาน)
- ข้อจำกัดด้านข้อความ: free text หยุดที่ 128 tokens และไม่รองรับ
maxLengthถ้าต้องการยาวกว่านี้ให้ใช้text_max_tokens= - ข้อจำกัดด้านโครงสร้าง: enum รับ 2 ถึง 26 ค่า, array ได้ไม่เกิน 50 items ต่อ call และ items ต้องไม่ซ้ำ, ไม่รองรับ arrays of arrays, arrays ที่อยู่ภายใน object,
return_probabilitiesภายใน array และเงื่อนไขwhenที่ทดสอบ object หรือ array - ตัวเลข decode แบบ decimal สูงสุด 32 หลักโดยค่าเริ่มต้น ปรับได้ด้วย
TypeLLMClient(numeric_max_digits=...) - ถ้า self-hosted ต้องเตรียม SGLang และเปิด prefix caching ตามคู่มือ deployment ของโมเดลที่เลือก
🔗 https://github.com/TypeLLM/TypeLLM
ข้อมูลจาก GitHub ณ 11 ต.ค. 2026: ⭐ 941 | ภาษา Python | license Apache-2.0