feat: add advisory product comparison tool
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
"""投顾 Agent 的公共底座实现。"""
|
||||
|
||||
import re
|
||||
from typing import Any
|
||||
|
||||
from app.core.contracts import AgentDefinition, AgentRequest, CoreResult, RequestContext
|
||||
@@ -21,6 +22,7 @@ class AdvisorAgent(FundQueryDemoAgent):
|
||||
"analyze_portfolio",
|
||||
"generate_asset_allocation",
|
||||
"recommend_products",
|
||||
"compare_products",
|
||||
),
|
||||
supported_intents=(
|
||||
"fund_quote",
|
||||
@@ -77,7 +79,16 @@ class AdvisorAgent(FundQueryDemoAgent):
|
||||
self._classified_intent is not None
|
||||
and self._classified_intent.intent == "comparison"
|
||||
):
|
||||
return CoreResult(text="对比分析需要明确两个或多个场内基金产品,请提供基金代码或名称。")
|
||||
codes = tuple(dict.fromkeys(re.findall(r"(?<!\d)(\d{6})(?!\d)", request.message)))
|
||||
if len(codes) < 2:
|
||||
return CoreResult(
|
||||
text="对比分析需要明确两个或多个场内基金产品,请提供基金代码或名称。"
|
||||
)
|
||||
output = await self.call_tool(
|
||||
"compare_products", {"product_codes": list(codes[:4])},
|
||||
intent="comparison", context=context,
|
||||
)
|
||||
return CoreResult(text=self._describe_comparison(output))
|
||||
return await super().handle(request, context)
|
||||
|
||||
@staticmethod
|
||||
@@ -156,3 +167,23 @@ class AdvisorAgent(FundQueryDemoAgent):
|
||||
if isinstance(item, dict)
|
||||
]
|
||||
return "推荐分析结果:" + "、".join(names) + "。方案须经审核发布,不构成交易指令。"
|
||||
|
||||
@staticmethod
|
||||
def _describe_comparison(result: object) -> str:
|
||||
if not isinstance(result, dict):
|
||||
return "基金对比分析暂不可用,请稍后重试。"
|
||||
if result.get("status") == "evidence_required":
|
||||
return "部分基金缺少当前有效的权威证据,暂不能完成可靠对比。"
|
||||
products = result.get("products")
|
||||
if result.get("status") != "ready" or not isinstance(products, list):
|
||||
return "对比分析需要至少两个有效的场内基金产品。"
|
||||
names = [
|
||||
f"{item.get('product_code')} {item.get('product_name')}"
|
||||
for item in products if isinstance(item, dict)
|
||||
]
|
||||
common = result.get("common_industries")
|
||||
common_text = "、".join(str(item) for item in common) if isinstance(common, list) else "无"
|
||||
return (
|
||||
"对比分析:" + ";".join(names)
|
||||
+ f"。共同行业暴露:{common_text}。仅供分析参考,不构成交易指令。"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user