Example Selector
Logic for selecting examples to include in prompts.
class langchain.prompts.example_selector.LengthBasedExampleSelector(*, examples, example_prompt, get_text_length=<function _get_length_based>, max_length=2048, example_text_lengths=[])[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select examples based on length.
Parameters
examples (List[dict]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
get_text_length (Callable[[str], int]) – 
max_length (int) – 
example_text_lengths (List[int]) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.
attribute get_text_length: Callable[[str], int] = <function _get_length_based>
Function to measure prompt length. Defaults to word count.
attribute max_length: int = 2048
Max length for the prompt, beyond which examples are cut.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) – 
Return type
None
select_examples(input_variables)[source]
Select which examples to use based on the input lengths.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.example_selector.MaxMarginalRelevanceExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None, fetch_k=20)[source]
Bases: langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
ExampleSelector that selects examples based on Max Marginal Relevance.
This was shown to improve performance in this paper:
https://arxiv.org/pdf/2211.13892.pdf
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) – 
k (int) – 
example_keys (Optional[List[str]]) – 
input_keys (Optional[List[str]]) – 
fetch_k (int) – 
Return type
None
attribute fetch_k: int = 20
Number of examples to fetch to rerank.
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, fetch_k=20, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An iniialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
fetch_k (int) – 
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.MaxMarginalRelevanceExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]
class langchain.prompts.example_selector.NGramOverlapExampleSelector(*, examples, example_prompt, threshold=- 1.0)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Select and order examples based on ngram overlap score (sentence_bleu score).
https://www.nltk.org/_modules/nltk/translate/bleu_score.html
https://aclanthology.org/P02-1040.pdf
Parameters
examples (List[dict]) – 
example_prompt (langchain.prompts.prompt.PromptTemplate) – 
threshold (float) – 
Return type
None
attribute example_prompt: langchain.prompts.prompt.PromptTemplate [Required]
Prompt template used to format the examples.
attribute examples: List[dict] [Required]
A list of the examples that the prompt template expects.
attribute 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.
add_example(example)[source]
Add new example to list.
Parameters
example (Dict[str, str]) – 
Return type
None
select_examples(input_variables)[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]
class langchain.prompts.example_selector.SemanticSimilarityExampleSelector(*, vectorstore, k=4, example_keys=None, input_keys=None)[source]
Bases: langchain.prompts.example_selector.base.BaseExampleSelector, pydantic.main.BaseModel
Example selector that selects examples based on SemanticSimilarity.
Parameters
vectorstore (langchain.vectorstores.base.VectorStore) – 
k (int) – 
example_keys (Optional[List[str]]) – 
input_keys (Optional[List[str]]) – 
Return type
None
attribute example_keys: Optional[List[str]] = None
Optional keys to filter examples to.
attribute input_keys: Optional[List[str]] = None
Optional keys to filter input to. If provided, the search is based on
the input variables instead of all variables.
attribute k: int = 4
Number of examples to select.
attribute vectorstore: langchain.vectorstores.base.VectorStore [Required]
VectorStore than contains information about examples.
add_example(example)[source]
Add new example to vectorstore.
Parameters
example (Dict[str, str]) – 
Return type
str
classmethod from_examples(examples, embeddings, vectorstore_cls, k=4, input_keys=None, **vectorstore_cls_kwargs)[source]
Create k-shot example selector using example list and embeddings.
Reshuffles examples dynamically based on query similarity.
Parameters
examples (List[dict]) – List of examples to use in the prompt.
embeddings (langchain.embeddings.base.Embeddings) – An initialized embedding API interface, e.g. OpenAIEmbeddings().
vectorstore_cls (Type[langchain.vectorstores.base.VectorStore]) – A vector store DB interface class, e.g. FAISS.
k (int) – Number of examples to select
input_keys (Optional[List[str]]) – If provided, the search is based on the input variables
instead of all variables.
vectorstore_cls_kwargs (Any) – optional kwargs containing url for vector store
Returns
The ExampleSelector instantiated, backed by a vector store.
Return type
langchain.prompts.example_selector.semantic_similarity.SemanticSimilarityExampleSelector
select_examples(input_variables)[source]
Select which examples to use based on semantic similarity.
Parameters
input_variables (Dict[str, str]) – 
Return type
List[dict]