feat: implement logging and refine agent core, execution, and prompts.

This commit is contained in:
2026-01-06 17:53:19 +08:00
parent 24870ba497
commit ca134e94c8
5 changed files with 8752 additions and 54 deletions

View File

@@ -208,13 +208,14 @@ class DataAnalysisAgent:
"continue": True,
}
def analyze(self, user_input: str, files: List[str] = None) -> Dict[str, Any]:
def analyze(self, user_input: str, files: List[str] = None, session_output_dir: str = None) -> Dict[str, Any]:
"""
开始分析流程
Args:
user_input: 用户的自然语言需求
files: 数据文件路径列表
session_output_dir: 指定的会话输出目录(可选)
Returns:
分析结果字典
@@ -225,9 +226,13 @@ class DataAnalysisAgent:
self.current_round = 0
# 创建本次分析的专用输出目录
self.session_output_dir = create_session_output_dir(
self.base_output_dir, user_input
)
if session_output_dir:
self.session_output_dir = session_output_dir
else:
self.session_output_dir = create_session_output_dir(
self.base_output_dir, user_input
)
# 初始化代码执行器,使用会话目录
self.executor = CodeExecutor(self.session_output_dir)
@@ -392,15 +397,25 @@ class DataAnalysisAgent:
# 解析响应,提取最终报告
try:
# 尝试解析YAML
yaml_data = self.llm.parse_yaml_response(response)
# 情况1: 标准YAML格式包含 action: analysis_complete
if yaml_data.get("action") == "analysis_complete":
final_report_content = yaml_data.get("final_report", "报告生成失败")
final_report_content = yaml_data.get("final_report", response)
# 情况2: 解析成功但没字段,或者解析失败
else:
final_report_content = (
"LLM未返回analysis_complete动作报告生成失败"
)
except:
# 如果解析失败,直接使用响应内容
# 如果内容看起来像Markdown报告包含标题直接使用
if "# " in response or "## " in response:
print("⚠️ 未检测到标准YAML动作但内容疑似Markdown报告直接采纳")
final_report_content = response
else:
final_report_content = "LLM未返回有效报告内容"
except Exception as e:
# 解析完全失败,直接使用原始响应
print(f"⚠️ YAML解析失败 ({e}),直接使用原始响应作为报告")
final_report_content = response
print("✅ 最终报告生成完成")