A method and device for generating SQL statements based on a large language model

By using historical information to concatenate and modify the input text of a large language model in multi-turn dialogues, the problems of low accuracy and efficiency in generating SQL statements are solved, achieving more efficient and accurate SQL statement generation.

CN118656387BActive Publication Date: 2024-11-12HISENSE GRP HLDG CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411139469.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-08-20
Publication Date
2024-11-12
Estimated Expiration
2044-08-20

AI Technical Summary

Technical Problem

Existing technologies for generating SQL statements have low accuracy and efficiency, especially when large language models need to generate complex SQL statements from scratch during multi-turn dialogues, resulting in low efficiency and accuracy.

Method used

In multi-turn dialogues, the historical text to be converted and the corresponding historical SQL statements of the previous dialogue rounds are obtained. The text to be converted, the data table structure information, the SQL statement field modification rules, and the historical text to be converted and the historical SQL statements are concatenated and input into the large language model. By determining the distinguishing fields and modifying the distinguishing fields according to the SQL statement field modification rules and the data table structure information, the target SQL statement is generated.

Benefits of technology

It improves the accuracy and efficiency of SQL statement generation, reduces the logical reasoning burden on large language models when generating complex SQL statements, and enhances the accuracy and efficiency of the generation process.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118656387B_ABST
    Figure CN118656387B_ABST
Patent Text Reader

Abstract

The present application relates to the field of artificial intelligence technology, and in particular to a method and device for generating SQL statements based on a large language model. In the process of generating SQL statements in multiple dialogue rounds, if the received text to be converted is the text of the non-first dialogue round of the current dialogue paragraph, in order to generate accurate SQL statements based on previous historical dialogues, the historical text to be converted and the historical SQL statements are obtained, and the large language model is used to determine the distinguishing fields based on the text to be converted and the historical text to be converted, and the target fields of the distinguishing fields in the historical SQL statements are modified according to the SQL statement field modification rules and data table structure information to obtain the target SQL statement, without using the large language model to generate SQL statements based on complex texts, thereby improving the accuracy and efficiency of SQL statement generation.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of artificial intelligence technology, and in particular to a method and device for generating SQL statements based on a large language model. Background Art

[0002] With the development of large language models, the application of generating structured query language (SQL) statements based on large language models is becoming more and more widespread. In a dialogue with database query tasks, it is necessary to convert natural language questions into SQL statements, and then perform database operations based on the SQL statements.

[0003] In the process of generating SQL statements in multiple rounds of dialogue, the related technology generally merges the text to be converted received in the current dialogue round with the text to be converted received in the previous dialogue round to obtain the semantically complete text to be converted, and the large language model then generates the SQL statement corresponding to the semantically complete text to be converted. However, for the large language model, it is more difficult and redundant to generate complex SQL statements based on text every time, because the entire generation process is equivalent to starting from scratch, and the thinking and logical reasoning process is usually very complicated, resulting in low efficiency and accuracy of the generated SQL statements.

[0004] Therefore, how to improve the accuracy and efficiency of generating SQL statements becomes an urgent problem to be solved. Summary of the invention

[0005] The embodiments of the present application provide a method and device for generating SQL statements based on a large language model, so as to solve the problem of low accuracy and efficiency in generating SQL statements in the prior art.

[0006] In a first aspect, the present application provides a method for generating SQL statements based on a large language model, the method comprising:

[0007] If the received text to be converted is the text of a non-first dialogue round of the current dialogue segment, then the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds are obtained;

[0008] The text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement are concatenated to obtain a first input text;

[0009] The first input text is input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rules and the data table structure information to obtain the target SQL statement of the text to be converted.

[0010] In a second aspect, the present application provides a method for generating SQL statements based on a large language model, the method comprising:

[0011] An acquisition module, used for acquiring historical texts to be converted and corresponding historical SQL statements of historical conversation rounds if the received text to be converted is text of a non-first conversation round of the current conversation paragraph;

[0012] A splicing module, used for splicing the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement to obtain a first input text;

[0013] A generation module is used to input the first input text into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rules and the data table structure information to obtain the target SQL statement of the text to be converted.

[0014] In a third aspect, the present application provides an electronic device, comprising a processor, wherein the processor is configured to implement the steps of any of the above-described methods for generating SQL statements based on a large language model when executing a computer program stored in a memory.

[0015] In a fourth aspect, the present application further provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of any of the above-mentioned methods for generating SQL statements based on a large language model.

[0016] In the embodiment of the present application, in the process of generating SQL statements for multiple dialogue rounds, if the received text to be converted is the text of a non-first dialogue round of the current dialogue paragraph, in order to generate accurate SQL statements based on previous historical dialogues, the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds are obtained, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statements are spliced ​​and input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rule and the data table structure information to obtain the target SQL statement of the text to be converted, uses the large model to analyze the distinguishing fields, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the preset SQL statement field modification rule, so that the large language model does not need to generate SQL statements based on complex texts, thereby improving the accuracy and efficiency of SQL statement generation. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0018] Figure 1 A flowchart of a SQL statement generation process based on a large language model provided in an embodiment of the present application;

[0019] Figure 2 A schematic diagram of inputting text provided in an embodiment of the present application;

[0020] Figure 3 A schematic diagram of a text input construction process provided in an embodiment of the present application;

[0021] Figure 4 A schematic diagram of the structure of a SQL statement generation device based on a large language model provided in an embodiment of the present application;

[0022] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present application clearer, the technical solution of the embodiment of the present application will be clearly and completely described below in conjunction with the accompanying drawings. Obviously, the described embodiment is only a part of the embodiment of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field belong to the scope of protection of this application.

[0024] The embodiment of the present application provides a method and device for generating SQL statements based on a large language model. In the method, if the received text to be converted is the text of a non-first dialogue round of the current dialogue paragraph, the historical text to be converted and the corresponding historical SQL statement of the historical dialogue round are obtained; the text to be converted, data table structure information, SQL statement field modification rules, historical text to be converted and historical SQL statements are spliced ​​to obtain a first input text; the first input text is input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and according to the SQL statement field modification rules and the data table structure information, the target field of the distinguishing field in the historical SQL statement is modified to obtain the target SQL statement of the text to be converted.

[0025] To facilitate understanding, some terms involved in the embodiments of the present application are explained below.

[0026] Dialogue turn: Each interaction between a user and an electronic device is called a dialogue turn. In a multi-turn dialogue, the user and the electronic device have conversations alternately, and each interaction constitutes a new dialogue turn.

[0027] Dialogue paragraph: During a conversation, multiple consecutive rounds of dialogue around the same topic or task, these dialogue rounds together constitute a complete dialogue paragraph.

[0028] Figure 1 A flowchart of a SQL statement generation process based on a large language model provided in an embodiment of the present application is shown in FIG. Figure 1 As shown, the process includes the following steps:

[0029] S101: If the received text to be converted is text of a non-first dialogue round of the current dialogue segment, then historical text to be converted and corresponding historical SQL statements of historical dialogue rounds are obtained.

[0030] The SQL statement generation method based on the large language model provided in the present application is applied to an electronic device, which may be a server, a PC, a mobile terminal, etc.

