首页 > 其他分享 >在langchain中使用自定义example selector

在langchain中使用自定义example selector

时间:2023-08-08 14:02:58浏览次数:36  
标签:自定义 self selector examples output input example

简介

在之前的文章中,我们提到了可以在跟大模型交互的时候,给大模型提供一些具体的例子内容,方便大模型从这些内容中获取想要的答案。这种方便的机制在langchain中叫做FewShotPromptTemplate。

如果例子内容少的话,其实无所谓,我们可以把所有的例子都发送给大语言模型进行处理。

但是如果例子太多的话,每次都发送如此多的内容,会让我们的钱包承受不住。毕竟那些第三方的大语言模型是按token收费的。

怎么办呢? 能不能找到一个经济又有效的方法来完成我们的工作呢?

答案就是使用example selector。

使用和自定义example selector

我们回想一下在使用FewShotPromptTemplate的时候,实际上是可以同时传入example_selector和examples。

prompt = FewShotPromptTemplate(
    example_selector=example_selector, 
    example_prompt=example_prompt, 
    suffix="Question: {input}", 
    input_variables=["input"]
)

这里我们使用了一个example_selector,那么什么是example_selector呢?

从名字上看他的主要作用就是从给定的examples中选择需要的examples出来,提供给大模型使用,从而减少会话的token数目。

langchain中提供了这样的example_selector的实现,我们先来看下它的基础类的定义是怎么样的:

class BaseExampleSelector(ABC):
    """Interface for selecting examples to include in prompts."""

    @abstractmethod
    def add_example(self, example: Dict[str, str]) -> Any:
        """Add new example to store for a key."""

    @abstractmethod
    def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
        """Select which examples to use based on the inputs."""

可以看到BaseExampleSelector继承自ABC,并且定义了两个需要实现的抽象方法。

一个方法叫做add_example。目的是向selector中添加一个example。

一个方法叫做select_examples,主要目的就是根据input,从examples中找出要select出来的内容。

那么什么是ABC呢?

ABC当然就是你了解到的ABC,但是他还有一些额外的含义。ABC的全称叫做Abstract Base Class,也叫做抽象基类。主要用于在Python程序中创建抽象基类。

他提供了一些@abstractmethod,@abstarctproperty这些装饰方法,来表明具体类的特征。

所以,如果我们想自定义一个ExampleSelector,只需要继承自BaseExampleSelector,然后实现这两个抽象方法即可。

langchain中的ExampleSelector实现

除了自定义实现之外,langchain已经为我们提供了几个常用的ExampleSelector实现,一起来看看吧。

LengthBasedExampleSelector

LengthBasedExampleSelector是根据example的长度来进行选择的选择器。

我们看下它的具体实现:

    def add_example(self, example: Dict[str, str]) -> None:
        """Add new example to list."""
        self.examples.append(example)
        string_example = self.example_prompt.format(**example)
        self.example_text_lengths.append(self.get_text_length(string_example))

add_example的逻辑是先把example添加到examples这个list中。

然后使用example_prompt对example进行格式化,得到最终的输出。

最后再把最后输出的text长度添加到example_text_lengths数组中。

    def select_examples(self, input_variables: Dict[str, str]) -> List[dict]:
        """Select which examples to use based on the input lengths."""
        inputs = " ".join(input_variables.values())
        remaining_length = self.max_length - self.get_text_length(inputs)
        i = 0
        examples = []
        while remaining_length > 0 and i < len(self.examples):
            new_length = remaining_length - self.example_text_lengths[i]
            if new_length < 0:
                break
            else:
                examples.append(self.examples[i])
                remaining_length = new_length
            i += 1
        return examples

select_examples方法实际上就是用max_length减去输入text的长度,然后再去匹配example_text的长度,匹配一个减去一个,最终得到特定长度的examples。

这个selector的最主要作用就是防止耗尽context window。因为对于大多数大语言模型来说,用户的输入是有长度限制的。

如果超出了输入长度,会产生意想不到的结果。

这个selector使用起来很简单,下面是具体的例子:

examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"},
    {"input": "energetic", "output": "lethargic"},
    {"input": "sunny", "output": "gloomy"},
    {"input": "windy", "output": "calm"},

example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template="Input: {input}\nOutput: {output}",
)
example_selector = LengthBasedExampleSelector(
    examples=examples, 
    example_prompt=example_prompt, 
    max_length=25,
)

SemanticSimilarityExampleSelector和MaxMarginalRelevanceExampleSelector

这两个selector是根据相似度来进行example的查找的。

其中MaxMarginalRelevanceExampleSelector是SemanticSimilarityExampleSelector的字类,他是对SemanticSimilarityExampleSelector进行了一些算法上的优化。所以这里我们把他们两个放在一起介绍。

这两个selector和之前介绍的selector有所不同。因为他们用到了向量数据库。

向量数据库是干什么用的呢?它的主要目的是把输入转换成各种向量然后存储起来。向量数据库可以方便的进行输入相识度的计算。

我们先来看下他们的add_example方法:

    def add_example(self, example: Dict[str, str]) -> str:
        """Add new example to vectorstore."""
        if self.input_keys:
            string_example = " ".join(
                sorted_values({key: example[key] for key in self.input_keys})
            )
        else:
            string_example = " ".join(sorted_values(example))
        ids = self.vectorstore.add_texts([string_example], metadatas=[example])
        return ids[0]

这个方法先把example的key加入到input_keys中,然后进行排序。最后通过调用vectorstore的add_texts,把key和value加入到向量数据库中。

这两个selector的add_example都是一样的。只有select_examples的方法不同。

其中SemanticSimilarityExampleSelector调用了vectorstore的similarity_search方法来实现相似度的搜索。

