-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGeneratePinouts.py
157 lines (119 loc) · 4.33 KB
/
GeneratePinouts.py
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
# EvaluatePinouts.py
import time
import json
import argparse
from Words2Wires import engineGPT4, engineGPT35Turbo, engineClaudeV1
# Load the device names, from a TSV file.
# The first column is the device name, the second column (optional) is the device description, the third column is the pinout.
# The data should be stored in a list of dictionaries.
def loadDeviceNames(filenameIn:str):
devices = []
with open(filenameIn, 'r') as f:
print ("* Loading device names from " + filenameIn + "...")
for line in f:
line = line.strip()
if (line == ''):
continue
if (line[0] == '#'):
continue
fields = line.split('\t')
deviceName = fields[0]
deviceDescription = fields[1]
devicePinout = fields[2]
packed = {
'name': deviceName,
'description': deviceDescription,
'pinout': devicePinout
}
devices.append(packed)
print("* Found " + str(len(devices)) + " device names.")
return devices
def doPinoutGeneration(deviceName:str, engine, temperature=0.0, maxTokensOut = 1000):
# Step 1: Create prompt
prompt = f"""Your task is to generate a description and pinout for an electronic component.
The specific electronic component to generate this output for is: {deviceName}
The output format is JSON, between code blocks, as shown in the example below:
```
{{
"7479": {{
"description": "Dual D positive-edge triggered flip flop, asynchronous preset and clear",
"pinout:"["#R1", "D1", "CLK1", "#PR1", "Q1", "#Q1", "VSS", "#Q2", "Q2", "#PR2", "CLK2", "D2", "#R2", "VDD"]
}}
}}
```
"""
# Step 2: Generate the output
response, success, numTokensSent, numTokensReceived = engine.getResponse(prompt, temperature, maxTokensOut)
# Step 3: Pack the output
packed = {
'deviceName': deviceName,
'prompt': prompt,
'response': response,
'success': success,
'numTokensSent': numTokensSent,
'numTokensReceived': numTokensReceived
}
return packed
#
# Parse command line arguments
#
def parseArgs():
desc = "Run Words2Wires"
parser = argparse.ArgumentParser(desc)
# Add engine (default gpt-4)
parser.add_argument("--engine", type=str, default="gpt-3.5-turbo",
help="The engine to use. Options: gpt-3.5-turbo, gpt-4, claude-v1. Default: %(default)s")
args = parser.parse_args()
params = vars(args)
return params
#
# Main
#
def main(args):
engineStr = args['engine']
filenameIn = "experiment1-pinouts/pins100-benchmark.tsv"
filenameOut = "generated.pinouts." + engineStr + ".json"
# Load the device names
devices = loadDeviceNames(filenameIn)
# Instantiate the GPT4 engine
engine = None
if (engineStr == "gpt-4"):
engine = engineGPT4()
elif (engineStr == "gpt-3.5-turbo"):
engine = engineGPT35Turbo()
elif (engineStr == "claude-v1"):
engine = engineClaudeV1()
else:
print("Error: Unknown engine " + engineStr)
return
print("Using engine: " + engineStr + " (" + engine.name + ")")
print("")
time.sleep(2)
responses = []
# For each device, generate a pinout
for idx, device in enumerate(devices):
deviceName = device['name']
deviceDescription = device['description']
print(f"Generating pinout for device {idx+1} of {len(devices)}: {deviceName}")
packedResponse = doPinoutGeneration(deviceName, engine)
responses.append(packedResponse)
# Add any keys from the device dictionary to the response dictionary
for key in device:
if (key not in packedResponse):
packedResponse[key] = device[key]
responseStr = packedResponse['response']
print(responseStr)
print("")
packedOut = {
"engine:": engine.name,
"responses": responses
}
# Save the responses to a JSON file (pretty print)
with open(filenameOut, 'w') as f:
json.dump(packedOut, f, indent=4)
print("Completed.")
if __name__ == "__main__":
# Parse command line arguments
args = parseArgs()
print(args)
main(args)