[0031] In a coherent conversation, the user's questions are usually closely related to the previous questions or answers. In the embodiment of the present application, the text to be converted in the current conversation round can be obtained by modifying the historical SQL statements that have been generated. For complex texts to be converted, a large language model is required to establish accurate logical relationships between multiple information fragments, which is prone to hallucination problems of large models. Compared with regenerating SQL statements based on text, modifying existing SQL statements involves less logical reasoning, which can alleviate the problem of hallucinations of large models, thereby improving the accuracy of SQL statement generation.

[0032] If the received text to be converted is the text of a non-first dialogue round of the current dialogue segment, it means that the target SQL statement corresponding to the text to be converted can be modified based on the historical SQL statement corresponding to the historical text to be converted. Therefore, in the embodiment of the present application, the historical text to be converted of the historical dialogue round of the current dialogue segment and the historical SQL statement corresponding to the historical text to be converted can be obtained. Among them, the historical text to be converted obtained can be the text to be converted of the previous dialogue round of the dialogue round where the text to be converted is located, or it can be the text to be converted of all dialogue rounds before the dialogue round where the text to be converted is located, or it can be the text to be converted of a preset number of dialogue rounds before the dialogue round where the text to be converted is located. In other words, the historical text to be converted obtained can be one or more, and similarly, the corresponding historical SQL statement can be one or more.

[0033] The following is an explanation of dialogue paragraphs and dialogue turns with the help of a specific example.

[0034] Turn 1:

[0035] Question: What are the names and ID card information of existing motor vehicle drivers in Qingdao?

[0036] SQL: SELECT name, idcard FROM drivinglicense v WHERE v. issuing_unitIN ('LuB', 'LuU') AND not REGEXP_LIKE(v.license_status, 'C|E|F|G').

[0037] Turn 2:

[0038] Question: Which ones are in Shinan District?

[0039] SQL: SELECT name, idcard FROM drivinglicense v WHERE v.issuing_unit IN('LuB', 'LuU') AND not REGEXP_LIKE(v.license_status, 'C|E|F|G') ANDv.district= 'Shinan District'.

[0040] Turn 3:

[0041] Question: Display the region.

[0042] SQL: SELECT name, idcrad, district FROM drivinglicense v WHEREv.issuing_unit IN ('LuB', 'LuU') AND not REGEXP_LIKE(v.license_status, 'C|E|F|G') AND v.district= 'Shinan District'.

[0043] The questions shown in the above example can be understood as questions input by the user, that is, the text to be converted in each dialogue turn. SQL can be understood as the response made by the large language model to the questions input by the user, that is, the SQL statement output by the large language model. The above three groups of "question-SQL" can be regarded as a dialogue paragraph, and Turn1, Turn2 and Turn3 represent three dialogue turns.

[0044] S102: The text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statements are concatenated to obtain a first input text.

[0045] After obtaining the historical text to be converted and the historical SQL statements, the text to be converted, the data table structure information, the SQL statement field modification rules, the historical text to be converted and the historical SQL statements can be spliced ​​to obtain the first input text. Among them, the database table structure information is a text that describes each data table in the database, so that the large language model knows the data tables included in the database, and the field information included in each data table and the meaning represented. The SQL statement field modification rules are used to prompt the large language model how to modify the historical SQL statements to obtain the text of the target SQL statement. Exemplarily, the SQL statement field modification rules can be: determine the field list ColumnList of the new question query, traverse ColumnList, and for each element in the field list, call the element CurrentColumn, if the CurrentColumn does not exist in the SELECT clause corresponding to the historical question, then add the CurrentColumn to the SELECT clause, if there is an element OldColumn in the old field list OldColumnList of the SELECT clause that is not included in ColumnList, then delete the OldColumn from the SELECT clause. It should be noted that when splicing the texts to obtain the first input text, the splicing can be performed in any order.

[0046] S103: Input the first input text into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rules and the data table structure information, so as to obtain the target SQL statement of the text to be converted.

[0047] After obtaining the first input text, the first input text can be input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted. That is to say, the large language model analyzes the text to be converted and the historical text to be converted, determines the difference between the two texts, and thus determines the distinguishing fields in the difference obtained by analysis. Exemplarily, the historical text to be converted is: What is the total number of motor vehicles in Beijing, and the text to be converted is: What about Changping District. After the large language model analyzes the text to be converted and the historical text, it can be determined that the text to be converted asks: What is the total number of motor vehicles in Changping District, Beijing. Then, it can be determined that the historical text to be converted asks the total number of all motor vehicles in all districts of Beijing, while the text to be converted asks the total number of motor vehicles in Changping District, Beijing, and it can be determined that the distinguishing field is "Changping District".

[0048] After determining the distinguishing fields, the large language model can modify the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rules and data table structure information to obtain the target SQL statements of the text to be converted.

[0049] For example, the target SQL statement obtained by analyzing the large language model can be analyzed according to the following ideas:

[0050] Question 1: Name and ID card information of existing motor vehicle drivers in Qingdao.

[0051] Let's think about it step by step. Question 1 can be generated directly instead of being modified from the previous SQL statement.

[0052] So, SQL1:

[0053] SELECT name, idcard FROM drivinglicense v WHERE v. issuing_unit IN ('LuB', 'LuU') AND not REGEXP_LIKE(v.license_status, 'C|E|F|G').

[0054] Question 2: Which ones are in Shinan District?

[0055] Let's think about it step by step. Question 2 can be obtained by modifying question 1. The previous question is to obtain the name and ID card information list of existing motor vehicle drivers in Qingdao. The current question needs to filter out the area of ​​"Shinan District" from the previous round, so the modification operation needs to be used as follows:

[0056] * The Select clause does not need to be modified

[0057] *Add a condition to the Where clause: AND v.district= 'Shinan District'

[0058] * The From clause does not need to be modified

[0059] * The OrderBy clause does not need to be modified

[0060] ...(list all clause modifications involved in the rules)

[0061] So SQL2:

[0062] SELECT name, idcard FROM drivinglicense v WHERE v.issuing_unit IN ('LuB', 'LuU') AND v.license_status NOT IN ('C', 'E', 'F', 'G') AND v.district= 'Shinan District'.

[0063] In the above example, "Shinan District" is the distinguishing field.

[0064] It should be noted that how a large language model generates a target SQL statement according to a prompt word is an existing technology, and technicians in this field can configure the specific content of the prompt word as needed. The prompt word is the first input text mentioned above.

[0065] In the embodiment of the present application, in the process of generating SQL statements for multiple dialogue rounds, if the received text to be converted is the text of a non-first dialogue round of the current dialogue paragraph, in order to generate accurate SQL statements based on previous historical dialogues, the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds are obtained, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statements are spliced ​​and input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rule and the data table structure information to obtain the target SQL statement of the text to be converted, uses the large model to analyze the distinguishing fields, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the preset SQL statement field modification rule, so that the large language model does not need to generate SQL statements based on complex texts, thereby improving the accuracy and efficiency of SQL statement generation.

[0066] In order to improve the accuracy of SQL statement generation, based on the above embodiment, in the embodiment of the present application, the method further includes:

[0067] If the received text to be converted is the text of the first dialogue round of the current dialogue paragraph, the text to be converted and the data table structure information are concatenated to obtain a second input text;

