BasilTh
commited on
Commit
·
cd7eb0b
1
Parent(s):
7ceb07f
Deploy updated SLM customer-support chatbot
Browse files- SLM_CService.py +36 -28
SLM_CService.py
CHANGED
@@ -126,17 +126,14 @@ ALLOWED_KEYWORDS = (
|
|
126 |
)
|
127 |
|
128 |
# Robust order detection:
|
129 |
-
# - "#67890" / "# 67890"
|
130 |
-
# - "order 67890", "order no. 67890", "order number 67890", "order id 67890"
|
131 |
ORDER_RX = re.compile(
|
132 |
-
r"(?:#\s*(\d{3,12})|order(?:\s*(?:no\.?|number|id))?\s*#?\s*(\d{3,12}))",
|
133 |
flags=re.I,
|
134 |
)
|
135 |
-
|
136 |
def extract_order(text: str):
|
137 |
-
|
138 |
-
|
139 |
-
return m.group(1) or m.group(2)
|
140 |
|
141 |
def handle_status(o): return f"Order #{o} is in transit and should arrive in 3–5 business days."
|
142 |
def handle_eta(o): return f"Delivery for order #{o} typically takes 3–5 days; you can track it at https://track.example.com/{o}"
|
@@ -147,7 +144,7 @@ def handle_return_policy(_=None):
|
|
147 |
"Would you like me to connect you with a human agent?")
|
148 |
def handle_warranty_policy(_=None):
|
149 |
return ("We provide a 1-year limited warranty against manufacturing defects. "
|
150 |
-
"
|
151 |
"Need help starting a claim?")
|
152 |
def handle_cancel(o=None):
|
153 |
return (f"I’ve submitted a cancellation request for order #{o}. If it has already shipped, "
|
@@ -159,7 +156,6 @@ def handle_ask_action(o): return (f"I’ve saved order #{o}. What would you like
|
|
159 |
stored_order = None
|
160 |
pending_intent = None
|
161 |
|
162 |
-
# public reset hook (called from app.py)
|
163 |
def reset_state():
|
164 |
global stored_order, pending_intent
|
165 |
stored_order = None
|
@@ -216,28 +212,37 @@ def chat_with_memory(user_input: str) -> str:
|
|
216 |
memory.save_context({"input": ui}, {"output": reply})
|
217 |
return reply
|
218 |
|
219 |
-
# 3)
|
220 |
new_o = extract_order(ui)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
221 |
if new_o:
|
222 |
stored_order = new_o
|
223 |
-
if pending_intent in ("status","eta","track","link","cancel"):
|
224 |
-
fn = {"status": handle_status,"eta": handle_eta,"track": handle_track,
|
225 |
-
"link": handle_link,"cancel": handle_cancel}[pending_intent]
|
226 |
-
reply = fn(stored_order); pending_intent = None
|
227 |
-
memory.save_context({"input": ui}, {"output": reply}); return reply
|
228 |
-
# no pending intent → ask what they want to do with this order
|
229 |
reply = handle_ask_action(stored_order)
|
230 |
memory.save_context({"input": ui}, {"output": reply})
|
231 |
return reply
|
232 |
|
233 |
-
#
|
234 |
-
if
|
235 |
-
|
236 |
-
|
237 |
-
|
238 |
-
return reply
|
239 |
|
240 |
-
#
|
241 |
if any(k in low for k in ["status","where is my order","check status"]):
|
242 |
intent = "status"
|
243 |
elif any(k in low for k in ["how long","eta","delivery time"]):
|
@@ -255,7 +260,7 @@ def chat_with_memory(user_input: str) -> str:
|
|
255 |
else:
|
256 |
intent = "fallback"
|
257 |
|
258 |
-
#
|
259 |
if intent in ("status","eta","track","link","cancel"):
|
260 |
if not stored_order:
|
261 |
pending_intent = intent
|
@@ -264,17 +269,20 @@ def chat_with_memory(user_input: str) -> str:
|
|
264 |
fn = {"status": handle_status,"eta": handle_eta,"track": handle_track,
|
265 |
"link": handle_link,"cancel": handle_cancel}[intent]
|
266 |
reply = fn(stored_order)
|
267 |
-
memory.save_context({"input": ui}, {"output": reply})
|
|
|
268 |
|
269 |
if intent == "warranty_policy":
|
270 |
reply = handle_warranty_policy()
|
271 |
-
memory.save_context({"input": ui}, {"output": reply})
|
|
|
272 |
|
273 |
if intent == "return_policy":
|
274 |
reply = handle_return_policy()
|
275 |
-
memory.save_context({"input": ui}, {"output": reply})
|
|
|
276 |
|
277 |
-
#
|
278 |
reply = _generate_reply(ui)
|
279 |
if is_sexual_or_toxic(reply): reply = REFUSAL
|
280 |
memory.save_context({"input": ui}, {"output": reply})
|
|
|
126 |
)
|
127 |
|
128 |
# Robust order detection:
|
|
|
|
|
129 |
ORDER_RX = re.compile(
|
130 |
+
r"(?:#\s*([\d]{3,12})|order(?:\s*(?:no\.?|number|id))?\s*#?\s*([\d]{3,12}))",
|
131 |
flags=re.I,
|
132 |
)
|
|
|
133 |
def extract_order(text: str):
|
134 |
+
if not text: return None
|
135 |
+
m = ORDER_RX.search(text)
|
136 |
+
return (m.group(1) or m.group(2)) if m else None
|
137 |
|
138 |
def handle_status(o): return f"Order #{o} is in transit and should arrive in 3–5 business days."
|
139 |
def handle_eta(o): return f"Delivery for order #{o} typically takes 3–5 days; you can track it at https://track.example.com/{o}"
|
|
|
144 |
"Would you like me to connect you with a human agent?")
|
145 |
def handle_warranty_policy(_=None):
|
146 |
return ("We provide a 1-year limited warranty against manufacturing defects. "
|
147 |
+
"Within 30 days you can return or exchange; afterwards, warranty service applies. "
|
148 |
"Need help starting a claim?")
|
149 |
def handle_cancel(o=None):
|
150 |
return (f"I’ve submitted a cancellation request for order #{o}. If it has already shipped, "
|
|
|
156 |
stored_order = None
|
157 |
pending_intent = None
|
158 |
|
|
|
159 |
def reset_state():
|
160 |
global stored_order, pending_intent
|
161 |
stored_order = None
|
|
|
212 |
memory.save_context({"input": ui}, {"output": reply})
|
213 |
return reply
|
214 |
|
215 |
+
# 3) PENDING-INTENT SHORT-CIRCUIT (fix for "It's #26790" case)
|
216 |
new_o = extract_order(ui)
|
217 |
+
if pending_intent:
|
218 |
+
if new_o:
|
219 |
+
stored_order = new_o
|
220 |
+
fn = {"status": handle_status, "eta": handle_eta,
|
221 |
+
"track": handle_track, "link": handle_link,
|
222 |
+
"cancel": handle_cancel}[pending_intent]
|
223 |
+
reply = fn(stored_order)
|
224 |
+
pending_intent = None
|
225 |
+
memory.save_context({"input": ui}, {"output": reply})
|
226 |
+
return reply
|
227 |
+
# still waiting for an order number
|
228 |
+
reply = "Got it—please share your order number (e.g., #12345)."
|
229 |
+
memory.save_context({"input": ui}, {"output": reply})
|
230 |
+
return reply
|
231 |
+
|
232 |
+
# 4) If message provides an order number (no pending intent yet), save it & ask action
|
233 |
if new_o:
|
234 |
stored_order = new_o
|
|
|
|
|
|
|
|
|
|
|
|
|
235 |
reply = handle_ask_action(stored_order)
|
236 |
memory.save_context({"input": ui}, {"output": reply})
|
237 |
return reply
|
238 |
|
239 |
+
# 5) Support-only guard (message must be support-ish)
|
240 |
+
if not any(k in low for k in ALLOWED_KEYWORDS) and not any(k in low for k in ("hi","hello","hey")):
|
241 |
+
reply = "I’m for store support only (orders, shipping, returns, warranty, account). How can I help with those?"
|
242 |
+
memory.save_context({"input": ui}, {"output": reply})
|
243 |
+
return reply
|
|
|
244 |
|
245 |
+
# 6) Intent classification
|
246 |
if any(k in low for k in ["status","where is my order","check status"]):
|
247 |
intent = "status"
|
248 |
elif any(k in low for k in ["how long","eta","delivery time"]):
|
|
|
260 |
else:
|
261 |
intent = "fallback"
|
262 |
|
263 |
+
# 7) Handle intents
|
264 |
if intent in ("status","eta","track","link","cancel"):
|
265 |
if not stored_order:
|
266 |
pending_intent = intent
|
|
|
269 |
fn = {"status": handle_status,"eta": handle_eta,"track": handle_track,
|
270 |
"link": handle_link,"cancel": handle_cancel}[intent]
|
271 |
reply = fn(stored_order)
|
272 |
+
memory.save_context({"input": ui}, {"output": reply})
|
273 |
+
return reply
|
274 |
|
275 |
if intent == "warranty_policy":
|
276 |
reply = handle_warranty_policy()
|
277 |
+
memory.save_context({"input": ui}, {"output": reply})
|
278 |
+
return reply
|
279 |
|
280 |
if intent == "return_policy":
|
281 |
reply = handle_return_policy()
|
282 |
+
memory.save_context({"input": ui}, {"output": reply})
|
283 |
+
return reply
|
284 |
|
285 |
+
# 8) LLM fallback (on-topic) + post-check
|
286 |
reply = _generate_reply(ui)
|
287 |
if is_sexual_or_toxic(reply): reply = REFUSAL
|
288 |
memory.save_context({"input": ui}, {"output": reply})
|