feat:修改投顾agent和nl2sql的功能
This commit is contained in:
@@ -106,6 +106,7 @@ def result_to_cache(result: DataQueryResult) -> dict[str, Any]:
|
||||
"row_count": result.row_count,
|
||||
"truncated": result.truncated,
|
||||
"summary": result.summary,
|
||||
"answer": result.answer,
|
||||
"markdown": result.markdown,
|
||||
"chart": result.chart,
|
||||
"metric_definitions": result.metric_definitions,
|
||||
|
||||
@@ -44,6 +44,7 @@ class DataQueryResult:
|
||||
row_count: int = 0
|
||||
truncated: bool = False
|
||||
summary: str | None = None
|
||||
answer: str | None = None
|
||||
markdown: str | None = None
|
||||
chart: dict[str, Any] | None = None
|
||||
metric_definitions: list[dict[str, Any]] = field(default_factory=list)
|
||||
|
||||
+24
-3
@@ -31,10 +31,31 @@ async def summarize_result(
|
||||
llm_client,
|
||||
max_prompt_rows: int = 20,
|
||||
) -> str:
|
||||
"""使用脱敏后的受控结果生成摘要,模型失败时返回固定话术。"""
|
||||
"""兼容旧调用方:返回面向用户的自然语言答案。"""
|
||||
return await render_answer(
|
||||
question,
|
||||
columns,
|
||||
rows,
|
||||
llm_client=llm_client,
|
||||
max_prompt_rows=max_prompt_rows,
|
||||
)
|
||||
|
||||
|
||||
async def render_answer(
|
||||
question: str,
|
||||
columns: list[str],
|
||||
rows: list[dict[str, Any]],
|
||||
*,
|
||||
llm_client,
|
||||
max_prompt_rows: int = 20,
|
||||
) -> str:
|
||||
"""将受控查询结果渲染为用户回答,不输出原始 JSON 或无关字段。"""
|
||||
fallback = f"查询完成,共返回 {len(rows)} 条记录。"
|
||||
prompt = (
|
||||
"请用简洁中文总结查询结果,只基于提供的数据,不要猜测。\n"
|
||||
"请根据用户问题生成简洁、准确的中文业务回答。\n"
|
||||
"只使用查询结果中的必要字段回答问题,不要输出 JSON、SQL、Markdown 表格或内部字段名。\n"
|
||||
"如果有多条记录,优先做合计、数量或关键项概括;只有用户明确需要明细时才列出明细。\n"
|
||||
"不得补充查询结果中没有的数据,不要解释你的处理过程。\n"
|
||||
f"问题:{question}\n"
|
||||
f"列名:{json.dumps(columns, ensure_ascii=False)}\n"
|
||||
f"结果:{json.dumps(rows[:max_prompt_rows], ensure_ascii=False, default=str)}"
|
||||
@@ -42,7 +63,7 @@ async def summarize_result(
|
||||
try:
|
||||
answer = await llm_client.chat(
|
||||
[
|
||||
{"role": "system", "content": "你是数据查询结果摘要助手。"},
|
||||
{"role": "system", "content": "你是基金平台的数据回答助手。不要输出 JSON。"},
|
||||
{"role": "user", "content": prompt},
|
||||
],
|
||||
temperature=0,
|
||||
|
||||
Reference in New Issue
Block a user