Method for increasing the accuracy of complex SQL generated by natural language using SQL examples
Through the user-added SQL examples and the Function call function of the large language model and the PreSQL example enhancement strategy, the problem that large language models are difficult to generate complex SQL is solved, and higher accuracy and efficiency are achieved.
Patent Information
- Application Number
- CN202411905167.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-23
- Publication Date
- 2025-03-11
- Estimated Expiration
- 2044-12-23
AI Technical Summary
In the prior art, large language models are difficult to accurately generate complex SQL, especially when operations such as outreach, subquery and window functions are involved.
Through the SQL examples added by the user, combining a hybrid method of vector search and full-text search, relevant SQL examples are retrieved from the database, and the Function call function of the large language model and the PreSQL example enhancement strategy are used to generate more accurate and complex SQL.
It improves the accuracy and efficiency of large language models to generate complex SQL, reduces user waiting time, and enhances the controllability of the system.
Smart Images

Figure CN119357217B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of converting natural language into structured query language of database, and in particular to a method for increasing the accuracy of complex SQL generated by natural language by using SQL examples. Background Art
[0002] The task of natural language to structured query language (NL2SQL) is to convert natural language into the database's structured query language (SQL). This technology enables users to interact with the database through natural language without knowing SQL. Due to the diversity of user question expressions, the complexity of database table structures and SQL, generating accurate SQL from natural language questions is a challenging task.
[0003] Traditional NL2SQL systems are usually based on rules and deep learning network methods. Although they have achieved good performance, rule-based methods rely on manual labor and are therefore expensive. Traditional neural network-based methods require continuous fine-tuning of models based on business areas, which is very inflexible as databases become increasingly complex.
[0004] In recent years, with the popularity of Large Language Models (LLM), NL2SQL based on LLM has become a widely-watched research direction. With its powerful language understanding ability, LLM can achieve good results with only prompt words without fine-tuning. Although NL2SQL based on large language models has made a breakthrough compared to previous methods, it is still difficult to accurately generate complex SQL.
[0005] Complex SQL may require operations such as outer joins, subqueries, and window functions. Usually, such SQL is longer and involves more tables. In the current large language model training phase, the number of SQLs containing these operations is small in the data set used. In the existing technology, large language models are not good at generating complex SQL.
[0006] Therefore, in view of the shortcomings of the prior art, it is necessary to provide a method for increasing the accuracy of complex SQL generated by natural language using SQL examples based on a large language model to overcome the shortcomings of the prior art. Summary of the invention
[0007] The purpose of the present invention is to avoid the shortcomings of the prior art and provide a method for increasing the accuracy of complex SQL generated by natural language using SQL examples. With the help of SQL examples added by users, the accuracy of SQL generated by LLM is enhanced, while the waiting time of users is reduced, and excellent performance is achieved when generating complex SQL.
[0008] The purpose of the present invention is achieved through the following technical measures.
[0009] A method is provided to increase the accuracy of complex SQL generated by natural language by using SQL examples. For the user's request Query, the following steps are followed:
[0010] S100. Rewrite the user's request;
[0011] S200. Retrieve the database schema related to the requested Query, and retrieve the relevant table schema from the database;
[0012] S300. Retrieve SQL examples related to the requested Query;
[0013] Retrieve relevant SQL examples from the database through a combination of vector search and full-text search;
[0014] S400. Request Query complexity evaluation based on SQL example, that is, the difficulty of requesting Query to generate SQL is evaluated. If the evaluation result is that the request Query only requires simple SQL to complete the query, then enter S500; otherwise, enter the process S900;
[0015] S500. Prompt word construction, that is, filling the query, table mode, and SQL example into the prompt template;
[0016] S600. PreSQL generation, that is, inputting the constructed prompt words into the large language model, and the large language model processes them to obtain SQL;
[0017] S700. Execute SQL. If no error is reported during the execution, execute step S1400. If an error is reported during the execution, execute step S800.
[0018] S800. Check whether the SQL example contains a SQL template, if so, proceed to step S900, otherwise proceed to step S1100;
[0019] S900. Enhance the example based on the Function call, that is, use the SQL example matched in step S300 to create the request Json of the Function call, and use the large language model to execute it, and build the result into SQL;
[0020] S1000. The SQL generated by the example enhancement, the execution result of the SQL, and the Query are constructed as prompt words and then input into the large language model to determine whether the execution result of the SQL can answer the Query. If the large language model determines that it can, step S1400 is executed, otherwise step S1200 is executed;
[0021] S1100. Prompt word construction based on PreSQL, that is, through a combination of vector search and full-text search, SQL examples and table patterns related to PreSQL are retrieved from the database, and the retrieved table patterns are filtered through the table used in PreSQL, and the filtered table patterns remove unused columns in PreSQL. Finally, the database pattern retrieved in step S200, the SQL examples retrieved in step S300, the SQL examples retrieved in this step, and the pattern description are combined in pairs to construct 4 groups of prompt word templates;
[0022] S1200. Based on the example enhancement of PreSQL, that is, generating SQL statements according to the prompt words input in step S1100;
[0023] S1300. First filter out the results of execution errors, then vote on the execution results of the correctly executed SQL, and return the result with the most votes as the final result; when there is a tie, the LLM selects the most appropriate result;
[0024] S1400. Return the result to the user.
[0025] Preferably, the above method of using SQL examples to increase the accuracy of complex SQL generated by natural language, S100. Rewriting the user's request specifically replaces the relative time and personal pronoun description in the request Query with a standard time description and a standard name.
[0026] Preferably, in the above method of using SQL examples to increase the accuracy of complex SQL generated by natural language, S200 retrieves relevant table patterns from the database by combining vector retrieval and full-text retrieval.
[0027] Preferably, the above method for increasing the accuracy of complex SQL generated by natural language using SQL examples pre-configures the database schema and SQL examples, and the SQL examples are manually added by the user in advance.
[0028] Preferably, in the above method of using SQL examples to increase the accuracy of complex SQL generated by natural language, in S300 , relevant SQL examples are retrieved from the database by combining vector retrieval and full-text retrieval.
[0029] Preferably, in the above method of using SQL examples to increase the accuracy of complex SQL generated by natural language, the standard of simple SQL in S400 is: no join operation is used in the SQL or no subquery is used in the SQL.
[0030] The method of the present invention for increasing the accuracy of complex SQL generated by natural language using SQL examples enhances the accuracy of SQL generated by LLM with the help of SQL examples added by users, while reducing the waiting time of users, and has excellent effects when generating complex SQL. The method of the present invention is provided with steps of user question rewriting, example enhancement based on Function call, and example enhancement based on PreSQL. The purpose of example enhancement based on Function call is to enhance the influence of examples manually added by users on the SQL generated by LLM, which can greatly improve the controllability of the generated results and greatly reduce the time required to generate SQL. Example enhancement based on PreSQL is a method for simplifying the table model through the table names and column names contained in the SQL examples retrieved by PreSQL. In summary, the method of the present invention can generate complex SQL. BRIEF DESCRIPTION OF THE DRAWINGS
[0031] The present invention is further described with reference to the accompanying drawings, but the contents in the accompanying drawings do not constitute any limitation to the present invention.
[0032] Figure 1 It is a schematic diagram of a method of the present invention for increasing the accuracy of complex SQL generated by natural language using SQL examples.
[0033] Figure 2 This is a partial screenshot of the SQL example preset in Example 2 of the present invention. DETAILED DESCRIPTION
[0034] The present invention will be further described with reference to the following examples.
[0035] Example 1
[0036] A method for increasing the accuracy of complex SQL generated by natural language using SQL examples. Before using the method of the present invention, a database schema and SQL examples are pre-configured. The database schema is a statement for creating a table in a database. The SQL examples can be added manually by a user.
[0037] like Figure 1 As shown, for the user's request Query, it is processed according to the following steps:
[0038] S100. Rewrite the user's request.
[0039] S100. Rewriting the user's request specifically involves replacing the relative time and personal pronoun description in the request Query with a standard time description and a standard name.
[0040] The purpose of user question rewriting is to convert the personal pronouns contained in the Query into specific user names, and at the same time convert relative time descriptions such as "today", "yesterday", and "last month" into absolute time descriptions such as "2024-08-20", "2024-08-19", and "2024-07".
[0041] S200. Retrieve the database schema related to the requested Query and retrieve the related table schema from the database. S200 can retrieve the related table schema from the database by combining vector retrieval and full-text retrieval.
[0042] S300. Retrieve SQL examples related to the requested query. Relevant SQL examples can be retrieved from the database through a mixture of vector retrieval and full-text retrieval. SQL examples can be added manually by the user. The purpose of example enhancement based on Function call is to enhance the impact of examples manually added by the user on the SQL generated by LLM, which can greatly improve the controllability of the generated results and greatly reduce the time required to generate SQL. Example enhancement based on PreSQL is a method of simplifying the table schema through the table names and column names contained in the SQL examples retrieved by PreSQL.
[0043] S400. Request Query complexity evaluation based on SQL examples, that is, difficulty evaluation of SQL to be generated for request Query. If the evaluation result is that the request Query only needs simple SQL to complete the query, then enter S500; otherwise enter process S900.
[0044] The criteria for judging simple SQL in S400 is: SQL that does not use join operations or does not use subqueries is simple SQL.
[0045] The query complexity evaluation strategy based on SQL examples in the method of the present invention aims to identify whether the current problem belongs to complex SQL. For complex SQL, the present invention proposes two processing solutions. One is Function call based on a large language model, which converts the SQL examples added by the user into functions and identifies the function parameters from the user's query. This solution allows LLM to stably generate SQL according to examples. The other is a two-stage SQL generation strategy, which reconstructs the table model based on the retrieved SQL examples, and enhances LLM's understanding of the database model in context learning.
[0046] S500. Prompt word construction, that is, filling Query, table mode, and SQL example into the prompt template.
[0047] S600. PreSQL generation, that is, inputting the constructed prompt words into the large language model, and the large language model processes them to obtain SQL.
[0048] S700. Execute SQL. If no error is reported during the execution, execute step S1400. If an error is reported during the execution, execute step S800.
[0049] S800. Check whether the SQL example contains an SQL template. If so, proceed to step S900; otherwise, proceed to step S1100.
[0050] S900. Enhance the Function call based on the example, that is, use the SQL example matched in step S300 to create the Function call request Json, and use the large language model to execute it, and build the result into SQL.
[0051] S1000. The SQL generated by example enhancement, the execution result of the SQL, and the Query are constructed as prompt words and input into the large language model to determine whether the execution result of the SQL can answer the Query. If the large language model determines that it can, execute step S1400, otherwise execute step S1200.
[0052] S1100. Prompt word construction based on PreSQL, that is, through a mixture of vector retrieval and full-text retrieval, SQL examples and table patterns related to PreSQL are retrieved from the database, and the queried table patterns are filtered through the table used in PreSQL. The filtered table patterns remove unused columns in PreSQL, and finally the database pattern retrieved in step S200, the SQL examples retrieved in step S300, the SQL examples retrieved in this step, and the pattern descriptions are combined in pairs to construct 4 groups of prompt word templates.
[0053] When generating SQL based on LLM, database schema and SQL examples are necessary. In S200 and S300, a set of database schema and SQL examples are obtained, referred to as database schema 1 and SQL example 1. In S1100, database schema 2 and SQL example 2 are obtained using PreSQL as the search condition. At this point, there are two sets of data that can be used to construct prompt words.
[0054] In order to improve the richness of prompts, the above two groups of arrays are combined to obtain: database mode 1 + SQL example 2 and database mode 2 + SQL example 1. That is, the four groups of prompt word templates are: database mode 1 + SQL example 1, database mode 2 + SQL example 2, database mode 1 + SQL example 2 and database mode 2 + SQL example 1.
[0055] S1200. Example enhancement based on PreSQL, that is, generating four groups of SQL statements according to the four groups of prompt words input in step S1100.
[0056] S1300. First filter out the results with execution errors, then vote on the execution results of the correctly executed SQL, and return the result with the most votes as the final result; when there is a tie, LLM selects the most appropriate result.
[0057] S1400. Return the result to the user.
[0058] The method of the present invention for increasing the accuracy of complex SQL generated by natural language using SQL examples enhances the accuracy of SQL generated by LLM with the help of SQL examples added by users, while reducing the waiting time of users, and has excellent effects when generating complex SQL. The method of the present invention sets the steps of user question rewriting, example enhancement based on Function call, and example enhancement based on PreSQL. The purpose of example enhancement based on Function call is to enhance the influence of examples manually added by users on the SQL generated by LLM, which can greatly improve the controllability of the generated results and greatly reduce the time required to generate SQL. Example enhancement based on PreSQL is a method for simplifying the table model through the table names and column names contained in the SQL examples retrieved by PreSQL. In summary, the method of the present invention can generate complex SQL.
[0059] Example 2
[0060] The method of using SQL examples to increase the accuracy of complex SQL generated by natural language is described with a specific application example.
[0061] Taking the SQL Service database storing attendance punch-in data as an example, the NL2SQL function of the method of the present invention is introduced. Before using the NL2SQL function, it is necessary to configure the database mode and SQL example. The database mode is the creation statement of the table in the database. The SQL example is manually added by the user, and the page is as follows Figure 2 shown.
[0062] like Figure 1 As shown, the system responds to the user's Query request in the following processing steps.
[0063] S100. Rewrite the user's request. S100. Rewrite the user's request specifically by replacing the relative time and personal pronoun description in the request Query with a standard time description and a standard person name.
[0064] The question rewriting step aims to solve the problem that the large language model cannot accurately describe the time and administrative district name. For example, the question "How many times did You Moushun take leave last month" is converted to "How many times did You Moushun take leave in 2024-08". This module consists of two parts. One is the named entity recognition based on BERT, which is used to extract time information and region information, as shown in Table 1.
[0065] Table 1
[0066]
[0067] The schema field is used to specify the entity type to be extracted. In info_list, offset indicates the position of the extracted content in text, and type indicates the type of the extracted content. Another module is a rule-based format conversion module, which can extract non-standard time descriptions from questions and express them in a unified format. For example, "August 23rd" and "August 23" will be converted into a unified time description "2024-08-23".
[0068] S200. Retrieve the database schema related to the requested Query, and retrieve the related table schema from the database by combining vector search and full-text search.
[0069] S300. Retrieve SQL examples related to the requested Query. Relevant SQL examples may be retrieved from the database by combining vector search and full-text search.
[0070] S400. Request Query complexity evaluation based on SQL examples, that is, difficulty evaluation of SQL to be generated for request Query. If the evaluation result is that the request Query only needs simple SQL to complete the query, then enter S500; otherwise enter the process S900. The judgment criteria for simple SQL in S400 are: no join operation is used in SQL or no subquery is used in SQL.
[0071] S500. Prompt word construction, that is, filling Query, table mode, and SQL example into the prompt template.
[0072] S600. PreSQL generation, that is, inputting the constructed prompt words into the large language model, and the large language model processes them to obtain SQL.
[0073] S700. Execute SQL. If no error is reported during the execution, execute step S1400. If an error is reported during the execution, execute step S800.
[0074] S800. Check whether the SQL example contains an SQL template. If so, proceed to step S900; otherwise, proceed to step S1100.
[0075] S900. Enhance the Function call based on the example, that is, use the SQL example matched in step S300 to create the Function call request Json, and use the large language model to execute it, and build the result into SQL.
[0076] S1000. The SQL generated by example enhancement, the execution result of the SQL, and the Query are constructed as prompt words and input into the large language model to determine whether the execution result of the SQL can answer the Query. If the large language model determines that it can, execute step S1400, otherwise execute step S1200.
[0077] S1100. Prompt word construction based on PreSQL, that is, through a mixture of vector retrieval and full-text retrieval, SQL examples and table patterns related to PreSQL are retrieved from the database, and the queried table patterns are filtered through the table used in PreSQL. The filtered table patterns remove unused columns in PreSQL, and finally the database pattern retrieved in step S200, the SQL examples retrieved in step S300, the SQL examples retrieved in this step, and the pattern descriptions are combined in pairs to construct 4 groups of prompt word templates.
[0078] S1200. Example enhancement based on PreSQL, that is, generating four groups of SQL statements according to the four groups of prompt words input in step S1100.
[0079] S1300. First filter out the results with execution errors, then vote on the execution results of the correctly executed SQL, and return the result with the most votes as the final result; when there is a tie, LLM selects the most appropriate result.
[0080] S1400. Return the result to the user.
[0081] The NL2SQL system is highly vulnerable. Even if the prompt word is the same and LLM is entered multiple times, the content generated by LLM is not completely fixed. The database is highly sensitive to SQL syntax, and any deviation in the characters in SQL may cause the SQL to fail to execute. Therefore, the uncontrollable results of LLM generation and the sensitivity of SQL to syntax lead to the high vulnerability of the NL2SQL system. In order to address this problem, other LLM-based NL2SQL methods in the prior art usually adopt the method of generating multiple SQLs. However, this method consumes a lot of Token resources and takes more time, which is not a good user experience. Moreover, the method of generating multiple SQLs is still difficult to take effect when dealing with complex queries. For example, when the user's Query needs to generate complex SQL to complete the query, it is difficult to get it right once even if it is generated repeatedly.
[0082] To address the above issues, this embodiment proposes an example enhancement solution, which allows LLM to generate executable SQL strictly according to the reference SQL provided in the example. The core principle is to convert the SQL example matched by the query into a function call. For example, when a user asks "check Cheng Moudong's workload in August", the question is matched to the SQL example shown in Table 2.
[0083] Table 2
[0084]
[0085] Treat the queried SQL examples as functions, that is, treat the list names in the SQL as parameters in the function, and treat the list values in the SQL as parameter values in the function, then you can get a function that can be used for the function call function using a large language model. For example, number 1 in the above table can get the following JSON for the function call request. [
[0087] {
[0088] "type": "function",
[0089] "function": {
[0090] "name": "check_work_hours_saturation",
[0091] "description": "{employee name} worked from {start time} to {end time}.",
[0092] "parameters": {
[0093] "type": "object",
[0094] "properties": [
[0095] {
[0096] "employeeName": {
[0097] "description": "Employee Name",
[0098] "type": "string"
[0099] }
[0100] },
[0101] {
[0102] "endTime": {
[0103] "description": "End time",
[0104] "type": "string"
[0105] }
[0106] },
[0107] {
[0108] "startTime": {
[0109] "description": "Start time",
[0110] "type": "string"
[0111] }
[0112] }
[0113] ],
[0114] "required": [
[0115] "employeeName",
[0116] "endTime",
[0117] "startTime" ]
[0119] }
[0120] }
[0121] }
[0122] ].
[0123] The function name comes from the Query part in the SQL example and is converted into English using the LLM prompt word template. The prompt word template is as follows.
[0124] # Task
[0125] You are an advanced programmer and need to give the function a concise name based on the function description. You must follow the "naming conventions" and output it in the "example" format.
[0126] Example format: (function description: English function name)
[0127] Output format: (Function description: the English name you choose)
[0128] # Naming conventions
[0129] 1. Only letters can be used, and special symbols other than "_" cannot be used
[0130] 2. No more than 50 characters
[0131] # Example
[0132] (Check whether {employee name}{year-month-day} is late: check_tardiness)
[0133] (Check who has taken leave at {year-month-day}: check_vacate)
[0134] # Input
[0135] {function_name}.
[0136] JSON properties are also converted to English through the LLM prompt word template. The prompt word template is as follows.
[0137] # Task
[0138] You are an advanced programmer and need to name variables. You must follow the "naming conventions" and "constraints" and output them in the "example" format.
[0139] The format of the example is: (Chinese description of variable: English variable name)
[0140] # Constraints
[0141] - No additional explanation is allowed
[0142] - Each variable can only be named once
[0143] # Naming conventions
[0144] 1. Only letters can be used, and special symbols other than "_" cannot be used
[0145] 2. Cannot exceed 20 characters
[0146] # Example
[0147] (Quantity: number)
[0148] (Date: date)
[0149] (name: name)
[0150] # Input
[0151] {function_properties} .
[0152] Finally, use the model to self-check whether the result of the enhanced function of the example can meet the user's needs. The template for self-check is as follows.
[0153] You are a database administrator. You need to judge whether a "query result" and the query SQL can satisfy the "user's question". Your answer should be "yes" or "no" without any additional explanation.
[0154] # Notice
[0155] - The current time is "{time}". If the date is not mentioned, the default is today. If the year and month are not mentioned, the default is {now.year} and {now.month}. If the year is not mentioned, the default is {now.year}.
[0156] - You should pay attention to whether the query condition where in "Query SQL" corresponds to the content of the user's question
[0157] - "The meaning of the query SQL" is the explanation of the "query SQL", so compared to the "query result", you should give priority to whether the "query SQL meaning" and "user question" have similar meanings. If so, reply "yes".
[0158] # Start
[0159] User question:{query}
[0160] Query SQL:
[0161] ```sql
[0162] {sql}
[0163] ```
[0164] Query results:{data}
[0165] The meaning of query SQL: {example_query}
[0166] Your reply: .
[0167] The following is a detailed description of the PreSQL-based example enhancement in the method of the present invention.
[0168] PreSQL-based example enhancement is a method for enhancing SQL generation by using SQL examples retrieved from previously generated SQL (PreSQL generated in step S600). The enhancement is performed in two ways, namely, PreSQL execution error records and SQL example tables and fields.
[0169] PreSQL-based execution error record: Use the PreSQL error prompt returned by the database as part of the prompt word to avoid the same error from occurring again in this question.
[0170] Tables and fields based on SQL examples: Since PreSQL contains more information than Query, PreSQL is used instead of Query to retrieve SQL examples and table patterns. The SQL examples obtained after retrieval are similar to PreSQL, so the retrieved table pattern set is filtered by the tables used by PreSQL and the tables used in the SQL examples. The filtered table pattern only retains the fields used in PreSQL and the SQL examples. In this way, a simplified table pattern is obtained. The simplified table pattern is shorter, so more tokens can be left for the SQL example, allowing the prompt word to contain richer information.
[0171] By combining the table schema and SQL examples obtained in this step with those obtained in steps S200 and S300 in pairs, four groups of prompts with differences can be obtained, so that the generated SQL has more variations.
[0172] After multiple SQL statements are executed, the SQL statements that failed to execute will be filtered out. If an error is reported during the execution of SQL statements, it is considered a failed SQL statement. Since SQL statements are generated by LLM, this situation is more common. For SQL statements that have been successfully executed, voting is performed based on the returned results. SQL statements that return the same results are considered to have voted for the same result, and the result with the most votes is returned to the user as the final result. When a tie occurs, the following prompt word template is used to allow the large language model to select the most appropriate result. The template for the large language model to select the most appropriate result is as follows.
[0173] You are a database administrator. Refer to the format of "Example" and select one from multiple SQL execution result numbers that best matches the user's question.
[0174] # Constraints
[0175] - Your response result can only be a numerical value, and no additional explanation can be given
[0176] - When the date is not mentioned, the default is today ({time}). When the year and month are not mentioned, the default is {now.year} and {now.month}. When the year is not mentioned, the default is {now.year}.
[0177] # Example
[0178] User Question: View You Zeshun's check-in record on August 2
[0179] SQL result 1: {example}
[0180] SQL result 2: (209, b"Ambiguous column name 'employee_id'.DB-Lib errormessage 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
[0181] SQL result 3:[]
[0182] The most suitable is: 1
[0183] # Start
[0184] User question: {query}
[0185] {sql_result}
[0186] The most suitable one is: .
[0187] Compared with other existing technical solutions, the present invention has the following advantages:
[0188] 1. When the user's question involves entities such as time and administrative area, the SQL generated by the present invention is more accurate than that generated by other existing NL2SQL systems.
[0189] 2. Higher controllability. Although the LLM-based NL2SQL system has significantly improved the effect compared with earlier rule-based and deep learning-based methods, a common problem is that the generated effect cannot be controlled, making the system very fragile. Even if LLM is guided by providing examples, it is very easy to make mistakes when performing complex situations such as multi-table queries and subqueries due to the difficulty in understanding the business model and its own lack of capabilities. The present invention regards SQL examples as functions and uses the function call function of the large language model to achieve effective control of the generated results through examples.
[0190] The method of the present invention for increasing the accuracy of complex SQL generated by natural language using SQL examples enhances the accuracy of SQL generated by LLM with the help of SQL examples added by users, while reducing the waiting time of users, and has excellent effects when generating complex SQL. The method of the present invention sets the steps of user question rewriting, example enhancement based on Function call, and example enhancement based on PreSQL. The purpose of example enhancement based on Function call is to enhance the influence of examples manually added by users on the SQL generated by LLM, which can greatly improve the controllability of the generated results and greatly reduce the time required to generate SQL. Example enhancement based on PreSQL is a method for simplifying the table model through the table names and column names contained in the SQL examples retrieved by PreSQL. In summary, the method of the present invention can generate complex SQL.
[0191] Finally, it should be noted that the above embodiments are only used to illustrate the technical solution of the present invention rather than to limit the scope of protection of the present invention. Although the present invention has been described in detail with reference to the preferred embodiments, those skilled in the art should understand that the technical solution of the present invention can be modified or replaced by equivalents without departing from the essence and scope of the technical solution of the present invention.
Claims
1. A method for increasing the accuracy of complex SQL generated by natural language using SQL examples, characterized in that: The user's request is processed as follows: S100. Rewrite the user's request Query; S200. Retrieve the database schema related to the requested Query, and retrieve the relevant table schema from the database; S300. Retrieve SQL examples related to the requested Query; S400. Request complexity evaluation based on SQL example, that is, difficulty evaluation of SQL to be generated for requesting Query. If the evaluation result is that the request can be completed by simple SQL, then enter S500; otherwise enter S900; S500. Prompt word construction, that is, filling the request query, table mode, and SQL example into the prompt template; S600. PreSQL generation, that is, inputting the constructed prompt words into the large language model, and the large language model processes them to obtain SQL. The SQL obtained in this step is PreSQL; S700. Execute SQL. If no error is reported during the execution, execute step S1400. If an error is reported during the execution, execute step S800. S800. Check whether the SQL example contains a SQL template, if so, proceed to step S900, otherwise proceed to step S1100; S900. Based on the example enhancement of Function call, that is, using the SQL example matched in step S300 to create the request Json of Function call, and using the large language model to execute, the result is constructed into SQL. The SQL constructed in this step is the SQL generated by example enhancement; S1000. The example enhanced generated SQL, the execution result of the SQL, and the Query are constructed as prompt words and then input to the large language model to determine whether the execution result of the SQL can answer the Query. When the large language model determines that it is "yes", step S1400 is executed, otherwise step S1200 is executed; S1100. Prompt word construction based on PreSQL, that is, through a combination of vector search and full-text search, SQL examples and table patterns related to PreSQL are retrieved from the database, and the retrieved table patterns are filtered through the table used in PreSQL. The filtered table patterns remove unused columns in PreSQL, and finally the database pattern retrieved in step S200, the SQL example retrieved in step S300, the SQL example retrieved in this step and the pattern description are combined in the format of "database pattern + SQL example" to construct 4 groups of prompt word templates; S1200. Based on the example enhancement of PreSQL, that is, generating SQL statements according to the prompt words input in step S1100; S1300. First filter out the results of execution errors, then vote on the execution results of the SQL statements that are executed correctly, and return the result with the most votes as the final result; When there is a tie, the LLM selects the most appropriate outcome; S1400. Return the result to the user.
2. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 1, characterized in that: S100. Rewriting the user's request specifically involves replacing the relative time and personal pronoun description in the request Query with a standard time description and a standard name.
3. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 2, characterized in that: S200 retrieves relevant table schemas from the database by combining vector retrieval and full-text retrieval.
4. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 3, characterized in that: Pre-configured database schema and SQL examples.
5. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 4, characterized in that: SQL examples are pre-added manually by users.
6. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 5, characterized in that: In S300, relevant SQL examples are retrieved from the database through a combination of vector search and full-text search.
7. The method for increasing the accuracy of complex SQL generated by natural language using SQL examples according to claim 6, characterized in that: The judgment criteria for simple SQL in S400 are: no join operation is used in the SQL or no subquery is used in the SQL.
Citation Information
Patent Citations
SQL query statement generation system and method
CN118585537A
Method, system and equipment for generating SQL (Structured Query Language) statement based on large model
CN119127913A