Source code for langchain.smith.evaluation.progress
"""A simple progress bar for the console."""importthreadingfromtypingimportAny,Dict,Optional,SequencefromuuidimportUUIDfromlangchain_core.callbacksimportbaseasbase_callbacksfromlangchain_core.documentsimportDocumentfromlangchain_core.outputsimportLLMResult
[docs]classProgressBarCallback(base_callbacks.BaseCallbackHandler):"""A simple progress bar for the console."""
[docs]def__init__(self,total:int,ncols:int=50,**kwargs:Any):"""Initialize the progress bar. Args: total: int, the total number of items to be processed. ncols: int, the character width of the progress bar. """self.total=totalself.ncols=ncolsself.counter=0self.lock=threading.Lock()self._print_bar()
[docs]defincrement(self)->None:"""Increment the counter and update the progress bar."""withself.lock:self.counter+=1self._print_bar()
def_print_bar(self)->None:"""Print the progress bar to the console."""progress=self.counter/self.totalarrow="-"*int(round(progress*self.ncols)-1)+">"spaces=" "*(self.ncols-len(arrow))print(f"\r[{arrow+spaces}] {self.counter}/{self.total}",end="")# noqa: T201