[0068] The second input text is input into the large language model, so that the large language model generates a target SQL statement for the text to be converted according to the text to be converted and the data table structure information.

[0069] Since there are no other historical conversation turns before the text of the first conversation turn of the current conversation paragraph, and the text to be converted of the first conversation turn of the current conversation paragraph is not necessarily similar to the intention / topic of other conversation paragraphs. Therefore, in an embodiment of the present application, if the received text to be converted is the text of the first conversation turn of the current conversation paragraph, the target SQL statement of the text to be converted can be directly generated based on the large language model. Specifically, the text to be converted and the data table structure information can be spliced ​​to obtain a second input text. And the second input text is input into the large language model, so that the large language model generates the target SQL statement of the text to be converted based on the text to be converted and the data table structure information. In an embodiment of the present application, a text that prompts the large language model to directly convert the text to be converted into an SQL statement can be added to the second input text.

[0070] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, the SQL statement field modification rule includes cost values ​​corresponding to different types of SQL clauses;

[0071] After the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and before modifying the target fields of the distinguishing fields in the historical SQL statements, the method further includes:

[0072] The large language model determines the target SQL clause to which the distinguishing field belongs in the SQL statement if the text to be converted is converted into an SQL statement, and each clause in the SQL statement is determined after the SQL statement is segmented according to preset keywords;

[0073] If the target cost value corresponding to the target type of the target SQL clause meets the preset threshold requirement, the subsequent step of modifying the target field of the distinguishing field in the historical SQL statement according to the SQL statement field modification rule and the data table structure information is continued.

[0074] In multiple rounds of dialogue, if there are too many modifications involved, the cost of modifying the historical SQL statements may exceed the cost of directly generating SQL statements based on text. In the embodiment of the present application, it is necessary to find a balance between directly generating SQL statements and modifying SQL statements. Therefore, the SQL statement field modification rules can include cost values ​​corresponding to different types of SQL clauses.

[0075] After determining the distinguishing field, before modifying the target field of the distinguishing field in the historical SQL statement, you can determine the target clause where the distinguishing field is located, and then determine whether to continue to execute the subsequent steps of modifying the target field of the distinguishing field in the historical SQL statement based on the cost value corresponding to the target clause.

[0076] In an embodiment of the present application, preset keywords can be pre-configured, and the preset keywords are used to split SQL statements. Exemplarily, the preset keywords may include: SELECT, FROM, Order By, WHERE, Group By, WITH, HAVING, Limit, Join, Union, FunctionCall, Subquery, etc. The preset keywords may be keywords that may appear in an SQL statement. In an embodiment of the present application, clauses including different keywords may be respectively determined as different types of SQL clauses. Exemplarily, the SQL statement "SELECT name, idcard FROM drivinglicense vWHERE v.issuing_unit IN ('Lu B', 'Lu U')" includes three SQL clauses, namely "SELECTname, idcard", "FROM drivinglicense v" and "WHERE v.issuing_unit IN ('Lu B', 'Lu U')". The types of these three SQL clauses are all different, namely, the SELECT clause type, the FROM clause type and the WHERE clause type.

[0077] When determining the target SQL clause to which the distinguishing field belongs in the SQL statement, the text to be converted can be converted into an SQL statement, and the SQL statement can be segmented according to preset keywords to obtain multiple clauses. The clause to which the distinguishing field belongs is determined, and the clause is determined as the target SQL clause.

[0078] After the target SQL clause is determined, the target cost value corresponding to the target type of the target SQL clause can be determined based on the correspondence between the pre-saved different types of clauses and the cost values. If the target cost value meets the preset threshold requirement, it means that the cost of modifying the historical SQL statement to obtain the target SQL statement is not very high, and the subsequent steps of modifying the target field of the distinguishing field in the historical SQL statement according to the SQL statement field modification rule and the data table structure information can be continued. Among them, the preset threshold requirement can be: the cost value is not higher than σ, where σ can be any value, for example, 0.8, 1.5, 2, 5, etc. Those skilled in the art can configure the cost values ​​corresponding to each type of SQL clause and the preset threshold requirements as needed, and the embodiments of the present application do not limit this.

[0079] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, the method further includes:

[0080] If the target cost value corresponding to the target type of the target SQL clause does not meet the preset threshold requirement, the large language model generates a target SQL statement for the text to be converted based on the text to be converted and the data table structure information.

[0081] If the determined target cost value does not meet the preset threshold requirement, it means that the cost of modifying the historical SQL statement to obtain the target SQL statement is very high, while the cost of directly generating an SQL statement based on the text to be converted is lower. Therefore, in an embodiment of the present application, the large language model can generate the target SQL statement of the text to be converted based on the text to be converted and the data table structure information.

[0082] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, if the distinguishing field includes multiple fields, the determination of the target SQL clause to which the distinguishing field belongs in the SQL statement when the text to be converted is converted into an SQL statement includes:

[0083] Respectively determine each target SQL clause to which each distinguishing field belongs in the SQL statement if the text to be converted is converted into an SQL statement;

[0084] The process of determining the target cost value corresponding to the target type of the target SQL clause includes:

[0085] Determine the first cost value corresponding to the target type of each target SQL clause according to the cost values ​​corresponding to the different types of SQL clauses included in the SQL statement field modification rule;

[0086] The target cost value is determined based on each of the first cost values.

[0087] Since there may be multiple distinguishing fields when modifying the historical SQL statement, when determining the target SQL clause, each target SQL clause to which each distinguishing field belongs in the SQL statement of the text to be converted may be determined respectively.

[0088] When determining the target cost value, the cost values ​​corresponding to different types of SQL clauses included in the SQL statement field modification rule may be used to respectively determine the first cost value corresponding to the target type of each target SQL clause.

[0089] After each first cost value is determined, a target cost value may be determined based on each first cost value, for example, the sum or product of all first cost values ​​may be determined as the target cost value.

[0090] Specifically, assume that the cost value corresponding to the SELECT clause type is 1, the cost value corresponding to the FROM clause type is 1.8, and the cost value corresponding to the WHERE clause type is 1.5. The distinguishing fields include distinguishing field A and distinguishing field B, where the target SQL clause to which distinguishing field A belongs is the SELECT clause (referred to as target SQL clause A), and the target SQL clause to which distinguishing field B belongs is the WHERE clause (referred to as target SQL clause B). Since the cost value corresponding to the SELECT clause type is 1, and the cost value corresponding to the WHERE clause type is 1.5, it can be determined that the first cost value corresponding to the target SQL clause A is 1, and the second cost value corresponding to the target SQL clause B is 1.5, and the target cost value can be determined to be 1+1.5=2.

[0091] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, after obtaining the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds, before splicing the text to be converted, the data table structure information, the SQL statement field modification rules, the historical text to be converted and the historical SQL statements, the method further includes:

[0092] Get pre-saved SQL statement field modification samples;

[0093] The step of concatenating the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted, and the historical SQL statement to obtain a first input text includes:

[0094] The SQL statement field modification sample, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement are concatenated to obtain a first input text; and the large language model is enabled to determine the distinguishing fields according to the text to be converted, the historical text to be converted and the SQL statement field modification sample.

