Inference enhanced RAG retrieval method and system based on semantic tree index
By constructing a semantic knowledge tree for a large language model and performing multi-step reasoning search, the problem of insufficient knowledge understanding of long documents in vertical business domains is solved, achieving high-precision information retrieval and answer generation, and reducing system maintenance costs and computing power requirements.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-26
- Publication Date
- 2026-04-07
AI Technical Summary
Existing large language models struggle to fully understand long document knowledge in vertical business domains, resulting in mismatches between generated content and the actual context, insufficient accuracy, and poor flexibility, inadequate real-time performance, and high computational costs.
By structuring long documents in a specific domain into an indexable semantic knowledge tree, and using a large language model for multi-step reasoning and path searching during the retrieval process, the system dynamically locks the most matching logical node, realizing the transformation from semantic similarity matching to structured logical positioning, and generating accurate and traceable answers.
It improves the semantic relevance and output accuracy of the RAG system in specific scenarios, reduces system maintenance costs, enhances dynamic adaptability and flexibility, and enables accurate and traceable information retrieval in long documents.
Smart Images

Figure CN121808045A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the fields of artificial intelligence and natural language processing, specifically to a reasoning-enhanced RAG retrieval method and system based on semantic tree indexing. Background Technology
[0002] With the popularization and development of large-scale models, Large Language Models (LLMs) have made significant progress in natural language understanding and generation, capable of handling text generation and semantic understanding tasks across multiple domains and tasks. Existing large-scale language models are typically trained on general corpora, and their generation capabilities are mainly aimed at universal language scenarios. In vertical business domains, the main focus is on building knowledge-based augmented retrieval (RAG) systems, the core of which is to vectorize documents and extract relevant knowledge through vector similarity algorithms.
[0003] In practical applications, different vertical scenarios have different accuracy requirements for the output of RAG systems. For example, in scenarios such as legal consultation, financial analysis, medical diagnosis, financial analysis, and legal advice, users expect the answers provided by the RAG system to solve their problems. However, when building knowledge bases for professional domains, the approach of segmenting long documents is still used. Some semantically coherent sentences and paragraphs are broken down, and even paragraphs and chapters are forcibly separated. In addition, the semantic space representation of similar algorithms is limited, making it difficult for RAG systems to fully understand the knowledge in long documents for specific scenarios. The generated content does not perfectly match the on-site context, resulting in insufficient relevance of the answers, decreased accuracy, and reduced user experience and application value.
[0004] Existing solutions mainly rely on fine-tuning the model, replacing it with a larger model, or vectorizing it by chapter to adapt to specific scenarios, but these methods have the following drawbacks:
[0005] Poor flexibility: It is difficult to quickly switch to other scenarios after fine-tuning the model;
[0006] Insufficient real-time capability: Unable to adapt to dynamically changing scenario requirements in a timely manner;
[0007] Increased computing costs: Model fine-tuning requires a large amount of labeled data and computing resources, and replacing large-sized models will also increase computing costs;
[0008] Impact on model quality: Even with vectorization methods, there are still issues with semantic integrity segmentation of long texts, and the performance remains poor for specific scenarios.
[0009] Therefore, there is an urgent need for a method that can achieve semantic enhancement of RAG systems in specific scenarios without vectorization, so as to improve the scenario relevance and accuracy of the system output, thereby enhancing user experience and application value. Summary of the Invention
[0010] To address the aforementioned issues, this invention provides a reasoning-enhanced RAG retrieval method and system based on semantic tree indexing. By structuring long documents for specific domain tasks into an indexable semantic knowledge tree, and guiding a large language model to perform multi-step reasoning and path searching on the tree during the retrieval process, the method dynamically locks down the logical node that best matches the query, achieving a fundamental shift from "semantic similarity matching" to "structured logical positioning" without relying on vector databases and embedding model training.
[0011] The specific plan is as follows:
[0012] In a first aspect, the present invention provides a reasoning-enhanced RAG retrieval method based on semantic tree indexing, specifically including the following steps:
[0013] S1. Document Reading and Parsing: Through the scenario document reading and parsing module, long documents are read and parsed into text in the target scenario, preparing for subsequent document structuring into a semantic knowledge tree;
[0014] S2, Document Structured Semantic Knowledge Tree; Employing the scenario long document structuring module, the document text content obtained from the scenario document reading and parsing module is deeply parsed into structured data, its inherent hierarchical structure (such as chapters, sections, paragraphs) is extracted, and a semantic knowledge tree that retains the complete context and logical relationship is generated. Each tree node contains a content summary, precise page references, and child node pointers.
[0015] S3, Tree Search and Retrieval: Based on the semantic knowledge tree generated by the long document structuring module, the tree search and retrieval module uses the reasoning ability of the large language model to perform multi-step reasoning and path search in the tree structure, dynamically locking and querying the most matching logical node, realizing a fundamental transformation from "semantic similarity matching" to "structured logical positioning".
[0016] S4, RAG system enhanced answer: Based on the nodes retrieved by the tree search and retrieval module, the RAG system enhanced answer module extracts the node content and generates enhanced context, and then uses a large language model to generate accurate and traceable answers.
[0017] Further, step S1 includes:
[0018] S11, DOC Class Document Parsing
[0019] Read and parse text content from DOC and DOCX documents Specifically, different reading schemes are used depending on the file extension of the document. For documents with the DOCX extension, a DOCX parsing library is used to extract the text content of each page. For documents with the DOC extension, the LIBREOFFICE cross-platform office software is first used to convert them into DOCX documents, and then the DOCX parsing library is used to extract the text content of each page.
[0020] S12, MARKDOWN Document Analysis
[0021] Read and parse text content from Markdown and TXT documents. Specifically, it reads directly using Python's built-in `open` function.
[0022] S13, PDF document parsing
[0023] Read and parse text content from PDF documents Specifically, two different PDF parsing libraries (PyPDF2 or PyMuPDF) are used to read PDF files and extract the text content of each page. The PyPDF2 parsing library is for simple parsing of PDF documents, while the PyMuPDF parsing library is for parsing PDF documents with more PDF features and can read from memory streams.
[0024] Further, step S2 includes:
[0025] S21. Directory Detection and Extraction
[0026] First, check if the document contains a table of contents (TOC); the specific method is as follows:
[0027] S211, Directory Page Detection
[0028] The system checks each page sequentially, starting from the first page of the document, using a large language model to determine if each page contains the table of contents. The detection process employs a fuzzy matching strategy, ignoring inconsistencies in whitespace. If consecutive table of contents pages are detected, the indexes of all table of contents pages are recorded.
[0029] Let the total number of pages in the document be N, and the table of contents detection function be: ,in Indicates the first Page content:
[0030] if ,but Marked as a directory page, the collection of directory pages:
[0031]
[0032] S212, Extracting Directory Contents
[0033] For the detected directory pages, extract the directory text content; during the extraction process, replace the ellipses (...) in the directory with colons (:) to unify the directory format; at the same time, check whether the directory contains page number information;
[0034] Let the extracted directory text be... The function for extracting page number content is:
[0035]
[0036] S22, Directory JSONization
[0037] The extracted table of contents text is converted into a structured JSON format using a large language model. During the conversion, each table of contents item is assigned a hierarchical structure number (structure), for example, the first chapter is "1", the first section of the first chapter is "1.1", the second section is "1.2", and so on. Each table of contents item contains the following information: structure number, title, and page number.
[0038] Let the set of directory items be
[0039] Each directory item The structure is as follows:
[0040] {
[0041] "structure": ,
[0042] "title": ,
[0043] page:
[0044] }
[0045] The hierarchical relationship of structure numbering is defined as follows: For structure numbering Its parent node structure number is Its hierarchical depth is depth(s) = the number of "." in s plus 1;
[0046] S23, Page Number Index Extraction and Mapping
[0047] S231, Processing of Tables of Contents with Page Numbers
[0048] If the table of contents contains page number information, first extract the page numbers from the table of contents, then determine the actual physical page number position of each chapter in the document by matching the table of contents item titles with the document body content; calculate the offset between the table of contents page numbers and the physical page numbers, and apply this offset to all table of contents items to obtain an accurate physical page number index.
[0049] Let the page numbers in the table of contents be The actual physical page numbers in the document are Then the page number offset The calculation formula is:
[0050] Calculate the most common offset by matching multiple directory entries:
[0051]
[0052] Here, `count()` represents the number of directory entries that meet the criteria. After obtaining the offset, the physical page number index of all directory entries is calculated as follows:
[0053]
[0054] S232, Handling Tables Without Page Numbers
[0055] If the table of contents does not contain page number information, group the document content according to the maximum token limit, and then use a large language model to find the starting page number corresponding to each table of contents title in each group; add physical page number index information to each table of contents item.
[0056] Let the total number of tokens for the document be... The maximum number of tokens is Then the number of document groups The calculation formula is:
[0057]
[0058] Average number of tokens per group The calculation formula is:
[0059]
[0060] Organize the document content according to Group the data and use a large language model in each group to find the physical page number index corresponding to the directory item title;
[0061] S233, Supplementing Missing Page Numbers
[0062] For cases where some table of contents entries may be missing page numbers, the page number range of the table of contents entry is determined based on the previous and next valid page numbers. Then, within this range, a large language model is used to find the starting page number corresponding to the title of the table of contents entry.
[0063] Let the current directory entry be The preceding valid page number is The next valid page number is ,but The page number range is Use a large language model to search within this range. Physical page number index corresponding to the title ;
[0064] S24. Tree structure generation without a directory
[0065] When a document does not contain a table of contents, the tree structure is generated directly from the document content; the specific method is as follows:
[0066] S241, Document Content Grouping
[0067] The document pages are grouped according to the maximum number of tokens, and the content of each group does not exceed the preset number of tokens. A certain amount of page overlap is maintained when grouping to ensure that chapter boundaries are not cut off.
[0068] Let the document page set be The number of tokens per page is The maximum number of tokens is The number of overlapping pages is The grouping algorithm is as follows:
[0069] if Then all pages will be merged into one group;
[0070] Otherwise, group according to the following rules:
[0071] Initialize the number of tokens in the current group. ;
[0072] For each page ,if Then start a new group, the new group starts from start;
[0073] Otherwise, Join the current group;
[0074] S242, Initial Tree Structure Generation
[0075] The initial tree structure is extracted from the content of the first set of documents using a large language model, identifying chapter titles and hierarchical relationships, and assigning a structure number and physical page index to each chapter; let the content of the first set of documents be... The initial tree structure generation function is ,but:
[0076] in This is the initial tree structure, containing the structure number, title, and physical page number index;
[0077] S243, Tree Structure Expansion
[0078] For subsequent document content groups, based on the existing tree structure, a large language model is used to continue extracting new chapter information, and the newly extracted chapters are added to the existing tree structure to form a complete semantic knowledge tree;
[0079] Assume the existing tree structure is The current document content group is The tree structure expansion function is ,but:
[0080]
[0081] in For newly extracted chapter information, Indicates a merge operation;
[0082] S25, Tree Structure Construction
[0083] S251, Conversion from Flat List to Tree Structure
[0084] Convert the flat list containing structure numbers into a tree structure. During the conversion, establish parent-child node relationships based on the hierarchical relationship of the structure numbers (such as "1", "1.1", "1.2", etc.). For example, the parent node of structure number "1.1" is "1", and the parent node of structure number "1.2.1" is "1.2".
[0085] Let the structure number be Then its parent node structure number The calculation formula is:
[0086] 1
[0087]
[0088] For each node ,if If it exists in the node set, then it will Add as The child nodes; otherwise, It is the root node;
[0089] S252, Node Boundary Determination
[0090] For each tree node, determine the start and end indexes; for nodes that are not the last, if the title of the next node appears at the beginning of the page, the end index of the current node is the start index of the next node minus 1; otherwise, the end index of the current node is equal to the start index of the next node; the end index of the last node is the total number of pages in the document.
[0091] Set nodes The starting index is The ending index is The next node is The indicator function for whether the next node title appears at the beginning of the page is: The total number of pages in the document is Then the index ends. for:
[0092] If i < n-1 (not the last node):
[0093]
[0094] S26. Tree Structure Verification and Correction
[0095] S261, Structural Verification
[0096] The generated tree structure is verified by randomly selecting a certain number of nodes and checking whether the title of each node actually appears at the corresponding physical page number position; the verification process is carried out in a concurrent manner to improve verification efficiency.
[0097] Let the total number of nodes in the tree structure be . The number of verification nodes is ,if Then all nodes are verified; the verification function is: ,but:
[0098]
[0099] For each Concurrent execution verification:
[0100]
[0101] Verification results ,in This indicates that the node title appears at the corresponding physical page number. This indicates that it did not appear;
[0102] S262, Error Correction
[0103] For nodes that fail to be verified, the starting page number corresponding to the node title is searched again within the page number range of the preceding and following correct nodes. The correction process is repeated multiple times until the maximum number of attempts is reached or all nodes pass verification.
[0104] Let the node that failed verification be The physical page number of its preceding correct node is The physical page number of the next correct node is The correction function is The maximum number of attempts is Polling Each time,
[0105]
[0106] If the result is Then it ends;
[0107] Corrected physical page number index In scope Inside.
[0108] Further, step S3 includes:
[0109] S31. Query understanding based on semantic knowledge tree
[0110] It receives user queries and performs query understanding based on the semantic knowledge tree (Tree) generated by the scenario long document structuring module. The semantic knowledge tree is output by the scenario long document structuring module and contains the following information: Tree = {
[0111] node_id: {
[0112] "title": title_i,
[0113] "start_index": start_i,
[0114] "end_index": end_i,
[0115] "summary": summary_i,
[0116] "nodes": [child_nodes]
[0117] }
[0118] }
[0119] First, we analyze the semantic intent of the query to identify key concepts, entities, and relationships in the query, providing guidance for subsequent tree search.
[0120] S32, Tree Search Based on Large Language Model
[0121] Using a large language model, inferential search is performed in the semantic knowledge tree to identify tree nodes that may contain the answer; the specific method is as follows:
[0122] S321, Tree Search Hint Construction
[0123] The tree search suggestion `prompt_search` is constructed by inputting the user query and the semantic knowledge tree (removing detailed text content and retaining only structural information) into the large language model:
[0124] prompt_search = f"""
[0125] Given a query and a tree structure of documents, find all tree nodes that may contain the answer; Query: {query} Semantic knowledge tree structure: {Tree_structure} Please reply in the following JSON format: {{
[0126] "thinking": <The reasoning process regarding which nodes are relevant>
[0127] "node_list": [node_id1, node_id2, ...]
[0128] }}
[0129] Tree_structure is the tree structure after removing detailed text content, containing only node ID, title, summary, and child node relationships;
[0130] S322. Inferential Node Selection: Large language models perform inference based on query semantics and tree structure, outputting a list of relevant nodes. Let the tree search function be f_tree_search(query, Tree_structure), then:
[0131] search_result = f_tree_search(query, Tree_structure)
[0132] search_result = {{
[0133] "thinking": reasoning_text,
[0134] "node_list": [node_id1, node_id2, ..., node_idk]
[0135] }}
[0136] Based on the inference results, the system extracts a set of relevant node IDs: relevant_nodes = {{node_id | node_id ∈ search_result["node_list"]}}
[0137] S33, Expert Knowledge Integration and Preference Alignment
[0138] It supports the integration of expert knowledge or user preferences, eliminating the need for model fine-tuning; simply add relevant knowledge to the tree search suggestions.
[0139] S331, Preference Knowledge Retrieval
[0140] When a query is received, the most relevant user preferences or expert knowledge fragments are selected from the knowledge base or domain-specific rules; let the preference retrieval function be f_preference_retrieve(query), then:
[0141] Preference = f_preference_retrieve(query)
[0142] Preference is obtained through keyword matching, semantic similarity, or relevance search based on a large language model;
[0143] S332, Enhanced Tree Search
[0144] Integrate preference knowledge into tree search suggestions to build enhanced tree search suggestions:
[0145] prompt_enhanced = f"""
[0146] Given a query and a tree structure of documents, find all tree nodes that may contain the answer; Query: {query} Semantic knowledge tree structure: {Tree_structure} Related expert knowledge: {Preference} Please reply in the following JSON format: {{
[0147] "thinking": <The reasoning process regarding which nodes are relevant>
[0148] "node_list": [node_id1, node_id2, ...]}}
[0149] The enhanced tree search function is: search_result_enhanced = f_tree_search(query,Tree_structure, Preference)
[0150] By integrating expert knowledge, node search becomes more accurate and effective, while leveraging document structure and domain-specific insights.
[0151] Further, step S4 includes:
[0152] S41. Extraction of relevant node content
[0153] Based on the list of relevant node IDs retrieved by the tree search and retrieval module, extract the detailed content of the corresponding node from the semantic knowledge tree;
[0154] S411. Node content mapping establishes a mapping relationship between node IDs and node content. Let the node mapping function be `create_node_mapping(Tree)`, then: `node_map = create_node_mapping(Tree)`.
[0155] node_map = {{
[0156] node_id: {{
[0157] "node": node_info,
[0158] "text": text_content,
[0159] "start_index": start_page,
[0160] "end_index": end_page
[0161] }}
[0162] }}
[0163] S412, Content Aggregation
[0164] Based on the retrieved node ID list node_list, extract and aggregate the text content of related nodes; let the content extraction function be extract_content(node_list, node_map), then:
[0165] relevant_content = extract_content(node_list, node_map)
[0166] relevant_content = "\n\n".join(node_map[node_id]["text"] for node_idin node_list)
[0167] The "\n\n" is used to separate the content of different nodes, maintaining the readability and structure of the content;
[0168] S413, Determining the Page Range
[0169] Determine the page range corresponding to the retrieved node for answer tracing. Let the page range of node_i be page_range_i, then:
[0170] page_range_i = {{
[0171] start_page: node_map[node_id]["start_index"],
[0172] end_page: node_map[node_id]["end_index"]}}
[0173] If start_page = end_page, then the page range is a single page number; otherwise, it is a range of page numbers.
[0174] S42, Context Enhancement Construction
[0175] Based on the extracted relevant content, an enhanced context is constructed to provide sufficient background information for the large language model to generate answers;
[0176] Construct a structured context containing the following information: context = {{
[0177] "query": query,
[0178] "relevant_nodes": node_list,
[0179] "content": relevant_content,
[0180] "page_references": page_ranges,
[0181] "tree_structure": Tree_structure}}
[0182] S43. Answer generation based on enhanced context
[0183] Use a large language model to generate answers based on enhanced context;
[0184] S431. The answer generation prompt system constructs the answer generation prompt_answer:
[0185] prompt_answer = f"""Answer the user's question based on the following context information: User query: {query} Related context: {optimized_content}"
[0186] Page references: {page_references}
[0187] Please generate an accurate and complete answer based on the above context; if the context does not contain enough information to answer the question, please state this clearly.
[0188] The page_references section contains page reference information, formatted as "node title: page range";
[0189] S432, Answer Generation
[0190] A large language model is used to generate answers based on prompts. Let the answer generation function be `f_generate_answer(prompt_answer)`, then: `answer = f_generate_answer(prompt_answer)`. The generated answer `answer` includes: a direct answer to the user query; a detailed explanation based on context; traceable page citation information; and a system that generates citation information for each key information point in the answer. Let the citation generation function be `generate_citations(answer, node_map)`, then: `citations = generate_citations(answer, node_map)`.
[0191] citations = [
[0192] {{
[0193] "text": cited_text,
[0194] "node_id": node_id,
[0195] "page_range": page_range,
[0196] "title": node_title
[0197] }}
[0198] The system formats the answer and citation information into the final output; let the final output be final_answer, then: final_answer = {{
[0199] "answer": answer,
[0200] "citations": citations,
[0201] "source_nodes": node_list,
[0202] "confidence": confidence_score}} The above formatted answer makes it easier for users to verify the accuracy of the answer and trace its source.
[0203] Secondly, the present invention provides a reasoning-enhanced RAG retrieval system based on semantic tree indexing, comprising:
[0204] Scene document reading and parsing module: Reads and parses long documents into text in the target scene, preparing for subsequent document structuring into a semantic knowledge tree;
[0205] The long document structuring module for scenarios reads and parses the document text content obtained from the scenario document reading and parsing module into structured data, extracts its inherent hierarchical structure (such as chapters, sections, and paragraphs), and generates a semantic knowledge tree that preserves the complete context and logical relationships. Each tree node contains a content summary, precise page references, and child node pointers.
[0206] Tree Search and Retrieval Module: Based on the semantic knowledge tree generated by the long document structuring module, it uses the reasoning ability of the large language model to perform multi-step reasoning and path search in the tree structure, dynamically locking and querying the most matching logical node, realizing a fundamental transformation from "semantic similarity matching" to "structured logical positioning".
[0207] The RAG system's enhanced answer module extracts node content and generates enhanced context based on nodes retrieved by the tree search and retrieval module. Then, it uses a large language model to generate accurate and traceable answers.
[0208] The beneficial effects of this invention are as follows:
[0209] Aimed at improving the semantic relevance and output accuracy of Retrieval Augmentation (RAG) systems in specific scenarios without vectorizing document chunks, this native reasoning retrieval method uses a semantic tree structure. It surpasses vector methods, providing human-like, accurate, and traceable information retrieval from complex and lengthy professional documents. Specific advantages are as follows:
[0210] 1. Vector-free architecture reduces maintenance costs: This invention directly parses semantic knowledge trees from long documents. The vector-free design eliminates the system's dependence on embedded model version upgrades and avoids the performance bottleneck of vector database expansion. This architectural design significantly reduces system maintenance costs and operational complexity.
[0211] 2. High-precision retrieval of long documents: In professional long document question-and-answer scenarios, this invention can accurately find relevant knowledge and identify and extract the information that users really need, significantly reducing the risk of "answering the wrong question".
[0212] 3. Strong dynamic adaptability: This invention can automatically adjust the retrieval strategy according to different document types and query modes, demonstrating good versatility and flexibility. Attached Figure Description
[0213] Figure 1 This is a flowchart of the method of the present invention. Detailed Implementation
[0214] The present invention will be further illustrated below with reference to the accompanying drawings and specific embodiments. It should be understood that the following specific embodiments are for illustrative purposes only and are not intended to limit the scope of the invention.
[0215] As shown in the figure, this embodiment provides a reasoning-enhanced RAG retrieval method based on semantic tree indexing, and the specific scheme is as follows:
[0216] 1. Document Reading and Parsing
[0217] The system supports reading and parsing document formats such as PDF, DOC, and DOCX.
[0218] 2. Document Structured Semantic Knowledge Tree Generation 2.1 Table of Contents Detection and Extraction System The system detects the table of contents page by page, starting from the first page. For page i, a table of contents detection prompt is constructed: prompt_toc_detect = f""" This detects whether the given text contains a table of contents. Given text: {page_text_i} Returned JSON format: {{
[0219] "thinking": <reasoning process>
[0220] The system calls the large language model `f_toc_detect(page_text_i)`. If it returns "yes", then `page_i` is marked as a directory page. The system records the indices of all directory pages: `toc_page_list = [i | f_toc_detect(page_i) = "yes"]`. If a directory page is detected, the system extracts the directory text content: `TOC_text = "".join(page_text_i for i in toc_page_list)`, and replaces the ellipsis with a colon: `TOC_text = TOC_text.replace("...", ":")`. 2.2 Directory Structure Transformation The system uses the large language model to convert the directory text into JSON format. Constructing the transformation prompt: `prompt_toc_transform = f"""` converts the directory into JSON format, including structure numbers, titles, and page numbers. Directory content: `{TOC_text}` Return format: `{{`
[0221] "table_of_contents": [
[0222] {{"structure": "1", "title": "Chapter 1", "page": 1}},
[0223] {{"structure": "1.1", "title": "Section 1", "page": 2}}, ... ]
[0226] The large language model is called to obtain toc_json = f_toc_transform(TOC_text). 2.3 The tree structure construction system converts the flat list into a tree structure. For a structure number s = "abc", its parent node is parent(s) = "ab". The system establishes parent-child relationships and constructs the tree structure Tree. For each node node_i, the start index start_i and end index end_i are determined: 2.4 The tree structure verification and correction system randomly selects N_verify nodes for verification (if N_verify is None, then all nodes are verified). For each node node_i, check if its title appears at the corresponding physical page number position: result_i = f_verify(node_i, page_list). If result_i = "no", it is marked as an incorrect node. For incorrect nodes, the system re-searches for the starting page number corresponding to the node title based on the page number range [prev_correct, next_correct] of the previous and next correct nodes: physical_index_new = f_fix(node_incorrect, [prev_correct, next_correct]). The correction process is repeated a maximum of max_attempts = 3 times. 3. The tree search and retrieval system receives the user query query = "What is the content about risk assessment in the document?". The system analyzes the semantic intent of the query and identifies the key concepts: {"risk assessment", "content"}. System tree search hint: prompt_search = f"""Given a query and a document's tree structure, find all tree nodes that may contain the answer. Query: {query} Semantic knowledge tree structure: {Tree_structure} Please reply in the following JSON format:{{
[0227] "thinking": <The reasoning process regarding which nodes are relevant>
[0228] "node_list": [node_id1, node_id2, ...]}} where Tree_structure is the tree structure after removing detailed text content, containing only node IDs, titles, summaries, and child node relationships. The search results are obtained by calling the large language model:
[0229] search_result = f_tree_search(query, Tree_structure)
[0230] search_result = {{
[0231] "thinking": "For queries involving risk assessment, you should refer to relevant sections such as the risk management section and the risk analysis section."
[0232] "node_list": ["node_001", "node_005", "node_012"]
[0233] 4. Enhanced RAG System Response 4.1 Related Node Content Extraction The system establishes a node mapping based on the retrieved node ID list node_list = ["node_001", "node_005", "node_012"]: node_map = create_node_mapping(Tree)
[0234] node_map = {{
[0235] "node_001": {{
[0236] "node": node_info_001,
[0237] "text": "Detailed content of the Risk Management chapter...",
[0238] "start_index": 15,
[0239] "end_index": 28
[0240] }},
[0241] "node_005": {{
[0242] "node": node_info_005,
[0243] "text": "Detailed explanation of risk analysis methods...",
[0244] "start_index": 45,
[0245] "end_index": 52
[0246] }},
[0247] "node_012": {{
[0248] "node": node_info_012,
[0249] "text": "Introduction to risk assessment models...",
[0250] "start_index": 78,
[0251] "end_index": 85
[0252] }}
[0253] The system extracts and aggregates the text content of relevant nodes: `relevant_content = "\n\n".join(node_map[node_id]["text"] for node_id in node_list)` `relevant_content = "Detailed content of the risk management chapter...\n\nDetailed explanation of risk analysis methods...\n\nIntroduction to risk assessment models..."`
[0254] 4.2 The final output system formats the answers and citation information into the final output:
[0255] final_answer = {{
[0256] "answer": answer,
[0257] "citations": citations,
[0258] "source_nodes": ["node_001", "node_005", "node_012"]
[0259] The system returns a final_answer to the user, who can then trace the source of the answer through citations to verify its accuracy.
[0260] In summary, this invention can detect the content of table of contents pages in different documents. It parses documents using various document parsing libraries to obtain relevant document text information. For detected table of contents pages, the table of contents text content is extracted. During extraction, ellipses (...) in the table of contents are replaced with colons (:) to standardize the table of contents format. Simultaneously, it detects whether the table of contents contains page number information.
[0261] This invention features a document semantic knowledge tree construction mechanism. It can convert a flat list containing structure numbers into a tree structure. During the conversion process, parent-child node relationships are established based on the hierarchical relationship of the structure numbers (e.g., "1", "1.1", "1.2", etc.). It can also determine the start and end indices for each tree node. For nodes other than the last one, if the title of the next node appears at the beginning of the page, the end index of the current node is the start index of the next node minus 1; otherwise, the end index of the current node is equal to the start index of the next node. The end index of the last node is the total number of pages in the document.
[0262] This invention features a tree path search mechanism. Leveraging the reasoning capabilities of a large language model, it performs multi-step reasoning and path searching within a tree structure, dynamically locking down the logical node that best matches the query, thus establishing a correspondence between the user's query and the relevant directory page number, and enabling the retrieval of the content from the relevant page.
[0263] This invention also features an enhanced mechanism for accurate responses in the RAG system. By directly accessing query-related page numbers and constructing contextual hints, the large model can accurately answer the user's query.
[0264] The above embodiments are merely typical illustrative methods of the present invention, and the scope of protection of the present invention is not limited thereto. All equivalent substitutions and improvements made under the concept of the present invention should fall within the scope of protection. It should be emphasized that any modifications or minor adjustments made by those skilled in the art without departing from the basic principles of the present invention should also be considered within the scope of protection of the present invention.
Claims
1. A reasoning-enhanced RAG retrieval method based on semantic tree indexing, characterized in that, Specifically, the following steps are included: S1. In the target scenario, read and parse the long document into text; S2. The document text content obtained from step S1 is deeply parsed into structured data, its inherent hierarchical structure is extracted, and a semantic knowledge tree that retains the complete context and logical relationship is generated. Each tree node contains a content summary, precise page reference, and child node pointer. S3. Based on the semantic knowledge tree generated in step S2, the reasoning ability of the large language model is used to perform multi-step reasoning and path search in the tree structure, and dynamically lock and query the most matching logical node. S4. Based on the nodes retrieved in step S3, extract the node content and generate enhanced context, and then use a large language model to generate accurate and traceable answers.
2. The reasoning-enhanced RAG retrieval method based on semantic tree indexing according to claim 1, characterized in that, Step S1 includes: S11, DOC Class Document Parsing Read and parse text content from DOC and DOCX documents Specifically, for documents with the DOCX extension, a DOCX parsing library is used to extract the text content of each page; for documents with the DOC extension, LIBREOFFICE cross-platform office software is first used to convert them into DOCX documents, and then a DOCX parsing library is used to extract the text content of each page. S12, MARKDOWN Document Analysis Read and parse text content from Markdown and TXT documents. Specifically, it reads directly using Python's built-in `open` function. S13, PDF document parsing Read and parse text content from PDF documents Specifically, use PyPDF2 or PyMuPDF to read PDF files and extract the text content of each page.
3. The reasoning-enhanced RAG retrieval method based on semantic tree indexing according to claim 1, characterized in that, Step S2 includes: S21. Directory Detection and Extraction First, check if the document contains a table of contents; the specific method is as follows: S211, Directory Page Detection The system checks each page sequentially, starting from the first page of the document, using a large language model to determine if each page contains the table of contents. The detection process employs a fuzzy matching strategy, ignoring inconsistencies in whitespace. If consecutive table of contents pages are detected, the indexes of all table of contents pages are recorded. Let the total number of pages in the document be N, and the table of contents detection function be: ,in Indicates the first Page content: if ,but Marked as a directory page, the collection of directory pages: S212, Extracting Directory Contents For the detected directory pages, extract the directory text content; during the extraction process, replace the ellipses (...) in the directory with colons (:) to unify the directory format; at the same time, check whether the directory contains page number information; Let the extracted directory text be The function for extracting page number content is: S22, Directory JSONization The extracted directory text is converted into structured JSON format using a large language model; during the conversion process, a hierarchical structure number is assigned to each directory item; each directory item contains the following information: structure number, title, and page number; Let the set of directory items be Each directory item The structure is as follows: { "structure": , "title": , "page": } The hierarchical relationship of structure numbering is defined as follows: For structure numbering Its parent node structure number is Its hierarchical depth is depth(s) = the number of "." in s plus 1; S23, Page Number Index Extraction and Mapping S231, Handling of Tables of Contents with Page Numbers If the table of contents contains page number information, first extract the page numbers from the table of contents, then determine the actual physical page number position of each chapter in the document by matching the table of contents item titles with the document body content; calculate the offset between the table of contents page numbers and the physical page numbers, and apply this offset to all table of contents items to obtain an accurate physical page number index. Let the page numbers in the table of contents be The actual physical page numbers in the document are Then the page number offset The calculation formula is: Calculate the most common offset by matching multiple directory entries: Where count() represents the number of directory entries that meet the conditions; after obtaining the offset, the physical page number index of all directory entries is calculated as follows: S232, Handling Tables Without Page Numbers If the table of contents does not contain page number information, group the document content according to the maximum token limit, and then use a large language model to find the starting page number corresponding to each table of contents title in each group; add physical page number index information to each table of contents item. Let the total number of tokens for the document be... The maximum number of tokens is Then the number of document groups The calculation formula is: Average number of tokens per group The calculation formula is: Organize the document content according to Group the data and use a large language model in each group to find the physical page number index corresponding to the directory item title; S233, Supplementing Missing Page Numbers For cases where some table of contents entries may be missing page numbers, the page number range of the table of contents entry is determined based on the previous and next valid page numbers. Then, within this range, a large language model is used to find the starting page number corresponding to the title of the table of contents entry. Let the current directory entry be The preceding valid page number is The next valid page number is ,but The page number range is Use a large language model to search within this range. Physical page number index corresponding to the title ; S24. Tree structure generation without a directory When a document does not contain a table of contents, the tree structure is generated directly from the document content; the specific method is as follows: S241, Document Content Grouping The document pages are grouped according to the maximum number of tokens, and the content of each group does not exceed the preset number of tokens. A certain amount of page overlap is maintained when grouping to ensure that chapter boundaries are not cut off. Let the document page set be The number of tokens per page is The maximum number of tokens is The number of overlapping pages is The grouping algorithm is as follows: if Then all pages will be merged into one group; Otherwise, group according to the following rules: Initialize the number of tokens in the current group. ; For each page ,if Then start a new group, the new group starts from start; Otherwise, Join the current group; S242, Initial Tree Structure Generation The initial tree structure is extracted from the content of the first set of documents using a large language model, identifying chapter titles and hierarchical relationships, and assigning a structure number and physical page index to each chapter; let the content of the first set of documents be... The initial tree structure generation function is ,but: in This is the initial tree structure, containing the structure number, title, and physical page number index; S243, Tree Structure Expansion For subsequent document content groups, based on the existing tree structure, a large language model is used to continue extracting new chapter information, and the newly extracted chapters are added to the existing tree structure to form a complete semantic knowledge tree; Assume the existing tree structure is The current document content group is The tree structure expansion function is ,but: in For newly extracted chapter information, Indicates a merge operation; S25, Tree Structure Construction S251, Conversion from Flat List to Tree Structure Convert a flat list containing structure numbers into a tree structure; during the conversion process, establish parent-child node relationships based on the hierarchical relationship of the structure numbers. Let the structure number be Then its parent node structure number The calculation formula is: 1 For each node ,if If it exists in the node set, then it will Add as The child nodes; otherwise, It is the root node; S252, Node Boundary Determination For each tree node, determine the start and end indexes; for nodes that are not the last, if the title of the next node appears at the beginning of the page, the end index of the current node is the start index of the next node minus 1; otherwise, the end index of the current node is equal to the start index of the next node; the end index of the last node is the total number of pages in the document. Set nodes The starting index is The ending index is The next node is The indicator function for whether the next node title appears at the beginning of the page is: The total number of pages in the document is Then the index ends. for: If i < n-1 (not the last node): S26. Tree Structure Verification and Correction S261, Structural Verification The generated tree structure is verified by randomly selecting a certain number of nodes and checking whether the title of each node actually appears at the corresponding physical page number position; the verification process is carried out in a concurrent manner to improve verification efficiency. Let the total number of nodes in the tree structure be . The number of verification nodes is ,if Then all nodes are verified; the verification function is: ,but: For each Concurrent execution verification: Verification results ,in This indicates that the node title appears at the corresponding physical page number. This indicates that it did not appear; S262, Error Correction For nodes that fail to be verified, the starting page number corresponding to the node title is searched again within the page number range of the preceding and following correct nodes. The correction process is repeated multiple times until the maximum number of attempts is reached or all nodes pass verification. Let the node that failed verification be The physical page number of its preceding correct node is The physical page number of the next correct node is The correction function is The maximum number of attempts is Polling Each time, If the result is Then it ends; Corrected physical page number index In scope Inside.
4. The reasoning-enhanced RAG retrieval method based on semantic tree indexing according to claim 1, characterized in that, Step S3 includes: S31. Query understanding based on semantic knowledge tree It receives user queries and performs query understanding based on the generated semantic knowledge tree. The semantic knowledge tree is output by the scenario long document structuring module and contains the following information: Tree = { node_id: { "title":title_i, "start_index": start_i, "end_index": end_i, "summary": summary_i, "nodes": [child_nodes] }} First, we analyze the semantic intent of the query to identify key concepts, entities, and relationships in the query, providing guidance for subsequent tree search. S32, Tree Search Based on Large Language Model Using a large language model, inferential search is performed in the semantic knowledge tree to identify tree nodes that may contain the answer; the specific method is as follows: S321, Tree Search Hint Construction Construct a tree search suggestion (prompt_search) and input the user query and semantic knowledge tree into the large language model: prompt_search = f""" Given a query and a tree structure of documents, find all tree nodes that may contain the answer; Query: {query} Semantic knowledge tree structure: {Tree_structure} Please reply in the following JSON format: {{ "thinking": <the reasoning process related to which nodes>, "node_list": [node_id1, node_id2, ...] }} Tree_structure is the tree structure after removing detailed text content, containing only node ID, title, summary, and child node relationships; S322. Inferential Node Selection: The large language model infers based on query semantics and tree structure, outputting a list of relevant nodes; let the tree search function be f_tree_search(query, Tree_structure), then: search_result = f_tree_search(query, Tree_structure) search_result = {{ "thinking": reasoning_text, "node_list": [node_id1, node_id2, ..., node_idk] }} Based on the inference results, the system extracts a set of relevant node IDs: relevant_nodes = {{node_id | node_id∈ search_result["node_list"]}} S33, Expert Knowledge Integration and Preference Alignment It supports the integration of expert knowledge or user preferences, eliminating the need for model fine-tuning; simply add relevant knowledge to the tree search suggestions. S331, Preference Knowledge Retrieval When a query is received, the most relevant user preferences or expert knowledge fragments are selected from the knowledge base or domain-specific rules; let the preference retrieval function be f_preference_retrieve(query), then: Preference = f_preference_retrieve(query) Preference is obtained through keyword matching, semantic similarity, or relevance search based on a large language model; S332, Enhanced Tree Search Integrate preference knowledge into tree search suggestions to build enhanced tree search suggestions: prompt_enhanced = f""" Given a query and a tree structure of documents, find all tree nodes that may contain the answer; Query: {query} Semantic knowledge tree structure: {Tree_structure} Related expert knowledge: {Preference} Please reply in the following JSON format: {{"thinking": <reasoning process related to which nodes>, "node_list": [node_id1, node_id2, ...]} }} The enhanced tree search function is: search_result_enhanced = f_tree_search(query, Tree_structure, Preference) By integrating expert knowledge, node search becomes more accurate and effective, while leveraging document structure and domain-specific insights.
5. The reasoning-enhanced RAG retrieval method based on semantic tree indexing according to claim 1, characterized in that, Step S4 includes: S41. Extraction of relevant node content Based on the list of relevant node IDs retrieved by the tree search and retrieval module, extract the detailed content of the corresponding node from the semantic knowledge tree; S411. Node content mapping establishes a mapping relationship from node ID to node content; let the node mapping function be create_node_mapping(Tree), then: node_map = create_node_mapping(Tree) node_map = {{ node_id: {{ "node": node_info, "text": text_content, "start_index": start_page, "end_index": end_page }}}} S412, Content Aggregation Based on the retrieved node ID list node_list, extract and aggregate the text content of related nodes; let the content extraction function be extract_content(node_list, node_map), then: relevant_content = extract_content(node_list, node_map) relevant_content = "\n\n".join(node_map[node_id]["text"] for node_id innode_list) The "\n\n" is used to separate the content of different nodes, maintaining the readability and structure of the content; S413, Determining the Page Range Determine the page range corresponding to the retrieved node for answer tracing; let the page range of node_i be page_range_i, then: page_range_i = {{ start_page: node_map[node_id]["start_index"],end_page: node_map[node_id]["end_index"] }} If start_page = end_page, then the page range is a single page number; otherwise, it is a range of page numbers. S42, Context Enhancement Construction Based on the extracted relevant content, an enhanced context is constructed to provide sufficient background information for the large language model to generate answers; Construct a structured context containing the following information: context = {{ "query": query, "relevant_nodes": node_list, "content": relevant_content, "page_references": page_ranges, "tree_structure": Tree_structure }} S43. Answer generation based on enhanced context Use a large language model to generate answers based on enhanced context; S431. The answer generation prompt system constructs the prompt_answer: prompt_answer = f""" Answer the user's question based on the following context information: User query: {query} Related context: {optimized_content} Page references: {page_references} Please generate an accurate and complete answer based on the above context; if the context does not contain enough information to answer the question, please state this clearly. The page_references section contains page reference information, formatted as "node title: page range"; S432, Answer Generation The large language model is used to generate answers based on prompts; let the answer generation function be f_generate_answer(prompt_answer), then: answer = f_generate_answer(prompt_answer). The generated answer answer includes: a direct answer to the user query, a detailed explanation based on context, traceable page citation information, and a system that generates citation information for each key information point in the answer; let the citation generation function be generate_citations(answer, node_map), then: citations = generate_citations(answer, node_map). citations = [ {{ "text": cited_text, "node_id": node_id, "page_range": page_range, "title": node_title The system formats the answer and citation information into the final output; let the final output be final_answer, then: final_answer = {{ "answer": answer, "citations": citations, "source_nodes": node_list, "confidence": confidence_score The formatted answers above make it easier for users to verify the accuracy of the answers and trace their source.
6. A reasoning-enhanced RAG retrieval system based on semantic tree indexing, characterized in that, include: Scene document reading and parsing module: Reads and parses long documents into text in the target scene, preparing for subsequent document structuring into a semantic knowledge tree; The long document structuring module for scenarios: It performs deep parsing of the document text content obtained from the scenario document reading and parsing module into structured data, extracts its inherent hierarchical structure, and generates a semantic knowledge tree that preserves the complete context and logical relationships. Each tree node contains a content summary, precise page references, and child node pointers. Tree Search and Retrieval Module: Based on the semantic knowledge tree generated by the long document structuring module, it uses the reasoning capabilities of a large language model to perform multi-step reasoning and path search within the tree structure, dynamically locking and querying the most matching logical node. The RAG system's enhanced answer module extracts node content and generates enhanced context based on nodes retrieved by the tree search and retrieval module. Then, it uses a large language model to generate accurate and traceable answers.
Citation Information
Cited By
Artificial intelligence-based document knowledge tree generation and interactive exploration method and system
CN122152825A