langchain_community.example_selectors.ngram_overlap.NGramOverlapExampleSelector

class langchain_community.example_selectors.ngram_overlap.NGramOverlapExampleSelector[source]

Bases: BaseExampleSelector, BaseModel

Select and order examples based on ngram overlap score (sentence_bleu score from NLTK package).

https://www.nltk.org/_modules/nltk/translate/bleu_score.html https://aclanthology.org/P02-1040.pdf

Create a new model by parsing and validating input data from keyword arguments.

Raises ValidationError if the input data cannot be parsed to form a valid model.

param example_prompt: PromptTemplate [Required]

Prompt template used to format the examples.

param examples: List[dict] [Required]

A list of the examples that the prompt template expects.

param threshold: float = -1.0

Threshold at which algorithm stops. Set to -1.0 by default.

For negative threshold: select_examples sorts examples by ngram_overlap_score, but excludes none. For threshold greater than 1.0: select_examples excludes all examples, and returns an empty list. For threshold equal to 0.0: select_examples sorts examples by ngram_overlap_score, and excludes examples with no ngram overlap with input.

async aadd_example(example: Dict[str, str]) Any

Add new example to store.

Parameters

example (Dict[str, str]) –

Return type

Any

add_example(example: Dict[str, str]) None[source]

Add new example to list.

Parameters

example (Dict[str, str]) –

Return type

None

async aselect_examples(input_variables: Dict[str, str]) List[dict]

Select which examples to use based on the inputs.

Parameters

input_variables (Dict[str, str]) –

Return type

List[dict]

select_examples(input_variables: Dict[str, str]) List[dict][source]

Return list of examples sorted by ngram_overlap_score with input.

Descending order. Excludes any examples with ngram_overlap_score less than or equal to threshold.

Parameters

input_variables (Dict[str, str]) –

Return type

List[dict]

Examples using NGramOverlapExampleSelector