Query statement generation method and device, equipment and storage medium
Patent Information
- Application Number
- CN202610955976.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-30
- Publication Date
- 2026-10-30
AI Technical Summary
[0016]根据本申请实施例所提供的技术内容,接收用户终端发送的自然语言问题,将自然语言问题输入文本嵌入模型,得到示例相关列表及元知识列表;根据示例相关列表及元知识列表,构建结构化提示词;将结构化提示词输入大语言模型,生成候选查询语句集;执行候选查询语句集中的各候选查询语句,得到有效执行结果集;对有效执行结果集中的各有效执行结果进行评分,根据评分结果从对应的候选查询语句中筛选出目标查询语句。通过将自然语言问题输入文本嵌入模型,构建结构化提示词,以及将结构化提示词输入大语言模型,生成候选查询语句集,再进行进一步筛选得到目标查询语句,实现结构化查询语句在生成准确率、领域适配灵活性上均提升,同时降低实施与维护成本,达到更适用于工业级领域库的实际应用场景,提高领域查询交互的高效化的效果。
Smart Images

Figure CN123011961A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of query technology, and in particular to a query statement generation method, apparatus, device, and storage medium. Background Technology
[0002] As is well known, Text-to-SQL (Text-to-Structured Query Language) aims to automatically convert natural language questions into structured query languages, thereby lowering the barrier to interactive queries, enabling "zero-technical" data queries, improving access efficiency, and allowing "non-technical business personnel" to directly call domain libraries and quickly obtain the information needed for decision-making.
[0003] Currently, Text-to-SQL technology has made significant progress. In the Spider benchmark, the highest Execution Accuracy score is 86.6; in the BIRD (including real noise and complex query logic) single-model benchmark, the highest Execution Accuracy score is 76.13. Spider and BIRD are both authoritative benchmark sets in the Text-to-SQL field, becoming core benchmarks for measuring model performance due to their high quality, rigorous evaluation standards, and industry recognition. However, when directly applying Text-to-SQL to industries such as automotive manufacturing, large models lack vertical domain adaptability. Therefore, it is necessary to use industry-specific text and SQL statements to train or fine-tune large models to improve Text-to-SQL accuracy.
[0004] However, since most of the models and code on the Spider and BIRD leaderboards are closed-source, they are generally not publicly available or usable, which increases the difficulty of adjustment. Furthermore, due to the complex structure and domain-specific terminology and business logic of industries such as automobile manufacturing, even fine-tuning the large model cannot accurately parse industry-specific terms, professional terminology, and specific business logic, leading to discrepancies in the matching of database tables, fields, and inter-table relationships during SQL generation. At the same time, since domain libraries often have hundreds or even more tables, most of which are redundant and irrelevant, table matching errors occur, reducing the accuracy of SQL generation. Of course, the large model itself also has generation errors, which leads to a significant performance degradation when applied to industry libraries such as automobile manufacturing, failing to meet the needs of reliable interactive query tools. Summary of the Invention
[0005] Based on this, this application provides a query statement generation method, apparatus, device, and storage medium. By inputting natural language questions into a text embedding model, constructing structured prompt words, and inputting the structured prompt words into a large language model, a candidate query statement set is generated. Further filtering is then performed to obtain the target query statement. This improves the generation accuracy and domain adaptation flexibility of structured query statements, while reducing implementation and maintenance costs. It is more suitable for practical application scenarios of industrial-grade domain libraries and improves the efficiency of domain query interaction.
[0006] Firstly, a method for generating query statements is provided, the method comprising: Receive natural language questions sent by user terminals, input the natural language questions into the text embedding model, and obtain an example-related list and a meta-knowledge list; Based on the example-related list and the meta-knowledge list, construct structured prompt words; Input the structured prompts into the large language model to generate a set of candidate query statements; Execute each candidate query statement in the candidate query statement set to obtain a valid execution result set; Each valid execution result in the valid execution result set is scored, and the target query statement is selected from the corresponding candidate query statements based on the score results.
[0007] In a further technical solution, the natural language problem is input into a text embedding model to obtain an example-related list and a meta-knowledge list, including: By inputting natural language problems into a text embedding model, we obtain a vector representation problem; Based on the vector representation problem, a list of similar examples is obtained by retrieving matching examples from the example vector library. Based on the relevant table field information in the example list, obtain the example-related list; Based on the vector representation problem, matching similar meta-knowledge is retrieved from the meta-knowledge vector database to obtain a list of meta-knowledge.
[0008] In a further technical solution, the valid execution results in the valid execution result set are scored, and the target query statement is selected from the corresponding candidate query statements based on the scoring results, including: Each valid execution result in the valid execution result set is scored to obtain the total score set of valid execution results; Filter the valid execution results with the highest total score from the set of valid execution results, and use the candidate query statement corresponding to the valid execution result with the highest total score as the target query statement.
[0009] In a further technical solution, each valid execution result in the valid execution result set is scored to obtain a total valid execution result set, including: Each valid execution result in the valid execution result set is scored according to the preset scoring dimensions to obtain the score corresponding to each valid execution result in the scoring dimension. Based on the scores of each valid execution result in the corresponding scoring dimension and the corresponding preset weighting ratio, the weighted score of each valid execution result in the corresponding scoring dimension is obtained; The total score for each valid execution result is obtained by weighting the scores according to the scoring dimensions corresponding to each valid execution result; Based on the total score of each valid execution result, the total score set of valid execution results is obtained.
[0010] In a further technical solution, the preset scoring dimensions include any one or more of the following: vote percentage scoring dimension, confidence scoring dimension, semantic similarity scoring dimension, execution result complexity scoring dimension, and execution time scoring dimension.
[0011] In a further technical solution, the effective execution results include the query statement results and the number of rows of execution results. Each effective execution result in the effective execution result set is scored according to a preset scoring dimension to obtain a score for each effective execution result corresponding to that dimension, including: Based on the query results, each valid execution result in the valid execution result set is scored according to the vote percentage scoring dimension, resulting in a score for each valid execution result corresponding to the vote percentage scoring dimension, and / or... Based on the number of rows in the execution result set, the query results, and the natural language questions, each valid execution result in the valid execution result set is scored according to the confidence score dimension, resulting in a score for each valid execution result corresponding to the confidence score dimension, and / or, Based on the query results and natural language questions, each valid execution result in the valid execution result set is scored according to the semantic similarity scoring dimension, and the corresponding semantic similarity scoring dimension score for each valid execution result is obtained.
[0012] In a further technical solution, the effective execution result also includes execution time. Each effective execution result in the effective execution result set is scored according to a preset scoring dimension to obtain a score for each effective execution result corresponding to that dimension. This also includes: Based on the query results, each valid execution result in the valid execution result set is scored according to the execution result complexity scoring dimension, resulting in a score for each valid execution result corresponding to the execution result complexity scoring dimension, and / or... Based on the execution time, each valid execution result in the valid execution result set is scored according to the execution time scoring dimension, and the corresponding score for each valid execution result is obtained.
[0013] Secondly, a query statement generation apparatus is provided, the apparatus comprising: The response unit is used to receive natural language questions sent by the user terminal, input the natural language questions into the text embedding model, and obtain the example-related list and the meta-knowledge list; The building unit is used to construct structured prompt words based on the example-related list and the meta-knowledge list; The generation unit is used to input structured prompts into a large language model and generate a set of candidate query statements; The execution unit is used to execute each candidate query statement in the candidate query statement set to obtain a valid execution result set; The filtering unit is used to score each valid execution result in the valid execution result set, and to filter out the target query statement from the corresponding candidate query statements based on the score results.
[0014] Thirdly, a computer device is provided, comprising: At least one processor; and A memory that is communicatively connected to at least one processor; wherein, The memory stores computer instructions that can be executed by at least one processor to enable the at least one processor to perform the methods involved in the first aspect above.
[0015] Fourthly, a computer-readable storage medium is provided having computer instructions stored thereon for causing a computer to perform the methods described in the first aspect above.
[0016] According to the technical content provided in the embodiments of this application, a natural language question sent by a user terminal is received; the natural language question is input into a text embedding model to obtain an example-related list and a meta-knowledge list; based on the example-related list and the meta-knowledge list, structured prompt words are constructed; the structured prompt words are input into a large language model to generate a candidate query statement set; each candidate query statement in the candidate query statement set is executed to obtain a valid execution result set; each valid execution result in the valid execution result set is scored, and the target query statement is selected from the corresponding candidate query statements based on the scoring results. By inputting the natural language question into a text embedding model to construct structured prompt words, and inputting the structured prompt words into a large language model to generate a candidate query statement set, and then further filtering to obtain the target query statement, the accuracy of structured query statement generation and the flexibility of domain adaptation are improved, while reducing implementation and maintenance costs. This makes it more suitable for practical application scenarios of industrial-grade domain libraries and improves the efficiency of domain query interaction. Attached Figure Description
[0017] Figure 1 This is a system architecture diagram of a query statement generation method in one embodiment; Figure 2This is a flowchart illustrating a query statement generation method in one embodiment; Figure 3 This is a schematic diagram of a preferred flow of a query statement generation method in one embodiment; Figure 4 This is a structural block diagram of a query statement generation device in one embodiment; Figure 5 This is a schematic structural diagram of a computer device in one embodiment. Detailed Implementation
[0018] The present application will be further described in detail below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative of the present application and are not intended to limit the scope of the present application.
[0019] For ease of understanding, the system to which this application applies will first be described. The query statement generation method provided in this application can be applied to systems such as... Figure 1The system architecture shown includes: a user terminal 101, a query server 103, and a database device 105. Specifically, the query server 103 responds to the natural language question input by the user terminal 101, inputs the natural language question into a text embedding model, and calls the database device 105 to obtain an example-related list and a meta-knowledge list; it constructs structured prompt words based on the example-related list and the meta-knowledge list; it inputs the structured prompt words into a large language model to generate a candidate query statement set; it executes each candidate query statement in the candidate query statement set to obtain a valid execution result set; it scores each valid execution result in the valid execution result set, and selects the target query statement from the corresponding candidate query statements based on the score results. The user terminal 101 can be a target system intelligent agent or a service caller; a target system intelligent agent refers to an intelligent entity that can autonomously perceive the environment, plan decisions, and execute actions to achieve a specific goal; a service caller refers to a functional entity that calls other systems, components, or services by sending requests. When user terminal 101 is the target system agent, it can interact with query server 103 through the MCP protocol; when user terminal 101 is the service caller, it can interact with query server 103 through the API protocol. Query server 103 refers to a server embedded with Text-to-SQL (Text-to-Structured Query Language) services. Database device 105 includes, but is not limited to, domain database, example vector library, and meta-knowledge vector library; domain library refers to a library system specifically designed for models and rules in a specific business domain, used for storing, managing, and querying the core business of that domain; example vector library is a vector library imported after converting a preset example set into vector representation using a text embedding model; meta-knowledge vector library is a vector library imported after converting a preset meta-knowledge set into vector representation using a text embedding model.
[0020] Figure 2 A flowchart illustrating a query statement generation method provided in this application embodiment, the method can be performed by, for example... Figure 1 The query server in the system shown is executing the query. For example... Figure 2 As shown, the method may include the following steps: Step S201: Receive the natural language question sent by the user terminal, input the natural language question into the text embedding model, and obtain the example related list and the meta-knowledge list.
[0021] Natural language questions refer to inquiries or requests made by users using everyday spoken or written expressions (rather than programming languages or specific query syntax) to obtain information or answers. Text embedding models are machine learning models that convert natural language text (such as words, sentences, or documents) into high-dimensional numerical vectors, making semantically similar texts appear close to each other in the vector space. No specific model is restricted here; any model that can achieve the above functions can be used.
[0022] Specifically, the query server receives natural language questions sent by user terminals, inputs the natural language questions into a text embedding model, transforms them into vector representations, and then obtains an example-related list and a meta-knowledge list by calling the database device.
[0023] Step S203: Construct structured prompt words based on the example related list and the meta-knowledge list.
[0024] Here, after obtaining the example-related list and the meta-knowledge list, structured prompt words can be constructed based on the example-related list and the meta-knowledge list, so that they can be input into the large language model to generate SQL (Structured Query Language) later, making up for the shortcomings of open-source large language models due to vertical domain pre-training and improving the logical consistency during generation.
[0025] Step S205: Input the structured prompts into the large language model to generate a set of candidate query statements.
[0026] Among them, large language models refer to deep learning models trained with massive amounts of text and containing a huge number of parameters (usually ranging from hundreds of millions to hundreds of billions). Open source large models such as Qwen2.5-Coder and XiYanSQL-QwenCoder can be used. There are no restrictions on the specific model. Any model that can achieve the corresponding function can be used.
[0027] Specifically, after obtaining the structured prompts, these prompts can be input into the large language model. Since the large language model does not generate SQL based on "logical reasoning" but rather predicts the next character based on "statistical patterns" observed during training, it is essentially "probabilistic generation" rather than "deterministic deduction," inevitably resulting in a certain probability of error. This problem is inherent to the large language model itself and can be mitigated and corrected through post-processing filtering. Therefore, a preset number of candidate query statements, i.e., a candidate query statement set, can be generated here for further filtering later.
[0028] Step S207: Execute each candidate query statement in the candidate query statement set to obtain a valid execution result set.
[0029] Here, after obtaining the candidate query statement set, each candidate query statement in the candidate query statement set can be executed, that is, query operations can be performed on the candidate query statements. After the execution is completed, the successfully executed candidate query statements (i.e., valid execution results) and the failed candidate query statements (i.e., invalid execution results) can be directly ignored, and only the valid execution results are used to form the valid execution result set.
[0030] Step S209: Score each valid execution result in the valid execution result set, and select the target query statement from the corresponding candidate query statements based on the score results.
[0031] Specifically, after obtaining the set of valid execution results, the valid execution results in the set can be scored and filtered to obtain the target query statement. Depending on the actual needs, the target query statement can be returned to the user terminal, or the valid execution results corresponding to the target query statement can be returned to the user terminal. At the same time, it can also be displayed according to the user's needs, such as in the form of lists, reports, and images.
[0032] As can be seen, this embodiment of the application receives natural language questions sent by user terminals, inputs the natural language questions into a text embedding model to obtain an example-related list and a meta-knowledge list; constructs structured prompt words based on the example-related list and the meta-knowledge list; inputs the structured prompt words into a large language model to generate a candidate query statement set; executes each candidate query statement in the candidate query statement set to obtain a valid execution result set; scores each valid execution result in the valid execution result set, and selects the target query statement from the corresponding candidate query statements based on the scoring results. By inputting natural language questions into a text embedding model to construct structured prompt words, and inputting the structured prompt words into a large language model to generate a candidate query statement set, and then further filtering to obtain the target query statement, the structured query statement achieves improved generation accuracy and domain adaptation flexibility, while reducing implementation and maintenance costs, making it more suitable for practical application scenarios of industrial-grade domain libraries, and improving the efficiency of domain query interaction.
[0033] The different steps in the above method flow are described in detail below. First, the step 201, "inputting the natural language problem into the text embedding model to obtain the example relevance list and the meta-knowledge list," will be described in detail with reference to the embodiment.
[0034] The natural language problem is input into the text embedding model to obtain the vector representation problem; similar examples are retrieved from the example vector library based on the vector representation problem to obtain the example list; the relevant table field information in the example list is used to obtain the example related list; similar meta-knowledge is retrieved from the meta-knowledge vector library based on the vector representation problem to obtain the meta-knowledge list.
[0035] Specifically, the query server receives the natural language question sent by the user terminal, inputs the natural language question into the text embedding model, and then transforms the natural language question into a vector representation through the text embedding model to obtain the vector representation question.
[0036] Based on the vector representation question, similar examples are retrieved from the example vector library to obtain an example list. Here, the example vector library uses a cosine similarity algorithm to retrieve several examples that are semantically similar to the input natural language question, resulting in an example list. The example vector library examples may include the library ID "db_id", the example question "question", the example SQL "query", and the tables involved in the examples "filter_tables". The cosine similarity algorithm is a method that determines the similarity between two vectors by calculating the directional consistency between them in space. That is, it can be calculated by the dot product of vectors A and B / (the magnitude of vector A × the magnitude of vector B). It does not focus on the length (size) of the vectors, but only on the angle between them. The specific expression can be represented as follows: ; Here, A represents the vector representation problem, which can be expressed as A = [a1, a2, ..., a...]. n ]; B represents the example vectors in the example vector library, which can be represented as B=[b1, b2, ..., b n ]; i represents the index of the vector; n represents the total number of vectors.
[0037] In this application, the similarity threshold can be represented by N and set to 0.7. That is, the example vectors in the example vector library with cosine similarity cos(θ)>0.7 are taken as similar examples, and thus the example list can be obtained.
[0038] Based on the relevant table field information in the example list, an example-related list is obtained. Here, due to the excessive length of the database schema in existing technologies, with numerous tables, fields, and complex relationships, the general model is prone to "schema overload" and interference from irrelevant information, leading to the misselection of unrelated tables and fields. This is especially problematic in multi-table scenarios, where the probability of table and field mismatches increases, directly impacting the accuracy of subsequent SQL generation for the large language model. Therefore, the schema can be constructed based on the table field information in the example list, i.e., "filter_tables," by combining them into the target related tables, thus obtaining the example-related list. It should be noted that if no matching example is found, the schema is constructed for the entire database.
[0039] Based on the vector representation problem, similar meta-knowledge is retrieved from the meta-knowledge vector library to obtain a meta-knowledge list. Here, after obtaining the vector representation problem, several industry-specific terms, professional terms, and specific business logic that are semantically similar to the input natural language problem can be retrieved from the meta-knowledge vector library using a cosine similarity algorithm to obtain a meta-knowledge list. The examples in the meta-knowledge vector library can include the title "title" and the meta-knowledge "content". The specific calculation process of the cosine similarity algorithm is the same as above; only the B vector needs to be replaced with the example vectors from the meta-knowledge vector library, which will not be repeated here.
[0040] The above operations retrieve similar examples from the example vector library using cosine similarity, perform a union based on the "filter_tables" field in the examples to filter tables relevant to the input natural language question, and then construct a schema based on the union of the filtered table names to exclude other irrelevant information. This reduces the cognitive load on the large language model, minimizes schema mapping errors at the source, improves the efficiency of the large language model in understanding schemas, ensures that the large language model can accurately match the target table, fields, and relationships between tables, and improves the accuracy of SQL generation in vertical domain application scenarios.
[0041] The following describes in detail step S209, namely, "scoring each valid execution result in the valid execution result set and selecting the target query statement from the corresponding candidate query statements based on the scoring results," with reference to the embodiments.
[0042] Each valid execution result in the valid execution result set is scored to obtain the total score set of valid execution results; the valid execution result with the highest total score is selected from the total score set of valid execution results, and the candidate query statement corresponding to the valid execution result with the highest total score is selected as the target query statement.
[0043] Specifically, after obtaining the set of valid execution results, each valid execution result in the set can be scored to obtain a total score for each valid execution result, forming a total score set for valid execution results. The valid execution result with the highest total score is selected from the total score set, indicating that this valid execution result is the most effective. Therefore, the candidate query statement corresponding to the valid execution result with the highest total score can be used as the target query statement.
[0044] The above operation, by scoring the valid execution results in the set of valid execution results, selecting the valid execution result with the highest total score, and using the candidate query statement corresponding to the valid execution result with the highest total score as the target query statement, achieves a query accuracy rate of over 95% in the automotive welding field database, realizing a technological breakthrough of "high accuracy, full-scenario, low cost, and easy expansion".
[0045] In some embodiments, scoring each valid execution result in the valid execution result set to obtain a total score set of valid execution results includes: scoring each valid execution result in the valid execution result set according to a preset scoring dimension to obtain a score for each valid execution result corresponding to a scoring dimension; obtaining a weighted score for each valid execution result corresponding to a scoring dimension based on the score for each valid execution result corresponding to a scoring dimension and a corresponding preset weighting ratio; obtaining a total score for each valid execution result based on the weighted score for each valid execution result corresponding to a scoring dimension; and obtaining a total score set of valid execution results based on the total score of each valid execution result.
[0046] The preset scoring dimensions include, but are not limited to, vote percentage, confidence level, semantic similarity, execution result complexity, and execution time. Valid execution results include, but are not limited to, query results, number of rows, and execution time.
[0047] In one feasible approach, based on the query results, each valid execution result in the valid execution result set is scored according to the vote percentage scoring dimension, thereby obtaining the score corresponding to the vote percentage scoring dimension for each valid execution result.
[0048] Here, after obtaining the query results for each valid execution result, the score is determined by how many valid execution results are identical. For example, if there are 6 valid execution results in the set, and 2 of them are identical, then the score for the vote percentage of these 2 identical results is 2 points, and the rest are 1 point. This process is repeated to obtain the score for each valid execution result based on its vote percentage.
[0049] By setting the above-mentioned voting percentage scoring dimensions, we can reflect the degree of consensus generated by multiple paths, which is an important manifestation of the correctness of the generated query results. This ensures that the SQL selected at the end appears most frequently in the candidate query statement set and has the best stability.
[0050] In one feasible approach, based on the number of rows in the execution result set, the query results, and the natural language question, each valid execution result in the valid execution result set is scored according to the confidence score dimension, thus obtaining the confidence score dimension score corresponding to each valid execution result.
[0051] Specifically, after obtaining each valid execution result, you can check if the number of execution result rows for each valid execution result is 0. If the number of execution result rows is 0, check if there are keywords such as "none", "does not exist", "no", "cannot be found" in the natural language question. If they exist, the confidence score dimension score of the corresponding valid execution result is increased by 0.5 points; if they do not exist, the confidence score dimension score of the corresponding valid execution result is increased by 0.1 points.
[0052] If the number of rows in the execution result is not 0, the confidence score dimension of the corresponding valid execution result will be increased by 0.3 points. At the same time, it is determined whether there are numbers in the query result. In addition, if the natural language question input contains keywords such as "how much", "quantity", "total", "average", "numerical value", etc., an additional 0.2 points will be added on the 0.3 points. That is, the confidence score dimension of the corresponding valid execution result will be 0.5 points.
[0053] It should also be noted that while ensuring the number of rows in the execution result is not zero, an excessive number of rows is undesirable. Therefore, when the number of rows in the execution result is less than 500, an additional 0.3 points are added to the above 0.5 points; when the number of rows in the execution result is less than 1000, an additional 0.2 points are added to the above 0.5 points; and when the number of rows in the execution result is less than 2000, an additional 0.1 points are added to the above 0.5 points. This process continues to obtain the confidence score for each valid execution result dimension.
[0054] By setting the confidence score dimensions as described above, we can determine the prediction probability of the large language model for the generated SQL, reflect the execution quality of the effective execution results, and ensure that the SQL not only looks fine but also produces reasonable results after execution.
[0055] In one feasible approach, based on the query results and the natural language question, each valid execution result in the valid execution result set is scored according to the semantic similarity scoring dimension, thereby obtaining the semantic similarity scoring dimension score corresponding to each valid execution result.
[0056] Specifically, the cosine similarity of each valid execution result in the effective execution result set can be calculated using the cosine similarity algorithm. The expression for the cosine similarity algorithm is the same as above, the only difference being the meaning of B. Here, B represents the vector representation of the valid execution result. After obtaining the cosine similarity, a score is calculated based on the cosine similarity value, which is the semantic similarity scoring dimension score. Assuming the cosine similarity... If the result is not 0, the score is 0.3, meaning the semantic similarity score for the corresponding valid execution result is 0.3. This process can be repeated to obtain the semantic similarity score for each valid execution result.
[0057] By using the semantic similarity scoring dimensions described above, we can measure the degree of matching between the SQL and the natural language question entered by the user, ensuring that the SQL meets the purpose of the question.
[0058] In one feasible approach, based on the query results, each valid execution result in the valid execution result set is scored according to the execution result complexity scoring dimension, thereby obtaining the score for each valid execution result corresponding to the execution result complexity scoring dimension.
[0059] Specifically, after obtaining the query results of each valid execution result, examine the keywords in the query results, such as "AND", "OR", "DISTINCT", "ORDER BY", "COUNT", "SUM", "AVG", "MAX", "MIN", "GROUP BY", "JOIN", "LEFT JOIN", "RIGHT JOIN", "INNER JOIN", "FULL JOIN", "CROSS JOIN", "OVER", and "SELECT". Here, the base score for the execution result complexity rating dimension can generally be set to 1 point. If any of "AND", "OR", or "DISTINCT" appears in the query result, add 0.5 points to the original 1 point, resulting in 1 + 0.5 = 1.5 points. If any of "ORDER BY", "COUNT", "SUM", "AVG", "MAX", "MIN", or "GROUP BY" appears in the query result, add 1 point to the original 1 point, resulting in 1 + 1 = 2 points. If any of "JOIN", "LEFT JOIN", "RIGHT JOIN", "INNER JOIN", "FULL JOIN", or "CROSS JOIN" appears in the query result, add 1.5 points to the original 1 point, resulting in 1 + 1.5 = 2.5 points. If any of "CROSS JOIN" appears in the query result... When any of the keywords "JOIN", "OVER", or "SELECT" is used, 2 points are added to the original score of 1, resulting in 1+2=3 points. The more keywords appear, the higher the score. The initial complexity score is denoted by "complexity", and the specific execution result complexity scoring dimension is denoted by "Score". The specific expression can be represented as follows: Score t =1.0 –((max(1, min(5, int(complexity + 0.5)))- 1) / (5 - 1)) *0.9; Here, further description is provided by way of example, assuming that the query statement result is SELECT * FROM person t1 WHERE t1.gender = 'male' AND t1.height>1.7 ORDER BY t1.weight DESC; It can be seen from the above rules that the base score of the execution result complexity scoring dimension is 1 point. In the above example, there is the keyword AND, so the score is 1+0.5=1.5. Meanwhile, there is also the keyword ORDER BY, so the score is 1.5+1=2.5 points. At this time, complexity = 2.5 points, which is substituted into the expression of the execution result complexity scoring dimension score Score c =1.0 –((max(1, min(5, int(2.5 + 0.5)))- 1) / (5 - 1)) * 0.9=0.55; By analogy, the execution result complexity scoring dimension score corresponding to each valid execution result can be obtained.
[0060] Through the above setting of the execution result complexity scoring dimension, it can be illustrated that the more SQL keywords there are, the higher the complexity and the lower the score, which can further reflect the simplicity and standardization of valid execution result generation, and achieve the effects of easy maintenance and high performance.
[0061] In an implementable manner, each valid execution result in the valid execution result set is scored according to the execution time and in the execution time scoring dimension, so as to obtain the execution time scoring dimension score corresponding to each valid execution result.
[0062] Specifically, after obtaining the execution time of each valid execution result, first, a very small positive number is set, which is assumed to be represented by epsilon, where epsilon=1e-9. This setting is mainly to avoid the error of division by zero when the execution time is 0; second, the reciprocal of the execution time of each valid execution result is taken to obtain the reciprocal of the execution time of each valid execution result. The shorter the execution time, the larger the reciprocal, and the higher the subsequent score; then, the maximum value and the minimum value among the reciprocals of the execution time of all valid execution results are found; the reciprocal range is calculated, that is, the difference between the maximum value and the minimum value is calculated. If the range is too small, a very small value is set, which is assumed to be represented by minimum, where minimum=1e-6. This setting is to prevent the subsequent division by zero; finally, the execution time scoring dimension score is calculated, that is, the difference obtained by subtracting the minimum reciprocal from the current reciprocal of the execution time of each valid execution result is divided by the reciprocal range, and the specific expression can be expressed as follows: Score t=(1.0 / (execution time + minimum positive number) - minimum reciprocal) / reciprocal range; Here, we further illustrate this with examples. Assume there are 3 valid execution results, represented by SQL: SQL1: 0.1 seconds (fastest), SQL2: 0.5 seconds, and SQL3: 1.0 seconds (slowest). Calculate the reciprocals of each SQL statement, i.e., reciprocals = [10, 2, 1]. As mentioned earlier, the maximum and minimum reciprocals are max=10 and min=1, respectively, so the reciprocal range is 10-1=9. The final scores are: SQL1: (10-1) / 9 = 1.0; SQL2: (2-1) / 9 = 0.11; SQL3: (1-1) / 9 = 0.0. And so on, we can obtain the execution time score for each valid execution result.
[0063] By setting the above execution time scoring dimensions, it can be seen that the shorter the SQL execution time, the higher the score, so as to achieve the effect of selecting fast execution time and high execution efficiency.
[0064] After obtaining the scores for each valid execution result across all scoring dimensions, a weighted score for each valid execution result across all scoring dimensions is calculated based on these scores and the corresponding preset weighting ratios. It should be noted that the number of scoring dimensions and the corresponding preset weighting ratios vary, and can be adjusted according to the business scenario and the quality of the library schema design; no specific restrictions are imposed here. Generally, the vote percentage scoring dimension and the result confidence scoring dimension can be considered mandatory to effectively reduce the generation error of large language models.
[0065] Here, we will only present an example that includes the following five scoring dimensions: vote percentage, confidence score, semantic similarity score, execution result complexity score, and execution time score. Assuming all five scoring dimensions are included, the corresponding preset weighting ratios can be set as follows: 35% for the vote percentage scoring dimension, 25% for the confidence score, 20% for the semantic similarity score, 15% for the execution result complexity score, and 5% for the execution time score. That is, the weighted score for the vote percentage scoring dimension is: vote percentage score * 35%; the weighted score for the confidence score is: confidence score * 25%; the weighted score for the semantic similarity score is: semantic similarity score * 20%; the weighted score for the execution result complexity score is: execution result complexity score * 15%; and the weighted score for the execution time score is: execution time score * 5%.
[0066] The total score for each valid execution result is obtained by weighting the scores according to the scoring dimensions corresponding to each valid execution result. The specific expression can be represented as follows: Total score for valid execution results = (Vote percentage score * 35%) + (Confidence score * 25%) + (Semantic similarity score * 20%) + (Execution result complexity score * 15%) + (Execution time score * 5%) By analogy, the total score for each valid execution result can be obtained.
[0067] Based on the total score of each valid execution result, a total score set of valid execution results is obtained. Here, after obtaining the total score of each valid execution result, a total score set of valid execution results is formed.
[0068] The above operations score each valid execution result in the effective execution result set according to different preset scoring dimensions to obtain the total score set of valid execution results. This enables the filtering of each valid execution result across multiple scoring dimensions, resulting in higher accuracy of the final selected optimal valid execution result. Furthermore, the setting of different weight ratios follows the principle of prioritizing correctness, followed by semantic matching, supplementing with simplicity, and complementing with efficiency, providing further assurance for the final target query statement.
[0069] Based on the implementation methods in the above embodiments, the following will be combined with... Figure 3 A preferred method flow provided in an embodiment of this application will be described by way of example. For instance... Figure 3 As shown, the method may include the following steps: Step S301: Receive the natural language question sent by the user terminal.
[0070] Step S303: Input the natural language problem into the text embedding model to obtain the vector representation problem.
[0071] Step S305: Based on the vector representation problem, retrieve matching similar examples from the example vector library to obtain an example list.
[0072] Step S307: Obtain the example-related list based on the relevant table field information in the example list.
[0073] Step S3099: Based on the vector representation problem, retrieve matching similar meta-knowledge from the meta-knowledge vector library to obtain a meta-knowledge list.
[0074] Step S311: Construct structured prompt words based on the example related list and the meta-knowledge list.
[0075] Step S313: Input the structured prompts into the large language model to generate a set of candidate query statements.
[0076] Step S315: Execute each candidate query statement in the candidate query statement set to obtain a valid execution result set.
[0077] Step S317: Score each valid execution result in the valid execution result set according to the preset scoring dimensions to obtain the score of each valid execution result corresponding to the scoring dimension; wherein, the preset scoring dimensions include any one or more of the following: vote percentage scoring dimension, confidence scoring dimension, semantic similarity scoring dimension, execution result complexity scoring dimension, and execution time scoring dimension.
[0078] Step S319: Based on the scores of each valid execution result corresponding to the scoring dimension and the corresponding preset weighting ratio, obtain the weighted score of each valid execution result corresponding to the scoring dimension.
[0079] Step S321: Weight the scores according to the scoring dimensions corresponding to each valid execution result to obtain the total score for each valid execution result.
[0080] Step S323: Based on the total score of each valid execution result, obtain the total score set of valid execution results.
[0081] Step S325: Filter the valid execution result with the highest total score from the set of valid execution results, and use the candidate query statement corresponding to the valid execution result with the highest total score as the target query statement.
[0082] It should be understood that, although Figures 2-3The steps in the flowchart are shown sequentially as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless explicitly stated in this application, there is no strict order restriction on the execution of these steps, and they can be executed in other orders. Furthermore, Figures 2-3 At least some of the steps in the process may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily completed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least some of the sub-steps or stages of other steps.
[0083] Figure 4 This is a schematic diagram of a query statement generation device provided in an embodiment of this application. The device can be disposed in... Figure 1 The query server in the system shown is used to perform tasks such as... Figure 2 , Figure 3 The method flow is shown below. Figure 4 As shown, the device may include: a response unit 401, a construction unit 403, a generation unit 405, an execution unit 407, and a filtering unit 409. The main functions of each component module are as follows: The response unit 401 is used to receive natural language questions sent by the user terminal, input the natural language questions into the text embedding model, and obtain an example-related list and a meta-knowledge list. Construction unit 403 is used to construct structured prompt words based on the example-related list and the meta-knowledge list; The generation unit 405 is used to input structured prompt words into the large language model and generate a set of candidate query statements; Execution unit 407 is used to execute each candidate query statement in the candidate query statement set to obtain a valid execution result set; The filtering unit 409 is used to score each valid execution result in the valid execution result set, and to filter the target query statement from the corresponding candidate query statements based on the scoring results.
[0084] In some embodiments, the response unit 401 is further configured to: By inputting natural language problems into a text embedding model, we obtain a vector representation problem; Based on the vector representation problem, a list of similar examples is obtained by retrieving matching examples from the example vector library. Based on the relevant table field information in the example list, obtain the example-related list; Based on the vector representation problem, matching similar meta-knowledge is retrieved from the meta-knowledge vector database to obtain a list of meta-knowledge.
[0085] In some embodiments, the filtering unit 409 is further configured to: Each valid execution result in the valid execution result set is scored to obtain the total score set of valid execution results; Filter the valid execution results with the highest total score from the set of valid execution results, and use the candidate query statement corresponding to the valid execution result with the highest total score as the target query statement.
[0086] In some embodiments, the filtering unit 409 is further configured to: Each valid execution result in the valid execution result set is scored according to the preset scoring dimensions to obtain the score corresponding to each valid execution result in the scoring dimension. Based on the scores of each valid execution result in the corresponding scoring dimension and the corresponding preset weighting ratio, the weighted score of each valid execution result in the corresponding scoring dimension is obtained; The total score for each valid execution result is obtained by weighting the scores according to the scoring dimensions corresponding to each valid execution result; Based on the total score of each valid execution result, the total score set of valid execution results is obtained.
[0087] In some embodiments, the preset scoring dimensions of the device include any one or more of the following: vote percentage scoring dimension, confidence scoring dimension, semantic similarity scoring dimension, execution result complexity scoring dimension, and execution time scoring dimension.
[0088] In some embodiments, the valid execution result includes the query statement result and the number of rows of execution result. The filtering unit 409 is also used for: Based on the query results, each valid execution result in the valid execution result set is scored according to the vote percentage scoring dimension, resulting in a score for each valid execution result corresponding to the vote percentage scoring dimension, and / or... Based on the number of rows in the execution result set, the query results, and the natural language questions, each valid execution result in the valid execution result set is scored according to the confidence score dimension, resulting in a score for each valid execution result corresponding to the confidence score dimension, and / or, Based on the query results and natural language questions, each valid execution result in the valid execution result set is scored according to the semantic similarity scoring dimension, and the corresponding semantic similarity scoring dimension score for each valid execution result is obtained.
[0089] In some embodiments, the effective execution result also includes execution time, and the filtering unit 409 is further configured to: Based on the query results, each valid execution result in the valid execution result set is scored according to the execution result complexity scoring dimension, resulting in a score for each valid execution result corresponding to the execution result complexity scoring dimension, and / or... Based on the execution time, each valid execution result in the valid execution result set is scored according to the execution time scoring dimension, and the corresponding score for each valid execution result is obtained.
[0090] The same or similar parts among the above embodiments can be referred to interchangeably. Each embodiment focuses on describing the differences from other embodiments. In particular, the device embodiments are basically similar to the method embodiments, so the description is relatively simple, and the relevant parts can be referred to the description of the method embodiments.
[0091] It should be noted that the embodiments of this application may involve the use of user data. In practical applications, user-specific personal data may be used in the scheme described herein within the scope permitted by applicable laws and regulations, provided that it complies with the applicable laws and regulations of the country (e.g., explicit consent from the user, actual notification to the user, explicit authorization from the user, etc.).
[0092] According to embodiments of this application, this application also provides a computer device and a computer-readable storage medium.
[0093] like Figure 5 The diagram shown is a block diagram of a computer device according to an embodiment of this application. The term "computer device" is intended to represent various forms of digital computers or mobile devices. The digital computer may include a desktop computer, a portable computer, a workbench, a personal digital assistant, a server, a mainframe computer, and other suitable computers. The mobile device may include a tablet computer, a smartphone, a wearable device, etc.
[0094] like Figure 5 As shown, device 500 includes a computing unit 501, a ROM 502, a RAM 503, a bus 504, and an input / output (I / O) interface 505. The computing unit 501, ROM 502, and RAM 503 are interconnected via the bus 504. The input / output (I / O) interface 505 is also connected to the bus 504.
[0095] The computing unit 501 can execute various processes in the method embodiments of this application according to computer instructions stored in the read-only memory (ROM) 502 or computer instructions loaded from the storage unit 508 into the random access memory (RAM) 503. The computing unit 501 can be various general-purpose and / or special-purpose processing components with processing and computing capabilities. The computing unit 501 can include, but is not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any suitable processor, controller, microcontroller, etc. In some embodiments, the methods provided in the embodiments of this application can be implemented as computer software programs, which are tangibly contained in a computer-readable storage medium, such as the storage unit 508.
[0096] RAM 503 can also store various programs and data required for the operation of device 500. Part or all of the computer program can be loaded and / or installed on device 500 via ROM 502 and / or communication unit 509.
[0097] The input unit 506, output unit 507, storage unit 508, and communication unit 509 in device 500 can be connected to I / O interface 505. The input unit 506 can be, for example, a keyboard, mouse, touchscreen, or microphone; the output unit 507 can be, for example, a display, speaker, or indicator light. Device 500 can exchange information and data with other devices through the communication unit 509.
[0098] It should be noted that the device may also include other components necessary for normal operation. It may also include only the components necessary for implementing the solution of this application, without necessarily including all the components shown in the figures.
[0099] Various implementations of the systems and techniques described herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SOCs), payload programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof.
[0100] The computer instructions used to implement the methods of this application may be written in any combination of one or more programming languages. These computer instructions may be provided to the computing unit 501 such that when executed by the computing unit 501, such as a processor, the computer instructions cause the execution of the steps involved in the embodiments of the methods of this application.
[0101] The computer-readable storage medium provided in this application can be a tangible medium that can contain or store computer instructions for performing the steps involved in the method embodiments of this application. The computer-readable storage medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, and other forms of storage media.
[0102] The specific embodiments described above do not constitute a limitation on the scope of protection of this application. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this application should be included within the scope of protection of this application.
Claims
1. A method for generating query statements, characterized in that, The method includes: Receive natural language questions sent by user terminals, input the natural language questions into a text embedding model, and obtain an example-related list and a meta-knowledge list; Based on the example-related list and the meta-knowledge list, construct structured prompt words; The structured prompts are input into a large language model to generate a set of candidate query statements; Execute each of the candidate query statements in the candidate query statement set to obtain a valid execution result set; Each valid execution result in the set of valid execution results is scored, and the target query statement is selected from the corresponding candidate query statements based on the score results.
2. The method according to claim 1, characterized in that, The natural language problem is input into a text embedding model to obtain an example-related list and a meta-knowledge list, including: The natural language problem is input into the text embedding model to obtain the vector representation problem; Based on the vector representation problem, similar examples are retrieved from the example vector library to obtain an example list; Based on the relevant table field information in the example list, obtain the example-related list; Based on the vector representation problem, matching similar meta-knowledge is retrieved from the meta-knowledge vector library to obtain a meta-knowledge list.
3. The method according to claim 1, characterized in that, The step of scoring each valid execution result in the valid execution result set and selecting the target query statement from the corresponding candidate query statements based on the scoring results includes: Each valid execution result in the set of valid execution results is scored to obtain a total score set of valid execution results; The valid execution result with the highest total score is selected from the set of valid execution results, and the candidate query statement corresponding to the valid execution result with the highest total score is selected as the target query statement.
4. The method according to claim 3, characterized in that, The step of scoring each valid execution result in the set of valid execution results to obtain a total score set of valid execution results includes: Each valid execution result in the set of valid execution results is scored according to a preset scoring dimension to obtain a score for each valid execution result corresponding to the scoring dimension. Based on the scores of each effective execution result corresponding to the scoring dimension and the corresponding preset weighting ratio, the weighted score of each effective execution result corresponding to the scoring dimension is obtained; The total score for each effective execution result is obtained by weighting the scores according to the scoring dimensions corresponding to each effective execution result. The total score set of valid execution results is obtained based on the total score of each valid execution result.
5. The method according to claim 4, characterized in that, The preset scoring dimensions include any one or more of the following: vote percentage scoring dimension, confidence scoring dimension, semantic similarity scoring dimension, execution result complexity scoring dimension, and execution time scoring dimension.
6. The method according to claim 5, characterized in that, The valid execution results include the query statement results and the number of rows of execution results. The step of scoring each valid execution result in the valid execution result set according to a preset scoring dimension to obtain a score for each valid execution result corresponding to that dimension includes: Based on the query results, each valid execution result in the valid execution result set is scored according to the vote percentage scoring dimension, resulting in a score for each valid execution result corresponding to the vote percentage scoring dimension, and / or, Based on the number of rows in the execution result set, the query result, and the natural language question, each valid execution result in the valid execution result set is scored according to the confidence score dimension, resulting in a confidence score dimension score for each valid execution result, and / or... Based on the query results and the natural language question, each valid execution result in the valid execution result set is scored according to the semantic similarity scoring dimension to obtain the semantic similarity scoring dimension score corresponding to each valid execution result.
7. The method according to claim 6, characterized in that, The effective execution result also includes execution time. The step of scoring each effective execution result in the effective execution result set according to a preset scoring dimension to obtain a score corresponding to each effective execution result further includes: Based on the query results, each valid execution result in the valid execution result set is scored according to the execution result complexity scoring dimension, resulting in a score for each valid execution result corresponding to the execution result complexity scoring dimension, and / or, Based on the execution time, each valid execution result in the set of valid execution results is scored according to the execution time scoring dimension to obtain the score corresponding to the execution time scoring dimension for each valid execution result.
8. A query statement generation device, characterized in that, The device includes: The response unit is used to receive natural language questions sent by the user terminal, input the natural language questions into the text embedding model, and obtain an example-related list and a meta-knowledge list. The construction unit is used to construct structured prompt words based on the example-related list and the meta-knowledge list; The generation unit is used to input the structured prompt words into the large language model and generate a set of candidate query statements; An execution unit is used to execute each of the candidate query statements in the candidate query statement set to obtain a valid execution result set; The filtering unit is used to score each of the valid execution results in the set of valid execution results, and to filter out the target query statement from the corresponding candidate query statements based on the scoring results.
9. A computer device, characterized in that, include: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores computer instructions executable by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the method of any one of claims 1-7.
10. A computer-readable storage medium storing computer instructions thereon, characterized in that, The computer instructions are used to cause the computer to perform the method according to any one of claims 1 to 7.
