Legal question and answer fact enhancement method, system and device based on large language model

By preprocessing legal consultation questions and extracting legal provisions, combined with fine-tuning of a large language model and reinforcement learning for hard sample perception, the illusion problem in legal question-and-answer systems has been solved, achieving highly accurate and reliable legal consultation services.

CN119621891BActive Publication Date: 2025-10-24ZHEJIANG UNIV +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411669224.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-11-21
Publication Date
2025-10-24
Estimated Expiration
2044-11-21

AI Technical Summary

Technical Problem

Existing legal question-answering systems based on large language models are prone to illusion problems when generating answers, which fails to effectively enhance the factual nature of the law and results in insufficient accuracy and reliability of consultations.

Method used

By acquiring and preprocessing legal consultation questions, extracting legal provisions using regular expressions, fine-tuning the model using a large language model and training dataset, and employing a hard-sample-aware reinforcement learning algorithm and multiple evaluation metrics to optimize the model, a legal question-answering model is constructed.

Benefits of technology

It significantly improves the accuracy and reliability of legal Q&A, enhances the factual basis of the model, and improves the quality of legal consultation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119621891B_ABST
    Figure CN119621891B_ABST
Patent Text Reader

Abstract

The application discloses a legal question and answer factuality enhancement method, system and device based on a large language model, and belongs to the field of intelligent justice. The method of the application first constructs a first training data set and a second training data set based on a large language model, fine-tunes a large language model base on the first training data set and the second training data set by using a supervised fine-tuning method, obtains a fine-tuned model, and iteratively fine-tunes the fine-tuned model for several times by using a reinforcement learning algorithm based on difficult sample perception until a preset iteration round threshold is reached or the model converges, so as to obtain a legal question and answer model. The application constructs two-stage high-quality training data, and enhances the factuality of legal question and answer based on the reinforcement learning algorithm based on difficult sample perception, and has a good application prospect.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the field of intelligent justice, and particularly relates to a legal question and answer fact enhancement method, system and device based on a large language model. BACKGROUND

[0002] With the popularity of the Internet and the continuous improvement of the legal consciousness of the public, the demand for basic legal consultation in China is increasing. However, not all consultants can consult professional legal practitioners offline in a timely manner when encountering legal problems. At the same time, not all consultants are willing to pay high fees for legal consultation. The limited legal practitioner resources and the increasing demand for legal consultation form a clear contradiction.

[0003] Under this background, the development of artificial intelligence technology, especially the progress of large language models, provides new possibilities for the convenience and popularization of judicial consultation. Through artificial intelligence technology (large language models), consultants can obtain efficient, low-cost and professional legal consultation services online, thereby better protecting their own rights and interests. This trend marks the gradual development of intelligent justice as an important field of artificial intelligence technology, and provides an innovative solution to alleviate the shortage of judicial resources.

[0004] Legal consultation tasks (i.e. question and answer tasks) are a core task in the field of intelligent justice, which requires the model to automatically generate corresponding answers and cite relevant legal provisions according to the questions input by the consultants. Traditional automated legal consultation models usually use methods based on machine learning or deep neural networks. Machine learning-based methods usually use statistical methods instead of manually written rules to learn patterns and rules from a large number of legal cases to automatically generate answers to new questions. Although deep neural network-based methods (such as pre-trained models and large language models) have made some progress in understanding legal terminology and applying legal knowledge, due to the limitations of the professional and timeliness of the training data, and the lack of effective alignment with legal experts, the model is prone to "hallucination problems", i.e. the generated answers do not match the actual legal facts. Therefore, how to design a legal question and answer method that can enhance the factuality in the legal field based on the basis of large language models has become a key problem for the long-term development of judicial popularization and intelligent justice. SUMMARY

[0005] The purpose of the present application is to solve the problems existing in the prior art and to provide a legal question and answer fact enhancement method, system and device based on a large language model.

[0006] In order to achieve the above-mentioned purpose of the application, the following technical solutions are specifically adopted:

[0007] In a first aspect, the present application provides a legal question and answer fact-based enhancement method based on a large language model, which comprises the following steps:

[0008] S1: Obtain legal consultation questions and form an initial question set, pre-process the initial question set to obtain a processed question set, input each legal consultation question in the processed question set and a pre-constructed first prompt word into a large language model, output an initial answer for each legal consultation question, extract all articles mentioned in each initial answer using a regular expression, find the article in the text total set that is most similar in semantics to each initial answer as a true article, input each legal consultation question in the processed question set, the true article of each initial answer, and a pre-constructed second prompt word into the large language model, output an article related only to each legal consultation question in the processed question set and form a revised article set, input each legal consultation question in the processed question set, the initial answer, the revised article set, and a pre-constructed third prompt word into the large language model to revise the initial answer and obtain a revised answer, and form a first training data set from each legal consultation question in the processed question set and its corresponding revised answer;

[0009] S2: Generate an extended article set from the first training data set, input the articles in the extended article set and a pre-constructed fourth prompt word into the large language model, obtain legal consultation questions related to the articles in the extended article set and form an extended question set, input each article in the extended article set and its corresponding extended question set together with a pre-constructed fifth prompt word into the large language model, obtain a reference answer, and form a second training data set from each legal consultation question in the extended question set and its corresponding reference answer;

[0010] S3: Use a supervised fine-tuning method to fine-tune the large language model base on the first training data set and the second training data set, and obtain a fine-tuned model;

[0011] S4: Use a reinforcement learning algorithm based on difficult sample perception to fine-tune the fine-tuned model several times, in each iteration round, construct a corresponding preference data set based on three evaluation indicators, and divide the preference data set according to a pre-set proportion to obtain a training set, a validation set, and a test set for difficult sample perception preference learning, calculate the total loss on the training set, update the parameters of the fine-tuned model based on minimizing the total loss, use the updated model in each iteration round to infer the legal consultation questions in the test set, and evaluate the inference results on the three evaluation indicators until a pre-set iteration threshold is reached or the model converges, and obtain a legal question and answer model.

[0012] On the basis of the above-mentioned scheme, each step can be implemented in the following preferred specific manner.

[0013] As a preferred embodiment of the first aspect, in step S1, a total text set of all statutes and regulations is obtained, the total text set including T legal codes, the i-th legal code l i consisting of N i statutes, the j-th statute in the i-th legal code consisting of a corresponding code name, statute number and statute name; i∈{0,…,T}, j∈{0,…,N i}, N i is the number of statutes included in the i-th legal code, and T is the total number of legal codes in the total text set;

[0014] As a preferred embodiment of the first aspect, in step S1, the specific process of finding the statute with the most similar semantic to each initial answer in the total text set as the real statute is as follows:

[0015] S11, embedding each statute from the initial answer by the trained encoder to obtain a first embedding representation, and embedding each statute from the total text set by the trained encoder to obtain a second embedding representation;

[0016] S12, for each statute from the initial answer, the cosine similarity between the corresponding first embedding representation and the second embedding representation of each statute in the total text set is calculated in turn, and the statute corresponding to the second embedding representation with the largest cosine similarity is taken as the real statute.

[0017] As a preferred embodiment of the first aspect, in step S2, the specific process of generating the expanded statute set from the first training data set is as follows:

