A method, device and medium for converting a natural language question into an SQL statement
By receiving natural language query statements input by users, matching them with query templates and large language models, and combining parsing and SQL templates, SQL query statements are generated step by step. This solves the problem of insufficient accuracy and interpretability in converting natural language problems into SQL statements in existing technologies, and achieves higher conversion accuracy and system flexibility.
Patent Information
- Application Number
- CN202411736750.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-29
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2044-11-29
AI Technical Summary
Existing technologies lack accuracy and interpretability when converting natural language questions into SQL statements, especially when dealing with complex queries.
By receiving natural language query statements input by users, candidate templates are determined based on the semantic similarity between the query statements and multiple query templates in the database, and a large language model is used for matching. In combination with predefined parsing and SQL templates, SQL query statements are generated in steps.
It improves the accuracy and explainability of natural language query to SQL conversion, reduces data governance costs and complexity, and enhances the flexibility and adaptability of the system.
Smart Images

Figure CN119597785B_ABST
Abstract
Description
Technical Field
[0001] The present application belongs to the technical field of database management and data analysis, and specifically relates to a method, device and medium for converting natural language questions into SQL statements. Background Art
[0002] In recent years, with the advancement of artificial intelligence and natural language processing technologies, the demand for database queries directly using natural language has continued to grow. However, the structural differences between natural language and SQL pose a challenge to direct conversion. The industry currently uses the following three solutions to address this problem:
[0003] (1) Rule-based approach: This approach relies on predefined rules and templates to map users’ natural language queries to corresponding SQL queries. Its disadvantage is that the rules are relatively fixed and difficult to adapt to unknown query forms, new query requirements, and language changes.
[0004] (2) Machine learning-based methods: Use training data to generate query SQL and achieve conversion through model learning and reasoning. Although this type of method is more flexible, it is not very accurate when processing complex queries and requires a large amount of training data.
[0005] (3) Large model-based solution: Use large pre-trained language models (such as ChatGPT) to directly perform NL2SQL conversion without training the model from scratch. This solution can be quickly launched without the need for extensive data annotation and model training. However, it does not fully understand natural language queries in specific business scenarios, resulting in low SQL generation accuracy.
[0006] Therefore, how to further improve the accuracy and interpretability of converting natural language questions into SQL statements is a topic worthy of research in this field. Summary of the Invention
[0007] In view of the above analysis, embodiments of the present invention aim to provide a method, device, and medium for converting natural language questions into SQL statements, so as to improve the accuracy and interpretability of converting natural language questions into SQL statements.
[0008] In a first aspect of the present application, a method for converting a natural language question into an SQL statement is provided, comprising:
[0009] Receive a query statement expressed in natural language input by a user;
[0010] Determining multiple candidate query templates that match the query statement based on semantic similarity between the query statement and multiple query templates in the database; the query template is a predefined template used to match a predetermined query type;
[0011] inputting the query sentence and a plurality of candidate query templates into a large language model, outputting a matching result that is most matched with the query sentence as a matched query template;
[0012] based on the matched query template, determining a corresponding parsing template and a corresponding SQL template, the parsing template being a predefined template for extracting query variables from a query sentence, and the SQL template being a predefined template for a SQL form expression for a predetermined query type;
[0013] based on the matched parsing template, parsing a parsing result from the query sentence;
[0014] filling the query variables in the parsing result into the SQL template to generate a SQL query sentence.
[0015] Optionally, the predefined process of the query template includes:
[0016] collecting query examples obtained from a plurality of information sources;
[0017] identifying a plurality of typical query types from the query examples;
[0018] defining one or more templates for each identified query type, the templates including necessary parameter placeholders;
[0019] storing the query templates in a database for query invocation.
[0020] Optionally, the parsing of the parsing result from the query sentence based on the matched parsing template includes:
[0021] extracting entity information from the query sentence using an entity recognition method of a large language model;
[0022] based on the matched parsing template, filling the entity information into the corresponding placeholders according to the structure defined in the parsing template to obtain the parsing result.
[0023] Optionally, the determination of a plurality of candidate query templates that are matched with the query sentence based on the semantic similarity between the query sentence and a plurality of query templates in the database includes:
[0024] determining a vector representation of the query sentence using an embedding vector model;
[0025] determining a vector representation of a plurality of query templates using an embedding vector model;
[0026] calculating the semantic similarity between the query sentence and the query templates to determine a plurality of candidate query templates that are most matched in similarity with the query sentence.
[0027] Optionally, the query statement and the plurality of candidate query templates are input into a large language model, and a matching result that is most matched with the query statement is output as a matched query template, including:
[0028] The query statement and the plurality of candidate query templates are combined to generate one or more query prompt words;
[0029] The generated query prompt words are subjected to applicability checking and logical consistency checking;
[0030] The checked query prompt words are input into a large language model, and a matching result that is most matched with the query statement is output as a matched query template.
[0031] Optionally, after the query statement and the plurality of candidate query templates are input into a large language model, and a matching result that is most matched with the query statement is output as a matched query template, the method further includes:
[0032] It is determined whether the current indicator in the matched query template matches the preposition indicator;
[0033] If not, a rejection processing procedure is entered, and a casual indicator is output.
[0034] Optionally, after the query statement and the plurality of candidate query templates are input into a large language model, and a matching result that is most matched with the query statement is output as a matched query template, the method further includes:
[0035] It is determined whether the query result is empty or missing key;
[0036] If not, a data completion procedure is entered, and the step of receiving a query statement expressed in natural language input by a user is re-executed.
[0037] Optionally, the filling of the query variable in the query result into the SQL template to generate a SQL query statement includes:
[0038] When the format of the query variable in the query result does not match the format in the SQL template, the query variable is subjected to format conversion, and the converted data is filled into the SQL template to generate a SQL query statement.
[0039] In a second aspect, the application provides a device for converting a natural language question into a SQL statement, including a memory and a processor, the memory storing a computer program, and the computer program being executed by the processor to implement the method for converting a natural language question into a SQL statement according to any one of the above.
[0040] In a third aspect, the present application provides a computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the method for converting a natural language question into an SQL statement according to any of the above.
[0041] The method for converting a natural language question into an SQL statement provided by the present application receives a query statement input by a user in natural language; determines a plurality of candidate query templates matched with the query statement based on the semantic similarity between the query statement and the plurality of query templates in the database; inputs the query statement and the plurality of candidate query templates into a large language model, and outputs a matching result most matched with the query statement as a matched query template; determines a corresponding parsing template and a corresponding SQL template based on the matched query template; parses a parsing result from the query statement based on the matched parsing template; and fills a query variable in the parsing result into the SQL template to generate an SQL query statement. The present application matches a query statement with a predefined query module, further dynamically selects and adjusts a parsing template and an SQL template, enhances flexibility and adaptability, improves the accuracy of natural language query to SQL conversion, and improves the explainability of natural language query to SQL conversion due to the business logic of the predefined query module.
[0042] In addition, the present application also provides a device and a medium for converting a natural language question into an SQL statement with the above technical effects. BRIEF DESCRIPTION OF DRAWINGS
[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed in the embodiment or prior art description will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments described in the present application, and other drawings can also be obtained by those skilled in the art based on these drawings.
[0044] Figure 1 A flow chart of one specific embodiment of the method for converting a natural language question into an SQL statement provided by the present application;
[0045] Figure 2 A flow chart of another specific embodiment of the method for converting a natural language question into an SQL statement provided by the present application;
[0046] Figure 3 A structural block diagram of the device for converting a natural language question into an SQL statement provided by the present application. DETAILED DESCRIPTION
[0047] In order to make the purpose, technical solutions and advantages of the embodiments of the present application clearer, the technical solutions in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, rather than all of the embodiments. It should be noted that, in the absence of conflict, the embodiments in this disclosure and the features in the embodiments can be combined, separated, interchanged and / or rearranged with each other. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0048] The terms used herein are for the purpose of describing specific embodiments and are not intended to be restrictive. As used herein, unless the context clearly indicates otherwise, the singular forms "one (kind, person)" and "said (the)" are also intended to include plural forms. In addition, when the terms "comprise" and / or "include" and their variations are used in this specification, the features, integral bodies, steps, operations, parts, assemblies and / or their groups stated are explained, but the presence or addition of one or more other features, integral bodies, steps, operations, parts, assemblies and / or their groups is not excluded. It should also be noted that, as used herein, the terms "substantially", "approximately" and other similar terms are used as approximate terms and not as degree terms, so that they are used to explain the inherent deviations of the measured values, calculated values and / or the values provided that will be recognized by those of ordinary skill in the art.
[0049] The flowchart of a specific implementation method of the method for converting natural language questions into SQL statements provided in this application is as follows Figure 1 As shown, the method specifically includes:
[0050] S101: receiving a query statement expressed in natural language input by a user.
[0051] Users can enter a query statement in natural language through the interactive interface, for example, "Find the product with the highest sales in 2022."
[0052] S102: Determine multiple candidate query templates that match the query statement based on semantic similarities between the query statement and multiple query templates in a database.
[0053] The query template is a predefined template used to match a predetermined query type.
[0054] The predefined process of query templates includes: collecting query examples obtained from multiple information sources; identifying multiple typical query types from the query examples; defining one or more templates for each identified query type, the templates including necessary parameter placeholders; storing the query templates in a database for query invocation.
[0055] Common query examples are collected from the target user group. This can specifically include direct input from users, log files, or feedback obtained through interaction with users. These queries are analyzed to identify several common query patterns. For example, a certain business may often need to query sales data, inventory information, or customer feedback, etc. One or more templates are defined for each identified query type. These templates include necessary parameter placeholders, such as date, location, specific value, etc. For example, for a sales query, the template can be "What is the sales of {product} in {time_range}?"
[0056] NLP techniques can be used to create semantic labels for each parameter in the template, which helps subsequent entity recognition and parameter filling.
[0057] After the query templates are constructed, after receiving the user input query statement, a recall model can be used to obtain candidate results, which specifically includes: determining the vector representation of the query statement using an embedding vector model; determining the vector representation of multiple query templates using an embedding vector model; calculating the semantic similarity between the query statement and the query templates to determine the multiple candidate query templates that are most similar to the query statement.
[0058] The user's natural language query is input into the embedding vector model to generate a vector representation of the query statement. This vector should capture the core semantic features of the query. Similarly, all predefined query templates are also input into the same embedding vector model to generate a vector representation for each template.
[0059] The similarity between each query template vector and the query statement vector is calculated using a similarity measure method (such as cosine similarity) to obtain the similarity score between each query template and the query statement. According to the calculated similarity score, the top few templates with the highest scores are selected as candidates. These top scoring templates are most semantically similar to the user's query statement and are therefore most likely to produce the SQL query expected by the user. A similarity threshold can be set or the top N templates with the highest similarity scores can be selected as candidate query templates.
[0060] S103: input the query statement and multiple candidate query templates into a large language model, output the matching result that is most matched to the query statement as the matched query template.
[0061] The query statement and the plurality of candidate query templates are combined to generate one or more query prompts; the generated query prompts are checked for applicability and logical consistency; and the checked query prompt words are input into a large language model.
[0062] The large language model uses an internal algorithm to evaluate the degree of matching between the query statement and each candidate query template, and can parse the specific query intent of the user in combination with the restriction conditions in the query prompt, and output the matching result that best matches the query statement as the matched query template.
[0063] S104: Based on the matched query template, determine the corresponding parsing template and the corresponding SQL template.
[0064] Generally, each query template is associated with a parsing template and a SQL template. After matching the query template, the corresponding parsing template and SQL template are determined according to the pre-defined association relationship. The parsing template is a pre-defined template for extracting query variables from the query statement. These templates define which information needs to be extracted from the query, such as time, location, and numerical value. The template usually contains placeholders for filling in actual data during the parsing process.
[0065] The SQL template is a pre-defined template for the SQL form expression of the predetermined query type. A specific SQL statement structure is provided to explain how to use the parameters extracted from the parsing template to form a complete SQL query.
[0066] S105: Based on the matched parsing template, parse the query statement to obtain a parsing result.
[0067] Specifically, the entity recognition method of the large language model is used to extract entity information from the query statement; based on the matched parsing template, the entity information is filled into the corresponding placeholders according to the structure defined in the parsing template to obtain the parsing result.
[0068] As a specific implementation, the parsing result can include but is not limited to:
[0069] (1) Target indicator
[0070] This refers to the core data indicator in the user's query intent, such as "total electricity consumption nationwide" and "total sales". The parsing result will clearly indicate this target so that the system knows which type of data needs to be retrieved from the database.
[0071] (2) Filter condition
[0072] These are parameters used to define the query range or conditions. The corresponding field name, operator, and value are extracted from the user's natural language query. For example:
[0073] Field: The corresponding column name in the database, such as "date", "region", etc.
[0074] Operator: Such as equal (=), greater than (>), less than (<), etc., used to express the relationship between the field and the value.
[0075] Value: Specific filtering values, such as "2022", "a certain region", etc., which are used to match database records in SQL queries.
[0076] (3) Aggregation function
[0077] Such as sum, average, maximum, minimum, etc., these functions are used to perform statistical calculations, usually associated with target indicators.
[0078] S106: Fill in the query variables in the analysis result into the SQL template to generate a SQL query statement.
[0079] According to the parameters parsed from the user query, fill in the corresponding position in the SQL template. As a specific implementation, the parsed values can be further converted to meet the format requirements of SQL queries. For example, convert the time expression to a specific date format. Combined with the filled template and field conversion, the final SQL query statement is generated.
[0080] The existing solution using large models is based on natural language combined with table structure information and reference examples to directly generate SQL at one time, while the solution provided by the present application is to first generate an analysis result, and then further convert the analysis result and the SQL template into a SQL statement. Through the step-by-step and hierarchical way, the parsing process and SQL generation process can be more accurately controlled to ensure that the conversion result meets the expected accuracy and logical consistency. The result of each step can be audited and optimized in detail. Through step-by-step processing, errors can be checked and corrected at each step, reducing SQL errors caused by understanding errors or inaccurate parsing, providing higher transparency and explainability.
[0081] In addition, by introducing the SQL template solution, the query accuracy can be greatly improved with little or no governance, reducing the cost and complexity of data governance.
[0082] The flowchart of another specific implementation of the method for converting natural language questions into SQL statements provided by the present application is shown in Figure 2 The method specifically includes:
[0083] S201: receiving a query sentence in natural language input by a user.
[0084] S202: determining a plurality of candidate query templates matching the query sentence based on semantic similarity between the query sentence and the plurality of query templates in a database.
[0085] The query template is a predefined template for matching a predetermined query type.
[0086] Specifically, the embedding model can be used to process the query sentence input by the user to generate a vector representation of the query sentence.
[0087] The embedding model is used to vectorize the query templates in the database. The recall model is used to calculate the similarity between the query sentence and the query template vector representation, and return the N most similar query templates. The query template is stored in the database, and has a unique index or ID corresponding thereto. The index is automatically generated when the template is added to the database, and can be used for fast retrieval.
[0088] The embodiment of the present application improves the accuracy of natural language query to SQL conversion through the cooperative work of the embedding model and the recall model.
[0089] S203: inputting the query sentence and the plurality of candidate query templates into a large language model, and outputting a matching result most matching the query sentence as a matched query template.
[0090] The query sentence and the plurality of candidate query templates are combined to form one or more complete query prompts (prompts) which are input into the large language model. It can be understood that the query prompt combines the user's original language input and the structured format of the template, and can be used for further processing and parsing. Through suitability check and logical consistency check on the query prompt, a unique selection result is output as a matched query template. Specifically, the coverage and accuracy standards can be used to select the most matching template. Among them, the coverage is to select the template that most comprehensively covers the user's query intention, and the accuracy is to select the template with the highest semantic understanding and structured conversion accuracy.
[0091] According to the user query and the database content, the query template is dynamically selected and adjusted to enhance the flexibility of the system.
[0092] S204: determining whether the current index in the matched query template matches the pre-index.
[0093] If no, enter the rejection processing flow, output the chatting index. The chatting here refers to the non-task-oriented dialogue content, such as daily exchange, casual questions or non-specific purpose dialogue.
[0094] If yes, enter S205.
[0095] In this step, if it is identified as a task-related query, the process of converting the query into SQL will be continued. If it is identified as chatting or non-task-related query, it can be transferred to a module specially processing non-task dialogue, such as entering the rejection processing flow.
[0096] S205: Based on the matched query template, determine the corresponding parsing template and the corresponding SQL template.
[0097] S206: Based on the matched parsing template, parse the parsing result from the query sentence.
[0098] S207: Judge whether the parsing result is empty or key missing.
[0099] If no, enter the data completion flow, and return to the step of receiving the query sentence in natural language expression input by the user to re-execute.
[0100] If yes, enter S208.
[0101] This step is used to judge whether the parsing result is correct. If the parsing result is empty or key missing, it means that the data is incomplete and needs to be completed. Then enter the data completion flow, and return to the step of receiving the query sentence in natural language expression input by the user to re-execute. If the parsing result is correct, further execute the subsequent step of generating SQL sentence.
[0102] S208: Fill the query variable in the parsing result into the SQL template to generate the SQL query sentence.
[0103] Wherein, when the format of the query variable in the parsing result does not match the format in the SQL template, the query variable is format-converted, and the converted data is filled into the SQL template to generate the SQL query sentence.
[0104] The application matches the query sentence with the predefined query module, further dynamically selects and adjusts the parsing template and the SQL template, enhances the flexibility and adaptability, improves the accuracy of natural language query to SQL conversion, and the predefined query module has business logic, improves the explainability of natural language query to SQL conversion.
[0105] The method for converting natural language questions into SQL statements provided by the present application is further described below with a specific embodiment. In this embodiment, the user inputs the query statement (Query): "What is the total electricity consumption in the country in the second half of last year?"
[0106] The predefined query module can be: "{time_val} What is the total electricity consumption in the country?"
[0107] Here, {time_val} is a placeholder that will be replaced by a specific time description during actual processing. The parsing template defines how to extract specific data from the user query. Its specific format can be:
[0108] {'target_measure': 'total electricity consumption in the country', 'filters': [{'field': 'yers_m', 'operator': '=', 'variable': '{time_val}'}]}
[0109] Among them, 'target_measure': 'total electricity consumption in the country' indicates that the query target is to obtain the total electricity consumption in the country. The 'filters' list contains the filter conditions, i.e. the time range, defined by the field yers_m, operator =, and variable {time_val}.
[0110] The parsing result can be:
[0111] {'target_measure': 'total electricity consumption in the country', 'filters': [{'field': 'yers_m', 'operator': '=', 'variable': 'last year second half'}]}
[0112] Here, the variable has been replaced by the specific time description "last year second half".
[0113] The SQL template is used to generate the final SQL query, which is filled based on the information in the parsing result. The SQL template is:
[0114] select sum(ifnull(usg, 0)) as total from ld_ent_elec_consume_view where yers_m = '{time_val}'
[0115] ifnull(usg, 0) ensures that even if some data is null, it can be calculated by replacing null with 0.
[0116] The finally generated SQL statement is dynamically generated based on the parsing result and filled with specific time information.
[0117] The SQL statement is:
[0118] select sum(ifnull(usg, 0)) as total from ld_ent_elec_consume_view where year(str_to_date(concat(yers_m, '-01'), '%Y-%m-%d')) = year(now())-1 and month(str_to_date(concat(yers_m, '-01'), '%Y-%m-%d'))>= 7;
[0119] Here, the yers_m field (year-month format) is converted into a date using the str_to_date and concat functions, and then the year and month are extracted using the year() and month() functions to match the time range of "last year's second half".
[0120] It can be understood that the field values in the SQL template are only the parsed results in the input, and sometimes appropriate conversion is needed when converted into a SQL expression.
[0121] In addition, the present application also provides a device for converting a natural language question into a SQL statement, which comprises: Figure 3 The device for converting a natural language question into a SQL statement provided by the present application is shown in a structural block diagram, and specifically comprises a memory 31 and a processor 32, the memory 31 stores a computer program, and the computer program is executed by the processor 32 to realize the method for converting a natural language question into a SQL statement according to any one of the above.
[0122] In addition, the present application also provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to realize the method for converting a natural language question into a SQL statement according to any one of the above.
[0123] Computer-readable storage media includes permanent and non-permanent, removable and non-removable media implemented by any method or technology for information storage. Information can be computer-readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, compact disc read-only memory (CD-ROM), digital versatile disc (DVD) or other optical storage, magnetic cassette, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transmission medium that can be used to store information accessible to a computing device. According to the definition herein, computer-readable media does not include transitory computer-readable media, such as modulated data signals and carriers.
[0124] The skilled person should further appreciate that the various illustrative units and algorithm steps described in connection with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or combinations of both. To clearly illustrate the interchangeability of hardware and software, various illustrative components, units, and steps have been described above generally in terms of their functionality, without limitation to any particular structural realization in terms of hardware or software. Depending upon the particular application, different methods can be used to implement each of the steps described, and these methods can be implemented in software or hardware without departing from the scope of the application.
[0125] The steps of a method or algorithm described in connection with the embodiments disclosed herein can be embodied directly in hardware, in a software module executed by a processor, or in a combination of the two. A software module can reside in random access memory (RAM), memory, read-only memory (ROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM), registers, hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.
[0126] The above detailed description has further described the purposes, technical solutions and beneficial effects of the present application. It should be understood that the above description is only a specific implementation of the present application and is not intended to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.
Claims
1. A method for converting natural language questions into SQL statements, characterized in that: include: Receive a query statement expressed in natural language input by a user; Determining multiple candidate query templates that match the query statement based on semantic similarities between the query statement and multiple query templates in a database; The query template is a predefined template for matching a predetermined query type; Inputting the query statement and multiple candidate query templates into a large language model, and outputting a matching result that best matches the query statement as a matched query template; Based on the matched query template, a corresponding parsing template and a corresponding SQL template are determined, wherein the parsing template is a predefined template for extracting query variables from the query statement, and the SQL template is a predefined template for SQL expression for a predetermined query type; Parsing the query statement to obtain a parsing result based on the matched parsing template; Filling the query variables in the parsing result into the SQL template to generate an SQL query statement; The query statement and multiple candidate query templates are input into the large language model, and the matching result that best matches the query statement is output, as the matched query templates including: Combining the query statement and multiple candidate query templates to generate one or more query prompt words; Perform applicability and logical consistency checks on the generated query hints; Inputting the checked query prompt words into the large language model, and outputting the matching result that best matches the query statement as the matched query template; The step of parsing the query statement based on the matched parsing template to obtain a parsing result includes: Extracting entity information from the query statement using an entity recognition method based on a large language model; Based on the matched parsing template, the entity information is filled into the corresponding placeholder according to the structure defined in the parsing template to obtain a parsing result.
2. The method for converting natural language questions into SQL statements according to claim 1, characterized in that: The predefinition process of the query template includes: Collect query examples obtained from multiple information sources; identifying multiple typical query types from the query examples; Define one or more templates for each identified query type, including necessary parameter placeholders; Store query templates in the database for easy query calls.
3. The method for converting natural language questions into SQL statements according to claim 2, characterized in that: The determining of multiple candidate query templates matching the query statement based on the semantic similarity between the query statement and multiple query templates in the database includes: Determine a vector representation of the query statement using an embedded vector model; Using an embedded vector model to determine vector representations of multiple query templates; The semantic similarity between the query statement and the query template is calculated, and a plurality of candidate query templates that best match the similarity with the query statement are determined.
4. The method for converting natural language questions into SQL statements according to claim 3, characterized in that: After inputting the query statement and multiple candidate query templates into the large language model and outputting a matching result that best matches the query statement as the matched query template, the method further includes: Determine whether the current indicator in the matched query template matches the previous indicator; If not, the response rejection process is entered and the chat indicator is output.
5. The method for converting natural language questions into SQL statements according to claim 4, characterized in that: After parsing the query statement based on the matched parsing template to obtain a parsing result, the method further includes: Determine whether the parsing result has an empty value or a missing key; If not, the process enters the data completion process and returns to the step of receiving the query statement expressed in natural language input by the user and re-executes it.
6. The method for converting natural language questions into SQL statements according to claim 5, characterized in that: Filling the query variables in the parsing result into the SQL template to generate an SQL query statement includes: When the format of the query variable in the parsing result does not match the format in the SQL template, the query variable is format converted, and the converted data is filled into the SQL template to generate an SQL query statement.
7. A device for converting natural language questions into SQL statements, characterized in that The method comprises a memory and a processor, wherein the memory stores a computer program, and when the computer program is executed by the processor, the method for converting a natural language question into an SQL statement according to any one of claims 1 to 6 is implemented.
8. A computer-readable storage medium, characterized in that A computer program is stored thereon, and when the computer program is executed by a processor, the method for converting a natural language question into an SQL statement according to any one of claims 1 to 6 is implemented.
Citation Information
Patent Citations
Mapping knowledge domain questioning and answering system and method based on template matching technique
CN105868313A
Database query method and device, computer equipment and storage medium
CN114253990A
Answer generation method and device, storage medium and electronic equipment
CN118069801A