Romanchello-bit commited on
Commit
5fa037a
·
1 Parent(s): c29d5e6

Refactor response and greeting generation logic

Browse files

Unified and enhanced the prompt construction for generate_response to support more flexible, instruction-driven replies with improved tone and brevity adaptation. Simplified generate_greeting to use a single prompt with context-based branching, and updated both functions for clearer structure and error handling.

Files changed (1) hide show
  1. app.py +54 -80
app.py CHANGED
@@ -156,99 +156,73 @@ def analyze_full_context(model, user_input, current_node, chat_history):
156
  except:
157
  return {"archetype": "UNKNOWN", "intent": "STAY", "reasoning": "Error"}
158
 
159
- def generate_response(model, context_text, user_input, intent, lead_info, archetype):
160
- # Визначаємо, наскільки детальним має бути спіч
161
- call_context = lead_info.get('context', 'Cold')
162
-
163
- verbosity_instruction = ""
164
- if "Cold" in call_context or "Холодний" in call_context:
165
- verbosity_instruction = "LENGTH: VERY SHORT. Elevator Pitch style. The client has no patience."
166
- else:
167
- verbosity_instruction = "LENGTH: Detailed and explanatory. The client is interested."
168
-
169
- # Інструкція по стилю (з попередніх кроків)
170
- style_instruction = ""
171
- if archetype == "DRIVER": style_instruction = "STYLE: Direct, ROI-focused."
172
- elif archetype == "ANALYST": style_instruction = "STYLE: Data-driven, precise."
173
- elif archetype == "EXPRESSIVE": style_instruction = "STYLE: Visionary, exciting."
174
- elif archetype == "CONSERVATIVE": style_instruction = "STYLE: Safe, supportive."
175
-
176
- if intent == "STAY":
177
- prompt = f"""
178
- ROLE: Adaptive Sales Rep.
179
- CONTEXT: {verbosity_instruction}
180
- ARCHETYPE: {style_instruction}
181
-
182
- SITUATION: Step "{context_text}". Client Objected: "{user_input}".
183
- TASK: Handle objection.
184
- """
185
- else:
186
- prompt = f"""
187
- ROLE: Adaptive Sales Rep.
188
- CONTEXT: {verbosity_instruction}
189
- ARCHETYPE: {style_instruction}
190
-
191
- GOAL: Transition to "{context_text}". User said: "{user_input}".
192
- TASK: Bridge to the next step naturally.
193
- """
194
-
195
- try:
196
- return model.generate_content(prompt).text.strip()
197
- except: return "..."
198
-
199
- def generate_greeting(model, start_node_text, lead_info):
200
  """
201
- Генерує привітання залежно від КОНТЕКСТУ (Холодний vs Теплий).
202
  """
 
203
  bot_name = lead_info.get('bot_name', 'Олексій')
204
  client_name = lead_info.get('name', 'Клієнт')
205
  company = lead_info.get('company', 'Компанія')
206
- context = lead_info.get('context', 'Cold Call')
207
 
208
- # Спеціальні інструкції для різних типів дзвінків
209
- if "Cold" in context or "Холодний" in context:
210
- # ХОЛОДНИЙ ДЗВІНОК: Жодних "я представляю компанію". Коротко, зухвало.
211
- strategy = f"""
212
- STRATEGY: COLD CALL (High Risk of Hangup).
213
- RULES:
214
- 1. NO generic intros ("Hello, my name is... I represent...").
215
- 2. Use a "Pattern Interrupt" or "Permission-based opener".
216
- 3. Be brief (under 10 seconds).
217
-
218
- Example format: "{client_name}? Це {bot_name}. Ми не знайомі, але я дзвоню щодо [Topic]. Маєте 30 секунд?"
219
- """
220
- elif "Warm" in context or "Теплий" in context:
221
- # ТЕПЛИЙ ЛІД: Клієнт чекає дзвінка.
222
- strategy = """
223
- STRATEGY: WARM LEAD (Inbound Request).
224
- RULES:
225
- 1. Acknowledge their request immediately.
226
- 2. Verify it's a good time to talk.
227
- 3. Tone: Helpful, responsive.
228
-
229
- Example format: "Вітаю, {client_name}! Це {bot_name} з SellMe. Ви залишали заявку на сайті, зручно зараз?"
230
- """
231
- else:
232
- # FOLLOW-UP: Ми вже говорили.
233
- strategy = """
234
- STRATEGY: FOLLOW-UP.
235
- RULES:
236
- 1. Remind who you are immediately.
237
- 2. Reference previous context.
238
- """
239
-
240
  prompt = f"""
241
- ROLE: Professional Sales Rep ({bot_name}).
242
  CLIENT: {client_name} from {company}.
243
- CONTEXT: {strategy}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
 
245
- TASK: Generate the opening phrase based on the instruction: "{start_node_text}".
246
- LANGUAGE: Ukrainian.
 
 
247
  """
