A text conversion query statement generation method, system, medium and device
Through multi-process collaboration and a two-level prediction mechanism, accurate SQL query statements are generated using database values and large language models, solving the problems of inaccurate entity selection and inconsistent SQL keyword correspondence, and improving the execution accuracy of SQL statements.
Patent Information
- Application Number
- CN202510857263.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-10-17
- Estimated Expiration
- 2045-06-25
AI Technical Summary
In existing Text-to-SQL technologies, inaccurate entity selection and inconsistent SQL keyword correspondence result in the generated SQL query statements being unable to accurately express the key information of the problem.
A multi-process collaborative approach is adopted to solve the Text-to-SQL task in stages. Database value completion mode links are used to design a two-level prediction mechanism to predict SQL keywords and question fragments. The correct SQL query statements are generated through context learning and self-correction through a large language model.
Improves the execution accuracy of SQL statements, reduces the problem of inconsistency between conditional predicates and database values, and ensures the accuracy of SQL keywords corresponding to information in user questions.
Smart Images

Figure CN120371853B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application belongs to the technical field of computer systems, and particularly relates to a text conversion query statement generation method, system, medium and device. BACKGROUND
[0002] The statements in this section merely provide background information related to the present application and do not necessarily constitute prior art.
[0003] As the core data carrier of modern information systems, databases play a very key role in the digital information era. With the continuous expansion of data size and the increasing complexity of business requirements, the traditional SQL syntax-based query method has a high technical threshold, bringing unprecedented pressure to professionals. To reduce the technical barriers of database queries, ordinary users can obtain the required information in the database through natural language questions, reducing the dependence on professional knowledge. Text-to-SQL technology emerges as the times require and becomes an important bridge connecting people and structured data. Text-to-SQL, i.e., natural language to structured query language conversion technology, is an important subtask of semantic analysis in the field of natural language processing. Its core function is to generate a standard SQL statement that accurately reflects the user's query intent and can be efficiently executed on the target database under the premise of given natural language questions and database schema (DB Schema), realizing the goal of "breaking down the barriers between people and structured data". Ordinary users can complete complex database queries and quickly obtain the required results through natural language description without mastering complex SQL syntax. In recent years, large language models (LLM) have shown their strong performance in various fields. LLM is trained on a vast amount of data and covers knowledge in various fields, which can capture the complexity of language, including grammar, semantics, context, and cultural and social knowledge. Especially in the field of natural language, it has shown outstanding performance. At the same time, the field of Text-to-SQL research has generally shifted to LLM-based methods to explore the generalization ability and context understanding advantages of LLM to improve the performance of Text-to-SQL systems.
[0004] However, in the existing method of converting natural language questions into SQL query statements, the selection of entity corresponding tables and fields is prone to incorrect phenomena, and the selection of entity and database values is inconsistent, and the condition information corresponding to the SQL keyword is inaccurate, resulting in the generated SQL query statement cannot accurately express the key information of the problem. SUMMARY
[0005] To solve the above problems, the present application provides a text conversion query statement generation method, system, medium and equipment, which adopts a multi-process cooperation method to solve the Text-to-SQL task in stages. The database value is used to complete the schema linking task, which enriches the schema linking and solves the problem that the problem value does not correspond to the actual storage value in the database. At the same time, a two-level prediction mechanism is designed to predict the SQL keywords and the corresponding problem fragments.
[0006] According to some embodiments, the first aspect of the present application provides a text conversion query statement generation method, which adopts the following technical scheme:
[0007] A text conversion query statement generation method comprises:
[0008] Based on the user problem, database schema information and user problem related external knowledge, an initial structured query statement and a predicted schema link are generated, and after extracting the database key value information of the predicate in the initial structured query statement, the key schema link is obtained by fusing the predicted schema link;
[0009] Based on the user problem, the user problem is classified after classification, and the aligned structured query keywords are obtained by structure prediction;
[0010] Based on the user problem, the database schema information, the user problem related external knowledge, the key schema link, the aligned structured query keywords and the sample prompt, a candidate structured query statement is generated, the candidate structured query statement is self-corrected and executed, and a correct structured query statement is obtained.
[0011] Further, the database key value information of the predicate in the initial structured query statement is extracted, specifically:
[0012] The related predicates in the initial structured query statement and the problem entities in the user problem are extracted, the filtered predicates are obtained based on the vector similarity of the related predicates and the problem entities;
[0013] The predicates are processed by a local sensitive hash function, and the predicate database hash value information is determined by searching in the database hash mapping table based on the processed predicate information;
[0014] Based on the cosine similarity of all predicate database hash value information and corresponding predicate database value information, the key predicate database hash value information is obtained, and the database key value information is obtained by filtering the key predicate database hash value information, the user problem and the user problem related external knowledge by using a large language model.
[0015] Further, based on the user question, database schema information, user question related external knowledge and sample prompts, a large language model is used for context learning to predict the relevance of the user question to the tables and columns in the database, and a predicted schema link is obtained.
[0016] Further, the user question is classified based on the user question, and the structured query keywords after alignment are obtained by structure prediction on the classified user question, specifically:
[0017] Based on whether the user question contains nested statements, if the user question contains nested statements, it is a difficult question; otherwise, it is a simple question;
[0018] Based on the simple question, simple structured query keywords are generated by structure prediction. After determining the sub-problems of the difficult question, sub-structure prediction is performed first, and then outer structure prediction is performed to generate overall difficult structured query keywords;
[0019] Based on the simple structured query keywords or the overall difficult structured query keywords, information alignment is performed with the user question slices to obtain the aligned structured query keywords.
[0020] Further, after determining the sub-problems of the difficult question, sub-structure prediction is performed first, and then outer structure prediction is performed to generate overall difficult structured query keywords, specifically:
[0021] If it is classified as a difficult question, the nested statements in the user question are split, and the intent of each split nested statement is identified to determine the sub-problems corresponding to the nested statements;
[0022] A large language model is used to perform structure prediction on each sub-problem to generate keywords required by the sub-query statement;
[0023] A large language model is used to perform structure prediction on all keywords required by the sub-query statement to generate overall difficult structured query keywords.
[0024] Further, based on the user question, database schema information, user question related external knowledge, key schema link, aligned structured query keywords and sample prompts, candidate structured query statements are generated, the candidate structured query statements are self-corrected and executed to obtain correct structured query statements, specifically:
[0025] Based on the user question, database schema information, user question related external information, key schema link, aligned structured query keywords and sample prompts, a large language model is used to generate candidate structured query statements;
[0026] The candidate structured query statement is self-corrected based on the user question, the external knowledge related to the user question, the key pattern link and the aligned structured query keywords, to obtain a structured query statement to be executed.
[0027] Based on the structured query statement to be executed, execution is performed in the database, and if the execution query fails, error information and the user question are input into the large language model, the structured query statement to be executed is corrected and then executed again to obtain a correct structured query statement.
[0028] Further, the candidate structured query statement is self-corrected based on the user question, the external knowledge related to the user question, the key pattern link and the aligned structured query keywords, to obtain a structured query statement to be executed, specifically:
[0029] The user question, the external knowledge related to the user question, the key pattern link and the aligned structured query keywords and the candidate structured query statement are taken as inputs, the large language model is used to check whether the candidate structured query statement conforms to the information of the key pattern link and the aligned structured query keywords, if yes, the candidate structured query statement is output as the structured query statement to be executed;
[0030] If not, the large language model is used to generate a corrected structured query statement based on the user question, the external knowledge related to the user question, the key pattern link and the aligned structured query keywords, as the structured query statement to be executed.
[0031] According to some embodiments, a second aspect of the present application provides a text conversion query statement generation system, adopting the following technical solution:
[0032] A text conversion query statement generation system comprises:
[0033] A key pattern link construction module is configured to generate an initial structured query statement and a predicted pattern link based on a user question, database schema information and external knowledge related to the user question, and to obtain a key pattern link by fusing the database key value information of the predicates in the initial structured query statement with the predicted pattern link after the extraction.
[0034] A structure prediction module is configured to classify a user question based on the user question, and to perform structure prediction on the classified user question to obtain aligned structured query keywords.
[0035] A statement correction module is configured to generate a candidate structured query statement based on a user question, database schema information, external knowledge related to the user question, a key pattern link, aligned structured query keywords and a sample prompt, to self-correct the candidate structured query statement and perform execution, and to obtain a correct structured query statement.
[0036] According to some embodiments, a third aspect of the present application provides a computer readable storage medium.
[0037] A computer readable storage medium having stored thereon a computer program which, when executed by a processor, implements the steps of a text-to-query sentence generation method according to the first aspect.
[0038] According to some embodiments, a fourth aspect of the present application provides a computer device.
[0039] A computer device comprising a memory, a processor, and a computer program stored on the memory and executable on the processor, wherein the processor implements the steps of a text-to-query sentence generation method according to the first aspect when executing the program.
[0040] Compared with the prior art, the present application has the following beneficial effects:
[0041] The present application fully utilizes the inference ability of the LLM by constructing a Text-to-SQL generation architecture with multi-functional collaboration, utilizes the actual stored values in the database, reduces the problem that the condition predicates in the LLM generated SQL statement are inconsistent with the stored values in the database, and reduces the problem that the fields and tables corresponding to the values of the condition predicates in the SQL statement are inaccurate in the database; and the two-level prediction mechanism ensures the accuracy of the corresponding information of the keywords in the SQL statement in the user question, and to a certain extent, improves the execution accuracy of the SQL statement. BRIEF DESCRIPTION OF DRAWINGS
[0042] The accompanying drawings, which form a part of this specification, are included to provide a further understanding of the application and are incorporated in and constitute a part of this specification. The embodiments of these drawings are set forth to explain the present application and do not limit the present application in any manner.
[0043] Figure 1 is a schematic diagram of a text-to-query sentence generation method according to an embodiment of the present application. DETAILED DESCRIPTION
[0044] The present application will be further described below with reference to the accompanying drawings and embodiments.
[0045] It should be noted that the following detailed description is exemplary in nature and is intended to provide further description of the present application. Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the present application belongs.
[0046] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, they indicate the presence of features, steps, operations, devices, components and / or combinations thereof.
[0047] In the absence of conflict, the embodiments of the present invention and the features thereof may be combined with each other.
[0048] Example 1
[0049] like Figure 1 As shown, this embodiment provides a method for generating text-converted query statements. This embodiment uses the method applied to a server as an example for illustration. It is understandable that the method can also be applied to a terminal, and can also be applied to a system including a terminal, a server, and a server, and is implemented through the interaction between the terminal and the server. The server can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network servers, cloud communications, middleware services, domain name services, security services CDN, and big data and artificial intelligence platforms. The terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited to this. The terminal and the server can be directly or indirectly connected via wired or wireless communication, which is not limited in this application. In this embodiment, the method includes the following steps:
[0050] Step S1: Based on the user question, database schema information, and external knowledge related to the user question, an initial structured query statement and a predicted schema link are generated. The database key value information of the predicate in the initial structured query statement is extracted and then fused with the predicted schema link to obtain a key schema link.
[0051] Step S2: Classify user questions based on their structure, and predict the structure of the classified user questions to obtain aligned structured query keywords.
[0052] Step S3: Generate candidate structured query statements based on user questions, database schema information, external knowledge related to user questions, key schema links, aligned structured query keywords, and sample prompts, perform self-correction on the candidate structured query statements, and execute them to obtain correct structured query statements.
[0053] The embodiment proposes a Text-to-SQL generation method with multi-functional cooperation, adopts multi-process cooperation, and solves the Text-to-SQL task in stages. The database value is used to complete the schema linking task, enriches the schema linking, and solves the problem that the problem value does not correspond to the actual storage value of the database. At the same time, a two-level prediction mechanism is designed to predict the SQL keywords and the corresponding problem fragments; mainly in the following three aspects, specifically:
[0054] Schema linking: Schema linking refers to associating the meaning of a natural language query with a database schema, enabling interaction between the problem and the database. Ensure that the words or phrases in the query are accurately matched with related database elements (such as tables, columns, or values). Correct schema linking means being able to accurately retrieve all database components related to the natural language query.
[0055] Structure prediction: Predict the keywords required for generating SQL statements through large language models to ensure structural stability.
[0056] SQL correction: Execute the candidate SQL in the database, and if an error occurs, input the user's question and error information into the large language model for correction.
[0057] Step S1: Based on the user's question, database schema information, and user's question-related external knowledge, generate an initial structured query statement and predict schema linking, extract the database key value information of the predicate in the initial structured query statement, and fuse it with the predicted schema linking to obtain the key schema linking.
[0058] Step S1.1: Use the user's question, database schema information, and user's question-related external knowledge, use the COT prompt, and generate an initial structured query statement (initial SQL statement) based on the large language model reasoning.
[0059] First, use the reasoning ability of the large language model to generate an initial SQL statement under the premise of given user question information, database schema information, and user question-related external knowledge using the COT prompt. Among them, the user question-related external knowledge refers to the common knowledge related to the user's question, that is, the explanation of the special entity in the user's question.
[0060] Database schema information defines the structure and organization of data, including tables, columns, data storage types, sequences, views, primary keys, and foreign keys.
[0061] Step S1.2: Extract the related predicates in the initial SQL statement and the problem entities in the user's question, filter based on the vector similarity of the related predicates and the problem entities, and obtain the filtered predicates.
[0062] Extract the relevant predicates in the initial SQL statement and the question entities in the user question, that is, extract the relevant predicates in the initial SQL statement (that is, the actual values of the database used in the SQL statement). At the same time, decompose the user question and extract the question entities in the user question.
[0063] Calculate the vector similarity between each relevant predicate and each question entity, and if it exceeds the set vector similarity threshold, select the question entity as the filtered predicate; otherwise, select the relevant predicate as the filtered predicate.
[0064] That is, the filtering of this step is to check whether the extracted relevant predicate is only part of the entire question entity. For example, the question "Please list the zip codes of all charter schools in the Fresno County Education Office." The relevant predicate extracted by the initial SQL is "Fresno County", and the extracted question entity is "Fresno County Education Office", and "Fresno County Education Office" is selected as the filtered predicate.
[0065] Step S1.3: Hash the predicates using a local sensitive hash function, retrieve based on the hashed predicate information in the database hash mapping table, and determine the predicate database hash value information.
[0066] Among them, the generation of the database hash mapping table is specifically:
[0067] Read the database values, filter out the same values in the same table and field based on the table and field where the database values are located, and ensure the uniqueness of the values to obtain the filtered database values;
[0068] Hash map the filtered database values using a local sensitive hash (LSH) function to obtain the database hash mapping table. Based on the above filtering of the same values in the same table and field, the problem of incorrect selection of entities in natural language conversion to SQL and the problem of inconsistency between the selected entities in natural language conversion to SQL and the values in the database are solved in the schema linking.
[0069] Among them, the predicate database hash value information is the table and column in the filtered database corresponding to the predicate.
[0070] Step S1.4: Based on the cosine similarity of all predicate database hash value information and corresponding predicate database value information, filter to obtain key predicate database hash value information, and use a large language model to filter the key predicate database hash value information, user question and user question related external knowledge to obtain database key value information.
[0071] Calculate the cosine similarity of all predicate database hash value information and corresponding predicate database value information;
[0072] The predicate database hash value information exceeding the set cosine similarity threshold is retained as the key predicate database hash value information.
[0073] The key predicate database hash value information, the user question, and the user question related external knowledge are filtered by using a large language model, and the key predicate database hash value information associated with the user question is selected as the database key value information.
[0074] Here, the filtering refers to taking the key predicate database hash value information retrieved above, the user question, and the user question related external knowledge as inputs of the large language model, and letting the large language model output value information with high relevance to the user question; for example, the user question is: “What is the equivalent of 400 US dollars in RMB on May 6, 2022”, and the value of May 6, 2022 may be in the “time column” of the “user table” or in the “time column” of the “spending table”. The “time column” in the “user table” is relevant to the user question. In this embodiment, the filtering by the large language model is to select May 6, 2022 in the “time column” of the “user table”.
[0075] Here, it can be understood that the predicate database value information is the table and column in the database where the predicate is located.
[0076] All the predicate database hash value information retrieved is sequentially filtered by cosine similarity and LLM, so as to ensure that the value exists in the database and is correctly written, and the selected table and field are correct.
[0077] Step S1.5: Based on the user question, the database schema information, the user question related external knowledge, and the sample prompt, a large language model is used for context learning to predict the relevance of the user question to the table and column in the database, and a predicted schema link is obtained.
[0078] The user question, the database schema information, and the user question related external knowledge are taken as inputs of the LLM, and a sample prompt is used to make the LLM perform context learning to predict the relevance of the question to the table and column in the database. The schema link is predicted.
[0079] Step S1.6: The predicted schema link is combined with the database key value information to form a final key schema link.
[0080] The schema link prompt word template is as follows:
[0081] Role:
[0082] You are an experienced database administrator.
[0083] Task description:
[0084] Your task is to determine the relevant tables and columns needed to generate SQL statements from the database schema by analyzing the question and hints in depth.
[0085] Database structure introduction:
[0086] Table: Table name.
[0087] Column: Column_name (Column_type), Column description -> Column description, example: (example of values stored in this column).
[0088] Objective:
[0089] Determine the relevant tables and columns in the database needed to generate SQL statements for the given question.
[0090] Example: Sample hints.
[0091] Database structure:
[0092] {DB Schema}.
[0093] Output: Tables and columns.
[0094] Step S2: Based on the user question, classify and structure predict the classified user question to get the aligned structured query keywords. This step mainly solves the problem of incorrect condition information conversion from natural language to SQL keywords in SQL.
[0095] Step S2.1: Classify based on whether the user question contains nested statements. If the user question contains nested statements, it is a difficult question; otherwise, it is a simple question.
[0096] Take the user question as input, first classify the question into two categories: simple and difficult. Simple question means no nested statement, difficult question is nested statement, reduce the classification of task, to a certain extent, alleviate the model illusion. That is, the basis for classifying simple and difficult questions: whether the question needs the result generated by the subquery to complete the overall query.
[0097] Step S2.2: Generate simple structured query keywords based on simple question structure prediction, and determine sub-problems based on difficult question. First, perform sub-structure prediction and then perform outer-structure prediction to generate overall difficult structured query keywords.
[0098] Perform structure prediction on the classified user question, that is, generate the keywords needed for SQL statements based on the user question. Specifically as follows:
[0099] If it is classified as a simple problem, directly use the large language module to perform structure prediction to generate simple structured query keywords such as "select", "join", and "group by".
[0100] If it is classified as a difficult problem, the nested statements in the user question are split, and the intent of each split nested statement is identified to determine the corresponding sub-problems of the nested statements.
[0101] Use the large language model to perform structure prediction on each sub-problem to generate the keywords required for the sub-query statement.
[0102] Use the large language model to perform structure prediction on all sub-query statement required keywords to generate overall difficult structured query keywords.
[0103] Step S2.3: Based on the simple structured query keywords or overall difficult structured query keywords, perform information alignment with the user question slices to obtain aligned structured query keywords.
[0104] Among them, the aligned structured query keywords include aligned simple structured query keywords and aligned overall difficult structured query keywords.
[0105] Align the obtained simple structured query keywords or difficult structured query keywords with the user question slices, that is, align the keywords with the information in the question. For example, the question "Which football player has the shortest height?" (simple) predicts the alignment "select (the shortest height of the athlete)". Reduce the divergence of the model when generating SQL statements, and let the large language model re-understand the question and correspond to the keywords.
[0106] The structure prediction prompt word template is as follows:
[0107] Role:
[0108] You are an experienced SQL language expert.
[0109] Task description:
[0110] Given a question and the prompts, schema links, database schema information, descriptions of columns in the database table, and examples of values stored in the columns.
[0111] Prompt: Better understand the question.
[0112] Information interpretation:
[0113] Table: Table name.
[0114] Column: Column_name (Column_type), column description -> column description, example: (example of values stored in this column).
[0115] Objective:
[0116] Determine the SQL keywords and functions that can be used in the SQL query generated for the given problem. Do not generate the SQL statement.
[0117] Output: SQL keywords and functions.
[0118] Step S3: Based on the user question, database schema information, user question related external knowledge, key schema link, aligned structured query keywords, and sample prompts, generate candidate structured query statements, self-correct the candidate structured query statements, and execute to obtain the correct structured query statements.
[0119] Step S3.1: Based on the user question, database schema information, user question related external information, key schema link, aligned structured query keywords, and sample prompts, generate candidate structured query statements using a large language model.
[0120] Input the user question, database schema information, user question related external information, key schema link, aligned structured query keywords, and sample prompts into the LLM, and let the model think step by step according to the input sample prompts to generate candidate structured query statements (candidate SQL statements).
[0121] Step S3.2: Based on the user question, user question related external knowledge, key schema link, and aligned structured query keywords, self-correct the candidate structured query statements to obtain the structured query statements to be executed, specifically:
[0122] Input the user question, user question related external knowledge, key schema link, and aligned structured query keywords, and candidate structured query statements into the large language model to check whether the candidate structured query statements meet the information of the key schema link and aligned structured query keywords. If they meet, output the candidate structured query statements as the structured query statements to be executed.
[0123] If they do not meet, generate corrected structured query statements based on the user question, user question related external knowledge, key schema link, and aligned structured query keywords using the large language model as the structured query statements to be executed.
[0124] Input the user question, user question related external knowledge, key schema link, and aligned structured query keywords to let the LLM self-correct the candidate SQL statements, ensuring that the previous information is fully utilized and preventing the generation of SQL statements directly based on the question and external knowledge without utilizing the key schema link and aligned structured query keywords.
[0125] Step S3.3: Execute the structured query statement in the database based on the to-be-executed structured query statement. If the execution of the query fails, input the error information and user problem into the large language model, correct the to-be-executed structured query statement, and then execute it again to obtain a correct structured query statement.
[0126] Execute the SQL statement obtained after self-correction in the database. If the result is empty or an error occurs, input the error information and user problem into the large language model, correct the to-be-executed structured query statement, and then execute it again to obtain a correct structured query statement (correct SQL statement).
[0127] Error correction prompt words are as follows:
[0128] Role:
[0129] You are an expert in SQLite error correction.
[0130] Task description:
[0131] You will be given a question, a hint, database schema information, an incorrect SQL statement, and related information about the SQL execution error. Analyze the error information and correct it.
[0132] Introduce the database architecture information:
[0133] Table: Table name.
[0134] Column: Column_name (Column_type), column description -> column description, example: (example of values stored in this column).
[0135] Step 1: Requirement check;
[0136] Confirm whether the SQL query meets the requirements specified in the problem.
[0137] Please pay close attention to "external knowledge" to obtain reference information that can help check the SQL. Verify whether the SQL query correctly understands and applies relevant concepts or constraints.
[0138] If there is no "external knowledge", please focus on the problem itself.
[0139] Step 2: Result rationality;
[0140] If the SQL involves arithmetic operations, check whether the data types in the arithmetic operation part are correct and write the analysis in a descriptive manner.
[0141] If it does not meet the requirements, modify the SQL query to ensure that it meets all requirements and generates logically reasonable results.
[0142] Only the corrected SQL statement is outputted without any explanation.
[0143] Rewrite a new SQL statement when none of the previous SQL statements meet the requirements.
[0144] Output: the corrected SQL statement.
[0145] Embodiment Two
[0146] The embodiment provides a text-to-query generation system, comprising:
[0147] A key pattern link construction module is configured to generate an initial structured query statement and a predicted pattern link based on a user question, database schema information, and external knowledge related to the user question, fuse the database key value information of the predicates in the initial structured query statement with the predicted pattern link after the extraction, and obtain a key pattern link.
[0148] A structure prediction module is configured to classify a user question based on the user question, perform structure prediction on the classified user question, and obtain an aligned structured query keyword.
[0149] A statement correction module is configured to generate a candidate structured query statement based on a user question, database schema information, external knowledge related to the user question, a key pattern link, an aligned structured query keyword, and a sample prompt, self-correct the candidate structured query statement, and execute the candidate structured query statement to obtain a correct structured query statement.
[0150] The above modules and the examples and application scenarios realized by the corresponding steps are the same, but are not limited to the content disclosed in the above embodiment one. It should be noted that the above modules, as part of the system, can be executed in a computer system such as a set of computer executable instructions.
[0151] The descriptions of the various embodiments in the above embodiments each have their own focuses, and the parts not described in detail in a certain embodiment can be referred to the related descriptions of other embodiments.
[0152] The proposed system can be implemented in other ways. For example, the system embodiments described above are only illustrative, for example, the division of the above modules is only a logical function division, and in actual implementation, another division mode can be used, for example, a plurality of modules can be combined or integrated into another system, or some features can be ignored or not executed.
[0153] Embodiment Three
[0154] The embodiment provides a computer readable storage medium, which stores a computer program, and the computer program is executed by a processor to implement steps in the text conversion query sentence generation method in the embodiment one.
[0155] Embodiment four
[0156] The embodiment provides a computer device, which comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor implements steps in the text conversion query sentence generation method in the embodiment one when executing the program.
[0157] Those skilled in the art should understand that the embodiments of the present application can be provided as a method, a system or a computer program product. Therefore, the present application can adopt a hardware embodiment, a software embodiment or an embodiment combining software and hardware aspects. Moreover, the present application can adopt a computer program product implemented on one or more computer usable storage media (including but not limited to disk storage and optical storage) containing computer usable program codes.
[0158] The present application is described with reference to flowcharts and / or block diagrams of the method, device (system) and computer program product according to the embodiments of the present application. It should be understood that each flow and / or block in the flowcharts and / or block diagrams, and the combination of the flows and / or blocks in the flowcharts and / or block diagrams can be implemented by computer program instructions. These computer program instructions can be provided to a general-purpose computer, a special-purpose computer, an embedded processor or other programmable data processing devices to produce a machine, so that the instructions executed by the computer or other programmable data processing devices generate a means for implementing the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 an apparatus for performing the functions specified in one or more flows and / or blocks.
[0159] These computer program instructions can also be stored in a computer readable storage medium capable of guiding the computer or other programmable data processing devices to work in a specific way, so that the instructions stored in the computer readable storage medium produce a product including instruction apparatus, which implements the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocks Figure 1 an apparatus for performing the functions specified in one or more flows and / or blocks.
[0160] These computer program instructions can also be loaded into a computer or other programmable data processing device to make a series of operation steps on the computer or other programmable device to produce a computer implemented process, so that the instructions executed on the computer or other programmable device provide a means for implementing the functions specified in the flowcharts and / or block diagrams. Figure 1 one or more flows and / or blocksFigure 1 the functions specified in the one or more blocks.
[0161] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the program can be stored in a computer readable storage medium. When the program is executed, it can include the processes of the above-mentioned embodiment methods. The storage medium can be a magnetic disc, an optical disc, a read-only memory (ROM), a random access memory (RAM), or the like.
[0162] Although the specific embodiments of the present application are described above in combination with the drawings, it is not a limitation on the scope of protection of the present application. Those skilled in the art should understand that various modifications or changes made by those skilled in the art on the basis of the technical solutions of the present application without creative labor are still within the scope of protection of the present application.
Claims
1. A method for generating a text-conversion query statement, characterized in that: include: Based on the user question, database schema information, and external knowledge related to the user question, an initial structured query statement and a predicted schema link are generated. The database key value information of the predicate in the initial structured query statement is extracted and then fused with the predicted schema link to obtain a key schema link. Based on user questions, database schema information, external knowledge related to user questions, and sample prompts, a large language model is used for contextual learning to predict the relevance of user questions to tables and columns in the database, and obtain predicted schema links. Extract the database key value information of the predicate in the initial structured query statement, specifically: Extract relevant predicates from the initial structured query and question entities from the user's question, filter them based on the vector similarity between the relevant predicates and question entities, and obtain filtered predicates; Specifically, the relevant predicates in the initial SQL statement and the problem entities in the user question are extracted. The relevant predicates in the initial SQL statement are extracted; the relevant predicates are the actual values of the database used in the SQL statement; at the same time, the user question is decomposed and the problem entities in the user question are extracted. Calculate the vector similarity between each relevant predicate and each question entity. If it exceeds the set vector similarity threshold, the question entity is selected as the filtered predicate; Otherwise, the relevant predicates are selected as filtered predicates; Using a locality-sensitive hash function to hash the predicate, searching the database hash map based on the hashed predicate information to determine the predicate database hash value information; Based on the cosine similarity between all predicate database hash value information and the corresponding predicate database value information, key predicate database hash value information is obtained. The key predicate database hash value information, user questions, and external knowledge related to user questions are filtered using a large language model to obtain database key value information. Specifically, the cosine similarity between all predicate database hash value information and the corresponding predicate database value information is calculated; the predicate database hash value information that exceeds a set cosine similarity threshold is retained as the key predicate database hash value information. Using a large language model to filter key predicate database hash value information, user questions, and external knowledge related to user questions, and select key predicate database hash value information associated with user questions as database key value information; The predicate database hash value information is the table and column in the filtered database where the corresponding predicate is located; The predicate database value information is the table and column in the database where the predicate is located; Classify user questions and perform structural prediction on the classified user questions to obtain aligned structured query keywords; Based on simple questions, structure prediction is performed to generate simple structured query keywords. After determining sub-questions based on difficult questions, sub-structure prediction is performed first and then external structure prediction to generate overall difficult structured query keywords. Aligning simple structured query keywords or overall difficult structured query keywords with user question slices to obtain aligned structured query keywords; Based on the user question, database schema information, external knowledge related to the user question, key schema links, aligned structured query keywords, and sample prompts, candidate structured query statements are generated. The candidate structured query statements are self-corrected and executed to obtain the correct structured query statement, specifically: Generate candidate structured query statements using a large language model based on user questions, database schema information, external information related to user questions, key schema links, aligned structured query keywords, and sample prompts; Based on user questions, external knowledge related to user questions, key pattern links, and aligned structured query keywords, the candidate structured query statements are self-corrected to obtain the structured query statements to be executed; The structured query statement to be executed is executed in the database. If the query fails, the error message and user question are input into the large language model. The structured query statement to be executed is corrected and then executed again to obtain the correct structured query statement.
2. A text conversion query statement generation method according to claim 1, characterized in that: The user questions are classified, and the structure of the classified user questions is predicted to obtain aligned structured query keywords, specifically: Classify the questions based on whether they contain nested statements. If they do, they are considered difficult questions; otherwise, they are considered easy questions. Based on simple questions, structure prediction is performed to generate simple structured query keywords. After determining sub-questions based on difficult questions, sub-structure prediction is performed first and then external structure prediction to generate overall difficult structured query keywords. Based on simple structured query keywords or overall difficult structured query keywords and user question slices, information alignment is performed to obtain aligned structured query keywords.
3. A text conversion query statement generation method according to claim 2, characterized in that: After determining the sub-questions based on the difficult question, first perform sub-structure prediction and then perform external structure prediction to generate the overall difficult structured query keywords, specifically: If the question is classified as difficult, the user's question is split based on the nested sentences in the question. The intent of each nested sentence is identified after the split, and the sub-question corresponding to the nested sentence is determined; Use a large language model to predict the structure of each sub-question and generate the keywords required for the sub-query statement; A large language model is used to predict the structure of keywords required for all sub-query statements and generate overall difficult structured query keywords.
4. The method for generating a text-to-query statement according to claim 1, wherein: Based on the user question, external knowledge related to the user question, key pattern links, and aligned structured query keywords, the candidate structured query statement is self-corrected to obtain the structured query statement to be executed, specifically: The system takes the user question, external knowledge related to the user question, key pattern links and aligned structured query keywords, and candidate structured query statements as input, and uses the large language model to check whether the candidate structured query statements conform to the key pattern links and aligned structured query keywords. If so, the candidate structured query statement is output as the structured query statement to be executed; If not, a corrected structured query statement is generated using a large language model based on the user question, external knowledge related to the user question, key pattern links, and aligned structured query keywords as the structured query statement to be executed.
5. A text conversion query statement generation system, characterized in that: include: A key pattern link building module is configured to generate an initial structured query statement and a predicted pattern link based on the user question, database pattern information, and external knowledge related to the user question, extract database key value information of the predicate in the initial structured query statement, and fuse it with the predicted pattern link to obtain a key pattern link; Based on user questions, database schema information, external knowledge related to user questions, and sample prompts, a large language model is used for contextual learning to predict the relevance of user questions to tables and columns in the database, and obtain predicted schema links. Extract the database key value information of the predicate in the initial structured query statement, specifically: Extract relevant predicates from the initial structured query and question entities from the user's question, filter them based on the vector similarity between the relevant predicates and question entities, and obtain filtered predicates; Specifically, the relevant predicates in the initial SQL statement and the problem entities in the user question are extracted. The relevant predicates in the initial SQL statement are extracted; the relevant predicates are the actual values of the database used in the SQL statement; at the same time, the user question is decomposed and the problem entities in the user question are extracted. Calculate the vector similarity between each relevant predicate and each question entity. If it exceeds the set vector similarity threshold, the question entity is selected as the filtered predicate; Otherwise, the relevant predicates are selected as the filtered predicates; Using a locality-sensitive hash function to hash the predicate, searching the database hash map based on the hashed predicate information to determine the predicate database hash value information; Based on the cosine similarity between all predicate database hash value information and the corresponding predicate database value information, the key predicate database hash value information is obtained. The key predicate database hash value information, user questions, and external knowledge related to user questions are filtered using a large language model to obtain database key value information. Specifically, the cosine similarity between all predicate database hash value information and the corresponding predicate database value information is calculated. The hash value information of the predicate database exceeding the set cosine similarity threshold is retained as the key predicate database hash value information; Using a large language model to filter key predicate database hash value information, user questions, and external knowledge related to user questions, and select key predicate database hash value information associated with user questions as database key value information; The predicate database hash value information is the table and column in the filtered database where the corresponding predicate is located; The predicate database value information is the table and column in the database where the predicate is located; The structure prediction module is configured to classify user questions based on the classification, perform structure prediction on the classified user questions, and obtain aligned structured query keywords; Based on simple questions, structure prediction is performed to generate simple structured query keywords. After determining sub-questions based on difficult questions, sub-structure prediction is performed first and then external structure prediction to generate overall difficult structured query keywords. Aligning simple structured query keywords or overall difficult structured query keywords with user question slices to obtain aligned structured query keywords; The statement correction module is configured to generate candidate structured query statements based on user questions, database schema information, external knowledge related to user questions, key schema links, aligned structured query keywords, and sample prompts, and self-correct and execute the candidate structured query statements to obtain the correct structured query statements. Specifically, Generate candidate structured query statements using a large language model based on user questions, database schema information, external information related to user questions, key schema links, aligned structured query keywords, and sample prompts; Based on user questions, external knowledge related to user questions, key pattern links, and aligned structured query keywords, the candidate structured query statements are self-corrected to obtain the structured query statements to be executed; The structured query statement to be executed is executed in the database. If the query fails, the error message and user question are input into the large language model. The structured query statement to be executed is corrected and then executed again to obtain the correct structured query statement.
6. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps in the text conversion query statement generation method according to any one of claims 1 to 4 are implemented.
7. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the text conversion query statement generation method according to any one of claims 1 to 4 are implemented.
Citation Information
Patent Citations
Large model-based sql statement generation method and device
CN119088824A
Context learning-based database query generation method and system and storage medium
CN119669265A