[0018] determine whether the number of occurrences of each statute in the first training data set is less than a preset number threshold: if not, do not process; if less than, the statutes less than the number threshold are taken as insufficient training statutes, and the expanded statute set is composed of all insufficient training statutes.

[0019] As a preferred embodiment of the first aspect, in step S3, in the process of fine-tuning the large language model base, the first training data set and the second training data set are combined and then randomly sorted to generate a fine-tuning data set, the legal consultation questions included in the fine-tuning data set are input into the large language model base, the large language model base generates the answer results of the legal consultation questions, the first negative log-likelihood loss is calculated based on the answers in the fine-tuning data set and the answer results of the legal consultation questions generated by the large language model base, the parameters of the large language model base are updated and optimized based on minimizing the first negative log-likelihood loss, until a preset iteration number threshold is reached, the large language model base converges, and a fine-tuned model is obtained.

[0020] Further, the function form of the first negative log-likelihood loss is as follows:

[0021]

[0022] where y t represents the t-th word of the answer in the fine-tuning data set; y <t represents a word sequence composed of the first t-1 words of the answer in the fine-tuning data set; T represents the length of the answer in the fine-tuning data set; x represents a legal consultation question contained in the fine-tuning data set; p(y t |y <t , x) represents the probability of predicting the t-th word as y <t by the large language model base under the given legal consultation question x and the word sequence y t .

[0023] As a preferred embodiment of the above first aspect, in step S4, the specific process of constructing the three evaluation indexes is as follows:

[0024] S41, obtain D legal consultation questions, and use regular expressions to extract two elements in the to-be-evaluated answer corresponding to each legal consultation question, the first element being all to-be-tested statutes mentioned in the to-be-evaluated answer and constituting a to-be-tested statute set, and the second element being a suggestion part in the to-be-evaluated answer and constituting a suggestion text;

[0025] S42, a triple is formed by the code name, statute number and statute name corresponding to each to-be-tested statute, and when the triple of the w-th to-be-tested statute belongs to the total text set, it is considered that the w-th to-be-tested statute has no hallucination, and the hallucination-free rate of the to-be-tested statute is taken as the first evaluation index;

[0026] Further, the calculation method of the hallucination-free rate NHSR of the to-be-tested statute is as follows:

[0027]

[0028] In the formula, I represents an indicator function; ∧ represents simultaneous satisfaction; M resp represents the total number of all to-be-tested statutes in all to-be-evaluated answers; represents the code name, statute number and statute name in the w-th to-be-tested statute;

[0029] S43, the hallucinated to-be-tested legal provision is regarded as irrelevant, and the non-hallucinated to-be-tested legal provision and the legal consultation question form a binary group, which is input into the large language model together with the sixth prompt word constructed in advance, whether the non-hallucinated to-be-tested legal provision and the legal consultation question have relevance is judged by the large language model, a scalar value is returned to represent the relevance judgment result, and the relevance between the to-be-tested legal provision and the legal consultation question raised by the user is taken as the second evaluation index;

[0030] Further, the calculation method of the relevance between the to-be-tested legal provision and the legal consultation question raised by the user is as follows:

[0031]

[0032] In the formula, s w ∈{0,0.5,1} represents the scalar value returned by the large language model to represent the relevance judgment result, s w =0 represents irrelevant, s w =0.5 represents weakly relevant, s w =1 represents strong relevance;

[0033] S44, the legal consultation question and the total set of texts are input into the retriever, and the retriever returns d legal provisions most relevant to the legal consultation question from the total set of texts to form a set of existing legal provisions, the legal consultation question, the set of existing legal provisions and the suggestion text form a ternary group, which is input into the large language model together with the seventh prompt word constructed in advance, and the large language model is required to judge whether the suggestion text is consistent with the set of existing legal provisions based on the legal consultation question, a scalar value is returned to represent the consistency judgment result, and the consistency between the suggestion text and the set of existing legal provisions is taken as the third evaluation index.

[0034] Further, the calculation method of the consistency LC between the suggestion text and the set of existing legal provisions is as follows:

[0035]

[0036] In the formula, l w ∈{0,1} represents the scalar value returned by the large language model to represent the consistency judgment result, l w =0 represents inconsistency, l w =1 represents consistency; D represents the number of legal consultation questions.

[0037] As a preferred embodiment of the above-mentioned first aspect, in step S4, the specific process of constructing the preference data set of the fth iteration round is as follows:

[0038] The fine-tuning dataset is divided into a supervised fine-tuning training set, a supervised fine-tuning test set, and a supervised fine-tuning validation set according to a preset ratio, the legal consultation questions and answers in the supervised fine-tuning training set are taken as the question part and the high-quality answer part respectively, and the question part and the high-quality answer part form the original dataset of the fth iteration round;

[0039] The question part is input into the updated model of the f-1th iteration round for inference, and the answer generated by the updated model of the f-1th iteration round, the legal consultation question in the question part, and the answer in the high-quality answer part form a triple and constitute an intermediate dataset;

[0040] Each answer generated by the updated model of the f-1th iteration round is taken as a to-be-detected answer, the statute part and the suggestion part in the to-be-detected answer are extracted by using a regular expression, if the statute part in a to-be-detected answer contains hallucination, or the statute part is irrelevant to the legal consultation question, or the suggestion part in the to-be-detected answer is inconsistent with the text total set, then the triple corresponding to the to-be-detected answer is added to the original dataset of the fth iteration round to update the original dataset, and finally the preference dataset of the fth iteration round is obtained.

[0041] As a preferred embodiment of the first aspect, in step S4, the answer in the preference dataset is formed by a legal consultation question x, a preferred answer y w and a rejected answer y l , and the triple constitutes the total loss , and the function form of the total loss

[0042]

[0043]

[0044] In the formula, α and β represent weight hyperparameters; ω represents an adjustment parameter; represents a second negative log-likelihood loss, represents a direct preference optimization loss; |y w | represents the length of y w ; σ is a sigmoid activation function; M θ (y w |x) represents the probability that the model calculates the output as y w given the legal consultation question x according to the current parameters θ thereof; M θ (y l |x) represents the probability that the model calculates the output as y l given the legal consultation question x according to the current parameters θ thereof; M f (y wx) represents the probability that the model updated at the previous iteration round calculates the output as y given the legal consultation question x w f (y l x) represents the probability that the model updated at the previous iteration round calculates the output as y given the legal consultation question x l .

[0045] As a preferred embodiment of the above-mentioned first aspect, in step S4, after obtaining the model updated at each iteration round, the legal consultation question in the test set is inferred, and it is determined whether the current iteration number has reached the preset maximum iteration number:

[0046] If yes, the iteration is stopped, and the trained legal question and answer model is outputted;

[0047] If not, the three evaluation index values in S42-S44 are calculated on the test set, and it is determined whether the three evaluation indexes have reached the expectation: if yes, the iteration is stopped, and the trained legal question and answer model is outputted; if one or more evaluation indexes have not reached the expectation, the evaluation indexes of each iteration round are compared with the evaluation indexes of the previous iteration round, and the iteration is stopped when all the three evaluation indexes decrease, and the trained legal question and answer model is outputted; otherwise, the training is continued until the preset maximum iteration number is reached or the model converges.

