Chuyển đến nội dung chính

RankGPT với vai trò Tác nhân Xếp hạng lại cho RAG (Hướng dẫn)

RankGPT là một phương pháp sử dụng các LLM như ChatGPT để xếp hạng lại tài liệu trong hệ thống RAG, nâng cao chất lượng đầu ra bằng cách ưu tiên thông tin liên quan nhất.
Đã cập nhật 5 thg 10, 2026  · 8 phút đọc

Khám phá cùng AI

ChatGPTClaudePerplexity

Retrieval augmented generation (RAG) là một kỹ thuật giúp mô hình ngôn ngữ lớn (LLM) trở nên thông minh và chính xác hơn bằng cách cho phép chúng sử dụng thông tin bên ngoài khi tạo văn bản.

Tuy nhiên, thách thức lớn là chọn đúng tài liệu hoặc đoạn văn từ một tập dữ liệu khổng lồ.

RankGPT giải quyết vấn đề này bằng cách cải thiện bước xếp hạng lại trong các pipeline RAG. Nó tận dụng khả năng hiểu sâu của LLM để đánh giá và (tái) xếp hạng thông tin nào liên quan nhất.

Trong bài viết này, chúng tôi sẽ giới thiệu RankGPT và minh họa cách bạn có thể tích hợp nó vào ứng dụng AI dựa trên RAG của bạn.

Tìm hiểu về Retrieval Augmented Generation (RAG)

Retrieval augmented generation (RAG) là một phương pháp kết hợp LLM với hệ thống truy xuất thông tin. Điều này có nghĩa là khi một LLM được yêu cầu tạo văn bản, nó có thể lấy thông tin liên quan từ các nguồn bên ngoài, giúp phản hồi chính xác và đầy đủ hơn.

RAG gồm hai thành phần chính — bộ truy xuất (retriever) và bộ tạo (generator) — cùng một thành phần tùy chọn là bộ xếp hạng lại (reranker):

  1. Retriever — Nhiệm vụ của retriever là tìm các tài liệu hoặc đoạn văn bản liên quan từ một tập tài liệu lớn dựa trên truy vấn của người dùng. Nó sử dụng các thuật toán như BM25 để xếp hạng tài liệu theo mức độ liên quan.
  2. Reranker (tùy chọn) — Reranker sẽ nhận tập tài liệu truy xuất ban đầu và sắp xếp lại để đảm bảo những tài liệu liên quan nhất nằm ở trên cùng. Điều này giúp lọc bỏ thông tin kém hữu ích và tập trung vào phần quan trọng.
  3. Generator — Generator là LLM sử dụng các tài liệu đã truy xuất để tạo đầu ra cuối cùng. Việc truy cập dữ liệu ngoài liên quan có thể tạo ra phản hồi chính xác hơn.

Vai trò và Lợi ích của RankGPT trong RAG

RankGPT sử dụng LLM để đánh giá mức độ liên quan của tài liệu hoặc đoạn văn được truy xuất, đảm bảo những nội dung quan trọng nhất ở vị trí đầu. Với RankGPT, bộ tạo trong pipeline RAG nhận được đầu vào chất lượng cao hơn, giúp phản hồi chính xác hơn.

Cải thiện mức độ liên quan và hiệu năng

RankGPT vượt ra ngoài việc khớp từ khóa đơn thuần bằng cách hiểu sâu ý nghĩa và ngữ cảnh của truy vấn và tài liệu. Nhờ đó, nó cung cấp thông tin chính xác hơn cho LLM, xác định nội dung liên quan nhất dựa trên ý nghĩa thực sự chứ không chỉ dựa vào từ khóa.

Khi sử dụng GPT-4 với kỹ thuật tạo hoán vị hướng dẫn zero-shot, RankGPT vượt trội hơn các hệ thống học có giám sát hàng đầu trên nhiều bộ đánh giá như TREC, BEIR và Mr.TyDi.

Chưng cất hiệu quả và tiết kiệm chi phí

