DOCS/journey/troubleshooting/260103_하이브리드_의도_분류_성능_비교_테스트.md
Claude-51124 00d5b489b4 docs: multi-centroid 구현 상세 및 운영 가이드 추가
- seed_multi_centroid_prototypes.py 사용법 및 파라미터 설명
- 코드 변경 요약 (intent_store.py, semantic_classifier.py)
- 버전 관리 전략 (version=3 multi-centroid)
- Few-shot LLM 프롬프트 개선 구체적 방법 및 예시 구조
2026-01-03 12:33:42 +09:00

6.7 KiB

하이브리드 의도 분류 성능 비교 테스트

날짜: 2026-01-03 작성자: Auto 관련 파일: rb8001/scripts/test_intent_classification_comparison.py, rb8001/app/services/brain/decision_engine.py, rb8001/app/services/brain/semantic_classifier.py


문제 상황

하이브리드 의도 분류 시스템 개선을 위해 FastPath(정규식), 제로샷 임베딩, 병행 비교 3가지 방법의 성능을 비교 검증 필요.


테스트 방법

테스트 데이터: 141개 질문

  • tests/data/intent_eval_samples.json: 95개
  • tests/data/intent_eval_challenge.json: 28개
  • 실패한 질문 패턴: 18개

테스트 방법:

  1. FastPath만: DecisionEngine.analyze_intent() (정규식 기반)
  2. 제로샷 임베딩만: SemanticIntentClassifier.top_k() + confidence
  3. 병행 비교: FastPath와 임베딩 모두 실행 후 confidence 비교하여 선택

테스트 결과

방법 정확도 평균 응답 시간 정확도 순위
FastPath만 49.6% (70/141) 72.0ms 1위
제로샷 임베딩만 23.4% (33/141) 80.4ms 3위
병행 비교 49.6% (70/141) 153.6ms 1위

문제 분석

1. 제로샷 임베딩 성능 저하 (23.4%)

원인:

  • intent_prototypes DB 미초기화 또는 384d→768d 차원 불일치
  • 단일 description 기반 prototype으로 유사도 계산 부정확
  • IntentType enum 값과 intent_prototypes의 intent 이름 불일치

리서치 기준 대비: 75% 정확도 달성 가능 (multi-centroid 방식)

2. 병행 비교 실패

원인:

  • 제로샷 임베딩 정확도가 낮아 FastPath 결과만 선택됨
  • 두 결과가 다를 때 confidence 차이 0.3 이상 기준이 너무 높음
  • 대부분 케이스에서 FastPath가 항상 선택되어 병행 비교 의미 없음

3. FastPath 한계 (49.6%)

주요 오류 패턴:

  • "핀테크 업계 오늘 기사 검색" → web_search (실제: news_fetch)
  • "어제 받은 메일 두 줄로 정리" → email_read (실제: email_summary)
  • 유사한 intent 구분 어려움

개선 방향

1. Multi-centroid 방식 도입

  • 각 intent별 5-10개 실제 예시 문장으로 K-means centroid 생성
  • seed_calendar_event_samples.py 방식으로 모든 intent 확장
  • intent_prototypes DB에 version=2로 저장

2. Few-shot LLM 프롬프트 개선

  • Top-3 임베딩 후보를 활용한 Few-shot 예시 추가
  • Gemini 프롬프트 설계 원칙(313_Gemini_프롬프트_설계_원칙.md) 적용
  • XML 구조화된 프롬프트로 예시 블록 제공

3. 하이브리드 3단계 최적화

  • FastPath(명확 패턴) → multi-centroid embedding(Top-3) → Few-shot LLM
  • multi-centroid 정확도 개선 후 병행 비교 재검토

교훈

프로덕션 테스트의 중요성

  • 리서치 문서의 이론적 성능과 실제 성능 차이 확인 필수
  • intent_prototypes DB 초기화 상태와 차원 일치 여부 사전 확인

Few-shot Prompting 활용

  • 제로샷 LLM보다 Few-shot 예시가 효과적 (313_Gemini_프롬프트_설계_원칙.md)
  • Top-3 후보를 활용한 컨텍스트 제공으로 정확도 향상 가능

