phind.com stream + response formatting

This commit is contained in:
t.me/xtekky
2023-04-20 14:51:55 +01:00
parent b2459a5897
commit fa113e6fa7

View File

@@ -204,25 +204,34 @@ class StreamingCompletion:
while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty(): while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty():
try: try:
message = StreamingCompletion.message_queue.get(timeout=0) chunk = StreamingCompletion.message_queue.get(timeout=0)
for token in findall(r'(?<=data: )(.+?)(?=\r\n\r\n)', message.decode()):
yield PhindResponse({ if chunk == b'data: \r\ndata: \r\ndata: \r\n\r\n':
'id' : f'cmpl-1337-{int(time())}', chunk = b'data: \n\n\r\n\r\n'
'object' : 'text_completion',
'created': int(time()), chunk = chunk.decode()
'model' : model,
'choices': [{ chunk = chunk.replace('data: \r\n\r\ndata: ', 'data: \n')
'text' : token, chunk = chunk.replace('\r\ndata: \r\ndata: \r\n\r\n', '\n\n\r\n\r\n')
'index' : 0, chunk = chunk.replace('data: ', '').replace('\r\n\r\n', '')
'logprobs' : None,
'finish_reason' : 'stop' yield PhindResponse({
}], 'id' : f'cmpl-1337-{int(time())}',
'usage': { 'object' : 'text_completion',
'prompt_tokens' : len(prompt), 'created': int(time()),
'completion_tokens' : len(token), 'model' : model,
'total_tokens' : len(prompt) + len(token) '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: except Empty:
pass pass