API Reference
API ทั้งหมดของ Nara AI
ทุก method, parameter, และ error code ที่คุณต้องรู้
NaraAI.from_base(base)
โหลดโมเดลพื้นฐานเข้า memory ครั้งเดียว แล้วเสียบส่วนขยาย LoRA:
from nara_ai import NaraAI
model = NaraAI.from_base(
base="gemma-4-12b", # โมเดลพื้นฐาน (12B ตัวเดียว)
device="auto", # auto / cpu / cuda / mps
quantization="auto", # auto / int8 / int4 / none
cache_dir="~/.nara" # ที่เก็บโมเดล
)
model.attach_lora("cpt-th") # เสียบส่วนขยายภาษาไทย model.ask(prompt, **kwargs)
ส่งคำสั่งเดี่ยวและรอคำตอบจนเสร็จ:
reply = model.ask(
"สรุปบทความนี้: ...",
max_tokens=200,
temperature=0.3,
system_prompt="คุณเป็นนักสรุป",
stop=["\n\n", "จบ"]
)
# คืนค่า: string Streaming responses
รับคำตอบทีละส่วน สำหรับ UI ที่ต้องการแสดงผลแบบเรียลไทม์:
# Python
for chunk in model.stream("เล่าเรื่องสั้นภาษาไทย"):
print(chunk, end="", flush=True)
# TypeScript
for await (const chunk of model.stream("เล่าเรื่องสั้น")) {
process.stdout.write(chunk);
} Parameters
| ชื่อ | ประเภท | ค่าเริ่มต้น | คำอธิบาย |
|---|---|---|---|
base | string | gemma-4-12b | โมเดลพื้นฐาน (12B ตัวเดียว) |
loras | string[] | ["cpt-th"] | รายการส่วนขยาย LoRA ที่จะเสียบ (เช่น cpt-th, sft, agent) |
max_tokens | int | 512 | จำนวนโทเคนสูงสุดที่จะสร้าง |
temperature | float | 0.7 | 0 = แม่นยำซ้ำซาก, 1 = สร้างสรรค์, 2 = สุ่มมาก |
top_p | float | 0.9 | นิวเคลียสแซมปลิง จำกัดความน่าจะเป็นสะสม |
system_prompt | string | null | กำหนดบทบาท/พฤติกรรมของโมเดล |
stream | bool | false | ส่งคำตอบทีละส่วน (streaming) แทนที่รอทีเดียวจบ |
stop | string[] | null | รายการคำที่เมื่อเจอแล้วให้หยุดสร้าง |
REST API
ถ้าใช้ HTTP ตรงๆ (เช่นจากภาษาอื่น):
POST /v1/chat
Authorization: Bearer NARA_API_KEY
Content-Type: application/json
{
"model": "gemma-4-12b+cpt-th",
"messages": [
{"role": "system", "content": "คุณเป็นผู้ช่วยภาษาไทย"},
{"role": "user", "content": "สวัสดีครับ"}
],
"max_tokens": 256,
"temperature": 0.7,
"stream": false
} Response:
{
"id": "chat_abc123",
"model": "gemma-4-12b+cpt-th",
"choices": [{
"message": {"role": "assistant", "content": "สวัสดีค่ะ! มีอะไรให้ช่วยไหมคะ?"},
"finish_reason": "stop"
}],
"usage": {"prompt_tokens": 18, "completion_tokens": 12, "total_tokens": 30}
} Error codes
| HTTP | Error | คำอธิบาย |
|---|---|---|
400 | INVALID_REQUEST | พารามิเตอร์ไม่ถูกต้อง เช่น max_tokens เกินขีดจำกัด |
401 | UNAUTHORIZED | API key ไม่ถูกต้องหรือหมดอายุ |
429 | RATE_LIMITED | เรียกเกิน rate limit รอแล้วลองใหม่ |
500 | MODEL_ERROR | โมเดลเจอปัญหาขณะประมวลผล ลองอีกครั้ง |
503 | MODEL_LOADING | โมเดลกำลังโหลดเข้า memory รอสักครู่ |
Rate limits
- Free tier: 60 requests/นาที, 1,000 requests/วัน
- Pro tier: 600 requests/นาที, ไม่จำกัดรายวัน
- Self-hosted: ไม่จำกัด (รันบนเครื่องคุณ)