46 lines
1.7 KiB
Python
46 lines
1.7 KiB
Python
"""场外基金专用 NL2SQL 适配器。
|
|
|
|
该适配器只隔离调用边界,不改写 `nl2sql_yc.query_dict` 的业务能力。
|
|
"""
|
|
|
|
from importlib import import_module
|
|
from typing import cast
|
|
|
|
from app.core.contracts import RequestContext
|
|
|
|
|
|
class OffsiteNl2SqlAdapter:
|
|
"""把场外基金上下文转换为 `nl2sql_yc.query_dict` 可接受的稳定参数。"""
|
|
|
|
script_path = "nl2sql_yc.py"
|
|
|
|
def query(self, question: str, context: RequestContext) -> dict[str, object]:
|
|
try:
|
|
query_dict = getattr(import_module("nl2sql_yc"), "query_dict", None)
|
|
if not callable(query_dict):
|
|
return {"status": "error", "message": "NL2SQL入口query_dict不存在"}
|
|
result = cast(dict[str, object], query_dict(
|
|
question,
|
|
self._auth_context(context),
|
|
use_llm=False,
|
|
persist_audit=False,
|
|
))
|
|
if isinstance(result, dict):
|
|
return result
|
|
return {"status": "error", "message": "NL2SQL返回格式不正确"}
|
|
except Exception as exc:
|
|
return {"status": "error", "message": f"NL2SQL调用失败:{type(exc).__name__}"}
|
|
|
|
@staticmethod
|
|
def _auth_context(context: RequestContext) -> dict[str, object]:
|
|
return {
|
|
"user_id": int(context.user_id) if context.user_id.isdigit() else None,
|
|
"roles": list(context.roles),
|
|
"allowed_domains": ["market_nav", "trading_account", "product_fee"],
|
|
"customer_scope": context.data_scope if context.data_scope in {
|
|
"self", "own_customers", "all",
|
|
} else "all",
|
|
"max_rows": 100,
|
|
"max_query_seconds": 10,
|
|
}
|