agent功能开发增加MCP后端
This commit is contained in:
@@ -1298,7 +1298,50 @@ class MCPAgentIntegrated:
|
||||
messages = [
|
||||
{
|
||||
"role": "system",
|
||||
"content": "你是专业的金融研究助手。根据执行结果,生成简洁清晰的报告。"
|
||||
"content": """你是专业的金融研究助手。根据执行结果,生成简洁清晰的报告。
|
||||
|
||||
## 数据可视化能力
|
||||
如果执行结果中包含数值型数据(如财务指标、交易数据、时间序列等),你可以使用 ECharts 生成图表来增强报告的可读性。
|
||||
|
||||
支持的图表类型:
|
||||
- 折线图(line):适合时间序列数据(如股价走势、财务指标趋势)
|
||||
- 柱状图(bar):适合对比数据(如不同年份的收入、利润对比)
|
||||
- 饼图(pie):适合占比数据(如业务结构、资产分布)
|
||||
|
||||
### 图表格式(使用 Markdown 代码块)
|
||||
在报告中插入图表时,使用以下格式:
|
||||
|
||||
```echarts
|
||||
{
|
||||
"title": {"text": "图表标题"},
|
||||
"tooltip": {},
|
||||
"xAxis": {"type": "category", "data": ["类别1", "类别2"]},
|
||||
"yAxis": {"type": "value"},
|
||||
"series": [{"name": "数据系列", "type": "line", "data": [100, 200]}]
|
||||
}
|
||||
```
|
||||
|
||||
### 示例
|
||||
如果有股价数据,可以这样呈现:
|
||||
|
||||
**股价走势分析**
|
||||
|
||||
近30日股价呈现上涨趋势,最高达到1850元。
|
||||
|
||||
```echarts
|
||||
{
|
||||
"title": {"text": "近30日股价走势", "left": "center"},
|
||||
"tooltip": {"trigger": "axis"},
|
||||
"xAxis": {"type": "category", "data": ["2024-01-01", "2024-01-02", "2024-01-03"]},
|
||||
"yAxis": {"type": "value", "name": "股价(元)"},
|
||||
"series": [{"name": "收盘价", "type": "line", "data": [1800, 1820, 1850], "smooth": true}]
|
||||
}
|
||||
```
|
||||
|
||||
**重要提示**:
|
||||
- ECharts 配置必须是合法的 JSON 格式
|
||||
- 只在有明确数值数据时才生成图表
|
||||
- 不要凭空捏造数据"""
|
||||
},
|
||||
{
|
||||
"role": "user",
|
||||
@@ -1309,7 +1352,7 @@ class MCPAgentIntegrated:
|
||||
执行结果:
|
||||
{results_text}
|
||||
|
||||
请生成专业的分析报告(300字以内)。"""
|
||||
请生成专业的分析报告(500字以内)。如果结果中包含数值型数据,请使用 ECharts 图表进行可视化展示。"""
|
||||
},
|
||||
]
|
||||
|
||||
@@ -1318,7 +1361,7 @@ class MCPAgentIntegrated:
|
||||
model="kimi-k2-turbo-preview", # 使用非思考模型,更快
|
||||
messages=messages,
|
||||
temperature=0.7,
|
||||
max_tokens=1000,
|
||||
max_tokens=2000, # 增加 token 限制以支持图表配置
|
||||
)
|
||||
|
||||
summary = response.choices[0].message.content
|
||||
@@ -1632,7 +1675,7 @@ async def agent_chat(request: AgentChatRequest):
|
||||
try:
|
||||
# 将执行步骤转换为JSON字符串
|
||||
steps_json = json.dumps(
|
||||
[{"tool": step.tool, "result": step.result} for step in response.steps],
|
||||
[{"tool": step.tool, "status": step.status, "result": step.result} for step in response.step_results],
|
||||
ensure_ascii=False
|
||||
)
|
||||
|
||||
@@ -1642,12 +1685,12 @@ async def agent_chat(request: AgentChatRequest):
|
||||
user_nickname=request.user_nickname or "匿名用户",
|
||||
user_avatar=request.user_avatar or "",
|
||||
message_type="assistant",
|
||||
message=response.final_answer,
|
||||
plan=response.plan,
|
||||
message=response.final_summary, # 使用 final_summary 而不是 final_answer
|
||||
plan=response.plan.dict() if response.plan else None, # 转换为字典
|
||||
steps=steps_json,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.error(f"保存 Agent 回复失败: {e}")
|
||||
logger.error(f"保存 Agent 回复失败: {e}", exc_info=True)
|
||||
|
||||
# 在响应中返回 session_id
|
||||
response_dict = response.dict()
|
||||
|
||||
Reference in New Issue
Block a user