0
0
Prompt Engineering / GenAIml~10 mins

Streaming responses to users in Prompt Engineering / GenAI - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to start streaming the AI model's response.

Prompt Engineering / GenAI
response = model.generate(input_text, stream=[1])
Drag options to blanks, or click blank then click option'
A0
BFalse
CNone
DTrue
Attempts:
3 left
💡 Hint
Common Mistakes
Using False disables streaming, so no partial outputs are sent.
2fill in blank
medium

Complete the code to process each chunk of the streamed response.

Prompt Engineering / GenAI
for chunk in response.[1]():
    print(chunk)
Drag options to blanks, or click blank then click option'
Aiter
Bstream
Cread
Dchunks
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read' tries to read all at once, not chunk by chunk.
3fill in blank
hard

Fix the error in the code to correctly handle streaming output.

Prompt Engineering / GenAI
response = model.generate(input_text, stream=True)
for chunk in response.[1]:
    print(chunk)
Drag options to blanks, or click blank then click option'
Aiter()
Bread()
Cstream
Dstream()
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses causes a TypeError when looping.
4fill in blank
hard

Fill both blanks to create a dictionary of tokens and their counts from streamed text.

Prompt Engineering / GenAI
token_counts = {}
for chunk in response.[1]():
    for token in chunk.split():
        token_counts[token] = token_counts.get(token, 0) [2] 1
Drag options to blanks, or click blank then click option'
Aiter
B+
C-=
D*=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-=' or '*=' will decrease or multiply counts incorrectly.
5fill in blank
hard

Fill all three blanks to collect streamed text chunks and join them into a full response string.

Prompt Engineering / GenAI
chunks = []
for chunk in response.[1]():
    chunks.[2](chunk)
full_response = [3].join(chunks)
Drag options to blanks, or click blank then click option'
Aiter
Bappend
C" "
D""
Attempts:
3 left
💡 Hint
Common Mistakes
Joining with a space adds unwanted spaces between chunks.