【LangChain】Prompts之示例选择器

LangChain学习文档


概要

如果您有大量示例,您可能需要选择要包含在提示中的哪个示例。示例选择器是负责执行此操作的类。

基本接口定义如下:

python 复制代码
class BaseExampleSelector(ABC):
    """用于选择要包含在提示中的示例的界面。"""

    @abstractmethod
    def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
        """根据输入选择要使用的示例。"""

它需要公开的唯一方法是 select_examples 方法。这需要接受输入变量,然后返回示例列表。如何选择这些示例取决于每个具体的实现。

自定义示例选择器(Custom example selector)

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

ExampleSelector 必须实现两个方法:

  1. add_example 方法接受一个示例并将其添加到 ExampleSelector

  2. select_examples 方法,它接受输入变量并返回部分示例列表或全部列表。

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

这里查看 LangChain 支持的当前示例选择器实现集。

实现自定义示例选择器(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:
        """添加新示例来存储密钥。"""
        self.examples.append(example)

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

参考api:BaseExampleSelector from langchain.prompts.example_selector.base

使用自定义示例选择器(Use custom example selector)

python 复制代码
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)

总结

本文讲解的是示例选择器。就当我们有多个示例时,可以帮助我们选择哪个示例!

套路,就两个主要步骤:

  1. add_example方法,它接受一个示例并将其添加到该ExampleSelector中。
  2. select_examples方法,它接受输入变量并返回部分示例列表或全部列表。

参考地址:

https://python.langchain.com/docs/modules/model_io/prompts/example_selectors/custom_example_selector

相关推荐
大数据追光猿6 小时前
【Prompt】Prompt Caching:原理、实现与高并发价值
人工智能·大模型·prompt·agent
user297525876126 小时前
AI实践:结合LangChain实现一个自动生成项目README的VSCode插件
langchain·node.js·visual studio code
Coder_Boy_7 小时前
基于DDD+Spring Boot 3.2+LangChain4j构建企业级智能客服系统 版本升级
java·人工智能·spring boot·后端·langchain
xier_ran9 小时前
Agent基础:大模型交互与推理技术Prompt 工程、Function Calling、ReAct、Self-Refine
react.js·prompt·交互
MoSTChillax10 小时前
用 Figma Make 提示词生成可交互应用原型:从入门到避坑
prompt·figma·原型设计·产品原型·figma make
小芳矶12 小时前
【langchain框架—组合链】利用组合链完成客服优先等级的划分
langchain
有点笨的蛋13 小时前
LangChain 入门与实践:从 LLM 调用到 AI 工作流的工程化思维
前端·langchain
工藤学编程13 小时前
AI Ping 赋能:基于 GLM-4.7(免费!)+ LangChain + Redis 打造智能AI聊天助手
人工智能·redis·langchain
semantist@语校13 小时前
第五十七篇|东京银星日本语学校的数据建模:高密度城市中的学习节律、制度边界与 Prompt 接口设计
大数据·数据库·人工智能·学习·百度·prompt·知识图谱
啊吧怪不啊吧13 小时前
新品限免|国产大模型工程化实战:GLM-4.7与MiniMax M2.1 免费选型对比
人工智能·机器学习·langchain