AI
AI Agents
Autonomous
LangChain
Automation
Advanced

AI Agents: ระบบ AI ที่ทำงานได้อัตโนมัติ

เรียนรู้ AI Agents คืออะไร ทำงานอย่างไร พร้อมตัวอย่างการสร้าง autonomous AI agents ด้วย LangChain, AutoGPT และ frameworks อื่นๆ

AI Unlocked Team
31/01/2568
AI Agents: ระบบ AI ที่ทำงานได้อัตโนมัติ

AI Agents: ระบบ AI ที่ทำงานได้อัตโนมัติ

AI Agents เป็นวิวัฒนาการขั้นถัดไปของ AI จากที่เคยเป็นแค่ระบบตอบคำถาม กลายเป็นระบบที่คิด วางแผน และลงมือทำได้ด้วยตัวเอง บทความนี้จะพาคุณไปรู้จักกับ AI Agents และวิธีสร้างระบบ autonomous AI

AI Agents คืออะไร?

AI Agent คือระบบ AI ที่สามารถ:

  1. รับ Goal จากผู้ใช้
  2. วางแผน ว่าจะทำอะไรบ้าง
  3. ลงมือทำ ด้วยการใช้ tools ต่างๆ
  4. สังเกตผลลัพธ์ และปรับแผน
  5. ทำซ้ำ จนบรรลุเป้าหมาย

Traditional AI vs AI Agents

Traditional AI (Chatbot):
User: "ช่วยจองตั๋วเครื่องบินให้หน่อย"
AI: "คุณต้องการไปที่ไหน วันไหนครับ?"
User: "กรุงเทพ-เชียงใหม่ วันที่ 15"
AI: "ลองเข้าไปที่ website ของสายการบินครับ"
→ AI แค่ให้คำแนะนำ

AI Agent:
User: "ช่วยจองตั๋วเครื่องบินกรุงเทพ-เชียงใหม่ วันที่ 15 งบ 2000 บาท"
AI Agent:
1. ค้นหาเที่ยวบินจาก API
2. เปรียบเทียบราคา
3. เลือกเที่ยวบินที่เหมาะสม
4. กรอกข้อมูลจอง
5. ส่งยืนยันให้ user
→ AI ลงมือทำให้จริง

Agent Architecture

┌─────────────────────────────────────────────────────────────────┐
│                    AI Agent Architecture                        │
├─────────────────────────────────────────────────────────────────┤
│                                                                 │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │                         User Goal                         │  │
│  │              "Book a flight to Chiang Mai"                │  │
│  └──────────────────────────┬───────────────────────────────┘  │
│                             │                                   │
│                             ▼                                   │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │                    Planning Module                        │  │
│  │  1. Search available flights                              │  │
│  │  2. Compare prices                                        │  │
│  │  3. Select best option                                    │  │
│  │  4. Make booking                                          │  │
│  └──────────────────────────┬───────────────────────────────┘  │
│                             │                                   │
│        ┌────────────────────┼────────────────────┐             │
│        │                    │                    │             │
│        ▼                    ▼                    ▼             │
│  ┌──────────┐        ┌──────────┐        ┌──────────┐         │
│  │ Tool 1   │        │ Tool 2   │        │ Tool 3   │         │
│  │ Search   │        │ Compare  │        │ Book     │         │
│  │ API      │        │ Logic    │        │ API      │         │
│  └────┬─────┘        └────┬─────┘        └────┬─────┘         │
│       │                   │                   │                │
│       └───────────────────┼───────────────────┘                │
│                           │                                    │
│                           ▼                                    │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │                   Memory / State                          │  │
│  │  - Conversation history                                   │  │
│  │  - Task progress                                          │  │
│  │  - Learned preferences                                    │  │
│  └──────────────────────────────────────────────────────────┘  │
│                           │                                    │
│                           ▼                                    │
│  ┌──────────────────────────────────────────────────────────┐  │
│  │                    Observation                            │  │
│  │  "Flight booked successfully. Confirmation: ABC123"       │  │
│  └──────────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────────┘

