GLM-5.3-Flash เข้ากันได้กับ OpenAI ดังนั้นคุณสามารถใช้ไคลเอ็นต์เดิมได้โดยเปลี่ยนเพียง Base URL และ Model ID จุดสำคัญคือรองรับอินพุตรูปภาพร่วมกับข้อความในคำขอเดียว ซึ่งต้องส่งเป็น content blocks ที่มีรูปแบบเฉพาะ
คู่มือนี้ครอบคลุมการขอคีย์ การส่งข้อความและรูปภาพ การตั้งค่า reasoning effort การสตรีม และ tool calling โดยทุกตัวอย่างใช้โมเดล glm-5.3-flash
หากต้องการข้อมูลพื้นฐานก่อนเริ่มใช้งาน ดู คำอธิบาย GLM-5.3-Flash ของเรา หากใช้งานโมเดลรุ่นใหญ่กว่าอยู่แล้ว ดู คู่มือ API ของ GLM-5.3 โปรดทราบว่า Model ID ราคา และเส้นทางการรับรูปภาพของสองโมเดลต่างกัน
รับ API key
สร้างบัญชีที่ z.ai เปิดหน้า API keys แล้วสร้างคีย์ เก็บคีย์ไว้ใน environment variable ไม่ใช่ในซอร์สโค้ด
export ZAI_API_KEY="your-key-here"
Base URL ของ Standard API คือ:
https://api.z.ai/api/paas/v4/
หากคุณเชื่อมต่อ Claude Code หรือ Cline จะใช้ Base URL สำหรับแผนการเขียนโค้ดโดยเฉพาะ ดู คู่มือ Claude Code และ Cline
เรียกใช้งานครั้งแรก
ปลายทางนี้เข้ากันได้กับ OpenAI SDK จึงใช้งานได้โดยเปลี่ยน Base URL และ Model ID
from openai import OpenAI
import os
client = OpenAI(
[REDACTED CREDENTIAL],
base_url="https://api.z.ai/api/paas/v4/",
)
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[
{"role": "user", "content": "Explain what a KV cache is in two sentences."}
],
)
print(response.choices[0].message.content)
ตัวอย่าง curl:
curl https://api.z.ai/api/paas/v4/chat/completions \
-H "Authorization: Bearer $ZAI_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "glm-5.3-flash",
"messages": [
{"role": "user", "content": "Explain what a KV cache is in two sentences."}
]
}'
ตัวอย่าง Node.js:
import OpenAI from "openai";
const client = new OpenAI({
[REDACTED CREDENTIAL],
baseURL: "https://api.z.ai/api/paas/v4/",
});
const response = await client.chat.completions.create({
model: "glm-5.3-flash",
messages: [
{ role: "user", content: "Explain what a KV cache is in two sentences." },
],
});
console.log(response.choices[0].message.content);
นอกจาก Base URL และ Model ID แล้ว ไม่มีสิ่งที่เฉพาะเจาะจงกับ GLM ทำให้คุณสามารถเปรียบเทียบโมเดลกับ workload จริงได้ง่าย
ส่งรูปภาพ
ต่างจาก GLM-5.3, GLM-5.3-Flash รับรูปภาพผ่าน content blocks โดยเปลี่ยน content จากสตริงเป็นอาร์เรย์
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[
{
"role": "user",
"content": [
{
"type": "text",
"text": "This screenshot shows a rendering bug. What is wrong with the layout?",
},
{
"type": "image_url",
"image_url": {
"url": "https://example.com/screenshots/broken-layout.png"
},
},
],
}
],
)
กฎสำคัญมี 3 ข้อ:
-
urlรับได้ทั้ง public URL และ Base64 data URL สำหรับไฟล์โลคัลหรือส่วนตัว ให้เข้ารหัสก่อนส่ง
import base64
with open("broken-layout.png", "rb") as f:
encoded = base64.b64encode(f.read()).decode("utf-8")
image_block = {
"type": "image_url",
"image_url": {"url": f"data:image/png;base64,{encoded}"},
}
-
หลายรูปภาพต้องส่งเป็นหลาย blocks หากต้องการเปรียบเทียบดีไซน์กับผลลัพธ์จริง ให้ใส่
image_urlแยกกันใน content เดียวกัน
content = [
{"type": "text", "text": "Does the second image match the design in the first?"},
{"type": "image_url", "image_url": {"url": design_data_url}},
{"type": "image_url", "image_url": {"url": built_data_url}},
]
- ลำดับมีผลต่อความเข้าใจ วางข้อความอธิบายงานก่อนรูปภาพที่เกี่ยวข้อง เช่น “เปรียบเทียบสองสิ่งนี้” แล้วตามด้วยภาพสองรูป
เอกสารของ Z.ai ยังระบุการส่งวิดีโอและไฟล์ด้วยกลไก content blocks เดียวกัน แต่ควรทดสอบกับสื่อจริงก่อนนำไปใช้ใน production
สำหรับ workflow ด้าน vision เช่น เปลี่ยน screenshot เป็นโค้ด หรือส่งรูปภาพพร้อมเอกสารยาวใน context 1M tokens ดู คู่มือ Vision API ของ GLM-5.3-Flash
ควบคุม reasoning effort
GLM-5.3-Flash มีสามระดับผ่าน reasoning_effort
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[{"role": "user", "content": "Refactor this function for clarity."}],
extra_body={"reasoning_effort": "low"},
)
ค่าที่ใช้ได้คือ low, high และ max โดยค่าเริ่มต้นคือ max ซึ่งมีต้นทุนสูงสุด สำหรับงานจัดประเภทหรือแยกข้อมูลจำนวนมาก ให้ตั้งค่า low เพื่อลด output tokens
ระดับ low เป็นความแตกต่างจาก GLM-5.2 ซึ่งมีเพียง High และ Max เท่านั้น เมื่อใช้ OpenAI Python SDK ให้ส่ง reasoning_effort ใน extra_body; หากเรียก curl โดยตรง จะเป็นฟิลด์ระดับบนสุดของ JSON
พารามิเตอร์การสุ่มตัวอย่างที่แนะนำ
| กรณีการใช้งาน | temperature |
top_p |
|---|---|---|
| ทั่วไป | 1.0 | 0.95 |
| การเขียนโค้ด | 0.95 | 1.0 |
ค่าแตกต่างกันไม่มาก แต่หากผลลัพธ์โค้ดไม่สม่ำเสมอ ให้เริ่มทดสอบด้วยโปรไฟล์การเขียนโค้ด
สตรีมคำตอบ
ใช้ streaming ตามมาตรฐาน OpenAI ได้โดยตรง
stream = client.chat.completions.create(
model="glm-5.3-flash",
messages=[{"role": "user", "content": "Write a bash script that rotates logs."}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)
จากการวิเคราะห์ของ Artificial Analysis โมเดลสร้างได้ประมาณ 49 tokens/วินาที เทียบกับ GLM-5.3 ที่ประมาณ 86 tokens/วินาที แต่ time-to-first-token อยู่ที่ 1.52 วินาที จึงเริ่มตอบเร็วและส่งผลลัพธ์ต่อเนื่อง เหมาะกับ UI แบบ streaming มากกว่างานสร้างเอกสารยาวที่ต้องคำนึงถึงเวลา
เรียกใช้เครื่องมือ
Tool calling ใช้ schema มาตรฐาน OpenAI
tools = [
{
"type": "function",
"function": {
"name": "get_deployment_status",
"description": "Returns the current status of a named deployment.",
"parameters": {
"type": "object",
"properties": {
"service": {
"type": "string",
"description": "The service name, for example 'checkout-api'.",
}
},
"required": ["service"],
},
},
}
]
response = client.chat.completions.create(
model="glm-5.3-flash",
messages=[{"role": "user", "content": "Is checkout-api healthy?"}],
tools=tools,
)
call = response.choices[0].message.tool_calls[0]
print(call.function.name, call.function.arguments)
ผลการทดสอบ Agentic ที่ Z.ai เผยแพร่ระบุว่า AutomationBench ได้ 48.8 เทียบกับ 26.2 ของ GLM-5.2 แม้เป็นตัวเลขจากผู้ให้บริการ แต่สอดคล้องกับการปรับโมเดลให้เหมาะกับลูป tool calling
หากต้องการสร้าง tool definitions จาก OpenAPI ของคุณ ดู วิธีเปลี่ยน OpenAPI specification เป็นเครื่องมือของ Agent
จัดการข้อผิดพลาดที่ควรมี
ปัญหา production ที่พบบ่อยมี 3 แบบ
Rate limits
ใช้ exponential backoff พร้อม jitter เพื่อหลีกเลี่ยงการ retry พร้อมกันจาก worker หลายตัว
import time, random
from openai import RateLimitError
def call_with_retry(**kwargs):
for attempt in range(5):
try:
return client.chat.completions.create(**kwargs)
except RateLimitError:
if attempt == 4:
raise
time.sleep((2 ** attempt) + random.random())
Context overflow
แม้ context window จะมีขนาด 1M tokens แต่เอกสารยาวและรูปภาพความละเอียดสูงก็ใช้ context ได้มาก ควรติดตาม token budget ระหว่างประกอบ prompt ไม่ใช่รอจน request ล้มเหลว
Truncated output
หากคำตอบจบกลางประโยค ให้ตรวจสอบ finish_reason หากค่าเป็น length แปลว่าชน output limit ไม่ใช่โมเดลหยุดตอบเอง เนื่องจากแหล่งข้อมูลระบุขีดจำกัด output ไม่ตรงกัน ควรตรวจสอบกับผู้ให้บริการที่ใช้งานจริง
อ่าน token usage
ทุก response มี usage ซึ่งเป็นข้อมูลที่เชื่อถือได้ที่สุดสำหรับคำนวณต้นทุน
print(response.usage.prompt_tokens, response.usage.completion_tokens)
ตรวจสอบ completion_tokens เป็นพิเศษ เมื่อใช้ค่าเริ่มต้น reasoning_effort="max" reasoning tokens จะถูกคิดเป็น output tokens ดังนั้นคำตอบที่มองเห็นสั้นอาจมี completion count สูง เปรียบเทียบตัวเลขนี้ระหว่าง reasoning levels กับ prompt จริงของคุณเพื่อเลือกค่าที่เหมาะสม
ค่าใช้จ่าย
ราคาปกติคือ:
- Input: $0.15 ต่อ 1 ล้าน tokens
- Output: $0.50 ต่อ 1 ล้าน tokens
- Cached input: $0.03 ต่อ 1 ล้าน tokens
ส่วนลดเปิดตัว 50% มีผลถึง 9 กันยายน 2026 ทำให้ราคาเหลือ $0.075, $0.25 และ $0.015 ตามลำดับ
ผู้ให้บริการอื่น เช่น OpenRouter, Cloudflare Workers AI, Vercel AI Gateway และ DeepInfra อาจคิดราคาแตกต่างกัน ดู การวิเคราะห์ราคาของเรา และตรวจสอบราคากับผู้ให้บริการจริงก่อนวางงบประมาณ
ทดสอบการผสานรวม
Multimodal payload โดยเฉพาะ Base64 image block เขียนและทดสอบซ้ำด้วย curl ได้ยาก อีกทั้งการเปลี่ยนโมเดลอาจเปลี่ยนรูปแบบ response แบบเงียบ ๆ
Apidog ช่วยจัดการทั้งสองกรณีได้: บันทึก text, image และ tool calls เป็น collection, เพิ่ม assertions ให้กับ response fields ที่แอปของคุณใช้งานจริง และเก็บ API key ใน environment variables เมื่อส่วนลดเปิดตัวสิ้นสุดลง คุณสามารถเปลี่ยน Model ID จุดเดียว แล้วรันชุดทดสอบเดิมกับ Flash และ GLM-5.3 เพื่อเปรียบเทียบผลลัพธ์ได้
วิธีนี้เปลี่ยนการย้ายโมเดลให้เป็น diff ที่ตรวจสอบได้ แทนที่จะเป็นการเปลี่ยนแปลงที่ต้องคาดหวังว่าจะทำงาน
คำถามที่พบบ่อย
Model ID ที่ถูกต้องคืออะไร? ใช้ glm-5.3-flash บน Z.ai API และใช้ z-ai/glm-5.3-flash บน OpenRouter
OpenAI SDK ใช้งานได้โดยไม่ต้องแก้โค้ดหรือไม่? ได้สำหรับ chat completions, streaming และ tool calling ส่วนพารามิเตอร์นอก schema มาตรฐาน เช่น reasoning_effort ต้องส่งผ่าน extra_body ใน Python SDK
ส่งรูปภาพได้กี่รูปต่อ request? ส่งได้หลายรูป โดยแต่ละรูปต้องเป็น image_url block ของตัวเอง ขีดจำกัดจริงขึ้นกับ context budget มากกว่าจำนวนรูปที่กำหนดตายตัว
ทำไมคำตอบยาวและช้า? เพราะ reasoning_effort มีค่าเริ่มต้นเป็น max สำหรับงานที่ไม่ต้องใช้การไตร่ตรองมาก ให้ตั้งค่าเป็น low
Output ยาวสูงสุดเท่าไร? แหล่งข้อมูลไม่ตรงกัน: OpenRouter ระบุ 131,072 tokens ส่วน Hugging Face card ระบุ 163,840 tokens ตรวจสอบกับผู้ให้บริการก่อนพึ่งพาการสร้าง output ขนาดใหญ่มาก

Top comments (0)