Method and device for converting natural language question into SQL statement and medium

By matching natural language queries with multiple query templates, using large language models to determine parsing and SQL templates, dynamically populating query variables to generate SQL query statements, solving the problem of insufficient accuracy and interpretability of natural language to SQL conversion in the existing technology, and achieving higher conversion accuracy and flexibility.

CN119597785AActive Publication Date: 2025-03-11ZHEJIANG ZHIPU XINPIAN TECHNOLOGY CO LTD

Patent Information

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

AI Technical Summary

Technical Problem

The prior art lacks accuracy and interpretability when converting natural language problems into SQL statements, especially when dealing with complex queries and specific business scenarios.

Method used

By receiving natural language query statements input by users, matching multiple query templates based on semantic similarity, using a large language model to output the most matching query template, and then determining the parsing template and SQL template, dynamically populating query variables to generate SQL query statements.

Benefits of technology

It improves the accuracy and interpretability of natural language query SQL transformations, enhances the flexibility and adaptability of the system, and reduces the cost and complexity of data governance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119597785A_ABST
    Figure CN119597785A_ABST
Patent Text Reader

Abstract

The invention relates to a method and equipment for converting a natural language problem into an SQL statement and a medium, and belongs to the technical field of database management and data analysis. The method comprises the steps of receiving a query statement which is input by a user and expressed by a natural language; based on the semantic similarity between the query statement and a plurality of query templates in a database, determining a plurality of candidate query templates matched with the query statement; inputting the query statement and a plurality of candidate query templates into a large language model, and outputting a matching result which is most matched with the query statement as a matched query template; determining a corresponding analysis template and a corresponding SQL (Structured Query Language) template based on the matched query template; based on the matched analysis template, analyzing the query statement to obtain an analysis result; the SQL template is filled with the query variables in the analysis result, and an SQL query statement is generated. According to the method, the accuracy and interpretability of conversion from natural language query to SQL are improved.
Need to check novelty before this filing date? Find Prior Art

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 technology, the demand for database queries directly through natural language has continued to grow. However, the structural differences between natural language and SQL pose a challenge to direct conversion. The industry currently mainly uses the following three solutions to solve this problem: (1) Rule-based method: This method relies on predefined rules and templates to map the user's natural language query to the corresponding SQL query. Its disadvantage is that the rules are relatively fixed and difficult to adapt to unknown query forms, new query requirements and language changes.

