0Pricing
AI Agents · บทเรียน

จากผู้ช่วยสู่เอเจนต์อัตโนมัติ

ช่วงต่อเนื่องจากแชตบอตสู่อัตโนมัติเต็มรูปแบบ: สิ่งที่เปลี่ยนไปในแต่ละขั้น

จากผู้ช่วยสู่เอเจนต์อัตโนมัติ เป็นบทเรียน AI Agents ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AI Agents และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน

ระดับความเป็นอิสระของเอเจนต์

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

ระดับ 1: แชตบอตล้วน

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

import anthropic

# Level 1: Pure chatbot — single turn, no memory, no tools
def pure_chatbot(user_message: str) -> str:
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')
    response = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=512,
        messages=[{'role': 'user', 'content': user_message}]
    )
    return response.content[0].text

# What it has:
#   - Language understanding
#   - Knowledge from training
# What it lacks:
#   - Memory (no history between sessions)
#   - Tools (cannot access external data)
#   - Goals (no objective to pursue)
#   - Proactivity (only responds, never initiates)

response = pure_chatbot('What is the capital of Australia?')
print(response)

ระดับ 2: เอเจนต์เสริมความสามารถด้วยเครื่องมือ

เอเจนต์เสริมความสามารถด้วยเครื่องมือเพิ่มความสามารถภายนอก เช่น การค้นหาเว็บ การสอบถามฐานข้อมูล การเรียกใช้โค้ด และการเรียก API จึงสามารถตอบคำถามที่ต้องใช้ข้อมูลล่าสุดได้ ความจำอาจคงอยู่ภายในเซสชัน ความต้องการการกำกับดูแลจากมนุษย์: ปานกลาง เพราะเอเจนต์อ่านข้อมูลได้ แต่การดำเนินการมักเป็นแบบอ่านอย่างเดียวหรือมีความเสี่ยงต่ำ

import anthropic

# Level 2: Tool-augmented agent
def tool_augmented_agent(user_message: str, conversation_history: list) -> str:
    client = anthropic.Anthropic(api_key='YOUR_API_KEY')

    tools = [
        {
            'name': 'search_web',
            'description': 'Search the web for current information',
            'input_schema': {'type': 'object',
                             'properties': {'query': {'type': 'string'}},
                             'required': ['query']}
        }
    ]

    conversation_history.append({'role': 'user', 'content': user_message})

    response = client.messages.create(
        model='claude-opus-4-5',
        max_tokens=1024,
        tools=tools,
        messages=conversation_history
    )

    # Handle tool use...
    return response.content[-1].text if response.stop_reason == 'end_turn' else '[tool called]'

# What changed vs Level 1:
#   + Tools (external data access)
#   + Session memory (conversation history)
# Still lacking:
#   - Persistent cross-session memory
#   - Goals (still reactive)
#   - Proactivity

ระดับ 3: เอเจนต์มุ่งเป้าหมาย

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

# Level 3: Goal-directed agent
class GoalDirectedAgent:
    def __init__(self, goal: str, tools: list, client):
        self.goal = goal
        self.tools = tools
        self.client = client
        self.memory = []  # persistent across steps
        self.plan = self._make_plan()

    def _make_plan(self) -> list:
        response = self.client.messages.create(
            model='claude-opus-4-5', max_tokens=512,
            messages=[{'role': 'user', 'content':
                f'Goal: {self.goal}\n'
                'Create a numbered list of steps to achieve this goal. '
                'Each step should be a single tool call or reasoning step.'
            }]
        )
        return response.content[0].text

    def step(self) -> str:
        # Execute next planned step
        return 'step executed'

# What changed vs Level 2:
#   + Goal: has an objective to pursue
#   + Planning: decomposes goal into sub-tasks
#   + Persistent memory across steps
# Still lacking:
#   - Self-directed (still initiated by human)
#   - Self-improvement

if __name__ == '__main__':
    class FakeContent:
        def __init__(self, text):
            self.text = text

    class FakeResponse:
        def __init__(self, text):
            self.content = [FakeContent(text)]

    class FakeMessages:
        def create(self, **kwargs):
            return FakeResponse('1. Search web for topic\n2. Summarize findings\n3. Draft report')

    class FakeClient:
        def __init__(self):
            self.messages = FakeMessages()

    agent = GoalDirectedAgent(goal='Write a market report', tools=[], client=FakeClient())
    print('Goal:', agent.goal)
    print('Plan:')
    print(agent.plan)

ระดับ 4: เอเจนต์กำกับตนเอง

เอเจนต์กำกับตนเองกำหนดเป้าหมายย่อยของตนเอง เฝ้าติดตามสภาพแวดล้อมเพื่อหาเหตุการณ์ที่เกี่ยวข้อง และเริ่มดำเนินการโดยไม่ต้องมีมนุษย์สั่ง เอเจนต์มีความจำระยะยาว แบบจำลองโลก และสามารถวางแผนใหม่เมื่อสถานการณ์เปลี่ยนแปลง ความต้องการการกำกับดูแลจากมนุษย์: สูงมาก เพราะเอเจนต์เริ่มดำเนินการได้อย่างอิสระ

import time

# Level 4: Self-directed agent (simplified sketch)
class SelfDirectedAgent:
    def __init__(self, mission: str, client):
        self.mission = mission
        self.client = client
        self.goals_queue = []
        self.long_term_memory = []
        self.running = False

    def start(self):
        self.running = True
        self._generate_initial_goals()
        while self.running:
            self._observe_environment()
            self._prioritise_goals()
            if self.goals_queue:
                goal = self.goals_queue.pop(0)
                self._pursue_goal(goal)
            time.sleep(60)  # autonomous monitoring loop

    def _observe_environment(self):
        # Agent monitors for events without being asked
        print('Observing environment...')

    def _generate_initial_goals(self):
        # Agent decomposes its mission into actionable goals
        self.goals_queue = ['Monitor inbox', 'Check project status']

    def _prioritise_goals(self):
        # Agent re-orders goals based on new observations
        pass

    def _pursue_goal(self, goal: str):
        print(f'Pursuing: {goal}')

if __name__ == '__main__':
    agent = SelfDirectedAgent(mission='Manage my inbox proactively', client=None)
    agent._generate_initial_goals()
    print('Initial goals:', agent.goals_queue)
    agent._observe_environment()
    goal = agent.goals_queue.pop(0)
    agent._pursue_goal(goal)

สิ่งที่เปลี่ยนแปลงในแต่ละระดับ: ความจำ

ข้อกำหนดด้านความจำเพิ่มขึ้นตามระดับความเป็นอิสระ ระดับ 1 ใช้เฉพาะหน้าต่างบริบท (ความจำของเซสชัน) ระดับ 2 เพิ่มประวัติเซสชันถาวร ระดับ 3 เพิ่มสถานะงานแบบมีโครงสร้าง ระดับ 4 ต้องมีความจำเชิงเหตุการณ์ (สิ่งที่เกิดขึ้น) ความจำเชิงความหมาย (สิ่งที่เอเจนต์รู้) และความจำเชิงกระบวนการ (วิธีดำเนินการต่าง ๆ)

MEMORY_BY_LEVEL = {
    'L1_chatbot': {
        'scope': 'context_window_only',
        'persistence': 'none',
        'implementation': 'messages list in current API call'
    },
    'L2_tool_augmented': {
        'scope': 'session',
        'persistence': 'in-memory (lost on restart)',
        'implementation': 'conversation_history list'
    },
    'L3_goal_directed': {
        'scope': 'task',
        'persistence': 'persists for task duration',
        'implementation': 'SQLite or Redis with task state'
    },
    'L4_self_directed': {
        'scope': 'long_term',
        'persistence': 'indefinite',
        'implementation': 'vector DB (episodic) + structured DB (semantic) + prompt cache (procedural)'
    }
}

for level, info in MEMORY_BY_LEVEL.items():
    print(f'{level}: {info["implementation"]}')

สิ่งที่เปลี่ยนแปลงในแต่ละระดับ: การวางแผน

ข้อกำหนดด้านการวางแผนก็เพิ่มขึ้นตามระดับความเป็นอิสระเช่นกัน ระดับ 1 ไม่มีการวางแผน ระดับ 2 อาจใช้การให้เหตุผลทีละขั้น ระดับ 3 ใช้การวางแผนหลายขั้นตอน (การคิดเป็นลำดับขั้น, ReAct) ระดับ 4 ต้องใช้การวางแผนแบบลำดับชั้น พร้อมวางแผนใหม่เมื่อเกิดความล้มเหลว

PLANNING_BY_LEVEL = {
    'L1': 'None — single response',
    'L2': 'Single-step tool selection (which tool to call now)',
    'L3': 'Multi-step plan (goal -> ordered sub-tasks -> tool calls)',
    'L4': 'Hierarchical plan (mission -> goals -> tasks -> actions) + replan on failure'
}

