Legal instrument generation method, device and equipment based on RAG and reflection technology

Through the legal document generation method of RAG and reflective technology, the problem of low generation efficiency in the existing technology is solved, efficient and accurate legal document generation is achieved, logical rigorous and semantic accuracy is ensured, and the quality and professionalism of legal documents are improved.

CN120541184APending Publication Date: 2025-08-26PING AN TECH (SHENZHEN) CO LTD

Patent Information

Application Number
CN202510656961.6
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-20
Publication Date
2025-08-26

AI Technical Summary

Technical Problem

The existing legal document generation methods are inefficient in generating efficiency, difficult to accurately understand and use legal professional terms, and lack effective reflection and optimization mechanisms, resulting in the generated documents lacking sufficient legal basis and practical support, especially in complex legal scenarios.

Method used

The legal document generation method based on RAG and reflection technology is adopted, and the input text is represented by vectors, legal information is screened using the search database, and initial documents are generated by splicing and decoding, and the reflection strategy chain is corrected to ensure logical rigorousness and semantic accuracy.

Benefits of technology

It improves the efficiency of legal documents generation, ensures that the generated documents are logically rigorous, comply with legal provisions and accurate semantic expression, and improves the professionalism and intelligence level of legal text processing.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120541184A_ABST
    Figure CN120541184A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of artificial intelligence, the scheme can be applied to the fields of medical treatment and finance, and the invention provides a legal instrument generation method, device and equipment based on RAG and the reflection technology. The method comprises the steps that an input legal text is processed, and a series of vector representations containing context information are obtained; screening corresponding legal information from a preset retrieval database according to the vector representation; splicing the legal text and the legal information to obtain spliced information; based on the spliced information, generating a corresponding initial legal document through a decoder; and correcting the initial legal document according to a preset reflection strategy chain to generate a corresponding legal document. According to the method, the information searching time can be shortened, the legal instrument generation efficiency can be improved, it can be ensured that the generated legal instruments are strict in logic, accord with legal regulations and are accurate in semantic expression, and the professional and intelligent level of text processing in the legal field is comprehensively improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method, device and equipment for generating legal documents based on RAG and reflection technology. Background Art

[0002] In the legal field, efficient and accurate generation of legal documents is crucial. Current legal text processing technology has numerous shortcomings. On the one hand, traditional text generation models often suffer from inaccurate terminology and confusion when processing legal terminology due to a lack of in-depth understanding of the legal knowledge system. For example, in contract drafting, key legal terms such as "force majeure" and "liability for breach of contract" may not be accurately expressed, resulting in loopholes in legal documents. On the other hand, existing technologies struggle to quickly and comprehensively retrieve and integrate relevant legal provisions, case studies, and other information during the generation of legal documents, resulting in the generated documents lacking sufficient legal basis and practical support.

[0003] In addition, the existing model lacks an effective reflection and optimization mechanism when generating legal documents. Once the preliminary text is generated, it is difficult to evaluate and improve the content from different perspectives, and it is impossible to form a complete and rigorous chain of thought. For example, in the generated legal analysis report, key legal points may be omitted, or the citation and analysis of cases may not be in-depth enough, affecting the quality and professionalism of the document. This problem is particularly prominent in complex legal scenarios, such as the generation of legal opinions for major cases.

[0004] Therefore, the existing legal document generation method has the problem of low generation efficiency. Summary of the Invention

[0005] The embodiments of the present invention provide a method, apparatus and device for generating legal documents based on RAG and reflection technology, aiming to solve the problem of low generation efficiency of existing legal document generation methods.

[0006] In a first aspect, an embodiment of the present invention provides a method for generating legal documents based on RAG and reflection technology, the method comprising:

[0007] Process the input legal text to obtain a series of vector representations containing context information;

[0008] Filtering corresponding legal information from a preset search database according to the vector representation;

[0009] Splicing the legal text and the legal information to obtain spliced ​​information;

[0010] Based on the spliced ​​information, a decoder is used to generate a corresponding initial legal document;

[0011] The initial legal document is revised according to a preset reflection strategy chain to generate a corresponding legal document.

[0012] In a second aspect, an embodiment of the present invention further provides a legal document generation device based on RAG and reflection technology, the device comprising:

[0013] A text processing unit, used to process the input legal text and obtain a series of vector representations containing context information;

[0014] an information screening unit, configured to screen corresponding legal information from a preset search database according to the vector representation;

[0015] A text splicing unit, configured to splice the legal text and the legal information to obtain spliced ​​information;

[0016] A document generation unit, configured to generate a corresponding initial legal document through a decoder based on the spliced ​​information;

[0017] The document revision unit is used to revise the initial legal document according to a preset reflection strategy chain to generate a corresponding legal document.

[0018] In a third aspect, an embodiment of the present invention further provides an electronic device comprising a memory and a processor, wherein the memory stores a computer program, and the processor implements the method described in the first aspect when executing the computer program.

[0019] In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, wherein the storage medium stores a computer program, wherein the computer program includes program instructions, and when the program instructions are executed by a processor, the method described in the first aspect can be implemented.

[0020] The present invention provides a method, device and equipment for generating legal documents based on RAG and reflection technology. The method includes: processing an input legal text to obtain a series of vector representations containing context information; filtering out corresponding legal information from a preset retrieval database based on the vector representation; splicing the legal text and the legal information to obtain spliced ​​information; generating a corresponding initial legal document through a decoder based on the spliced ​​information; and amending the initial legal document according to a preset reflection strategy chain to generate a corresponding legal document. The embodiment of the present invention can utilize RAG (Retrieval-Augmented Generation) technology to filter out corresponding legal information from a preset retrieval database, reduce information search time, and improve the efficiency of legal document generation; at the same time, amending the initial legal document through the reflection strategy chain to ensure that the generated legal document is logically rigorous, complies with legal provisions, and has accurate semantic expression, thereby comprehensively improving the professionalism and intelligence level of text processing in the legal field. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0022] Figure 1 A flowchart of a method for generating legal documents based on RAG and reflection technology provided by an embodiment of the present invention;