248
  try:
249
  return model.generate_content(prompt).text.strip()
250
  except:
251
- return f"{client_name}? Це {bot_name}."
252
 
253
 
254
  # --- UI COMPONENTS ---
 
156
  except:
157
  return {"archetype": "UNKNOWN", "intent": "STAY", "reasoning": "Error"}
158
 
159
+ def generate_response(model, instruction_text, user_input, intent, lead_info, archetype):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
160
  """
161
+ Генерує ЖИВУ відповідь на основі інструкції.
162
  """
163
+ # 1. Формуємо портрет клієнта
164
  bot_name = lead_info.get('bot_name', 'Олексій')
165
  client_name = lead_info.get('name', 'Клієнт')
166
  company = lead_info.get('company', 'Компанія')
167
+ context = lead_info.get('context', 'Cold')
168
 
169
+ # 2. Налаштування стилю (Tone of Voice)
170
+ tone = "Professional, confident."
171
+ if archetype == "DRIVER": tone = "Direct, concise, results-oriented (Time is money)."
172
+ elif archetype == "ANALYST": tone = "Logical, factual, detailed."
173
+ elif archetype == "EXPRESSIVE": tone = "Energetic, inspiring, emotional."
174
+ elif archetype == "CONSERVATIVE": tone = "Calm, supportive, reassuring."
175
+
176
+ # 3. Налаштування довжини (Brevity)
177
+ length_instruction = "Keep it concise."
178
+ if "Cold" in context: length_instruction = "Extremely short and punchy (Elevator Pitch)."
179
+
180
+ # 4. Формуємо Промпт
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
181
  prompt = f"""
182
+ ROLE: You are {bot_name}, a top-tier sales representative at SellMe AI.
183
  CLIENT: {client_name} from {company}.
184
+ CURRENT GOAL (INSTRUCTION): "{instruction_text}"
185
+ USER SAID: "{user_input}"
186
+ INTENT DETECTED: {intent}
187
+ ARCHETYPE: {archetype}
188
+
189
+ TASK: Generate the spoken response in Ukrainian.
190
+
191
+ CRITICAL RULES:
192
+ 1. DO NOT output the instruction itself. ACT IT OUT.
193
+ 2. Adapt to the client's tone ({tone}).
194
+ 3. {length_instruction}
195
+ 4. If INTENT is 'STAY' (Objection): Acknowledge the objection, reframe it, and steer back to the goal.
196
+ 5. If INTENT is 'MOVE': Validate the user's answer and transition smoothly to the goal.
197
+
198
+ OUTPUT: Just the spoken words. No "Option 1", no quotes.
199
+ """
200
+
201
+ try:
202
+ return model.generate_content(prompt).text.strip()
203
+ except Exception as e:
204
+ return f"[System Error: {e}]"
205
+
206
+ def generate_greeting(model, start_instruction, lead_info):
207
+ bot_name = lead_info.get('bot_name', 'Manager')
208
+ client_name = lead_info.get('name', 'Client')
209
+ context = lead_info.get('context', 'Cold')
210
+
211
+ prompt = f"""
212
+ ROLE: Sales Rep {bot_name}.
213
+ CLIENT: {client_name}.
214
+ CONTEXT: {context} call.
215
+ INSTRUCTION: "{start_instruction}"
216
 
217
+ TASK: Generate the opening line.
218
+ - If Cold Call: Be brief, aggressive (pattern interrupt).
219
+ - If Warm Call: Be welcoming, reference the application.
220
+ - Language: Ukrainian.
221
  """
222
  try:
223
  return model.generate_content(prompt).text.strip()
224
  except:
225
+ return f"Алло, {client_name}? Це {bot_name}."
226
 
227
 
228
  # --- UI COMPONENTS ---