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

RAG高級優化:基于問題生成的文檔檢索增強 原創

發布于 2024-9-14 14:18
瀏覽
0收藏

我們將在本文中介紹一種文本增強技術,該技術利用額外的問題生成來改進矢量數據庫中的文檔檢索。通過生成和合并與每個文本片段相關的問題,增強系統標準檢索過程,從而增加了找到相關文檔的可能性,這些文檔可以用作生成式問答的上下文。

實現步驟

通過用相關問題豐富文本片段,我們的目標是顯著提高識別文檔中包含用戶查詢答案的最相關部分的準確性。具體的方案實現一般包含以下步驟:

  • 文檔解析和文本分塊:處理PDF文檔并將其劃分為可管理的文本片段。
  • 問題增強:使用語言模型在文檔和片段級別生成相關問題。
  • 矢量存儲創建:使用??向量模型?計算文檔的嵌入,并創建FAISS矢量存儲。
  • 檢索和答案生成:使用FAISS查找最相關的文檔,并根據提供的上下文生成答案。

我們可以通過設置,指定在文檔級或片段級進行問題增強。

class QuestionGeneration(Enum):
    """
    Enum class to specify the level of question generation for document processing.


    Attributes:
        DOCUMENT_LEVEL (int): Represents question generation at the entire document level.
        FRAGMENT_LEVEL (int): Represents question generation at the individual text fragment level.
    """
    DOCUMENT_LEVEL = 1
    FRAGMENT_LEVEL = 2


RAG高級優化:基于問題生成的文檔檢索增強-AI.x社區


方案實現

問題生成

def generate_questions(text: str) -> List[str]:
    """
    Generates a list of questions based on the provided text using OpenAI.


    Args:
        text (str): The context data from which questions are generated.


    Returns:
        List[str]: A list of unique, filtered questions.
    """
    llm = ChatOpenAI(model="gpt-4o-mini", temperature=0)
    prompt = PromptTemplate(
        input_variables=["context", "num_questions"],
        template="Using the context data: {context}\n\nGenerate a list of at least {num_questions} "
                 "possible questions that can be asked about this context. Ensure the questions are "
                 "directly answerable within the context and do not include any answers or headers. "
                 "Separate the questions with a new line character."
    )
    chain = prompt | llm.with_structured_output(QuestionList)
    input_data = {"context": text, "num_questions": QUESTIONS_PER_DOCUMENT}
    result = chain.invoke(input_data)
    
    # Extract the list of questions from the QuestionList object
    questions = result.question_list
    
    filtered_questions = clean_and_filter_questions(questions)
    return list(set(filtered_questions))

處理主流程

def process_documents(content: str, embedding_model: OpenAIEmbeddings):
    """
    Process the document content, split it into fragments, generate questions,
    create a FAISS vector store, and return a retriever.


    Args:
        content (str): The content of the document to process.
        embedding_model (OpenAIEmbeddings): The embedding model to use for vectorization.


    Returns:
        VectorStoreRetriever: A retriever for the most relevant FAISS document.
    """
    # Split the whole text content into text documents
    text_documents = split_document(content, DOCUMENT_MAX_TOKENS, DOCUMENT_OVERLAP_TOKENS)
    print(f'Text content split into: {len(text_documents)} documents')


    documents = []
    counter = 0
    for i, text_document in enumerate(text_documents):
        text_fragments = split_document(text_document, FRAGMENT_MAX_TOKENS, FRAGMENT_OVERLAP_TOKENS)
        print(f'Text document {i} - split into: {len(text_fragments)} fragments')
        
        for j, text_fragment in enumerate(text_fragments):
            documents.append(Document(
                page_cnotallow=text_fragment,
                metadata={"type": "ORIGINAL", "index": counter, "text": text_document}
            ))
            counter += 1
            
            if QUESTION_GENERATION == QuestionGeneration.FRAGMENT_LEVEL:
                questions = generate_questions(text_fragment)
                documents.extend([
                    Document(page_cnotallow=question, metadata={"type": "AUGMENTED", "index": counter + idx, "text": text_document})
                    for idx, question in enumerate(questions)
                ])
                counter += len(questions)
                print(f'Text document {i} Text fragment {j} - generated: {len(questions)} questions')
        
        if QUESTION_GENERATION == QuestionGeneration.DOCUMENT_LEVEL:
            questions = generate_questions(text_document)
            documents.extend([
                Document(page_cnotallow=question, metadata={"type": "AUGMENTED", "index": counter + idx, "text": text_document})
                for idx, question in enumerate(questions)
            ])
            counter += len(questions)
            print(f'Text document {i} - generated: {len(questions)} questions')


    for document in documents:
        print_document("Dataset", document)


    print(f'Creating store, calculating embeddings for {len(documents)} FAISS documents')
    vectorstore = FAISS.from_documents(documents, embedding_model)


    print("Creating retriever returning the most relevant FAISS document")
    return vectorstore.as_retriever(search_kwargs={"k": 1})

該技術為提高基于向量的文檔檢索系統的信息檢索質量提供了一種方法。此實現使用了大模型的API,這可能會根據使用情況產生成本。


本文轉載自公眾號哎呀AIYA

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



?著作權歸作者所有,如需轉載,請注明出處,否則將追究法律責任
標簽
已于2024-9-14 14:18:55修改
收藏
回復
舉報
回復
相關推薦
主站蜘蛛池模板: 成人精品国产免费网站 | 国产精品久久av | 久久久久久成人 | 亚洲视频在线观看一区二区三区 | 国产一区二区在线视频 | 国产精品一区二区在线 | 国产一区二区三区在线 | 国内精品久久久久 | 色偷偷888欧美精品久久久 | 日本免费黄色一级片 | 免费视频成人国产精品网站 | 国产成人精品综合 | 亚洲精品久久久久久一区二区 | 久久精品综合网 | 国产成人av一区二区三区 | 日本手机看片 | 成人欧美一区二区 | 精品亚洲一区二区三区四区五区 | 一级毛片在线播放 | 国产真实精品久久二三区 | 午夜精品久久久久久久久久久久久 | 国产精品免费一区二区三区四区 | 久久久国产精品视频 | 久久久久9999亚洲精品 | 国产精品亚洲成在人线 | 亚洲另类春色偷拍在线观看 | 久久久999免费视频 999久久久久久久久6666 | www日本在线 | 日韩一区二区在线观看 | 99精品网 | 亚洲欧美在线一区 | 国产精品久久久久久亚洲调教 | 久久久久久国产精品 | 久久久国产一区二区三区 | 成人一区二区在线 | 亚洲国产精品人人爽夜夜爽 | 亚洲福利av | 日韩欧美精品一区 | 日韩中文一区 | 国产精品亚洲一区 | 欧美激情国产精品 |