Skip to content

Commit

Permalink
Add back official API
Browse files Browse the repository at this point in the history
  • Loading branch information
F33RNI committed Feb 27, 2023
1 parent 028885f commit 383c941
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 120 deletions.
107 changes: 69 additions & 38 deletions AIHandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,50 +97,81 @@ 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)
# API type 0
if self.authenticator.api_type == 0:
# Initialize conversation id
if len(str(self.settings['chatgpt_api_0']['existing_conversation_id'])) > 0:
self.conversation_id = str(self.settings['chatgpt_api_0']['existing_conversation_id'])
logging.info('Conversation id: ' + str(self.conversation_id))
else:
self.conversation_id = None

# Get chatbot from Authenticator class
chatbot = self.authenticator.chatbot

# Ask
for data in chatbot.ask_stream(str(container.request), conversation_id=self.conversation_id):
# Initialize response
if api_response is None:
api_response = ''

# Append response
api_response += str(data)

# Remove tags
api_response = api_response.replace('<|im_end|>', '').replace('<|im_start|>', '')

# API type 1
elif self.authenticator.api_type == 1:
# 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))

# Initialize conversation_id and parent_id
if self.conversation_id is None:
self.conversation_id = str(self.settings['chatgpt_dialog']['conversation_id']) if \
len(str(self.settings['chatgpt_dialog']['conversation_id'])) > 0 else None
logging.info('Initial conversation id: ' + str(self.conversation_id))
if self.parent_id is None:
self.parent_id = str(self.settings['chatgpt_dialog']['parent_id']) if \
len(str(self.settings['chatgpt_dialog']['parent_id'])) > 0 else None
logging.info('Initial parent id: ' + str(self.parent_id))

# Ask
for data in chatbot.ask(str(container.request),
conversation_id=self.conversation_id,
parent_id=self.parent_id):
# Get last response
api_response = data['message']

# Store conversation_id
if data['conversation_id'] is not None:
self.conversation_id = data['conversation_id']

# Log conversation id and parent id
logging.info('Current conversation id: ' + str(self.conversation_id)
+ '\tParent id: ' + str(self.parent_id))
# 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))

# Initialize conversation_id and parent_id
if self.conversation_id is None:
if len(str(self.settings['chatgpt_api_1']['chatgpt_dialog']['conversation_id'])) > 0:
self.conversation_id \
= str(self.settings['chatgpt_api_1']['chatgpt_dialog']['conversation_id'])
logging.info('Initial conversation id: ' + str(self.conversation_id))
if self.parent_id is None:
if len(str(self.settings['chatgpt_api_1']['chatgpt_dialog']['parent_id'])) > 0:
self.parent_id = str(self.settings['chatgpt_api_1']['chatgpt_dialog']['parent_id'])
logging.info('Initial parent id: ' + str(self.parent_id))

# Ask
for data in chatbot.ask(str(container.request),
conversation_id=self.conversation_id,
parent_id=self.parent_id):
# Get last response
api_response = data['message']

# Store conversation_id
if data['conversation_id'] is not None:
self.conversation_id = data['conversation_id']

# Log conversation id and parent id
logging.info('Current conversation id: ' + str(self.conversation_id)
+ '\tParent id: ' + str(self.parent_id))

# Wrong api type
else:
raise Exception('Wrong chatgpt_api_type')

# Log response
logging.info(str(api_response))
Expand Down
120 changes: 79 additions & 41 deletions Authenticator.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
import time
from urllib import request

from revChatGPT.V1 import Chatbot

import useragents

PROXY_FROM_URL = 'http://free-proxy-list.net/'
Expand Down Expand Up @@ -58,6 +56,7 @@ def initialize_chatbot(proxy, config, chatbots_and_proxies_queue):
config_['proxy'] = proxy

# Initialize chatbot
from revChatGPT.V1 import Chatbot
chatbot = Chatbot(config=config_)

# Append working chatbot and proxy
Expand All @@ -71,11 +70,12 @@ class Authenticator:
def __init__(self, settings):
self.settings = settings

