Skip to content

Commit b54c39d

Browse files
committed
WIP: hot cache work in progress
1 parent cf79ef3 commit b54c39d

File tree

9,537 files changed

+4491965
-26
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

9,537 files changed

+4491965
-26
lines changed

DeepSeekCodeAnalyzer.py

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
import requests
2+
import json
3+
import os
4+
from pathlib import Path
5+
import readline
6+
import glob
7+
8+
class DeepSeekCodeAnalyzer:
9+
def __init__(self, api_key):
10+
self.api_key = api_key
11+
self.api_url = "https://api.deepseek.com/v1/chat/completions" # 请替换为实际API端点
12+
self.headers = {
13+
"Authorization": f"Bearer {api_key}",
14+
"Content-Type": "application/json"
15+
}
16+
17+
def analyze_code(self, code_file_path, prompt_template=None):
18+
# 读取代码文件
19+
with open(code_file_path, 'r', encoding='utf-8') as f:
20+
code_content = f.read()
21+
22+
# 准备提示词
23+
if prompt_template is None:
24+
prompt = f"""
25+
请分析以下C++代码的时间复杂度以及可以改进的点:
26+
27+
{code_content}
28+
29+
请提供:
30+
1. 整体时间复杂度分析
31+
2. 关键函数/方法的时间复杂度
32+
3. 内存使用分析
33+
4. 具体的改进建议
34+
5. 代码优化示例
35+
"""
36+
else:
37+
prompt = prompt_template.format(code=code_content)
38+
39+
# 准备API请求数据
40+
payload = {
41+
"model": "deepseek-coder", # 使用代码专用模型
42+
"messages": [
43+
{"role": "user", "content": prompt}
44+
],
45+
"temperature": 0.1, # 低随机性以获得更确定的回答
46+
"max_tokens": 4000 # 根据需要进行调整
47+
}
48+
49+
# 发送请求
50+
try:
51+
response = requests.post(
52+
self.api_url,
53+
headers=self.headers,
54+
json=payload,
55+
timeout=None # None = 無限等待
56+
)
57+
response.raise_for_status()
58+
59+
# 解析响应
60+
result = response.json()
61+
analysis = result['choices'][0]['message']['content']
62+
63+
return analysis
64+
65+
except Exception as e:
66+
print(f"API请求失败: {e}")
67+
return None
68+
69+
def save_analysis(self, analysis_result, output_path):
70+
with open(output_path, 'w', encoding='utf-8') as f:
71+
f.write(analysis_result)
72+
print(f"分析结果已保存到: {output_path}")
73+
74+
def _complete_path(text, state):
75+
# 展開 ~ 與 $ENV
76+
text = os.path.expanduser(os.path.expandvars(text))
77+
78+
# 空字串時,預設從當前目錄開始
79+
if not text:
80+
text = './'
81+
82+
# 拆出目錄與前綴
83+
dirname, prefix = os.path.split(text)
84+
if not dirname:
85+
dirname = '.'
86+
87+
try:
88+
entries = os.listdir(dirname)
89+
except Exception:
90+
entries = []
91+
92+
# 做匹配
93+
matches = []
94+
for name in entries:
95+
if name.startswith(prefix):
96+
full = os.path.join(dirname, name)
97+
# 目錄補全時自動加 '/'
98+
if os.path.isdir(full):
99+
full += '/'
100+
matches.append(full)
101+
102+
# 也支援使用者原本寫的 glob 型式(例如 src/*.cpp)
103+
if '*' in text or '?' in text or '[' in text:
104+
matches.extend(glob.glob(text))
105+
106+
# 去重、排序
107+
matches = sorted(set(matches))
108+
109+
return matches[state] if state < len(matches) else None
110+
111+
# 讓 '/' 不被當成分隔符,保留路徑連續性
112+
readline.set_completer_delims(' \t\n;')
113+
114+
# macOS 內建 Python 多半是 libedit;Linux/自裝 Python 多半是 GNU readline
115+
if 'libedit' in readline.__doc__:
116+
# libedit 的綁定語法
117+
readline.parse_and_bind("bind ^I rl_complete")
118+
else:
119+
# GNU readline 的綁定語法
120+
readline.parse_and_bind("tab: complete")
121+
122+
readline.set_completer(_complete_path)
123+
124+
# 使用示例
125+
if __name__ == "__main__":
126+
# 替换为您的API密钥
127+
API_KEY = "sk-9cafb2e074bf4b348af4a075c19ccf6b"
128+
129+
# 创建分析器实例
130+
analyzer = DeepSeekCodeAnalyzer(API_KEY)
131+
132+
# 讓使用者輸入檔案名稱
133+
code_file = input("請輸入要分析的程式碼檔案名稱(含路徑):").strip()
134+
135+
# 自定义提示词(可选)
136+
custom_prompt = """
137+
作为资深C++性能优化专家,请详细分析以下LLVM IRTranslator代码:
138+
139+
{code}
140+
141+
请重点关注:
142+
1. 算法复杂度分析(最好、最坏、平均情况)
143+
2. 内存访问模式和缓存友好性
144+
3. 潜在的性能瓶颈
145+
4. 并行化机会
146+
5. LLVM特定最佳实践
147+
6. 具体重构建议和代码示例
148+
"""
149+
150+
# 执行分析
151+
print("正在分析代码,请稍候...")
152+
result = analyzer.analyze_code(code_file, custom_prompt)
153+
154+
if result:
155+
# 保存分析结果
156+
output_file = "code_analysis_report.txt"
157+
analyzer.save_analysis(result, output_file)
158+
159+
# 打印部分结果预览
160+
print("\n分析结果预览:")
161+
print("=" * 50)
162+
print(result)
163+
else:
164+
print("分析失败")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'c' 'e'

0 commit comments

Comments
 (0)