15
15
OTHER DEALINGS IN THE SOFTWARE.
16
16
"""
17
17
import asyncio
18
+ import json
18
19
import logging
19
20
import os
20
21
import queue
@@ -132,35 +133,54 @@ def set_chat(self, chat_id: int, conversation_id=None, parent_id=None):
132
133
chats = {}
133
134
save_json (os .path .join (self .chats_dir , 'chats.json' ), chats )
134
135
135
- def save_conversation (self , chatbot_ , conversation_id ) -> None :
136
+ def save_conversation (self , chatbot_ , conversation_id ) -> bool :
136
137
"""
137
138
Saves conversation
138
139
:param chatbot_:
139
140
:param conversation_id:
140
- :return:
141
+ :return: True if no error
141
142
"""
142
- logging .info ('Saving conversation ' + conversation_id )
143
- # API type 0
144
- if int (self .settings ['modules' ]['chatgpt_api_type' ]) == 0 :
145
- conversations_file = CONVERSATION_DIR_OR_FILE + '.json'
146
- chatbot_ .conversations .save (os .path .join (self .chats_dir , conversations_file ))
147
-
148
- # API type 3
149
- elif int (self .settings ['modules' ]['chatgpt_api_type' ]) == 3 :
150
- if not os .path .exists (os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE )):
151
- os .makedirs (os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE ))
152
- conversation_file = os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE , conversation_id + '.json' )
153
- chatbot_ .save (conversation_file , conversation_id )
154
-
155
- def load_conversation (self , chatbot_ , conversation_id ) -> None :
143
+ logging .info ('Saving conversation ' + str (conversation_id ))
144
+ try :
145
+ if conversation_id is None :
146
+ logging .info ('conversation_id is None. Skipping saving' )
147
+ return False
148
+
149
+ # API type 0
150
+ if int (self .settings ['modules' ]['chatgpt_api_type' ]) == 0 :
151
+ conversations_file = CONVERSATION_DIR_OR_FILE + '.json'
152
+ chatbot_ .conversations .save (os .path .join (self .chats_dir , conversations_file ))
153
+
154
+ # API type 3
155
+ elif int (self .settings ['modules' ]['chatgpt_api_type' ]) == 3 :
156
+ # Create conversation directory
157
+ if not os .path .exists (os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE )):
158
+ os .makedirs (os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE ))
159
+
160
+ # Save as json file
161
+ conversation_file = os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE ,
162
+ conversation_id + '.json' )
163
+ with open (conversation_file , 'w' , encoding = 'utf-8' ) as json_file :
164
+ json .dump (chatbot_ .conversation , json_file , indent = 4 )
165
+ json_file .close ()
166
+ except Exception as e :
167
+ logging .error ('Error saving conversation ' + str (conversation_id ) + ' ' + str (e ), e , exc_info = True )
168
+ return False
169
+ return True
170
+
171
+ def load_conversation (self , chatbot_ , conversation_id ) -> bool :
156
172
"""
157
173
Loads conversation
158
174
:param chatbot_:
159
175
:param conversation_id:
160
- :return:
176
+ :return: True if no error
161
177
"""
162
- logging .info ('Loading conversation ' + conversation_id )
178
+ logging .info ('Loading conversation ' + str ( conversation_id ) )
163
179
try :
180
+ if conversation_id is None :
181
+ logging .info ('conversation_id is None. Skipping loading' )
182
+ return False
183
+
164
184
# API type 0
165
185
if int (self .settings ['modules' ]['chatgpt_api_type' ]) == 0 :
166
186
conversations_file = CONVERSATION_DIR_OR_FILE + '.json'
@@ -170,11 +190,16 @@ def load_conversation(self, chatbot_, conversation_id) -> None:
170
190
elif int (self .settings ['modules' ]['chatgpt_api_type' ]) == 3 :
171
191
conversation_file = os .path .join (self .chats_dir , CONVERSATION_DIR_OR_FILE , conversation_id + '.json' )
172
192
if os .path .exists (conversation_file ):
173
- chatbot_ .load (conversation_file , conversation_id )
193
+ # Load from json file
194
+ with open (conversation_file , 'r' , encoding = 'utf-8' ) as json_file :
195
+ chatbot_ .conversation = json .load (json_file )
196
+ json_file .close ()
174
197
else :
175
198
logging .warning ('File ' + conversation_file + ' not exists!' )
176
199
except Exception as e :
177
- logging .warning ('Error loading conversation ' + conversation_id + ' ' + str (e ))
200
+ logging .warning ('Error loading conversation ' + str (conversation_id ) + ' ' + str (e ))
201
+ return False
202
+ return True
178
203
179
204
def delete_conversation (self , conversation_id ) -> None :
180
205
"""
@@ -289,7 +314,8 @@ def gpt_loop(self):
289
314
290
315
# Try to load conversation
291
316
if conversation_id is not None :
292
- self .load_conversation (chatbot , conversation_id )
317
+ if not self .load_conversation (chatbot , conversation_id ):
318
+ conversation_id = None
293
319
294
320
# Try to load conversation
295
321
if conversation_id is not None :
@@ -312,7 +338,8 @@ def gpt_loop(self):
312
338
if conversation_id is None :
313
339
conversation_id = str (uuid .uuid4 ())
314
340
chatbot .save_conversation (conversation_id )
315
- self .save_conversation (chatbot , conversation_id )
341
+ if not self .save_conversation (chatbot , conversation_id ):
342
+ conversation_id = None
316
343
self .set_chat (container .chat_id , conversation_id , parent_id )
317
344
except Exception as e :
318
345
logging .warning ('Error saving conversation! ' + str (e ))
@@ -375,14 +402,14 @@ async def ask_async(request_, conversation_id_):
375
402
376
403
# API type 3
377
404
elif int (self .settings ['modules' ]['chatgpt_api_type' ]) == 3 :
405
+ # Try to load conversation
406
+ if not self .load_conversation (chatbot , conversation_id ):
407
+ conversation_id = None
408
+
378
409
# Generate conversation ID
379
410
if conversation_id is None :
380
411
conversation_id = str (uuid .uuid4 ())
381
412
382
- # Try to load conversation
383
- else :
384
- self .load_conversation (chatbot , conversation_id )
385
-
386
413
# Ask
387
414
for data in chatbot .ask (str (container .request ), convo_id = conversation_id ):
388
415
# Initialize response
@@ -393,9 +420,14 @@ async def ask_async(request_, conversation_id_):
393
420
api_response += str (data )
394
421
395
422
# Save conversation id
396
- self .save_conversation (chatbot , conversation_id )
423
+ if not self .save_conversation (chatbot , conversation_id ):
424
+ conversation_id = None
397
425
self .set_chat (container .chat_id , conversation_id )
398
426
427
+ # Reset conversation
428
+ if conversation_id is not None :
429
+ chatbot .reset (conversation_id )
430
+
399
431
# Wrong api type
400
432
else :
401
433
raise Exception ('Wrong chatgpt_api_type' )
0 commit comments