Created
July 10, 2024 00:39
-
-
Save theptrk/9a62961535fafc6a0949809110033d68 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mock_api_stream(): | |
MESSAGE_CONTENTS = [ | |
"mess", | |
"age", | |
" start", | |
":", | |
" This", | |
" is", | |
" a", | |
" nice ", | |
"mess", | |
"age", | |
" buddy", | |
"message", | |
" end", | |
":", | |
] | |
for x in MESSAGE_CONTENTS: | |
yield x | |
start_message = "message start:" | |
end_message = "message end:" | |
def handle_stream(token): | |
""" | |
Goal: do not yield the start_message or end_message | |
print below: | |
{'message_partial': ' This'} | |
{'message_partial': ' is a'} | |
{'message_partial': 'message : hello'} | |
""" | |
stream = mock_api_stream() | |
for token in stream: | |
yield {"message_partial": token} | |
def main(): | |
for token in handle_stream(): | |
print({"message_partial": token}) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment