Method and device for generating query script and nonvolatile storage medium

By filtering case libraries with high semantic similarity and keyword-containing in the user's private vertical business field and generating query scripts, the existing Text2SQL method has solved the problem of low SQL generation in this field, improving the accuracy rate and reducing the dependence on the capabilities of large models.

CN120123364APending Publication Date: 2025-06-10CHINA TELECOM ARTIFICIAL INTELLIGENCE TECHNOLOGY (BEIJING) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510182691.X
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-02-18
Publication Date
2025-06-10

AI Technical Summary

Technical Problem

The existing method of converting large-model-based natural language into structured query language (Text2SQL) has low accuracy in generating SQL in the user's private vertical business field, and strongly relies on the training data and capabilities of large-models.

Method used

By receiving natural language query requests, determine its corresponding case library, and filter cases with high semantic similarity and keyword-containing cases in the case library, and fill in the conversion template to generate query scripts.

Benefits of technology

Improves the accuracy of the Text2SQL method to generate query scripts in private vertical business areas, and reduces the dependence on large model capabilities.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120123364A_ABST
    Figure CN120123364A_ABST
Patent Text Reader

Abstract

The invention discloses a method and device for generating a query script and a nonvolatile storage medium. The method comprises the steps that a query request is received, and the format of the query request is a natural language; a case library corresponding to the query request is determined, a target query request in a target business scene and a target query script corresponding to the target query request are recorded in the case library, the format of the target query script is structured query language (SQL), and the target business scene is a business scene to which the query request belongs; determining a first type of cases in a case library, the similarity between the first type of cases and the semantics of the query request being greater than a preset value, and determining a second type of cases containing keywords in the case library, the keywords being contained in the query request; filling the query request, the first type of case and the second type of case into a conversion template to obtain a conversion result; and generating a query script corresponding to the query request according to the conversion result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of data processing, and in particular, to a method and device for generating a query script, and a non-volatile storage medium. Background Art

[0002] Currently, the method of converting natural language into Structured Query Language (SQL) code based on large models strongly depends on the training data, parameter scale, and its own capabilities of the large models; in the private vertical business field of user scenarios, the above method of converting natural language into Structured Query Language (Text2SQL) that strongly depends on large models has the problem of poor SQL generation effect; specifically, the Text2SQL ability of the large model itself is insufficient, the Text2SQL performance on different types of databases varies, and the understanding of the knowledge in the private vertical business field is not in place, resulting in a low accuracy rate of the generated SQL.

[0003] In response to the above problems, no effective solution has been proposed yet. Summary of the Invention

[0004] Embodiments of the present application provide a method and device for generating a query script, and a non-volatile storage medium, so as to at least solve the technical problem that the method of converting natural language into structured query language in related technologies strongly depends on large models and has an insufficient understanding of the knowledge in the private vertical business field of user scenarios, resulting in a low accuracy rate of the output SQL when applied in the vertical field of user scenarios.

[0005] According to one aspect of the embodiments of the present application, a method for generating a query script is provided, including: receiving a query request, where the query request format is natural language; determining a case library corresponding to the query request, where the case library records a target query request in a target business scenario, a target query script corresponding to the target query request, the format of the target query script is Structured Query Language SQL, and the target business scenario is the business scenario to which the query request belongs; determining a first type of cases in the case library whose semantic similarity to the query request is greater than a preset value, and determining a second type of cases in the case library that contain keywords, where the keywords are included in the query request; filling the query request, the first type of cases, and the second type of cases into a conversion template to obtain a conversion result; generating a query script corresponding to the query request according to the conversion result.

[0006] Optionally, the case library is generated by the following method: obtaining a plurality of cases corresponding to different business scenarios, where each case records a query request and a query script corresponding to the query request; for each case, determining the query purpose corresponding to the case according to the query request recorded in the case, clustering a plurality of cases with the same query purpose to obtain a case set, where different case sets correspond to different business scenarios; for each case set, determining the structural similarity between any two cases in the case set according to the data structure of the query script recorded in the case; clustering a plurality of cases corresponding to a plurality of structural similarities greater than a preset structural similarity to obtain the case library.

[0007] Optionally, after clustering a plurality of cases corresponding to a plurality of structural similarities greater than a preset structural similarity, the method further includes: determining a plurality of identical cases with the same data structure among the clustered cases; in the case of multiple identical cases, only retaining one identical case in the case library.

[0008] Optionally, the conversion template includes a case placeholder and an input statement placeholder, where the position of the case placeholder is used to fill in the cases in the case library, and the position of the input statement placeholder is used to fill in the query request; filling the query request, the first type of cases, and the second type of cases into the conversion template, including: replacing the case placeholder with the first type of cases and the second type of cases, and replacing the input statement placeholder with the query request.

[0009] Optionally, the keywords are determined by the following method: performing word segmentation on the query request to obtain a plurality of words, where at least one character is included in the words; for each word, determining the occurrence frequency of the word according to the number of times the word appears in the query request, and determining the score value of the word according to the occurrence frequency, where the score value is used to indicate the importance of the word in the query request; determining the keywords among the plurality of words according to the score value.

