summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authort.me/xtekky <98614666+xtekky@users.noreply.github.com>2023-04-20 15:51:55 +0200
committert.me/xtekky <98614666+xtekky@users.noreply.github.com>2023-04-20 15:51:55 +0200
commitfa113e6fa7b2db677ebef8537daf54a35eb4948a (patch)
tree6fd9ef8f5859d4c15a357bf1089a623394b2eb8e
parentphind major improvement ( stream ) (diff)
downloadgpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar.gz
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar.bz2
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar.lz
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar.xz
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.tar.zst
gpt4free-fa113e6fa7b2db677ebef8537daf54a35eb4948a.zip
-rw-r--r--phind/__init__.py47
1 files changed, 28 insertions, 19 deletions
diff --git a/phind/__init__.py b/phind/__init__.py
index 469c09b7..69468703 100644
--- a/phind/__init__.py
+++ b/phind/__init__.py
@@ -204,25 +204,34 @@ class StreamingCompletion:
while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty():
try:
- message = StreamingCompletion.message_queue.get(timeout=0)
- for token in findall(r'(?<=data: )(.+?)(?=\r\n\r\n)', message.decode()):
- yield PhindResponse({
- 'id' : f'cmpl-1337-{int(time())}',
- 'object' : 'text_completion',
- 'created': int(time()),
- 'model' : model,
- 'choices': [{
- 'text' : token,
- 'index' : 0,
- 'logprobs' : None,
- 'finish_reason' : 'stop'
- }],
- 'usage': {
- 'prompt_tokens' : len(prompt),
- 'completion_tokens' : len(token),
- 'total_tokens' : len(prompt) + len(token)
- }
- })
+ chunk = StreamingCompletion.message_queue.get(timeout=0)
+
+ if chunk == b'data: \r\ndata: \r\ndata: \r\n\r\n':
+ chunk = b'data: \n\n\r\n\r\n'
+
+ chunk = chunk.decode()
+
+ chunk = chunk.replace('data: \r\n\r\ndata: ', 'data: \n')
+ chunk = chunk.replace('\r\ndata: \r\ndata: \r\n\r\n', '\n\n\r\n\r\n')
+ chunk = chunk.replace('data: ', '').replace('\r\n\r\n', '')
+
+ yield PhindResponse({
+ 'id' : f'cmpl-1337-{int(time())}',
+ 'object' : 'text_completion',
+ 'created': int(time()),
+ 'model' : model,
+ 'choices': [{
+ 'text' : chunk,
+ 'index' : 0,
+ 'logprobs' : None,
+ 'finish_reason' : 'stop'
+ }],
+ 'usage': {
+ 'prompt_tokens' : len(prompt),
+ 'completion_tokens' : len(chunk),
+ 'total_tokens' : len(prompt) + len(chunk)
+ }
+ })
except Empty:
pass