[0095] In order to better enable the large language model to understand the SQL statement modification task, a small number of examples, i.e., few-shot data, are provided in the prompt words to guide the large language model to generate outputs that are more in line with expectations. In an embodiment of the present application, after obtaining the historical SQL statements, before splicing to obtain the first input text, a pre-saved SQL statement field modification sample can be obtained. The SQL statement field modification sample can be pre-written by relevant personnel, so that the large language model can determine the target SQL statement based on the analysis ideas of the SQL statement field modification sample. The SQL statement field modification sample can be one or more. When there are multiple SQL statement field modification samples, although the sample texts between different SQL statement field modification samples are different, the analysis ideas should be the same.

[0096] In an embodiment of the present application, when splicing and determining the first input text, the SQL statement field modification sample, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement can be spliced ​​to obtain the first input text. Subsequently, the large language model is processed based on the first input text, that is, the large language model is used to determine the distinguishing fields based on the text to be converted, the historical text to be converted and the SQL statement field modification sample. When determining the distinguishing fields, the analysis ideas of the large language model for the text to be converted and the historical text to be converted are similar to the analysis ideas in the SQL statement field modification sample.

[0097] In order to improve the efficiency of determining the SQL statement field modification sample, on the basis of the above embodiments, in the embodiment of the present application, the process of determining the SQL statement field modification sample includes:

[0098] Obtaining a first SQL statement of a first sample text and a second SQL statement of a second sample text, wherein the second sample text is a text for conducting a follow-up inquiry based on the first sample text;

[0099] Determine a first SQL syntax tree for the first SQL statement, and a second SQL syntax tree for the second SQL statement;

[0100] Determine a modification scheme for modifying the first SQL syntax tree into the second SQL syntax tree;

[0101] The first sample text, the second sample text and the modification scheme are determined as the SQL statement field modification sample.

[0102] In order to improve the efficiency of determining SQL statement field modification examples, in an embodiment of the present application, a large language model can be used to generate SQL statement field modification examples. In an embodiment of the present application, a first sample text and a second sample text can be obtained, wherein the second sample text can be a text that is asked on the basis of the first sample text. The first sample text and the second sample text can be pre-written or collected by relevant personnel, and the embodiment of the present application does not limit the method for obtaining the first sample and the second sample. Exemplarily, the first sample text can be: What is the highest temperature in Beijing today? The second sample text asks on the basis of the first sample text, and the second sample text can be: What about Shanghai?

[0103] In an embodiment of the present application, a first SQL statement of a first sample text and a second SQL statement of a second sample can be determined. When determining the second SQL statement of the second sample text, the second sample text can be supplemented based on the first sample text. Exemplarily, the second sample text can be supplemented using a large language model or manually. Still using the above example to illustrate, the supplemented second sample text can be: What is the highest temperature in Shanghai today?

[0104] The SQL syntax tree is an abstract representation of the grammatical structure and is widely used to represent the program statement structure. It expresses the grammatical structure of the source code vividly by stripping off some unimportant details and retaining only the grammatical information. The advantage of the tree structure is that the program can implement a top-down and bottom-up traversal process by depth-first traversal or breadth-first traversal, thereby extracting the required information. In an embodiment of the present application, after determining the first SQL statement and the second SQL statement, the first syntax tree of the first SQL statement and the second SQL syntax tree of the second SQL statement can be determined. In other words, the abstract syntax trees of the first SQL statement and the second SQL statement are determined respectively. It should be noted that how to determine the syntax tree of an SQL statement is a prior art, and the embodiments of the present application will not be repeated here.

[0105] Since the purpose of the SQL statement field modification example is to modify the first SQL statement into the second SQL statement. Therefore, in the embodiment of the present application, a modification scheme for modifying the first SQL syntax tree into the second SQL syntax tree can be determined. The modification content of each step is recorded in the modification scheme. Subsequently, the first sample text, the second sample text and the modification scheme can be determined as the SQL statement field modification example.

[0106] Specifically, suppose that

[0107] The first sample text is: the existing names and ID card information of motor vehicle drivers in Qingdao City;

[0108] The second sample text is: Which ones are in Shinan District.

[0109] The first SQL statement of the first sample text is:

[0110] SELECT name, idcard

[0111] FROM drivinglicense v

[0112] WHERE v. issuing_unit IN ('Lu B', 'Lu U')

[0113] AND not REGEXP_LIKE(v.license_status, 'C|E|F|G');

[0114] The second SQL statement of the second sample text is:

[0115] SELECT name, idcard

[0116] FROM drivinglicense v

[0117] WHERE v.issuing_unit IN ('Lu B', 'Lu U')

[0118] AND not REGEXP_LIKE(v.license_status, 'C|E|F|G')

[0119] AND v.district = 'Shinan District'.

[0120] In order to facilitate the subsequent determination of the modification plan, in the embodiments of the present application, the SQL syntax trees corresponding to the first sample text and the second sample text can be determined. It should be noted that how to determine the SQL syntax tree is the prior art, and the embodiments of the present application will not elaborate on this.

[0121] Since a multi-round dialogue task generally includes multiple rounds of dialogue, such as three or four rounds of dialogue, in order to further improve the accuracy of SQL statement generation, in the SQL statement field modification sample, you can also ask questions based on the second sample text. In the embodiment of the present application, this text is called a subsequent sample text. If the subsequent sample text is obtained, after the modification plan is determined, the second sample text can be updated to the first sample text, and the subsequent sample text can be determined as the second sample text, and then the steps of obtaining the first SQL statement of the first sample text and the second SQL statement of the second sample text are continued. Since the SQL syntax tree of the original second sample text (updated first sample text) has been determined before, it is only necessary to determine the SQL syntax tree of the subsequent sample text (updated second sample text) here.

[0122] For example, the subsequent sample text is: Display the region. This subsequent sample text is based on the second sample text. Then, the SQL statement of the subsequent sample text is:

[0123] SELECT name, id, district

[0124] FROM drivinglicense v

[0125] WHERE v.issuing_unit IN ('Lu B', 'Lu U')

[0126] AND not REGEXP_LIKE(v.license_status, 'C|E|F|G')

[0127] AND v.district= 'Shinan District'.

[0128] SQL syntax trees are generally composed of multiple subtrees. Table 1 lists some common subtree types:

[0129] Table 1 Subtree types and definitions

[0130]

[0131] In a multi-round interaction of SQL statement generation, if the SQL statement of the current round of dialogue can be modified from the SQL statement of the previous round of dialogue, the following modification steps may be involved:

[0132] 1. Clause elements increase:

[0133] The added elements of this clause include columns and conditions.

[0134] Column addition: The SQL statement in the second sample text is changed to the SQL statement in the third sample text, from [SELECT name, idcard] to [SELECT name, idcrad, district], and the district column, that is, the field name, is added.

[0135] Condition addition: Add the condition AND v.district= 'Shinan District' from the SQL statement of the first sample text to the SQL statement of the second sample text.

[0136] It should be noted that, for the addition of clause elements, those skilled in the art may also configure and add other contents. The addition is not limited to the above examples. The clause elements may be increased by one or more.

[0137] 2. Clause element deletion:

[0138] The deletion of the clause elements includes the deletion of columns and conditions.

