Spaces:
Running
Running
axeronis commited on
Commit ·
cf62886
1
Parent(s): 6df5db6
feat: add smart LLM semantic analysis for voice preference detection
Browse files
backend/app/services/ai_core/response_generator.py
CHANGED
|
@@ -91,7 +91,7 @@ async def generate_sales_response_impl(
|
|
| 91 |
ai_message_count = len([m for m in conversation_history if get_sender_type(m) == 'ai']) if conversation_history else 0
|
| 92 |
|
| 93 |
# DETECT USER'S LATEST VOICE PREFERENCE
|
| 94 |
-
#
|
| 95 |
user_requested_no_voice = False
|
| 96 |
user_messages_with_idx = [(i, get_content(m)) for i, m in enumerate(conversation_history or []) if get_sender_type(m) == 'user']
|
| 97 |
|
|
@@ -100,7 +100,7 @@ async def generate_sales_response_impl(
|
|
| 100 |
for _, msg in user_messages_with_idx[-5:]: # Log last 5
|
| 101 |
logger.info(f" {msg[:80]}...")
|
| 102 |
|
| 103 |
-
# Keywords for preferences
|
| 104 |
no_voice_keywords = [
|
| 105 |
'не присылай', 'не отправляй', 'не шли', 'не надо голос', 'не голос',
|
| 106 |
'без голосов', 'только текст', 'пиши текст', 'текстом', 'зачем голос',
|
|
@@ -108,31 +108,69 @@ async def generate_sales_response_impl(
|
|
| 108 |
]
|
| 109 |
yes_voice_keywords = [
|
| 110 |
'отправь голос', 'давай голос', 'голосом', 'голосовыми', 'хочу голос',
|
| 111 |
-
'send voice', 'use voice', 'говори', 'скажи голосом'
|
| 112 |
]
|
| 113 |
|
| 114 |
-
# Find the LATEST preference
|
| 115 |
last_voice_preference = None # None = no preference, 'no' = no voice, 'yes' = want voice
|
| 116 |
last_pref_idx = -1
|
| 117 |
|
| 118 |
for idx, msg in user_messages_with_idx:
|
| 119 |
msg_lower = msg.lower()
|
| 120 |
-
# Check for no-voice
|
| 121 |
if any(kw in msg_lower for kw in no_voice_keywords):
|
| 122 |
if idx > last_pref_idx:
|
| 123 |
last_voice_preference = 'no'
|
| 124 |
last_pref_idx = idx
|
| 125 |
-
logger.info(f"🔇
|
| 126 |
-
# Check for yes-voice
|
| 127 |
if any(kw in msg_lower for kw in yes_voice_keywords):
|
| 128 |
if idx > last_pref_idx:
|
| 129 |
last_voice_preference = 'yes'
|
| 130 |
last_pref_idx = idx
|
| 131 |
-
logger.info(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
user_requested_no_voice = (last_voice_preference == 'no')
|
| 134 |
if last_voice_preference:
|
| 135 |
-
logger.info(f"📢
|
| 136 |
|
| 137 |
# 4. DYNAMIC CONTEXT (Things that change per request)
|
| 138 |
dynamic_context = f"""
|
|
|
|
| 91 |
ai_message_count = len([m for m in conversation_history if get_sender_type(m) == 'ai']) if conversation_history else 0
|
| 92 |
|
| 93 |
# DETECT USER'S LATEST VOICE PREFERENCE
|
| 94 |
+
# DUAL approach: fast keyword check + smart LLM semantic analysis
|
| 95 |
user_requested_no_voice = False
|
| 96 |
user_messages_with_idx = [(i, get_content(m)) for i, m in enumerate(conversation_history or []) if get_sender_type(m) == 'user']
|
| 97 |
|
|
|
|
| 100 |
for _, msg in user_messages_with_idx[-5:]: # Log last 5
|
| 101 |
logger.info(f" {msg[:80]}...")
|
| 102 |
|
| 103 |
+
# 1. FAST CHECK: Keywords for obvious preferences
|
| 104 |
no_voice_keywords = [
|
| 105 |
'не присылай', 'не отправляй', 'не шли', 'не надо голос', 'не голос',
|
| 106 |
'без голосов', 'только текст', 'пиши текст', 'текстом', 'зачем голос',
|
|
|
|
| 108 |
]
|
| 109 |
yes_voice_keywords = [
|
| 110 |
'отправь голос', 'давай голос', 'голосом', 'голосовыми', 'хочу голос',
|
| 111 |
+
'send voice', 'use voice', 'говори', 'скажи голосом', 'озвучь'
|
| 112 |
]
|
| 113 |
|
| 114 |
+
# Find the LATEST preference from keywords
|
| 115 |
last_voice_preference = None # None = no preference, 'no' = no voice, 'yes' = want voice
|
| 116 |
last_pref_idx = -1
|
| 117 |
|
| 118 |
for idx, msg in user_messages_with_idx:
|
| 119 |
msg_lower = msg.lower()
|
|
|
|
| 120 |
if any(kw in msg_lower for kw in no_voice_keywords):
|
| 121 |
if idx > last_pref_idx:
|
| 122 |
last_voice_preference = 'no'
|
| 123 |
last_pref_idx = idx
|
| 124 |
+
logger.info(f"🔇 Keyword: NO VOICE at idx {idx}: {msg[:50]}")
|
|
|
|
| 125 |
if any(kw in msg_lower for kw in yes_voice_keywords):
|
| 126 |
if idx > last_pref_idx:
|
| 127 |
last_voice_preference = 'yes'
|
| 128 |
last_pref_idx = idx
|
| 129 |
+
logger.info(f"� Keyword: YES VOICE at idx {idx}: {msg[:50]}")
|
| 130 |
+
|
| 131 |
+
# 2. SMART LLM CHECK: Semantic analysis for non-obvious requests
|
| 132 |
+
# Only run if keyword check didn't find anything, but there are recent messages
|
| 133 |
+
if last_voice_preference is None and len(user_messages_with_idx) >= 3:
|
| 134 |
+
# Check last 5 user messages for semantic preference
|
| 135 |
+
recent_msgs = [msg for _, msg in user_messages_with_idx[-5:]]
|
| 136 |
+
user_context = "\n".join([f"- {msg}" for msg in recent_msgs])
|
| 137 |
+
|
| 138 |
+
try:
|
| 139 |
+
voice_analysis = await ai_core.llm.generate_response(
|
| 140 |
+
prompt=f"""Analyze these user messages. Does the user express any preference about receiving VOICE/AUDIO messages vs TEXT messages?
|
| 141 |
+
|
| 142 |
+
Messages:
|
| 143 |
+
{user_context}
|
| 144 |
+
|
| 145 |
+
Look for:
|
| 146 |
+
- Complaints about voice/audio messages (e.g. "зачем аудио", "неудобно слушать", "я на работе")
|
| 147 |
+
- Requests for voice (e.g. "скажи голосом", "озвучь", "хочу послушать")
|
| 148 |
+
- Implicit preferences
|
| 149 |
+
|
| 150 |
+
Answer with ONE word only:
|
| 151 |
+
- WANT_VOICE - if user wants voice messages
|
| 152 |
+
- NO_VOICE - if user doesn't want voice messages
|
| 153 |
+
- NEUTRAL - if no clear preference""",
|
| 154 |
+
system_instruction="Output only: WANT_VOICE, NO_VOICE, or NEUTRAL",
|
| 155 |
+
context=None,
|
| 156 |
+
chat_history=[]
|
| 157 |
+
)
|
| 158 |
+
|
| 159 |
+
llm_result = voice_analysis["text"].strip().upper()
|
| 160 |
+
if "NO_VOICE" in llm_result:
|
| 161 |
+
last_voice_preference = 'no'
|
| 162 |
+
logger.info(f"🧠 LLM detected: NO VOICE preference")
|
| 163 |
+
elif "WANT_VOICE" in llm_result:
|
| 164 |
+
last_voice_preference = 'yes'
|
| 165 |
+
logger.info(f"🧠 LLM detected: WANT VOICE preference")
|
| 166 |
+
else:
|
| 167 |
+
logger.info(f"🧠 LLM detected: NEUTRAL (no preference)")
|
| 168 |
+
except Exception as e:
|
| 169 |
+
logger.warning(f"LLM voice preference check failed: {e}")
|
| 170 |
|
| 171 |
user_requested_no_voice = (last_voice_preference == 'no')
|
| 172 |
if last_voice_preference:
|
| 173 |
+
logger.info(f"📢 FINAL voice preference: {last_voice_preference.upper()}")
|
| 174 |
|
| 175 |
# 4. DYNAMIC CONTEXT (Things that change per request)
|
| 176 |
dynamic_context = f"""
|