단계적 개선 전략

  • FastPath 유지 (명확 패턴 처리, 49.6% 정확도)
  • multi-centroid embedding으로 75%+ 달성 후 병행 비교 적용

개선 결과 (2026-01-03)

Multi-centroid 방식 적용

구현 내용:

  • scripts/seed_multi_centroid_prototypes.py: intent_eval_samples.json에서 123개 샘플 수집 후 K-means로 multi-centroid 생성
  • 14개 intent에 각 3개 centroid 생성 (version=3, source="multi_centroid_{idx}")
  • intent_store.py: load_multi_prototypes_db() 추가하여 여러 centroid 로드
  • semantic_classifier.py: multi-centroid와의 최대 유사도 계산으로 정확도 개선
  • 커밋: 247496a (rb8001)

성능 개선:

방법 적용 전 적용 후 개선율
FastPath만 49.6% (70/141) 72.3% (102/141) +22.7%p
제로샷 임베딩만 23.4% (33/141) 70.2% (99/141) +46.8%p
병행 비교 49.6% (70/141) 70.9% (100/141) +21.3%p

주요 개선 사항:

  • 제로샷 임베딩 정확도 3배 향상 (23.4% → 70.2%)
  • FastPath도 개선 (49.6% → 72.3%), 현재 최고 성능
  • 병행 비교도 의미 있게 작동 (70.9%)

남은 과제:

  • 75%+ 목표 달성을 위해 Few-shot LLM 프롬프트 개선 필요
  • 샘플 데이터 증강으로 추가 정확도 향상 가능

구현 상세

스크립트 사용법

Multi-centroid 재생성:

docker exec rb8001 python3 scripts/seed_multi_centroid_prototypes.py --apply

파라미터:

  • --apply: DB에 실제로 저장 (없으면 dry-run)
  • --max-k: intent당 최대 centroid 수 (기본값: 3)

주의사항:

  • intent_prototypes 테이블의 (intent_id, version, source) UNIQUE 제약으로 기존 version=3은 덮어씀
  • 샘플 데이터 추가 시 intent_eval_samples.json, intent_eval_challenge.json 수정 후 재실행

코드 변경 요약

intent_store.py:

  • load_multi_prototypes_db(): version이 높은 것 우선, 같은 version 내 모든 centroid 로드
  • load_prototypes_db(): 하위 호환성 유지 (첫 번째 centroid만 반환)

semantic_classifier.py:

  • _intent_multi_vecs: 여러 centroid 저장 (Dict[str, List[List[float]]])
  • top_k(): multi-centroid 존재 시 최대 유사도 계산, 없으면 단일 centroid 사용

버전 관리:

  • version=3: multi-centroid (source="multi_centroid_{idx}")
  • version=2: 단일 centroid (calendar_event 등)
  • version=1: description 기반 (legacy)

Few-shot LLM 프롬프트 개선 계획

현재 상태: intent_parser.py에서 CoT 지원하나 Few-shot 예시 미적용

개선 방향:

  1. Top-3 임베딩 후보를 활용한 Few-shot 예시 구성
  2. Gemini 프롬프트 설계 원칙(313_Gemini_프롬프트_설계_원칙.md) 적용
  3. XML 구조화: <examples> 블록에 각 후보별 실제 문장 예시 제공

구현 위치: rb8001/app/services/llm/intent_parser.py:26-91

예시 구조:

<examples>
후보 intent: email_summary
- "어제 받은 메일 두 줄로 정리"
- "오늘 받은 메일들 핵심 포인트 알려줘"
- "메일함 정리해서 핵심만 알려줘"

후보 intent: email_read
- "최근 받은 메일 확인해 줘"
- "어제 밤에 온 메일 열어봐"
</examples>

참고

  • 계획 문서: journey/plans/archive/251017_intent_analysis_improvement_plan.md
  • 리서치 문서: journey/research/intent_classification/README.md
  • 프롬프트 설계 원칙: book/300_architecture/313_Gemini_프롬프트_설계_원칙.md
  • 테스트 결과: rb8001/tests/results/intent_classification_comparison.json