Structured query statement generation method and system
Through the large language model, user question keywords are extracted and matched with the database, user questions are rewritten and reconstructed, the minimum link table and the most relevant examples are built, and accurate structured query statements are generated. This solves the accuracy problem of NL2SQL technology when facing ambiguous questions and complex queries, and improves the adaptability and reliability of the generation method.
Patent Information
- Application Number
- CN202511298735.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-09-11
- Publication Date
- 2025-10-14
AI Technical Summary
The accuracy of structured query statements generated by existing NL2SQL technology is significantly reduced when faced with ambiguous questions, complex queries, and updating database schemas.
The large language model is used to extract keywords from user questions, match them with database information, determine target keywords, rewrite and reconstruct user questions, build a minimum link table and the most relevant examples, generate context engineering, obtain prompt words and generate structured query statements.
The accuracy and adaptability of structured query statement generation are improved, the understanding deviation caused by the mismatch between natural language and database terminology is reduced, and the reliability of the method in processing diverse natural language queries is enhanced.
Smart Images

Figure CN120780730A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, and particularly relates to a structured query statement generation method and system. BACKGROUND
[0002] NL2SQL (Natural Language to SQL) is a natural language processing technology that aims to convert natural language queries into structured query statements. This technology enables users to interact with databases using natural language without having to write complex query code. However, NL2SQL technology still has deficiencies in dealing with question ambiguity, complex queries, and updating database schema, which significantly reduces the accuracy of the generated structured query statements. SUMMARY
[0003] In order to solve the deficiencies of the prior art, the present application adopts the following technical solutions: In a first aspect, the present application provides a structured query statement generation method, which includes the following steps: Obtain a user question, extract at least one keyword in the user question through a large language model, match each keyword with information in a database, calculate the similarity of each keyword with the information in the database, and select the information with the highest similarity as the target keyword corresponding to each keyword; Based on the target keyword, rewrite and reconstruct the user question, if it is confirmed that the rewritten and reconstructed user question is correct, perform a search in the database based on the rewritten and reconstructed user question, and construct a minimal link table and a most relevant example according to the search result; Based on the large language model, perform component recognition on the user question to obtain an initial structured statement about the user question, optimize the initial structured statement based on the rewritten and reconstructed user question, and obtain an optimized structured statement; Based on the minimal link table, the most relevant example, and the optimized structured statement, construct a context engineering, obtain a prompt word through the context engineering, input the prompt word into the large language model, generate a pre-generated structured query statement, and execute the pre-generated structured query statement in the database to obtain an answer to the user question.
[0004] In summary, the application provides a structured query statement generation method. The method extracts keywords in the user question through a large language model and matches them with database data to determine target keywords. Based on the target keywords, the user question is rewritten and reconstructed. Through precise matching of keywords and rewriting of user questions, the understanding deviation caused by mismatch between natural language and database terminology is avoided. Based on the rewritten and reconstructed user question, a context engineering is constructed through the minimum link table, the most relevant example and the structured statement, providing a reference basis for the generation of query statements that fits the database structure. This helps the large language model to more accurately grasp the logic and expression of the user question, accurately captures and translates the user's query intent, improves the accuracy of structured query statement generation, and enhances the adaptability and reliability of the method in processing diversified natural language queries.
[0005] Further, the method further comprises: If the rewriting is confirmed to be correct, scene-based business retrieval is performed based on the rewritten and reconstructed user question, and the similarity between the rewritten and reconstructed user question and the structured query statement in the database is calculated. When there is a structured query statement with a similarity greater than a preset statement threshold to the rewritten and reconstructed user question, the structured query statement with the maximum preset statement threshold is selected, and the structured query statement is executed in the database to obtain the answer to the user question. When there is no structured query statement with a similarity greater than a preset statement threshold to the rewritten and reconstructed user question, retrieval is performed in the database based on the rewritten and reconstructed user question, and a minimum link table and a most relevant example are constructed according to the retrieval result.
[0006] Further, the scene-based business retrieval comprises: The structured query statements in the database are subjected to interface solidification processing, and the rewritten and reconstructed user question is subjected to vectorization processing. The similarity between the vectorized user question and the interface solidified structured query statement is calculated.
[0007] Further, the method further comprises: A verification model is constructed to verify the answer obtained through scene-based business retrieval. The answer and the rewritten and reconstructed user question are input into the verification model. The verification model performs self-reflection and counter-question verification according to a preset number of rounds to determine whether the answer is correct. If the determination result is correct, the answer is output. If the determination result is incorrect, the user is prompted to supplement the question information. The keywords are extracted again in combination with the user question and the supplemented question information.
[0008] Further, the method further comprises: vectorizing the restructured user question, performing similarity calculation on the vectorized user question and description information of each table in the database, and selecting a table with a similarity greater than a preset table threshold as a selected table; Based on the selected table, the similarity calculation is performed on the vectorized user question and the description information of each column in the selected table, and the column with a similarity greater than a preset column threshold is selected as a selected column; Based on the foreign key information of the selected table, the data definition language information and the data of the selected column, the minimum link table is generated.
[0009] Further, the method further comprises: constructing the prompt word through a prompt word layout and an example sample method, the prompt word containing one or more of the following: operation instructions, the minimum link table, the foreign key information of the selected table, the field of the selected table, the optimized structured statement and the most relevant example; The prompt word is input into the large language model, and the large language model generates the pre-generated structured query language based on the content contained in the prompt word.
[0010] Further, the method further comprises: Constructing key-value pair data, the key-value pair data taking a past user question as a key and a structured query statement corresponding to the past user question as a value; Based on the large language model, the restructured user question and the past user question in the key-value pair data are masked; The similarity calculation is performed on the masked user question and the past user question in the masked key-value pair data, and the key-value pair data with a similarity greater than a prediction example threshold is selected as the most relevant example.
[0011] Further, the method further comprises: Constructing a verification model to verify an answer obtained by executing the pre-generated structured query statement, inputting the answer and the restructured user question into the verification model, and the verification model performing self-reflection and counter-question verification to determine whether the answer is correct; If the determination result is correct, the answer is output; If the determination result is incorrect, corresponding error information is generated, the prompt word is updated based on the error information and the pre-generated structured query statement, and the pre-generated structured query statement is reacquired.
[0012] Further, the method further comprises: The component recognition is performed on the user question by the large language model, and the initial structured statement is obtained, the initial structured statement including one or more of the following recognition results: query object, query condition, and aggregation operation component. The one or more recognition results in the initial structured statement are replaced based on the rewritten and reconstructed user question, and the optimized structured statement is obtained.
[0013] In a second aspect, the present application further provides a structured query statement generation system, which applies the above structured query statement generation method. BRIEF DESCRIPTION OF DRAWINGS
[0014] Figure 1 A step flowchart of the structured query statement generation method provided by an embodiment of the present application is provided. Figure 2 A step flowchart of generating a minimum link table in the structured query statement generation method provided by an embodiment of the present application is provided. Figure 3 A step flowchart of generating a most relevant example in the structured query statement generation method provided by an embodiment of the present application is provided. Figure 4 A step flowchart of component recognition on a user question in the structured query statement generation method provided by an embodiment of the present application is provided. Figure 5 A flowchart of the structured query statement generation system provided by an embodiment of the present application is provided. DETAILED DESCRIPTION
[0015] The present application will be described in detail below with reference to the specific embodiments shown in the accompanying drawings, but these embodiments do not limit the present application, and the structural, method, or functional changes made by those of ordinary skill in the art based on these embodiments are included in the protection scope of the present application.
[0016] To solve the problems in the prior art, in a first aspect, an embodiment of the present application provides a structured query statement generation method, as shown in Figure 1 The generation method includes the following steps: In step S11, a user question is obtained, at least one keyword in the user question is extracted by a large language model, each keyword is matched with information in a database, the similarity of each keyword and the information in the database is calculated, and the information with the highest similarity to each keyword is selected as the target keyword corresponding to each keyword.
[0017] Step S12, based on the target keyword, the user question is restructured, if the restructured user question is correct, the restructured user question is searched in the database, and the minimum link table and the most relevant example are constructed according to the search result.
[0018] Step S13, based on the large language model, the user question is component recognized, the initial structured sentence about the user question is obtained, and the initial structured sentence is optimized based on the restructured user question, and the optimized structured sentence is obtained.
[0019] Step S14, based on the minimum link table, the most relevant example and the optimized structured sentence, the context engineering is constructed, the prompt word is obtained through the context engineering, the prompt word is input into the large language model, the pre-generated structured query sentence is generated, and the pre-generated structured query sentence is executed in the database to obtain the answer of the user question.
[0020] Specifically, the user input question is obtained, the user question is usually in the form of natural language in daily communication, for example, "query the product sales in the first quarter of this year". With the help of the large language model, the keywords of the user question are extracted, the large language model can identify the words carrying the user query intention in the question based on the understanding ability of language, and define the words as the keywords of the user question. For example, in the above example, multiple keywords such as "the first quarter of this year" and "product sales" can be extracted.
[0021] The keywords are matched with the information in the database, and a large amount of structured data is stored in the database, including various tables, columns and specific field values, etc., for example, there may be "2024 first quarter" and "commodity sales amount" data. In the matching process, based on semantic association and expression consistency, the similarity of the extracted keywords and the information in the database is calculated, for example, the similarity of "the first quarter of this year" and "2024 first quarter" in time direction and expression is calculated. Through similarity calculation, the information with the highest similarity with the extracted keywords is selected from the database, which is determined as the target keyword.
[0022] It should be noted that there may be multiple keywords for a user question, each keyword is matched with the information in the database, the similarity of each keyword and the information in the database is calculated, and the information with the highest similarity with each keyword is selected as the target keyword of the corresponding keyword.
[0023] The target keyword can realize the accurate docking of the user question and the database data, ensure that the subsequent query and sentence generation are more consistent with the actual structure and content of the database, and avoid the situation that the keywords in the user question may not be expressed in a standard manner and inconsistent with the database terminology.
[0024] The target keyword is used to restructure the user question, replacing part of the keywords in the user question with the target keyword, so that the rewritten user question is more consistent with the database. To further illustrate the target keyword provided by the embodiments of the present application, the following provides an example of restructuring the user question: user question: the turnover of the business hall last month; keyword extraction: {{'original question keywords': 'business hall', 'target keyword': 'business hall (west lake store)','similarity data between the two': 0.9}}; rewritten user question: the turnover of the business hall (west lake store) last month.
[0025] After the rewriting and reconstruction is completed, the rewritten question is confirmed. If the rewritten question does not match the original user question, the process returns to step S11 to re-extract keywords from the user question. When re-extracting keywords, the focus of keyword extraction is adjusted based on the error reason in the previous round to avoid repeated rewriting errors. Through the above-mentioned cyclic adjustment mechanism, the deviation in question processing is effectively corrected, and the subsequent steps are ensured to be based on the correct user intent.
[0026] When it is confirmed that the rewritten and reconstructed user question is correct, information retrieval is performed in the database based on the rewritten user question. The retrieval process searches for related tables, columns, and data records in the database according to core elements in the user question, such as query objects and time ranges. Based on the retrieval results, a minimum link table and a most relevant example are constructed. The minimum link table is the most concise set of database tables that meet the current query requirements. The minimum link table is formed by filtering the tables and columns in the database that have the highest relevance to the query content, and integrating the association relationships (such as foreign key associations) between these tables. The minimum link table provides a clear table structure reference for subsequent query statement generation, avoiding the introduction of irrelevant tables that lead to statement redundancy or errors.
[0027] The most relevant example is a query case selected from the database information that is most similar to the rewritten user question in terms of query intent and structure. The most relevant example can provide a reference template for subsequent structured query statement generation, helping the large language model to more accurately grasp the logic and format of structured query statement generation. By constructing the minimum link table and the most relevant example, a reference basis is provided for subsequent steps, improving the accuracy of structured query statement generation.
[0028] The large language model identifies components in the user question. The large language model identifies components in the initial user question based on understanding of natural language and structured query logic, including query objects, query conditions, aggregation operations, and other key elements in the user question. After identifying the above components, the structured query statement is constructed in combination with the target keyword. The structured query statement can clearly and normatively present the core elements of the query and their relationships. Further, the optimized structured query statement is obtained by giving the rewritten user question.
[0029] The structured query statement converts the user's natural language query intent into a form closer to the logic of the structured query statement, providing an explicit framework and element guide for generating a specific structured query statement, so that the large language model can accurately grasp each key part of the query when generating the statement.
[0030] Based on the minimum link table, the most relevant example, and the structured query statement, a context engineering is constructed. The context engineering organically integrates the above information, fuses the table structure and associated information provided by the minimum link table, the reference cases provided by the most relevant example, and the query element framework provided by the structured query statement to form a complete and clear context, thereby obtaining a prompt word for the large language model. The prompt word contains information required for generating a structured query statement, such as tables and columns to be queried, query conditions, reference examples, table foreign key information, and fields.
[0031] The prompt word is input into the large language model, which can output a pre-generated structured query statement based on the information in the prompt word. The database executes the pre-generated structured query statement, filters and calculates relevant data in the corresponding table according to the instructions in the statement, and finally obtains the answer to the user question. Optionally, the temperature parameter of the large language model can also be set to zero in the embodiment of the present application to improve the accuracy of the pre-generated structured query statement.
[0032] According to the above description, the method for generating a structured query statement provided by the embodiment of the present application extracts key words in the user question and matches them with database data to determine target keywords, and rewrites and reconstructs the user question based on the target keywords. Through the accurate matching of keywords and the rewriting mechanism of the user question, the understanding deviation caused by the mismatch between natural language and database terminology is avoided. Based on the rewritten and reconstructed user question, a context engineering is constructed by using the minimum link table, the most relevant example, and the structured query statement, which provides a reference basis for the generation of the query statement that conforms to the database structure, helps the large language model to more accurately grasp the logic and expression of the user question, accurately captures and converts the user query intent, improves the accuracy of the structured query statement generation, and enhances the adaptability and reliability of the method in processing diversified natural language queries.
[0033] As an optional implementation, after the rewriting reconstruction of the user question is completed, the generation method further includes: If it is confirmed that the rewriting is correct, scene-based business retrieval is performed based on the rewritten and reconstructed user question, and similarity of the rewritten and reconstructed user question and structured query statements in a database is calculated.
[0034] When there is a structured query statement with similarity greater than a preset statement threshold to the rewritten and reconstructed user question, the structured query statement with the greatest preset statement threshold is selected, and the structured query statement is executed in the database to obtain an answer to the user question.
[0035] When there is no structured query statement with similarity greater than the preset statement threshold to the rewritten and reconstructed user question, retrieval is performed in the database based on the rewritten and reconstructed user question, and a minimum link table and a most relevant example are constructed according to a retrieval result.
[0036] Specifically, after it is confirmed that the rewritten and reconstructed question is correct, it is indicated that the rewritten user question can accurately reflect the query intention of the user and conforms to the expression specification of the database, and scene-based business retrieval is performed at this time. A large number of historical or commonly used structured query statements are stored in the database, and these statements are written for specific business requirements, have a standard format, and have clear query logic. Scene-based business retrieval analyzes and retrieves the rewritten question in the database, and similarity of the rewritten and reconstructed user question and existing structured query statements in the database is calculated in the scene-based business retrieval process.
[0037] When the similarity is calculated, comparison can be performed from multiple dimensions such as semantics, structure, and query elements, for example, whether the query object, condition, and operation involved in the rewritten question match the existing structured query statements in the database and how much the match is, so as to determine whether there is a structured query statement in the database that can meet the current query requirement.
[0038] A preset statement threshold is set, which is determined according to business requirements and query accuracy requirements. The structured query statements similar enough to the user question are filtered out through the preset statement threshold, so as to ensure that the selected statements can accurately meet the query requirements of the user. According to the scenario-based business retrieval result, if it is found that there is a structured query statement in the database that is similar to the user question after rewriting and reconstruction and the similarity is greater than the preset statement threshold, the structured query statement with the greatest similarity is selected. After the selected statement is determined, the structured query statement is executed in the database, and the database performs data retrieval, filtering, calculation and other operations according to the instructions in the statement, and finally returns the answer corresponding to the user question. Through the above processing mode, the existing resources in the database are fully utilized, and the step of generating a structured query statement is reduced, thereby saving time and computing resources, improving the response speed, and being suitable for common or repeated query scenarios.
[0039] Further, when it is found that there is no structured query statement in the database that is similar to the user question after rewriting and reconstruction and the similarity is greater than the preset statement threshold, it indicates that the existing structured query statement cannot directly meet the current query requirement. At this time, the database is searched again based on the user question after rewriting and reconstruction. Based on the rewritten user question, various types of data information related to the question are comprehensively searched, including the database tables involved, the fields in the tables, the association relationship between the tables and the like. A minimum link table is constructed based on the search result. Moreover, the most relevant example closest to the current question in terms of query intention, structure or business scenario is selected from the historical query cases of the database, and the most relevant example is constructed. The most relevant example can provide a reference in terms of logic and format for generating a new structured query statement, and help to improve the accuracy and efficiency of subsequent statement generation.
[0040] Further, as an optional implementation manner, in the calculation of the similarity between the user question after rewriting and reconstruction and the structured query statement in the database, the structured query statements in the database are subjected to interface solidification processing, and the user question after rewriting and reconstruction is subjected to vectorization processing. The similarity is calculated based on the user question after vectorization processing and the structured query statement after interface solidification processing.
[0041] Specifically, the structured query statements in the database are subjected to interface solidification processing. The existing various structured query statements in the database are standardized and modified to form a unified interface form, so that each structured query statement follows fixed syntax specifications and expression forms, and the interference on the similarity calculation is eliminated. Moreover, the related business description and interface calling mode of the interface are stored in the database, so as to be subjected to similarity calculation and interface calling.
[0042] The user question after rewriting and reconstruction is vectorized, and the user question in natural language form is converted into a numerical vector that can be calculated and processed by a computer. The description information of the structured query statement after the interface solidification processing is obtained, and the vectorized user question and the description information are calculated for similarity. Through the above-mentioned similarity calculation, it can be effectively judged whether there is a structured query statement with a similarity to the user question exceeding a preset statement threshold, thereby providing a reliable basis for subsequent query execution or statement generation.
[0043] Further, as an optional implementation, in the generation method provided by the embodiments of the present application, the answer obtained through the scenario-based business retrieval further includes: A verification model is constructed to verify the answer obtained through the scenario-based business retrieval. The answer and the rewritten and reconstructed user question are input into the verification model. The verification model performs self-reflection and counter-question verification according to a preset number of rounds to determine whether the answer is correct.
[0044] If the determination result is correct, the answer is output; if the determination result is incorrect, the user is prompted to supplement the question information, and the key words are extracted again in combination with the user question and the supplemented question information.
[0045] Specifically, the verification model is constructed. The verification model is based on the understanding and reasoning ability of a large language model and is trained to adapt to the verification scene of the structured query result. The verification model can identify verification points such as logical consistency and data correlation between the answer and the user question.
[0046] In the specific verification, the answer and the rewritten and reconstructed user question are input into the verification model. The verification model reviews the core demand of the user question (such as the query object, the condition, the expected operation, etc.) by itself, and compares the answer with the demand one by one to determine whether the answer completely covers the core demand of the user, whether the data is within the condition range defined by the user, and whether the potential logical requirements (such as the correctness of the aggregation operation) are met. For example, if the user question is "query the total sales of mobile phones in each region in the first quarter of 2024", the verification model reflects whether the answer contains the elements "the first quarter of 2024", "each region", "mobile phone sales", "total", and whether the data is accurate.
[0047] Further, the verification model also asks questions in reverse for the answer, such as "does this answer omit the data of a certain region?" and "does the calculation of the total cover the entire quarter?", so as to check possible omissions or errors. Through the combination of self-reflection and counter-question verification, the verification model verifies the accuracy of the answer obtained through the scenario-based business retrieval, ensures that the output answer can accurately match the query intention of the user, and avoids result deviation caused by errors in scenario-based business retrieval.
[0048] When the verification model determines the answer is correct after multiple rounds of verification, it indicates that the answer accurately matches the user's query intent and meets the logical requirements of the scenario-based business, and the answer is output. If the verification model determines the answer is incorrect, the information supplement mechanism is activated, prompting the user to provide additional information about the question, usually details related to the query. After obtaining the user's additional information, the original user question is combined with the additional information and keyword extraction is performed again. In this way, user feedback is used to fill in information gaps, optimize the accuracy of keyword extraction, and thus generate answers that better meet user needs.
[0049] As an optional implementation, such as Figure 2 As shown, in step S12, the minimum link table can be generated by the following steps: Step S121 : vectorize the rewritten and reconstructed user question, calculate the similarity between the vectorized user question and the description information of each table in the database, and select the table with a similarity greater than a preset form threshold as the selected table.
[0050] Step S122 : Based on the selected table, similarity calculation is performed between the vectorized user question and the description information of each column in the selected table, and the columns with similarity greater than a preset column threshold are selected as the selected columns.
[0051] Step S123 : generating a minimum link table based on the foreign key information, data definition language information and data of the selected columns of the selected table.
[0052] Specifically, the rewritten and reconstructed user questions are vectorized, converting the natural language user questions into computer-processable numerical vectors to facilitate subsequent similarity calculations. Each table in the database has a corresponding description, which typically includes the table's business meaning, the type of data stored, and its primary purpose. For example, the description of an "order table" might read "Stores customer order information, including order number, order time, customer ID, etc." A similarity calculation is performed between the vectorized user questions and the description of each table in the database to determine whether the table is relevant to the user's query.
[0053] Set a preset form threshold, which is pre-determined based on business needs and query accuracy. When the calculated similarity exceeds the preset form threshold, the corresponding table can provide data support for the query in question and is selected as the selected table. This method can quickly eliminate irrelevant tables from the numerous tables in the database, retaining candidate tables that may participate in the query, reducing subsequent processing workload and improving overall efficiency.
[0054] Furthermore, each column in the selected table has its own specific descriptive information, which generally includes the meaning of the column, data type, business indicators represented, etc. Based on the obtained selected table, the similarity between the vectorized user questions and the descriptive information of each column in each selected table is calculated. A preset column threshold is set. When the similarity between the descriptive information of a column and the user question is greater than the preset column threshold, it means that the column contains relevant data for the user query and is selected as the selected column. Through the above steps, on the basis of the filtered selected tables, the columns related to the user questions are further accurately filtered to ensure that the subsequent constructed link table avoids interference from redundant information.
[0055] Based on the selected tables, by analyzing the foreign key information between them, the business logic of each table is clarified, and the connection paths between the tables are determined to ensure that the relevant data in different tables can be correctly linked and integrated. The data definition language information of the selected tables includes the table structure definition, such as primary key, data type, and constraints. The data definition language information can help clarify the structural characteristics and data rules of the selected tables. Combined with the data definition language information of the selected tables, it ensures that the database design specifications are followed when constructing the minimum link table, avoiding data association errors caused by structural mismatches.
[0056] Furthermore, the data in the selected columns, which contain the specific data content the user is searching for, is integrated. By integrating the association information and structure information of the selected tables with the selected column data, a minimum linked table that meets the query requirements is formed. This minimum linked table, which includes the selected tables and columns relevant to the query, satisfies the user's query data needs and provides a clear and accurate data structure reference for the subsequent generation of structured query statements, improving query accuracy and execution efficiency.
[0057] As an optional implementation, such as Figure 3 As shown, in step S12, the most relevant examples can be generated by the following steps: Step S124 , constructing key-value pair data, wherein the key-value pair data uses previous user questions as keys and structured query statements corresponding to the previous user questions as values.
[0058] Step S125 : Mask the rewritten and reconstructed user questions and previous user questions in the key-value pair data based on the large language model.
[0059] Step S126 , performing similarity calculation on the masked user question and the previous user questions in the masked key-value pair data, and selecting the key-value pair data with a similarity greater than a prediction example threshold as the most relevant example.
[0060] Specifically, the valid query data generated in the past user interaction with the system is standardized and sorted, and the "past user question" refers to various natural language queries raised by the past user to the system, which may involve different business scenarios, query intentions and expression methods; the "structured query statement corresponding to the past user question" is an effective statement generated for the past user question, and based on the statement, the answer corresponding to the past user question can be found in the database environment.
[0061] The past user question is taken as a "key", and the structured query statement corresponding to the past user question is taken as a "value" to form a key-value pair data, and a direct association between the question and the query statement is established, so as to facilitate subsequent quick retrieval of historical cases similar to the current question. By constructing the key-value pair data, rich historical interaction experience can be accumulated, laying a data foundation for subsequent accurate identification of the most relevant examples.
[0062] Based on the large language model, the rewritten and reconstructed user question and the past user question in the key-value pair data are masked. The large language model can accurately identify the entity components (such as time, place, object, etc.) in the question based on the understanding of natural language, strip the specific entity information (such as specific names, numerical values, etc.) in the question, and retain the semantic integrity and structural rationality of the masked question. Through the masking process, the system can focus more on the essential features of the question, providing a more reliable basis for subsequent similarity calculation.
[0063] After completing the masking process, the masked user question and the past user question in the masked key-value pair data are subjected to similarity calculation. A preset example threshold is set, and when the similarity between the masked past user question in the key-value pair data and the masked current user question is greater than the preset example threshold, it means that the two are highly matched in core structure and query intention, and the key-value pair data with the largest similarity is selected as the most relevant example. The most relevant example contains a query scenario similar to the current question and the corresponding effective answer, which can provide an intuitive reference template for generating a structured query statement, helping the large language model more accurately grasp the logic and expression method of the current question, thereby improving the accuracy of the generated result.
[0064] As an optional implementation manner, as shown in Figure 4 obtaining the structured statement about the user question in step 13 further includes the following steps: In step S131, the large language model is used to identify the components of the user question to obtain an initial structured statement, and the initial structured statement includes one or more of the following identification results: query object, query condition, and aggregation operation component.
[0065] Step S132, based on the rewritten and reconstructed user question, replacing one or more recognition results in the initial structured statement to obtain an optimized structured statement.
[0066] Specifically, the large language model comprehensively analyzes the user question based on the understanding ability of natural language and the cognition of structured query logic. Through deep analysis of the user question by the large language model, the following recognition results can be obtained from the user question: query object, query condition, and aggregation operation component.
[0067] Among them, the query object refers to the data subject that the user wants to query, that is, the data item that the user is concerned about, for example, in the question "What is the total sales of mobile phones in each region in 2024?", "mobile phone sales" is the query object, and the query object directly points to the specific data field that needs to be extracted in the database. The query condition is the restriction or constraint set by the user for the query result, usually including time, place, range and other elements, for example, "2024" and "each region" in the above question belong to the query condition, which limits the time range and regional range of the search, ensuring the pertinence of the query result. The aggregation operation component is the statistical or calculation operation that the user wants to perform on the query result, such as sum, average, count, etc., and "total" in the above example is the aggregation operation component, which determines the processing method of the query object data.
[0068] Further, based on the recognition results (query object, query condition, and aggregation operation component), an initial structured statement about the user question is constructed, which includes one or more of the following recognition results. Based on the rewritten and reconstructed user question, component replacement is performed on the initial structured statement, which includes: according to the actual structure of the database and the query requirements, replacing the target keyword with the corresponding recognition result to ensure that the replaced and optimized structured statement can correspond to the table, column or operation logic in the database.
[0069] In order to further illustrate the structured statement provided by the embodiments of the present application, the following provides an example of component recognition for a user question: user question: sales of the business hall last month; initial structured statement: {{"query object": ["sales"], "query condition": ["business hall", "last month"], "aggregation operation": none}; target keyword replacement: { 'original question keyword': 'business hall','matched database keyword': 'business hall (west lake store)','similarity data between the two': 0.9}; rewritten and reconstructed user question: sales of business hall (west lake store) last month; optimized structured statement: { "query object": ["business hall (west lake store)"], "query condition": ["business hall", "last month"], "aggregation operation": none}.
[0070] Through component replacement, the structured statement presents the core elements of the user query in a clear and standardized manner, such as "Query object: mobile phone sales quantity; query condition: time from April to June 2024, divided by provinces; aggregation operation: SUM (total)". The formation of the structured statement realizes the precise docking of the user query intention and the database query logic, retains the core information of the user's question, meets the syntax and logic requirements of structured queries, and effectively improves the efficiency and accuracy of subsequent steps.
[0071] As an optional implementation manner, the method further comprises: in step S14, constructing the prompt word through a prompt word layout and an example sample method, the prompt word containing one or more of the following: operation instruction, minimum link table, foreign key information of the selected table, field of the selected table, optimized structured statement, and most relevant example.
[0072] The prompt word is input into the large language model, and the large language model generates a pre-generated structured query language based on the content contained in the prompt word.
[0073] Specifically, the prompt word layout can include target, guide, answer output and the like, the target is the task that the large language model needs to complete, the guide is the rule that the large language model needs to follow when constructing the answer, and the answer output is the output result of the large language model. The format of the prompt word is planned based on the prompt word layout, the query related information is classified and integrated, and a logically coherent input instruction is formed to clearly define the task target of the large language model, so that the large language model can clearly understand the work to be done, so that the large language model can accurately understand the query requirement. For further illustration, a related example of a prompt word based on a prompt word layout is provided as follows: Prompt word: {Target: (According to the provided database structure and problem, construct an accurate structured query statement. Please use the provided database structure, problem and related information to generate an accurate structured query statement. When processing, do not split the fields matching the database keywords. For fields that are numerical, date or query conditions, please use them directly. Please pay special attention to the fact that if the value in the query condition matches the data in the database, the specific table name and column name must be used in the final structured query statement.) Guide: (Use database structure: According to the provided database structure information, identify the correct table and column. Maintain field integrity: Do not split fields corresponding to database keywords. Match components and table columns: If the value in the query condition matches the value of a column in a table in the database, the table and column must be used in SQL for condition filtering.) Answer output:}.
[0074] And, the introduction of historical cases similar to the current query problem using the example sample method can provide a direct reference template for the large language model. The large language model can generate a structured query statement conforming to the current user problem by analyzing the construction logic of the structured query statement in the example sample, thereby improving the accuracy of the large language model.
[0075] Through the combination of the prompt word layout and the example sample method, the constructed prompt word can fully integrate various types of information to provide accurate and comprehensive guidance for the large language model, so that the pre-generated structured query statement generated by the large language model more accurately reflects the user's intention, and improves the quality and efficiency of the structured query statement generation.
[0076] Further, the prompt word includes operation instructions, a minimum link table, foreign key information of a selected table, fields of the selected table, an optimized structured statement, and the most relevant example. Among them, the operation instructions can explicitly indicate the task that the large language model needs to complete, such as “generate a structured query statement conforming to the database syntax based on the following information to obtain the answer to the user's question”, to ensure that the large language model clearly understands the problem target; the minimum link table provides a data structure basis for the large language model and clearly indicates the table range of the query; the foreign key information of the selected table is used to indicate the associated fields between different tables to ensure that the query can obtain associated data across tables; the fields of the selected table are the specific column information related to the query to avoid irrelevant fields referenced by the large language model; the optimized structured statement is a standardized expression of the user's question (such as query object, condition, and aggregation operation), which provides a logical framework for the large language model to ensure that the generated statement can cover the core needs of the user; and the most relevant example helps the large language model to imitate the statement generation logic and format through the demonstration of the example.
[0077] The contents included in the prompt word collectively constitute a context environment related to the user's question, ensuring that the large language model can fully understand the query background, data structure, and user's intention, thereby improving the accuracy of the pre-generated structured query statement. The constructed prompt word is input into the large language model, and based on the various types of information included in the prompt word, the large language model can convert the query demand at the natural language level into a structured query statement understandable and executable by the database through comprehensive processing of the various types of information in the prompt word, thereby generating a pre-generated structured query statement. The pre-generated structured query statement is executed in the database environment, thereby obtaining an answer to the user's question.
[0078] Further, as an optional implementation manner, in the generation method provided by the embodiments of the present application, the obtained answer further includes: The verification model is constructed to verify the answer obtained by executing the pre-generated structured query statement. The answer and the rewritten and reconstructed user question are input into the verification model. The verification model performs self-reflection and counter-checking to determine whether the answer is correct.
[0079] If the determination result is correct, the answer is output. If the determination result is incorrect, corresponding error information is generated. The prompt word is updated based on the error information and the pre-generated structured query statement. The pre-generated structured query statement is regenerated.
[0080] The description of the verification model has been described above and will not be repeated here. When the verification model determines that the answer is correct, it means that the pre-generated structured query statement can accurately reflect the user's query intention, and the database execution result meets the expectation. The answer is directly output. If the verification model determines that the answer is incorrect, the error correction mechanism is started. The verification model generates corresponding error information and updates the prompt word based on the original pre-generated structured query statement. Based on the updated prompt word, the large language model is guided to correct the error specifically, and the pre-generated structured query statement is reacquired. The reacquired pre-generated structured query statement is executed in the database again, and the verification process is repeated until the generated answer passes the verification of the verification model.
[0081] Through the above correction mechanism based on the verification model, the possible deviation of the pre-generated structured query statement is effectively solved, the accuracy of the structured query statement generation is significantly improved, and it is ensured that the output answer can accurately meet the user's query demand.
[0082] According to the above description, the method for generating a structured query statement provided by the embodiments of the present application extracts the keywords in the user question through a large language model and matches them with the database data to determine the target keywords. Based on the target keywords, the user question is rewritten and reconstructed and confirmed to ensure that the question expression is close to the database content to reduce natural language ambiguity. Based on the confirmed rewritten question, the minimum link table and the most relevant example are retrieved and constructed to provide accurate context information support for the query. Through the component recognition of the user question by the large language model, the structured query statement is obtained to clearly determine the core elements and logical relationships of the query. In combination with the minimum link table, the most relevant example and the structured query statement, the pre-generated structured query statement is obtained through the large language model and executed to obtain the answer.
[0083] The generation method provided in the embodiments of the present application avoids understanding deviation caused by mismatch between natural language and database terms through accurate matching of keywords and rewriting mechanism of user questions; provides reference basis for query statement generation that conforms to database structure by means of structured context construction of minimum link table and most relevant examples; converts user intention into structured expression through component identification, ensures clear transmission of query elements, realizes accurate capture and conversion of user query intention, improves accuracy of structured query statement generation, and enhances adaptability and reliability of the method in processing natural language queries.
[0084] In a second aspect, based on the same aspect of the concept, the embodiments of the present application also provide a structured query statement generation system. The generation system applies the structured query statement generation method described above, can improve the accuracy of the system in generating structured query statements, and enhance the adaptability and reliability in processing natural language queries.
[0085] For further illustration, the overall process of the generation system provided in the embodiments of the present application is shown in Figure 5 As shown in the figure, after the user inputs a question, the system first extracts keywords from the user question, then matches the keywords with the target database, and reconstructs the user question based on the matching result. The reconstructed question is confirmed by the user. If the user's confirmation is not passed, it means that the question reconstruction may have deviation, and the keyword extraction step is restarted to ensure the accuracy of the question expression. If the user's confirmation is passed, it means that the reconstructed question accurately reflects the user's intention, and enters the scene-based business retrieval link and the component replacement link.
[0086] The scene-based business retrieval link retrieves whether there is a matching answer in the database based on the reconstructed question confirmed by the user. If an answer is retrieved, it is judged by a verification model whether the answer conforms to the user question. If no answer is retrieved, the most relevant example selection and the minimum link table selection are entered, and the most relevant example and the minimum link table together provide context reference for generating a query statement. The component identification path processes the user question to identify components such as query object, query condition and aggregation operation components. Based on the identification result, the rewritten and reconstructed user question is replaced by components, and the rewritten and reconstructed user question is converted into a structured statement.
[0087] Based on the most relevant example, the minimum link table and the structured statement generation prompt word, the large language model is guided by the prompt word to output a pre-generated structured query statement. After the pre-generated structured query statement is generated, the pre-generated structured query statement is executed in the database to obtain an answer to the question. The obtained answer to the question is verified. If the verification is passed, the answer is output; if the verification is not passed, the prompt word generation link is returned, and the pre-generated structured query statement is reacquired until the obtained answer passes the verification.
[0088] The whole system process forms a closed loop problem processing mechanism through keyword extraction and matching, user confirmation, component identification, scenario-based retrieval, example and link table selection, context generation, verification and other links, realizes the conversion of user natural language questions to accurate structured query statements, improves the accuracy and reliability of the query results, and enhances the fault tolerance and adaptability of the system through the user confirmation and verification links.
[0089] It can be understood that the word "exemplary" used herein means "serving as an example, instance, or illustration". Any embodiment described as "exemplary" is not necessarily preferred or superior over other embodiments and / or does not exclude features that can be combined with other embodiments. It should be understood that certain features of the present application described in the context of separate embodiments can also be provided in combination, for clarity and for illustration, various features of the present application described in the context of a single embodiment can also be provided separately or in any appropriate combination or as part of any other described embodiment of the present application.
[0090] In the description of the present application, unless otherwise specified, " / " means "or", for example, A / B can mean A or B. "And / or" in this paper is only a description of the relationship between the associated objects, which means that there can be three relationships, for example, A and / or B, which can mean: A exists alone, A and B exist together, and B exists alone. In addition, "at least one" means one or more, and "multiple" means two or more. "First", "second", etc. do not limit the quantity and execution order, and "first", "second", etc. do not necessarily mean different.
[0091] The above disclosure is only the preferred embodiment of the present application, and is not intended to limit the scope of the present application. Those skilled in the art can understand that changes, modifications, substitutions, combinations and simplifications without departing from the spirit and scope of the present application and the appended claims, are equivalent replacement methods still within the scope of the present application.
Claims
1. A method for generating a structured query statement, characterized in that: The generation method comprises the following steps: Obtain a user question, extract at least one keyword from the user question using a large language model, match each keyword with information in a database, calculate the similarity between each keyword and the information in the database, and select the information with the highest similarity to each keyword as a target keyword corresponding to the keyword; Rewrite and reconstruct the user question based on the target keyword. If it is confirmed that the rewritten and reconstructed user question is correctly rewritten, search the database based on the rewritten and reconstructed user question, and construct a minimum link table and the most relevant examples according to the search results. Identifying components of the user question based on the large language model to obtain an initial structured statement about the user question, and optimizing the initial structured statement based on the rewritten and reconstructed user question to obtain an optimized structured statement; Based on the minimum link table, the most relevant example, and the optimized structured statement, a context project is constructed, prompt words are obtained through the context project, the prompt words are input into the large language model, a pre-generated structured query statement is generated, and the pre-generated structured query statement is executed in a database to obtain an answer to the user question.
2. The generation method according to claim 1, characterized in that The method further comprises: If the rewriting is confirmed to be correct, a scenario-based business search is performed based on the rewritten and reconstructed user question, and the similarity between the rewritten and reconstructed user question and the structured query statement in the database is calculated; When there is a structured query statement with a similarity greater than a preset statement threshold to the rewritten and reconstructed user question, select the structured query statement with the largest preset statement threshold, and execute the structured query statement in the database to obtain an answer to the user question; When there is no structured query statement with a similarity greater than a preset statement threshold to the rewritten and reconstructed user question, a search is performed in the database based on the rewritten and reconstructed user question, and a minimum link table and the most relevant examples are constructed according to the search results.
3. The generation method according to claim 2, characterized in that The scenario-based service retrieval includes: The structured query statements in the database are subjected to interface solidification processing, the rewritten and reconstructed user questions are vectorized, and similarity calculation is performed based on the vectorized user questions and the structured query statements subjected to interface solidification processing.
4. The generation method according to claim 2, characterized in that The method further comprises: Constructing a verification model to verify the answers obtained through the scenario-based business search, inputting the answers and the rewritten and reconstructed user questions into the verification model, and the verification model performs self-reflection and counter-questioning verification according to a preset number of rounds to determine whether the answers are correct; If the judgment result is correct, the answer is output; If the judgment result is wrong, the user is prompted to supplement the question information, and the keywords are re-extracted in combination with the user question and the supplementary question information.
5. The generation method according to claim 1, characterized in that The method further comprises: Vectorizing the rewritten and reconstructed user question, calculating the similarity between the vectorized user question and the description information of each table in the database, and selecting a table with a similarity greater than a preset form threshold as a selected table; Based on the selected table, similarity is calculated between the vectorized user question and the description information of each column in the selected table, and a column with a similarity greater than a preset column threshold is selected as the selected column; The minimum link table is generated based on the foreign key information, data definition language information of the selected table and the data of the selected column.
6. The generation method according to claim 5, characterized in that The method further includes: constructing the prompt word by using a prompt word layout and example sample method, wherein the prompt word includes one or more of the following: an operation instruction, the minimum link table, foreign key information of the selected table, a field of the selected table, the optimized structured statement, and the most relevant example; The prompt word is input into the large language model, and the large language model generates the pre-generated structured query language based on the content contained in the prompt word.
7. The generation method according to claim 1, characterized in that The method further comprises: Constructing key-value pair data, wherein the key-value pair data uses previous user questions as keys and structured query statements corresponding to the previous user questions as values; Based on the large language model, masking the rewritten and reconstructed user questions and the previous user questions in the key-value pair data; A similarity calculation is performed on the masked user question and the previous user questions in the masked key-value pair data, and the key-value pair data with a similarity greater than a prediction example threshold is selected as the most relevant example.
8. The generation method according to claim 1, characterized in that The method further comprises: Constructing a verification model to verify the answer obtained by executing the pre-generated structured query statement, inputting the answer and the rewritten and reconstructed user question into the verification model, and the verification model performing self-reflection and counter-question verification to determine whether the answer is correct; If the judgment result is correct, the answer is output; If the judgment result is an error, a corresponding error message is generated, the prompt word is updated based on the error message and the pre-generated structured query statement, and the pre-generated structured query statement is regenerated.
9. The generation method according to claim 1, characterized in that The method further comprises: Performing component recognition on the user question using the large language model to obtain the initial structured sentence, wherein the initial structured sentence includes one or more of the following recognition results: query object, query condition, and aggregation operation component; Based on the rewritten and reconstructed user question, one or more recognition results in the initial structured sentence are replaced to obtain the optimized structured sentence.
10. A system for generating structured query statements, characterized in that: The generation system applies the method for generating structured query statements according to any one of claims 1 to 8.
Citation Information
Patent Citations
Data question and answer method, system, equipment and computer program product
CN119597868A
Method and device for realizing text SQL (Structured Query Language) based on cue words of large language model
CN120030041A
Data intelligent interaction enhanced query method and system based on large model
CN120162344A
Cited By
Fine adjustment method of preset model, question and answer method and equipment based on preset model
CN121009964A
Fine-tuning method of preset model, question and answer method based on preset model, and device
CN121009964B
Retrieval enhancement generation method fusing dynamic adaptive prompt engineering and semantic enhancement
CN121166733A
Query method based on knowledge base and large model
CN121188083A
