การใช้งาน Claude ผ่าน API ไม่ได้จำกัดอยู่ที่การสร้างข้อความเท่านั้น — คุณสามารถสอนให้ Claude ใช้เครื่องมือภายนอก (Tools) เพื่อค้นหาข้อมูล คำนวณตัวเลข หรือดำเนินการต่าง ๆ ได้อย่างมีประสิทธิภาพ บทความนี้จะพาคุณเจาะลึกการตั้งค่า tools parameter และเทคนิค multi-turn tool calling พร้อมกรณีศึกษาเฉพาะทางสำหรับนักพัฒนาอีคอมเมิร์ซ และองค์กรที่ต้องการสร้างระบบ RAG

ทำความรู้จัก Claude Tools API

Claude Tools (หรือที่เรียกว่า Function Calling) คือความสามารถที่ช่วยให้ AI สามารถ:

กรณีศึกษาที่ 1: AI ลูกค้าสัมพันธ์สำหรับร้านค้าอีคอมเมิร์ซ

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

import anthropic

client = anthropic.Anthropic(
    api_key="YOUR_HOLYSHEEP_API_KEY",
    base_url="https://api.holysheep.ai/v1"
)

กำหนดเครื่องมือที่ AI สามารถใช้งานได้

tools = [ { "name": "check_inventory", "description": "ตรวจสอบจำนวนสินค้าคงคลัง", "input_schema": { "type": "object", "properties": { "product_id": {"type": "string", "description": "รหัสสินค้า"}, "location": {"type": "string", "description": "คลังสินค้าที่ต้องการตรวจสอบ"} }, "required": ["product_id"] } }, { "name": "create_shipment", "description": "สร้างคำสั่งจัดส่งสินค้า", "input_schema": { "type": "object", "properties": { "order_id": {"type": "string", "description": "รหัสคำสั่งซื้อ"}, "shipping_method": {"type": "string", "enum": ["express", "standard", "economy"]} }, "required": ["order_id"] } } ] message = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, tools=tools, messages=[ {"role": "user", "content": "ลูกค้าสั่งซื้อสินค้า #ORD-2024-888 ต้องการจัดส่งแบบด่วน เช็คสต็อกและดำเนินการให้หน่อย"} ] )

ตรวจสอบว่า AI ต้องการใช้ tool หรือไม่

for block in message.content: if block.type == "tool_use": print(f"Claude ต้องการใช้: {block.name}") print(f"พารามิเตอร์: {block.input}")

โครงสร้าง Tools Parameter เพิ่มเติม

สำหรับการใช้งานขั้นสูง คุณสามารถกำหนด output schema ที่ชัดเจนเพื่อให้ Claude ตอบกลับในรูปแบบที่ต้องการ:

# กรณี RAG System - ค้นหาเอกสารองค์กร
tools_rag = [
    {
        "name": "search_documents",
        "description": "ค้นหาเอกสารภายในองค์กรจาก vector database",
        "input_schema": {
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "คำค้นหา"},
                "top_k": {"type": "integer", "description": "จำนวนผลลัพธ์ที่ต้องการ", "default": 5},
                "department": {"type": "string", "description": "แผนกที่ต้องการค้นหา"}
            },
            "required": ["query"]
        }
    },
    {
        "name": "generate_report",
        "description": "สร้างรายงานจากข้อมูลที่ค้นหาได้",
        "input_schema": {
            "type": "object",
            "properties": {
                "summary": {"type": "string", "description": "สรุปเนื้อหาที่ได้"},
                "format": {"type": "string", "enum": ["markdown", "pdf", "html"], "default": "markdown"}
            },
            "required": ["summary"]
        }
    }
]

เรียกใช้ RAG workflow

response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=2048, tools=tools_rag, messages=[ {"role": "user", "content": "หานโยบายการลางานของแผนก IT และสร้างสรุปมาให้หน่อย"} ] )

Multi-turn Tool Calling Workflow

การใช้งาน tools หลายตัวในลำดับต่อเนื่อง (chaining) เป็นรูปแบบที่พบบ่อยมาก ตัวอย่างเช่น:

# Multi-turn Tool Calling Implementation
def execute_multi_turn_tools(messages, tool_results):
    """ฟังก์ชันสำหรับดำเนินการ multi-turn tool calling"""
    
    # รวม tool result เข้ากับ conversation
    all_messages = messages + tool_results
    
    # ส่ง request ครั้งถัดไป
    response = client.messages.create(
        model="claude-sonnet-4-20250514",
        max_tokens=1024,
        tools=tools,
        messages=all_messages
    )
    
    return response

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

initial_response = client.messages.create( model="claude-sonnet-4-20250514", max_tokens=1024, tools=tools, messages=[{"role": "user", "content": "เช็คสต็อกสินค้า SKU-001 ในคลังกรุงเทพ"}] )

ตรวจสอบว่ามี tool_calls หรือไม่

if initial_response.content[0].type == "tool_use": tool_name = initial_response.content[0].name tool_params = initial_response.content[0].input # จำลองการเรียกใช้ tool if tool_name == "check_inventory": result = {"inventory": 150, "location": "Bangkok Warehouse"} else: result = {"status": "unknown_tool"} # ส่งผลลัพธ์กลับให้ Claude tool_result_message = { "role": "user", "content": f"ผลลัพธ์จาก {tool_name}: {result}" } # รอบที่สอง - Claude จะสร้างคำตอบสมบูรณ์ final_response = execute_multi_turn_tools( messages=[{"role": "user", "content": "เช็คสต็อกสินค้า SKU-001 ในคลังกรุงเทพ"}], tool_results=[tool_result_message] ) print(final_response.content[0].text)

ข้อผิดพลาดที่พบบ่อยและวิธีแก้ไข

1. Error 400: Invalid tool schema

สาเหตุ: schema ของ tool input ไม่ถูกต้องตาม JSON Schema specification

วิธีแก้: ตรวจสอบว่า type ของ parameter ตรงกับที่กำหนด (string, integer, object, array) และไม่มี properties ที่ซ้ำซ้อน

# ❌ ผิด - thi type ไม่มีใน JSON Schema
{"type": "thi", "properties": {"id": {"type": "string"}}}

✅ ถูก - ใช้ type ที่ถูกต้อง

{"type": "object", "properties": {"id": {"type": "string"}}}

2. Error 400: Tool use limit exceeded

สาเหตุ: เรียกใช้ tools มากเกินจำนวนที่กำหนดใน max_tokens หรือ exceed โควต้า

วิธีแก้: เพิ่มค่า max_tokens หรือตรวจสอบโควต้า API ของคุณ หากใช้ HolySheep AI สามารถดูการใช้งานได้ที่แดชบอ