[0139] Column deletion: For example, the text to be converted in the previous round of dialogue is: query the name, ID card information and district of the motor vehicle driver; the text to be converted in the current round of dialogue is: only display the name and ID card. The district column, that is, the field name, needs to be deleted. Then the corresponding SQL statement is from [SELECT name, idcard, district] to [SELECT name, idcard], and the deleted "district" means "district". Among them, name means name and idcard means ID card number.

[0140] Condition deletion: For example, the text to be converted in the previous round of dialogue is: Motor vehicle drivers in Shinan District, Qingdao; the text to be converted in the current round of dialogue is: Motor vehicle drivers in Qingdao. If the Shinan District condition needs to be deleted, then in the corresponding SQL statement, the condition AND v.district= 'Shinan District' needs to be deleted.

[0141] It should be noted that, for the deletion of clause elements, technicians in this field can also configure and add other content. It is not limited to the above examples. When deleting clause elements, one or more clause elements can be deleted.

[0142] 3. Addition and deletion of clause elements:

[0143] It is actually a combination of the above rules. The modifications include both additions and deletions, and the number of additions and deletions is no less than 1.

[0144] Add Column, Delete Column: For example, the text to be converted in the previous round of dialogue is: Query name and ID card information; the text to be converted in the current round of dialogue is: Display the districts of these people. You need to add the district column, i.e. the field name, to the SQL statement, and delete the ID card information column. Then the mapping in the SQL statement is from [select name, idcard] to [SELECT name, district], i.e. add district and delete idcard.

[0145] Specifically, in the multi-round dialogue interaction process, the SQL statements of each dialogue round (assuming there are n rounds) of each dialogue paragraph are processed to generate an SQL syntax tree: SQLTrees=[Tree1,Tree2,……,Tree (i-1) ,Tree i, ……,Tree n ], where SQLTrees represents the set of SQL syntax trees for a certain dialogue segment, Tree1 represents the SQL syntax tree of the SQL statement in the first dialogue round of the dialogue segment, and Tree n The SQL syntax tree representing the SQL statement of the nth dialogue round of the dialogue segment. According to the pre-saved SQL statement field modification rules, compare the Tree i-1 With Tree i For each subtree node, record the unchanged, added or deleted nodes in the modification plan in sequence using natural language description.

[0146] For example, if the first SQL syntax tree corresponding to the first sample text "Names and ID card information of existing motor vehicle drivers in Qingdao" in the above example is modified to the second SQL syntax tree corresponding to the second sample text "Which are in Shinan District", the determined modification scheme is:

[0147] {

[0148] UpdateList:[

[0149] {''update'':''Select clause does not need to be modified''},

[0150] {''update'':''From clause does not need to be modified''},

[0151] {''update'':''Where clause adds AND v.district= ''Shinan District''},

[0152] ...(list all child nodes that have changed) ]

[0154] }

[0155] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in an embodiment of the present application, after determining the modification scheme of modifying the first SQL syntax tree into the second SQL syntax tree, and before determining the first sample text, the second sample text and the modification scheme as the SQL statement field modification sample, the method further includes:

[0156] Get the cost values ​​corresponding to different types of SQL clauses saved in advance;

[0157] For each sub-scheme in the modification scheme, determine the clause to be modified corresponding to the sub-scheme in turn, and sum the second-generation value of the target SQL clause type of the clause to be modified and the second cost value of the target SQL clause type corresponding to other previous sub-schemes; add the sum value for the sub-scheme in the modification scheme, and if the sum value does not meet the preset threshold requirement, add a prompt text for directly generating an SQL statement in the modification scheme.

[0158] The modification scheme may also include relevant contents of the cost value. After the modification scheme is determined, before the first sample text, the second sample text and the modification scheme are determined as SQL statement field modification samples, the cost values ​​corresponding to different types of SQL clauses saved in advance may also be obtained. For each sub-scheme in the modification scheme, the clause to be modified corresponding to the sub-scheme is determined in turn, and the sum of the second cost value of the target SQL clause type of the clause to be modified and the second cost value of the target SQL clause type corresponding to other sub-schemes before it is calculated.

[0159] If the sum meets the preset threshold requirement, it means that the cost of modifying the SQL statement up to the current sub-scheme is not very high, and the SQL statement can be determined by modification, and the sum can be added to the sub-scheme in the modification scheme. The preset threshold requirement can be determined through a large number of experiments, and the embodiment of the present application does not limit this.

[0160] If the sum value does not meet the preset threshold requirement, it means that the cost of modifying the SQL statement up to the current sub-scheme is relatively high, and the cost of directly generating the SQL statement based on the text is relatively small. Therefore, in the embodiment of the present application, the sum value can be added to the modification scheme for the sub-scheme, and a prompt text for directly generating the SQL statement can be added. It should be noted that the added prompt text for directly generating the SQL statement can be recorded for the sub-scheme, or can be recorded at a preset position in the modification scheme, such as at the end of the modification scheme.

[0161] Exemplarily, the modification scheme may include the following:

[0162] {

[0163] UpdateList:[

[0164] {''update'':''Select clause does not need to be modified'',''cost'':0},

[0165] {''update'':''From clause does not need to be modified'',''cost'':0},

[0166] {''update'':''Where clause adds AND v.district=''Shinan District'',''cost'':1.2},

[0167] …

[0168] { ''update'':''Order by clause increases...,''cost'': σ+0.3}

[0169] ],

[0170] Update_Flag: The current SQL is not suitable for modification and needs to be directly generated by integrating historical text.

[0171] }

[0172] Among them, the content included in each "{}" of UpdateList can be regarded as a sub-scheme. The content after "'update" in each sub-scheme indicates the modified content, and the content after "cost" indicates the corresponding cost value up to the current sub-scheme, that is, the sum value described in the above embodiment. The σ in the above example identifies the preset threshold value. When the sum value exceeds the preset threshold value σ, it can be considered that the sum value does not meet the preset threshold requirement. Since the sum value corresponding to the sub-scheme "Order by clause increase..." is σ+0.3, which has exceeded the preset threshold, a prompt text for directly generating SQL statements can be added to the modification plan. "Update_Flag: The current SQL is not suitable for modification and needs to be directly generated by integrating historical text." is the prompt text for directly generating SQL statements added in the modification plan.

[0173] Since in the embodiment of the present application, the sum of each sub-scheme is determined based on the cumulative cost values ​​of the previous sub-schemes, when the sum corresponding to a sub-scheme does not meet the preset threshold requirement, the sum is no longer determined based on the second cost value, that is, the cost value judgment is stopped.

[0174] It should be noted that if the same type of SQL clause needs to be modified multiple times, the cost values ​​corresponding to the SQL clauses of this type will be accumulated. Assume that when modifying a certain type of SQL clause, two addition operations are required, and the cost value corresponding to the SQL clause of this type is 1. Then the final cost value is 1+1=2.

[0175] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, the process of determining the cost values ​​corresponding to different types of SQL clauses includes:

[0176] Generate a preset number of sample SQL statements of a third sample text using the large language model;

[0177] Counting the correctness rate of each type of SQL clause in the sample SQL statements;

[0178] Each type of SQL clause is sorted according to the correctness, and a cost value is assigned to each type of SQL clause, wherein a cost value corresponding to any SQL clause in each type of sorted SQL clauses is greater than a cost value corresponding to other subsequent SQL clauses.

