成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

RAG高級優化:檢索后處理模塊成竹在胸 原創

發布于 2024-9-25 10:16
瀏覽
0收藏

通過上文的方法??RAG高級優化:一文看盡query的轉換之路??,我們召回了一些相關片段,本文我們將介紹在將召回片段送入大模型之前的一些優化手段,它們能幫助大模型更好的理解上下文知識,給出最佳的回答:

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

Long-text Reorder

根據論文 Lost in the Middle: How Language Models Use Long Contexts,的實驗表明,大模型更容易記憶開頭和結尾的文檔,而對中間部分的文檔記憶能力不強,因此可以根據召回的文檔和query的相關性進行重排序。

RAG高級優化:檢索后處理模塊成竹在胸-AI.x社區

核心的代碼可以參考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

原文鏈接:??https://mp.weixin.qq.com/s/-orAp5c6LGfnse83Lgy7Jw???

?著作權歸作者所有,如需轉載,請注明出處,否則將追究法律責任
標簽
收藏
回復
舉報
回復
相關推薦
主站蜘蛛池模板: 久久久久久久久久性 | 亚洲一本| 日韩一区二区在线视频 | 在线观看成人av | 久久久久久亚洲精品 | 色影视| 国产精品久久久久久婷婷天堂 | 欧美在线二区 | 国产99精品| 国产精品美女久久久久久久网站 | 91伊人 | 天堂视频中文在线 | 国产成人小视频 | 日韩字幕 | 天天艹天天干天天 | 在线视频成人 | 久久精品国产亚洲a | 婷婷福利视频导航 | 91在线视频观看免费 | 亚洲 欧美 另类 综合 偷拍 | 精品成人一区二区 | 国产999精品久久久 日本视频一区二区三区 | av成年人网站 | 在线观看免费国产 | 日韩免费1区二区电影 | 日韩精品四区 | 精品av | 国产98色在线 | 日韩 | 国产一级黄色网 | 亚洲天堂av在线 | 91精品国产色综合久久 | 久久国产精品久久 | 97热在线| 欧美日韩专区 | 国产精品久久久久aaaa | 国产高清视频 | 国产成人网 | 精品一区二区视频 | 亚洲在线一区 | se婷婷| av在线成人 |