Spaces:
Sleeping
Sleeping
Commit ·
7566ec4
1
Parent(s): d531fba
language反應
Browse files- features/mcp/agent_bridge.py +50 -8
features/mcp/agent_bridge.py
CHANGED
|
@@ -585,10 +585,16 @@ class MCPAgentBridge:
|
|
| 585 |
|
| 586 |
logger.info(f"✅ GPT 選擇工具: {normalized_name}")
|
| 587 |
logger.debug(f"工具參數: {_safe_json(arguments)}")
|
| 588 |
-
|
| 589 |
-
# 提取情緒(從 content 或
|
| 590 |
-
|
| 591 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 592 |
intent_result = (True, {
|
| 593 |
"type": "mcp_tool",
|
| 594 |
"tool_name": normalized_name,
|
|
@@ -779,7 +785,7 @@ YouBike 查詢(重要!參數提取規則):
|
|
| 779 |
"""從回應內容中提取情緒標籤 [EMOTION:xxx]"""
|
| 780 |
if not content:
|
| 781 |
return "neutral"
|
| 782 |
-
|
| 783 |
# 優先從標籤提取
|
| 784 |
import re
|
| 785 |
emotion_match = re.search(r'\[EMOTION:(neutral|happy|sad|angry|fear|surprise)\]', content, re.IGNORECASE)
|
|
@@ -787,18 +793,54 @@ YouBike 查詢(重要!參數提取規則):
|
|
| 787 |
emotion = emotion_match.group(1).lower()
|
| 788 |
logger.info(f"😊 從標籤提取情緒: {emotion}")
|
| 789 |
return emotion
|
| 790 |
-
|
| 791 |
# 降級:從內容搜尋英文關鍵字
|
| 792 |
content_lower = content.lower()
|
| 793 |
emotions = ["happy", "sad", "angry", "fear", "surprise"]
|
| 794 |
-
|
| 795 |
for emotion in emotions:
|
| 796 |
if emotion in content_lower:
|
| 797 |
logger.debug(f"從內容搜尋到情緒關鍵字: {emotion}")
|
| 798 |
return emotion
|
| 799 |
-
|
| 800 |
return "neutral"
|
| 801 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 802 |
# 舊版 _detect_intent_legacy 已移除,改用 _detect_intent_with_function_calling
|
| 803 |
|
| 804 |
def _get_intent_schema(self) -> Dict[str, Any]:
|
|
|
|
| 585 |
|
| 586 |
logger.info(f"✅ GPT 選擇工具: {normalized_name}")
|
| 587 |
logger.debug(f"工具參數: {_safe_json(arguments)}")
|
| 588 |
+
|
| 589 |
+
# 提取情緒(從 content 或直接從用戶訊息分析)
|
| 590 |
+
content = response.get("content", "")
|
| 591 |
+
if content:
|
| 592 |
+
emotion = self._extract_emotion_from_content(content)
|
| 593 |
+
else:
|
| 594 |
+
# 當 GPT 只回傳 tool_calls 時,直接從用戶訊息分析情緒
|
| 595 |
+
logger.debug(f"🔍 GPT content 為空,從用戶訊息分析情緒")
|
| 596 |
+
emotion = await self._analyze_emotion_from_message(message)
|
| 597 |
+
|
| 598 |
intent_result = (True, {
|
| 599 |
"type": "mcp_tool",
|
| 600 |
"tool_name": normalized_name,
|
|
|
|
| 785 |
"""從回應內容中提取情緒標籤 [EMOTION:xxx]"""
|
| 786 |
if not content:
|
| 787 |
return "neutral"
|
| 788 |
+
|
| 789 |
# 優先從標籤提取
|
| 790 |
import re
|
| 791 |
emotion_match = re.search(r'\[EMOTION:(neutral|happy|sad|angry|fear|surprise)\]', content, re.IGNORECASE)
|
|
|
|
| 793 |
emotion = emotion_match.group(1).lower()
|
| 794 |
logger.info(f"😊 從標籤提取情緒: {emotion}")
|
| 795 |
return emotion
|
| 796 |
+
|
| 797 |
# 降級:從內容搜尋英文關鍵字
|
| 798 |
content_lower = content.lower()
|
| 799 |
emotions = ["happy", "sad", "angry", "fear", "surprise"]
|
| 800 |
+
|
| 801 |
for emotion in emotions:
|
| 802 |
if emotion in content_lower:
|
| 803 |
logger.debug(f"從內容搜尋到情緒關鍵字: {emotion}")
|
| 804 |
return emotion
|
| 805 |
+
|
| 806 |
return "neutral"
|
| 807 |
|
| 808 |
+
async def _analyze_emotion_from_message(self, message: str) -> str:
|
| 809 |
+
"""直接從用戶訊息分析情緒(當 GPT content 為空時使用)"""
|
| 810 |
+
try:
|
| 811 |
+
import services.ai_service as ai_service
|
| 812 |
+
|
| 813 |
+
system_prompt = (
|
| 814 |
+
"分析用戶訊息的情緒狀態。\n"
|
| 815 |
+
"情緒類型:neutral(平靜)、happy(開心)、sad(難過)、angry(生氣)、fear(害怕)、surprise(驚訝)\n"
|
| 816 |
+
"只回傳情緒類型的英文單字,不要有任何其他文字。"
|
| 817 |
+
)
|
| 818 |
+
|
| 819 |
+
messages = [
|
| 820 |
+
{"role": "system", "content": system_prompt},
|
| 821 |
+
{"role": "user", "content": message}
|
| 822 |
+
]
|
| 823 |
+
|
| 824 |
+
emotion = await ai_service.generate_response_async(
|
| 825 |
+
messages=messages,
|
| 826 |
+
model="gpt-4o-mini",
|
| 827 |
+
max_tokens=10,
|
| 828 |
+
)
|
| 829 |
+
|
| 830 |
+
emotion = emotion.strip().lower()
|
| 831 |
+
valid_emotions = ["neutral", "happy", "sad", "angry", "fear", "surprise"]
|
| 832 |
+
|
| 833 |
+
if emotion in valid_emotions:
|
| 834 |
+
logger.info(f"😊 從用戶訊息分析情緒: {emotion}")
|
| 835 |
+
return emotion
|
| 836 |
+
else:
|
| 837 |
+
logger.warning(f"⚠️ 情緒分析結果無效: {emotion},使用預設 neutral")
|
| 838 |
+
return "neutral"
|
| 839 |
+
|
| 840 |
+
except Exception as e:
|
| 841 |
+
logger.warning(f"⚠️ 情緒分析失敗: {e},使用預設 neutral")
|
| 842 |
+
return "neutral"
|
| 843 |
+
|
| 844 |
# 舊版 _detect_intent_legacy 已移除,改用 _detect_intent_with_function_calling
|
| 845 |
|
| 846 |
def _get_intent_schema(self) -> Dict[str, Any]:
|