正在为人工智能或全栈开发而苦恼?我们的专家将为您提供指导:量身定制的建议、技术整合等。联系我们 [email protected].

RAG:利用语义搜索和生成式人工智能对领域知识进行问题解答

根据领域知识(如内部文档、合同、书籍等)回答问题具有挑战性。在本文中,我们探讨了一种名为 "检索增强生成"(RAG)的先进技术,它将语义搜索和文本生成与 ChatDolphin、LLaMA、ChatGPT、GPT-4 等模型相结合,从而以极高的准确率实现这一目标。

问题解答

回答领域知识问题的挑战

根据领域知识进行问题解答需要首先向人工智能模型发送一些上下文,然后再提出相关问题。

例如,您可以发送以下上下文:

All NLP Cloud plans can be stopped anytime. You only pay for the time you used the service. In case of a downgrade, you will get a discount on your next invoice.

现在,你可能想问下面这个问题:

When can plans be stopped?

人工智能会这样回答

Anytime

更多详情,请参阅我们的问题解答文档 这里.

这种方法的问题在于,上下文的大小(即输入文本的大小)是有限的。因此,您无法将整个领域的知识作为上下文发送。

假设您想构建一个支持聊天机器人,让它了解产品文档的所有内容,这样最终用户就可以向聊天机器人提出任何与产品相关的问题,而无需联系真正的支持代理。很可能,您的文档将由数百或数千字,甚至数百万字组成......

让我们来探讨如何克服这一限制,在超大型文档中执行问题解答。

语义搜索 VS 生成式人工智能

说到问题解答,可以使用两种技术。文本生成(生成式人工智能)和语义搜索。

第一种是文本生成,基本上就是我刚才介绍的内容。它通常采用先进的文本生成模型,如 ChatDolphin、LLaMA、ChatGPT、GPT-4 等。它能理解人类的问题,也能像人类一样做出回应。然而,它并不适用于大型文档。使用您的领域知识对人工智能生成模型进行微调效果不会很好,因为微调并不是为模型添加知识的好技术。

语义搜索基本上就是以与 Google 相同的方式搜索文档,但要基于您自己的领域知识。

为此,您需要将内部文档转换成向量(也称为 "嵌入")。然后,您还应将您的问题转换为向量,然后执行向量搜索(也称为 "语义相似性"),以检索与您的问题最接近的领域知识部分。

第一种解决方案是将嵌入数据存储在专门的矢量数据库(如 PG Vector)中。另一种解决方案是用自己的领域知识编码自己的语义搜索模型,并将其部署在 GPU 上(这是我们在 NLP Cloud 上提出的解决方案,因为它能提供最佳响应时间)。然后,一旦您的矢量数据库准备就绪,或者一旦您的模型创建完成,您就可以用自然语言提问,您的人工智能模型就会返回最能回答您问题的领域知识摘要。

语义搜索通常非常快速,而且相对便宜。它也比文本生成微调策略更可靠,因此你不会遇到任何人工智能幻觉问题。但它无法正确 "回答 "问题。它只会返回一段包含问题答案的文本。然后,用户就需要阅读整篇文本,以找到问题的答案。

更多详情,请参阅我们的语义搜索文档 这里.

好消息是,我们可以将语义搜索和生成式人工智能结合起来,以获得先进的结果!

混合语义搜索和生成式人工智能的问题解答

为了回答有关领域知识的问题,我们在 NLP Cloud 上采用的策略如下:首先通过语义搜索提出请求,以检索最能回答问题的资源,然后根据这些资源像人类一样使用文本生成功能来回答问题。

假设我们是惠普打印机的经销商,我们希望在网站上回答客户的问题。

首先,我们需要计算嵌入并将其存储到向量数据库中,或者创建我们自己的语义搜索模型。这里只包含 3 个示例,但在实际生活中,在 NLP Cloud 上使用语义搜索时,可以包含多达 100 万个示例。我们只需创建一个 CSV 文件并将以下内容放入其中:

