Urban rail transit emergency field question and answer method and system based on optimized RAG
Through the optimized RAG technology, combined with word embedding model and large language model, the complexity of urban rail transit emergency knowledge retrieval is solved, efficient and logical information query and answer presentation is achieved, and emergency plan preparation and passenger knowledge are improved. The efficiency and quality of popular science.
Patent Information
- Application Number
- CN202411105322.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-13
- Publication Date
- 2025-01-14
- Estimated Expiration
- 2044-08-13
AI Technical Summary
There are many types of emergency knowledge for urban rail transit, high cost of manual retrieval and sorting, and passengers lack emergency response ideas, which makes emergency plans time-consuming and labor-intensive and difficult to deal with when facing complex situations, resulting in unnecessary casualties.
Using optimized RAG technology, by combining the problem with the stylized prompt template of urban rail transit, splitting and analyzing urban rail emergency knowledge base documents to form parent document blocks and child document blocks, and using the word embedding model to calculate the new problem vector and Document block similarity, recall relevant documents and reorder them, and finally generate replies in the large language model.
It simplifies the maintenance of the knowledge base and information query, provides more friendly and logical answer presentation, improves the efficiency and quality of domain question query, and reduces the process complexity of knowledge entry and maintenance.
Smart Images

Figure CN119311793A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and specifically to an urban rail transit emergency field question answering method and system based on optimized RAG. Background Art
[0002] RAG refers to the Retrieval-Augmented Generation (RAG) technology based on fine-tuning of large language models. With the rapid development of urban rail transit systems, there are more and more emergency incidents. The preparation of urban rail emergency plans and the popularization of emergency knowledge have become key measures to ensure operational safety and reduce the harm of accidents. However, there are many types of urban rail emergency-related knowledge, which are usually distributed in different locations such as the Internet and literature in a multi-source heterogeneous form. The cost of manual retrieval and sorting is high, and the preparation of urban rail emergency plans is time-consuming and labor-intensive. At the same time, urban rail passengers lack ideas for dealing with common emergency incidents. Faced with complex situations and changing information, they often panic and cause unnecessary casualties.
[0003] Based on this, it is necessary to develop a question-answering method and system in the field of urban rail transit emergency based on the optimized RAG to assist manual preparation of emergency plans and set up in subway stations for the popularization of knowledge for passengers. Summary of the invention
[0004] In view of the above-mentioned problems, the present invention is proposed.
[0005] Therefore, the technical problem solved by the present invention is: how to provide an urban rail transit emergency field question-answering method and system based on the optimized RAG.
[0006] In order to solve the above technical problems, the present invention provides the following technical solutions: a question-answering method in the field of urban rail transit emergency based on optimized RAG, comprising: combining question Q with a stylized prompt template of urban rail transit into a new question Q'; identifying different document types in the urban rail transit emergency knowledge base and performing file parsing; segmenting the parsed documents in the urban rail transit emergency knowledge base to form multiple parent document blocks, obtaining child document blocks according to the parent document blocks, and obtaining a new question vector Q'E according to a word embedding model, a parent document block, a child document block, and a new question Q'; respectively calculating the new question vector Q'E. The similarity between the quantity Q'E and each of the parent document block or child document block vectors is measured, and recall is performed according to the similarity to form a recalled document set D that is most relevant to the question; the document blocks in the recalled document set D are scored for relevance, and are reordered to form a rearranged document set D'; a prompt template for model reply is constructed, and the new question Q' and the rearranged document set D' are passed into the prompt template for the model reply, and the prompt template for the model reply is passed into the base large language model that has not been adjusted or has been adjusted and optimized in the field of urban rail emergency, to obtain a generated response A of the large language model.
[0007] As a preferred solution of the urban rail transit emergency field question-answering method based on the optimized RAG of the present invention, wherein: the document in the urban rail emergency knowledge base after segmentation and analysis is formed into multiple parent document blocks, and the child document blocks are obtained according to the parent document blocks, including the following steps: the parsed document is divided into multiple parent document blocks, and the maximum threshold is set as L p-max ; Add a preset ID to the parent document block in the form of a key-value pair, and then divide the parent document block into n blocks with a maximum threshold of L through mapping. s-max The parent document block and the child document block ID are consistent, where n = L p-max / L s-max , rounded up; according to the word embedding model, the parent document block, the child document block, and the new question Q', a new question vector Q'E is obtained, which includes the following steps: embedding all document blocks using the word embedding model to obtain vector representations of all document blocks; persistently storing the vector representations of all document blocks in a local vector database; embedding the new question Q' to obtain a new question vector Q'E.
[0008] As a preferred solution of the urban rail transit emergency field question answering method based on the optimized RAG described in the present invention, the word embedding model uses an artificially constructed urban rail transit emergency field data set for initial screening and evaluation and then selects the best one. After selection, it uses multiple high-quality urban rail transit emergency field corpora for secondary fine-tuning training.
[0009] As a preferred solution of the urban rail transit emergency field question answering method based on optimized RAG described in the present invention, wherein: the similarity between the new question vector Q'E and each of the parent document block or child document block vectors is calculated respectively, and the recall is performed according to the similarity to form the recalled document set D most relevant to the question, including the following steps: the word embedding model is passed into the query engine Q-Engine; the word embedding model is used to calculate the similarity between the new question vector Q'E and all document block vectors, and the top K document blocks with the highest similarity are recalled; the top K recalled document blocks are merged according to ID, and only the parent document is taken for the parent-child document blocks with the same ID; and K r document blocks to form the recalled document set D that is most relevant to the question.
[0010] As a preferred solution of the urban rail transit emergency question-answering method based on the optimized RAG of the present invention, wherein: the relevance scoring of the document blocks in the recalled document set D and reordering to form the rearranged document set D' comprises the following steps: loading the rearranger Reranker and passing the rearranger Reranker into the query engine Q-Engine; rThe document blocks are passed into the reranker, and the new question vector Q'E is respectively compared with the K r The document blocks are passed into the trained cross encoder, and deep learning modeling is performed through the deep neural network to obtain the correlation between the vectors; the new question vector Q'E and the K are calculated based on the deep neural network. r The relevance scores of the document blocks are sorted from high to low, and the top K with the strongest relevance are selected. r ' document blocks form a rearranged document set D'.
[0011] As a preferred solution of the urban rail transit emergency question-answering method based on the optimized RAG described in the present invention, it also includes the self-generation of a vertical domain data set based on fine-tuning of a large language model, specifically including the following steps: constructing the prompt template Prompt required when the vertical domain data set instruction I is generated instruction And the prompt template required when output O is generated output ; Manually annotate 20 vertical field seed instruction sets D S-I , the seed instruction set D S-I The elements should consist of instructions I and output O; at the same time, a document library D is constructed to store references in vertical fields. ur ; In the seed instruction set D S-I Randomly select 2 seed instructions from the self-generated instruction set D' S-I Randomly select a self-generated instruction from the self-generated instruction set D' S-I When it is empty, then the seed instruction set D S-I Select 1 instruction, a total of 3 instructions as input instruction set D I Input into the large language model; randomly select the input instruction set D I With the prompt template Prompt instruction After splicing, as a new question, according to the optimized retrieval enhancement generation method, the unadjusted large language model is used at this time, and the large model reply A containing n vertical field instructions is obtained I ; Reply the large model to A I Parse according to a specific format to obtain n structured vertical field instructions I1-I n , and save the self-generated instructions as the self-generated instruction set D' S-I ; Repeat the above process until the number of vertical field instructions reaches the preset threshold m, then proceed to the next step, the number of iterations δ = m / n, δ is rounded up; the self-generated instruction set D' S-I All instructions in the prompt template are in sequence outputAfter concatenation, it is used as a new question. According to the optimized retrieval enhancement generation method, the large language model that has not been fine-tuned is used at this time, and the large model response A containing m is obtained. O , and finally obtain m outputs O corresponding to the instruction I one by one; the instruction set D' S-I All instructions in the above code are combined with their corresponding output O in turn and saved to obtain m vertical domain datasets used for fine-tuning the large language model.
[0012] As a preferred solution of the urban rail transit emergency field question and answer method based on the optimized RAG described in the present invention, the document parsing includes the following steps: judging whether the document is an xlsx or xls type file, if it is the xlsx or xls file, converting it into a csv file using the pandas library, and then using PagedCSVReader to parse the file; judging whether the document is a pdf type file, if it is the pdf file, using PyMuPDFReader to parse the file; judging whether the document is a csv type file, if it is the csv file, using PagedCSVReader to parse the file; judging whether the document is a doc type file, if it is the doc file, using DocxReader to parse the file.
[0013] To further solve the above technical problems, the present invention provides the following technical solutions: an urban rail transit emergency field question-answering system based on optimized RAG, comprising: a question construction module, used to combine the question Q raised by the user with the stylized prompt template of the urban rail transit into a new question Q'; a document type recognition module, used to identify different document types in the urban rail emergency knowledge base, and perform corresponding file parsing; a document segmentation and vectorization module, used to segment the parsed documents in the urban rail emergency knowledge base to form multiple parent document blocks, obtain child document blocks according to the parent document blocks, and obtain a new question vector Q'E according to the word embedding model, the parent document block, the child document block, and the new question Q'; calculate A recall calculation module is used to calculate the similarity between the new question vector Q'E and each of the parent document block or child document block vectors, and recall according to the similarity to form a recalled document set D that is most relevant to the question; a scoring and sorting module is used to score the relevance of the document blocks in the recalled document set D, and re-sort them to form a rearranged document set D'; a reply generation module is used to construct a prompt template for the model reply, pass the new question Q' and the rearranged document set D' into the prompt template of the model reply, and pass the prompt template of the model reply into the base large language model that has not been adjusted or adjusted and optimized in the urban rail emergency field to obtain a generated reply A of the large language model.
[0014] A computer device includes a memory and a processor, wherein the memory stores a computer program, and is characterized in that when the processor executes the computer program, the steps of the urban rail transit emergency field question and answer method based on the optimized RAG as described above are implemented.
[0015] A computer-readable storage medium having a computer program stored thereon, characterized in that when the computer program is executed by a processor, the steps of the urban rail transit emergency field question-and-answer method based on the optimized RAG as described above are implemented.
[0016] The beneficial effects of the present invention are as follows: (1) by using the semantic understanding ability of a large language model and optimized retrieval enhancement generation technology to establish a vectorized corpus index, information query and integration are provided in the form of natural language question and answer, which can simplify the maintenance of the knowledge base while providing a more friendly and logical answer presentation; (2) compared with the traditional knowledge base that needs to prepare fixed question and answer pairs, the present invention converts different types of files into document blocks and searches through the vectorized corpus, which greatly simplifies the process of entering and maintaining knowledge; (3) the file is divided into a number of sub-document blocks, and then the several document blocks are converted into word embedding vectors using a fine-tuned word embedding model, and combined to form a vectorized corpus, which is helpful to build a vectorized corpus that is more suitable for urban rail transit emergency scenarios; (4) the introduction of a rearrangement model, the secondary rearrangement of the first recall result, and the higher recall quality of the document block; (5) the large language model is fine-tuned by high-quality urban rail emergency data, and the fine-tuned large model is used to integrate user questions into formatted paragraphs, and the answers are presented in a logical way, thereby improving the efficiency and quality of domain problem queries. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other accompanying drawings can be obtained based on these accompanying drawings without paying creative work.
[0018] Figure 1 An overall flow chart of an urban rail transit emergency field question-answering method based on optimized RAG provided by an embodiment of the present invention.
[0019] Figure 2 This is an exemplary flow chart of dividing a parent document into sub-documents of the urban rail transit emergency field question-answering method based on the optimized RAG of the present invention.
[0020] Figures 3(a), 3(b) and 3(c) are evaluation effect diagrams drawn after the semantic recognition evaluation of the word embedding model of the urban rail transit emergency field question-answering method based on the optimized RAG of the present invention.
[0021] Figures 4(a), 4(b) and 4(c) are effect diagrams drawn after the recall document evaluation of the word embedding model of the urban rail transit emergency field question answering method based on the optimized RAG of the present invention.
[0022] Figure 5(a) and Figure 5(b) are effect diagrams drawn after fine-tuning and re-evaluation of the preferred word embedding model of the urban rail transit emergency field question-answering method based on the optimized RAG of the present invention.
[0023] Figure 6 The present invention is a flow chart of a method for self-generating a data set in the urban rail emergency field for fine-tuning a base model.
[0024] Figure 7 This is an exemplary module diagram of the urban rail transit emergency field question-answering system based on optimized retrieval enhancement generation of the present invention. DETAILED DESCRIPTION
[0025] In order to make the above-mentioned purposes, features and advantages of the present invention more obvious and easy to understand, the specific implementation methods of the present invention are described in detail below in conjunction with the drawings of the specification. Obviously, the described embodiments are part of the embodiments of the present invention, but not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary persons in the art without creative work should fall within the scope of protection of the present invention.
[0026] In the following description, many specific details are set forth to facilitate a full understanding of the present invention, but the present invention may also be implemented in other ways different from those described herein, and those skilled in the art may make similar generalizations without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below.
[0027] Example 1
[0028] Reference Figure 1 to Figure 6 , which is an embodiment of the present invention, provides an urban rail transit emergency field question answering method based on optimized RAG, including:
[0029] S1: Question rewriting: Construct a stylized prompt template for urban rail transit and combine question Q with the stylized prompt template for urban rail transit into a new question Q'.
[0030] Specifically, to pass in a question, you first need to pass in a question q. In general production practice, the passed in question may be relatively simple and cannot fully contain the semantics of the urban rail emergency field, which may result in the recall of document blocks with asymmetric semantics. Therefore, it is necessary to combine the passed in question q with an urban rail emergency style prompt template into a new question q'. The prompt template can be a sentence, a question or a topic related to the urban rail emergency field. The prompt template can guide the large language model to generate information or answers related to the prompt word. For example, the prompt template can be "Please answer in combination with the characteristics of the station hall facilities and equipment of the urban rail transit station:" The template is combined with the passed in question into a new question q'.
[0031] S2: Document parsing: Identify different document types in the urban rail emergency knowledge base and use specific document readers to parse files according to different types.
[0032] Specifically, a knowledge base in the field of urban rail transit emergency is constructed, and different types of files such as degree theses, journal documents, and related statistical tables in the field of urban rail transit emergency organization management are collected. Different types of files are read and parsed according to the designed file processing logic.
[0033] Furthermore, it is determined whether the document is an xlsx or xls type file. If it is an xlsx or xls file, the pandas library is used to convert it into a csv file, and then PagedCSVReader is used to parse the file.
[0034] Determine whether the document is a pdf type file. If it is the pdf file, use PyMuPDFReader to parse the file.
[0035] Determine whether the document is a csv type file. If it is a csv file, use PagedCSVReader to parse the file.
[0036] Determine whether the document is a doc type file. If it is a doc file, use DocxReader to parse the file.
[0037] S3: Split the parsed documents in the urban rail emergency knowledge base into multiple parent document blocks, obtain child document blocks based on the parent document blocks, and obtain a new question vector Q'E based on the word embedding model, the parent document blocks, the child document blocks, and the new question Q'.
[0038] Specifically, this step mainly involves document segmentation and vector embedding. Specifically, the documents in the parsed urban rail emergency knowledge base are segmented to form multiple parent document blocks. Each parent document block is divided into more fine-grained child document blocks, and the child document block and the parent document block ID are kept the same. The document block is embedded using the optimal and fine-tuned word embedding model to obtain multiple document block vectors. The user's new question Q' is then embedded using the word embedding model to obtain a new question vector Q'E.
[0039] Furthermore, all the parsed files are segmented according to a certain threshold, and the documents are segmented into no more than L p-max and not less than L p-min At the same time, the sliding window between adjacent document blocks is set to s to ensure semantic coherence. As needed, each parent document block can be further divided into child document blocks, that is, the parent document block is added with a preset ID in the form of a key-value pair, and then the parent document block is divided into n sub-document blocks with a maximum threshold of L through mapping. s-max The sub-document block of the parent document block and the sub-document block ID are consistent, where n = L p-max / L s-max , round up.
[0040] In practical applications, whether to adopt the segmentation method can be selected based on the retrieval effect after segmentation of the parent-child document blocks. Specifically, if the recall effect is worse than the recall effect without segmentation of the parent document blocks, it is not necessary to segment each parent document block into child document blocks.
[0041] In practical applications, the threshold L for document block segmentation is p-max and L p-min Need to debug according to needs.
[0042] Furthermore, according to the word embedding model, the parent document block, the child document block, and the new question Q', obtaining the new question vector Q'E includes the following steps:
[0043] All document blocks are embedded using a word embedding model to obtain vector representations of all document blocks. The vector representations of all document blocks are persistently stored in a local vector database; the persistent storage of document vectors saves document vectors persistently in the vector database, avoiding repeated loading of data in each question and answer. The new question Q' is embedded to obtain a new question vector Q'E.
[0044] Preferably, the word embedding model uses an artificially constructed urban rail transit emergency field data set for preliminary screening and evaluation, and then selects the best one. After selection, it uses multiple high-quality urban rail transit emergency field corpora for secondary fine-tuning training;
[0045] This step uses a selected and fine-tuned word embedding model. The word embedding model uses an artificially constructed urban rail emergency field dataset for initial screening and evaluation, and then selects the best. After selection, it uses hundreds of high-quality urban rail transit emergency field corpora for secondary fine-tuning training, which greatly improves the correct recall and semantic discrimination of the urban rail emergency field corpus of the word embedding model.
[0046] S4: Calculate the similarity between the new question vector Q'E and each parent document block or child document block vector respectively, perform recall based on the similarity, and form the recalled document set D that is most relevant to the question.
[0047] Specifically, this step mainly involves vector retrieval and document recall, specifically, calculating the similarity between the new question vector Q'E and each parent document block or child document block vector, and recalling the most relevant first K document blocks based on the similarity. For parent and child document blocks with the same ID, only the parent document block is taken. Finally, K r document blocks to form the recalled document set D that is most relevant to the question.
[0048] Furthermore, multiple candidate word embedding models are selected, which can be based on any word embedding method such as Word2Vec, GloVe, etc. This embodiment does not specifically limit the model structure of the word vector word embedding model. Evaluate multiple word embedding models, select the word embedding model that best suits the urban rail emergency corpus, use the word embedding model to embed and calculate the similarity between the new question vector Q'E and each parent document block or child document block vector, and recall the most relevant top K document blocks based on the similarity. For parent and child document blocks with the same ID, only the parent document block is taken. Finally, K r document blocks to form the recalled document set D that is most relevant to the question.
[0049] For further understanding, please refer to Figure 3(a) to Figure 3(c) 4(a), 4(b) and 4(c), in one embodiment, the word embedding model selection in S4 includes the following steps:
[0050] S4.1: Obtain industry specifications or standards in the field of urban rail emergency response.
[0051] According to the content of the National Urban Rail Transit Operation Emergency Plan, the semantic pairs in the urban rail emergency field are manually annotated and added to the test set of the Chinese-sts-b dataset. At the same time, data is self-generated based on the content to obtain corpus pairs consisting of one-to-one corresponding questions and question-related corpora, and the training and test sets are divided according to a certain ratio;
[0052] S4.2: Randomly select several data sets from the test set of the Chinese-STS-B data set with semantic pairs in the urban rail emergency field to evaluate the selected word embedding model, and draw a scatter plot as shown below: Figure 3(a) to Figure 3(c) , determine the word embedding model that works best at the level of urban rail emergency semantic judgment;
[0053] S4.3: Randomly select several data sets from the test set of the Chinese-STS-B data set with semantic pairs in the urban rail emergency field to evaluate the selected word embedding model, and draw a scatter plot as shown below: Figure 3(a) to Figure 3(c) , determine the word embedding model that works best at the level of urban rail emergency semantic judgment;
[0054] S4.4: Use the test set of the self-generated dataset in S4.1 to evaluate the recall of the selected word embedding model, and draw the combination diagrams as shown in Figure 4(a), Figure 4(b) and Figure 4(c) to determine the word embedding model with the best recall effect in the urban rail corpus;
[0055] S4.5: Based on the evaluation results, in this embodiment, the word embedding model that is most suitable for the urban rail emergency corpus is the bge-base-zh-v1.5 model.
[0056] S4.6: Further fine-tune the selected word embedding model.
[0057] In practical applications, the dataset format for fine-tuning the word embedding model is i ,c>, where c is the corpus into which the document is segmented, and q i In order to solve the problem of one-to-one correspondence with the corpus blocks, this specification does not limit the large model used for self-generation of data sets.
[0058] In practical applications, the index types used for embedding the document block can be various, and can be indexes based on different vector databases, or article abstract indexes, etc. The index type is not limited in this specification.
[0059] S5: Use a cross encoder to score the relevance of the document blocks in the recalled document set D, re-rank them, and select the top K in the document set D that are most relevant to the question. r ' document blocks form a rearranged document set D'.
[0060] Specifically, in one embodiment, the specific process of the cross encoder scoring the relevance of the document blocks in the recalled document set d is:
[0061] Use the supervised trained cross encoder to combine the user's new question q' with the document block d_c in the recalled document set d i Splice two by two as encoder input <q’,d_c i > and modeled through deep neural network transformers to obtain deep interactive representations;
[0062] Based on the deep interaction representation, a multi-layer perceptron is applied to predict the combination of the new user question and the document block. <q’,d_c i >Relevance score in embedding space;
[0063] Re-rank the relevance scores and select the top k most relevant to the new question r 'Document blocks form a rearranged document set d'.
[0064] S6: Construct a prompt template for the model's response, into which the new question Q' and the rearranged document set D' are passed, and the prompt template is passed into the base large language model that has not been fine-tuned or has been fine-tuned and optimized in the field of urban rail emergency, to obtain a generated response A from the large language model.
[0065] For more specific understanding, please refer to Figure 6 In one embodiment, the base language model in S6 can be fine-tuned using a high-quality urban rail emergency field self-generated dataset, including the following steps:
[0066] S6.1: Obtain high-quality documents in the field of urban rail emergency and construct the prompt template required for generating the vertical field dataset instruction I instruction And the prompt template required when output O is generated output ;
[0067] In practical applications, knowledge in vertical fields can be extracted from specialized documents in vertical fields, including journals, dissertations, etc. For example, knowledge can be extracted from relevant domestic and foreign literature research on urban rail emergency response, national standards for urban rail emergency response plans, and industry specifications.
[0068] In practical applications, the prompt template can be a paragraph or a topic that is conducive to the self-generation of high-quality data sets. The prompt template can guide the large language model to generate information or answers related to the prompt word. For example, the prompt template Prompt instructionIt can be: "Please strictly refer to the following information to generate some instructions, {context_str}, and these instructions must meet the following requirements, the requirements are: {query_str}" Among them, {context_str} is the recalled content, and {query_str} can be a paragraph or a topic that is conducive to the self-generation of high-quality data sets. For example: "You are an expert in the field of urban rail emergency and are asked to provide 5 questions. The following are 3 example instructions. You continue to write 5 questions based on the information. The questions all belong to urban rail emergency. The following are the requirements that you need to meet when providing instructions: 1. The generated questions must be detailed and the content belongs to urban rail emergency. 2. The generated questions must be closely related to the provided information and cannot be generated using prior knowledge. 3. The instructions should be in Chinese. The following are 3 example instructions: [Instruction generation]". The prompt template Prompt output It can be: "Combined with the context, {context} gives the answer to the following question from the perspective of a subway operation expert. The question is: {query}, answer:" {context} is the recalled content, and {query} is the self-generated instruction I that needs to be answered.
[0069] S6.2: Manually annotate 20 seed instruction sets D in the field of urban rail emergency according to the high-quality documents S-I , the elements of the seed instruction set should consist of instruction I and output O. At the same time, a document library D for storing reference documents in the field of urban rail transit is constructed. ur ;
[0070] S6.3: In the seed instruction set D S-I Randomly select 2 seed instructions in the self-generated instruction set D' S-I Randomly select a self-generated instruction, D' S-I If it is empty, then from D S-I Select 1 instruction, a total of 3 instructions as input instruction set D I Input into a large language model;
[0071] S6.4: randomly select the instruction set D I With the prompt template Prompt instruction After splicing, it is used as a new question and processed according to the process of the urban rail transit emergency field question-answering method based on the optimized RAG generated based on the optimized retrieval enhancement. At this time, the article abstract index is used to obtain a large model reply A containing n urban rail emergency field instructions. I ; Reply the model to A I Parse according to a specific format to obtain n structured vertical field instructions I1-I n , and save the self-generated instructions as a self-generated instruction set D' S-I ;
[0072] S6.5: Repeat the above process until the number of vertical field instructions reaches the set threshold m, and then proceed to the next step, the number of iterations δ = m / n, δ is rounded up;
[0073] S6.7: The self-generated instruction set D' S-I All instructions in the prompt template are in sequence output After splicing, it is used as a new question and processed according to the process of the urban rail transit emergency field question-answering method based on the optimized RAG generated based on the optimized retrieval enhancement. At this time, the ordinary vector database-based index is used to obtain a large model reply A containing m O Finally, we get m outputs O that correspond to the instruction I one by one. S-I All instructions in the above are combined with their corresponding output O in turn and saved to obtain m urban rail emergency data sets used for fine-tuning of the large language model;
[0074] S6.8: Clean the self-generated urban rail emergency data set to remove low-quality and duplicate data. After obtaining high-quality data, use LoRA and / or QLoRA or methods to fine-tune the base model. The two fine-tuning techniques are efficient parameter fine-tuning techniques, which freeze the pre-trained model weights, simulate parameter updates through low-rank decomposition, and only train a few additional parameters, which can achieve efficient fine-tuning.
[0075] In summary, the main process of this embodiment is to first build a knowledge base in the field of urban rail emergency and a search enhancement generation application, and then use methods such as splitting parent-child documents, fine-tuning word embedding models, and using rearrangement models to optimize enhanced search generation in the field of urban rail emergency. Then, a self-generated high-quality urban rail emergency field data set is used to fine-tune the base model ChatGLM3-6B, and the fine-tuned model is evaluated for factual knowledge to obtain a large language model for subsequent reasoning. When the user inputs a question to the LLM, the word embedding model is used to match the user's question vector with the document block vector of the knowledge base, and then the rearrangement model is used to rearrange the recall vector block to complete the relevant knowledge retrieval. Finally, the integrated prompt template is used as the actual input of the large language model to complete the question and answer of the current time. Multiple rounds of dialogue repeat the online process.
[0076] Example 2
[0077] Reference Figure 1-Figure 7 , which is an embodiment of the present invention, provides an urban rail transit emergency field question-answering system based on optimized RAG, including:
[0078] The data acquisition module is used to obtain the initial user question data and the user uploaded files, pre-process the initial user questions to obtain new questions, and embed the document blocks and questions. In one embodiment, the data acquisition module can obtain natural language query requests through user input. When the user makes an inquiry, request or instruction, the user can input a natural language query request through a human-computer interaction port (for example, a user terminal). In one embodiment, the data acquisition module can accept other forms of user input, such as pdf, xls files, etc., and embed and store the files using the knowledge base construction module.
[0079] A question construction module is used to combine the question Q raised by the user with the stylized prompt template of urban rail transit into a new question Q';
[0080] The document type identification module is used to identify different document types in the urban rail emergency knowledge base and perform corresponding file parsing;
[0081] The document segmentation and vectorization module is used to segment the documents in the urban rail emergency knowledge base after parsing to form multiple parent document blocks, obtain child document blocks based on the parent document blocks, and obtain the new question vector Q'E based on the word embedding model, parent document blocks, child document blocks, and new question Q';
[0082] A recall calculation module is used to calculate the similarity between the new question vector Q'E and each parent document block or child document block vector, and to perform recall according to the similarity to form a recall document set D that is most relevant to the question;
[0083] The scoring and sorting module is used to score the relevance of the document blocks in the recalled document set D and re-sort them to form a re-sorted document set D';
[0084] The reply generation module is used to construct a prompt template for the model reply, pass the new question Q' and the rearranged document set D' into the prompt template for the model reply, pass the prompt template for the model reply into the base large language model that has not been adjusted or adjusted and optimized in the urban rail emergency field, and obtain the generated reply A of the large language model.
[0085] The user management module is used to manage user names and passwords. In one embodiment, the user management module can perform hash processing on the user's user name and password, and can also add new users and modify and delete existing user information.
[0086] Example 3
[0087] It is an embodiment of the present invention, which is different from the previous embodiment in that:
[0088] If the functions are implemented in the form of software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product. The computer software product is stored in a storage medium, including several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the methods described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc., which can store program codes.
[0089] The logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as an ordered list of executable instructions for implementing logical functions, and can be embodied in any computer-readable medium for use by an instruction execution system, device or apparatus (such as a computer-based system, a system including a processor, or other system that can fetch instructions from an instruction execution system, device or apparatus and execute instructions), or in conjunction with such instruction execution systems, devices or apparatuses. For the purposes of this specification, "computer-readable medium" can be any device that can contain, store, communicate, propagate or transmit a program for use by an instruction execution system, device or apparatus, or in conjunction with such instruction execution systems, devices or apparatuses.
[0090] More specific examples of computer-readable media (a non-exhaustive list) include the following: an electrical connection with one or more wires (electronic device), a portable computer disk case (magnetic device), a random access memory (RAM), a read-only memory (ROM), an erasable and programmable read-only memory (EPROM or flash memory), an optical fiber device, and a portable compact disk read-only memory (CDROM). In addition, the computer-readable medium may even be a paper or other suitable medium on which the program is printed, since the program may be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, deciphering or, if necessary, processing in another suitable manner, and then stored in a computer memory.
[0091] It should be understood that the various parts of the present invention can be implemented by hardware, software, firmware or a combination thereof. In the above-mentioned embodiments, a plurality of steps or methods can be implemented by software or firmware stored in a memory and executed by a suitable instruction execution system. For example, if implemented by hardware, as in another embodiment, it can be implemented by any one of the following technologies known in the art or their combination: a discrete logic circuit having a logic gate circuit for implementing a logic function for a data signal, a dedicated integrated circuit having a suitable combination of logic gate circuits, a programmable gate array (PGA), a field programmable gate array (FPGA), etc.
[0092] It should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention rather than to limit it. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solutions of the present invention can be modified or replaced by equivalents without departing from the spirit and scope of the technical solutions of the present invention, which should be included in the scope of the claims of the present invention.
Claims
1. The urban rail transit emergency question-answering method based on the optimized RAG is characterized by: include: Combine question Q with the urban rail transit stylized prompt template to form a new question Q'; Identify different document types in the urban rail emergency knowledge base and perform file parsing; The parsed documents in the urban rail emergency knowledge base are segmented to form a plurality of parent document blocks, and child document blocks are obtained according to the parent document blocks. A new question vector Q'E is obtained according to the word embedding model, the parent document blocks, the child document blocks, and the new question Q'; Calculate the similarity between the new question vector Q'E and each of the parent document block or child document block vectors respectively, and perform recall according to the similarity to form a recall document set D that is most relevant to the question; Scoring the relevance of the document blocks in the recalled document set D, and re-ranking them to form a re-ranked document set D'; A prompt template for the model's reply is constructed, the new question Q' and the rearranged document set D' are passed into the prompt template for the model's reply, and the prompt template for the model's reply is passed into a base large language model that has not been adjusted or has been adjusted and optimized in the field of urban rail emergency, to obtain a generated reply A of the large language model.
2. The urban rail transit emergency field question-answering method based on the optimized RAG as claimed in claim 1 is characterized by: The segmentation and parsing of the documents in the urban rail emergency knowledge base to form a plurality of parent document blocks, and obtaining child document blocks according to the parent document blocks, comprises the following steps: Divide the parsed document into multiple parent document blocks, and set the maximum threshold to L p-max ; The parent document block is added with a preset ID in the form of a key-value pair, and then the parent document block is divided into n blocks with a maximum threshold of L by mapping. s-max The parent document block and the child document block ID are consistent, where n = L p-max / L s-max , round up; According to the word embedding model, the parent document block, the child document block, and the new question Q', the new question vector Q'E is obtained, which includes the following steps: All document blocks are embedded using the word embedding model to obtain vector representations of all document blocks; Persistently store vector representations of all document chunks into a local vector database; The new question Q' is embedded to obtain a new question vector Q'E.
3. The urban rail transit emergency field question-answering method based on the optimized RAG as claimed in claim 2 is characterized by: The word embedding model uses an artificially constructed urban rail transit emergency field data set for initial screening and evaluation, and then selects the best one. After selection, it uses multiple high-quality urban rail transit emergency field corpora for secondary fine-tuning training.
4. The urban rail transit emergency field question-answering method based on the optimized RAG as claimed in claim 3 is characterized by: The steps of respectively calculating the similarity between the new question vector Q'E and each of the parent document block or child document block vectors, and performing recall according to the similarity to form a recall document set D most relevant to the question include the following steps: The word embedding model is passed into the query engine Q-Engine; Use the word embedding model to calculate the similarity between the new question vector Q'E and all document block vectors, and recall the top K document blocks with the highest similarity; The first K recalled document blocks are merged according to their IDs, and only the parent document is taken from the parent-child document blocks with the same ID; Get K r document blocks to form the recalled document set D that is most relevant to the question.
5. The urban rail transit emergency field question-answering method based on optimized RAG as claimed in claim 4 is characterized by: The step of scoring the relevance of the document blocks in the recalled document set D and reordering them to form a reordered document set D' comprises the following steps: Loading the reranker Reranker, and transferring the reranker Reranker into the query engine Q-Engine; The K r The document blocks are passed into the reranker, and the new question vector Q'E is respectively compared with the K r The document blocks are passed into the trained cross encoder, and deep learning modeling is performed through the deep neural network to obtain the correlation between vectors; Based on the deep neural network, the new question vector Q'E and the K are calculated respectively. r The relevance score of each document block; Sort the correlation scores from high to low and select the top K with the strongest correlation. r ' document blocks form a rearranged document set D'.
6. The urban rail transit emergency field question-answering method based on optimized RAG as claimed in claim 5, characterized in that: It also includes the self-generation of vertical domain datasets used for fine-tuning large language models, which specifically includes the following steps: Construct the prompt template required when generating the vertical field data set instruction I instruction And the prompt template required when output O is generated output ; Manually annotate 20 vertical field seed instruction sets D S-I , the seed instruction set D S-I The elements of are composed of instructions I and output O; at the same time, a document library D is constructed to store references in vertical fields. ur ; In the seed instruction set D S-I Randomly select 2 seed instructions from the self-generated instruction set D' S-I Randomly select a self-generated instruction from the self-generated instruction set D' S-I When it is empty, then the seed instruction set D S-I Select 1 instruction, a total of 3 instructions as input instruction set D I Input into a large language model; The randomly selected input instruction set D I With the prompt template Prompt instruction After splicing, as a new question, according to the optimized retrieval enhancement generation method, the unadjusted large language model is used at this time, and the large model reply A containing n vertical field instructions is obtained I ; Reply the large model to A I Parse according to a specific format to obtain n structured vertical field instructions I1-I n , and save the self-generated instructions as the self-generated instruction set D' S-I ; Repeat the above process until the number of vertical field instructions reaches the preset threshold m, and then proceed to the next step, the number of iterations δ = m / n, δ is rounded up; The self-generated instruction set D' S-I All instructions in the prompt template are in sequence output After concatenation, it is used as a new question. According to the optimized retrieval enhancement generation method, the large language model that has not been fine-tuned is used at this time, and the large model response A containing m is obtained. O , and finally get m outputs O corresponding to the instruction I one by one; The instruction set D' S-I All instructions in the above code are combined with their corresponding output O in turn and saved to obtain m vertical domain datasets used for fine-tuning the large language model.
7. The urban rail transit emergency field question-answering method based on optimized RAG as claimed in claim 6 is characterized by: The document parsing comprises the following steps: Determine whether the document is an xlsx or xls type file. If it is an xlsx or xls file, use the pandas library to convert it into a csv file, and then use PagedCSVReader to parse the file; Determine whether the document is a pdf type file. If it is the pdf file, use PyMuPDFReader to parse the file; Determine whether the document is a csv type file. If it is a csv file, use PagedCSVReader to parse the file; Determine whether the document is a doc type file. If it is a doc file, use DocxReader to parse the file.
8. A system using the urban rail transit emergency field question-answering method based on optimized RAG as described in any one of claims 1 to 7, characterized in that: include: A question construction module is used to combine the question Q raised by the user with the stylized prompt template of urban rail transit into a new question Q'; The document type identification module is used to identify different document types in the urban rail emergency knowledge base and perform corresponding file parsing; The document segmentation and vectorization module is used to segment the parsed documents in the urban rail emergency knowledge base to form multiple parent document blocks, obtain child document blocks according to the parent document blocks, and obtain a new question vector Q'E according to the word embedding model, the parent document blocks, the child document blocks, and the new question Q'; A recall calculation module, used to respectively calculate the similarity between the new question vector Q'E and each of the parent document block or child document block vectors, and perform recall according to the similarity to form a recall document set D that is most relevant to the question; A scoring and sorting module, used to score the relevance of the document blocks in the recalled document set D, and re-sort them to form a re-sorted document set D'; The reply generation module is used to construct a prompt template for the model reply, pass the new question Q' and the rearranged document set D' into the prompt template for the model reply, pass the prompt template for the model reply into the base large language model that has not been adjusted or has been adjusted and optimized in the urban rail emergency field, and obtain the generated reply A of the large language model.
9. A computer device comprising a memory and a processor, wherein the memory stores a computer program, wherein: When the processor executes the computer program, the steps of the urban rail transit emergency field question and answer method based on the optimized RAG described in any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the steps of the urban rail transit emergency field question and answer method based on the optimized RAG according to any one of claims 1 to 7 are implemented.
Citation Information
Patent Citations
Statistical report generation method and system based on large language model
CN117875296A
Large model-based vertical domain question and answer method and system
CN118260393A
Systems and methods for structure discovery and structure-based analysis in natural language processing models
US11861321B1
Cited By
RAG enhanced small-scale language model question and answer accuracy improving method and system
CN120162415A
Audible voice interaction method and system
CN120564710A
