-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelEvent.py
174 lines (147 loc) · 6.53 KB
/
ModelEvent.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
from abc import ABC, abstractmethod
from Event import *
import pickle
MESSAGE_LENGTH_LIMIT = 1900
class AIEvent(ModelEvent):
def __init__(self, response_object, message_content, context, engine, chatbot):
# response_object = msg or cmd
self.object = response_object
self.message_content = message_content
self.engine = engine
self.context = context
self.chatbot = chatbot
def get_object(self):
return self.object
def get_message_content(self):
return self.message_content
@abstractmethod
def process(self):
pass
class DiscordMessageAIEvent(AIEvent):
def process(self):
try:
message_pair = self.context.add_message_pair(self.message_content, self.object.author.id) # context for user
response = self.engine.get_response(self.message_content, self.context)
message_pair.set_reply(response) # context for response
response_list = MessageSplitter.split_response(response)
for message in response_list:
self.chatbot.enqueue_event(DiscordMessageEvent(self.object, message))
except Exception as e:
self.chatbot.enqueue_event(DiscordMessageEvent(self.object, "An error occurred when generating your response. You might want to reset the conversation or switch to other engines.\nError details:\n" + str(e)))
class DiscordMessageStreamAIEvent(AIEvent):
def process(self):
try:
message_pair = self.context.add_message_pair(self.message_content, self.object.author.id) # context for user
counter = 0
response_generator = self.engine.get_stream_response(self.message_content, self.context)
message_to_stream_list = []
for response in response_generator:
message_pair.set_reply(response)
response_list = MessageSplitter.split_response(response)
while len(message_to_stream_list) < len(response_list):
message_to_stream_list.append(DiscordMessageStreamAIEvent.MessageToStream(self))
for i in range(len(response_list)):
message_to_stream_list[i].stream(response_list[i])
except Exception as e:
self.chatbot.enqueue_event(DiscordMessageEvent(self.object, "An error occurred when generating your response. You might want to reset the conversation or switch to other engines.\nError details:\n" + str(e)))
class MessageToStream:
def __init__(self, outer_instance):
self.last_message = ""
self.outer_instance = outer_instance
self.init = False
self.return_message_list = []
def stream(self, message):
if message != self.last_message:
self.last_message = message
if self.init is False:
self.init = True
self.outer_instance.chatbot.enqueue_event(
DiscordStreamInitMessageEvent(
self.outer_instance.object, message, self.return_message_list
)
)
else:
self.outer_instance.chatbot.enqueue_event(
DiscordStreamMessageEditEvent(
self.outer_instance.object, message, self.return_message_list
)
)
class ThreadTerminationException(Exception):
def __init__(self, message):
super().__init__(message)
class ThreadTerminateEvent(ModelEvent):
def process(self):
raise ThreadTerminationException("Thread terminated")
class DataSaveEvent(ModelEvent):
def __init__(self, object_to_save):
self.object = object_to_save
def process(self):
self.object.save_data()
class MessageSplitter:
@staticmethod
def split_response(response):
parts = response.split("```")
response_list = []
is_code_block = True
response = ""
for part in parts:
is_code_block = not is_code_block
index = 0
if is_code_block:
response += "```"
while index < len(part):
next_index = index + MESSAGE_LENGTH_LIMIT - len(response)
response += part[index: next_index]
index = next_index
if len(response) >= MESSAGE_LENGTH_LIMIT:
if is_code_block:
response += "```"
response_list.append(response)
if is_code_block:
response = "```"
else:
response = ""
if is_code_block:
response += "```"
response_list.append(response)
return response_list
"""
@staticmethod
def find_triple_backticks_indices(text):
indices = []
i = 0
while i < len(text) - 2:
if text[i:i+3] == '```':
indices.append(i)
i += 3
else:
i += 1
return indices
@staticmethod
def split_response(response):
# Start a new message for coding blocks (Discord would display as if they were the same message), handle ``` in the middle of a message
response_list = []
index_list = MessageSplitter.find_triple_backticks_indices(response)
is_code_block = False
index = 0
for end_index in index_list:
start_with_code_block = False
while index < end_index:
temp_response = "```" if start_with_code_block else ""
start_with_code_block = False
next_index = index + MESSAGE_LENGTH_LIMIT
if next_index < end_index:
if is_code_block:
temp_response += response[index:next_index] + "```"
start_with_code_block = True
else:
temp_response += response[index:next_index]
else:
next_index = end_index + 3 if is_code_block else end_index
temp_response += response[index : next_index]
response_list.append(temp_response)
index = next_index
is_code_block = not is_code_block
response_list.append(response[index:])
return response_list
"""