Legal question and answer factuality enhancement method, system and equipment based on large language model

By adopting a legal question-and-answer factual enhancement method based on large language models in the legal question-and-answer model, the problem that legal question-and-answer models in the prior art is easily generated by ‘illusion problems’, and the accuracy and reliability of the legal question-and-answer model are improved.

CN119621891AActive Publication Date: 2025-03-14ZHEJIANG UNIV +1

Patent Information

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

AI Technical Summary

Technical Problem

In the prior art, legal question and answer models based on large language models are prone to 'illusion problems', that is, the generated answers do not match the actual legal facts, resulting in insufficient accuracy and reliability of legal question and answers.

Method used

A legal question-and-answer factual enhancement method based on large language models is adopted. By obtaining legal consulting questions and preprocessing them, regular expressions are used to extract the laws in the initial answer, finding the real laws with the most similar semantics, correcting the answers, and iteratively fine-tuning the model through supervised fine-tuning and reinforcement learning algorithms based on difficult sample perception to improve the factuality of the model.

Benefits of technology

It significantly improves the accuracy and reliability of the legal question-and-answer model, reduces the occurrence of "illusion problems", and improves the application performance of the model in legal consultation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119621891A_ABST
    Figure CN119621891A_ABST
Patent Text Reader

Abstract

The invention 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 judicial. The method comprises the following steps: firstly, constructing a first training data set and a second training data set based on a large language model, and performing fine tuning on a large language model base on the first training data set and the second training data set by using a supervised fine tuning method to obtain a fine-tuned model; and utilizing a reinforcement learning algorithm based on difficult sample perception to carry out iteration fine tuning on the fine-tuned model for a plurality of times until a preset iteration round threshold value is reached or the model is converged, and obtaining a legal question and answer model. According to the method, two-stage high-quality training data are constructed, the legal question and answer factuality is enhanced based on the reinforcement learning algorithm difficult to perceive by samples, and the method has a good application prospect.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of intelligent justice, and in particular relates to a method, system and device for enhancing the factuality of legal question and answer based on a large language model. Background Art

[0002] With the popularization of the Internet and the continuous improvement of people's legal awareness, the demand for basic legal consultation in my country is increasing. However, in reality, not all consultants can consult professional legal practitioners offline in a timely manner when they encounter legal problems. At the same time, not all consultants are willing to pay high fees for legal consultation. There is an obvious contradiction between the limited legal practitioner resources and the growing demand for legal consultation.

[0003] In this context, the development of artificial intelligence technology, especially the progress of large language models, has provided 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 consulting services online, so as to better safeguard their own rights and interests. This trend indicates that intelligent justice has gradually become an important field for the implementation of artificial intelligence technology, and has provided innovative solutions to alleviate the shortage of judicial resources.

[0004] Legal consultation tasks (i.e., question-answering tasks) are a core task in the field of intelligent justice. This task requires the model to automatically generate corresponding answers and cite relevant legal provisions based on the questions input by the consultant. 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 laws from a large number of legal cases to automatically generate answers to new questions. Although methods based on deep neural networks (such as pre-trained models and large language models) have made certain progress in understanding legal terms and applying legal knowledge, due to the professional and timeliness limitations of training data and the lack of effective alignment with legal experts, the model is prone to "hallucination problems", that is, the generated answers do not match the actual legal facts. Therefore, how to design a legal question-answering method that can enhance the factuality of the legal field based on the large language model has become a key issue in promoting judicial popularization and achieving the long-term development of intelligent justice. Summary of the invention

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

[0006] In order to achieve the above-mentioned invention object, the present invention specifically adopts the following technical solutions:

[0007] In a first aspect, the present invention provides a method for enhancing the factuality of legal question answering based on a large language model, which comprises the following steps:

[0008] 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 to 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 real legal provisions. Input each legal consultation question in the processed question set, the real legal provisions of each initial answer, and a pre-constructed second prompt word into the large language model, output the legal provisions 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 a 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.

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

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

[0011] S4: Use the reinforcement learning algorithm based on hard sample perception to perform several iterations of fine-tuning on the fine-tuned model. In each iteration round, it is necessary to construct a corresponding preference data set based on the three evaluation indicators, and divide the preference data set according to a preset ratio to obtain the training set, validation set and test set of hard 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 consulting questions in the test set, and evaluate the inference results based on the three evaluation indicators until the preset iteration round threshold is reached or the model converges to obtain a legal question-answering model.

[0012] Based on the above 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 laws and regulations is obtained, and the total text set includes T codes, the i-th code l i By N i The jth article in the i-th code It is composed of the corresponding code name, article number and article name; i∈{0,…,T}, j∈{0,…,N i},N i is the number of articles contained in the i-th code, and T is the total number of codes in the total set of texts;

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

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

[0016] S12. For each legal provision from the initial answer, calculate the cosine similarity between its corresponding first embedding representation and the second embedding representation corresponding to each legal provision in the total text set, and take the legal provision corresponding to the second embedding representation with the largest cosine similarity as the true legal provision.

[0017] As a preferred embodiment of the first aspect, in step S2, the specific process of generating the extended legal provisions set from the first training data set is:

