Files
mini-openclaw/backend/api/risk_assess.py
T

618 lines
32 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Agent 1: 风险研判助手 API(双轨驱动版)
"""
from fastapi import APIRouter
from pydantic import BaseModel
from typing import List, Dict, Any, Optional, Union
from datetime import datetime
router = APIRouter(tags=["risk-assess"])
# ==================== 请求/响应模型 ====================
class RiskAssessRequest(BaseModel):
customer_name: str
project_type: str
amount: float
debt_ratio: float
credit_query_count: int
litigation_count: int
external_guarantee: float
# ==================== API端点 ====================
@router.post("/analyze")
async def analyze_risk(req: dict):
"""风险研判分析(双轨驱动)- 返回伪造的完整数据"""
# 直接从dict中获取数据
customer_name = req.get("customer_name", "")
project_type = req.get("project_type", "")
amount = float(req.get("amount", 0))
debt_ratio = float(req.get("debt_ratio", 0))
credit_query_count = int(req.get("credit_query_count", 0))
litigation_count = int(req.get("litigation_count", 0))
external_guarantee = float(req.get("external_guarantee", 0))
# 生成完整的伪造数据
risk_items = []
# 风险项1: 资产负债率
if req.debt_ratio >= 0.60:
risk_items.append({
"level": "high",
"title": "资产负债率接近红线",
"description": f"{req.customer_name}资产负债率为 {req.debt_ratio*100:.1f}%,距离70%红线仅剩 {(0.70-req.debt_ratio)*100:.1f}% 空间。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第12条",
"page": 12,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.87
},
"data_based": {
"sample_size": 23,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "降额通过", "percentage": 60.9, "count": 14, "avg_processing_days": 4.2},
{"decision": "退回补充材料", "percentage": 26.1, "count": 6, "avg_processing_days": 7.5},
{"decision": "附加条件通过", "percentage": 13.0, "count": 3, "avg_processing_days": 5.8}
],
"similar_cases": [
{
"case_id": "CAS-2025-08-156",
"customer_name": "华东光伏设备公司",
"decision": "降额通过(降15%)",
"mitigation": "增加法人个人担保,提供政府补贴承诺函"
},
{
"case_id": "CAS-2025-07-089",
"customer_name": "绿能公交运营公司",
"decision": "降额通过(降12%)",
"mitigation": "调整还款期限,追加车辆抵押"
},
{
"case_id": "CAS-2025-06-234",
"customer_name": "中部新能源投资",
"decision": "附加条件通过",
"mitigation": "要求季度现金流报告,设置财务预警指标"
}
]
}
},
"suggestion": "建议业务经理提前准备降额方案或增加抵押物,避免评审会直接退回。",
"smart_recommendation": {
"primary_action": f"主动降低授信额度12-15%(约 {req.amount * 0.12:.1f}-{req.amount * 0.15:.1f} 亿元),调整后负债率可控",
"alternative_actions": [
f"增加抵押物或第三方担保,覆盖率≥授信额度的20%(约 {req.amount * 0.20:.1f} 亿元)",
"提供详细现金流测算报告,证明偿债能力充足",
"调整还款期限,降低单期还款压力"
],
"reference_case": {
"case_id": "CAS-2025-08-156",
"customer_name": "华东光伏设备公司",
"scenario": f"债务率 {req.debt_ratio*100:.1f}%,新能源行业,申请 {req.amount:.1f} 亿授信",
"outcome": "降额15%后通过,增加法人担保 + 政府补贴承诺函"
}
}
})
# 风险项2: 征信查询
if req.credit_query_count >= 6:
risk_items.append({
"level": "medium",
"title": "征信查询次数偏多",
"description": f"{req.customer_name}近6个月征信查询 {req.credit_query_count} 次,高于行业平均(4次)。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第8条 - 征信管理",
"page": 8,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.82
},
"data_based": {
"sample_size": 15,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 53.3, "count": 8, "avg_processing_days": 3.5},
{"decision": "要求补充说明", "percentage": 33.3, "count": 5, "avg_processing_days": 5.2},
{"decision": "降额通过", "percentage": 13.3, "count": 2, "avg_processing_days": 6.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-09-012",
"customer_name": "东方物流集团",
"decision": "正常通过",
"mitigation": "提供多头融资说明(用于业务扩张),核实无实质风险"
}
]
}
},
"suggestion": "建议业务经理核实客户是否存在多头融资,了解征信查询原因(业务扩张 vs 资金紧张)。",
"smart_recommendation": {
"primary_action": "要求客户提供征信查询原因说明书,核实多头融资情况",
"alternative_actions": [
"提供资金用途说明和还款计划,证明融资用于业务扩张",
"补充近6个月银行流水,证明现金流充足"
],
"reference_case": {
"case_id": "CAS-2025-09-012",
"customer_name": "东方物流集团",
"scenario": f"征信查询 {req.credit_query_count} 次,用于扩张业务",
"outcome": "提供业务扩张说明后正常通过,核实无资金链风险"
}
}
})
# 风险项3: 涉诉记录
if req.litigation_count >= 2:
risk_items.append({
"level": "medium",
"title": "存在涉诉记录",
"description": f"{req.customer_name}存在 {req.litigation_count} 起涉诉记录,需核实诉讼性质和金额。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第10条 - 法律风险评估",
"page": 10,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.85
},
"data_based": {
"sample_size": 12,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 58.3, "count": 7, "avg_processing_days": 4.0},
{"decision": "要求提供诉讼详情", "percentage": 33.3, "count": 4, "avg_processing_days": 6.5},
{"decision": "附加条件通过", "percentage": 8.3, "count": 1, "avg_processing_days": 8.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-08-203",
"customer_name": "华泰建设工程",
"decision": "正常通过",
"mitigation": "确认为小额合同纠纷(<50万),不影响偿债能力"
}
]
}
},
"suggestion": "建议业务经理提供每起诉讼的详细情况(原告/被告、金额、进展),评估对财务的影响。",
"smart_recommendation": {
"primary_action": "提供诉讼清单(案由、金额、当前状态),说明对财务状况的影响评估",
"alternative_actions": [
"如为小额合同纠纷(<50万/起),提供说明后影响较小",
"提供法律顾问意见书,说明诉讼风险可控"
],
"reference_case": {
"case_id": "CAS-2025-08-203",
"customer_name": "华泰建设工程",
"scenario": f"{req.litigation_count} 起小额合同纠纷,总金额<100万",
"outcome": "确认为日常经营纠纷,不影响偿债能力,正常通过"
}
}
})
# 风险项4: 对外担保
guarantee_ratio = req.external_guarantee / req.amount if req.amount > 0 else 0
if guarantee_ratio >= 0.8:
risk_items.append({
"level": "high",
"title": "对外担保金额较大",
"description": f"{req.customer_name}对外担保 {req.external_guarantee:.1f} 亿元,为本次申请金额的 {guarantee_ratio:.1f} 倍。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第11条 - 担保风险",
"page": 11,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.88
},
"data_based": {
"sample_size": 18,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "要求提供担保详情", "percentage": 66.7, "count": 12, "avg_processing_days": 5.8},
{"decision": "降额通过", "percentage": 22.2, "count": 4, "avg_processing_days": 7.2},
{"decision": "附加条件通过", "percentage": 11.1, "count": 2, "avg_processing_days": 6.5}
],
"similar_cases": [
{
"case_id": "CAS-2025-07-234",
"customer_name": "华信集团",
"decision": "附加条件通过",
"mitigation": "提供被担保方财务报告,设置代偿预警机制"
}
]
}
},
"suggestion": "建议业务经理提供对外担保明细(被担保方、金额、期限),评估代偿风险。",
"smart_recommendation": {
"primary_action": "提供对外担保明细清单,包括被担保方资质、财务状况、代偿可能性评估",
"alternative_actions": [
"说明被担保方为关联公司/优质客户,代偿风险低",
f"考虑降低授信额度至 {req.amount * 0.8:.1f} 亿元,降低风险敞口"
],
"reference_case": {
"case_id": "CAS-2025-07-234",
"customer_name": "华信集团",
"scenario": f"对外担保为申请额度的 {guarantee_ratio:.1f} 倍",
"outcome": "提供被担保方财务报告,设置代偿预警,附加条件通过"
}
}
})
# 风险项5: 行业风险
if req.project_type == "对公新能源":
risk_items.append({
"level": "medium",
"title": "新能源行业政策依赖性",
"description": f"{req.customer_name}属于新能源行业,收入可能依赖政府补贴,需关注补贴到位情况。",
"evidence": {
"rule_based": {
"source": "《行业风险指引》第3条 - 新能源行业",
"page": 3,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.90
},
"data_based": {
"sample_size": 31,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 48.4, "count": 15, "avg_processing_days": 4.5},
{"decision": "要求补贴承诺函", "percentage": 38.7, "count": 12, "avg_processing_days": 6.2},
{"decision": "附加条件通过", "percentage": 12.9, "count": 4, "avg_processing_days": 7.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-09-067",
"customer_name": "绿动公交集团",
"decision": "正常通过",
"mitigation": "补贴延迟但现金流充足,运营收入稳定"
}
]
}
},
"suggestion": "建议业务经理提供政府补贴承诺函、历史补贴到位情况,说明现金流不完全依赖补贴。",
"smart_recommendation": {
"primary_action": "要求提供地方政府补贴承诺函,核实历史补贴到位率(建议≥80%)",
"alternative_actions": [
"提供运营收入构成,证明补贴占比<40%,现金流多元化",
"说明补贴延迟应对预案(如银行授信、应收账款保理)"
],
"reference_case": {
"case_id": "CAS-2025-09-067",
"customer_name": "绿动公交集团",
"scenario": "新能源公交运营,补贴延迟但运营收入稳定",
"outcome": "证明运营收入占比65%,补贴延迟不影响还款,正常通过"
}
}
})
# 统计汇总
high_count = sum(1 for r in risk_items if r["level"] == "high")
medium_count = sum(1 for r in risk_items if r["level"] == "medium")
low_count = sum(1 for r in risk_items if r["level"] == "low")
# 提取关注领域
focus_areas = []
if any("资产负债率" in r["title"] for r in risk_items):
focus_areas.append("财务杠杆")
if any("征信" in r["title"] for r in risk_items):
focus_areas.append("信用状况")
if any("涉诉" in r["title"] for r in risk_items):
focus_areas.append("法律风险")
if any("担保" in r["title"] for r in risk_items):
focus_areas.append("对外担保")
if any("行业" in r["title"] or "政策" in r["title"] for r in risk_items):
focus_areas.append("行业风险")
if not focus_areas:
focus_areas = ["租赁物合规", "现场尽调"]
# 计算综合置信度
total_confidence = sum(r["evidence"]["rule_based"]["confidence"] for r in risk_items) / len(risk_items) if risk_items else 0.95
# 计算数据覆盖
total_samples = sum(r["evidence"]["data_based"]["sample_size"] for r in risk_items)
data_coverage = f"{total_samples} 个案例"
return {
"customer_name": req.customer_name,
"analysis_timestamp": datetime.now().isoformat(),
"risk_items": risk_items,
"summary": {
"high_count": high_count,
"medium_count": medium_count,
"low_count": low_count,
"focus_areas": focus_areas,
"overall_confidence": round(total_confidence, 2),
"data_coverage": data_coverage
}
}
@router.get("/test")
async def test_endpoint():
"""测试端点 - 返回完整的伪造数据"""
return {
"customer_name": "远东新能源公交公司",
"analysis_timestamp": datetime.now().isoformat(),
"risk_items": [
{
"level": "high",
"title": "资产负债率接近红线",
"description": "远东新能源公交公司资产负债率为 65.0%,距离70%红线仅剩 5.0% 空间。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第12条",
"page": 12,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.87
},
"data_based": {
"sample_size": 23,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "降额通过", "percentage": 60.9, "count": 14, "avg_processing_days": 4.2},
{"decision": "退回补充材料", "percentage": 26.1, "count": 6, "avg_processing_days": 7.5},
{"decision": "附加条件通过", "percentage": 13.0, "count": 3, "avg_processing_days": 5.8}
],
"similar_cases": [
{
"case_id": "CAS-2025-08-156",
"customer_name": "华东光伏设备公司",
"decision": "降额通过(降15%)",
"mitigation": "增加法人个人担保,提供政府补贴承诺函"
},
{
"case_id": "CAS-2025-07-089",
"customer_name": "绿能公交运营公司",
"decision": "降额通过(降12%)",
"mitigation": "调整还款期限,追加车辆抵押"
},
{
"case_id": "CAS-2025-06-234",
"customer_name": "中部新能源投资",
"decision": "附加条件通过",
"mitigation": "要求季度现金流报告,设置财务预警指标"
}
]
}
},
"suggestion": "建议业务经理提前准备降额方案或增加抵押物,避免评审会直接退回。",
"smart_recommendation": {
"primary_action": "主动降低授信额度12-15%(约 0.36-0.45 亿元),调整后负债率可控",
"alternative_actions": [
"增加抵押物或第三方担保,覆盖率≥授信额度的20%(约 0.60 亿元)",
"提供详细现金流测算报告,证明偿债能力充足",
"调整还款期限,降低单期还款压力"
],
"reference_case": {
"case_id": "CAS-2025-08-156",
"customer_name": "华东光伏设备公司",
"scenario": "债务率 65.0%,新能源行业,申请 3.0 亿授信",
"outcome": "降额15%后通过,增加法人担保 + 政府补贴承诺函"
}
}
},
{
"level": "medium",
"title": "征信查询次数偏多",
"description": "远东新能源公交公司近6个月征信查询 8 次,高于行业平均(4次)。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第8条 - 征信管理",
"page": 8,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.82
},
"data_based": {
"sample_size": 15,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 53.3, "count": 8, "avg_processing_days": 3.5},
{"decision": "要求补充说明", "percentage": 33.3, "count": 5, "avg_processing_days": 5.2},
{"decision": "降额通过", "percentage": 13.3, "count": 2, "avg_processing_days": 6.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-09-012",
"customer_name": "东方物流集团",
"decision": "正常通过",
"mitigation": "提供多头融资说明(用于业务扩张),核实无实质风险"
},
{
"case_id": "CAS-2025-07-145",
"customer_name": "鑫盛商贸公司",
"decision": "要求补充说明",
"mitigation": "说明征信查询原因,提供现金流充足证明"
}
]
}
},
"suggestion": "建议业务经理核实客户是否存在多头融资,了解征信查询原因(业务扩张 vs 资金紧张)。",
"smart_recommendation": {
"primary_action": "要求客户提供征信查询原因说明书,核实多头融资情况",
"alternative_actions": [
"提供资金用途说明和还款计划,证明融资用于业务扩张",
"补充近6个月银行流水,证明现金流充足",
"如为临时资金需求,提供订单合同等业务凭证"
],
"reference_case": {
"case_id": "CAS-2025-09-012",
"customer_name": "东方物流集团",
"scenario": "征信查询 8 次,用于扩张业务",
"outcome": "提供业务扩张说明后正常通过,核实无资金链风险"
}
}
},
{
"level": "medium",
"title": "存在涉诉记录",
"description": "远东新能源公交公司存在 2 起涉诉记录,需核实诉讼性质和金额。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第10条 - 法律风险评估",
"page": 10,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.85
},
"data_based": {
"sample_size": 12,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 58.3, "count": 7, "avg_processing_days": 4.0},
{"decision": "要求提供诉讼详情", "percentage": 33.3, "count": 4, "avg_processing_days": 6.5},
{"decision": "附加条件通过", "percentage": 8.3, "count": 1, "avg_processing_days": 8.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-08-203",
"customer_name": "华泰建设工程",
"decision": "正常通过",
"mitigation": "确认为小额合同纠纷(<50万),不影响偿债能力"
},
{
"case_id": "CAS-2025-06-178",
"customer_name": "顺丰供应链公司",
"decision": "要求提供诉讼详情",
"mitigation": "提供案件进展说明,评估财务影响<3%"
}
]
}
},
"suggestion": "建议业务经理提供每起诉讼的详细情况(原告/被告、金额、进展),评估对财务的影响。",
"smart_recommendation": {
"primary_action": "提供诉讼清单(案由、金额、当前状态),说明对财务状况的影响评估",
"alternative_actions": [
"如为小额合同纠纷(<50万/起),提供说明后影响较小",
"如为债务纠纷,需评估败诉风险和财务影响",
"提供法律顾问意见书,说明诉讼风险可控"
],
"reference_case": {
"case_id": "CAS-2025-08-203",
"customer_name": "华泰建设工程",
"scenario": "2 起小额合同纠纷,总金额<100万",
"outcome": "确认为日常经营纠纷,不影响偿债能力,正常通过"
}
}
},
{
"level": "high",
"title": "对外担保金额较大",
"description": "远东新能源公交公司对外担保 3.5 亿元,为本次申请金额的 1.2 倍。",
"evidence": {
"rule_based": {
"source": "《融资租赁评审手册》第11条 - 担保风险",
"page": 11,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.88
},
"data_based": {
"sample_size": 18,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "要求提供担保详情", "percentage": 66.7, "count": 12, "avg_processing_days": 5.8},
{"decision": "降额通过", "percentage": 22.2, "count": 4, "avg_processing_days": 7.2},
{"decision": "附加条件通过", "percentage": 11.1, "count": 2, "avg_processing_days": 6.5}
],
"similar_cases": [
{
"case_id": "CAS-2025-07-234",
"customer_name": "华信集团",
"decision": "附加条件通过",
"mitigation": "提供被担保方财务报告,设置代偿预警机制"
},
{
"case_id": "CAS-2025-09-045",
"customer_name": "宏达投资公司",
"decision": "降额通过(降20%)",
"mitigation": "降低授信以控制担保风险敞口"
}
]
}
},
"suggestion": "建议业务经理提供对外担保明细(被担保方、金额、期限),评估代偿风险。",
"smart_recommendation": {
"primary_action": "提供对外担保明细清单,包括被担保方资质、财务状况、代偿可能性评估",
"alternative_actions": [
"说明被担保方为关联公司/优质客户,代偿风险低",
"提供反担保措施说明(如抵押物、股权质押)",
"考虑降低授信额度至 2.4 亿元,降低风险敞口"
],
"reference_case": {
"case_id": "CAS-2025-07-234",
"customer_name": "华信集团",
"scenario": "对外担保为申请额度的 1.2 倍",
"outcome": "提供被担保方财务报告,设置代偿预警,附加条件通过"
}
}
},
{
"level": "medium",
"title": "新能源行业政策依赖性",
"description": "远东新能源公交公司属于新能源行业,收入可能依赖政府补贴,需关注补贴到位情况。",
"evidence": {
"rule_based": {
"source": "《行业风险指引》第3条 - 新能源行业",
"page": 3,
"extracted_by": "AI自动提取(已通过人工审核)",
"confidence": 0.90
},
"data_based": {
"sample_size": 31,
"time_range": "过去12个月",
"decision_stats": [
{"decision": "正常通过", "percentage": 48.4, "count": 15, "avg_processing_days": 4.5},
{"decision": "要求补贴承诺函", "percentage": 38.7, "count": 12, "avg_processing_days": 6.2},
{"decision": "附加条件通过", "percentage": 12.9, "count": 4, "avg_processing_days": 7.0}
],
"similar_cases": [
{
"case_id": "CAS-2025-08-112",
"customer_name": "晨光新能源科技",
"decision": "要求补贴承诺函",
"mitigation": "地方政府出具补贴承诺函,补贴到位率>85%"
},
{
"case_id": "CAS-2025-09-067",
"customer_name": "绿动公交集团",
"decision": "正常通过",
"mitigation": "补贴延迟但现金流充足,运营收入稳定"
}
]
}
},
"suggestion": "建议业务经理提供政府补贴承诺函、历史补贴到位情况,说明现金流不完全依赖补贴。",
"smart_recommendation": {
"primary_action": "要求提供地方政府补贴承诺函,核实历史补贴到位率(建议≥80%)",
"alternative_actions": [
"提供运营收入构成,证明补贴占比<40%,现金流多元化",
"说明补贴延迟应对预案(如银行授信、应收账款保理)",
"提供同行业可比公司补贴情况,证明行业普遍性"
],
"reference_case": {
"case_id": "CAS-2025-09-067",
"customer_name": "绿动公交集团",
"scenario": "新能源公交运营,补贴延迟但运营收入稳定",
"outcome": "证明运营收入占比65%,补贴延迟不影响还款,正常通过"
}
}
}
],
"summary": {
"high_count": 2,
"medium_count": 3,
"low_count": 0,
"focus_areas": ["财务杠杆", "信用状况", "法律风险", "对外担保", "行业风险"],
"overall_confidence": 0.86,
"data_coverage": "99 个案例"
}
}
@router.get("/health")
async def health_check():
return {"status": "ok", "service": "风险研判助手(双轨驱动版)"}