Spaces:
Running
Running
axeronis commited on
Commit ·
de3fd85
1
Parent(s): 9208c68
feat: Smart Qualification (Inference + Stage Criteria Link)
Browse files
backend/app/services/ai_core/lead_analyzer.py
CHANGED
|
@@ -149,6 +149,26 @@ async def extract_qualification_data(
|
|
| 149 |
ai_settings = tenant.ai_settings or {}
|
| 150 |
qualification_fields = ai_settings.get("qualification_fields") or ["Имя", "Контакт", "Потребность"]
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# Build conversation text
|
| 153 |
conversation_text = "\n".join([
|
| 154 |
f"{msg.sender_type.upper()}: {msg.content}"
|
|
@@ -161,26 +181,26 @@ async def extract_qualification_data(
|
|
| 161 |
|
| 162 |
fields_str = ", ".join([f'"{f}"' for f in qualification_fields])
|
| 163 |
|
| 164 |
-
prompt = f"""Проанализируй диалог и извлеки информацию по
|
| 165 |
-
|
| 166 |
-
БИЗНЕС
|
| 167 |
-
{
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
Ответ
|
| 183 |
-
{{{", ".join([f'"{f}": "значение
|
| 184 |
|
| 185 |
try:
|
| 186 |
response = await ai_core.llm.generate_response(
|
|
|
|
| 149 |
ai_settings = tenant.ai_settings or {}
|
| 150 |
qualification_fields = ai_settings.get("qualification_fields") or ["Имя", "Контакт", "Потребность"]
|
| 151 |
|
| 152 |
+
# Merge with fields from Active Funnel Stages (Exit Criteria)
|
| 153 |
+
try:
|
| 154 |
+
from app.models.sales_stage import SalesStage
|
| 155 |
+
from sqlmodel import select
|
| 156 |
+
|
| 157 |
+
stages_query = select(SalesStage).where(
|
| 158 |
+
SalesStage.tenant_id == lead.tenant_id,
|
| 159 |
+
SalesStage.is_active == True
|
| 160 |
+
)
|
| 161 |
+
stages_result = await session.execute(stages_query)
|
| 162 |
+
stages = stages_result.scalars().all()
|
| 163 |
+
|
| 164 |
+
for stage in stages:
|
| 165 |
+
if stage.exit_criteria:
|
| 166 |
+
for field in stage.exit_criteria:
|
| 167 |
+
if field and field not in qualification_fields:
|
| 168 |
+
qualification_fields.append(field)
|
| 169 |
+
except Exception as e:
|
| 170 |
+
logger.warning(f"Failed to fetch stage criteria for qualification: {e}")
|
| 171 |
+
|
| 172 |
# Build conversation text
|
| 173 |
conversation_text = "\n".join([
|
| 174 |
f"{msg.sender_type.upper()}: {msg.content}"
|
|
|
|
| 181 |
|
| 182 |
fields_str = ", ".join([f'"{f}"' for f in qualification_fields])
|
| 183 |
|
| 184 |
+
prompt = f"""Проанализируй диалог и извлеки информацию по полям квалификации.
|
| 185 |
+
|
| 186 |
+
БИЗНЕС: {business_description}
|
| 187 |
+
ТИП: {business_type}
|
| 188 |
+
|
| 189 |
+
ПОЛЯ ДЛЯ ЗАПОЛНЕНИЯ: [{fields_str}]
|
| 190 |
+
|
| 191 |
+
ДИАЛОГ:
|
| 192 |
+
{conversation_text}
|
| 193 |
+
|
| 194 |
+
ИНСТРУКЦИЯ:
|
| 195 |
+
1. Действуй умно, как опытный менеджер.
|
| 196 |
+
2. Извлекай факты, даже если клиент сказал не прямым текстом (логический вывод).
|
| 197 |
+
3. Если клиент просто поздоровался или данных нет — пиши "не выявлено".
|
| 198 |
+
4. Имя: используй реальное имя, если клиент представился (или оно очевидно).
|
| 199 |
+
5. Контакт: ищи телефон/email.
|
| 200 |
+
6. Потребность: сформулируй кратко, что хочет клиент.
|
| 201 |
+
|
| 202 |
+
Ответ строго JSON:
|
| 203 |
+
{{{", ".join([f'"{f}": "значение"' for f in qualification_fields])}}}"""
|
| 204 |
|
| 205 |
try:
|
| 206 |
response = await ai_core.llm.generate_response(
|