[0179] In order to further improve the accuracy of SQL statement generation, when determining the cost value corresponding to each type of SQL clause, it can be determined based on a large number of pre-analyses. Through research, it is found that the large language model has different performances in generating different texts. For example, the accuracy of generating SELECT clauses is higher than that of generating GroupBy clauses. Therefore, in an embodiment of the present application, a large language model can be used to generate a preset number of sample SQL statements of a third sample text, and the accuracy of each type of SQL clause in the sample SQL statement can be counted. Specifically, after each sample SQL statement is obtained, manual participation can be used to determine whether each type of clause in the sample SQL statement is correct and record it.

[0180] After obtaining all the sample SQL clauses, the electronic device can calculate the correctness of each type of SQL clause in all sample SQL statements based on manual records. And sort each type of SQL clause according to the determined correctness. After obtaining each type of sorted SQL statement, a cost value can be assigned to each type of SQL clause. In the sorted sequence, the cost value corresponding to any SQL clause in the sequence is greater than the cost value corresponding to other SQL clauses after it. In other words, a high correctness corresponds to a small cost value, and a low correctness corresponds to a high cost value.

[0181] It should be noted that the cost value described in this application can be a cost value set for the action of "modify" or a cost value set for the action of "generate". For example, if a certain clause is modified to obtain a simpler SQL statement, then the cost value set for the action of "modify" can be 0.1. If the cost value set for the action of "generate" for this clause can be 0.9. In other words, it is simpler to obtain the clause by modifying it when generating the SQL clause, and it is relatively complicated to obtain it by directly generating it. In other words, those skilled in the art can configure the cost value as needed.

[0182] Specifically, Table 2 is a SQL statement field modification rule provided in an embodiment of the present application:

[0183] Table 2 SQL statement field modification rules

[0184]

[0185] According to Table 2 above, the cost values ​​for the Select and From clauses are both 0.8, which means that the accuracy of the large language model in generating this type of clause is similar, while the cost value of the Group By clause is 1.2, which means that the accuracy of the large language model in generating this type of clause is lower than that of the Select and From clauses.

[0186] In a possible implementation, a generation cost value may be set in advance for each type of SQL clause, and the cost value corresponding to each type of SQL clause may be determined based on the formula φ=modification / generation. In the formula, "generation" means a generation cost value, and "modification" means a modification cost value, and the generation cost value and the modification cost value may be configured in advance based on statistics. The size of φ=modification / generation is determined by the generation cost of different subtrees. The larger the generation cost value, the smaller φ, and the smaller the generation cost value, the larger φ.

[0187] It should be noted that those skilled in the art can configure the cost values ​​of different types of SQL clauses as needed. For example, the cost value corresponding to the SELECT clause can be set to 1, the cost value corresponding to the WHERE clause can be set to 0.8, and the cost value corresponding to the Group By clause can be set to 0.6. It is sufficient to ensure that the design idea of ​​the cost value matches the preset threshold requirements of the configuration.

[0188] The following is a description of the SQL statement generation process based on a large language model in conjunction with a specific embodiment. In this application, the generation of SQL statements relies on the large language model, and the key to interacting with the large language model lies in the design of prompt words. The embodiment of this application combines the thought chain, the description of the SQL statement field modification rules, and the generated Few-shot data to construct the input text into the large language model. This enables the large model to handle the generation of SQL statements that are not the first round of dialogue to be converted or have a high modification cost, and to support the target SQL statement that rewrites the text to be converted for the current round of dialogue based on historical SQL statements. The specific input data design is as follows: Figure 2 As shown, Figure 2 A schematic diagram of inputting text provided in an embodiment of the present application, such as Figure 2 As shown, the input text input into the large language model may include: role and task settings, data table structure information, business knowledge limitations, SQL statement field modification rules, cost value judgment, output limitations, few-shot examples, and task input information.

[0189] Role and task settings are used to indicate the tasks that the large language model needs to perform. In other words, the large language model is given a role to describe the tasks to be completed. For example:

[0190] "You are a database expert. Based on the database table information, you need to convert the problem into an executable SQL statement. The database table information is as follows:".

[0191] The data table structure information is used to inform the structure information of the data tables included in the database involved in the large language model. In other words, it describes the information of the query database table, including the meaning of field attributes and sample data. For example:

[0192] “# The data table drivinglicense driver table contains the following attribute fields:

[0193] driver_id: driver serial number

[0194] name: driver's name

[0195] idcard: driver’s ID number

[0196] issuing_unit: Issuing authority

[0197] license_status: driving license status, enumeration value: A: normal, B: full 12 points, C: transfer out, D: temporarily withheld, E: revoked, F: suspended, G: cancelled

[0198] district: administrative division

[0199] # Data table data example

[0200] driver_id name idcard issuing_unit license_status district

[0201] 1 Zhang San 37021XX Lu BA Shinan District

[0202] 2 Li Si 37022XX Lu UB Shibei District

[0203] ”.

[0204] Business knowledge limitation is used to inform the big language model of some special requirements. In other words, it describes the business restrictions that need to be reflected in the SQL statement and informs the big model of the business information that it does not know. For example:

[0205] “#Business knowledge limited

[0206] 1. To calculate the number of drivers, the driver status field must be restricted and those that do not meet the following rules must be removed: REGEXP_LIKE(license_status, 'C|E|F|G')

[0207] 2.…”.

[0208] The SQL statement field modification rules are used to prompt the large language model on what rules to modify when modifying historical SQL statements. In other words, it tells the large model how to modify and obtain the current SQL statement based on the existing SQL statement, and records the cost values ​​of different types of SQL clauses. For example:

[0209] "#[SQL statement field modification rules] The rules are as follows:

[0210] 1. Modify the Select clause: determine the field list ColumnList for the new question query, traverse ColumnList, for each element CurrentColumn, if it does not exist in the historical question SELECT clause, add CurrentColumn to the SELECT clause, if OldColumn exists in the SELECT clause OldColumnList but not in ColumnList, delete oldColumn from the SELECT clause. The cost of modifying each Column is: 1

[0211] 2. Modify the From clause condition: Determine the new question table CurrentTable to be queried. If it is inconsistent with the table OldTable in the From clause of the historical question, replace OldTable with CurrentTable. The cost of modifying each Table is: 1

[0212] 3.…”.

[0213] Cost value judgment is used to prompt the large language model, under what circumstances to directly generate SQL statements, under what circumstances to modify historical SQL statements. In other words, in the process of modifying historical SQL statements in sequence according to the rules, it is determined whether to continue to modify or directly generate according to the judgment rules. For example:

[0214] “# Modify or generate judgment:

[0215] For user query input, the modification is determined one by one according to the [SQL statement field modification rules], and the total cost of the modified node is calculated after each rule is modified. Determine whether to modify the SQL or directly generate SQL according to the following rules:

[0216] 1. First, consider whether the current query is in the same conversation topic or conversation paragraph as the historical information. If the topic or conversation paragraph is switched, the SQL statement is directly generated;

[0217] 2. When the total cost M is greater than the preset threshold σ, that is, M>σ, stop modifying the task and directly generate SQL statements by integrating the historical text.

