清理表情

This commit is contained in:
2026-01-31 18:00:05 +08:00
parent 674f48c74b
commit 5eb13324c2
15 changed files with 394 additions and 156 deletions

View File

@@ -18,6 +18,7 @@ from utils.extract_code import extract_code_from_response
from utils.data_loader import load_and_profile_data
from utils.llm_helper import LLMHelper
from utils.code_executor import CodeExecutor
from utils.script_generator import generate_reusable_script
from config.llm_config import LLMConfig
from prompts import data_analysis_system_prompt, final_report_system_prompt, data_analysis_followup_prompt
@@ -61,6 +62,8 @@ class DataAnalysisAgent:
self.session_output_dir = None
self.executor = None
self.data_profile = "" # 存储数据画像
self.data_files = [] # 存储数据文件列表
self.user_requirement = "" # 存储用户需求
def _process_response(self, response: str) -> Dict[str, Any]:
"""
@@ -76,7 +79,7 @@ class DataAnalysisAgent:
yaml_data = self.llm.parse_yaml_response(response)
action = yaml_data.get("action", "generate_code")
print(f"🎯 检测到动作: {action}")
print(f"[TARGET] 检测到动作: {action}")
if action == "analysis_complete":
return self._handle_analysis_complete(response, yaml_data)
@@ -85,11 +88,11 @@ class DataAnalysisAgent:
elif action == "generate_code":
return self._handle_generate_code(response, yaml_data)
else:
print(f"⚠️ 未知动作类型: {action}按generate_code处理")
print(f"[WARN] 未知动作类型: {action}按generate_code处理")
return self._handle_generate_code(response, yaml_data)
except Exception as e:
print(f"⚠️ 解析响应失败: {str(e)}尝试提取代码并按generate_code处理")
print(f"[WARN] 解析响应失败: {str(e)}尝试提取代码并按generate_code处理")
# 即使YAML解析失败也尝试提取代码
extracted_code = extract_code_from_response(response)
if extracted_code:
@@ -100,7 +103,7 @@ class DataAnalysisAgent:
self, response: str, yaml_data: Dict[str, Any]
) -> Dict[str, Any]:
"""处理分析完成动作"""
print(" 分析任务完成")
print("[OK] 分析任务完成")
final_report = yaml_data.get("final_report", "分析完成,无最终报告")
return {
"action": "analysis_complete",
@@ -113,7 +116,7 @@ class DataAnalysisAgent:
self, response: str, yaml_data: Dict[str, Any]
) -> Dict[str, Any]:
"""处理图片收集动作"""
print("📊 开始收集图片")
print("[CHART] 开始收集图片")
figures_to_collect = yaml_data.get("figures_to_collect", [])
collected_figures = []
@@ -130,10 +133,10 @@ class DataAnalysisAgent:
description = figure_info.get("description", "")
analysis = figure_info.get("analysis", "")
print(f"📈 收集图片 {figure_number}: {filename}")
print(f" 📂 路径: {file_path}")
print(f" 📝 描述: {description}")
print(f" 🔍 分析: {analysis}")
print(f"[GRAPH] 收集图片 {figure_number}: {filename}")
print(f" [DIR] 路径: {file_path}")
print(f" [NOTE] 描述: {description}")
print(f" [SEARCH] 分析: {analysis}")
# 使用seen_paths集合来去重防止重复收集
@@ -145,7 +148,7 @@ class DataAnalysisAgent:
# 检查是否已经收集过该路径
abs_path = os.path.abspath(file_path)
if abs_path not in seen_paths:
print(f" 文件存在: {file_path}")
print(f" [OK] 文件存在: {file_path}")
# 记录图片信息
collected_figures.append(
{
@@ -158,12 +161,12 @@ class DataAnalysisAgent:
)
seen_paths.add(abs_path)
else:
print(f" ⚠️ 跳过重复图片: {file_path}")
print(f" [WARN] 跳过重复图片: {file_path}")
else:
if file_path:
print(f" ⚠️ 文件不存在: {file_path}")
print(f" [WARN] 文件不存在: {file_path}")
else:
print(f" ⚠️ 未提供文件路径")
print(f" [WARN] 未提供文件路径")
return {
"action": "collect_figures",
@@ -195,7 +198,7 @@ class DataAnalysisAgent:
code = code.strip()
if code:
print(f"🔧 执行代码:\n{code}")
print(f"[TOOL] 执行代码:\n{code}")
print("-" * 40)
# 执行代码
@@ -203,7 +206,7 @@ class DataAnalysisAgent:
# 格式化执行结果
feedback = format_execution_result(result)
print(f"📋 执行反馈:\n{feedback}")
print(f"[LIST] 执行反馈:\n{feedback}")
return {
"action": "generate_code",
@@ -215,7 +218,7 @@ class DataAnalysisAgent:
}
else:
# 如果没有代码说明LLM响应格式有问题需要重新生成
print("⚠️ 未从响应中提取到可执行代码要求LLM重新生成")
print("[WARN] 未从响应中提取到可执行代码要求LLM重新生成")
return {
"action": "invalid_response",
"error": "响应中缺少可执行代码",
@@ -246,6 +249,8 @@ class DataAnalysisAgent:
self.conversation_history = []
self.analysis_results = []
self.current_round = 0
self.data_files = files or [] # 保存数据文件列表
self.user_requirement = user_input # 保存用户需求
# 创建本次分析的专用输出目录
if session_output_dir:
@@ -264,12 +269,12 @@ class DataAnalysisAgent:
# 设用工具生成数据画像
data_profile = ""
if files:
print("🔍 正在生成数据画像...")
print("[SEARCH] 正在生成数据画像...")
try:
data_profile = load_and_profile_data(files)
print(" 数据画像生成完毕")
print("[OK] 数据画像生成完毕")
except Exception as e:
print(f"⚠️ 数据画像生成失败: {e}")
print(f"[WARN] 数据画像生成失败: {e}")
# 保存到实例变量供最终报告使用
self.data_profile = data_profile
@@ -282,11 +287,11 @@ class DataAnalysisAgent:
if data_profile:
initial_prompt += f"\n\n{data_profile}\n\n请根据上述【数据画像】中的统计信息(如高频值、缺失率、数据范围)来制定分析策略。如果发现明显的高频问题或异常分布,请优先进行深度分析。"
print(f"🚀 开始数据分析任务")
print(f"📝 用户需求: {user_input}")
print(f"[START] 开始数据分析任务")
print(f"[NOTE] 用户需求: {user_input}")
if files:
print(f"📁 数据文件: {', '.join(files)}")
print(f"📂 输出目录: {self.session_output_dir}")
print(f"[FOLDER] 数据文件: {', '.join(files)}")
print(f"[DIR] 输出目录: {self.session_output_dir}")
# 添加到对话历史
self.conversation_history.append({"role": "user", "content": initial_prompt})
@@ -297,8 +302,8 @@ class DataAnalysisAgent:
if max_rounds is None:
current_max_rounds = 10 # 追问通常不需要那么长的思考链10轮足够
print(f"\n🚀 继续分析任务 (追问模式)")
print(f"📝 后续需求: {user_input}")
print(f"\n[START] 继续分析任务 (追问模式)")
print(f"[NOTE] 后续需求: {user_input}")
# 重置当前轮数计数器,以便给新任务足够的轮次
self.current_round = 0
@@ -308,18 +313,21 @@ class DataAnalysisAgent:
follow_up_prompt = f"后续需求: {user_input}\n(注意这是后续追问请直接针对该问题进行分析无需从头开始执行完整SOP。)"
self.conversation_history.append({"role": "user", "content": follow_up_prompt})
print(f"🔢 本次最大轮数: {current_max_rounds}")
print(f"[NUM] 本次最大轮数: {current_max_rounds}")
if self.force_max_rounds:
print(f" 强制模式: 将运行满 {current_max_rounds}忽略AI完成信号")
print(f"[FAST] 强制模式: 将运行满 {current_max_rounds}忽略AI完成信号")
print("=" * 60)
# 保存原始 max_rounds 以便恢复(虽然 analyze 结束后不需要恢复,但为了逻辑严谨)
original_max_rounds = self.max_rounds
self.max_rounds = current_max_rounds
# 初始化连续失败计数器
consecutive_failures = 0
while self.current_round < self.max_rounds:
self.current_round += 1
print(f"\n🔄{self.current_round} 轮分析")
print(f"\n[LOOP]{self.current_round} 轮分析")
# 调用LLM生成响应
try: # 获取当前执行环境的变量信息
notebook_variables = self.executor.get_environment_info()
@@ -340,15 +348,15 @@ class DataAnalysisAgent:
formatted_system_prompt = base_system_prompt.format(
notebook_variables=notebook_variables
)
print(f"🐛 [DEBUG] System Prompt Head:\n{formatted_system_prompt[:500]}...\n[...]")
print(f"🐛 [DEBUG] System Prompt Rules Check: 'stop_words' in prompt? {'stop_words' in formatted_system_prompt}")
print(f"[DEBUG] [DEBUG] System Prompt Head:\n{formatted_system_prompt[:500]}...\n[...]")
print(f"[DEBUG] [DEBUG] System Prompt Rules Check: 'stop_words' in prompt? {'stop_words' in formatted_system_prompt}")
response = self.llm.call(
prompt=self._build_conversation_prompt(),
system_prompt=formatted_system_prompt,
)
print(f"🤖 助手响应:\n{response}")
print(f"[AI] 助手响应:\n{response}")
# 使用统一的响应处理方法
process_result = self._process_response(response)
@@ -356,9 +364,9 @@ class DataAnalysisAgent:
# 根据处理结果决定是否继续(仅在非强制模式下)
if process_result.get("action") == "invalid_response":
consecutive_failures += 1
print(f"⚠️ 连续失败次数: {consecutive_failures}/3")
print(f"[WARN] 连续失败次数: {consecutive_failures}/3")
if consecutive_failures >= 3:
print(f" 连续3次无法获取有效响应分析终止。请检查网络或配置。")
print(f"[ERROR] 连续3次无法获取有效响应分析终止。请检查网络或配置。")
break
else:
consecutive_failures = 0 # 重置计数器
@@ -366,7 +374,7 @@ class DataAnalysisAgent:
if not self.force_max_rounds and not process_result.get(
"continue", True
):
print(f"\n 分析完成!")
print(f"\n[OK] 分析完成!")
break
# 添加到对话历史
@@ -398,7 +406,7 @@ class DataAnalysisAgent:
feedback = f"已收集 {len(collected_figures)} 个有效图片及其分析。"
if missing_figures:
feedback += f"\n⚠️ 以下图片未找到,请检查代码是否成功保存了这些图片: {missing_figures}"
feedback += f"\n[WARN] 以下图片未找到,请检查代码是否成功保存了这些图片: {missing_figures}"
self.conversation_history.append(
{
@@ -421,7 +429,7 @@ class DataAnalysisAgent:
except Exception as e:
error_msg = f"LLM调用错误: {str(e)}"
print(f" {error_msg}")
print(f"[ERROR] {error_msg}")
self.conversation_history.append(
{
"role": "user",
@@ -430,7 +438,7 @@ class DataAnalysisAgent:
)
# 生成最终总结
if self.current_round >= self.max_rounds:
print(f"\n⚠️ 已达到最大轮数 ({self.max_rounds}),分析结束")
print(f"\n[WARN] 已达到最大轮数 ({self.max_rounds}),分析结束")
return self._generate_final_report()
@@ -456,8 +464,8 @@ class DataAnalysisAgent:
if result.get("action") == "collect_figures":
all_figures.extend(result.get("collected_figures", []))
print(f"\n📊 开始生成最终分析报告...")
print(f"📂 输出目录: {self.session_output_dir}")
print(f"\n[CHART] 开始生成最终分析报告...")
print(f"[DIR] 输出目录: {self.session_output_dir}")
# --- 自动补全/发现图片机制 ---
# 扫描目录下所有的png文件
@@ -475,7 +483,7 @@ class DataAnalysisAgent:
for png_path in existing_pngs:
abs_png_path = os.path.abspath(png_path)
if abs_png_path not in collected_paths:
print(f"🔍 [自动发现] 补充未显式收集的图片: {os.path.basename(png_path)}")
print(f"[SEARCH] [自动发现] 补充未显式收集的图片: {os.path.basename(png_path)}")
all_figures.append({
"figure_number": "Auto",
"filename": os.path.basename(png_path),
@@ -484,11 +492,11 @@ class DataAnalysisAgent:
"analysis": "该图表由系统自动捕获Agent未提供具体分析文本请结合图表标题理解"
})
except Exception as e:
print(f"⚠️ 自动发现图片失败: {e}")
print(f"[WARN] 自动发现图片失败: {e}")
# ---------------------------
print(f"🔢 总轮数: {self.current_round}")
print(f"📈 收集图片: {len(all_figures)}")
print(f"[NUM] 总轮数: {self.current_round}")
print(f"[GRAPH] 收集图片: {len(all_figures)}")
# 构建用于生成最终报告的提示词
final_report_prompt = self._build_final_report_prompt(all_figures)
@@ -512,12 +520,12 @@ class DataAnalysisAgent:
except:
pass # 解析失败则保持原样
print(" 最终报告生成完成")
print("[OK] 最终报告生成完成")
print(" 最终报告生成完成")
print("[OK] 最终报告生成完成")
except Exception as e:
print(f" 生成最终报告时出错: {str(e)}")
print(f"[ERROR] 生成最终报告时出错: {str(e)}")
final_report_content = f"报告生成失败: {str(e)}"
# 保存最终报告到文件
@@ -525,9 +533,21 @@ class DataAnalysisAgent:
try:
with open(report_file_path, "w", encoding="utf-8") as f:
f.write(final_report_content)
print(f"📄 最终报告已保存至: {report_file_path}")
print(f"[DOC] 最终报告已保存至: {report_file_path}")
except Exception as e:
print(f" 保存报告文件失败: {str(e)}")
print(f"[ERROR] 保存报告文件失败: {str(e)}")
# 生成可复用脚本
script_path = ""
try:
script_path = generate_reusable_script(
analysis_results=self.analysis_results,
data_files=self.data_files,
session_output_dir=self.session_output_dir,
user_requirement=self.user_requirement
)
except Exception as e:
print(f"[WARN] 脚本生成失败: {e}")
# 返回完整的分析结果
return {
@@ -538,6 +558,7 @@ class DataAnalysisAgent:
"conversation_history": self.conversation_history,
"final_report": final_report_content,
"report_file_path": report_file_path,
"reusable_script_path": script_path,
}
def _build_final_report_prompt(self, all_figures: List[Dict[str, Any]]) -> str:
@@ -584,7 +605,7 @@ class DataAnalysisAgent:
# 在提示词中明确要求使用相对路径
prompt += """
📁 **图片路径使用说明**
[FOLDER] **图片路径使用说明**
报告和图片都在同一目录下,请在报告中使用相对路径引用图片:
- 格式:![图片描述](./图片文件名.png)
- 示例:![营业总收入趋势](./营业总收入趋势.png)