Complete the code to enable streaming in the LangChain LLM call.
llm = OpenAI(streaming=[1])Setting streaming=True enables the model to send partial outputs as they are generated.
Complete the code to define a callback handler for streaming tokens.
class StreamHandler(BaseCallbackHandler): def on_llm_new_token(self, token: str, **kwargs): print([1])
kwargs instead of the token string.self which is the class instance.The token parameter contains the new token received during streaming.
Fix the error in attaching the callback handler to the LLM for streaming.
llm = OpenAI(streaming=True, callbacks=[[1]])
You must pass an instance of the handler, not the class itself.
Fill both blanks to create a streaming chain with a prompt and callback handler.
chain = LLMChain(llm=llm, prompt=[1], callbacks=[[2]])
The prompt_template is the prompt object, and StreamHandler() is the callback instance for streaming.
Fill all three blanks to start streaming and handle tokens with a callback.
llm = OpenAI(streaming=[1], callbacks=[[2]]) chain = LLMChain(llm=llm, prompt=[3])
Streaming must be enabled with True, the callback handler instance is StreamHandler(), and the prompt is prompt_template.