[docs]defcast_to_run(self,previous_result:pd.DataFrame,*args,**kwargs):logger.info(f"Running generator node - {self.__class__.__name__} module...")assert("prompts"inprevious_result.columns),"previous_result must contain prompts column."prompts=previous_result["prompts"].tolist()returnprompts
[docs]defstructured_output(self,prompts:List[str],output_cls):response,_,_=self._pure(prompts)parser=PydanticOutputParser(output_cls)result=[]forresinresponse:try:result.append(parser.parse(res))exceptExceptionase:logger.warning(f"Error parsing response: {e}\nSo returning None instead in this case.")result.append(None)returnresult
[docs]defgenerator_node(func):@functools.wraps(func)@result_to_dataframe(["generated_texts","generated_tokens","generated_log_probs"])defwrapper(project_dir:Union[str,Path],previous_result:pd.DataFrame,llm:str,**kwargs)->Tuple[List[str],List[List[int]],List[List[float]]]:""" This decorator makes a generator module to be a node. It automatically extracts prompts from previous_result and runs the generator function. Plus, it retrieves the llm instance from autorag.generator_models. :param project_dir: The project directory. :param previous_result: The previous result that contains prompts, :param llm: The llm name that you want to use. :param kwargs: The extra parameters for initializing the llm instance. :return: Pandas dataframe that contains generated texts, generated tokens, and generated log probs. Each column is "generated_texts", "generated_tokens", and "generated_log_probs". """logger.info(f"Running generator node - {func.__name__} module...")assert("prompts"inprevious_result.columns),"previous_result must contain prompts column."prompts=previous_result["prompts"].tolist()iffunc.__name__=="llama_index_llm":ifllmnotingenerator_models:raiseValueError(f"{llm} is not a valid llm name. Please check the llm name.""You can check valid llm names from autorag.generator_models.")batch=kwargs.pop("batch",16)ifllm=="huggingfacellm":model_name=kwargs.pop("model",None)ifmodel_nameisnotNone:kwargs["model_name"]=model_nameelse:if"model_name"notinkwargs.keys():raiseValueError("`model` or `model_name` parameter must be provided for using huggingfacellm.")kwargs["tokenizer_name"]=kwargs["model_name"]llm_instance=generator_models[llm](**kwargs)result=func(prompts=prompts,llm=llm_instance,batch=batch)delllm_instancereturnresultelse:returnfunc(prompts=prompts,llm=llm,**kwargs)returnwrapper