[0023] Figure 2 A schematic block diagram of a legal document generation device based on RAG and reflection technology provided by an embodiment of the present invention;

[0024] Figure 3 A schematic block diagram of an electronic device provided in an embodiment of the present invention;

[0025] Figure 4 A schematic diagram of the application environment of the legal document generation method based on RAG and reflection technology provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0026] The following will clearly and completely describe the technical solutions in the embodiments of the present invention in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of them. All other embodiments obtained by ordinary technicians in this field based on the embodiments of the present invention without making any creative efforts shall fall within the scope of protection of the present invention.

[0027] It will be understood that when used in this specification and the appended claims, the terms “comprises” and “comprising” indicate the presence of described features, integers, steps, operations, elements and / or components, but do not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components and / or groups thereof.

[0028] It should also be understood that the terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to limit the present invention. As used in the specification and appended claims, the singular forms "a," "an," and "the" are intended to include the plural forms unless the context clearly indicates otherwise.

[0029] It should be further understood that the term "and / or" used in the present specification and the appended claims refers to any combination of one or more of the associated listed items and all possible combinations, and includes these combinations. The embodiment of the present invention provides a method, device and apparatus for generating legal documents based on RAG and reflection technology. The method for generating legal documents based on RAG and reflection technology can be found in Figure 4 , Figure 4 Schematic diagram of the application environment of the legal document generation method based on RAG and reflection technology provided by the embodiment of the present invention. The legal document generation method based on RAG and reflection technology is applied in Figure 4 In an application environment, a processing terminal communicates with at least one user terminal through a network; a user inputs a legal text on a user interface through the user terminal; the processing terminal processes the input legal text to obtain a series of vector representations containing context information; the corresponding legal information is filtered out from a preset retrieval database according to the vector representation; the legal text and the legal information are spliced ​​to obtain spliced ​​information; based on the spliced ​​information, a corresponding initial legal document is generated through a decoder; the initial legal document is corrected according to a preset reflection strategy chain to generate a corresponding legal document and send it back to the user terminal; the processing terminal can be a server or a personal computer, wherein the server can be implemented as an independent server or a server cluster composed of multiple servers, and the user terminal can be, but is not limited to, a server, a smart phone, a tablet computer, a desktop computer and other electronic devices. The present invention is described in detail below through specific embodiments.

[0030] Figure 1 Schematic diagram of the process of generating legal documents based on RAG and reflection technology provided by the embodiment of the present invention. Figure 1 As shown, the method includes the following steps S110-S150.

[0031] S110: Process the input legal text to obtain a series of vector representations containing context information.

[0032] In this embodiment, the user inputs legal text on the user interface through the user terminal, and the processing terminal processes the input legal text to obtain a series of vector representations containing context information; the legal text can be but is not limited to legal provisions, case descriptions, etc.

[0033] The method processes the input legal text to generate a series of vector representations containing contextual information, including: using the Transformer architecture as the basic model framework, and using an encoder to convert the input legal text into a series of vector representations containing contextual information. This solution can be applied to medical business scenarios such as medical collaboration, medical device procurement, and medical service outsourcing, as well as financial business scenarios such as loan contracts, insurance contracts, and investment agreements.

[0034] Example 1: In the medical field, the legal text you enter might be "Develop a cooperation agreement between the internet medical platform and the contracted doctors, which must include the scope of diagnosis and treatment, patient privacy protection, profit sharing ratio, and termination clauses for illegal operations."

[0035] Example 2: In the financial field, the legal text you input could be "A bank customer's loan is overdue for 3 months, and repeated collection efforts have been fruitless. A lawyer's letter is generated, requesting a list of the amount owed, the legal basis, and the consequences of the lawsuit."

[0036] Specifically, let's assume the input legal text sequence is S = \{w_1, w_2, ..., w_L\}, where w_i is a word or token and L is the length of the text. After being processed by the encoder, a series of vector representations (i.e., vector sequences) containing context information are obtained: H = \{h_1, h_2, ..., h_L\}, where h_i is the context vector at the corresponding position. The calculation process is based on the Transformer's multi-head attention mechanism and feedforward neural network. For example, the calculation of the multi-head attention mechanism can be expressed as:

[0037] \text{MultiHead}(Q,K,V)=\text{Concat}(\text{head}_1,...,\text{head}_h)W^O;

[0038] Where Q, K, and V are query, key, and value matrices, h is the number of heads, \text{head}_i=\text{Attention}(QW_i^Q,KW_i^K,VW_i^V), \text{Attention}(Q,K,V)=\text{Softmax}(\frac{QK^T}{\sqrt{d_k}})V, d_k is the dimension of the key vector, and W_i^Q, W_i^K, W_i^V, and W^O are weight matrices.

[0039] Before adopting the Transformer architecture as the basic model framework and converting the input legal text into a series of vector representations containing contextual information through the encoder, it also includes: downloading the model's pre-trained weights to obtain a common language knowledge reserve; given that legal texts are characterized by dense terminology, rigorous logic, and complex structure, it is necessary to adjust the model's hyperparameters in a targeted manner: for example, by increasing the number of network layers, the model's ability to capture long-sequence dependencies and complex semantics of legal texts is enhanced; synchronously adjust parameters such as the learning rate and optimizer to maintain the stability of the training process while improving the model's expressiveness and avoiding the risk of overfitting; after completing the hyperparameter optimization, the input legal text is converted into a vector representation containing contextual semantic information through the encoder, laying a solid foundation for subsequent tasks such as legal document generation.

[0040] S120 , filtering out corresponding legal information from a preset search database according to the vector representation.