[0010] Optionally, generating the query script corresponding to the query request according to the conversion result includes: processing the conversion result by using a large language model to obtain an initial query script, and validating the initial query script, where the large language model is a machine learning model with text understanding and generation functions; in the case where the query script passes the validation, determining the initial query script as the query script corresponding to the query request; in the case where the query script fails to pass the validation, outputting a prompt message, where the prompt message is used to remind to correct the initial query script.

[0011] Optionally, the method for generating the query script further includes: combining the corrected initial query script and the query request into a new case, and storing the new case in the case library.

[0012] According to another aspect of the embodiments of the present application, there is also provided a device for generating a query script, including: a receiving module, configured to receive a query request, wherein the format of the query request is natural language; a first determination module, configured to determine a case library corresponding to the query request, wherein the case library records a target query request in a target business scenario, a target query script corresponding to the target query request, the format of the target query script is Structured Query Language (SQL), and the target business scenario is the business scenario to which the query request belongs; a second determination module, configured to determine a first type of cases in the case library whose semantic similarity to the query request is greater than a preset value, and determine a second type of cases in the case library that contain keywords, wherein the keywords are included in the query request; a processing module, configured to fill the query request, the first type of cases, and the second type of cases into a conversion template to obtain a conversion result; a generation module, configured to generate a query script corresponding to the query request according to the conversion result.

[0013] According to another aspect of the embodiments of the present application, there is also provided a non-volatile storage medium storing a computer program, wherein the method for generating a query script as described above is executed by the device where the non-volatile storage medium is located by running the computer program.

[0014] According to another aspect of the embodiments of the present application, there is also provided an electronic device including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to execute the method for generating a query script as described above by the computer program.

[0015] According to another aspect of the embodiments of the present application, there is also provided a computer program product including computer instructions, and when the computer instructions are executed by a processor, the steps of the method for generating a query script as described above are implemented.

[0016] In the embodiments of the present application, a query request is received, where the query request format is natural language; a case library corresponding to the query request is determined, where the case library records target query requests in a target business scenario, target query scripts corresponding to the target query requests, the format of the target query scripts is structured query language SQL, and the target business scenario is the business scenario to which the query request belongs; first-class cases with a semantic similarity greater than a preset value to the semantics of the query request are determined in the case library, and second-class cases containing keywords are determined in the case library, where the keywords are included in the query request; the query request, the first-class cases, and the second-class cases are filled into a conversion template to obtain a conversion result; by generating a query script corresponding to the query request in a manner that combines natural language technologies such as semantics and text word frequency, business cases related to the query request (Query) are recalled by combining natural language technologies such as semantics and text word frequency, and by leveraging these few cases related to the business of the query request and combining with a query script template, a query script that meets the requirements is quickly generated, achieving the technical effect of improving the accuracy of generating query scripts by the Text2SQL method in a specific business domain. Specifically, the method learns relevant information of the cases based on the business cases related to the request (Query), and after learning the case information in a specific domain, generates a query script, thereby solving the technical problem in the related art that the method of converting natural language into structured query language strongly depends on a large model and has an insufficient understanding of the private vertical business domain knowledge of the user scenario, resulting in a low accuracy of the output SQL when applied in the vertical domain of the user scenario. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] The accompanying drawings described herein are used to provide a further understanding of the present application and constitute a part of the present application. The illustrative embodiments and descriptions thereof of the present application are used to explain the present application and do not constitute an improper limitation of the present application. In the drawings:

[0018] Figure 1 is a hardware structure block diagram of a computer terminal for implementing a method of generating a query script according to the related art;

[0019] Figure 2 is a flowchart of the steps of a method of generating a query script according to an embodiment of the present application;

[0020] Figure 3 is a schematic diagram of a conversion template (prompt) according to an embodiment of the present application;

[0021] Figure 4 is a structural diagram of a device for generating a query script according to an embodiment of the present application;

[0022] Figure 5It is a schematic diagram of the working process of a device for generating query scripts according to an embodiment of the present application. Detailed implementation manners

[0023] In order to enable those skilled in the art of the present technology to better understand the solutions of the present application, the technical solutions in the embodiments of the present application will be clearly and completely described below with reference to the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the protection scope of the present application.

[0024] It should be noted that the terms "first", "second", etc. in the specification, claims and above-mentioned drawings of the present application are used to distinguish similar objects, and do not have to be used to describe a specific order or sequence. It should be understood that such used data 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 "including" and "having" and any variations thereof are intended to cover non-exclusive inclusion. For example, a process, method, system, product or device including a series of steps or units does not have to be limited to those steps or units clearly listed, but may include other steps or units not clearly listed or inherent to these processes, methods, products or devices.

[0025] In the related art, the ability of the large model Text2SQL is used to solve the cumbersome drag-and-drop operations of traditional intelligent software, and intelligent data extraction is realized in the way of natural language dialogue, aiming to lower the threshold of data analysis and improve the efficiency of data analysis. The current main technology in this intelligent application direction is to utilize the natural language understanding ability, representation ability and generalization ability of the large model itself, and with the help of prompt engineering, convert the user's problem into an executable database SQL script, and execute this SQL script to achieve the purpose of the user querying data. However, the current main technology of the Text2SQL method based on the large model strongly depends on the training data, parameter scale and its own ability of the large model, and has an insufficient understanding of the relevant knowledge in the user's private vertical business field. When directly applying the Text2SQL method in the related art that strongly depends on the large model to the user's private vertical business field, there is a problem of low accuracy of the generated SQL script. To solve this problem, relevant solutions are provided in the embodiments of the present application, which are described in detail below.

