Spaces:
Running
Running
axeronis commited on
Commit ·
ff263d8
1
Parent(s): 8ad92f2
fix: demo chat generation error - use direct Gemini call
Browse files- backend/app/api/demo.py +35 -37
backend/app/api/demo.py
CHANGED
|
@@ -44,10 +44,8 @@ async def demo_chat(
|
|
| 44 |
Does NOT save to database - purely for testing AI behavior.
|
| 45 |
"""
|
| 46 |
try:
|
| 47 |
-
from app.services.ai_core import get_ai_core
|
| 48 |
-
from app.models.lead import Lead
|
| 49 |
-
from app.models.message import Message
|
| 50 |
from app.services.ai_core.settings_assistant import parse_ai_action, execute_setting_change, remove_action_from_response
|
|
|
|
| 51 |
import re
|
| 52 |
|
| 53 |
# Get tenant
|
|
@@ -55,46 +53,46 @@ async def demo_chat(
|
|
| 55 |
if not tenant:
|
| 56 |
raise HTTPException(status_code=404, detail="Tenant not found")
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
|
| 60 |
-
id=None, # Not saved
|
| 61 |
-
tenant_id=tenant.id,
|
| 62 |
-
name="Demo User",
|
| 63 |
-
heat_score=50,
|
| 64 |
-
psychotype="analytical"
|
| 65 |
-
)
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
for msg in request.history[-10:]:
|
| 70 |
-
conversation_history.append({
|
| 71 |
-
"sender_type": msg.role,
|
| 72 |
-
"content": msg.content,
|
| 73 |
-
"created_at": None
|
| 74 |
-
})
|
| 75 |
-
|
| 76 |
-
# Add corrections to custom_instructions temporarily
|
| 77 |
-
original_instructions = tenant.custom_instructions or ""
|
| 78 |
if request.corrections:
|
| 79 |
-
|
| 80 |
for i, correction in enumerate(request.corrections, 1):
|
| 81 |
-
|
| 82 |
-
tenant.custom_instructions = original_instructions + corrections_text
|
| 83 |
|
| 84 |
-
#
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
lead=demo_lead,
|
| 90 |
-
user_message=request.message,
|
| 91 |
-
conversation_history=conversation_history
|
| 92 |
-
)
|
| 93 |
|
| 94 |
-
#
|
| 95 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 96 |
|
| 97 |
-
ai_response = response.
|
|
|
|
|
|
|
| 98 |
|
| 99 |
# ========================================================================
|
| 100 |
# SMART SETTINGS ASSISTANT: Parse and execute __AI_ACTION__ if present
|
|
|
|
| 44 |
Does NOT save to database - purely for testing AI behavior.
|
| 45 |
"""
|
| 46 |
try:
|
|
|
|
|
|
|
|
|
|
| 47 |
from app.services.ai_core.settings_assistant import parse_ai_action, execute_setting_change, remove_action_from_response
|
| 48 |
+
from google import genai
|
| 49 |
import re
|
| 50 |
|
| 51 |
# Get tenant
|
|
|
|
| 53 |
if not tenant:
|
| 54 |
raise HTTPException(status_code=404, detail="Tenant not found")
|
| 55 |
|
| 56 |
+
# Build FULL context using memory manager (includes KB, products, settings)
|
| 57 |
+
tenant_context = await memory_manager.build_tenant_context(session, tenant)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
# Add corrections to context if any
|
| 60 |
+
corrections_prompt = ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
if request.corrections:
|
| 62 |
+
corrections_prompt = "\n\n[ВАЖНЫЕ ПРАВКИ ОТ ВЛАДЕЛЬЦА - СТРОГО СЛЕДУЙ]:\n"
|
| 63 |
for i, correction in enumerate(request.corrections, 1):
|
| 64 |
+
corrections_prompt += f"{i}. {correction}\n"
|
|
|
|
| 65 |
|
| 66 |
+
# Build conversation history
|
| 67 |
+
history_text = ""
|
| 68 |
+
for msg in request.history[-10:]:
|
| 69 |
+
role = "Клиент" if msg.role == "user" else "Ты"
|
| 70 |
+
history_text += f"{role}: {msg.content}\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
+
# Build full prompt
|
| 73 |
+
full_prompt = f"""{tenant_context}
|
| 74 |
+
|
| 75 |
+
{corrections_prompt}
|
| 76 |
+
|
| 77 |
+
=== ИСТОРИЯ ДИАЛОГА ===
|
| 78 |
+
{history_text}
|
| 79 |
+
Клиент: {request.message}
|
| 80 |
+
|
| 81 |
+
=== ТВОЙ ОТВЕТ ===
|
| 82 |
+
Ответь как ассистент, следуя всем правилам выше.
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
# Generate response using Gemini
|
| 86 |
+
client = genai.Client(api_key=settings.GEMINI_API_KEY)
|
| 87 |
+
|
| 88 |
+
response = client.models.generate_content(
|
| 89 |
+
model=settings.GEMINI_MODEL,
|
| 90 |
+
contents=full_prompt
|
| 91 |
+
)
|
| 92 |
|
| 93 |
+
ai_response = response.text if response.text else "Не удалось сгенерировать ответ"
|
| 94 |
+
ai_response = ai_response.strip()
|
| 95 |
+
|
| 96 |
|
| 97 |
# ========================================================================
|
| 98 |
# SMART SETTINGS ASSISTANT: Parse and execute __AI_ACTION__ if present
|