RankGPT sử dụng chưng cất hoán vị để chuyển năng lực xếp hạng của các mô hình lớn như GPT-4 sang các mô hình nhỏ hơn, chuyên biệt hơn.

Những mô hình nhỏ này vẫn duy trì hiệu năng cao trong khi hiệu quả hơn nhiều. Ví dụ, một mô hình chưng cất 440M vượt trội hơn mô hình có giám sát 3B trên bộ BEIR, giảm đáng kể chi phí tính toán đồng thời đạt kết quả tốt hơn.

Xử lý thông tin mới và chưa biết

RankGPT bao gồm bộ kiểm thử NovelEval để đảm bảo độ vững và giải quyết lo ngại về nhiễm bẩn dữ liệu. Bộ này đánh giá khả năng của mô hình trong việc xếp hạng các đoạn văn dựa trên thông tin mới và chưa được biết đến.

GPT-4 đạt hiệu năng dẫn đầu trên bài kiểm thử này, cho thấy khả năng xử lý hiệu quả các truy vấn mới và chưa từng gặp.

Hiệu năng Benchmark của RankGPT

RankGPT (gpt-4) vượt trội hơn tất cả các mô hình khác trên TREC và BEIR, với điểm nDCG@10 trung bình 53,68 như thể hiện trong bảng dưới đây. Nó đạt kết quả cao nhất trên các tập dữ liệu BEIR, vượt qua các mô hình có giám sát mạnh như monoT5 (3B) và Cohere Rerank-v2. Ngay cả với gpt-3.5-turbo, RankGPT vẫn ghi điểm cạnh tranh, chứng minh đây là một bộ xếp hạng lại rất hiệu quả.

Hiệu năng trên TREC và BEIR

Nguồn: Weiwei Sun et al., 2023

RankGPT (gpt-4) cũng thể hiện mạnh mẽ trên các tập Mr.TyDi, dẫn đầu với điểm nDCG@10 trung bình 62,93, vượt qua cả BM25 và mmarcoCE. Nó liên tục vượt BM25 và thậm chí vượt mmarcoCE ở nhiều ngôn ngữ, đặc biệt là tiếng Indonesia và tiếng Swahili.

Nhìn chung, RankGPT đạt điểm cao nhất ở nhiều ngôn ngữ như tiếng Bengali, tiếng Indonesia và tiếng Nhật, chỉ có một vài trường hợp hơi kém hơn mmarcoCE.

Hiệu năng trên Mr.TyDi

Nguồn: Weiwei Sun et al., 2023

Cuối cùng, RankGPT được kiểm thử trên bộ NovelEval, đo lường khả năng xếp hạng các đoạn văn dựa trên thông tin mới và không quen thuộc. RankGPT (gpt-4) đạt điểm cao nhất ở tất cả chỉ số đánh giá (nDCG@1, nDCG@5 và nDCG@10), đặc biệt là điểm nDCG@10 đạt 90,45. Nó vượt qua các mô hình mạnh khác như monoT5 (3B) và monoBERT (340M), cho thấy năng lực xếp hạng lại vượt trội.

Hiệu năng trên NovelEval

Nguồn: Weiwei Sun et al., 2023

Trên tất cả kết quả benchmark, RankGPT (gpt-4) liên tục vượt các phương pháp khác, dù là có giám sát hay không giám sát, chứng minh năng lực vượt trội trong xếp hạng lại.

Triển khai RankGPT trong Pipeline RAG

Dưới đây là cách tích hợp RankGPT vào một pipeline RAG.

Bước 1: Clone kho RankGPT

Đầu tiên, bạn cần clone kho RankGPT. Chạy lệnh sau trong terminal:

git clone https://github.com/sunnweiwei/RankGPT

Bước 2: Thiết lập môi trường

Đi đến thư mục RankGPT và cài đặt các gói cần thiết. Bạn có thể tạo một môi trường ảo và cài đặt gói bằng requirements.txt được cung cấp:

pip install -r requirements.txt

Bước 3: Triển khai RankGPT