[0218] Output restrictions are used to remind the large language model of matters that should be paid attention to when outputting related data. The output results can be restricted, including output format, content information, etc. For example:

[0219] “#Please note:

[0220] 1. The output result must be an SQL statement without any unnecessary explanation;

[0221] 2. The fields in the generated SQL statement must exist in the data table;

[0222] 3.…”.

[0223] Few-shot examples are examples for analysis by the large language model, i.e., the SQL statement field modification examples described in the above embodiments. In other words, examples generated by multiple rounds of interactive SQL modification are described through the thought chain prompt method to guide the large model to fully understand the tasks and constraints. For example:

[0224] "#Example:

[0225] Question 1: Names and ID card information of existing motor vehicle drivers in Qingdao

[0226] Let's think about it step by step.

[0227] Question 1 can be generated directly instead of being modified from the previous SQL.

[0228] Therefore, SQL1:SELECT name, idcard FROM drivinglicense v WHERE v. issuing_unit IN ('LuB', 'LuU') AND not REGEXP_LIKE(v.license_status, 'C|E|F|G')

[0229] Question 2: Which ones are in Shinan District?

[0230] Let's think about it step by step.

[0231] Question 2 can be obtained by modifying question 1. The previous question is to obtain the name and ID card information list of existing motor vehicle drivers in Qingdao. The current question needs to filter out the area of ​​"Shinan District" from the previous round, so the modification operation needs to be used as follows:

[0232] * The Select clause does not need to be modified

[0233] *Add a condition to the Where clause: AND v.district= 'Shinan District' cost=0.9

[0234] * The From clause does not need to be modified

[0235] * The OrderBy clause does not need to be modified

[0236] ...(list all clause modifications involved in the rules)

[0237] So SQL2:

[0238] SELECT name, idcard FROM drivinglicense v WHERE v.issuing_unit IN ('LuB', 'LuU')

[0239] AND v.license_status NOT IN ('C', 'E', 'F', 'G') AND v.district= 'Shinan District'".

[0240] The task input information is the text composed of the current query statement of the user and the historical conversation records. The current query statement of the user is the text to be converted. The data of the historical text to be converted is determined according to the specific business scenario. For example, if the number of interaction rounds does not exceed 4, the historical text to be converted and the historical SQL statements of the previous 4 rounds of the current conversation round can be obtained. For example:

[0241] “# Historical dialogue query and sql: {historical dialogue (question, SQL)}

[0242] The user's current query is: {question}".

[0243] The historical dialogue (question, SQL) is the historical text to be converted - the historical SQL statement, where question represents the historical text to be converted and SQL represents the historical SQL statement. The question in the user's current query: {question} represents the text to be converted.

[0244] The following is a description of the process of determining the prompt word in the embodiment of the present application in conjunction with another specific embodiment. Figure 3 A schematic diagram of a text input construction process provided in an embodiment of the present application is shown in FIG. Figure 3 As shown, the input text includes role and task settings, data table structure information, business knowledge restrictions, SQL statement field modification rules, cost value judgment, output restrictions, few-shot examples, and task input information. Among them, the few-shot example is the SQL statement field modification sample, and the determination process of the SQL statement field modification sample includes the following steps:

[0245] A multi-round dialogue dataset is obtained, in which a set number of historical dialogue segments are stored.

[0246] Determine the SQL syntax tree of the SQL statement recorded in the multi-round dialogue data set, and compare the SQL syntax trees according to the order of the historical dialogue paragraphs to determine the modification plan, which records the cost value corresponding to each sub-plan. If the total cost value corresponding to any sub-plan in the modification plan is greater than the threshold σ, it is determined to directly generate SQL statements based on the text, and a prompt text for directly generating SQL statements is added to the modification plan. If the total cost values ​​corresponding to the sub-plans in the modification plan are not greater than the threshold σ, it can be determined that the historical SQL statement can be modified, and each modification step is recorded in the modification plan.

[0247] After obtaining the modification plan, the text and SQL statement designed for the corresponding historical dialogue paragraph and the modification plan are spliced ​​to obtain a natural language description, thereby obtaining a small sample (Few-shot) example, which is the SQL statement field modification sample.

[0248] The text to be converted, historical conversation records and input text are input as task information and input into the large language model to obtain the target SQL statement output by the large language model, which is the SQL statement of the current round. The input text includes role and task settings, data table structure information, business knowledge restrictions, SQL statement field modification rules, cost value judgment, output restrictions, and few-shot examples.

[0249] Based on the same inventive concept, the embodiment of the present application also provides a SQL statement generation device based on a large language model. Figure 4 A schematic diagram of the structure of a SQL statement generation device based on a large language model provided in an embodiment of the present application, the device comprising:

[0250] The acquisition module 401 is used to acquire the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds if the received text to be converted is the text of the current dialogue paragraph other than the first dialogue round;

[0251] A splicing module 402 is used to splice the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement to obtain a first input text;

[0252] A generation module 403 is used to input the first input text into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and modifies the target fields of the distinguishing fields in the historical SQL statements according to the SQL statement field modification rules and the data table structure information to obtain the target SQL statement of the text to be converted.

[0253] In a possible implementation, the concatenation module 402 is further configured to concatenate the text to be converted and the data table structure information to obtain a second input text if the received text to be converted is the text of the first dialogue round of the current dialogue paragraph;

[0254] The generating module 403 is further configured to input the second input text into the large language model, so that the large language model generates a target SQL statement for the text to be converted according to the text to be converted and the data table structure information.

[0255] In a possible implementation, the SQL statement field modification rule includes cost values ​​corresponding to different types of SQL clauses;

[0256] The generation module 403 is specifically used for the large language model to determine the target SQL clause to which the distinguishing field belongs in the SQL statement if the text to be converted is converted into an SQL statement, and each clause in the SQL statement is determined after the SQL statement is segmented according to preset keywords; if the target cost value corresponding to the target type of the target SQL clause meets the preset threshold requirement, then continue to execute the subsequent step of modifying the target field of the distinguishing field in the historical SQL statement according to the SQL statement field modification rule and the data table structure information.

[0257] In a possible implementation, the generation module 403 is also used to generate a target SQL statement for the text to be converted based on the text to be converted and the data table structure information if the target cost value corresponding to the target type of the target SQL clause does not meet the preset threshold requirement.

[0258] In a possible implementation, if the distinguishing fields include multiple ones, the generating module 403 is specifically used to respectively determine each target SQL clause to which each distinguishing field belongs in the SQL statement if the text to be converted is converted into an SQL statement; determine the first generation value corresponding to the target type of each target SQL clause according to the cost values ​​corresponding to the different types of SQL clauses included in the SQL statement field modification rule; and determine the target cost value according to each of the first generation values.

[0259] In a possible implementation, the acquisition module 401 is further used to acquire a pre-saved SQL statement field modification sample;

[0260] The splicing module 402 is further used to splice the SQL statement field modification sample, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement to obtain a first input text; and enable the large language model to determine the distinguishing fields based on the text to be converted, the historical text to be converted and the SQL statement field modification sample.

[0261] In a possible implementation, the acquisition module 401 is further used to acquire a first SQL statement of a first sample text and a second SQL statement of a second sample text, wherein the second sample text is a text for further questioning based on the first sample text;