[0041] In this embodiment, corresponding legal information is filtered out from a preset retrieval database based on a series of the vector representations, and the retrieval database includes various legal provisions, decided cases, legal documents, etc.; specifically, based on each of the vector representations, the retrieval component in the RAG technology is utilized, and a vector similarity matching algorithm is used to retrieve relevant legal provisions, judicial cases, and professional discussions as the legal information from the retrieval database.

[0042] Before filtering out the corresponding legal information from a preset retrieval database based on the vector representation, the method also includes: collecting and organizing various legal provisions, cases, documents and other data to build a retrieval database; preprocessing the data, including text cleaning, word segmentation, tagging and other operations; using a vector database (such as Faiss) to store the vector representation of the data for rapid similarity retrieval; and regularly updating the database during the data collection process to ensure the timeliness of the data.

[0043] In one embodiment, step S120 includes: filtering out legal information related to the vector representation from the search database based on a vector similarity matching algorithm.

[0044] In this embodiment, based on the vector similarity matching algorithm, legal information related to the vector representation is screened out from the retrieval database; specifically, the input vector representation is \mathbf{q}, the document vector representation in the retrieval database is \mathbf{d}_i, and the cosine similarity is calculated.

[0045] \text{Cosine}(\mathbf{q},\mathbf{d}_i)=\frac{\mathbf{q}\cdot\mathbf{d}_i}{\|\mathbf{q}\|\|\mathbf{d}_i\|}, filtering out legal information related to the vector representation from the retrieval database.

[0046] In one embodiment, the method of filtering out legal information related to the vector representation from the retrieval database based on the vector similarity matching algorithm includes: performing similarity calculation on the vector representation and the document vector representation in the retrieval database based on the vector similarity matching algorithm to obtain a similarity calculation result; sorting candidate documents according to the similarity calculation result; and selecting the top N candidate documents with higher similarity as legal information.

[0047] In this embodiment, based on the vector similarity matching algorithm, the vector representation is similar to the document vector representation in the retrieval database to obtain a similarity calculation result; the candidate documents are sorted in descending order according to the similarity calculation result; the top N candidate documents with higher similarity are used as legal information; wherein N can be set according to actual application.

[0048] S130: Splice the legal text and the legal information to obtain spliced ​​information.

[0049] In this embodiment, the legal text and the legal information are spliced ​​to obtain spliced ​​information, and based on the spliced ​​information, a corresponding initial legal document is generated by a decoder.

[0050] S140. Based on the spliced ​​information, generate a corresponding initial legal document through a decoder.

[0051] In this embodiment, the concatenated information is fed into a Transformer-based generative model, which then generates the corresponding initial legal document via a decoder. During the generation process, the generative model uses its learned linguistic and legal knowledge to calculate a probability distribution for each generated token. A token is the basic unit of text (the concatenated information) in natural language processing, and can be a word, a subword, or a character. For example, the term "legal document" might be split into two tokens: "Law" and "Document."

[0052] Let the probability distribution of the generative model generating a token at step t be P(a_t|S_t), where S_t is the current state (including the generated text and retrieved information). The generative model determines P(a_t|S_t) through its own parameters and computational logic, ensuring that the generated text meets legal requirements. For example, when generating a clause regarding "compensation for breach of contract," the generative model uses retrieved contract-related legal provisions and case studies, combined with its own understanding of legal language, to generate reasonable text content (the initial legal document).

[0053] Preferably, a large amount of legal text data is used to train the retrieval component, and the parameters of the vector similarity matching algorithm (such as the similarity threshold, the number of retrieval results, etc.) and the parameters of the generation model (such as the attention mechanism parameters, the feedforward neural network parameters, etc.) are adjusted to improve the accuracy of retrieval and generation; during the training process, techniques such as contrastive learning are used to enhance the correlation between the retrieval results and the generated text.

[0054] S150: Modify the initial legal document according to the preset reflection strategy chain to generate a corresponding legal document.

[0055] In this embodiment, the reflection strategy chain includes multiple reflection strategies. The present invention uses multiple reflection strategies to revise the initial legal document and generate corresponding legal documents to ensure that the content of the document is logically rigorous, complies with legal regulations and is semantically rich, thereby improving the quality and reliability of the legal document.

[0056] In one embodiment, step S150 includes: constructing the reflection strategy chain according to the first reflection strategy, the second reflection strategy and the third reflection strategy; performing a logical consistency check on the initial legal document through the first reflection strategy, and generating a first reflection legal document based on the check result; matching the first reflection legal document through the second reflection strategy, and generating a second reflection legal document based on the matching result; evaluating the second reflection legal document through the third reflection strategy, and generating a third reflection legal document as the legal document based on the evaluation result.

[0057] In this embodiment, the reflection strategy chain is constructed according to the first reflection strategy, the second reflection strategy and the third reflection strategy; wherein, the first reflection strategy is a logical consistency reflection strategy, the second reflection strategy is a legal compliance reflection strategy, and the third reflection strategy is a semantic richness reflection strategy; specifically, 1. Implementation of logical consistency reflection: construct a legal logic reasoning rule library, and formalize common legal reasoning patterns (such as syllogistic reasoning, analogical reasoning, etc.); use natural language processing technology to convert the generated initial legal documents into logical expressions, and match and verify them with the legal logic reasoning rule library; 2. Implementation of legal compliance reflection: regularly update the legal and regulatory library to ensure the accuracy and timeliness of the data; use natural language matching algorithms (such as BM 25) Compare the generated first reflective legal document with the legal and regulatory database; during the comparison process, use text summarization technology to extract key information from the first reflective legal document and the legal and regulatory provisions to improve matching efficiency; if any content is found that does not comply with legal provisions, make corrections by fine-tuning the parameters of the generation model or regenerating relevant texts; 3. Semantic richness reflection implementation: Use a pre-trained semantic model (such as Sentence-BERT) to perform semantic analysis on the generated second reflective legal document; calculate the semantic similarity between the second reflective legal document and the reference text, and optimize the generated text based on the similarity results; during the optimization process, use text generation technology (such as text generation based on reinforcement learning) to expand and improve the second reflective legal document.