Ở đây, chúng ta dùng truy vấn ví dụ đơn giản và các tài liệu truy xuất do kho RankGPT gốc cung cấp.

item = {
    'query': 'How much impact do masks have on preventing the spread of the COVID-19?',
    'hits': [
        {'content': 'Title: Universal Masking is Urgent in the COVID-19 Pandemic: SEIR and Agent Based Models, Empirical Validation, Policy Recommendations Content: We present two models for the COVID-19 pandemic predicting the impact of universal face mask wearing upon the spread of the SARS-CoV-2 virus--one employing a stochastic dynamic network based compartmental SEIR (susceptible-exposed-infectious-recovered) approach, and the other employing individual ABM (agent-based modelling) Monte Carlo simulation--indicating (1) significant impact under (near) universal masking when at least 80% of a population is wearing masks, versus minimal impact when only 50% or less of the population is wearing masks, and (2) significant impact when universal masking is adopted early, by Day 50 of a regional outbreak, versus minimal impact when universal masking is adopted late. These effects hold even at the lower filtering rates of homemade masks. To validate these theoretical models, we compare their predictions against a new empirical data set we have collected'},
        {'content': 'Title: Masking the general population might attenuate COVID-19 outbreaks Content: The effect of masking the general population on a COVID-19 epidemic is estimated by computer simulation using two separate state-of-the-art web-based softwares, one of them calibrated for the SARS-CoV-2 virus. The questions addressed are these: 1. Can mask use by the general population limit the spread of SARS-CoV-2 in a country? 2. What types of masks exist, and how elaborate must a mask be to be effective against COVID-19? 3. Does the mask have to be applied early in an epidemic? 4. A brief general discussion of masks and some possible future research questions regarding masks and SARS-CoV-2. Results are as follows: (1) The results indicate that any type of mask, even simple home-made ones, may be effective. Masks use seems to have an effect in lowering new patients even the protective effect of each mask (here dubbed"one-mask protection") is'},
        {'content': 'Title: To mask or not to mask: Modeling the potential for face mask use by the general public to curtail the COVID-19 pandemic Content: Face mask use by the general public for limiting the spread of the COVID-19 pandemic is controversial, though increasingly recommended, and the potential of this intervention is not well understood. We develop a compartmental model for assessing the community-wide impact of mask use by the general, asymptomatic public, a portion of which may be asymptomatically infectious. Model simulations, using data relevant to COVID-19 dynamics in the US states of New York and Washington, suggest that broad adoption of even relatively ineffective face masks may meaningfully reduce community transmission of COVID-19 and decrease peak hospitalizations and deaths. Moreover, mask use decreases the effective transmission rate in nearly linear proportion to the product of mask effectiveness (as a fraction of potentially infectious contacts blocked) and coverage rate (as'}
    ]
}

Bạn có thể dùng pipeline hoán vị có sẵn để dễ dàng xếp hạng lại các tài liệu truy xuất với RankGPT.

from rank_gpt import permutation_pipeline
new_item = permutation_pipeline(
    item,
    rank_start=0,
    rank_end=3,
    model_name='gpt-3.5-turbo',
    api_key='Your OPENAI Key!'
)
print(new_item)

Điều này sẽ cho ra thứ tự tài liệu mới như sau:

{
    'query': 'How much impact do masks have on preventing the spread of the COVID-19?',
    'hits': [
        {'content': 'Title: Universal Masking is Urgent in the COVID-19 Pandemic: SEIR and Agent Based Models, Empirical Validation, Policy Recommendations Content: We present two models for the COVID-19 pandemic predicting the impact of universal face mask wearing upon the spread of the SARS-CoV-2 virus--one employing a stochastic dynamic network based compartmental SEIR (susceptible-exposed-infectious-recovered) approach, and the other employing individual ABM (agent-based modelling) Monte Carlo simulation--indicating (1) significant impact under (near) universal masking when at least 80% of a population is wearing masks, versus minimal impact when only 50% or less of the population is wearing masks, and (2) significant impact when universal masking is adopted early, by Day 50 of a regional outbreak, versus minimal impact when universal masking is adopted late. These effects hold even at the lower filtering rates of homemade masks. To validate these theoretical models, we compare their predictions against a new empirical data set we have collected'},
        {'content': 'Title: To mask or not to mask: Modeling the potential for face mask use by the general public to curtail the COVID-19 pandemic Content: Face mask use by the general public for limiting the spread of the COVID-19 pandemic is controversial, though increasingly recommended, and the potential of this intervention is not well understood. We develop a compartmental model for assessing the community-wide impact of mask use by the general, asymptomatic public, a portion of which may be asymptomatically infectious. Model simulations, using data relevant to COVID-19 dynamics in the US states of New York and Washington, suggest that broad adoption of even relatively ineffective face masks may meaningfully reduce community transmission of COVID-19 and decrease peak hospitalizations and deaths. Moreover, mask use decreases the effective transmission rate in nearly linear proportion to the product of mask effectiveness (as a fraction of potentially infectious contacts blocked) and coverage rate (as'},
        {'content': 'Title: Masking the general population might attenuate COVID-19 outbreaks Content: The effect of masking the general population on a COVID-19 epidemic is estimated by computer simulation using two separate state-of-the-art web-based softwares, one of them calibrated for the SARS-CoV-2 virus. The questions addressed are these: 1. Can mask use by the general population limit the spread of SARS-CoV-2 in a country? 2. What types of masks exist, and how elaborate must a mask be to be effective against COVID-19? 3. Does the mask have to be applied early in an epidemic? 4. A brief general discussion of masks and some possible future research questions regarding masks and SARS-CoV-2. Results are as follows: (1) The results indicate that any type of mask, even simple home-made ones, may be effective. Masks use seems to have an effect in lowering new patients even the protective effect of each mask (here dubbed"one-mask protection") is'}
    ]
}

Tạo Hoán vị Hướng dẫn theo Từng Bước

Để triển khai pipeline hoán vị theo từng bước, bạn có thể tương tác trực tiếp với RankGPT để tạo và xử lý hướng dẫn hoán vị như sau:

from rank_gpt import (
    create_permutation_instruction,
    run_llm,
    receive_permutation
)
# Create permutation generation instruction
messages = create_permutation_instruction(
    item=item,
    rank_start=0,
    rank_end=3,
    model_name='gpt-3.5-turbo'
)
[{'role': 'system',
  'content': 'You are RankGPT, an intelligent assistant that can rank passages based on their relevancy to the query.'},
 {'role': 'user',
  'content': 'I will provide you with 3 passages, each indicated by number identifier []. \\nRank the passages based on their relevance to query: How much impact do masks have on preventing the spread of the COVID-19?.'},
 {'role': 'assistant', 'content': 'Okay, please provide the passages.'},
 {'role': 'user',
  'content': '[1] Title: Universal Masking is Urgent in the COVID-19 Pandemic: SEIR and Agent Based Models, Empirical Validation, Policy Recommendations Content: We present two models for the COVID-19 pandemic predicting the impact of universal face mask wearing upon the spread of the SARS-CoV-2 virus--one employing a stochastic dynamic network based compartmental SEIR (susceptible-exposed-infectious-recovered) approach, and the other employing individual ABM (agent-based modelling) Monte Carlo simulation--indicating (1) significant impact under (near) universal masking when at least 80% of a population is wearing masks, versus minimal impact when only 50% or less of the population is wearing masks, and (2) significant impact when universal masking is adopted early, by Day 50 of a regional outbreak, versus minimal impact when universal masking is adopted late. These effects hold even at the lower filtering rates of homemade masks. To validate these theoretical models, we compare their predictions against a new empirical data set we have collected'},
 {'role': 'assistant', 'content': 'Received passage [1].'},
 {'role': 'user',
  'content': '[2] Title: Masking the general population might attenuate COVID-19 outbreaks Content: The effect of masking the general population on a COVID-19 epidemic is estimated by computer simulation using two separate state-of-the-art web-based softwares, one of them calibrated for the SARS-CoV-2 virus. The questions addressed are these: 1. Can mask use by the general population limit the spread of SARS-CoV-2 in a country? 2. What types of masks exist, and how elaborate must a mask be to be effective against COVID-19? 3. Does the mask have to be applied early in an epidemic? 4. A brief general discussion of masks and some possible future research questions regarding masks and SARS-CoV-2. Results are as follows: (1) The results indicate that any type of mask, even simple home-made ones, may be effective. Masks use seems to have an effect in lowering new patients even the protective effect of each mask (here dubbed"one-mask protection") is'},
 {'role': 'assistant', 'content': 'Received passage [2].'},
 {'role': 'user',
  'content': '[3] Title: To mask or not to mask: Modeling the potential for face mask use by the general public to curtail the COVID-19 pandemic Content: Face mask use by the general public for limiting the spread of the COVID-19 pandemic is controversial, though increasingly recommended, and the potential of this intervention is not well understood. We develop a compartmental model for assessing the community-wide impact of mask use by the general, asymptomatic public, a portion of which may be asymptomatically infectious. Model simulations, using data relevant to COVID-19 dynamics in the US states of New York and Washington, suggest that broad adoption of even relatively ineffective face masks may meaningfully reduce community transmission of COVID-19 and decrease peak hospitalizations and deaths. Moreover, mask use decreases the effective transmission rate in nearly linear proportion to the product of mask effectiveness (as a fraction of potentially infectious contacts blocked) and coverage rate (as'},
 {'role': 'assistant', 'content': 'Received passage [3].'},
 {'role': 'user',
  'content': 'Search Query: How much impact do masks have on preventing the spread of the COVID-19?. \\nRank the 3 passages above based on their relevance to the search query. The passages should be listed in descending order using identifiers. The most relevant passages should be listed first. The output format should be [] > [], e.g., [1] > [2]. Only response the ranking results, do not say any word or explain.'}]
# Get ChatGPT predicted permutation
permutation = run_llm(
    messages,
    api_key='Your OPENAI Key!',
    model_name='gpt-3.5-turbo'
)
'[1] > [3] > [2]'
# Use permutation to re-rank the passage
item = receive_permutation(
    item,
    permutation,
    rank_start=0,
    rank_end=3
)
{'query': 'How much impact do masks have on preventing the spread of the COVID-19?',
 'hits': [{'content': 'Title: Universal Masking is Urgent in the COVID-19 Pandemic: SEIR and Agent Based Models, Empirical Validation, Policy Recommendations Content: We present two models for the COVID-19 pandemic predicting the impact of universal face mask wearing upon the spread of the SARS-CoV-2 virus--one employing a stochastic dynamic network based compartmental SEIR (susceptible-exposed-infectious-recovered) approach, and the other employing individual ABM (agent-based modelling) Monte Carlo simulation--indicating (1) significant impact under (near) universal masking when at least 80% of a population is wearing masks, versus minimal impact when only 50% or less of the population is wearing masks, and (2) significant impact when universal masking is adopted early, by Day 50 of a regional outbreak, versus minimal impact when universal masking is adopted late. These effects hold even at the lower filtering rates of homemade masks. To validate these theoretical models, we compare their predictions against a new empirical data set we have collected'},
  {'content': 'Title: To mask or not to mask: Modeling the potential for face mask use by the general public to curtail the COVID-19 pandemic Content: Face mask use by the general public for limiting the spread of the COVID-19 pandemic is controversial, though increasingly recommended, and the potential of this intervention is not well understood. We develop a compartmental model for assessing the community-wide impact of mask use by the general, asymptomatic public, a portion of which may be asymptomatically infectious. Model simulations, using data relevant to COVID-19 dynamics in the US states of New York and Washington, suggest that broad adoption of even relatively ineffective face masks may meaningfully reduce community transmission of COVID-19 and decrease peak hospitalizations and deaths. Moreover, mask use decreases the effective transmission rate in nearly linear proportion to the product of mask effectiveness (as a fraction of potentially infectious contacts blocked) and coverage rate (as'},
  {'content': 'Title: Masking the general population might attenuate COVID-19 outbreaks Content: The effect of masking the general population on a COVID-19 epidemic is estimated by computer simulation using two separate state-of-the-art web-based softwares, one of them calibrated for the SARS-CoV-2 virus. The questions addressed are these: 1. Can mask use by the general population limit the spread of SARS-CoV-2 in a country? 2. What types of masks exist, and how elaborate must a mask be to be effective against COVID-19? 3. Does the mask have to be applied early in an epidemic? 4. A brief general discussion of masks and some possible future research questions regarding masks and SARS-CoV-2. Results are as follows: (1) The results indicate that any type of mask, even simple home-made ones, may be effective. Masks use seems to have an effect in lowering new patients even the protective effect of each mask (here dubbed"one-mask protection") is'}]}