[0018] Determine whether the number of occurrences of each legal provision in the first training data set is less than a preset number threshold: if not, no processing is performed; if less, the legal provisions less than the number threshold are regarded as insufficiently trained legal provisions, and all insufficiently trained legal provisions constitute an extended legal provision set.

[0019] As a preferred embodiment of the above-mentioned 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 merged and then randomly sorted to generate a fine-tuning data set, the legal consultation questions contained in the fine-tuning data set are input into the large language model base, and the large language model base generates answers to the legal consultation questions. The first negative log-likelihood loss is calculated based on the answers in the fine-tuning data set and the answers to the legal consultation questions generated by the large language model base, and the parameters of the large language model base are updated and optimized based on minimizing the first negative log-likelihood loss until a preset threshold number of iterations is reached, the large language model base converges, and a fine-tuned model is obtained.

[0020] Furthermore, the first negative log-likelihood loss The functional form is:

[0021]

[0022] Among them, y t represents the tth word of the answer in the fine-tuning dataset; y <t represents a word sequence consisting of the first t-1 words of the answer in the fine-tuning dataset; T represents the length of the answer in the fine-tuning dataset; x represents the legal consultation question contained in the fine-tuning dataset; p(y t |y <t ,x) represents the given legal consultation question x and word sequence y <t Next, the large language model base predicts that the tth word is y t probability.

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

[0024] S41. Obtain D legal consulting questions, and use regular expressions to extract two elements in the answer to be evaluated corresponding to each legal consulting question, wherein the first element is all the laws to be tested mentioned in the answer to be evaluated and constitutes a set of laws to be tested, and the second element is the suggestion part in the answer to be evaluated and constitutes a suggestion text;

[0025] S42, a triplet is formed by the code name, the article number and the article name corresponding to each of the w-th article to be tested. When the triplet of the w-th article to be tested belongs to the total set of texts, it is considered that the w-th article to be tested has no hallucination, and the hallucination-free rate of the article to be tested is used as the first evaluation indicator;

[0026] Furthermore, the calculation method of the no hallucination rate NHSR of the tested law is:

[0027]

[0028] In the formula, represents the indicator function; ∧ represents simultaneous satisfaction; M resp It indicates the total number of all laws to be tested in all the answers to be evaluated; Indicates the code name, article number and article name in the wth article to be tested;

[0029] S43, the law to be tested with hallucinations is regarded as irrelevant, and the law to be tested without hallucinations and the legal consultation question are formed into a tuple, which is input into the large language model together with the pre-constructed sixth prompt word, and the large language model determines whether the law to be tested without hallucinations and the legal consultation question are relevant, and returns a scalar value used to represent the result of the relevance judgment, and uses the relevance between the law to be tested and the legal consultation question raised by the user as the second evaluation indicator;

[0030] Furthermore, the correlation between the tested legal provisions and the legal consultation questions raised by the user is calculated 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 means no correlation, s w =0.5 represents weak correlation, s w =1 represents strong correlation;

[0033] S44. The legal consultation question and the total set of texts are input into the search engine, which returns the d legal provisions that are most relevant to the legal consultation question from the total set of texts and forms a set of current legal provisions. The legal consultation question, the current set of legal provisions and the suggested text are formed into a triple, which is input into the large language model together with the pre-constructed seventh prompt word, requiring the large language model to judge whether the suggested text is consistent with the current set of legal provisions based on the legal consultation question, return a scalar value used to represent the consistency judgment result, and use the consistency between the suggested text and the current set of legal provisions as the third evaluation indicator.

[0034] Furthermore, the consistency LC between the proposed text and the set of existing legal provisions is calculated 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 means inconsistent, l w =1 represents agreement; D represents the number of legal consultation questions.

[0037] As a preferred embodiment of the 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 respectively used as the question part and the high-quality answer part. The question part and the high-quality answer part constitute the original dataset of the f-th iteration round;

[0039] The question part is input into the model updated in the f-1th iteration round for inference, and the answer generated by the model updated in 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;

[0040] Each answer generated by the updated model in the f-1th iteration round is taken as the answer to be tested, and the legal part and the suggestion part are extracted using regular expressions. If the legal part of an answer to be tested contains hallucinations, or the legal part is irrelevant to the legal consultation question, or the suggestion part of the answer to be tested is inconsistent with the total text set, then the triplet corresponding to the answer to be tested is added to the original data set of the fth iteration round to update the original data set, and finally the preference data set of the fth iteration round is obtained.

[0041] As a preferred embodiment of the first aspect, in step S4, the answers in the preference data set are composed of legal consultation question x, preferred answer y, w And the rejected answer y l The triplet formed constitutes the total loss The functional form is:

[0042]

[0043]

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

[0045] As a preferred embodiment of the first aspect, in step S4, after obtaining the updated model in each iteration round, the legal consulting questions in the test set are inferred, and it is determined whether the current number of iterations has not reached the preset maximum number of iterations:

[0046] If it is reached, the iteration stops and the trained legal question answering model is output;

[0047] If the preset maximum number of iterations is not reached, the values ​​of the three evaluation indicators in S42-S44 are calculated on the test set, and it is continued to be determined whether the three evaluation indicators all meet expectations: if all meet expectations, the iteration is stopped and the trained legal question answering model is output; if one or more of the evaluation indicators do not meet expectations, the evaluation indicators of each iteration round are compared with the evaluation indicators of the previous iteration round. When all three evaluation indicators decrease, the iteration is stopped and the trained legal question answering model is output; otherwise, training is continued until the preset maximum number of iterations is reached or the model converges.

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

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

