0
0
Prompt Engineering / GenAIml~10 mins

Streaming responses 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_stream(prompt=[1])
Drag options to blanks, or click blank then click option'
Atext
Binput_text
Cprompt
Dquery
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names like 'text' or 'query' which are not defined in this context.
2fill in blank
medium

Complete the code to read the streamed tokens from the response.

Prompt Engineering / GenAI
for token in response.[1]():
    print(token)
Drag options to blanks, or click blank then click option'
Astream_tokens
Bstream
Citer_tokens
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using generic method names like 'read' or 'stream' which do not exist.
3fill in blank
hard

Fix the error in the code to properly handle streaming with a callback function.

Prompt Engineering / GenAI
def on_token(token):
    print(token)

model.generate_stream(prompt=prompt, [1]=on_token)
Drag options to blanks, or click blank then click option'
Acallback
Bon_token
Cstream_callback
Dtoken_handler
Attempts:
3 left
💡 Hint
Common Mistakes
Passing the function with incorrect parameter names like 'on_token' or 'token_handler'.
4fill in blank
hard

Fill both blanks to correctly initialize streaming with a timeout and a callback.

Prompt Engineering / GenAI
response = model.generate_stream(prompt=prompt, [1]=5, [2]=handle_token)
Drag options to blanks, or click blank then click option'
Atimeout
Bmax_tokens
Ccallback
Dstream
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing 'max_tokens' with 'timeout' or using wrong parameter names.
5fill in blank
hard

Fill both blanks to collect streamed tokens into a string with a callback and print final output.

Prompt Engineering / GenAI
collected = []
def collect_token(token):
    collected.[1](token)

model.generate_stream(prompt=prompt, [2]=collect_token)
print(''.join(collected))
Drag options to blanks, or click blank then click option'
Aappend
Bcallback
D.strip()
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong list method like 'extend' or adding extra string methods after join.