HP® LaserJets have unmatched printing speed, performance and reliability that you can trust. Enjoy Low Prices and Free Shipping when you buy now online.
Every HP printer comes with at least a one-year HP commercial warranty (or HP Limited Warranty). Some models automatically benefit from a three-year warranty, which is the case of the HP Color LaserJet Plus, the HP Color LaserJet Pro, and the HP Color LaserJet Expert.
HP LaserJet ; Lowest cost per page on mono laser printing. · $319.99 ; Wireless options available. · $109.00 ; Essential management features. · $209.00.

然后,我们将 CSV 数据集上传到 NLP Cloud,并点击 "创建模型"。过一会儿,包含我们自己领域知识的语义搜索模型就会准备就绪,我们将收到一个私有 API URL 以使用它。

让我们使用 NLP Cloud Python 客户端向我们的全新模型提问:

import nlpcloud

# We use a fake model name and a fake API key for illustration reasons.
client = nlpcloud.Client("custom-model/5d8e6s8w5", "poigre5754gaefdsf5486gdsa56", gpu=True)
client.semantic_search("How long is the warranty on the HP Color LaserJet Pro?")

该模型可在短时间内快速返回以下信息:

{
"search_results": [
    {
        "score": 0.99,
        "text": "Every HP printer comes with at least a one-year HP commercial warranty (or HP Limited Warranty). Some models automatically benefit from a three-year warranty, which is the case of the HP Color LaserJet Plus, the HP Color LaserJet Pro, and the HP Color LaserJet Expert."
    },
    {
        "score": 0.74,
        "text": "All consumer PCs and printers come with a standard one-year warranty. Care packs provide an enhanced level of support and/or an extended period of coverage for your HP hardware. All commercial PCs and printers come with either a one-year or three-year warranty."
    },
    {
        "score": 0.68,
        "text": "In-warranty plan · Available in 2-, 3-, or 4-year extension plans · Includes remote problem diagnosis support and Next Business Day Exchange Service."
    },
    ]
}

现在,我们检索得分最高的答案(我们也完全可以检索多个答案): "Every HP printer comes with at least a one-year HP commercial warranty (or HP Limited Warranty). Some models automatically benefit from a three-year warranty, which is the case of the HP Color LaserJet Plus, the HP Color LaserJet Pro, and the HP Color LaserJet Expert."

这个回答是正确的,但对用户来说并不友好,因为用户需要阅读很长一段文字才能得到答案。因此,现在我们使用 ChatDolphin 模型,再次向我们的问题解答端点提出同样的问题。我们将使用语义搜索响应作为上下文:

import nlpcloud

client = nlpcloud.Client("chatdolphin", "poigre5754gaefdsf5486gdsa56", gpu=True)
client.question(
    """How long is the warranty on the HP Color LaserJet Pro?""",
    context="""Every HP printer comes with at least a one-year HP commercial warranty (or HP Limited Warranty). Some models automatically benefit from a three-year warranty, which is the case of the HP Color LaserJet Plus, the HP Color LaserJet Pro, and the HP Color LaserJet Expert."""
)

返回的答案如下

{
    "answer": "The warranty lasts for three years."
}

很不错吧?

结论

尽管最近在生成式人工智能模型(如 ChatDolphin、LLaMA、ChatGPT、GPT-4 等)方面取得了进展,但由于请求数量有限,无法将这些优秀的模型用于特定领域知识的问题解答。遗憾的是,对这些模型进行微调并不能很好地用于此类用例...

一个好的策略是实施 RAG 系统。首先通过将文档转换为嵌入并将其存储在向量数据库中,或从文档中创建自己的语义搜索模型来回答问题,然后使用基于生成式人工智能的常规问题解答模型,以便返回对初始问题的人类答案。

如果您想实施这一策略,请毫不犹豫地在 NLP Cloud 上创建自己的语义搜索模型: 请参见此处的相关文档!

Mark
NLP Cloud 应用工程师