Database statement generation method based on large language model and related device
By generating multiple variant statements based on a large language model, expanding the database retrieval coverage, solving the problem of poor accuracy of database statements in the prior art, and achieving higher accuracy and reliability.
Patent Information
- Application Number
- CN202411534562.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-10-31
- Publication Date
- 2024-12-27
- Estimated Expiration
- 2044-10-31
AI Technical Summary
The existing technology has a problem of small coverage when users input natural statements to retrieve schema from the database, which leads to poor accuracy of generated database statements and inability to effectively self-correct and identify implicit errors, which is prone to hallucinations of large language models. .
Using a method based on a large language model, by generating multiple variant statements that are logically consistent with the user input statement but have different syntax structures, the target data structure is obtained from the database, the coverage of the data structure is expanded, and multiple iterations and negative labels are used. Processing improves the accuracy and credibility of database statements.
Improve the accuracy and recall rate of database statements, reduce error rate, avoid the hallucination problem of large language models, and enhance the reliability of database operations.
Smart Images

Figure CN119201979A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a method for generating database statements based on a large language model and related devices. Background Art
[0002] With the development of natural language processing technology, computers can better understand and process natural language. Therefore, in order to improve the user experience, users can be allowed to directly enter natural sentences when they want to perform relevant operations on the database. The computer will generate corresponding database sentences based on the natural sentences entered by the user to implement operations on the database, such as data query.
[0003] Currently, computers can directly obtain relevant data structures (schemas) from the database through natural statements input by users, and then generate corresponding database statements based on the retrieved schema, but this method also has certain limitations.
[0004] Specifically, the method of directly retrieving the schema from the database through natural statements input by the user may have the problem of small schema coverage, resulting in poor accuracy of the generated database statements. Summary of the invention
[0005] In view of this, the purpose of the present application is to provide a database statement generation method and related devices based on a large language model to expand the coverage of the retrieved data structure, thereby improving the accuracy of the generated database statements.
[0006] In order to achieve the above purpose, the technical solution adopted in the embodiment of the present application is as follows: In a first aspect, the present invention provides a method for generating database sentences based on a large language model, the method comprising: Obtain a sentence to be processed input by a user, and generate multiple variant sentences corresponding to the sentence to be processed through a large language model; the sentence to be processed and the variant sentences are both natural languages, and the variant sentences have different grammatical structures from the sentence to be processed but consistent semantic logic; Obtaining a target data structure from a database according to the plurality of variant sentences using the large language model, and generating a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure; A target database statement is determined from the multiple database statements; the target database statement is a database statement that can be successfully executed and has the highest confidence.
[0007] In an optional implementation, obtaining a target data structure from a database according to the plurality of variant sentences using the large language model, and generating a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure, includes: Inputting each of the variant sentences into a large language model for processing to obtain a first semantic structure corresponding to each of the variant sentences; According to the first semantic structure corresponding to each of the variant statements, the target table and the target field corresponding to each of the variant statements are acquired from the database, and a union of the target table and the target field is taken to obtain the target data structure; The target data structure and each of the variant sentences are input into the large language model for processing to obtain a database sentence corresponding to each of the variant sentences.
[0008] In an optional embodiment, the method further comprises: Inputting the sentence to be processed into the large language model for processing, obtaining a second semantic structure corresponding to the sentence to be processed, and determining whether a target first semantic structure having the same semantics as the second semantic structure satisfies a first preset condition; If the target first semantic structure does not meet the first preset condition, negative labels are added to other semantic structures except the target first semantic structure, and the other first semantic structures with the negative labels added and the variant sentences corresponding to the other first semantic structures are re-input into the large language model for processing to obtain a new first semantic structure corresponding to the variant sentence, and the target first semantic structure is determined from the new first semantic structure until the target first semantic structure meets the first preset condition.
[0009] In an optional implementation, acquiring a target table and a target field corresponding to each of the variant statements from the database according to the first semantic structure corresponding to each of the variant statements, taking a union of the target table and the target field, and obtaining the target data structure includes: If the target first semantic structure satisfies the first preset condition, acquiring a target table and a target field corresponding to a variant statement corresponding to the target first semantic structure from the database according to each target first semantic structure; Taking a union of the target table and the target field to obtain the target data structure; The step of inputting the target data structure and each of the variant sentences into the large language model for processing to obtain a database sentence corresponding to each of the variant sentences includes: The target data structure and the variant sentences corresponding to each of the target first semantic structures are input into the large language model for processing to obtain database sentences corresponding to each of the variant sentences corresponding to the target first semantic structures.
[0010] In an optional implementation, the determining a target database statement from the plurality of database statements includes: Executing each of the database statements respectively, and determining a plurality of first candidate database statements from the plurality of database statements according to the execution results corresponding to each of the database statements; generating a regenerated statement corresponding to each of the first candidate database statements, and determining a second candidate database statement from a plurality of the first candidate database statements according to the regenerated statement corresponding to each of the first candidate database statements; Wherein, the regenerated sentence is the natural language corresponding to the first candidate database sentence; If there are multiple second candidate database statements, determine the target database statement from the multiple second candidate database statements according to the to-be-processed statement, the regenerated statements corresponding to each of the second candidate database statements, and the execution results; If there is one second candidate database statement, the second candidate database statement is determined as the target database statement.
[0011] In an optional implementation, generating a regenerated statement corresponding to each of the first candidate database statements, and determining a second candidate database statement from a plurality of first candidate database statements according to the regenerated statement corresponding to each of the first candidate database statements, includes: Inputting each of the first candidate database sentences into the large language model for processing, respectively, to obtain a regenerated sentence corresponding to each of the first candidate database sentences; A first candidate database statement corresponding to the regenerated statement that is semantically logically consistent with the statement to be processed is determined as the second candidate database statement.
[0012] In an optional implementation, before generating the regeneration statements corresponding to each of the first candidate database statements, the method further includes: Determining whether the first candidate database statement satisfies a second preset condition; If the first candidate database statement does not satisfy the second preset condition, determining a first variant statement according to other database statements in the database statements except the first candidate database statement, regenerating a new database statement corresponding to the first variant statement, and determining a first candidate database statement from the new database statement, until the first candidate database statement satisfies the second preset condition; The generating of the regenerated statements corresponding to the first candidate database statements includes: If the first candidate database statements meet the second preset condition, then a regenerated statement corresponding to each of the first candidate database statements is generated.
[0013] In an optional embodiment, the method further comprises: If there is no regenerated statement that is semantically logically consistent with the statement to be processed, the variant statement corresponding to each of the first candidate database statements is determined as a second variant statement, and each of the first candidate database statements is used as a negative example to regenerate a new database statement corresponding to each of the second variant statements, and the first candidate database statement is obtained from the new database statement to generate a regenerated statement corresponding to each of the first candidate database statements until at least one regenerated statement that is semantically logically consistent with the statement to be processed is obtained.
[0014] In an optional implementation, determining a target database statement from a plurality of second candidate database statements according to the statement to be processed, the regenerated statements corresponding to each of the second candidate database statements, and the execution results includes: Calculating similarity scores corresponding to the statements of the second candidate database respectively according to the statements to be processed and the regenerated statements corresponding to the statements of the second candidate database; the similarity scores represent the similarity between the regenerated statements of the second candidate database statements and the statements to be processed; According to the execution result of the second candidate database statement, voting and scoring each of the second candidate database statements to obtain a voting score of each of the second candidate database statements; the voting score represents the credibility of the second candidate database statement; For each of the second candidate database statements, respectively, calculate a confidence score corresponding to the second candidate database statement according to the similarity score and the voting score; The second candidate database statement with the highest confidence score is determined as the target database statement.
[0015] In a second aspect, the present invention provides a database statement generation device based on a large language model, the device comprising: A generation module is used to obtain a sentence to be processed input by a user, and generate multiple variant sentences corresponding to the sentence to be processed through a large language model; the sentence to be processed and the variant sentences are both natural languages, and the variant sentences have different grammatical structures from the sentence to be processed, but have consistent semantic logic; The generating module is further configured to obtain a target data structure from a database according to the plurality of variant sentences through the large language model, and generate a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure; The determination module is used to determine a target database statement from the multiple database statements; the target database statement is a database statement that can be successfully executed and has the highest confidence.
[0016] In a third aspect, the present invention provides an electronic device, comprising a processor and a memory, wherein the memory stores a computer program executable by the processor, and the processor can execute the computer program to implement the method described in any one of the aforementioned embodiments.
[0017] In a fourth aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the method described in any one of the aforementioned embodiments.
[0018] The database statement generation method based on the large language model and the related device provided in the embodiment of the present application can generate multiple variant statements corresponding to the statement to be processed through the large language model after obtaining the statement to be processed input by the user, so that the target data structure can be obtained from the database according to the multiple variant statements. Since the variant statement is logically consistent with the statement to be processed input by the user but has a different grammatical structure, the target data structure can be obtained from the database through multiple variant statements to expand the coverage of the retrieved data structure. Based on this, the corresponding search space can be expanded when generating database statements based on the target data structure, thereby improving the accuracy of the target database statement finally generated.
[0019] In order to make the invention more obvious and understandable, preferred embodiments are given below in conjunction with the accompanying drawings for detailed description. BRIEF DESCRIPTION OF THE DRAWINGS
[0020] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0021] Figure 1 A block diagram of an electronic device provided by an embodiment of the present application is shown; Figure 2 A schematic diagram of a process of generating a database statement based on a large language model provided in an embodiment of the present application is shown; Figure 3An example diagram of a statement for selecting a target database is shown; Figure 4 Another schematic diagram of a process for generating a database statement based on a large language model provided in an embodiment of the present application is shown; Figure 5 A functional module diagram of a database statement generation device based on a large language model provided in an embodiment of the present application is shown.
[0022] Icon: 100 - memory; 110 - processor; 120 - communication module; 200 - generation module; 210 - determination module. DETAILED DESCRIPTION
[0023] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various different configurations.
[0024] Therefore, the following detailed description of the embodiments of the present application provided in the accompanying drawings is not intended to limit the scope of the present application for which protection is sought, but merely represents selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0025] It should be noted that relational terms such as "first" and "second" are used only to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprises" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, an element defined by the sentence "comprises a ..." does not exclude the existence of other identical elements in the process, method, article or device including the element.
[0026] With the development of natural language processing technology, computers can better understand and process natural language. Therefore, in order to improve the user experience, users can be allowed to directly enter natural sentences when they want to perform relevant operations on the database. The computer will generate relevant database sentences based on the natural sentences entered by the user to implement operations on the database, such as data query.
[0027] Currently, computers can directly obtain relevant data structures (schemas) from databases through natural statements input by users, and then generate corresponding database statements based on the retrieved schemas. However, this method has the following problems: 1. There are certain retrieval limitations. Specifically, since the natural sentences input by users may be ambiguous or unclear in meaning, and the database may have many similar tables or similar fields, this will lead to a small coverage problem when directly retrieving the schema from the database through the natural sentences input by users. Therefore, the search space is small when generating database statements, resulting in poor accuracy of the generated database statements.
[0028] 2. In the prior art, when verifying the generated database statements, the verification often relies on the explicit errors of the database statements, such as execution errors, etc., but does not pay attention to the implicit errors of the database statements, such as execution logic errors although the execution is successful, etc., which will also lead to poor accuracy of the generated database statements.
[0029] 3. In the prior art, when an error occurs when generating a database statement, it is often impossible to self-correct it, resulting in a high error rate in the generation of database statements.
[0030] 4. The existing technology often has a high error rate when retrieving schema, and since this part of the work is generally borne by a large language model, it is easy to cause the hallucination problem of the large language model, resulting in a poor recall rate of the generated database statements.
[0031] It should be noted that the model hallucination problem refers to the situation where the model generates or predicts information or results that are inconsistent with reality when processing data.
[0032] Based on this, an embodiment of the present application provides a database statement generation method and related devices based on a large language model to solve the above problems.
[0033] Specifically, Figure 1 For a block diagram of an electronic device provided in an embodiment of the present application, please refer to Figure 1 The electronic device includes a memory 100, a processor 110 and a communication module 120. The memory 100, the processor 110 and the communication module 120 are electrically connected to each other directly or indirectly to achieve data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.
[0034] The memory 100 is used to store computer programs or data that can be executed by the processor. The memory 100 can be, but is not limited to, a random access memory (RAM), a read only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable read-only memory (EEPROM), etc.
[0035] The processor 110 is used to read / write data or computer programs stored in the memory, and execute the computer program to implement the database statement generation method based on a large language model provided in an embodiment of the present application.
[0036] The communication module is used to establish a communication connection between the electronic device and other communication terminals through the network, and is used to send and receive data through the network.
[0037] Optionally, the electronic device may be a PC, a server or other device, or a mobile terminal device such as a mobile phone or a tablet.
[0038] It should be understood that Figure 1 The structure shown is only a schematic diagram of the structure of the electronic device. The electronic device may also include Figure 1 More or fewer components as shown, or with Figure 1 Different configurations are shown. Figure 1 Each component shown in the figure can be implemented by hardware, software or a combination thereof.
[0039] Next, the above Figure 1 The electronic device in the embodiment is the execution subject, and the method for generating a database statement based on a large language model provided by the embodiment of the present application is introduced exemplarily in combination with the flowchart. Specifically, Figure 2 A flowchart of a method for generating database statements based on a large language model provided in an embodiment of the present application is shown in FIG. Figure 2 , the method comprising: Step S20, obtaining a sentence to be processed input by the user, and generating multiple variant sentences corresponding to the sentence to be processed through a large language model.
[0040] Among them, the statement to be processed and the variant statement are both natural languages, and the variant statement has a different grammatical structure from the statement to be processed, but the semantic logic is consistent.
[0041] Optionally, the statement to be processed may be a query statement of a user for some data. It is understandable that the statement to be processed may represent the query intention of the user.
[0042] In this embodiment, the grammatical structure may include the specific vocabulary used, sentence structure, and sentence complexity. In other words, the variant sentence refers to a sentence that is different from the sentence to be processed in at least one of the word usage, sentence complexity, and sentence structure, but has the same logical meaning.
[0043] In an example, if the statement to be processed is "Find the car model with the highest sales between July 15, 2024 and July 20, 2024", the variant statements may include "Which car sells the best between July 15, 2024 and July 20, 2024", "Which car model has the most impressive sales data between July 15, 2024 and July 20, 2024", "If we focus on the sales period between July 15, 2024 and July 20, 2024, which car is the most popular in the market", "If we examine the car sales between July 15, 2024 and July 20, 2024, which car has the highest sales volume", etc.
[0044] In this embodiment, the sentence to be processed can be processed by a pre-trained large language model to generate a plurality of corresponding variant sentences. Optionally, the large language model can be guided to perform the expected processing by generating relevant prompts.
[0045] It should be noted that Prompt refers to the input text used to guide or stimulate the model to generate a specific type of output. Prompt can be a question, instruction, partial sentence or any form of text. Its purpose is to stimulate the model to produce the expected response or behavior.
[0046] In one possible implementation, the electronic device may process the sentence to be processed by using a preset variant sentence generation template, thereby generating a prompt corresponding to the sentence to be processed, and inputting the prompt into the large language model to output multiple variant sentences.
[0047] Step S21, obtaining a target data structure from a database according to a plurality of variant sentences through a large language model, and generating a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure.
[0048] Optionally, the target data structure may be used to guide generation of corresponding database statements.
[0049] Optionally, the type of the database statement can be set by the user according to actual application conditions. In a possible implementation, the database statement can be SQL.
[0050] Optionally, since the multiple generated database statements may be inconsistent, the electronic device also needs to select a suitable target database statement from the multiple database statements to return.
[0051] Step S22: determining a target database statement from a plurality of database statements.
[0052] The target database statement is a database statement that can be successfully executed and has the highest confidence.
[0053] In the method for generating database statements based on a large language model provided in the embodiment of the present application, after obtaining the statement to be processed input by the user, the electronic device can generate multiple variant statements corresponding to the statement to be processed, so that the target data structure can be obtained from the database according to the multiple variant statements. Since the variant statements are logically consistent with the statement to be processed input by the user but have different grammatical structures, the target data structure can be obtained from the database through multiple variant statements to expand the coverage of the retrieved data structure. Based on this, the corresponding search space can be expanded when generating database statements based on the target data structure, thereby improving the accuracy of the target database statement finally generated.
[0054] Next, a possible implementation method is provided for how an electronic device can obtain a target data structure from a database according to multiple variant sentences through a large language model, and generate database sentences corresponding to each variant sentence according to each variant sentence and the target data structure.
[0055] In this embodiment, the electronic device can first input each variant sentence into the large language model for processing to obtain the first semantic structure corresponding to each variant sentence, and then obtain the target table and target field corresponding to each variant sentence from the database according to the first semantic structure corresponding to each variant sentence, take the union of the target table and target field, and obtain the target data structure.
[0056] Optionally, the first semantic structure may represent the semantic logic of the variant sentence.
[0057] In one possible implementation, the first semantic structure may be in a semi-structured form, such as a dictionary structure of {entity: [...], relationship: [...]}, wherein the entity may be a noun in the variant sentence, and the relationship may be a relationship between nouns.
[0058] For example, if the variant statement is "find students in class one of school A", the entity may include school A, class one, and students, and the relationship is the relationship between these three.
[0059] Optionally, the electronic device may process each variant sentence according to a pre-stored semantic structure generation template to obtain a Prompt corresponding to each variant sentence, and then input the Prompt corresponding to each variant sentence into the large language model to generate a first semantic structure corresponding to each variant sentence.
[0060] Optionally, the electronic device may input the first semantic structures corresponding to the variant sentences into the large language model for processing, and the large language model may obtain the target table and target field corresponding to the variant sentences from the database according to the first semantic structures.
[0061] It is understandable that before inputting the first semantic structures into the large language model, the electronic device also needs to process the first semantic structures through a pre-stored query template to obtain the Prompt corresponding to each first semantic structure.
[0062] It can be understood that, since the target data structure includes the target tables and target fields corresponding to all variant statements, the coverage of the data structure can be expanded, thereby expanding the search space when generating database statements.
[0063] Optionally, after obtaining the target data structure, the electronic device may also input the target data structure and each variant sentence into a large language model for processing, thereby obtaining a database sentence corresponding to each variant sentence.
[0064] Optionally, the electronic device can process each variant statement according to a pre-stored database statement generation template to obtain the Prompt corresponding to each variant statement, and then input the target data structure and the Prompt corresponding to each variant statement into the large language model for processing, thereby obtaining the database statement corresponding to each variant statement.
[0065] In a possible implementation, the electronic device may input a variant sentence and the target data structure into the large language model each time, thereby generating a database sentence corresponding to the variant sentence.
[0066] Optionally, in order to effectively eliminate the hallucination problem of the large language model, the electronic device may also preprocess the first semantic structure before obtaining the target table and target field corresponding to each variant sentence from the database according to the first semantic structure corresponding to each variant sentence.
[0067] Specifically, the electronic device may input the sentence to be processed into the large language model for processing, obtain a second semantic structure corresponding to the sentence to be processed, and determine whether the target first semantic structure having the same semantics as the second semantic structure satisfies a first preset condition.
[0068] In a possible implementation manner, the first preset condition may be that the ratio of the number of target first semantic structures to the number of all first semantic structures reaches a first preset value.
[0069] Optionally, the first preset value can be set according to actual application conditions, for example, to 80%, and this application does not impose too many restrictions on this.
[0070] In another possible implementation manner, the first preset condition may also be that the number of target first semantic structures reaches a first preset number.
[0071] Optionally, the first preset number can be set by the user according to actual application conditions, for example, 10, etc., and this application does not impose too many restrictions on this.
[0072] Optionally, the electronic device may generate a template based on a pre-stored semantic structure to process the sentence to be processed, thereby obtaining a Prompt corresponding to the sentence to be processed, and then input the Prompt corresponding to the sentence to be processed into the large language model to generate a second semantic structure corresponding to the sentence to be processed.
[0073] In this embodiment, the second semantic structure is in a semi-structured form, such as a dictionary structure of {entity: [...], relationship: [...]}, wherein the entity may be a noun in the variant sentence, and the relationship may be a relationship between nouns.
[0074] Optionally, the electronic device may input each of the first semantic structure and the second semantic structure into the large language model for processing, so as to determine whether the first semantic structure is a target first semantic structure having the same semantics as the second semantic structure.
[0075] It can be understood that if the first semantic structure and the second semantic structure have the same semantics, it means that the variant statement corresponding to the first semantic structure is logically consistent with the statement to be processed, and the variant statement is parsed successfully. If the first semantic structure and the second semantic structure are semantically different, it means that the variant statement corresponding to the first semantic structure is logically inconsistent with the statement to be processed, and the parsing of the variant statement may fail.
[0076] It is understandable that if there are fewer variant statements that are parsed successfully and the first preset condition is not met, the variant statements that failed to parse need to be re-parsed until a certain number of variant statements are parsed successfully, thereby ensuring the coverage and accuracy of the retrieved target data structure.
[0077] Specifically, if the target first semantic structure does not meet the first preset condition, the electronic device may add negative labels to other semantic structures except the target first semantic structure, and re-input the other first semantic structures with negative labels and the variant sentences corresponding to the other first semantic structures into the large language model for processing, obtain a new first semantic structure corresponding to the variant sentence, and determine the target first semantic structure from the new first semantic structure until the target first semantic structure meets the first preset condition.
[0078] Optionally, the other semantic structures except the target first semantic structure refer to the first semantic structure corresponding to the variant sentence that failed to parse.
[0079] Optionally, the negative label may characterize the first semantic structure as an erroneous negative example, so as to prompt the large language model to avoid the result and output a new result.
[0080] Optionally, after obtaining new first semantic structures, the electronic device may determine the target first semantic structure again among the new first semantic structures, and determine whether all the target first semantic structures meet the first preset condition in combination with the previously generated target first semantic structures.
[0081] For example, if there are 10 variant sentences, 2 variant sentences are parsed successfully, and 8 variant sentences fail to parse, the electronic device can use the first semantic structures corresponding to these 8 variant sentences that failed to parse as negative examples, re-input these 8 variant sentences that failed to parse into the large language model for processing, generate 8 new first semantic structures, and then determine the target first semantic structure from these 8 new first semantic structures.
[0082] In this example, if the first preset condition is that the number of target first semantic structures accounts for eighty percent of the total number of first semantic structures, and the number of re-determined target first semantic structures reaches six among the eight new first semantic structures, then combined with the two variant statements that were successfully parsed before, it can be determined that eight of the ten variant statements were successfully parsed, and therefore it can be determined that the target first semantic structure meets the first preset condition.
[0083] In this example, if the number of target first semantic structures re-determined among the eight new first semantic structures does not reach six, for example, only four new first semantic structures are target first semantic structures, then combined with the two variant sentences that were successfully parsed before, it can be determined that only six of the ten variant sentences were successfully parsed, and the target first semantic structure does not meet the first preset condition. Therefore, for the four variant sentences that have not yet been parsed successfully, it is necessary to continue to input their corresponding first semantic structures as negative examples into the large language model for processing until the number of target first semantic structures finally determined reaches eight.
[0084] Optionally, considering that there may be a situation where the target first semantic structure cannot meet the first preset condition even after repeating the above steps for many times, a possible processing method for this situation is given below.
[0085] In a possible implementation, if the number of repetitions of the above steps reaches a preset number and the target first semantic structure still does not meet the first preset condition, an error may be directly reported.
[0086] In another possible implementation, if the above steps are repeated for a preset number of times and the target first semantic structure still does not meet the first preset condition, it can also be considered that there may be a problem of incorrect parsing of the second semantic structure. Therefore, the second semantic structure can be used as a negative example, a negative label can be added, and the second semantic structure and the sentence to be processed can be input into the large language model for reprocessing to obtain a new second semantic structure, and it is re-determined whether the target first semantic structure with the same semantics as the new second semantic structure meets the first preset condition.
[0087] In this embodiment, the electronic device verifies the first semantic structure, that is, determines whether the first semantic structure is the target first semantic structure, and determines whether the number of target first semantic structures reaches a certain number. If the verification fails, other first semantic structures other than the target first semantic structure can be re-input into the large language model as negative examples to guide the large language model to regenerate new first semantic structures, thereby constructing a correction loop to achieve self-correction and reduce the error rate of database statement generation.
[0088] In this embodiment, if the target first semantic structure satisfies the first preset condition, the electronic device can obtain the target table and target field corresponding to the variant statements corresponding to the target first semantic structure from the database according to each target first semantic structure, so as to take the union of the target table and the target field to obtain the target data structure, and then input the target data structure and the variant statements corresponding to each target first semantic structure into the large language model for processing to obtain the database statements corresponding to each variant statement corresponding to the target first semantic structure.
[0089] Optionally, if the target first semantic structure satisfies the first preset condition, the electronic device may directly delete the variant statement that failed to parse, and obtain the corresponding target table and target field according to the target first semantic structure corresponding to the variant statement that successfully parsed.
[0090] Please continue to refer to the above example. If the number of target first semantic structures reaches 8, the electronic device can delete the two variant statements that failed to parse, and obtain the target tables and target fields corresponding to the 8 variant statements that were successfully parsed based on the 8 target first semantic structures, thereby obtaining the target data structure corresponding to the entire variant statement that was successfully parsed by taking the union of the target tables and target fields corresponding to the 8 variant statements.
[0091] In this example, the electronic device may also input the target data structure and the eight variant sentences into a large language model for processing, thereby obtaining eight database sentences.
[0092] In this embodiment, by re-inputting the semantic structure corresponding to the variant sentence that failed to parse as a negative example into the large language model and re-extracting the new semantic structure corresponding to the variant sentence, the hallucination problem of the large language model can be eliminated to the greatest extent. In this way, the burden of the large language model when retrieving the target table and target field can be effectively reduced, the recall rate of the target table and target field can be improved, and the recall rate of the generated database statement can be improved.
[0093] Optionally, since multiple database statements are generated at this time, and there may be certain differences between the database statements, it is also necessary to select the most appropriate one from the multiple database statements to be returned as the result.
[0094] Next, a possible implementation method is provided for determining a target database statement from multiple database statements.
[0095] Specifically, the electronic device may first execute each database statement separately, and determine multiple first candidate database statements from multiple database statements according to the execution results corresponding to each database statement.
[0096] Optionally, the first candidate database statement is a successfully executed database statement.
[0097] In this embodiment, the electronic device may further generate a regenerated statement corresponding to each first candidate database statement, and determine a second candidate database statement from a plurality of first candidate database statements according to the regenerated statement corresponding to each first candidate database statement.
[0098] The regenerated sentence is the natural language corresponding to the first candidate database sentence.
[0099] Optionally, in order to verify whether a successfully executed database statement has hidden problems such as logic errors, the electronic device may generate corresponding regenerated statements for each successfully executed first candidate database statement, that is, generate natural statements based on the database statements.
[0100] Optionally, since the regenerated statement can be used to determine whether the successfully executed first candidate database statement has implicit problems such as logic errors, the second candidate database statement refers to a database statement that has neither explicit execution error problems nor implicit logic error problems.
[0101] In this embodiment, if there are multiple second candidate database statements, the electronic device can determine the target database statement from the multiple second candidate database statements based on the statement to be processed, the regenerated statements corresponding to each second candidate database statement, and the execution result; if there is only one second candidate database statement, the second candidate database statement is determined as the target database statement.
[0102] Optionally, if there are multiple second candidate database statements, the electronic device can calculate a measurement score, such as a confidence score, for each second candidate database statement based on the statement to be processed, the regenerated statement corresponding to each second candidate database statement, and the execution result, so as to select the target database statement based on the confidence score. It can be understood that in the process of screening the target database statement, the electronic device can not only verify whether the database statement is successfully executed, thereby avoiding the problem of explicit errors, but also verify whether there are logical errors in the database statement, thereby avoiding the problem of implicit errors, so the accuracy of the target database statement finally screened can be improved.
[0103] Optionally, considering that if the number of first candidate database statements is too small, it may result in the inability to screen out suitable second candidate database statements, before generating the regenerated statements corresponding to each first candidate database statement, the electronic device may also determine whether the first candidate database statement meets the second preset condition.
[0104] In a possible implementation manner, the second preset condition may be that the ratio of the number of first candidate database statements to the number of all database statements reaches a second preset value.
[0105] Optionally, the second preset value can be set according to actual application conditions, for example, to 80%, and this application does not impose too many restrictions on this.
[0106] In another possible implementation manner, the first preset condition may also be that the number of target first semantic structures reaches a first preset number.
[0107] Optionally, the first preset number can be set by the user according to actual application conditions, for example, 10, etc., and this application does not impose too many restrictions on this.
[0108] Optionally, if the first candidate database statement does not meet the second preset condition, the first variant statement is determined based on other database statements in the database statement except the first candidate database statement, a new database statement corresponding to the first variant statement is regenerated, and the first database statement is determined from the new database statements until the first candidate database statement meets the second preset condition.
[0109] Optionally, all database statements except the first candidate database statement are database statements that have failed to execute.
[0110] In this embodiment, if the first candidate database statement does not meet the second preset condition, the electronic device can determine a variant statement that failed to execute, that is, the first variant statement, based on the database statement that failed to execute, and regenerate a new database statement corresponding to the first variant statement, thereby filtering out the first candidate database statement from the new database statements, until the newly filtered first candidate database statement and the previously filtered first candidate database statement jointly meet the second preset condition.
[0111] In an example, if there are 10 database statements, the second preset condition is that the number of first candidate database statements accounts for 80% of the total number of database statements, and only 2 first candidate database statements are screened out from the 10 database statements, and 8 database statements fail to execute, the electronic device can determine the variant statements corresponding to the 8 database statements as the first variant statements, re-generate new database statements for the 8 first variant statements, and re-determine the first candidate database statement from the 8 new database statements.
[0112] In this example, if the number of first candidate database statements determined from the eight new database statements reaches six, then combined with the two first candidate database statements determined previously, it can be determined that the number of first candidate database statements that are successfully executed among the ten database statements accounts for 80%, and therefore it can be determined that the first candidate database statement meets the second preset condition.
[0113] In this example, if the number of first candidate database statements determined from the eight new database statements does not reach six, for example, only four, then combined with the two first candidate database statements determined previously, it can be determined that the number of first candidate database statements that are successfully executed among the ten database statements is only six, and its proportion does not reach 80%, and the second preset condition is still not met. Therefore, new database statements can be generated again for the variant statements corresponding to the four database statements that were not successfully executed, and the first candidate database statements can be re-determined from the four new database statements until the number of first candidate database statements finally determined accounts for 80%.
[0114] Optionally, considering that there may be a situation where the first candidate database statement cannot meet the first preset condition even if the above steps are repeated many times, at this time, in order to prevent falling into an infinite loop, the electronic device may report an error.
[0115] In this embodiment, the electronic device can verify the database statement, that is, determine whether the database statement is the first candidate database statement, and determine whether the number of first candidate database statements reaches a certain number. If the verification fails, the database statements other than the first candidate database statements can be re-input into the large language model as negative examples to guide the large language model to regenerate new database statements, thereby building a correction loop to achieve self-correction and reduce the error rate of database statement generation.
[0116] Optionally, for query scenarios, the specific situations of execution failure may include the following two situations: 1. Query field error, that is, there is no relevant field or relevant table; 2. Return is empty, that is, no relevant data is found, so different methods can be used to generate new database statements for these two situations respectively.
[0117] In this embodiment, if a query field error occurs, it may be because an error occurs in the target data structure retrieved from the database. Therefore, for the first variant statement corresponding to the database statement in which the query field error occurs, the electronic device can add a negative label to the database statement, and re-input the error cause (field error), error field, the database statement, and the first variant statement into the large language model for processing, thereby regaining the target table and target field corresponding to the first variant statement, and then regaining the target data structure to regenerate the database statement.
[0118] In this embodiment, if the return value is empty, it may be because there is a problem with the generated database statement itself. Therefore, for the first variant statement corresponding to the database statement that returns empty, the electronic device can add a negative label to the database statement, and re-input the error cause (returned empty), the database statement and the first variant statement into the large language model for processing, so as to regain the target data structure to regenerate the database statement.
[0119] It can be understood that regenerating corresponding database statements in different ways according to different execution failure reasons can maximize the execution success rate of the regenerated database statements.
[0120] In this embodiment, if the first candidate database statements meet the second preset condition, the electronic device may generate regenerated statements corresponding to each first candidate database statement.
[0121] Next, a possible implementation method is provided for how to generate a regenerated statement corresponding to each first candidate database statement, and determine a second candidate database statement from multiple first candidate database statements according to the regenerated statement corresponding to each first candidate database statement.
[0122] Specifically, the electronic device can input each first candidate database statement into the large language model for processing, obtain the regenerated statement corresponding to each first candidate database statement, and then determine the first candidate database statement corresponding to the regenerated statement that is consistent with the semantic logic of the statement to be processed as the second candidate database statement.
[0123] Optionally, the electronic device may process each first candidate database statement according to a pre-stored regeneration template to obtain a Prompt corresponding to each first candidate database statement, and then input the Prompt corresponding to each first candidate database statement into the large language model for processing to obtain a regenerated statement corresponding to each first candidate database statement.
[0124] Optionally, the electronic device may also determine, through the large language model, a regenerated statement that is semantically logically consistent with the statement to be processed, thereby determining the first candidate database statement corresponding to the regenerated statement as the second candidate database statement.
[0125] In one possible implementation, the large language model can represent the parsing result with a binary pseudo-label, for example, 0, 1, where 0 represents that the semantic logic of the regenerated statement is consistent with that of the statement to be processed, and 1 represents that the semantic logic of the regenerated statement is inconsistent with that of the statement to be processed. The electronic device can then determine whether the first candidate database statement corresponding to the regenerated statement is the second candidate database statement based on the output pseudo-label.
[0126] It is understandable that if there is no second candidate database statement, it means that the successfully executed database statements all have logical errors, that is, these database statements are generated incorrectly. Therefore, the electronic device can regenerate database statements for all variant statements corresponding to the first candidate database statements in the absence of the second candidate database statement to re-screen the second candidate database statements.
[0127] In one possible implementation, if there is no regenerated statement that is semantically logically consistent with the statement to be processed, the electronic device may determine the variant statement corresponding to each first candidate database statement as a second variant statement, and use each first candidate database statement as a negative example to regenerate a new database statement corresponding to each second variant statement, and obtain the first candidate database statement from the new database statement, and generate a regenerated statement corresponding to each first candidate database statement until at least one regenerated statement that is semantically logically consistent with the statement to be processed is obtained.
[0128] Optionally, the electronic device may take each first candidate database sentence as a negative example, add a negative label to each first candidate database sentence, and re-input each first candidate database sentence and its corresponding second variant sentence into the large language model for processing to regenerate the database sentence corresponding to each second variant sentence.
[0129] Optionally, the negative label may indicate that the first candidate database statement has a logical error.
[0130] Optionally, in order to ensure the executability of the regenerated database statement, the electronic device may determine whether the regenerated database statement is the first candidate database statement, and then regenerate a regenerated statement corresponding to the first candidate database statement, and determine whether the semantic logic of the regenerated statement is consistent with that of the statement to be processed, until at least one regenerated statement that is consistent with the semantic logic of the statement to be processed is obtained.
[0131] In this embodiment, the electronic device can verify the first candidate database statement, that is, determine whether the second candidate database statement exists in the first candidate database statement. If the verification fails, the first candidate database statement can be re-input into the large language model as a negative example to guide the large language model to regenerate a new database statement, thereby building a correction loop to achieve self-correction and reduce the error rate of database statement generation.
[0132] In this embodiment, if only one second candidate database statement is obtained, the second candidate database statement can be directly determined as the target database statement. If multiple second candidate database statements are obtained, the electronic device also needs to filter out the target database statement from the multiple second candidate database statements.
[0133] Next, a possible implementation method is provided for determining a target database statement from multiple second candidate database statements according to the statement to be processed, the regenerated statements corresponding to each second candidate database statement, and the execution results.
[0134] Specifically, the electronic device can calculate the similarity scores corresponding to each second candidate database statement based on the statement to be processed and the regenerated statements corresponding to each second candidate database statement, and vote and score each second candidate database statement based on the execution result of the second candidate database statement to obtain the voting score of each second candidate database statement.
[0135] The similarity score represents the similarity between the regenerated statement of the second candidate database statement and the statement to be processed, and the voting score represents the credibility of the second candidate database statement.
[0136] Optionally, the electronic device may calculate the similarity score by using a vector similarity technique, and calculate the voting score by using a result voting scoring method.
[0137] In this embodiment, the voting scoring method accurately counts the occurrence frequency of each second candidate database statement, automatically identifies and assigns a score corresponding to each execution result according to the occurrence frequency, and thus calculates the voting score according to the score corresponding to each execution result.
[0138] Specifically, the more frequently an execution result occurs, the higher the score corresponding to the execution result.
[0139] In this embodiment, whether the execution result of the second candidate database statement is consistent with the execution results of other second candidate database statements can be determined through result voting and scoring.
[0140] It can be understood that if the similarity score is higher, the semantic logic of the second candidate database statement is more similar to the semantic logic of the statement to be processed input by the user; if the voting score is higher, it means that the execution result corresponding to the second candidate database statement appears more times, and the second candidate database statement is more credible.
[0141] In this embodiment, the electronic device can calculate the confidence score corresponding to each second candidate database statement according to the similarity score and the voting score, thereby determining the second candidate database statement with the highest confidence score as the target database statement.
[0142] Optionally, corresponding weight values can be set in advance for the similarity score and the voting score, and the electronic device can calculate the confidence score of each second candidate database statement according to its similarity score, the weight value corresponding to the similarity score, the voting score, and the weight value corresponding to the voting score.
[0143] In one possible implementation, the confidence score may be calculated according to the following formula: X=aM+bN Among them, X represents the confidence score, a represents the weight value corresponding to the similarity score, b represents the weight value corresponding to the voting score, M represents the similarity score, and N represents the voting score.
[0144] In one example, Figure 3 For an example diagram of a statement for selecting a target database, see Figure 3For the statement to be processed input by the user, "Find the car model with the highest sales between July 15 and July 20", four variant statements can be generated, among which the database statement corresponding to variant statement No. 3 fails to execute, and the regenerated statement corresponding to the database statement corresponding to variant statement No. 4 has different semantic logic from the statement to be processed. Therefore, it can be determined that the second candidate database statement is the database statement generated corresponding to variant statement No. 1 and the database statement generated corresponding to variant statement No. 2.
[0145] In this example, the electronic device can respectively calculate the confidence score of the database statement generated corresponding to variant statement No. 1 and the confidence score of the database statement generated corresponding to variant statement No. 2, thereby ultimately determining that the target database statement is the database statement generated corresponding to variant statement No. 2.
[0146] It can be understood that in this embodiment, the electronic device can converge the expanded search space by calculating the confidence level, thereby further improving the recall rate of the database statement.
[0147] Next, we take the query problem of the statement to be processed input by the user and the database language as SQL as an example. Figure 4 The method for generating database statements based on a large language model provided in the embodiment of the present application is introduced as a whole. Specifically, Figure 4 For another flowchart of the method for generating database statements based on a large language model provided in the embodiment of the present application, see Figure 4 .
[0148] First, users can input user questions, and the Large Language Model (LLM) will output multiple variant questions.
[0149] Optionally, the electronic device can also extract semantic structures, i.e., entities and relationships, from user questions and multiple generated variant questions through a large language model, and thereby determine whether the extracted entities and relationships are logically consistent with the original user questions based on the first semantic structure corresponding to each variant question and the second semantic structure corresponding to the user question.
[0150] If the number of consistent target first semantic structures does not reach a certain number, the first semantic structure corresponding to the variant question that is inconsistent with the logic of the original user question is used as a negative example, and the first semantic structure and the variant question are re-input into the large language model to extract entities and relationships until the number of consistent target first semantic structures reaches a certain number.
[0151] If the number of consistent target first semantic structures reaches a certain number, the target first semantic structure is input into the large language model, and the large language model retrieves the target table and target field corresponding to the variant sentences corresponding to each target first semantic structure from the database, and takes the union of multiple target tables and target fields to obtain the target data structure.
[0152] The union (target data structure) and the variant questions corresponding to each target first semantic structure are input into the large language model for processing, so as to generate a corresponding SQL statement for each variant question and execute each SQL statement.
[0153] If the number of SQL statements that failed to execute reaches a certain number, SQL statements can be regenerated according to the reasons for passing the execution. That is, if the execution failed due to a field error, it can be input as a negative example into the large language model to reselect the target table and target field; if the execution failed due to a null return, it can be used as a negative example to re-guide the generation of the corresponding SQL statement until a certain number of SQL statements are executed.
[0154] If the number of SQL statements that have been executed reaches a certain number, the large language model can be used to generate re-generated questions for the SQL statements that have been executed, and the large language model can be used to determine whether the semantic logic of the user questions and each re-generated question is consistent.
[0155] If all the regenerated questions are inconsistent with the semantic logic of the user's questions, they can be used as negative examples to re-guide the generation of corresponding SQL statements.
[0156] If there is at least one regenerated question that is consistent with the semantic logic of the user question (mainly for the case where there are multiple regenerated questions that are consistent with the semantic logic of the user question), the results of the SQL statements corresponding to each regenerated question can be voted on to obtain the voting score of each SQL statement.
[0157] In addition, it is necessary to determine the similarity score of each SQL statement by calculating the embedding similarity between each regenerated question and the user question.
[0158] Finally, the final target SQL statement is determined by weighted calculation of the voting score and the similarity score, and the data queried according to the target SQL statement is output.
[0159] In order to execute the corresponding steps in the above embodiments and various possible methods, a method for implementing a database statement generation device based on a large language model is provided below. Optionally, the database statement generation device based on a large language model can adopt the above Figure 1 For further information, please refer to Figure 5 , Figure 5 A functional module diagram of a database statement generation device based on a large language model provided in an embodiment of the present application. It should be noted that the basic principle and technical effects of the database statement generation device based on a large language model provided in this embodiment are the same as those in the above-mentioned embodiment. For the sake of brief description, for the parts not mentioned in this embodiment, reference can be made to the corresponding contents in the above-mentioned embodiment. The database statement generation device based on a large language model includes: a generation module 200 and a determination module 210.
[0160] The generation module 200 is used to obtain a sentence to be processed input by a user, and generate multiple variant sentences corresponding to the sentence to be processed through a large language model; the sentence to be processed and the variant sentences are both natural languages, and the variant sentences have different grammatical structures from the sentence to be processed, but consistent semantic logic.
[0161] It can be understood that the generation module 200 can also be used to execute the above step S20.
[0162] The generation module 200 is also used to obtain a target data structure from a database according to a plurality of variant sentences using a large language model, and to generate a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure.
[0163] It can be understood that the generation module 200 can also be used to execute the above step S21.
[0164] The determination module 210 is used to determine a target database statement from multiple database statements; the target database statement is a database statement that can be successfully executed and has the highest confidence.
[0165] It can be understood that the determination module 210 can also be used to execute the above step S22.
[0166] Optionally, the generation module 200 is further used to input each variant statement into the large language model for processing to obtain a first semantic structure corresponding to each variant statement; obtain a target table and a target field corresponding to each variant statement from a database according to the first semantic structure corresponding to each variant statement, take a union of the target table and the target field, and obtain a target data structure; input the target data structure and each variant statement into the large language model for processing to obtain a database statement corresponding to each variant statement.
[0167] Optionally, the generation module 200 is also used to input the sentence to be processed into the large language model for processing, obtain the second semantic structure corresponding to the sentence to be processed, and determine whether the target first semantic structure with the same semantics as the second semantic structure meets the first preset condition; if the target first semantic structure does not meet the first preset condition, add negative labels to the semantic structures other than the target first semantic structure, and re-input the other first semantic structures with the negative labels and the variant sentences corresponding to the other first semantic structures into the large language model for processing, obtain the new first semantic structure corresponding to the variant sentence, and determine the target first semantic structure from the new first semantic structure until the target first semantic structure meets the first preset condition.
[0168] Optionally, the generation module 200 is also used to obtain the target table and target field corresponding to the variant statements corresponding to the target first semantic structure from the database according to each target first semantic structure if the target first semantic structure satisfies the first preset condition; take the union of the target table and the target field to obtain the target data structure; input the target data structure and the variant statements corresponding to each target first semantic structure into the large language model for processing to obtain the database statements corresponding to each variant statement corresponding to the target first semantic structure.
[0169] Optionally, the determination module 210 is further configured to execute each database statement separately, and determine multiple first candidate database statements from multiple database statements according to the execution results corresponding to each database statement; generate regenerated statements corresponding to each first candidate database statement, and determine a second candidate database statement from multiple first candidate database statements according to the regenerated statements corresponding to each first candidate database statement; wherein the regenerated statements are natural languages corresponding to the first candidate database statements; if there are multiple second candidate database statements, determine a target database statement from multiple second candidate database statements according to the statement to be processed, the regenerated statements corresponding to each second candidate database statement, and the execution results; if there is one second candidate database statement, determine the second candidate database statement as the target database statement.
[0170] Optionally, the determination module 210 is further used to input each first candidate database statement into the large language model for processing, to obtain a regenerated statement corresponding to each first candidate database statement; and to determine the first candidate database statement corresponding to the regenerated statement that is semantically logically consistent with the statement to be processed as the second candidate database statement.
[0171] Optionally, the determination module 210 is further used to determine whether the first candidate database statement satisfies a second preset condition; if the first candidate database statement does not satisfy the second preset condition, determine the first variant statement according to other database statements in the database statement except the first candidate database statement, regenerate a new database statement corresponding to the first variant statement, and determine the first candidate database statement from the new database statement until the first candidate database statement satisfies the second preset condition; if the first candidate database statement satisfies the second preset condition, generate the regenerated statements corresponding to each first candidate database statement.
[0172] Optionally, the determination module 210 is further used to determine the variant statement corresponding to each first candidate database statement as a second variant statement if there is no regenerated statement that is semantically logically consistent with the statement to be processed, and to use each first candidate database statement as a negative example to regenerate a new database statement corresponding to each second variant statement, and to obtain the first candidate database statement from the new database statement, and to generate a regenerated statement corresponding to each first candidate database statement, until at least one regenerated statement that is semantically logically consistent with the statement to be processed is obtained.
[0173] Optionally, the determination module 210 is further used to calculate the similarity score corresponding to each second candidate database statement according to the statement to be processed and the regenerated statement corresponding to each second candidate database statement; the similarity score represents the similarity between the regenerated statement of the second candidate database statement and the statement to be processed; according to the execution result of the second candidate database statement, each second candidate database statement is voted and scored to obtain the voting score of each second candidate database statement; the voting score represents the credibility of the second candidate database statement; for each second candidate database statement, according to the similarity score and the voting score, the confidence score corresponding to the second candidate database statement is calculated; and the second candidate database statement with the highest confidence score is determined as the target database statement.
[0174] Optionally, the above modules can be stored in the form of software or firmware. Figure 1 The memory shown in the figure or solidified in the operating system (OS) of the electronic device, and can be Figure 1 Meanwhile, the data and program codes required for executing the above modules can be stored in the memory.
[0175] The embodiment of the present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method for generating a database statement based on a large language model provided in the embodiment of the present application is implemented.
[0176] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present application. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of the code, and a part of the module, program segment or code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0177] In addition, the functional modules in the various embodiments of the present application may be integrated together to form an independent part, or each module may exist separately, or two or more modules may be integrated to form an independent part.
[0178] If the function is implemented in the form of a software function module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present application can be essentially or partly embodied in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, server, or network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, and other media that can store program codes.
[0179] The above description is only the preferred embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various modifications and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method for generating database sentences based on a large language model, characterized in that: The method comprises: Obtaining a sentence to be processed input by a user, and generating multiple variant sentences corresponding to the sentence to be processed through a large language model; the sentence to be processed and the variant sentences are both natural languages, and the variant sentences have different grammatical structures from the sentence to be processed but consistent semantic logic; Obtaining a target data structure from a database according to the plurality of variant sentences using the large language model, and generating a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure; A target database statement is determined from the multiple database statements; the target database statement is a database statement that can be successfully executed and has the highest confidence.
2. The method according to claim 1, characterized in that The step of obtaining a target data structure from a database according to the plurality of variant sentences by using the large language model, and generating a database statement corresponding to each variant sentence according to each variant sentence and the target data structure, comprises: Inputting each of the variant sentences into a large language model for processing to obtain a first semantic structure corresponding to each of the variant sentences; According to the first semantic structure corresponding to each of the variant statements, the target table and the target field corresponding to each of the variant statements are acquired from the database, and a union of the target table and the target field is taken to obtain the target data structure; The target data structure and each of the variant sentences are input into the large language model for processing to obtain a database sentence corresponding to each of the variant sentences.
3. The method according to claim 2, characterized in that The method further comprises: Inputting the sentence to be processed into the large language model for processing, obtaining a second semantic structure corresponding to the sentence to be processed, and determining whether a target first semantic structure having the same semantics as the second semantic structure satisfies a first preset condition; If the target first semantic structure does not meet the first preset condition, negative labels are added to other semantic structures except the target first semantic structure, and the other first semantic structures with the negative labels added and the variant sentences corresponding to the other first semantic structures are re-input into the large language model for processing to obtain a new first semantic structure corresponding to the variant sentence, and the target first semantic structure is determined from the new first semantic structure until the target first semantic structure meets the first preset condition.
4. The method according to claim 3, characterized in that The step of acquiring a target table and a target field corresponding to each of the variant statements from the database according to the first semantic structure corresponding to each of the variant statements, taking a union of the target table and the target field, and obtaining the target data structure includes: If the target first semantic structure satisfies the first preset condition, acquiring a target table and a target field corresponding to a variant statement corresponding to the target first semantic structure from the database according to each target first semantic structure; Taking a union of the target table and the target field to obtain the target data structure; The step of inputting the target data structure and each of the variant sentences into the large language model for processing to obtain a database sentence corresponding to each of the variant sentences includes: The target data structure and the variant sentences corresponding to each of the target first semantic structures are input into the large language model for processing to obtain database sentences corresponding to each of the variant sentences corresponding to the target first semantic structures.
5. The method according to claim 1, characterized in that The step of determining a target database statement from the plurality of database statements comprises: Executing each of the database statements respectively, and determining a plurality of first candidate database statements from the plurality of database statements according to the execution results corresponding to each of the database statements; generating a regenerated statement corresponding to each of the first candidate database statements, and determining a second candidate database statement from a plurality of the first candidate database statements according to the regenerated statement corresponding to each of the first candidate database statements; Wherein, the regenerated sentence is the natural language corresponding to the first candidate database sentence; If there are multiple second candidate database statements, determine the target database statement from the multiple second candidate database statements according to the to-be-processed statement, the regenerated statements corresponding to each of the second candidate database statements, and the execution results; If there is one second candidate database statement, the second candidate database statement is determined as the target database statement.
6. The method according to claim 5, characterized in that The generating of the regenerated statements corresponding to the first candidate database statements, and determining the second candidate database statement from the plurality of the first candidate database statements according to the regenerated statements corresponding to the first candidate database statements, comprises: Inputting each of the first candidate database sentences into the large language model for processing, respectively, to obtain a regenerated sentence corresponding to each of the first candidate database sentences; A first candidate database statement corresponding to the regenerated statement that is semantically logically consistent with the statement to be processed is determined as the second candidate database statement.
7. The method according to claim 5, characterized in that Before generating the regenerated statements corresponding to the first candidate database statements, the method further includes: Determining whether the first candidate database statement satisfies a second preset condition; If the first candidate database statement does not satisfy the second preset condition, determining a first variant statement according to other database statements in the database statements except the first candidate database statement, regenerating a new database statement corresponding to the first variant statement, and determining a first candidate database statement from the new database statement, until the first candidate database statement satisfies the second preset condition; The generating of the regenerated statements corresponding to the first candidate database statements includes: If the first candidate database statements meet the second preset condition, then a regenerated statement corresponding to each of the first candidate database statements is generated.
8. The method according to claim 5, characterized in that The method further comprises: If there is no regenerated statement that is semantically logically consistent with the statement to be processed, the variant statement corresponding to each of the first candidate database statements is determined as a second variant statement, and each of the first candidate database statements is used as a negative example to regenerate a new database statement corresponding to each of the second variant statements, and the first candidate database statement is obtained from the new database statement to generate a regenerated statement corresponding to each of the first candidate database statements until at least one regenerated statement that is semantically logically consistent with the statement to be processed is obtained.
9. The method according to claim 5, characterized in that The step of determining a target database statement from a plurality of second candidate database statements according to the statement to be processed, the regenerated statements corresponding to each of the second candidate database statements, and the execution results includes: Calculating the similarity scores corresponding to the second candidate database statements respectively according to the statements to be processed and the regenerated statements corresponding to the second candidate database statements; the similarity scores represent the similarity between the regenerated statements of the second candidate database statements and the statements to be processed; According to the execution result of the second candidate database statement, voting and scoring each of the second candidate database statements to obtain a voting score of each of the second candidate database statements; the voting score represents the credibility of the second candidate database statement; For each of the second candidate database statements, respectively, calculate a confidence score corresponding to the second candidate database statement according to the similarity score and the voting score; The second candidate database statement with the highest confidence score is determined as the target database statement.
10. A database statement generation device based on a large language model, characterized in that: The device comprises: A generation module is used to obtain a sentence to be processed input by a user, and generate multiple variant sentences corresponding to the sentence to be processed through a large language model; the sentence to be processed and the variant sentences are both natural languages, and the variant sentences have different grammatical structures from the sentence to be processed, but have consistent semantic logic; The generating module is further configured to obtain a target data structure from a database according to the plurality of variant sentences through the large language model, and generate a database sentence corresponding to each variant sentence according to each variant sentence and the target data structure; The determination module is used to determine a target database statement from the multiple database statements; the target database statement is a database statement that can be successfully executed and has the highest confidence.
11. An electronic device, characterized in that: The method comprises a processor and a memory, wherein the memory stores a computer program executable by the processor, and the processor can execute the computer program to implement the method according to any one of claims 1 to 9.
12. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.
Citation Information
Patent Citations
Similar statement generation method and device based on pre-training language model
CN113807074A
Data processing method and device, medium and computing equipment
CN117539893A
Method, device, equipment and medium for converting natural language problem into SQL (Structured Query Language) statement
CN117667992A
SQL (Structured Query Language) statement generation method and device based on large language model, equipment and medium
CN117743371A
Intelligent query and visualization method based on natural language processing
CN117931852A
Cited By
Large model training-oriented multi-dimensional generalizable data generation method and device and software system
CN121189320A
A multi-dimensional generalizable data generation method, device and software system for large model training
CN121189320B
