Take a look at the current set of example selector implementations supported in LangChain here.Implement custom example selector‚Äãfrom langchain.prompts.example_selector.base import BaseExampleSelectorfrom typing import Dict, Listimport numpy as npclass CustomExampleSelector(BaseExampleSelector):        def __init__(self, examples: List[Dict[str, str]]):        self.examples = examples        def add_example(self, example: Dict[str, str]) -> None:        """Add new example to store for a key."""        self.examples.append(example)    def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:        """Select which examples to use based on the inputs."""        return np.random.choice(self.examples, size=2, replace=False)Use custom example selector‚Äãexamples = [    {"foo": "1"},    {"foo": "2"},    {"foo": "3"}]# Initialize example selector.example_selector = CustomExampleSelector(examples)# Select examplesexample_selector.select_examples({"foo": "foo"})# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)# Add new example to the set of examplesexample_selector.add_example({"foo": "4"})example_selector.examples# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]# Select examplesexample_selector.select_examples({"foo": "foo"})# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)PreviousExample selectorsNextSelect by lengthImplement custom example selectorUse custom example selectorCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.
In this tutorial, we'll create a custom example selector that selects examples randomly from a given list of examples.
In this tutorial, we'll create a custom example selector that selects examples randomly from a given list of examples. ->: Take a look at the current set of example selector implementations supported in LangChain here.Implement custom example selector‚Äãfrom langchain.prompts.example_selector.base import BaseExampleSelectorfrom typing import Dict, Listimport numpy as npclass CustomExampleSelector(BaseExampleSelector):        def __init__(self, examples: List[Dict[str, str]]):        self.examples = examples        def add_example(self, example: Dict[str, str]) -> None:        """Add new example to store for a key."""        self.examples.append(example)    def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:        """Select which examples to use based on the inputs."""        return np.random.choice(self.examples, size=2, replace=False)Use custom example selector‚Äãexamples = [    {"foo": "1"},    {"foo": "2"},    {"foo": "3"}]# Initialize example selector.example_selector = CustomExampleSelector(examples)# Select examplesexample_selector.select_examples({"foo": "foo"})# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)# Add new example to the set of examplesexample_selector.add_example({"foo": "4"})example_selector.examples# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]# Select examplesexample_selector.select_examples({"foo": "foo"})# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)PreviousExample selectorsNextSelect by lengthImplement custom example selectorUse custom example selectorCommunityDiscordTwitterGitHubPythonJS/TSMoreHomepageBlogCopyright ¬© 2023 LangChain, Inc.