[0058] The initial legal document is checked for logical consistency through a first reflection strategy, and a first reflected legal document is generated based on the check result; the first reflected legal document is matched through a second reflection strategy, and a second reflected legal document is generated based on the matching result; the second reflected legal document is evaluated through a third reflection strategy, and a third reflected legal document is generated as the legal document based on the evaluation result.

[0059] The legal document generation process can be systematically divided into four closely linked core stages, each of which deeply integrates Retrieval Enhanced Generation (RAG) and reflective techniques to form a complete and rigorous chain of thought. In the problem analysis stage, the input legal text is precisely understood and structured, extracting key legal elements and points of contention. In the legal basis retrieval stage, guided by the key information output by the problem analysis, RAG technology is used to efficiently filter matching legal provisions, judicial interpretations, and typical cases from the search database to construct a supporting system for arguments. In the legal reasoning stage, relying on the legal basis retrieved in the previous stage, logical reasoning rules and legal interpretation methods are applied to bidirectionally match case facts with legal norms, forming a rigorous legal argumentation process. Finally, in the conclusion generation stage, an initial legal document is generated based on the reasoning results. Reflective techniques are used to perform multiple rounds of verification and optimization for the document's logical consistency, legal compliance, and semantic richness, resulting in a complete and professional legal document. Each stage is interconnected, and the output of the previous stage is naturally transformed into the input for the next stage, ensuring the systematic and professional nature of the legal document generation process. Furthermore, the various stages of legal document generation should be clarified, and input and output interfaces should be designed for each stage. In the interface design, the data formats between the various stages should be ensured to be consistent so that data can be transferred smoothly. For example, the output of the problem analysis stage should be designed as structured data containing key information to facilitate processing in the legal basis retrieval stage.

[0060] During the operation of the thinking chain, reflection technology serves as the intelligent center, continuously conducting in-depth verification of the output of each stage, and driving the model to make dynamic strategic adjustments based on the analysis results; for example, when the legal compliance reflection link identifies potential problems, the model will quickly start the backtracking mechanism, return to the legal basis retrieval stage, and use RAG technology to re-screen and supplement more comprehensive legal provisions and authoritative cases; through this dynamic adjustment and feedback mechanism, it not only ensures the coherence and logical consistency of each link in the thinking chain, but also significantly improves the accuracy and authority of the generated legal documents.

[0061] In one embodiment, the initial legal document is subjected to a logical consistency check through a first reflection strategy, and a first reflective legal document is generated based on the check result, including: constructing a legal logic reasoning rule library according to the logical reasoning rules in the first reflection strategy; performing a logical consistency check on the initial legal document according to the legal logic reasoning rule library to obtain a check result; if the check result is logical inconsistency, adjusting the initial legal document according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document.

[0062] In this embodiment, a legal logic reasoning rule library is constructed according to the logical reasoning rules in the first reflection strategy, and then common legal reasoning patterns (such as syllogistic reasoning, analogical reasoning, etc.) are formalized; the initial legal document is checked for logical consistency according to the legal logic reasoning rule library to obtain a check result; if the check result is logical inconsistency, the initial legal document is adjusted according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document; if the check result is logical consistency, the initial legal document is used as the first reflective legal document and the first reflective legal document is matched through the second reflection strategy, and a second reflective legal document is generated based on the matching result.

[0063] Specifically, during the logical consistency check, the test results are obtained by checking whether the logical deduction between the premise and the conclusion is reasonable / whether the cited legal provisions match the argumentation point of view; for a legal argument A = \{p_1, p_2, ..., p_n\Rightarrow c\}, where p_i is the premise and c is the conclusion, by searching the legal logic reasoning rule base for relevant legal relations and rules, it is verified whether the reasoning path from p_i to c exists and is reasonable; if logical inconsistency is found, the initial legal document is adjusted according to the correct logical relationship in the legal logic reasoning rule base.

[0064] For the sake of explanation, let’s take the medical scenario as an example:

[0065] In the medical dispute scenario, there is a legal argument A: premise p_1 is that the doctor did not strictly follow the surgical operation specifications during the operation, premise p_2 is that the patient suffered serious postoperative complications due to the operation, and conclusion c is that the doctor must bear all medical accident liability. When checking the logical consistency of this argument, a search of the legal logic reasoning rule library revealed that the determination of medical malpractice liability cannot be based solely on the doctor's non-standard operation and the patient's complications. In actual judgment, it is also necessary to consider whether the complications are unavoidable under current medical technology and the degree of direct causal relationship between the doctor's non-standard operation and the patient's complications. Therefore, directly deducing conclusion c from premises p_1 and p_2 alone is logically irrational and constitutes a case of logical inconsistency. To address this issue, the original legal document was adjusted based on the correct logical relationships in the legal logic reasoning rule library. The adjusted argument is as follows: premise p_1 is that the doctor did not strictly follow the surgical procedure standards during the operation; premise p_2 is that the patient suffered serious postoperative complications due to the operation; premise p_3 is added, stating that, according to professional medical evaluation, the postoperative complications were directly caused by the doctor's non-standard operation and were not unavoidable under current medical technology. Ultimately, conclusion c is reached: the doctor must bear the corresponding medical malpractice liability (the specific liability ratio will be determined based on the actual situation). The adjusted argument has a more reasonable logical deduction between the premises and conclusion, and the cited legal provisions are more consistent with the argument, meeting the requirements of legal logical consistency.

