Xây dựng AI Chatbot cho Doanh nghiệp: Kiến trúc và Best Practices
Thiết kế AI chatbot production-grade — intent classification, RAG, conversation memory, escalation flow, và evaluation metrics.
AI chatbot doanh nghiệp cần intent routing, knowledge grounding, conversation memory, và escalation logic — không chỉ là một API call đến LLM.
Kiến trúc
Tin nhắn người dùng → Input Guard → Intent Classifier
↓
FAQ Handler | RAG Handler | Task Handler
↓
Response Generator (LLM)
↓
Output Guard → Escalation Check → Human Agent (nếu confidence < 0.7)
1. Phân loại Intent
INTENT_SYSTEM = """Phân loại tin nhắn vào một trong các intent:
- faq: câu hỏi chung về sản phẩm/dịch vụ
- order_status: tra cứu đơn hàng
- technical_support: vấn đề kỹ thuật
- complaint: khiếu nại
- escalate: yêu cầu gặp nhân viên
Trả về JSON: {"intent": "...", "confidence": 0.0-1.0}"""
def classify_intent(message: str) -> dict:
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": INTENT_SYSTEM},
{"role": "user", "content": message},
],
response_format={"type": "json_object"},
temperature=0,
)
return eval(response.choices[0].message.content)
2. RAG-Grounded Responses
def retrieve_context(query: str, collection: str) -> list[str]:
embedding = get_embedding(query)
results = qdrant.search(
collection_name=collection,
query_vector=embedding,
limit=4,
score_threshold=0.75,
)
return [r.payload["text"] for r in results]
3. Conversation Memory với Redis
def save_message(session_id: str, role: str, content: str) -> None:
msg = json.dumps({"role": role, "content": content})
redis_client.rpush(f"chat:{session_id}", msg)
redis_client.expire(f"chat:{session_id}", 3600) # TTL 1 giờ
4. Escalation Logic
ESCALATION_TRIGGERS = ["gặp nhân viên", "muốn nói chuyện người thật", "bức xúc"]
def should_escalate(message: str, intent: dict, failures: int) -> bool:
if any(t in message.lower() for t in ESCALATION_TRIGGERS):
return True
if intent["confidence"] < 0.5 and failures >= 2:
return True
if intent["intent"] == "complaint":
return True
return False
5. Metrics Đánh giá
| Metric | Mục tiêu | |--------|----------| | Intent accuracy | > 90% | | Tỷ lệ tự giải quyết | > 75% | | CSAT | > 4.0/5 | | Số lượt hội thoại để giải quyết | < 4 | | Tỷ lệ escalate | < 20% |
Kết luận
Ventra Rocket đã xây dựng chatbot xử lý hàng nghìn cuộc hội thoại/ngày, đạt 80%+ tỷ lệ tự giải quyết.
Bài viết liên quan
Kiến trúc RAG cho xử lý tài liệu doanh nghiệp
Hướng dẫn thiết kế hệ thống RAG (Retrieval-Augmented Generation) để xử lý và truy vấn tài liệu nội bộ doanh nghiệp với độ chính xác cao.
Best Practice tích hợp LLM cho doanh nghiệp
Hướng dẫn thực chiến tích hợp Large Language Models vào hệ thống doanh nghiệp — prompt engineering, cost optimization, safety guardrails và evaluation framework.