feat(flywheel): few-shot 在线检索注入打通风险规则编译链路
- 新增 FewShotStore:独立 Qdrant collection few_shot_samples,向量 upsert/search/delete, 全程失败降级不阻塞主链路 - 新增 FewShotIngestionService:RiskObservation confirmed/false_positive → FewShotSample + 向量,带 sample_key 幂等去重 - 新增 FewShotRetriever:按 case 特征检索相似历史样本,去重 + token 预算 + 单条字符上限裁剪 - risk_observations.create_feedback commit 后挂 hook 自动入库,带 feature flag 和 try/except 兜底 - risk_rule_generation_prompt 新增 few_shot_samples 可选参数,合并进 examples 并标 source=historical_confirmed;risk_rule_generation 构造 prompt 前调 retriever,失败降级为空
This commit is contained in:
@@ -14,10 +14,15 @@ def build_risk_rule_compiler_messages(
|
||||
expense_category_label: str,
|
||||
natural_language: str,
|
||||
available_fields: list[dict[str, Any]],
|
||||
few_shot_samples: list[dict[str, Any]] | None = None,
|
||||
) -> list[dict[str, str]]:
|
||||
"""构造自然语言规则编译提示词。
|
||||
|
||||
大模型只负责把业务语言拆成“语义计划”,后端会校验字段、操作符和模板。
|
||||
|
||||
``few_shot_samples`` 是从已确认历史样本中检索出来的相似案例,会被合并进
|
||||
``examples`` 字段并标注 ``source: "historical_confirmed"``,让编译器参考
|
||||
过往人工结论。传 ``None`` 或空列表时行为与历史完全一致(向后兼容)。
|
||||
"""
|
||||
|
||||
schema = {
|
||||
@@ -161,6 +166,20 @@ def build_risk_rule_compiler_messages(
|
||||
},
|
||||
}
|
||||
]
|
||||
historical_examples: list[dict[str, Any]] = []
|
||||
if few_shot_samples:
|
||||
for sample in few_shot_samples:
|
||||
historical_examples.append(
|
||||
{
|
||||
"source": "historical_confirmed",
|
||||
"label": sample.get("label"),
|
||||
"domain": sample.get("domain") or "",
|
||||
"risk_type": sample.get("risk_type") or "",
|
||||
"conclusion": sample.get("conclusion") or "",
|
||||
"context": sample.get("context") or {},
|
||||
}
|
||||
)
|
||||
merged_examples = historical_examples + examples
|
||||
return [
|
||||
{
|
||||
"role": "system",
|
||||
@@ -186,7 +205,7 @@ def build_risk_rule_compiler_messages(
|
||||
"natural_language": natural_language,
|
||||
"available_fields": available_fields,
|
||||
"required_json_shape": response_schema,
|
||||
"examples": examples,
|
||||
"examples": merged_examples,
|
||||
},
|
||||
ensure_ascii=False,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user