-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
310 lines (257 loc) · 11.7 KB
/
main.py
File metadata and controls
310 lines (257 loc) · 11.7 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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# imported the libraries and modules [FROM Jupyter Notebook]
import openai
import os
import httpx
import inspect
from typing import Any, Mapping
import logging
import asyncio
import uuid
from dataclasses import dataclass, field
# using all the required functions [FROM Jupyter Notebook]
from openai.types.chat import (
ChatCompletionMessageParam, ChatCompletionAssistantMessageParam,
ChatCompletionUserMessageParam, ChatCompletionSystemMessageParam
)
_framework_log = logging.getLogger('permai.util')
openai.api_key = os.getenv('OPENAI_KEY')
@dataclass
class GenerationResponse():
text: str
model: str
task: str
prompt_id: str
input_tokens: int | None = None
output_tokens: int | None = None
failure_reason: str | None = None
response_id: str = field(default_factory=lambda: str(uuid.uuid4()), repr=False)
def __post_init__(self):
"""
mypy will catch most typing errors, but objects which may get loaded from files
may need additional validation. also colab doesn't really do mypy, so some
extra validation is nice. __post_init__ is a handy dunder method for running
validations on dataclasses without messing with the default __init__ method,
which can get messy, to say the least.
"""
for fkey in ['text', 'model', 'task', 'prompt_id']:
if not isinstance(getattr(self, fkey), str):
raise ValueError(f'{fkey} field expected string, got {getattr(self, fkey)}')
for fkey in ['input_tokens', 'output_tokens']:
if (fv := getattr(self, fkey)) and not isinstance(fv, int):
raise ValueError(f'{fkey} field expected int, got {fv}')
for fkey in ['failure_reason', 'response_id']:
if (fv := getattr(self, fkey)) and not isinstance(fv, str):
raise ValueError(f'{fkey} field expected str, got {fv}')
def openai_dicts_to_message_params(messages: list[dict[str, str]]) -> list[ChatCompletionMessageParam]:
foo: list[ChatCompletionMessageParam] = []
for m in messages:
if m['role'] == 'system':
foo.append(ChatCompletionSystemMessageParam(content=m['content'], role='system'))
elif m['role'] == 'user':
foo.append(ChatCompletionUserMessageParam(content=m['content'], role='user'))
elif m['role'] == 'assistant':
foo.append(ChatCompletionAssistantMessageParam(content=m['content'], role='assistant'))
else:
raise ValueError(f'unknown role {m["role"]}')
return foo
async def call_openai_compat_with_retries(
model: str,
messages: list[dict[str, str]],
client: openai.AsyncOpenAI,
provider_name: str = 'openai',
max_retry: int = 10,
n: int = 1, # NOTE: only for openai usage
model_parameters: Mapping[str, Any] | None = None,
*,
prompt_id: str = 'adhoc',
task: str = 'default',
raise_exceptions=False,
quiet: bool = False) -> list[GenerationResponse]:
"""
This is the real function that gets used for calling OpenAI. Highly recommend
using n=3 rather than n=1.
"""
if model_parameters is None:
model_parameters = {}
local_logger = _framework_log.info
if quiet:
local_logger = _framework_log.debug
message_params = openai_dicts_to_message_params(messages)
_framework_log.debug(f'Using model {model}, {n} generations')
attempts = 0
response = None
# some parameters from togetherai have to be passed as 'extra body' as the openai
# client library does not support them directly on the completions API
funcsig = inspect.signature(client.chat.completions.create)
openai_params = {
k: v for k, v in model_parameters.items() if k in funcsig.parameters
}
extra_body = {
k: v for k, v in model_parameters.items() if k not in funcsig.parameters
}
while attempts < max_retry:
try:
attempts += 1
response = await client.chat.completions.create(
messages=message_params,
model=model,
timeout=120,
n=n,
extra_body=extra_body,
**openai_params,
)
break
except openai.RateLimitError:
_framework_log.info(f'openai.RateLimitError...Retrying in {10 * attempts} seconds')
await asyncio.sleep(10 * attempts)
except openai.APITimeoutError:
_framework_log.info(f'openai.Timeout...Retrying in {2 * attempts} seconds')
await asyncio.sleep(2 * attempts)
except openai.APIConnectionError:
_framework_log.info(f'openai.APIConnectionError...Retrying in {2 * attempts} seconds')
await asyncio.sleep(2 * attempts)
except openai.APIError as exc:
_framework_log.warning(f'Unretryable error: openai.APIError error content is {str(exc)}', exc_info=exc)
break
except TimeoutError:
_framework_log.info(f'TimeoutError...Retrying in {2 * attempts} seconds')
await asyncio.sleep(2 * attempts)
except httpx.ReadTimeout:
_framework_log.info(f'httpx TimeoutError...Retrying in {2 * attempts} seconds')
await asyncio.sleep(2 * attempts)
except Exception as exc:
_framework_log.warning(f'Unhandled exception type raised: {exc}', exc_info=exc)
_framework_log.warning('Giving up since this is unexpected.')
break
if response is None and raise_exceptions:
raise RuntimeError(f'{provider_name} calls failed after {max_retry} attempts.')
elif response is None:
_framework_log.error('Failed to generate a response, but not raising an exception.')
else:
_framework_log.debug(response.choices)
avg_output_tokens = response.usage.completion_tokens // len(response.choices)
return [GenerationResponse(
text=choice.message.content,
model=model,
task=task,
prompt_id=prompt_id,
input_tokens=response.usage.prompt_tokens,
output_tokens=avg_output_tokens)
for choice in response.choices]
return []
OPENAI_CLIENT: openai.AsyncOpenAI | None = None
def init_openai(openai_key: str | None = None) -> openai.AsyncOpenAI:
global OPENAI_CLIENT
if not OPENAI_CLIENT:
openai_key = openai_key or os.getenv('OPENAI_KEY')
OPENAI_CLIENT = openai.AsyncOpenAI(api_key=openai_key)
return OPENAI_CLIENT
def get_openai_client() -> openai.AsyncOpenAI:
return init_openai()
# MAIN FUNCTIONALITY BEGINS HERE
# function to identify the programming language of user code
async def identify_language(user_code):
client = get_openai_client()
user_message = user_code
model = 'gpt-4o'
responses = await call_openai_compat_with_retries(
model = model,
messages = [
{'role': 'system', 'content': 'Identify the programming language of this code. Only provide the language, not a complete sentence'},
{'role': 'user', 'content': user_message},],
client = client,
)
return responses[0].text.strip().capitalize()
# function to check if user code is pythonic or not
async def is_code_pythonic(user_code):
client = get_openai_client()
user_message = user_code
model = 'gpt-4o'
responses = await call_openai_compat_with_retries(
model = model,
messages = [
{'role': 'system', 'content': """Analyze the following Python code and determine \
whether it is Pythonic. Respond with "Yes" or "No" \
followed by a brief explanation. If the code is not Pythonic, explain why and provide suggestions in bullet points and labeled it 'Suggestions'. \
Please do not provide the more Pythonic version of the code yet.""",
},
{'role': 'user', 'content': user_message},],
client = client,
)
return responses[0].text.strip().capitalize()
# function to generate more pythonic version of user code
async def more_pythonic_code(user_code):
client = get_openai_client()
user_message = user_code
model = 'gpt-4o'
responses = await call_openai_compat_with_retries(
model = model,
messages = [
{'role': 'system', 'content': """Rewrite the user code in more Pythonic way first, no explaination. Then \
provide a brief explanation of why it's more preferable.""",
},
{'role': 'user', 'content': user_message},],
client = client,
)
return responses[0].text.strip().capitalize()
async def compare_outputs(user_code, pythonic_code):
client = get_openai_client()
model = 'gpt-4o'
responses = await call_openai_compat_with_retries(
model = model,
messages = [
{'role': 'system', 'content': """Compare the outputs between the user's provided code and the more Pythonic version \
of the code with no explaination first. Then explain the similarities and differences (if any) between the two outputs.""",
},
{'role': 'user', 'content': f"User Code:\n{user_code}\n\nPythonic Code:\n{pythonic_code}"},],
client = client,
)
return responses[0].text.strip().capitalize()
async def main():
language = None
while language != 'python':
# ensure the user enters Python code
try:
user_code = input("Please enter a Python code: ")
language = await identify_language(user_code)
language = language.capitalize().strip()
if language != 'Python':
print("\n")
print("This is not Python code, please enter a Python code.")
# goes onto to analyzing how Pythonic the code is
else:
print("\n")
print("Thank you for providing Python code. Please wait...")
print("\n")
pythonic_analysis = await is_code_pythonic(user_code)
pythonic_analysis = str(pythonic_analysis).strip()
print(f"Is the code Pythonic or not: {pythonic_analysis}")
# if user code is already Pythonic, no need for the evaluator to continue
if pythonic_analysis.capitalize().startswith('Yes'):
print("\n")
print("Your code is already Pythonic!")
break
else:
# provide more Pythonic version depending on user's choice and the outputs between the two
print("\n")
user_choice = input("Would you like to see the more Pythonic version of your code and the output between the two? (y/n): ").strip().lower()
if user_choice == 'y':
pythonic_version = await more_pythonic_code(user_code)
print("\n")
print("Here's the more Pythonic version of your code:")
print(pythonic_version)
comparison_result = await compare_outputs(user_code, pythonic_version)
print("\n")
print("Comparing the outputs:")
print("\n")
print(comparison_result)
# user rather not see the more Pythonic version
else:
print("\n")
print("Okay, I hope the suggestions help! Goodbye!")
break
except Exception as e:
print(f"An error occurred: {e}")
break
if __name__ == "__main__":
asyncio.run(main())