Quest 33 - Function Calling Implementer
Quest 33: Function Calling Implementer
medium 25 minutes🎯 Learning Objectives
- เข้าใจ concept ของ function calling ใน LLM tool use
- สร้าง function schema ที่มี name, description, parameters ครบถ้วน
- เขียน validate function ที่ตรวจสอบ arguments ได้ถูกต้อง
- เรียนรู้ว่า structured outputs ทำให้ LLM return ข้อมูลที่เชื่อถือได้
📖 Concept: Function Calling ใน LLM
Function calling คือความสามารถที่ให้ LLM เรียกใช้ functions ที่เรากำหนดไว้ได้ แทนที่จะ return แค่ free-form text — LLM จะ return structured data ที่เราใช้ต่อได้ทันที
ลองนึกภาพว่าคุณกำลังสร้าง chatbot ที่ต้อง check ราคาสินค้า แทนที่ LLM จะตอบแบบ “ราคาน่าจะประมาณ 200 บาท” — function calling ทำให้มัน return ข้อมูลแบบ { action: "checkPrice", params: { productId: "ABC" } } ซึ่งเราเอาไปเรียก API จริงได้เลย
นี่คือหัวใจของ structured outputs — การที่ LLM ไม่ได้แค่ “เดา” แต่ return ข้อมูลในรูปแบบที่ program ของเรา validate และใช้งานได้จริง
⚙️ How It Works
โครงสร้างของ Function Schema
ทุก function ที่ LLM จะเรียกใช้ต้องมี schema ที่บอกว่า:
{ name: "getWeather", // ชื่อ function description: "...", // อธิบายว่า function ทำอะไร parameters: { // ข้อมูลที่ต้องการ type: "object", properties: { city: { type: "string", description: "City name" }, units: { type: "string", enum: ["celsius", "fahrenheit"] } }, required: ["city"] }}Flow ของ Function Calling
1. LLM ได้รับ user request ↓2. LLM ตัดสินใจว่าต้องเรียก function ไหน ↓3. LLM return structured output ตาม schema ↓4. Code ของเรา validate arguments ↓5. เรียก function จริง แล้ว return result ให้ LLMการ Validate Arguments
ส่วนสำคัญคือ validate(args) — เราต้องตรวจสอบว่า LLM ส่ง arguments มาครบและถูก type:
function validate(args) { // ตรวจสอบ required fields if (!args.city || typeof args.city !== 'string') return false; // ตรวจสอบ enum values if (args.units && !['celsius', 'fahrenheit'].includes(args.units)) return false; return true;}💡 Example: สร้าง Function Schema
function defineFunction(name, description, parameters) { return { name, description, parameters, validate(args) { if (typeof args !== 'object' || args === null) return false;
// ตรวจสอบ required parameters const required = parameters.required || []; for (const field of required) { if (!(field in args)) return false; }
// ตรวจสอบ types for (const [key, schema] of Object.entries(parameters.properties || {})) { if (key in args) { if (schema.type === 'string' && typeof args[key] !== 'string') return false; if (schema.type === 'number' && typeof args[key] !== 'number') return false; if (schema.enum && !schema.enum.includes(args[key])) return false; } }
return true; } };}
// ตัวอย่างการใช้งานconst getWeather = defineFunction( 'getWeather', 'Get current weather for a city', { type: 'object', properties: { city: { type: 'string', description: 'City name' }, units: { type: 'string', enum: ['celsius', 'fahrenheit'] } }, required: ['city'] });
getWeather.validate({ city: 'Bangkok' }); // true ✅getWeather.validate({ city: 'Bangkok', units: 'celsius' }); // true ✅getWeather.validate({ units: 'celsius' }); // false ❌ (city missing)getWeather.validate({ city: 123 }); // false ❌ (wrong type)⚠️ Common Mistakes
Mistake 1: ไม่ validate required fields
“ส่ง parameters ไปแล้ว LLM น่าจะส่งมาครบ” → LLM บางครั้งจะ omit required fields หรือส่ง null — ต้อง validate เสมอ
Mistake 2: ไม่ตรวจสอบ enum values
“ถ้า LLM ส่ง string มา ก็ปล่อยผ่าน” → LLM อาจส่งค่าที่ไม่อยู่ใน enum — เช่น ส่ง “farenheit” แทน “fahrenheit”
Mistake 3: ลืม return validate function
“schema ดูดีแล้ว ไม่ต้องมี validate ก็ได้” → Validate คือ safety net — ถ้าไม่มี invalid arguments จะเข้า system จริงและทำให้เกิด bugs
Mistake 4: ทำ schema กำกวมเกินไป
“description ยาวๆ ดีกว่าสั้นๆ” → LLM เข้าใจ description ที่กระชับและชัดเจนได้ดีกว่า — ระบุ parameter types และ constraints ให้ครบ
📝 Knowledge Check
📝 Knowledge Check
Q1:Function calling ใน LLM มีจุดประสงค์หลักคืออะไร?
Q2:ทำไมต้องมี validate function ใน function schema?
Q3:LLM มักจะส่ง error แบบไหนบ่อยที่สุดในการ function calling?
🏋️ Quest: Function Calling Implementer
พร้อมแล้วลุยเลย! Implement function calling สำหรับ LLM tool use
-
Download ไฟล์เริ่มต้นของ quest:
Terminal window npx bluebeltdojo download quest-33-function-callingcd quest-33-function-calling -
เปิด
problem.jsใน editor ของคุณพร้อมความช่วยเหลือของ AI -
Implement
defineFunction(name, description, parameters)ที่ return{ name, description, parameters, validate(args) } -
ตรวจสอบ solution ของคุณ:
Terminal window node test.js -
เมื่อ tests ผ่านทั้งหมด ส่งคำตอบ:
Terminal window npx bluebeltdojo submit
💡 Tip: อ่าน Learning Objectives ข้างบนอีกครั้ง — นี่คือ skill ที่ใช้ได้จริงในการสร้าง AI agents
คำใบ้
- ดูตัวอย่าง function schema ใน section “How It Works”
- อย่าลืม validate
requiredfields และtypechecking - ถ้าติดขัด ลอง test กับ inputs หลายแบบ — ทั้ง valid และ invalid