Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【RAG论文】HyDE:Precise Zero-Shot Dense Retrieval without Relevance Labels

【RAG论文】HyDE:Precise Zero-Shot Dense Retrieval without Relevance Labels

作者头像
致Great
发布于 2024-11-23 02:54:49
发布于 2024-11-23 02:54:49
12800
代码可运行
举报
文章被收录于专栏:自然语言处理自然语言处理
运行总次数:0
代码可运行

paper:https://arxiv.org/pdf/2212.10496 code:https://github.com/texttron/hyde

这篇文章主要做zero-shot场景下的稠密检索,通过借助LLM的力量不需要Relevance Labels,开箱即用。作者提出Hypothetical Document Embeddings (HyDE)方法,即“假设”文档嵌入。具体的做法是通过GPT生成虚构的文档,并使用无监督检索器对其进行编码,并在其嵌入空间中进行搜索,从而不需要任何人工标注数据。

模型结构如下图所示,HyDE将密集检索分解为两个任务,即 instruction-following的LM生成任务和对比编码器执行的文档相似性任务。

  • 首先生成一个问题的答案对于给定一个query,将由InstructGPT生成一个能回答该query的假设文档,即a hypothetical document。
  • 使用生成的答案进行检索:使用无监督的稠密检索模型(Contriever)把该文档表示为稠密向量。
  • 最后基于最近邻从语料库中找到相似的文档,作为支撑信息然后进行问答。

实现

https://github.com/gomate-community/GoMate/blob/main/gomate/modules/rewriter/hyde_rewriter.py

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import pandas as pd
from tqdm import tqdm
import os
from gomate.modules.generator.llm import GLMChat
from gomate.modules.retrieval.dense_retriever import DenseRetriever, DenseRetrieverConfig
from gomate.modules.rewriter.base import BaseRewriter
from gomate.modules.rewriter.promptor import Promptor
from gomate.modules.document.common_parser import CommonParser

class HydeRewriter(BaseRewriter):
    def __init__(self, promptor, generator, retriever):
        self.promptor = promptor
        self.generator = generator
        self.retriever = retriever

    def prompt(self, query):
        return self.promptor.build_prompt(query)

    def rewrite(self, query):
        prompt = self.promptor.build_prompt(query)
        hypothesis_document, _ = self.generator.chat(prompt, llm_only=True)
        return hypothesis_document

    def retrieve(self, query, top_k=5):
        hypothesis_document = self.rewrite(query)
        hits = self.retriever.retrieve(hypothesis_document, top_k=top_k)
        return {'hypothesis_document': hypothesis_document, 'retrieve_result': hits}

检索效果对比:

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2024-11-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验