[0048] In a third aspect, the present application provides a computer electronic device, comprising a memory and a processor;

[0049] The memory is used to store a computer program;

[0050] The processor is used to realize the legal question and answer fact enhancement method based on a large language model according to the first aspect when the computer program is executed.

[0051] Compared with the prior art, the present application has the following beneficial effects:

[0052] The present application is used to realize the legal consultation question and answer task in intelligent justice, and compared with the traditional legal consultation model, the present application uses the preference data and the reinforcement learning algorithm for difficult sample perception to enhance the fact of the model after fine-tuning on the basis of the large language model, which significantly improves the legal question and answer accuracy and reliability of the model.

[0053] ​The present application first combines difficult sample perception and preference data fine-tuning, simultaneously proposes a two-stage method for constructing high-quality training data, and introduces multiple evaluation indexes to evaluate and optimize model performance. Compared with other legal large language models, the present application has innovation and uniqueness in algorithm and practical application level, and its performance is at the leading level in the industry, providing an innovative solution for the application of intelligent justice in legal consultation. BRIEF DESCRIPTION OF DRAWINGS

[0054] Figure 1 A flowchart of the method of the present application is shown.

[0055] Figure 2 A schematic diagram of the construction of training data in the method of the present application is shown.

[0056] Figure 3 A system block diagram of the present application is shown. DETAILED DESCRIPTION

[0057] In order to make the above-mentioned purposes, features and advantages of the present application more obvious and easy to understand, the specific embodiments of the present application will be described in detail below with reference to the accompanying drawings. In the following description, many specific details are set forth in order to provide a thorough understanding of the present application. However, the present application can be implemented in many other ways different from those described herein, and those skilled in the art can make similar improvements without departing from the scope of the present application, so the present application is not limited to the specific embodiments disclosed below. The technical features in each embodiment of the present application can be combined accordingly without conflict.

[0058] In the description of the present application, it should be understood that the terms "first", "second" are only used for the purpose of description, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of indicated technical features. Therefore, the features defined with "first", "second" can explicitly or implicitly include at least one of the features.

[0059] As shown in Figure 1 In a preferred implementation of the present application, the above-mentioned legal fact-based enhancement method based on large language model includes the following S1-S4 steps. The specific implementation process will be described in detail below.

[0060] S1: Obtain legal consultation questions and form an initial question set. After preprocessing the initial question set, obtain a processed question set. Input each legal consultation question in the processed question set and a pre-constructed first prompt word into the large language model, output the initial answer for each legal consultation question, use regular expressions to extract all legal provisions mentioned in each initial answer, and find the legal provisions with the most similar semantics to the legal provisions of each initial answer in the total text set as the true legal provisions. Input each legal consultation question in the processed question set, the true legal provisions of each initial answer, and a pre-constructed second prompt word into the large language model, output the legal provisions that are only related to each legal consultation question in the processed question set and form a revised legal provision set. Input each legal consultation question in the processed question set, the initial answer, the revised legal provision set, and the pre-constructed third prompt word into the large language model to revise the initial answer to obtain a revised answer. Each legal consultation question in the processed question set and its corresponding revised answer constitute the first training data set.

[0061] It should be noted that, in the embodiment of the present invention, the legal consultation questions contained in the public data set are extracted, and the extracted legal consultation questions constitute the initial question set Q part1 ={q part1,1 ,q part1,2 …,q part1,n1},q part1,1 ,q part1,2 …,q part1,n1 represents the 1st, 2nd…, n1th legal consultation questions in the initial question set, where n1 is the number of legal consultation questions contained in the initial question set.

[0062] It should be noted that, in the embodiment of the present invention, the initial question set Q part1 Each question in the question set is filtered for length and checked for semantics and syntax. After this preprocessing, the final processed question set Q is obtained. part1 ′={q′ part1,1 ,q′ part1,2 …,q′ part1,m1}, expressed as:

[0063] Q part1 ′=f sem&syn (f length (Q part1 ))

[0064] Where q′ part1,1 ,q′ part1,2 …,q′ part1,m1 represents the 1st, 2nd, ..., m1th legal consultation questions in the processed question set, where m1 is the number of legal consultation questions in the processed question set; f lengthrepresents a length filtering function, which is used to perform length filtering on each legal consultation question in the initial question set, and directly eliminates the legal consultation question with a length less than a preset length threshold, and in the embodiment, the length threshold is set to 25;f sem&syn represents a semantic and syntactic checking function, which is used to perform semantic and syntactic checking on the result after length filtering, and eliminate the legal consultation question with incorrect semantics and syntax errors.

[0065] It should be noted that in the embodiment of the present application, each legal consultation question in the processed question set and the first prompt word constructed in advance are provided to the large language model, and the large language model is required to generate an initial answer a LLM_ori,1 ,a LLM_ori,2 …,a LLM_ori,m1 If the inference process of the large language model (LLM) is represented as a function LLM(·), then the process of generating an initial answer using LLM can be represented as:

[0066] a LLM_oril,i1 = LLM(q ′ part1,i1 ), i1∈{1,…,m1}

[0067] where i1∈{1,…,m1} represents the index of each legal consultation question in the initial answer or the processed question set; a LLM_oril,i1 represents the initial answer generated by the large language model for the legal consultation question q′ part1,i1 .

[0068] It should be noted that due to the timeliness and breadth of the internal legal knowledge of the large language model, the initial answer directly generated by the large language model often has a large number of hallucinations in the part related to the legal provisions. In order to collect higher quality training data, the subsequent steps avoid the drawbacks of traditional use of large language models to collect training data in a clever way.

[0069] It should be noted that in the present application, regular expressions are used to extract all legal provisions contained in each initial answer. In the embodiment, each initial answer contains p i1 legal provisions, and a first set corresponding to each initial answer is formed by these legal provisions. Among them, the first set corresponding to the initial answer a LLM_oril,i1 can be represented as:

[0070]

[0071] wherein, represents the 1,2,…,p i1 legal provisions contained in the first set, and p i1 represents the number of legal provisions contained in the initial answer.

[0072] It should be noted that, in the present invention, the total set of texts of all laws and regulations is L = {l1, l2, ..., l T}, the total text set contains T codes (i.e. l1, l2, ..., l T ), Code i By N i Article j in the i-th code By the corresponding code name t name 、Article No. t number and the name of the law content Composition, i∈{0,…,T}, j∈{0,…,N i}, N i is the number of legal articles contained in the i-th code, and T is the total number of codes in the total text set. In this embodiment, the legal articles and regulations revised as of January 1, 2024, constitute the above-mentioned total text set.

[0073] It should be noted that, in step S1 of the present invention, the specific process of searching for the legal provision with the most similar semantics to each initial answer in the total text set as the actual legal provision is as follows:

[0074] S11. Each legal article from the initial answer is embedded by the trained encoder to obtain a first embedding representation, and each legal article from the total text set is embedded by the trained encoder to obtain a second embedding representation.

