Skip to main content

自定义示例选择器

在本教程中,我们将创建一个自定义示例选择器,该选择器从给定的示例列表中选择每个交替示例。

ExampleSelector 必须实现两个方法:

  1. add_example 方法,接受一个示例并将其添加到 ExampleSelector 中
  2. select_examples 方法,接受输入变量(用于用户输入)并返回要在 few shot prompt 中使用的示例列表。

让我们实现一个简单的自定义 ExampleSelector,它只会随机选择两个示例。

:::{note} 查看当前在 LangChain 中支持的示例选择器实现,请点击这里。:::

实现自定义示例选择器

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:
"""将新示例添加到存储中的键。"""
self.examples.append(example)

def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
"""根据输入选择要使用的示例。"""
return np.random.choice(self.examples, size=2, replace=False)

API 参考:

使用自定义示例选择器


examples = [
{"foo": "1"},
{"foo": "2"},
{"foo": "3"}
]

# 初始化示例选择器。
example_selector = CustomExampleSelector(examples)


# 选择示例
example_selector.select_examples({"foo": "foo"})
# -> array([{'foo': '2'}, {'foo': '3'}], dtype=object)

# 将新示例添加到示例集中
example_selector.add_example({"foo": "4"})
example_selector.examples
# -> [{'foo': '1'}, {'foo': '2'}, {'foo': '3'}, {'foo': '4'}]

# 选择示例
example_selector.select_examples({"foo": "foo"})
# -> array([{'foo': '1'}, {'foo': '4'}], dtype=object)