[0050] The processor is used to implement the method for enhancing the factuality of legal question and answer based on a large language model as described in the first aspect when executing the computer program.

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

[0052] The present invention is used to realize the legal consultation question and answer tasks in intelligent justice. Compared with the traditional legal consultation model, the present invention uses preference data and a reinforcement learning algorithm based on difficult sample perception to enhance the factuality of the fine-tuned model on the basis of a large language model, thereby significantly improving the accuracy and reliability of the model's legal question and answer.

[0053] This invention combines the perception of difficult samples with the fine-tuning of preference data for the first time, proposes a two-stage method for constructing high-quality training data, and introduces multiple evaluation indicators to evaluate and optimize model performance. Compared with other legal large language models, this invention is innovative and unique in both algorithm and practical application. Its performance is at the leading level in the industry, and it provides an innovative solution for the application of intelligent justice in legal consultation. BRIEF DESCRIPTION OF THE DRAWINGS

[0054] Figure 1 It is a schematic diagram of the process of the present invention;

[0055] Figure 2 A schematic diagram of the construction of training data in the method of the present invention;

[0056] Figure 3 It is a system block diagram of the present invention. DETAILED DESCRIPTION

[0057] In order to make the above-mentioned purpose, features and advantages of the present invention more obvious and easy to understand, the specific implementation mode of the present invention is described in detail below in conjunction with the accompanying drawings. In the following description, many specific details are set forth to facilitate a full understanding of the present invention. However, the present invention can be implemented in many other ways different from those described herein, and those skilled in the art can make similar improvements without violating the connotation of the present invention. Therefore, the present invention is not limited to the specific embodiments disclosed below. The technical features in each embodiment of the present invention can be combined accordingly without conflicting with each other.

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

[0059] like Figure 1 As shown, in a preferred implementation of the present invention, the above-mentioned method for enhancing the factuality of legal question answering based on a large language model includes the following steps S1 to S4. The specific implementation process is 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 those 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 provision 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 consulting questions in the initial question set, and n1 is the number of legal consulting questions included 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 Q 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] In the formula, 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 contained in the processed question set; f lengthrepresents a length filtering function, which is used to filter the length of each legal consultation question in the initial question set, and directly eliminate the legal consultation questions whose length is less than a preset length threshold. In this embodiment, the length threshold is set to 25; sem&syn Represents a semantic and syntactic check function, which is used to perform semantic and syntactic checks on the results after length filtering, and eliminate legal consulting issues that are semantically incoherent and contain grammatical errors.

[0065] It should be noted that in the embodiment of the present invention, each legal consultation question in the processed question set and the pre-constructed first prompt word are provided to the large language model, requiring it to generate an initial answer a corresponding to each of the above legal consultation questions. 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 the initial answer using LLM can be expressed 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 processed question set; a LLM_oril,i1 Represents the legal consultation question q′ generated by the large language model part1,i1 The initial answer generated.

[0068] It should be noted that, due to the timeliness and breadth of the legal knowledge in the large language model, the initial answers directly generated by the large language model often have a lot of hallucinations in the parts involving legal provisions. In order to collect higher quality training data, the subsequent steps circumvented 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 invention, a regular expression is used to extract all the legal provisions contained in each initial answer. In this embodiment, each initial answer contains p i1 The first set of legal provisions corresponding to each initial answer is composed of these legal provisions. LLM_oril,i1 The corresponding first set can be expressed as:

[0070]

[0071] In the formula, Indicates the 1st, 2nd, ..., pth i1 Article 1, p i1 Indicates the number of laws 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 No. By N i The jth article in the i-th code By the corresponding code name t name 、Article No. number and the name of the law content Composition, i∈{0,…,T}, j∈{0,…,N i},N i is the number of laws contained in the i-th code, and T is the total number of codes in the total text set. In this embodiment, the laws and regulations revised as of January 1, 2024, and these laws and regulations constitute the above total text set.

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

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

[0075] In this embodiment, each legal provision from the initial answer is embedded by the trained encoder, and each legal provision 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 line from a LLM_oril,i1 The law Embed and get the first embedding representation

[0076] In this embodiment, each legal article from the pre-constructed text collection is embedded by the trained encoder, and each legal article from the text collection will be correspondingly represented by 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 In the second embedding representation of j elements.

[0079] S12. For each legal provision from the initial answer, calculate the cosine similarity between its corresponding first embedding representation and the second embedding representation corresponding to each legal provision in the total text set, and take the legal provision corresponding to the second embedding representation with the largest cosine similarity as the true legal provision.

[0080] It should be noted that, in the present invention, each legal consultation question q′ in the processed question set is part1,i1 , the real legal provisions of each initial answer and the pre-constructed second prompt word are input into the large language model, and the LLM is used to determine whether the real legal provisions of each initial answer are related to each legal consultation question in the processed question set. The large language model outputs only the legal provisions related to each legal consultation question in the processed question set and forms a revised legal provision set l revise,i1 .

