Spaces:
Sleeping
Sleeping
Delete src/functional.py
Browse files- src/functional.py +0 -116
src/functional.py
DELETED
|
@@ -1,116 +0,0 @@
|
|
| 1 |
-
from bot_backend import *
|
| 2 |
-
import base64
|
| 3 |
-
import time
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
def chat_completion(bot_backend: BotBackend):
|
| 7 |
-
model_choice = bot_backend.gpt_model_choice
|
| 8 |
-
config = bot_backend.config
|
| 9 |
-
kwargs_for_chat_completion = bot_backend.kwargs_for_chat_completion
|
| 10 |
-
|
| 11 |
-
assert config['model'][model_choice]['available'], f"{model_choice} is not available for you API key"
|
| 12 |
-
|
| 13 |
-
response = openai.ChatCompletion.create(**kwargs_for_chat_completion)
|
| 14 |
-
return response
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
def add_function_response_to_bot_history(content_to_display, history, unique_id):
|
| 18 |
-
images, text = [], []
|
| 19 |
-
|
| 20 |
-
# terminal output
|
| 21 |
-
error_occurred = False
|
| 22 |
-
for mark, out_str in content_to_display:
|
| 23 |
-
if mark in ('stdout', 'execute_result_text', 'display_text'):
|
| 24 |
-
text.append(out_str)
|
| 25 |
-
elif mark in ('execute_result_png', 'execute_result_jpeg', 'display_png', 'display_jpeg'):
|
| 26 |
-
if 'png' in mark:
|
| 27 |
-
images.append(('png', out_str))
|
| 28 |
-
else:
|
| 29 |
-
images.append(('jpg', out_str))
|
| 30 |
-
elif mark == 'error':
|
| 31 |
-
text.append(delete_color_control_char(out_str))
|
| 32 |
-
error_occurred = True
|
| 33 |
-
text = '\n'.join(text).strip('\n')
|
| 34 |
-
if error_occurred:
|
| 35 |
-
history.append([None, f'❌Terminal output:\n```shell\n\n{text}\n```'])
|
| 36 |
-
else:
|
| 37 |
-
history.append([None, f'✔️Terminal output:\n```shell\n{text}\n```'])
|
| 38 |
-
|
| 39 |
-
# image output
|
| 40 |
-
for filetype, img in images:
|
| 41 |
-
image_bytes = base64.b64decode(img)
|
| 42 |
-
temp_path = f'cache/temp_{unique_id}'
|
| 43 |
-
if not os.path.exists(temp_path):
|
| 44 |
-
os.mkdir(temp_path)
|
| 45 |
-
path = f'{temp_path}/{hash(time.time())}.{filetype}'
|
| 46 |
-
with open(path, 'wb') as f:
|
| 47 |
-
f.write(image_bytes)
|
| 48 |
-
history.append(
|
| 49 |
-
[
|
| 50 |
-
None,
|
| 51 |
-
f'<img src=\"file={path}\" style=\'width: 600px; max-width:none; max-height:none\'>'
|
| 52 |
-
]
|
| 53 |
-
)
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
def parse_json(function_args: str, finished: bool):
|
| 57 |
-
"""
|
| 58 |
-
GPT may generate non-standard JSON format string, which contains '\n' in string value, leading to error when using
|
| 59 |
-
`json.loads()`.
|
| 60 |
-
Here we implement a parser to extract code directly from non-standard JSON string.
|
| 61 |
-
:return: code string if successfully parsed otherwise None
|
| 62 |
-
"""
|
| 63 |
-
parser_log = {
|
| 64 |
-
'met_begin_{': False,
|
| 65 |
-
'begin_"code"': False,
|
| 66 |
-
'end_"code"': False,
|
| 67 |
-
'met_:': False,
|
| 68 |
-
'met_end_}': False,
|
| 69 |
-
'met_end_code_"': False,
|
| 70 |
-
"code_begin_index": 0,
|
| 71 |
-
"code_end_index": 0
|
| 72 |
-
}
|
| 73 |
-
try:
|
| 74 |
-
for index, char in enumerate(function_args):
|
| 75 |
-
if char == '{':
|
| 76 |
-
parser_log['met_begin_{'] = True
|
| 77 |
-
elif parser_log['met_begin_{'] and char == '"':
|
| 78 |
-
if parser_log['met_:']:
|
| 79 |
-
if finished:
|
| 80 |
-
parser_log['code_begin_index'] = index + 1
|
| 81 |
-
break
|
| 82 |
-
else:
|
| 83 |
-
if index + 1 == len(function_args):
|
| 84 |
-
return ''
|
| 85 |
-
else:
|
| 86 |
-
temp_code_str = function_args[index + 1:]
|
| 87 |
-
if '\n' in temp_code_str:
|
| 88 |
-
return temp_code_str.strip('\n')
|
| 89 |
-
else:
|
| 90 |
-
return json.loads(function_args + '"}')['code']
|
| 91 |
-
elif parser_log['begin_"code"']:
|
| 92 |
-
parser_log['end_"code"'] = True
|
| 93 |
-
else:
|
| 94 |
-
parser_log['begin_"code"'] = True
|
| 95 |
-
elif parser_log['end_"code"'] and char == ':':
|
| 96 |
-
parser_log['met_:'] = True
|
| 97 |
-
else:
|
| 98 |
-
continue
|
| 99 |
-
if finished:
|
| 100 |
-
for index, char in enumerate(function_args[::-1]):
|
| 101 |
-
back_index = -1 - index
|
| 102 |
-
if char == '}':
|
| 103 |
-
parser_log['met_end_}'] = True
|
| 104 |
-
elif parser_log['met_end_}'] and char == '"':
|
| 105 |
-
parser_log['code_end_index'] = back_index - 1
|
| 106 |
-
break
|
| 107 |
-
else:
|
| 108 |
-
continue
|
| 109 |
-
code_str = function_args[parser_log['code_begin_index']: parser_log['code_end_index'] + 1]
|
| 110 |
-
if '\n' in code_str:
|
| 111 |
-
return code_str.strip('\n')
|
| 112 |
-
else:
|
| 113 |
-
return json.loads(function_args)['code']
|
| 114 |
-
|
| 115 |
-
except Exception as e:
|
| 116 |
-
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|