เคยเจอปัญหาข้อความภาษาจีนจาก AI API กลายเป็นตัวอักษรแปลกๆ เช่น ä½ å¥½ หรือ ð\u008c™\u008e¬ หรือ ä¸\u0081ä¸\u008bªå\u008f\u0089 หรือไม่? บทความนี้จะพาคุณวิเคราะห์สาเหตุและแก้ไขปัญหา Chinese encoding ใน AI API อย่างเป็นระบบ
กรณีศึกษา: ระบบ RAG ของบริษัทโลจิสติกส์
ทีมพัฒนาของบริษัทขนส่งรายใหญ่แห่งหนึ่งเพิ่งเปิดตัวระบบ RAG (Retrieval-Augmented Generation) สำหรับค้นหาเอกสารภาษาจีน โดยใช้ HolySheep AI เป็น backend แต่พบว่าคำตอบจาก AI กลายเป็นชุดตัวอักษรเพี้ยนทุกครั้ง ทีมลองตรวจสอบทุกจุดตั้งแต่ chunking เอกสาร, vector search, ไปจนถึง response formatting แต่ก็ยังหาสาเหตุไม่พบ
ปัญหาจริงๆ อยู่ที่ encoding mismatch ระหว่าง Python, HTTP request และ database — บทความนี้จะแสดงให้เห็นว่าปัญหานี้เกิดขึ้นได้อย่างไร และแก้ไขอย่างไร
ทำความเข้าใจปัญหา: Unicode และ Encoding คืออะไร
ก่อนจะไปแก้ปัญหา ต้องเข้าใจพื้นฐานก่อน:
- Unicode — มาตรฐานที่กำหนดตัวเลขเฉพาะ (code point) ให้กับทุกตัวอักษรบนโลก รวมถึงอักษรจีน ภาษาไทย และอีโมจิ
- UTF-8 — วิธีเข้ารหัส Unicode เป็น bytes ที่ใช้กันแพร่หลายที่สุด ตัวอักษรจีน 1 ตัวใช้ 3-4 bytes
- UTF-16, GBK, Big5 — วิธีเข้ารหัสอื่นๆ ที่ใช้กันในบริบทเฉพาะ
- ASCII — รองรับเฉพาะอักษรภาษาอังกฤษ 128 ตัว
เมื่อระบบส่งข้อมูลผ่าน HTTP โดยไม่ระบุ encoding ที่ถูกต้อง bytes ของอักษรจีนจะถูกตีความผิดทำให้เกิดอาการ "乱码" (เลอ เม่า) หรือตัวอักษรเพี้ยนนั่นเอง
การตั้งค่า Encoding สำหรับ HolySheep AI API
ปัญหาที่พบบ่อยที่สุดคือ encoding ไม่ตรงกันระหว่าง client และ server ด้านล่างนี้คือวิธีแก้ไขสำหรับภาษาโปรแกรมยอดนิยม
Python — requests และ openai SDK
import requests
import json
วิธีที่ 1: ตั้งค่า Content-Type พร้อม charset
headers = {
"Authorization": "Bearer YOUR_HOLYSHEEP_API_KEY",
"Content-Type": "application/json; charset=utf-8"
}
payload = {
"model": "gpt-4.1",
"messages": [
{"role": "user", "content": "解释机器学习中的过拟合问题"}
]
}
response = requests.post(
"https://api.holysheep.ai/v1/chat/completions",
headers=headers,
json=payload,
timeout=30
)
ตรวจสอบว่า response เป็น UTF-8
response.encoding = "utf-8"
result = response.json()
print(result["choices"][0]["message"]["content"])
Python — openai SDK
from openai import OpenAI
ตั้งค่า base_url เป็น HolySheep
client = OpenAI(
api_key="YOUR_HOLYSHEEP_API_KEY",
base_url="https://api.holysheep.ai/v1"
)
ส่ง prompt ภาษาจีน
response = client.chat.completions.create(
model="gpt-4.1",
messages=[
{"role": "user", "content": "请用中文解释什么是Transformer架构"}
],
timeout=30
)
อ่าน response เป็น UTF-8
content = response.choices[0].message.content
print(content) # ควรแสดงภาษาจีนถูกต้อง
Node.js — fetch API
const response = await fetch("https://api.holysheep.ai/v1/chat/completions", {
method: "POST",
headers: {
"Authorization": "Bearer YOUR_HOLYSHEEP_API_KEY",
"Content-Type": "application/json; charset=utf-8"
},
body: JSON.stringify({
model: "gpt-4.1",
messages: [
{ role: "user", content: "用中文介绍区块链技术" }
]
})
});
const data = await response.json();
console.log(data.choices[0].message.content);
Node.js — openai SDK
import OpenAI from "openai";
const client = new OpenAI({
apiKey: "YOUR_HOLYSHEEP_API_KEY",
baseURL: "https://api.holysheep.ai/v1"
});
async function askInChinese() {
const response = await client.chat.completions.create({
model: "gpt-4.1",
messages: [
{ role: "user", content: "请解释什么是迁移学习" }
],
timeout: 30000
});
return response.choices[0].message.content;
}
askInChinese().then(console.log);
ข้อผิดพลาดที่พบบ่อยและวิธีแก้ไข
ข้อผิดพลาดที่ 1: Content-Type ไม่มี charset
อาการ: Response เป็นตัวอักษรเพี้ยน เช่น ä½ å¥½
สาเหตุ: Header Content-Type: application/json ไม่ระบุ charset ทำให้ server/client ใช้ default encoding ผิด
วิธีแก้ไข: เปลี่ยนเป็น Content-Type: application/json; charset=utf-8
# ❌ ไม่ถูกต้อง
headers = {"Content-Type": "application/json"}
✅ ถูกต้อง
headers = {"Content-Type": "application/json; charset=utf-8"}
ข้อผิดพลาดที่ 2: อ่านไฟล์ด้วย encoding ผิด
อาการ: ข้อความจีนจากไฟล์ที่อ่านเข้ามามีปัญหา แม้ API จะตอบถูกต้อง
สาเหตุ: Python อ่านไฟล์ด้วย encoding ของระบบ (cp1252 บน Windows) แทน UTF-8
วิธีแก้ไข: ระบุ encoding อย่างชัดเจนเมื่ออ่านไฟล์
# ❌ อาจใช้ encoding ผิด
with open("data.json", "r") as f:
data = json.load(f)
✅ ระบุ UTF-8 ชัดเจน
with open("data.json", "r", encoding="utf-8") as f:
data = json.load(f)
หรือใช้ pathlib ที่รองรับ UTF-8 ดีกว่า
from pathlib import Path
content = Path("data.json").read_text(encoding="utf-8")
ข้อผิดพลาดที่ 3: บันทึกไฟล์ด้วย encoding ผิด
อาการ: ไฟล์ output ที่บันทึกออกมาภาษาจีนเพี้ยน แม้ในโค้ดจะถูกต้อง
สาเหตุ: ไม่ได้ระบุ encoding ตอนเขียนไฟล์ ทำให้ Windows ใช้ cp1252 หรือ GBK
วิธีแก้ไข: ระบุ encoding เมื่อเขียนไฟล์เสมอ
# ❌ อาจบันทึกเป็น encoding ผิด
with open("output.txt", "w") as f:
f.write(response_text)
✅ ระบุ UTF-8 ตอนเขียน
with open("output.txt", "w", encoding="utf-8") as f:
f.write(response_text)
หรือใช้ pathlib
from pathlib import Path
Path("output.txt").write_text(response_text, encoding="utf-8")
ข้อผิดพลาดที่ 4: Database encoding mismatch
อาการ: ข้อมูลภาษาจีนที่บันทึกลง database แสดงผลเพี้ยน แม้จะแสดงผ่าน API ได้ถูกต้อง
สาเหตุ: Database connection ใช้ encoding ผิด (เช่น latin1, utf8mb4 แต่ตั้งค่าผิด)
วิธีแก้ไข: ตั้งค่า charset ตอนสร้าง connection
# MySQL/MariaDB
import pymysql
connection = pymysql.connect(
host="localhost",
user="root",
password="password",
database="app_db",
charset="utf8mb4", # รองรับอีโมจิและอักษรจีน
cursorclass=pymysql.cursors.DictCursor
)
PostgreSQL
import psycopg2
connection = psycopg2.connect(
host="localhost",
user="postgres",
password="password",
database