68 lines
2.2 KiB
Python
68 lines
2.2 KiB
Python
"""
|
|
Causal Inference Agent CLI
|
|
|
|
命令行入口,支持直接对数据文件执行因果推断分析。
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from causal_agent.agent import CausalInferenceAgent
|
|
from causal_agent.core.config import AgentConfig
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(
|
|
description="通用因果推断 Agent",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog="""
|
|
示例:
|
|
python -m causal_agent --data data.csv --output report.json
|
|
python -m causal_agent --data data.xlsx --model qwen3.5-35b
|
|
python -m causal_agent --data data.csv --prompt "分析教育干预对成绩的影响"
|
|
""",
|
|
)
|
|
parser.add_argument("--data", "-d", required=True, help="数据文件路径")
|
|
parser.add_argument("--output", "-o", help="报告输出 JSON 文件路径")
|
|
parser.add_argument("--prompt", "-p", help="自定义分析提示词")
|
|
parser.add_argument("--base-url", help="LLM API Base URL")
|
|
parser.add_argument("--model", help="LLM 模型名称")
|
|
parser.add_argument("--temperature", type=float, help="LLM 温度参数")
|
|
parser.add_argument("--log-path", help="日志文件路径")
|
|
parser.add_argument("--corr-threshold", type=float, help="相关性筛查阈值")
|
|
|
|
args = parser.parse_args()
|
|
|
|
if not os.path.exists(args.data):
|
|
print(f"错误:数据文件不存在:{args.data}", file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
# 构建配置
|
|
config = AgentConfig.from_env()
|
|
if args.base_url:
|
|
config.llm_base_url = args.base_url
|
|
if args.model:
|
|
config.llm_model = args.model
|
|
if args.temperature is not None:
|
|
config.llm_temperature = args.temperature
|
|
if args.log_path:
|
|
config.log_path = args.log_path
|
|
if args.corr_threshold is not None:
|
|
config.corr_threshold = args.corr_threshold
|
|
|
|
agent = CausalInferenceAgent(config)
|
|
result = agent.analyze(args.data, custom_prompt=args.prompt)
|
|
|
|
if args.output:
|
|
with open(args.output, "w", encoding="utf-8") as f:
|
|
json.dump(result["report"], f, ensure_ascii=False, indent=2)
|
|
print(f"\n报告已保存到:{args.output}")
|
|
|
|
print("\n分析完成!")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|