[0066] In one embodiment, the first reflective legal document is matched with the second reflective strategy, and the second reflective legal document is generated based on the matching result, including: matching the first reflective legal document with the legal and regulatory library in the second reflective strategy based on a natural language matching algorithm to obtain a matching result; if the matching result is a matching failure, returning to execute the step of filtering out the corresponding legal information from a preset retrieval database according to the vector representation to generate a second reflective legal document that complies with legal provisions.

[0067] In this embodiment, the first reflective legal document is matched with the legal and regulatory library in the second reflective strategy based on a natural language matching algorithm (such as BM25, etc.) to obtain a matching result; if the matching result is a matching failure, the step of filtering out the corresponding legal information from the preset retrieval database according to the vector representation is returned to generate a second reflective legal document that complies with legal provisions; if the matching result is a matching success, the first reflective legal document is used as the second reflective legal document and the second reflective legal document is evaluated through the third reflection strategy, and a third reflective legal document is generated as the legal document based on the evaluation result.

[0068] Specifically, in the matching process, let the text fragment in the first reflective legal document be T = \{t_1,t_2,...,t_m\}, and the legal provisions in the legal database be L = \{l_1,l_2,...,l_n\}, and determine whether the text fragment matches the legal provisions by calculating the BM25 score; wherein, BM25(T,L) = \sum_{i=1}^{m}\text{idf}(t_i)\frac{f(t_i,L)(k_1+1)}{f(t_i,L)+k_1(1-b+b\frac{|L|}{\text{avgdl}})}, \text{idf}(t_i) is the inverse document frequency, f(t_i,L) is the frequency of t_i in L, |L| is the length of the provision, \text{avgdl} is the average document length, and k_1 and b are parameters.

[0069] In one embodiment, the second reflective legal document is evaluated by the third reflective strategy, and a third reflective legal document is generated as the legal document based on the evaluation result, including: calculating the semantic similarity between the second reflective legal document and the reference text in the third reflective strategy through a preset semantic model; evaluating the second reflective legal document according to the semantic similarity to obtain an evaluation result; if the evaluation result is content that does not comply with legal provisions, semantically completing the second reflective legal document according to the legal and regulatory database to generate a third reflective legal document as the legal document.

[0070] In this embodiment, the semantic similarity between the second reflective legal document and the reference text in the third reflective strategy is calculated by a preset semantic model (such as Sentence-BERT, etc.); the second reflective legal document is evaluated according to the semantic similarity to determine whether the second reflective legal document is fully and accurately expressed, and an evaluation result is obtained; if the evaluation result is that the content does not comply with the legal provisions, the second reflective legal document is semantically completed according to the legal database to generate a third reflective legal document as the legal document; if the evaluation result is that the content complies with the legal provisions, the second reflective legal document is used as the legal document.

[0071] Specifically, during the evaluation process, let the vector representation of the second reflective legal document be \mathbf{g}, the vector representation of the reference text be \mathbf{r}, and the semantic similarity be \text{SemanticSim}(\mathbf{g},\mathbf{r})=\text{Cosine}(\mathbf{g},\mathbf{r}); if the semantic similarity is low, then the evaluation result is determined to be content that does not comply with legal provisions. At this time, the semantic features of the reference text are used to expand and optimize the second reflective legal document, and the second reflective legal document is semantically complemented by adding explanations of legal terms, enriching argumentation details, etc., to generate a third reflective legal document as the legal document.

[0072] In summary, the embodiments of the present invention can utilize RAG (Retrieval-Augmented Generation) technology to filter out corresponding legal information from a preset retrieval database, reduce information search time, and improve the efficiency of legal document generation; at the same time, the initial legal document can be revised through the reflection strategy chain to ensure that the generated legal document is logically rigorous, complies with legal provisions, and has accurate semantic expression, thereby comprehensively improving the professionalism and intelligence level of text processing in the legal field.

[0073] Figure 2 Schematic block diagram of a legal document generation device based on RAG and reflection technology provided by an embodiment of the present invention. Figure 2 As shown, corresponding to the above-mentioned legal document generation method based on RAG and reflection technology, the present invention also provides a legal document generation device based on RAG and reflection technology, the device is configured as follows Figure 4 In an application environment, a processing terminal communicates with at least one user terminal through a network; a user inputs a legal text on a user interface through the user terminal; the processing terminal processes the input legal text to obtain a series of vector representations containing context information; the corresponding legal information is filtered out from a preset retrieval database according to the vector representation; the legal text and the legal information are spliced ​​to obtain spliced ​​information; based on the spliced ​​information, a corresponding initial legal document is generated through a decoder; the initial legal document is corrected according to a preset reflection strategy chain to generate a corresponding legal document and send it back to the user terminal; the processing terminal can be a server or a personal computer, wherein the server can be implemented as an independent server or a server cluster composed of multiple servers, and the user terminal can be, but is not limited to, servers, smart phones, tablet computers, desktop computers and other electronic devices. Specifically, please refer to Figure 2 The legal document generation device 700 based on RAG and reflection technology includes:

[0074] A text processing unit 701 is used to process the input legal text to obtain a series of vector representations containing context information;

[0075] An information screening unit 702 is configured to screen corresponding legal information from a preset search database based on the vector representation;

[0076] A text splicing unit 703 is used to splice the legal text and the legal information to obtain spliced ​​information;

[0077] A document generation unit 704 is configured to generate a corresponding initial legal document through a decoder based on the spliced ​​information;

[0078] The document revision unit 705 is configured to revise the initial legal document according to a preset reflection strategy chain to generate a corresponding legal document.

[0079] In some embodiments, when the document revision unit 705 amends the initial legal document according to the preset reflection strategy chain to generate the corresponding legal document, it is specifically configured to:

