Spaces:
Running

axeronis commited on
Commit
6aced91
·
1 Parent(s): c5b0923

feat: systematic AI memory - inject conversation facts from lead.ai_summary_json

Browse files
backend/app/core/config.py CHANGED
@@ -59,7 +59,7 @@ class Settings(BaseSettings):
59
  TELEGRAM_WEBHOOK_URL: Optional[str] = None
60
 
61
  # AI Settings
62
- MAX_CONTEXT_MESSAGES: int = 30 # Increased to 30 (will be trimmed by tokens in LLM client)
63
  TEMPERATURE: float = 0.7
64
  MAX_TOKENS: int = 1024
65
 
 
59
  TELEGRAM_WEBHOOK_URL: Optional[str] = None
60
 
61
  # AI Settings
62
+ MAX_CONTEXT_MESSAGES: int = 50 # Increased from 30 to 50 for better conversation memory
63
  TEMPERATURE: float = 0.7
64
  MAX_TOKENS: int = 1024
65
 
backend/app/services/ai_core/response_generator.py CHANGED
@@ -137,7 +137,33 @@ Answer with ONE word only: WANT_VOICE, NO_VOICE, or NEUTRAL""",
137
  elif user_requested_no_voice:
138
  logger.info(f"📢 FINAL voice preference: NO (user requested)")
139
 
140
- # 4. DYNAMIC CONTEXT (Things that change per request)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
141
  dynamic_context = f"""
142
  [CURRENT CONTEXT - {datetime.now().strftime("%Y-%m-%d %H:%M")}]
143
  User Name: {lead.name or 'Unknown'}
@@ -145,6 +171,7 @@ Psychotype: {lead.psychotype} (Adapt tone accordingly)
145
  Funnel Stage: {current_stage.name if current_stage else 'General'}
146
  Goal: {current_stage.ai_goal if current_stage else 'Assist user'}
147
  Message Count: {ai_message_count} (0-1 = Первое сообщение, используй голос!)
 
148
 
149
  [BOOKING AVAILABILITY]
150
  {booking_context if booking_context else "No specific slots needed yet."}
 
137
  elif user_requested_no_voice:
138
  logger.info(f"📢 FINAL voice preference: NO (user requested)")
139
 
140
+ # 4. SYSTEMATIC MEMORY: Extract key facts from AI Summary (prevents amnesia)
141
+ conversation_facts = ""
142
+ if lead.ai_summary_json:
143
+ summary = lead.ai_summary_json
144
+ facts = []
145
+
146
+ # Extract BANT facts
147
+ if summary.get("budget") and "не" not in str(summary.get("budget", "")).lower():
148
+ facts.append(f"💰 Бюджет: {summary['budget']}")
149
+ if summary.get("timeline") and "не" not in str(summary.get("timeline", "")).lower():
150
+ facts.append(f"⏰ Сроки: {summary['timeline']}")
151
+ if summary.get("authority") and "не" not in str(summary.get("authority", "")).lower():
152
+ facts.append(f"👤 ЛПР: {summary['authority']}")
153
+ if summary.get("intent"):
154
+ facts.append(f"🎯 Намерение: {summary['intent']}")
155
+
156
+ # Extract pain points and objections
157
+ if summary.get("pain_points") and isinstance(summary["pain_points"], list):
158
+ facts.append(f"😣 Боли: {', '.join(summary['pain_points'][:3])}")
159
+ if summary.get("objections") and isinstance(summary["objections"], list):
160
+ facts.append(f"🛡️ Возражения: {', '.join(summary['objections'][:3])}")
161
+
162
+ if facts:
163
+ conversation_facts = "\n[ПАМЯТЬ ДИАЛОГА - НЕ СПРАШИВАЙ ПОВТОРНО!]\n" + "\n".join(facts)
164
+ logger.info(f"🧠 Injecting {len(facts)} conversation facts into context")
165
+
166
+ # 5. DYNAMIC CONTEXT (Things that change per request)
167
  dynamic_context = f"""
168
  [CURRENT CONTEXT - {datetime.now().strftime("%Y-%m-%d %H:%M")}]
169
  User Name: {lead.name or 'Unknown'}
 
171
  Funnel Stage: {current_stage.name if current_stage else 'General'}
172
  Goal: {current_stage.ai_goal if current_stage else 'Assist user'}
173
  Message Count: {ai_message_count} (0-1 = Первое сообщение, используй голос!)
174
+ {conversation_facts}
175
 
176
  [BOOKING AVAILABILITY]
177
  {booking_context if booking_context else "No specific slots needed yet."}