在本文中,我们将深入探讨Rerank模型在Retrieval-Augmented Generation(RAG)中的应用,以及如何使用HuggingFace的Text Embedding Inference(TEI)工具部署Rerank模型,并在LlamaIndex的RAG流程中集成Rerank功能。

1. Rerank模型介绍

Rerank是RAG中的一个关键组件,它的作用是对检索到的文档进行重新排序,确保与查询问题最相关的文档排在前面。这有助于提高LLM生成回答的准确性和质量。

RAG概述

RAG是一种结合了检索和生成的语言模型技术。当提出问题时,RAG首先检索相关信息,然后基于这些信息生成回答。

2. Rerank模型部署

1) 选择Rerank模型

目前可用的Rerank模型包括Cohere的在线模型和智源的bge-reranker-basebge-reranker-large等开源模型。本文将使用bge-reranker-large进行部署演示。

2)使用TEI部署Rerank模型

TEI是HuggingFace推出的一个工具,用于部署文本嵌入和序列分类模型。它支持Embedding模型的部署,同时也支持Rerank模型。

安装TEI

  • 安装Rust:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  • 1.
  • 克隆TEI仓库并安装:
git clone https://github.com/huggingface/text-embeddings-inference.git
cd text-embeddings-inference
cargo install --path router -F candle -F metal
  • 1.
  • 2.
  • 3.

启动TEI服务

使用以下命令启动TEI服务,并部署Rerank模型:

text-embeddings-router --model-id BAAI/bge-reranker-large --revision refs/pr/5 --port 8080
  • 1.
3)验证Rerank接口

使用Curl工具调用Rerank接口进行验证:

curl -X 'POST' \
  'http://localhost:8080/rerank' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "query": "What is Deep Learning?",
  "texts": [
    "Deep Learning is ...",
    "hello"
  ]
}'
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

3. 在LlamaIndex中使用Rerank功能

1)LlamaIndex RAG流程

LlamaIndex使用RAG流程检索文档并生成回答。我们可以通过自定义NodePostProcessor组件来集成Rerank功能。

2)自定义NodePostProcessor

创建一个CustomRerank类,继承自BaseNodePostprocessor,并实现_postprocess_nodes方法,调用Rerank接口进行文档重新排序。

3)集成CustomRerank

在LlamaIndex的as_query_engine方法中,通过node_postprocessors参数传递CustomRerank实例。

4)结果验证

运行LlamaIndex查询,观察是否只返回最相关的文档。

本文介绍了Rerank模型在RAG中的重要性和部署方法,以及如何在LlamaIndex中集成Rerank功能。通过使用TEI工具部署Rerank模型,我们可以显著提高RAG的效果,生成更准确、更高质量的回答。