[0080] The reflective strategy chain is constructed according to the first reflective strategy, the second reflective strategy and the third reflective strategy; the initial legal document is checked for logical consistency through the first reflective strategy, and a first reflective legal document is generated based on the check result; the first reflective legal document is matched through the second reflective strategy, and a second reflective legal document is generated based on the matching result; the second reflective legal document is evaluated through the third reflective strategy, and a third reflective legal document is generated as the legal document based on the evaluation result.

[0081] In some embodiments, when performing the step of performing a logical consistency check on the initial legal document using the first reflection strategy and generating a first reflected legal document based on the check result, the document revision unit 705 is specifically configured to:

[0082] A legal logic reasoning rule library is constructed according to the logical reasoning rules in the first reflection strategy; the initial legal document is checked for logical consistency according to the legal logic reasoning rule library to obtain a check result; if the check result is logical inconsistency, the initial legal document is adjusted according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document.

[0083] In some embodiments, when performing the step of matching the first reflected legal document using the second reflection strategy and generating the second reflected legal document based on the matching result, the document correction unit 705 is specifically configured to:

[0084] Based on a natural language matching algorithm, the first reflective legal document is matched with the legal and regulatory library in the second reflective strategy to obtain a matching result; if the matching result is a matching failure, the process returns to the step of filtering out the corresponding legal information from a preset retrieval database based on the vector representation to generate a second reflective legal document that complies with legal provisions.

[0085] In some embodiments, when the document revision unit 705 evaluates the second reflective legal document using the third reflective strategy and generates a third reflective legal document based on the evaluation result as the legal document step, it is specifically configured to:

[0086] The semantic similarity between the second reflective legal document and the reference text in the third reflective strategy is calculated through a preset semantic model; the second reflective legal document is evaluated based on the semantic similarity to obtain an evaluation result; if the evaluation result is that the content does not comply with the legal provisions, the second reflective legal document is semantically completed according to the legal and regulatory database to generate a third reflective legal document as the legal document.

[0087] In some embodiments, when performing the step of filtering corresponding legal information from a preset search database according to the vector representation, the information screening unit 702 is specifically configured to:

[0088] Based on a vector similarity matching algorithm, legal information related to the vector representation is filtered out from the search database.

[0089] In some embodiments, when the information screening unit 702 performs the step of screening out legal information related to the vector representation from the search database based on the vector similarity matching algorithm, it is specifically configured to:

[0090] Based on the vector similarity matching algorithm, similarity is calculated between the vector representation and the document vector representation in the retrieval database to obtain a similarity calculation result; candidate documents are sorted according to the similarity calculation result; and the top N candidate documents with higher similarity are used as legal information.

[0091] It should be noted that technical personnel in the relevant field can clearly understand that the specific implementation process of the above-mentioned legal document generation device based on RAG and reflection technology and each unit can refer to the corresponding description in the aforementioned method embodiment. For the convenience and conciseness of the description, it will not be repeated here.

[0092] The legal document generation device based on RAG and reflection technology can be implemented in the form of a computer program. The computer program can be used in Figure 3 Runs on the electronic devices shown.

[0093] See also Figure 3 , Figure 3 8 is a schematic block diagram of an electronic device provided by an embodiment of the present invention. The electronic device 800 can be a terminal or a server, wherein the terminal can be an electronic device with communication functions. The server can be a standalone server or a server cluster consisting of multiple servers.

[0094] See Figure 3 The electronic device 800 includes a processor 802 , a memory, and a network interface 805 connected via a system bus 801 , wherein the memory may include a non-volatile storage medium 803 and an internal memory 804 .

[0095] The non-volatile storage medium 803 can store an operating system 8031 ​​and a computer program 8032. The computer program 8032 includes program instructions, which, when executed, can enable the processor 802 to execute a legal document generation method based on RAG and reflection technology.

[0096] The processor 802 is used to provide computing and control capabilities to support the operation of the entire electronic device 800.

[0097] The internal memory 804 provides an environment for the operation of the computer program 8032 in the non-volatile storage medium 803. When the computer program 8032 is executed by the processor 802, the processor 802 can execute a legal document generation method based on RAG and reflection technology.

[0098] The network interface 805 is used to communicate with other devices over the network. Figure 3 The structure shown in the figure is merely a block diagram of a portion of the structure related to the solution of the present invention, and does not constitute a limitation on the electronic device 800 to which the solution of the present invention is applied. The specific electronic device 800 may include more or fewer components than those shown in the figure, or combine certain components, or have a different component arrangement.

[0099] The processor 802 is configured to execute a computer program 8032 stored in the memory to implement the following steps:

[0100] The input legal text is processed to obtain a series of vector representations containing contextual information; the corresponding legal information is filtered out from a preset retrieval database based on the vector representation; the legal text and the legal information are spliced ​​to obtain spliced ​​information; based on the spliced ​​information, a corresponding initial legal document is generated through a decoder; the initial legal document is revised according to a preset reflection strategy chain to generate a corresponding legal document.

[0101] In some embodiments, when the processor 802 amends the initial legal document according to the preset reflection strategy chain to generate the corresponding legal document, the processor 802 specifically implements the following steps:

[0102] The reflective strategy chain is constructed according to the first reflective strategy, the second reflective strategy and the third reflective strategy; the initial legal document is checked for logical consistency through the first reflective strategy, and a first reflective legal document is generated based on the check result; the first reflective legal document is matched through the second reflective strategy, and a second reflective legal document is generated based on the matching result; the second reflective legal document is evaluated through the third reflective strategy, and a third reflective legal document is generated as the legal document based on the evaluation result.

[0103] In some embodiments, when the processor 802 performs a logical consistency check on the initial legal document using the first reflection strategy and generates a first reflected legal document based on the check result, the processor 802 specifically implements the following steps:

[0104] A legal logic reasoning rule library is constructed according to the logical reasoning rules in the first reflection strategy; the initial legal document is checked for logical consistency according to the legal logic reasoning rule library to obtain a check result; if the check result is logical inconsistency, the initial legal document is adjusted according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document.