[0081] It should be noted that in the present invention, each legal consultation question, initial answer, and revised legal article set {q′ part1,i1 ,a LLM_oril,i1 ,l revise,i1} and the pre-constructed third prompt word are input into the large language model to correct the initial answer and obtain the corrected answer a LLM_revised,i1 :

[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 invention, Figure 2 As shown, the above-mentioned initial question set is preprocessed to extract legal consulting questions with smooth semantics and appropriate length. The large language model is used to generate preliminary answers (initial answers) to the above-mentioned legal consulting questions. Subsequently, specific rules (i.e., regular expressions) are used to identify the legal clause part in the initial answer, and the real legal clauses that are closest to the legal clauses in the initial answer are searched from the legal clause library (i.e., the total text collection) in an embedded manner to calibrate the content of the initial answer, and finally generate high-quality revised answers, thereby effectively reducing the probability of the large language model generating hallucination content. Thus, through the above process, each legal consulting 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 data set and the huge number of legal provisions, the first training data set 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 data set D1 and obtain the second training data set 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 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: determine whether the number of occurrences of each law in the first training data set is less than a preset number threshold: if not, no processing is performed; if less, the laws less than the number threshold are regarded 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 through analysis to have not been mentioned or are 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-constructed 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 legal provision in the extended legal provision set and its corresponding extended question set together with the pre-built fifth prompt word are input into the large language model to obtain the reference answer for each legal consultation question in the extended question set. In the process of generating the reference answer, since the reference legal provision is provided in the context of LLM, This has largely mitigated the possibility of illusory legal provisions appearing in the reference answers. x ∈{1,…,Q b Legal consultation questions For example, compare it with the law and the corresponding prompt words are provided to the large language model, requiring LLM to Generate reference answers

[0090]

[0091] Therefore, if Figure 2 As shown in the figure, for the legal articles that have not been fully covered, the large language model and prompt words are used to generate legal consultation questions related to these specific legal articles. Then the legal article and the newly generated legal consultation questions are given to the large language model for answering, and the reference answers to the newly generated legal consultation questions are obtained. The data covering the legal article is constructed based on this, and finally the second training data set D2 is obtained:

[0092]

[0093] S3: Use the supervised fine-tuning method to fine-tune the large language model base on the first training dataset and the second training dataset to obtain the fine-tuned model M SFT .

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

[0095] It should be noted that in step S3, during the process of fine-tuning the large language model base, the first training data set and the second training data set are merged and then randomly sorted to generate a fine-tuning data set D SFT. The legal consultation questions contained in the fine-tuning dataset are input into the large language model base, and the large language model base generates answers to the legal consultation questions. The first negative log-likelihood loss is calculated based on the answers in the fine-tuning dataset and the answers to 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 the preset iteration number threshold is reached and the large language model base converges to obtain the fine-tuned model.

[0096] The first negative log-likelihood loss mentioned above The functional form is:

[0097]

[0098] Among them, y t represents the tth word of the answer in the fine-tuning dataset; y <t represents a word sequence consisting of the first t-1 words of the answer in the fine-tuning dataset; T represents the length of the answer in the fine-tuning dataset; x represents the legal consultation question contained in the fine-tuning dataset; p(y t |y <t ,x) represents the given legal consultation question x and word sequence y <t Next, the large language model base predicts that the tth word is y t probability.

[0099] In this embodiment, the fine-tuning dataset D SFT Essentially, it can be considered as a binary (x, y), where x is the legal consultation question and y is the answer. Further, the fine-tuning dataset can be split into two parts according to the ratio of 8:1:1, respectively as supervised fine-tuning (SFT) training set D SFT_Train , validation set D SFT_Val And the test set D SFT_Test Then, during the training process, this embodiment updates and optimizes the parameters θ of the large language model base by minimizing the negative log-likelihood loss, so that the large language model base can generate more expected outputs for given inputs. Finally, the fine-tuning dataset D SFT And negative log-likelihood loss, set appropriate training hyperparameters, and obtain the fine-tuned model.

[0100] S4: Use the hard sample-aware reinforcement learning algorithm to fine-tune the model M SFT Perform several iterations of fine-tuning. In the fth iteration round, it is necessary to construct the corresponding preference data set based on the three evaluation indicators, and divide the preference data set according to the preset ratio to obtain the training set of Hard Sample Perceptual Preference Learning (HIPO) Validation set And the test set Calculate the total loss on the training set, update the parameters of the fine-tuned model based on minimizing the total loss, and use the updated model in each iteration round The legal consultation questions in the test set are inferred and the inference results are evaluated on three evaluation indicators until the preset iteration round threshold is reached or the model converges to obtain a legal question-answering model.

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

[0102] S41. Obtain D legal consultation questions, and record the answer to be evaluated corresponding to a legal consultation question as a resp First, a regular expression is used to extract two elements from each answer to be evaluated. The first element to be extracted is all the laws to be tested mentioned in the answer to be evaluated and constitute the set of laws to be tested. The second element to be extracted is the suggestion part of the answer to be evaluated and constitutes the suggestion text s resp .in, represents the wth law to be tested, They respectively represent the code name, article number and article name in the wth article to be tested.

[0103] In this embodiment, the regular expression must satisfy the set of rules to be tested in the present invention. resp Not a suggested text resp part of, and therefore strictly satisfies relationship.

[0104] In this embodiment, the suggestion part of the answer to be evaluated specifically refers to the specific solution given to the legal consultation question raised by the user. This part is often a specific development of what the user should do. In order to distinguish it from the extracted legal provisions, this part does not contain any legal provisions or explanations of the legal provisions.

[0105] S42, the code name, the article number and the article name corresponding to each of the w-th article to be tested form a triple, when the triple of the w-th article to be tested If it belongs to the total set of texts L, then the wth legal clause to be tested is considered No hallucination, and the no hallucination rate NHSR of the tested law is used as the first evaluation indicator.

[0106] In this embodiment, the law to be tested is free of hallucinations, which specifically means that the law to be tested is real and not fabricated, that is, the law to be tested is in the total set of texts.

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

[0108]

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

[0110] S43. The law to be tested that has hallucinations shall be regarded as irrelevant. If there is no hallucination, it is recorded as Then the non-illusioned legal provisions and legal consultation questions are combined 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 law clause to be tested without hallucination. Whether it is relevant to the legal consultation question Q, a scalar value used to represent the result of the relevance judgment 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 merely correct legal provision. Using only the evaluation index in S42 has certain limitations. Therefore, the present invention designs a second evaluation index, which can be specifically seen 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 between the tested legal provisions in the answer to be evaluated and the legal consultation question raised by the user.

[0115] In this embodiment, the correlation between the tested legal provision and the legal consultation question raised by the user means that when conducting legal consultation or solving specific legal consultation problems, it is necessary to directly quote or refer to the legal provisions. In other words, when the tested legal provision can be used as a basis for answering the legal consultation question without hallucination, the two are considered to be related.

[0116] S44, input the legal consultation question Q and the total text set L into the retriever (such as BGE), and require the retriever to return the d legal provisions that are most relevant to the legal consultation question Q from the total text set L and form the current legal provisions set l retriever , the legal consultation question, the set of current legal provisions and the proposed text are formed into a triple {Q,l retriever ,s resp}, together with the pre-built seventh prompt word, is input to the large language model, requiring the large language model to judge the recommended text s based on the legal consultation question resp Whether it is consistent with the current legal provisions retriever Consistency, returns a scalar value used to represent the consistency judgment result, and uses the consistency between the proposed text and the current set of legal provisions as the third evaluation indicator.

[0117] Furthermore, the consistency LC between the proposed text and the set of existing legal provisions is calculated 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 means inconsistent, l w =1 represents agreement; D represents the number of legal consultation questions.

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

[0121] In this embodiment, the consistency between the suggested text and the current legal clause set refers to whether the suggested part of the answer to be tested is consistent with the current legal clause set l based on the legal consultation question raised by the user. retriever Consistent, if the proposed part of the answer to be tested is consistent with the current legal provisions set l retrieverContrary (inconsistent), that is, the solution or opinion proposed in the suggestion of a certain legal consultation question is inconsistent with the current legal provisions, or even conflicts with them. In this case, it is considered that the suggestion part of the answer to be tested is not consistent with the current set of legal provisions, or the two are inconsistent.

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

[0123] 1) 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 respectively used as the question part Q SFT_Train and High-quality Answers Part A SFT_Train , 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 Indicates the 1st, 2nd, ..., Cth legal consultation questions in the question section; a Train,1 ,a Train,2 ,...,a Train,C Indicates the 1st, 2nd, ..., Cth answers in the high-quality answer section.

[0127] 2) Input the problem part into the updated model after the f-1th iteration Inference is performed in the model updated by the f-1th iteration round The generated answers, the legal consultation questions in the question section, and the answers in the high-quality answer section form triplets and constitute the intermediate dataset.

