ข้อกำหนดเบื้องต้น
ก่อนเริ่มต้นการตั้งค่า โปรดตรวจสอบว่าคุณมีสิ่งต่อไปนี้:
1. บัญชี z.ai พร้อม GLM Coding Plan
- ต้องมีบัญชี z.ai ที่สมัครสมาชิกแล้ว
- ต้องสมัคร GLM Coding Plan subscription
- สามารถสมัครได้ที่: https://z.ai
2. OpenCode ติดตั้งในระบบ
- ดาวน์โหลด OpenCode จากเว็บไซต์ทางการ
- รองรับ Windows, macOS และ Linux
3. API Key จาก z.ai
- ต้องได้รับ API Key จาก z.ai dashboard
- API Key จะใช้ในการยืนยันตัวตนกับ GLM API
การดาวน์โหลดและติดตั้ง
ดาวน์โหลดจากเว็บไซต์ทางการ
URL ดาวน์โหลด: https://opencode.ai (หรือตรวจสอบ URL ทางการล่าสุด)
การติดตั้งบน macOS
# ติดตั้งผ่าน Homebrew (แนะนำ)
brew install opencode
# หรือดาวน์โหลดไฟล์ .dmg จากเว็บไซต์
# จากนั้นลาก OpenCode ไปใส่ใน Applications folder
การติดตั้งบน Windows
# ติดตั้งผ่าน winget
winget install OpenCode
# หรือดาวน์โหลดไฟล์ .exe จากเว็บไซต์
# จากนั้นรันตัวติดตั้งและทำตามขั้นตอน
การติดตั้งบน Linux
# ติดตั้งผ่าน Snap
sudo snap install opencode
# หรือติดตั้งผ่าน AUR (Arch Linux)
yay -S opencode
# หรือดาวน์โหลดไฟล์ .AppImage
chmod +x opencode.AppImage
./opencode.AppImage
ตรวจสอบการติดตั้ง
หลังติดตั้งเสร็จสิ้น ตรวจสอบว่า OpenCode พร้อมใช้งาน:
opencode --version
การตั้งค่า Configuration
API Endpoint
OpenCode จะเชื่อมต่อกับ GLM API ผ่าน endpoint ที่รองรับ Anthropic-compatible format:
https://open.bigmodel.cn/api/anthropic
API Key Setup
API Key สามารถรับได้จาก z.ai Dashboard:
เข้าสู่ระบบที่ https://z.ai
ไปที่ Dashboard > API Keys
คลิก Create New API Key
ตั้งชื่อ API Key (เช่น "OpenCode Production")
คัดลอก API Key ที่ได้ (จะแสดงเพียงครั้งเดียว)
คำเตือน: อย่าเปิดเผย API Key กับบุคคลอื่น หรือ commit ลงใน git repository
การตั้งค่าผ่าน Environment Variable
# เพิ่มใน ~/.bashrc หรือ ~/.zshrc
export OPENCODE_API_KEY="your-api-key-here"
export OPENCODE_API_ENDPOINT="https://open.bigmodel.cn/api/anthropic"
การตั้งค่าผ่าน Configuration File
สร้างไฟล์ configuration ที่ ~/.config/opencode/config.json:
{
"api": {
"endpoint": "https://open.bigmodel.cn/api/anthropic",
"key": "your-api-key-here"
},
"model": {
"default": "glm-4-plus",
"fallback": "glm-4-flash"
},
"settings": {
"maxTokens": 4096,
"temperature": 0.7
}
}
โมเดลที่รองรับ
| โมเดล | คำอธิบาย | ความเร็ว | คุณภาพ |
|---|---|---|---|
| glm-4-plus | โมเดลระดับพรีเมียม เหมาะกับงานซับซ้อน | ปานกลาง | สูงมาก |
| glm-4-flash | โมเดลความเร็วสูง เหมาะกับงานทั่วไป | เร็วมาก | สูง |
| glm-4-air | โมเดลสมดุล เหมาะกับการใช้งานประจำ | เร็ว | ดี |
คู่มือการตั้งค่าทีละขั้นตอน
ขั้นตอนที่ 1: ดาวน์โหลด OpenCode
# macOS
brew install opencode
# Windows
winget install OpenCode
# Linux
sudo snap install opencode
ขั้นตอนที่ 2: เปิด OpenCode
# เปิด OpenCode จาก command line
opencode
# หรือเปิดจาก Applications menu
ขั้นตอนที่ 3: เข้าสู่ Settings
- เปิด OpenCode
- กด
Cmd + ,(macOS) หรือCtrl + ,(Windows/Linux) - เลือก Provider หรือ API Settings
ขั้นตอนที่ 4: กำหนดค่า API Endpoint
ในช่อง API Endpoint ให้กรอก:
https://open.bigmodel.cn/api/anthropic
ขั้นตอนที่ 5: กรอก API Key
- ไปที่ช่อง API Key
- วาง API Key ที่ได้จาก z.ai Dashboard
- กด Save หรือ Verify
# หรือตั้งค่าผ่าน command line
opencode config set api.key "your-api-key-here"
opencode config set api.endpoint "https://open.bigmodel.cn/api/anthropic"
ขั้นตอนที่ 6: เลือกโมเดล
- ไปที่ Model Selection
- เลือกโมเดลที่ต้องการใช้:
glm-4-plus- สำหรับงานที่ต้องการความแม่นยำสูงglm-4-flash- สำหรับการตอบสนองรวดเร็วglm-4-air- สำหรับการใช้งานทั่วไป
# ตั้งค่าโมเดลผ่าน command line
opencode config set model.default "glm-4-plus"
ขั้นตอนที่ 7: ทดสอบการเชื่อมต่อ
# ทดสอบการเชื่อมต่อ
opencode test
# หรือส่งคำถามทดสอบ
opencode ask "Hello, can you help me write a function to calculate factorial?"
หากการทดสอบสำเร็จ คุณจะได้รับการตอบกลับจาก GLM AI
ตัวอย่างการใช้งาน
คำสั่งพื้นฐาน
# เริ่มต้น OpenCode ในโปรเจคปัจจุบัน
opencode init
# ถามคำถามง่ายๆ
opencode ask "How do I reverse a string in Python?"
# ขอให้ AI อธิบายโค้ด
opencode explain path/to/file.py
# ขอให้ AI refactor โค้ด
opencode refactor path/to/file.py
# ขอให้ AI เขียน tests
opencode test path/to/file.py
การใช้งานกับโปรเจคจริง
# เข้าไปในโฟลเดอร์โปรเจค
cd /path/to/your/project
# เริ่ม OpenCode
opencode
# ใน OpenCode session:
# User: วิเคราะห์โครงสร้างโปรเจคนี้ให้หน่อย
# AI: [วิเคราะห์โครงสร้างโปรเจค...]
# User: ช่วยเขียน function สำหรับ validate email
# AI: [เขียนโค้ดให้...]
# User: มี bugs อะไรที่น่าจะเกิดขึ้นในโค้ดนี้บ้าง?
# AI: [วิเคราะห์และแนะนำ...]
ตัวอย่างการเขียนโค้ดร่วมกับ AI
# ขอให้ AI เขียน API endpoint
opencode ask "Create a REST API endpoint for user authentication using Express.js with JWT tokens"
# ขอให้ AI เพิ่ม error handling
opencode ask "Add comprehensive error handling to this code: $(cat server.js)"
# ขอให้ AI เขียน documentation
opencode doc generate path/to/module.js
การใช้งาน Keyboard Shortcuts
| Shortcut | Action |
|---|---|
| Cmd + K / Ctrl + K | เปิด AI chat |
| Cmd + Shift + K / Ctrl + Shift + K | Inline code generation |
| Cmd + I / Ctrl + I | อธิบายโค้ดที่เลือก |
| Cmd + Shift + I / Ctrl + Shift + I | Refactor โค้ดที่เลือก |
| Tab | ยอมรับ suggestion |
| Esc | ปฏิเสธ suggestion |
ตัวอย่างโค้ดที่ AI สร้างให้
# ตัวอย่าง: ขอให้ AI เขียน function คำนวณ factorial
def factorial(n: int) -> int:
"""
คำนวณ factorial ของจำนวนเต็มบวก
Args:
n: จำนวนเต็มบวกที่ต้องการคำนวณ factorial
Returns:
factorial ของ n
Raises:
ValueError: ถ้า n เป็นค่าลบ
"""
if n < 0:
raise ValueError("Factorial is not defined for negative numbers")
if n <= 1:
return 1
return n * factorial(n - 1)
# ตัวอย่างการใช้งาน
if __name__ == "__main__":
print(factorial(5)) # Output: 120
print(factorial(10)) # Output: 3628800
การแก้ไขปัญหาทั่วไป
ปัญหา: API Key Invalid
อาการ:
Error: Invalid API Key
Authentication failed
สาเหตุที่เป็นไปได้:
- API Key ไม่ถูกต้องหรือหมดอายุ
- API Key ถูก copy ผิด (มีช่องว่างเกินมา)
- Subscription หมดอายุ
วิธีแก้ไข:
# 1. ตรวจสอบ API Key format
opencode config get api.key
# 2. ตรวจสอบว่าไม่มีช่องว่าง
# API Key ควรมีลักษณะเช่น: sk-xxxxxxxxxxxxxxxx
# 3. สร้าง API Key ใหม่จาก z.ai Dashboard
# แล้วตั้งค่าใหม่
opencode config set api.key "new-api-key-here"
# 4. ทดสอบการเชื่อมต่อ
opencode test
ปัญหา: Connection Failed
อาการ:
Error: Connection failed
Unable to reach API endpoint
Timeout error
สาเหตุที่เป็นไปได้:
- ปัญหาเครือข่าย
- API Endpoint ผิด
- Firewall บล็อกการเชื่อมต่อ
- Proxy settings ไม่ถูกต้อง
วิธีแก้ไข:
# 1. ตรวจสอบ API Endpoint
opencode config get api.endpoint
# ควรได้: https://open.bigmodel.cn/api/anthropic
# 2. ทดสอบการเชื่อมต่อด้วย curl
curl -I https://open.bigmodel.cn/api/anthropic
# 3. หากใช้ proxy ให้ตั้งค่า
export HTTP_PROXY="http://proxy.example.com:8080"
export HTTPS_PROXY="http://proxy.example.com:8080"
# 4. เพิ่ม timeout ใน configuration
opencode config set api.timeout 60000 # 60 seconds
ปัญหา: Model Not Available
อาการ:
Error: Model not found
Model 'glm-4-plus' is not available
วิธีแก้ไข:
# 1. ตรวจสอบโมเดลที่รองรับ
opencode models list
# 2. ตั้งค่าโมเดลที่ถูกต้อง
opencode config set model.default "glm-4-flash"
# 3. ตรวจสอบ subscription plan
# เข้าไปที่ z.ai Dashboard เพื่อดูโมเดลที่รองรับในแผนของคุณ
ปัญหา: Rate Limit Exceeded
อาการ:
Error: Rate limit exceeded
Too many requests
วิธีแก้ไข:
# 1. รอสักครู่แล้วลองใหม่
# Rate limit มักจะ reset หลังจากผ่านไป 1 นาที
# 2. ตั้งค่า retry logic
opencode config set api.retry.enabled true
opencode config set api.retry.maxAttempts 3
opencode config set api.retry.delay 5000 # 5 seconds
# 3. ตรวจสอบ usage ใน Dashboard
# เข้าไปที่ z.ai Dashboard > Usage เพื่อดูการใช้งาน
ปัญหา: Response Quality Issues
อาการ:
- คำตอบไม่เกี่ยวข้อง
- โค้ดที่สร้างมีปัญหา
- การตอบสนองช้า
วิธีแก้ไข:
# 1. ลองใช้โมเดลที่ทรงพลังกว่า
opencode config set model.default "glm-4-plus"
# 2. ปรับ temperature
opencode config set model.temperature 0.3 # ต่ำลง = แม่นยำกว่า
opencode config set model.temperature 0.9 # สูงขึ้น = สร้างสรรค์กว่า
# 3. เพิ่ม context ในคำถาม
# แทนที่จะถาม: "เขียน function sort"
# ลองถาม: "เขียน function sort สำหรับ list ของ dictionaries
# โดยเรียงตาม key 'name' แบบ case-insensitive"
ตารางสรุปการแก้ไขปัญหา
| ปัญหา | Error Message | วิธีแก้ไขหลัก |
|---|---|---|
| API Key Invalid | Invalid API Key | ตรวจสอบ/สร้าง API Key ใหม่ |
| Connection Failed | Connection failed | ตรวจสอบ network/proxy |
| Model Not Available | Model not found | เปลี่ยนโมเดล/ตรวจสอบ subscription |
| Rate Limit | Rate limit exceeded | รอ/ปรับ retry settings |
| Timeout | Request timeout | เพิ่ม timeout value |
| Insufficient Credits | Insufficient balance | เติมเครดิต/upgrade plan |
ข้อมูลเพิ่มเติม
ลิงก์ที่เป็นประโยชน์
- z.ai Dashboard: https://z.ai/dashboard
- GLM API Documentation: https://open.bigmodel.cn/dev/api
- OpenCode Documentation: https://opencode.ai/docs
- Support: https://z.ai/support
การติดต่อฝ่ายสนับสนุน
หากพบปัญหาที่ไม่สามารถแก้ไขได้จากคู่มือนี้:
- ส่ง email ไปที่: support@z.ai
- หรือใช้ live chat ที่ z.ai Dashboard
- แนบข้อมูลดังนี้:
- Error message ที่แสดง
- Screenshot ของปัญหา
- OpenCode version (
opencode --version) - OS และ version
สรุป
การตั้งค่า OpenCode กับ GLM Coding Plan มีขั้นตอนหลักๆ ดังนี้:
สมัครและรับ API Key จาก z.ai
ติดตั้ง OpenCode บนระบบของคุณ
กำหนดค่า API Endpoint เป็น https://open.bigmodel.cn/api/anthropic
กรอก API Key ใน settings
เลือกโมเดล ที่เหมาะสมกับการใช้งาน
ทดสอบการเชื่อมต่อ เพื่อยืนยันว่าทุกอย่างทำงาน
หลังจากตั้งค่าเสร็จสิ้น คุณสามารถเริ่มใช้งาน OpenCode เพื่อเพิ่มประสิทธิภาพการเขียนโค้ดได้ทันที!