self.api_type = 0
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']
self.chatbots_and_proxies_queue = multiprocessing.Queue(maxsize=int(self.settings['chatgpt_api_1']['proxy']
['max_number_of_processes']) * 2)
self.current_proxy = None
self.conversation_id = None
Expand All @@ -87,27 +87,57 @@ def start_check_loop(self):
Starts background thread
:return:
"""
# No proxy
if int(self.settings['proxy']['check_interval_seconds']) <= 0 or not self.settings['proxy']['enabled']:
logging.info('Proxy checks disabled. Initializing chatbot...')
if self.chatbot is None:
try:
self.chatbot = Chatbot(config=self.get_chatbot_config())
self.chatbot_working = True
# Official API
if int(self.settings['modules']['chatgpt_api_type']) == 0:
self.api_type = 0
# Proxy
proxy_ = str(self.settings['chatgpt_api_0']['proxy'])
if len(proxy_) > 0:
self.current_proxy = proxy_
try:
from revChatGPT.V0 import Chatbot
# Initialize chatbot
self.chatbot = Chatbot(str(self.settings['chatgpt_api_0']['open_ai_api_key']),
engine=str(self.settings['chatgpt_api_0']['engine']),
proxy=proxy_)
self.chatbot_working = True

# Error initializing chatbot
except Exception as e:
logging.warning('Error initializing chatbot!' + str(e))
self.chatbot_working = False
return
# Error initializing chatbot
except Exception as e:
logging.warning('Error initializing chatbot!' + str(e))
self.chatbot_working = False

# revChatGPT API version 1
elif int(self.settings['modules']['chatgpt_api_type']) == 1:
self.api_type = 1
# No proxy
if int(self.settings['chatgpt_api_1']['proxy']['check_interval_seconds']) <= 0 \
or not self.settings['chatgpt_api_1']['proxy']['enabled']:
logging.info('Proxy checks disabled. Initializing chatbot...')
if self.chatbot is None:
try:
from revChatGPT.V1 import Chatbot
self.chatbot = Chatbot(config=self.get_chatbot_config())
self.chatbot_working = True

# Error initializing chatbot
except Exception as e:
logging.warning('Error initializing chatbot!' + str(e))
self.chatbot_working = False
return

# Set flag
self.check_loop_running = True
# Set flag
self.check_loop_running = True

# Start thread
thread = threading.Thread(target=self.proxy_checker_loop)
thread.start()
logging.info('Checking thread: ' + thread.name)
# Start thread
thread = threading.Thread(target=self.proxy_checker_loop)
thread.start()
logging.info('Checking thread: ' + thread.name)

# Other than 0 or 1
else:
logging.error('Wrong chatgpt_api_type!')
raise Exception('Wrong chatgpt_api_type')

def proxy_get(self):
"""
Expand Down Expand Up @@ -137,7 +167,7 @@ def proxy_get(self):

# Check data and append to list
if len(ip.split('.')) == 4 and len(port) > 1 and \
(is_https or not self.settings['proxy']['https_only']):
(is_https or not self.settings['chatgpt_api_1']['proxy']['https_only']):
# proxies_list.append(('https://' if is_https else 'http://') + ip + ':' + port)
self.proxy_list.append('http://' + ip + ':' + port)
except:
Expand Down Expand Up @@ -168,11 +198,13 @@ def proxy_checker_loop(self):
time.sleep(1)

# Ask test message
logging.info('Asking test question: ' + str(self.settings['proxy']['check_message']).strip())
logging.info('Asking test question: ' + str(self.settings['chatgpt_api_1']
['proxy']['check_message']).strip())
chatbot_response = ''
for data in self.chatbot.ask(str(self.settings['proxy']['check_message']).strip(),
for data in self.chatbot.ask(str(self.settings['chatgpt_api_1']['proxy']['check_message']).strip(),
conversation_id=self.conversation_id,
timeout=int(self.settings['proxy']['check_message_timeout'])):
timeout=int(self.settings['chatgpt_api_1']
['proxy']['check_message_timeout'])):
# Get response
chatbot_response = data['message']

Expand All @@ -181,11 +213,13 @@ def proxy_checker_loop(self):
self.conversation_id = data['conversation_id']