[0003] (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.

[0004] (3) Solution based on large models: Use large pre-trained language models (such as ChatGPT) to directly perform NL2SQL conversion without training the model from scratch. It can be quickly launched without the need for large amounts of data annotation and model training. However, it does not fully understand natural language queries in specific business scenarios, resulting in low accuracy in SQL generation.

[0005] Therefore, how to further improve the accuracy and interpretability of converting natural language questions into SQL statements is a topic worthy of study in this field. Summary of the invention

[0006] In view of the above analysis, the 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.

[0007] In a first aspect of the present application, a method for converting a natural language question into an SQL statement is provided, comprising: Receiving a query statement expressed in natural language input by a user; Based on the semantic similarity between the query statement and multiple query templates in the database, determine multiple candidate query templates that match the query statement; the query template is a predefined template used to match 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, determine a corresponding parsing template and a corresponding SQL template, wherein the parsing template is a predefined template for extracting query variables from a query statement, and the SQL template is a predefined template for SQL form expression for a predetermined query type; Based on the matched parsing template, parse the query statement to obtain a parsing result; Fill the query variables in the parsing result into the SQL template to generate an SQL query statement.

[0008] Optionally, the predefinition process of the query template includes: Collect query examples obtained from multiple information sources; identifying a plurality of 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.

[0009] Optionally, the 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 of 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.

[0010] Optionally, the determining, based on the semantic similarity between the query statement and a plurality of query templates in a database, a plurality of candidate query templates matching the query statement comprises: Using an embedded vector model to determine a vector representation of the query statement; 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.

[0011] Optionally, the query statement and multiple candidate query templates are input into a large language model, and a matching result that best matches the query statement is output, where the matched query template includes: Combining the query statement and a plurality of candidate query templates to generate one or more query prompt words; Perform applicability and logical consistency checks on the generated query hints; The checked query prompt words are input into the large language model, and the matching result that best matches the query statement is output as the matched query template.

[0012] Optionally, after inputting the query statement and a plurality of candidate query templates into a large language model and outputting a matching result that best matches the query statement as a matched query template, the method further includes: Determine whether the current indicator in the matched query template matches the previous indicator; If not, the refusal process is entered and the chat indicator is output.

[0013] Optionally, after obtaining a parsing result from the query statement based on the matched parsing template, the method further includes: Determine whether the parsing result has an empty value or a missing key; If not, the data completion process is entered, and the step of receiving the query statement expressed in natural language input by the user is returned and re-executed.

[0014] Optionally, 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.

[0015] In a second aspect of the present application, a device for converting a natural language question into an SQL statement is provided, comprising 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 the above-described methods is implemented.

[0016] According to a third aspect of the present application, a computer-readable storage medium is provided, on which a computer program is stored. 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 the above methods is implemented.

[0017] The method for converting a natural language question into an SQL statement provided by the present application is as follows: receiving a query statement expressed in natural language input by a user; determining multiple candidate query templates matching the query statement based on the semantic similarity between the query statement and multiple query templates in a database; inputting the query statement and multiple candidate query templates into a large language model, and outputting the matching result that best matches the query statement as a matched query template; determining a corresponding parsing template and a corresponding SQL template based on the matched query template; parsing the query statement to obtain a parsing result based on the matched parsing template; and filling the query variables in the parsing result into the SQL template to generate an SQL query statement. The present application enhances flexibility and adaptability, improves the accuracy of natural language query to SQL conversion, and the predefined query module has business logic, which improves the interpretability of natural language query to SQL conversion.

[0018] In addition, the present application also provides a device and medium for converting natural language questions into SQL statements with the above-mentioned technical effects. BRIEF DESCRIPTION OF THE DRAWINGS

[0019] In order to more clearly illustrate the embodiments of this specification or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in the embodiments of this specification. For ordinary technicians in this field, other drawings can also be obtained based on these drawings.

[0020] Figure 1 A flowchart of a specific implementation of the method for converting a natural language question into an SQL statement provided by the present application; Figure 2 A flowchart of another specific implementation of the method for converting a natural language question into an SQL statement provided by the present application; Figure 3 This is a structural block diagram of the device provided in this application for converting natural language questions into SQL statements. DETAILED DESCRIPTION

[0021] In order to make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme in the embodiments of the present application will be clearly and completely described in combination 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 the present disclosure and the features in the embodiments can be combined, separated, interchanged and / or rearranged with each other. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0022] The terms used here are for the purpose of describing specific embodiments, and are not intended to be restrictive. As used here, 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, it is explained that there are stated features, integral bodies, steps, operations, parts, assemblies and / or their groups, but it is not excluded that there are or add one or more other features, integral bodies, steps, operations, parts, assemblies and / or their groups. It should also be noted that, as used here, 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.

[0023] A flowchart of a specific implementation of the method for converting a natural language question into an SQL statement provided in the present application is as follows Figure 1 As shown, the method specifically includes: S101: receiving a query statement expressed in natural language input by a user.

[0024] The user can enter a query statement through the interactive interface, which is a statement expressed in natural language, for example, "find the product with the highest sales in 2022".

[0025] S102: Determine a plurality of candidate query templates matching the query statement based on semantic similarities between the query statement and a plurality of query templates in a database.

[0026] The query template is a predefined template used to match a predetermined query type.

[0027] The pre-definition process of the query template 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, wherein the template includes necessary parameter placeholders; and storing the query template in a database for query calls.

[0028] Collect common query examples from the target user group. This can include direct user input, log files, or feedback obtained through interaction with users. Analyze these queries and identify several common query patterns. For example, a business may often need to query sales data, inventory information, or customer feedback. Define one or more templates for each identified query type. These templates include necessary parameter placeholders such as date, location, specific values, etc. For example, for a sales query, the template might be "What is the sales volume of {product} for {time_range}?" NLP technology can be used to create semantic tags for each parameter in the template, which helps with subsequent entity recognition and parameter filling.

[0029] After constructing the query template, after receiving the query statement input by the user, the recall model can be specifically used to obtain candidate results. The process specifically includes: using the embedded vector model to determine the vector representation of the query statement; using the embedded vector model to determine the vector representation of multiple query templates; calculating the semantic similarity between the query statement and the query template, and determining multiple candidate query templates that best match the similarity with the query statement.

