55
66def intent_router (state : CustomsAgentState ) -> CustomsAgentState :
77 """사용자 쿼리의 의도를 분류합니다."""
8+
9+ # 이전 대화에서 관세 예측 중인지 확인
10+ messages = state .get ("messages" , [])
11+ is_in_tariff_session = False
12+
13+ # 최근 메시지들을 확인하여 관세 예측 세션 중인지 판단
14+ for msg in messages [- 5 :]: # 최근 5개 메시지 확인
15+ if hasattr (msg , 'content' ) and isinstance (msg .content , str ):
16+ content = msg .content .lower ()
17+ # 관세 예측 관련 키워드가 있거나 HS 코드 선택 메시지가 있으면 관세 예측 세션으로 판단
18+ if any (keyword in content for keyword in [
19+ 'hs6 코드 후보' , 'hs10 코드 후보' , '번호를 선택' , '관세 계산' , '관세 예측' ,
20+ '상품묘사' , '구매 국가' , '상품 가격' , '시나리오 선택' , 'tariff_prediction'
21+ ]):
22+ is_in_tariff_session = True
23+ break
24+
25+ # 이전 의도가 tariff_prediction이었는지도 확인
26+ if messages and len (messages ) > 0 :
27+ last_msg = messages [- 1 ]
28+ if hasattr (last_msg , 'content' ) and isinstance (last_msg .content , str ):
29+ if 'tariff_prediction' in last_msg .content :
30+ is_in_tariff_session = True
31+
32+ # 현재 쿼리가 숫자 선택인지 확인
33+ current_query = state ["query" ].strip ()
34+ is_number_selection = False
35+
36+ # 숫자 선택 패턴 확인 (1번, 2번, 3번, 1, 2, 3 등)
37+ import re
38+ number_patterns = [
39+ r'^\d+번?$' , # 1번, 2번, 3번
40+ r'^\d+$' , # 1, 2, 3
41+ r'^\d+\.?\d*$' , # 8471.60, 8517.70 등 HS 코드
42+ r'^\d{4,6}$' , # 8471, 851770 등 HS 코드
43+ r'^\d{4}\.\d{2}$' , # 8471.60 등 HS 코드
44+ ]
45+
46+ for pattern in number_patterns :
47+ if re .match (pattern , current_query ):
48+ is_number_selection = True
49+ break
50+
51+ # 관세 예측 세션 중이고 숫자 선택이면 무조건 tariff_prediction
52+ if is_in_tariff_session and is_number_selection :
53+ state ["intent" ] = "tariff_prediction" # type: ignore
54+ state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { state ['intent' ]} (세션 연속성 유지)" ))
55+ print (state )
56+ return state
57+
58+ # 숫자 선택이지만 관세 예측 세션이 아닌 경우에도 tariff_prediction으로 분류
59+ # (HS 코드 직접 입력 등의 경우)
60+ if is_number_selection :
61+ state ["intent" ] = "tariff_prediction" # type: ignore
62+ state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { state ['intent' ]} (숫자 선택 감지)" ))
63+ print (state )
64+ return state
65+
66+ # 일반적인 의도 분류
867 llm = get_llm ()
968
1069 classification_prompt = """
@@ -40,8 +99,8 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
4099 intent = str (result .content ).strip ()
41100 if intent not in ["customs_tracking" , "tariff_prediction" , "qna" ]:
42101 intent = "qna" # 기본값
43- # 타입 힌트에 맞게 Literal로 제한
102+
44103 state ["intent" ] = intent # type: ignore
45104 state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { intent } " ))
46- print (state ) # type: ignore
105+ print (state )
47106 return state
0 commit comments