diff --git a/backend/app.py b/backend/app.py index d35ee27..a325dc6 100644 --- a/backend/app.py +++ b/backend/app.py @@ -2691,7 +2691,9 @@ ANIMAL_CHAT_SYSTEM_PROMPT = """당신은 약국의 동물약 전문 상담사입 **응답 형식:** - 짧고 명확하게 (3-5문장) -- 추천 제품이 있으면 이름과 특징 언급 +- ⚠️ 체중별 제품은 반드시 **정확한 전체 이름** 사용: + - ❌ "안텔민" (X) → ✅ "안텔민킹(5kg이상)" 또는 "안텔민뽀삐(5kg이하)" + - ❌ "하트세이버" (X) → ✅ "하트세이버츄어블M(12~22kg)" - 체중/종류 확인 필요시 질문 {available_products} @@ -2882,9 +2884,14 @@ def api_animal_chat(): # 응답에서 언급된 보유 제품 찾기 (부분 매칭) mentioned_products = [] + # 공백 제거한 버전도 준비 (AI가 띄어쓰기 넣을 수 있음) ai_response_lower = ai_response.lower() + ai_response_nospace = ai_response_lower.replace(' ', '') - for drug in animal_drugs: + # 제품명 길이순 정렬 (긴 이름 우선 매칭 - "안텔민킹"이 "안텔민"보다 먼저) + sorted_drugs = sorted(animal_drugs, key=lambda x: len(x['name']), reverse=True) + + for drug in sorted_drugs: drug_name = drug['name'] # 제품명에서 핵심 키워드 추출 (괄호 앞부분) # 예: "다이로하트정M(12~22kg)" → "다이로하트" @@ -2896,10 +2903,31 @@ def api_animal_chat(): base_name = base_name.strip() # 핵심 키워드가 AI 응답에 포함되어 있는지 확인 - if len(base_name) >= 2 and base_name.lower() in ai_response_lower: - # 중복 방지 (같은 제품 계열은 하나만) - already_added = any(base_name.lower() in p['name'].lower() for p in mentioned_products) - if not already_added: + # 공백 있는 버전과 없는 버전 모두 체크 + base_lower = base_name.lower() + base_nospace = base_lower.replace(' ', '') + + # 기본 매칭 + matched = (len(base_name) >= 2 and + (base_lower in ai_response_lower or base_nospace in ai_response_nospace)) + + # 추가: 상위 키워드 매칭 (예: "안텔민" 언급 시 "안텔민킹", "안텔민뽀삐"도 매칭) + # APC 있는 제품 우선 + if not matched and drug.get('apc'): + # 제품명의 핵심 부분 추출 (예: "안텔민킹" → "안텔민") + core_name = base_nospace.rstrip('킹뽀삐') + if len(core_name) >= 2 and core_name in ai_response_nospace: + matched = True + + if matched: + # 중복 방지: 이미 추가된 더 specific한 제품이 있으면 건너뜀 + # 예: "안텔민킹" 추가됨 → "안텔민" 건너뜀 + already_covered = any( + base_nospace in p['name'].lower().replace(' ', '') or + p['name'].lower().replace(' ', '').startswith(base_nospace) + for p in mentioned_products + ) + if not already_covered: mentioned_products.append({ 'name': drug_name, 'price': drug['price'],