t.me/xtekky
commited on
Commit
•
fa113e6
1
Parent(s):
b2459a5
phind.com stream + response formatting
Browse files- 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 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
|
214 |
-
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
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
|