ประเภทของ AI Agents

1. Simple Reflex Agents

ตอบสนองตาม input โดยไม่มี memory

# Simple reflex agent
def simple_agent(input):
    if "hello" in input.lower():
        return "Hello! How can I help?"
    elif "bye" in input.lower():
        return "Goodbye!"
    else:
        return "I don't understand"

2. Model-Based Agents

มี internal model ของโลกภายนอก

class ModelBasedAgent:
    def __init__(self):
        self.world_model = {}

    def perceive(self, observation):
        # Update world model
        self.world_model.update(observation)

    def act(self, goal):
        # Use world model to decide action
        if self.world_model.get("obstacle_ahead"):
            return "turn_left"
        return "move_forward"

3. Goal-Based Agents

วางแผนเพื่อบรรลุเป้าหมาย

class GoalBasedAgent:
    def __init__(self, tools):
        self.tools = tools
        self.plan = []

    def create_plan(self, goal):
        # Use AI to create plan
        self.plan = self.ai.plan(goal, self.tools)

    def execute(self):
        for step in self.plan:
            result = self.tools[step.tool].execute(step.params)
            if not result.success:
                self.replan()

4. Learning Agents

เรียนรู้และปรับปรุงตัวเอง

class LearningAgent:
    def __init__(self):
        self.experience = []
        self.performance_history = []

    def act(self, state):
        action = self.policy(state)
        return action

    def learn(self, state, action, reward, next_state):
        self.experience.append((state, action, reward, next_state))
        self.update_policy()

    def update_policy(self):
        # Improve based on experience
        pass

สร้าง AI Agent ด้วย LangChain

Basic Agent with Tools

from langchain.agents import create_openai_functions_agent, AgentExecutor
from langchain_openai import ChatOpenAI
from langchain.tools import tool
from langchain import hub

# Define tools
@tool
def search_web(query: str) -> str:
    """Search the web for information."""
    # Implement web search
    return f"Search results for: {query}"

@tool
def calculate(expression: str) -> str:
    """Calculate mathematical expressions."""
    try:
        result = eval(expression)
        return str(result)
    except:
        return "Error in calculation"

@tool
def send_email(to: str, subject: str, body: str) -> str:
    """Send an email."""
    # Implement email sending
    return f"Email sent to {to}"

# Create agent
llm = ChatOpenAI(model="gpt-4o", temperature=0)
tools = [search_web, calculate, send_email]
prompt = hub.pull("hwchase17/openai-functions-agent")

agent = create_openai_functions_agent(llm, tools, prompt)
agent_executor = AgentExecutor(agent=agent, tools=tools, verbose=True)

# Run agent
result = agent_executor.invoke({
    "input": "Search for the current Bitcoin price and calculate 10% of it"
})
print(result["output"])

Agent with Memory

from langchain.memory import ConversationBufferMemory
from langchain.agents import create_openai_functions_agent, AgentExecutor

# Create memory
memory = ConversationBufferMemory(
    memory_key="chat_history",
    return_messages=True
)

# Create agent with memory
agent_executor = AgentExecutor(
    agent=agent,
    tools=tools,
    memory=memory,
    verbose=True
)

# Conversation with memory
result1 = agent_executor.invoke({"input": "My name is John"})
print(result1["output"])

result2 = agent_executor.invoke({"input": "What's my name?"})
print(result2["output"])  # "Your name is John"

ReAct Agent (Reasoning + Acting)

from langchain.agents import create_react_agent
from langchain import hub

# Get ReAct prompt
prompt = hub.pull("hwchase17/react")

# Create ReAct agent
react_agent = create_react_agent(llm, tools, prompt)

agent_executor = AgentExecutor(
    agent=react_agent,
    tools=tools,
    verbose=True,
    handle_parsing_errors=True
)

# ReAct agent shows reasoning
result = agent_executor.invoke({
    "input": "What is 25% of the current price of Apple stock?"
})

# Output shows:
# Thought: I need to find the current Apple stock price first
# Action: search_web
# Action Input: "current Apple stock price"
# Observation: Apple stock is currently $175
# Thought: Now I need to calculate 25% of $175
# Action: calculate
# Action Input: "175 * 0.25"
# Observation: 43.75
# Final Answer: 25% of Apple's current stock price is $43.75

สร้าง Custom AI Agent

Complete Agent Implementation

import json
from typing import List, Dict, Any
from openai import OpenAI

class CustomAgent:
    """Custom AI Agent with tools and memory"""

    def __init__(self, system_prompt: str = None):
        self.client = OpenAI()
        self.tools = {}
        self.memory = []
        self.max_iterations = 10
        self.system_prompt = system_prompt or """You are a helpful AI agent.
You can use tools to accomplish tasks.
Always think step by step."""

    def register_tool(self, name: str, func, description: str, parameters: dict):
        """Register a tool for the agent to use"""
        self.tools[name] = {
            "function": func,
            "schema": {
                "type": "function",
                "function": {
                    "name": name,
                    "description": description,
                    "parameters": parameters
                }
            }
        }

    def get_tool_schemas(self) -> List[dict]:
        """Get OpenAI-compatible tool schemas"""
        return [tool["schema"] for tool in self.tools.values()]

    async def run(self, user_input: str) -> str:
        """Run the agent with a user input"""
        self.memory.append({"role": "user", "content": user_input})

        for iteration in range(self.max_iterations):
            # Call LLM
            response = self.client.chat.completions.create(
                model="gpt-4o",
                messages=[
                    {"role": "system", "content": self.system_prompt},
                    *self.memory
                ],
                tools=self.get_tool_schemas() if self.tools else None,
                tool_choice="auto"
            )

            message = response.choices[0].message

            # Check if agent wants to use a tool
            if message.tool_calls:
                # Execute each tool call
                for tool_call in message.tool_calls:
                    tool_name = tool_call.function.name
                    tool_args = json.loads(tool_call.function.arguments)

                    print(f"Agent using tool: {tool_name}")
                    print(f"Arguments: {tool_args}")

                    # Execute tool
                    if tool_name in self.tools:
                        result = await self.tools[tool_name]["function"](**tool_args)
                    else:
                        result = f"Error: Tool '{tool_name}' not found"

                    # Add tool result to memory
                    self.memory.append(message)
                    self.memory.append({
                        "role": "tool",
                        "tool_call_id": tool_call.id,
                        "content": str(result)
                    })

            else:
                # Agent is done, return final response
                final_response = message.content
                self.memory.append({"role": "assistant", "content": final_response})
                return final_response

        return "Agent reached maximum iterations without completing."

    def clear_memory(self):
        """Clear agent memory"""
        self.memory = []

# Usage
async def main():
    agent = CustomAgent()

    # Register tools
    async def search_products(query: str, max_results: int = 5) -> str:
        # Simulate product search
        return json.dumps([
            {"name": "Product A", "price": 100},
            {"name": "Product B", "price": 150}
        ])

    async def add_to_cart(product_name: str, quantity: int = 1) -> str:
        return f"Added {quantity}x {product_name} to cart"

    async def checkout(payment_method: str) -> str:
        return f"Order placed successfully with {payment_method}"

    agent.register_tool(
        name="search_products",
        func=search_products,
        description="Search for products in the catalog",
        parameters={
            "type": "object",
            "properties": {
                "query": {"type": "string", "description": "Search query"},
                "max_results": {"type": "integer", "description": "Max results"}
            },
            "required": ["query"]
        }
    )

    agent.register_tool(
        name="add_to_cart",
        func=add_to_cart,
        description="Add a product to shopping cart",
        parameters={
            "type": "object",
            "properties": {
                "product_name": {"type": "string"},
                "quantity": {"type": "integer"}
            },
            "required": ["product_name"]
        }
    )

    agent.register_tool(
        name="checkout",
        func=checkout,
        description="Complete the purchase",
        parameters={
            "type": "object",
            "properties": {
                "payment_method": {"type": "string"}
            },
            "required": ["payment_method"]
        }
    )

    # Run agent
    result = await agent.run(
        "Find a laptop under $500 and buy it with credit card"
    )
    print(result)

Multi-Agent Systems

Agent Collaboration

from typing import List

class MultiAgentSystem:
    """System for multiple agents to collaborate"""

    def __init__(self):
        self.agents = {}
        self.coordinator = None

    def add_agent(self, name: str, agent: CustomAgent, role: str):
        """Add an agent with a specific role"""
        self.agents[name] = {
            "agent": agent,
            "role": role
        }

    def set_coordinator(self, coordinator_agent: CustomAgent):
        """Set the coordinator agent"""
        self.coordinator = coordinator_agent

    async def execute_task(self, task: str) -> str:
        """Execute a task using multiple agents"""
        # Coordinator creates plan
        plan = await self.coordinator.run(f"""
        Task: {task}

        Available agents and their roles:
        {json.dumps({name: info['role'] for name, info in self.agents.items()})}

        Create a step-by-step plan assigning tasks to appropriate agents.
        Return JSON: {{"steps": [{{"agent": "name", "task": "description"}}]}}
        """)

        steps = json.loads(plan)["steps"]

        results = []
        for step in steps:
            agent_name = step["agent"]
            agent_task = step["task"]

            if agent_name in self.agents:
                result = await self.agents[agent_name]["agent"].run(agent_task)
                results.append({
                    "agent": agent_name,
                    "task": agent_task,
                    "result": result
                })

        # Coordinator summarizes
        summary = await self.coordinator.run(f"""
        Task completed. Results:
        {json.dumps(results)}

        Provide a final summary of what was accomplished.
        """)

        return summary

# Example: Research Team
async def research_team():
    system = MultiAgentSystem()

    # Create specialized agents
    researcher = CustomAgent(system_prompt="You are a research agent. Search and gather information.")
    analyst = CustomAgent(system_prompt="You are an analyst. Analyze data and find insights.")
    writer = CustomAgent(system_prompt="You are a writer. Create clear, well-structured reports.")
    coordinator = CustomAgent(system_prompt="You are a project coordinator. Plan and manage tasks.")

    # Register tools for each agent
    # (Register appropriate tools for each role)

    system.add_agent("researcher", researcher, "Gathers information from various sources")
    system.add_agent("analyst", analyst, "Analyzes data and provides insights")
    system.add_agent("writer", writer, "Creates written reports and summaries")
    system.set_coordinator(coordinator)

    # Execute complex task
    result = await system.execute_task(
        "Research the impact of AI on job market in 2024 and create a report"
    )
    print(result)

AI Agent Use Cases

1. Customer Service Agent

class CustomerServiceAgent(CustomAgent):
    def __init__(self, knowledge_base):
        super().__init__(system_prompt="""
You are a customer service agent for XYZ Company.
Be helpful, friendly, and professional.
If you cannot solve the issue, escalate to human support.
        """)
        self.knowledge_base = knowledge_base

        # Register tools
        self.register_tool(
            name="search_kb",
            func=self.search_knowledge_base,
            description="Search company knowledge base",
            parameters={"type": "object", "properties": {"query": {"type": "string"}}, "required": ["query"]}
        )

        self.register_tool(
            name="create_ticket",
            func=self.create_support_ticket,
            description="Create a support ticket for human review",
            parameters={"type": "object", "properties": {
                "issue": {"type": "string"},
                "priority": {"type": "string", "enum": ["low", "medium", "high"]}
            }, "required": ["issue"]}
        )

    async def search_knowledge_base(self, query: str) -> str:
        # Search RAG system
        results = self.knowledge_base.search(query)
        return json.dumps(results)

    async def create_support_ticket(self, issue: str, priority: str = "medium") -> str:
        # Create ticket in ticketing system
        ticket_id = await create_ticket(issue, priority)
        return f"Ticket created: {ticket_id}"

