t.me/xtekky commited on
Commit
fa113e6
1 Parent(s): b2459a5

phind.com stream + response formatting

Browse files
Files changed (1) hide show
  1. phind/__init__.py +28 -19
phind/__init__.py CHANGED
@@ -204,25 +204,34 @@ class StreamingCompletion:
204
 
205
  while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty():
206
  try:
207
- message = StreamingCompletion.message_queue.get(timeout=0)
208
- for token in findall(r'(?<=data: )(.+?)(?=\r\n\r\n)', message.decode()):
209
- yield PhindResponse({
210
- 'id' : f'cmpl-1337-{int(time())}',
211
- 'object' : 'text_completion',
212
- 'created': int(time()),
213
- 'model' : model,
214
- 'choices': [{
215
- 'text' : token,
216
- 'index' : 0,
217
- 'logprobs' : None,
218
- 'finish_reason' : 'stop'
219
- }],
220
- 'usage': {
221
- 'prompt_tokens' : len(prompt),
222
- 'completion_tokens' : len(token),
223
- 'total_tokens' : len(prompt) + len(token)
224
- }
225
- })
 
 
 
 
 
 
 
 
 
226
 
227
  except Empty:
228
  pass
 
204
 
205
  while StreamingCompletion.stream_completed != True or not StreamingCompletion.message_queue.empty():
206
  try:
207
+ chunk = StreamingCompletion.message_queue.get(timeout=0)
208
+
209
+ if chunk == b'data: \r\ndata: \r\ndata: \r\n\r\n':
210
+ chunk = b'data: \n\n\r\n\r\n'
211
+
212
+ chunk = chunk.decode()
213
+
214
+ chunk = chunk.replace('data: \r\n\r\ndata: ', 'data: \n')
215
+ chunk = chunk.replace('\r\ndata: \r\ndata: \r\n\r\n', '\n\n\r\n\r\n')
216
+ chunk = chunk.replace('data: ', '').replace('\r\n\r\n', '')
217
+
218
+ yield PhindResponse({
219
+ 'id' : f'cmpl-1337-{int(time())}',
220
+ 'object' : 'text_completion',
221
+ 'created': int(time()),
222
+ 'model' : model,
223
+ 'choices': [{
224
+ 'text' : chunk,
225
+ 'index' : 0,
226
+ 'logprobs' : None,
227
+ 'finish_reason' : 'stop'
228
+ }],
229
+ 'usage': {
230
+ 'prompt_tokens' : len(prompt),
231
+ 'completion_tokens' : len(chunk),
232
+ 'total_tokens' : len(prompt) + len(chunk)
233
+ }
234
+ })
235
 
236
  except Empty:
237
  pass