Pair black and bright defect evidence for priority products
This commit is contained in:
@@ -259,18 +259,38 @@ def synthesize(*, base_url: str, api_key: str, model: str, question: str,
|
||||
request = Request(base_url.rstrip('/') + '/chat/completions',
|
||||
data=json.dumps(body, ensure_ascii=False).encode(),
|
||||
headers={'Content-Type': 'application/json', 'Authorization': 'Bearer ' + api_key}, method='POST')
|
||||
def verified_fallback():
|
||||
# Focus selection is presentation-only. If its model response is malformed,
|
||||
# render the already verified facts with a deterministic default selection.
|
||||
# Never promote missing server evidence to a complete answer.
|
||||
selected = [candidates[0]['id'] for candidates in pools.values() if candidates]
|
||||
missing = list(dict.fromkeys(report.get('missingEvidence', [])))[:10]
|
||||
return {'answer':render_report(report,selected,missing),
|
||||
'evidenceSufficient':not missing,'missingEvidence':missing,
|
||||
'modelDeclaredEvidenceSufficient':None,'completionAdjusted':True,
|
||||
'model':model,'usage':{},'finishReason':'selection_fallback',
|
||||
'selectedFactIds':selected,'factCount':len(report['facts']),
|
||||
'selectionAdjusted':True,'selectionFallback':True}
|
||||
try:
|
||||
with (opener or _open_model)(request, timeout=FINAL_TIMEOUT_SECONDS) as response:
|
||||
raw = response.read(2 * 1024 * 1024 + 1)
|
||||
if cancel_event.is_set(): raise FinalizationUnavailable('已取消')
|
||||
if len(raw) > 2 * 1024 * 1024: raise ValueError('oversize')
|
||||
document = json.loads(raw)
|
||||
try:
|
||||
document = json.loads(raw)
|
||||
except (ValueError, TypeError):
|
||||
return verified_fallback()
|
||||
if not isinstance(document,dict):return verified_fallback()
|
||||
if document.get('model') not in (None, model): raise ValueError('model changed')
|
||||
choice = document['choices'][0]
|
||||
answer = choice['message']['content']
|
||||
if choice.get('finish_reason') != 'stop' or not isinstance(answer, str) or not answer.strip():
|
||||
raise ValueError('incomplete final answer')
|
||||
parsed=json.loads(answer)
|
||||
try:
|
||||
choice = document['choices'][0]
|
||||
answer = choice['message']['content']
|
||||
if choice.get('finish_reason') != 'stop' or not isinstance(answer, str) or not answer.strip():
|
||||
return verified_fallback()
|
||||
parsed=json.loads(answer)
|
||||
except (ValueError, TypeError, KeyError, IndexError, AttributeError):
|
||||
return verified_fallback()
|
||||
if not isinstance(parsed,dict):return verified_fallback()
|
||||
selected=[]
|
||||
selection_adjusted=False
|
||||
for field,candidates in pools.items():
|
||||
@@ -283,7 +303,7 @@ def synthesize(*, base_url: str, api_key: str, model: str, question: str,
|
||||
selected.extend(valid)
|
||||
sufficient=parsed.get('evidenceSufficient');missing=parsed.get('missingEvidence')
|
||||
if (not isinstance(sufficient,bool) or not isinstance(missing,list)
|
||||
or any(not isinstance(x,str) for x in missing)):raise ValueError('invalid evidence completion')
|
||||
or any(not isinstance(x,str) for x in missing)):return verified_fallback()
|
||||
missing=list(dict.fromkeys(item.strip() for item in missing if item.strip()))[:10]
|
||||
# 模型的孤立 false 不能证明数据缺失;具体缺项仍阻止完成,调用方另行检查服务端覆盖。
|
||||
completion_adjusted = not sufficient and not missing
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
"""Plan missing black/bright source-defect queries for the same product scope.
|
||||
|
||||
Only current-turn, release-bound evidence is passed in by the runtime. This
|
||||
module never estimates a missing stage or reuses another product's evidence.
|
||||
"""
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _single_stage_requested(question: str, context: Any) -> bool:
|
||||
if isinstance(context, dict) and context.get("stage") in ("black", "bright"):
|
||||
return True
|
||||
text = question if isinstance(question, str) else ""
|
||||
if re.search(r"(?:只|仅)(?:看|查|分析|要|关注)?(?:黑片|亮片)", text):
|
||||
return True
|
||||
stages = [stage for label, stage in (("黑片", "black"), ("亮片", "bright")) if label in text]
|
||||
return len(stages) == 1 and not re.search(r"整体|总体|综合|全部|两阶段|重点产品|重点规格", text)
|
||||
|
||||
|
||||
def plan_missing_product_defect_stages(
|
||||
documents: list[dict[str, Any]], question: str, context: Any = None,
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Return the missing counterpart queries; identical scope/period is required."""
|
||||
if _single_stage_requested(question, context):
|
||||
return []
|
||||
groups: dict[str, dict[str, Any]] = {}
|
||||
for document in documents:
|
||||
result = document.get("result") if isinstance(document, dict) else None
|
||||
if not isinstance(result, dict) or result.get("kind") != "quality_fine_defects":
|
||||
continue
|
||||
if result.get("dimension") != "defect" or result.get("stage") not in ("black", "bright"):
|
||||
continue
|
||||
filters = result.get("filters")
|
||||
product = filters.get("product") if isinstance(filters, dict) else None
|
||||
if not isinstance(product, str) or not product.strip():
|
||||
continue
|
||||
if result.get("category") is not None or result.get("defect") is not None:
|
||||
continue
|
||||
release = result.get("releaseId")
|
||||
period = result.get("period")
|
||||
comparison = result.get("comparisonPeriod")
|
||||
if (not isinstance(release, str) or not release
|
||||
or not isinstance(period, dict) or (comparison is not None and not isinstance(comparison, dict))):
|
||||
continue
|
||||
sort = result.get("sort")
|
||||
if sort not in ("scrap_delta", "current_scrap"):
|
||||
continue
|
||||
identity = json.dumps([release, period, comparison, filters, sort], ensure_ascii=False, sort_keys=True)
|
||||
group = groups.setdefault(identity, {"stages": set(), "source": result})
|
||||
group["stages"].add(result["stage"])
|
||||
|
||||
queries = []
|
||||
for group in groups.values():
|
||||
if group["stages"] == {"black", "bright"}:
|
||||
continue
|
||||
source = group["source"]
|
||||
other = "bright" if "black" in group["stages"] else "black"
|
||||
args = {"mode": "defects", "releaseId": source["releaseId"],
|
||||
"period": source["period"], "filters": source["filters"],
|
||||
"dimension": "defect", "stage": other, "sort": source["sort"], "limit": 20}
|
||||
if source.get("comparisonPeriod") is not None:
|
||||
args["comparisonPeriod"] = source["comparisonPeriod"]
|
||||
queries.append(args)
|
||||
return queries
|
||||
@@ -1980,6 +1980,49 @@ def run_turn(payload: dict[str, Any], *, stdout: Any = sys.stdout, stdin: Any =
|
||||
actor_root=actor_root, cancel_event=cancel_event,
|
||||
timeout=480 if model_name == "mimo-v2.6-flash" else TURN_TIMEOUT_SECONDS,
|
||||
)
|
||||
# The model may investigate one stage of a priority product and forget
|
||||
# the other. Complete the same release/period/filter pair before the
|
||||
# evidence-based answer and charts are assembled. Never borrow a result
|
||||
# from another turn or infer a missing stage from the first one.
|
||||
pairable_stop = status in ("COMPLETED", "PARTIAL") or (
|
||||
status == "FAILED" and stop_reason in ("length", "timeout", "超时停止")
|
||||
and stop_succeeded is True)
|
||||
if pairable_stop and not cancel_event.is_set():
|
||||
try:
|
||||
from analysis_visual_evidence import load_visual_evidence
|
||||
from nao_product_defect_pairing import plan_missing_product_defect_stages
|
||||
from nao_quality_tools import QualityTools
|
||||
documents = load_visual_evidence(actor_root / "evidence", request["releaseId"],
|
||||
exclude_files=prior_evidence_files, budget_seconds=2.0)
|
||||
missing_stages = plan_missing_product_defect_stages(
|
||||
documents, request["question"], request["context"])
|
||||
if missing_stages:
|
||||
pairing_api = DelegatedApi(access_path, runtime_root=manager.users_root,
|
||||
expected_actor_id=request["actorId"],
|
||||
expected_session_id=request["sessionId"], timeout=5).login()
|
||||
quality_tools = QualityTools(api_factory=lambda: pairing_api,
|
||||
evidence_dir=actor_root / "evidence")
|
||||
for arguments in missing_stages:
|
||||
if cancel_event.is_set():
|
||||
break
|
||||
stage_name = "黑片" if arguments["stage"] == "black" else "亮片"
|
||||
emit({"type": "progress", "message": f"正在补查同规格的{stage_name}具体缺陷…"})
|
||||
try:
|
||||
paired = quality_tools.quality_data(arguments)
|
||||
except (ValueError, OSError, RuntimeError):
|
||||
emit({"type": "progress", "message": f"{stage_name}具体缺陷补查未成功,结论将保留该阶段的数据限制。"})
|
||||
continue
|
||||
call = {"tool": "quality_data", "toolCallId": "pair-" + paired["evidenceRef"],
|
||||
"label": f"补查{stage_name}具体缺陷", "status": "completed",
|
||||
"startedAt": datetime.now(timezone.utc).isoformat(),
|
||||
"finishedAt": datetime.now(timezone.utc).isoformat(),
|
||||
"outputSummary": f"同规格、同期间补查完成;依据 {paired['evidenceRef']}"}
|
||||
accumulator.tool_calls.append(call)
|
||||
accumulator.record_event("tool_result", tool=call["tool"],
|
||||
toolCallId=call["toolCallId"], label=call["label"],
|
||||
status="completed", text=call["outputSummary"])
|
||||
except (ValueError, OSError, RuntimeError):
|
||||
emit({"type": "progress", "message": "阶段配对检查未完成,已有查询结果仍按原范围呈现。"})
|
||||
from nao_evidence_finalizer import eligible, evidence_summaries, synthesize, coverage_sufficient, FinalizationUnavailable
|
||||
finalized = False
|
||||
evidence_completion = None
|
||||
|
||||
Reference in New Issue
Block a user