[0128] In this embodiment, the problem part is input into the updated model after the f-1th iteration round. Inference is performed in the model updated by the f-1th iteration round The process of generating answers can be expressed as:

[0129]

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

[0131] in, Indicates the use of the model after the f-1th round of iterations The inference process of q Train,c Indicates from Q SFT_Train The cth question, a f_resp,c Representation Model For question q Train,c The generated answer, c∈{1,…,C}; A f_resp Representation Model For Q SFT_Train The set of answers generated for all questions in .

[0132] In this embodiment, the model updated by the f-1th iteration round The generated answers, the legal consultation questions in the question section, and the answers in the high-quality answer section form triplets and constitute the intermediate dataset:

[0133]

[0134] 3) Update the model after the f-1th iteration Each answer generated is used as the answer to be tested, and the legal part and the suggestion part are extracted using regular expressions. If the legal part of an answer to be tested contains hallucinations, or the legal part is irrelevant to the legal consultation question, or the suggestion part of the answer to be tested is inconsistent with the total text set, then the triple corresponding to the answer to be tested is added to the original data set of the fth iteration round. In order to update the original data set, the preferred data set of the fth iteration round is finally obtained.

[0135] In this embodiment, the model updated in the f-1th iteration round Generate the answer a to question c f_resp,c Take the following as an example, and use regular expressions to extract the legal clauses. f_resp,c and suggested sections f_resp,c Then use the indicators (NHSR, REL, LC) proposed in S42-S44 above to determine the answer a f_resp,c The legal part of f_resp,c Does it involve hallucinations and is it relevant to legal advice? Answer a f_resp,c Recommended Sections f_resp,cWhether it is consistent with the total text set L, finally, the triples that meet the following conditions are added to the original data set of the f-th iteration round to update the original data set, and finally the preference data set of the f-th iteration round is obtained:

