-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
182 lines (152 loc) · 5.08 KB
/
run.py
File metadata and controls
182 lines (152 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""
代码分析Agent系统启动脚本
"""
import os
import sys
import subprocess
def check_python_version():
"""检查Python版本"""
if sys.version_info < (3, 8):
print("❌ 需要Python 3.8或更高版本")
sys.exit(1)
print(f"✅ Python版本: {sys.version}")
def install_requirements():
"""安装依赖"""
print("📦 检查并安装依赖...")
try:
import langchain
import langgraph
import rich
print("✅ 主要依赖已安装")
return True
except ImportError:
print("⚠️ 检测到缺少依赖,正在安装...")
try:
subprocess.check_call([
sys.executable, "-m", "pip", "install", "-r", "requirements.txt"
])
print("✅ 依赖安装完成")
return True
except subprocess.CalledProcessError:
print("❌ 依赖安装失败")
return False
def check_environment():
"""检查环境配置"""
print("🔧 检查环境配置...")
# 检查LLM提供商
llm_provider = os.getenv('LLM_PROVIDER', 'openai').lower()
print(f"🤖 LLM提供商: {llm_provider.upper()}")
if llm_provider == 'qwen':
# 检查Qwen API Key
api_key = os.getenv('QWEN_API_KEY')
if not api_key:
print("⚠️ 未设置QWEN_API_KEY环境变量")
print("请设置您的通义千问API密钥:")
print("export QWEN_API_KEY='your-qwen-api-key-here'")
print("或在.env文件中配置")
return False
else:
# 检查OpenAI API Key
api_key = os.getenv('OPENAI_API_KEY')
if not api_key:
print("⚠️ 未设置OPENAI_API_KEY环境变量")
print("请设置您的OpenAI API密钥:")
print("export OPENAI_API_KEY='your-api-key-here'")
print("或在.env文件中配置")
return False
print("✅ 环境配置检查通过")
return True
def show_usage():
"""显示使用说明"""
print("""
🤖 代码分析Agent系统
==================
使用方法:
python run.py demo # 运行演示模式
python run.py analyze [目录] [需求] # 分析指定目录
python run.py example # 运行使用示例
python run.py test # 运行测试
python run.py model-info # 显示模型信息
环境配置:
设置LLM_PROVIDER=openai 或 qwen
OpenAI: 设置OPENAI_API_KEY
Qwen: 设置QWEN_API_KEY
示例:
python run.py demo
python run.py analyze ./my_project "分析代码质量"
python run.py model-info
""")
def run_demo():
"""运行演示模式"""
print("🎯 启动演示模式...")
os.system(f"{sys.executable} main.py --demo")
def run_analysis(directory=None, request=None):
"""运行代码分析"""
directory = directory or "."
request = request or "分析项目代码质量"
print(f"🔍 开始分析目录: {directory}")
print(f"📋 分析需求: {request}")
cmd = f'{sys.executable} main.py --directory "{directory}" --request "{request}" --save'
os.system(cmd)
def run_example():
"""运行使用示例"""
print("💡 运行使用示例...")
os.system(f"{sys.executable} example.py")
def run_tests():
"""运行测试"""
print("🧪 运行测试...")
# 检查pytest是否安装
try:
import pytest
except ImportError:
print("📦 安装pytest...")
subprocess.check_call([sys.executable, "-m", "pip", "install", "pytest", "pytest-asyncio"])
# 运行测试
os.system(f"{sys.executable} -m pytest tests/ -v")
def show_model_info():
"""显示模型信息"""
print("🤖 模型信息查看...")
os.system(f"{sys.executable} main.py --model-info")
def main():
"""主函数"""
print("🚀 代码分析Agent系统启动器")
print("=" * 40)
# 检查Python版本
check_python_version()
# 解析命令行参数
if len(sys.argv) < 2:
show_usage()
return
command = sys.argv[1].lower()
# 安装依赖
if not install_requirements():
return
# 检查环境(除了测试模式)
if command != 'test' and not check_environment():
print("⚠️ 环境检查失败,但您仍可以运行测试和示例")
# 执行命令
if command == 'demo':
run_demo()
elif command == 'analyze':
directory = sys.argv[2] if len(sys.argv) > 2 else None
request = sys.argv[3] if len(sys.argv) > 3 else None
run_analysis(directory, request)
elif command == 'example':
run_example()
elif command == 'test':
run_tests()
elif command == 'model-info':
show_model_info()
else:
print(f"❌ 未知命令: {command}")
show_usage()
if __name__ == "__main__":
try:
main()
except KeyboardInterrupt:
print("\n🛑 用户中断操作")
except Exception as e:
print(f"❌ 执行错误: {e}")
import traceback
traceback.print_exc()