[0105] In some embodiments, when the processor 802 matches the first reflected legal document using the second reflection strategy and generates the second reflected legal document based on the matching result, the processor 802 specifically implements the following steps:

[0106] Based on a natural language matching algorithm, the first reflective legal document is matched with the legal and regulatory library in the second reflective strategy to obtain a matching result; if the matching result is a matching failure, the process returns to the step of filtering out the corresponding legal information from a preset retrieval database based on the vector representation to generate a second reflective legal document that complies with legal provisions.

[0107] In some embodiments, when the processor 802 implements the step of evaluating the second reflective legal document using the third reflective strategy and generating the third reflective legal document as the legal document based on the evaluation result, the processor 802 specifically implements the following steps:

[0108] The semantic similarity between the second reflective legal document and the reference text in the third reflective strategy is calculated through a preset semantic model; the second reflective legal document is evaluated based on the semantic similarity to obtain an evaluation result; if the evaluation result is that the content does not comply with the legal provisions, the second reflective legal document is semantically completed according to the legal and regulatory database to generate a third reflective legal document as the legal document.

[0109] In some embodiments, when implementing the step of filtering out corresponding legal information from a preset search database according to the vector representation, the processor 802 specifically implements the following steps:

[0110] Based on a vector similarity matching algorithm, legal information related to the vector representation is filtered out from the search database.

[0111] In some embodiments, when implementing the step of filtering out legal information related to the vector representation from the search database based on the vector similarity matching algorithm, the processor 802 specifically implements the following steps:

[0112] Based on the vector similarity matching algorithm, similarity is calculated between the vector representation and the document vector representation in the retrieval database to obtain a similarity calculation result; candidate documents are sorted according to the similarity calculation result; and the top N candidate documents with higher similarity are used as legal information.

[0113] It should be understood that in the embodiment of the present invention, the processor 802 may be a central processing unit (CPU), or may be other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.

[0114] Those skilled in the art will appreciate that all or part of the steps in the method of the above-described embodiment can be implemented by instructing the relevant hardware through a computer program. The computer program includes program instructions, which can be stored in a storage medium that is computer-readable. The program instructions are executed by at least one processor in the computer system to implement the steps in the method of the above-described embodiment.

[0115] Therefore, the present invention also provides a storage medium. The storage medium may be a computer-readable storage medium. The storage medium stores a computer program, wherein the computer program includes program instructions. When the program instructions are executed by a processor, the processor performs the following steps:

[0116] The input legal text is processed to obtain a series of vector representations containing contextual information; the corresponding legal information is filtered out from a preset retrieval database based on the vector representation; the legal text and the legal information are spliced ​​to obtain spliced ​​information; based on the spliced ​​information, a corresponding initial legal document is generated through a decoder; the initial legal document is revised according to a preset reflection strategy chain to generate a corresponding legal document.

[0117] In one embodiment, when the processor executes the program instructions to amend the initial legal document according to the preset reflection strategy chain and generate the corresponding legal document, the processor specifically implements the following steps:

[0118] The reflective strategy chain is constructed according to the first reflective strategy, the second reflective strategy and the third reflective strategy; the initial legal document is checked for logical consistency through the first reflective strategy, and a first reflective legal document is generated based on the check result; the first reflective legal document is matched through the second reflective strategy, and a second reflective legal document is generated based on the matching result; the second reflective legal document is evaluated through the third reflective strategy, and a third reflective legal document is generated as the legal document based on the evaluation result.

[0119] In one embodiment, when the processor executes the program instructions to perform a logical consistency check on the initial legal document using the first reflection strategy and generates a first reflected legal document based on the check result, the processor specifically implements the following steps:

[0120] A legal logic reasoning rule library is constructed according to the logical reasoning rules in the first reflection strategy; the initial legal document is checked for logical consistency according to the legal logic reasoning rule library to obtain a check result; if the check result is logical inconsistency, the initial legal document is adjusted according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document.

[0121] In one embodiment, when the processor executes the program instructions to match the first reflected legal document using the second reflection strategy and generate the second reflected legal document based on the matching result, the processor specifically implements the following steps:

[0122] Based on a natural language matching algorithm, the first reflective legal document is matched with the legal and regulatory library in the second reflective strategy to obtain a matching result; if the matching result is a matching failure, the process returns to the step of filtering out the corresponding legal information from a preset retrieval database based on the vector representation to generate a second reflective legal document that complies with legal provisions.

[0123] In one embodiment, when the processor executes the program instructions to evaluate the second reflective legal document using the third reflective strategy and generates the third reflective legal document based on the evaluation result as the legal document step, the processor specifically implements the following steps:

[0124] The semantic similarity between the second reflective legal document and the reference text in the third reflective strategy is calculated through a preset semantic model; the second reflective legal document is evaluated based on the semantic similarity to obtain an evaluation result; if the evaluation result is that the content does not comply with the legal provisions, the second reflective legal document is semantically completed according to the legal and regulatory database to generate a third reflective legal document as the legal document.

[0125] In one embodiment, when the processor executes the program instructions to implement the step of filtering out corresponding legal information from a preset search database based on the vector representation, the processor specifically implements the following steps:

[0126] Based on a vector similarity matching algorithm, legal information related to the vector representation is filtered out from the search database.

[0127] In one embodiment, when the processor executes the program instructions to implement the step of filtering out legal information related to the vector representation from the search database based on a vector similarity matching algorithm, the processor specifically implements the following steps:

[0128] Based on the vector similarity matching algorithm, similarity is calculated between the vector representation and the document vector representation in the retrieval database to obtain a similarity calculation result; candidate documents are sorted according to the similarity calculation result; and the top N candidate documents with higher similarity are used as legal information.

[0129] The storage medium may be any computer-readable storage medium that can store program codes, such as a USB flash drive, a mobile hard disk, a read-only memory (ROM), a magnetic disk, or an optical disk.

