Spaces:
Sleeping
Sleeping
Update training_tools.py
Browse files- training_tools.py +14 -38
training_tools.py
CHANGED
|
@@ -384,7 +384,8 @@ def get_correct_ai_correction(customer_question: str) -> dict[str, Any] | None:
|
|
| 384 |
def apply_correct_ai_correction(
|
| 385 |
customer_question: str,
|
| 386 |
corrected_reply: str,
|
| 387 |
-
|
|
|
|
| 388 |
) -> str:
|
| 389 |
"""
|
| 390 |
Apply one canonical Correct AI answer after reviewing related Qdrant chunks.
|
|
@@ -392,42 +393,19 @@ def apply_correct_ai_correction(
|
|
| 392 |
Use this only for the Correct AI workflow. The approved answer is stored as
|
| 393 |
exactly one ai_correction point keyed by the normalized customer question.
|
| 394 |
|
| 395 |
-
Use
|
| 396 |
-
-
|
| 397 |
canonical correction.
|
| 398 |
-
-
|
| 399 |
-
-
|
| 400 |
-
|
| 401 |
|
| 402 |
Never pass unrelated semantic-search results.
|
| 403 |
"""
|
| 404 |
question = clean_text(customer_question)
|
| 405 |
answer = clean_text(corrected_reply)
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
for item in chunk_actions or []:
|
| 409 |
-
if not isinstance(item, dict):
|
| 410 |
-
return "Correction failed because each chunk action must include a point_id and action."
|
| 411 |
-
|
| 412 |
-
point_id = clean_text(str(item.get("point_id") or ""))
|
| 413 |
-
action = clean_text(str(item.get("action") or "")).lower()
|
| 414 |
-
if action not in {"add", "update", "retire"}:
|
| 415 |
-
return "Correction failed because chunk actions must be add, update, or retire."
|
| 416 |
-
if action == "add":
|
| 417 |
-
if point_id:
|
| 418 |
-
return "Correction failed because an add action must not include a point_id."
|
| 419 |
-
has_add_action = True
|
| 420 |
-
continue
|
| 421 |
-
if not point_id:
|
| 422 |
-
return "Correction failed because update and retire actions require a point_id."
|
| 423 |
-
|
| 424 |
-
existing_action = actions_by_id.get(point_id)
|
| 425 |
-
if existing_action and existing_action != action:
|
| 426 |
-
return "Correction failed because a point_id cannot be both updated and retired."
|
| 427 |
-
actions_by_id[point_id] = action
|
| 428 |
-
|
| 429 |
-
if has_add_action and actions_by_id:
|
| 430 |
-
return "Correction failed because add cannot be combined with update or retire actions."
|
| 431 |
|
| 432 |
if not question or not answer:
|
| 433 |
return "Correction failed because the customer question or approved answer was empty."
|
|
@@ -435,13 +413,11 @@ def apply_correct_ai_correction(
|
|
| 435 |
ensure_collection()
|
| 436 |
correction_key = normalized_correction_key(question)
|
| 437 |
canonical_point_id = stable_point_id(f"ai_correction:{correction_key}")
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
point_id for point_id, action in actions_by_id.items() if action == "retire"
|
| 444 |
-
]
|
| 445 |
replaced_point_ids = [*update_point_ids, *retire_point_ids]
|
| 446 |
print(
|
| 447 |
"CORRECT AI APPLY START: "
|
|
|
|
| 384 |
def apply_correct_ai_correction(
|
| 385 |
customer_question: str,
|
| 386 |
corrected_reply: str,
|
| 387 |
+
update_point_ids: list[str] | None = None,
|
| 388 |
+
retire_point_ids: list[str] | None = None,
|
| 389 |
) -> str:
|
| 390 |
"""
|
| 391 |
Apply one canonical Correct AI answer after reviewing related Qdrant chunks.
|
|
|
|
| 393 |
Use this only for the Correct AI workflow. The approved answer is stored as
|
| 394 |
exactly one ai_correction point keyed by the normalized customer question.
|
| 395 |
|
| 396 |
+
Use these IDs only when they were inspected by this workflow's search:
|
| 397 |
+
- update_point_ids: exact outdated matching chunks to replace with the
|
| 398 |
canonical correction.
|
| 399 |
+
- retire_point_ids: exact conflicting duplicates to remove.
|
| 400 |
+
- For add, leave both lists empty. This creates or updates the canonical
|
| 401 |
+
correction without removing another point.
|
| 402 |
|
| 403 |
Never pass unrelated semantic-search results.
|
| 404 |
"""
|
| 405 |
question = clean_text(customer_question)
|
| 406 |
answer = clean_text(corrected_reply)
|
| 407 |
+
update_point_ids = normalize_point_ids(update_point_ids or [])
|
| 408 |
+
retire_point_ids = normalize_point_ids(retire_point_ids or [])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
|
| 410 |
if not question or not answer:
|
| 411 |
return "Correction failed because the customer question or approved answer was empty."
|
|
|
|
| 413 |
ensure_collection()
|
| 414 |
correction_key = normalized_correction_key(question)
|
| 415 |
canonical_point_id = stable_point_id(f"ai_correction:{correction_key}")
|
| 416 |
+
update_point_ids = [point_id for point_id in update_point_ids if point_id != canonical_point_id]
|
| 417 |
+
retire_point_ids = [point_id for point_id in retire_point_ids if point_id != canonical_point_id]
|
| 418 |
+
overlapping_point_ids = set(update_point_ids).intersection(retire_point_ids)
|
| 419 |
+
if overlapping_point_ids:
|
| 420 |
+
return "Correction failed because a point_id cannot be both updated and retired."
|
|
|
|
|
|
|
| 421 |
replaced_point_ids = [*update_point_ids, *retire_point_ids]
|
| 422 |
print(
|
| 423 |
"CORRECT AI APPLY START: "
|