-
Notifications
You must be signed in to change notification settings - Fork 14
/
ChatGPT.py
128 lines (95 loc) · 3.73 KB
/
ChatGPT.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
import json
import sublime
import sublime_plugin
import threading
import urllib.request as request
class ChatGptCommand(sublime_plugin.TextCommand):
def run(self, edit):
self.set_settings()
self.show_input()
def set_settings(self):
settings = sublime.load_settings('ChatGPT.sublime-settings')
self.settings = {
'api_key': str(settings.get('api_key')),
'timeout': int(settings.get('timeout', 10)),
'model': str(settings.get('model', 'text-davinci-003')),
'temperature': float(settings.get('temperature', 0.5)),
'max_tokens': int(settings.get('max_tokens', 1024)),
'debug': bool(settings.get('debug', False))
}
self.debug('settings', self.settings)
def show_input(self):
self.window = sublime.active_window()
self.view = self.window.active_view()
self.window.show_input_panel(
self.show_input_title(),
self.show_input_value(),
self.show_input_done,
None,
None
)
def show_input_title(self):
return 'ChatGPT Question (model: %s | timeout: %s)' % (self.settings['model'], self.settings['timeout'])
def show_input_value(self):
if len(self.settings['api_key']) == 0:
return 'You must set the API Key (Preferences > Package Settings > ChatGPT > Settings)'
for region in self.view.sel():
if not region.empty():
return self.view.substr(region)
return self.view.settings().get('show_input_last', '')
def show_input_done(self, input_string):
if len(self.settings['api_key']) == 0:
return
self.debug('show_input_done[input_string]', input_string)
if len(input_string) == 0:
return
self.view.settings().set('show_input_last', input_string)
Request(self.view, self.settings, input_string).start()
def debug(self, key, value):
if (self.settings['debug']):
print(key, value)
class Request(threading.Thread):
def __init__(self, view, settings, prompt):
self.view = view
self.settings = settings
self.prompt = prompt
super(Request, self).__init__()
def run(self):
contents = self.request().replace('\\', '\\\\').replace('$', '\\$')
self.view.run_command('insert_snippet', {'contents': contents})
def request(self):
response = self.request_response()
data = self.request_data()
timeout = self.settings['timeout']
self.debug('request[data]', data)
try:
text = request.urlopen(response, data=data, timeout=timeout).read().decode('utf-8')
self.debug('request[response]', text)
text = str(json.loads(text)['choices'][0]['text'])
if len(text) == 0:
text = '# No Response #'
except Exception as e:
text = '# Error: %s #' % str(e)
self.debug('request[text]', text)
return text
def request_response(self):
return request.Request(
url='https://api.openai.com/v1/completions',
method='POST',
headers=self.request_headers()
)
def request_headers(self):
return {
'Authorization': 'Bearer %s' % self.settings['api_key'],
'Content-Type': 'application/json'
}
def request_data(self):
return json.dumps({
'prompt': self.prompt,
'model': self.settings['model'],
'temperature': self.settings['temperature'],
'max_tokens': self.settings['max_tokens']
}).encode()
def debug(self, key, value):
if (self.settings['debug']):
print(key, value)