-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
185 lines (144 loc) · 7.63 KB
/
main.py
File metadata and controls
185 lines (144 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# main.py - 메인 애플리케이션 파일
import time
from modules.config import LLM, DATABASE
from modules.chat_history import ChatHistoryManager
from modules.intent_processor import IntentProcessor
from modules.path_finder import PathFinder
from modules.place_searcher import PlaceSearcher
from modules.bus_matcher import BusRouteManager
from modules.rag_chain import RAGChainManager
class ChatbotApp:
"""챗봇 애플리케이션의 메인 클래스"""
def __init__(self):
self.session_id = "abc123"
self.last_interaction_time = time.time()
self.history_manager = ChatHistoryManager()
self.rag_manager = None
self.intent_processor = None
self.path_finder = None
self.place_searcher = None
self.bus_route_manager = None
def initialize_modules(self):
"""모든 필요한 모듈을 초기화합니다."""
try:
# 모듈 초기화
self.intent_processor = IntentProcessor(LLM)
self.path_finder = PathFinder()
self.place_searcher = PlaceSearcher()
self.bus_route_manager = BusRouteManager(self.path_finder)
self.rag_manager = RAGChainManager(LLM, DATABASE, self.history_manager.get_session_history)
print("✅ 모든 모듈이 성공적으로 초기화되었습니다.")
return True
except Exception as e:
print(f"❌ 모듈 초기화 중 오류 발생: {str(e)}")
return False
def reset_if_idle(self, timeout=60):
"""일정 시간 동안 상호작용이 없으면 세션을 초기화합니다."""
if time.time() - self.last_interaction_time > timeout:
self.history_manager.reset_session(self.session_id)
self.last_interaction_time = time.time()
def process_user_input(self, user_input):
"""사용자 입력을 처리하고 적절한 응답을 반환합니다."""
self.last_interaction_time = time.time()
# 특수 명령어 처리
if user_input.lower() == "e":
return "exit"
elif user_input.lower() == "r":
self.history_manager.reset_session(self.session_id)
return "reset"
try:
# 의도 및 목적지 추출
intent, destination = self.intent_processor.detect_intent_and_extract_destination(user_input)
print(f"🎯 감지된 의도: {intent if intent else '알 수 없음'}")
print(f"📍 추출된 목적지: {destination if destination else '없음'}")
# 의도에 따른 처리
if intent is not None:
return self.process_intent(intent, destination)
else:
# RAG 체인을 통한 일반 응답
return self.get_rag_response(user_input)
except Exception as e:
error_msg = f"처리 중 오류가 발생했습니다: {str(e)}"
print(f"❌ {error_msg}")
return error_msg
def process_intent(self, intent, destination):
"""감지된 의도에 따라 적절한 기능을 실행합니다."""
if intent == "위치 찾기" and destination:
return self.process_location_search(destination)
elif intent == "길찾기" and destination:
return self.process_path_finding(destination)
elif intent == "버스 노선" and destination:
return self.process_bus_route(destination)
return "의도를 처리할 수 없습니다."
def process_location_search(self, destination):
"""위치 찾기 처리를 수행합니다."""
places = self.place_searcher.find_places(destination)
if places:
formatted_places = []
for idx, (place_name, x, y) in enumerate(places, start=1):
formatted_places.append(f"{idx}. {place_name}")
places_text = "\n".join(formatted_places)
return f"'{destination}' 검색 결과:\n{places_text}"
else:
return f"'{destination}'에 대한 검색 결과가 없습니다."
def process_path_finding(self, destination):
"""길찾기 처리를 수행합니다."""
route_data, place_name = self.path_finder.find_path(destination)
formatted_result = self.path_finder.format_path_result(route_data, place_name or destination)
place = formatted_result.get("place_name", "")
routes = formatted_result.get("routes_text", "")
coords = formatted_result.get("formatted_coordinates", "")
return f"{routes}\n{coords}"
def process_bus_route(self, destination):
"""버스 노선 정보 처리를 수행합니다."""
result = self.bus_route_manager.process_bus_route(destination)
status = result.get("status")
if status == "버스_도착정보_있음":
match_buses = result.get("match_buses")
arrival_info = result.get("arrival_info")
bus_list = ", ".join([str(bus) for bus in match_buses])
arrival_text = "\n".join([f"{info['버스번호']}번 버스: {info['도착예정시간']}" for info in arrival_info])
return f"'{destination}'(으)로 가는 버스: {bus_list}\n\n현재 도착 정보:\n{arrival_text}"
elif status == "버스_도착정보_없음":
match_buses = result.get("match_buses")
bus_list = ", ".join([str(bus) for bus in match_buses])
return f"'{destination}'(으)로 가는 버스: {bus_list}\n\n현재 도착 예정인 버스가 없습니다."
elif status == "길찾기_수행":
route_data = result.get("route")
place_name = result.get("place_name")
formatted_result = self.path_finder.format_path_result(route_data, place_name or destination)
routes = formatted_result.get("routes_text", "")
coords = formatted_result.get("formatted_coordinates", "")
return f"{destination}(으)로 가는 버스가 없습니다. 대신 길찾기 결과를 알려드립니다.\n{routes}\n{coords}"
return "버스 노선 정보를 처리할 수 없습니다."
def get_rag_response(self, user_input):
"""RAG 체인을 사용하여 일반 응답을 생성합니다."""
try:
ai_response = self.rag_manager.get_ai_response(user_input, self.session_id)
response_text = "".join([chunk for chunk in ai_response])
return response_text
except Exception as e:
error_msg = f"응답 생성 중 오류가 발생했습니다: {str(e)}"
print(f"❌ {error_msg}")
return error_msg
def run(self):
"""챗봇 애플리케이션의 메인 실행 루프"""
print("🚏 고흥 AI 챗봇 🤖 (종료: 'e', 초기화: 'r')")
if not self.initialize_modules():
print("❌ 초기화에 실패했습니다. 프로그램을 종료합니다.")
return
while True:
self.reset_if_idle()
user_input = input("👤: ")
response = self.process_user_input(user_input)
if response == "exit":
print("👋 챗봇을 종료합니다.")
break
elif response == "reset":
print("🔄 대화 기록이 초기화되었습니다.")
continue
else:
print(f"🤖: {response}")
if __name__ == "__main__":
app = ChatbotApp()
app.run()