[0026] According to an embodiment of the present application, an embodiment of a method for generating a query script is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. And although the logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in a different order than here.

[0027] The method embodiment provided by the embodiment of the present application can be executed on a mobile terminal, a computer terminal or a similar computing device. Figure 1 The hardware structure block diagram of a computer terminal for implementing the method of generating a query script is shown. As Figure 1 shown, the computer terminal 10 may include one or more (shown as 102a, 102b,..., 102n in the figure) processors 102 (the processor 102 may include, but is not limited to, a processing device such as a microprocessor MCU or a programmable logic device FPGA), a memory 104 for storing data, and a transmission device 106 for communication functions. In addition, it may further include: a display, an input / output interface (I / O interface), a universal serial bus (USB) port (which can be included as one of the ports of the BUS bus), a network interface, a power supply and / or a camera. Those of ordinary skill in the art can understand that Figure 1 the structure shown is only schematic and does not limit the structure of the above-mentioned electronic device. For example, the computer terminal 10 may further include more or fewer components than Figure 1 shown, or have a different configuration from Figure 1 shown.

[0028] It should be noted that the above one or more processors 102 and / or other data processing circuits are generally referred to as "data processing circuits" in this article. The data processing circuit can be embodied in whole or in part as software, hardware, firmware or any combination thereof. In addition, the data processing circuit can be a single independent processing module, or be incorporated in whole or in part into any one of the other elements in the computer terminal 10. As involved in the embodiment of the present application, the data processing circuit is a kind of processor control (such as the selection of a variable resistor terminal path connected to an interface).

[0029] The memory 104 can be used to store software programs and modules of application software, such as the program instructions / data storage device corresponding to the method of generating a query script in the embodiments of the present application. The processor 102 executes various functional applications and data processing by running the software programs and modules stored in the memory 104, that is, implements the above-mentioned method of generating a query script. The memory 104 may include a high-speed random access memory, and may also include a non-volatile memory, such as one or more magnetic storage devices, flash memories, or other non-volatile solid-state memories. In some instances, the memory 104 may further include a memory remotely disposed relative to the processor 102, and these remote memories can be connected to the computer terminal 10 through a network. Examples of the above network include but are not limited to the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0030] The transmission device 106 is used to receive or send data via a network. Specific examples of the above network may include a wireless network provided by a communication provider of the computer terminal 10. In one instance, the transmission device 106 includes a network adapter (Network Interface Controller, NIC), which can be connected to other network devices through a base station and thus can communicate with the Internet. In one instance, the transmission device 106 can be a radio frequency (RF) module, which is used to communicate with the Internet wirelessly.

[0031] The display can be, for example, a touch-screen liquid crystal display (LCD), and the liquid crystal display enables a user to interact with the user interface of the computer terminal 10.

[0032] The embodiments of the present application provide a method for generating a query script that can run in the above operating environment. Figure 2 It is a step flowchart of the method for generating a query script provided by the embodiments of the present application, as Figure 2 shown, and the method includes the following steps:

[0033] Step S202, receive a query request, where the query request format is natural language.

[0034] In order to solve the problem of low accuracy in generating query scripts in the Text2SQL for vertical business scenarios under the conditions of limited hardware resources and insufficient Text2SQL capabilities of large models, an embodiment of this application provides a method for improving the Text2SQL capabilities of large models by combining a small number of business cases (few-shot) of multi-channel recall. When any device / device / system executes the method provided in the embodiment of this application, it first executes step S202: Receive a query request (Query) in the form of natural language input by the user. For example, if the user inputs "Query the average customer satisfaction score of all services in Region A in the fourth quarter of 2023", then this statement is a query request.

[0035] Step S204, determine the case library corresponding to the query request, where the case library records the target query request in the target business scenario and the target query script corresponding to the target query request. The format of the target query script is the structured query language SQL, and the target business scenario is the business scenario to which the query request belongs.

[0036] To improve the accuracy of the Text2SQL method in generating SQL scripts (i.e., query scripts) in the user's private vertical business domain, after receiving the query request, in step S204, determine the business scenario corresponding to the query request (i.e., the target business scenario), and obtain the cases related to the target business scenario generated before receiving the query request. These cases form the case library corresponding to the query request; that is, the case library corresponding to the query request contains multiple cases, and each case is an event in which a query request in the form of natural language was converted into an SQL script (i.e., a query script) that occurred in the target scenario; then each case in the case library records at least the following information: the query request that was input (i.e., the target query request), the query script generated by converting the target query request (i.e., the target query script); in this embodiment, all query scripts are in the format of the structured query language SQL. For example, when the query request is "Query the average customer satisfaction score of all services in Region A in the fourth quarter of 2023" in step S202, the target business scenario is: "Customer satisfaction analysis".