[0136]

[0137] Among them, NHSR(l f_resp,c )、REL(l f_resp,c ) and LC(s f_resp,c ) represent the answer a f_resp,c The hallucination-free rate of the tested legal provisions, the correlation between the tested legal provisions and the legal consultation questions raised by users, and the consistency between the recommended text and the total text set.

[0138] Thus, after obtaining the preference data set of the fth iteration round, it is randomly sorted and split into two parts in a ratio of 8:1:1, which are used as the training sets of Hard Sample Perceptual Preference Learning (HIPO) and Hard Sample Perceptual Preference Learning (HIPO). Validation set And the test set The preference dataset can essentially be viewed as a set of triples (x, y w ,y l ), x is the legal consultation question, y w is the preferred answer, y l Here, the preferred answer specifically refers to the answer that is rejected using the fine-tuning dataset D SFT The answers in the corresponding high-quality answers section, the rejected answers specifically refer to the answers obtained by reasoning using the model trained in the previous iteration round.

[0139] It should be noted that in step S4 of the present invention, the total loss The functional form is:

[0140]

[0141] In this embodiment, the total loss is obtained by weighted summing the second negative log-likelihood loss and the direct preference optimization loss. Specifically, the second negative log-likelihood loss The functional form is:

[0142]

[0143] The direct preference optimization (DPO) loss uses the model M from the previous iteration. f-1 As a reference model, the calculation formula is as follows:

[0144]

[0145] In the above formula, α and β represent weight hyperparameters; ω represents the adjustment parameter; represents the second negative log-likelihood loss, represents the direct preference optimization (DPO) loss; |y w | means y w The length of M; σ is the sigmoid activation function; θ (y w |x) means that given a legal consulting question x, the model calculates the output y based on its current parameters θ w The probability of θ (y l |x) means that given a legal consulting question x, the model calculates the output y based on its current parameters θ l The probability of f (y w |x) means that given a legal consulting question x, the model updated in the previous iteration round calculates the output y based on its current parameters w The probability of f (y l |x) means that given a legal consulting question x, the model updated in the previous iteration round calculates the output y based on its current parameters l probability.

[0146] In this embodiment, the larger the value of the adjustment parameter ω is, the larger the gap between the positive and negative samples is, and the model will more strongly learn the signals that distinguish them; if the value of ω is smaller, the gap is narrowed and the learning process of the model will be smoother.

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

[0148] If it is reached, the iteration stops and the trained legal question answering model is output;

[0149] If the preset maximum number of iterations is not 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 all reached expectations. If they have all reached expectations, the iteration is stopped and the trained legal question answering model is output. If one or more of the evaluation indicators have not reached expectations, the evaluation indicator NHSR of the fth iteration round is set to 0. f , REL f , LC f And the evaluation index NHSR of the f-1th iteration round f-1 , REL f-1 , LC f-1In comparison, when all three evaluation indicators decrease, the iteration is stopped and the trained legal question answering model is output; otherwise, the training continues until the preset maximum number of iterations is reached or the model converges.

[0150] The present invention will now use a specific example to demonstrate the application effect of the method for enhancing the factuality of legal question answering based on a large language model described in S1 to S4 in the above embodiments on a specific data set, so as to facilitate understanding of the essence of the present invention.

[0151] Example

[0152] The specific implementation process of the method for enhancing the factuality of legal question answering based on a large language model adopted 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 piece of data in the data set consists of three parts: legal consultation questions, reference legal provisions, 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 laws 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 present invention, 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 present invention is compared with the traditional supervised fine-tuning (SFT) model and the direct preference optimization[4] (DPO) model. The indicators NHSR, REL, LC proposed in S1, as well as the traditional evaluation indicators METEOR[5], BERTScore[6], and Rouge-L[7] are used for evaluation. The experimental results are shown in Table 2, where the SFT model is trained for 2 rounds, and the DPO model and the method of the present invention are trained for 3 rounds. The results show that the method proposed in the present invention has good performance in terms of factuality and helpfulness of the answers, and exceeds the baseline model.

[0157] Table 2. Overall performance

[0158]

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

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

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

[0162] [3]Dubey A,et al.The Llama 3Herd 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 ProcessingSystems36:Direct Preference Optimization:Your Language Model is Secretly aReward Model[C].2024.

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

[0165] [6]Zhang T,Kishore V,Wu F,et al.BERTScore:Evaluating Text Generationwith 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 invention provides a method for enhancing the factuality of legal question answering based on a large language model. The method enhances the factuality of legal question answering in the following two stages:

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

