Skip to content

Commit e83de3b

Browse files
authored
Merge pull request #21 from F33RNI/next
Fix API type 3 conversation saving
2 parents 6a7e208 + 9ed8df3 commit e83de3b

File tree

4 files changed

+62
-30
lines changed

4 files changed

+62
-30
lines changed

Diff for: AIHandler.py

+59-27
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
OTHER DEALINGS IN THE SOFTWARE.
1616
"""
1717
import asyncio
18+
import json
1819
import logging
1920
import os
2021
import queue
@@ -132,35 +133,54 @@ def set_chat(self, chat_id: int, conversation_id=None, parent_id=None):
132133
chats = {}
133134
save_json(os.path.join(self.chats_dir, 'chats.json'), chats)
134135

135-
def save_conversation(self, chatbot_, conversation_id) -> None:
136+
def save_conversation(self, chatbot_, conversation_id) -> bool:
136137
"""
137138
Saves conversation
138139
:param chatbot_:
139140
:param conversation_id:
140-
:return:
141+
:return: True if no error
141142
"""
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:
156172
"""
157173
Loads conversation
158174
:param chatbot_:
159175
:param conversation_id:
160-
:return:
176+
:return: True if no error
161177
"""
162-
logging.info('Loading conversation ' + conversation_id)
178+
logging.info('Loading conversation ' + str(conversation_id))
163179
try:
180+
if conversation_id is None:
181+
logging.info('conversation_id is None. Skipping loading')
182+
return False
183+
164184
# API type 0
165185
if int(self.settings['modules']['chatgpt_api_type']) == 0:
166186
conversations_file = CONVERSATION_DIR_OR_FILE + '.json'
@@ -170,11 +190,16 @@ def load_conversation(self, chatbot_, conversation_id) -> None:
170190
elif int(self.settings['modules']['chatgpt_api_type']) == 3:
171191
conversation_file = os.path.join(self.chats_dir, CONVERSATION_DIR_OR_FILE, conversation_id + '.json')
172192
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()
174197
else:
175198
logging.warning('File ' + conversation_file + ' not exists!')
176199
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
178203

179204
def delete_conversation(self, conversation_id) -> None:
180205
"""
@@ -289,7 +314,8 @@ def gpt_loop(self):
289314

290315
# Try to load conversation
291316
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
293319

294320
# Try to load conversation
295321
if conversation_id is not None:
@@ -312,7 +338,8 @@ def gpt_loop(self):
312338
if conversation_id is None:
313339
conversation_id = str(uuid.uuid4())
314340
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
316343
self.set_chat(container.chat_id, conversation_id, parent_id)
317344
except Exception as e:
318345
logging.warning('Error saving conversation! ' + str(e))
@@ -375,14 +402,14 @@ async def ask_async(request_, conversation_id_):
375402

376403
# API type 3
377404
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+
378409
# Generate conversation ID
379410
if conversation_id is None:
380411
conversation_id = str(uuid.uuid4())
381412

382-
# Try to load conversation
383-
else:
384-
self.load_conversation(chatbot, conversation_id)
385-
386413
# Ask
387414
for data in chatbot.ask(str(container.request), convo_id=conversation_id):
388415
# Initialize response
@@ -393,9 +420,14 @@ async def ask_async(request_, conversation_id_):
393420
api_response += str(data)
394421

395422
# Save conversation id
396-
self.save_conversation(chatbot, conversation_id)
423+
if not self.save_conversation(chatbot, conversation_id):
424+
conversation_id = None
397425
self.set_chat(container.chat_id, conversation_id)
398426

427+
# Reset conversation
428+
if conversation_id is not None:
429+
chatbot.reset(conversation_id)
430+
399431
# Wrong api type
400432
else:
401433
raise Exception('Wrong chatgpt_api_type')

Diff for: main.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
import BotHandler
3030
from JSONReaderWriter import load_json
3131

32-
TELEGRAMUS_VERSION = 'beta_2.0.1'
32+
TELEGRAMUS_VERSION = 'beta_2.0.2'
3333

3434
# Logging level (INFO for debug, WARN for release)
3535
LOGGING_LEVEL = logging.INFO

Diff for: requirements.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
psutil>=5.9.1
22
telegram~=0.0.1
33
python-telegram-bot~=20.1
4-
revChatGPT==3.3.4
4+
revChatGPT==3.3.5
55
openai>=0.26.4
66
tiktoken>=0.2.0
77
OpenAIAuth>=0.3.2

Diff for: settings.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"__comment01__": "SPECIFY WHAT MODULES WILL BE INCLUDED IN TELEGRAM BOT AND revChatGPT API TYPE",
33
"__comment02__": "0 - OFFICIAL CHATGPT API. MORE STUPID MODEL. AUTHORIZATION VIA OPENAI API KEY",
44
"__comment03__": "1 - SAME AS OFFICIAL WEBSITE. FREE BUT LIMITED NUMBER OF REQUESTS. AUTHORIZATION VIA ACCESS_TOKEN, SESSION_TOKEN (NOT TESTED) OR EMAIL/PASS (NOT TESTED)",
5-
"__comment04__": "2 - (MAY NOT WORK) FREE API FOR CHATGPT. AUTHORIZATION VIA OPENAI API KEY",
5+
"__comment04__": "2 - FREE API FOR CHATGPT. AUTHORIZATION VIA OPENAI API KEY",
66
"__comment05__": "3 - (RECOMMENDED) OFFICIAL CHATGPT API. AUTHORIZATION VIA OPENAI API KEY",
77
"modules": {
88
"chatgpt": true,

0 commit comments

Comments
 (0)