# วิธีใช้ Unlid ใน LiteLLM

เชื่อม LiteLLM กับ Unlid: สร้าง API key ตั้ง base URL ด้านล่างใน LiteLLM แล้วเลือก model id ทุกโมเดลของ Unlid ใช้ค่าเดียวกัน

## ใช้ Unlid ในแอปของคุณ

แอปใดก็ตามที่เพิ่ม provider แบบ **OpenAI-compatible** ได้ จะใช้โมเดลของ Unlid ได้ทุกตัว สิ่งที่ต้องใช้มีแค่ 3 อย่างเสมอ:

|  |  |
| --- | --- |
| Base URL | `https://api.unlid.ai/v1` |
| API key | สร้างได้ที่ [บัญชี → API keys](https://unlid.ai/account/keys) ขึ้นต้นด้วย `ul-` และแสดงเพียงครั้งเดียว |
| โมเดล | id จากหน้า [โมเดล](https://unlid.ai/models.md?lang=th) เช่น `glm-5.3-flash-uncensored` แอปส่วนใหญ่ดึงรายการให้เอง |

กฎ 3 ข้อนี้ครอบคลุมปัญหาการตั้งค่าเกือบทั้งหมด:

- กรอก base URL **ให้มี** `/v1` และ **ไม่ต้องมี** `/chat/completions` — แอปจะเติมให้เอง (ยกเว้น Janitor AI และ goose ดูด้านล่าง)
- เมื่อแอปถามประเภท API ให้เลือก **OpenAI Compatible / Chat Completions** ซึ่งเป็นโปรโตคอลหลักของ Unlid ส่วน endpoint แบบ Anthropic (`/v1/messages`) และ OpenAI Responses (`/v1/responses`) มีไว้สำหรับ [Claude Code](https://unlid.ai/docs/clients/claude-code.md?lang=th) และ [Codex](https://unlid.ai/docs/clients/codex-cli.md?lang=th) ที่ไม่รองรับโปรโตคอลอื่น ทั้งสองถูกแปลงเป็น chat completions และรองรับฟีเจอร์น้อยกว่า
- หากแอปมีการตั้งค่า **context window** ให้กรอกตามหน้าโมเดล (glm-5.3-flash-uncensored: 262,144 token) ค่าเริ่มต้นที่เล็กเกินไปจะตัดแชตยาว ส่วนค่าที่ใหญ่เกินไปจะจบที่ `context_length_exceeded`

ไม่แน่ใจว่าคีย์ใช้ได้ไหม? รันคำสั่งนี้จากเครื่องเดียวกันก่อน — เป็นวิธีที่เร็วที่สุดในการแยกปัญหาของแอปออกจากปัญหาเครือข่าย:

```
curl https://api.unlid.ai/v1/models -H "Authorization: Bearer ul-…"
```

## LiteLLM

```
import litellm
r = litellm.completion(model="openai/glm-5.3-flash-uncensored", api_base="https://api.unlid.ai/v1", api_key="ul-…",
                       messages=[{"role": "user", "content": "Hello"}])
```

ใน `config.yaml` ของ LiteLLM proxy ใช้ `model: openai/glm-5.3-flash-uncensored` พร้อม `api_base: https://api.unlid.ai/v1` prefix `openai/` คือสิ่งที่ทำให้ LiteLLM ใช้ Chat Completions

มีปัญหา? ดู[การแก้ปัญหา](https://unlid.ai/docs/clients.md?lang=th#troubleshooting) หรือ [ตั้งค่าแอปอื่น](https://unlid.ai/docs/clients.md?lang=th)
