Voice Chat Agent - Enhanced Version
ระบบ Voice Chat Agent ที่รองรับการจัดการ Intent ผ่าน Database และใช้ Azure Speech Service สำหรับ STT/TTS
คุณสมบัติ
- ✅ จัดการ Intent ผ่าน Database (เพิ่ม/แก้ไข/ลบ)
- ✅ ใช้ Azure Speech Service สำหรับ Speech-to-Text (STT)
- ✅ ใช้ Azure Speech Service สำหรับ Text-to-Speech (TTS)
- ✅ หน้า Control Panel สำหรับจัดการ Intent และการตั้งค่าระบบ
- ✅ บันทึกการสนทนาลง Database
- ✅ รองรับการตั้งค่าผ่าน Database
โครงสร้างโฟลเดอร์
voice-chat-agent/
├─ config/
│ ├─ config.example.php
│ └─ config.php (สร้างเอง)
├─ database/
│ └─ schema.sql (SQL สำหรับสร้างตาราง)
├─ public/
│ ├─ api/
│ │ ├─ intents.php (API จัดการ Intent)
│ │ └─ settings.php (API จัดการการตั้งค่า)
│ ├─ control/
│ │ └─ index.html (หน้า Control Panel)
│ └─ index.php (API endpoint หลัก)
├─ src/
│ ├─ AzureSpeechClient.php (Azure Speech Service client)
│ ├─ Bootstrap.php
│ ├─ ConversationHelper.php
│ ├─ Database.php (Database helper)
│ ├─ helpers.php
│ ├─ HttpClient.php
│ ├─ IntentClassifier.php (อ่าน Intent จาก Database)
│ ├─ IntentRouter.php
│ ├─ OpenAiClient.php
│ ├─ ResponseFormatter.php
│ └─ TextToSpeech.php (ใช้ Azure)
├─ storage/
│ └─ logs/
└─ README.md
การติดตั้ง
1. ติดตั้ง Database
รัน SQL script เพื่อสร้างตาราง:
mysql -u root -p < database/schema.sql
หรือเปิดไฟล์ database/schema.sql และรันใน phpMyAdmin
2. ตั้งค่า Configuration
คัดลอกไฟล์ config:
cp config/config.example.php config/config.php
แก้ไข config/config.php:
return [
// Database
'db_host' => 'localhost',
'db_name' => 'voice_chat_agent',
'db_user' => 'root',
'db_pass' => 'your_password',
// Azure Speech Service
'azure_speech_key' => 'your_azure_key',
'azure_speech_region' => 'southeastasia',
// OpenAI (สำหรับ Chat และ Intent Classification)
'openai_api_key' => 'your_openai_key',
// ... อื่นๆ
];
3. ตั้งค่าผ่าน Control Panel
- เปิดเบราว์เซอร์ไปที่:
http://your-domain/voice-chat-agent/public/control/ - ไปที่แท็บ "การตั้งค่าระบบ"
- กรอกข้อมูล Azure Speech Service และ OpenAI
- บันทึกการตั้งค่า
การใช้งาน
API Endpoint หลัก
POST /voice-chat-agent/public/index.php
Request Body:
{
"message": "สวัสดี",
"sessionId": "session_123",
"userId": "user_123",
"conversation_history": [
{
"question": "สวัสดี",
"answer": "สวัสดีค่ะ"
}
]
}
Response:
{
"status": "ok",
"transcript": "",
"response": "สวัสดีค่ะ น้องแอมยินดีให้บริการค่ะ",
"intent": "greeting",
"intent_confidence": "high",
"audio_base64": "...",
"audio_mime_type": "audio/mpeg",
"sessionId": "session_123"
}
จัดการ Intent
ดูรายการ Intent:
GET /voice-chat-agent/public/api/intents.php?action=list
เพิ่ม Intent:
POST /voice-chat-agent/public/api/intents.php
Content-Type: application/json
{
"name": "new_intent",
"display_name": "Intent ใหม่",
"description": "คำอธิบาย",
"keywords": ["keyword1", "keyword2"],
"system_prompt": "System prompt...",
"api_endpoint": "rooms",
"http_method": "GET",
"is_active": 1,
"sort_order": 10
}
แก้ไข Intent:
PUT /voice-chat-agent/public/api/intents.php?id=1
Content-Type: application/json
{
"id": 1,
"name": "updated_intent",
...
}
ลบ Intent:
DELETE /voice-chat-agent/public/api/intents.php?id=1
จัดการการตั้งค่า
ดูการตั้งค่าทั้งหมด:
GET /voice-chat-agent/public/api/settings.php
อัปเดตการตั้งค่า:
PUT /voice-chat-agent/public/api/settings.php
Content-Type: application/json
{
"azure_speech_key": "new_key",
"azure_speech_region": "southeastasia",
"openai_api_key": "new_key",
...
}
Control Panel
เปิดเบราว์เซอร์ไปที่:
http://your-domain/voice-chat-agent/public/control/
ฟีเจอร์
-
จัดการ Intent
- เพิ่ม Intent ใหม่
- แก้ไข Intent ที่มีอยู่
- ลบ Intent
- เปิด/ปิด Intent
-
การตั้งค่าระบบ
- ตั้งค่า Azure Speech Service
- ตั้งค่า OpenAI
- ตั้งค่า API Base URL
- ตั้งค่าชื่อและบุคลิกภาพผู้ช่วย
หมายเหตุ
- ระบบใช้ Azure Speech Service สำหรับ STT และ TTS แทน OpenAI
- Intent ทั้งหมดถูกเก็บใน Database และสามารถจัดการผ่าน Control Panel
- การตั้งค่าสามารถบันทึกใน Database หรือ config file
- ระบบจะอ่านการตั้งค่าจาก Database ก่อน ถ้าไม่มีจะใช้จาก config file
Troubleshooting
| ปัญหา | วิธีแก้ |
|---|---|
| Database connection failed | ตรวจสอบ db_host, db_user, db_pass ใน config.php |
| Azure Speech Service ไม่ทำงาน | ตรวจสอบ azure_speech_key และ azure_speech_region |
| Intent ไม่แสดง | ตรวจสอบว่า Intent ถูกตั้งค่า is_active = 1 |
| Control Panel ไม่โหลด | ตรวจสอบ path และ permissions ของไฟล์ |