[0075] In this embodiment, each legal article from the initial answer is embedded by the trained encoder, and each legal article from the initial answer will obtain an embedded representation, namely the first embedded representation. LLM_oril,i1 The corresponding first set l extract,i1 For example, the encoder E(·) is used to encode each piece of data from a LLM_oril,i1 Laws Embed and get the first embedding representation

[0076] In this embodiment, each legal article from the pre-built text collection is embedded by the trained encoder, and each legal article from the text collection will receive an embedding representation, namely the second embedding representation. For example, the corresponding second embedding representation It can be written as:

[0077]

[0078] in, Respectively represent the jth article in the i-th code the first embedding representation of the 1st, 2nd, …, Nth element in the second embedding representation. j

[0079] S12, for each statute from the initial answer, the cosine similarity between the corresponding first embedding representation and the second embedding representation of each statute in the total set of texts is calculated in turn, and the statute corresponding to the second embedding representation with the largest cosine similarity is taken as the true statute.

[0080] It should be noted that in the present application, each legal consultation question q' part1,i1 in the processed question set, the true statute of each initial answer and the pre-constructed second prompt are input into the large language model, and the LLM is used to determine whether the true statute of each initial answer is related to each legal consultation question in the processed question set. The statute output by the large language model only related to each legal consultation question in the processed question set and constitutes the revised statute set l revise,i1 .

