-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.py
183 lines (159 loc) · 6.69 KB
/
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import logging
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.CopyToClipboardAction import CopyToClipboardAction
from ulauncher.api.shared.action.DoNothingAction import DoNothingAction
import requests
import json
logger = logging.getLogger(__name__)
EXTENSION_ICON = 'images/icon.png'
def wrap_text(text, max_w):
words = text.split()
lines = []
current_line = ''
for word in words:
if len(current_line + word) <= max_w:
current_line += ' ' + word
else:
lines.append(current_line.strip())
current_line = word
lines.append(current_line.strip())
return '\n'.join(lines)
class GPTExtension(Extension):
"""
Ulauncher extension to generate text using GPT-3
"""
def __init__(self):
super(GPTExtension, self).__init__()
logger.info('GPT-3 extension started')
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
"""
Event listener for KeywordQueryEvent
"""
def on_event(self, event, extension):
endpoint = "https://api.openai.com/v1/chat/completions"
logger.info('Processing user preferences')
# Get user preferences
try:
api_key = extension.preferences['api_key']
max_tokens = int(extension.preferences['max_tokens'])
frequency_penalty = float(
extension.preferences['frequency_penalty'])
presence_penalty = float(extension.preferences['presence_penalty'])
temperature = float(extension.preferences['temperature'])
top_p = float(extension.preferences['top_p'])
system_prompt = extension.preferences['system_prompt']
line_wrap = int(extension.preferences['line_wrap'])
model = extension.preferences['model']
# pylint: disable=broad-except
except Exception as err:
logger.error('Failed to parse preferences: %s', str(err))
return RenderResultListAction([
ExtensionResultItem(icon=EXTENSION_ICON,
name='Failed to parse preferences: ' +
str(err),
on_enter=CopyToClipboardAction(str(err)))
])
# Get search term
search_term = event.get_argument()
logger.info('The search term is: %s', search_term)
# Display blank prompt if user hasn't typed anything
if not search_term:
logger.info('Displaying blank prompt')
return RenderResultListAction([
ExtensionResultItem(icon=EXTENSION_ICON,
name='Type in a prompt...',
on_enter=DoNothingAction())
])
# Create POST request
headers = {
'content-type': 'application/json',
'Authorization': 'Bearer ' + api_key
}
body = {
"messages": [
{
"role": "system",
"content": system_prompt
},
{
"role": "user",
"content": search_term
}
],
"temperature": temperature,
"max_tokens": max_tokens,
"top_p": top_p,
"frequency_penalty": frequency_penalty,
"presence_penalty": presence_penalty,
"model": model,
}
body = json.dumps(body)
logger.info('Request body: %s', str(body))
logger.info('Request headers: %s', str(headers))
# Send POST request
try:
logger.info('Sending request')
response = requests.post(
endpoint, headers=headers, data=body, timeout=10)
# pylint: disable=broad-except
except Exception as err:
logger.error('Request failed: %s', str(err))
return RenderResultListAction([
ExtensionResultItem(icon=EXTENSION_ICON,
name='Request failed: ' + str(err),
on_enter=CopyToClipboardAction(str(err)))
])
logger.info('Request succeeded')
logger.info('Response: %s', str(response))
# Get response
# Choice schema
# { message: Message, finish_reason: string, index: number }
# Message schema
# { role: string, content: string }
try:
response = response.json()
choices = response['choices']
# pylint: disable=broad-except
except Exception as err:
logger.error('Failed to parse response: %s', str(response))
errMsg = "Unknown error, please check logs for more info"
try:
errMsg = response['error']['message']
except Exception:
pass
return RenderResultListAction([
ExtensionResultItem(icon=EXTENSION_ICON,
name='Failed to parse response: ' +
errMsg,
on_enter=CopyToClipboardAction(str(errMsg)))
])
items: list[ExtensionResultItem] = []
try:
for choice in choices:
message = choice['message']['content']
message = wrap_text(message, line_wrap)
items.append(ExtensionResultItem(icon=EXTENSION_ICON, name="Assistant", description=message,
on_enter=CopyToClipboardAction(message)))
# pylint: disable=broad-except
except Exception as err:
logger.error('Failed to parse response: %s', str(response))
return RenderResultListAction([
ExtensionResultItem(icon=EXTENSION_ICON,
name='Failed to parse response: ' +
str(response),
on_enter=CopyToClipboardAction(str(err)))
])
try:
item_string = ' | '.join([item.description for item in items])
logger.info("Results: %s", item_string)
except Exception as err:
logger.error('Failed to log results: %s', str(err))
logger.error('Results: %s', str(items))
return RenderResultListAction(items)
if __name__ == '__main__':
GPTExtension().run()