core: Add async methods to LengthBasedExampleSelector (#19640)

This commit is contained in:
Christophe Bornet 2024-03-27 18:05:58 +01:00 committed by GitHub
parent c7f1962f73
commit 409c6eeb0b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -34,6 +34,10 @@ class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):
string_example = self.example_prompt.format(**example) string_example = self.example_prompt.format(**example)
self.example_text_lengths.append(self.get_text_length(string_example)) self.example_text_lengths.append(self.get_text_length(string_example))
async def aadd_example(self, example: Dict[str, str]) -> None:
"""Add new example to list."""
self.add_example(example)
@validator("example_text_lengths", always=True) @validator("example_text_lengths", always=True)
def calculate_example_text_lengths(cls, v: List[int], values: Dict) -> List[int]: def calculate_example_text_lengths(cls, v: List[int], values: Dict) -> List[int]:
"""Calculate text lengths if they don't exist.""" """Calculate text lengths if they don't exist."""
@ -61,3 +65,7 @@ class LengthBasedExampleSelector(BaseExampleSelector, BaseModel):
remaining_length = new_length remaining_length = new_length
i += 1 i += 1
return examples return examples
async def aselect_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the input lengths."""
return self.select_examples(input_variables)