[0081] It should be noted that in the present application, each legal consultation question in the processed question set, the initial answer, the revised statute set {q' part1,i1 ,a LLM_oril,i1 ,l revise,i1} and the pre-constructed third prompt are input into the large language model to revise the initial answer, and the revised answer a LLM_revised,i1 is obtained:

[0082] a LLM_revised,i1 = LLM(q' part1,i1 ,a LLM_oril,i1 ,l revise,i1 )

[0083] It should be noted that in the present application, as shown in Figure 2 , the above initial question set is preprocessed to extract legal consultation questions with proper semantics and appropriate length. The large language model is used to generate the initial answer (initial answer) of the above legal consultation question. Subsequently, specific rules (i.e. regular expressions) are used to identify the legal clause part in the initial answer, and the embedding method is used to search the true statute with the closest semantic to the initial answer from the statute library (i.e. the total set of texts) to calibrate the content of the initial answer, and finally generate a high-quality revised answer, thereby effectively reducing the probability of hallucination content generated by the large language model. Thus, through the above process, each legal consultation question in the processed question set and its corresponding revised answer constitute the first training data set D1 = {(q' part1,1 ,a LLM_revised,1 ),..,(q' part1,m1 ,a LLM_revised,m1 ​)}. However, due to the data distribution of the public dataset and the large number of legal provisions, the first training dataset cannot fully cover all legal provisions. In order to collect more comprehensive training data, the subsequent steps propose a data supplement strategy for the first training dataset D1 and obtain the second training dataset D2. LLM_revised,1 ,...,a LLM_revised,m1 This means that the initial answer a LLM_ori,1 ,…,a LLM_ori,m1 The corrected answer obtained after making the correction.

[0084] S2: Generate an extended set of legal provisions from the first training data set, input the legal provisions in the extended set of legal provisions and the pre-constructed fourth prompt word into the large language model, obtain legal consultation questions related to the legal provisions in the extended set of legal provisions and form an extended question set, input each legal provision in the extended set of legal provisions and its corresponding extended question set together with the pre-constructed fifth prompt word into the large language model, obtain reference answers, and each legal consultation question in the extended question set and its corresponding reference answer constitute the second training data set.

[0085] It should be noted that, in the present invention, the specific process of generating an extended set of laws from the first training data set is as follows: determining whether the number of occurrences of each law in the first training data set is less than a preset threshold: if not, no processing is performed; if less, the laws that are less than the threshold are treated as insufficiently trained laws, and all the insufficiently trained laws constitute the extended set of laws. expand ={w exp,1 ,w exp,2 ,...,w exp,q1}, q1 is the number of laws in the extended set of laws.

[0086] In this embodiment, the laws in the extended set of laws are all laws that are found, after analysis, to have not been mentioned or not fully covered in the first training data set.

[0087] It should be noted that in the present invention, it is assumed that training data related to each article in the extended set of articles is generated. First, the articles in the extended set of articles and the pre-built fourth prompt word are input into the large language model to obtain legal consultation questions related to the articles in the extended set of articles and form an extended question set. b ∈{1,…,q1} laws For example, provide it together with the prompt word to LLM and ask LLM to return the legal provisions. Related Q b Legal advice questions and constitute an extended set of questions

[0088]

[0089] Then each law article in the extended law article set and its corresponding set of extended questions are input into the large language model together with the pre-constructed fifth prompt word, and the reference answers of each legal consultation question in the set of extended questions are obtained. In the process of generating reference answers, since the reference law articles are provided in the context of the LLM x ∈{1,…,Q b} legal consultation questions are taken as examples, which are provided to the large language model together with the law articles and the corresponding prompt words, and the LLM is required to generate reference answers according to the law articles

[0090]

[0091] Therefore, as shown in Figure 2 , for law articles that have not been fully covered, legal consultation questions related to these specific law articles are generated using the large language model and the prompt words. Then the law articles and the newly generated legal consultation questions are given to the large language model for answering, and the reference answers of the newly generated legal consultation questions are obtained, and the data covering the law articles are constructed, and finally the second training data set D2 is obtained:

[0092]

[0093] S3: using a supervised fine-tuning method to fine-tune the large language model base on the first training data set and the second training data set, and obtaining the fine-tuned model M SFT .

[0094] It should be noted that in the present application, the large language model specifically refers to a large language model that is currently on the market with stable general-purpose capabilities, mature commercial and closed-source, which is used for judgment, used for generating initial answers in training data and correcting final answers. The large language model base refers to the large language model that has been open source and pre-trained.

[0095] It should be noted that in the process of fine-tuning the large language model base, the first training data set and the second training data set are combined and then randomly sorted to generate the fine-tuning data set D SFTThe legal consultation question contained in the fine-tuning data set is input into the large language model base, the large language model base generates the answer result of the legal consultation question, the first negative log-likelihood loss is calculated based on the answer in the fine-tuning data set and the answer result of the legal consultation question generated by the large language model base, the parameters of the large language model base are updated and optimized based on the minimization of the first negative log-likelihood loss, until the preset iteration threshold is reached, the large language model base converges, and the fine-tuned model is obtained.

[0096] The first negative log-likelihood loss is in the form of a function:

[0097]

[0098] Wherein, y t represents the t-th word of the answer in the fine-tuning data set; y <t represents a word sequence composed of the first t-1 words of the answer in the fine-tuning data set; T represents the length of the answer in the fine-tuning data set; x represents the legal consultation question contained in the fine-tuning data set; p(y t |y <t ,x) represents the probability of predicting the t-th word as y <t given the legal consultation question x and the word sequence y t .

[0099] In this embodiment, the fine-tuning data set D SFT can be essentially regarded as being composed of binary tuples (x, y), where x is a legal consultation question and y is an answer. Further, the fine-tuning data set can be divided into a supervised fine-tuning (SFT) training set D SFT_Train , a validation set D SFT_Val and a test set D SFT_Test in a ratio of 8:1:1. Then in the training process, the parameters θ of the large language model base are updated and optimized by minimizing the negative log-likelihood loss, so that the large language model base can generate more expected outputs on given inputs. Finally, the fine-tuned model is obtained by setting appropriate training hyperparameters based on the fine-tuning data set D SFT and the negative log-likelihood loss.

[0100] S4: Use the reinforcement learning algorithm based on difficult sample perception to fine-tune the fine-tuned model M SFT for several iterations. In the f-th iteration round, a corresponding preference data set is constructed based on the three evaluation indicators, and the preference data set is divided into a difficult sample perception preference learning (HIPO) training set , a validation set and a test set calculating a total loss on the training set, performing parameter update on the fine-tuned model based on minimizing the total loss, using the model updated at each iteration round inference on the legal consultation questions in the test set, and evaluation of the inference results on three evaluation indexes until a preset iteration round threshold is reached or the model converges, to obtain the legal question and answer model.

[0101] It should be noted that in step S4 of the present application, three evaluation indexes are designed to evaluate the ability of the legal question and answer model and control the number of iterations of the legal question and answer model. The specific process of constructing the three evaluation indexes is as follows:

[0102] S41, obtaining D legal consultation questions, and recording the answer to be evaluated corresponding to each legal consultation question as a resp First, regular expressions are used to extract two elements in each answer to be evaluated. The first element to be extracted is all the test statutes mentioned in the answer to be evaluated and constitutes a test statute set The second element to be extracted is the suggestion part in the answer to be evaluated and constitutes a suggestion text s resp . Among them, represents the wth test statute, respectively represents the code name, statute number and statute name in the wth test statute.

[0103] In this embodiment, the regular expression needs to satisfy the relationship that the test statute set l resp is not part of the suggestion text s resp , so it strictly satisfies .

[0104] In this embodiment, the suggestion part in the answer to be evaluated specifically refers to the specific solution given to the legal consultation question raised by the user. This part often specifically expands on what the user should do. In order to distinguish from the extracted statute part, this part does not contain any statute and explanation of the statute.

[0105] S42, a triple is formed by the code name, statute number and statute name corresponding to the wth test statute respectively. When the triple of the wth test statute belongs to the total text set L, it is considered that the wth test statute has no hallucination, and the hallucination-free rate NHSR of the test statute is taken as the first evaluation index.

[0106] In this embodiment, the hallucination-free of the test statute specifically refers to the hallucination-free of the statute, which means that the test statute is real and not fabricated, i.e. the test statute is in the total text set.

[0107] In this embodiment, the purpose of proposing the evaluation index NHSR is to calculate the ratio of the number of test items without hallucinations to all test items among all the answers to be evaluated. When NHSR = 0, it means that all test items have hallucinations, while NHSR = 1 means that all test items have no hallucinations. Therefore, the calculation method of the hallucination-free rate NHSR of the test items is:

[0108]

[0109] Where, Represents the indicator function; ∧ represents simultaneous satisfaction; M resp Indicates the total number of all tested legal clauses in all the answers to be evaluated.

[0110] S43. The law to be tested that has hallucinations is considered irrelevant. If there is no hallucination, it is recorded as Then the non-hallucination tested legal provisions and legal consultation questions are formed into two tuples Together with the pre-built sixth prompt word, it is input into the large language model, and the large language model determines the test clause without hallucination. Whether it is relevant to the legal consultation question Q, a scalar value used to represent the relevance judgment result is returned, and the correlation between the legal provision to be tested and the legal consultation question raised by the user is used as the second evaluation indicator.

[0111] In this embodiment, the user needs a legal provision that is highly relevant to the legal consultation question raised, rather than a legal provision that is merely correct. Therefore, using only the evaluation index in S42 has certain limitations. Therefore, the present invention designs a second evaluation index, as specifically described in S43.

[0112] In this embodiment, the scalar value returned by the large language model is s w ∈{0,0.5,1}, where s w =0 means no correlation, s w =0.5 represents weak correlation, s w =1 represents a strong correlation. Therefore, the correlation REL between the tested legal provision and the legal consultation question raised by the user is calculated as follows:

[0113]

[0114] In general, the closer the correlation value is to the no-hallucination rate (NHSR), the higher the correlation is between the tested legal provisions in the answer to be evaluated and the legal consultation questions raised by the user.

[0115] In the present embodiment, the correlation between the law provision to be tested and the legal consultation question raised by the user refers to the need to directly quote or refer to the legal provision when providing legal consultation or solving the specific legal consultation question. In other words, when the law provision to be tested can be used as the basis for answering the legal consultation question without hallucination, it is considered that the two are correlated.

[0116] S44, input the legal consultation question Q and the text total set L into a retriever (Retriever, for example, BGE), and require the retriever to return d law provisions most relevant to the legal consultation question Q from the text total set L and form the current legal provision set l retriever The legal consultation question, the current legal provision set, and the suggestion text form a triple {Q, l retriever ,s resp} together with the seventh prompt word pre-built, and are input into the large language model, and the large language model is required to judge whether the suggestion text s resp is consistent with the current legal provision set l retriever , return a scalar value representing the consistency judgment result, and take the consistency between the suggestion text and the current legal provision set as the third evaluation index.

[0117] Further, the calculation method of the consistency LC between the suggestion text and the current legal provision set is as follows:

[0118]

[0119] In the formula, l w ∈{0,1} represents the scalar value returned by the large language model to represent the consistency judgment result, l w =0 represents inconsistency, and l w =1 represents consistency; D represents the number of legal consultation questions.

[0120] In the present embodiment, since each legal consultation question corresponds to an answer to be evaluated, and a suggestion text can be extracted from each answer to be evaluated, the total number of suggestion texts is equal to the total number of answers to be evaluated, that is, the total number of legal consultation questions. Therefore, the number D of legal consultation questions can also represent the number of answers to be evaluated, that is, the number of suggestion texts.

[0121] In the present embodiment, the consistency between the suggestion text and the current legal provision set refers to whether the suggestion part of the answer to be tested is consistent with the current legal provision set l retriever based on the legal consultation question raised by the user, if the suggestion part of the answer to be tested is consistent with the current legal provision set l retrieverContradiction, that is, the solution or opinion proposed in the advice of a certain legal consultation question is inconsistent with the existing legal provisions, even conflicting, then it is considered that the consistency of the advice part of the answer to be tested with the existing legal provision set is not strong, or the two are inconsistent.

[0122] It should be noted that in step S4 of the present application, the specific process of constructing the preference data set of the fth iteration round is as follows:

[0123] 1) The fine-tuning data set is divided into a supervised fine-tuning training set, a supervised fine-tuning test set, and a supervised fine-tuning validation set according to a preset proportion, and the legal consultation questions and answers in the supervised fine-tuning training set are respectively taken as the question part Q SFT_Train and the high-quality answer part A SFT_Train , and the original data set of the fth iteration round is composed of the question part and the high-quality answer part.

[0124] Q SFT_Train ={q Train,1 ,q Train,2 ,...,q Train,C}

[0125] A SFT_Train ={a Train,1 ,a Train,2 ,...,a Train,C}

[0126] Where C is the number of data contained in the supervised fine-tuning training set; q Train,1 ,q Train,2 ,...,q Train,C represent the 1st, 2nd,..., Cth legal consultation questions in the question part; a Train,1 ,a Train,2 ,...,a Train,C represent the 1st, 2nd,..., Cth answers in the high-quality answer part.

[0127] 2) The question part is input into the model updated in the f-1th iteration round to perform inference, and the answers generated by the model updated in the f-1th iteration round , the legal consultation questions in the question part, and the answers in the high-quality answer part form a triple and constitute an intermediate data set.

