-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChatGPT_main.py
154 lines (111 loc) · 4.22 KB
/
ChatGPT_main.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
import openai
import re
import os
from termcolor import colored
from prettytable import PrettyTable
from key import OpenAi_Key as GptKey
openai.api_key = GptKey
Model = 'gpt-3.5-turbo'
def search(prompt):
postPromt = "{type: smart contract; programming language: solidity;}"
prompt = prompt+postPromt
response=openai.ChatCompletion.create(
model=Model,
messages=[{"role":"user",
"content":prompt}
],
max_tokens=2048,
)
Message = response.choices[0].message.content
# Message = """pragma solidity ^0.4.24;
# contract Addition {
# function add(uint a, uint b) public pure returns (uint) {
# return a + b;
# }
# }
# For debugging purpose"""
history[prompt] = Message
# print(Message)
return Message
def advanced_search(prompt):
postPromt = "{type: smart contract; programming language: solidity;}"
prompt = prompt+postPromt
response = openai.ChatCompletion.create(
model=Model,
messages=[{"role":"assistant", "content":history[list(history)[-1]]},
{"role":"user",
"content":prompt}
],
max_tokens=2048,
)
Message = response.choices[0].message.content
# Message = """pragma solidity ^0.4.24;
# contract Advanced_Addition {
# function add(uint a, uint b) public pure returns (uint) {
# return a + b;
# }
# }
# For debugging purpose"""
history[prompt] = Message
# print(Message)
return Message
def user_satisfaction(message):
user_choice = input(colored("Satisfy with Output? (y/n) >>> ", 'yellow', attrs=['bold']))
if user_choice == "y":
return
elif user_choice == "n":
user_prompt = input(colored("Enter Improvisations in Output? >>> ", 'light_magenta', attrs=['bold']))
return advanced_search(user_prompt)
else:
print(colored("Invalid Input, breaking to main...", 'red', attrs=['bold']))
return
def History():
for key, value in history.items():
table.add_row([key, value], divider=True)
print(table)
return
def create_SOLFile():
pattern = r"(?s)pragma solidity.*\}"
matches = re.findall(pattern, history[list(history)[-1]])
solidity_code = ''.join(matches)
filepath = './Contracts'
file_base_name = 'SOLFile'
file_extension = '.sol'
i = 1
filename = os.path.join(filepath, file_base_name + file_extension)
while os.path.exists(filename):
filename = os.path.join(filepath, f'{file_base_name}_{i}{file_extension}')
i += 1
try:
with open(filename, 'w') as f:
f.write(solidity_code)
f.close()
except IOError:
print(colored(f"Error: Could not write to file '{filename}'", 'red', attrs=['bold']))
return None
print(colored("SOL File Created", 'green', attrs=['bold']))
with open('FileHistory.txt', 'a') as f:
f.write(filename)
f.write("\n")
f.close()
return filename
history = {}
if __name__ == '__main__':
prompt = input(colored("Enter Prompt you want to search for? >>> ", 'light_magenta', attrs=['bold']))
table = PrettyTable(["Prompt", "Output"])
table.align = "l"
table.max_width = 120
history = {}
search(prompt)
print(colored("\n<-----Searching Done----->\n", 'red', attrs=['bold']))
history_check = input(colored("Do you want to see history? (y/n) >>> ", 'yellow', attrs=['bold']))
if history_check == "y":
History()
else:
print(colored("History still be stored in dictonary...", 'red', attrs=['bold']))
deploy = input(colored("Do you want to deploy this code in a SOL file? (y/n) >>> ", 'yellow', attrs=['bold']))
if deploy == "y":
FileName = create_SOLFile()
else:
print(colored("Solidity File not created...", 'red', attrs=['bold']))
print(colored("Thankyou for using our service", 'red', attrs=['bold']))