Skip to content

Instantly share code, notes, and snippets.

@theptrk
Created July 10, 2024 00:39
Show Gist options
  • Save theptrk/9a62961535fafc6a0949809110033d68 to your computer and use it in GitHub Desktop.
Save theptrk/9a62961535fafc6a0949809110033d68 to your computer and use it in GitHub Desktop.
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