# L3 multi-step planning example:
def plan_goal(goal: str, client) -> list:
    import anthropic
    response = client.messages.create(
        model='claude-opus-4-5', max_tokens=512,
        messages=[{'role': 'user', 'content':
            f'Break this goal into 3-5 concrete steps:\nGoal: {goal}\n'
            'Return JSON: {"steps": [{"step": int, "action": str, "tool": str}]}'
        }]
    )
    import json
    return json.loads(response.content[0].text)

for lvl, desc in PLANNING_BY_LEVEL.items():
    print(f'{lvl}: {desc}')

ข้อกำหนดด้านการกำกับดูแลโดยมนุษย์ในแต่ละระดับ

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

OVERSIGHT_BY_LEVEL = {
    'L1_chatbot': [
        'None required (output only)'
    ],
    'L2_tool_augmented': [
        'Review tool permissions (read-only vs write)',
        'Audit logs of tool calls'
    ],
    'L3_goal_directed': [
        'Human approval before irreversible actions',
        'Plan review before execution starts',
        'Progress checkpoints',
        'Full audit trail'
    ],
    'L4_self_directed': [
        'Human approval before high-impact actions',
        'Real-time action streaming to oversight dashboard',
        'Emergency stop mechanism',
        'Anomaly detection on goal drift',
        'Regular review of long-term memory state',
        'Corrigibility: agent must accept shutdown'
    ]
}

for level, requirements in OVERSIGHT_BY_LEVEL.items():
    print(f'{level}:')
    for req in requirements:
        print(f'  - {req}')

ความเชิงรุก: การเปลี่ยนแปลงสำคัญ

การเปลี่ยนแปลงพื้นฐานที่สุดจากผู้ช่วยไปสู่เอเจนต์อิสระคือ ความเชิงรุก ผู้ช่วยรอ ส่วนเอเจนต์เป็นฝ่ายเริ่ม ซึ่งหมายความว่าเอเจนต์ต้องเฝ้าติดตามสภาพแวดล้อม รับรู้เหตุการณ์ที่เกี่ยวข้อง และตัดสินใจว่าจะดำเนินการเมื่อใด ทั้งหมดนี้โดยไม่ต้องมีคำสั่ง

# Proactive monitoring pattern
import asyncio
from datetime import datetime

class ProactiveMonitor:
    def __init__(self, agent_fn, check_fn, interval_seconds: int = 60):
        self.agent_fn = agent_fn
        self.check_fn = check_fn
        self.interval = interval_seconds

    async def run(self):
        print(f'Proactive monitor started, checking every {self.interval}s')
        while True:
            try:
                events = await self.check_fn()
                for event in events:
                    print(f'[{datetime.utcnow().isoformat()}] Event: {event}')
                    await self.agent_fn(event)
            except Exception as e:
                print(f'Monitor error: {e}')
            await asyncio.sleep(self.interval)

# Example: agent monitors for new emails every 5 minutes
# and proactively drafts replies or flags urgent ones
async def example_setup():
    monitor = ProactiveMonitor(
        agent_fn=lambda e: print(f'Agent handling: {e}'),
        check_fn=lambda: [],  # replace with real inbox check
        interval_seconds=300
    )
    # await monitor.run()

if __name__ == '__main__':
    import asyncio

    async def demo():
        async def check_fn():
            return ['New email from boss@example.com']

        async def agent_fn(event):
            print(f'Agent drafting reply for: {event}')

        monitor = ProactiveMonitor(agent_fn=agent_fn, check_fn=check_fn, interval_seconds=1)
        try:
            await asyncio.wait_for(monitor.run(), timeout=0.3)
        except asyncio.TimeoutError:
            pass

    asyncio.run(demo())

การกู้คืนข้อผิดพลาดในแต่ละระดับ

ข้อกำหนดด้านการกู้คืนข้อผิดพลาดก็เพิ่มขึ้นตามระดับเช่นกัน แชตบอตเพียงกล่าวขอโทษ เอเจนต์ที่ใช้เครื่องมือเรียกใช้เครื่องมือซ้ำ เอเจนต์มุ่งเป้าหมายวางแผนใหม่โดยหลีกเลี่ยงขั้นตอนที่ล้มเหลว ส่วนเอเจนต์กำกับตนเองจะตรวจจับ จัดหมวดหมู่ และส่งต่อข้อผิดพลาดโดยอัตโนมัติ พร้อมปรับปรุงแบบจำลองโลกเพื่อหลีกเลี่ยงความล้มเหลวเดิมในอนาคต

# Error recovery strategies by autonomy level

def chatbot_error_recovery(error: Exception) -> str:
    return 'I\'m sorry, I encountered an error. Please try again.'

