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

RAG高級(jí)優(yōu)化:基于問(wèn)題生成的文檔檢索增強(qiáng) 原創(chuàng)

發(fā)布于 2024-9-14 14:18
瀏覽
0收藏

我們將在本文中介紹一種文本增強(qiáng)技術(shù),該技術(shù)利用額外的問(wèn)題生成來(lái)改進(jìn)矢量數(shù)據(jù)庫(kù)中的文檔檢索。通過(guò)生成和合并與每個(gè)文本片段相關(guān)的問(wèn)題,增強(qiáng)系統(tǒng)標(biāo)準(zhǔn)檢索過(guò)程,從而增加了找到相關(guān)文檔的可能性,這些文檔可以用作生成式問(wèn)答的上下文。

實(shí)現(xiàn)步驟

通過(guò)用相關(guān)問(wèn)題豐富文本片段,我們的目標(biāo)是顯著提高識(shí)別文檔中包含用戶(hù)查詢(xún)答案的最相關(guān)部分的準(zhǔn)確性。具體的方案實(shí)現(xiàn)一般包含以下步驟:

  • 文檔解析和文本分塊:處理PDF文檔并將其劃分為可管理的文本片段。
  • 問(wèn)題增強(qiáng):使用語(yǔ)言模型在文檔和片段級(jí)別生成相關(guān)問(wèn)題。
  • 矢量存儲(chǔ)創(chuàng)建:使用??向量模型?計(jì)算文檔的嵌入,并創(chuàng)建FAISS矢量存儲(chǔ)。
  • 檢索和答案生成:使用FAISS查找最相關(guān)的文檔,并根據(jù)提供的上下文生成答案。

我們可以通過(guò)設(shè)置,指定在文檔級(jí)或片段級(jí)進(jìn)行問(wèn)題增強(qiáng)。

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高級(jí)優(yōu)化:基于問(wèn)題生成的文檔檢索增強(qiáng)-AI.x社區(qū)


方案實(shí)現(xiàn)

問(wèn)題生成

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})

該技術(shù)為提高基于向量的文檔檢索系統(tǒng)的信息檢索質(zhì)量提供了一種方法。此實(shí)現(xiàn)使用了大模型的API,這可能會(huì)根據(jù)使用情況產(chǎn)生成本。


本文轉(zhuǎn)載自公眾號(hào)哎呀AIYA

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



?著作權(quán)歸作者所有,如需轉(zhuǎn)載,請(qǐng)注明出處,否則將追究法律責(zé)任
標(biāo)簽
已于2024-9-14 14:18:55修改
收藏
回復(fù)
舉報(bào)
回復(fù)
相關(guān)推薦
主站蜘蛛池模板: 日韩精品av一区二区三区 | 99精品国产在热久久 | 国产午夜精品一区二区三区嫩草 | 蜜桃视频在线观看www社区 | 91福利网 | 国产精品美女久久久免费 | 免费黄色a级毛片 | 9999国产精品欧美久久久久久 | 亚洲精品在线播放 | 九九久久精品 | 久久久91精品国产一区二区精品 | 欧美一级高潮片免费的 | 亚洲精品视频三区 | 国产精品综合久久 | 在线观看中文字幕 | 99精品国产一区二区青青牛奶 | 91免费视频 | 欧美性大战xxxxx久久久 | 亚洲精品456 | 免费观看一级毛片 | 99国产精品视频免费观看一公开 | 日韩精品在线免费 | 天天干成人网 | 香蕉视频91 | 精品一区二区在线观看 | 在线观看中文字幕一区二区 | 精品久久伊人 | 免费观看黄 | 欧美偷偷 | 免费在线观看av片 | 欧美国产精品一区二区 | 成人 在线| av大片 | 黄色在线免费观看 | 九九热免费在线观看 | 亚洲一页 | 波多野结衣一二三区 | 日本三级线观看 视频 | 国产日韩欧美在线播放 | 中文字幕国产视频 | 天堂一区在线观看 |