dongsiqie commited on
Commit
bbdc675
·
1 Parent(s): 562f746

Delete src/response_parser.py

Browse files
Files changed (1) hide show
  1. src/response_parser.py +0 -200
src/response_parser.py DELETED
@@ -1,200 +0,0 @@
1
- from abc import ABCMeta, abstractmethod
2
- from functional import *
3
-
4
-
5
- class ChoiceStrategy(metaclass=ABCMeta):
6
- def __init__(self, choice):
7
- self.choice = choice
8
- self.delta = choice['delta']
9
-
10
- @abstractmethod
11
- def support(self):
12
- pass
13
-
14
- @abstractmethod
15
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
16
- pass
17
-
18
-
19
- class RoleChoiceStrategy(ChoiceStrategy):
20
-
21
- def support(self):
22
- return 'role' in self.delta
23
-
24
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
25
- bot_backend.set_assistant_role_name(assistant_role_name=self.delta['role'])
26
- return history, whether_exit
27
-
28
-
29
- class ContentChoiceStrategy(ChoiceStrategy):
30
- def support(self):
31
- return 'content' in self.delta and self.delta['content'] is not None
32
-
33
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
34
- # null value of content often occur in function call:
35
- # {
36
- # "role": "assistant",
37
- # "content": null,
38
- # "function_call": {
39
- # "name": "python",
40
- # "arguments": ""
41
- # }
42
- # }
43
- bot_backend.add_content(content=self.delta.get('content', ''))
44
- history[-1][1] = bot_backend.content
45
- return history, whether_exit
46
-
47
-
48
- class NameFunctionCallChoiceStrategy(ChoiceStrategy):
49
- def support(self):
50
- return 'function_call' in self.delta and 'name' in self.delta['function_call']
51
-
52
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
53
- function_dict = bot_backend.jupyter_kernel.available_functions
54
- bot_backend.set_function_name(function_name=self.delta['function_call']['name'])
55
- bot_backend.copy_current_bot_history(bot_history=history)
56
- if bot_backend.function_name not in function_dict:
57
- history.append(
58
- [
59
- None,
60
- f'GPT attempted to call a function that does '
61
- f'not exist: {bot_backend.function_name}\n '
62
- ]
63
- )
64
- whether_exit = True
65
-
66
- return history, whether_exit
67
-
68
-
69
- class ArgumentsFunctionCallChoiceStrategy(ChoiceStrategy):
70
-
71
- def support(self):
72
- return 'function_call' in self.delta and 'arguments' in self.delta['function_call']
73
-
74
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
75
- bot_backend.add_function_args_str(function_args_str=self.delta['function_call']['arguments'])
76
-
77
- if bot_backend.function_name == 'python': # handle hallucinatory function calls
78
- '''
79
- In practice, we have noticed that GPT, especially GPT-3.5, may occasionally produce hallucinatory
80
- function calls. These calls involve a non-existent function named `python` with arguments consisting
81
- solely of raw code text (not a JSON format).
82
- '''
83
- temp_code_str = bot_backend.function_args_str
84
- bot_backend.update_display_code_block(
85
- display_code_block="\n🔴Working:\n```python\n{}\n```".format(temp_code_str)
86
- )
87
- history = copy.deepcopy(bot_backend.bot_history)
88
- history[-1][1] += bot_backend.display_code_block
89
- else:
90
- temp_code_str = parse_json(function_args=bot_backend.function_args_str, finished=False)
91
- if temp_code_str is not None:
92
- bot_backend.update_display_code_block(
93
- display_code_block="\n🔴Working:\n```python\n{}\n```".format(
94
- temp_code_str
95
- )
96
- )
97
- history = copy.deepcopy(bot_backend.bot_history)
98
- history[-1][1] += bot_backend.display_code_block
99
-
100
- return history, whether_exit
101
-
102
-
103
- class FinishReasonChoiceStrategy(ChoiceStrategy):
104
- def support(self):
105
- return self.choice['finish_reason'] is not None
106
-
107
- def execute(self, bot_backend: BotBackend, history: List, whether_exit: bool):
108
- function_dict = bot_backend.jupyter_kernel.available_functions
109
-
110
- if bot_backend.content:
111
- bot_backend.add_gpt_response_content_message()
112
-
113
- bot_backend.update_finish_reason(finish_reason=self.choice['finish_reason'])
114
- if bot_backend.finish_reason == 'function_call':
115
- try:
116
-
117
- code_str = self.get_code_str(bot_backend)
118
-
119
- bot_backend.update_display_code_block(
120
- display_code_block="\n🟢Working:\n```python\n{}\n```".format(code_str)
121
- )
122
- history = copy.deepcopy(bot_backend.bot_history)
123
- history[-1][1] += bot_backend.display_code_block
124
-
125
- # function response
126
- text_to_gpt, content_to_display = function_dict[
127
- bot_backend.function_name
128
- ](code_str)
129
-
130
- # add function call to conversion
131
- bot_backend.add_function_call_response_message(function_response=text_to_gpt, save_tokens=True)
132
-
133
- add_function_response_to_bot_history(
134
- content_to_display=content_to_display, history=history, unique_id=bot_backend.unique_id
135
- )
136
-
137
- except json.JSONDecodeError:
138
- history.append(
139
- [None, f"GPT generate wrong function args: {bot_backend.function_args_str}"]
140
- )
141
- whether_exit = True
142
- return history, whether_exit
143
-
144
- except Exception as e:
145
- history.append([None, f'Backend error: {e}'])
146
- whether_exit = True
147
- return history, whether_exit
148
-
149
- bot_backend.reset_gpt_response_log_values(exclude=['finish_reason'])
150
-
151
- return history, whether_exit
152
-
153
- @staticmethod
154
- def get_code_str(bot_backend):
155
- if bot_backend.function_name == 'python':
156
- code_str = bot_backend.function_args_str
157
- else:
158
- code_str = parse_json(function_args=bot_backend.function_args_str, finished=True)
159
- if code_str is None:
160
- raise json.JSONDecodeError
161
- return code_str
162
-
163
-
164
- class ChoiceHandler:
165
- strategies = [
166
- RoleChoiceStrategy, ContentChoiceStrategy, NameFunctionCallChoiceStrategy,
167
- ArgumentsFunctionCallChoiceStrategy, FinishReasonChoiceStrategy
168
- ]
169
-
170
- def __init__(self, choice):
171
- self.choice = choice
172
-
173
- def handle(self, bot_backend: BotBackend, history: List, whether_exit: bool):
174
- for Strategy in self.strategies:
175
- strategy_instance = Strategy(choice=self.choice)
176
- if not strategy_instance.support():
177
- continue
178
- history, whether_exit = strategy_instance.execute(
179
- bot_backend=bot_backend,
180
- history=history,
181
- whether_exit=whether_exit
182
- )
183
- return history, whether_exit
184
-
185
-
186
- def parse_response(chunk, history, bot_backend: BotBackend):
187
- """
188
- :return: history, whether_exit
189
- """
190
- whether_exit = False
191
- if chunk['choices']:
192
- choice = chunk['choices'][0]
193
- choice_handler = ChoiceHandler(choice=choice)
194
- history, whether_exit = choice_handler.handle(
195
- history=history,
196
- bot_backend=bot_backend,
197
- whether_exit=whether_exit
198
- )
199
-
200
- return history, whether_exit