2. Research Agent

class ResearchAgent(CustomAgent):
    def __init__(self):
        super().__init__(system_prompt="""
You are a research assistant.
Thoroughly research topics using multiple sources.
Always cite your sources.
        """)

        self.register_tool("web_search", self.web_search, "Search the web", {...})
        self.register_tool("read_paper", self.read_paper, "Read academic paper", {...})
        self.register_tool("summarize", self.summarize, "Summarize text", {...})

    async def research(self, topic: str) -> dict:
        result = await self.run(f"""
        Research this topic thoroughly: {topic}

        1. Search for recent information
        2. Read relevant papers or articles
        3. Summarize key findings
        4. Provide citations
        """)
        return result

3. Coding Agent

class CodingAgent(CustomAgent):
    def __init__(self):
        super().__init__(system_prompt="""
You are an expert programmer.
Write clean, well-documented code.
Always test your code before returning.
        """)

        self.register_tool("read_file", self.read_file, "Read a file", {...})
        self.register_tool("write_file", self.write_file, "Write to a file", {...})
        self.register_tool("run_code", self.run_code, "Execute code", {...})
        self.register_tool("run_tests", self.run_tests, "Run tests", {...})

    async def implement_feature(self, spec: str) -> str:
        return await self.run(f"""
        Implement this feature:
        {spec}

        Steps:
        1. Read relevant existing code
        2. Plan the implementation
        3. Write the code
        4. Write tests
        5. Run tests and fix any issues
        """)

Best Practices

1. Tool Design

# Good tool design
@tool
def search_products(
    query: str,
    category: str = None,
    min_price: float = None,
    max_price: float = None,
    sort_by: str = "relevance"
) -> str:
    """
    Search for products in the catalog.

    Args:
        query: Search query string
        category: Filter by category (optional)
        min_price: Minimum price filter (optional)
        max_price: Maximum price filter (optional)
        sort_by: Sort results by 'relevance', 'price_low', 'price_high'

    Returns:
        JSON string with list of matching products
    """
    # Implementation
    pass

2. Error Handling

class RobustAgent(CustomAgent):
    async def run(self, user_input: str) -> str:
        try:
            return await super().run(user_input)
        except Exception as e:
            # Log error
            logger.error(f"Agent error: {e}")

            # Graceful degradation
            return "I encountered an issue. Let me try a different approach."

    def validate_tool_output(self, tool_name: str, output: Any) -> bool:
        """Validate tool output before using"""
        if output is None:
            return False
        if isinstance(output, str) and "error" in output.lower():
            return False
        return True

3. Monitoring

class MonitoredAgent(CustomAgent):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.metrics = {
            "total_runs": 0,
            "successful_runs": 0,
            "tool_usage": {},
            "avg_iterations": 0
        }

    async def run(self, user_input: str) -> str:
        self.metrics["total_runs"] += 1
        start_time = time.time()

        try:
            result = await super().run(user_input)
            self.metrics["successful_runs"] += 1
            return result
        finally:
            duration = time.time() - start_time
            self.log_metrics(duration)

สรุป

AI Agents เป็นอนาคตของ AI applications ที่จะเปลี่ยนวิธีที่เราใช้งาน AI จากการถามตอบ เป็นการมอบหมายงานให้ AI ทำแทนได้อย่างอิสระ


อ่านบทความที่เกี่ยวข้อง


พร้อมสร้าง AI Agent แล้วหรือยัง?

ติดต่อทีม AI Unlocked สำหรับคำปรึกษาและการพัฒนา AI Agent ที่ตอบโจทย์ธุรกิจของคุณ


เขียนโดย

AI Unlocked Team