Spaces:
Running
Running
Fin-DataPilot Deploy Bot commited on
Commit ·
15272d6
1
Parent(s): 36ef4c6
ci: 6959553
Browse files- app/agent/policy.py +34 -3
app/agent/policy.py
CHANGED
|
@@ -12,7 +12,21 @@ from datetime import date
|
|
| 12 |
from typing import Any
|
| 13 |
|
| 14 |
FINANCIAL_ADVICE_TERMS = ("买入", "卖出", "推荐", "建仓", "加仓", "减仓", "止损", "目标价", "值得买", "该买吗")
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
LONG_WINDOW_TERMS = {
|
| 17 |
"一年": 365,
|
| 18 |
"半年": 180,
|
|
@@ -34,11 +48,11 @@ class PolicyDecision:
|
|
| 34 |
|
| 35 |
def assess_user_query(query: str) -> PolicyDecision:
|
| 36 |
text = query.lower()
|
| 37 |
-
if
|
| 38 |
return PolicyDecision(
|
| 39 |
allowed=False,
|
| 40 |
code="EXECUTION_NOT_SUPPORTED",
|
| 41 |
-
message="
|
| 42 |
)
|
| 43 |
notices: list[str] = []
|
| 44 |
if any(term in text for term in FINANCIAL_ADVICE_TERMS):
|
|
@@ -46,6 +60,23 @@ def assess_user_query(query: str) -> PolicyDecision:
|
|
| 46 |
return PolicyDecision(allowed=True, notices=tuple(notices))
|
| 47 |
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
def assess_tool_call(name: str, args: dict[str, Any]) -> PolicyDecision:
|
| 50 |
"""Enforce the 30-day financial-query limit on continuous indicator windows."""
|
| 51 |
if name != "financial-query":
|
|
|
|
| 12 |
from typing import Any
|
| 13 |
|
| 14 |
FINANCIAL_ADVICE_TERMS = ("买入", "卖出", "推荐", "建仓", "加仓", "减仓", "止损", "目标价", "值得买", "该买吗")
|
| 15 |
+
# Match requests to perform an action, rather than financial vocabulary.
|
| 16 |
+
# In particular, “交易日/交易量/交易策略” are ordinary research concepts.
|
| 17 |
+
_ACTION = r"(?:下单|挂单|撤单|转账|提现|代买|代卖|买入|卖出|建仓|加仓|减仓|清仓|申购|赎回|交易)"
|
| 18 |
+
_MODIFIER = r"(?:请|帮我|替我|代我|为我|给我|立即|立刻|马上|现在|直接|自动|实际|执行|进行|发起|提交|确认|启动|取消|操作|一键|一下|一笔|一次|这笔|此笔|\s)*"
|
| 19 |
+
_EXECUTION_PATTERNS = (
|
| 20 |
+
re.compile(r"(?:帮我|帮忙|替我|代我|为我|给我|请你|让你|麻烦你|请|立即|立刻|马上|现在|直接|自动|实际|执行|进行|发起|提交|确认|启动|取消)" + _MODIFIER + _ACTION),
|
| 21 |
+
re.compile(r"(?:^|[,,。;;!?!?\n]|然后|接着|并且|并)\s*" + _MODIFIER + _ACTION),
|
| 22 |
+
re.compile(r"(?:^|[,,。;;!?!?\n])\s*" + _MODIFIER + r"(?:把|将)[^,,。;;!?!?\n]{1,40}?(?:转给|转到|转入|转出)"),
|
| 23 |
+
)
|
| 24 |
+
_RESEARCH_SUFFIX = re.compile(
|
| 25 |
+
r"^(?:的)?(?:日|量|额|所|数据|记录|明细|统计|策略|模型|规则|制度|时间|时段|"
|
| 26 |
+
r"成本|费用|手续费|风险|建议|时机|信号|逻辑|理由|流程|步骤|原理|机制|失败|原因|条件|限制|示例|"
|
| 27 |
+
r"前|是什么|是什么意思|怎么|如何|是否|能否|吗|么|呢|后多久到账)"
|
| 28 |
+
)
|
| 29 |
+
_NEGATED_REQUEST = re.compile(r"(?:不要|不必|无需|不用|别|禁止|不是(?:让你)?)\s*$")
|
| 30 |
LONG_WINDOW_TERMS = {
|
| 31 |
"一年": 365,
|
| 32 |
"半年": 180,
|
|
|
|
| 48 |
|
| 49 |
def assess_user_query(query: str) -> PolicyDecision:
|
| 50 |
text = query.lower()
|
| 51 |
+
if _requests_execution(text):
|
| 52 |
return PolicyDecision(
|
| 53 |
allowed=False,
|
| 54 |
code="EXECUTION_NOT_SUPPORTED",
|
| 55 |
+
message="我可以帮你查询和分析相关数据,但不能替你实际下单、转账或操作账户。",
|
| 56 |
)
|
| 57 |
notices: list[str] = []
|
| 58 |
if any(term in text for term in FINANCIAL_ADVICE_TERMS):
|
|
|
|
| 60 |
return PolicyDecision(allowed=True, notices=tuple(notices))
|
| 61 |
|
| 62 |
|
| 63 |
+
def _requests_execution(text: str) -> bool:
|
| 64 |
+
"""Conservatively recognize explicit execution requests in each clause.
|
| 65 |
+
|
| 66 |
+
Read-only questions and negated requests are allowed. Negation is local
|
| 67 |
+
to a matched request: “不要解释,直接下单” still contains an action.
|
| 68 |
+
Actual capabilities remain restricted by the read-only tool registry.
|
| 69 |
+
"""
|
| 70 |
+
for pattern in _EXECUTION_PATTERNS:
|
| 71 |
+
for match in pattern.finditer(text):
|
| 72 |
+
if _NEGATED_REQUEST.search(text[:match.start()]):
|
| 73 |
+
continue
|
| 74 |
+
if _RESEARCH_SUFFIX.match(text[match.end():].lstrip(" \t\n”\"’'」』")):
|
| 75 |
+
continue
|
| 76 |
+
return True
|
| 77 |
+
return False
|
| 78 |
+
|
| 79 |
+
|
| 80 |
def assess_tool_call(name: str, args: dict[str, Any]) -> PolicyDecision:
|
| 81 |
"""Enforce the 30-day financial-query limit on continuous indicator windows."""
|
| 82 |
if name != "financial-query":
|