RAG高级优化:检索后处理模块成竹在胸

人工智能
根据论文 Lost in the Middle: How Language Models Use Long Contexts,的实验表明,大模型更容易记忆开头和结尾的文档,而对中间部分的文档记忆能力不强,因此可以根据召回的文档和query的相关性进行重排序。

本文我们将介绍在将召回片段送入大模型之前的一些优化手段,它们能帮助大模型更好的理解上下文知识,给出最佳的回答:

  • Long-text Reorder
  • Contextual compression
  • Refine
  • Emotion Prompt

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 the
    middle of the list and more relevant elements at beginning / end.
    See: https://arxiv.org/abs//2307.03172"""

    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 ContextualCompressionRetriever
from langchain.retrievers.document_compressors import LLMChainExtractor
from langchain_openai import OpenAI
 
llm = 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)

Refine

对最后大模型生成的回答进行进一步的改写,保证回答的准确性。主要涉及提示词工程,参考的提示词如下:

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

同样是提示词工程的一部分,思路来源于微软的论文:

Large Language Models Understand and Can Be Enhanced by Emotional Stimuli

在论文中,微软研究员提出,在提示词中增加一些情绪情感相关的提示,有助于大模型输出高质量的回答。

参考提示词如下:

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, ep03
emotion_stimuli_dict["ep06"] = (
    emotion_stimuli_dict["ep01"]
    + emotion_stimuli_dict["ep02"]
    + emotion_stimuli_dict["ep03"]
)
 
 
from llama_index.prompts import PromptTemplate
 
 
qa_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)
责任编辑:武晓燕 来源: 哎呀AIYA
相关推荐

2011-07-14 16:58:19

CIO虚拟化私有云

2024-09-24 14:32:17

RAG高级优化Fusion

2013-05-13 10:35:41

创业成功创业创业者

2015-07-03 10:46:26

PHP程序员工作高效

2023-10-14 17:46:17

RAG提示工程GPT-3

2024-09-19 09:12:50

RAG系统技术

2017-03-07 16:08:36

2024-07-08 12:44:11

2024-09-11 16:36:39

2024-01-29 08:49:36

RAG模型检索

2024-05-20 08:31:33

检索增强生成LLM大型语言模型

2024-10-11 09:04:55

2024-05-22 09:38:25

2024-02-18 09:00:00

RAG工具LlamaIndexChatGPT

2024-08-30 11:27:55

父文档检索RAG技术人工智能

2024-06-19 16:11:22

2024-11-04 10:40:00

AI模型

2024-10-31 14:46:31

2024-11-19 13:05:40

2024-09-19 13:04:41

点赞
收藏

51CTO技术栈公众号