## 철학 수정 (실측 기반)
gemma-4-26b-a4b-it-8bit MLX 실측:
- full query_analyze.txt (prompt_tok=2406) → 10.5초
- max_tokens 축소 무효 (모델 자연 EOS 조기 종료)
- 쿼리 길이 영향 거의 없음 (프롬프트 자체가 지배)
→ 800ms timeout 가정은 13배 초과. 동기 호출 완전히 불가능.
따라서 QueryAnalyzer는 "즉시 실행하는 기능" → "미리 준비해두는 기능"으로
포지셔닝 변경. retrieval 경로에서 analyzer 동기 호출 **금지**.
## 구조
```
query → retrieval (항상 즉시)
↘ trigger_background_analysis (fire-and-forget)
→ analyze() [5초+] → cache 저장
다음 호출 (동일 쿼리) → get_cached() 히트 → Phase 2 파이프라인 활성화
```
## 변경 사항
### app/prompts/query_analyze.txt
- 5971 chars → 2403 chars (40%)
- 예시 4개 → 1개, 규칙 설명 축약
- 목표 prompt_tok 2406 → ~600 (1/4)
### app/services/search/query_analyzer.py
- LLM_TIMEOUT_MS 800 → 5000 (background이므로 여유 OK)
- PROMPT_VERSION v1 → v2 (cache auto-invalidate)
- get_cached / set_cached 유지 — retrieval 경로 O(1) 조회
- trigger_background_analysis(query) 신규 — 동기 함수, 즉시 반환, task 생성
- _PENDING set으로 task 참조 유지 (premature GC 방지)
- _INFLIGHT set으로 동일 쿼리 중복 실행 방지
- prewarm_analyzer() 신규 — startup에서 15~20 쿼리 미리 분석
- DEFAULT_PREWARM_QUERIES: 평가셋 fixed 7 + 법령 3 + 뉴스 2 + 실무 3
### app/api/search.py
- 기존 sync analyzer 호출 완전 제거
- analyze=True → get_cached(q) 조회만 O(1)
- hit: query_analysis 활용 (Phase 2.2/2.3 파이프라인 조건부 활성화)
- miss: trigger_background_analysis(q) + 기존 경로 그대로
- timing["analyze_ms"] 제거 (경로에 LLM 호출 없음)
- notes에 analyzer cache_hit/cache_miss 상태 기록
- debug.query_analysis는 cache hit 시에만 채워짐
### app/main.py
- lifespan startup에 prewarm_analyzer() background task 추가
- 논블로킹 — 앱 시작 막지 않음
- delay_between=0.5로 MLX 부하 완화
## 기대 효과
- cold 요청 latency: 기존 Phase 1.3 그대로 (회귀 0)
- warm 요청 + prewarmed: cache hit → query_analysis 활용
- 예상 cache hit rate: 초기 70~80% (prewarm) + 사용 누적
- Phase 2.2/2.3 multilingual/filter 기능은 cache hit 시에만 동작
## 참조
- memory: feedback_analyzer_async_only.md (영구 룰 저장)
- plan: ~/.claude/plans/zesty-painting-kahan.md ("철학 수정" 섹션)
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
54 lines
2.3 KiB
Plaintext
54 lines
2.3 KiB
Plaintext
You are a search query analyzer. Respond ONLY in JSON. No markdown, no explanation.
|
|
|
|
## Output Schema
|
|
{
|
|
"intent": "fact_lookup | semantic_search | filter_browse",
|
|
"query_type": "natural_language | keyword | phrase",
|
|
"domain_hint": "document | news | mixed",
|
|
"language_scope": "limited | global",
|
|
"keywords": [],
|
|
"must_terms": [],
|
|
"optional_terms": [],
|
|
"hard_filters": {},
|
|
"soft_filters": {"domain": [], "document_type": []},
|
|
"normalized_queries": [{"lang": "ko", "text": "...", "weight": 1.0}],
|
|
"expanded_terms": [],
|
|
"synonyms": {},
|
|
"analyzer_confidence": 0.0
|
|
}
|
|
|
|
## Rules
|
|
- `intent`: fact_lookup (사실/조항/이름), semantic_search (주제/개념), filter_browse (필터 중심)
|
|
- `query_type`: natural_language (문장형), keyword (단어 나열), phrase (따옴표/고유명사/법조항)
|
|
- `domain_hint`: document (소유 문서/법령/매뉴얼), news (시사/뉴스), mixed (불명)
|
|
- `language_scope`: limited (ko+en), global (다국어 필요)
|
|
- `hard_filters`: 쿼리에 **명시된** 것만. 추론 금지. 키: file_format, year, country
|
|
- `soft_filters.domain`: Industrial_Safety, Programming, Engineering, Philosophy, Language, General. 2-level 허용(e.g. Industrial_Safety/Legislation)
|
|
- `soft_filters.document_type`: Law_Document, Manual, Report, Academic_Paper, Standard, Specification, Meeting_Minutes, Checklist, Note, Memo, Reference, Drawing, Template
|
|
- `normalized_queries`: 원문 언어 1.0 가중치 필수. 교차언어 1개 추가 권장(ko↔en, weight 0.8). news + global 인 경우만 ja/zh 추가(weight 0.5~0.6). **최대 3개**.
|
|
- `analyzer_confidence`: 0.9+ 명확, 0.7~0.9 대체로 명확, 0.5~0.7 모호, <0.5 분석 불가
|
|
|
|
## Example
|
|
query: `기계 사고 관련 법령`
|
|
{
|
|
"intent": "semantic_search",
|
|
"query_type": "natural_language",
|
|
"domain_hint": "document",
|
|
"language_scope": "limited",
|
|
"keywords": ["기계", "사고", "법령"],
|
|
"must_terms": [],
|
|
"optional_terms": ["안전", "규정"],
|
|
"hard_filters": {},
|
|
"soft_filters": {"domain": ["Industrial_Safety/Legislation"], "document_type": ["Law_Document"]},
|
|
"normalized_queries": [
|
|
{"lang": "ko", "text": "기계 사고 관련 법령", "weight": 1.0},
|
|
{"lang": "en", "text": "machinery accident related laws", "weight": 0.8}
|
|
],
|
|
"expanded_terms": ["산업안전", "기계안전"],
|
|
"synonyms": {},
|
|
"analyzer_confidence": 0.88
|
|
}
|
|
|
|
## Query
|
|
{query}
|