aws-samples / amazon-bedrock-workshop

This is a workshop designed for Amazon Bedrock a foundational model service.
https://catalog.us-east-1.prod.workshops.aws/workshops/a4bdb007-5600-4368-81c5-ff5b4154f518/en-US/20-intro
MIT No Attribution
1.27k stars 545 forks source link

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

Closed jicowan closed 2 months ago

jicowan commented 2 months ago

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):

! Additional context !

w601sxs commented 2 months ago

I fixed this in the latest update and tested with titan text premier