[0037] Step S206, determine the first type of cases in the case library whose semantic similarity to the query request is greater than the preset value, and determine the second type of cases in the case library that contain keywords, where the keywords are included in the query request.

[0038] Next, in order to implement a method for improving the capabilities of the large language model Text2SQL by combining a small number of few-shot business cases with multi-channel recall, in step S206, a semantic recall method and a Term Frequency-Inverse Document Frequency (TFIDF) recall method are combined to determine business cases that are similar or relevant to the query request from the case library (i.e., the first type of cases and the second type of cases). Among them, when using the semantic recall method to determine business cases that are similar or relevant to the query request from the case library, the recalled cases are those (first type of cases) whose semantic similarity to the query request is greater than the preset semantic similarity (i.e., the preset value, for example: 0.8, 80%); when using the TFIDF recall method to determine business cases that are similar or relevant to the query request from the case library, the recalled cases (second type of cases) contain the keywords recorded in the query request; the above recall is used to indicate filtering cases from the case library. For example, when the query request is "query the average customer satisfaction score of all services in region A in the fourth quarter of 2023" in step S202, the following method can be used for the first type of cases: Use semantic recall, such as the embedded vectors of the Bi-Encoder with General Entity (BGE) model and the Re-Ranking Model (rerank) model, to determine a set of cases with high core semantic similarity to "customer satisfaction", "quarter", "region", and "average value" and greater than the preset value (for example, 0.8); all cases in this set of cases are the first type of cases. The following method can be used for the second type of cases: Use the TF-IDF recall algorithm to determine a set of cases that contain the keywords "the fourth quarter of 2023", "region A", "customer satisfaction score", and "average value"; all cases in this set of cases are the second type of cases. It should be noted that since the first type of cases and the second type of cases are screened using different methods, there may be the same cases among the first type of cases and the second type of cases, which will not affect the accuracy of the finally output SQL-formatted query script.

[0039] Optionally, the keywords are determined by the following method: The query request is tokenized to obtain multiple words, where at least one character is included in the words; for each word, the occurrence frequency of the word is determined according to the number of times the word appears in the query request, and the score value of the word is determined according to the occurrence frequency, where the score value is used to indicate the importance of the word in the query request; the keywords are determined from the multiple words according to the score value.

[0040] When screening cases in step S206, it is necessary to determine keywords from the query request. Specifically, the following method can be used to determine them. First, perform word segmentation on the query requests in the case library, calculate the occurrence frequency of each word in the query request received in step S202, then use the TF-IDF algorithm to determine the keyword score value, and finally select the word with the highest score value from the multiple words obtained by performing word segmentation on the query request received in step S202 as the keyword. Among them, the TF-IDF algorithm can be expressed in the form of the following formula: score value = occurrence frequency * TF-IDF value, and the TF-IDF value is calculated based on the frequency and importance of words in the entire case library and is used to reflect the importance degree of words in a specific query request. The fewer times a word appears in the case library, but the higher the frequency of its appearance in the current query request, the higher its TF-IDF value, and the score value increases accordingly. The words obtained by word segmentation here can be single characters, or words, phrases, etc., and word segmentation is performed according to the actual semantics.

[0041] According to some optional embodiments of the present application, the case library is generated by the following method: obtain multiple cases corresponding to different business scenarios, where each case records a query request and a query script corresponding to the query request; for each case, determine the query purpose corresponding to the case according to the query request recorded in the case, cluster multiple cases with the same query purpose to obtain a case set, where different case sets correspond to different business scenarios; for each case set, determine the structural similarity between any two cases in the case set according to the data structure of the query script recorded in the case; cluster multiple cases corresponding to multiple structural similarities greater than the preset structural similarity to obtain the case library.

[0042] In step S206, the case library corresponding to the query request is obtained by screening multiple cases in different business scenarios (these multiple cases corresponding to different business scenarios can be obtained through the following methods: obtained from the business case set constructed by the large model or obtained from the common and high-frequency business case set written by business experts). For example, in this embodiment, the following screening is performed according to the query request and query script included in the case as the screening conditions: After obtaining cases in multiple different business scenarios (such as customer satisfaction analysis, sales trend analysis, etc.), cases with similar query purposes (such as cases for querying the average satisfaction in each region in customer satisfaction analysis) are clustered to obtain a case set. When performing the above clustering, cases belonging to the same target scenario will be clustered into a case set. Among them, the query purpose of each case is determined according to the query request in the structured format (i.e., query script) included in each case. For example, natural language processing (NLP) technology is used to analyze the query request in the case to determine the query purpose of the case, such as statistical query, trend analysis, maximum or minimum value query, average value calculation, etc. After obtaining the case set, for each case set, based on the data structure of the query script, the structural similarity between cases is determined. For example, the string similarity algorithm (Levenshtein) or the tree edit distance algorithm can be used to determine the structural similarity between two cases. Finally, cases with a structural similarity higher than the preset value (i.e., the preset structural similarity) are retained; in this embodiment, the preset structural similarity and the preset semantic similarity can be the same or different; they can be set by the user according to actual needs. The above data structure of the query script is the composition and format of the SQL query script, such as field usage, table connection, function call, etc. Among them, field usage refers to the reference to the database table fields in the SQL query script. For example, when querying "the product category with the highest sales in the second quarter of 2023", the field usage may involve the product category (product_category) field in the product table and the sales amount (sales_amount) field in the sales table, as well as possible time fields (quarter) or time interval restrictions. Table connection refers to the table connection in the SQL query script (such as inner join (INNER JOIN), left outer join (LEFT JOIN), right outer join (RIGHT JOIN), etc.) used to merge data in two or more tables according to a certain field or condition. For example, to obtain product category and sales amount data, it may be necessary to connect the product table and the sales table, and the connection condition is usually that the product identifier (ID) in the product table matches the product ID in the sales table.Function call: In SQL query scripts, the purpose of function call is to perform specific calculations or data processing, such as statistical aggregation functions (sum (SUM), average (AVG)), date processing functions (date formatting (DATE_FORMAT), extraction (EXTRACT)), string operation functions (substring interception (SUBSTRING), uppercase conversion (UPPER)), etc. For example, in order to find the product category with the highest sales, you may need to call the SUM function to calculate the total sales under each category, and call the AVG function to calculate the average sales.