而MaxMarginalRelevanceExampleSelector则是调用vectorstore的max_marginal_relevance_search方法来实现搜索的。

两者的搜索算法不太一样。

因为使用了向量数据库,所以他们的调用方法和其他的也不太一样:

examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"},
    {"input": "energetic", "output": "lethargic"},
    {"input": "sunny", "output": "gloomy"},
    {"input": "windy", "output": "calm"},
]

example_selector = SemanticSimilarityExampleSelector.from_examples(
    examples, 
    # 使用的ebeddings
    OpenAIEmbeddings(), 
    # 向量数据库
    Chroma, 
    # 要返回的数目
    k=1
)

NGramOverlapExampleSelector

最后一个要介绍的是NGramOverlapExampleSelector。这个selector使用的是ngram 重叠矩阵来选择相似的输入。

具体的实现算法和原理这里就不介绍了。大家有兴趣的可以自行探索。

这个selector也不需要使用向量数据库。

使用起来是这样的:

example_selector = NGramOverlapExampleSelector(
    examples=examples,
    example_prompt=example_prompt,
    threshold=-1.0,
)

这里有个不太一样的参数叫做threshold。

对于负阈值:Selector按ngram重叠分数对示例进行排序,不排除任何示例。

对于大于1.0的阈值:选择器排除所有示例,并返回一个空列表。

对于等于0.0的阈值:选择器根据ngram重叠分数对示例进行排序,并且排除与输入没有ngram重叠的那些。

总结

有了这些selector我们就可以在提供的examples中进行特定的选择,然后再把选择的结果输入给大语言模型。

从而有效的减少token的浪费。

标签:自定义,self,selector,examples,output,input,example
From: https://www.cnblogs.com/flydean/p/17614001.html

相关文章

  • 在langchain中使用自定义example selector
    简介在之前的文章中,我们提到了可以在跟大模型交互的时候,给大模型提供一些具体的例子内容,方便大模型从这些内容中获取想要的答案。这种方便的机制在langchain中叫做FewShotPromptTemplate。如果例子内容少的话,其实无所谓,我们可以把所有的例子都发送给大语言模型进行处理。但是如......
  • 自定义类给窗体的控件赋值
    前面我们有说到 多线程给窗体的控件赋值 详见遇到问题-UI界面无响应,多线程解决UI界面无响应问题现在有一种新的情况,我想在另一个类中给窗体的控件赋值(这在记录程序执行进度的时候常用到),我们仍可以用委托 首先在自定义类的外面 声明一个委托模块 publicdelegate......
  • 基于Qt编写超精美自定义控件
    一、前言无论是哪一门开发框架,如果涉及到UI这块,肯定需要用到自定义控件,越复杂功能越多的项目,自定义控件的数量就越多,最开始的时候可能每个自定义控件都针对特定的应用场景,甚至里面带了特定的场景的一些设置和处理,随着项目数量的增多,有些控件又专门提取出来共性,做成了通用的自定义控......
  • You are using the runtime-only build of Vue where the template compiler is not a
    使用vue-cli搭建的项目,页面自定义带template内容的组件无法渲染,控制台报错,页面不展示组件内容,代码如下:<template><divclass="hello">my-component:<my-component></my-component></div></template><script>importVuefrom"vue"......
  • php多维数组自定义排序 uasort()
    对数组进行排序PHP有一些用来排序数组的函数,这个文档会把它们列出来。主要区别有:有些函数基于array的键来排序,而其他的基于值来排序的:$array['key']='value';。排序之后键和值之间的关联关系是否能够保持,是指排序之后数组的键可能会被重置为数字型的(0,1,2...)。排......
  • 支持多数据源联合查询的SQL运行引擎sycnany-SQL添加使用自定义函数
    在微服务和云原生愈发流行的今天,数据的分布也愈发脱离单库单机而更加复杂,使用的数据库类型也会更多,但业务的复杂依然会带来了大量的数据查询和导出需求,而很多时候我们很难为数据量的大部分系统创建完整的BI数仓系统,这时候你是不是觉得为这些需求查询和导出数据就会是一个十分困难且......
  • 如何自定义书写中间件?
    一、什么是中间件?中间件是一种装配到应用管道以处理请求和响应的软件。是介于request与response处理过程之间的一个插件(一道处理过程),相对比较轻量级,并且在全局上会影响到request对象和response对象的属性。因为改变的是全局,所以需要谨慎实用,用不好会影响到性能。每个组件:1、选......
  • 【我和openGauss的故事】带有out参数的存储过程及自定义函数的重载测试
    【我和openGauss的故事】带有out参数的存储过程及自定义函数的重载测试DarkAthenaopenGauss2023-08-0418:01发表于四川背景先说下数据库里说的函数重载是什么。比如我们知道数据库可能会有同名的函数,但是函数的参数声明不一样selectto_char(sysdate,'yyyymmdd')fromdual;se......
  • Zig 自定义字符类型
    Zig自定义字符类型在Zig​中整数型有无符号Unsigned​有符号Signed​在默认情况下生命的整数型变量都是有符号的类型。长度有符号类型无符号类型8位​i8​​u8​16位​i16​​u16​32位​i32​​u32​64位​i64​​u64​128位​i128......
  • FreeSWITCH添加自定义endpoint之媒体交互
    操作系统:CentOS7.6_x64FreeSWITCH版本:1.10.9 之前写过FreeSWITCH添加自定义endpoint的文章:https://www.cnblogs.com/MikeZhang/p/fsAddEndpoint20230528.html今天记录下endpoint媒体交互的过程并提供示例代码及相关资源下载,本文涉及示例代码和资源可从如下渠道获取:关......