[0262] The determination module 404 is used to determine a first SQL syntax tree for the first SQL statement and a second SQL syntax tree for the second SQL statement; determine a modification plan for modifying the first SQL syntax tree into the second SQL syntax tree; and determine the first sample text, the second sample text and the modification plan as the SQL statement field modification sample.

[0263] In a possible implementation, the acquisition module 401 is further used to acquire cost values ​​corresponding to different types of SQL clauses that are stored in advance;

[0264] The determination module 404 is further used to determine, for each sub-scheme in the modification scheme in turn, the clause to be modified corresponding to the sub-scheme, and sum the second-generation value of the target SQL clause type of the clause to be modified with the second cost value of the target SQL clause type corresponding to other previous sub-schemes; add the sum value for the sub-scheme in the modification scheme, and if the sum value does not meet the preset threshold requirement, add a prompt text for directly generating an SQL statement in the modification scheme.

[0265] In a possible implementation, the determination module 404 is further used to generate a preset number of sample SQL statements of a third sample text using the large language model; count the accuracy of each type of SQL clause in the sample SQL statements; sort each type of SQL clause according to the accuracy, and assign a cost value to each type of SQL clause, wherein the cost value corresponding to any SQL clause in each type of SQL clause after sorting is greater than the cost values ​​corresponding to other subsequent SQL clauses.

[0266] Based on the same inventive concept, an embodiment of the present application further provides an electronic device, Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 5 As shown, it includes: a processor 501, a communication interface 502, a memory 503 and a communication bus 504, wherein the processor 501, the communication interface 502, and the memory 503 communicate with each other through the communication bus 504;

[0267] The memory 503 stores a computer program. When the program is executed by the processor 501, the processor 501 executes the steps of the method for generating SQL statements based on a large language model described in any of the above embodiments.

[0268] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The communication interface 502 is used for communication between the above electronic device and other devices. The memory may include a random access memory (RAM) and may also include a non-volatile memory (NVM), such as at least one disk storage. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.

[0269] The above-mentioned processor can be a general-purpose processor, including a central processing unit, a network processor (NP), etc.; it can also be a digital signal processing processor (DSP), an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc.

[0270] On the basis of the above embodiments, an embodiment of the present invention further provides a computer-readable storage medium, which stores a computer program executable by a processor. When the program runs on the processor, the processor implements the steps of the method for generating SQL statements based on a large language model as described in the above embodiments.

[0271] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the equivalent technology of the present application, the present application is also intended to include these modifications and variations.

Claims

1. A method for generating SQL statements based on a large language model, characterized in that: The method comprises: If the received text to be converted is the text of a non-first dialogue round of the current dialogue segment, then the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds are obtained; The text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement are concatenated to obtain a first input text, wherein the SQL statement field modification rule includes cost values ​​corresponding to different types of SQL clauses; The first input text is input into the large language model, so that the large language model determines the distinguishing fields according to the text to be converted and the historical text to be converted, and determines the target SQL clause to which the distinguishing fields belong in the SQL statement if the text to be converted is converted into an SQL statement, each clause in the SQL statement being determined after the SQL statement is segmented according to preset keywords; if a target cost value corresponding to a target type of the target SQL clause meets a preset threshold requirement, the target field of the distinguishing fields in the historical SQL statement is modified according to the SQL statement field modification rule and the data table structure information to obtain the target SQL statement of the text to be converted.

2. The method according to claim 1, characterized in that The method further comprises: If the received text to be converted is the text of the first dialogue round of the current dialogue paragraph, the text to be converted and the data table structure information are concatenated to obtain a second input text; The second input text is input into the large language model, so that the large language model generates a target SQL statement for the text to be converted according to the text to be converted and the data table structure information.

3. The method according to claim 1, characterized in that The method further comprises: If the target cost value corresponding to the target type of the target SQL clause does not meet the preset threshold requirement, the large language model generates a target SQL statement for the text to be converted based on the text to be converted and the data table structure information.

4. The method according to claim 1, characterized in that: If the difference field includes multiple fields, the determining of the target SQL clause to which the difference field belongs in the SQL statement when the text to be converted is converted into an SQL statement includes: Respectively determine each target SQL clause to which each distinguishing field belongs in the SQL statement if the text to be converted is converted into an SQL statement; The process of determining the target cost value corresponding to the target type of the target SQL clause includes: Determine the first cost value corresponding to the target type of each target SQL clause according to the cost values ​​corresponding to the different types of SQL clauses included in the SQL statement field modification rule; The target cost value is determined based on each of the first cost values.

5. The method according to claim 1, characterized in that After obtaining the historical text to be converted and the corresponding historical SQL statements of the historical dialogue rounds, and before splicing the text to be converted, the data table structure information, the SQL statement field modification rules, the historical text to be converted and the historical SQL statements, the method further includes: Get pre-saved SQL statement field modification samples; The step of concatenating the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted, and the historical SQL statement to obtain a first input text includes: The SQL statement field modification sample, the text to be converted, the data table structure information, the SQL statement field modification rule, the historical text to be converted and the historical SQL statement are concatenated to obtain a first input text; and the large language model is enabled to determine the distinguishing fields according to the text to be converted, the historical text to be converted and the SQL statement field modification sample.

6. The method according to claim 5, characterized in that The process of determining the SQL statement field modification example includes: Obtaining a first SQL statement of a first sample text and a second SQL statement of a second sample text, wherein the second sample text is a text for conducting a follow-up inquiry based on the first sample text; Determine a first SQL syntax tree for the first SQL statement, and a second SQL syntax tree for the second SQL statement; Determine a modification scheme for modifying the first SQL syntax tree into the second SQL syntax tree; The first sample text, the second sample text and the modification scheme are determined as the SQL statement field modification sample.

7. The method according to claim 6, characterized in that After determining the modification scheme for modifying the first SQL syntax tree into the second SQL syntax tree, and before determining the first sample text, the second sample text, and the modification scheme as the SQL statement field modification sample, the method further includes: Get the cost values ​​corresponding to different types of SQL clauses saved in advance; For each sub-scheme in the modification scheme, determine the clause to be modified corresponding to the sub-scheme in turn, and sum the second-generation value of the target SQL clause type of the clause to be modified and the second cost value of the target SQL clause type corresponding to other previous sub-schemes; add the sum value for the sub-scheme in the modification scheme, and if the sum value does not meet the preset threshold requirement, add a prompt text for directly generating an SQL statement in the modification scheme.

8. The method according to claim 1 or 7, characterized in that: The process of determining the cost values ​​corresponding to different types of SQL clauses includes: Generate a preset number of sample SQL statements of a third sample text using the large language model; Counting the correctness rate of each type of SQL clause in the sample SQL statements; Each type of SQL clause is sorted according to the correctness, and a cost value is assigned to each type of SQL clause, wherein a cost value corresponding to any SQL clause in each type of sorted SQL clauses is greater than a cost value corresponding to other subsequent SQL clauses.

9. An electronic device, characterized in that: The electronic device includes a processor, and the processor is used to implement the steps of the SQL statement generation method based on a large language model as described in any one of claims 1 to 8 when executing a computer program stored in a memory.