构建LangChain应用程序的示例代码:27、FLARE:前瞻性主动检索增强生成技术实现与应用的示例

FLARE:前瞻性主动检索增强生成

这个示例是前瞻性主动检索增强生成(FLARE)的实现。

请查看原始仓库

基本思想是:

  • 开始回答问题
  • 如果开始生成模型不确定的标记,查找相关文档
  • 使用这些文档继续生成
  • 重复直到完成

在查找相关文档的方式上有很多有趣的细节。
基本上,模型不确定的标记会被突出显示,然后调用一个大型语言模型(LLM)生成一个会导致该答案的问题。例如,如果生成的文本是“Joe Biden went to Harvard”,模型不确定的标记是“Harvard”,那么一个生成的好问题可能是“Joe Biden 上的哪所大学?”。然后,这个生成的问题用于检索步骤来获取相关文档。

为了设置这个链,我们需要三件事:

  • 一个用于生成答案的 LLM
  • 一个用于生成用于检索的假设问题的 LLM
  • 一个用于查找答案的检索器

我们用来生成答案的 LLM 需要返回 logprobs,这样我们才能识别不确定的标记。因此,我们强烈推荐你使用 OpenAI 包装器(注意:不是 ChatOpenAI 包装器,因为它不返回 logprobs)。

我们用来生成用于检索的假设问题的 LLM 可以是任何东西。在这个示例中,我们将使用 ChatOpenAI,因为它快速且便宜。

检索器可以是任何东西。在这个示例中,我们将使用 SERPER 搜索引擎,因为它便宜。

其他需要理解的重要参数:

  • max_generation_len: 在停止检查是否有任何不确定之前生成的最大标记数
  • min_prob: 生成概率低于此的任何标记将被视为不确定

导入

import os

# 设置 SERPER 和 OPENAI 的 API 密钥
os.environ["SERPER_API_KEY"] = ""
os.environ["OPENAI_API_KEY"] = ""
from typing import Any, List

from langchain.callbacks.manager import (
    AsyncCallbackManagerForRetrieverRun,
    CallbackManagerForRetrieverRun,
)
from langchain_community.utilities import GoogleSerperAPIWrapper
from langchain_core.documents import Document
from langchain_core.retrievers import BaseRetriever
from langchain_openai import ChatOpenAI, OpenAI

检索器

class SerperSearchRetriever(BaseRetriever):
    # Google Serper API 包装器
    search: GoogleSerperAPIWrapper = None

# 创建检索器实例
retriever = SerperSearchRetriever(search=GoogleSerperAPIWrapper())

FLARE 链

# 我们这样设置,以便可以看到确切发生了什么
from langchain.globals import set_verbose

# 开启详细模式
set_verbose(True)
from langchain.chains import FlareChain

# 创建 FLARE 链实例
flare = FlareChain.from_llm(
    # 使用 ChatOpenAI 生成答案
    ChatOpenAI(temperature=0),
    # 设置检索器
    retriever=retriever,
    # 设置最大生成长度
    max_generation_len=164,
    # 设置最小概率值
    min_prob=0.3,
)
# 运行 FLARE 链,回答问题
query = "explain in great detail the difference between the langchain framework and baby agi"
flare.run(query)

’ LangChain is a framework for developing applications powered by language models. It provides a standard interface for chains, lots of integrations with other tools, and end-to-end chains for common applications. On the other hand, Baby AGI is an AI system that is exploring and demonstrating the potential of large language models, such as GPT, and how it can autonomously perform tasks. Baby AGI has the ability to complete tasks, generate new tasks based on previous results, and prioritize tasks in real-time. ’

# 使用 OpenAI 直接回答问题
llm = OpenAI()
llm.invoke(query)

‘\n\nThe Langchain framework and Baby AGI are both artificial intelligence (AI) frameworks that are used to create intelligent agents. The Langchain framework is a supervised learning system that is based on the concept of “language chains”. It uses a set of rules to map natural language inputs to specific outputs. It is a general-purpose AI framework and can be used to build applications such as natural language processing (NLP), chatbots, and more.\n\nBaby AGI, on the other hand, is an unsupervised learning system that uses neural networks and reinforcement learning to learn from its environment. It is used to create intelligent agents that can adapt to changing environments. It is a more advanced AI system and can be used to build more complex applications such as game playing, robotic vision, and more.\n\nThe main difference between the two is that the Langchain framework uses supervised learning while Baby AGI uses unsupervised learning. The Langchain framework is a general-purpose AI framework that can be used for various applications, while Baby AGI is a more advanced AI system that can be used to create more complex applications.’

# 运行 FLARE 链,回答另一个问题
query = "how are the origin stories of langchain and bitcoin similar or different?"
flare.run(query)

’ The origin stories of LangChain and Bitcoin are quite different. Bitcoin was created in 2009 by an unknown person using the alias Satoshi Nakamoto. LangChain was created in late October 2022 by Harrison Chase. Bitcoin is a decentralized cryptocurrency, while LangChain is a framework built around LLMs. ’


总结与扩展知识

FLARE 是一种结合了前瞻性主动检索和生成的技术,它通过在生成过程中检索相关信息来增强语言模型的生成能力。FLARE 的核心思想是在生成过程中,当模型对某些标记不确定时,通过检索相关文档来辅助生成更准确的答案。

在这个实现中,我们使用了以下技术和组件:

  1. 大型语言模型(LLM):用于生成答案和假设问题。这里推荐使用 OpenAI 的 API,因为它可以返回 logprobs,帮助我们识别模型不确定的标记。

  2. 检索器(Retriever):用于查找与生成问题相关的文档。在这个示例中,使用了 SERPER 搜索引擎,因为它成本较低。

  3. FlareChain:这是 LangChain 框架中的一个组件,用于构建和运行 FLARE 链。

  4. GoogleSerperAPIWrapper:一个包装器,用于简化与 Google SERPER 搜索引擎的交互。

  5. 环境变量:用于存储 API 密钥,保护敏感信息。

  6. LangChain 框架:一个用于构建和运行复杂语言模型链的框架,提供了一系列的工具和接口。

  7. 参数调整max_generation_lenmin_prob 是两个重要的参数,用于控制生成过程和不确定性标记的识别。

通过这些技术和组件的结合,FLARE 能够提供一种更加智能和准确的文本生成和检索方法,适用于需要高度定制化和准确性的问答系统。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hugo_Hoo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值