Update services/chat_service.py
Browse files- services/chat_service.py +13 -9
services/chat_service.py
CHANGED
@@ -177,30 +177,34 @@ class ChatService:
|
|
177 |
async def chat(
|
178 |
self,
|
179 |
user_input: str,
|
180 |
-
session_id:
|
181 |
max_length: int = 1000
|
182 |
) -> Tuple[str, List[Dict[str, Any]]]:
|
183 |
"""Main chat method that coordinates the entire conversation flow"""
|
184 |
try:
|
|
|
|
|
|
|
|
|
185 |
# Get chat history
|
186 |
chat_history = self.conversation_manager.get_history(session_id)
|
187 |
-
|
188 |
# Search all sources
|
189 |
search_results = await self.search_all_sources(user_input)
|
190 |
-
|
191 |
# Build context
|
192 |
context = self.build_context(search_results, chat_history)
|
193 |
-
|
194 |
# Create prompt
|
195 |
prompt = (
|
196 |
f"Context:\n{context}\n\n"
|
197 |
f"User: {user_input}\n"
|
198 |
"Assistant:"
|
199 |
)
|
200 |
-
|
201 |
# Generate response
|
202 |
response = await self.generate_response(prompt, max_length)
|
203 |
-
|
204 |
# Store interaction
|
205 |
self.conversation_manager.add_interaction(
|
206 |
session_id,
|
@@ -208,9 +212,9 @@ class ChatService:
|
|
208 |
response,
|
209 |
{'search_results': search_results}
|
210 |
)
|
211 |
-
|
212 |
return response, search_results
|
213 |
-
|
214 |
except Exception as e:
|
215 |
logger.error(f"Error in chat: {e}")
|
216 |
-
raise
|
|
|
177 |
async def chat(
|
178 |
self,
|
179 |
user_input: str,
|
180 |
+
session_id: Any, # Allow any type to ensure flexibility, validate later
|
181 |
max_length: int = 1000
|
182 |
) -> Tuple[str, List[Dict[str, Any]]]:
|
183 |
"""Main chat method that coordinates the entire conversation flow"""
|
184 |
try:
|
185 |
+
# Ensure session_id is a string
|
186 |
+
if not isinstance(session_id, str):
|
187 |
+
session_id = str(session_id)
|
188 |
+
|
189 |
# Get chat history
|
190 |
chat_history = self.conversation_manager.get_history(session_id)
|
191 |
+
|
192 |
# Search all sources
|
193 |
search_results = await self.search_all_sources(user_input)
|
194 |
+
|
195 |
# Build context
|
196 |
context = self.build_context(search_results, chat_history)
|
197 |
+
|
198 |
# Create prompt
|
199 |
prompt = (
|
200 |
f"Context:\n{context}\n\n"
|
201 |
f"User: {user_input}\n"
|
202 |
"Assistant:"
|
203 |
)
|
204 |
+
|
205 |
# Generate response
|
206 |
response = await self.generate_response(prompt, max_length)
|
207 |
+
|
208 |
# Store interaction
|
209 |
self.conversation_manager.add_interaction(
|
210 |
session_id,
|
|
|
212 |
response,
|
213 |
{'search_results': search_results}
|
214 |
)
|
215 |
+
|
216 |
return response, search_results
|
217 |
+
|
218 |
except Exception as e:
|
219 |
logger.error(f"Error in chat: {e}")
|
220 |
+
raise
|