Enhanced retrieval generation method and device based on document structure knowledge graph
The method constructs a knowledge graph from document structure to enhance generation-style language models, addressing real-time responsiveness and traceability issues, ensuring accurate and complete answers.
Patent Information
- Application Number
- CN202510239236.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-03
- Publication Date
- 2025-07-15
AI Technical Summary
The existing generative language models have shortcomings in real-time, knowledge update speed, traceability and reasoning capabilities, especially when dealing with private domain knowledge and complex tasks, it is difficult to meet the data integrity and accuracy requirements in finance, medical and other fields.
By analyzing the document, building a document structure knowledge graph, preserving the hierarchical relationship and information integrity of the document, generating answers using preset language models, and combining graph databases and text content embedding processing, improving the reasoning ability and traceability of answers.
It improves the real-time response ability, knowledge update speed and answer accuracy of the generative language model, ensures the traceability and integrity of the generated results, and is suitable for scenarios such as finance and medical care that require high data integrity.
Smart Images

Figure CN120317348A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the technical field of generative language models, and particularly to an enhanced retrieval generation method and device based on a document structure knowledge graph. Background Art
[0002] With the development of generative language models (such as the GPT series), they have demonstrated powerful language generation capabilities in the field of natural language processing. However, these models still have multiple technical problems. First, generative language models mainly rely on large-scale prediction data training and generate answers based on probability distributions, resulting in insufficient real-time performance and being unable to quickly respond to the dynamic needs of users. In addition, the model knowledge update speed is slow, unable to reflect the latest information in a timely manner, and lacks traceability of the generation process, making it difficult to verify the source and accuracy of the generated content. Moreover, generative language models also show certain deficiencies in processing private domain knowledge, especially in the application of industry-specific knowledge.
[0003] To address these problems, the Retrieval-Augmented Generation (RAG) method has been proposed. RAG combines retrieval and generation technologies to improve the performance of generative AI models. However, traditional RAG methods still have some drawbacks. For example, GraphRAG is a currently relatively close technology that combines a graph database with the RAG method and has achieved good results, but it still faces the following problems in some complex tasks:
[0004] Loss of document information: After converting document content into knowledge graph triples, GraphRAG loses some detailed information in the document. For business scenarios with high requirements for data integrity (such as finance, healthcare, contract approval, etc.), the lack of information may lead to decision-making errors and serious consequences.
[0005] Lack of document structure information: In the construction of the knowledge graph based on GraphRAG, the document content is sliced and split into multiple entities and relationships, resulting in the inability to completely retain the hierarchical structure and context information of the original document. This problem makes it difficult to trace the generated answers and verify whether the answers are correct.
[0006] Insufficient reasoning ability: Traditional language models show great limitations in reasoning and integrating multi-source knowledge, lack the ability to reason about complex logic, and are unable to efficiently integrate information from different sources and generate accurate answers. Summary of the Invention
[0007] In view of this, embodiments of this application provide an enhanced retrieval generation method and device based on a document structure knowledge graph to solve the problems in the prior art of insufficient reasoning ability, inability to fully trace the generation results, and inability to verify whether the answers are correct.
[0008] In the first aspect of the embodiments of the present application, an enhanced retrieval and generation method based on a document structure knowledge graph is provided, including: parsing a document to obtain document parsing content, where the document parsing content includes basic information and structured information of the document; constructing a document entity and a text fragment entity according to the document parsing content, and establishing an association relationship between the document entity and the text fragment entity, as well as between text fragment entities; constructing a knowledge graph of the document structure according to the association relationship between the document entity and the text fragment entity, as well as between text fragment entities; storing the knowledge graph in a graph database, and performing text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content; in response to a user's query request, retrieving text fragment entities related to the query request from the knowledge graph, and generating a corresponding query result according to the text content of the retrieved text fragment entities; using a preset language model to process the query result to generate an answer corresponding to the query request.
[0009] In the second aspect of the embodiments of the present application, an enhanced retrieval and generation device based on a document structure knowledge graph is provided, including: a parsing module for parsing a document to obtain document parsing content, where the document parsing content includes basic information and structured information of the document; a first construction module for constructing a document entity and a text fragment entity according to the document parsing content, and establishing an association relationship between the document entity and the text fragment entity, as well as between text fragment entities; a second construction module for constructing a knowledge graph of the document structure according to the association relationship between the document entity and the text fragment entity, as well as between text fragment entities; a processing module for storing the knowledge graph in a graph database, and performing text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content; a retrieval module for retrieving text fragment entities related to the query request from the knowledge graph in response to a user's query request, and generating a corresponding query result according to the text content of the retrieved text fragment entities; a generation module for using a preset language model to process the query result to generate an answer corresponding to the query request.
[0010] In the third aspect of the embodiments of the present application, an electronic device is provided, including a memory, a processor, and a computer program stored on the memory and executable on the processor, where the processor implements the steps of the above method when executing the computer program.
[0011] In the fourth aspect of the embodiments of the present application, a computer-readable storage medium is provided, where the computer-readable storage medium stores a computer program, and the computer program implements the steps of the above method when executed by a processor.
[0012] The above at least one technical solution adopted in the embodiments of the present application can achieve the following beneficial effects:
[0013] The document parsing content is obtained by parsing the document, where the document parsing content includes the basic information and structured information of the document; according to the document parsing content, document entities and text fragment entities are constructed, and the association relationships between the document entities and the text fragment entities, as well as between the text fragment entities, are established; according to the association relationships between the document entities and the text fragment entities, as well as between the text fragment entities, a knowledge graph of the document structure is constructed; the knowledge graph is stored in a graph database, and the text content of the text fragment entities in the knowledge graph is embedded to generate a vector representation of the text content; in response to a user's query request, text fragment entities related to the query request are retrieved from the knowledge graph, and corresponding query results are generated according to the text content of the retrieved text fragment entities; a preset language model is used to process the query results to generate an answer corresponding to the query request. This application can improve the reasoning ability, improve the traceability of the generated results and the accuracy of the answers. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following will briefly introduce the accompanying drawings required for use in the embodiments or the description of the prior art. Obviously, the accompanying drawings in the following description are only some embodiments of the present application. For those of ordinary skill in the art, without creative efforts, other accompanying drawings can also be obtained based on these drawings.
[0015] Figure 1 is a schematic diagram of the overall implementation process of the RAG method for extracting a knowledge graph based on the document structure provided by the embodiments of the present application;
[0016] Figure 2 is a schematic diagram of the process of the enhanced retrieval and generation method based on the document structure knowledge graph provided by the embodiments of the present application;
[0017] Figure 3 is a schematic diagram of the structure of the enhanced retrieval and generation device based on the document structure knowledge graph provided by the embodiments of the present application;
[0018] Figure 4 is a schematic diagram of the structure of the electronic device provided by the embodiments of the present application. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0019] In the following description, specific details such as specific system structures and technologies are presented for the purpose of illustration rather than limitation, so as to thoroughly understand the embodiments of the present application. However, those skilled in the art should clearly understand that the present application can also be implemented in other embodiments without these specific details. In other cases, the detailed descriptions of well-known systems, devices, circuits, and methods are omitted to avoid unnecessary details from interfering with the description of the present application.
[0020] Generative language models are based on large-scale prediction training and rely on probability distribution. They have problems such as insufficient real-time performance, slow knowledge update, lack of traceability, and lack of private domain knowledge. Traditional RAG (Retrieval-Augmented Generation) methods have problems such as insufficient reasoning ability, lack of factuality and traceability.
[0021] The RAG (Retrieval-Augmented Generation) technology framework based on knowledge graph is an innovative method to enhance the factuality, interpretability and knowledge depth of generative artificial intelligence (such as the GPT series of models). The knowledge graph is structured, and highly factual data can provide a high-quality source of knowledge. At the same time, the graph structure and triple storage can improve the efficiency of knowledge retrieval and enhance the generation ability of language models.
[0022] In the existing technology, GraphRAG is an innovative knowledge retrieval and question-answering enhancement framework that cleverly combines graph database technology with the retrieval enhancement generation (RAG) method. GraphRAG often achieves better results than traditional RAG in processing complex data relationship tasks and is one of the hot engineering directions in the current LLM field.
[0023] Although the traditional RAG method has achieved good results in generative AI, it still has the following problems:
[0024] Loss of document information: After GraphRag extracts document content into knowledge graph triples, it actually only retains the relevant knowledge information and loses some content details of the original text. In business scenarios with strict requirements on data integrity, such as finance, medical care, company rules and regulations, contract approval, etc., the loss of information has a relatively large impact.
[0025] Missing document structure information: Knowledge graph retrieval based on GraphRag is based on token length to slice documents, split entities and relationships, build knowledge graphs, and then perform rag. This method does not retain the data information of the document layer and the original document structure, cannot fully trace the rag results, and it is difficult to verify whether the source of the generated results is correct.
[0026] Insufficient reasoning ability: Language models lack the ability to integrate knowledge through logical reasoning.
[0027] In view of the problems existing in the prior art, this application proposes a RAG method based on extracting a knowledge graph from a document structure. The knowledge graph is constructed according to the document structure, the hierarchical relationship of the document is retained, the integrity and accuracy of the knowledge representation are maintained, and the complete document traceability and credibility of the answer information are improved. The overall implementation process of the RAG method based on extracting a knowledge graph from a document structure in this application will be summarized below with reference to the accompanying drawings. Figure 1 It is a schematic diagram of the overall implementation process of the RAG method based on extracting a knowledge graph from a document structure provided by an embodiment of this application. As Figure 1 shown, the RAG method based on extracting a knowledge graph from a document structure may specifically include the following contents:
[0028] 1. Document parsing module: Use regular expressions to extract information such as the document author, publication time, main title, subtitle, and text content.
[0029] 2. Knowledge graph construction module based on document structure:
[0030] a) Construct entities with the document tag.
[0031] b) Construct entities with the chunk tag.
[0032] c) Construct the containment relationship between the document and the chunk and construct the front-back dependency relationship between the chunks.
[0033] d) Use the Cypher language to construct a graph
[0034] e) Store the constructed knowledge graph in the Neo4j graph database.
[0035] 3. Embedding of graph content: Embed the text attribute content of the chunk entity.
[0036] 4. Construct the RAG process: Use the Langchain architecture to construct a complete RAG retrieval and generation link based on the user query, prompt, graph retrieval statement retriever, and LLM model.
[0037] 5. Optimize the RAG effect: Optimize the prompt and graph retrieval statements according to the question and the generated answer, and then optimize the retrieval and generation effect.
[0038] 6. Package the RAG Q&A service: Use Flask and Gradio to package the RAG service.
[0039] The content of the technical solution of this application will be described in detail below with reference to the accompanying drawings and specific embodiments.
[0040] Figure 2It is a schematic flowchart of an enhanced retrieval generation method based on a document structure knowledge graph provided by an embodiment of the present application. As Figure 2 shown, the enhanced retrieval generation method based on the document structure knowledge graph may specifically include:
[0041] S201, parsing the document to obtain document parsing content, where the document parsing content includes basic information and structured information of the document;
[0042] S202, constructing document entities and text fragment entities according to the document parsing content, and establishing association relationships between the document entities and the text fragment entities, as well as between the text fragment entities;
[0043] S203, constructing a knowledge graph of the document structure according to the association relationships between the document entities and the text fragment entities, as well as between the text fragment entities;
[0044] S204, storing the knowledge graph in a graph database, and performing text content embedding processing on the text fragment entities in the knowledge graph to generate vector representations of the text content;
[0045] S205, in response to a user's query request, retrieving text fragment entities related to the query request from the knowledge graph, and generating corresponding query results according to the text content of the retrieved text fragment entities;
[0046] S206, using a preset language model to process the query results to generate answers corresponding to the query requests.
[0047] In some embodiments, parsing the document to obtain document parsing content includes:
[0048] Using a program to read the document content and convert the document content into a unified string format; writing a regular expression according to the document content to extract the basic information in the document; organizing the extracted basic information according to the structure of the document and converting it into a structured data format.
[0049] Specifically, in this embodiment, a Python program is used to process the document file. Different formats of documents (such as PDF, Word, TXT, etc.) need to be read through different processing methods. First, the program reads the document content as a text string by adapting the corresponding libraries (such as using PyPDF2 to read PDF documents, python-docx to read Word documents, or directly reading TXT files). The read document content includes all the text information in the document but removes the format information, such as fonts, colors, layout, etc.
[0050] Extract basic information from the document: Next, use regular expressions to extract key information from the document. First, the program will locate specific fields according to the document format and extract the following basic information through regular expressions:
[0051] Document author: The author of the document can usually be extracted by matching keywords (such as "author", "author name") that appear in the document header or cover.
[0052] Publication time: Publication information is generally located at the beginning or cover of the document. The specific date or time can be extracted by using regular expressions to match keywords such as "publication time", "date", etc.
[0053] Title and subheadings: The title is usually located at the top or a specific position of the document, and the subheadings are extracted according to the hierarchical structure of the document. Usually, they are distinguished according to the format (such as bold, different font sizes, etc.). Regular expressions can be written to identify headings at different levels.
[0054] Extract document content and body: The body content in the document is usually associated with the title and subheadings. Each title usually contains the corresponding body paragraphs. In this embodiment, the body content is extracted through regular expressions. The body content includes the detailed descriptions or texts under each title. The text content is grouped by title and the corresponding relationship with the title is retained. The program will separate the content of each part by detecting the markers of the title (such as specific fonts or positions) so as to match the title with its corresponding body text.
[0055] Organization and storage of structured data: All the key information extracted (such as document title, author, publication time, subheadings, body content, etc.) is organized according to the hierarchical structure of the document and converted into a structured data format. For example, the JSON format is very suitable for storing this kind of structured data, where the main information of the document (such as title, author, etc.) is used as the primary key, and the subheadings and the corresponding content are stored as lists or nested objects. The structured data format can facilitate subsequent processing and transmission, such as for constructing a knowledge graph or storing in a graph database.
[0056] This way of storing structured data can ensure the integrity of information and provide a solid data foundation for subsequent knowledge graph construction. Through this method, the information contained in the document is not only extracted, but also organized according to the hierarchical relationship and logical association, which is convenient for subsequent query and analysis.
[0057] This document parsing method is applicable not only to simple text documents, but also can be extended to complex format reports, contracts, policy documents, etc. It is particularly significant for scenarios that require in-depth analysis, classification, or generation of Q&A for document content. By structuring the document, the system can effectively retrieve key information in the document, provide content-based automatic Q&A services, and be able to provide high-quality and traceable answers during the generation process.
[0058] In some embodiments, according to the document parsing content, document entities and text fragment entities are constructed, including:
[0059] According to the document parsing content, a unique identifier and related information corresponding to the document are generated, and the unique identifier and related information corresponding to the document are converted into a first data file to construct a document entity;
[0060] According to the document parsing content, the information related to the text content and text fragments in the document is extracted by a program, and the extracted information related to the text content and text fragments is converted into a second data file to construct a text fragment entity.
[0061] Specifically, in this embodiment, the process of constructing document entities and text fragment entities is carried out according to the document parsing content. First, by parsing the basic information, title, sub-title, and body content in the document, the document content is structured and identified, so as to generate a data file suitable for constructing a knowledge graph. This process includes the following steps:
[0062] 1) Construct document entity data (document entity):
[0063] Generate a document unique identifier: According to the content of document parsing, the program generates a unique identifier doc_id for each document. This identifier is usually automatically generated by the system to ensure the uniqueness of each document in the database.
[0064] Document related information: Based on the document parsing content, the program extracts the basic information of the document from the document, such as document name, author, release time, etc. These information are the core content of the document entity and must be accurately extracted and associated with the document entity.
[0065] Generate a JSON file of the document entity: Organize the unique identifier and related information of the document (such as document name, author, release time, etc.) into a structured JSON format file. The structure of the JSON file includes the main information of the document and the key information fields of each document, which is convenient for subsequent storage and processing.
[0066] 2) Construct text fragment entity data (Chunk entity):
[0067] Unique identifier for generating text fragments: Based on the parsed content of the document, the program generates a unique identifier chunk_id for each text fragment (such as each title and its corresponding body text) to distinguish different text fragments.
[0068] Text title and content extraction: The program extracts the titles (such as sub-titles) and corresponding text content in the document. Each title is usually associated with a certain range of body text. The program extracts the body text through the hierarchical structure of the document and corresponds it to the title one by one.
[0069] Location information of text fragments: During the process of extracting text fragments, the program also records the location of each text fragment (such as the starting position and ending position of the text fragment in the document). This location information helps with the subsequent tracing and verification of the document content.
[0070] Parent title location: In the case of having sub-titles, the program records the location of the parent title corresponding to each text fragment (that is, which title this text fragment belongs to) for the complete reconstruction of the document hierarchical structure.
[0071] Generating a JSON file for text fragment entities: Based on the above-extracted text titles, text content, locations, and parent title information, the program organizes them into a JSON file to ensure that each text fragment entity can accurately describe the document structure.
[0072] Furthermore, the document entity and the text fragment entity are associated through the relationship between doc_id and chunk_id. The doc_id in the document entity and the chunk_id in the text fragment entity can establish the connection between the document and the multiple text fragments it contains.
[0073] For example, a document may contain multiple titles, and there are relevant text fragments under each title. The program ensures the integrity of the document hierarchical structure by establishing the association between doc_id and chunk_id.
[0074] Furthermore, the generated JSON file above will be stored in the database for subsequent retrieval and generation of the document and text fragments.
[0075] These structured data (document entities and text fragment entities) will provide basic support for the subsequent construction of a knowledge graph based on the document content, ensuring that the generated knowledge graph can reflect the actual content and structure of the document.
[0076] Through the methods of the above embodiments, this embodiment has successfully extracted the basic information and structured content of the document, and constructed document entities and text fragment entities according to the hierarchical structure of the document. The generated structured data not only provides important basic data for the construction of the knowledge graph, but also improves the efficiency of subsequent queries and processing, ensuring that the document content has a clear organization and traceability throughout the process.
[0077] In some embodiments, establishing the association relationships between document entities and text fragment entities, and between text fragment entities, includes:
[0078] According to the document hierarchy in the document parsing content, establishing the inclusion relationship between the document entity and the text fragment entity, and establishing the front-back dependency relationship between text fragment entities, and storing the inclusion relationship and the front-back dependency relationship in a third data file.
[0079] Specifically, this embodiment aims to establish the association relationship between the document entity and the text fragment entity by parsing the document content, and construct a complete document structure through this association. In this process, by analyzing the hierarchical structure of the document, establishing the inclusion relationship between document levels and the front-back dependency relationship between text fragments, and storing these relationships in a structured JSON file. In addition, a graph database (such as Neo4j) is used to store the relationship between the document structure and entities, thus laying a foundation for the subsequent construction of the knowledge graph.
[0080] First, according to the hierarchical structure in the document parsing content, the program will identify the main headings, subheadings and related body paragraphs in the document. In the document, there is a certain hierarchical structure relationship between the headings and the body paragraphs. There may be multiple subheadings under a heading, and the body content may correspond to the subheadings. Therefore, we need to establish the inclusion relationship between the document entity and the text fragment entity.
[0081] Furthermore, in the hierarchical structure of text fragment entities, there is also a front-back dependency relationship between each text fragment (such as subheadings and body paragraphs). The front-back dependency relationship reflects the order between text fragments. For example, the heading of "Income Situation" is usually located before the heading of "Cost Analysis", and the body content between them also has an order.
[0082] Through document parsing, the program will generate a unique identifier (chunk_id) for each text fragment and record the order of each fragment in the document. For example, the chunk_id of the heading of "Income Situation" will point to the next fragment (such as the heading of "Cost Analysis") to form a front-back dependency relationship. The dependency relationship can be represented by previous_chunk_id and next_chunk_id to ensure that the order of text fragments in the document is correctly preserved.
[0083] Furthermore, the relationship data (such as inclusion relationships and sequential dependencies) between all the extracted document structures and text fragments will be organized and stored as a JSON file. This file will contain the following content:
[0084] Basic information of each document entity, such as the document title, document unique identifier, etc.
[0085] The title, body content, unique identifier of each text fragment entity (chunk), and its position in the document.
[0086] The inclusion relationship between the document entity and the text fragment entity.
[0087] The sequential dependencies between text fragment entities.
[0088] Furthermore, based on the extracted JSON file, graph relationships in the Neo4j graph database are constructed through Cypher language. Specifically:
[0089] Construct document entities: Create document nodes using doc_id and document_title, and save the basic information of the document (such as title, author, etc.) in the document nodes.
[0090] Construct text fragment entities: Create a text fragment node for each chunk_id, and store the title, content, and sequential dependencies as attributes of the node.
[0091] Establish the inclusion relationship between the document and the text fragment: According to the hierarchical structure parsed from the document, establish the inclusion relationship between the document node and the text fragment node through the CREATE command in Neo4j.
[0092] Establish the sequential dependencies between text fragments: Establish the sequential dependencies between text fragment nodes through the previous_chunk_id and next_chunk_id attributes.
[0093] These nodes and relationships ultimately form a graph structure that can efficiently store the hierarchical structure of the document and facilitate subsequent queries and generation.
[0094] Finally, through the storage of the Neo4j graph database, the document and text fragment entities and their relationships are effectively managed. Users can quickly retrieve the document structure through the graph database query interface and utilize the efficient query capabilities of the graph for in-depth analysis or generation tasks.
[0095] In this embodiment, by establishing the inclusion relationship between document entities and text fragment entities and the sequential dependency relationship between text fragments, a structured storage and query method for document content is provided. By storing these relationships in the Neo4j graph database, not only the hierarchical structure and relevance of the document content are ensured, but also strong data support is provided for subsequent knowledge graph construction and document-based intelligent question answering.
[0096] In some embodiments, text content embedding processing is performed on the text fragment entities in the knowledge graph to generate a vector representation of the text content, including:
[0097] Process the text fragment entities in the knowledge graph to extract the text content in the text fragment entities;
[0098] Use a model to perform embedding processing on the text content in the extracted text fragment entities to convert the text content into a high-dimensional vector representation;
[0099] Associate the high-dimensional vector representation with the text fragment entities and construct a graph index of the text content, and store the graph index in the graph database.
[0100] Specifically, the purpose of this embodiment is to perform text content embedding processing on the text fragment entities in the knowledge graph, convert the text content into a high-dimensional vector representation, and perform storage and index construction through the graph database, so as to improve the retrieval and generation efficiency of text fragments. The specific steps are as follows:
[0101] First, extract the text content in the text fragment entities (chunks) from the already constructed knowledge graph. This text content usually includes title and body information and may contain hundreds to thousands of characters. For each text fragment entity, the program extracts corresponding attributes such as chunk_id, title, content, etc. from the graph database, and in particular, the content (text content) needs to be extracted as the input for subsequent embedding processing.
[0102] Furthermore, after the text content is extracted, a deep learning model is then used to perform embedding processing on the text. Specifically, the text_embedding_v3 model is used to embed the content of each text fragment entity, converting it into a high-dimensional vector representation. This model captures the semantic information of the text, converts it into a fixed-length vector, and can retain the semantic features of the text content. This embedding process can be completed through the Neo4jVector class in langchain_community.vectorstores.
[0103] The text_embedding_v3 model is used to generate high-dimensional vectors of text, which not only reflect the surface vocabulary of the text but also capture the semantic and contextual information therein.
[0104] By calling the Neo4jVector.from_existing_graph method, the program can associate the embedding vectors of text fragments with the text fragment entities.
[0105] Furthermore, after the embedding process is completed, the program associates the generated high-dimensional vector representation with the corresponding text fragment entity (chunk). Each text fragment entity stores a corresponding high-dimensional vector, which is the result of the model embedding process. In this way, each text fragment entity in the graph database now not only contains the original text content but also is associated with its corresponding vector representation, making subsequent text retrieval and generation more efficient.
[0106] In addition, these embedding vectors are stored as part of the graph database nodes for quick access during similarity retrieval.
[0107] Furthermore, to optimize query efficiency and the retrieval of text fragment entities, the program constructs a graph index of the text content in the Neo4j graph database. Through the Neo4jVector class, the program generates a graph index based on the embedding vectors of each text fragment. This index enables efficient retrieval based on the semantics of the text during subsequent queries, rather than relying solely on keyword matching.
[0108] The construction of the graph index generally includes the following aspects:
[0109] Store the embedding vectors as node attributes to ensure that each text fragment entity can quickly access its corresponding vector.
[0110] Based on the similarity relationship between the embedding vectors, construct an index between text fragments for vector-based similarity retrieval.
[0111] Furthermore, after completing the text content embedding and graph index construction, the program stores the generated graph index in the Neo4j graph database. The graph database can efficiently store these vectors and support graph queries based on similarity. Through the graph database, users can perform fast similarity queries to obtain text fragment entities similar to the input query text.
[0112] Furthermore, through the embedding processing of text content and the construction of graph indexes, this embodiment can significantly improve the query efficiency and accuracy of document content. Since text fragments have been transformed into high-dimensional vectors and optimized through graph indexes, the system can perform fast retrieval based on semantic similarity. This approach is particularly suitable for scenarios involving processing large amounts of document content, such as intelligent question-and-answer systems, knowledge graph construction, information extraction, and other application fields.
[0113] In addition, since the embedded vectors contain the semantic information of the text, the system can achieve more intelligent query generation, overcome the limitations of traditional keyword-matching-based retrieval methods, and improve the system's question-and-answer quality and user experience.
[0114] In this embodiment, through the text embedding processing of text fragment entities in the knowledge graph, high-dimensional vector representations of the text are generated, and these vectors are associated with the text fragment entities, thereby constructing a graph index of the text content and storing it in the Neo4j graph database. This process improves the retrieval efficiency of text fragments and enhances the query ability based on semantic similarity, providing strong data support for subsequent applications such as knowledge graph construction and intelligent question-and-answer.
[0115] In some embodiments, in response to a user's query request, text fragment entities related to the query request are retrieved from the knowledge graph, and corresponding query results are generated based on the text content of the retrieved text fragment entities, including:
[0116] Determine the user's question based on the query request, and use the query statement in the graph database to retrieve relevant text fragment entities from the knowledge graph according to the user's question;
[0117] Generate a corresponding graph retrieval statement based on the retrieved text fragment entities and associated path information, and use the graph retrieval statement to perform graph information retrieval;
[0118] According to the retrieval query statement and a predetermined similarity score threshold between the user's question and the graph content, use the retriever to screen the information in the graph database and extract text fragment entities relevant to the query request;
[0119] Use a preset language model to process the text fragment entities relevant to the query request that are extracted to generate query results;
[0120] Combine the query request, the retriever, the query results, and the language model to form a complete question-and-answer link, and use the question-and-answer link to output the answer corresponding to the query request.
[0121] Specifically, this embodiment describes how to retrieve relevant text fragment entities from a knowledge graph when responding to a user's query request and generate a query result based on the retrieved text content. This process combines the query capabilities of a graph database and the generation capabilities of a language model to achieve intelligent question answering through a question-and-answer link. The specific steps are as follows:
[0122] After receiving the user's query request, it is first necessary to analyze the content of the user's question. The user's question may involve specific information in the document, and the system will perform natural language processing (NLP) on the question to identify keywords, entities, and context. The system determines the type of information the user needs by analyzing the question and prepares for subsequent queries and retrievals.
[0123] For example, when the user queries "What is the revenue situation in 2024?", the system will analyze the question and extract "revenue situation" and "2024" as the keywords for retrieval.
[0124] Furthermore, based on the user's question, the system will use query statements in the graph database (such as Cypher statements) to retrieve relevant text fragment entities (chunks) from the knowledge graph. The graph database identifies the parts relevant to the user's question by querying the information in the text fragment entities.
[0125] In this embodiment, the system uses the Neo4jVector.from_existing_index method to execute the query. Specifically, the system generates a graph retrieval statement based on the user's query keywords (such as "revenue situation" and "2024"). Then, the system uses Cypher statements to find the relevant Chunk entities for the query, and based on the chunk_id, finds all the leaf nodes downward, and finally finds back to the Document node through path finding.
[0126] This query process not only considers the content of the text fragments but also, through the relational structure of the graph, ensures that the returned results are the parts of the document highly relevant to the question.
[0127] Furthermore, once the retrieval statement is generated and executed, the system returns multiple relevant text fragment entities from the graph database. At this time, the system uses the graph retrieval statement and the similarity score related to the user's query to filter the most relevant text fragments.
[0128] In this embodiment, the query results generated by the Neo4jVector.from_existing_index method will be compared with the set similarity threshold. The system default sets similarity_score_threshold to 0.6, which means that only text fragment entities with a similarity higher than 0.6 will be selected as query results. This screening mechanism ensures the relevance of the returned text fragment entities to the user's question.
[0129] Further, after screening out the text fragment entities most relevant to the query, the system passes these entities along with their content to a preset language model (such as qwen-plus) for processing. The language model will generate the final query answer based on the retrieved text content.
[0130] For example, assume the retrieved text fragment entities contain "The revenue in 2024 increased by 15%". The language model will generate a more complete and natural answer based on this information, such as "According to the 2024 financial report, the revenue increased by 15%, mainly from new market expansion."
[0131] Finally, to automate the entire query process and form a complete Q&A chain, the system uses the langchain framework to build a RAG (Retrieval-Augmented Generation) Q&A link. Specifically, the system combines the following elements through the create_stuff_documents_chain and create_retrieval_chain methods:
[0132] User query (user_query)
[0133] Generated query prompt (prompt)
[0134] Graph retriever (retrieval_query)
[0135] Screened relevant text fragment entities
[0136] Preset language model (such as qwen-plus)
[0137] Through these elements, the system builds a complete RAG Q&A link that can, when receiving a query, retrieve relevant text fragments through the graph database and then generate high-quality answers through the language model.
[0138] Furthermore, through the complete RAG link, the system will generate and return the final answer based on the user's query request. The query result is based on the semantic retrieval and language model generation capabilities of the graph database, ensuring the accuracy and semantic coherence of the answer. Users can get timely and accurate answers through the interface.
[0139] This embodiment realizes an intelligent question-answering system based on knowledge graph by combining the query capability of graph database with the generation capability of language model. The system goes through the following steps: analyzing and determining user questions; retrieving related text fragment entities using graph database; filtering related results based on similarity; generating query results using preset language model; combining all components to build a complete question-answering chain and provide accurate answers. This method enables the question-answering system to provide accurate and real-time answers in scenarios with large-scale documents and complex queries, and is suitable for various automated knowledge retrieval and question-answering generation tasks.
[0140] In some embodiments, the method further comprises:
[0141] According to the query request and the answer corresponding to the query request generated by the language model, the input prompt of the language model and the graph retrieval statement are iteratively optimized.
[0142] Specifically, this embodiment describes how to iteratively optimize the input prompts and graph retrieval statements of the language model based on the user's query request and the answer generated by the language model to continuously improve the effect of the question-answering system. Through the feedback mechanism, the optimization process can improve the accuracy and response quality of the system, thereby providing higher quality query answers in practical applications.
[0143] First, the user makes a query request, and the system retrieves relevant text fragment entities from the knowledge graph based on the query request, and generates preliminary query results using a preset language model (such as qwen-plus). In this process, the system generates input prompts for the language model based on the document structure and relevant text fragments, and extracts relevant information from the graph database through graph retrieval statements.
[0144] In the initial stage, the query results generated by the system may be able to answer the user's question, but due to some reasons (such as incomplete context, inaccurate information, etc.), the system's initial answer may not fully meet the user's needs.
[0145] Furthermore, after generating preliminary answers, the system will evaluate the quality of the query results. The evaluation criteria include:
[0146] Accuracy: Whether the generated answer correctly answers the user's question.
[0147] Relevance: Whether the answer makes full use of the content of the document that is relevant to the query request.
[0148] Completeness: Does the answer include all key information, avoiding omissions or ambiguities?
[0149] If there are problems with the query results, the system will optimize the input prompts of the language model and the graph retrieval statements. The goal of the optimization is to improve the system's understanding ability of the query and make the generated answers more accurate and relevant.
[0150] Furthermore, the system will analyze the generated answers based on the effect of the preliminary Q&A and optimize the generation process by modifying the input prompts of the language model. The optimization process may include:
[0151] Adjust the expression of the question: For some user queries, it may be because the language model does not understand the question accurately. Therefore, it is necessary to adjust the description of the question to make it more clear and easy to understand.
[0152] Add background information: By providing more context or relevant information to the language model, it helps the model generate answers better. For example, in some complex questions, more background materials or relevant conditions may need to be added to the prompt to improve the accuracy of the model.
[0153] Optimize the prompt structure: According to the quality of the generated answers, adjust the structure of the prompt to ensure that the language model can better extract and generate the required information.
[0154] By continuously optimizing the prompts, the system can improve the quality of the generated answers and avoid vague or incomplete answers.
[0155] Furthermore, the optimization of the graph retrieval statements is carried out according to the relevance and accuracy of the query results. The purpose of optimizing the graph retrieval statements is to ensure that the system retrieves the most relevant text fragments from the knowledge graph. The optimization process may include:
[0156] Adjust the retrieval keywords: If the keywords used in the graph retrieval statements are too broad or inaccurate, the system may retrieve some irrelevant text fragments. At this time, the system can adjust the query statement according to the Q&A results, refine the keywords or improve the query conditions.
[0157] Adjust the similarity threshold: The system can affect the screening criteria of the retrieval results by adjusting the similarity score threshold (such as setting similarity_score_threshold). Increasing the threshold can reduce the return of irrelevant texts, while decreasing the threshold can expand the retrieval scope to ensure that more relevant content is retrieved.
[0158] Optimize the path search strategy: During the process of searching for relevant text fragments, the system can optimize the path search strategy according to the query request, making the retrieved text fragments more accurate and relevant. For example, by optimizing the path search from chunk_id to the document node, ensure that the retrieved text maximizes the relevance to the user's question.
[0159] Furthermore, once the prompt and graph retrieval statement are optimized, the system will regenerate the query results and adjust and optimize them again based on the feedback of the evaluation model. This process is iterative, that is, each round of optimization will be adjusted based on the query results and feedback effects generated in the previous round until the query results reach the accuracy and relevance expected by the user.
[0160] For example, after several rounds of optimization, the query results of the system may gradually improve from the initial incomplete answers to more complete and accurate answers. The feedback mechanism during the optimization process is crucial, which can continuously improve the performance of the question-answering system.
[0161] Furthermore, after repeated optimization, the system can finally generate a high-quality query answer. This answer is based on the improved graph retrieval statement and the optimized input prompt, ensuring that the most relevant information is obtained from the knowledge graph and generating accurate and natural answers through the language model.
[0162] Finally, the system will output the complete answer to the query request and form a closed loop for the user to obtain high-quality query results.
[0163] In this embodiment, based on the query request and the preliminary answer generated by the language model, the feedback mechanism is used to iteratively optimize the input prompt (prompt) of the language model and the graph retrieval statement, continuously improving the accuracy and relevance of the query results. This iterative optimization process ensures that the system can generate high-quality answers according to the user's needs and provide a continuously optimized question-answering experience in practical applications.
[0164] In some embodiments, this embodiment describes how to encapsulate the RAG question-answering generation process based on the knowledge graph into front-end and back-end services, enabling users to query through a Web interface and obtain answers in real time. By using Flask to encapsulate the back-end service and Gradio to encapsulate the front-end service, the system can provide a convenient question-answering service platform.
[0165] At the back end, the Flask framework is used to provide RESTful APIs to encapsulate the entire retrieval and generation process. Flask is a lightweight Web framework suitable for rapid development of Web services. The functions of the back-end service include receiving the user's query request, performing retrieval operations on the graph database, calling the preset language model to generate query results, and returning the final answer. The specific process is as follows:
[0166] Receive user queries: By defining a Flask API endpoint (e.g., / query), users can send queries to the server via HTTP requests. The API will receive query parameters such as the question entered by the user.
[0167] Execute graph database retrieval: After receiving the query request, the Flask backend will call a graph database (such as Neo4j) for graph retrieval. The system will construct corresponding graph retrieval statements based on the user's question and retrieve relevant text fragment entities (chunks) and related information from the graph database.
[0168] Call the language model to generate answers: After obtaining the text fragments related to the query, the Flask backend will call a preset language model (such as qwen-plus) to generate the final query answer. This language model generates a more natural and accurate answer based on the retrieved relevant text fragments.
[0169] Return the query result: The generated answer is wrapped in JSON format through Flask's jsonify() method and returned as the API's response to the frontend. In this way, users can see the query answer in real time.
[0170] Furthermore, use Gradio to encapsulate the frontend service. Gradio is an easy-to-use Python library that can quickly build a web interface. Gradio is used for the frontend service, providing an intuitive interface where users can enter queries and view the answers generated by the system. The specific implementation is as follows:
[0171] Create the frontend interface: Use Gradio to create a simple user interface that includes a text box for users to enter query questions. The questions entered by users will be sent to the backend Flask service for processing.
[0172] Connect to the Flask backend service: The frontend calls the API endpoint of the Flask backend via an HTTP request. After the user enters a query, Gradio will automatically send the entered text to the / query endpoint of Flask via a POST request. After the backend service processes the request, it returns the generated answer to the frontend.
[0173] Display the query result: After the frontend interface receives the answer returned by the backend, it displays it on the page for users to view. Gradio provides rich components such as text boxes and buttons to customize the frontend interaction method.
[0174] Supports interactive operations: The interface of Gradio supports multi-round interactions, that is, users can input multiple questions and view the system's answers. Each query is processed by the backend and a new answer is generated.
[0175] By combining Gradio and Flask, the system effectively integrates the front-end and back-end services to form a complete Q&A service platform. The communication between the front-end and the back-end is carried out through the HTTP API. Users can input queries on the Gradio interface, and the Flask back-end will process the queries and return answers. This interactive process enables users to ask questions in real time and get accurate answers.
[0176] For example, the Gradio front-end service is integrated with the Flask back-end in the following way: Gradio receives the questions input by users and sends them to the Flask back-end through an HTTP request. The Flask back-end queries the graph database, generates query results, and the Gradio front-end receives the answers and displays them on the interface.
[0177] Finally, users can use this system for Q&A services. After users input queries, the system will retrieve through the graph database in the back-end and generate answers using the language model, and display them on the front-end interface in real time. This process not only improves the user experience but also ensures the efficiency and accuracy of the Q&A service.
[0178] The following is an embodiment of the apparatus of the present application, which can be used to execute the method embodiment of the present application. For details not disclosed in the apparatus embodiment of the present application, please refer to the method embodiment of the present application.
[0179] Figure 3 It is a schematic structural diagram of an enhanced retrieval and generation apparatus based on a document structure knowledge graph provided by an embodiment of the present application. As Figure 3 shown, the enhanced retrieval and generation apparatus based on the document structure knowledge graph includes:
[0180] A parsing module 301, configured to parse a document to obtain document parsing content, where the document parsing content includes basic information and structured information of the document;
[0181] A first construction module 302, configured to construct a document entity and a text fragment entity according to the document parsing content, and establish an association relationship between the document entity and the text fragment entity, and between text fragment entities;
[0182] A second construction module 303, configured to construct a knowledge graph of the document structure according to the association relationship between the document entity and the text fragment entity, and between text fragment entities;
[0183] The processing module 304 is configured to store the knowledge graph in a graph database and perform text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content;
[0184] The retrieval module 305 is configured to, in response to a user's query request, retrieve text fragment entities related to the query request from the knowledge graph and generate corresponding query results according to the text content of the retrieved text fragment entities;
[0185] The generation module 306 is configured to process the query results using a preset language model to generate an answer corresponding to the query request.
[0186] In some embodiments, Figure 3 the parsing module 301 of reads the document content using a program and converts the document content into a unified string format; according to the document content, regular expressions are written to extract the basic information in the document; the extracted basic information is organized according to the structure of the document and converted into a structured data format.
[0187] In some embodiments, Figure 3 the first construction module 302 of generates a unique identifier and related information corresponding to the document according to the document parsing content, converts the unique identifier and related information corresponding to the document into a first data file to construct a document entity; according to the document parsing content, a program is used to extract the information related to the text content and text fragments in the document, and the extracted information related to the text content and text fragments is converted into a second data file to construct text fragment entities.
[0188] In some embodiments, Figure 3 the second construction module 303 of establishes an inclusion relationship between the document entity and the text fragment entities according to the document hierarchy in the document parsing content, and establishes a front-back dependency relationship between the text fragment entities, and stores the inclusion relationship and the front-back dependency relationship in a third data file.
[0189] In some embodiments, Figure 3 the processing module 304 of processes the text fragment entities in the knowledge graph, extracts the text content in the text fragment entities; uses a model to perform embedding processing on the text content in the extracted text fragment entities, converts the text content into a high-dimensional vector representation; associates the high-dimensional vector representation with the text fragment entities, and constructs a graph index of the text content, and stores the graph index in a graph database.
[0190] In some embodiments, Figure 3The retrieval module 305 determines the user's question according to the query request, and uses the query statements in the graph database to retrieve relevant text fragment entities from the knowledge graph according to the user's question; generates corresponding graph retrieval statements according to the retrieved text fragment entities and the associated path information, and performs graph information retrieval using the graph retrieval statements; filters the information in the graph database using the retrieval query statement and a predetermined similarity score threshold between the user's question and the graph content, and extracts text fragment entities related to the query request; processes the text fragment entities related to the query request extracted using a preset language model to generate a query result; combines the query request, the retriever, the query result, and the language model to form a complete question-and-answer link, and outputs the answer corresponding to the query request using the question-and-answer link.
[0191] In some embodiments, Figure 3 The optimization module 307 iteratively optimizes the input prompt of the language model and the graph retrieval statement according to the query request and the answer corresponding to the query request generated by the language model.
[0192] It should be understood that the magnitudes of the sequence numbers of the steps in the above embodiments do not mean the order of execution. The execution order of each process should be determined according to its function and internal logic, and should not constitute any limitation to the implementation process of the embodiments of the present application.
[0193] Figure 4 is a schematic structural diagram of the electronic device 4 provided by the embodiment of the present application. As Figure 4 shown, the electronic device 4 of this embodiment includes: a processor 401, a memory 402, and a computer program 403 stored in the memory 402 and executable on the processor 401. When the processor 401 executes the computer program 403, the steps in the above-mentioned method embodiments are implemented. Alternatively, when the processor 401 executes the computer program 403, the functions of each module / unit in the above-mentioned device embodiments are implemented.
[0194] Exemplarily, the computer program 403 can be divided into one or more modules / units. One or more modules / units are stored in the memory 402 and executed by the processor 401 to complete the present application. One or more modules / units can be a series of computer program instruction segments capable of performing specific functions, and the instruction segments are used to describe the execution process of the computer program 403 in the electronic device 4.
[0195] The electronic device 4 can be a desktop computer, a notebook, a palm computer, a cloud server, and other electronic devices. The electronic device 4 may include, but is not limited to, the processor 401 and the memory 402. Those skilled in the art can understand, Figure 4This is only an example of the electronic device 4 and does not constitute a limitation on the electronic device 4. It may include more or fewer components than those shown in the figure, or combine certain components, or different components. For example, the electronic device may also include input and output devices, network access devices, buses, etc.
[0196] The processor 401 may be a central processing unit (CPU), or may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or the processor may also be any conventional processor, etc.
[0197] The memory 402 may be an internal storage unit of the electronic device 4. For example, the hard disk or memory of the electronic device 4. The memory 402 may also be an external storage device of the electronic device 4. For example, a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 4. Further, the memory 402 may also include both the internal storage unit and the external storage device of the electronic device 4. The memory 402 is used to store computer programs and other programs and data required by the electronic device. The memory 402 may also be used to temporarily store data that has been output or will be output.
[0198] Those skilled in the art can clearly understand that for the convenience and simplicity of description, only the above division of each functional unit and module is used as an example. In actual applications, the above functions can be assigned to different functional units and modules according to needs, that is, the internal structure of the device is divided into different functional units or modules to complete all or part of the functions described above. Each functional unit and module in the embodiment may be integrated into one processing unit, or each unit may exist physically alone, or two or more units may be integrated into one unit. The above integrated unit may be implemented in the form of hardware or in the form of a software functional unit. In addition, the specific names of each functional unit and module are only for the convenience of mutual distinction and do not limit the protection scope of the present application. The specific working processes of the units and modules in the above system can refer to the corresponding processes in the foregoing method embodiments and will not be described herein again.
[0199] In the above embodiments, the descriptions of the various embodiments have their own emphases. For parts not detailed or recorded in a certain embodiment, reference may be made to the relevant descriptions of other embodiments.
[0200] Those of ordinary skill in the art can realize that the units and algorithm steps of the examples described in combination with the embodiments disclosed herein can be implemented by electronic hardware, or by a combination of computer software and electronic hardware. Whether these functions are executed in a hardware or software manner depends on the specific application and design constraints of the technical solution. Professional technicians can use different methods for each specific application to implement the described functions, but such implementation should not be considered to exceed the scope of this application.
[0201] In the embodiments provided in this application, it should be understood that the disclosed device / computer device and method can be implemented in other ways. For example, the device / computer device embodiments described above are merely illustrative. For example, the division of modules or units is only a logical function division. In actual implementation, there may be other division methods. Multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed couplings or direct couplings or communication connections to each other can be through some interfaces. The indirect couplings or communication connections of the devices or units can be in electrical, mechanical or other forms.
[0202] The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0203] In addition, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically alone, or two or more units can be integrated into one unit. The above integrated units can be implemented in the form of hardware or in the form of software functional units.
[0204] When the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, to implement all or part of the processes in the above-described embodiment methods of this application, it can also be completed by instructing relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by a processor, the steps of the above-described various method embodiments can be implemented. The computer program can include computer program code, and the computer program code can be in the form of source code, object code, executable file, or some intermediate form, etc. The computer-readable medium can include: any entity or device capable of carrying the computer program code, recording medium, USB flash drive, mobile hard disk, magnetic disk, optical disc, computer memory, read-only memory (ROM), random access memory (RAM), electrical carrier signal, telecommunication signal, and software distribution medium, etc.
[0205] The above embodiments are only used to illustrate the technical solutions of this application, rather than to limit them; although this application has been described in detail with reference to the foregoing embodiments, those of ordinary skill in the art should understand that: they can still modify the technical solutions recorded in the foregoing embodiments, or perform equivalent replacements for some of the technical features; and these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the various embodiments of this application, and should all be included in the protection scope of this application.
Claims
1. An enhanced retrieval and generation method based on a document structure knowledge graph, characterized in that Including: Parsing the document to obtain document parsing content, where the document parsing content includes the basic information and structured information of the document; Constructing a document entity and text fragment entities according to the document parsing content, and establishing an association relationship between the document entity and the text fragment entities, as well as between the text fragment entities; Constructing a knowledge graph of the document structure according to the association relationship between the document entity and the text fragment entities, as well as between the text fragment entities; Storing the knowledge graph in a graph database, and performing text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content; In response to a user's query request, retrieving text fragment entities related to the query request from the knowledge graph, and generating a corresponding query result according to the text content of the retrieved text fragment entities; Processing the query result using a preset language model to generate an answer corresponding to the query request.
2. The method according to claim 1, characterized in that, The parsing the document to obtain document parsing content includes: Using a program to read the document content and convert the document content into a unified string format; writing a regular expression according to the document content to extract the basic information in the document; organizing the extracted basic information according to the structure of the document and converting it into a structured data format.
3. The method according to claim 1, characterized in that, The constructing a document entity and text fragment entities according to the document parsing content includes: Generating a unique identifier and related information corresponding to the document according to the document parsing content, and converting the unique identifier and related information corresponding to the document into a first data file to construct the document entity; Extracting information related to the text content and text fragments in the document using a program according to the document parsing content, and converting the extracted information related to the text content and text fragments into a second data file to construct the text fragment entities.
4. The method according to claim 1, wherein The establishing an association relationship between the document entity and the text fragment entities, as well as between the text fragment entities includes: Establishing an inclusion relationship between the document entity and the text fragment entities according to the document hierarchy structure in the document parsing content, and establishing a front-back dependency relationship between the text fragment entities, and storing the inclusion relationship and the front-back dependency relationship in a third data file.
5. The method according to claim 1, wherein The performing text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content includes: Processing the text fragment entities in the knowledge graph to extract the text content in the text fragment entities; Using a model to perform embedding processing on the text content in the extracted text fragment entities to convert the text content into a high-dimensional vector representation; Associating the high-dimensional vector representation with the text fragment entities, and constructing a graph index of the text content, and storing the graph index in the graph database.
6. The method according to claim 1, wherein The in response to a user's query request, retrieving text fragment entities related to the query request from the knowledge graph, and generating a corresponding query result according to the text content of the retrieved text fragment entities includes: Determine the user's question according to the query request, and use the query statement in the graph database to retrieve relevant text fragment entities from the knowledge graph according to the user's question; Generate a corresponding graph retrieval statement according to the retrieved text fragment entities and associated path information, and use the graph retrieval statement to perform graph information retrieval; According to the retrieval query statement and a predetermined similarity score threshold between the user's question and the graph content, use a retriever to screen the information in the graph database and extract text fragment entities related to the query request; Use a preset language model to process the text fragment entities related to the query request that are extracted to generate a query result; Combine the query request, the retriever, the query result, and the language model to form a complete question-and-answer link, and use the question-and-answer link to output the answer corresponding to the query request.
7. The method according to claim 1, wherein The method further includes: Iteratively optimize the input prompt of the language model and the graph retrieval statement according to the query request and the answer corresponding to the query request generated by the language model.
8. An enhanced retrieval and generation device based on a document structure knowledge graph, characterized in that Including: A parsing module for parsing a document to obtain document parsing content, where the document parsing content includes basic information and structured information of the document; A first construction module for constructing document entities and text fragment entities according to the document parsing content, and establishing an association relationship between the document entities and the text fragment entities, and between the text fragment entities; A second construction module for constructing a knowledge graph of the document structure according to the association relationship between the document entities and the text fragment entities, and between the text fragment entities; A processing module for storing the knowledge graph in a graph database and performing text content embedding processing on the text fragment entities in the knowledge graph to generate a vector representation of the text content; A retrieval module for, in response to a user's query request, retrieving text fragment entities related to the query request from the knowledge graph and generating a corresponding query result according to the text content of the retrieved text fragment entities; A generation module for using a preset language model to process the query result to generate the answer corresponding to the query request.
9. An electronic device, comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, characterized in that, When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
Meteorological equipment maintenance knowledge base construction method, equipment and medium
CN121168598A
Query response and code generation method and system based on power field document analysis
CN121958316A
Query response and code generation method and system based on power field document parsing
CN121958316B