Chiến lược Cửa sổ Trượt (SWA) cho RankGPT

Nếu bạn cần xếp hạng nhiều tài liệu hơn khả năng mô hình xử lý cùng lúc, hãy dùng chiến lược cửa sổ trượt. Đây là cách áp dụng chiến lược cửa sổ trượt để xếp hạng lại tài liệu:

from rank_gpt import sliding_windows
api_key = "Your OPENAI Key"
new_item = sliding_windows(
    item,
    rank_start=0,
    rank_end=3,
    window_size=2,
    step=1,
    model_name='gpt-3.5-turbo',
    api_key=api_key
)
print(new_item)

Trong ví dụ này, cửa sổ trượt có kích thước 2 và bước trượt 1, nghĩa là xử lý hai tài liệu mỗi lần, sau đó tiến lên một tài liệu cho lượt xếp hạng tiếp theo.

Kết luận

Bằng cách sử dụng LLM để đánh giá tốt hơn mức độ liên quan của thông tin, RankGPT nâng cao độ chính xác của việc sắp xếp và xếp hạng lại nội dung.

Điều này giải quyết các vấn đề thường gặp như đảm bảo nội dung đúng trọng tâm, cải thiện hiệu suất và giảm khả năng tạo thông tin gây hiểu lầm.

Tổng thể, RankGPT góp phần xây dựng các ứng dụng RAG tin cậy và chính xác hơn.


Ryan Ong's photo
Author
Ryan Ong
LinkedIn
Twitter

Ryan là trưởng nhóm khoa học dữ liệu, chuyên xây dựng các ứng dụng AI sử dụng LLM. Anh là nghiên cứu sinh Tiến sĩ về Xử lý Ngôn ngữ Tự nhiên và Đồ thị Tri thức tại Imperial College London, nơi anh cũng hoàn thành bằng Thạc sĩ Khoa học Máy tính. Ngoài lĩnh vực khoa học dữ liệu, anh viết bản tin Substack hàng tuần, The Limitless Playbook, nơi anh chia sẻ một ý tưởng có thể áp dụng ngay từ các tư tưởng gia hàng đầu thế giới và đôi khi viết về những khái niệm cốt lõi của AI.

Chủ đề
Trí tuệ Nhân tạo
Mô hình Ngôn ngữ Lớn

Học AI với các khóa học này!

Khóa học

Xây dựng ứng dụng AI với Pinecone

3 giờ
12.2K
Khám phá cách cơ sở dữ liệu vector Pinecone đang cách mạng hóa phát triển ứng dụng AI!
Xem chi tiếtRight Arrow
Bắt Đầu Khóa Học
Xem thêmRight Arrow
Liên quan

blog

Claude Opus 4.6: Tính năng, Điểm chuẩn, Bài kiểm tra thực hành và hơn thế nữa

Mô hình mới nhất của Anthropic dẫn đầu ở mã hóa tác tử và lập luận phức tạp. Thêm vào đó, nó có cửa sổ ngữ cảnh 1M.
Matt Crabtree's photo

Matt Crabtree

10 phút

Xem ThêmXem Thêm