A text-based SQL statement acquisition method, device and medium
By standardizing user text and reusing abstract syntax trees, combined with an LLM model and the Apache Calcite tool to generate SQL statements, the unpredictability problem of pre-trained Text2SQL models is solved, and the accuracy and precision of SQL statements are improved.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-06
- Publication Date
- 2026-04-10
AI Technical Summary
Pre-trained Text2SQL models are sensitive to the wording, word order, or synonyms of the input text, which can lead to the generation of incorrect or logically flawed SQL statements, affecting the accuracy of the generated SQL statements.
The initial text of the target user is standardized by the first LLM model. If a matching abstract syntax tree exists in the preset standard text or historical text, it is used as the target abstract syntax tree. Otherwise, the target abstract syntax tree is generated by the second LLM model. The SQL statement is generated by combining the Apache Calcite tool. The validated syntax tree is reused first to reduce model calls.
It effectively reduces SQL errors caused by model illusion or input sensitivity, improves the accuracy of generated SQL statements, and achieves minimal dependence on large model calls.
Smart Images

Figure CN121070962B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of natural language processing, and in particular to a text-based SQL statement acquisition method, device and medium. BACKGROUND
[0002] With the rapid development of artificial intelligence technology, especially the rise of large language models (LLM), natural language processing technology has been widely applied in various fields, such as the civil aviation field. Generating SQL statements based on natural language significantly improves the efficiency of data query and analysis. In the prior art, the method of generating SQL statements based on natural language usually relies on a pre-trained Text2SQL model. The text is input into the pre-trained Text2SQL model to directly generate the corresponding SQL statement.
[0003] However, the above method has the following technical problems:
[0004] The pre-trained Text2SQL model has strong unpredictability and is sensitive to minor changes in wording, order or synonymous expressions of input text, which may result in generating completely different or even incorrect SQL statements. Moreover, the pre-trained Text2SQL model may generate SQL statements that are grammatically correct but logically incorrect, i.e., "model hallucination", which easily affects the accuracy of the generated SQL statements. SUMMARY
[0005] To solve the above technical problems, the technical solution adopted by the present application is as follows:
[0006] According to a first aspect of the present application, a text-based SQL statement acquisition method is provided, which comprises the following steps:
[0007] S1, standardizing the initial text A corresponding to the target user through a first LLM model to obtain the standard text B corresponding to A.
[0008] S2, if there is a preset standard text identical to B in the preset standard text list, then the first abstract syntax tree corresponding to the preset standard text identical to B is taken as the target abstract syntax tree corresponding to B; if there is a historical text identical to B in the historical text list, then the second abstract syntax tree corresponding to the historical text identical to B is taken as the target abstract syntax tree corresponding to B; if there is no preset standard text identical to B in the preset standard text list and no historical text identical to B in the historical text list, then B is input into a second LLM model to obtain the target abstract syntax tree corresponding to B.
[0009] S3, obtaining the SQL statement corresponding to A according to the target abstract syntax tree corresponding to B and sending it to the target user.
[0010] According to a second aspect of the present application, there is provided a non-transitory computer-readable storage medium, storing a computer program, which is loaded and executed by a processor to implement the method described above.
[0011] According to a third aspect of the present application, there is provided an electronic device, comprising a processor, a memory, and a computer program stored on the memory and executable on the processor, wherein the processor implements the method described above when executing the computer program.
[0012] The present application has at least the following beneficial effects:
[0013] The present application provides a text-based SQL statement acquisition method, device and medium, the method comprises the following steps: a first LLM model is used to standardize an initial text corresponding to a target user to obtain a standard text corresponding to the initial text; if a preset standard text list contains a preset standard text identical to the standard text, a first abstract syntax tree corresponding to the preset standard text identical to the standard text is taken as a target abstract syntax tree corresponding to the standard text; if a historical text list contains a historical text identical to the standard text, a second abstract syntax tree corresponding to the historical text identical to the standard text is taken as the target abstract syntax tree corresponding to the standard text; if the preset standard text list does not contain a preset standard text identical to the standard text and the historical text list does not contain a historical text identical to the standard text, the standard text is input into a second LLM model to obtain a target abstract syntax tree corresponding to the standard text; a SQL statement corresponding to the initial text is obtained according to the target abstract syntax tree corresponding to the standard text and sent to the target user. It can be seen that the present application preferentially determines the target abstract syntax tree from the first abstract syntax trees corresponding to a plurality of preset standard texts and the second abstract syntax trees corresponding to a plurality of historical texts; when the target abstract syntax tree cannot be determined from the first abstract syntax trees corresponding to a plurality of preset standard texts and the second abstract syntax trees corresponding to a plurality of historical texts, the second LLM model is used to obtain the target abstract syntax tree, and the SQL statement is obtained according to the target abstract syntax tree; the minimization of large model calling is realized, and the LLM model is not completely relied on, which effectively reduces SQL errors caused by model hallucination or input sensitivity, and is beneficial to improving the accuracy of the generated SQL statement. BRIEF DESCRIPTION OF DRAWINGS
[0014] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings needed in the embodiment description will be briefly introduced as follows. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can also be obtained by those skilled in the art without creative labor.
[0015] Figure 1 A flowchart of a text-based SQL statement acquisition method provided by an embodiment of the present application is shown in the following. DETAILED DESCRIPTION
[0016] The technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings of the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work belong to the scope of protection of the present application.
[0017] It should be noted that the terms "first", "second", and the like in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar tasks, and do not necessarily indicate a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "include" and "have" and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, product or server including a series of steps or units does not necessarily limit to those steps or units clearly listed, but can include other steps or units not clearly listed or inherent to these processes, methods, products or devices.
[0018] Embodiments of the present application provide a text-based SQL statement acquisition method, which comprises the following steps, as shown in the following. Figure 1
[0019] S1, standardizing the initial text A corresponding to the target user by a first LLM model to obtain the standard text B corresponding to A.
[0020] Specifically, step S1 can be understood as: inputting A into the first LLM model to make the first LLM model standardize A to generate B and output; wherein the first LLM model performs standardization processing based on a plurality of keyword mapping combinations in the first database, each keyword mapping combination includes a non-standard keyword and a standard keyword corresponding to the non-standard keyword; wherein the non-standard keyword and the standard keyword corresponding to the non-standard keyword are both keywords determined in advance by those skilled in the art based on the target field, which will not be described here; the target field is a field determined in advance by those skilled in the art, for example: civil aviation field.
[0021] Specifically, the target user can be understood as a user who currently needs to acquire a SQL statement.
[0022] In a specific embodiment, step S1 can be replaced by: performing conditional integrity detection on A by the first LLM model, if the missing condition is detected, sending an information supplement prompt to the target user, and generating B based on the supplement content of A and the target user feedback.
[0023] By the above steps, the initial text is processed to obtain the standard text corresponding to the initial text. It can be understood that the initial text is converted into a standard text with unified format, vocabulary and structure, the user's ambiguous expression is converted into a more accurate language description, the influence of expression diversity on obtaining the SQL statement is eliminated, and the accuracy of the obtained SQL statement is improved.
[0024] S2, if there is a preset standard text identical to B in the preset standard text list, the first abstract syntax tree corresponding to the preset standard text identical to B is taken as the target abstract syntax tree corresponding to B; if there is a historical text identical to B in the historical text list, the second abstract syntax tree corresponding to the historical text identical to B is taken as the target abstract syntax tree corresponding to B; if there is no preset standard text identical to B in the preset standard text list and there is no historical text identical to B in the historical text list, B is input into the second LLM model to obtain the target abstract syntax tree corresponding to B.
[0025] Specifically, the preset standard text list includes a plurality of preset standard texts; wherein each preset standard text corresponds to a preset standard SQL statement; for each preset standard text, the abstract syntax tree corresponding to the preset standard SQL statement corresponding to the preset standard text is taken as the first abstract syntax tree corresponding to the preset standard text; any method for obtaining an abstract syntax tree corresponding to a SQL statement in the prior art belongs to the protection scope of the present application, for example: using the SQL Parser parser in Apache Calcite to parse the SQL statement, and converting the SQL statement into an abstract syntax tree, wherein Apache Calcite is an open source dynamic data management framework; it can be understood as a query engine; details are not repeated here.
[0026] Specifically, the historical text list includes a plurality of historical texts; wherein each historical text corresponds to a historical SQL statement; for each historical text, the abstract syntax tree corresponding to the historical SQL statement corresponding to the historical text is taken as the second abstract syntax tree corresponding to the historical text; the method for obtaining the abstract syntax tree corresponding to the historical SQL statement is the same as the method for obtaining the abstract syntax tree corresponding to the preset standard SQL statement, and details are not repeated here.
[0027] Specifically, the preset standard text is completely the same as B, indicating that the length of the preset standard text is the same as the length of B, and the jth character in the preset standard text is the same as the jth character in B, 1≤j≤n, n being the number of characters in B.
[0028] Further, the historical text is completely the same as B, indicating that the length of the historical text is the same as the length of B, and the jth character in the historical text is the same as the jth character in B.
[0029] In a specific embodiment, the first LLM model, the second LLM model and the third LLM model are the same LLM model, wherein the first, the second and the third are only used to distinguish the roles of the LLM model when performing different tasks.
[0030] In a specific embodiment, the first LLM model, the second LLM model and the third LLM model are three independent LLM models.
[0031] Specifically, the second LLM model is a model obtained by supervised fine-tuning a large language model focusing on structured output using a training sample set; for example, a Code Llama-13B model is fine-tuned using a training sample set to obtain the second LLM model; the supervised fine-tuning can be LoRA fine-tuning; that is, the general semantic understanding ability of the large language model is retained, and the structured generation performance of the large language model on specific tasks is enhanced.
[0032] Further, in the method, the following step of constructing a training sample set is further included:
[0033] S10, obtaining a specific SQL statement list, the specific SQL statement list including a plurality of specific SQL statements, the specific SQL statements being determined from an open source SQL data set, an internal SQL corpus and a historical SQL database, wherein the internal SQL corpus includes SQL statements accumulated in the business execution process of the target field for a long time; the historical SQL database includes historical SQL statements, which can be understood as SQL statements sent to users before the current time point.
[0034] S20, obtaining an abstract syntax tree corresponding to each specific SQL statement.
[0035] S30, obtaining a list of translated texts corresponding to each specific SQL statement, wherein when the specific SQL statement is derived from the historical SQL database, the list of translated texts corresponding to the specific SQL statement includes three translated texts, wherein the first translated text is obtained by manual annotation, the second translated text is obtained by the fourth LLM model, and the third translated text is the historical text corresponding to the specific SQL statement; when the specific SQL statement is derived from the open source SQL dataset or the internal SQL corpus, the list of translated texts corresponding to the specific SQL statement includes two translated texts, wherein the first translated text is obtained by manual annotation, and the second translated text is obtained by the fourth LLM model.
[0036] In one specific embodiment, the fourth LLM model is the same as the first LLM model, the second LLM model and the third LLM model, wherein the fourth is only used to distinguish the roles of the LLM model in different tasks performed.
[0037] In one specific embodiment, the fourth LLM model is an LLM model different from the first LLM model, the second LLM model and the third LLM model.
[0038] S40, for each specific SQL statement, taking the translated text corresponding to the specific SQL statement as an input feature, and taking the abstract syntax tree corresponding to the specific SQL statement as an output feature label of the input feature to construct a training sample set; wherein the training sample set includes a plurality of training samples, each training sample includes an input feature and an output label corresponding to the input feature.
[0039] Through the above steps, a plurality of specific SQL statements are determined from the open source SQL dataset, the internal SQL corpus and the historical SQL database, a training sample set is constructed according to the abstract syntax tree corresponding to the SQL statement and the translated text corresponding to the SQL statement, and the training sample set fuses artificial annotation data, LLM model generated data and historical real data; based on the training sample set, the large language model focusing on structured output is supervised fine-tuned to obtain the second LLM model, which can make the second LLM model more accurately understand complex and non-standard natural language input, generate correct abstract syntax trees, significantly enhance the robustness and anti-interference ability of the second LLM model to user diversified expression, and provide a good foundation for subsequent SQL statement generation.
[0040] S3, obtaining the SQL statement corresponding to A according to the target abstract syntax tree corresponding to B and sending to the target user; any method for obtaining the SQL statement according to the abstract syntax tree in the prior art belongs to the protection scope of the present application, for example: using the Query Optimizer optimizer in Apache Calcite to convert the abstract syntax tree into a logical execution plan and optimizing it, and then using the SQL Generator generator in Apache Calcite to reversely convert the optimized logical execution plan into the SQL statement; details are not repeated here.
[0041] Through the above steps, if there is a preset standard text identical to the standard text in the preset standard text list, the first abstract syntax tree corresponding to the preset standard text identical to the standard text is taken as the target abstract syntax tree corresponding to the standard text; if there is a history text identical to the standard text in the history text list, the second abstract syntax tree corresponding to the history text identical to the standard text is taken as the target abstract syntax tree corresponding to the standard text; if there is no preset standard text identical to the standard text in the preset standard text list and there is no history text identical to the standard text in the history text list, the standard text is input into the second LLM model to obtain the target abstract syntax tree corresponding to the standard text; the target abstract syntax tree corresponding to the standard text is obtained; the SQL statement corresponding to the initial text is obtained and sent to the target user; the target abstract syntax tree is determined from the first abstract syntax trees corresponding to the several preset standard texts and the second abstract syntax trees corresponding to the several history texts, the first abstract syntax tree or the second abstract syntax tree that has been verified and ensured to pass the verification is taken as the target abstract syntax tree, the reuse of the abstract syntax tree is realized; when the target abstract syntax tree cannot be determined from the first abstract syntax trees corresponding to the several preset standard texts and the second abstract syntax trees corresponding to the several history texts, the target abstract syntax tree is obtained through the second LLM model, and the SQL statement is obtained according to the target abstract syntax tree; the minimization of the large model calling is realized and the LLM model is not completely relied on, the SQL error caused by the model illusion or the input sensitivity is effectively reduced, and the accuracy of the generated SQL statement is improved.
[0042] Specifically, after step S3, it further includes:
[0043] S4, obtaining the feedback score of the target user, the value range of the feedback score being 0 to 10.
[0044] S5. If the target user's feedback score is not less than the preset feedback score, then the SQL statement corresponding to A is taken as the historical SQL statement, A is taken as the historical text corresponding to the historical SQL statement, and the historical SQL statement and the historical text corresponding to the historical SQL statement are stored in the historical SQL database.
[0045] Specifically, the preset feedback score ranges from 8 to 10.
[0046] Through the above steps, when the target user's feedback score is not less than the preset feedback score, it indicates that the target user is relatively satisfied with the received SQL statement, and the SQL statement has reference or reuse value. Therefore, when the target user's feedback score is not less than the preset feedback score, the SQL statement is used as a historical SQL statement, and the initial text corresponding to the SQL statement is stored in the historical database as the historical text corresponding to the historical SQL statement to achieve reuse. Otherwise, it indicates that the target user is not satisfied with the received SQL statement, and the SQL statement is likely an incorrect SQL statement with no reference or reuse value.
[0047] Specifically, step S3 includes the following steps:
[0048] S31. Generate a list of SQL statements C=(C1, C2, ..., C...) based on the target abstract syntax tree corresponding to B. i , ..., C m ), C i Let m be the i-th SQL statement corresponding to B, where 1 ≤ i ≤ m, and m is the number of SQL statements corresponding to B. Each SQL statement in C corresponds to a different database management system. For example, if m = 3, C1 corresponds to MySQL, C2 corresponds to Oracle, and C3 corresponds to PostgreSQL.
[0049] S32, C i Input into the third LLM model to obtain C i The corresponding target text D i .
[0050] S33, Obtain D i The vector similarity E between the semantic feature vector of A and the semantic feature vector of A i Among them, E i The larger D is i The more similar the semantic feature vector of A is to the semantic feature vector of A, the better.
[0051] Specifically, 0≤E i ≤1.
[0052] In one specific embodiment, the vector distance between the semantic feature vector of the target text and the semantic feature vector of the initial text is converted into a value between 0 and 1, and the value is used as the vector similarity between the semantic feature vector of the target text and the semantic feature vector of the initial text. The smaller the vector distance, the closer the value between 0 and 1 obtained from the vector distance conversion is to 1; the larger the vector distance, the closer the value between 0 and 1 obtained from the vector distance conversion is to 0. In the prior art, any method that converts the vector distance into a value between 0 and 1 such that the smaller the vector distance, the closer the value between 0 and 1 obtained from the vector distance conversion is to 1; and the larger the vector distance, the closer the value between 0 and 1 obtained from the vector distance conversion is to 0, falls within the protection scope of this invention and will not be elaborated further here.
[0053] S34. If E1, E2, ..., E i , ..., E m The variance is not greater than the preset variance and E1, E2, ..., E i , ..., E m If none of them are less than a preset similarity threshold, then from C1, C2, ..., C i , ..., C m The SQL statement corresponding to A is determined and sent to the target user.
[0054] Specifically, in step S34, if the target user specifies a database management system, then C1, C2, ..., C i , ..., C m The SQL statement corresponding to the database management system specified by the target user is determined as the SQL statement corresponding to A; if the target user does not specify a database management system, then C1, C2, ..., C... i , ..., C m The SQL statement corresponding to the preset database management system is determined as the SQL statement corresponding to A.
[0055] Specifically, the preset similarity threshold ranges from 0.9 to 1.
[0056] Specifically, the preset variance is a variance that is pre-set by those skilled in the art according to actual needs, such as 0.02, 0.04, or 0.06, which will not be elaborated here.
[0057] In a specific embodiment, if E1, E2, ..., E i , ..., E m The variance is greater than the preset variance or E1, E2, ..., E i , ..., E m If there are vectors with similarity less than a preset similarity threshold, then max(E1, E2, ..., E...) will be used.i , …, E m ) the corresponding target text as B and enter step S2; wherein max(·) is a maximum value acquisition function.
[0058] Through the above steps, the target abstract syntax tree corresponding to the target abstract syntax tree is generated according to the target abstract syntax tree, and the target text corresponding to each SQL statement of the target abstract syntax tree is obtained through the third LLM model; the vector similarity between the semantic feature vector of each target text and the semantic feature vector of the initial text is obtained, the greater the vector similarity, the more similar the semantic feature vector of the target text and the semantic feature vector of the initial text, and the more similar the target text and the initial text; Further, if the variance of the vector similarity between the semantic feature vector of all target texts and the semantic feature vector of the initial text is not greater than the preset variance, it means that the difference between the vector similarity between the semantic feature vector of all target texts and the semantic feature vector of the initial text is small; Therefore, if the variance of the vector similarity between the semantic feature vector of all target texts and the semantic feature vector of the initial text is not greater than the preset variance and the vector similarity between the semantic feature vector of each target text and the semantic feature vector of the initial text is not less than the preset similarity, it means that each SQL statement corresponding to the target abstract syntax tree is a correct and usable SQL statement, and the SQL statement corresponding to the initial text can be determined from all SQL statements corresponding to the target abstract syntax tree, otherwise, there are some vector similarities with large differences between the vector similarity between the semantic feature vector of all target texts and the semantic feature vector of the initial text or there are target texts that are not similar to the initial text, there may be incorrect or unusable SQL statements in the several SQL statements corresponding to the target abstract syntax tree, at this time, the SQL statement corresponding to the initial text cannot be determined from all SQL statements corresponding to the target abstract syntax tree; Further, the SQL statement corresponding to the initial text needs to be obtained, which is beneficial to improve the accuracy of the obtained SQL statement.
[0059] Further, the abstract syntax tree has good database independence and can be used as a unified intermediate representation; the same abstract syntax tree can be flexibly generated to generate SQL statements that conform to the syntax specifications of different database management systems, significantly reducing the adaptation and maintenance costs in cross-database scenarios.
[0060] Specifically, in step S2, inputting B into the second LLM model to obtain the target abstract syntax tree corresponding to B includes the following steps:
[0061] S21, input B into the second LLM model to obtain the abstract syntax tree output by the second LLM model.
[0062] S22, check the abstract syntax tree output by the second LLM model, and obtain a check result of the abstract syntax tree output by the second LLM model; any method for checking an abstract syntax tree and obtaining a check result in the prior art belongs to the protection scope of the present application, for example: using the SQL Validator checker in Apache Calcite to check the abstract syntax tree to obtain the check result of the abstract syntax tree; details are not repeated here.
[0063] In a specific embodiment, the abstract syntax tree output by the second LLM model needs to be checked in combination with a target field database, the target field database including table structure metadata of the target field and dimension-related data of the target field, and being used to verify the semantic legality of the abstract syntax tree in the scenario of the target field.
[0064] S23, when the check result of the abstract syntax tree output by the second LLM model indicates that the check is passed, the abstract syntax tree output by the second LLM model is taken as the target abstract syntax tree corresponding to B.
[0065] In a specific embodiment, step S23 can be replaced by: when the check result of the abstract syntax tree output by the second LLM model indicates that the check is passed, performing structure compression on the abstract syntax tree output by the second LLM model to obtain the target abstract syntax tree corresponding to B.
[0066] Through the above steps, the abstract syntax tree output by the second LLM model is checked, which can effectively identify and intercept abnormal situations such as illegal queries and semantic errors, and prevent the execution of incorrect SQL statements; the correctness and security of the generated SQL statements can be improved.
[0067] Embodiments of the present application also provide a non-transitory computer readable storage medium, which can be arranged in an electronic device to save a computer program related to a method in the method embodiment, and the computer program is loaded and executed by the processor to implement the method provided by the above embodiments.
[0068] Embodiments of the present application also provide an electronic device, comprising: a processor, a memory, and a computer program stored on the memory and executable on the processor, wherein the processor executes the computer program to implement the method provided by the above embodiments.
[0069] Embodiments of the present application also provide a computer program product, which includes program code for causing an electronic device to perform the steps of the method according to various exemplary embodiments of the present application described above when the program product is executed on the electronic device.
[0070] The application provides a text-based SQL statement acquisition method and device and a medium, the method performs standardization processing on an initial text corresponding to a target user by a first LLM model to obtain a standard text corresponding to the initial text; if a preset standard text list contains a preset standard text identical to the standard text, a first abstract syntax tree corresponding to the preset standard text identical to the standard text is taken as a target abstract syntax tree corresponding to the standard text; if a history text list contains a history text identical to the standard text, a second abstract syntax tree corresponding to the history text identical to the standard text is taken as the target abstract syntax tree corresponding to the standard text; if the preset standard text list does not contain a preset standard text identical to the standard text and the history text list does not contain a history text identical to the standard text, the standard text is input into a second LLM model to obtain a target abstract syntax tree corresponding to the standard text; and a SQL statement corresponding to the initial text is obtained according to the target abstract syntax tree corresponding to the standard text and is sent to the target user. It can be known that the application preferentially determines a target abstract syntax tree from a first abstract syntax tree corresponding to a plurality of preset standard texts and a second abstract syntax tree corresponding to a plurality of history texts; when the target abstract syntax tree cannot be determined from the first abstract syntax tree corresponding to the plurality of preset standard texts and the second abstract syntax tree corresponding to the plurality of history texts, the target abstract syntax tree is obtained by the second LLM model, the SQL statement is obtained according to the target abstract syntax tree; minimization of large model calling is realized and the LLM model is not completely relied on, SQL errors caused by model hallucination or input sensitivity are effectively reduced, and the accuracy of the generated SQL statement is improved.
[0071] Although some specific embodiments of the application have been described in detail by examples, those skilled in the art should understand that the above examples are only for illustration, not for limiting the scope of the application. Those skilled in the art should also understand that various modifications can be made to the embodiments without departing from the scope and spirit of the application.
Claims
1. A text-based method for obtaining SQL statements, characterized in that, The method includes the following steps: S1. Standardize the initial text A corresponding to the target user using the first LLM model to obtain the standard text B corresponding to A. S2. If there is a preset standard text in the preset standard text list that is exactly the same as B, then the first abstract syntax tree corresponding to the preset standard text that is exactly the same as B is used as the target abstract syntax tree corresponding to B; if there is a historical text in the historical text list that is exactly the same as B, then the second abstract syntax tree corresponding to the historical text that is exactly the same as B is used as the target abstract syntax tree corresponding to B. If there is no preset standard text exactly the same as B in the preset standard text list and no historical text exactly the same as B in the historical text list, then input B into the second LLM model to obtain the target abstract syntax tree corresponding to B; S3. Obtain the SQL statement corresponding to A based on the target abstract syntax tree corresponding to B and send it to the target user, including: S31. Generate a list of SQL statements C=(C1, C2, ..., C...) based on the target abstract syntax tree corresponding to B. i , ..., C m ), C i Let be the i-th SQL statement corresponding to B, 1≤i≤m, where m is the number of SQL statements corresponding to B; and each SQL statement in C corresponds to a different database management system. S32, C i Input into the third LLM model to obtain C i The corresponding target text D i ; S33, Obtain D i The vector similarity E between the semantic feature vector of A and the semantic feature vector of A i Among them, E i The larger D is i The more similar the semantic feature vector of A is to the semantic feature vector of A; S34. If E1, E2, ..., E i , ..., E m The variance is not greater than the preset variance and E1, E2, ..., E i , ..., E m If none of them are less than a preset similarity threshold, then from C1, C2, ..., C i , ..., C m The SQL statement corresponding to A is determined and sent to the target user.
2. The text-based SQL statement retrieval method according to claim 1, characterized in that, In step S2, inputting B into the second LLM model to obtain the target abstract syntax tree corresponding to B includes the following steps: S21. Input B into the second LLM model to obtain the abstract syntax tree output by the second LLM model; S22. Validate the abstract syntax tree output by the second LLM model and obtain the validation result of the abstract syntax tree output by the second LLM model. S23. When the validation result of the abstract syntax tree output by the second LLM model is "passed", the abstract syntax tree output by the second LLM model is taken as the target abstract syntax tree corresponding to B.
3. The text-based SQL statement retrieval method according to claim 1, characterized in that, The list of preset standard texts includes several preset standard texts; each preset standard text corresponds to a preset standard SQL statement; for each preset standard text, the abstract syntax tree corresponding to the preset standard SQL statement is used as the first abstract syntax tree corresponding to the preset standard text.
4. The text-based SQL statement retrieval method according to claim 1, characterized in that, The historical text list includes several historical texts; each historical text corresponds to a historical SQL statement; for each historical text, the abstract syntax tree corresponding to the historical SQL statement is used as the second abstract syntax tree corresponding to the historical text.
5. The text-based SQL statement retrieval method according to claim 1, characterized in that, The default standard text is exactly the same as B, which means that the length of the default standard text is the same as the length of B and the j-th character in the default standard text is the same as the j-th character in B, 1≤j≤n, where n is the number of characters in B.
6. The text-based SQL statement retrieval method according to claim 5, characterized in that, The historical text is exactly the same as B, meaning that the length of the historical text is the same as the length of B and the j-th character in the historical text is the same as the j-th character in B.
7. The text-based SQL statement retrieval method according to claim 1, characterized in that, The preset similarity threshold ranges from 0.9 to 1.
8. A non-transitory computer-readable storage medium, characterized in that, The storage medium stores a computer program, which is loaded and executed by a processor to implement the text-based SQL statement retrieval method as described in any one of claims 1-7.
9. An electronic device, comprising: A processor, a memory, and a computer program stored in the memory and executable on the processor, characterized in that, when the processor executes the computer program, it implements the text-based SQL statement retrieval method as described in any one of claims 1-7.
Citation Information
Patent Citations
Question-answering system and method based on semantic similarity
CN110362665A
Data query method and device, medium and electronic equipment
CN117971875A