def tool_agent_error_recovery(tool_name: str, error: Exception, retries: int) -> str:
    if retries < 3:
        return f'Retrying {tool_name} ({retries+1}/3)'
    return f'Tool {tool_name} unavailable after 3 retries, skipping'

def goal_agent_error_recovery(failed_step: dict, plan: list, client) -> list:
    import anthropic, json
    client_obj = anthropic.Anthropic(api_key='YOUR_API_KEY')
    response = client_obj.messages.create(
        model='claude-opus-4-5', max_tokens=256,
        messages=[{'role': 'user', 'content':
            f'Step failed: {failed_step}\nRemaining plan: {plan}\n'
            'Revise the remaining plan to work around the failure. Return JSON plan.'
        }]
    )
    return json.loads(response.content[0].text)

print('Error recovery patterns defined for each autonomy level')

การเลือกระดับที่เหมาะสม

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

def recommend_autonomy_level(requirements: dict) -> str:
    needs_realtime = requirements.get('realtime_data', False)
    needs_multistep = requirements.get('multi_step_tasks', False)
    needs_unsupervised = requirements.get('runs_unsupervised', False)
    needs_initiative = requirements.get('initiates_actions', False)

    if needs_initiative and needs_unsupervised:
        return 'L4_self_directed (high oversight required)'
    if needs_multistep:
        return 'L3_goal_directed (plan review recommended)'
    if needs_realtime:
        return 'L2_tool_augmented (audit logs required)'
    return 'L1_chatbot (minimal oversight)'

# Examples:
print(recommend_autonomy_level({
    'realtime_data': True, 'multi_step_tasks': False,
    'runs_unsupervised': False, 'initiates_actions': False
}))
print(recommend_autonomy_level({
    'realtime_data': True, 'multi_step_tasks': True,
    'runs_unsupervised': True, 'initiates_actions': True
}))

ตรวจสอบความเข้าใจ

ความสามารถพื้นฐานที่แตกต่างกันมากที่สุดระหว่างเอเจนต์ที่มุ่งเป้าหมาย (ระดับ 3) กับเอเจนต์ที่กำกับตนเอง (ระดับ 4) คืออะไร

สรุป: จากผู้ช่วยสู่เอเจนต์อิสระ

ยอดเยี่ยม! นี่คือสิ่งที่คุณได้เรียนรู้:

  • แชตบอตระดับ 1: ตอบสนองอย่างเดียว ใช้ข้อความเท่านั้น ไม่มีความจำ และไม่ต้องมีการกำกับดูแล
  • เอเจนต์ระดับ 2 ที่เสริมความสามารถด้วยเครื่องมือ: มีความจำของเซสชัน เรียกใช้เครื่องมือ และต้องมีการกำกับดูแลในระดับปานกลาง
  • เอเจนต์ระดับ 3 ที่มุ่งเป้าหมาย: วางแผนหลายขั้นตอน มีความจำตามขอบเขตงาน และมีจุดตรวจอนุมัติ
  • เอเจนต์ระดับ 4 ที่กำกับตนเอง: ทำงานเชิงรุก มีความจำระยะยาว วางแผนแบบลำดับชั้น และต้องมีการกำกับดูแลระดับสูง
  • หลักทั่วไป: เริ่มจากระดับต่ำสุดที่ตรงตามข้อกำหนด

ถัดไป: แบบจำลองโลกและการวางแผนเชิงคาดการณ์ — วิธีที่เอเจนต์จำลองผลลัพธ์ก่อนลงมือทำ

คำถามที่พบบ่อย

บทเรียน “จากผู้ช่วยสู่เอเจนต์อัตโนมัติ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “จากผู้ช่วยสู่เอเจนต์อัตโนมัติ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AI Agents ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AI Agents มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “จากผู้ช่วยสู่เอเจนต์อัตโนมัติ”

ช่วงต่อเนื่องจากแชตบอตสู่อัตโนมัติเต็มรูปแบบ: สิ่งที่เปลี่ยนไปในแต่ละขั้น คุณปฏิบัติ AI Agents ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AI Agents หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AI Agents บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “จากผู้ช่วยสู่เอเจนต์อัตโนมัติ” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน AI Agents นี้ได้ไหม

ได้ บทเรียน AI Agents ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. จากผู้ช่วยสู่เอเจนต์อัตโนมัติ
  2. โมเดลโลกและการวางแผนเชิงคาดการณ์
  3. ความท้าทายด้านการจัดแนวในเอเจนต์อัตโนมัติ
  4. แนวหน้าการวิจัย: AGI และก้าวต่อไป
← กลับไปที่ AI Agents