Safety law system intelligent question-answering system based on RAG architecture
The intelligent question-and-answer system based on the RAG architecture for a secure legal system solves the problems of computing power, data quality, and professionalism in the legal field for large language models, and achieves efficient and professional legal information services, which are suitable for national security and legal education scenarios.
Patent Information
- Application Number
- CN202511035807.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-25
- Publication Date
- 2025-11-07
AI Technical Summary
Existing large language models suffer from high computational barriers, data quality issues, difficulties in knowledge retrieval and reasoning, insufficient professional capabilities and traceability, and imperfect prompting engineering in legal professional scenarios, resulting in low response speed, insufficient accuracy, and insufficient professionalism of legal information.
The intelligent question-answering system for the security legal system, based on the RAG architecture, achieves efficient retrieval of legal knowledge and generation of professional answers by combining BERT and Transformer models through data collection and preprocessing, information extraction and vector index construction, hybrid retrieval, answer generation and post-processing optimization. It employs a hybrid retrieval strategy and a rigorous Prompt template constraint generation logic to ensure the accuracy and traceability of the answers.
It significantly improves the accuracy and authority of legal advice, enables timely updates to legal norms, adapts to national security and legal education needs, and provides efficient and professional legal information services.
Smart Images

Figure CN120910208A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of artificial intelligence and legal information processing technology, in particular to a security legal system intelligent question and answer system using a retrieval-augmented generation (RAG) framework, which is used to realize efficient retrieval, accurate matching and professional answer generation of legal regulations, judicial cases and principled legal knowledge, and is suitable for professional scenarios such as national security and personal data protection and popular law scenarios. BACKGROUND
[0002] With the rapid development of digital economy, traditional legal popularization and online legal consultation methods have been difficult to meet the public demand for real-time, accurate and authoritative legal information. At present, although large language models (such as GPT series) perform well in general NLP tasks, there are still many deficiencies in legal professional scenarios:
[0003] High computational threshold of general large models: fine-tuning and training a large amount of data requires a large amount of GPU resources, but if the computational requirement cannot be guaranteed, the response rate of the large model will be reduced and the training time will be prolonged.
[0004] Data quality problems: legal documents vary in quality, with errors, missing structures, etc., and many regulations and cases are scattered in special networks, so training data is easily affected by dirty data and bias.
[0005] Knowledge retrieval and reasoning difficulty: the semantic association of legal texts is complex, and the model is still insufficient in retrieval, fusion and deep reasoning.
[0006] Insufficient professional ability and traceability: in a number of legal task evaluations, the model scores low in memory, understanding and application, and the generated content lacks clear references, which is not conducive to rigorous legal consultation.
[0007] Incomplete prompt engineering: instruction design and prompt word sorting have not yet formed a systematic method, affecting model interaction and task execution effect.
[0008] Therefore, a new intelligent question and answer system combining external authoritative legal databases and large model generation capabilities is needed, which integrates retrieval and generation modules through the RAG framework to fill the above technical and application gaps, improve the accuracy, professionalism, traceability and real-time updating ability of legal answers, and meet the needs of more extensive popularization of law and legal services. SUMMARY
[0009] In order to solve the above problems in the prior art, the present application provides a security legal system intelligent question and answer system based on RAG framework, which includes the steps of data collection and preprocessing, information extraction and vector index construction, mixed retrieval, answer generation and post-processing optimization.
[0010] In particular, the scheme comprises the following steps:
[0011] S1: determining the required tools, configuring the corresponding environment and tools;
[0012] S2: determining and collecting the required legal knowledge base, and preprocessing the data;
[0013] S3: extracting key information from the data and dividing the extracted authoritative text into clauses;
[0014] S4: using the BERT model to process the text for vectorization and completing the construction of the index;
[0015] S5: using a hybrid retrieval method to combine two or more search algorithms to improve the relevance of search results;
[0016] S6: converting the retrieved document information into a user-understandable and professional legal answer;
[0017] S7: completing the front-end and back-end build to generate a public link for external access;
[0018] Further, because the inverted index technology is only suitable for low-dimensional and uniformly distributed data, to overcome the limitations of traditional inverted indexes in high-dimensional data processing, when a user presents a relatively vague legal question, the application uses a text vectorization based on the BERT model and an approximate nearest neighbor (ANN) index construction method. The specific steps are as follows:
[0019] S41: inputting the text processed in S1 and S2 into the BERT model for vectorization;
[0020] S42: importing the normalized vector into the target collection through the data writing interface of the database platform (such as ZillizCloud, etc.);
[0021] S43: setting the index parameters through the index construction interface of the database platform;
[0022] The index parameters include index type, distance measure, cluster number, and quantization parameters.
[0023] Further, to overcome the limitations of the traditional RAG retrieval method, the application uses a hybrid retrieval strategy in the document retrieval stage, which comprehensively calculates the content similarity and semantic similarity to achieve comprehensive and accurate retrieval of user queries. The specific steps are as follows:
[0024] S51: using the TF-IDF algorithm to calculate the content similarity of the preprocessed query and the document collection;
[0025] TF (Term Frequency) refers to the term frequency of the query keyword in each document, and the calculation formula is:
[0026]
[0027] wherein w represents the word to be calculated TF, d is a document containing the word, n i,j is the number of times the word appears in the document, and k n k,j is the total number of words in the document;
[0028] IDF (Inverse Document Frequency) refers to the inverse document frequency of the keyword, and the calculation formula is:
[0029]
[0030] wherein w is the word to be calculated IDF, D is the document set, and i |j:t j represents the number of documents containing the word w, and +1 is to avoid the case that the denominator is 0;
[0031] The calculation of TF-IDF is to combine the term frequency (TF) and the inverse document frequency (IDF) to calculate the similarity score between the document and the query. The calculation formula is:
[0032] TF-IDF (w, d, D) = TF (w, d) x IDF (w, D)
[0033] S52: Calculate the semantic similarity using the cosine similarity method;
[0034] wherein the value of the cosine similarity is closer to 1, indicating that the semantics between the two are more similar.
[0035] S53: Fuse the similarity scores of S51 and S52 to obtain the preliminary score;
[0036] S54: Use a ranking learning model to comprehensively score each document. The scoring model learns the importance of different features to relevance based on training data, so that the comprehensive score can better reflect the matching degree of the document and the query;
[0037] Further, to generate the retrieved document into a professional answer, the following steps are required:
[0038] S61: Select a Transformer model with deep semantic understanding and natural language generation capability;
[0039] S62: Use the pre-set built-in Prompt to process the user's original query, fuse the pre-prepared legal information summary, and extract the key information;
[0040] The prompt includes system role definition, system function definition, and answer generation template;
[0041] S63: The model generates multiple candidate answers, and finally selects the answer with the highest comprehensive score as the output result through a pre-designed evaluation mechanism;
[0042] Through the integration of deep structured processing of legal knowledge base, mixed retrieval strategy and generation model optimization, the system realizes efficient matching and professional answer output of legal provisions, judicial cases and legal knowledge. The core technology includes legal text dynamic blocking, BERT vectorization index construction, content and semantic mixed retrieval algorithm fusion, generation control based on rigorous Prompt template, and answer evaluation and optimization mechanism. The scheme effectively overcomes the defects of general large models in legal scenarios, such as dependence on computing power, data quality deviation, weak knowledge correlation, etc., significantly improves the accuracy, authority and practicality of legal consultation, and provides intelligent support for national security, personal data protection and legal education scenarios.
[0043] Beneficial effects: The construction method of the safety legal system intelligent question and answer system based on the RAG architecture effectively integrates multi-source data such as laws, administrative regulations, judicial interpretations, and cleanses the data to build a safety legal special knowledge base, providing rich knowledge support for safety legal system intelligent question and answer.
[0044] The invention has built-in rigorous Prompt template constraint generation logic, which forces to reference legal provisions and mark the effectiveness level, and through a multi-candidate answer evaluation mechanism to filter the accuracy and individuality of the optimal result, and based on the existing legal provisions to analyze the user's input query, to provide legal basis and personalized suggestions for the user, to ensure that the user can quickly obtain the most relevant and valuable information. At the same time, it significantly improves the accuracy and reliability of the safety legal system intelligent question and answer, effectively solving the problem of inaccurate and illusory answers of traditional large language models in the legal field.
[0045] The invention adopts a dynamic updating mechanism, which can timely incorporate new legal norms into the knowledge base, maintain the timeliness and accuracy of the model, and enable the system to better cope with the dynamic changes of legal norms and adapt to the constantly updated national security propaganda and legal education needs. BRIEF DESCRIPTION OF DRAWINGS
[0046] Figure 1 The present application provides a safety legal system intelligent question and answer system based on RAG architecture overall process schematic diagram.
[0047] Figure 2 The present application provides a safety legal knowledge base information blocking process flowchart.
[0048] Figure 3 The answer generation flowchart provided by the present application.
[0049] Figure 4 The built-in Prompt structure diagram provided by the present application. DETAILED DESCRIPTION
[0050] In order to improve the professionalism and accuracy of artificial intelligence answers, the present application provides a safe legal system intelligent question and answer system based on RAG architecture, and the core technologies include steps of legal information extraction and blocking, vector index construction, mixed retrieval, answer generation and post-processing optimization.
[0051] The technical method of the present application will be further described below in combination with embodiments and drawings. As shown in the drawings, the method comprises the following steps: Figure 1
[0052] S1: Determine the required tools, configure the corresponding environment and tools;
[0053] S2: Determine and collect the required legal knowledge base, and pre-process the data;
[0054] Specifically, the present embodiment classifies the collected legal regulations of the safe legal system of the present application. It includes two categories of national security and personal security, and the national security is divided into two aspects of overall planning and specific fields. Among them, the specific fields include 20 aspects of political security, territorial security, military security, cultural security, and technological security. Personal security is divided into concept protection and personal information data security.
[0055] S3: Extract the key information in the data and block the extracted authoritative text by clauses;
[0056] As shown in the drawings, the flowchart completely shows the logic of information blocking. First, determine whether the document is a law article or a case. If it is a law article, first extract the legal name and effective time, and construct hierarchical data according to its number, chapter, article and clause. Then perform dynamic blocking management, and block according to its association with the context, context, logic, etc. If it is a case, first extract the case number and case stage, and then record its judgment basis, judgment logic and fact statement to construct judicial metadata. Then determine whether it is a general case or a guiding case to construct a dynamic blocking strategy. After the above blocking processing, the data can be preliminarily blocked. Figure 2
[0057] S4: Use the BERT model to process the text for vectorization and complete the construction of the index;
[0058] Further, because the inverted index technology is only suitable for low-dimensional and uniformly distributed data, in order to overcome the limitations of traditional inverted index in high-dimensional data processing, when the user proposes a more fuzzy legal question, the application adopts a text vectorization method based on the BERT model and an approximate nearest neighbor (ANN) index construction method. The specific steps are as follows:
[0059] S41: The processed text in S1 and S2 is input into the BERT model for vectorization piece by piece;
[0060] S42: The normalized vector is imported into the target collection through the data writing interface of the database platform (such as ZillizCloud, etc.);
[0061] S43: Through the index construction interface of the database platform, the index hyperparameters are set
[0062] The index parameters include index type, distance measure, cluster number, and quantization parameter, etc.
[0063] S5: A hybrid retrieval method is used to improve the relevance of search results by combining two or more search algorithms;
[0064] Further, in order to overcome the limitations of the traditional RAG retrieval method, the application adopts a hybrid retrieval strategy in the document retrieval stage, which realizes comprehensive and accurate retrieval of user queries through comprehensive calculation of content similarity and semantic similarity. The specific steps are as follows:
[0065] S51: The TF-IDF algorithm is used to calculate the content similarity of the preprocessed query and the document set;
[0066] TF (Term Frequency) refers to the term frequency of the query keyword in each document, and the calculation formula is:
[0067]
[0068] Where w represents the word to be calculated for TF, d is a document containing the word, n_(i,j) is the number of times the word appears in the document, and ∑ k n k,j is the total number of words in the document;
[0069] IDF (Inverse Document Frequency) refers to the inverse document frequency of the keyword, and the calculation formula is:
[0070]
[0071] Where w is the word to be calculated for IDF, D is the document set, |j:t i ∈d j| represents the number of documents containing the word w, +1 is to avoid the denominator being 0;
[0072] The calculation of TF-IDF is to combine term frequency (TF) and inverse document frequency (IDF) to calculate the similarity score between the document and the query. The calculation formula is:
[0073] TF-IDF (w, d, D) = TF (w, d) x IDF (w, D)
[0074] S52: Calculate the semantic similarity using the cosine similarity method;
[0075] Wherein, the value of cosine similarity is closer to 1, indicating that the semantics between the two are more similar.
[0076] S53: Fuse the similarity scores of S51 and S52 to get the preliminary score;
[0077] S54: Use a ranking learning model to comprehensively score each document. The scoring model learns the importance of different features to relevance based on training data, so that the comprehensive score can better reflect the matching degree of the document and the query;
[0078] S6: Convert the retrieved document information into a user-understandable and professional legal answer;
[0079] Further, for generating a professional answer from the retrieved document, as shown in the following steps: Figure 2
[0080] S61: Select a Transformer model with deep semantic understanding and natural language generation capability;
[0081] S62: Use the pre-set built-in Prompt to process the user's original query, fuse the pre-prepared legal information summary, and extract the key information;
[0082] As shown in the following formula: Figure 4 The Prompt includes system role definition, system function definition, and answer generation template. Among them, the system role definition stipulates that the large model generates answers only based on the provided legal knowledge base, and requires legal language, prohibits subjective speculation or free play; The system function definition requires the system to generate answers to directly give clear conclusions (whether it constitutes xx crime), to explain that it cannot provide legal basis when there is no relevant content, to delete any hypothetical expression, to check that the legal citations and knowledge base are completely consistent after generating the answer; The answer generation template needs to provide the relevant law articles and mark the issuing organ and document number, sort multiple law articles according to legal validity, and use
newly added basis
[0083] S63: The model generates a plurality of candidate answers, and finally selects the answer with the highest comprehensive score as the output result through a pre-designed evaluation mechanism;
[0084] To sum up, the intelligent question and answer system based on the RAG architecture is used to realize efficient retrieval of legal knowledge and generation of professional answers. Specifically, the high-dimensional index is constructed by dynamic blocking of legal texts and vectorization of BERT models, breaking through the semantic retrieval limitations of traditional inverted indexes and significantly improving the matching accuracy of fuzzy queries; the TF-IDF content similarity and cosine semantic similarity are calculated, and the comprehensive score is optimized by combining the ranking learning model to ensure that the retrieval results fully cover the user's needs; the rigorous Prompt template constraint generation logic is built in to force the reference to legal articles and label the effectiveness level, eliminate subjective speculation and free play, and enhance the authority and traceability of the answers; the scheme solves the defects of weak data correlation and unprofessional generated content of general large models in the legal scene, provides intelligent and highly reliable legal consulting services for national security, personal data protection and popularization of law, and promotes the deep integration of artificial intelligence and legal information processing technology.
[0085] The above describes in detail the safety legal system intelligent question and answer method based on the retrieval enhancement generation (RAG) architecture provided by the present application, and the principle and implementation manner of the present application are described by applying specific examples; the above examples are only used to help understand the method of the present application and its core idea; meanwhile, for those skilled in the art, according to the idea of the present application, the specific implementation manner and application range will be changed, and the above description should not be understood as a limitation of the present application.
Claims
1. A method for constructing a security legal system intelligent question-answering system based on a RAG architecture, characterized in that By legal text dynamic blocking, BERT vectorization indexing, hybrid retrieval strategy (TF-IDF and cosine semantic fusion), and strict Prompt template constraint generation logic, efficient retrieval of legal knowledge, professional answer generation, and authoritative citation annotation are realized, solving the weak knowledge association and uncontrollable content generation problems of general large models in legal scenarios. Including the following steps: S1: Determine the required tools, configure the corresponding environment and tools; S2: Determine and collect the required legal knowledge base, and preprocess the data; S3: Extract the key information in the data and divide the extracted authoritative text into clauses; S4: Use the BERT model to process the text for vectorization and complete the construction of the index; S5: Use a hybrid retrieval method to combine two or more search algorithms to improve search result relevance; S6: Convert the retrieved document information into a user-understandable and professional legal answer; S7: Complete the front-end and back-end build to generate a public link for external access.
2. The RAG architecture-based security legal system intelligent answering system construction method according to claim 1, characterized in that, S4 uses BERT model-based text vectorization and approximate nearest neighbor index construction methods. S4 includes: S41: Input the text processed in S1 and S2 into the BERT model for vectorization; S42: Import the normalized vector into the target collection (Collection) through the data write interface of the database platform (such as ZillizCloud, etc.); S43: Set the index parameters through the index construction interface of the database platform Index parameters include index type, distance measure, cluster number, and quantization parameters.
3. The RAG architecture-based security legal system intelligent question-answering system construction method according to claim 1, characterized in that, S5 uses a hybrid retrieval method to overcome the limitations of traditional RAG retrieval methods. Through comprehensive calculation of content similarity and semantic similarity, comprehensive and accurate retrieval of the query is achieved. S5 includes: S51: Use the TF-IDF algorithm to calculate the content similarity between the preprocessed query and the document collection; TF (Term Frequency) refers to the term frequency of the query keyword in each document, and the calculation formula is: where w denotes the term for which the TF is to be computed, d is a document containing the term, n i,j is the number of occurrences of the term in the document, and k n k,j is the total number of terms in the document. IDF (Inverse Document Frequency) refers to the inverse document frequency of the keyword, and the calculation formula is: where w is the term for which the IDF is to be computed, D is the set of documents, |D| is the total number of documents, and |Dj: w| is the number of documents containing the term w. i ∈d j | represents the number of documents containing the term w, and +1 is to avoid the case where the denominator is 0. The calculation of TF-IDF is to combine the term frequency (TF) and the inverse document frequency (IDF) to calculate the similarity score between the document and the query. The calculation formula is: TF-IDF (w, d, D) = TF (w, d) x IDF (w, D) S52: Use cosine similarity to calculate semantic similarity; Where, the closer the value of cosine similarity to 1, the more similar the semantics between the two. S53: Fuse the similarity scores of S51 and S52 to get the preliminary score; S54: Use a ranking learning model to comprehensively score each document. The scoring model learns the importance of different features to relevance based on training data, making the comprehensive score better reflect the matching degree of the document and the query.
4. The RAG architecture-based security legal system intelligent question answering system construction method according to claim 1, characterized in that, S6 optimizes the generated answers, and S6 includes: S61: Select a Transformer model with deep semantic understanding and natural language generation capabilities; S62: Using the pre-set Prompt, the original query of the user is processed, the pre-prepared legal information summary is fused, and the extracted key information is extracted; The Prompt includes system role definition, system function definition, and answer generation template; S63: The model generates multiple candidate answers, and finally selects the answer with the highest comprehensive score as the output result through the pre-designed evaluation mechanism.
Citation Information
Cited By
Intelligent legal question-answering method and system based on multi-agent dynamic role scheduling and mixed retrieval, electronic equipment and storage medium
CN121524312A
Law intelligent question and answer method and system based on multi-agent dynamic role scheduling and hybrid retrieval, electronic device and storage medium
CN121524312B