[0043] According to other optional embodiments of the present application, after clustering multiple cases corresponding to multiple structural similarities greater than a preset structural similarity, the method also includes: determining multiple identical cases with the same data structure among the multiple cases after clustering; and when there are multiple identical cases, retaining only one identical case in the case library.

[0044] In order to ensure that the method provided by the embodiment of the present application can still be implemented under limited hardware resource conditions, the embodiment of the present application reduces the cases used when generating query scripts as much as possible, and selects the cases most relevant to the target business scenario as much as possible. Therefore, in this embodiment, after screening out cases with high structural similarity, identify those cases with only different query screening conditions, and only retain one (identical) case of this type of identical cases, wherein the difference in query screening conditions only means that the data structures of the two cases are exactly the same but the numerical values ​​or text parameters are different. It should be noted that the case screened out as the retained case from the above-mentioned cases with only different query screening conditions (i.e., the same cases) is the most representative case; for example, select the case containing keywords as the retained case; or determine the representativeness score of the case according to the frequency of use of the case in its business scenario, and select the case with the highest representativeness score as the retained case among these identical cases, wherein the higher the frequency of use, the higher the representativeness score.

[0045] Step S208, filling the query request, the first type of cases and the second type of cases into the conversion template to obtain a conversion result.

[0046] Furthermore, after a small number of business cases (i.e., first-category cases and second-category cases) that are similar or related to the business corresponding to the query request are screened out in step S206, the query request is converted into SQL format with the help of prompt engineering in step S208; specifically, a pre-set conversion template is used to fill part of the information in the query request, part of the information in the first-category cases, and part of the information in the second-category cases into the conversion template to obtain a query request in SQL format (i.e., the conversion result).

[0047] Optionally, the conversion template includes case placeholders and input statement placeholders. The position of the case placeholder is used to fill in the cases in the case library, and the position of the input statement placeholder is used to fill in the query request. Filling the query request, the first type of cases, and the second type of cases into the conversion template includes: replacing the case placeholders with the first type of cases and the second type of cases, and replacing the input statement placeholder with the query request.

[0048] As mentioned in the above embodiments, in the embodiments of the present application, a pre-set conversion template is adopted to fill some information in the query request, some information in the first type of cases, and some information in the second type of cases into the conversion template to obtain a query request in SQL format (i.e., the conversion result). Specifically, the pre-set conversion template in this embodiment includes case placeholders and input statement placeholders. Only the positions of the case placeholders and the input statement placeholders in the conversion template need to be filled with information. Among them, the information filled in the position of the case placeholder is the first type of cases and the second type of cases; the information filled in the position of the input statement placeholder is the query request input by the user in step S202. When filling the first type of cases and the second type of cases into the position of the case placeholder, each case is inserted into the conversion template in order from the largest to the smallest correlation with the target business scenario. The correlation between the case and the target business scenario can be determined by semantic similarity and structural similarity, and the semantic similarity has the highest priority. That is, the first type of cases are inserted into the conversion template in order from the largest to the smallest correlation with the target business scenario, and then the second type of cases are inserted into the conversion template in order from the largest to the smallest correlation with the target business scenario. Figure 3 is a schematic diagram of the conversion template (prompt). As Figure 3 shown, among them, the {} in Cases1{}, Cases2{}, and Cases3{} are case placeholders for filling in the first type of cases and the second type of cases; the {} in User Query{} is the input statement placeholder, and the {} is used to fill in the query request input by the user in step S202; the conversion template filled with the first type of cases, the second type of cases, and the query request input by the user in step S202 is the conversion result.

[0049] Step S210, generate a query script corresponding to the query request according to the conversion result.

[0050] After obtaining the query request in SQL format (i.e., the conversion result) through step S208; in step S210, by processing the query request in SQL format (i.e., the conversion result), the final conversion result (i.e., the query script) corresponding to the query request can be determined. Specifically, a general large language model with text understanding ability and generation function can be used to process the conversion result and output the query script corresponding to the query request.

