Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -176,6 +176,29 @@ def _get_steps_for_action(best_doc: str, actions: list) -> Optional[str]:
|
|
| 176 |
return txt.strip()
|
| 177 |
return None
|
| 178 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 179 |
def _get_steps_for_action_global(actions: list, prefer_doc: Optional[str] = None) -> Tuple[Optional[str], Optional[str]]:
|
| 180 |
if not actions:
|
| 181 |
return None, None
|
|
@@ -205,11 +228,26 @@ def _get_steps_for_action_global(actions: list, prefer_doc: Optional[str] = None
|
|
| 205 |
_, best_doc_global, best_text = candidates[0]
|
| 206 |
return best_doc_global, best_text
|
| 207 |
|
| 208 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 209 |
order = ("creation", "updation", "update", "deletion", "delete", "cancel")
|
| 210 |
sections = []
|
| 211 |
for d in bm25_docs:
|
| 212 |
-
m = d.get("meta", {})
|
| 213 |
if m.get("filename") == best_doc and m.get("intent_tag") == "steps":
|
| 214 |
title = (m.get("section") or "").strip().lower()
|
| 215 |
if title:
|
|
@@ -394,7 +432,7 @@ def _detect_next_intent(user_query: str) -> bool:
|
|
| 394 |
]
|
| 395 |
return any(k in q for k in keys)
|
| 396 |
|
| 397 |
-
def _resolve_next_steps(user_query: str, numbered_text: str, max_next: int = 6, min_score: float = 0.
|
| 398 |
"""
|
| 399 |
Smarter anchor: verb overlap boost + keyword anchors.
|
| 400 |
"""
|
|
@@ -1065,6 +1103,9 @@ async def chat_with_ai(input_data: ChatInput):
|
|
| 1065 |
context_preformatted = False
|
| 1066 |
full_steps = None
|
| 1067 |
|
|
|
|
|
|
|
|
|
|
| 1068 |
action_steps = _get_steps_for_action(best_doc, kb_results.get("actions", []))
|
| 1069 |
if action_steps:
|
| 1070 |
full_steps = action_steps
|
|
@@ -1074,8 +1115,16 @@ async def chat_with_ai(input_data: ChatInput):
|
|
| 1074 |
if alt_steps:
|
| 1075 |
best_doc = alt_doc
|
| 1076 |
full_steps = alt_steps
|
| 1077 |
-
|
| 1078 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1079 |
if not full_steps and asked_action:
|
| 1080 |
exact_action_steps = _get_steps_text_for_action(best_doc, asked_action)
|
| 1081 |
if exact_action_steps:
|
|
@@ -1125,10 +1174,9 @@ async def chat_with_ai(input_data: ChatInput):
|
|
| 1125 |
else:
|
| 1126 |
context = full_steps
|
| 1127 |
context_preformatted = False
|
| 1128 |
-
|
| 1129 |
-
filt_info = {'mode': None, 'matched_count': None, 'all_sentences': None}
|
| 1130 |
context_found = True
|
| 1131 |
-
|
|
|
|
| 1132 |
elif best_doc and detected_intent == "errors":
|
| 1133 |
full_errors = get_best_errors_section_text(best_doc)
|
| 1134 |
if full_errors:
|
|
|
|
| 176 |
return txt.strip()
|
| 177 |
return None
|
| 178 |
|
| 179 |
+
def _get_steps_by_action_tag(best_doc: str, action: Optional[str]) -> Optional[str]:
|
| 180 |
+
"""
|
| 181 |
+
Generic: return the section text from `best_doc` whose meta.action_tag == action.
|
| 182 |
+
No hard-coded SOP words; relies purely on KB metadata.
|
| 183 |
+
"""
|
| 184 |
+
if not best_doc or not action:
|
| 185 |
+
return None
|
| 186 |
+
target = (action or "").strip().lower()
|
| 187 |
+
for d in bm25_docs:
|
| 188 |
+
m = d.get("meta", {}) or {}
|
| 189 |
+
if m.get("intent_tag") != "steps":
|
| 190 |
+
continue
|
| 191 |
+
if (m.get("filename") or "") != best_doc:
|
| 192 |
+
continue
|
| 193 |
+
tag = (m.get("action_tag") or "").strip().lower()
|
| 194 |
+
if tag and tag == target:
|
| 195 |
+
sec = (m.get("section") or "").strip()
|
| 196 |
+
if sec:
|
| 197 |
+
txt = get_section_text(best_doc, sec)
|
| 198 |
+
if txt and txt.strip():
|
| 199 |
+
return txt.strip()
|
| 200 |
+
return None
|
| 201 |
+
|
| 202 |
def _get_steps_for_action_global(actions: list, prefer_doc: Optional[str] = None) -> Tuple[Optional[str], Optional[str]]:
|
| 203 |
if not actions:
|
| 204 |
return None, None
|
|
|
|
| 228 |
_, best_doc_global, best_text = candidates[0]
|
| 229 |
return best_doc_global, best_text
|
| 230 |
|
| 231 |
+
def _pick_default_action_section_with_preference(best_doc: str, prefer_action: Optional[str]) -> Optional[str]:
|
| 232 |
+
"""
|
| 233 |
+
Generic: if there are sections tagged with the requested action (via action_tag),
|
| 234 |
+
prefer them; otherwise fall back to your original order.
|
| 235 |
+
"""
|
| 236 |
+
# 1) Prefer exact action_tag match
|
| 237 |
+
if prefer_action:
|
| 238 |
+
for d in bm25_docs:
|
| 239 |
+
m = d.get("meta", {}) or {}
|
| 240 |
+
if m.get("filename") == best_doc and m.get("intent_tag") == "steps":
|
| 241 |
+
tag = (m.get("action_tag") or "").strip().lower()
|
| 242 |
+
sec = (m.get("section") or "").strip()
|
| 243 |
+
if tag == prefer_action.lower() and sec:
|
| 244 |
+
return sec
|
| 245 |
+
|
| 246 |
+
# 2) Fall back to your original generic order (no SOP-specific words)
|
| 247 |
order = ("creation", "updation", "update", "deletion", "delete", "cancel")
|
| 248 |
sections = []
|
| 249 |
for d in bm25_docs:
|
| 250 |
+
m = d.get("meta", {}) or {}
|
| 251 |
if m.get("filename") == best_doc and m.get("intent_tag") == "steps":
|
| 252 |
title = (m.get("section") or "").strip().lower()
|
| 253 |
if title:
|
|
|
|
| 432 |
]
|
| 433 |
return any(k in q for k in keys)
|
| 434 |
|
| 435 |
+
def _resolve_next_steps(user_query: str, numbered_text: str, max_next: int = 6, min_score: float = 0.30):
|
| 436 |
"""
|
| 437 |
Smarter anchor: verb overlap boost + keyword anchors.
|
| 438 |
"""
|
|
|
|
| 1103 |
context_preformatted = False
|
| 1104 |
full_steps = None
|
| 1105 |
|
| 1106 |
+
# Detect asked action generically using ACTION_SYNONYMS_EXT (already defined)
|
| 1107 |
+
asked_action = _detect_action_from_query(input_data.user_message) # returns 'create'/'update'/'delete'/None
|
| 1108 |
+
|
| 1109 |
action_steps = _get_steps_for_action(best_doc, kb_results.get("actions", []))
|
| 1110 |
if action_steps:
|
| 1111 |
full_steps = action_steps
|
|
|
|
| 1115 |
if alt_steps:
|
| 1116 |
best_doc = alt_doc
|
| 1117 |
full_steps = alt_steps
|
| 1118 |
+
|
| 1119 |
+
if not full_steps and asked_action:
|
| 1120 |
+
via_tag_steps = _get_steps_by_action_tag(best_doc, asked_action)
|
| 1121 |
+
if via_tag_steps:
|
| 1122 |
+
full_steps = via_tag_steps
|
| 1123 |
+
if not full_steps:
|
| 1124 |
+
default_sec = _pick_default_action_section_with_preference(best_doc, asked_action)
|
| 1125 |
+
if default_sec:
|
| 1126 |
+
full_steps = get_section_text(best_doc, default_sec)
|
| 1127 |
+
|
| 1128 |
if not full_steps and asked_action:
|
| 1129 |
exact_action_steps = _get_steps_text_for_action(best_doc, asked_action)
|
| 1130 |
if exact_action_steps:
|
|
|
|
| 1174 |
else:
|
| 1175 |
context = full_steps
|
| 1176 |
context_preformatted = False
|
|
|
|
|
|
|
| 1177 |
context_found = True
|
| 1178 |
+
filt_info = {'mode': None, 'matched_count': None, 'all_sentences': None}
|
| 1179 |
+
|
| 1180 |
elif best_doc and detected_intent == "errors":
|
| 1181 |
full_errors = get_best_errors_section_text(best_doc)
|
| 1182 |
if full_errors:
|