Skip to content

Commit

Permalink
phind.com stream + response formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
xtekky committed Apr 20, 2023
1 parent b2459a5 commit fa113e6
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions phind/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,34 @@ def create(

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
Expand Down

0 comments on commit fa113e6

Please sign in to comment.