Files
iov_ana/cli.py
OpenClaw Agent 96927a789d feat: 四层架构数据分析 Agent
- Layer 1 Planner: 意图规划,将问题转为结构化分析计划
- Layer 2 Explorer: 自适应探索循环,多轮迭代动态生成 SQL
- Layer 3 InsightEngine: 异常检测 + 主动洞察
- Layer 4 ContextManager: 多轮对话上下文记忆

安全设计:AI 只看 Schema + 聚合结果,不接触原始数据。
支持任意 OpenAI 兼容 API(OpenAI / Ollama / DeepSeek / vLLM)
2026-03-19 12:21:04 +08:00

113 lines
3.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
交互式 CLI —— 四层架构自适应分析
用法: python3 cli.py [数据库路径]
"""
import os
import sys
sys.path.insert(0, os.path.dirname(__file__))
from config import DB_PATH, LLM_CONFIG, MAX_EXPLORATION_ROUNDS
from agent import DataAnalysisAgent
def print_help():
print("""
可用命令:
<问题> 分析一个问题
rounds=<N> <问题> 设置探索轮数
schema 查看数据库 Schema
history 查看分析历史
audit 查看 SQL 审计日志
clear 清空分析历史
help 显示帮助
quit / q 退出
""")
def main():
db_path = sys.argv[1] if len(sys.argv) > 1 else DB_PATH
if not os.path.exists(db_path):
print(f"❌ 数据库不存在: {db_path}")
print(f" 请先运行 python3 demo.py 创建示例数据库,或指定已有数据库路径")
sys.exit(1)
if not LLM_CONFIG["api_key"]:
print("⚠️ 未配置 LLM_API_KEY请先设置环境变量")
print(" export LLM_API_KEY=sk-xxx")
print(" export LLM_BASE_URL=https://api.openai.com/v1 # 或 Ollama 等")
print(" export LLM_MODEL=gpt-4o")
sys.exit(1)
# 初始化 Agent
agent = DataAnalysisAgent(db_path)
print("=" * 60)
print(" 🤖 数据分析 Agent —— 四层架构")
print("=" * 60)
print(f"\n🔗 LLM: {LLM_CONFIG['model']} @ {LLM_CONFIG['base_url']}")
print(f"🔄 最大探索轮数: {MAX_EXPLORATION_ROUNDS}")
print(f"💾 数据库: {db_path}")
print(f"\n💬 输入分析问题help 查看命令)\n")
while True:
try:
user_input = input("📊 > ").strip()
except (EOFError, KeyboardInterrupt):
print("\n👋 再见!")
break
if not user_input:
continue
cmd = user_input.lower()
if cmd in ("quit", "exit", "q"):
print("👋 再见!")
break
elif cmd == "help":
print_help()
continue
elif cmd == "schema":
print(agent.get_schema())
continue
elif cmd == "history":
print(agent.get_history())
continue
elif cmd == "audit":
print(agent.get_audit())
continue
elif cmd == "clear":
agent.clear_history()
print("✅ 历史已清空")
continue
# 解析可选参数rounds=3
max_rounds = MAX_EXPLORATION_ROUNDS
question = user_input
if "rounds=" in question.lower():
parts = question.split("rounds=")
question = parts[0].strip()
try:
max_rounds = int(parts[1].strip().split()[0])
except (ValueError, IndexError):
pass
try:
report = agent.analyze(question, max_rounds=max_rounds)
print("\n" + report)
print("\n" + "~" * 60)
except Exception as e:
print(f"\n❌ 分析出错: {e}")
import traceback
traceback.print_exc()
# 退出时显示审计
print("\n📋 本次会话审计:")
print(agent.get_audit())
if __name__ == "__main__":
main()