[0128] In this embodiment, the question part is input into the model updated in the f-1th iteration round to perform inference, and the process of generating answers by the model updated in the f-1th iteration round can be represented as:

[0129]

[0130] A i_resp ={a f_resp,1 ,a f_resp,2 ,...,a f_resp,C}

[0131] wherein, denotes the inference process using the model updated by the f-1th iteration round; q Train,c denotes the cth question from Q SFT_Train , a f_resp,c denotes the answer generated by the model to the question q Train,c , c e {1,...,C}; A f_resp denotes the answer set generated by the model to all questions in Q SFT_Train .

[0132] In this embodiment, the answer generated by the model updated by the f-1th iteration round, the legal consultation question in the question part, and the answer in the high-quality answer part form a triple and constitute an intermediate data set:

[0133]

[0134] 3) Take each answer generated by the model updated by the f-1th iteration round as a to-be-detected answer, extract the statute part and the suggestion part in the to-be-detected answer by using a regular expression, if the statute part in a to-be-detected answer contains hallucination, or the statute part is irrelevant to the legal consultation question, or the suggestion part in the to-be-detected answer is inconsistent with the text total set, then add the triple corresponding to the to-be-detected answer to the original data set of the fth iteration round to update the original data set, and finally obtain the preference data set of the fth iteration round.

[0135] In this embodiment, take the answer a f_resp,c generated by the model updated by the f-1th iteration round to the cth question as an example, extract the statute part l f_resp,c and the suggestion part s f_resp,c in it by using a regular expression. Then use the indexes (NHSR, REL, LC) proposed in S42-S44 above to judge whether the statute part l f_resp,c in the answer a f_resp,c contains hallucination and is relevant to the legal consultation question, and whether the suggestion part s f_resp,c in the answer a f_resp,cwhether consistent with the total set of texts L, finally the triplets satisfying the following conditions will be added to the original dataset of the fth iteration round to update the original dataset, and finally the preference dataset of the fth iteration round is obtained:

[0136]

[0137] wherein, NHSR(l f_resp,c ), REL(l f_resp,c ) and LC(s f_resp,c ) represent the hallucination rate of the law article to be tested of the answer a f_resp,c , the correlation between the law article to be tested and the legal consultation question raised by the user, and the consistency of the suggestion text and the total set of texts respectively.

[0138] Thus, after obtaining the preference dataset of the fth iteration round, it is randomly sorted, and the data is divided in the ratio of 8:1:1 to serve as the training set , the verification set and the test set of the hard sample perception preference learning (HIPO) respectively. The preference dataset can be essentially regarded as being composed of triplets (x, y w , y l ), wherein x is a legal consultation question, y w is a preferred answer, and y l is a rejected answer. Here, the preferred answer specifically refers to the answer in the high-quality answer part corresponding to the fine-tuning dataset D SFT , and the rejected answer specifically refers to the answer obtained by reasoning using the model trained in the last iteration round.

[0139] It should be noted that in step S4 of the present application, the function form of the total loss is as follows:

[0140]

[0141] In the present embodiment, the total loss is obtained by weighted summation of the second negative log-likelihood loss and the direct preference optimization loss. Specifically, the function form of the second negative log-likelihood loss is as follows:

[0142]

[0143] The direct preference optimization (DPO) loss uses the model M f-1 of the last iteration round as a reference model, and the calculation formula is as follows:

[0144]

[0145] In the above formula, a and β represent weight hyperparameters; ω represents an adjustment parameter; represents a second negative log-likelihood loss, represents a direct preference optimization (DPO) loss; |y w represents the length of y w ; σ is a sigmoid activation function; M θ (y w |x) represents the probability that the model computes the output as y w given the legal consultation question x according to its current parameters θ; M θ (y l |x) represents the probability that the model computes the output as y l given the legal consultation question x according to its current parameters θ; M f (y w |x) represents the probability that the model computes the output as y w given the legal consultation question x according to its current parameters θ; M f (y l |x) represents the probability that the model computes the output as y l given the legal consultation question x according to its current parameters θ.

[0146] In this embodiment, the greater the value of the adjustment parameter ω, the greater the gap between positive and negative samples is amplified, and the model will more strongly learn to distinguish between them; if ω takes a small value, the gap is reduced, and the learning process of the model will be smoother.

[0147] It should be noted that in step S4, after obtaining the model updated in each iteration round, the legal consultation questions in the test set are inferred, and it is determined whether the current iteration number has reached the preset maximum iteration number:

[0148] If so, stop iteration and output the trained legal question and answer model;

[0149] If the preset maximum iteration number has not been reached, the values of the three evaluation indicators in S42-S44 are calculated on the test set, and it is further determined whether the three evaluation indicators have reached the expectation: if all of them have reached the expectation, stop iteration and output the trained legal question and answer model; if one or more of the evaluation indicators have not reached the expectation, the evaluation indicator NHSR f , REL f , LC f of the fth iteration round and the evaluation indicators NHSR f-1 , REL f-1 , LC f-1 of the f-1th iteration round are compared, and the one with the smaller value is selected as the evaluation indicator of the fth iteration round.When all three evaluation indexes decrease, the iteration is stopped, and the trained legal Q&A model is output; otherwise, the training is continued until the preset maximum number of iterations is reached or the model converges.

[0150] The application will be described below through a specific example to show the application effect of the legal Q&A factual enhancement method based on a large language model described in S1-S4 in the above embodiments on a specific data set, so as to understand the essence of the application.

[0151] Embodiments

[0152] The specific implementation process of the legal Q&A factual enhancement method based on a large language model used in this embodiment is as described above and will not be repeated here.

[0153] This embodiment is tested on a manually annotated data set. The data set contains 1988 legal questions, covering more than 800 different legal provisions. Each data in the data set consists of three parts: legal consultation questions, reference articles and reference answers. The statistical results of the above data set are shown in Table 1.

[0154] Table 1. Statistical results of the test data set

[0155] Civil Consultation Criminal Consultation Total Total number of statutes involved 750 85 835 Number of questions 1,384 604 1,988 Average length of questions 172.52 299.37 211.06 Average length of reference answers 471.43 295.20 417.89

[0156] This embodiment uses the open source model GLM4 Chat 9B[1] as the base. In order to objectively evaluate the performance of the method of the application, this embodiment uses the closed source model GPT4o[2] and the open source model Llama3.1 405B[3] as the baseline. At the same time, the method proposed in the application is also compared with the traditional supervised fine-tuning (SFT) model and the direct preference optimization[4](DPO) model. The indexes NHSR, REL and LC proposed in S1, as well as the traditional evaluation indexes METEOR[5], BERTScore[6] and Rouge-L[7] are used for evaluation. The experimental results are shown in Table 2, wherein the SFT model is trained for 2 rounds, the DPO model and the method of the application are trained for 3 rounds. The results show that the method proposed in the application has good performance in answering the facts and helping, and exceeds the benchmark model.