[0030] The user's natural language query is input into the embedding vector model to generate a vector representation of the query. 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.

[0031] Use the similarity measurement method between vectors (such as cosine similarity) to calculate the similarity between each query template vector and the query statement vector, and obtain the similarity score between each query template and the query statement. According to the calculated similarity score, select the query templates with the highest scores as candidates. These templates with the highest scores are semantically closest to the user's query statement, so they are most likely to generate the SQL query expected by the user. You can set a similarity threshold or select the top N templates with the highest similarity scores as candidate query templates.

[0032] S103: Input the query statement and multiple candidate query templates into a large language model, and output a matching result that best matches the query statement as a matched query template.

[0033] The query statement and multiple 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.

[0034] The large language model uses an internal algorithm to evaluate the degree of match between the query statement and each candidate query template, and can combine the restrictions in the query prompt to parse the user's specific query intent and output the matching result that best matches the query statement as the matched query template.

[0035] S104: Determine a corresponding parsing template and a corresponding SQL template based on the matched query template.

[0036] Usually, each query template is associated with a parsing template and an SQL template. After the query template is matched, the corresponding parsing template and SQL template are determined according to the predefined association relationship. The parsing template is a predefined template for extracting query variables from the query statement. These templates define what information needs to be extracted from the query, such as time, location, value, etc. The template usually contains placeholders for filling in actual data during the parsing process.

[0037] The SQL template is a predefined template for SQL expression of a predetermined query type, and provides a specific SQL statement structure, illustrating how to use parameters extracted from the parsing template to form a complete SQL query.

[0038] S105: Based on the matched parsing template, parse the query statement to obtain a parsing result.

[0039] Specifically, an entity recognition method of a large language model is used to extract entity information from the query statement; based on a matched parsing template, the entity information is filled into corresponding placeholders according to a structure defined in the parsing template to obtain a parsing result.

[0040] As a specific implementation, the analysis results may include but are not limited to: (1) Target indicators This refers to the core data indicators in the user's query intent, such as "total electricity consumption in the country", "total sales", etc. The parsing results will clarify this goal so that the system knows what type of data needs to be retrieved from the database.

[0041] (2) Filter conditions These are parameters used to limit the scope or conditions of the query. The corresponding field names, operators, and values ​​are extracted from the user's natural language query. For example: Field: the corresponding column name in the database, such as "Date", "Region", etc.

[0042] Operators: such as equal to (=), greater than (>), less than (<), etc., are used to express the relationship between fields and values.

[0043] Value: Specific filter value, such as "2022", "region", etc. These values ​​are used to match database records in SQL queries.

[0044] (3) Aggregation functions These functions, such as sum, average, maximum, minimum, etc., are used to perform statistical calculations, usually associated with a target metric.

[0045] S106: Fill the query variables in the parsing result into the SQL template to generate an SQL query statement.

[0046] According to the parameters parsed from the user query, the corresponding positions in the SQL template are filled. As a specific implementation, the parsed values ​​can be further converted to adapt to the format requirements of the SQL query. For example, the time expression is converted to a specific date format. Combined with the filled template and field conversion, the final SQL query statement is generated.

[0047] The existing solution using a large model is to directly generate SQL at one time based on natural language combined with table structure information and reference examples, while the solution provided by this application is to first parse and generate parsing results, and then further convert them into SQL statements based on the parsing results and SQL templates. Through a step-by-step and layered approach, the parsing process and SQL generation process can be controlled more accurately to ensure that the conversion results meet the expected accuracy and logical consistency. The results of each step can be reviewed and optimized in detail. By processing in steps, errors can be checked and corrected at each step, reducing SQL errors directly caused by misunderstandings or inaccurate parsing, and providing higher transparency and explainability.

[0048] In addition, by introducing the SQL template solution, this application can greatly improve query accuracy and reduce the cost and complexity of data management with little or no management.

[0049] The flowchart of another specific implementation method of the method for converting a natural language question into an SQL statement provided by the present application is as follows: Figure 2 As shown, the method specifically includes: S201: receiving a query statement expressed in natural language input by a user.

[0050] S202: Determine multiple candidate query templates matching the query statement based on the semantic similarity between the query statement and multiple query templates in a database.

