234 lines
6.3 KiB
TypeScript
234 lines
6.3 KiB
TypeScript
import { getClassifierMaxTokens } from "../../../config.js";
|
|
import { withClassifierSpan } from "@obs/spans";
|
|
import type { LlmClient } from "@llm/llm-client";
|
|
import {
|
|
detectLightweightIntent,
|
|
isRemainingStatusQuestion,
|
|
shouldSkipMessage,
|
|
} from "../message-filter.js";
|
|
import type { ClassifiedIntent, Intent } from "./onboarding-state.js";
|
|
|
|
interface ClassifierLlmShape {
|
|
intent?: string;
|
|
}
|
|
|
|
function intent(kind: Intent["kind"]): Intent {
|
|
return { kind };
|
|
}
|
|
|
|
function mapTriggerIntent(trigger: string): Intent | null {
|
|
switch (trigger) {
|
|
case "nickname-invalid":
|
|
return intent("nickname-question");
|
|
case "photo-failed":
|
|
return intent("photo-question");
|
|
case "progress-update":
|
|
case "timeout-warning":
|
|
return intent("status-question");
|
|
case "completed":
|
|
return intent("acknowledgement");
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function classifyByRegex(userMessage: string): ClassifiedIntent {
|
|
const trimmed = userMessage.trim();
|
|
const lower = trimmed.toLowerCase();
|
|
|
|
if (!trimmed) {
|
|
return { intent: intent("unknown"), confidence: 0, source: "regex" };
|
|
}
|
|
|
|
if (/^@\S+/u.test(trimmed)) {
|
|
return { intent: intent("off-topic-to-other"), confidence: 0.99, source: "regex" };
|
|
}
|
|
|
|
if (shouldSkipMessage(trimmed)) {
|
|
return { intent: intent("chitchat"), confidence: 0.95, source: "regex" };
|
|
}
|
|
|
|
const lightweightIntent = detectLightweightIntent(trimmed);
|
|
if (lightweightIntent === "greeting") {
|
|
return { intent: intent("greeting"), confidence: 0.99, source: "regex" };
|
|
}
|
|
if (lightweightIntent === "acknowledgement") {
|
|
return {
|
|
intent: intent("acknowledgement"),
|
|
confidence: 0.99,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
if (isRemainingStatusQuestion(trimmed)) {
|
|
return {
|
|
intent: intent("status-question"),
|
|
confidence: 0.96,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
if (/(어디|위치|어딨어|찾겠|찾을)/u.test(trimmed)) {
|
|
return { intent: intent("help-where"), confidence: 0.9, source: "regex" };
|
|
}
|
|
|
|
if (/(어떻게|방법|어케|어케해|어케 함|어케함)/u.test(trimmed)) {
|
|
return { intent: intent("help-how"), confidence: 0.9, source: "regex" };
|
|
}
|
|
|
|
if (/(디스코드|디코|discord)/u.test(lower)) {
|
|
return {
|
|
intent: intent("discord-question"),
|
|
confidence: 0.95,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
if (/(나이|연령|27세|나이\s*제한|몇\s*살|몇살)/u.test(trimmed)) {
|
|
return { intent: intent("age-question"), confidence: 0.93, source: "regex" };
|
|
}
|
|
|
|
if (/(닉네임|닉변|형식|이름)/u.test(trimmed)) {
|
|
return {
|
|
intent: intent("nickname-question"),
|
|
confidence: 0.9,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
if (/(하트|인증|사진|스크린샷|스샷|프로필)/u.test(trimmed)) {
|
|
return {
|
|
intent: intent("photo-question"),
|
|
confidence: 0.88,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
if (
|
|
/(가입|등록|절차|도움|도와|뭐해야|뭐 해야|해야 돼|해야해|무엇을 해야)/u.test(trimmed)
|
|
) {
|
|
return { intent: intent("help-how"), confidence: 0.72, source: "regex" };
|
|
}
|
|
|
|
return { intent: intent("unknown"), confidence: 0, source: "regex" };
|
|
}
|
|
|
|
function parseIntent(raw: unknown): Intent {
|
|
if (
|
|
raw === "greeting" ||
|
|
raw === "acknowledgement" ||
|
|
raw === "status-question" ||
|
|
raw === "help-how" ||
|
|
raw === "help-where" ||
|
|
raw === "nickname-question" ||
|
|
raw === "photo-question" ||
|
|
raw === "discord-question" ||
|
|
raw === "age-question" ||
|
|
raw === "chitchat" ||
|
|
raw === "off-topic-to-other"
|
|
) {
|
|
return { kind: raw };
|
|
}
|
|
|
|
return { kind: "unknown" };
|
|
}
|
|
|
|
async function classifyByLlm(
|
|
llmClient: LlmClient,
|
|
trigger: string,
|
|
userMessage: string,
|
|
): Promise<ClassifiedIntent> {
|
|
if (!llmClient.hasEndpoints()) {
|
|
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
|
}
|
|
|
|
const response = await llmClient.chatCompletion(
|
|
[
|
|
{
|
|
role: "system",
|
|
content:
|
|
'다음 유저 메시지의 intent를 JSON으로만 분류해. {"intent":"..."} 형식만 출력하고, intent는 greeting, acknowledgement, status-question, help-how, help-where, nickname-question, photo-question, discord-question, age-question, chitchat, off-topic-to-other, unknown 중 하나만 사용해.',
|
|
},
|
|
{
|
|
role: "user",
|
|
content: `trigger=${trigger}\nmessage=${userMessage}`,
|
|
},
|
|
],
|
|
{
|
|
jsonMode: true,
|
|
maxTokens: getClassifierMaxTokens(),
|
|
temperature: 0,
|
|
},
|
|
);
|
|
|
|
if (!response) {
|
|
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
|
}
|
|
|
|
try {
|
|
const parsed = JSON.parse(response.content) as ClassifierLlmShape;
|
|
return {
|
|
intent: parseIntent(parsed.intent),
|
|
confidence: parsed.intent ? 0.75 : 0,
|
|
source: "llm",
|
|
};
|
|
} catch {
|
|
return { intent: intent("unknown"), confidence: 0, source: "llm" };
|
|
}
|
|
}
|
|
|
|
export class OnboardingIntentClassifier {
|
|
constructor(private readonly llmClient: LlmClient) {}
|
|
|
|
async classify(
|
|
userMessage: string | undefined,
|
|
trigger: string,
|
|
): Promise<ClassifiedIntent> {
|
|
return withClassifierSpan(
|
|
{
|
|
"agent.trigger": trigger,
|
|
},
|
|
async (span) => {
|
|
const triggerIntent = mapTriggerIntent(trigger);
|
|
if (triggerIntent) {
|
|
span.setAttributes({
|
|
"agent.classifier.confidence": 1,
|
|
"agent.classifier.source": "regex",
|
|
"agent.intent": triggerIntent.kind,
|
|
});
|
|
return {
|
|
intent: triggerIntent,
|
|
confidence: 1,
|
|
source: "regex",
|
|
};
|
|
}
|
|
|
|
const regexResult = classifyByRegex(userMessage ?? "");
|
|
if (regexResult.confidence >= 0.75) {
|
|
span.setAttributes({
|
|
"agent.classifier.confidence": regexResult.confidence,
|
|
"agent.classifier.source": regexResult.source,
|
|
"agent.intent": regexResult.intent.kind,
|
|
});
|
|
return regexResult;
|
|
}
|
|
|
|
const llmResult = await classifyByLlm(
|
|
this.llmClient,
|
|
trigger,
|
|
userMessage ?? "",
|
|
);
|
|
|
|
const result =
|
|
llmResult.intent.kind === "unknown" ? regexResult : llmResult;
|
|
span.setAttributes({
|
|
"agent.classifier.confidence": result.confidence,
|
|
"agent.classifier.source": result.source,
|
|
"agent.intent": result.intent.kind,
|
|
});
|
|
return result;
|
|
},
|
|
);
|
|
}
|
|
}
|