Skip to content

Intent classifier (greeting / thanks / meta)

Source: platform/apps/app/src/lib/chat/intent.ts · rendered from main on every deploy — edit in the repo, not here

platform/apps/app/src/lib/chat/intent.ts
/**
* Lightweight intent classifier for conversational queries.
* Detects greetings, thanks, meta-questions, and small talk
* that should NOT go through the RAG pipeline.
*/
export type Intent = 'greeting' | 'thanks' | 'meta' | 'farewell' | 'knowledge';
const GREETING_PATTERNS = [
// Thai
/^(สวัสดี|หวัดดี|ดีครับ|ดีค่ะ|ดีจ้า|ฮัลโหล|หวัดดีครับ|หวัดดีค่ะ|hello|halo|hi|hey|yo)/i,
/^(ว่าไง|เป็นไงบ้าง|เป็นยังไง|สบายดี)/i,
];
const THANKS_PATTERNS = [
/^(ขอบคุณ|ขอบใจ|ขอบคุณครับ|ขอบคุณค่ะ|thanks|thank you|thx)/i,
/^(เข้าใจแล้ว|โอเค|ok|ได้เลย|รับทราบ)/i,
];
const META_PATTERNS = [
// "Who are you" / "What can you do"
/คุณคือ(ใคร|อะไร)/,
/คุณเป็น(ใคร|อะไร)/,
/คุณ(ทำ|ช่วย)อะไรได้/,
/คุณ(ชื่อ|เรียก)อะไร/,
/who are you/i,
/what (can you do|are you)/i,
/what('s| is) your name/i,
/ปุจฉา(คือ|เป็น)/,
/ช่วยอะไรได้บ้าง/,
];
const FAREWELL_PATTERNS = [/^(ลาก่อน|บ๊ายบาย|bye|goodbye|ไว้คุยกัน|แล้วเจอกัน)/i];
export function classifyIntent(query: string): Intent {
const q = query.trim();
for (const p of GREETING_PATTERNS) {
if (p.test(q)) return 'greeting';
}
for (const p of THANKS_PATTERNS) {
if (p.test(q)) return 'thanks';
}
for (const p of META_PATTERNS) {
if (p.test(q)) return 'meta';
}
for (const p of FAREWELL_PATTERNS) {
if (p.test(q)) return 'farewell';
}
return 'knowledge';
}
/** Quick responses for non-knowledge intents (no LLM needed). */
export function getQuickResponse(
intent: Intent,
locale: string,
tenantName?: string,
): string | null {
const name = tenantName || 'Puccha';
if (locale === 'th') {
switch (intent) {
case 'greeting':
return `สวัสดีค่ะ! 😊 ยินดีต้อนรับสู่ ${name} Assistant ดิฉันพร้อมช่วยตอบคำถามเกี่ยวกับ ${name} ค่ะ มีอะไรให้ช่วยไหมคะ?`;
case 'thanks':
return `ยินดีค่ะ! 😊 หากมีคำถามเพิ่มเติม สามารถถามได้เลยนะคะ`;
case 'meta':
return `ดิฉันคือ ${name} Assistant ผู้ช่วยตอบคำถามอัจฉริยะค่ะ สามารถช่วยตอบคำถามเกี่ยวกับบริการ สินค้า และข้อมูลต่างๆ ของ ${name} ได้ค่ะ ลองถามคำถามได้เลยนะคะ!`;
case 'farewell':
return `ลาก่อนค่ะ! 👋 หากต้องการความช่วยเหลือเพิ่มเติม กลับมาได้ทุกเมื่อนะคะ`;
}
} else {
switch (intent) {
case 'greeting':
return `Hello! 😊 Welcome to ${name} Assistant. I'm here to help answer questions about ${name}. How can I help you?`;
case 'thanks':
return `You're welcome! 😊 Feel free to ask if you have more questions.`;
case 'meta':
return `I'm ${name} Assistant, an AI-powered knowledge helper. I can answer questions about ${name}'s services, products, and more. Try asking me a question!`;
case 'farewell':
return `Goodbye! 👋 Come back anytime if you need help.`;
}
}
return null;
}