Category: AI

  • Claude Code Subagent คืออะไร? วิธีสร้างและใช้งาน Subagent อย่างมีประสิทธิภาพ

    Claude Code Subagent คืออะไร? วิธีสร้างและใช้งาน Subagent อย่างมีประสิทธิภาพ

    Claude Code เป็น AI coding assistant ที่ทำงานกับไฟล์บนเครื่องของเราได้โดยตรง ช่วยลดเวลาในการเปลี่ยนหน้าจอในการทำงาน และทุ่นแรงในการเขียนและรีวิว code ได้

    ความโดดเด่นหนึ่งของ Claude คือ subagent ซึ่งช่วยให้ Claude ทำงานที่หลากหลายและซับซ้อนได้ดีขึ้น

    ในบทความนี้ ผมจะพาทุกคนไปรู้จักกับ subagent ตั้งแต่ subagent คืออะไร ใช้ยังไง การใช้งานที่ควรและไม่ควรใช้ subagent

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย



    🤖 Subagent มีไว้ทำไม?

    Subagent คือ AI agent ที่จะทำงานเฉพาะอย่างที่ Claude สั่ง

    เวลาถูกเรียกใช้งาน subagent จะทำงานใน context หรือความจำของตัวเอง แล้วส่งผลลัพธ์กลับไปให้ Claude

    เช่น:

    เราสั่งให้ Claude อธิบาย code
    Claude สั่งให้ subagent อ่านไฟล์
    Subagent อ่านและทำความเข้าใจไฟล์
    Subagent สรุปข้อมูลและส่งให้ Claude
    Claude ประมวลผลและตอบคำถามเรา

    ข้อดี คือ ประหยัด context ในการทำงาน เพราะ Claude ไม่ต้องเก็บข้อมูลที่ใช้ในการทำงานทั้งหมด (เช่น ไฟล์ที่ถูกอ่าน) แต่จะเก็บเฉพาะผลลัพธ์ที่ subagent ส่งกลับมา

    แต่ข้อเสีย คือ เราจะไม่รู้ว่า ผลลัพธ์ที่ได้มาเกิดขึ้นยังไงบ้าง เพราะ Claude จะไม่เห็นการทำงานหรือข้อมูลที่ subagent เห็น


    🦾 วิธีสร้าง Subagent

    เราสร้าง subagent ได้ 2 วิธี:

    1. ให้ Claude สร้างให้ (แนะนำ)
    2. สร้างเอง

    ตัวอย่างคำสั่งสร้าง subagent ด้วย Claude:

    สร้าง agent ที่รีวิว code และแนะนำวิธีแก้ให้หน่อย

    📑 โครงสร้างไฟล์ MD ของ Subagent

    ข้อมูลของ subagent จะถูกเก็บไว้ในไฟล์ markdown (MD) เช่น code-reviewer.md และประกอบด้วย 6 ส่วน:

    1. name
    2. description
    3. tools (optional)
    4. model (optional)
    5. color (optional)
    6. Main body

    ตัวอย่างไฟล์ MD:

    ---
    name: code-reviewer
    description: Reviews code for quality, security, and maintainability issues. Use proactively after writing or modifying code, before committing.
    tools: Read, Grep, Glob
    model: sonnet
    color: green
    ---
    You are a senior code reviewer focused on correctness, security, and readability.
    When invoked:
    1. Run `git diff` to see recent changes (or review the specified files).
    2. Focus only on the changed code, not the whole codebase.
    3. Check for: security vulnerabilities, unhandled errors, unclear naming, missing tests, and duplicated logic.
    For each issue found, report:
    - File path and line number
    - Description of the problem
    - Suggested fix (code snippet if applicable)
    - Severity: low / medium / high
    Be direct and specific. Skip praise — focus on what needs to change.

    .

    👉 ส่วนที่ 1. name

    ตัวอย่าง:

    name: code-reviewer

    ใช้ระบุตัว subagent

    .

    👉 ส่วนที่ 2. description

    ตัวอย่าง:

    description: Reviews code for quality, security, and maintainability issues. Use proactively after writing or modifying code, before committing.

    บอก Claude 2 อย่างเกี่ยวกับ subagent:

    1. What: subagent นี้ทำอะไร
    2. When: เมื่อไรถึงจะเรียกใช้ subagent นี้

    เคล็ดลับ:

    1. ใช้คำว่า proactively เพื่อทำให้ Claude เรียกใช้ subagent นี้บ่อยขึ้น (อย่างในตัวอย่าง เราบอกว่า ให้ใช้ proactively ทุกครั้งที่เขียนหรือแก้ไข code เสร็จ)

    .

    👉 ส่วนที่ 3. tools

    ตัวอย่าง:

    tools: Read, Grep, Glob

    ระบุ action ที่ subagent สามารถทำได้

    อย่างในตัวอย่าง เราเลือก:

    • Read: อ่านไฟล์
    • Grep: ค้นหาเนื้อหาในไฟล์
    • Glob: ค้นหาไฟล์

    Note:

    • ดูตัวเลือก action ทั้งหมดได้ที่ Tools reference
    • เราสามารถเลือกไม่ระบุได้ โดย Claude จะเลือกใช้ค่า default ของระบบ

    .

    👉 ส่วนที่ 4. model

    ตัวอย่าง:

    model: sonnet

    ระบุ model ที่จะใช้งาน ซึ่งมี 4 ตัวเลือก:

    1. Haiku: เหมาะกับงานที่เน้นความเร็ว
    2. Opus: เหมาะกับงานที่เน้นการคิดวิเคราะห์
    3. Sonnet: งานที่เน้นความสมดุลระหว่างความเร็วและการวิเคราะห์
    4. Inherit: ใช้ model เดียวกับ Claude (เช่น Claude ใช้ Haiku อยู่ subagent ก็จะใช้ Haiku ด้วย)

    ถ้าเราไม่กำหนด model, Claude จะใช้ model เดียวกับที่ใน session นั้น ๆ

    .

    👉 ส่วนที่ 5. color

    ตัวอย่าง:

    color: green

    เลือกสีของ subagent เพื่อให้รู้ว่า การทำงานไหนเป็นของ subagent ไหน

    รูปจาก Introduction to Subagents

    .

    👉 ส่วนที่ 6. Main Body

    ตัวอย่าง:

    You are a senior code reviewer focused on correctness, security, and readability.
    When invoked:
    1. Run `git diff` to see recent changes (or review the specified files).
    2. Focus only on the changed code, not the whole codebase.
    3. Check for: security vulnerabilities, unhandled errors, unclear naming, missing tests, and duplicated logic.
    For each issue found, report:
    - File path and line number
    - Description of the problem
    - Suggested fix (code snippet if applicable)
    - Severity: low / medium / high
    Be direct and specific. Skip praise — focus on what needs to change.

    ใช้กำหนดพฤติกรรมการทำงานของ subagent

    เคล็ดลับ:

    1. ควรกำหนด output format หรือหน้าตางานที่ subagent จะต้องส่งกลับไปให้ Claude
    2. ควรบอกให้ subagent ส่งรายละเอียดการทำงานบางส่วนให้กับ Claude เพื่อช่วยให้ Claude เข้าใจผลลัพธ์ที่ subagent ส่งกลับมามากขึ้น

    🤔 เมื่อไรที่ควรและไม่ควรใช้ Subagent?

    ✅ งานที่ subagent ทำได้ดี:

    ข้อ 1. วิจัยและสืบค้น (research and investigation) เช่น อ่านและอธิบาย code ที่มีคนเขียนแล้ว

    ข้อ 2. งานเฉพาะทางที่ต้องการความแม่นยำ เช่น งาน copywriting และการจัด style การเขียน code

    ข้อ 3. การตรวจทาน code

    ❌ สิ่งที่ไม่ควรใช้กับ subagent:

    ข้อ 1. Prompt ให้ subagent มีความรู้เฉพาะทาง

    เช่น “คุณเป็น software tester ที่มีความละเอียด”

    Subagent มีความรู้เหล่านี้อยู่แล้ว และการ prompt เพิ่มจะไม่ช่วยให้ subagent ทำงานได้ดีขึ้น

    ข้อ 2. ใช้ subagent ทำงานต่อ ๆ กัน

    เช่น subagent 1 ตรวจหา error ใน code และส่งให้ subagent 2 แก้ code

    Subagent จะเห็นแต่ผลลัพธ์ แต่ไม่เห็นการทำงานของกันและกัน (เช่น subagent 2 จะเห็นแต่ผลลัพธ์ว่ามี error แต่ไม่รู้ว่า error ที่จุดไหน) ซึ่งทำให้งานที่ได้มีโอกาสผิดพลาดสูง

    ข้อ 3. ใช้ subagent ทดสอบ code

    Subagent มักจะรายงานผลการทดสอบ แต่ไม่บอกรายละเอียดที่ทำให้เกิด error ทำให้เราต้องลงไปค้นหาสาเหตุของ error เอง


    🫵 Your Turn

    หลังอ่านบทความนี้แล้ว ลองใช้มา Claude Code กันดูนะครับ:


    📃 References

  • Claude Code Skill คืออะไร? วิธีสร้างและใช้งาน Skill อย่างมีประสิทธิภาพ

    Claude Code Skill คืออะไร? วิธีสร้างและใช้งาน Skill อย่างมีประสิทธิภาพ

    Claude Code เป็น AI coding assistant ที่ทำงานกับไฟล์บนเครื่องของเราได้โดยตรง ช่วยลดเวลาในการเปลี่ยนหน้าจอในการทำงาน และทุ่นแรงในการเขียนและรีวิว code ได้

    จุดเด่นหนึ่งของ Claude คือ skill ซึ่งช่วยให้ Claude ปรับตัวและทำงานที่มีความซับซ้อนและหลากหลายได้

    ในบทความนี้ ผมจะพาทุกคนไปรู้จักกับ skill ตั้งแต่ skill คืออะไร ใช้ยังไง ไปจนถึงเคล็ดลับในการใช้ skill อย่างมีประสิทธิภาพ

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย



    🤨 Skill ใน Claude Code คืออะไร? ใช้เพื่ออะไร?

    Skill เป็นไฟล์ markdown ที่เก็บคำชี้แจง script และข้อมูลต่าง ๆ ที่ Claude เรียกใช้ได้เมื่อจำเป็น

    Skill ใช้แก้ปัญหา 2 อย่าง:

    1. ลดการพูดซ้ำ
    2. ใช้ทำงานย่อย ๆ หรืองานเฉพาะกิจ

    งานย่อย ๆ หรือสิ่งที่เรามักต้องพูดซ้ำกับ Claude (เช่น รีวิว code ยังไง, แก้ bug ยังไง, deploy บน production ยังไง) ให้เราสร้างเป็น skill เพื่อให้ Claude รู้ว่าต้องทำอะไรโดยที่เราไม่ต้องสั่งอีก


    🕰️ Skill จะใช้เมื่อไร?

    Claude จะเรียกใช้งาน skill โดยอัตโนมัติเมื่อ skill มีความเกี่ยวข้องกับงานตรงหน้า หรือเมื่อเราเรียกใช้งานเอง

    ตัวอย่างการเรียกใช้งานแบบอัตโนมัติ:

    รีวิว code ใน app.py ให้หน่อย

    ถ้า Claude มี skill สำหรับรีวิว code (เช่น code-review) Claude จะเรียกใช้งาน skill นี้โดยที่เราไม่ต้องบอกแต่แรก

    ตัวอย่างการเรียกใช้งานเอง:

    ใช้ /code-review รีวิว code ใน app.py ให้หน่อย

    ในกรณีนี้ Claude จะค้นหา skill ชื่อ code-review และเอามาใช้รีวิว code ตามคำสั่ง


    📎 สร้าง Skill ยังไง?

    เราสร้าง skill ได้ 3 วิธี:

    1. สั่งให้ Claude สร้างให้ (แนะนำ)
    2. เขียนเอง
    3. โหลด skill ที่มีคนสร้างไว้แล้ว

    ตัวอย่างการสั่งให้ Claude สร้างให้:

    สร้าง skill สำหรับรีวิว code ให้หน่อย

    📑 Skill สร้างแล้วเก็บไว้ที่ไหน?

    Skill จะถูกเก็บไว้ใน SKILL.md ซึ่งประกอบไปด้วย 5 ส่วน:

    1. name
    2. description
    3. allowed-tools (optional)
    4. model (optional)
    5. Instruction

    ตัวอย่าง SKILL.md:

    ---
    name: code-review
    description: Reviews code changes for bugs, style issues, and best practices. Use when the user asks for a code review, wants feedback on a diff or pull request, or asks "does this look right?" about recent changes.
    allowed-tools: Read, Grep, Glob, Bash(git diff:*)
    model: sonnet
    ---
    # Code Review
    Review the code changes in the current diff or specified files.
    ## Steps
    1. Run `git diff` (or read the specified files) to see what changed
    2. Check for:
    - Logic errors or edge cases the code doesn't handle
    - Inconsistent style with the rest of the codebase
    - Missing error handling
    - Unclear naming or missing comments where logic is non-obvious
    - Potential security issues (unsanitized input, hardcoded secrets)
    3. Skip nitpicks on formatting if a linter/formatter is already configured
    ## Output Format
    - Group feedback by file
    - Mark each issue as **Blocking**, **Suggestion**, or **Nit**
    - Keep comments specific — point to the line and explain *why*, not just *what*
    - End with a one-line summary verdict (e.g. "Ready to merge" / "Needs changes")

    .

    👉 ส่วนที่ 1. name

    ตัวอย่าง:

    name: code-review

    ใช้ระบุ skill และควรตั้งให้มีความเจาะจงเพื่อไม่ให้ซ้ำกัน เช่น:

    • code-review-for-security
    • code-review-for-rendering
    • code-review-for-general-functions

    .

    👉 ส่วนที่ 2. description

    ตัวอย่าง:

    description: Reviews code changes for bugs, style issues, and best practices. Use when the user asks for a code review, wants feedback on a diff or pull request, or asks "does this look right?" about recent changes.

    ใช้บอก Claude 2 อย่าง:

    1. What: skill นี้ทำอะไร
    2. When: เมื่อไรถึงจะเรียกใช้ skill นี้

    อย่างในตัวอย่างบอก Claude ว่า:

    1. What: ใช้รีวิว code เพื่อหา bug และแนะนำวิธีแก้
    2. When: ใช้เมื่อ user ต้องการรีวิว code หรือถามเกี่ยวกับ code

    .

    👉 ส่วนที่ 3. allowed-tools

    ตัวอย่าง:

    allowed-tools: Read, Grep, Glob, Bash(git diff:*)

    ใช้ระบุ action ที่ skill นี้ทำได้

    อย่างในตัวอย่าง เราเลือก:

    • Read: อ่านไฟล์
    • Grep: ค้นหาเนื้อหาในไฟล์
    • Glob: ค้นหาไฟล์
    • Bash: รัน shell command

    Note:

    • ดูตัวเลือก action ทั้งหมดได้ที่ Tools reference
    • เราสามารถเลือกไม่ระบุได้ โดย Claude จะเลือกใช้ค่า default ของระบบ

    .

    👉 ส่วนที่ 4. model

    ตัวอย่าง:

    model: sonnet

    ระบุ model ที่จะใช้งาน ซึ่งมี 3 ตัวเลือก:

    1. Haiku: เหมาะกับงานที่เน้นความเร็ว
    2. Opus: เหมาะกับงานที่เน้นการคิดวิเคราะห์
    3. Sonnet: งานที่เน้นความสมดุลระหว่างความเร็วและการวิเคราะห์

    เราเลือกที่จะไม่กำหนด model ได้ โดยในกรณีนั้น Claude จะใช้ model ที่ใช้งานอยู่ใน session นั้น ๆ

    .

    👉 ส่วนที่ 5. Instruction

    ตัวอย่าง:

    # Code Review
    Review the code changes in the current diff or specified files.
    ## Steps
    1. Run `git diff` (or read the specified files) to see what changed
    2. Check for:
    - Logic errors or edge cases the code doesn't handle
    - Inconsistent style with the rest of the codebase
    - Missing error handling
    - Unclear naming or missing comments where logic is non-obvious
    - Potential security issues (unsanitized input, hardcoded secrets)
    3. Skip nitpicks on formatting if a linter/formatter is already configured
    ## Output Format
    - Group feedback by file
    - Mark each issue as **Blocking**, **Suggestion**, or **Nit**
    - Keep comments specific — point to the line and explain *why*, not just *what*
    - End with a one-line summary verdict (e.g. "Ready to merge" / "Needs changes")

    เป็นคำชี้แจงในการทำงานให้กับ Claude

    อย่างในตัวอย่าง เราระบุ:

    1. ขั้นตอนการรีวิว code
    2. สิ่งที่จะต้องส่งกลับมา

    ✌️ ระดับและการจัดลำดับ Skill

    Skill แบ่งได้เป็น 4 ระดับ:

    1. Enterprise: skill ที่ใช้ร่วมกันทั้งองค์กร (เก็บอยู่ใน managed-settings.json)
    2. Personal: skill ที่ใช้ร่วมกันทุก project ที่เราทำงาน (เก็บอยู่ใน ~/.claude/skills)
    3. Project: skill ที่ใช้เฉพาะ project (เก็บอยู่ใน <project_folder>/.claude/skills)
    4. Plugin: skill ที่มาพร้อม plugin (เก็บอยู่ใน <project_folder>/.claude-plugins/skills)

    ในกรณีที่ skill มีชื่อซ้ำกันหรือทำงานเหมือนกัน Claude จะเลือกใช้ skill ตามลำดับนี้:

    Enterprise > Personal > Project > Plugin

    ตัวอย่าง:

    เรามี skill ชื่อ code-review ใน root directory (ระดับ Personal) ซึ่งซ้ำกับ code-review ใน managed-settings.json (ระดับ Enterprise) Claude จะเลือกใช้ code-review ใน managed-settings.json ก่อน


    🤫 เคล็ดลับการใช้ Skill

    .

    1️⃣ ข้อที่ 1. เขียน name และ description ให้ดี

    Claude เลือกใช้ skill โดยอ่านจาก name และ description

    ถ้าทั้ง name และ description ไม่สื่อความหมายที่เกี่ยวข้องกับงานตรงหน้า Claude จะไม่เรียกมาใช้งาน แม้ว่าเราต้องการให้ใช้ก็ตาม

    วิธีแก้ คือ เขียน name และ description ให้ดี:

    1. name ควรมีความเฉพาะเจาะจง ไม่ธรรมดาจนเกินไป
    2. description ควรบอกว่า skill ทำอะไร และควรใช้เมื่อไร

    .

    2️⃣ ข้อที่ 2. เขียน Instruction ให้กระชับ

    เมื่อ Claude เลือกใช้ skill ก็จะโหลด instruction ใน SKILL.md เข้ามาอ่าน ซึ่งจะทำให้ Claude มี context หรือความจำในการทำงานน้อยลง

    ดังนั้น เราควรเขียน instruction กระชับเพื่อประหยัดการใช้ context ของ Claude โดย SKILL.md ไม่ควรยาวเกิน 500 บรรทัด

    ในกรณีที่ instruction ยาว เราสามารถตัดแบ่งบางส่วนไปใส่ในไฟล์อื่น และเขียนอ้างอิงไว้ใน SKILL.md แทนได้

    ตัวอย่าง:

    ---
    name: api-integration
    description: Build and debug integrations with our internal API. Use when the user asks to add an endpoint, authenticate a request, or troubleshoot API errors.
    ---
    # API Integration
    This skill covers our internal API conventions. Keep this file as your starting point — don't guess at details covered in the linked files below; open them when the task needs that level of detail.
    ## Quick Reference
    - Base URL: `https://api.internal.example.com/v1`
    - Auth: Bearer token in `Authorization` header
    - All responses are JSON with a `data` and `error` envelope
    ## When You Need More Detail
    - **Authentication flows** (OAuth, token refresh, service accounts) → see [auth.md](auth.md)
    - **Full endpoint list and request/response schemas** → see [endpoints.md](endpoints.md)
    - **Error codes and retry behavior** → see [errors.md](errors.md)
    - **Example requests in curl, Python, and JS** → see [examples.md](examples.md)
    Only read the file(s) relevant to the current task. Don't load all of them upfront.

    เมื่อทำอย่างนี้ Claude จะโหลดข้อมูลจากไฟล์ที่เราอ้างอิงใน SKILL.md เมื่อจำเป็นเท่านั้น ทำให้เราประหยัดพื้นที่ใน context ไปได้

    .

    3️⃣ ข้อที่ 3. ใช้ Skill ใน Subagent

    Subagent เป็น agent ที่ Claude เรียกใช้งานเพื่อทำงานเฉพาะทางบางอย่างได้

    Subagent จะไม่สามารถใช้ skill ได้เองจนกว่าเราจะบอกว่าใช้ได้ โดยกำหนด skill ใน skills ในไฟล์ markdown ของ subagent

    ตัวอย่าง:

    ---
    name: api-developer
    description: Implement API endpoints following team conventions
    tools: Read, Write, Edit, Grep, Glob, Bash
    skills:
    - api-conventions
    - error-handling-patterns
    ---
    You are an API developer. Implement endpoints following the conventions
    and patterns from the preloaded skills above.

    เมื่อกำหนดแล้ว subagent จะเรียกใช้งาน skill ทุกครั้งที่ตัวเองถูกใช้งาน ซึ่งต่างจาก main agent ที่เลือกใช้ skill ได้ตามหน้างาน


    🫵 Your Turn

    หลังอ่านบทความนี้แล้ว ลองใช้มา Claude Code กันดูนะครับ:


    📃 References

  • CLAUDE.md คืออะไร? วิธีสร้างและใช้ CLAUDE.md สำหรับ Claude Code

    CLAUDE.md คืออะไร? วิธีสร้างและใช้ CLAUDE.md สำหรับ Claude Code

    Claude Code เป็น AI coding assistant ที่ทำงานกับไฟล์บนคอมพิวเตอร์ได้ สามารถช่วยนักพัฒนาเขียนและแก้ไข code ได้ในเวลาอันรวดเร็ว และช่วยคนทั่วไปเขียน code โดยไม่ต้องมีความรู้มาก่อน

    ในบทความนี้ ผมจะมาแนะนำวิธีใช้ CLAUDE.md เพื่อช่วยให้ทำงานกับ Claude Code ได้อย่างมีประสิทธิภาพมากขึ้น

    เนื้อหาแบ่งเป็น 3 ส่วน:

    1. CLAUDE.md คืออะไร
    2. วิธีใช้ CLAUDE.md
    3. วิธีเขียน CLAUDE.md ที่ดี

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย



    🤔 CLAUDE.md คืออะไร? สำคัญยังไง?

    CLAUDE.md เป็น ไฟล์ markdown ที่ช่วยให้ Claude เข้าใจบริบทของงานและกำกับการทำงานของ Claude

    สิ่งที่ควรใส่ไว้ใน CLAUDE.md:

    1. สิ่งที่ Claude ควรรู้เกี่ยวกับงานที่ทำ (เช่น เป้าหมาย, tech stack ที่ใช้)
    2. แนวทางการทำงาน (เช่น สไตล์การเขียน code, วิธีเขียน commit message)

    อะไรที่ Claude ต้องรู้ก่อนลงมือทำงาน และอะไรที่เราอยากให้ Claude ทำโดยที่เราไม่ต้องสั่งทุกครั้ง ให้ใส่ไว้ใน CLAUDE.md

    ตัวอย่าง CLAUDE.md:

    # Project Overview
    - เป้าหมาย: สร้าง e-commerce API สำหรับร้านขายเสื้อผ้าออนไลน์
    - Tech stack: Node.js, Express, PostgreSQL, TypeScript
    # Coding Style
    - ใช้ 2-space indentation
    - ใช้ TypeScript strict mode เสมอ
    - API endpoints อยู่ใน src/api/handlers/

    ถ้าเราเริ่ม session โดยไม่มี CLAUDE.md, Claude จะต้องเรียนรู้เกี่ยวกับงานที่จะทำใหม่ทุกครั้ง

    แต่เมื่อมี CLAUDE.md, Claude จะเข้าใจงานที่ต้องทำโดยที่เราไม่ต้องอธิบายใหม่ทุกครั้ง


    💻 CLAUDE.md ใช้ยังไง?

    .

    🧑‍💻 วิธีใช้ CLAUDE.md

    Claude จะโหลด CLAUDE.md เข้ามาโดยอัตโนมัติเมื่อเริ่ม session ใหม่

    สิ่งที่เราต้องทำ คือ สร้าง CLAUDE.md ก่อนจะเริ่ม session

    .

    🆕 วิธีสร้าง CLAUDE.md

    เราสร้าง CLAUDE.md ได้ 2 วิธี:

    1. เขียนเอง
    2. ใช้คำสั่ง /init เพื่อให้ Claude อ่านไฟล์ในโฟลเดอร์และสร้าง CLAUDE.md ขึ้นมาให้

    .

    📝 วิธีแก้ไข CLAUDE.md

    เราแก้ไข CLAUDE.md ได้ 2 วิธี:

    1. แก้ไฟล์ใน text editor
    2. ใช้คำสั่ง /memory แล้วแก้ไข CLAUDE.md

    ✍️ การเขียน CLAUDE.md ที่ดี

    CLAUDE.md ที่ดีมี 4 ลักษณะ:

    .

    1️⃣ ลักษณะที่ 1. ไม่เกิน 200 บรรทัด

    CLAUDE.md ควรมีความยาวไม่เกิน 200 บรรทัด

    ถ้าเกิน 200 บรรทัด Claude อาจทำตามคำสั่งใน CLAUDE.md ได้ไม่เต็มที่

    .

    2️⃣ ลักษณะที่ 2. มีโครงสร้างชัดเจน (Structured)

    ใช้ markdown ในการเขียน เพื่อสื่อสารความสัมพันธ์และลำดับความสำคัญของข้อมูล

    ❌ ตัวอย่างที่ไม่ดี:

    โปรเจกต์นี้เป็น blog platform ใช้ Next.js กับ Tailwind CSS เขียนบทความเป็น markdown เก็บไว้ใน content/posts/ รูปภาพให้ optimise ก่อน upload เสมอ และ compress ให้ไม่เกิน 200KB ส่วนชื่อไฟล์ให้ใช้ kebab-case เช่น my-first-post.md ก่อน deploy ทุกครั้งต้อง run npm run build เพื่อเช็ค error ก่อน

    ✅ ตัวอย่างที่ดี:

    # Project Overview
    - Tech stack: Next.js, Tailwind CSS
    - บทความเก็บเป็น markdown ใน content/posts/
    # File Naming
    - ใช้ kebab-case เช่น my-first-post.md
    # Images
    - Optimise และ compress ก่อน upload เสมอ (ไม่เกิน 200KB)
    # Before Deploy
    - Run `npm run build` เพื่อเช็ค error ก่อนทุกครั้ง

    .

    3️⃣ ลักษณะที่ 3. เจาะจง (Specific)

    เขียนให้เจาะจง เพื่อให้ Claude ทำตามได้ง่าย

    ❌ ตัวอย่างที่ไม่ดี:

    เขียน commit message ให้อ่านง่าย ๆ

    ✅ ตัวอย่างที่ดี:

    เขียน commit message ในรูปแบบนี้:
    [Add/Edit/Delete] concise commit message
    เช่น:
    - [Add] login endpoint สำหรับผู้ใช้งาน
    - [Edit] แก้ไข bug การคำนวณราคาสินค้าใน checkout
    - [Delete] ลบไฟล์ config ที่ไม่ได้ใช้แล้ว

    .

    4️⃣ ลักษณะที่ 4. เสมอต้นเสมอปลาย (Consistent)

    CLAUDE.md ควรมีเนื้อหาที่สอดคล้องกัน

    ถ้าเนื้อหาขัดแย้งกัน Claude จะเลือกทำตามข้อใดข้อหนึ่งแล้วแต่สถานการณ์ ทำให้งานที่ออกมาไม่คงเส้นคงวา

    .

    ☑️ Checklist

    ก่อนใช้ CLAUDE.md เราควรจะรีวิวเนื้อหาก่อนว่า:

    1. ไม่เกิน 200 บรรทัด
    2. ใช้ markdown ในการเขียน
    3. มีความชัดเจน
    4. เนื้อหาไม่ขัดแย้งกันเอง

    🫵 Your Turn

    หลังจบบทความนี้แล้ว ลองมาใช้ Claude Code กันนะครับ


    📃 References

  • วิธีสร้าง RAG Pipeline ด้วย Haystack 2.0 ใน Python

    วิธีสร้าง RAG Pipeline ด้วย Haystack 2.0 ใน Python

    ในบทความก่อน ผมได้แนะนำให้ทุกคนได้รู้จักกับ Haystack 2.0 ซึ่งเป็น package สำหรับสร้าง LLM (large language model) application ใน Python

    ในบทความนี้ ผมจะมาแนะนำการใช้ Haystack 2.0 ในการทำ RAG (Retrieval-Augmented Generation) pipeline ซึ่งเป็นการใช้งานหลักของ Haystack 2.0

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. 🤨 What Is RAG?
    2. 🗂️ Pipeline 1. Indexing
      1. 1️⃣ Step 1. Create Documents
      2. 2️⃣ Step 2. Create a Splitter
      3. 3️⃣ Step 3. Create Embedder
      4. 4️⃣ Step 4. Create a Vector Store
      5. 5️⃣ Step 5. Create a Document Writer
      6. 6️⃣ Step 6. Create an Indexing Pipeline
      7. 7️⃣ Step 7. Run the Pipeline
    3. 🔍 Pipeline 2. Querying
      1. 1️⃣ Step 1. Create a Query Embedder
      2. 2️⃣ Step 2. Create a Retriever
      3. 3️⃣ Step 3. Create a Prompt Builder
      4. 4️⃣ Step 4. Create a Response Generator
      5. 5️⃣ Step 5. Create a Pipeline
      6. 6️⃣ Step 6. Run the Pipeline
    4. 💪 Summary
    5. ⏭️ Next
    6. 📃 References

    🤨 What Is RAG?

    RAG เป็นเทคนิคที่ช่วยให้ LLM ตอบคำถามได้ดีขึ้น โดยส่งเอกสารที่เกี่ยวข้องไปให้ LLM สร้างคำตอบ

    RAG pipeline ประกอบด้วย 2 ส่วน:

    1. Indexing pipeline: จัดเตรียมเอกสาร
    2. Querying pipeline: ค้นหาเอกสารและเขียนคำตอบ

    Pipeline 1. Indexing pipeline:

    Documents
    Split Documents into chunks
    Embed chunks into vectors
    Write Documents to vector store

    Pipeline 2. Querying pipeline:

    Query
    Embed query
    Retrieve relevant chunks from vector store
    Add retrieved chunks to prompt
    Send prompt to LLM
    Return answer

    เราไปดูการสร้างแต่ละส่วนด้วย Haystack 2.0 กัน ผ่านตัวอย่างการสร้างบอทตอบคำถามข่าวจาก BBC News Archive dataset กัน


    🗂️ Pipeline 1. Indexing

    Indexing pipeline มี 5 ส่วนประกอบ:

    1. Documents: เอกสารต้นทาง
    2. Splitter: แยกเอกสาร
    3. Embedder: แปลงเอกสารให้เป็น vector
    4. Vector store: ตัวจัดเก็บ vector
    5. Writer: ส่ง vector ไปเก็บที่ vector store

    และเราสามารถสร้าง pipeline ได้ใน 7 ขั้นตอน:

    1. Create Documents
    2. Create a splitter
    3. Create an embedder
    4. Create a vector store
    5. Create a document writer
    6. Create an indexing pipeline
    7. Run the pipeline

    ไปดูการเขียน code ในแต่ละขั้นตอนกัน

    .

    1️⃣ Step 1. Create Documents

    ในขั้นแรก เราจะสร้าง Document object หรือเอกสารที่จะใส่เข้าไปใน RAG pipeline

    ในตัวอย่าง เราจะโหลดเอกสารที่เก็บไว้ในไฟล์ CSV แบบนี้:

    Python
    # Import the package
    import pandas as pd
    # Load the CSV
    df = pd.read_csv("bbc-news-data.csv", sep="\t")
    # Inspect the first 5 rows
    df.head(5)

    ผลลัพธ์:

    และแปลงให้เป็น Document object แบบนี้:

    Python
    # Import the package
    from haystack import Document
    # Instantiate a collector
    docs = []
    # Loop through the records
    for row in df.itertuples():
    doc = Document(
    content=row.content,
    meta={
    "title": row.title,
    "category": row.category,
    "filename": row.filename
    }
    )
    docs.append(doc)
    # Inspect one Document
    docs[0]

    ผลลัพธ์:

    Document(id=7fe87ca8e57420adb9ed47866809d38424003dd43b4eefdf6b1a400eb3709677, content: ' Quarterly profits at US media giant TimeWarner jumped 76% to $1.13bn (£600m) for the three months t...', meta: {'title': 'Ad sales boost Time Warner profit', 'category': 'business', 'filename': '001.txt'})

    .

    2️⃣ Step 2. Create a Splitter

    ในขั้นที่ 2 เราจะสร้าง splitter ที่จะแยกเอกสารออกเป็นก้อน ๆ หรือ chunk ซึ่งจะทำให้ค้นหาเอกสารได้ดีขึ้น

    Splitter มีหลายประเภท:

    SplitterSplit Method
    DocumentSplitter()แบ่งตามคำ บรรทัด ย่อหน้า หรือหน้า
    ChonkieTokenDocumentSplitter()แบ่งตามจำนวน token
    RecursiveDocumentSplitter()แบ่งตาม list ที่เรากำหนด

    ในตัวอย่าง เราจะใช้ RecursiveDocumentSplitter() กัน:

    Python
    # Import the package
    from haystack.components.preprocessors import RecursiveDocumentSplitter
    # Create a splitter
    splitter = RecursiveDocumentSplitter(
    split_length=180,
    split_overlap=30,
    split_unit="word",
    separators=[
    "\n\n",
    "sentence",
    "\n",
    " ",
    ]
    )

    .

    3️⃣ Step 3. Create Embedder

    ในขั้นที่ 3 เราจะสร้าง embedder ที่จะเปลี่ยน chunk ให้เป็น vector หรือตัวเลขที่ใช้ค้นหาเอกสารใน RAG pipeline

    ตัวอย่าง chunk:

    British Airways has blamed high fuel prices ...

    ตัวอย่าง vector:

    [
    -0.0312,
    0.0847,
    -0.0129,
    0.0455,
    0.0068,
    -0.0974,
    0.0381,
    0.0616,
    -0.0442,
    0.0193,
    ...
    ]

    RAG pipeline จะใช้ vector ในการค้นหาเอกสารที่เกี่ยวข้อง โดยเอกสารและคำถามที่มี vector คล้ายกันก็ยิ่งมีเนื้อหาที่เกี่ยวข้องกัน

    เช่น “ข่าวเทคโนโลยี” จะมี vector ที่ใกล้เคียงกับหัวข่าว “Apple เปิดตัว iPhone รุ่นใหม่” มากกว่า “ผลบอลพรีเมียร์ลีกเมื่อคืนนี้”

    ในตัวอย่าง เราจะใช้ embedder ชื่อ Sentence Transformers จาก HuggingFace กัน:

    Python
    # Import the package
    from haystack_integrations.components.embedders.sentence_transformers import (
    SentenceTransformersDocumentEmbedder
    )
    # Create a document embedder
    document_embedder = SentenceTransformersDocumentEmbedder(
    model="sentence-transformers/all-MiniLM-L6-v2",
    meta_fields_to_embed=["title"],
    )

    .

    4️⃣ Step 4. Create a Vector Store

    ในขั้นที่ 4 เราจะสร้าง vector store สำหรับจัดเก็บเอกสารและ vector กัน

    ในตัวอย่าง เราจะสร้าง vector store ชั่วคราวบนเครื่องแบบนี้:

    Python
    # Import the package
    from haystack.document_stores.in_memory import InMemoryDocumentStore
    # Create a vector store
    vector_store = InMemoryDocumentStore(
    embedding_similarity_function="cosine"
    )

    .

    5️⃣ Step 5. Create a Document Writer

    ในขั้นที่ 5 เราจะสร้าง document writer ที่จะส่งเอกสารไปเก็บใน vector store:

    Python
    # Import the package
    from haystack.components.writers import DocumentWriter
    # Create a document writer
    document_writer = DocumentWriter(
    document_store=vector_store
    )

    .

    6️⃣ Step 6. Create an Indexing Pipeline

    ในขั้นที่ 6 เราจะเชื่อมต่อส่วนต่าง ๆ เข้าด้วยกัน

    เริ่มจากสร้าง Pipeline object:

    Python
    # Import the package
    from haystack import Pipeline
    # Instantiate a Pipeline object
    indexing_pipeline = Pipeline()

    เพิ่มส่วนประกอบ โดยกำหนดชื่อและตัวแปร:

    Python
    # Add the components
    indexing_pipeline.add_component("splitter", splitter)
    indexing_pipeline.add_component("embedder", document_embedder)
    indexing_pipeline.add_component("writer", document_writer)

    แล้วเชื่อมส่วนประกอบเข้าด้วยกัน โดยกำหนดว่า แต่ละส่วนประกอบจะส่งและรับอะไรจากกันบ้าง:

    pipeline.connect("sender.output", "receiver.input")

    ตัวอย่าง:

    Python
    # Connect the components
    indexing_pipeline.connect("splitter.documents", "embedder.documents")
    indexing_pipeline.connect("embedder.documents", "writer.documents")

    สุดท้าย เราสามารถดู pipeline ที่สร้างขึ้นได้แบบนี้:

    Python
    # Display the pipeline
    indexing_pipeline.show()

    ผลลัพธ์:

    .

    7️⃣ Step 7. Run the Pipeline

    ในขั้นสุดท้าย เราจะรัน pipeline เพื่อเอาเอกสารเข้าไปเก็บใน vector store:

    Python
    # Run the indexing pipeline
    indexing_result = indexing_pipeline.run(
    {
    "splitter": {
    "documents": docs
    }
    }
    )
    # Display the result
    print(indexing_result)

    ผลลัพธ์:

    {'writer': {'documents_written': 7182}}

    เท่านี้ เราก็มี indexing pipeline ไว้ใช้งานแล้ว


    🔍 Pipeline 2. Querying

    Querying pipeline มี 4 ส่วนประกอบ:

    1. Query embedder: แปลงคำถามเป็น vector
    2. Retriever: ค้นหาเอกสารที่เกี่ยวข้อง
    3. Prompt builder: สร้าง prompt
    4. Response generator: เขียนคำตอบ

    และเราสามารถสร้างและประกอบทั้ง 4 อย่างเข้าด้วยกันได้ใน 6 ขั้นตอน:

    1. Create a query embedder
    2. Create a retriever
    3. Create a prompt builder
    4. Create a response generator
    5. Create a querying pipeline
    6. Run the pipeline

    .

    1️⃣ Step 1. Create a Query Embedder

    ในขั้นแรก เราจะสร้าง embedder ที่จะเปลี่ยนคำถามของผู้ใช้งานให้เป็น vector ที่เราจะใช้ค้นหาเอกสารที่เกี่ยวข้องได้:

    Python
    # Import the package
    from haystack_integrations.components.embedders.sentence_transformers import (
    SentenceTransformersTextEmbedder
    )
    # Create a question embedder
    question_embedder = SentenceTransformersTextEmbedder(
    model="sentence-transformers/all-MiniLM-L6-v2"
    )

    .

    2️⃣ Step 2. Create a Retriever

    ในขั้นที่ 2 เราจะสร้าง retriever หรือตัวค้นหาเอกสารที่เกี่ยวข้องใน vector store:

    Python
    # Import the package
    from haystack.components.retrievers.in_memory import InMemoryEmbeddingRetriever
    # Create a retriever
    retriever = InMemoryEmbeddingRetriever(
    document_store=vector_store,
    top_k=5
    )

    .

    3️⃣ Step 3. Create a Prompt Builder

    ในขั้นที่ 3 เราจะสร้าง prompt builder ที่จะประกอบ prompt ที่จะส่งไปให้ LLM เพื่อตอบคำถาม

    เราจะเริ่มจากกำหนด system prompt ที่จะกำหนดพฤติกรรมการตอบคำถาม:

    Python
    # Set the system prompt
    system_prompt = """
    You are a helpful assistant for a historical BBC News Archive.
    Answer only from the retrieved BBC article passages.
    Rules:
    - Be concise.
    - Do not use outside knowledge.
    - Do not invent facts that are not supported by the passages.
    - The archive is historical, so do not present its content as current news.
    - Cite factual claims using source labels such as [1] or [2].
    - If the retrieved passages do not contain enough evidence, say that clearly.
    - Response in short bullet points.
    - At the end of the answer, include a "Sources:" section listing only the sources that were actually cited in the answer. Format each source as:
    - [n] <Title>
    """

    และ user prompt ที่จะส่งเอกสารที่เกี่ยวข้องกับคำถามของผู้ใช้งานไปให้กับ LLM:

    Python
    # Set the user prompt
    user_prompt = """
    Question:
    {{ question }}
    Retrieved BBC archive passages:
    {% for doc in docs %}
    [{{ loop.index }}]
    Title: {{ doc.meta["title"] }}
    Category: {{ doc.meta["category"] }}
    Filename: {{ doc.meta["filename"] }}
    Passage:
    {{ doc.content }}
    {% endfor %}
    Write a concise answer using only the retrieved passages.
    After the answer, include:
    Sources:
    {% raw %}- [n] <Title>{% endraw %}
    where each title corresponds to the cited passage, and list only the sources that were actually cited in the answer.
    """

    สังเกตว่า เราใช้ Jinja syntax (การเขียน {{}} และ {}) เพื่อทำให้เราแทนที่เอกสารลงไปในตัวแปรใน prompt ได้

    หลังจากกำหนด system และ user prompt แล้ว เราจะสร้าง prompt builder แบบนี้:

    Python
    # Import the packages
    from haystack.components.builders import ChatPromptBuilder
    from haystack.dataclasses import ChatMessage
    # Create a prompt builder
    prompt_builder = ChatPromptBuilder(
    template=[
    ChatMessage.from_system(system_prompt.strip()),
    ChatMessage.from_user(user_prompt.strip())
    ],
    required_variables=["question", "docs"],
    )

    .

    4️⃣ Step 4. Create a Response Generator

    ในขั้นที่ 4 เราจะสร้าง response generator หรือ LLM ที่จะตอบคำถามขึ้นมา

    เริ่มจากกำหนด API key:

    Python
    # Import the packages
    import os
    from dotenv import load_dotenv
    # Load the .env content
    load_dotenv()
    # Get the API key
    GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

    และสร้าง response generator:

    Python
    # Import the packages
    from haystack.utils import Secret
    from haystack_integrations.components.generators.google_genai import (
    GoogleGenAIChatGenerator
    )
    # Create a generator
    generator = GoogleGenAIChatGenerator(
    model="gemini-2.5-flash",
    api_key=Secret.from_token(GEMINI_API_KEY),
    generation_kwargs={
    "temperature": 0.2
    }
    )

    .

    5️⃣ Step 5. Create a Pipeline

    ในขั้นที่ 5 เราจะเชื่อมต่อทุกส่วนใน querying pipeline เข้าด้วยกัน โดยใช้วิธีเดียวกันกับ indexing pipeline

    สร้าง Pipeline object:

    Python
    # Import the package
    from haystack import Pipeline
    # Instantiate a Pipeline object
    querying_pipeline = Pipeline()

    เพิ่มส่วนประกอบ:

    Python
    # Add the components
    querying_pipeline.add_component("question_embedder", question_embedder)
    querying_pipeline.add_component("retriever", retriever)
    querying_pipeline.add_component("prompt_builder", prompt_builder)
    querying_pipeline.add_component("generator", generator)

    เชื่อมส่วนประกอบเข้าด้วยกัน:

    Python
    # Connect the components
    querying_pipeline.connect("question_embedder.embedding", "retriever.query_embedding")
    querying_pipeline.connect("retriever.documents", "prompt_builder.docs")
    querying_pipeline.connect("prompt_builder.prompt", "generator.messages")

    ดู pipeline ที่สร้างขึ้นมา:

    Python
    # Display the pipeline
    querying_pipeline.show()

    ผลลัพธ์:

    .

    6️⃣ Step 6. Run the Pipeline

    ในขั้นสุดท้าย เราจะเรียกใช้งาน querying pipeline กัน

    เริ่มจากสร้าง function สำหรับเรียกใช้งาน pipeline เพื่อให้ง่ายต่อการเรียกใช้:

    Python
    # Create the function
    def run_query(query: str):
    # Run the pipeline
    result = querying_pipeline.run(
    {
    "question_embedder": {
    "text": query
    },
    "prompt_builder": {
    "question": query
    }
    }
    )
    # Return the result
    return result["generator"]["replies"][0].text

    จากนั้น เรียกใช้งานโดยถามเกี่ยวกับข่าวที่อยู่ใน BBC News Archive เช่น:

    Python
    # Set the question
    my_question = "What concerns did users raise about Google’s AutoLink feature?"
    # Run the query
    query_result = run_query(query=my_question)
    # Display the result
    print(query_result)

    ผลลัพธ์:

    Users raised several concerns about Google’s AutoLink feature:
    * It directs people to pre-selected commercial websites [1].
    * Google's dominant market position could give a competitive edge to firms like Amazon [1].
    * It creates links based on webpage information without the publisher's permission [1].
    * Online libraries and other websites might direct users to commercial sites like Amazon or rival services against their will or in conflict with their own advertising [4].
    * Some users felt it would only be fair if websites had to opt-in or receive revenue for "click throughs" to commercial sites [2].
    * Concerns were raised about user choice, transparency regarding Google's payments, and the ability to substitute preferred companies for those chosen by Google [3].
    * There was an objection to users being forced or tricked into using the service [3].
    * The feature was compared to Microsoft's Smart Tags, which was widely criticised [2].
    Sources:
    - [1] Google's toolbar sparks concern
    - [2] Google's toolbar sparks concern
    - [3] Google's toolbar sparks concern
    - [4] Google's toolbar sparks concern

    💪 Summary

    RAG pipeline ใน Haystack 2.0 ประกอบด้วย 2 ส่วน ซึ่งสร้างได้แบบนี้:

    Pipeline 1. Indexing pipeline:

    1. Create Documents
    2. Create a splitter
    3. Create an embedder
    4. Create a vector store
    5. Create a document writer
    6. Create a pipeline
    7. Run the pipeline

    Pipeline 2. Querying pipeline:

    1. Create a query embedder
    2. Create a retriever
    3. Create a prompt builder
    4. Create a response generator
    5. Create a querying pipeline
    6. Run the pipeline

    ⏭️ Next

    หลังอ่านบทความจบแล้ว ลองมาใช้ Haystack 2.0 กันดูนะครับ:

    1. ดูตัวอย่าง dataset และ code ในบทความนี้
    2. ดูวิธีสร้าง LLM application ด้วย Haystack 2.0
    3. ดูคู่มือการใช้งาน Haystack 2.0

    📃 References

  • 4 ขั้นตอนการใช้งาน Claude Code เพื่อให้ได้ Code ที่ตอบโจทย์

    4 ขั้นตอนการใช้งาน Claude Code เพื่อให้ได้ Code ที่ตอบโจทย์

    Claude Code เป็น AI coding assistant ที่กำลังได้รับความนิยม ด้วยความสามารถในการประมวลผลคำสั่ง และความสามารถในการทำงานกับไฟล์บนคอมพิวเตอร์ได้โดยตรง ช่วยลดเวลาในการ copy-paste code ไปมาระหว่าง Claude และไฟล์

    แม้ Claude Code จะสามารถเขียน code ได้ดี แต่ก็ไม่ได้หมายความว่า เราจะได้ code ที่ตอบโจทย์ทุกครั้ง

    เช่น เราสั่งให้ Claude Code สร้างเว็บ chatbot ทั้งหน้าตา การทำงาน และการเก็บข้อมูลอาจไม่ได้เป็นไปตามที่เราคาดหวัง ทำให้เราต้องเวลาเพิ่มขึ้นเพื่อสั่งให้ Claude แก้ไขงาน

    ในบทความนี้ ผมจะมาแนะนำ 4 ขั้นตอนในการทำงานกับ Claude Code เพื่อให้ได้ code ที่ตอบโจทย์

    ถ้าทุกคนพร้อมแล้ว ไปเริ่มกันเลย


    1. 🤨 ทำไมต้อง 4 ขั้นตอน?
    2. 🧑‍💻 วิธีใช้ Claude Code ที่ถูกต้อง ใน 4 ขั้นตอน
      1. 🔍 Step 1. Explore
      2. 🗺️ Step 2. Plan
      3. 💻 Step 3. Code
      4. 🚀 Step 4. Commit
    3. 💪 Summary
    4. 🫵 Your Turn
    5. 📃 References

    🤨 ทำไมต้อง 4 ขั้นตอน?

    4 ขั้นตอนในการทำงานกับ Claude Code ได้แก่:

    1. Explore
    2. Plan
    3. Code
    4. Commit

    สังเกตว่า ทั้ง 4 ขั้นเหมือนกับการเขียน code ของมนุษย์ ถ้าเราสั่งให้ developer สร้างเว็บไซต์ สิ่งที่ developer จะทำ คือ:

    1. ทำความเข้าใจความต้องการและข้อมูลที่มี (explore)
    2. วางแผนและออกแบบการเขียน code (plan)
    3. ลงมือเขียน (code)
    4. ส่งงานให้ตรวจ (commit)

    จะเห็นว่า แต่ละขั้นส่งเสริมซึ่งกันและกัน ถ้าไม่ทำความเข้าใจงาน ก็วางแผนไม่ได้ และถ้าวางแผนไม่ได้ ก็เขียน code ได้ไม่ดี และทำให้งานที่ส่งไม่มีคุณภาพ

    การทำงานกับ Claude ก็เช่นกัน ถ้าเราสั่งให้เขียน code โดยไม่ทำความเข้าใจงานและวางแผนก่อน code ที่ออกมาก็มีโอกาสที่จะผิดพลาดสูง

    ดังนั้น เพื่อให้ได้ code ที่ตอบโจทย์ เราจะต้องทำงานใน 4 ขั้นตอน:

    1. Explore
    2. Plan
    3. Code
    4. Commit

    🧑‍💻 วิธีใช้ Claude Code ที่ถูกต้อง ใน 4 ขั้นตอน

    .

    🔍 Step 1. Explore

    ในขั้นแรก เราจะต้องบอกให้ Claude ทำความเข้าใจงานก่อน โดย:

    1. เปิด plan mode โดยกด shift + tab จนกว่าจะถึง plan mode หรือพิมพ์คำสั่ง /plan
    2. สั่งให้ Claude อ่านไฟล์ใน folder ที่เรากำหนด โดยใน folder นี้สามารถมีได้ทั้งไฟล์ความต้องการ และ code ที่เราเคยเขียนไว้

    ตัวอย่าง:

    อ่านไฟล์ requirements.md
    เพื่อเข้าใจเป้าหมาย user persona
    ปัญหาที่ต้องการแก้ด้วยเว็บไซต์
    และการแบ่ง module ในการพัฒนา

    .

    🗺️ Step 2. Plan

    ในขั้นที่ 2 เราจะสั่งให้ Claude วางแผนงาน

    ตัวอย่าง:

    วางแผนการออกแบบ module 1 ของเว็บไซต์
    เริ่มตั้งแต่ user เข้าสู่ระบบไปจนถึง ...

    Tips:

    ถ้าต้องการให้ Claude คิดเยอะ ๆ เราใช้คำสั่ง think เข้ามาช่วยได้:

    KeywordLevel of Thinking
    thinkคิดระดับพื้นฐาน
    think moreคิดเพิ่มขึ้น
    think a lotคิดให้ครอบคลุม
    think longerคิดให้นานขึ้น
    ultrathinkคิดให้มากที่สุด

    ตัวอย่าง:

    ultrathink วางแผนการออกแบบ module 1 ของเว็บไซต์

    หมายถึงให้คิดเกี่ยวกับแผนการทำงาน module 1 ให้มากที่สุดเท่าที่จะทำได้

    Note: ยิ่งคิดเยอะก็จะยิ่งใช้ token เยอะ และทำให้เราติด limit ในการใช้งานเร็วขึ้น

    เมื่อ Claude ส่งแผนกลับมาแล้ว เราสามารถแก้ไขแผนเพื่อช่วยกำหนดทิศทางก่อนที่จะเขียน code ได้ ซึ่งเราทำได้ 2 วิธี:

    1. กด ctrl + G เพื่อแก้ไขแผนเอง
    2. บอก Claude ว่าอะไรที่ยังไม่ใช่ เพื่อให้ Claude แก้แผนให้

    ตัวอย่าง:

    เพิ่มการ unit test สำหรับแต่ละ function

    .

    💻 Step 3. Code

    เมื่อพอใจกับแผนแล้ว เราจะสั่งให้ Claude เขียน code ตามแผนที่วางไว้:

    1. ออกจาก plan mode ด้วยคำสั่ง /plan หรือกด shift + tab
    2. สั่งให้เขียน code ตามแผน

    ตัวอย่าง:

    เขียน code ตามแผนด้านบน
    เขียนแล้ว ให้ทดสอบและแก้ไขจุดผิดพลาดที่เจอด้วย
    ก่อนจะส่งงานกลับมา

    .

    🚀 Step 4. Commit

    ในขั้นสุดท้าย เราจะสั่งให้ Claude ช่วย commit code ให้

    ตัวอย่าง:

    Commit code
    พร้อม commit message สั้น ๆ
    เพื่อรอ review

    💪 Summary

    การทำงานกับ Claude Code ที่ดีไม่ใช่การสั่งให้เขียน code เลย แต่จะต้อง:

    1. Explore: ทำความเข้าใจเนื้องานที่ต้องทำ
    2. Plan: กำหนดทิศทางและออกแบบการทำงาน
    3. Code: ลงมือทำ
    4. Commit: ส่งงานรอรีวิว

    เมื่อทำงานกับ Claude Code ใน 4 ขั้นตอนนี้แล้ว เราก็จะได้ code ที่ตอบโจทย์มากขึ้น 👍


    🫵 Your Turn

    หลังอ่านบทความนี้แล้ว ลองมาใช้ Claude Code กันดูนะครับ:


    📃 References

  • 5 วิธีจัดการ Context ใน Claude Code เพื่อให้เขียน Code ได้ดีขึ้น

    5 วิธีจัดการ Context ใน Claude Code เพื่อให้เขียน Code ได้ดีขึ้น

    Claude Code เป็น AI ช่วยเขียน code ที่ช่วยเราวางแผน เขียน และแก้ไข code บนคอมพิวเตอร์ได้ผ่านคำสั่งภาษาธรรมชาติ (natural language)

    หนึ่งในสิ่งที่ทำให้ Claude Code ทำงานได้อย่างมีประสิทธิภาพ คือ context

    ถ้าไม่มี context, Claude จะขาดบริบทในการทำงาน และไม่สามารถเขียน code ได้อย่างมีประสิทธิภาพได้

    ในบทความนี้ เราจะมาทำความรู้จักกับ context และวิธีใช้งาน context ใน Claude Code อย่างถูกวิธีกัน

    บทความนี้แบ่งเป็น 2 ส่วน:

    1. ความหมายของ context
    2. วิธีจัดการ context ที่ถูกต้อง

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. 🤨 Context ใน Claude Code คืออะไร? และสำคัญยังไง?
    2. 👍 วิธีจัดการ Context ที่ถูกต้อง
      1. ✏️ วิธีที่ 1. เขียน Prompt ให้เจาะจง
      2. 📑 วิธีที่ 2. ใช้ CLAUDE.md
      3. ⛏️ วิธีที่ 3. ปิดใช้งาน Skill และ MCP
      4. 🕵️‍♀️ วิธีที่ 4. ใช้ Subagent
      5. 💻 วิธีที่ 5. ลบหรือสรุปข้อมูล
    3. 💪 บทสรุป
    4. 🫵 Your Turn
    5. 📃 References

    🤨 Context ใน Claude Code คืออะไร? และสำคัญยังไง?

    Context คือ ความจำในการทำงานของ Claude (working memory)

    Context เก็บประวัติการพูดคุยระหว่างเรากับ Claude รวมถึงการทำงานของ Claude เช่น การอ่านไฟล์และการแก้ไขไฟล์ ซึ่งช่วยให้ Claude มีข้อมูลบริบทในการทำงาน

    เมื่อ context เต็ม Claude จะลืมการพูดคุยช่วงต้น และมีโอกาสทำงานผิดพลาดมากขึ้น

    เช่น ตอนต้น session เราบอกให้ Claude เขียน comment กำกับ code ทุกครั้ง พอ context เต็ม Claude จะลืมคำสั่งนี้ และไม่ comment code ตามที่เราต้องการ

    ดังนั้น เราควรจะจัดการ context ให้ดี เพื่อให้ Claude ทำงานได้อย่างมีประสิทธิภาพมากที่สุด


    👍 วิธีจัดการ Context ที่ถูกต้อง

    5 วิธีในการจัดการ context ไม่ให้เต็มเร็ว:

    1. เขียน prompt ให้เจาะจง
    2. ใช้ CLAUDE.md
    3. ปิดใช้งาน skills and MCPs
    4. ใช้ subagent
    5. ลบหรือสรุปข้อมูล

    .

    ✏️ วิธีที่ 1. เขียน Prompt ให้เจาะจง

    เขียน prompt ให้ชัดเจนและเจาะจง เพื่อที่ Claude จะไม่ต้องคิดทบทวนคำสั่งหลายรอบ

    ❌ ตัวอย่าง prompt ที่เปลือง context:

    แก้ code ให้หน่อย

    เพราะ Claude จะต้องคิดก่อนลงมือทำ:

    • มองหาจุดไหนที่ต้องแก้
    • คิดว่าแก้เพื่ออะไร
    • คิดว่าแก้ยังไง
    • ลงมือแก้

    ทำให้เปลือง context โดยไม่จำเป็น

    ✅ ตัวอย่าง prompt ที่ประหยัด context:

    แก้ calculate_sum() ในไฟล์ @calculator.py
    เพื่อ handle data type ที่ไม่ใช่ int หรือ float

    เพราะ Claude สามารถคิดและลงมือแก้ได้เลย โดยตีกรอบบริบทว่าอะไรที่ต้องแก้บ้าง

    .

    📑 วิธีที่ 2. ใช้ CLAUDE.md

    ใส่บริบทและสิ่งที่ Claude ควรรู้ไว้ใน CLAUDE.md ซึ่งเป็นไฟล์กำกับการทำงานของ Claude เพื่อที่ Claude จะได้เข้าใจบริบทในการทำงานได้โดยไม่ต้องอ่าน code ทั้งหมดใหม่ทุกครั้ง

    ไม่มี CLAUDE.mdมี CLAUDE.md
    อ่าน code ทั้งหมดเพื่อเข้าใจบริบทอ่านแค่ CLAUDE.md

    2 วิธีในการสร้าง CLAUDE.md:

    1. สั่งให้ Claude สร้างให้ ด้วยคำสั่ง /init (แนะนำ)
    2. สร้างเองใน text editor

    ตัวอย่าง CLAUDE.md:

    # Project: My To-Do App
    ## What this is
    A simple to-do list app.
    Frontend only, no backend yet.
    Built with HTML, CSS, and vanilla JavaScript.
    ## Do
    - Keep the code in plain JavaScript
    - Explain any code changes in plain English before making them
    - Always test in the browser before marking tasks as complete
    ## Don't
    - Don't use React, Vue, or any framework
    - Don't rewrite the whole file for a small fix
    - Don't delete comments that explain "why", only "what"

    .

    ⛏️ วิธีที่ 3. ปิดใช้งาน Skill และ MCP

    ปิด skill และ MCP ที่ไม่ใช้งาน

    Claude จะได้รับข้อมูล skill และ MCP ที่เปิดใช้งานทุกครั้ง ไม่ว่า skill หรือ MCP เหล่านี้ถูกใช้งานหรือไม่ก็ตาม

    ดังนั้น เมื่อเราไม่ปิด skill และ MCP ที่เราไม่ใช้แล้ว เราจะเสีย context ไปโดยไม่จำเป็น

    คำสั่งสำหรับปิด skill และ MCP:

    • /skills เพื่อจัดการ Skill
    • /mcp เพื่อจัดการ MCP

    .

    🕵️‍♀️ วิธีที่ 4. ใช้ Subagent

    ใช้งาน subagent หรือ AI agent ที่จะทำงานเฉพาะอย่าง

    Subagent มี context ที่แยกต่างหากจาก main agent ทำให้การทำงานของ subagent ไม่นำมาคิดเป็น context ของ main agent

    วิธีใช้ subagent:

    1. ใช้คำสั่ง /agents เพื่อจัดการ subagent (เช่น สร้างหรือเปิด-ปิดการใช้งาน)
    2. เรียกใช้ subagent ด้วยคำสั่ง @ ตามด้วยชื่อ subagent (เช่น @code-reviewer).

    .

    💻 วิธีที่ 5. ลบหรือสรุปข้อมูล

    ลบหรือสรุปข้อมูล เพื่อเพิ่มพื้นที่ใน context

    3 คำสั่งสำหรับลบและสรุปข้อมูล:

    1. /context: ดูสิ่งที่เก็บไว้ใน context
    2. /clear: ลบข้อมูลใน context
    3. /compact: สรุปข้อมูลใน context

    เมื่อไรควรลบและเมื่อไรควรสรุปข้อมูล?

    • ลบเมื่อ context เต็ม และไม่ต้องการทำงานต่อจากเดิม (เช่น พัฒนา feature ใหม่)
    • สรุปเมื่อ context เต็ม แต่ต้องทำงานต่อจากเดิม (เช่น ปรับปรุง feature เดิม)

    Note: ข้อเสียของการสรุป คือ Claude อาจจะลืมรายละเอียดเล็ก ๆ น้อย ๆ ไปได้


    💪 บทสรุป

    Context เก็บข้อมูลเพื่อให้ Claude เข้าใจบริบทและสามารถทำงานได้อย่างมีประสิทธิภาพ

    5 วิธีในการจัดการ context ไม่ให้เต็ม:

    1. ใช้ prompt ที่มีความเจาะจง
    2. เก็บสิ่งที่ Claude ควรรู้ไว้ใน CLAUDE.md
    3. ปิด skill และ MCP ที่ไม่ใช้งาน
    4. ใช้ subagent ทำงานย่อย
    5. ลบหรือสรุปข้อมูลที่ใน context

    🫵 Your Turn

    หลังอ่านบทความนี้แล้ว ลองมาใช้ Claude Code กันดูนะครับ:


    📃 References

  • 30+ คำสั่ง Claude Code ที่ควรรู้ พร้อมวิธีใช้งาน

    30+ คำสั่ง Claude Code ที่ควรรู้ พร้อมวิธีใช้งาน

    Updated: 13 Sep 2026

    .

    Claude Code เป็น AI coding assistant ที่เราใช้ช่วยเขียน code ลงบนคอมพิวเตอร์ได้โดยตรง

    แม้ว่า Claude Code จะรองรับการทำงานกับภาษาธรรมชาติ (natural language) แต่เราสามารถสั่งงาน Claude ผ่าน command line หรือคำสั่งลัดใน Terminal ได้ด้วยเช่นกัน

    ในบทความนี้ ผมจะพาไปดู 30+ คำสั่งที่ทุกคนควรรู้สำหรับการใช้งาน Claude Code ให้ได้อย่างมืออาชีพกัน

    ทั้ง 30+ คำสั่งแบ่งได้เป็น 3 ส่วน:

    1. Basic: คำสั่งพื้นฐานในการทำงานกับ Claude (14 คำสั่ง)
    2. Intermediate: คำสั่งที่ช่วยให้ Claude ตอบโจทย์เรามากขึ้น (12 คำสั่ง)
    3. Advanced: คำสั่งที่ทำให้ Claude ทำงานโดยอัตโนมัติ (8 คำสั่ง)

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย



    😎 Claude Code Command Cheat Sheet

    คำสั่งใช้เพื่อ
    claudeเริ่มใช้งาน Claude Code และสร้าง session ใหม่
    ! <command>รัน shell command ระหว่างใช้งาน Claude Code
    @<file>ชี้ไฟล์ให้ Claude Code
    /modelเลือก model ที่ต้องการใช้งาน
    /effortกำหนดระดับการคิดของ Claude Code
    /exitออกจาก session
    /planวางแผนการทำงานก่อนเริ่มเขียนหรือแก้ไข code
    /initสร้างไฟล์ CLAUDE.md สำหรับกำหนดบริบทและแนวทางการทำงาน
    /memoryเปิด CLAUDE.md เพื่อดูหรือแก้ไข และจัดการ auto-memory
    /btwคุยกับ Claude ระหว่างที่ Claude กำลังทำงาน
    /diffดูการเปลี่ยนแปลงของ code
    /rewindย้อน session กลับไปขั้นก่อนหน้า
    /review หรือ /code-reviewตรวจสอบ code และแนะนำการแก้ไข
    /helpดูคำสั่งและวิธีใช้งาน Claude Code
    claude -cกลับไปใช้งาน session ล่าสุด
    claude --resume <session-name>กลับไปใช้งาน session ที่ต้องการ
    /resumeเลือก session ที่ต้องการกลับไปใช้งานต่อ
    /rename <session-name>เปลี่ยนชื่อ session
    /contextดู context ที่ถูกใช้ไปใน session
    /compactสรุป context เพื่อเพิ่มพื้นที่สำหรับการทำงานต่อ
    /clearลบข้อมูลใน context ของ session ปัจจุบัน
    /skillsดูและจัดการ skills ที่ Claude Code ใช้งานได้
    /mcpดูและจัดการ MCP ที่ Claude Code ใช้งานได้
    /agentsดูวิธีจัดการ subagents
    /pluginดูและจัดการ plugins ของ Claude Code
    /usage หรือ /costดูปริมาณการใช้งาน Claude Code
    /goalกำหนดเป้าหมายให้ Claude Code ทำงานต่อเนื่องจนสำเร็จ
    /batchแบ่งงานขนาดใหญ่เป็นงานย่อยและทำงานแบบขนาน
    /scheduleตั้งเวลาให้ Claude Code ทำงานตามกำหนด
    /loopให้ Claude Code ทำงานซ้ำตามช่วงเวลาที่กำหนด
    /backgroundย้ายงานไปทำงานเบื้องหลัง เพื่อใช้ Terminal ต่อได้
    /tasksดูรายการงานที่กำลังทำงานอยู่เบื้องหลัง
    /doctorตรวจสอบการตั้งค่า Claude Code เมื่อเกิดปัญหา
    /debugช่วยค้นหาและอธิบายสาเหตุของปัญหา

    ดาวน์โหลด cheat sheet ได้ด้านล่าง 👇:

    ดูรายละเอียดของแต่ละคำสั่งด้านล่าง


    1️⃣ Part 1. Basic: คำสั่งพื้นฐานสำหรับเริ่มใช้ Claude Code

    14 คำสั่งในกลุ่มแรกเป็นคำสั่งพื้นฐานสำหรับเริ่มใช้งาน Claude Code และแบ่งเป็น 4 กลุ่มย่อยตามลักษณะการใช้งาน:

    1. เริ่มใช้งาน
    2. เขียน code
    3. รีวิวงาน
    4. ดูคู่มือ

    .

    🏁 Group 1. คำสั่งเริ่มใช้งาน

    👉 คำสั่งที่ 1. claude:

    เริ่ม session ใช้งาน Claude

    (1 session เท่ากับ 1 ห้องแชทในการคุยกับ Claude)

    ในแต่ละ session, Claude มีความจำ (context) ที่แยกออกจากกัน ทำให้สิ่งที่เราคุยในแต่ละ session ไม่ปะปนกัน

    .

    👉 คำสั่งที่ 2. ! <command>:

    รัน shell command ระหว่างใช้งาน Claude Code

    ตัวอย่าง:

    Use ! to execute shell command in Claude Code

    .

    👉 คำสั่งที่ 3. @<file>:

    ชี้ไฟล์ให้ Claude

    ใช้เมื่อต้องการให้ Claude ทำงานกับไฟล์

    ตัวอย่าง:

    Use @ to refer to file in Claude Code

    .

    👉 คำสั่งที่ 4. /model:

    เลือก model ที่จะใช้งาน เช่น:

    1. Haiku
    2. Sonnet
    3. Opus
    4. Fable

    ตัวอย่าง:

    Use /model to select model in Claude Code

    .

    👉 คำสั่งที่ 5. /effort:

    กำหนดระดับการคิดของ Claude Code

    ยิ่งสูง Claude Code จะยิ่งทำงานที่ซับซ้อนได้ดีขึ้น แต่ก็ใช้ token มากขึ้นเช่นกัน

    ตัวอย่าง:

    Use /effort to adjust thinking parametre in Claude Code

    .

    👉 คำสั่งที่ 6. /exit:

    ออกจาก session

    ใช้เมื่อทำงานเสร็จแล้ว

    .

    💻 Group 2. คำสั่งสำหรับเขียน Code

    👉 คำสั่งที่ 7. /plan:

    เข้าโหมดวางแผน (plan mode) เพื่อกำหนดทิศทางการทำงานก่อนเขียน code จริง ซึ่งจะทำให้ Claude เขียน code ได้ตอบโจทย์มากขึ้น

    Note: กด shift + tab เพื่อเปลี่ยนโหมดได้เหมือนกัน

    ตัวอย่าง:

    Use /plan to enter plan mode in Claude Code

    .

    👉 คำสั่งที่ 8. /init:

    ให้ Claude อ่านไฟล์ในโฟลเดอร์ที่ใช้งานอยู่ เพื่อเขียน CLAUDE.md ซึ่งเป็นไฟล์ที่กำหนดบริบทการทำงานให้กับ Claude

    ตัวอย่าง CLAUDE.md:

    # Code style
    - Follow PEP 8; format with `black` before committing
    - Use type hints on all function signatures
    - Prefer pandas vectorized operations over row-wise loops
    # Workflow
    - Activate the venv with `source .venv/bin/activate` before running scripts
    - Run `pytest tests/` after any change to `src/`
    - Keep notebooks out of version control; convert final logic into `.py` modules

    .

    👉 คำสั่งที่ 9. /memory:

    ตั้งค่าการอัปเดตความจำของ Claude โดยอัตโนมัติ (auto-memory) หรือแก้ไข CLAUDE.md

    ตัวอย่าง:

    Use /memory to toggle auto-memory on/off and edit CLAUDE.md

    .

    👉 คำสั่งที่ 10. /btw:

    คุยกับ Claude ระหว่างที่ Claude กำลังเขียน code

    ใช้เวลามีคำถามและไม่ต้องการรอให้ Claude ทำงานเสร็จ

    ตัวอย่าง:

    Use /btw to chat with Claude Code while it's still working

    .

    🧐 Group 3. คำสั่งรีวิวงาน

    👉 คำสั่งที่ 11. /diff:

    ดูการเปลี่ยนแปลงของ code ที่เกิดขึ้น

    .

    👉 คำสั่งที่ 12. /rewind:

    เปิดดูประวัติการทำงาน เพื่อย้อน session กลับไปขั้นก่อนหน้า

    ตัวอย่าง:

    Use /rewind to go back in time in Claude Code

    .

    👉 คำสั่งที่ 13. /review หรือ /code-review:

    สั่งให้ Claude ตรวจ code และแนะนำการแก้ไขให้

    ตัวอย่าง:

    Use /code-review to have Claude Code review your code

    .

    📖 Group 4. คำสั่งสำหรับดูคู่มือการใช้งาน

    👉 คำสั่งที่ 14. /help:

    เปิดดูคำสั่งสำหรับใช้ Claude Code

    ตัวอย่าง:

    Use /help to see all available commands in Claude Code

    2️⃣ Part 2. Intermediate: คำสั่งเพิ่มประสิทธิภาพการทำงาน

    12 คำสั่งในกลุ่ม 2 ช่วยเพิ่มประสิทธิภาพการทำงานของ Claude Code และแบ่งได้เป็น 4 กลุ่มย่อย:

    1. คำสั่งจัดการ session
    2. คำสั่งจัดการ context
    3. คำสั่งตั้งค่า Claude Code
    4. คำสั่งดูการใช้งาน Claude Code

    .

    🗣️ Group 1. คำสั่งจัดการ Session

    👉 คำสั่งที่ 15. claude -c:

    กลับไปใช้ session ล่าสุด

    ใช้เมื่อเราออกจาก session และต้องการกลับเข้าไปใช้งานใหม่

    .

    👉 คำสั่งที่ 16. claude --resume <session-name>:

    เปิด session ที่ใช้งาน session ก่อนหน้านี้

    ตัวอย่าง:

    claude --resume my-session

    .

    👉 คำสั่งที่ 17. /resume:

    เปิดดูรายการ session ทั้งหมด เพื่อเลือก session ที่ต้องการกลับไปใช้งานต่อ

    ตัวอย่าง:

    Use /resume to select and go to another session

    หรือใส่ชื่อ session ตามหลัง เพื่อย้ายไป session ที่ต้องการ

    ตัวอย่าง:

    /resume my-session

    .

    👉 คำสั่งที่ 18. /rename <session-name>:

    เปลี่ยนชื่อ session เพื่อให้หา session ได้ง่ายขึ้น

    ตัวอย่าง:

    Use /rename to rename your session in Claude Code

    .

    💬 Group 2. คำสั่งจัดการ Context

    👉 คำสั่งที่ 19. /context:

    เปิดดู context ที่ถูกใช้ไปใน session เพื่อดูว่าเหลือพื้นที่สำหรับการทำงานอีกเท่าไร

    ตัวอย่าง:

    Use /context to see how much session context has been used in Claude Code

    .

    👉 คำสั่งที่ 20. /compact:

    สรุปข้อมูลใน context เพื่อเพิ่มพื้นที่การทำงานให้กับ Claude

    ใช้เมื่อต้องการเพิ่มพื้นที่ โดยไม่เสียบริบทในการทำงานไป เช่น context เต็มแล้ว แต่ต้องการพัฒนา feature ที่มีอยู่ต่อ

    Note: ข้อเสีย คือ Claude จะจำรายละเอียดบางอย่างไม่ได้

    ตัวอย่าง:

    Use /compact to summarise existing session context in Claude Code

    .

    👉 คำสั่งที่ 21. /clear:

    ลบข้อมูลใน context ออกทั้งหมด

    ใช้เพิ่มพื้นที่ใน context เมื่อไม่ต้องการเก็บบริบทในการทำงานที่มีอยู่เอาไว้ เช่น เวลาจะพัฒนา feature ใหม่

    .

    🦾 Group 3. คำสั่งตั้งค่า Claude Code

    👉 คำสั่งที่ 22. /skills:

    เปิดดู skill ที่ Claude เข้าถึงได้

    ใช้จัดการ skill ที่ Claude เรียกใช้งานได้

    ตัวอย่าง:

    Use /skills to view all skills that Claude Code has access to in the current session

    .

    👉 คำสั่งที่ 23. /mcp:

    เปิดดู MCP (Model Context Protocol) หรือเครื่องมือ/ข้อมูลภายนอกที่ Claude เข้าถึงได้

    ใช้จัดการ MCP ที่ Claude เรียกใช้งานได้

    ตัวอย่าง:

    Use /mcp to view all available MCP that Claude Code has access to in the current session

    .

    👉 คำสั่งที่ 24. /agents:

    เปิดดูวิธีสร้างและแก้ไข subagent ที่ Claude สามารถเข้าถึงได้

    ตัวอย่าง:

    Use /agents to view how to create a sub-agent in Claude Code

    .

    👉 คำสั่งที่ 25. /plugin:

    เปิดดู plugin ที่ Claude ใช้งานได้

    ใช้สำหรับจัดการ plugin ที่ Claude เรียกใช้งานได้

    ตัวอย่าง:

    Use /plugin to view all available plugins that Claude Code has access to in the current session

    .

    🪙 Group 4. คำสั่งดูการใช้งาน Claude Code

    👉 คำสั่งที่ 26. /usage หรือ /cost:

    ดูปริมาณการใช้งาน Claude เช่น เงินที่ใช้ไป หรือจำนวนข้อความคงเหลือที่ใช้ได้

    ตัวอย่าง:

    Use /usage to view your Claude Code usage and limits

    3️⃣ Part 3. Advanced: คำสั่งขั้นสูงสำหรับทำงานอัตโนมัติ

    8 คำสั่งขั้นสูงในกลุ่มสุดท้าย ใช้สำหรับสั่งให้ Claude ทำงานโดยอัตโนมัติ และแก้ปัญหาการใช้งาน Claude

    ทั้ง 8 คำสั่งแบ่งเป็น 2 กลุ่ม:

    1. คำสั่งสำหรับทำงานอัตโนมัติ
    2. คำสั่งแก้ปัญหาการใช้งาน Claude Code

    .

    🤖 Group 1. คำสั่งสำหรับทำงานอัตโนมัติ

    👉 คำสั่งที่ 27. /goal:

    กำหนดเป้าหมายที่ต้องการ โดย Claude จะทำงานไปเรื่อย ๆ จนกว่าจะไปถึงเป้าหมายนี้

    เหมาะกับงานที่ต้องการให้ Claude ทำไปเอง โดยไม่ต้องมีเราอยู่ด้วยระหว่างทาง

    ตัวอย่าง:

    Use /goal to set a goal for Claude Code to work towards to without your supervision

    .

    👉 คำสั่งที่ 28. /batch:

    สั่งให้ Claude แตกงานขนาดใหญ่เป็นงานย่อย ๆ และรันงานแบบขนานกันไป

    เหมาะสำหรับการแก้ code หลายจุดพร้อมกัน

    .

    👉 คำสั่งที่ 29. /schedule:

    ตั้งเวลาที่จะให้ Claude ทำงานบางอย่าง

    เช่น ทดสอบ code ทุก 10 โมงเช้า

    .

    👉 คำสั่งที่ 30. /loop:

    สั่งให้ Claude ทำงานตามช่วงเวลาที่กำหนด

    เช่น ทดสอบ code ทุก ๆ 1 ชั่วโมง

    .

    👉 คำสั่งที่ 31. /background:

    ย้าย session ไปอยู่ใน background ซึ่งจะทำให้เราใช้ Terminal ได้ในขณะที่ Claude Code ยังทำงานอยู่

    .

    👉 คำสั่งที่ 32. /tasks:

    เปิดดูรายการ task และ subagent ที่รันอยู่ใน background

    ตัวอย่าง:

    Use /tasks to view all tasks and sub-agents running in the background

    .

    🩺 Group 2. คำสั่งแก้ปัญหาการใช้งาน Claude Code

    👉 คำสั่งที่ 33. /doctor:

    ตรวจสอบการตั้งค่า Claude Code เมื่อมีปัญหาการใช้งาน

    ตัวอย่าง:

    Use /doctor to diagnose what's wrong with Claude Code

    .

    👉 คำสั่งที่ 34. /debug:

    สั่งให้ Claude ค้นหาและอธิบายสาเหตุของปัญหา ผ่านการอ่าน session log

    ตัวอย่าง:

    Use /debug to debug what's wrong with Claude Code

    👀 หลังจบบทความนี้

    หลังอ่านบทความนี้แล้ว ลองมาใช้ Claude Code กันดูนะครับ:


    📃 อ้างอิง

  • สรุป 25 บทเรียนจาก 5 เวทีที่น่าสนใจ ในงาน “How Do I Live? อยู่อย่างไร เมื่อ AI ล้อมไว้หมดแล้ว”: What to Build, Writing, Creativity, AI Can Free Up Time, & Philosophy

    สรุป 25 บทเรียนจาก 5 เวทีที่น่าสนใจ ในงาน “How Do I Live? อยู่อย่างไร เมื่อ AI ล้อมไว้หมดแล้ว”: What to Build, Writing, Creativity, AI Can Free Up Time, & Philosophy

    “How Do I Live? อยู่อย่างไร เมื่อ AI ล้อมไว้หมดแล้ว” เป็นงานที่รวม speaker มาตอบคำถามที่หลาย ๆ คนอาจจะสงสัย ว่าในยุคที่ AI ทำได้เกือบเหมือนมนุษย์ทุกอย่าง เราจะอยู่ยังไง

    ในบทความนี้ ผมจะมาสรุป 25 บทเรียนจาก 5 เวทีที่น่าสนใจในงาน ที่จะตอบคำถามนี้กัน

    โดยบทความแบ่งออกเป็น 5 ส่วน:

    1. What to build
    2. Writing
    3. Creativity
    4. AI can free up time
    5. Philosophy

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. 🤨 Session 1. What to Build
      1. 🔶 Lesson 1. AI Can Build Anything. Doesn’t Mean Everything Should be Built
      2. 🔶 Lesson 2. TADA: Challenges to Building With AI
      3. 🔶 Lesson 3. KEY: What You Should Build With AI
      4. 🔶 Lesson 4. Focus on Wealth
      5. 🔶 Lesson 5. Takeaway on What to Build
    2. ✏️ Session 2. Writing
      1. 🔶 Lesson 6. Human Writing Can Still Win
      2. 🔶 Lesson 7. The Secret Formula
      3. 🔶 Lesson 8. P for Process
      4. 🔶 Lesson 9. V for Value
      5. 🔶 Lesson 10. E1 for Experiences
      6. 🔶 Lesson 11. E2 for Empathy
    3. 🎨 Session 3. Creativity
      1. 🔶 Lesson 12. Will AI Replace Human Creativity?
      2. 🔶 Lesson 13. Slow/Stop, Not Fast
      3. 🔶 Lesson 14. Question, Not Answer
      4. 🔶 Lesson 15. Quality, Not Quantity
      5. 🔶 Lesson 16. AI = Adaptability Intelligence
      6. 🔶 Lesson 17. Think Outside the Bot
    4. 🕦 Session 4. AI Can Free Up Time
      1. 🔶 Lesson 18. AI Can Free Up Time
      2. 🔶 Lesson 19. Paypers as Example
      3. 🔶 Lesson 20. Next Question After AI
    5. 🧘 Session 5. Philosophy
      1. 🔶 Lesson 21. Agency
      2. 🔶 Lesson 22. What to Choose
      3. 🔶 Lesson 23. What Feels Like Play
      4. 🔶 Lesson 24. Do What’s Right
      5. 🔶 Lesson 25. Become the Best Version of Yourself

    🤨 Session 1. What to Build

    จาก “ถ้าคุณสร้างอะไรก็ได้ คุณจะสร้างอะไร” โดย สิทธินันท์ พลวิสุทธิ์ศักดิ์ (แบงค์)

    .

    🔶 Lesson 1. AI Can Build Anything. Doesn’t Mean Everything Should be Built

    AI สร้างอะไรให้เราก็ได้

    แค่เรารู้ว่าต้อง prompt ยังไง เราก็จะได้สิ่งที่ต้องการภายในไม่กี่คลิก

    แต่เพียงเพราะเราสร้างได้ ไม่ได้หมายความว่า เราจะต้องสร้าง

    และไม่ได้หมายความว่า ทุกอย่างจะสร้างง่ายเหมือนเสกได้

    .

    🔶 Lesson 2. TADA: Challenges to Building With AI

    การสร้างด้วย AI มีความท้าทาย 4 ข้อ:

    ข้อ 1. T = Technical difficulties:

    • เราต้องมีความรู้ทางเทคนิค
    • ต้องรู้ว่าสิ่งที่ AI สร้างขึ้นเกิดขึ้นได้ยังไง
    • เช่น ให้ AI สร้างเว็บไซต์ เราต้องมีความรู้เกี่ยวกับ domain, server, hosting, etc.

    ข้อ 2. A = Acquisition channel:

    • สร้างได้ ≠ จะมีคนใช้
    • ถ้าเราไม่มีช่องทางเข้าถึง หรือของไม่ตอบโจทย์ สิ่งที่สร้างมาก็ไร้ความหมาย

    ข้อ 3. D = Domain expertise:

    • เราต้องมีความรู้ในสิ่งที่สร้าง
    • เช่น ถ้าเราจะสร้าง medical chatbot เราต้องมีความรู้ทางการแพทย์มากพอที่จะตรวจสอบ bot ของเราได้

    ข้อ 4. A = Authority:

    • เราต้องมีความน่าเชื่อถือมากพอ เพื่อให้สิ่งที่เราสร้างดูน่าเข้าหา

    .

    🔶 Lesson 3. KEY: What You Should Build With AI

    สิ่งที่เราควรสร้างกับ AI มีอยู่ 3 อย่าง:

    ข้อ 1. K = Knowledge:

    • ความรู้ที่จะช่วยให้เราก้าวไปข้างหน้าอยู่เสมอ

    ข้อ 2. E = Engine:

    • ใช้ AI ต่อยอดสิ่งที่เรามี

    ข้อ 3. Y = Why:

    • ใช้ AI ค้นหาคุณค่าของสิ่งที่ต้องสร้าง

    .

    🔶 Lesson 4. Focus on Wealth

    AI ควรช่วยสร้าง wealth ให้กับเรา ไม่ว่าจะเป็น:

    ข้อ 1. Time:

    • อิสระในการเลือกใช้เวลาด้วยตัวเอง

    ข้อ 2. Social:

    • ความสัมพันธ์ทางสังคมที่ดี

    ข้อ 3. Mental:

    • ความสงบในจิตใจและศักยภาพในการเติบโต

    ข้อ 4. Physical:

    • สุขภาพทางกายที่ดี

    ข้อ 5. Financial:

    • ความร่ำรวยทางการเงิน

    .

    🔶 Lesson 5. Takeaway on What to Build

    ข้อ 1. Process over result:

    • Focus ที่ขั้นตอนและกระบวนการมากกว่าผลลัพธ์ที่ได้

    ข้อ 2. Bite size:

    • หาโจทย์ที่ทำได้พอตัว ไม่ต้องเป็นโจทย์ที่เปลี่ยนโลก

    ✏️ Session 2. Writing

    จาก “เขียนให้ชนะ AI” โดย วีรยา ถาวรวัฒนยงค์ (ลุงพล)

    .

    🔶 Lesson 6. Human Writing Can Still Win

    ในยุคที่ AI สร้าง content ได้ง่ายและเร็ว และคนมี short attention span การเขียน long-form ยังสร้าง engagement และรายได้หลักล้านได้

    .

    🔶 Lesson 7. The Secret Formula

    สูตรสำเร็จในการเขียน คือ

    PVE^2

    .

    🔶 Lesson 8. P for Process

    เน้นที่ขั้นตอน/กระบวนการ ไม่ใช่ผลลัพธ์

    แม้ AI สร้างรูปได้สวยและหลากหลาย แต่ก็สู้เอกลักษณ์ของจิตรกรที่นั่งลงพู่กันในงานแต่งสด ๆ ไม่ได้

    .

    🔶 Lesson 9. V for Value

    คุณค่าที่ต้องการส่งต่อ

    อย่างในกรณีลุงพล คือ การส่งต่อสรุปเนื้อหาดี ๆ ถึงคนที่ไม่ได้เข้าร่วมงาน ให้เหมือนได้ไปอยู่ในงานด้วยตัวเอง

    .

    🔶 Lesson 10. E1 for Experiences

    งานเขียนขึ้นอยู่กับประสบการณ์ที่นักเขียนถ่ายทอดลงในงานเขียน

    ประสบการณ์ของมนุษย์เป็นสิ่งที่ AI ยัง copy ไม่ได้ และเป็นสิ่งที่สร้าง individuality ให้กับงานเขียนของเรา

    .

    🔶 Lesson 11. E2 for Empathy

    ตัวแปรสุดท้ายในสมการลับ คือ empathy

    ความสามารถที่จะทำให้คนอ่านเข้าใจสิ่งที่เราต้องการสื่อสาร ทั้งเนื้อหาและบรรยากาศงาน เหมือนกับเขาได้ไปนั่งอยู่ในงานกับเรา


    🎨 Session 3. Creativity

    จาก “AI Is the End of Human Creativity” โดย สิทธิพงศ์ ศิริมาศเกษม

    .

    🔶 Lesson 12. Will AI Replace Human Creativity?

    Creativity ของมนุษย์ยังคงมีที่ยืนในยุคที่ AI สร้างงานที่มนุษย์ใช้เวลาครึ่งวันทำได้ภายใน 3 นาที ด้วยเหตุผล 3 ข้อ:

    1. Slow/stop, not fast
    2. Question, not answer
    3. Quality, not quantity

    .

    🔶 Lesson 13. Slow/Stop, Not Fast

    Creativity ไม่ได้เกิดจากความเร็ว

    แต่เกิดจากการคิดให้ช้าหรือหยุดคิด

    ความคิดดี ๆ มักจะเกิดขึ้นตอนที่เราไม่ได้จดจ่อกับงาน แต่ออกไปเดินเล่นหรือทำกิจกรรมอื่น

    .

    🔶 Lesson 14. Question, Not Answer

    Creativity ไม่ได้เกิดจากคำตอบ

    แต่มาจากการตั้งคำถามที่ดี

    .

    🔶 Lesson 15. Quality, Not Quantity

    AI สามารถคิด option จำนวนมากให้เราได้ภายในเวลาอันสั้น

    แต่ creativity ไม่ได้เกิดจากจำนวนของ option

    แต่จากการคัดกรองไอเดียที่ใช่และเชื่อมโยงกัน

    .

    🔶 Lesson 16. AI = Adaptability Intelligence

    แม้ AI จะมาแทน human creativity ไม่ได้ แต่ AI ช่วยให้เราพัฒนาและปรับตัวเข้ากับยุคต่าง ๆ ได้

    ในมุมนี้ AI จะไม่ใช่ artificial intelligence แต่เป็น adaptability intelligence

    .

    🔶 Lesson 17. Think Outside the Bot

    ในยุคที่ AI คิดแทนเราได้ สิ่งที่จะทำให้เราแตกต่างออกไป คือ การคิดนอกบอท

    นั่นคือ การคิดด้วยตัวเอง และไม่พึ่งพา AI จนมากเกินไป


    🕦 Session 4. AI Can Free Up Time

    จาก “How AI Helps Startups: สร้างธุรกิจให้เร็วขึ้นด้วย AI” โดย อภิญญา สหัสสะรังษี (พลอย Paypers)

    .

    🔶 Lesson 18. AI Can Free Up Time

    เป็นครั้งแรกในประวัติศาสตร์มนุษย์ที่เทคโนโลยีเอางานไปจากเรา

    AI ช่วยลดงานและคืนเวลาในชีวิตให้กับเราได้

    .

    🔶 Lesson 19. Paypers as Example

    ตัวอย่างเช่น Paypers ที่ช่วยจัดเก็บและทำเอกสารบัญชี ช่วยลดงาน routine และปล่อยเวลา 30–60% ให้กับเจ้าของธุรกิจได้ทำงานที่มีคุณค่ามากกว่า เช่น:

    • หาไอเดียใหม่ ๆ
    • วางแผน วางระบบให้ธุรกิจเติบโต

    .

    🔶 Lesson 20. Next Question After AI

    คำถามที่ตามมาจากการที่ AI ปลดงานออกจากเรา คือ เราจะใช้เวลาที่ได้มาไปกับอะไร?


    🧘 Session 5. Philosophy

    จาก “One Person Business: ธุรกิจคนเดียวที่ไม่ทำคนเดียว” โดย อานันท์ ตรีฐิติพัฒน์, กษิดิศ สตางค์มงคล, และทีปกร วุฒิพิทยามงคล

    .

    🔶 Lesson 21. Agency

    Agency คือ ความสามารถในการเลือกทางเดินของตัวเอง

    คนเราเกิดมาพร้อมกับ agency ไม่ว่าจะกินจะนอนเวลาไหนก็ไม่มีใครสั่งเราได้

    แต่สังคมหล่อหลอมให้เรากลายเป็น version ที่มี agency น้อยลง

    .

    🔶 Lesson 22. What to Choose

    ถ้าจะต้องเลือกทางเดิน เราจะเลือกไปทางไหน?

    คำตอบมีอยู่ 3 อย่าง:

    1. What feels like play
    2. What’s right
    3. Better version of yourself

    .

    🔶 Lesson 23. What Feels Like Play

    เลือกทำในสิ่งที่เป็นเหมือนการเล่นสำหรับเรา แต่เป็นงานสำหรับคนอื่น

    เช่น งานออกแบบ UX ที่เรารู้สึกสนุก แต่คนอื่นมองว่าเป็นงาน UX design เป็นสิ่งที่เราควรเลือก เพราะเป็นสิ่งที่เราจะสร้างคุณค่าให้กับโลกได้อย่างยั่งยืน

    Find what feels like play to you, but looks like work to others. — Naval Ravikant

    .

    🔶 Lesson 24. Do What’s Right

    ทำในสิ่งที่ถูกต้อง เพราะสิ่งที่ไม่ดีกับส่วนรวมก็ไม่ดีกับปัจเจกบุคคล และสิ่งที่เราทำจะกลับมาหาเราเอง

    What brings no benefit to the hive brings none to the bee. — Marcus Aurelius

    .

    🔶 Lesson 25. Become the Best Version of Yourself

    ทำสิ่งที่จะทำให้เราเป็น version ที่ดีขึ้นของตัวเองในทุก ๆ วัน

    There is nothing noble in being superior to your fellow man; true nobility is being superior to your former self. — Ernest Hemingway

  • วิธีใช้ Haystack 2.0 เชื่อมต่อ LLM ด้วย Python ใน 4 ขั้นตอน

    วิธีใช้ Haystack 2.0 เชื่อมต่อ LLM ด้วย Python ใน 4 ขั้นตอน

    ในบทความก่อน ผมพาทุกคนไปรู้จักกับการใช้ LangChain ซึ่งเป็น package ยอดนิยมสำหรับสร้าง LLM (large language model) application อย่าง chatbot

    ในบทความนี้ ผมจะพาทุกคนไปรู้จักกับ Haystack 2.0 ซึ่งเป็นอีกหนึ่ง package ที่ทำงานได้เหมือนกับ LangChain

    แม้ว่า LangChain เป็นที่นิยมและใช้งานง่าย แต่ก็แลกมาด้วยข้อจำกัดใน “การมองเห็น” เพราะ LangChain ซ่อนการทำงานหลาย ๆ อย่างไว้หลัง code ไม่กี่บรรัดเพื่อให้เราใช้งานง่าย เมื่อเกิด bug ขึ้นมา เราจะไม่รู้เลยว่า error อยู่ที่จุดไหน จนกว่าเราจะแกะการทำงานเบื้องหลัง code ที่เราเขียนออกมา

    Haystack เป็น package ที่มี “การมองเห็น” ที่สูงกว่า เพราะมีการทำงานแบบแยกส่วนประกอบ หรือ component เหมือนกับรถยนต์ที่ประกอบขึ้นมาจากหลายชิ้นส่วน อย่างเครื่องยนต์ ถังน้ำมัน ล้อ และพวงมาลัย เราสามารถกำหนดได้ว่า เราอยากได้ชิ้นส่วนไหนแบบไหน และแต่ละชิ้นส่วนจะประกอบกันยังไง เมื่อเป็นเช่นนี้ เราจะรู้ได้ว่าข้อผิดพลาดอยู่ที่ไหน เพราะเราเป็นคนที่ประกอบทุกอย่างขึ้นมาเอง

    ในบทความนี้ ผมจะพาทุกคนไปดูการใช้ Haystack เพื่อเชื่อมต่อกับ LLM ผ่านตัวอย่างการสร้าง AI ตอบคำถามเกี่ยวกับ mental model กัน

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. ⭐️ High-Level View
    2. 📜 Step 1. Create a Prompt Builder
    3. 🤖 Step 2. Create an LLM Instance
    4. 🪈 Step 3. Create a Pipeline
    5. 🏃 Step 4. Run
    6. 💪 Summary
    7. 😺 GitHub
    8. 📃 References

    ⭐️ High-Level View

    ในการใช้ Haystack เชื่อมต่อกับ LLM เราจะใช้ component 3 อย่าง:

    1. Prompt builder สำหรับเก็บ prompt
    2. LLM instance สำหรับเรียก LLM
    3. Pipeline สำหรับเชื่อมทุก component เข้าด้วยกัน

    โดยเราจะเรียกใช้งาน 3 components นี้ใน 4 ขั้นตอน:

    1. Create a prompt builder
    2. Create an LLM instance
    3. Create a pipeline
    4. Run

    ไปดูตัวอย่างทั้ง 4 ขั้นตอนกัน


    📜 Step 1. Create a Prompt Builder

    ในขั้นแรก เราจะสร้าง prompt builder สำหรับเก็บ prompt กัน

    เราจะเริ่มจากสร้าง system prompt และ user prompt:

    Python
    # System prompt
    system_prompt = """
    You are an expert curator of mental models across science, philosophy, and applied reasoning.
    Your task is to explain mental models clearly and accurately using a fixed schema.
    If the origin of a model is unclear or debated, state that explicitly.
    Do not invent historical sources. Be concise and concrete.
    """
    # User prompt
    user_prompt = "Explain the following mental model: {{model_query}}"

    Note: เราใช้ {{}} ใน user prompt เพื่อที่เราจะสร้างแทนที่ค่าใน user prompt ได้ในภายหลัง เช่น ถ้าเราต้องการถามเกี่ยวกับ “First-Principles Thinking” user prompt ของเราก็จะกลายเป็น:

    "Explain the following mental model: First-Principles Thinking"

    จากนั้น เราจะใส่ prompts ลงใน ChatPromptBuilder():

    Python
    # Import required packages
    from Haystack.components.builders import ChatPromptBuilder
    from Haystack.dataclasses import ChatMessage
    # Create a prompt builder instance
    prompt_builder = ChatPromptBuilder(
    template=[
    ChatMessage.from_system(system_prompt.strip()),
    ChatMessage.from_user(user_prompt)
    ],
    required_variables=["model_query"]
    )

    Note: เรากำหนด required_variables เพื่อกันไม่ให้ {{model_query}} เป็นค่าว่าง


    🤖 Step 2. Create an LLM Instance

    ในขั้นที่ 2 เราจะสร้าง LLM instance สำหรับเชื่อมต่อกับ LLM โดยในตัวอย่าง เราจะใช้ Gemini

    เราจะเริ่มจากดึง API key ที่เก็บไว้ใน .env:

    Python
    # Import required packages
    import os
    # Retrieve the API key
    GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")

    จากนั้น กำหนด structured output หรือหน้าตา output ที่เราต้องการ ด้วย Pydantic:

    Python
    # Import required packages
    from pydantic import BaseModel, Field
    from typing import List, Literal
    # Define the output schema
    class MentalModel(BaseModel):
    # Mental model name
    model_name: str = Field(
    description="The commonly accepted name of the mental model"
    )
    # Origin or source
    origin: str = Field(
    description="Where the model comes from (a person, book, field, or cultural origin)"
    )
    # Brief description
    description: str = Field(
    description="A brief explanation of what the mental model is and why it matters"
    )
    # Example
    example: str = Field(
    description="A concrete real-world example illustrating the mental model"
    )
    # Tags
    tags: List[str] = Field(
    description="Short tags such as decision-making, systems thinking, learning, and philosophy"
    )

    หน้าตา structured output ที่เราจะได้:

    {
    "model_name": "",
    "origin": "",
    "description": "",
    "example": "",
    "tags": []
    }

    สุดท้าย เราจะใส่ API key และ structured output ลงใน LLM instance:

    Python
    # Import required packages
    from Haystack.utils import Secret
    from Haystack_integrations.components.generators.google_genai import GoogleGenAIChatGenerator
    # Create an LLM instance
    llm_with_structured_output = GoogleGenAIChatGenerator(
    model="gemini-2.5-flash",
    api_key=Secret.from_token(GEMINI_API_KEY),
    generation_kwargs={
    "temperature": 0.5,
    "response_format": MentalModel
    }
    )

    🪈 Step 3. Create a Pipeline

    ในขั้นที่ 3 เราจะเชื่อมต่อ prompt builder และ LLM instance เข้าด้วยกัน

    เริ่มจากสร้าง pipeline:

    Python
    # Import the required package
    from Haystack import Pipeline
    # Create a pipeline instance
    chain = Pipeline()

    เพิ่ม prompt builder และ LLM instance ลงใน pipeline:

    Python
    # Add the prompt builder and LLM instances
    chain.add_component(
    "prompt_builder", # Name
    prompt_builder # Instance
    )
    chain.add_component(
    "llm", # Name
    llm_with_structured_output # Instance
    )

    แล้วเชื่อมต่อ prompt builder และ LLM instance เข้าด้วยกัน:

    Python
    # Connect the components
    chain.connect(
    "prompt_builder.prompt", # Sender: prompt from prompt builder
    "llm.messages" # Receiver: message from LLM instance
    )

    Note: เรากำหนดให้ prompt builder ส่ง system และ user prompts ให้เป็น input สำหรับ LLM instance:

    Prompt FROM prompt builder
    Messages TO LLM instance

    🏃 Step 4. Run

    ในขั้นสุดท้าย เราจะรัน pipeline และดึงผลลัพธ์ที่ต้องการออกมา

    ตัวอย่างเช่น ถามเกี่ยวกับ “Compound Interest”:

    Python
    # Run the pipeline with a mental-model query
    raw_result = chain.run(
    data={
    "prompt_builder": {"model_query": "Compound Interest"}
    }
    )

    ดึงผลลัพธ์ที่ต้องการ:

    Python
    # Extract the structured-output text from the raw result
    reply_json = raw_result["llm"]["replies"][0].text
    # Validate the structured output
    result = MentalModel.model_validate_json(reply_json)
    # Print the validated structured output
    print(result.model_dump())

    ผลลัพธ์:

    {
    "model_name": "Compound Interest",
    "origin": (
    "Finance, Mathematics. The concept dates back to ancient Mesopotamia, "
    "with formalization in mathematics and finance over centuries."
    ),
    "description": (
    "Compound interest is the interest on a loan or deposit calculated "
    "based on both the initial principal and the accumulated interest from "
    "previous periods. It matters because it illustrates exponential growth, "
    "where small, consistent gains over time lead to significantly larger "
    "returns, making it a powerful force in finance and many other systems."
    ),
    "example": (
    "If you invest $1,000 at a 5% annual interest rate, after one year "
    "you have $1,050. In the second year, you earn 5% not just on the "
    "initial $1,000, but on the full $1,050, resulting in $1,102.50. "
    "This 'interest on interest' accelerates growth significantly over "
    "decades compared to simple interest."
    ),
    "tags": [
    "Finance",
    "Growth",
    "Long-term thinking",
    "Mathematics",
    "Systems thinking",
    ],
    }

    💪 Summary

    ในบทความนี้ เราได้ทำความรู้จักกับ Haystack ซึ่งเป็น package สำหรับสร้าง LLM application กัน

    เราได้เรียนเกี่ยวกับ 3 components สำหรับเชื่อมต่อกับ LLM:

    1. Prompt builder
    2. LLM instance
    3. Pipeline

    และการเชื่อมต่อกับ LLM ใน 4 ขั้นตอน:

    1. Create a prompt builder
    2. Create an LLM instance
    3. Create a pipeline
    4. Run

    😺 GitHub

    ดูตัวอย่าง code ทั้งหมดได้ที่ GitHub


    📃 References

  • วิธีสร้าง RAG Pipeline ด้วย LangChain ใน Python

    วิธีสร้าง RAG Pipeline ด้วย LangChain ใน Python

    RAG (Retrieval-Augmented Generation) เป็นเทคนิคที่ช่วยให้ LLM (large language model) ตอบคำถามได้แม่นยำขึ้น และไม่ถูกจำกัดด้วย knowledge cutoff หรือความรู้ที่จำกัดจากตอน train model

    RAG ทำงานใน 2 ขั้นตอน:

    1. Retrieve: ดึงเอกสารที่เกี่ยวข้อง
    2. Generate: สร้างคำตอบจากเอกสารที่ได้มา

    RAG มีข้อดี 3 ข้อ:

    1. คำตอบมีความแม่นยำมากขึ้น
    2. คำตอบมีความเกี่ยวข้องกับคำถามมากขึ้น
    3. ช่วยอัปเดตความรู้ให้กับ LLM ได้โดยไม่ต้อง train model ใหม่

    ในบทความนี้ เราจะมาดูวิธีการสร้าง RAG pipeline ด้วย LangChain ซึ่งเป็น framework ในการพัฒนาแอปพลิเคชัน LLM กัน

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. 🔆 Overview
    2. 📑 Step 1. Load Documents
    3. 📚 Step 2. Split Text
    4. 💾 Step 3. Embed & Store Chunks
    5. 🔎 Step 4. Create a Retriever
    6. 🤖 Step 5. Generate a Response
    7. 💪 Summary
    8. 😺 GitHub
    9. 📃 References

    🔆 Overview

    เราใช้ LangChain สร้าง RAG pipeline ได้ใน 5 ขั้นตอน

    1. Load documents
    2. Split text
    3. Embed and store chunks
    4. Create a retriever
    5. Generate a response

    เราไปดูการสร้าง RAG pipeline กับตัวอย่างบอทตอบคำถามเกี่ยวกับนโยบาย HR เช่น การลาและสวัสดิ กัน


    📑 Step 1. Load Documents

    ในขั้นแรก เราจะโหลดเอกสารที่เป็นข้อมูลของ RAG pipeline ก่อน

    LangChain มีหลาย functions สำหรับโหลดเอกสาร เช่น:

    FunctionDocument
    TextLoader()Text file
    UnstructuredMarkdownLoader()Markdown file
    CSVLoader()CSV file
    JSONLoader()JSON file
    PyPDFLoader()PDF file
    DirectoryLoader()ไฟล์จากในโฟลเดอร์

    ในตัวอย่าง เราจะใช้ DirectoryLoader() เพราะเราเก็บเอกสารไว้ในโฟลเดอร์ชื่อ documents:

    documents/
    ├── benefits_policy.txt
    ├── compensation_policy.txt
    ├── leave_policy.txt
    └── remote_work_policy.txt

    ตัวอย่างข้อมูลในเอกสาร benefits_policy.txt:

    DataWise Co. Benefits Policy
    Full-time employees receive health insurance after completing probation.
    The company provides annual health checkups once per year.
    Employees can claim up to 2,000 THB per month for wellness activities such as fitness memberships, yoga classes, or mental health support.
    Employees are also eligible for learning support. The company reimburses up to 10,000 THB per year for approved online courses, books, or professional certificates.

    วิธีใช้ DirectoryLoader():

    Python
    # Import packages
    from langchain_community.document_loaders import DirectoryLoader
    from langchain_community.document_loaders import TextLoader
    # Initialise loader
    loader = DirectoryLoader(
    path="documents",
    glob="*.txt",
    loader_cls=TextLoader,
    loader_kwargs={"encoding": "utf-8"}
    )
    # Load documents
    docs = loader.load()

    การใช้งาน DirectoryLoader():

    • path = โฟลเดอร์ที่ต้องการโหลด
    • glob = pattern ชื่อไฟล์ที่ต้องการโหลด (เช่น "*.txt" หมายถึง ไฟล์ที่ลงชื่อด้วย .txt ทั้งหมด)
    • loader_cls = function ที่จะใช้โหลด (เช่น TextLoader())
    • loader_kwargs = argument เพิ่มเติมสำหรับ function ที่จะใช้โหลด

    เราสามารถดูตัวอย่างเอกสารที่โหลดแล้วได้แบบนี้:

    Python
    # View loaded documents
    for doc in docs:
    print("=" * 50)
    print(doc.metadata["source"])
    print("=" * 50)
    print(doc.page_content[:200])

    ผลลัพธ์:

    ==================================================
    documents/remote_work_policy.txt
    ==================================================
    DataWise Co. Remote Work Policy
    Employees may work from home up to 2 days per week.
    Remote work must be approved by the employee's direct manager.
    Employees must be reachable on Slack during core w
    ==================================================
    documents/benefits_policy.txt
    ==================================================
    DataWise Co. Benefits Policy
    Full-time employees receive health insurance after completing probation.
    The company provides annual health checkups once per year.
    Employees can claim up to 2,000 THB
    ==================================================
    documents/compensation_policy.txt
    ==================================================
    DataWise Co. Compensation Policy
    Salary is paid on the last working day of each month.
    Performance bonuses are reviewed once per year in December.
    Employees may receive an annual salary adjustment
    ==================================================
    documents/leave_policy.txt
    ==================================================
    DataWise Co. Leave Policy
    Full-time employees receive 10 days of annual leave per year after completing probation.
    Employees receive 15 days of paid sick leave per year.
    Sick leave of 3 consecutive

    📚 Step 2. Split Text

    ในขั้นที่ 2 เราจะแบ่ง text ในเอกสารออกเป็นก้อน ๆ หรือ chunk เพราะการแบ่ง text จะช่วยให้การค้นหาข้อมูลง่ายขึ้น

    LangChain มี 3 functions หลักในการแบ่ง text:

    FunctionMethod
    CharacterTextSplitter()แบ่งตามจำนวน character ที่กำหนด
    TokenTextSplitter()แบ่งตามจำนวน token ที่กำหนด
    RecursiveCharacterTextSplitter()แบ่งตามย่อหน้า บรรทัด และประโยค

    ในตัวอย่าง เราจะใช้ RecursiveCharacterTextSplitter() เพราะเป็นวิธีที่เก็บรักษาความหมายของ text ได้ดีกว่าวิธีอื่น:

    วิธีใช้ RecursiveCharacterTextSplitter():

    Python
    # Import package
    from langchain_text_splitters import RecursiveCharacterTextSplitter
    # Create splitter
    text_splitter = RecursiveCharacterTextSplitter(
    chunk_size=500,
    chunk_overlap=100
    )
    # Split documents
    chunks = text_splitter.split_documents(docs)

    ดูตัวอย่าง text ที่แบ่งแล้วได้ตามนี้:

    Python
    # View results
    for i, chunk in enumerate(chunks[:5]):
    print(f"Chunk {i+1}")
    print("Source:", chunk.metadata["source"])
    print(chunk.page_content)
    print("-" * 50)

    ผลลัพธ์:

    Chunk 1
    Source: documents/remote_work_policy.txt
    DataWise Co. Remote Work Policy
    Employees may work from home up to 2 days per week.
    Remote work must be approved by the employee's direct manager.
    Employees must be reachable on Slack during core working hours from 10:00 AM to 4:00 PM.
    Employees working remotely are responsible for maintaining a stable internet connection and a quiet work environment.
    New employees may request remote work only after completing their first month.
    --------------------------------------------------
    Chunk 2
    Source: documents/benefits_policy.txt
    DataWise Co. Benefits Policy
    Full-time employees receive health insurance after completing probation.
    The company provides annual health checkups once per year.
    Employees can claim up to 2,000 THB per month for wellness activities such as fitness memberships, yoga classes, or mental health support.
    Employees are also eligible for learning support. The company reimburses up to 10,000 THB per year for approved online courses, books, or professional certificates.
    --------------------------------------------------
    Chunk 3
    Source: documents/compensation_policy.txt
    DataWise Co. Compensation Policy
    Salary is paid on the last working day of each month.
    Performance bonuses are reviewed once per year in December.
    Employees may receive an annual salary adjustment based on company performance, individual performance, and market benchmarks.
    Overtime pay is available only for non-managerial employees and must be approved by a manager before the overtime work begins.
    --------------------------------------------------
    Chunk 4
    Source: documents/leave_policy.txt
    DataWise Co. Leave Policy
    Full-time employees receive 10 days of annual leave per year after completing probation.
    Employees receive 15 days of paid sick leave per year.
    Sick leave of 3 consecutive days or more requires a medical certificate.
    Employees should submit annual leave requests at least 7 days in advance through the HR system.
    Unused annual leave can be carried over for up to 5 days into the next calendar year.
    --------------------------------------------------

    สังเกตว่า text ถูกแบ่งย่อหน้า ทำให้ chunk ที่ได้มีความหมายที่ครบถ้วนในตัวเอง


    💾 Step 3. Embed & Store Chunks

    ในขั้นที่ 3 เราจะ embed และเก็บข้อมูลลงใน vector database

    Embedding คือ การแปลง chunk ให้กลายเป็น vector คือ ชุดตัวเลขที่เป็นตัวแทนของ chunk

    ตัวอย่าง chunk:

    "Employees can work from home up to two days per week."

    ตัวอย่าง vector:

    [
    0.021,
    -0.184,
    0.736,
    0.094,
    -0.511,
    0.302,
    0.087,
    -0.624
    ]

    Vector เป็นสิ่งที่ระบบจะใช้ในการค้นหาเอกสารที่เกี่ยวข้อง โดย vector ที่มีความหมายใกล้เคียงกัน จะมีตัวเลขที่ใกล้เคียงกัน เมื่อเราต้องการหาเอกสาร ระบบจะดึงเอกสารที่มี vector ใกล้เคียงกับคำถามของเราขึ้นมาให้

    ใน LangChain เราสามารถเลือก model ที่จะใช้ embedding ได้ ในตัวอย่าง เราจะใช้ Gemini กัน:

    Python
    # Import packages
    import os
    from langchain_google_genai import GoogleGenerativeAIEmbeddings
    # Get API key
    GEMINI_API_KEY = os.getenv("GEMINI_API_KEY")
    # Create embedder
    document_embedder = GoogleGenerativeAIEmbeddings(
    model="gemini-embedding-001",
    task_type="retrieval_document",
    google_api_key=GEMINI_API_KEY
    )

    หลังจากได้ embedding model แล้ว เราจะสร้าง vector database เพื่อเก็บ vector โดยในตัวอย่างเราจะใช้ FAISS database:

    Python
    # Import package
    from langchain_community.vectorstores import FAISS
    # Build vector DB
    vectorstore = FAISS.from_documents(
    documents=chunks,
    embedding=document_embedder
    )

    สังเกตว่า เราใส่ document_embedder ไปใน vector database ด้วย เพื่อแปลง chunk เป็น vector และเก็บลงใน database


    🔎 Step 4. Create a Retriever

    ในขั้นที่ 4 เราจะสร้าง retriever ที่ทำหน้าที่ค้นหา vector โดยใช้ .as_retriever() แบบนี้:

    Python
    # Creater retriever
    retriever = vectorstore.as_retriever(
    search_type="similarity",
    search_kwargs={"k": 2}
    )

    เราสามารถทดสอบ retriever เพื่อดูว่า จะได้เอกสารอะไรกลับมา ได้แบบนี้:

    Python
    # Test retriever
    question = "Do I need a medical certificate for sick leave?"
    relevant_docs = retriever.invoke(question)
    for i, doc in enumerate(relevant_docs, start=1):
    print(f"Retrieved chunk {i}")
    print("Source:", doc.metadata["source"])
    print(doc.page_content)
    print("-" * 60)

    ผลลัพธ์:

    Retrieved chunk 1
    Source: documents/leave_policy.txt
    DataWise Co. Leave Policy
    Full-time employees receive 10 days of annual leave per year after completing probation.
    Employees receive 15 days of paid sick leave per year.
    Sick leave of 3 consecutive days or more requires a medical certificate.
    Employees should submit annual leave requests at least 7 days in advance through the HR system.
    Unused annual leave can be carried over for up to 5 days into the next calendar year.
    ------------------------------------------------------------
    Retrieved chunk 2
    Source: documents/compensation_policy.txt
    DataWise Co. Compensation Policy
    Salary is paid on the last working day of each month.
    Performance bonuses are reviewed once per year in December.
    Employees may receive an annual salary adjustment based on company performance, individual performance, and market benchmarks.
    Overtime pay is available only for non-managerial employees and must be approved by a manager before the overtime work begins.
    ------------------------------------------------------------

    🤖 Step 5. Generate a Response

    ในขั้นสุดท้าย เราจะให้ LLM สร้างคำตอบโดยใช้ข้อมูลใน vector database

    ในตัวอย่างเราจะลองใช้ Gemini ช่วยคิดคำตอบให้กับเรา

    เราจะเริ่มจากเชื่อมต่อกับ Gemini และสร้าง prompt ก่อน:

    Python
    # Import packages
    from langchain_google_genai import ChatGoogleGenerativeAI
    from langchain_core.prompts import ChatPromptTemplate
    # Initialise Gemini
    llm = ChatGoogleGenerativeAI(
    model="gemini-2.5-flash",
    temperature=0,
    google_api_key=GEMINI_API_KEY
    )
    # Create prompt template
    prompt = ChatPromptTemplate.from_template("""
    You are an HR policy assistant.
    Answer the user's question using only the policy context below.
    Rules:
    - Do not use outside knowledge.
    - If the answer is not in the context, say:
    "I could not find this information in the available company policies."
    - Keep the answer concise.
    - Mention the source policy file when possible.
    Policy context:
    {context}
    User question:
    {question}
    """)

    จากนั้น กำหนดคำถามและดึงเอกสารที่เกี่ยวข้องจาก vector database

    Python
    # Ask a question
    question = "Do I need a medical certificate for sick leave?"
    # Retrieve relevant document chunks
    relevant_docs = retriever.invoke(question)
    # Combine retrieved chunks into one context string
    context = "\n\n".join(
    [
    f"Source: {doc.metadata['source']}\n"
    f"{doc.page_content}"
    for doc in relevant_docs
    ]
    )
    # Inspect retrieved context before sending it to Gemini
    print("Retrieved context:")
    print(context)

    ผลลัพธ์:

    Retrieved context:
    Source: documents/leave_policy.txt
    DataWise Co. Leave Policy
    Full-time employees receive 10 days of annual leave per year after completing probation.
    Employees receive 15 days of paid sick leave per year.
    Sick leave of 3 consecutive days or more requires a medical certificate.
    Employees should submit annual leave requests at least 7 days in advance through the HR system.
    Unused annual leave can be carried over for up to 5 days into the next calendar year.
    Source: documents/compensation_policy.txt
    DataWise Co. Compensation Policy
    Salary is paid on the last working day of each month.
    Performance bonuses are reviewed once per year in December.
    Employees may receive an annual salary adjustment based on company performance, individual performance, and market benchmarks.
    Overtime pay is available only for non-managerial employees and must be approved by a manager before the overtime work begins.

    แล้วส่งข้อมูลคำถามและเอกสารให้กับ Gemini:

    Python
    # Add context and question to prompt template
    messages = prompt.invoke(
    {
    "context": context,
    "question": question
    }
    )
    # Send prompt to Gemini
    response = llm.invoke(messages)
    # Print Gemini's answer
    print(response.content)

    ผลลัพธ์:

    Yes, sick leave of 3 consecutive days or more requires a medical certificate. (Source: documents/leave_policy.txt)

    เพื่อให้เราใช้งาน RAG pipeline ได้ง่าย เราสามารถแปลงโค้ดชุดนี้ให้เป็น function ได้:

    Python
    # Convert to function
    def ask_policy_question(question: str) -> str:
    """
    Retrieve relevant policy chunks, send them to Gemini,
    and return Gemini's answer.
    """
    # Retrieve relevant chunks
    relevant_docs = retriever.invoke(question)
    # Combine retrieved chunks into context
    context = "\n\n".join(
    [
    f"Source: {doc.metadata['source']}\n"
    f"{doc.page_content}"
    for doc in relevant_docs
    ]
    )
    # Add context and question to prompt template
    messages = prompt.invoke(
    {
    "context": context,
    "question": question
    }
    )
    # Send completed prompt to Gemini
    response = llm.invoke(messages)
    # Return answer text
    return response.content

    เพื่อที่เราจะเขียนโค้ดสั้นลงในครั้งถัด ๆ ไป:

    Python
    # Test function
    answer = ask_policy_question("Who is eligible for health insurance?")
    print(answer)

    ผลลัพธ์:

    Full-time employees receive health insurance after completing probation. (Source: documents/benefits_policy.txt)

    💪 Summary

    ในบทความนี้ เราได้เรียนรู้การสร้าง RAG pipeline ด้วย LangChain ใน 5 ขั้นตอน:

    1. Load documents: โหลดเอกสารสำหรับ RAG pipeline
    2. Split text: แบ่ง text ในเอกสารเป็น chunk
    3. Embed and store chunks: แปลง chunk เป็น vector และเก็บลงใน database
    4. Create a retriever: สร้างตัวค้นหาเอกสารจาก vector database
    5. Generate a response: สร้างคำตอบจากเอกสาร

    😺 GitHub

    ดูตัวอย่าง code และเอกสารทั้งหมดได้ที่ GitHub


    📃 References

  • วิธีใช้ LangChain เชื่อมต่อ LLM ด้วย Python ใน 5 ขั้นตอน

    วิธีใช้ LangChain เชื่อมต่อ LLM ด้วย Python ใน 5 ขั้นตอน

    langchain เป็น framework สำหรับสร้างแอปพลิเคชั่นที่ใช้ large language model (LLM) ที่ช่วยลดความยุ่งยากการเรียกใช้งาน API โดยตรง

    langchain มีข้อดี 3 อย่าง:

    1. Modular: ใช้งานง่าย ด้วยการเขียนเป็นส่วน ๆ หรือ module (เหมือนเลโก้)
    2. Use case: รองรับการใช้งานหลายหลาก เพราะสามารถประกอบ module เข้าด้วยกันได้หลายแบบ (ต่อเลโก้ได้หลายแบบ)
    3. Integration: ใช้งานร่วมเครื่องมือได้กับหลากหลาย เช่น OpenAI, Hugging Face, databricks

    ในบทความนี้ เรามาดูวิธีใช้ langchain เพื่อทำงานกับ LLM อย่างง่ายกัน

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. ☀️ Overview
    2. 🤖 Step 1. Set LLM
    3. 💬 Step 2. Set Prompt
    4. 📦 Step 3. Set Output Structure
    5. ⛓️‍💥 Step 4. Chain
    6. 🏃 Step 5. Run
      1. ☝️ Single Run
      2. 🎳 Batch Run
    7. 💪 Summary
    8. 🫵 Your Turn
    9. 📄 References

    ☀️ Overview

    การใช้งาน langchain มีอยู่ 5 ขั้นตอน ได้แก่:

    1. Set LLM: เลือก LLM ที่ต้องการ
    2. Set prompt: สร้าง prompt สำหรับคุยกับ LLM
    3. Set output structure: กำหนดหน้าตา output ที่ต้องการ
    4. Chain: เชื่อม LLM และ prompt เข้าด้วยกัน เพื่อสร้าง pipeline ในการเรียกใช้ LLM
    5. Run: เรียกใช้งาน LLM

    เราไปดูการใช้งาน ผ่านการสร้าง chatbot ตอบคำถามเกี่ยวกับ mental model กัน


    🤖 Step 1. Set LLM

    langchain รองรับการใช้งาน LLM หลายเจ้า เช่น:

    ในตัวอย่าง เราจะเลือกใช้ Gemini โดยเริ่มจากโหลด module สำหรับเชื่อมกับ Gemini:

    Python
    # Import package
    from langchain_google_genai import ChatGoogleGenerativeAI

    แล้วสร้าง LLM instance ขึ้นมา:

    Python
    # Create model instance
    llm = ChatGoogleGenerativeAI(
    model="gemini-2.5-flash",
    temperature=0.5,
    api_key="YOUR_API_KEY"
    )
    • model: ชื่อ LLM ที่เราต้องการใช้งาน
    • temperature: ระดับความสุ่มของคำตอบจาก LLM (ค่ายิ่งสูง คำตอบยิ่งมีความหลากหลาย ส่วนยิ่งค่าน้อย คำตอบจะยิ่งมีความคล้ายคลึงกัน)
    • api_key: API key สำหรับใช้งาน LLM (ดูวิธีการสร้าง Gemini API key ฟรี)

    💬 Step 2. Set Prompt

    ในขั้นที่ 2 เราจะสร้าง prompt สำหรับคุยกับ LLM กัน:

    1. โหลด module
    2. กำหนด prompt

    ขั้นย่อยที่ 1. โหลด module สำหรับสร้าง prompt:

    Python
    # Import package
    from langchain_core.prompts import ChatPromptTemplate

    ขั้นย่อยที่ 2. กำหนด prompt โดยแยกระหว่าง:

    1. System prompt: บทบาทและสไตล์การตอบคำถามของ LLM (เช่น ผู้เชี่ยวชาญด้าน mental model)
    2. User prompt: คำถามที่จะส่งให้ LLM (เช่น บอกให้อธิบาย mental model ที่ต้องการ)

    ตัวอย่าง system และ user prompts:

    Python
    # Define system prompt
    system_prompt = """
    You are an expert curator of mental models across science, philosophy, and applied reasoning.
    Your task is to explain mental models clearly and accurately using a fixed schema.
    If the origin of a model is unclear or debated, state that explicitly.
    Do not invent historical sources. Be concise and concrete.
    """
    # Define user prompt
    user_prompt = "Explain the following mental model: {model_query}"

    จากนั้น ประกอบ prompts เข้าด้วยกัน:

    Python
    # Create prompt template
    prompts = ChatPromptTemplate.from_messages(
    [
    # System prompt
    ("system", system_prompt.strip()),
    # User prompt
    ("human", user_prompt)
    ]
    )

    เราสามารถดูตัวอย่าง prompt ได้ด้วย .format_messages():

    Python
    # Inspect prompt template
    prompts.format_messages(model_query="Pareto Principle")

    ผลลัพธ์:

    [SystemMessage(content='You are an expert curator of mental models across science, philosophy, and applied reasoning.\\n\\nYour task is to explain mental models clearly and accurately using a fixed schema.\\n\\nIf the origin of a model is unclear or debated, state that explicitly.\\n\\nDo not invent historical sources. Be concise and concrete.', additional_kwargs={}, response_metadata={}),
    HumanMessage(content='Explain the following mental model: Pareto Princinple', additional_kwargs={}, response_metadata={})]

    📦 Step 3. Set Output Structure

    ในขั้นที่ 3 เราจะกำหนดหน้าตา output ที่เราต้องการ

    เช่น ถ้าเราต้องการให้คืนค่า JSON แบบนี้:

    {
    "model_name": "",
    "origin": "",
    "description": "",
    "example": "",
    "tags": []
    }

    เรากำหนดได้โดยใช้ pydantic และ typing packages แบบนี้:

    Python
    # Import packages
    from pydantic import BaseModel, Field
    from typing import List, Literal
    # Define output structure
    class MentalModel(BaseModel):
    # Mental model name
    model_name: str = Field(description="The commonly accepted name of the mental model")
    # Source/origin
    origin: str = Field(
    description="Where the model comes from (person, book, field, or cultural origin)"
    )
    # Brief description
    description: str = Field(
    description="A brief explanation of what the mental model is and why it matters"
    )
    # Example
    example: str = Field(
    description="A concrete real-world example illustrating the mental model"
    )
    # Tags
    tags: List[str] = Field(
    description="Short tags such as decision-making, systems thinking, learning, philosophy"
    )

    Note: ดูวิธีใช้ Pydantic

    หลังกำหนดหน้าตา output แล้ว ให้ใส่เข้าไปใน LLM instance แบบนี้:

    Python
    # Add output structure to LLM
    llm_with_structured_output = llm.with_structured_output(MentalModel)

    ⛓️‍💥 Step 4. Chain

    ในขั้นที่ 4 ให้เราสร้าง chain โดยเชื่อม LLM instance เข้ากับ prompts ด้วย pipe operator (|):

    Python
    # Build chain
    chain = prompts | llm_with_structured_output

    Note: ให้มองว่า | เป็นลูกศรชี้ทางขวา (prompt → llm)


    🏃 Step 5. Run

    ในขั้นสุดท้าย เราจะเรียกใช้งาน chain ซึ่งทำได้ 2 แบบ:

    1. Single run: เรียกใช้ครั้งเดียว
    2. Batch run: เรียกใช้หลายครั้งพร้อมกัน

    .

    ☝️ Single Run

    เราเรียกใช้งานครั้งเดียวด้วย .invoke()

    ในตัวอย่าง เราจะถามเกี่ยวกับ “compound interest” กัน:

    Python
    # Run query
    result = chain.invoke({"model_query": "Compound Interest"})

    แสดงผลลัพธ์ใน console:

    Python
    # Import package
    import json
    # Load result
    result_dict = result.model_dump()
    # Print
    print(json.dumps(result_dict, indent=4))

    ผลลัพธ์:

    {
    "model_name": "Compound Interest",
    "origin": "Finance, Mathematics; concept dates back to ancient times, formalized in the Renaissance.",
    "description": "Compound interest is the interest on a loan or deposit calculated based on both the initial principal and the accumulated interest from previous periods. It is often called 'interest on interest' and leads to exponential growth over time, making it a powerful force in finance for both wealth creation and debt accumulation.",
    "example": "If you invest $1,000 at an annual interest rate of 5% compounded annually, after the first year you'll have $1,050. In the second year, the 5% interest is calculated on $1,050, not just the original $1,000, leading to a balance of $1,102.50. This snowball effect accelerates over decades, significantly increasing the total return compared to simple interest.",
    "tags": [
    "Finance",
    "Economics",
    "Wealth Building",
    "Decision-making",
    "Long-term thinking"
    ]
    }

    .

    🎳 Batch Run

    สำหรับการรันหลายครั้งพร้อมกัน เราจะใช้ .batch() แบบนี้:

    Python
    # Create list of mental models
    mental_model_queries = [
    "First Principles Thinking",
    "Occam's Razor",
    "Confirmation Bias"
    ]
    # Create batch inputs
    batch_inputs = [{"model_query": query} for query in mental_model_queries]
    # Run queries
    results = chain.batch(batch_inputs)
    # Instantiate collector
    query_collector = [result.model_dump() for result in results]

    แสดงผลลัพธ์ใน console:

    Python
    # Instantiate counter
    i = 1
    # Loop through elements in collector
    for result in query_collector:
    # Print result
    print(f"👉 Query {i}:")
    print(json.dumps(result, indent=4))
    print("\\n")
    # Add 1 to counter
    i += 1

    ผลลัพธ์:

    👉 Query 1:
    {
    "model_name": "First Principles Thinking",
    "origin": "Often attributed to Aristotle; popularized in modern business by Elon Musk.",
    "description": "First Principles Thinking involves breaking down complex problems into their most basic, fundamental truths or 'first principles,' rather than reasoning by analogy or conventional wisdom. It matters because it allows for innovative solutions by challenging assumptions and building new knowledge from the ground up.",
    "example": "Instead of accepting the high cost of batteries for electric cars, Elon Musk famously broke down a battery into its constituent raw materials (cobalt, nickel, lithium, etc.) to understand their actual cost, then sought ways to procure and assemble them more efficiently, leading to significant cost reductions and innovation.",
    "tags": [
    "Problem-solving",
    "Innovation",
    "Critical Thinking",
    "Decision-making"
    ]
    }
    👉 Query 2:
    {
    "model_name": "Occam's Razor",
    "origin": "William of Ockham (14th-century philosopher and theologian)",
    "description": "Occam's Razor is a problem-solving principle stating that among competing hypotheses that explain an event or phenomenon equally well, the simplest solution is most likely the correct one. It advocates for parsimony, suggesting that one should not multiply entities beyond necessity, thereby favoring theories with fewer assumptions.",
    "example": "If you hear hoofbeats outside, it is more likely to be horses than zebras, assuming you are in a location where horses are common and zebras are not. The 'horse' explanation is simpler and requires fewer extraordinary assumptions.",
    "tags": [
    "Philosophy",
    "Decision-making",
    "Problem-solving",
    "Critical thinking",
    "Science"
    ]
    }
    👉 Query 3:
    {
    "model_name": "Confirmation Bias",
    "origin": "Psychology; early concepts traced to Francis Bacon's Novum Organum (1620)",
    "description": "Confirmation bias is the tendency to search for, interpret, favor, and recall information in a way that confirms one's pre-existing beliefs or hypotheses. It matters because it can lead to flawed reasoning, poor decision-making, and resistance to new or contradictory evidence, hindering objective analysis.",
    "example": "A person who believes a certain stock will perform well might selectively read news articles and analyst reports that support this positive outlook, while ignoring or downplaying any negative news or warnings about the company.",
    "tags": [
    "cognitive bias",
    "decision-making",
    "psychology",
    "critical thinking"
    ]
    }

    💪 Summary

    เราจะได้เห็นได้ว่า langchain เป็น package ที่ใช้ทำงานกับ LLM ได้อย่างง่าย ๆ ใน 5 ขั้นตอน:

    Set LLM: เลือก model
    Set prompt: กำหนด system และ user prompt
    Set output: กำหนด output structure
    Chain: สร้าง pipeline
    Run: เรียกใช้งาน (single & batch)

    🫵 Your Turn

    อ่านบทความจบแล้ว ลองมาใช้ langchain กันนะครับ:


    📄 References

  • AI Literacy: สรุป 31 ข้อคิดการใช้ AI ให้อยู่รอด จาก session แชร์ความรู้ให้กับนักศึกษา ม.หอการค้าไทย

    AI Literacy: สรุป 31 ข้อคิดการใช้ AI ให้อยู่รอด จาก session แชร์ความรู้ให้กับนักศึกษา ม.หอการค้าไทย

    สัปดาห์ที่แล้ว ผมมีโอกาสแชร์ความรู้การใช้ AI ในหัวข้อ AI literacy ให้กับนักศึกษาคณะการศึกษาปฐมวัย มหาวิทยาลัยหอการค้าไทย

    ในบทความนี้ ผมจะมาสรุป 31 ข้อคิดที่ผมแชร์ใน session โดยแบ่งเป็น 5 กลุ่ม:

    1. Why AI literacy: ความสำคัญของ AI literacy
    2. Working with AI: แนวคิดการทำงานกับ AI
    3. How to prompt: วิธีเขียน prompt
    4. Future trends: แนวโน้มของ AI ในอนาคต
    5. Be human: การเป็นมนุษย์ในยุคของ AI

    ถ้าพร้อมแล้ว ไปเริ่มกันเลย


    1. 🤖 Part I. Why AI Literacy
    2. 💼 Part II. Working With AI
    3. 👷 Part III. How to Prompt
    4. 🚀 Part IV. Future Trends
    5. 😌 Part V. Be Human

    🤖 Part I. Why AI Literacy

    .

    ข้อ 1. Pareto Principle (80/20 rule)

    Pareto principle เป็น mental model หรือแนวคิดช่วยตัดสินใจที่บอกว่า 80% ของความสำเร็จมักมาจาก 20% ของสิ่งที่เราทำ

    ในยุคที่ AI เข้ามามีบทบาทในหลายด้านของชีวิต ทักษะ AI เป็นสิ่งง่าย ๆ ที่เราทำได้เพื่อช่วยให้เราอยู่รอด

    การเรียนรู้เกี่ยวกับ AI คือ 20% ที่เราทำได้ เพื่อให้โอกาสอยู่รอดถึง 80%

    .

    ข้อ 2. What is AI literacy?

    AI literacy คือ ความเข้าใจและความสามารถในการใช้งาน AI ได้อย่างมีประสิทธิภาพ

    .

    ข้อ 3. AI can do many things

    AI สามารถทำได้หลายอย่าง เช่น:

    • Content: สร้าง content เช่น ข้อความ ภาพ เสียง และวิดีโอ
    • Analysis: วิเคราะห์ข้อมูลขนาดใหญ่อย่างรวดเร็ว
    • Automation: ทำงานโดยอัตโนมัติ (เช่น คุยกับลูกค้าในขณะที่เราหลับ)

    .

    ข้อ 4. Jobs at risk

    ความสามารถของ AI ทำให้มีหลายงานเสี่ยงที่จะถูกแทนที่ แม้กระทั่งงานที่ปกติจะต้องใช้มนุษย์ เช่น:

    • ล่าม/นักแปลภาษา
    • นักเขียน
    • โปรแกรมเมอร์
    • Customer service

    เมื่อเป็นอย่างนี้ AI จะมาแทนที่มนุษย์ไหม?

    .

    ข้อ 5. AI still has limitations

    แม้ AI จะทำได้หลายอย่าง แต่ก็ยังมีข้อจำกัดอยู่ เช่น:

    .

    ข้อ 6. We must not fear AI, but people who use AI

    ด้วยข้อจำกัดของ AI เรายังไม่ต้องกลัวว่า AI จะมาแทนที่เรา

    แต่เราควรจะกลัวคนที่ใช้ AI เป็นมากกว่า

    AI won’t replace people, but maybe people that use AI will replace people that don’t. — Andrew Ng

    คนที่ใช้ AI เป็นสามารถไปได้ไกลกว่าคนอื่น

    เช่น ถ้าไม่ใช้ AI เราอาจจะใช้เวลา 2 วันเพื่อเขียนรายงานส่งอาจารย์

    แต่เมื่อใช้ AI เราอาจใช้เวลาแค่ 2 ชั่วโมง และมีเวลาสำหรับอ่านหนังสือสอบมากขึ้น ทำให้เรามีโอกาสได้เกรดที่ดีกว่าคนอื่น

    .

    ข้อ 7. The one who survives is the one who levels up

    แต่ละครั้งที่มีเทคโนโลยีใหม่เกิดขึ้น คนที่อยู่รอด คือ คนที่ยกระดับตัวเอง

    สมัยก่อน เราผลิตหนังสือโดยใช้ scribe หรือชาวบ้านที่ฝึกคัดลอกหนังสือมาโดยเฉพาะ scribe ใช้เวลาฝึกฝนนานหลายปีกว่าจะสามารถคัดหนังสือได้

    วันหนึ่ง เครื่องพิมพ์ถูกพัฒนาขึ้น เราสามารถพิมพ์หนังสือได้หลายพันหน้าในวันเดียว โดยไม่ต้องพึ่ง scribe

    ความต้องการ scribe ลดน้อยลงเรื่อย ๆ และ scribe ที่ยึดติดกับวิธีการผลิตหนังสือแบบเดิม ก็ค่อย ๆ หายไปพร้อมกับความต้องการของตลาด

    ส่วน scribe ที่ปรับตัวเข้าหาเทคโนโลยี และฝึกควบคุมเครื่องพิมพ์ ยังคงอยู่รอดต่อไป

    การมาถึงของ AI ก็เหมือนเครื่องพิมพ์ ถ้าเราไม่เรียนรู้ที่จะใช้ AI เราก็จะค่อย ๆ ถูกลืม เหมือนกับ scribe ที่ยังคัดหนังสือด้วยมือ

    .

    ข้อ 8. We are at a crossroad: choose

    AI พัฒนาเร็วขึ้นและก้าวกระโดดมากขึ้นเรื่อย ๆ

    ในช่วงแรกที่ ChatGPT เปิดตัวใหม่ ๆ เราต้องรอนานหลายเดือนกว่าจะได้ใช้ ChatGPT เวอร์ชั่นใหม่ที่มีความสามารถไม่ต่างจากเวอร์ชั่นก่อนหน้ามากนัก

    ในปัจจุบัน เราจะเห็น ChatGPT มีการอัปเดตที่ถี่ขึ้น และในอัปเดตแต่ละครั้ง ChatGPT มีความสามารถมากกว่าเวอร์ชั่นก่อนมาก

    การที่ AI พัฒนาเร็วขึ้นเรื่อย ๆ ทำให้เรามีเวลาปรับตัวน้อยลงเรื่อย ๆ

    และตอนนี้ เราเหมือนอยู่ที่ทางแยกที่เราจะต้องเลือกว่า เราจะเรียนรู้การใช้ AI ให้เป็นและอยู่รอดในยุคของ AI หรือเราจะใช้ AI แบบเดิม ๆ และถูกทิ้งไว้ข้างหลัง

    The people who will come out of this well won’t be the ones who mastered one tool. They’ll be the ones who got comfortable with the pace of change itself. — Matt Shumer


    💼 Part II. Working With AI

    .

    ข้อ 9. Maslow’s hammer

    I suppose it is tempting, if the only tool you have is a hammer, to treat everything as if it were a nail. — Abraham Maslow

    Maslow’s hammer เป็น mental model ที่บอกว่า เครื่องมือสามารถจำกัดมุมมองของเราได้

    เช่น ถ้าเรามีค้อน เราจะมองทุกอย่างเป็นตะปู

    ในยุคของ AI เราอาจมองว่าทุกอย่างแก้ได้ด้วย AI:

    • ทำงานเร็วขึ้น
    • ผิดพลาดน้อยลง
    • มีเวลามากขึ้น

    แต่ไม่ใช่ทุกปัญหาจะแก้ได้ด้วย AI เพราะ AI ไม่ใช่เครื่องมือสำหรับแก้ทุกอย่าง

    ถ้าเราอยากตอกตะปู เราจะต้องใช้ค้อน ไม่ใช่ AI

    การใช้ AI ที่ถูกต้อง คือ เริ่มต้นจากปัญหาและความต้องการของเรา แล้วเลือกเครื่องมือที่ตอบโจทย์ ซึ่งเครื่องมือนั้นอาจจะเป็น AI หรือไม่ก็ได้

    .

    ข้อ 10. AI is built in man’s image

    AI เกิดจากการ train model ด้วยข้อมูลจากอินเทอร์เน็ตซึ่งมาจากมนุษย์

    Human -> Data -> Train -> AI

    เพราะ AI ถูกสร้างจากข้อมูลของมนุษย์ และเรามองได้ว่า AI เป็นเหมือนเป็นมนุษย์คนหนึ่ง

    .

    ข้อ 11. AI as capable but junior assistant

    ถ้าเรามอง AI เป็นคน AI จะเป็นเหมือนผู้ช่วยที่มีความรู้รอบด้านและมีศักยภาพสูง

    แต่สิ่งเดียวที่ผู้ช่วยคนนี้ยังขาดไป คือ ทิศทาง

    .

    ข้อ 12. Even a fried egg is hard to get right

    การทำงานกับ AI ก็เหมือนสั่งไข่ดาว แม้จะดูง่าย แต่ก็ไม่ง่ายอย่างที่คิด

    บางครั้ง เราอยากกินไข่ไม่สุก แต่ได้แบบสุกมาแทน

    บางครั้ง เราอยากให้ AI สร้างรูปในแบบที่เราคิด แต่ไม่เคยได้ภาพนั้นสักที

    .

    ข้อ 13. Principal-agent dilemma

    Principal-agent dilemma เป็น mental model ที่บอกว่า คนทำงาน (agent) มักทำตามความต้องการของคนสั่ง (principal) ไม่ได้ เพราะทั้งสองฝ่ายมีข้อมูลไม่เท่ากัน

    ในตัวอย่าง เราไม่ได้ไข่ดาวที่ต้องการ เพราะคนทอดไข่ไม่รู้ว่าเราชอบไข่สุกหรือไม่สุก

    เช่นเดียวกัน AI สร้างรูปที่เราต้องการไม่ได้ เพราะ AI ไม่รู้ว่ารูปที่เราคิดต้องการเป็นยังไง

    .

    ข้อ 14. Fixing the egg

    ถ้าเราอยากได้ไข่ดาวที่ต้องการ เราจะต้องทำให้ AI รู้เท่ากับเรา เช่น ให้ข้อมูลอย่าง:

    1. Goal: ภาพปลายทางที่เราต้องการ (ภาพแมวน่ารัก)
    2. Steps: ขั้นตอนที่จะไปถึงจุดหมาย (วาดแมวก่อน แล้วค่อยวาดองค์ประกอบอื่น ๆ ในภาพ)
    3. Constraints: ข้อจำกัดหรือสิ่งที่ไม่ควรทำ (เช่น ไม่เอาแมวสีดำ ฉากหลังต้องดูสดใส)

    .

    ข้อ 15. Human in the loop: taste and iterate

    Taste: เมื่อไข่ดาวมาเสิร์ฟ เราจะไม่รู้ว่าไข่ดาวอร่อยไหม จนกว่าจะได้ลองชิมด้วยตัวเอง

    การทำงานกับ AI ก็เช่นกัน เราไม่ควรจะบอกว่า สิ่งที่ AI ส่งกลับมาดีไหม จนกว่าจะได้เช็กด้วยตัวเอง

    Iterate: ถ้าชิมแล้วไข่ดาวไม่อร่อย เราจะบอกกับคนทอดว่า ไม่อร่อยเพราะอะไร และจะทำยังไงให้อร่อยมากขึ้น และรอชิมไข่จานต่อไป

    ถ้าสิ่งที่ AI ส่งกลับมาไม่ตรงใจ เราควรจะบอก AI ว่าอะไรที่ยังไม่ถูกใจ เพื่อให้ AI ปรับผลลัพธ์และส่งกลับมาให้เราเช็กจนกว่าเราจะพอใจกับงานของ AI

    .

    ข้อ 16. Be accountable

    เราควรจะเช็กงานของ AI ทุกครั้ง เพราะถ้าเราไม่รับผิดชอบกับงานของ AI เราอาจจะเป็นเหมือนทนายความจากออสเตรเลียที่ถูกตรวจสอบ หลังจากศาลพบว่าเอกสารที่ทนายนำส่งเป็นข้อมูลที่ไม่มีอยู่จริง

    แม้ทนายจะอ้างว่ารู้เท่าไม่ถึงการณ์ว่า AI ที่บริษัทให้ใช้สามารถสร้างข้อมูลที่ไม่มีอยู่จริงได้ และตัวเองควรตรวจสอบข้อมูลจาก AI ก่อน ศาลยังสั่งให้ทนายงดว่าความด้วยตัวเองเป็นเวลา 2 ปี โดยในระยะเวลานี้จะต้องทำงานเป็นลูกจ้างของคนอื่น และต้องรายงานต่อศาลทุกไตรมาส

    ดังนั้น ไม่ว่างานของ AI จะดูดีขนาดไหน เราควรจะตรวจสอบด้วยตัวเองก่อนที่จะนำงานไปใช้จริง


    👷 Part III. How to Prompt

    .

    ข้อ 17. Prompt and prompt engineering

    Prompt คือ คำสั่งสำหรับทำงานกับ AI ซึ่งจะเป็น:

    • ข้อความ
    • ภาพ
    • เสียง

    หรือสื่ออื่น ๆ ก็ได้

    Prompt engineering คือ การออกแบบ prompt เพื่อทำให้ AI ทำงานได้อย่างมีประสิทธิภาพสูงสุด

    .

    ข้อ 18. Effective prompts

    Prompt ที่ดีมีลักษณะ 3 อย่าง:

    1. Clear: ชัดเจนว่า สิ่งที่ต้องทำคืออะไร
    2. Specific: มีความเจาะจง ไม่คลุมเครือ
    3. Structured: มีโครงสร้างที่ดี รู้ว่าข้อมูลไหนคืออะไรและต้องใช้ยังไง

    นอกจากนี้ ทั้ง 3 อย่างต้องทำงานภายในเป้าหมายและขอบเขตงานที่เราต้องทำ

    .

    ข้อ 19. How and what of prompting

    การเขียน prompt ที่เราจะต้องรู้มี 2 อย่าง:

    1. Prompting technique (how): วิธีเขียน prompt ให้ AI เข้าใจ
    2. Prompting framework (what): สิ่งที่เราจะควรใส่ลงใน prompt

    .

    ข้อ 20. Prompting technique: n-shot

    n-shot technique เป็นการสั่ง AI โดยให้ตัวอย่าง (shot) และแบ่งได้เป็น 3 ประเภท:

    • Zero-shot: สั่งโดยไม่ให้ตัวอย่าง
    • One-shot: สั่งโดยให้ 1 ตัวอย่าง
    • Few-shot: สั่งโดยให้หลายตัวอย่าง

    เราจะใช้ shot น้อยเมื่อต้องการให้คำตอบของ AI มีความหลากหลาย (มีความสร้างสรรค์)

    และใช้ shot เยอะเมื่อต้องการให้คำตอบของ AI ใกล้เคียงกับภาพที่เราต้องการมากที่สุด

    ตัวอย่างการใช้ n-shot:

    จะสังเกตว่า ยิ่งให้ shot เยอะ คำตอบของ AI ก็จะยิ่งใกล้เคียงกับตัวอย่างมากขึ้น (zero-shot ให้สัตว์ป่า แต่ few-shot ให้สัตว์เลี้ยง)

    .

    ข้อ 21. Prompting technique: COT

    COT ย่อมาจาก chain-of-thought ซึ่งเป็นวิธีเขียน prompt โดยกำหนดวิธีคิดให้กับ AI

    เช่น แทนที่ให้ AI แก้โจทย์เลขในทันที:

    A ซื้อส้ม 2 ลูก ลูกละ 10 บาท A ต้องจ่ายเงินเท่าไร

    เราจะสอนให้ AI คิดก่อน:

    A ซื้อส้ม 2 ลูก ลูกละ 10 บาท
    วิธีคิด:
    1. หาว่า ส้มราคาลูกละเท่าไร
    2. คูณจำนวนราคาด้วยจำนวนส้มที่ต้องซื้อ
    A ต้องจ่ายเงินเท่าไร

    COT เหมาะกับงานที่ซับซ้อนหรือมีหลายขั้นตอน เช่น:

    • แก้สมการเลข
    • วิเคราะห์งานวิจัย
    • การวางแผนเชิงกลยุทธ์

    .

    ข้อ 22. Prompting framework: theatre model

    Theatre model เป็นแนวการเขียน prompt ที่มนุษย์เป็นเหมือนผู้กำกับ และ AI เป็นนักแสดงบนเวทีของเรา

    Theatre model ประกอบด้วย 6 ส่วน ได้แก่:

    1. Role: บทบาทของ AI
    2. Context: setting ของละคร (บริบทในการทำงาน)
    3. Task: การเดินเรื่อง (เป้าหมาย ขั้นตอน และข้อจำกัด)
    4. Output format: จุดจบของเรื่องจะเป็นยังไง (ส่งที่ AI ต้องส่งให้เรา)
    5. Input: อุปกรณ์ที่จะให้นักแสดงใช้ (ข้อมูลสำหรับ AI)
    6. Execution: “Action!” (คำสั่งให้ AI)

    ตัวอย่างการใช้ theatre model เพื่อสร้างสูตรอาหารใหม่:

    PartExample
    Roleคุณเป็น cook มืออาชีพ มีประสบการณ์ทำงานอาหารไทยและนานาชาติมากกว่า 30 ปี
    Contextคุณกำลังเข้าร่วมแข่งอยู่ในรายการทำอาหาร เพื่อชิงเงินรางวัล 10 ล้านบาท โจทย์คืออาหารไทยฟิวชั่น
    Taskคิดสูตรอาหารไทยฟิวชัน โดยต้องมีวัตถุดิบที่กำหนดอยู่ในอาหาร

    ตั้งชื่อจาน และบอกวิธีการเตรียมอาหาร

    อาหารจะต้องมีความเป็นไทย และถูกปากคนทุกชาติ

    ห้ามเป็นอาหารที่มีอยู่แล้ว
    Output formatส่งกลับมาในรูปแบบนี้:

    ชื่ออาหาร:
    xxx

    ขั้นตอนการทำ:
    xxx
    Inputวัตถุดิบที่ต้องมี:
    1. ใบโหระพา
    2. เนื้อไก่
    3. ผักชี
    Executionคิดสูตรอาหารเลย

    .

    ข้อ 23. Annotation

    เพื่อช่วยให้ AI เข้าใจ prompt ได้มากขึ้น เราควรจัด format ให้อ่านง่ายโดยใช้ XML tags และ markdown:

    • XML tags เช่น <example>ตัวอย่าง</example>
    • Markdown เช่น # และ *

    อ่านเพิ่มเติมเกี่ยวกับ XML tags และ markdown

    .

    ข้อ 24. Iterate

    Prompt แรกอาจจะไม่ให้ในสิ่งที่เราต้องการเสมอไป

    สิ่งที่เราต้องทำ คือ วิเคราะห์ว่า ผลลัพธ์ยังขาดอะไรไป และมีส่วนไหนของ prompt ที่เราปรับได้ แล้วส่ง prompt ที่แก้แล้วให้ AI อีกครั้ง

    ทำอย่างนี้วนไปจนกว่าจะได้ผลลัพธ์ที่เราต้องการ

    .

    ข้อ 25. Ask AI

    ถ้าไม่รู้ว่าจะเขียน prompt ยังไง หรือ prompt ยังขาดอะไรไป เราสามารถถาม AI ได้ให้ช่วยเราได้

    ตัวอย่าง:


    .

    ข้อ 26. AI, more agentic

    AI จะทำงานแบบอัตโนมัติมากขึ้น และมีมนุษย์เข้ามาเกี่ยวข้องน้อยลง

    .

    ข้อ 27. Human and AI getting closer

    เพราะ AI จะทำงานได้ด้วยตัวเองมากขึ้น AI จะเข้ามามีบทบาทในการทำงานมากขึ้น ทำให้มนุษย์จะทำงานกับ AI อย่างใกล้ชิดมากขึ้น


    😌 Part V. Be Human

    .

    ข้อ 28. Humans required

    แม้ว่า AI จะสามารถทำงานหลาย ๆ อย่างแทนมนุษย์ได้ แต่ในบางงาน เรายังต้องการมนุษย์ด้วยกันเองอยู่ เช่น:

    • นักบิน: แม้ AI จะขับเครื่องบินได้ แต่เราก็อยากให้มีนักบินที่สามารถแก้ปัญหาเฉพาะหน้าอยู่ในห้องนักบิน
    • หมอ: แม้ AI จะวินิจฉัยโรคได้แม่นยำกว่ามนุษย์ แต่เราก็ยังต้องการให้มีคนบอกข่าวดี/ร้ายเป็นคนที่เข้าใจความรู้สึกของเรา
    • Customer service: บางครั้ง เราก็ต้องการคุยกับคนมากกว่า chatbot ที่ตอบเป็น pattern

    .

    ข้อ 29. Skill, like muscle

    ทักษะก็เป็นเหมือนกล้ามเนื้อ เมื่อไม่ได้ใช้งาน ก็จะอ่อนแอลงเรื่อย ๆ

    ถ้าเราใช้ AI ทำทุกอย่างให้เรา ทักษะที่เราเคยมีก็จะค่อย ๆ หายไป

    .

    ข้อ 30. What not to outsource to AI

    4 ทักษะที่เราควรฝึกพัฒนา และไม่ควรให้ AI ทำแทนเรา ได้แก่:

    1. Thinking: การคิด เพราะถ้าเราคิดไม่ได้แล้ว เราจะไม่ประเมินงานของ AI ได้ว่าดี/ไม่ดี
    2. Learning: ถ้าเราไม่เรียนรู้สิ่งใหม่ ๆ เราจะถูกทิ้งไว้ข้างหลัง โดยเฉพาะในยุคที่การเปลี่ยนแปลงเกิดขึ้นอย่างรวดเร็ว
    3. Writing and reading: การเขียนและการอ่านเป็นทักษะที่ช่วยให้เราคิดและเรียนรู้ได้อย่างมีประสิทธิภาพ
    4. Empathy: การเข้าใจคนอื่นเป็นทักษะที่ช่วยให้เราเข้าใจคนอื่นและเชื่อมโยงถึงกันและกันในแบบที่ AI ยังไม่สามารถทำได้

    .

    ข้อ 31. When to use AI

    3 กรณีที่เราจะใช้ AI:

    1. Routine: ใช้ AI ทำงานจำเจหรืองานที่ต้องเป็นประจำ เพื่อที่เราจะได้โฟกัสงานที่ต้องใช้ความคิดมากขึ้น
    2. What and how: ใช้ AI ทำงานในขณะที่เราโฟกัสกับภาพใหญ่ ซึ่งได้แก่ when (ทำเมื่อไร) และ why (ทำไมต้องทำ)
    3. Brainstorm: ใช้ AI ช่วยระดมความคิด เพราะ AI มีข้อมูลเยอะ และช่วยให้เห็นมุมมองที่เราคิดไม่ถึงมาก่อนได้

    🔔 ใครที่ชอบบทความนี้ ฝากกด subscribe และติดตามกันได้ที่: