Amazon Lex
Amazon Lex คือบริการสร้าง chatbot และ voice assistant แบบอัจฉริยะที่ขับเคลื่อนด้วยเทคโนโลยีเดียวกับ Amazon Alexa บริการนี้ใช้ Natural Language Understanding (NLU) และ Automatic Speech Recognition (ASR) ในการเข้าใจเจตนา (intent) ของผู้ใช้จากข้อความหรือเสียง ทำให้สามารถสร้างบทสนทนาอัตโนมัติที่เป็นธรรมชาติได้โดยไม่ต้องมีความเชี่ยวชาญด้าน ML
Amazon Lex V2 (เวอร์ชันล่าสุด) รองรับการสร้างบอทที่รองรับหลายภาษาในบอทเดียว มี visual conversation flow builder สำหรับออกแบบ dialog แบบลากวาง และรองรับ streaming conversations ที่ตอบสนองแบบ real-time สามารถ integrate กับ Amazon Connect สำหรับ call center, Kendra สำหรับ intelligent search และ Lambda สำหรับ business logic ที่ซับซ้อน
AWS Docs: https://docs.aws.amazon.com/lexv2/latest/dg/what-is.html
สถาปัตยกรรม
ฟีเจอร์หลัก
Intents (เจตนาของผู้ใช้)
หัวใจหลักของ Lex คือการกำหนด Intent ซึ่งแทนสิ่งที่ผู้ใช้ต้องการทำ เช่น "BookFlight", "CheckBalance", "CancelOrder" แต่ละ Intent มี sample utterances (ตัวอย่างประโยคที่ผู้ใช้อาจพูด) เพื่อให้ระบบเรียนรู้รูปแบบภาษา รองรับ built-in intents เช่น AMAZON.FallbackIntent และ AMAZON.StopIntent
Slots (ข้อมูลที่ต้องรวบรวม)
Slot คือตัวแปรที่ Lex ต้องรวบรวมจากผู้ใช้เพื่อดำเนินการ Intent เช่น Intent "BookFlight" ต้องการ slot: วันเดินทาง, จุดหมาย, จำนวนผู้โดยสาร Lex จะถามผู้ใช้อัตโนมัติจนครบ slot ที่กำหนด
Slot Types (ประเภทข้อมูล)
กำหนดประเภทของ Slot เช่น AMAZON.Date, AMAZON.Number, AMAZON.City หรือสร้าง custom slot type เอง เช่น รายการสาขาธนาคาร, ประเภทบัญชี รองรับ regex patterns และ custom values พร้อม synonyms
Fulfillment (การดำเนินการ)
เมื่อรวบรวม slot ครบแล้ว Lex จะส่งต่อไปยัง AWS Lambda เพื่อดำเนินการ business logic เช่น เรียก API, ค้นหาฐานข้อมูล, ส่ง notification ผลลัพธ์จะถูกส่งกลับมาแสดงในบทสนทนา
DTMF Input
รองรับการรับ input จากการกด keypad โทรศัพท์ (Dual-Tone Multi-Frequency) สำหรับระบบ IVR (Interactive Voice Response) ใน call center ผู้ใช้กด 1 สำหรับบริการ A, 2 สำหรับบริการ B เป็นต้น
Multi-language Support (Lex V2)
สร้างบอทที่รองรับหลายภาษาในบอทเดียว เช่น ภาษาอังกฤษ, สเปน, ฝรั่งเศส, ญี่ปุ่น, จีน (ประมาณ 10 ภาษา) โดยแต่ละภาษามี NLU model แยกกัน
Streaming Conversations (Lex V2)
รองรับ bidirectional streaming ที่ Lex ตอบสนองแบบ incremental ขณะที่ผู้ใช้ยังพูดอยู่ (interrupt capability) ทำให้บทสนทนาเป็นธรรมชาติมากขึ้นคล้าย human conversation
Visual Conversation Builder
GUI สำหรับออกแบบ conversation flow แบบลากวางใน AWS Console ไม่ต้องเขียน JSON configuration เอง เหมาะสำหรับ business analyst ที่ไม่คุ้นเคยกับการเขียนโค้ด
Integration กับ Amazon Connect
เชื่อมต่อ Lex บอทกับ Amazon Connect (cloud contact center) โดยตรง ทำให้ลูกค้าโทรเข้า call center แล้วพูดคุยกับ AI ก่อน หาก AI ไม่สามารถจัดการได้จะส่งต่อพนักงาน
Integration กับ Amazon Kendra
เชื่อมต่อ Lex กับ Kendra เพื่อให้บอทค้นหาคำตอบจากเอกสารองค์กรได้อัตโนมัติ เมื่อผู้ใช้ถามคำถาม บอทจะค้นหาจาก knowledge base และตอบพร้อมอ้างอิงเอกสาร
การติดตั้งและการตั้งค่า
สร้าง Bot ผ่าน AWS Console
- ไปที่ AWS Console > Amazon Lex > Bots > Create Bot
- กำหนดชื่อบอท, ภาษา, IAM role
- สร้าง Intent และกำหนด sample utterances
- สร้าง Slots พร้อม slot types
- เชื่อมต่อ Lambda สำหรับ fulfillment
- Build และ Test บอทใน Console
- Deploy ไปยัง channel (Web, Slack, Facebook, Connect)
IAM Permissions ที่จำเป็น
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"lex:RecognizeText",
"lex:RecognizeUtterance",
"lex:StartConversation"
],
"Resource": "arn:aws:lex:ap-southeast-1:*:bot-alias/*"
}
]
}
ติดตั้ง SDK
pip install boto3
วิธีใช้งาน
ตัวอย่าง: ส่งข้อความหา Lex Bot (Python)
import boto3
client = boto3.client("lexv2-runtime", region_name="ap-southeast-1")
response = client.recognize_text(
botId="MYBOT123",
botAliasId="TSTALIASID",
localeId="th_TH",
sessionId="user-session-001",
text="ฉันต้องการโอนเงิน 1000 บาทไปยังบัญชี 123-456-789"
)
print("Bot response:", response["messages"][0]["content"])
print("Intent:", response["sessionState"]["intent"]["name"])
ตัวอย่าง: จัดการ Fulfillment ด้วย Lambda
def lambda_handler(event, context):
intent_name = event["sessionState"]["intent"]["name"]
slots = event["sessionState"]["intent"]["slots"]
if intent_name == "TransferMoney":
amount = slots["Amount"]["value"]["interpretedValue"]
account = slots["AccountNumber"]["value"]["interpretedValue"]
# เรียก business logic
result = process_transfer(amount, account)
return {
"sessionState": {
"dialogAction": {"type": "Close"},
"intent": {
"name": intent_name,
"state": "Fulfilled"
}
},
"messages": [
{
"contentType": "PlainText",
"content": f"โอนเงิน {amount} บาทไปยังบัญชี {account} สำเร็จ"
}
]
}
ตัวอย่าง: สร้าง Bot ด้วย AWS CLI
# สร้าง bot
aws lexv2-models create-bot \
--bot-name "BankingBot" \
--role-arn "arn:aws:iam::123456789:role/LexRole" \
--data-privacy '{"childDirected": false}' \
--idle-session-ttl-in-seconds 300
# สร้าง bot locale
aws lexv2-models create-bot-locale \
--bot-id "BOTID123" \
--bot-version "DRAFT" \
--locale-id "th_TH" \
--nlu-intent-confidence-threshold 0.40
ตัวอย่าง: Embed ใน Web App
// ใช้ AWS Amplify + Lex
import { Interactions } from 'aws-amplify';
await Interactions.configure({
bots: {
BankingBot: {
name: "BankingBot",
alias: "$LATEST",
region: "ap-southeast-1",
}
}
});
const response = await Interactions.send("BankingBot", "ตรวจสอบยอดเงิน");
console.log(response.message);
ราคา (ประมาณการในบาท)
ราคาคำนวณจาก 1 USD = 35 บาท (ราคา US East region)
Text Requests
| ปริมาณ | USD/request | บาท/request |
|---|---|---|
| ทุก request | $0.004 | ~0.14 |
Voice/Speech Requests
| ปริมาณ | USD/วินาที | บาท/วินาที |
|---|---|---|
| ทุก request | $0.00075 | ~0.026 |
Free Tier (12 เดือนแรก)
- Text: 10,000 requests/เดือน
- Voice: 5,000 requests/เดือน (ประมาณ 10,000 วินาที)
ตัวอย่างค่าใช้จ่ายรายเดือน
- บอท customer service รับ 50,000 text request/เดือน: ~200 บาท
- Call center IVR (30,000 call เฉลี่ย 2 นาที/call): ~9,450 บาท
- บอทผสม text+voice (100,000 text + 20,000 voice 30 วินาที): ~694 บาท
เหมาะสำหรับ
- ธุรกิจที่ต้องการ customer service chatbot บน website หรือ mobile app
- Call center ที่ต้องการ IVR อัจฉริยะที่เข้าใจภาษาธรรมชาติแทนการกด keypad
- ธนาคาร บริษัทประกัน และบริการทางการเงินที่ต้องการ self-service แบบอัตโนมัติ
- ร้านค้าออนไลน์ที่ต้องการบอทรับคำสั่งซื้อและติดตาม order
- โรงพยาบาลที่ต้องการ appointment booking bot
- บริษัทที่ต้องการ internal helpdesk bot สำหรับพนักงาน
ใช้ร่วมกับ AWS Services
- AWS Lambda — business logic, database queries, API calls
- Amazon Connect — cloud contact center, IVR
- Amazon Kendra — intelligent search จาก knowledge base
- Amazon DynamoDB — เก็บ session state และ user profile
- Amazon Polly — text-to-speech สำหรับ voice response
- Amazon Transcribe — speech-to-text เพิ่มเติม
- Amazon CloudWatch — monitor bot performance, logs
- Amazon SNS/SES — ส่ง notification หลังจาก intent สำเร็จ
- AWS Amplify — embed bot ใน web/mobile app
- Amazon Alexa — ใช้ skill เดียวกันบน Alexa device
Use Case ตัวอย่าง
1. Customer Service Bot ธนาคารออนไลน์ไทย
ธนาคารออนไลน์ใช้ Amazon Lex สร้างบอทบน LINE Official Account และ mobile app รองรับ intent ต่าง ๆ เช่น ตรวจสอบยอดเงิน, โอนเงิน, ชำระบิล, รายงานบัตรหาย, ค้นหา ATM ใกล้เคียง บอทเชื่อมต่อกับ Lambda ที่เรียก Core Banking API และ DynamoDB เพื่อดึงข้อมูลลูกค้า สามารถจัดการคำถามได้ 70% โดยไม่ส่งต่อพนักงาน ลดค่าใช้จ่าย call center ได้ 45% และให้บริการได้ตลอด 24 ชั่วโมง 7 วัน
2. ระบบ IVR อัจฉริยะสำหรับ Call Center สายการบิน
สายการบินไทยแห่งหนึ่งใช้ Lex V2 เชื่อมต่อกับ Amazon Connect สร้างระบบ IVR ที่ลูกค้าโทรเข้ามาพูดได้เลยว่าต้องการอะไร เช่น "อยากเปลี่ยนที่นั่งเที่ยวบิน TG100 วันพรุ่งนี้" ระบบเข้าใจ intent "ChangeSeat" และ slot flight_number, date จากนั้น Lambda เรียก booking system เพื่อตรวจสอบที่นั่งว่างและดำเนินการ ลดเวลาการจัดการต่อสายจาก 8 นาทีเหลือ 2 นาที
3. HR Helpdesk Bot ภายในองค์กร
บริษัทขนาดใหญ่ที่มีพนักงาน 3,000 คนใช้ Lex สร้าง HR chatbot บน Microsoft Teams พนักงานถามเรื่อง วันหยุด, การลางาน, สวัสดิการ, นโยบายบริษัท บอทเชื่อมต่อกับ Kendra ที่ index เอกสาร HR policy จาก SharePoint และ Lambda ที่เรียก HR system API สำหรับการลา-ขอสิทธิ์ต่าง ๆ ลดภาระ HR team ในการตอบคำถามซ้ำๆ ลงได้ 80%