feat:修改投顾agent和nl2sql的功能
This commit is contained in:
@@ -3,6 +3,7 @@ from __future__ import annotations
|
||||
|
||||
from dataclasses import asdict
|
||||
from typing import Any
|
||||
from uuid import uuid4
|
||||
|
||||
from agent.advisor_agent.auth import ensure_customer_access
|
||||
from agent.data_query.agent import DataQueryAgent
|
||||
@@ -15,6 +16,51 @@ from nl2sql.schema import load_authoritative_schema
|
||||
from service.nl2sql.permission_service import load_query_permission
|
||||
from service.nl2sql.query_service import QueryServiceError
|
||||
from tool.llm import llm as default_llm
|
||||
from repositories.fin_holdings import FinHoldingsRepo
|
||||
from repositories.fin_product import FinProductRepo
|
||||
|
||||
|
||||
def _is_current_holdings_query(question: str) -> bool:
|
||||
text = "".join((question or "").split())
|
||||
return any(term in text for term in ("当前持仓", "目前持仓", "现有持仓", "持仓明细", "持仓情况"))
|
||||
|
||||
|
||||
async def _query_current_holdings(db, *, customer_id: int, trace_id: str) -> dict[str, Any]:
|
||||
holdings = await FinHoldingsRepo(db).list_by_customer(customer_id, status="持有中")
|
||||
product_repo = FinProductRepo(db)
|
||||
rows: list[dict[str, Any]] = []
|
||||
total_value = 0
|
||||
for holding in holdings:
|
||||
product = await product_repo.get(holding.product_id)
|
||||
rows.append(
|
||||
{
|
||||
"产品代码": product.product_code if product else None,
|
||||
"产品名称": product.product_name if product else None,
|
||||
"风险等级": product.risk_level if product else None,
|
||||
"持有份额": f"{holding.shares:.4f}",
|
||||
"成本金额": f"{holding.cost_amount:.2f}",
|
||||
"当前市值": f"{holding.current_value:.2f}",
|
||||
"盈亏": f"{holding.profit_loss:.2f}",
|
||||
"收益率": f"{holding.profit_ratio:.4f}",
|
||||
"状态": holding.status,
|
||||
}
|
||||
)
|
||||
total_value += holding.current_value
|
||||
names = [row["产品名称"] for row in rows if row["产品名称"]]
|
||||
return {
|
||||
"query_id": f"holdings-{uuid4().hex}",
|
||||
"trace_id": trace_id,
|
||||
"columns": list(rows[0].keys()) if rows else ["产品代码", "产品名称", "风险等级", "持有份额", "成本金额", "当前市值", "盈亏", "收益率", "状态"],
|
||||
"rows": rows,
|
||||
"row_count": len(rows),
|
||||
"truncated": False,
|
||||
"summary": f"当前持仓共 {len(rows)} 条记录。",
|
||||
"answer": (
|
||||
f"当前持有 {len(rows)} 只基金,总市值约 {total_value:.2f} 元。"
|
||||
+ (f"包括:{'、'.join(names[:6])}。" if names else "")
|
||||
),
|
||||
"sql": None,
|
||||
}
|
||||
|
||||
|
||||
async def execute_advisor_data_query(
|
||||
@@ -42,6 +88,12 @@ async def execute_advisor_data_query(
|
||||
``customer_id``,避免投顾借助 NL2SQL 查询其他客户数据。
|
||||
"""
|
||||
await ensure_customer_access(db, advisor_id=advisor_id, customer_id=customer_id)
|
||||
if _is_current_holdings_query(question):
|
||||
return await _query_current_holdings(
|
||||
db,
|
||||
customer_id=customer_id,
|
||||
trace_id=trace_id,
|
||||
)
|
||||
permission = await load_query_permission(db, advisor_id)
|
||||
if not permission.get("can_query", False):
|
||||
raise QueryServiceError("当前投顾没有 NL2SQL 查询权限")
|
||||
|
||||
Reference in New Issue
Block a user