diff options
Diffstat (limited to 'gpt4free/theb/__init__.py')
-rw-r--r-- | gpt4free/theb/__init__.py | 29 |
1 files changed, 24 insertions, 5 deletions
diff --git a/gpt4free/theb/__init__.py b/gpt4free/theb/__init__.py index 75a15068..0177194e 100644 --- a/gpt4free/theb/__init__.py +++ b/gpt4free/theb/__init__.py @@ -17,9 +17,10 @@ class Completion: timer = None message_queue = Queue() stream_completed = False + last_msg_id = None @staticmethod - def request(prompt: str, proxy: Optional[str]=None): + def request(prompt: str, proxy: Optional[str] = None): headers = { 'authority': 'chatbot.theb.ai', 'content-type': 'application/json', @@ -28,26 +29,35 @@ class Completion: } proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None - + + options = {} + if Completion.last_msg_id: + options['parentMessageId'] = Completion.last_msg_id + requests.post( 'https://chatbot.theb.ai/api/chat-process', headers=headers, proxies=proxies, content_callback=Completion.handle_stream_response, - json={'prompt': prompt, 'options': {}}, + json={'prompt': prompt, 'options': options}, + timeout=100000 ) Completion.stream_completed = True @staticmethod - def create(prompt: str, proxy: Optional[str]=None) -> Generator[str, None, None]: + def create(prompt: str, proxy: Optional[str] = None) -> Generator[str, None, None]: + Completion.stream_completed = False + Thread(target=Completion.request, args=[prompt, proxy]).start() while not Completion.stream_completed or not Completion.message_queue.empty(): try: message = Completion.message_queue.get(timeout=0.01) for message in findall(Completion.regex, message): - yield loads(Completion.part1 + message + Completion.part2)['delta'] + message_json = loads(Completion.part1 + message + Completion.part2) + Completion.last_msg_id = message_json['id'] + yield message_json['delta'] except Empty: pass @@ -55,3 +65,12 @@ class Completion: @staticmethod def handle_stream_response(response): Completion.message_queue.put(response.decode()) + + @staticmethod + def get_response(prompt: str, proxy: Optional[str] = None) -> str: + response_list = [] + for message in Completion.create(prompt, proxy): + response_list.append(message) + return ''.join(response_list) + + Completion.message_queue.put(response.decode(errors='replace')) |