11from langchain_core .messages import SystemMessage , AIMessage
22import re
33from typing import List , Optional
4- import uuid
54
65from core .shared .states .states import CustomsAgentState
76from core .shared .utils .llm import get_llm
@@ -90,61 +89,57 @@ def _classify_with_llm(query: str) -> str:
9089
9190
9291def _add_classification_message (state : CustomsAgentState , intent : str , reason : str ) -> None :
93- # 의도 분류 메시지를 messages에 남기지 않음
94- pass
92+ """의도 분류 완료 메시지를 추가합니다."""
93+ state ["messages" ].append (
94+ AIMessage (content = f"의도 분류 완료: { intent } ({ reason } )" )
95+ )
9596
9697
9798def intent_router (state : CustomsAgentState ) -> CustomsAgentState :
98- prev_intent = state .get ("intent" )
99+ """사용자 쿼리의 의도를 분류합니다."""
100+
99101 current_query = state ["query" ].strip ()
100102 if not current_query :
101103 state ["intent" ] = DEFAULT_INTENT
102104 _add_classification_message (state , DEFAULT_INTENT , "빈 쿼리" )
103105 return state
106+
104107 # 세션 연속성 확인
105108 is_in_tariff_session = _is_in_tariff_session (state )
109+
106110 # 관세 예측 세션 중이면 무조건 tariff_prediction으로 분류
107111 if is_in_tariff_session :
108112 state ["intent" ] = "tariff_prediction"
109113 _add_classification_message (state , "tariff_prediction" , "관세 예측 세션 연속성 유지" )
110114 return state
115+
111116 # 패턴 기반 분류
112117 is_number_selection = _is_number_selection (current_query )
113118 is_question = _is_question (current_query )
119+
114120 # 질문 형태이면서 관세 예측 세션이 아닌 경우 QnA로 분류
115121 if is_question :
116122 state ["intent" ] = "qna"
117- _add_classification_message (state , "qna" , "질문 형태 감지(우선)" )
118- if prev_intent == "tariff_prediction" :
119- try :
120- from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
121- workflow_manager .cleanup_session ()
122- except Exception :
123- pass
123+ _add_classification_message (state , "qna" , "질문 형태 감지" )
124124 return state
125+
125126 # 키워드 기반 분류
126127 keyword_intent = _classify_by_keywords (current_query )
127128 if keyword_intent :
128129 state ["intent" ] = keyword_intent
129130 _add_classification_message (state , keyword_intent , "키워드 기반 분류" )
130- if prev_intent == "tariff_prediction" and keyword_intent != "tariff_prediction" :
131- try :
132- from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
133- workflow_manager .cleanup_session ()
134- except Exception :
135- pass
136131 return state
132+
133+ # 숫자 선택이지만 관세 예측 세션이 아닌 경우에도 tariff_prediction으로 분류
134+ # (HS 코드 직접 입력 등의 경우)
137135 if is_number_selection :
138136 state ["intent" ] = "tariff_prediction"
139137 _add_classification_message (state , "tariff_prediction" , "숫자 선택 감지" )
140138 return state
139+
140+ # LLM 기반 의도 분류를 수행
141141 intent = _classify_with_llm (current_query )
142142 state ["intent" ] = intent
143143 _add_classification_message (state , intent , "LLM 분류" )
144- if prev_intent == "tariff_prediction" and intent != "tariff_prediction" :
145- try :
146- from core .tariff_prediction .agent .tariff_prediction_agent import workflow_manager
147- workflow_manager .cleanup_session ()
148- except Exception :
149- pass
144+
150145 return state
0 commit comments