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][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

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#

Async add new example to store.

Parameters:

example (dict[str, str]) – A dictionary with keys as input variables and values as their values.

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]#

Async select which examples to use based on the inputs.

Parameters:

input_variables (dict[str, str]) – A dictionary with keys as input variables and values as their values.

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