[0051] The query template is a predefined template used to match a predetermined query type.

[0052] Specifically, an embedding model can be used to process the query statement input by the user to generate a vector representation of the query statement.

[0053] 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 statement and the query template vector representation, and the N query templates that are most similar to the query statement are returned. The query templates are stored in the database and have a unique index or ID corresponding to them. The index is automatically generated when the template is added to the database and can be used for fast retrieval.

[0054] The embodiment of the present application improves the accuracy of natural language query to SQL conversion through the collaborative work of the embedding model and the recall model.

[0055] S203: Input the query statement and multiple candidate query templates into a large language model, and output a matching result that best matches the query statement as a matched query template.

[0056] The query statement and multiple candidate query templates are combined to form one or more complete query prompts, which are input into the large language model. It is understandable 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. By performing applicability checks and logical consistency checks on the query prompt, a unique selection result is output as the matched query template. Specifically, coverage, accuracy and other criteria can be used to select the best matching template. Among them, coverage is to select the template that most comprehensively covers the user's query intent, and accuracy is to select the template with the highest accuracy in semantic understanding and structured conversion.

[0057] Dynamically select and adjust query templates based on user queries and database content to enhance system flexibility.

[0058] S204: Determine whether the current indicator in the matched query template matches the previous indicator.

[0059] If not, the refusal process is entered and the idle chat indicator is output. The idle chat here refers to non-task-oriented conversation content, such as daily communication, casual questions, or conversations with no specific purpose.

[0060] If yes, proceed to S205.

[0061] In this step, if it is identified as a task-related query, the query conversion process will continue. If it is identified as a chat or non-task-related query, it may be transferred to a module dedicated to handling non-task-related conversations, such as entering the refusal to answer process.

[0062] S205: Determine a corresponding parsing template and a corresponding SQL template based on the matched query template.

[0063] S206: Based on the matched parsing template, parse the query statement to obtain a parsing result.

[0064] S207: Determine whether the parsing result shows that the value is empty or the key is missing.

[0065] If not, the data completion process is entered, and the step of receiving the query statement expressed in natural language input by the user is returned and re-executed.

[0066] If yes, proceed to S208.

[0067] This step is used to determine whether the parsing result is correct. If the parsing result shows that the value is empty or the key is missing, it means that the data is incomplete and needs to be completed. Then enter the data completion process and return to the step of receiving the query statement expressed in natural language input by the user and re-execute it. If the parsing result is correct, further execute the subsequent step of generating SQL statements.

[0068] S208: Fill the query variables in the parsing result into the SQL template to generate an SQL query statement.

[0069] 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.

[0070] The present application enhances flexibility and adaptability and improves the accuracy of natural language query to SQL conversion by matching query statements with predefined query modules, then filtering out matching query templates, and further dynamically selecting and adjusting parsing templates and SQL templates. The predefined query modules have business logic, which improves the interpretability of natural language query to SQL conversion.

[0071] 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, a user inputs a query statement (Query): "What was the total electricity consumption in the country in the second half of last year?".

[0072] The predefined query module can be: "What is the total electricity consumption in the country at {time_val}?" Here, {time_val} is a placeholder that is 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: {'target_measure':'Total electricity consumption in China','filters':[{'field':'yers_m','operator':'=','variable':'{time_val}'}]} Among them, 'target_measure': 'National total electricity consumption' indicates that the query target is to obtain the total national electricity consumption. 'filters': The list contains the filter conditions, that is, the time range, which is defined by the field yers_m, the operator =, and the variable {time_val}.

[0073] The parsing results can be: {'target_measure':'Total electricity consumption in China','filters':[{'field':'yers_m','operator':'=','variable':'The second half of last year'}]} The variable here has been replaced by the specific time description "the second half of last year".

[0074] The SQL template is used to generate the final SQL query template, which is filled based on the information in the parsing results. The SQL template is: select sum(ifnull(usg, 0)) as total from ld_ent_elec_consume_viewwhere yers_m = '{time_val}' ifnull(usg, 0) ensures that calculations can be performed even if some data is null, using 0 instead of null.

[0075] The final generated SQL statement is dynamically generated based on the parsing results and filled with specific time information.