[0157] Table 2. Overall performance

[0158]

[0159] [1] Glim T, Zeng A, Xu B, et al. ChatGLM: A Family of Large Language Models from GLM-130B to GLM-4 All Tools [DB / OL]. (2024-07-30) [2024-11-13]. https: / / arxiv.org / abs / 2406.12793.

[0160] [2] OpenAI, et al. GPT-4 Technical Report

[0161] [DB / OL]. (2024-05-04) [2024-11-13]. https: / / arxiv.org / abs / 2303.08774.

[0162] [3] Dubey A, et al. The Llama 3 Herd of Models [DB / OL]. (2024-08-15) [2024-11-13]. https: / / arxiv.org / abs / 2407.21783.

[0163] [4] Rafailov R, Rafael, et al. Advances in Neural Information Processing Systems 36: Direct Preference Optimization: Your Language Model is Secretly a Reward Model [C]. 2024.

[0164] [5] Satanjeev B. METEOR: An Automatic Metric for MT Evaluation with Improved Correlation with Human Judgments [J]. ACL-2005, 2005: 228-231.

[0165] [6] Zhang T, Kishore V, Wu F, et al. BERTScore: Evaluating Text Generation with BERT [C] / / International Conference on Learning Representations. 2020.

[0166] [7] Lin C Y. ROUGE: A Package for Automatic Evaluation of summaries [C] / / In Proceedings of the Workshop on Text Summarization Branches Out (WAS 2004). 2004. DOI: doi:http: / / dx.doi.org / .

[0167] In summary, the present application provides a legal Q&A factuality enhancement method based on a large language model, which enhances the factuality of legal Q&A in the following two stages:

[0168] 1) SFT stage: the present application uses a supervised fine-tuning method to fine-tune the large language model base on the first training data set and the second training data set, and uses the high-quality training data in the two data sets to enable the large language model base to better understand legal questions and generate higher-quality answers.

[0169] 2) Reinforcement learning stage: specifically, this stage improves the fine-tuned model obtained after the SFT stage in the following two aspects. First, in the selection of samples for iterative training, the present application detects the answers generated by the updated model in the last round in three evaluation indicators to determine whether each evaluation indicator meets the corresponding condition. And the samples corresponding to the answers that meet the conditions are excluded from the training set. With the advancement of iteration, simple samples are gradually filtered out, and only more challenging samples are finally retained. Training challenging samples again can enable the model to better master complex problems to improve legal Q&A factuality. Second, the present application focuses on unlearned information. The positive example samples are mostly content that the model has not yet mastered. The goal of the present application is to ensure that the model learns non-illusory legal provisions, so in the HIPO stage, the NLL loss and direct preference optimization loss are introduced to strengthen the model's ability to learn from positive examples.

[0170] Similarly, based on the same inventive concept, another preferred embodiment of the present application also provides a legal Q&A factuality enhancement system based on a large language model corresponding to the legal Q&A factuality enhancement method based on a large language model provided by the above-mentioned embodiments, as shown in Figure 3 It includes:

[0171] A data acquisition module for acquiring legal consultation questions raised by users;

[0172] A result acquisition module for inputting the legal consultation questions raised by the user into the legal Q&A model trained by the legal Q&A factuality enhancement method based on a large language model shown in the above-mentioned embodiments, and outputting the answers to the legal consultation questions.

[0173] Similarly, based on the same inventive concept, another preferred embodiment of the present invention further provides a computer electronic device corresponding to the method for enhancing the factuality of legal question answering based on a large language model provided in the above embodiment, which includes a memory and a processor;

[0174] The memory is used to store computer programs;

[0175] The processor is configured to implement the method for enhancing the factuality of legal question answering based on a large language model in the above embodiment when executing the computer program.

[0176] Furthermore, the logic instructions in the aforementioned memory can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, or the portion that contributes to the prior art, or a portion 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 several instructions for causing a computer device (which can be a personal computer, server, or network device, etc.) to execute all or part of the steps of the method described in each embodiment of the present invention.

[0177] It is understandable that the above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, and discrete hardware components.

[0178] It should also be noted that those skilled in the art can clearly understand that for the convenience and brevity of description, the specific working process of the system described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the various embodiments provided in this application, the division of steps or modules in the system and method is only a logical function division. In actual implementation, there may be other division methods, for example, multiple modules or steps can be combined or integrated together, and a module or step can also be split.

[0179] The above-described embodiments are only the preferred ones of the present application, and are not intended to limit the present application. Those skilled in the art can make various changes and modifications without departing from the spirit and scope of the present application. Therefore, any technical solutions obtained by equivalent replacement or equivalent transformation shall fall within the protection scope of the present application.

Claims

1. A large language model-based legal question and answer fact enhancement method, characterized in that, The method comprises the following steps: S1: obtaining legal consultation questions and forming an initial question set, preprocessing the initial question set to obtain a processed question set, inputting each legal consultation question in the processed question set and a first prompt word constructed in advance into a large language model, outputting an initial answer of each legal consultation question, extracting all laws mentioned in each initial answer by using a regular expression, finding a law with the most similar semantics to the law of each initial answer in a total text set as a real law, inputting each legal consultation question in the processed question set, the real law of each initial answer and a second prompt word constructed in advance into the large language model, outputting a law related only to each legal consultation question in the processed question set and forming a revised law set, inputting each legal consultation question in the processed question set, the initial answer, the revised law set and a third prompt word constructed in advance into the large language model to revise the initial answer, and obtaining a revised answer, wherein the first training data set is formed by each legal consultation question in the processed question set and the corresponding revised answer; S2: generating an extended law set from the first training data set, inputting the laws in the extended law set and a fourth prompt word constructed in advance into the large language model, obtaining legal consultation questions related to the laws in the extended law set and forming an extended question set, inputting each law in the extended law set and the corresponding extended question set together with a fifth prompt word constructed in advance into the large language model, obtaining a reference answer, and forming the second training data set by each legal consultation question in the extended question set and the corresponding reference answer; S3: using a supervised fine-tuning method to fine-tune the large language model base on the first training data set and the second training data set, and obtaining a fine-tuned model; S4: using a reinforcement learning algorithm based on difficult sample perception to iteratively fine-tune the fine-tuned model for several times, in each iteration round, a corresponding preference data set is constructed based on three evaluation indexes, and the preference data set is divided according to a preset proportion to obtain a training set, a validation set and a test set for difficult sample perception preference learning, the total loss is calculated on the training set, the parameters of the fine-tuned model are updated based on minimizing the total loss, the legal consultation questions in the test set are inferred using the model updated in each iteration round, and the inference results are evaluated on the three evaluation indexes until a preset iteration threshold is reached or the model converges, and a legal question and answer model is obtained.

