Views
No views yet
| 라벨 | 의미 | 예시 발화 |
|---|---|---|
images | 사진·움짤·일러스트 등 시각자료 요구 | 고화질 사진 좀 찾아줘 |
videos | 유튜브·예고편·하이라이트 등 영상 요구 | 하이라이트 영상 틀어줘 |
news | 최신 기사·속보 요구 | 오늘 뉴스 헤드라인 알려줘 |
books | 도서 정보 요구 | 하루키 소설 추천해줘 |
extract | URL/링크의 본문 텍스트 추출 요구 | 이 링크 본문 긁어와 |
text | 실시간 정보 또는 명시적 웹검색 요구 | 지금 환율 얼마인지 검색해줘 |
chat | 일상대화 + LLM 지식으로 답 가능한 질문 | 파이썬 리스트 정렬법 알려줘 |
images, "블랙홀 다큐 틀어줘"→videos,
"블랙홀이 어떻게 생기는지 설명해줘"→chat, "블랙홀 최신 관측 결과 검색해줘"→text.
URL이 포함돼도 본문 추출 요구가 아니면 extract가 아닙니다.1import re
2import torch
3import torch.nn.functional as F
4from transformers import AutoModelForSequenceClassification, AutoTokenizer
5
6_CORE = re.compile(r"[가-힣a-zA-Z0-9]")
7
8
9class IntentRouter:
10 def __init__(self, model_id="MelissaJ/koelectra-search-7-base", device=None):
11 self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
12 self.tokenizer = AutoTokenizer.from_pretrained(model_id)
13 self.model = (
14 AutoModelForSequenceClassification.from_pretrained(model_id)
15 .to(self.device)
16 .eval()
17 )
18 self.id2label = {int(k): v for k, v in self.model.config.id2label.items()}
19 self.route("워밍업") # 첫 호출 지연 제거
20
21 @torch.no_grad()
22 def route(self, text: str) -> dict:
23 # 완성형 한글/영숫자가 없으면 노이즈("ㅋㅋ", "?", "...") → 모델 없이 chat 폴백.
24 # 길이 기준(len < 3 등)으로 바꾸면 "환율" 같은 정상 2글자 질의가 오라우팅됨.
25 if not _CORE.search(text.strip()):
26 return {"label": "chat", "confidence": 1.0, "rule": "noise_guard"}
27 enc = self.tokenizer(
28 text, truncation=True, max_length=64, return_tensors="pt"
29 ).to(self.device)
30 probs = F.softmax(self.model(**enc).logits[0], dim=-1)
31 idx = int(probs.argmax())
32 return {
33 "label": self.id2label[idx],
34 "confidence": round(float(probs[idx]), 4),
35 "rule": "model",
36 }
37
38
39router = IntentRouter()
40router.route("지금 원달러 환율 얼마야")
41# {'label': 'text', 'confidence': 0.986, 'rule': 'model'}⚠️ 위 수치는 학습 데이터와 동일 분포의 합성 데이터 기준입니다. 실제 사용자 발화에 대한 성능은 별도 측정이 필요하며, 이보다 낮을 수 있습니다.
| 디바이스 | 평균 | p95 |
|---|---|---|
| GPU (RTX급) | 7.4ms | 10.4ms |
| CPU | 15.4ms | 17.8ms |
monologg/koelectra-base-v3-discriminatorimages, "커피 추출 원리"→chat, "에어컨 틀어줘"→chat)noise_guard 규칙으로 걸러야 합니다.