Skip to content

Commit d6af8fd

Browse files
committed
refactor: 관세 예측 라우터 리펙토링
1 parent 703ddd7 commit d6af8fd

6 files changed

Lines changed: 398 additions & 565 deletions

File tree

app/service.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
from app.dto.response import Response
22
from core.graphs.runner import run_customs_agent
3+
from langchain_core.messages import HumanMessage, AIMessage
4+
5+
# 세션 맵: {session_id: messages}
6+
session_map = {}
37

48
def run_model(question: str, session_id: str = None) -> "Response":
5-
"""
6-
중앙 관리 모델을 통해 각 요청을 적절한 모델로 라우팅
7-
아무 모델과도 관계없는 경우 중앙 모델에서 적절한 응답을 생성해야 합니다.
8-
ex : "안녕, 너는 뭘 할 수 있어?"
9-
예시 코드는 아래와 같습니다.
10-
"""
11-
state = run_customs_agent(question, session_id=session_id)
9+
# session_id가 없으면 대화내역 없이 동작
10+
if not session_id:
11+
messages = [HumanMessage(content=question)]
12+
else:
13+
# session_map에 없으면 빈 리스트 할당
14+
if session_id not in session_map:
15+
session_map[session_id] = []
16+
messages = session_map[session_id]
17+
messages.append(HumanMessage(content=question))
18+
19+
# agent 실행 (messages를 넘김)
20+
state = run_customs_agent(question, session_id=session_id, messages=messages)
21+
22+
# AI 응답 메시지 추가 (state["final_response"] 기준)
23+
ai_reply = state.get("final_response")
24+
if session_id and ai_reply:
25+
messages.append(AIMessage(content=ai_reply))
26+
session_map[session_id] = messages
27+
1228
return Response(
13-
reply=state.get("final_response"),
29+
reply=ai_reply,
1430
progress_details=state.get("progress_details"),
1531
error_reason=state.get("error_reason"),
1632
success=True,
17-
session_id=state.get("session_id")
33+
session_id=session_id # session_id를 응답에 포함
1834
)

core/graphs/runner.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44
from core.shared.states.states import CustomsAgentState
55

66

7-
def run_customs_agent(query: str, session_id: str = None) -> CustomsAgentState:
7+
def run_customs_agent(query: str, session_id: str = None, messages=None) -> CustomsAgentState:
88
"""관세청 에이전트를 실행합니다."""
99

1010
# 그래프 생성
1111
app = create_customs_graph()
1212

13+
# messages가 없으면 query만 포함
14+
if messages is None:
15+
messages = [HumanMessage(content=query)]
16+
1317
# 초기 상태 설정
1418
initial_state = CustomsAgentState(
15-
messages=[HumanMessage(content=query)],
19+
messages=messages,
1620
query=query,
1721
intent=None,
1822
final_response="",

core/shared/router/intent_router.py

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,6 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
107107
if is_in_tariff_session:
108108
state["intent"] = "tariff_prediction"
109109
_add_classification_message(state, "tariff_prediction", "관세 예측 세션 연속성 유지")
110-
if not state.get("session_id") or state.get("session_id") in [None, '']:
111-
state["session_id"] = str(uuid.uuid4())
112110
return state
113111
# 패턴 기반 분류
114112
is_number_selection = _is_number_selection(current_query)
@@ -117,49 +115,36 @@ def intent_router(state: CustomsAgentState) -> CustomsAgentState:
117115
if is_question:
118116
state["intent"] = "qna"
119117
_add_classification_message(state, "qna", "질문 형태 감지(우선)")
120-
if prev_intent == "tariff_prediction" and state.get("session_id"):
118+
if prev_intent == "tariff_prediction":
121119
try:
122120
from core.tariff_prediction.agent.tariff_prediction_agent import workflow_manager
123121
workflow_manager.cleanup_session(state["session_id"])
124122
except Exception:
125123
pass
126-
state["session_id"] = None
127124
return state
128125
# 키워드 기반 분류
129126
keyword_intent = _classify_by_keywords(current_query)
130127
if keyword_intent:
131128
state["intent"] = keyword_intent
132129
_add_classification_message(state, keyword_intent, "키워드 기반 분류")
133-
if prev_intent == "tariff_prediction" and keyword_intent != "tariff_prediction" and state.get("session_id"):
130+
if prev_intent == "tariff_prediction" and keyword_intent != "tariff_prediction":
134131
try:
135132
from core.tariff_prediction.agent.tariff_prediction_agent import workflow_manager
136133
workflow_manager.cleanup_session(state["session_id"])
137134
except Exception:
138135
pass
139-
if keyword_intent == "tariff_prediction":
140-
if not state.get("session_id") or state.get("session_id") in [None, '']:
141-
state["session_id"] = str(uuid.uuid4())
142-
else:
143-
state["session_id"] = None
144136
return state
145137
if is_number_selection:
146138
state["intent"] = "tariff_prediction"
147139
_add_classification_message(state, "tariff_prediction", "숫자 선택 감지")
148-
if not state.get("session_id") or state.get("session_id") in [None, '']:
149-
state["session_id"] = str(uuid.uuid4())
150140
return state
151141
intent = _classify_with_llm(current_query)
152142
state["intent"] = intent
153143
_add_classification_message(state, intent, "LLM 분류")
154-
if prev_intent == "tariff_prediction" and intent != "tariff_prediction" and state.get("session_id"):
144+
if prev_intent == "tariff_prediction" and intent != "tariff_prediction":
155145
try:
156146
from core.tariff_prediction.agent.tariff_prediction_agent import workflow_manager
157147
workflow_manager.cleanup_session(state["session_id"])
158148
except Exception:
159149
pass
160-
if intent == "tariff_prediction":
161-
if not state.get("session_id") or state.get("session_id") in [None, '']:
162-
state["session_id"] = str(uuid.uuid4())
163-
else:
164-
state["session_id"] = None
165150
return state

0 commit comments

Comments
 (0)