[0130] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the composition and steps of each example according to function. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0131] In the several embodiments provided herein, it should be understood that the disclosed devices and methods can be implemented in other ways. For example, the device embodiments described above are merely illustrative. For example, the division of the various units is merely a logical functional division, and actual implementation may employ other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be omitted or not implemented.

[0132] The steps in the methods of the embodiments of the present invention may be adjusted in order, combined, or deleted as needed. The units in the devices of the embodiments of the present invention may be combined, divided, or deleted as needed. Furthermore, the functional units in the various embodiments of the present invention may be integrated into a single processing unit, each unit may exist physically separately, or two or more units may be integrated into a single unit.

[0133] If this integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a storage medium. Based on this understanding, the technical solution of the present invention, or the part that contributes to the existing technology, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes a number of instructions for causing an electronic device (such as a personal computer, terminal, or network device) to execute all or part of the steps of the method described in various embodiments of the present invention.

[0134] The above description is merely a specific embodiment of the present invention, but the scope of protection of the present invention is not limited thereto. Any person skilled in the art can easily conceive of various equivalent modifications or substitutions within the technical scope disclosed in the present invention, and such modifications or substitutions are intended to be within the scope of protection of the present invention. Therefore, the scope of protection of the present invention shall be subject to the scope of protection of the claims.

Claims

1. A legal document generation method based on RAG and reflection technology, characterized in that: The method comprises: Process the input legal text to obtain a series of vector representations containing context information; Filtering corresponding legal information from a preset search database according to the vector representation; Splicing the legal text and the legal information to obtain spliced ​​information; Based on the spliced ​​information, a decoder is used to generate a corresponding initial legal document; The initial legal document is revised according to a preset reflection strategy chain to generate a corresponding legal document.

2. The legal document generation method based on RAG and reflection technology according to claim 1 is characterized in that: The step of revising the initial legal document according to the preset reflection strategy chain to generate a corresponding legal document includes: constructing the reflective strategy chain according to the first reflective strategy, the second reflective strategy and the third reflective strategy; Performing a logical consistency check on the initial legal document using a first reflection strategy, and generating a first reflected legal document based on the check result; matching the first reflective legal document using a second reflective strategy, and generating a second reflective legal document based on the matching result; The second reflective legal document is evaluated using a third reflective strategy, and a third reflective legal document is generated as the legal document based on the evaluation result.

3. The method for generating legal documents based on RAG and reflection technology according to claim 2 is characterized in that: The step of performing a logical consistency check on the initial legal document using the first reflection strategy and generating a first reflected legal document based on the check result includes: Building a legal logic reasoning rule library based on the logic reasoning rules in the first reflection strategy; Performing a logic consistency check on the initial legal document according to the legal logic reasoning rule base to obtain a check result; If the inspection result is logical inconsistency, the initial legal document is adjusted according to the logical relationship in the legal logic reasoning rule library to generate a first reflective legal document.

4. The method for generating legal documents based on RAG and reflection technology according to claim 2 is characterized in that: The step of matching the first reflected legal document with the second reflection strategy and generating the second reflected legal document based on the matching result includes: Matching the first reflection legal document with the legal and regulatory database in the second reflection strategy based on a natural language matching algorithm to obtain a matching result; If the matching result is a matching failure, the process returns to the step of filtering out corresponding legal information from a preset search database based on the vector representation to generate a second reflective legal document that complies with legal provisions.

5. The method for generating legal documents based on RAG and reflection technology according to claim 2 is characterized in that: The step of evaluating the second reflective legal document by using a third reflective strategy and generating a third reflective legal document as the legal document based on the evaluation result includes: Calculating the semantic similarity between the second reflective legal document and the reference text in the third reflective strategy using a preset semantic model; Evaluate the second reflective legal document according to the semantic similarity to obtain an evaluation result; If the evaluation result is that the content does not comply with the legal provisions, the second reflective legal document is semantically completed according to the legal database to generate a third reflective legal document as the legal document.

6. The method for generating legal documents based on RAG and reflection technology according to claim 1 is characterized in that: The step of filtering out corresponding legal information from a preset search database according to the vector representation includes: Based on a vector similarity matching algorithm, legal information related to the vector representation is filtered out from the search database.

7. The method for generating legal documents based on RAG and reflection technology according to claim 6 is characterized in that: The method of filtering out legal information related to the vector representation from the search database based on the vector similarity matching algorithm includes: Calculating similarity between the vector representation and the document vector representation in the retrieval database based on the vector similarity matching algorithm to obtain a similarity calculation result; Sorting the candidate documents according to the similarity calculation results; The top N candidate documents with the highest similarity are taken as legal information.

8. A legal document generation device based on RAG and reflection technology, characterized in that: The device comprises: A text processing unit, used to process the input legal text and obtain a series of vector representations containing context information; an information screening unit, configured to screen corresponding legal information from a preset search database according to the vector representation; A text splicing unit, configured to splice the legal text and the legal information to obtain spliced ​​information; A document generation unit, configured to generate a corresponding initial legal document through a decoder based on the spliced ​​information; The document revision unit is used to revise the initial legal document according to a preset reflection strategy chain to generate a corresponding legal document.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the legal document generation method based on RAG and reflection technology as described in any one of claims 1 to 7 is implemented.

10. A computer-readable storage medium, characterized in that The storage medium stores a computer program, which includes program instructions. When the program instructions are executed by a processor, the processor executes the legal document generation method based on RAG and reflection technology as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • Intelligent question answering method and system based on multi-module collaborative optimization

    CN119557409A

  • Systems and methods for aligning large multimodal models (LMMs) or large language models (LLMs) with domain-specific principles

    US12182678B1

Cited By

  • Text generation method, legal instrument generation method and text generation system

    CN121859859A