mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-02 03:26:17 +00:00
Docs refactor (#480)
Big docs refactor! Motivation is to make it easier for people to find resources they are looking for. To accomplish this, there are now three main sections: - Getting Started: steps for getting started, walking through most core functionality - Modules: these are different modules of functionality that langchain provides. Each part here has a "getting started", "how to", "key concepts" and "reference" section (except in a few select cases where it didnt easily fit). - Use Cases: this is to separate use cases (like summarization, question answering, evaluation, etc) from the modules, and provide a different entry point to the code base. There is also a full reference section, as well as extra resources (glossary, gallery, etc) Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
This commit is contained in:
68
docs/modules/prompts/examples/custom_example_selector.md
Normal file
68
docs/modules/prompts/examples/custom_example_selector.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# Create a custom example selector
|
||||
|
||||
In this tutorial, we'll create a custom example selector that selects examples every alternate example given a list of examples.
|
||||
|
||||
An `ExampleSelector` must implement two methods:
|
||||
|
||||
1. An `add_example` method which takes in an example and adds it into the ExampleSelector
|
||||
2. A `select_examples` method which takes in input variables (which are meant to be user input) and returns a list of examples to use in the few shot prompt.
|
||||
|
||||
Let's implement a custom `ExampleSelector` that just selects two examples at random.
|
||||
|
||||
:::{note}
|
||||
Take a look at the current set of example selector implementations supported in LangChain [here](../getting_started.md).
|
||||
:::
|
||||
|
||||
<!-- TODO(shreya): Add the correct link. -->
|
||||
|
||||
## Implement custom example selector
|
||||
|
||||
```python
|
||||
from langchain.prompts.example_selector.base import BaseExampleSelector
|
||||
from typing import Dict, List
|
||||
import numpy as np
|
||||
|
||||
|
||||
class 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
|
||||
|
||||
```python
|
||||
|
||||
examples = [
|
||||
{"foo": "1"},
|
||||
{"foo": "2"},
|
||||
{"foo": "3"}
|
||||
]
|
||||
|
||||
# Initialize example selector.
|
||||
example_selector = CustomExampleSelector(examples)
|
||||
|
||||
|
||||
# Select examples
|
||||
example_selector.select_examples({"foo": "foo"})
|
||||
# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)
|
||||
|
||||
# Add new example to the set of examples
|
||||
example_selector.add_example({"foo": "4"})
|
||||
example_selector.examples
|
||||
# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]
|
||||
|
||||
# Select examples
|
||||
example_selector.select_examples({"foo": "foo"})
|
||||
# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)
|
||||
```
|
Reference in New Issue
Block a user