[0051] According to some alternative embodiments of the present application, generating a query script corresponding to a query request based on the conversion result includes: processing the conversion result using a large language model to obtain an initial query script, and validating the initial query script, where the large language model is a machine learning model with text understanding and generation capabilities; when the query script passes the validation, determining the initial query script as the query script corresponding to the query request; when the query script fails to pass the validation, outputting a prompt message, where the prompt message is used to remind to correct the initial query script.

[0052] In this embodiment, a pre-trained machine learning model with text understanding and generation capabilities (i.e., a large language model) is called to generate an SQL script based on the conversion result. When using the large language model to process the conversion result, the large language model generates an initial SQL query script (i.e., the initial query script) based on learning the data structure in the cases of the conversion result and based on the learned data structure and the user Query in the conversion result (i.e., the query request received in step S202). Further, a series of validation processes are subsequently performed on the initial query script. First, syntax validation is performed to ensure that the generated SQL script has correct syntax and conforms to the database specifications, such as using correct keywords, table names, and column names, as well as appropriate syntax structures; next, semantic validation is performed to check whether the SQL script accurately reflects the user's query intention, such as whether the query time range, regional restrictions, data filtering conditions, etc. are consistent with the natural language description input by the user. If the initial query script passes the syntax and semantic validations, then it can be directly determined as the final query script corresponding to the user's query request (i.e., the query script finally output in step S208) for executing the user's requirements in the database. If the initial query script fails to pass the validation, such as having a syntax error or semantic deviation, the system will output a prompt message indicating the problems existing in the initial query script, such as "Syntax error: incorrect clause" or "Semantic error: query range does not match the request". These prompt messages are used to remind and guide the user to correct the initial query script or further optimize the prompt design of few-shot learning so as to generate a more accurate SQL query script in the next attempt. For example, when the query request received in step S202 is the statement "Query the average customer satisfaction score of all services in region A in the fourth quarter of 2023", the initial query script (the corrected initial query script) that passes the validation should be in the following form: {"SELECT AVG(satisfaction_score) AS 'Average satisfaction score' FROM customer_feedback WHERE location = 'Region A' AND quarter = '$Fourth quarter$'"}.

[0053] In this embodiment, the large language model can be loaded into the memory. For example, the original data of the large language model can be loaded from the non-volatile memory to the volatile memory so that the processor can run the large language model. The original data of the large language model refers to the unprocessed data, which usually includes the parameters and structure data of the large language model. The structure data can be the calculation relationship based on the parameters, such as the forward propagation calculation relationship between intermediate layers and between neurons. Specifically, the structure data can include the code related to the structure of the large language model, such as the code used to perform the relevant calculations between intermediate layers and between neurons.

[0054] In one implementation, an area for loading the large language model can be divided in the memory, which can include a structure data storage area and a parameter storage area. The structure data storage area is used to store the structure-related code, and the parameters it references can point to the addresses of specific parameters in the parameter storage area through pointers. During the training process of the large language model, the parameters may need to be updated frequently, so only the parameter values in the parameter storage area need to be updated.

[0055] Optionally, the method for generating the query script further includes: combining the corrected initial query script and the query request into a new case, and storing the new case in the case library.

[0056] The method provided in the embodiments of the present application also adopts a hotfix mechanism, that is, when the script generated by the large language model fails the verification, after outputting the prompt message, based on the user feedback or the correction during the verification process, the corrected initial query script and the original Query (that is, the query request received in step S202) are combined into a new case, which is fed back and stored in the case library to achieve the hotfix and continuous optimization of the case library.

[0057] Through the above steps, it is possible to recall business cases related to the input query request (Query) by combining natural language technologies such as semantics and text frequency. With the understanding ability and generalization ability of the large language model, and few-shot engineering enables the large language model to quickly learn cases in the vertical business field on a small number of samples to adapt to new tasks and output correct SQL query scripts. For the specific task of improving the accuracy of the large model Text2SQL, there is no need to fine-tune the large language model, which greatly reduces the R & D cost and cycle. With the maintenance and expansion of the business case library by the hotfix machine, new application scenarios are added. Using the ability of the large model Text2SQL, the conversion template (prompt) for automatically designing and constructing business cases is designed, and cases and business metrics are dynamically injected, enabling the method for generating query scripts provided in the embodiments of the present application to be automatically and quickly hot-started in the business domain.

[0058] Figure 4 It is a structural diagram of a device for generating a query script according to the present application, asFigure 4 As shown, the device includes: a receiving module 40 for receiving a query request, where the format of the query request is natural language; a first determination module 42 for determining a case library corresponding to the query request, where the case library records a target query request in a target business scenario, a target query script corresponding to the target query request, the format of the target query script being structured query language SQL, and the target business scenario being the business scenario to which the query request belongs; a second determination module 44 for determining, in the case library, a first type of case whose semantic similarity to the query request is greater than a preset value, and for determining, in the case library, a second type of case that includes keywords, where the keywords are included in the query request; a processing module 46 for filling the query request, the first type of case, and the second type of case into a conversion template to obtain a conversion result; and a generation module 48 for generating a query script corresponding to the query request according to the conversion result.

[0059] It should be noted that Figure 4 For the preferred implementation manners of the embodiments shown, reference may be made to Figure 2 the relevant descriptions of the embodiments shown, which will not be elaborated here.

