-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvisual_debugger.py
More file actions
106 lines (91 loc) · 3.99 KB
/
Copy pathvisual_debugger.py
File metadata and controls
106 lines (91 loc) · 3.99 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
import os
import time
import base64
class VisualDebugger:
def __init__(self, llm_client, model_name="gpt-5"):
self.llm = llm_client
self.model_name = model_name
self.last_completion = None
self.last_latency = None
self.last_status = None
self.expected_behaviors = {
"Oscillator": "The output (Vout) should be a periodic waveform with stable amplitude.",
"Schmitt": "The output (Vout) should be a square wave with hysteresis.",
"Integrator": "The output should be the time integral of the input.",
"Differentiator": "The output should be the time derivative of the input.",
"LowPass": "High gain at low frequencies, roll-off at high frequencies.",
"HighPass": "Attenuation at low frequencies, high gain at high frequencies.",
"Amplifier": "The output should be an amplified undistorted version of the input.",
"Opamp": "High open-loop gain and stable DC behavior."
}
def _encode_image(self, image_path):
if not image_path or not os.path.exists(image_path):
return None
try:
with open(image_path, "rb") as image_file:
return base64.b64encode(image_file.read()).decode("utf-8")
except:
return None
# 新增方法:为了适配 DesignOptimizer 的调用
def construct_debug_message(self, image_path, error_info, task_description):
base64_image = self._encode_image(image_path)
return {
"has_image": True if base64_image else False,
"content_text": f"Task: {task_description}\nSimulation Info: {error_info}",
"content_image": base64_image
}
def debug(self, image_path, task_description):
self.last_completion = None
self.last_latency = None
self.last_status = None
base64_image = self._encode_image(image_path)
if not base64_image:
self.last_status = "no_image"
return "No waveform image found."
expected_behavior = "The output should match expected circuit behavior."
for key, desc in self.expected_behaviors.items():
if key.lower() in str(task_description).lower():
expected_behavior = desc
break
prompt_text = (
f"Circuit Type: {task_description}\n"
f"Expected Behavior: {expected_behavior}\n"
f"Functional verification failed.\n\n"
f"Analyze the waveform image and provide:\n"
f"1) Visual analysis\n"
f"2) Diagnosis\n"
f"3) Suggested parameter or topology fixes\n"
)
t0 = time.time()
try:
# 🔹 统一使用 OpenAI 标准接口
# 不再区分 gpt-5 还是 local,只要 self.llm 是 OpenAI 客户端,调用方式就一致
response = self.llm.chat.completions.create(
model=self.model_name,
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt_text},
{
"type": "image_url",
"image_url": {
"url": f"data:image/png;base64,{base64_image}"
},
},
],
}
],
max_tokens=1024,
temperature=0.01,
)
# 获取内容的路径固定为 choices[0].message.content
content = response.choices[0].message.content
self.last_latency = time.time() - t0
self.last_completion = response
self.last_status = "ok"
return content
except Exception as e:
self.last_latency = time.time() - t0
self.last_status = "error"
return f"Visual analysis failed: {str(e)}"