dongsiqie commited on
Commit
d4fa575
·
1 Parent(s): bb66a44

Delete src/bot_backend.py

Browse files
Files changed (1) hide show
  1. src/bot_backend.py +0 -231
src/bot_backend.py DELETED
@@ -1,231 +0,0 @@
1
- import json
2
- import openai
3
- import os
4
- import copy
5
- import shutil
6
- from jupyter_backend import *
7
- from typing import *
8
-
9
- functions = [
10
- {
11
- "name": "execute_code",
12
- "description": "This function allows you to execute Python code and retrieve the terminal output. If the code "
13
- "generates image output, the function will return the text '[image]'. The code is sent to a "
14
- "Jupyter kernel for execution. The kernel will remain active after execution, retaining all "
15
- "variables in memory.",
16
- "parameters": {
17
- "type": "object",
18
- "properties": {
19
- "code": {
20
- "type": "string",
21
- "description": "The code text"
22
- }
23
- },
24
- "required": ["code"],
25
- }
26
- }
27
- ]
28
-
29
- system_msg = '''You are an AI code interpreter.
30
- Your goal is to help users do a variety of jobs by executing Python code.
31
-
32
- You should:
33
- 1. Comprehend the user's requirements carefully & to the letter.
34
- 2. Give a brief description for what you plan to do & call the execute_code function to run code
35
- 3. Provide results analysis based on the execution output.
36
- 4. If error occurred, try to fix it.
37
-
38
- Note: If the user uploads a file, you will receive a system message "User uploaded a file: filename". Use the filename as the path in the code. '''
39
-
40
-
41
- def get_config():
42
- with open('config.json') as f:
43
- config = json.load(f)
44
- return config
45
-
46
-
47
- def config_openai_api(api_type, api_base, api_version, api_key):
48
- openai.api_type = api_type
49
- openai.api_base = api_base
50
- openai.api_version = api_version
51
- openai.api_key = api_key
52
-
53
-
54
- class GPTResponseLog:
55
- def __init__(self):
56
- self.assistant_role_name = ''
57
- self.content = ''
58
- self.function_name = None
59
- self.function_args_str = ''
60
- self.display_code_block = ''
61
- self.finish_reason = 'stop'
62
- self.bot_history = None
63
-
64
- def reset_gpt_response_log_values(self, exclude=None):
65
- if exclude is None:
66
- exclude = []
67
-
68
- attributes = {'assistant_role_name': '',
69
- 'content': '',
70
- 'function_name': None,
71
- 'function_args_str': '',
72
- 'display_code_block': '',
73
- 'finish_reason': 'stop',
74
- 'bot_history': None}
75
-
76
- for attr_name in exclude:
77
- del attributes[attr_name]
78
- for attr_name, value in attributes.items():
79
- setattr(self, attr_name, value)
80
-
81
- def set_assistant_role_name(self, assistant_role_name: str):
82
- self.assistant_role_name = assistant_role_name
83
-
84
- def add_content(self, content: str):
85
- self.content += content
86
-
87
- def set_function_name(self, function_name: str):
88
- self.function_name = function_name
89
-
90
- def copy_current_bot_history(self, bot_history: List):
91
- self.bot_history = copy.deepcopy(bot_history)
92
-
93
- def add_function_args_str(self, function_args_str: str):
94
- self.function_args_str += function_args_str
95
-
96
- def update_display_code_block(self, display_code_block):
97
- self.display_code_block = display_code_block
98
-
99
- def update_finish_reason(self, finish_reason: str):
100
- self.finish_reason = finish_reason
101
-
102
-
103
- class BotBackend(GPTResponseLog):
104
- def __init__(self):
105
- super().__init__()
106
- self.unique_id = hash(id(self))
107
- self.jupyter_work_dir = f'cache/work_dir_{self.unique_id}'
108
- self.jupyter_kernel = JupyterKernel(work_dir=self.jupyter_work_dir)
109
- self.gpt_model_choice = "GPT-3.5"
110
- self.revocable_files = []
111
- self._init_conversation()
112
- self._init_api_config()
113
- self._init_kwargs_for_chat_completion()
114
-
115
- def _init_conversation(self):
116
- first_system_msg = {'role': 'system', 'content': system_msg}
117
- if hasattr(self, 'conversation'):
118
- self.conversation.clear()
119
- self.conversation.append(first_system_msg)
120
- else:
121
- self.conversation: List[Dict] = [first_system_msg]
122
-
123
- def _init_api_config(self):
124
- self.config = get_config()
125
- api_type = self.config['API_TYPE']
126
- api_base = self.config['API_base']
127
- api_version = self.config['API_VERSION']
128
- if self.config['API_KEY']:
129
- api_key = self.config['API_KEY']
130
- else:
131
- api_key = os.getenv('OPENAI_API_KEY')
132
-
133
- config_openai_api(api_type, api_base, api_version, api_key)
134
-
135
- def _init_kwargs_for_chat_completion(self):
136
- self.kwargs_for_chat_completion = {
137
- 'stream': True,
138
- 'messages': self.conversation,
139
- 'functions': functions,
140
- 'function_call': 'auto'
141
- }
142
-
143
- model_name = self.config['model'][self.gpt_model_choice]['model_name']
144
-
145
- if self.config['API_TYPE'] == 'azure':
146
- self.kwargs_for_chat_completion['engine'] = model_name
147
- else:
148
- self.kwargs_for_chat_completion['model'] = model_name
149
-
150
- def _clear_all_files_in_work_dir(self):
151
- for filename in os.listdir(self.jupyter_work_dir):
152
- os.remove(
153
- os.path.join(self.jupyter_work_dir, filename)
154
- )
155
-
156
- def add_gpt_response_content_message(self):
157
- self.conversation.append(
158
- {'role': self.assistant_role_name, 'content': self.content}
159
- )
160
-
161
- def add_text_message(self, user_text):
162
- self.conversation.append(
163
- {'role': 'user', 'content': user_text}
164
- )
165
- self.revocable_files.clear()
166
- self.update_finish_reason(finish_reason='new_input')
167
-
168
- def add_file_message(self, path, bot_msg):
169
- filename = os.path.basename(path)
170
- work_dir = self.jupyter_work_dir
171
-
172
- shutil.copy(path, work_dir)
173
-
174
- gpt_msg = {'role': 'system', 'content': f'User uploaded a file: {filename}'}
175
- self.conversation.append(gpt_msg)
176
- self.revocable_files.append(
177
- {
178
- 'bot_msg': bot_msg,
179
- 'gpt_msg': gpt_msg,
180
- 'path': os.path.join(work_dir, filename)
181
- }
182
- )
183
-
184
- def add_function_call_response_message(self, function_response: str, save_tokens=True):
185
- self.conversation.append(
186
- {
187
- "role": self.assistant_role_name,
188
- "name": self.function_name,
189
- "content": self.function_args_str
190
- }
191
- )
192
-
193
- if save_tokens and len(function_response) > 500:
194
- function_response = f'{function_response[:200]}\n[Output too much, the middle part output is omitted]\n ' \
195
- f'End part of output:\n{function_response[-200:]}'
196
- self.conversation.append(
197
- {
198
- "role": "function",
199
- "name": self.function_name,
200
- "content": function_response,
201
- }
202
- )
203
-
204
- def revoke_file(self):
205
- if self.revocable_files:
206
- file = self.revocable_files[-1]
207
- bot_msg = file['bot_msg']
208
- gpt_msg = file['gpt_msg']
209
- path = file['path']
210
-
211
- assert self.conversation[-1] is gpt_msg
212
- del self.conversation[-1]
213
-
214
- os.remove(path)
215
-
216
- del self.revocable_files[-1]
217
-
218
- return bot_msg
219
- else:
220
- return None
221
-
222
- def update_gpt_model_choice(self, model_choice):
223
- self.gpt_model_choice = model_choice
224
- self._init_kwargs_for_chat_completion()
225
-
226
- def restart(self):
227
- self._clear_all_files_in_work_dir()
228
- self.revocable_files.clear()
229
- self._init_conversation()
230
- self.reset_gpt_response_log_values()
231
- self.jupyter_kernel.restart_jupyter_kernel()