Source code for langchain_core.example_selectors.length_based
"""Select examples based on length."""importrefromtypingimportCallable,Dict,Listfromlangchain_core.example_selectors.baseimportBaseExampleSelectorfromlangchain_core.prompts.promptimportPromptTemplatefromlangchain_core.pydantic_v1importBaseModel,validatordef_get_length_based(text:str)->int:returnlen(re.split("\n| ",text))
[docs]classLengthBasedExampleSelector(BaseExampleSelector,BaseModel):"""Select examples based on length."""examples:List[dict]"""A list of the examples that the prompt template expects."""example_prompt:PromptTemplate"""Prompt template used to format the examples."""get_text_length:Callable[[str],int]=_get_length_based"""Function to measure prompt length. Defaults to word count."""max_length:int=2048"""Max length for the prompt, beyond which examples are cut."""example_text_lengths:List[int]=[]#: :meta private:"""Length of each example."""
[docs]defadd_example(self,example:Dict[str,str])->None:"""Add new example to list. Args: example: A dictionary with keys as input variables and values as their values. """self.examples.append(example)string_example=self.example_prompt.format(**example)self.example_text_lengths.append(self.get_text_length(string_example))
[docs]asyncdefaadd_example(self,example:Dict[str,str])->None:"""Async add new example to list. Args: example: A dictionary with keys as input variables and values as their values. """self.add_example(example)
@validator("example_text_lengths",always=True)defcalculate_example_text_lengths(cls,v:List[int],values:Dict)->List[int]:"""Calculate text lengths if they don't exist."""# Check if text lengths were passed inifv:returnv# If they were not, calculate themexample_prompt=values["example_prompt"]get_text_length=values["get_text_length"]string_examples=[example_prompt.format(**eg)foreginvalues["examples"]]return[get_text_length(eg)foreginstring_examples]
[docs]defselect_examples(self,input_variables:Dict[str,str])->List[dict]:"""Select which examples to use based on the input lengths. Args: input_variables: A dictionary with keys as input variables and values as their values. Returns: A list of examples to include in the prompt. """inputs=" ".join(input_variables.values())remaining_length=self.max_length-self.get_text_length(inputs)i=0examples=[]whileremaining_length>0andi<len(self.examples):new_length=remaining_length-self.example_text_lengths[i]ifnew_length<0:breakelse:examples.append(self.examples[i])remaining_length=new_lengthi+=1returnexamples
[docs]asyncdefaselect_examples(self,input_variables:Dict[str,str])->List[dict]:"""Async select which examples to use based on the input lengths. Args: input_variables: A dictionary with keys as input variables and values as their values. Returns: A list of examples to include in the prompt. """returnself.select_examples(input_variables)