2. The legal Q&A fact enhancement method based on a large language model according to claim 1, wherein, In step S1, a total set of texts of all statutes and regulations is acquired, the total set of texts including T legal codes, the i-th legal code being denoted as i consisting of N i statutes, the j-th statute in the i-th legal code being denoted as consisting of a corresponding legal code name, statute number and statute name; i∈{0,…,T}, j∈{0,…,N i}, N i being the number of statutes included in the i-th legal code, and T being the total number of legal codes in the total set of texts. The specific process of finding a law with the most similar semantics to the law of each initial answer in the total text set as a real law is as follows: S11, embedding each law from the initial answer by a trained encoder to obtain a first embedding representation, embedding each law from the total text set by a trained encoder to obtain a second embedding representation; S12, for each statute from the initial answer, sequentially calculate the cosine similarity between the corresponding first embedding representation and the second embedding representation of each statute in the total set of texts, and take the statute corresponding to the second embedding representation with the largest cosine similarity as the real statute.

3. The method of claim 1, wherein the method further comprises: In step S2, the specific process of generating the extended statute set from the first training data set is as follows: Determine whether the number of occurrences of each statute in the first training data set is less than a preset number threshold: if not, do not process; if yes, take the statutes with less number of occurrences as training insufficient statutes, and form the extended statute set from all training insufficient statutes.

4. The legal Q&A fact enhancement method based on a large language model of claim 1, wherein, In step S3, in the process of fine-tuning the large language model base, the first training data set and the second training data set are combined and then randomly sorted to generate a fine-tuning data set. The legal consultation question contained in the fine-tuning data set is input into the large language model base to generate the answer result of the legal consultation question. The first negative log-likelihood loss is calculated based on the answer in the fine-tuning data set and the answer result of the legal consultation question generated by the large language model base. The parameters of the large language model base are updated and optimized based on the minimization of the first negative log-likelihood loss until a preset iteration number threshold is reached. The large language model base converges to obtain the fine-tuned model.

5. The legal Q&A fact enhancement method based on a large language model according to claim 1, wherein, In step S4, the specific process of constructing three evaluation indexes is as follows: S41, obtain D legal consultation questions, and extract two elements in the to-be-evaluated answer corresponding to each legal consultation question using regular expressions. The first element is all to-be-tested statutes mentioned in the to-be-evaluated answer and constitutes a to-be-tested statute set. The second element is the suggestion part in the to-be-evaluated answer and constitutes a suggestion text; S42, the triple is composed of the code name, statute number and statute name corresponding to the wth to-be-tested statute. When the triple of the wth to-be-tested statute belongs to the total set of texts, it is considered that the wth to-be-tested statute has no illusion, and the illusion-free rate of the to-be-tested statute is taken as the first evaluation index; S43, the to-be-tested statute with illusion is regarded as irrelevant, and the to-be-tested statute without illusion and the legal consultation question constitute a binary tuple, which is input into the large language model together with the pre-constructed sixth prompt word. The large language model judges whether the to-be-tested statute without illusion and the legal consultation question have relevance, returns a scalar value representing the relevance judgment result, and takes the relevance between the to-be-tested statute and the legal consultation question proposed by the user as the second evaluation index; S44, input the legal consultation question and the total set of texts into the retriever, and return the d statutes most relevant to the legal consultation question from the total set of texts to form a current legal provision set. Form a triple of the legal consultation question, the current legal provision set and the suggestion text, and input it into the large language model together with the pre-constructed seventh prompt word. The large language model is required to judge whether the suggestion text is consistent with the current legal provision set based on the legal consultation question, and returns a scalar value representing the consistency judgment result. The consistency between the suggestion text and the current legal provision set is taken as the third evaluation index.

6. The legal Q&A fact enhancement method based on a large language model according to claim 5, wherein, In step S4, the specific process of constructing the preference data set of the fth iteration round is as follows: The fine-tuning data set is divided into a supervised fine-tuning training set, a supervised fine-tuning test set, and a supervised fine-tuning validation set according to a preset ratio. The legal consultation questions in the supervised fine-tuning training set and the answers are taken as the question part and the high-quality answer part, respectively. The question part and the high-quality answer part form an original data set of the fth iteration round. The question part is input into the model updated in the f-1th iteration round for inference. The answers generated by the model updated in the f-1th iteration round, the legal consultation questions in the question part, and the answers in the high-quality answer part form triples and constitute an intermediate data set. Each answer generated by the model updated in the f-1th iteration round is taken as a to-be-detected answer. The statute part and the suggestion part in the to-be-detected answer are extracted by using a regular expression. If the statute part in the to-be-detected answer contains hallucination, or the statute part is irrelevant to the legal consultation question, or the suggestion part in the to-be-detected answer is inconsistent with the text total set, the triple corresponding to the to-be-detected answer is added to the original data set of the fth iteration round to update the original data set. Finally, the preference data set of the fth iteration round is obtained.

7. The method of claim 6, wherein the method further comprises: In step S4, the answer in the preference dataset is formed by the legal consultation question x, the preferred answer y w and the rejected answer y l The formed triplets constitute, the total loss The function form is: where a and b represent weight hyperparameters; w represents a tuning parameter; represents the second negative log-likelihood loss, represents the direct preference optimization loss; |y w | represents the length of y w ; s is a sigmoid activation function; M θ (y w ) represents the probability that the model computes an output of y w given the legal consultation question x according to its current parameters θ ; M l (y l ) represents the probability that the model computes an output of y f given the legal consultation question x according to its current parameters w ; M w (y f ) represents the probability that the model computes an output of y l given the legal consultation question x according to its current parameters l updated at the previous iteration.

8. The legal Q&A fact enhancement method based on a large language model of claim 7, wherein, In step S4, after obtaining the model updated in each iteration round, the legal consultation questions in the test set are inferred, and it is determined whether the current iteration number reaches a preset maximum iteration number: If yes, the iteration is stopped, and the trained legal question and answer model is output. If no, the three evaluation index values in S42-S44 are calculated on the test set, and it is determined whether the three evaluation indexes reach an expectation: if yes, the iteration is stopped, and the trained legal question and answer model is output; if one or more evaluation indexes do not reach the expectation, the evaluation indexes of each iteration round and the evaluation indexes of the last iteration round are compared. When all the three evaluation indexes decrease, the iteration is stopped, and the trained legal question and answer model is output; otherwise, the training is continued until the preset maximum iteration number is reached or the model converges.

9. A large language model-based legal question and answer fact enhancement system, characterized in that, The method comprises the following steps: The data acquisition module is configured to acquire a legal consultation question raised by a user. The result acquisition module is configured to input the legal consultation question raised by the user into a legal question and answer model trained based on the legal question and answer fact-based enhancement method based on a large language model according to any one of claims 1-8, and output an answer to the legal consultation question.

10. A computer electronic device, comprising: The memory is configured to store a computer program. The processor is configured to implement the legal question and answer fact-based enhancement method based on a large language model according to any one of claims 1-8 when executing the computer program. The memory is configured to store a computer program. The processor is configured to implement the legal question and answer fact-based enhancement method based on a large language model according to any one of claims 1-8 when executing the computer program.

Citation Information

Patent Citations

  • RGBT target tracking method based on difficult sample perception

    CN112801182A

  • Legal consultation reply method and legal field generative large model training method

    CN116822591A