amazon-bedrock-workshop icon indicating copy to clipboard operation
amazon-bedrock-workshop copied to clipboard

Amazon Bedrock Workshop - [Bug] Streaming example in bedrock_basics.ipynb fails to output text

Open jicowan opened this issue 3 months ago • 0 comments

Describe the bug When you run the invoke_model_with_response_stream example in the bedrock_basics notebook, nothing is streamed to stdout.

Here is the code from the example:

    if stream:
        for event in stream:
            chunk = event.get('chunk')
            if chunk:
                chunk_obj = json.loads(chunk.get('bytes').decode())
                if 'outputText' in chunk_obj:
                    text = chunk_obj.get('outputText', None)
                    print(text,end='')
                    if not text :
                        break
                    #text = chunk_obj['outputText']
                    clear_output(wait=True)
                    output.append(text)
                    display_markdown(Markdown(''.join(output)))

I replaced the code block above with the following:

    if stream:
        for event in stream:
            chunk = event.get('chunk')
            if chunk:
                chunk_obj = json.loads(chunk.get('bytes').decode())
                text = chunk_obj['outputText']
                display_markdown(Markdown(print(text, end='')))

Expected behavior The results should be streamed to stdout

Desktop (please complete the following information):

  • OS: MacOS
  • Browser Chrome
  • Version NA

! Additional context !

  • Using PyCharm 2024.1

jicowan avatar Apr 23 '24 15:04 jicowan