提高 RAG 推理能力的一个好方法是添加查询理解层 ——在实际查询向量存储之前添加查询转换。以下是四种不同的查询转换:
HyDE来自于Precise Zero-Shot Dense Retrieval without Relevance Labels,这篇文章主要做zero-shot场景下的稠密检索,通过借助LLM的力量不需要Relevance Labels,开箱即用。作者提出Hypothetical Document Embeddings (HyDE)方法,即“假设”文档嵌入。具体的做法是通过GPT生成虚构的文档,并使用无监督检索器对其进行编码,并在其嵌入空间中进行搜索,从而不需要任何人工标注数据模型结构如下图所示,HyDE将密集检索分解为两个任务,即 instruction-following的LM生成任务和对比编码器执行的文档相似性任务。
paper:https://arxiv.org/pdf/2212.10496
code:https://github.com/texttron/hyde
典型的密集信息检索过程包括以下步骤:
我们在LangChain上实际使用一下。
from langchain.embeddings import OpenAIEmbeddings
from langchain.chains import LLMChain,HypotheticalDocumentEmbedder
from langchain.prompts import PromptTemplate
from langchain.chat_models import ChatOpenAI
from dotenv import load_dotenv
#set the environment variables
load_dotenv()
#prepare the prompt template for document generation
Prompt_template="""回答问题。
问题:{question}
回答:”””
llm=ChatOpenAI()
#multi_llm=ChatOpenAI(n=4)
prompt=PromptTemplate(input_variables=["question"],template=prompt_template)
llm_chain=LLMChain(llm=llm,prompt=prompt,verbose=True)
#initialize the hypothetical document embedder
base_embeddings=OpenAIEmbeddings()
embeddings=HypotheticalDocumentEmbedder(llm_chain=llm_chain,base_embeddings=base_embeddings)
result=embeddings.embed_query("塞尔达传说的主角是谁?")
len(result)| 欢迎光临 链载Ai (https://www.lianzai.com/) | Powered by Discuz! X3.5 |