Source code for langchain.callbacks.streaming_stdout_final_only
"""Callback Handler streams to stdout on new llm token."""importsysfromtypingimportAny,Dict,List,Optionalfromlangchain_core.callbacksimportStreamingStdOutCallbackHandlerDEFAULT_ANSWER_PREFIX_TOKENS=["Final","Answer",":"]
[docs]classFinalStreamingStdOutCallbackHandler(StreamingStdOutCallbackHandler):"""Callback handler for streaming in agents. Only works with agents using LLMs that support streaming. Only the final output of the agent will be streamed. """
[docs]def__init__(self,*,answer_prefix_tokens:Optional[List[str]]=None,strip_tokens:bool=True,stream_prefix:bool=False,)->None:"""Instantiate FinalStreamingStdOutCallbackHandler. Args: answer_prefix_tokens: Token sequence that prefixes the answer. Default is ["Final", "Answer", ":"] strip_tokens: Ignore white spaces and new lines when comparing answer_prefix_tokens to last tokens? (to determine if answer has been reached) stream_prefix: Should answer prefix itself also be streamed? """super().__init__()ifanswer_prefix_tokensisNone:self.answer_prefix_tokens=DEFAULT_ANSWER_PREFIX_TOKENSelse:self.answer_prefix_tokens=answer_prefix_tokensifstrip_tokens:self.answer_prefix_tokens_stripped=[token.strip()fortokeninself.answer_prefix_tokens]else:self.answer_prefix_tokens_stripped=self.answer_prefix_tokensself.last_tokens=[""]*len(self.answer_prefix_tokens)self.last_tokens_stripped=[""]*len(self.answer_prefix_tokens)self.strip_tokens=strip_tokensself.stream_prefix=stream_prefixself.answer_reached=False
[docs]defon_llm_start(self,serialized:Dict[str,Any],prompts:List[str],**kwargs:Any)->None:"""Run when LLM starts running."""self.answer_reached=False
[docs]defon_llm_new_token(self,token:str,**kwargs:Any)->None:"""Run on new LLM token. Only available when streaming is enabled."""# Remember the last n tokens, where n = len(answer_prefix_tokens)self.append_to_last_tokens(token)# Check if the last n tokens match the answer_prefix_tokens list ...ifself.check_if_answer_reached():self.answer_reached=Trueifself.stream_prefix:fortinself.last_tokens:sys.stdout.write(t)sys.stdout.flush()return# ... if yes, then print tokens from now onifself.answer_reached:sys.stdout.write(token)sys.stdout.flush()