Skip to content

Commit

Permalink
Merge pull request #10 from F33RNI/next
Browse files Browse the repository at this point in the history
Beta 1.6.0
  • Loading branch information
F33RNI authored Feb 20, 2023
2 parents 6b8579b + 028885f commit 34f6612
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
18 changes: 17 additions & 1 deletion AIHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import openai

import Authenticator
import RequestResponseContainer

EMPTY_RESPONSE_ERROR_MESSAGE = 'Empty response or unhandled error!'
Expand Down Expand Up @@ -96,12 +97,23 @@ def gpt_loop(self):
api_response = None
raise Exception(ERROR_CHATGPT_DISABLED)

# Too many requests in 1 hour
if self.authenticator.chatbot_too_many_requests:
raise Exception(Authenticator.TOO_MANY_REQUESTS_MESSAGE)

# Wait for chatbot
chatbot = self.authenticator.chatbot
while not self.authenticator.chatbot_working or chatbot is None:
time.sleep(1)
chatbot = self.authenticator.chatbot

# Too many requests in 1 hour
if self.authenticator.chatbot_too_many_requests:
raise Exception(Authenticator.TOO_MANY_REQUESTS_MESSAGE)

# Lock chatbot
self.authenticator.chatbot_locked = True

# Log request
logging.info('Asking: ' + str(container.request))

Expand Down Expand Up @@ -170,7 +182,8 @@ def gpt_loop(self):
# Error
except Exception as e:
# Wake up authenticator check loop from sleep
self.authenticator.chatbot_working = False
if not self.authenticator.chatbot_too_many_requests:
self.authenticator.chatbot_working = False

# Print error message
error_message = str(e)
Expand Down Expand Up @@ -200,6 +213,9 @@ def gpt_loop(self):
# Clear processing container
self.processing_container = None

# Release lock
self.authenticator.chatbot_locked = False

# Loop finished
logging.warning('AIHandler loop finished')
self.loop_running = False
24 changes: 23 additions & 1 deletion Authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

PROXY_FROM_URL = 'http://free-proxy-list.net/'

TOO_MANY_REQUESTS_MESSAGE = 'Too many requests in 1 hour'


def kill_all_processes(processes_and_times):
"""
Expand Down Expand Up @@ -70,6 +72,8 @@ def __init__(self, settings):
self.settings = settings

self.chatbot = None
self.chatbot_locked = False
self.chatbot_too_many_requests = False
self.chatbot_working = False
self.chatbots_and_proxies_queue = multiprocessing.Queue(maxsize=int(self.settings['proxy']
['max_number_of_processes']) * 2)
Expand Down Expand Up @@ -159,6 +163,10 @@ def proxy_checker_loop(self):
check_successful = False
if self.chatbot is not None:
try:
# Wait for response for previous question
while self.chatbot_locked:
time.sleep(1)

# Ask test message
logging.info('Asking test question: ' + str(self.settings['proxy']['check_message']).strip())
chatbot_response = ''
Expand All @@ -175,11 +183,25 @@ def proxy_checker_loop(self):
# Check response
if str(self.settings['proxy']['check_reply_must_include']).strip() in chatbot_response:
check_successful = True
self.chatbot_too_many_requests = False
else:
raise Exception('No ' + self.settings['proxy']['check_reply_must_include'] + ' in response!')

except Exception as e:
logging.warning('Error checking chatbot! ' + str(e))
# Too many requests in 1 hour
if TOO_MANY_REQUESTS_MESSAGE in str(e):
logging.warning(str(e))

# Wait before next try
wait_seconds = int(self.settings['chatgpt_dialog']['too_many_requests_wait_time_seconds'])
logging.warning('Waiting ' + str(wait_seconds) + ' seconds...')
self.chatbot_too_many_requests = True
time.sleep(wait_seconds)

# Other error
else:
self.chatbot_too_many_requests = False
logging.warning('Error checking chatbot! ' + str(e))

# Sleep for next cycle in check is successful
if check_successful:
Expand Down
2 changes: 1 addition & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import Authenticator
import BotHandler

TELEGRAMUS_VERSION = 'beta_1.5.0'
TELEGRAMUS_VERSION = 'beta_1.6.0'

# Logging level (INFO for debug, WARN for release)
LOGGING_LEVEL = logging.INFO
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
psutil>=5.9.1
telegram~=0.0.1
python-telegram-bot~=20.0
revChatGPT>=2.3.3
revChatGPT>=2.3.4
openai>=0.26.4
tiktoken>=0.2.0
OpenAIAuth>=0.3.2
3 changes: 2 additions & 1 deletion settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
"__comment15__": "WHEN YOU START FOR THE FIRST TIME, LEAVE THEM BLANK, ASK A QUESTION AND LOOK IN THE CONSOLE",
"chatgpt_dialog": {
"conversation_id": "",
"parent_id": ""
"parent_id": "",
"too_many_requests_wait_time_seconds": 600
},

"__comment16__": "PROVIDE YOUR BOT API KEY FROM https://t.me/BotFather",
Expand Down

0 comments on commit 34f6612

Please sign in to comment.