SQL (Structured Query Language) code generation method and device and financial data query method, device and equipment
Through the generation model of generalized knowledge and specific knowledge training, the SQL code mismatch problem caused by the complexity of user-question statements is solved, efficient and accurate SQL code generation is achieved, and the efficiency and accuracy of database query are improved.
Patent Information
- Application Number
- CN202411952277.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-27
- Publication Date
- 2025-05-02
AI Technical Summary
During the data query process, the diversity and complexity of user question statements lead to mismatch between the generated SQL code and user question statements, which affects the query efficiency and accuracy.
By using generalized knowledge and specific knowledge training to generate large models, they have generalized understanding and specific understanding capabilities. In the process of generating SQL code, they can quickly generate SQL code that matches the question statement, and use generalized knowledge to generate flexible SQL code when it cannot be matched.
It improves the efficiency and accuracy of the SQL code generation process, improves the matching degree of the resulting SQL code with the question statement, thereby improving the efficiency and accuracy of database queries.
Smart Images

Figure CN119918522A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a SQL code generation method and device, and a financial data query method, device and equipment. Background Art
[0002] With the explosive growth of data in various fields, the application of Structured Query Language (SQL) in the data query process has become the mainstream data query method due to its efficient data query capabilities and consistency and integrity of data queries. When generating SQL code for data query, it is necessary to generate corresponding SQL code according to the user's question intention. However, in actual situations, the user's question statements are diverse and complex, which leads to the problem that the generated SQL code and the user's question statements are prone to mismatch. Therefore, it is urgent to propose a new SQL code generation method to cope with complex and diverse actual situations. Summary of the invention
[0003] The present application provides a SQL code generation method and device, a financial data query method, device and equipment, which use generalized knowledge and specific knowledge to train a large model, so that the large model has generalized understanding ability and specific understanding ability for question statements, thereby improving the efficiency and accuracy of the SQL code generation process, and improving the matching degree between the obtained SQL code and the question statement.
[0004] In order to achieve the above objectives, the main technical solutions adopted in this application include:
[0005] In a first aspect, an embodiment of the present application provides a method for generating SQL code, the method comprising:
[0006] Obtaining the question statements raised by the target object regarding the target field;
[0007] Using a large generative model to identify the intent of the question sentence, so as to construct contextual prompt words corresponding to the question sentence; wherein the large generative model is pre-trained using generalized knowledge corresponding to the target field and specific knowledge corresponding to the enterprise to which the target object belongs;
[0008] Using the generated large model, the question statement is accurately matched with the SQL generation template according to the context prompt words to obtain an accurate matching result;
[0009] If the exact matching result indicates that a target SQL generation template corresponding to the question statement is matched, then generating a SQL code corresponding to the question statement based on the target SQL generation template to answer the question statement;
[0010] If the exact matching result indicates that the target SQL generation template corresponding to the question statement is not matched, then an SQL code corresponding to the question statement is generated according to the context prompt word.
[0011] The SQL code generation method proposed in the embodiment of the present application utilizes generalized knowledge and specific knowledge to train and generate a large model, so that the large model has generalized understanding ability and specific understanding ability for question statements. Therefore, in the process of generating SQL code, not only can specific knowledge be used to quickly generate SQL code that matches the question statement, thereby improving the accuracy of the generated SQL code; it can also use generalized knowledge to understand the question statement when the question statement cannot match the specific knowledge to generate the corresponding SQL, thereby improving the flexibility of the obtained SQL code. Compared with the related art, the present application can generate SQL code for the question statement in a variety of ways to adapt to the complexity of the actual question statement, improve the matching degree between the obtained SQL code and the question statement, and thus improve the efficiency and accuracy of database queries.
[0012] Optionally, the generated large model is obtained by:
[0013] Vectorizing the generalized knowledge and the specific knowledge respectively, and constructing a knowledge vector library according to the vectorized generalized knowledge and the specific knowledge;
[0014] Using the knowledge vector library to perform model training to obtain the generated large model;
[0015] The generalized knowledge includes the data structure and data model of the target domain, and the specific knowledge includes the professional terminology definition, historical question-SQL code key-value pairs and historical question-method template key-value pairs of the enterprise to which the target object belongs.
[0016] Optionally, the step of using the generated large model to perform intent recognition on the question sentence to construct context prompt words corresponding to the question sentence includes:
[0017] Using the generated large model to perform semantic decomposition on the question sentence to obtain semantic variables corresponding to the question sentence;
[0018] A similarity search is performed in the knowledge vector library according to the semantic variable to generate the context prompt word.
[0019] Optionally, using the generated large model to perform an accurate match of the SQL generation template on the question statement according to the context prompt words to obtain an accurate matching result includes:
[0020] Determining, according to the context prompt words, a plurality of preset SQL generation templates corresponding to the intention of the question statement;
[0021] Compare the similarity between the placeholders in any preset SQL generation template and the keywords in the question statement to obtain the exact matching result;
[0022] If the target SQL generation template corresponding to the question statement is matched according to the exact matching result, then generating the SQL code corresponding to the question statement based on the target SQL generation template includes:
[0023] If the exact matching result indicates that the similarity between the placeholder and the keyword meets the similarity condition, any one of the preset SQL generation templates is used as the target SQL generation template;
[0024] The target SQL generation template is filled with the keywords to generate SQL codes corresponding to the question statement.
[0025] Optionally, generating an SQL code corresponding to the question statement according to the context prompt word includes:
[0026] The generated large model is used to perform rule analysis on the question sentence based on the context prompt words, so as to generate the SQL code according to the result of the rule analysis.
[0027] Optionally, the method further comprises:
[0028] In the case where the query keyword in the question sentence does not match the field in the queried database, determining the prompt word to be queried that does not match;
[0029] The intermediate query code corresponding to the prompt word to be queried is used to perform data query in the database, and the target field corresponding to the prompt word to be queried is determined to generate the SQL code.
[0030] In a second aspect, an embodiment of the present application provides a financial data query method, the method comprising:
[0031] Obtain the SQL code generated by the method described in any one of the above embodiments;
[0032] Performing a query in a financial database based on the SQL code, and generating a visualization chart based on the query result;
[0033] In response to a feedback operation on the visualization chart, feedback data of the query result is determined to continuously train the generated large model.
[0034] In a third aspect, an embodiment of the present application provides a SQL code generation device, the device comprising:
[0035] A question statement acquisition module is used to acquire question statements issued by the target object for the target field;
[0036] A question intention recognition module is used to use a large generation model to perform intention recognition on the question sentence to construct a context prompt word corresponding to the question sentence; wherein the large generation model is pre-trained using generalized knowledge corresponding to the target field and specific knowledge corresponding to the enterprise to which the target object belongs;
[0037] A template precise matching module, used to use the generated large model to perform precise matching of the SQL generated template on the question statement according to the context prompt words to obtain a precise matching result;
[0038] A template matching generation module, used to generate SQL code, if the target SQL generation template corresponding to the question statement is matched according to the exact matching result, then generate SQL code corresponding to the question statement based on the target SQL generation template to answer the question statement;
[0039] The model prompt generation module is used to generate SQL code. If the exact matching result shows that the target SQL generation template corresponding to the question statement is not matched, the SQL code corresponding to the question statement is generated according to the context prompt word.
[0040] In a fourth aspect, an embodiment of the present application provides a financial data query device, the device comprising:
[0041] A code acquisition module, used to acquire the SQL code generated by any of the methods in the above embodiments;
[0042] A result acquisition module, used to query the financial database based on the SQL code and generate a visual chart based on the query result;
[0043] A feedback training module is used to determine feedback data of the query result in response to a feedback operation on the visualization chart to continuously train the generated large model.
[0044] In a fifth aspect, an embodiment of the present application provides a computer device, comprising: a memory and a processor, wherein the memory and the processor are communicatively connected to each other, computer instructions are stored in the memory, and the processor executes the method described in any one of the above embodiments by executing the computer instructions.
[0045] In a sixth aspect, an embodiment of the present application provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute any one of the methods in the above embodiments.
[0046] In a seventh aspect, an embodiment of the present application provides a computer program product, comprising computer instructions, wherein the computer instructions are used to enable a computer to execute any one of the methods described in the above embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] In order to more clearly illustrate the specific implementation methods of the present application or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0048] Figure 1 A step diagram of the SQL code generation method provided in an embodiment of the present application;
[0049] Figure 2 A diagram showing the steps for generating a large model in an embodiment of the present application;
[0050] Figure 3 A diagram showing the steps of generating context prompt words in an embodiment of the present application;
[0051] Figure 4 A diagram of the steps for obtaining accurate matching in an embodiment of the present application;
[0052] Figure 5 A diagram showing the steps of generating SQL code using an SQL generation template in an embodiment of the present application;
[0053] Figure 6 A diagram showing the steps of generating SQL code by using a large model in an embodiment of the present application;
[0054] Figure 7 A step diagram of a financial data query method provided in an embodiment of the present application;
[0055] Figure 8 A closed-loop schematic diagram of the SQL code generation method and the financial data query method in the embodiment of the present application;
[0056] Fig. 9 A module diagram of a SQL code generation device provided in an embodiment of the present application;
[0057] Fig.10 A module diagram of a financial data query device provided in an embodiment of the present application;
[0058] Fig.11 A schematic diagram of the structure of a computer device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0059] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present application.
[0060] With the explosive growth of data in various fields, the application of Structured Query Language (SQL) in the data query process has become the mainstream data query method due to its efficient data query capabilities and consistency and integrity of data query. When generating SQL code for data query, it is necessary to generate the corresponding SQL code according to the user's question intention.
[0061] There are many ways to generate SQL code in the related technology, including template matching, rule matching, and large wide tables based on data pre-calculation. Among them, when generating SQL code by template matching and rule matching, since the process of building templates and rules relies on manual work, the generated templates and generated rules obtained thereby cannot cover the scope of all actual question statements, resulting in some question statements being unable to match the corresponding generated templates, thereby affecting the SQL code generation process. In addition, large wide tables based on data pre-calculation cannot cope with changing question statements because they need to pre-calculate and store a large amount of data, which limits the flexibility of database queries.
[0062] In actual situations, users' question statements are diverse and complex, which leads to the problem that the generated SQL code is easily mismatched with the user's question statements. Therefore, it is urgent to propose a new SQL code generation method to cope with complex and diverse actual situations.
[0063] Based on the above problems, the present application provides a method and device for generating SQL code, a method, device and equipment for querying financial data, wherein the method for generating SQL code includes: obtaining a question statement issued by a target object for a target field; using a large generation model to identify the intent of the question statement to construct context prompt words; wherein the large generation model is obtained by pre-training with generalized knowledge and specific knowledge; performing an accurate match of the SQL generation template on the question statement according to the context prompt words; if the accurate match result shows that the target SQL generation template is matched, then generating an SQL code corresponding to the question statement based on the target SQL generation template; if the accurate match result shows that the target SQL generation template is not matched, then generating an SQL code corresponding to the question statement according to the context prompt words. The method for querying financial data includes: obtaining SQL code; querying in a financial database based on the SQL code, and generating a visualization chart based on the query result; in response to the feedback operation on the visualization chart, determining the feedback data of the query result to continuously train the large generation model.
[0064] The SQL code generation method proposed in the embodiment of the present application utilizes generalized knowledge and specific knowledge training to generate a large model, so that the large model has generalized understanding ability and specific understanding ability for question statements. Therefore, in the process of generating SQL code, it is not only possible to use specific knowledge to quickly generate SQL code that matches the question statement, thereby improving the accuracy of the generated SQL code; it is also possible to use generalized knowledge to understand the question statement when the question statement cannot match the specific knowledge to generate the corresponding SQL, thereby improving the flexibility of the obtained SQL code. Compared with the related art, the present application can generate SQL code for the question statement in a variety of ways, thereby adapting to the complexity of the actual question statement, improving the matching degree between the obtained SQL code and the question statement, and thus improving the efficiency and accuracy of database queries.
[0065] The SQL code generation method provided in this specification can be applied to generate SQL codes related to database query, and the SQL code is used to query the database of the target field, which may include the financial field or other fields.
[0066] According to an embodiment of the present application, an embodiment of a SQL code generation method 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 a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.
[0067] In this embodiment, a SQL code generation method is provided, which can be used to generate SQL code for querying a database in a target domain. Figure 1As shown, the method includes:
[0068] S110. Obtaining a question sentence issued by the target object regarding the target field.
[0069] S120. Use the generative big model to identify the intent of the question sentence to construct context prompt words corresponding to the question sentence; wherein the generative big model is pre-trained using the generalized knowledge corresponding to the target field and the specific knowledge corresponding to the enterprise to which the target object belongs.
[0070] S130. Using the generated large model, the SQL generation template is accurately matched to the question statement according to the context prompt words to obtain an accurate matching result.
[0071] S140. If the exact matching result indicates that the target SQL generation template corresponding to the question statement is matched, then a SQL code corresponding to the question statement is generated based on the target SQL generation template to answer the question statement.
[0072] S150. If the exact matching result indicates that the target SQL generation template corresponding to the question statement is not matched, then generate the SQL code corresponding to the question statement according to the context prompt words.
[0073] Among them, the question statement can be a statement issued by the target object for the target field, which is used to query the database in the target field to obtain the target data. The question statement includes prompt information and keywords, wherein the keywords are used to indicate the type of target data that the target object needs to query, etc., and the prompt information is used to indicate the target object's question intention and content. It can be understood that the number of prompt information contained in the question statement can be different corresponding to the way of generating SQL statements. In the process of generating SQL code based on the target SQL generation template, the prompt information contained in the question statement can be less, so that the corresponding SQL code can be directly obtained by using the SQL generation template. In the process of generating SQL code based on context prompt words, the prompt information contained in the question statement can be more, so that the generated large model can accurately identify the target object's body temperature intention, etc.
[0074] Generalized knowledge can be common sense knowledge in the target field that is unrelated to the enterprise to which the target object belongs, and can include definitions of common terms in the target field, etc. The large model is generated through generalized knowledge training, so that the generated large model has generalized understanding ability and can understand the question sentence with the generalized knowledge of the target field.
[0075] Specific knowledge can be specific knowledge unique to the enterprise to which the target object belongs, and can include code data assets accumulated by the enterprise during historical data query. By training the generated large model with specific knowledge, the generated large model can obtain specific understanding capabilities and understand the query statements with the specific knowledge accumulated by the enterprise, thereby generating SQL code that better meets the needs of the enterprise.
[0076] Specifically, first obtain the question statements issued by the target object for the target field, and input the question statements into the pre-trained generation model. Since the generation model has been trained with generalized knowledge and specific knowledge, it has a certain understanding of the target field and the relevant knowledge of the enterprise to which the target object belongs. At the same time, the generation model also has the ability to recognize the intent contained in the question statement. Therefore, the generation model is used to identify the intent of the question statement, so as to understand the question intent of the target object contained in the question statement, and generate corresponding context prompt words based on the question intent, so as to further help the generation model understand the question statement and improve the accuracy of the generated SQL code.
[0077] Furthermore, since the knowledge used to train and generate the large model includes specific knowledge of the enterprise to which the target object belongs, the specific knowledge may include SQL generation templates for generating SQL codes. These SQL generation templates may be designed by the enterprise according to its own needs, or may be code data assets accumulated by the enterprise during the data query process. According to the context prompt words, the question statement is accurately matched with the SQL generation template in the specific knowledge to obtain an accurate matching result. It is understandable that the accurate matching result includes whether there is an SQL generation template corresponding to the question statement, and the type of the corresponding target SQL generation template.
[0078] Furthermore, if the exact match result indicates that the target SQL generation template corresponding to the question statement can be matched, then the SQL code corresponding to the question statement is generated based on the target SQL generation template, so as to query the database of the target field through the SQL code to obtain the answer corresponding to the question statement. It can be understood that the method of generating SQL code using the target SQL generation template can shorten the time required to obtain the SQL code and improve the efficiency of data query; and because the SQL generation template is pre-set, its accuracy for data query is higher, which improves the accuracy of data query results. In addition, the target SQL generation template is fixed, which can effectively reduce the risk of prompt injection caused by the target object to the database.
[0079] Furthermore, if the exact match result indicates that the target SQL generation template corresponding to the question statement cannot be matched, that is, the SQL generation template cannot be used to generate SQL code for the question statement, then the large model is generated to further understand the question statement based on the context prompt words, thereby generating SQL code corresponding to the question statement based on the understanding result of the question statement. It can be understood that the SQL code generated by the large model is not restricted by the SQL generation template, has a certain degree of flexibility, and can be adaptively changed according to the question statement of the target object, so as to cope with the complex and changeable question intentions of the target object in actual situations.
[0080] Compared with the related art, the SQL generation method provided by the present application can cope with the diversity of actual question statements and automatically select the corresponding SQL code generation method. When the question statement can match the target SQL generation template, the target SQL generation template can be used to quickly and accurately generate SQL code, thereby improving the efficiency of data query and the accuracy of data query results. And when the question statement fails to match the target SQL generation template, the large generation model can be used to understand the question statement and generate SQL code, thereby improving the flexibility of data query and expanding the scope of application of the present method.
[0081] It should be noted that after the SQL code corresponding to the question statement is generated according to the context prompt words, the SQL code obtained at this time can also be fed back to the generation model, and the SQL code is template-recognized by the generation model, so as to form a new SQL generation template according to the SQL code. The SQL generation template can be reused as new specific knowledge in the subsequent SQL code generation process in the precise matching process, thereby improving the specific understanding ability of the generation model, and further improving the efficiency and accuracy of data query.
[0082] The SQL code generation method provided in this embodiment uses generalized knowledge and specific knowledge training to generate a large model, so that the large model has generalized understanding ability and specific understanding ability for question statements. Therefore, in the process of generating SQL code, it is not only possible to use specific knowledge to quickly generate SQL code that matches the question statement, thereby improving the accuracy of the generated SQL code; it is also possible to use generalized knowledge to understand the question statement when the question statement cannot match the specific knowledge to generate the corresponding SQL, thereby improving the flexibility of the obtained SQL code. Compared with the related art, the present application can generate SQL code for the question statement in a variety of ways, thereby adapting to the complexity of the actual question statement, improving the matching degree between the obtained SQL code and the question statement, and thus improving the efficiency and accuracy of database queries.
[0083] Reference Figure 2As shown, as an embodiment of the present application, a large model is generated by the following method:
[0084] S122. Vectorize the generalized knowledge and specific knowledge respectively, and construct a knowledge vector library based on the vectorized generalized knowledge and specific knowledge; wherein the generalized knowledge includes the data structure and data model of the target domain, and the specific knowledge includes the professional terminology definition, historical question-SQL code key-value pairs, and historical question-method template key-value pairs of the enterprise to which the target object belongs.
[0085] S124. Use the knowledge vector library to train the model and generate a large model.
[0086] Specifically, generalized knowledge includes the data structure and data model of the target domain, including DDL table creation statements (Data Definition Language), etc., so that the generated large model has the ability to understand the database of the target domain. Specific knowledge includes the professional terminology definition, historical question-SQL code key-value pairs (Question-SQL) and historical question-method template key-value pairs (Function template) of the enterprise to which the target object belongs. Among them, the professional terminology definition of the enterprise to which the target object belongs can be the internal document (Documentation) of the enterprise, so as to understand the way the data is sorted within the enterprise. The historical question-SQL code key-value pair can be the correct data query SQL code accumulated by the enterprise in the process of historical data query and the meaning of the queried data, and the historical question-method template key-value pair can be the method template for querying data accumulated by the enterprise in the process of historical data query. Using the specific knowledge of the enterprise for model training effectively utilizes the code data assets accumulated by the enterprise and improves the accuracy of data query.
[0087] Compared with the related art, this application uses generalized knowledge and specific knowledge to train and generate a large model, so that the large model has both generalized understanding ability and specific understanding ability for question statements. In the process of generating SQL code, it can not only use specific knowledge to quickly generate SQL code that matches the question statement, improving the accuracy of the generated SQL code; it can also use generalized knowledge to understand the question statement when the question statement cannot match the specific knowledge to generate the corresponding SQL, thereby improving the flexibility of the SQL code.
[0088] It can be understood that the generation model in this method combines specific knowledge as prior knowledge and the ability of generalized understanding of the target domain to generate SQL code, which simulates the process of manually writing SQL code to a certain extent. Therefore, it has reference value in understanding question statements and improving the accuracy of the resulting SQL code. Technical personnel can further improve this method based on the performance of the generation model in the process of generating SQL code.
[0089] Reference Figure 3 As shown, as an embodiment of the present application, the intention of the question sentence is recognized by using the generated large model to construct the context prompt words corresponding to the question sentence, including:
[0090] S126. Use the generative large model to perform semantic decomposition on the question sentence to obtain the semantic variables corresponding to the question sentence.
[0091] S128. Perform similarity search in the knowledge vector library based on the semantic variables to generate context prompt words.
[0092] Specifically, the generative large model can be any natural language processing model that has a certain ability to understand natural language. Therefore, the generative large model can be used to perform semantic decomposition on the question sentence to obtain the semantic variables corresponding to the question sentence.
[0093] Furthermore, similarity retrieval is performed in the knowledge vector library based on the semantic variables, and multiple similar knowledge that meets the similarity requirements with the question sentence is obtained by comparing the semantic variables with the generalized knowledge and the specific knowledge. The multiple similar knowledge is input into the generation model, and the multiple similar knowledge is sorted and understood by the generation model, so as to generate context prompt words corresponding to the question sentence.
[0094] Reference Figure 4 and Figure 5 As shown, as an embodiment of the present application, a large model is generated, and the SQL generation template is accurately matched to the question statement according to the context prompt words to obtain an accurate matching result, including:
[0095] S132. Determine multiple preset SQL generation templates corresponding to the intention of the question sentence according to the context prompt words.
[0096] S134. Compare the similarity between the placeholders in any preset SQL generation template and the keywords in the question statement to obtain an accurate matching result.
[0097] If the exact matching result indicates that the target SQL generation template corresponding to the question statement is matched, then the SQL code corresponding to the question statement is generated based on the target SQL generation template, including:
[0098] S142. If the exact matching result indicates that the similarity between the placeholder and the keyword meets the similarity condition, any preset SQL generation template is used as the target SQL generation template.
[0099] S144. Use keywords to fill in the target SQL generation template to generate SQL code corresponding to the question statement.
[0100] Among them, the context prompt words can be prompt words constructed by the generation model, which are similar to the question intent of the question sentence. The preset SQL generation templates can be multiple templates classified according to the query intent. Each preset SQL generation template has multiple placeholders. The placeholders are used to fill in variables to generate corresponding SQL codes. Each placeholder corresponds to variable-related features such as variable type and name.
[0101] Specifically, in the precise matching process, the question intent corresponding to the context prompt word is first compared with the query intent corresponding to the preset SQL generation template, so as to perform intent matching on the preset SQL generation template and determine multiple preset SQL generation templates that meet the intent of the question statement. The placeholders in any preset SQL generation template are matched with the keywords in the question statement for similarity to compare whether the variable-related features such as the variable type and name corresponding to the placeholder are consistent with the features of the keywords in the question statement, thereby obtaining a precise matching result.
[0102] Furthermore, if the similarity between the placeholder and the keyword meets the similarity condition, it means that any preset SQL generation template can be used as the target SQL generation template. According to the exact matching result, the keywords in the question statement are filled into the corresponding placeholders in the target SQL generation template, thereby generating the SQL code corresponding to the question statement.
[0103] As an embodiment of the present application, generating SQL code corresponding to the question statement according to the context prompt word includes:
[0104] S152. Using the generated large model, rule analysis is performed on the question statement based on the context prompt words to generate SQL code according to the result of the rule analysis.
[0105] Specifically, if the exact matching result shows that the target SQL generation template corresponding to the question statement is not matched, it means that there is no SQL generation template matching the question statement in the existing specific knowledge. At this time, the generalized knowledge can be used to generate a large model, and the question statement can be analyzed based on the context prompt words to obtain the question rules of the question statement, and the SQL code can be generated according to the obtained question rules.
[0106] It can be understood that the SQL code obtained by the above method is not restricted by the preset SQL generation template, but is generated only based on the generation model's understanding of the question statement. Therefore, compared with the SQL code generated by the target SQL generation template, the SQL code is more flexible and expands the scope of data query, so that it can adapt to the needs of actual scenarios.
[0107] Furthermore, before using the generated large model to perform rule analysis on the question statement, a similarity search can be performed again in the knowledge vector library based on the question statement to obtain supplementary prompt words and input them into the generated large model to help the large model understand the rules of the question statement and generate SQL code. It should be noted that the similarity condition of the similarity search at this time is different from the similarity condition when the context prompt words are obtained, so the supplementary prompt words are different from the context prompt words, and the scope of the supplementary prompt words is larger than the context prompt words, so as to enhance the ability of the generated large model to understand the question statement. After receiving the supplementary prompt words, the generated large model combines the context prompt words and the supplementary prompt words to perform rule analysis on the question statement, so as to generate the corresponding SQL code.
[0108] In some embodiments, if the generated SQL code cannot be executed correctly, the SQL code and the error code at this time are input into the generated large model, and the generated large model repairs the SQL code according to the error code, so as to correctly execute the query process. It is understandable that in this embodiment, new context prompt words can also be constructed from the knowledge vector library according to the error code to improve the ability of the generated large model to understand the question statement, so as to generate a new SQL code.
[0109] Reference Figure 6 As shown, as an embodiment of the present application, the method further includes:
[0110] S162. When the query keyword in the question statement does not match the field in the queried database, determine the prompt words to be queried that do not match.
[0111] S164. Use the intermediate query code corresponding to the prompt word to be queried to perform data query in the database, determine the target field corresponding to the prompt word to be queried, and use it to generate SQL code.
[0112] Specifically, the question statement issued by the target object contains multiple query keywords, which represent the query purpose and query object of the target object, and thus generate SQL code for query. However, the query keyword may not match the field in the queried database. For example, in the financial field, the target object issues a question statement to query the closing price of any stock. The query keyword used to refer to the stock in the question statement is the name of the stock, while the field used to refer to the stock in the queried database is the stock code of the stock. At this time, since the query keyword and the field do not match, the generated SQL code cannot be used directly to query the database.
[0113] Further, in the above case, the query keywords that do not match the fields are first determined to obtain the prompt words to be queried. The corresponding intermediate query code is constructed for the prompt words to be queried using the generated large model, and the intermediate query code is used to perform data query in the database to determine the target field corresponding to the prompt words to be queried, thereby replacing the prompt words to be queried with the target field to generate new SQL code. By constructing the intermediate query code and using the intermediate query code to construct the association between the prompt words to be queried and the target field, when the question statement issued by the target object does not match the field in the database, the SQL code generation method provided by the present application can still obtain the SQL code that can be correctly executed, thereby improving the efficiency and accuracy of data query.
[0114] Reference Figure 7 As shown, the embodiment of the present application provides a financial data query method, the method comprising:
[0115] S710. Obtain the SQL code generated by the method in any one of the above embodiments.
[0116] S720. Query the financial database based on the SQL code, and generate a visual chart based on the query result.
[0117] S730. In response to the feedback operation on the visualization chart, determine the feedback data of the query result to continuously train the generated large model.
[0118] Specifically, the target object determines whether the query result is correct based on the visual chart, and performs feedback operations on the generated large model. In the feedback operation, the feedback data of the target object can be the current question-SQL code key-value pair containing the current question statement and SQL code. The knowledge vector library is updated through the feedback data, so that in the subsequent or other SQL code generation process, the generated large model can combine the knowledge of the current question-SQL code key-value pair to generate more accurate context prompt words and SQL codes. Through continuous update training, the corpus accumulation in the knowledge vector library is enriched, thereby gradually improving the accuracy of the resulting SQL code.
[0119] Reference Figure 8 As shown, the SQL code generation method and financial data query method provided by the present application constitute a closed-loop training for the generated large model. After the SQL code is generated by the SQL code generation method and the query is performed, the knowledge vector library is updated according to the feedback data of the query result, thereby further improving the generated large model's ability to understand the question statement. Through continuous training of the generated large model, the accuracy of the obtained SQL code is gradually improved during the data query process, thereby improving the efficiency and effect of data query.
[0120] Accordingly, please refer to Fig. 9 , an embodiment of the present application provides a SQL code generation device, the device comprising:
[0121] The question sentence acquisition module 910 is used to acquire the question sentence issued by the target object for the target field.
[0122] The question intention recognition module 920 is used to use the generated large model to recognize the intention of the question sentence to construct context prompt words corresponding to the question sentence; wherein the generated large model is pre-trained using the generalized knowledge corresponding to the target field and the specific knowledge corresponding to the enterprise to which the target object belongs.
[0123] The template precise matching module 930 is used to use the generated large model to perform precise matching of the SQL generated template on the question statement according to the context prompt words to obtain a precise matching result.
[0124] Template matching generation module 940 is used to generate SQL code. If the exact matching result shows that the target SQL generation template corresponding to the question statement is matched, then the SQL code corresponding to the question statement is generated based on the target SQL generation template to answer the question statement.
[0125] The model prompt generation module 950 is used to generate SQL code. If the exact matching result shows that the target SQL generation template corresponding to the question statement is not matched, the SQL code corresponding to the question statement is generated according to the context prompt words.
[0126] In some optional embodiments, the device further includes a generation model training module, including:
[0127] The knowledge vectorization unit is used to vectorize the generalized knowledge and the specific knowledge respectively, and to construct a knowledge vector library based on the vectorized generalized knowledge and the specific knowledge.
[0128] The model training unit is used to perform model training using the knowledge vector library to generate a large model.
[0129] Among them, generalized knowledge includes the data structure and data model of the target field, and specific knowledge includes the professional terminology definition, historical question-SQL code key-value pairs, and historical question-method template key-value pairs of the enterprise to which the target object belongs.
[0130] In some optional implementations, the question intention identification module 920 includes:
[0131] The semantic decomposition unit is used to perform semantic decomposition on the question sentence by using the generated large model to obtain the semantic variables corresponding to the question sentence.
[0132] The similarity search unit is used to perform similarity retrieval in the knowledge vector library according to the semantic variables to generate context prompt words.
[0133] In some optional implementations, the template exact matching module 930 includes:
[0134] The intention matching unit is used to determine multiple preset SQL generation templates corresponding to the intention of the question sentence according to the context prompt words.
[0135] The similarity matching unit is used to compare the similarity between the placeholders in any preset SQL generation template and the keywords in the question statement to obtain an accurate matching result.
[0136] The template matching generation module 940 includes:
[0137] The target template determination unit is used to determine the target SQL generation template. If the exact matching result shows that the similarity between the placeholder and the keyword meets the similarity condition, any preset SQL generation template is used as the target SQL generation template.
[0138] The keyword filling unit is used to fill the target SQL generation template with keywords to generate SQL code corresponding to the question statement.
[0139] In some optional implementations, the model hint generation module 950 includes:
[0140] The rule analysis unit is used to use the generated large model to perform rule analysis on the question sentence based on the context prompt words, so as to generate SQL code according to the result of the rule analysis.
[0141] In some optional implementations, the device further includes an intermediate query module, including:
[0142] The prompt word determination unit to be queried is used to determine the prompt word to be queried that does not match when the query keyword in the question sentence does not match the field in the queried database.
[0143] The intermediate query unit is used to perform data query in the database using the intermediate query code corresponding to the prompt word to be queried, and determine the target field corresponding to the prompt word to be queried, so as to generate SQL code.
[0144] Please refer to Fig.10 The present application also provides a financial data query device, which includes:
[0145] The code acquisition module 1010 is used to acquire the SQL code generated by the method in any one of the above embodiments.
[0146] The result acquisition module 1020 is used to query the financial database based on the SQL code and generate a visual chart based on the query result.
[0147] The feedback training module 1030 is used to determine the feedback data of the query result in response to the feedback operation on the visualization chart, so as to continuously train the generated large model.
[0148] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.
[0149] The SQL code generation device and financial data query device in this embodiment are presented in the form of functional units, where the units refer to ASIC (Application Specific Integrated Circuit) circuits, processors and memories that execute one or more software or fixed programs, and / or other devices that can provide the above functions.
[0150] See also Fig.11 , Fig.11 It is a structural diagram of a computer device provided by an embodiment of the present application. As shown in the figure, the computer device includes: one or more processors 10, a memory 20, and an interface for connecting various components, including a high-speed interface and a low-speed interface. Each component uses different buses to communicate with each other, and can be installed on a common motherboard or installed in other ways as needed. The processor can process instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories and multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Fig.11 A processor 10 is taken as an example.
[0151] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.
[0152] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.
[0153] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.
[0154] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.
[0155] The computer device further comprises a communication interface 30 for the computer device to communicate with other devices or a communication network.
[0156] The embodiment of the present application also provides a computer-readable storage medium. The above method according to the embodiment of the present application can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.
[0157] The embodiment of the present application provides a computer program product, which includes computer instructions, which are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method of any embodiment of the present application.
[0158] Although the embodiments of the present application are described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present application, and such modifications and variations are all within the scope defined by the appended claims.
[0159] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.
[0160] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.
[0161] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0162] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0163] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1A function specified in one or more boxes.
[0164] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process in the computer or other programmable device. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0165] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.
[0166] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0167] The above is only the embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.
[0168] Although the embodiments of the present application have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present application, and such modifications and variations are all within the scope defined by the appended claims.
Claims
1. A method for generating SQL code, characterized in that: The method comprises: Obtaining the question statements raised by the target object regarding the target field; Using a large generative model to identify the intent of the question sentence, so as to construct contextual prompt words corresponding to the question sentence; wherein the large generative model is pre-trained using generalized knowledge corresponding to the target field and specific knowledge corresponding to the enterprise to which the target object belongs; Using the generated large model, the question statement is accurately matched with the SQL generation template according to the context prompt words to obtain an accurate matching result; If the exact matching result indicates that a target SQL generation template corresponding to the question statement is matched, then generating a SQL code corresponding to the question statement based on the target SQL generation template to answer the question statement; If the exact matching result indicates that the target SQL generation template corresponding to the question statement is not matched, then an SQL code corresponding to the question statement is generated according to the context prompt word.
2. The method according to claim 1, characterized in that: The generated large model is obtained by the following method: Vectorizing the generalized knowledge and the specific knowledge respectively, and constructing a knowledge vector library according to the vectorized generalized knowledge and the specific knowledge; Using the knowledge vector library to perform model training to obtain the generated large model; The generalized knowledge includes the data structure and data model of the target domain, and the specific knowledge includes the professional terminology definition, historical question-SQL code key-value pairs and historical question-method template key-value pairs of the enterprise to which the target object belongs.
3. The method according to claim 2, characterized in that The step of using the generated large model to perform intent recognition on the question sentence to construct context prompt words corresponding to the question sentence includes: Using the generated large model to perform semantic decomposition on the question sentence to obtain semantic variables corresponding to the question sentence; A similarity search is performed in the knowledge vector library according to the semantic variable to generate the context prompt word.
4. The method according to claim 1, characterized in that: The method of using the generated large model to accurately match the SQL generation template on the question statement according to the context prompt words to obtain an accurate matching result includes: Determining, according to the context prompt words, a plurality of preset SQL generation templates corresponding to the intention of the question statement; Compare the similarity between the placeholders in any preset SQL generation template and the keywords in the question statement to obtain the exact matching result; If the target SQL generation template corresponding to the question statement is matched according to the exact matching result, then generating the SQL code corresponding to the question statement based on the target SQL generation template includes: If the exact matching result indicates that the similarity between the placeholder and the keyword meets the similarity condition, any one of the preset SQL generation templates is used as the target SQL generation template; The target SQL generation template is filled with the keywords to generate SQL codes corresponding to the question statement.
5. The method according to claim 1, characterized in that The step of generating an SQL code corresponding to the question statement according to the context prompt word includes: The generated large model is used to perform rule analysis on the question sentence based on the context prompt words, so as to generate the SQL code according to the result of the rule analysis.
6. The method according to any one of claims 1 to 5, characterized in that The method further comprises: In the case where the query keyword in the question sentence does not match the field in the queried database, determining the prompt word to be queried that does not match; The intermediate query code corresponding to the prompt word to be queried is used to perform data query in the database, and the target field corresponding to the prompt word to be queried is determined to generate the SQL code.
7. A financial data query method, characterized in that: The method comprises: Obtaining the SQL code generated by the method according to any one of claims 1 to 6; Performing a query in a financial database based on the SQL code, and generating a visualization chart based on the query result; In response to a feedback operation on the visualization chart, feedback data of the query result is determined to continuously train the generated large model.
8. A SQL code generation device, characterized in that: The device comprises: A question statement acquisition module is used to acquire question statements issued by the target object for the target field; A question intention recognition module is used to use a large generation model to perform intention recognition on the question sentence to construct a context prompt word corresponding to the question sentence; wherein the large generation model is pre-trained using generalized knowledge corresponding to the target field and specific knowledge corresponding to the enterprise to which the target object belongs; A template precise matching module, used to use the generated large model to perform precise matching of the SQL generated template on the question statement according to the context prompt words to obtain a precise matching result; A template matching generation module, used to generate SQL code, if the target SQL generation template corresponding to the question statement is matched according to the exact matching result, then generate SQL code corresponding to the question statement based on the target SQL generation template to answer the question statement; The model prompt generation module is used to generate SQL code. If the exact matching result shows that the target SQL generation template corresponding to the question statement is not matched, the SQL code corresponding to the question statement is generated according to the context prompt word.
9. A financial data query device, characterized in that: The device comprises: A code acquisition module, used to acquire the SQL code generated by the method according to any one of claims 1 to 6; A result acquisition module, used to query the financial database based on the SQL code and generate a visual chart based on the query result; A feedback training module is used to determine feedback data of the query result in response to a feedback operation on the visualization chart to continuously train the generated large model.
10. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the method according to any one of claims 1 to 7 by executing the computer instructions.
Citation Information
Cited By
Code generation method and device, electronic equipment, storage medium and program product
CN120508572A
Data processing method and device, and related equipment and product
CN120911589A