Skip to content

Commit

Permalink
fix stream newline
Browse files Browse the repository at this point in the history
  • Loading branch information
tuananhdao committed Nov 29, 2023
1 parent 1a246bb commit 667b961
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions stream_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,33 +24,33 @@ async def run(prompt):
generate_text.arun(prompt),
stream_callback.done)
)
yield '{"choices": [{"index": 0, "delta": {"content": " end"}, "context": {"followup_questions": [], "data_points": []}, "session_state": null}]}'
await asyncio.sleep(0.1)
yield '{"choices": [{"index": 0, "delta": {"content": " end"}, "context": {"followup_questions": [], "data_points": []}, "session_state": null}]}\n'
#await asyncio.sleep(0.1)
batch = 5
iter = 0
batch_string = ''
async for token in stream_callback.aiter():
#print(token)
# Use server-sent-events to stream the response
if iter == batch:
yield '{"choices": [{"index": 0, "delta": {"content": "'+batch_string+'"}, "context": {"followup_questions": []}, "session_state": null}]}'
print(batch_string)
yield '{"choices": [{"index": 0, "delta": {"content": "'+batch_string+'"}, "context": {"followup_questions": []}, "session_state": null}]}\n'
batch_string = ''
iter = 0
batch_string += token
iter += 1
print(batch_string)
yield '{"choices": [{"index": 0, "delta": {"content": "'+batch_string+'"}, "context": {"followup_questions": []}, "session_state": null}]}\n'

await task
#stream_callback.done.clear()
await asyncio.sleep(0.1)
yield '{"choices": [{"index": 0, "delta": {"content": "'+batch_string+'"}, "context": {"followup_questions": []}, "session_state": null}]}'
await asyncio.sleep(0.1)
#await asyncio.sleep(0.1)
yield '{"choices": [{"index": 0, "delta": {}, "context": {"followup_questions": []}, "session_state": null}]}'

# yield '{"choices": [{"index": 0, "delta": {"content": " end"}, "context": {"followup_questions": [], "data_points": []}, "session_state": null}]}'
# await asyncio.sleep(0.1)
# yield '{"choices": [{"index": 0, "delta": {"content": " end"}, "context": {"followup_questions": [], "data_points": []}, "session_state": null}]}\n'
# for i in range(100):
# yield '{"choices": [{"index": 0, "delta": {"content": "'+str(i)+'?aa "}, "context": {"followup_questions": []}, "session_state": null}]}'
# await asyncio.sleep(0.1)
# print(i)
# yield '{"choices": [{"index": 0, "delta": {"content": "'+str(i)+'?aa "}, "context": {"followup_questions": []}, "session_state": null}]}\n'
# yield '{"choices": [{"index": 0, "delta": {}, "context": {"followup_questions": []}, "session_state": null}]}'

@app.post("/q")
Expand Down

0 comments on commit 667b961

Please sign in to comment.