If you have a large number of examples, you may need to select which ones to include in the prompt. The Example Selector is the class responsible for doing so.
class BaseExampleSelector(ABC):
"""Interface for selecting examples to include in prompts."""
@abstractmethod
def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""Select which examples to use based on the inputs."""
@abstractmethod
def add_example(self, example: Dict[str, str]) -> Any:
"""Add new example to store."""
The only method it needs to define is a select_examples method. This takes in the input variables and then returns a list of examples. It is up to each specific implementation as to how those examples are selected.
from langchain_core.prompts.few_shot import FewShotPromptTemplate
from langchain_core.prompts.prompt import PromptTemplate
example_prompt = PromptTemplate.from_template("Input: {input} -> Output: {output}")
prompt = FewShotPromptTemplate(
example_selector=example_selector,
example_prompt=example_prompt,
suffix="Input: {input} -> Output:",
prefix="Translate the following words from English to Italain:",
input_variables=["input"],
)
print(prompt.format(input="word"))
Similarity Uses semantic similarity between inputs and examples to decide which examples to choose.
MMR Uses Max Marginal Relevance between inputs and examples to decide which examples to choose.
Length Selects examples based on how many can fit within a certain length
Ngram Uses ngram overlap between inputs and examples to decide which examples to choose.
No comments:
Post a Comment