66from core .shared .constants import (
77 TARIFF_SESSION_KEYWORDS ,
88 NUMBER_SELECTION_PATTERNS ,
9- TARIFF_PREDICTION_KEYWORDS ,
10- CUSTOMS_TRACKING_KEYWORDS ,
119 INTENT_CLASSIFICATION_PROMPT ,
1210 INTENT_TYPES ,
1311 DEFAULT_INTENT
1614def intent_router (state : CustomsAgentState ) -> CustomsAgentState :
1715 """사용자 쿼리의 의도를 분류합니다."""
1816
17+ # 현재 쿼리가 숫자 선택인지 확인
18+ current_query = state ["query" ].strip ()
19+ is_number_selection = False
20+
21+ # 숫자 선택 패턴 확인 (1번, 2번, 3번, 1, 2, 3 등)
22+ for pattern in NUMBER_SELECTION_PATTERNS :
23+ if re .match (pattern , current_query ):
24+ is_number_selection = True
25+ break
26+
1927 # 이전 대화에서 관세 예측 중인지 확인
2028 messages = state .get ("messages" , [])
2129 is_in_tariff_session = False
2230
23- # 최근 메시지들을 확인하여 관세 예측 세션 중인지 판단
31+ # 1. state의 intent 필드 확인 (가장 중요)
32+ if state .get ("intent" ) == "tariff_prediction" :
33+ is_in_tariff_session = True
34+
35+ # 2. 최근 메시지들을 확인하여 관세 예측 세션 중인지 판단
2436 for msg in messages [- 5 :]: # 최근 5개 메시지 확인
2537 if hasattr (msg , 'content' ) and isinstance (msg .content , str ):
2638 content = msg .content .lower ()
@@ -29,7 +41,7 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
2941 is_in_tariff_session = True
3042 break
3143
32- # 이전 의도가 tariff_prediction이었는지도 확인
44+ # 3. 이전 의도가 tariff_prediction이었는지도 확인
3345 if messages and len (messages ) > 0 :
3446 last_msg = messages [- 1 ]
3547 if hasattr (last_msg , 'content' ) and isinstance (last_msg .content , str ):
@@ -43,23 +55,6 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
4355 print (state )
4456 return state
4557
46- # 현재 쿼리가 숫자 선택인지 확인
47- current_query = state ["query" ].strip ()
48- is_number_selection = False
49-
50- # 숫자 선택 패턴 확인 (1번, 2번, 3번, 1, 2, 3 등)
51- for pattern in NUMBER_SELECTION_PATTERNS :
52- if re .match (pattern , current_query ):
53- is_number_selection = True
54- break
55-
56- # 관세 예측 세션 중이고 숫자 선택이면 무조건 tariff_prediction
57- if is_in_tariff_session and is_number_selection :
58- state ["intent" ] = "tariff_prediction" # type: ignore
59- state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { state ['intent' ]} (세션 연속성 유지)" ))
60- print (state )
61- return state
62-
6358 # 숫자 선택이지만 관세 예측 세션이 아닌 경우에도 tariff_prediction으로 분류
6459 # (HS 코드 직접 입력 등의 경우)
6560 if is_number_selection :
@@ -68,22 +63,7 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
6863 print (state )
6964 return state
7065
71- # 관세 예측 관련 키워드가 있으면 무조건 tariff_prediction으로 분류
72- current_query_lower = current_query .lower ()
73- if any (keyword in current_query_lower for keyword in TARIFF_PREDICTION_KEYWORDS ):
74- state ["intent" ] = "tariff_prediction" # type: ignore
75- state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { state ['intent' ]} (관세 예측 키워드 감지)" ))
76- print (state )
77- return state
78-
79- # 운송장/배송 관련 키워드가 있으면 customs_tracking으로 분류 (배송은 제외)
80- if any (keyword in current_query_lower for keyword in CUSTOMS_TRACKING_KEYWORDS ):
81- state ["intent" ] = "customs_tracking" # type: ignore
82- state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { state ['intent' ]} (배송 추적 키워드 감지)" ))
83- print (state )
84- return state
85-
86- # 일반적인 의도 분류 (LLM 사용)
66+ # LLM 기반 의도 분류를 수행
8767 llm = get_llm ()
8868
8969 try :
@@ -99,6 +79,6 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
9979 intent = DEFAULT_INTENT # 오류 시에도 tariff_prediction으로 분류
10080
10181 state ["intent" ] = intent # type: ignore
102- state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { intent } " ))
82+ state ["messages" ].append (AIMessage (content = f"의도 분류 완료: { intent } (LLM 분류) " ))
10383 print (state )
10484 return state
0 commit comments