// Hands-On Guide
Build Your First Agent
สร้าง AI Agent จาก 0 ด้วย Python + OpenAI Agents SDK — ใช้เวลาแค่ 30 นาที ไม่ต้องมีพื้นฐาน ML
// Prerequisites
สิ่งที่ต้องมีก่อนเริ่ม
Python 3.10+
ติดตั้ง Python 3.10 ขึ้นไป — ตรวจสอบด้วย python --version
OpenAI API Key
สมัคร account ที่ platform.openai.com แล้วสร้าง API key
pip packages
openai>=1.66, python-dotenv — ติดตั้งด้วย pip install
01
ติดตั้ง Dependencies
# สร้าง virtual environment (แนะนำ)
python -m venv agent_env
source agent_env/bin/activate # Mac/Linux
agent_env\Scripts\activate # Windows
# ติดตั้ง packages
pip install openai python-dotenv
# ตรวจสอบ version
pip show openai # ต้องเป็น 1.66+ สำหรับ Agents SDK
02
ตั้งค่า API Key
# สร้างไฟล์ .env ในโฟลเดอร์โปรเจกต์
OPENAI_API_KEY=sk-proj-xxxxxxxxxxxxxxxxxxxx
# ใน Python โหลด key ด้วย dotenv
from dotenv import load_dotenv
import os
load_dotenv()
api_key = os.getenv("OPENAI_API_KEY")
03
สร้าง Agent แรก — Research Agent
from agents import Agent, Runner, WebSearchTool
from dotenv import load_dotenv
load_dotenv()
# 1. สร้าง Tool — Web Search
web_search = WebSearchTool()
# 2. สร้าง Agent พร้อม persona และ tools
research_agent = Agent(
name="ResearchBot",
instructions="""
คุณคือ AI Research Assistant ที่เก่งด้านการค้นหาข้อมูล
ให้ค้นหาข้อมูลล่าสุดและสรุปผลอย่างชัดเจนเป็นภาษาไทย
อ้างอิงแหล่งข้อมูลทุกครั้ง
""",
model="gpt-4o",
tools=[web_search],
)
# 3. รัน Agent ด้วย task
result = Runner.run_sync(
research_agent,
"สรุป 5 เทรนด์ AI ที่น่าสนใจที่สุดในปี 2025"
)
print(result.final_output)
04
เพิ่ม Custom Tools
from agents import Agent, Runner, function_tool
import requests, json
# สร้าง custom tool ด้วย decorator @function_tool
@function_tool
def get_weather(city: str) -> str:
"""Get current weather for a city"""
# เรียก weather API
url = f"https://wttr.in/{city}?format=3"
response = requests.get(url)
return response.text
@function_tool
def calculate(expression: str) -> str:
"""Safely evaluate math expression"""
try:
result = eval(expression, {"__builtins__": {}})
return f"Result: {result}"
except Exception as e:
return f"Error: {e}"
# Agent พร้อม custom tools
assistant = Agent(
name="SmartAssistant",
instructions="คุณเป็น assistant ที่ช่วยตรวจสอบอากาศและคำนวณ",
model="gpt-4o-mini",
tools=[get_weather, calculate],
)
result = Runner.run_sync(assistant, "อากาศกรุงเทพวันนี้เป็นยังไง และ 1234 * 5678 = ?")
print(result.final_output)
// Pro Tips
เคล็ดลับสำหรับ Production Agent
เขียน System Prompt ให้ชัด
ระบุ persona, ขอบเขตงาน, format output และข้อห้ามอย่างชัดเจน — prompt คือ "วิญญาณ" ของ agent
ออกแบบ Tools ให้ atomic
แต่ละ tool ควรทำสิ่งเดียวให้ดี มี description ชัดเจน agent จะเลือกใช้ถูกต้องกว่า tool ที่ทำหลายอย่าง
เพิ่ม Error Handling
tool ต้องจัดการ error แล้ว return ข้อความที่อ่านได้ ไม่ raise exception — ให้ agent ปรับแผนได้เมื่อ tool ล้มเหลว
ควบคุม Token Usage
ตั้ง max_turns เพื่อจำกัด loop, ใช้ gpt-4o-mini สำหรับงานง่าย, streaming สำหรับ long responses
Log ทุก action
บันทึก thought, action, observation ทุก step เพื่อ debug — ใช้ trace ใน OpenAI SDK หรือ LangSmith
Human-in-the-loop
สำหรับ action ที่กระทบจริงเช่น ส่ง email, ลบไฟล์ — ขอ approval จากมนุษย์ก่อนเสมอใน production
พร้อมขึ้น Level สู่ Multi-Agent?
เรียนรู้การสร้างระบบ agents หลายตัวทำงานร่วมกัน — Orchestration และ Communication patterns