[0169] 2) Reinforcement learning stage: Specifically, this stage improves the fine-tuned model obtained after the completion of the SFT stage in the following two aspects. First, in the selection of samples for iterative training, the present invention detects the answers generated by the updated model in the previous round on three evaluation indicators to determine whether each evaluation indicator meets the corresponding conditions. And exclude the samples corresponding to the answers that meet the conditions from the training set. As the iteration proceeds, simple samples are gradually filtered, and only more challenging samples are retained. Retraining challenging samples can allow the model to better grasp complex issues and improve the factuality of legal questions and answers. Secondly, the present invention focuses on information that has not been mastered. Positive example samples are mostly content that the model has not yet mastered. The goal of the present invention is to ensure that the model learns legal provisions without hallucinations. Therefore, in the reinforcement learning (HIPO) stage, 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 invention also provides a legal question answering factual enhancement system based on a large language model corresponding to the legal question answering factual enhancement method based on a large language model provided in the above embodiment, such as Figure 3 As shown, it includes:

[0171] A data acquisition module, used to obtain legal consultation questions raised by users;

[0172] The result acquisition module is used to input the legal consultation questions raised by the users into the legal question and answer model trained by the legal question and answer factual enhancement method based on the large language model shown in the above embodiment, and output the answers to the legal consultation questions.

[0173] Similarly, based on the same inventive concept, another preferred embodiment of the present invention also provides a computer electronic device corresponding to the method for enhancing the factuality of legal question and answer 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 used 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] In addition, the logic instructions in the above-mentioned memory can be implemented in the form of software functional units and can be stored in a computer-readable storage medium when sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, or the part that contributes to the prior art, or the part of the technical solution, can be embodied in the form of a software product, which is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the 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, discrete hardware components.

[0178] It should also be noted that those skilled in the art can clearly understand that for the convenience and simplicity 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, and there may be other division methods in actual implementation, such as multiple modules or steps can be combined or integrated together, and a module or step can also be split.

[0179] The above-described embodiment is only a preferred solution of the present invention, but it is not intended to limit the present invention. A person skilled in the relevant technical field may make various changes and modifications without departing from the spirit and scope of the present invention. Therefore, any technical solution obtained by equivalent replacement or equivalent transformation falls within the protection scope of the present invention.

Claims

1. A method for enhancing the factuality of legal question answering based on a large language model, characterized in that: The following steps are involved: 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 to 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 real legal provisions. Input each legal consultation question in the processed question set, the real legal provisions of each initial answer, and a pre-constructed second prompt word into the large language model, output the legal provisions 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 a 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. S2: Generate an extended legal article set from the first training data set, input the legal articles in the extended legal article set and the pre-constructed fourth prompt word into the large language model, obtain legal consultation questions related to the legal articles in the extended legal article set and form an extended question set, input each legal article in the extended legal article set and its corresponding extended question set together with the pre-constructed fifth prompt word into the large language model, obtain a reference answer, and each legal consultation question in the extended question set and its corresponding reference answer constitute a second training data set; S3: fine-tune the large language model base on the first training data set and the second training data set using a supervised fine-tuning method to obtain a fine-tuned model; S4: Use the reinforcement learning algorithm based on hard sample perception to perform several iterations of fine-tuning on the fine-tuned model. In each iteration round, it is necessary to construct a corresponding preference data set based on the three evaluation indicators, and divide the preference data set according to a preset ratio to obtain the training set, validation set and test set of hard 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 consulting questions in the test set, and evaluate the inference results based on the three evaluation indicators until the preset iteration round threshold is reached or the model converges to obtain a legal question-answering model.

2. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 1, characterized in that: In step S1, the total text set of all laws and regulations is obtained, and the total text set includes T codes, the i-th code l i By N i The jth article in the i-th code It is composed of the corresponding code name, article number and article name; i∈{0,…,T}, j∈{0,…,N i },N i is the number of articles contained in the i-th code, and T is the total number of codes in the total set of texts; The specific process of finding the legal article with the most similar semantics to each initial answer in the total text set as the real legal article is as follows: S11, embedding each legal provision from the initial answer by the trained encoder to obtain a first embedding representation, and embedding each legal provision from the total text set by the trained encoder to obtain a second embedding representation; S12. For each legal provision from the initial answer, calculate the cosine similarity between its corresponding first embedding representation and the second embedding representation corresponding to each legal provision in the total text set, and take the legal provision corresponding to the second embedding representation with the largest cosine similarity as the true legal provision.

3. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 1, characterized in that: In step S2, the specific process of generating the extended legal provisions set from the first training data set is as follows: Determine whether the number of occurrences of each legal provision in the first training data set is less than a preset number threshold: if not, no processing is performed; if less, the legal provisions less than the number threshold are regarded as insufficiently trained legal provisions, and all insufficiently trained legal provisions constitute an extended legal provision set.

4. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 1, characterized in that: In step S3, during the process of fine-tuning the large language model base, the first training data set and the second training data set are merged and then randomly sorted to generate a fine-tuning data set, the legal consultation questions contained in the fine-tuning data set are input into the large language model base, and the large language model base generates answers to the legal consultation questions. The first negative log-likelihood loss is calculated based on the answers in the fine-tuning data set and the answers to the legal consultation questions generated by the large language model base, and 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 and the large language model base converges to obtain a fine-tuned model.

5. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 1, characterized in that: In step S4, the specific process of constructing three evaluation indicators is as follows: S41. Obtain D legal consulting questions, and use regular expressions to extract two elements in the answer to be evaluated corresponding to each legal consulting question, wherein the first element is all the laws to be tested mentioned in the answer to be evaluated and constitutes a set of laws to be tested, and the second element is the suggestion part in the answer to be evaluated and constitutes a suggestion text; S42, a triplet is formed by the code name, the article number and the article name corresponding to each of the w-th article to be tested. When the triplet of the w-th article to be tested belongs to the total set of texts, it is considered that the w-th article to be tested has no hallucination, and the hallucination-free rate of the article to be tested is used as the first evaluation indicator; S43, the law to be tested with hallucinations is regarded as irrelevant, and the law to be tested without hallucinations and the legal consultation question are formed into a tuple, which is input into the large language model together with the pre-constructed sixth prompt word, and the large language model determines whether the law to be tested without hallucinations and the legal consultation question are relevant, and returns a scalar value used to represent the result of the relevance judgment, and uses the relevance between the law to be tested and the legal consultation question raised by the user as the second evaluation indicator; S44. The legal consultation question and the total set of texts are input into the search engine, which returns the d legal provisions that are most relevant to the legal consultation question from the total set of texts and forms a set of current legal provisions. The legal consultation question, the current set of legal provisions and the suggested text are formed into a triple, which is input into the large language model together with the pre-constructed seventh prompt word, requiring the large language model to judge whether the suggested text is consistent with the current set of legal provisions based on the legal consultation question, return a scalar value used to represent the consistency judgment result, and use the consistency between the suggested text and the current set of legal provisions as the third evaluation indicator.

6. A method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 5, characterized in that: In step S4, the specific process of constructing the preference data set for the fth iteration round is as follows: 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 respectively used as the question part and the high-quality answer part. The question part and the high-quality answer part constitute the original dataset of the f-th iteration round; The question part is input into the model updated in the f-1th iteration round for inference, and the answer generated by the model updated in 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; Each answer generated by the updated model in the f-1th iteration round is taken as the answer to be tested, and the legal part and the suggestion part are extracted using regular expressions. If the legal part of an answer to be tested contains hallucinations, or the legal part is irrelevant to the legal consultation question, or the suggestion part of the answer to be tested is inconsistent with the total text set, then the triplet corresponding to the answer to be tested is added to the original data set of the fth iteration round to update the original data set, and finally the preference data set of the fth iteration round is obtained.

7. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 6, characterized in that: In step S4, the answers in the preference data set are composed of legal consultation question x, preferred answer y w And the rejected answer y l The triplet formed constitutes the total loss The functional form is: In the formula, α and β represent weight hyperparameters; ω represents the adjustment parameter; represents the second negative log-likelihood loss, represents the direct preference optimization loss; |y w | means y w The length of M; σ is the sigmoid activation function; θ (y w |x) means that given a legal consulting question x, the model calculates the output y based on its current parameters θ w The probability of θ (y l |x) means that given a legal consulting question x, the model calculates the output y based on its current parameters θ l The probability of f (y w |x) means that given a legal consulting question x, the model updated in the previous iteration round calculates the output y based on its current parameters w The probability of f (y l |x) means that given a legal consulting question x, the model updated in the previous iteration round calculates the output y based on its current parameters l probability.

8. The method for enhancing the factuality of legal question answering based on a large language model as claimed in claim 7, characterized in that: In step S4, after obtaining the updated model in each iteration round, the legal consultation questions in the test set are inferred, and it is determined whether the current number of iterations has not reached the preset maximum number of iterations: If it is reached, the iteration stops and the trained legal question answering model is output; If the preset maximum number of iterations is not reached, the values ​​of the three evaluation indicators in S42-S44 are calculated on the test set, and it is continued to be determined whether the three evaluation indicators all meet expectations: if all meet expectations, the iteration is stopped and the trained legal question answering model is output; if one or more of the evaluation indicators do not meet expectations, the evaluation indicators of each iteration round are compared with the evaluation indicators of the previous iteration round. When all three evaluation indicators decrease, the iteration is stopped and the trained legal question answering model is output; otherwise, training is continued until the preset maximum number of iterations is reached or the model converges.

9. A legal question-answering factual enhancement system based on a large language model, characterized in that: include: A data acquisition module, used to obtain legal consultation questions raised by users; The result acquisition module is used to input the legal consultation questions raised by the user into the legal question and answer model trained by the legal question and answer factual enhancement method based on the large language model as described in any one of claims 1 to 8, and output the answers to the legal consultation questions.

10. A computer electronic device, characterized in that: including memory and processor; The memory is used to store computer programs; The processor is used to implement the method for enhancing the factuality of legal question answering based on a large language model as described in any one of claims 1 to 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

  • Text recognition model reliability regularization method based on perception and semantic relevance

    CN117151111A

  • Community question-answer website answer sorting method and system combined with active learning

    US20230035338A1

Cited By

  • Big language model fine-grained deprejudice method and system based on prejudice intensity

    CN120030133A

  • Sample data generation method and electronic equipment

    CN120086601A

  • Hierarchical legal criminal name judgment method and device based on big and small model collaboration, and medium

    CN120745802A

  • Intelligent question and answer matching method based on machine learning

    CN121328728A

  • A machine learning-based question and answer intelligent matching method

    CN121328728B