[0076] The SQL statement is: select sum(ifnull(usg, 0)) as total from ld_ent_elec_consume_viewwhere year(str_to_date(concat(yers_m, '-01'), '%Y-%m-%d')) = year(now())-1and month(str_to_date(concat(yers_m, '-01'), '%Y-%m-%d'))>= 7; Here, the str_to_date and concat functions are used to convert the yers_m field (year-month format) into a date, and then the year and month are extracted by the year() and month() functions to match the time range of "the second half of last year".

[0077] It is understandable that the field values ​​in the SQL template are only the results of parsing in the input, and sometimes need to be appropriately converted when converted to SQL expressions.

[0078] In addition, the present application also provides a device for converting natural language questions into SQL statements, such as Figure 3 As shown in the structural block diagram of the device for converting natural language questions into SQL statements provided in the present application, the device specifically includes a memory 31 and a processor 32, and the memory 31 stores a computer program. When the computer program is executed by the processor 32, it implements the method for converting natural language questions into SQL statements according to any one of the above-mentioned methods.

[0079] In addition, the present application also provides a computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method for converting a natural language question into an SQL statement according to any one of the above methods is implemented.

[0080] Computer readable storage media include permanent and non-permanent, removable and non-removable media that can be implemented by any method or technology to store information. 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 technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic tape magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0081] The professionals should also be further aware that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented with electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of this application.

[0082] The steps of the method or algorithm described in conjunction with the embodiments disclosed herein may be implemented using hardware, a software module executed by a processor, or a combination of the two. The software module may be placed in a random access memory (RAM), a memory, a read-only memory (ROM), an electrically programmable ROM, an electrically erasable programmable ROM, a register, a hard disk, a removable disk, a CD-ROM, or any other form of storage medium known in the art.

[0083] The specific implementation methods described above further illustrate the purpose, technical solutions and beneficial effects of the present application in detail. It should be understood that the above description is only the specific implementation method of the present application and is not intended to limit the scope of protection of the present application. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present application should be included in the scope of protection of the present application.

Claims

1. A method for converting natural language questions into SQL statements, characterized in that: include: Receiving a query statement expressed in natural language input by a user; Determining multiple candidate query templates matching 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, determine a corresponding parsing template and a corresponding SQL template, wherein the parsing template is a predefined template for extracting query variables from a query statement, and the SQL template is a predefined template for SQL form expression for a predetermined query type; Based on the matched parsing template, parse the query statement to obtain a parsing result; Fill the query variables in the parsing result into the SQL template to generate an SQL query statement.

2. The method for converting natural language questions into SQL statements according to claim 1, characterized in that: The pre-definition process of the query template includes: Collect query examples obtained from multiple information sources; identifying a plurality of 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 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 of 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.

4. The method for converting natural language questions into SQL statements according to claim 3, 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: Using an embedded vector model to determine a vector representation of the query statement; 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.

5. The method for converting a natural language question into an SQL statement according to any one of claims 1 to 4, characterized in that: The query statement and multiple candidate query templates are input into the large language model, and a matching result that best matches the query statement is output, where the matched query templates include: Combining the query statement and a plurality of candidate query templates to generate one or more query prompt words; Perform applicability and logical consistency checks on the generated query hints; The checked query prompt words are input into the large language model, and the matching result that best matches the query statement is output as the matched query template.

6. The method for converting natural language questions into SQL statements according to claim 5, characterized in that: After inputting the query statement and a plurality of 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 refusal process is entered and the chat indicator is output.

7. The method for converting natural language questions into SQL statements according to claim 6, characterized in that: After parsing the query statement to obtain a parsing result based on the matched parsing template, the method further includes: Determine whether the parsing result has empty value or missing key; If not, the data completion process is entered, and the step of receiving the query statement expressed in natural language input by the user is returned and re-executed.

8. The method for converting natural language questions into SQL statements according to claim 6, 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.

9. 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 8 is implemented.

10. 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 8 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

  • Network operation and maintenance analysis method and device and storage medium

    CN118916618A

  • Natural language to query language transformation

    EP4428710A1

Cited By

  • Query statement conversion method and device based on large model, equipment and medium

    CN120578683A

  • Intelligent number asking method and device based on natural language, intelligent agent and electronic equipment

    CN120723799A

  • Intelligent agent-based statement conversion method and device, electronic equipment and storage medium

    CN120804302A