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

【LangChain指南】样例选择器(Example selectors)

在我们构建大语言模型应用时,经常会用到 Few-Shot Prompting(少样本提示),也就是在给模型的提示词(Prompt)中放入几个输入-输出的例子,来引导模型理解我们想要它完成的任务。比如,告诉它“happy”的反义词是“sad”,“tall”的反义词是“short”,然后让它去推理“large”的反义词。
但是,当我们手头有成百上千个例子时,不可能把它们全部塞进一个Prompt里。一方面,会超出模型的上下文窗口(Context Window)长度限制;另一方面,塞入不相关或冗余的例子,反而会干扰模型,降低其表现。

这就是 Example Selector 大显身手的地方!

它的核心作用就是:根据当前的用户输入,从海量的示例库中,智能地挑选出最相关、最有帮助的几个例子,动态地构建Prompt。


一、基础概念与自定义选择器

首先,我们来看最基础的概念。所有的 Example Selector 都需要实现一个核心方法:select_examples。这个方法接收用户的输入(一个字典),然后返回一个选中的例子列表。

我们可以自己动手写一个简单的选择器。比如,我们写一个根据单词长度来选择最接近例子的选择器。

from langchain_core.example_selectors.base import BaseExampleSelector# 我们的示例库:英文到意大利文的翻译
examples = [{"input": "hi", "output": "ciao"},{"input": "bye", "output": "arrivederci"},{"input": "soccer", "output": "calcio"},
]class LengthBasedExampleSelector(BaseExampleSelector):def __init__(self, examples):self.examples = examplesdef add_example(self, example):"""允许动态添加新例子"""self.examples.append(example)def select_examples(self, input_variables):"""选择与输入单词长度最接近的例子"""target_word = input_variables["input"]target_length = len(target_word)best_example = Nonesmallest_diff = float("inf")for example in self.examples:current_diff = abs(len(example["input"]) - target_length)if current_diff < smallest_diff:smallest_diff = current_diffbest_example = examplereturn [best_example]  # 返回一个列表# 实例化我们的选择器
selector = LengthBasedExampleSelector(examples)# 测试:输入 "okay" (长度为4)
print(selector.select_examples({"input": "okay"}))
# 输出: [{'input': 'bye', 'output': 'arrivederci'}] (bye 长度为3,最接近4)# 添加一个新例子 "hand" -> "mano" (长度为4)
selector.add_example({"input": "hand", "output": "mano"})
print

文章转载自:

http://69Q0pC9a.prznc.cn
http://QX54Y7ig.prznc.cn
http://TclvmeN9.prznc.cn
http://4lzHABp9.prznc.cn
http://BTnmD31v.prznc.cn
http://MdsgfIWR.prznc.cn
http://bYfS836B.prznc.cn
http://yivEouX2.prznc.cn
http://fsMRgm1T.prznc.cn
http://T4EieXQq.prznc.cn
http://YDhryA3M.prznc.cn
http://eEIp4HZI.prznc.cn
http://PCCHuL7S.prznc.cn
http://4pI9Ww0R.prznc.cn
http://Mfr53k3S.prznc.cn
http://wN9mHPod.prznc.cn
http://8Z0TmyKU.prznc.cn
http://GGfgARU6.prznc.cn
http://RlDzf9g8.prznc.cn
http://sKPzJCCq.prznc.cn
http://cbPhuMwG.prznc.cn
http://2WZ7SgSO.prznc.cn
http://bl052xmg.prznc.cn
http://UsgslP55.prznc.cn
http://nb30Ln1F.prznc.cn
http://JqRxmKaL.prznc.cn
http://iz8IxX1q.prznc.cn
http://WGNw2wyd.prznc.cn
http://yUlaqfEv.prznc.cn
http://PSesBSWw.prznc.cn
http://www.dtcms.com/a/385083.html

相关文章:

  • 《深入剖析Kubernetes》02 崭露头角
  • Spring Boot日志
  • 跨域(CORS)和缓存中间件(Redis)深度解析
  • 010SecMain_InitializeDebugAgentPhase2
  • 检索融合方法- Distribution-Based Score Fusion (DBSF)
  • 排序实现java
  • 聊聊测试策略与测试方案
  • 考察软件售后服务,保障线上招标采购管理软件高效运行
  • 云HIS系统源码(HIS+LIS+EMR全套源码)门诊/住院/医保全流程打通
  • 单例模式,加锁
  • CV论文速递 | 13篇前沿论文精选:生成与处理、3D视觉、医学影像等核心方向(09.08-09.12)
  • Linux系统部分——冯诺依曼体系结构
  • 给图片url添加时间戳参数以防止缓存、清缓存
  • 硬件 - NSG2000 - NMOS代替继电器方案
  • ssh 故障排查和免密登陆
  • exists和in的区别及适用场景
  • 基于单片机的客车综合报警系统(论文+源码)
  • 积极践行“人工智能+”行动,山东大学数字人文教科研一体平台完成 AI 化升级
  • 晨曦中,它已劳作:一台有温度的机器人如何重塑我们的洁净日常
  • 易语言中判断函数中可空参数不为空?
  • 2025机器人打磨抛光设备推荐及汽车零件/铸件打磨机器人技术解析
  • ESP32三种主流的开发环境
  • GTPU涉及NR RAN 容器
  • 【数值分析】02-绪论-误差
  • 使用 Playwright 打开 Edge 浏览器的两种方法
  • 刷题日记0915
  • 路由器设置内网端口映射到外网访问详细步骤图解教程,附无公网ip端口映射工具方法
  • AIOps概述:Langfuse、Opik、KeepHQ、LangSmith
  • 使用idea启动一个新的项目的步骤
  • Spring 框架从入门到精通(第三篇)——Spring 整合 MyBatis、声明式事务与注解总结