Spaces:
Running
Running
axeronis commited on
Commit ·
2b9b87f
1
Parent(s): 478d0d2
feat: AI-based smart rejection detection - understands context not keywords
Browse files- backend/app/api/jobs.py +39 -14
backend/app/api/jobs.py
CHANGED
|
@@ -231,30 +231,55 @@ async def trigger_non_responder_followup(
|
|
| 231 |
if not messages:
|
| 232 |
continue
|
| 233 |
|
| 234 |
-
#
|
| 235 |
-
|
| 236 |
-
"
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
]
|
| 240 |
|
| 241 |
-
|
|
|
|
| 242 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 243 |
is_refused = False
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
if is_refused:
|
| 250 |
-
logger.info(f"🛑
|
| 251 |
-
lead.status = LeadStatus.LOST
|
| 252 |
session.add(lead)
|
| 253 |
await session.commit()
|
| 254 |
skipped_refused += 1
|
| 255 |
continue
|
| 256 |
|
| 257 |
-
# Generate AI personalized
|
| 258 |
conversation_summary = "\n".join([
|
| 259 |
f"{'Клиент' if m.sender_type == 'user' else 'Бот'}: {m.content[:100]}"
|
| 260 |
for m in reversed(messages[-5:])
|
|
|
|
| 231 |
if not messages:
|
| 232 |
continue
|
| 233 |
|
| 234 |
+
# AI-based rejection analysis (smart, not keywords)
|
| 235 |
+
conversation_summary = "\n".join([
|
| 236 |
+
f"{'Клиент' if m.sender_type == 'user' else 'Бот'}: {m.content[:150]}"
|
| 237 |
+
for m in reversed(messages[-8:])
|
| 238 |
+
])
|
|
|
|
| 239 |
|
| 240 |
+
from app.services.llm_client import get_llm_client
|
| 241 |
+
llm = get_llm_client()
|
| 242 |
|
| 243 |
+
# First: Check if client refused/rejected
|
| 244 |
+
rejection_prompt = f"""Проанализируй диалог и определи: ОТКАЗАЛСЯ ли клиент от общения?
|
| 245 |
+
|
| 246 |
+
ДИАЛОГ:
|
| 247 |
+
{conversation_summary}
|
| 248 |
+
|
| 249 |
+
ПРИЗНАКИ ОТКАЗА:
|
| 250 |
+
- Прямой отказ ("не интересует", "не нужно")
|
| 251 |
+
- Негатив, раздражение, грубость
|
| 252 |
+
- Просьба не писать/не звонить
|
| 253 |
+
- Жалоба на спам
|
| 254 |
+
- Игнорирование нескольких сообщений подряд БЕЗ интереса ранее
|
| 255 |
+
|
| 256 |
+
ПРИЗНАКИ НЕ-ОТКАЗА:
|
| 257 |
+
- Просто занят, не ответил
|
| 258 |
+
- Ранее проявлял интерес
|
| 259 |
+
- Вежливо попросил паузу
|
| 260 |
+
- Задавал вопросы
|
| 261 |
+
|
| 262 |
+
ОТВЕТ (только одно слово): ОТКАЗ или ПРОДОЛЖАТЬ"""
|
| 263 |
+
|
| 264 |
is_refused = False
|
| 265 |
+
try:
|
| 266 |
+
rejection_response = await llm.generate_response(prompt=rejection_prompt, temperature=0.1)
|
| 267 |
+
rejection_text = rejection_response.get("text", "").strip().upper()
|
| 268 |
+
is_refused = "ОТКАЗ" in rejection_text
|
| 269 |
+
logger.info(f"🤖 AI rejection analysis for {lead.name}: {rejection_text}")
|
| 270 |
+
except Exception as ai_err:
|
| 271 |
+
logger.error(f"AI rejection check failed: {ai_err}")
|
| 272 |
+
is_refused = False
|
| 273 |
|
| 274 |
if is_refused:
|
| 275 |
+
logger.info(f"🛑 AI detected rejection for {lead.name}")
|
| 276 |
+
lead.status = LeadStatus.LOST
|
| 277 |
session.add(lead)
|
| 278 |
await session.commit()
|
| 279 |
skipped_refused += 1
|
| 280 |
continue
|
| 281 |
|
| 282 |
+
# Generate AI personalized follow-up message
|
| 283 |
conversation_summary = "\n".join([
|
| 284 |
f"{'Клиент' if m.sender_type == 'user' else 'Бот'}: {m.content[:100]}"
|
| 285 |
for m in reversed(messages[-5:])
|