当前位置: 首页 > news >正文

【LangChain】Prompts之示例选择器

LangChain学习文档

  • 【LangChain】向量存储(Vector stores)
  • 【LangChain】向量存储之FAISS
  • 【LangChain】Prompts之Prompt templates
  • 【LangChain】Prompts之自定义提示模板
  • 【LangChain】Prompts之示例选择器

概要

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

基本接口定义如下:

class BaseExampleSelector(ABC):"""用于选择要包含在提示中的示例的界面。"""@abstractmethoddef 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)

from langchain.prompts.example_selector.base import BaseExampleSelector
from typing import Dict, List
import numpy as npclass CustomExampleSelector(BaseExampleSelector):def __init__(self, examples: List[Dict[str, str]]):self.examples = examplesdef 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)


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

http://www.lryc.cn/news/121830.html

相关文章:

  • Neo4j之CREATE基础
  • Kali Hyper-V安装正常启动后 黑屏 只能进命令模式
  • 【人工智能124种任务大集合】-集齐了自然语言处理(NLP),计算机视觉(CV),语音识别,多模态等任务
  • IntelliJ IDEA快捷键大全
  • 阿里云轻量应用服务器_2核4G4M_2核2G3M_性能测评
  • 猿人学刷题系列(第一届比赛)——第二题( js 混淆 - 动态cookie 1)
  • ubuntu网络管理
  • 您可能并不需要单页应用程序
  • 基于低代码和数字孪生技术的电力运维平台设计
  • 【Github】SourceTree技巧汇总
  • 人工智能轨道交通行业周刊-第55期(2023.8.7-8.13)
  • 向量数据库 Milvus Cloud Partition Key:租户数量多,单个租户数据少的三种解决方案
  • 文本三剑客之grep命令和awk命令 1.0 版本
  • 【软件测试】Linux环境Ant调用Jmeter脚本并且生成测试报告(详细)
  • MySQL的YEAR函数
  • 208、仿真-51单片机脉搏心率与心电报警Proteus仿真设计(程序+Proteus仿真+配套资料等)
  • 787. 归并排序
  • 【马蹄集】第二十二周——进位制与字符串专题
  • 【Spring Cloud +Vue+UniApp】智慧建筑工地平台源码
  • 使用一个python脚本抓取大量网站【2/3】
  • 黑马项目一完结后阶段面试45题 JavaSE基础部分20题(二)
  • 防御第九次作业
  • Java刷题——代码随想录Day1
  • android,Compose,消息列表和动画(点击item的时候,就会删除)
  • go-admin 使用开发
  • 力扣的板子
  • 基于Matlab实现路径规划算法(附上15个完整仿真源码)
  • 纯跟踪(Pure Pursuit)路径跟踪算法研究(2)
  • 前后端分离------后端创建笔记(02)
  • Webpack5 Preload/Prefetch技术