Spaces:
Running
Running
Update training_tools.py
Browse files383 unmodified lines3843853863873883893901 unmodified line3923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324331 unmodified line435436437438439440441442443444445446447383 unmodified linesdef apply_correct_ai_correction(customer_question: str,corrected_reply: str,chunk_actions: list[dict[str, str]] | None = None,) -> str:"""Apply one canonical Correct AI answer after reviewing related Qdrant chunks.1 unmodified lineUse this only for the Correct AI workflow. The approved answer is stored asexactly one ai_correction point keyed by the normalized customer question.Use chunk_actions only for point IDs inspected by this workflow's search:- action "update": replace an exact outdated matching chunk with thecanonical correction.- action "retire": remove an exact conflicting duplicate.- action "add": use only when no existing chunk is selected; it creates orupdates the canonical correction without removing another point.Never pass unrelated semantic-search results."""question = clean_text(customer_question)answer = clean_text(corrected_reply)actions_by_id: dict[str, str] = {}has_add_action = Falsefor item in chunk_actions or []:if not isinstance(item, dict):return "Correction failed because each chunk action must include a point_id and action."point_id = clean_text(str(item.get("point_id") or ""))action = clean_text(str(item.get("action") or "")).lower()if action not in {"add", "update", "retire"}:return "Correction failed because chunk actions must be add, update, or retire."if action == "add":if point_id:return "Correction failed because an add action must not include a point_id."has_add_action = Truecontinueif not point_id:return "Correction failed because update and retire actions require a point_id."existing_action = actions_by_id.get(point_id)if existing_action and existing_action != action:return "Correction failed because a point_id cannot be both updated and retired."actions_by_id[point_id] = actionif has_add_action and actions_by_id:return "Correction failed because add cannot be combined with update or retire actions."if not question or not answer:return "Correction failed because the customer question or approved answer was empty."1 unmodified lineensure_collection()correction_key = normalized_correction_key(question)canonical_point_id = stable_point_id(f"ai_correction:{correction_key}")actions_by_id.pop(canonical_point_id, None)update_point_ids = [point_id for point_id, action in actions_by_id.items() if action == "update"]retire_point_ids = [point_id for point_id, action in actions_by_id.items() if action == "retire"]replaced_point_ids = [*update_point_ids, *retire_point_ids]print("CORRECT AI APPLY START: "383 unmodified lines3843853863873883893903911 unmodified line3933943953963973983994004014024034044054064074084094104111 unmodified line413414415416417418419420421422423383 unmodified linesdef apply_correct_ai_correction(customer_question: str,corrected_reply: str,update_point_ids: list[str] | None = None,retire_point_ids: list[str] | None = None,) -> str:"""Apply one canonical Correct AI answer after reviewing related Qdrant chunks.1 unmodified lineUse this only for the Correct AI workflow. The approved answer is stored asexactly one ai_correction point keyed by the normalized customer question.Use these IDs only when they were inspected by this workflow's search:- update_point_ids: exact outdated matching chunks to replace with thecanonical correction.- retire_point_ids: exact conflicting duplicates to remove.- For add, leave both lists empty. This creates or updates the canonicalcorrection without removing another point.Never pass unrelated semantic-search results."""question = clean_text(customer_question)answer = clean_text(corrected_reply)update_point_ids = normalize_point_ids(update_point_ids or [])retire_point_ids = normalize_point_ids(retire_point_ids or [])if not question or not answer:return "Correction failed because the customer question or approved answer was empty."1 unmodified lineensure_collection()correction_key = normalized_correction_key(question)canonical_point_id = stable_point_id(f"ai_correction:{correction_key}")update_point_ids = [point_id for point_id in update_point_ids if point_id != canonical_point_id]retire_point_ids = [point_id for point_id in retire_point_ids if point_id != canonical_point_id]overlapping_point_ids = set(update_point_ids).intersection(retire_point_ids)if overlapping_point_ids:return "Correction failed because a point_id cannot be both updated and retired."replaced_point_ids = [*update_point_ids, *retire_point_ids]print("CORRECT AI APPLY START: "
training_tools.py+14-38