[0060] Figure 5 is a schematic diagram of the working process of the device for generating a query script. As Figure 5As shown, when using the device for generating query scripts to generate a query script corresponding to a query request input by a user, first, the receiving module 40 receives the query request (Query) input by the user in natural language form. The receiving module 40 transmits the Query to the first determination module 42. The first module 42 determines the target business scenario to which it belongs based on the received query request and selects the corresponding case library from the system. The case library is a database containing query requests processed in history and their corresponding correct SQL query scripts, and each case is associated with a specific business scenario. The first determination module 42 matches the case library by identifying business features (such as time, location, business metrics, etc.) in the query request to ensure that the selected case library is highly relevant to the business scenario of the query request. After determining the case library, the second determination module 44 performs a two-step recall process. First, it uses a semantic similarity algorithm (such as based on a text similarity model (BGE Embedding)) to find the first type of cases in the case library whose semantic similarity to the query request is greater than a preset semantic similarity. The preset semantic similarity can be adjusted according to actual business requirements and recall strategies to balance the number and quality of recalled cases. Secondly, the second determination module 44 uses the TE-IDF algorithm to locate all cases in the case library that contain the keywords in the query request based on keyword recognition as the second type of cases. The determination method of keywords is mainly based on the frequency and importance of words. The first type of cases and the second type of cases will be stored in a vector database (VectorDB). Before filling the first type of cases and the second type of cases into the conversion template, a re-ranking model (Bi-Encoder for Generative Encoders, BGE Rerank) can be used to rank the first type of cases and the second type of cases according to relevance to generate the cases most relevant to the query request (i.e., Top-K Case). The processing module 46 then dynamically fills the query request, the first type of cases, and the second type of cases into the conversion template (few-shot PromptTemplate) with a small number of cases to generate a conversion result. This conversion template can be a carefully designed prompt for guiding the large language model to generate an SQL script. When filling the conversion template, the processing module 46 will ensure that the format of each case and the query request is consistent with the requirements of the template. At the same time, it will also provide the SQL script and business scenario description of the cases in the case library as background information to the large language model (LLM). Finally, the generation module 48 calls the large language model to process the conversion result generated by the processing module 46 to generate the SQL query script corresponding to the query request. The large model generates a structured SQL query script based on the filled prompt, using its pre-trained text understanding and generation capabilities, as well as the guidance of cases in few-shot learning.The generated SQL script will then be verified to ensure the accuracy of syntax and semantics. If the verification fails, the processing module 48 will feedback to the processing module 46 to further optimize the prompt or output a prompt message for manual correction.

[0061] An embodiment of the present application also provides a non-volatile storage medium, in which a computer program is stored. Wherein, the device where the non-volatile storage medium is located executes the above method for generating a query script by running the computer program.

[0062] The above non-volatile storage medium is used to store a program for executing the following functions: receiving a query request, where the query request is in the format of natural language; determining a case library corresponding to the query request, where the case library records a target query request in a target business scenario, a target query script corresponding to the target query request, the target query script is in the format of structured query language SQL, and the target business scenario is the business scenario to which the query request belongs; determining a first type of case in the case library whose semantic similarity to the query request is greater than a preset value, and determining a second type of case in the case library that contains keywords, where the keywords are included in the query request; filling the query request, the first type of case, and the second type of case into a conversion template to obtain a conversion result; generating a query script corresponding to the query request according to the conversion result.

[0063] An embodiment of the present application also provides an electronic device, including a memory and a processor. A computer program is stored in the memory, and the processor is configured to execute the above method for generating a query script by the computer program.

[0064] The processor in the above electronic device is used to run a program for executing the following functions: receiving a query request, where the query request is in the format of natural language; determining a case library corresponding to the query request, where the case library records a target query request in a target business scenario, a target query script corresponding to the target query request, the target query script is in the format of structured query language SQL, and the target business scenario is the business scenario to which the query request belongs; determining a first type of case in the case library whose semantic similarity to the query request is greater than a preset value, and determining a second type of case in the case library that contains keywords, where the keywords are included in the query request; filling the query request, the first type of case, and the second type of case into a conversion template to obtain a conversion result; generating a query script corresponding to the query request according to the conversion result.

[0065] An embodiment of the present application also provides a computer program product, including computer instructions, and when the computer instructions are executed by a processor, the steps of the above method for generating a query script are implemented.

[0066] It should be noted that each module in the above device for generating query scripts can be a program module (for example, a set of program instructions for implementing a specific function), or a hardware module. For the latter, it can be presented in the following forms, but not limited to: the manifestation of each of the above modules is a processor, or the functions of each of the above modules are implemented by a processor.

[0067] The serial numbers of the above embodiments of the present application are only for description and do not represent the advantages or disadvantages of the embodiments.

[0068] In the above embodiments of the present application, the descriptions of each embodiment have their own emphases. For the parts not detailed in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0069] In the several embodiments provided by the present application, it should be understood that the disclosed technical content can be implemented in other ways. Among them, the device embodiments described above are only illustrative. For example, the division of the units can be a logical function division. In actual implementation, there can be other division methods. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the displayed or discussed coupling or direct coupling or communication connection between each other can be through some interfaces. The indirect coupling or communication connection of units or modules can be in an electrical or other form.

