Update services/chat_service.py
Browse files- services/chat_service.py +23 -12
services/chat_service.py
CHANGED
|
@@ -12,7 +12,7 @@ class ChatService:
|
|
| 12 |
self.tts_service = tts_service
|
| 13 |
self.multilingual_manager = rag_system.multilingual_manager
|
| 14 |
|
| 15 |
-
def respond(self, message: str, chat_history: List
|
| 16 |
"""Tạo phản hồi cho tin nhắn đầu vào dựa trên lịch sử trò chuyện và ngôn ngữ."""
|
| 17 |
if chat_history is None:
|
| 18 |
chat_history = []
|
|
@@ -25,11 +25,18 @@ class ChatService:
|
|
| 25 |
language = self.multilingual_manager.detect_language(message)
|
| 26 |
llm_model = self.multilingual_manager.get_llm_model(language)
|
| 27 |
|
| 28 |
-
# Xây dựng messages từ chat history
|
| 29 |
messages = []
|
| 30 |
-
for
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
messages.append({"role": "user", "content": message})
|
| 34 |
|
| 35 |
# Tìm kiếm RAG
|
|
@@ -67,11 +74,13 @@ If reference information is relevant, use it. Otherwise, rely on your general kn
|
|
| 67 |
temperature=0.7,
|
| 68 |
)
|
| 69 |
|
| 70 |
-
# SỬA QUAN TRỌNG: Cách truy cập response đúng
|
| 71 |
assistant_message = completion.choices[0].message.content.strip()
|
| 72 |
|
| 73 |
-
# Cập nhật chat history
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
| 75 |
|
| 76 |
# Tạo TTS
|
| 77 |
tts_audio_path = None
|
|
@@ -84,13 +93,15 @@ If reference information is relevant, use it. Otherwise, rely on your general kn
|
|
| 84 |
print(f"⚠️ Lỗi TTS: {tts_error}")
|
| 85 |
# Vẫn tiếp tục nếu TTS fail
|
| 86 |
|
| 87 |
-
return "",
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
error_msg = f"❌ Lỗi trong quá trình tạo phản hồi: {str(e)}"
|
| 91 |
-
chat_history.
|
| 92 |
-
|
|
|
|
|
|
|
| 93 |
|
| 94 |
-
def clear_chat_history(self, chat_history: List
|
| 95 |
"""Xóa lịch sử trò chuyện."""
|
| 96 |
return [], []
|
|
|
|
| 12 |
self.tts_service = tts_service
|
| 13 |
self.multilingual_manager = rag_system.multilingual_manager
|
| 14 |
|
| 15 |
+
def respond(self, message: str, chat_history: List) -> tuple:
|
| 16 |
"""Tạo phản hồi cho tin nhắn đầu vào dựa trên lịch sử trò chuyện và ngôn ngữ."""
|
| 17 |
if chat_history is None:
|
| 18 |
chat_history = []
|
|
|
|
| 25 |
language = self.multilingual_manager.detect_language(message)
|
| 26 |
llm_model = self.multilingual_manager.get_llm_model(language)
|
| 27 |
|
| 28 |
+
# Xây dựng messages từ chat history (CHUYỂN SANG FORMAT MESSAGES)
|
| 29 |
messages = []
|
| 30 |
+
for msg in chat_history:
|
| 31 |
+
if isinstance(msg, dict) and 'role' in msg and 'content' in msg:
|
| 32 |
+
messages.append(msg)
|
| 33 |
+
elif isinstance(msg, (list, tuple)) and len(msg) == 2:
|
| 34 |
+
# Convert từ format tuple cũ sang format messages mới
|
| 35 |
+
user_msg, assistant_msg = msg
|
| 36 |
+
messages.append({"role": "user", "content": user_msg})
|
| 37 |
+
messages.append({"role": "assistant", "content": assistant_msg})
|
| 38 |
+
|
| 39 |
+
# Thêm tin nhắn hiện tại
|
| 40 |
messages.append({"role": "user", "content": message})
|
| 41 |
|
| 42 |
# Tìm kiếm RAG
|
|
|
|
| 74 |
temperature=0.7,
|
| 75 |
)
|
| 76 |
|
|
|
|
| 77 |
assistant_message = completion.choices[0].message.content.strip()
|
| 78 |
|
| 79 |
+
# Cập nhật chat history với FORMAT MESSAGES MỚI
|
| 80 |
+
# Thêm cả user message và assistant message vào history
|
| 81 |
+
new_history = chat_history.copy()
|
| 82 |
+
new_history.append({"role": "user", "content": message})
|
| 83 |
+
new_history.append({"role": "assistant", "content": assistant_message})
|
| 84 |
|
| 85 |
# Tạo TTS
|
| 86 |
tts_audio_path = None
|
|
|
|
| 93 |
print(f"⚠️ Lỗi TTS: {tts_error}")
|
| 94 |
# Vẫn tiếp tục nếu TTS fail
|
| 95 |
|
| 96 |
+
return "", new_history, new_history, tts_audio_path, language
|
| 97 |
|
| 98 |
except Exception as e:
|
| 99 |
error_msg = f"❌ Lỗi trong quá trình tạo phản hồi: {str(e)}"
|
| 100 |
+
new_history = chat_history.copy()
|
| 101 |
+
new_history.append({"role": "user", "content": message})
|
| 102 |
+
new_history.append({"role": "assistant", "content": error_msg})
|
| 103 |
+
return "", new_history, new_history, None, "unknown"
|
| 104 |
|
| 105 |
+
def clear_chat_history(self, chat_history: List) -> tuple:
|
| 106 |
"""Xóa lịch sử trò chuyện."""
|
| 107 |
return [], []
|