srilakshu012456 commited on
Commit
22ff6d0
·
verified ·
1 Parent(s): 2e68e1d

Update main.py

Browse files
Files changed (1) hide show
  1. main.py +58 -11
main.py CHANGED
@@ -200,6 +200,42 @@ def _get_steps_for_action(best_doc: str, actions: list) -> Optional[str]:
200
  return None
201
 
202
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  # --- Default section picker when query doesn't reveal action ---
204
  def _pick_default_action_section(best_doc: str) -> Optional[str]:
205
  """
@@ -1219,19 +1255,30 @@ async def chat_with_ai(input_data: ChatInput):
1219
  if action_steps:
1220
  full_steps = action_steps
1221
  else:
1222
- # 2) If no user action, pick a sensible default section (creation > update > deletion)
1223
- default_sec = _pick_default_action_section(best_doc)
1224
- if default_sec:
1225
- full_steps = get_section_text(best_doc, default_sec)
1226
- # 3) Last resort: aggregated steps for the doc
 
 
 
 
 
 
 
 
1227
  if not full_steps:
1228
- full_steps = get_best_steps_section_text(best_doc)
1229
- # 4) Final fallback: top hit section
 
1230
  if not full_steps:
1231
- sec = (top_meta or {}).get("section")
1232
- if sec:
1233
- full_steps = get_section_text(best_doc, sec)
1234
-
 
 
1235
  if full_steps:
1236
  # Always add Save lines if present anywhere in the doc (independent of query wording)
1237
  save_lines = _find_save_lines_in_doc(best_doc, max_lines=2)
 
200
  return None
201
 
202
 
203
+ def _get_steps_for_action_global(actions: list, prefer_doc: Optional[str] = None) -> Tuple[Optional[str], Optional[str]]:
204
+ """
205
+ Search ALL SOP docs for a 'steps' section whose action_tag matches one of `actions`.
206
+ Returns (doc_name, steps_text). Prefers sections from `prefer_doc` and the 'appointments' module.
207
+ """
208
+ if not actions:
209
+ return None, None
210
+ act_set = set(a.strip().lower() for a in actions if a)
211
+
212
+ candidates: List[Tuple[float, str, str]] = [] # (score, doc, text)
213
+ for d in bm25_docs:
214
+ m = d.get("meta", {}) or {}
215
+ if m.get("intent_tag") != "steps":
216
+ continue
217
+ tag = ((m.get("action_tag") or "").strip().lower())
218
+ if tag and tag in act_set:
219
+ doc = m.get("filename")
220
+ sec = (m.get("section") or "").strip()
221
+ txt = get_section_text(doc, sec)
222
+ if not txt or not txt.strip():
223
+ continue
224
+ score = 1.0
225
+ if prefer_doc and doc == prefer_doc:
226
+ score += 0.5
227
+ mtags = (m.get("module_tags") or "").lower()
228
+ if "appointments" in mtags:
229
+ score += 0.3
230
+ candidates.append((score, doc, txt.strip()))
231
+
232
+ if not candidates:
233
+ return None, None
234
+
235
+ candidates.sort(key=lambda x: x[0], reverse=True)
236
+ _, best_doc_global, best_text = candidates[0]
237
+ return best_doc_global, best_text
238
+
239
  # --- Default section picker when query doesn't reveal action ---
240
  def _pick_default_action_section(best_doc: str) -> Optional[str]:
241
  """
 
1255
  if action_steps:
1256
  full_steps = action_steps
1257
  else:
1258
+ if kb_results.get("actions"):
1259
+ alt_doc, alt_steps = _get_steps_for_action_global(kb_results["actions"], prefer_doc=best_doc)
1260
+ if alt_steps:
1261
+ best_doc = alt_doc # switch to the doc that actually has the section
1262
+ full_steps = alt_steps
1263
+
1264
+ asked_action = _detect_action_from_query(input_data.user_message)
1265
+ if not kb_results.get("actions") and asked_action:
1266
+ alt_doc, alt_steps = _get_steps_for_action_global([asked_action], prefer_doc=best_doc)
1267
+ if alt_steps:
1268
+ best_doc = alt_doc # switch to the doc that actually has the section
1269
+ full_steps = alt_steps
1270
+
1271
  if not full_steps:
1272
+ default_sec = _pick_default_action_section(best_doc)
1273
+ if default_sec:
1274
+ full_steps = get_section_text(best_doc, default_sec)
1275
  if not full_steps:
1276
+ full_steps = get_best_steps_section_text(best_doc)
1277
+ if not full_steps:
1278
+ sec = (top_meta or {}).get("section")
1279
+ if sec:
1280
+ full_steps = get_section_text(best_doc, sec)
1281
+
1282
  if full_steps:
1283
  # Always add Save lines if present anywhere in the doc (independent of query wording)
1284
  save_lines = _find_save_lines_in_doc(best_doc, max_lines=2)