|
|
@ -17,6 +17,7 @@ class Completion:
|
|
|
|
timer = None
|
|
|
|
timer = None
|
|
|
|
message_queue = Queue()
|
|
|
|
message_queue = Queue()
|
|
|
|
stream_completed = False
|
|
|
|
stream_completed = False
|
|
|
|
|
|
|
|
last_msg_id = None
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@staticmethod
|
|
|
|
def request(prompt: str, proxy: Optional[str] = None):
|
|
|
|
def request(prompt: str, proxy: Optional[str] = None):
|
|
|
@ -28,26 +29,35 @@ class Completion:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
proxies = {'http': 'http://' + proxy, 'https': 'http://' + proxy} if proxy else None
|
|
|
|
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(
|
|
|
|
requests.post(
|
|
|
|
'https://chatbot.theb.ai/api/chat-process',
|
|
|
|
'https://chatbot.theb.ai/api/chat-process',
|
|
|
|
headers=headers,
|
|
|
|
headers=headers,
|
|
|
|
proxies=proxies,
|
|
|
|
proxies=proxies,
|
|
|
|
content_callback=Completion.handle_stream_response,
|
|
|
|
content_callback=Completion.handle_stream_response,
|
|
|
|
json={'prompt': prompt, 'options': {}},
|
|
|
|
json={'prompt': prompt, 'options': options},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
Completion.stream_completed = True
|
|
|
|
Completion.stream_completed = True
|
|
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@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()
|
|
|
|
Thread(target=Completion.request, args=[prompt, proxy]).start()
|
|
|
|
|
|
|
|
|
|
|
|
while not Completion.stream_completed or not Completion.message_queue.empty():
|
|
|
|
while not Completion.stream_completed or not Completion.message_queue.empty():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
message = Completion.message_queue.get(timeout=0.01)
|
|
|
|
message = Completion.message_queue.get(timeout=0.01)
|
|
|
|
for message in findall(Completion.regex, message):
|
|
|
|
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:
|
|
|
|
except Empty:
|
|
|
|
pass
|
|
|
|
pass
|
|
|
|