# Check response
if str(self.settings['proxy']['check_reply_must_include']).strip() in chatbot_response:
if str(self.settings['chatgpt_api_1']['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!')
raise Exception('No ' + self.settings['chatgpt_api_1']['proxy']['check_reply_must_include']
+ ' in response!')

except Exception as e:
# Too many requests in 1 hour
Expand Down Expand Up @@ -213,7 +247,8 @@ def proxy_checker_loop(self):

# Sleep and check for self.chatbot_working
sleep_started_time = time.time()
while time.time() - sleep_started_time < int(self.settings['proxy']['check_interval_seconds']):
while time.time() - sleep_started_time \
< int(self.settings['chatgpt_api_1']['proxy']['check_interval_seconds']):
if not self.chatbot_working:
logging.info('Sleep interrupted!')
break
Expand All @@ -222,15 +257,15 @@ def proxy_checker_loop(self):
# Check is not successful
else:
# Get proxy
if self.settings['proxy']['enabled']:
if self.settings['chatgpt_api_1']['proxy']['enabled']:
# Auto proxy
if self.settings['proxy']['auto']:
if self.settings['chatgpt_api_1']['proxy']['auto']:
# Get new proxy list
if len(self.proxy_list) <= 0:
self.proxy_get()
# Manual proxy
else:
self.proxy_list = [self.settings['proxy']['manual_proxy']]
self.proxy_list = [self.settings['chatgpt_api_1']['proxy']['manual_proxy']]
# Proxy disabled
else:
break
Expand All @@ -249,7 +284,8 @@ def proxy_checker_loop(self):
while True:
# Create and start processes
while len(self.proxy_list) > 0 \
and len(processes_and_times) < int(self.settings['proxy']['max_number_of_processes']):
and len(processes_and_times) \
< int(self.settings['chatgpt_api_1']['proxy']['max_number_of_processes']):
proxy = self.proxy_list.pop(0)
process = multiprocessing.Process(target=initialize_chatbot,
args=(proxy,
Expand All @@ -270,7 +306,7 @@ def proxy_checker_loop(self):
# No more proxies
if len(self.proxy_list) == 0:
# Get new proxy list in auto mode
if self.settings['proxy']['auto']:
if self.settings['chatgpt_api_1']['proxy']['auto']:
self.proxy_get()

# Exit if manual mode and no more processes
Expand Down Expand Up @@ -302,7 +338,8 @@ def proxy_checker_loop(self):
process_, time_ = processes_and_times[i]
if process_ is not None and time_ is not None:
# Kill on timeout or exit
if time.time() - time_ > int(self.settings['proxy']['initialization_timeout']) \
if time.time() - time_ \
> int(self.settings['chatgpt_api_1']['proxy']['initialization_timeout']) \
or not process_.is_alive():
if process_.is_alive():
logging.info('Killing process with PID: ' + str(process_.pid) + ' due to timeout')
Expand Down Expand Up @@ -332,17 +369,18 @@ def get_chatbot_config(self):
config = {}

# Use email/password
if len(self.settings['chatgpt_auth']['email']) > 0 and len(self.settings['chatgpt_auth']['password']) > 0:
config['email'] = self.settings['chatgpt_auth']['email']
config['password'] = self.settings['chatgpt_auth']['password']
if len(self.settings['chatgpt_api_1']['chatgpt_auth']['email']) > 0 \
and len(self.settings['chatgpt_api_1']['chatgpt_auth']['password']) > 0:
config['email'] = self.settings['chatgpt_api_1']['chatgpt_auth']['email']
config['password'] = self.settings['chatgpt_api_1']['chatgpt_auth']['password']

# Use session_token
elif len(self.settings['chatgpt_auth']['session_token']) > 0:
config['session_token'] = self.settings['chatgpt_auth']['session_token']
elif len(self.settings['chatgpt_api_1']['chatgpt_auth']['session_token']) > 0:
config['session_token'] = self.settings['chatgpt_api_1']['chatgpt_auth']['session_token']

# Use access_token
elif len(self.settings['chatgpt_auth']['access_token']) > 0:
config['access_token'] = self.settings['chatgpt_auth']['access_token']
elif len(self.settings['chatgpt_api_1']['chatgpt_auth']['access_token']) > 0:
config['access_token'] = self.settings['chatgpt_api_1']['chatgpt_auth']['access_token']

# No credentials
else:
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.6.0'
TELEGRAMUS_VERSION = 'beta_1.7.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.4
revChatGPT>=2.3.14
openai>=0.26.4
tiktoken>=0.2.0
OpenAIAuth>=0.3.2
Loading

0 comments on commit 383c941

Please sign in to comment.