企业宣传,产品推广,广告招商,广告投放联系seowdb

RAG初级优化 检索后处置模块心中有数

经过上文的方法​ ​RAG初级优化:一文看尽query的转换之路​ ​,咱们召回了一些关系片段,本文咱们将引见在将召回片段送入大模型之前的一些优化手腕,它们能协助大模型更好的了解高低文常识,给出最佳的回答:

Long-text Reorder

依据论文 Lost in the Middle: How Language Models Use Long Contexts,的试验标明,大模型更容易记忆扫尾和开头的文档,而对两边局部的文档记忆才干不强,因此可以依据召回的文档和query的关系性启动重排序。

**的代码可以参考langchain的成功:

def _litm_reordering(documents: List[Document]) -> List[Document]:"""Lost in the middle reorder: the less relevant documents will be at themiddle of the list and more relevant elements at beginning / end.See:"""documents.reverse()reordered_result = []for i, value in enumerate(documents):if i % 2 == 1:reordered_result.append(value)else:reordered_result.insert(0, value)return reordered_result

Contextual compression

实质上应用LLM去判别检索之后的文档和用户query的关系性,只前往关系度最高的k个。

from langchain.retrievers import ContextualCompressionRetrieverfrom langchain.retrievers.document_compressors import LLMChainExtractorfrom langchain_openai import OpenAIllm = OpenAI(temperature=0)compressor = LLMChainExtractor.from_llm(llm)compression_retriever = ContextualCompressionRetriever(base_compressor=compressor, base_retriever=retriever)compressed_docs = compression_retriever.get_relevant_documents("What did the president say about Ketanji Jackson Brown")print(compressed_docs)

对最后大模型生成的回答进后退一步的改写,保障回答的准确性。重要触及揭示词工程,参考的揭示词如下:

The original query is as follows: {query_str}We have provided an existing answer: {existing_answer}We have the opportunity to refine the existing answer (only if needed) with some more context below.------------{context_msg}------------Given the new context, refine the original answer to better answer the query. If the context isn't useful, return the original answer.Refined Answer:

Emotion Prompt

在论文中,微软钻研员提出,在揭示词中参与一些心情情感关系的揭示,有助于大模型输入高品质的回答。

参考揭示词如下:

emotion_stimuli_dict = {"ep01": "Write your answer and give me a confidence score between 0-1 for your answer. ","ep02": "This is very important to my career. ","ep03": "You'd better be sure.",# add more from the paper here!!}# NOTE: ep06 is the combination of ep01, ep02, ep03emotion_stimuli_dict["ep06"] = (emotion_stimuli_dict["ep01"]+ emotion_stimuli_dict["ep02"]+ emotion_stimuli_dict["ep03"])from llama_index.prompts import PromptTemplateqa_tmpl_str = """\Context information is below.---------------------{context_str}---------------------Given the context information and not prior knowledge, \answer the query.{emotion_str}Query: {query_str}Answer: \"""qa_tmpl = PromptTemplate(qa_tmpl_str)

原文链接:​ ​​ ​​

© 版权声明
评论 抢沙发
加载中~
每日一言
不怕万人阻挡,只怕自己投降
Not afraid of people blocking, I'm afraid their surrender