[0070] The units described as separate components may or may not be physically separated. The components displayed as units may or may not be physical units, that is, they can be located in one place, or distributed to multiple units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0071] In addition, each functional unit in the various embodiments of the present application can be integrated in a processing unit, or each unit exists physically alone, or two or more units are integrated in one unit. The above integrated units can be implemented in the form of hardware or in the form of software functional units.

[0072] When the integrated unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the related technology, or all or part of this technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of this application. The aforementioned storage medium includes: various media that can store program codes, such as USB flash drives, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), mobile hard disks, magnetic disks, or optical discs.

[0073] The above are only the preferred embodiments of this application. It should be noted that for those of ordinary skill in the art of this technology, without departing from the principle of this application, several improvements and refinements can be made, and these improvements and refinements should also be regarded as the protection scope of this application.

Claims

1. A method for generating a query script, characterized in that: include: Receiving a query request, wherein the query request format is a natural language; Determine a case library corresponding to the query request, wherein the case library records a target query request under a target business scenario and a target query script corresponding to the target query request, the format of the target query script is structured query language SQL, and the target business scenario is the business scenario to which the query request belongs; Determine in the case library a first type of case whose semantic similarity with the query request is greater than a preset value, and determine in the case library a second type of case containing a keyword, wherein the keyword is contained in the query request; Filling the query request, the first category of cases, and the second category of cases into a conversion template to obtain a conversion result; A query script corresponding to the query request is generated according to the conversion result.

2. The method according to claim 1, characterized in that The case library is generated by the following method: Acquire multiple cases corresponding to different business scenarios, wherein each case records a query request and a query script corresponding to the query request; For each of the cases, determining the query purpose corresponding to the case according to the query request recorded in the case, clustering multiple cases with the same query purpose to obtain a case set, wherein different case sets correspond to different business scenarios; For each of the case sets, determining the structural similarity between any two of the cases in the case set according to the data structure of the query script recorded in the case; The multiple cases corresponding to the multiple structural similarities greater than the preset structural similarity are clustered to obtain the case library.

3. The method according to claim 2, characterized in that After clustering the multiple cases corresponding to the multiple structural similarities greater than the preset structural similarity, the method further includes: Determining a plurality of identical cases having the same data structure among the plurality of clustered cases; In the case where there are multiple identical cases, only one identical case is retained in the case library.

4. The method according to claim 2, characterized in that: The conversion template includes a case placeholder and an input statement placeholder, wherein the position of the case placeholder is used to fill in the case in the case library, and the position of the input statement placeholder is used to fill in the query request; Filling the query request, the first type of cases, and the second type of cases into a conversion template includes: The case placeholder is replaced by the first type of case and the second type of case, and the input statement placeholder is replaced by the query request.

5. The method according to claim 1, characterized in that The keywords are determined by: Performing word segmentation processing on the query request to obtain a plurality of words, wherein the words contain at least one character; For each of the words, determining the frequency of occurrence of the word according to the number of times the word appears in the query request, and determining a score value of the word according to the frequency of occurrence, wherein the score value is used to indicate the importance of the word in the query request; The keyword is determined among the multiple words according to the score value.

6. The method according to claim 1, characterized in that Generating a query script corresponding to the query request according to the conversion result includes: Processing the conversion result using a large language model to obtain an initial query script, and verifying the initial query script, wherein the large language model is a machine learning model with text understanding and generation functions; In the case where the query script passes the verification, determining the initial query script as the query script corresponding to the query request; In the case where the query script fails to pass the verification, a prompt message is output, wherein the prompt message is used to remind the user to correct the initial query script.

7. The method according to claim 6, characterized in that The method further includes: combining the revised initial query script and the query request into a new case, and storing the new case in the case library.

8. A device for generating a query script, characterized in that: include: A receiving module, used for receiving a query request, wherein the query request format is a natural language; A first determination module is used to determine a case library corresponding to the query request, wherein the case library records a target query request under a target business scenario and a target query script corresponding to the target query request, the format of the target query script is a structured query language SQL, and the target business scenario is the business scenario to which the query request belongs; A second determination module is used to determine, in the case library, first-category cases whose semantic similarity with the query request is greater than a preset value, and to determine, in the case library, second-category cases containing a keyword, wherein the keyword is contained in the query request; A processing module, used for filling the query request, the first type of cases and the second type of cases into a conversion template to obtain a conversion result; A generation module is used to generate a query script corresponding to the query request according to the conversion result.

9. A non-volatile storage medium, characterized in that: The non-volatile storage medium stores a computer program, wherein the method for generating a query script according to any one of claims 1 to 7 is executed by running the computer program on the device where the non-volatile storage medium is located.

10. An electronic device comprising a memory and a processor, characterized in that: A computer program is stored in the memory, and the processor is configured to execute the method for generating a query script according to any one of claims 1 to 7 through the computer program.

11. A computer program product comprising computer instructions, characterized in that: When the computer instructions are executed by a processor, the steps of the method for generating a query script according to any one of claims 1 to 7 are implemented.