Query method and system for lightweight enterprise NL2SQL model, medium and equipment

By generating a set of metadata vectors based on enterprise scenarios and industry experience and fine-tuning the NL2SQL model, the problem of insufficient accuracy of query generation in existing technology in enterprise scenarios is solved, and more efficient and accurate SQL query generation is achieved.

CN120179758APending Publication Date: 2025-06-20GUANGZHOU FRONTOP DIGITAL ORIGINALITY TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202510261491.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-03-06
Publication Date
2025-06-20

AI Technical Summary

Technical Problem

Existing NL2SQL solutions show limitations in enterprise scenarios, including insufficient understanding of enterprise-specific data table structure, field naming and industry terms, resulting in insufficient accuracy and applicability of query generation.

Method used

Prompt words are obtained by generating a set of metadata vectors based on enterprise scenario data tables and industry experience data sets, and matching them using strong models such as GPT-4, Gemini or Claude. Then, based on the Q&A training data, the weak model is fine-tuned to generate an NL2SQL model that is more suitable for practical application scenarios.

Benefits of technology

It improves the understanding of the enterprise-specific environment, reduces the need for new data annotation, improves the speed and accuracy of query generation, ensures the high quality and high correlation of SQL query statements, and is suitable for specific enterprise needs.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120179758A_ABST
    Figure CN120179758A_ABST
Patent Text Reader

Abstract

The invention discloses a lightweight enterprise NL2SQL model query method and system, a medium and equipment. The lightweight enterprise NL2SQL model query method comprises the steps of obtaining a user natural language query text; matching the natural language query text of the user with a preset metadata vector set to obtain prompt words; wherein the metadata vector set is generated by processing an enterprise scene data table and an industry experience data set according to a preset strong model; the strong model is any one of a GPT-4 model, a Gemini model or a Clade model; according to a preset NL2SQL model, processing the cue word to obtain an SQL query statement corresponding to the natural language query text of the user, and completing query; wherein the NL2SQL model is obtained by finely adjusting a preset weak model according to question and answer training data; the question and answer training data is generated by processing an enterprise scene data table and an industry experience data set according to the strong model, and the weak model is any one of a Qwen model, a GLM4 model or a Lma model. By optimizing the query generation method, high-precision conversion of natural language query in a specific environment of an enterprise is realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of natural language processing (NLP) and database query technology, and relates to a query method, system, medium and device for a lightweight enterprise NL2SQL model. Background Art

[0002] In today's enterprise environment, data analysis has become an integral part of the decision-making process. As the complexity of business and the amount of data grow, business personnel need an easy way to get the required information directly from the database. Traditional data analysis methods often rely on professional technicians to write complex SQL queries, which not only increases labor costs but also limits non-technical personnel's instant access to data. To meet this demand, Natural Language to SQL (NL2SQL) technology came into being, which allows users to ask questions in natural language and automatically convert them into corresponding SQL queries, thereby simplifying the process of data query.

[0003] However, existing NL2SQL solutions show obvious limitations when applied to enterprise scenarios. First, since most of these models are trained in general fields, they have very limited understanding of the data table structure, field naming, and industry terminology that are unique to the enterprise, resulting in poor adaptability to the specific environment of the enterprise. Secondly, in actual applications, when faced with complex business needs or unseen data patterns, the SQL queries generated by existing models are often not accurate enough, and even logical errors may occur, which seriously affects the reliability and practicality of the query results. Therefore, the lack of accuracy in query generation by existing technologies in enterprise applications has become a key issue that needs to be solved urgently. Summary of the invention

[0004] In view of the deficiencies in the prior art, the present application provides a query method, system, medium and device for a lightweight enterprise NL2SQL model, which achieves high-precision conversion of natural language queries in an enterprise-specific environment by optimizing the query generation method.

[0005] To achieve the above objectives, in a first aspect, the present invention provides a query method for a lightweight enterprise NL2SQL model, comprising:

[0006] Obtain user natural language query text;

[0007] Matching the user's natural language query text with a preset metadata vector set to obtain a prompt word; wherein the metadata vector set is generated by processing an enterprise scenario data table and an industry experience data set according to a preset strong model; the strong model is any one of the following models: a GPT-4 model, a Gemini model, or a Claude model;

[0008] Process the prompt according to a preset NL2SQL model to obtain an SQL query statement corresponding to the user's natural language query text, and complete the query; wherein, the NL2SQL model is obtained by fine-tuning a preset weak model according to question-and-answer training data; wherein, the question-and-answer training data is generated by processing an enterprise scenario data table and an industry experience data set according to the strong model, and the weak model is any one of the following models: Qwen model, GLM4 model or Llama model.

[0009] Compared with the prior art, the embodiments of the present application have the following beneficial effects: generating a metadata vector set based on an enterprise scenario data table and an industry experience data set can make full use of existing experience and data, enhance the understanding ability of a specific enterprise environment, and reduce the need for new data annotation; by using this metadata vector set, it is possible to quickly locate enterprise data table information and query patterns related to the user's input text, thereby improving the speed and accuracy of query generation; at the same time, the metadata vector set is generated based on a strong model with powerful natural language understanding and generation capabilities, ensuring that the vector set can provide high-quality and highly relevant matching results; using the question-and-answer training data generated based on the enterprise scenario data table and the industry experience data set to fine-tune the weak model to obtain the NL2SQL model can make the NL2SQL model more suitable for the actual application scenario, improve the accuracy and applicability of SQL query generation while maintaining high efficiency, and provide customized services for specific enterprise needs.

[0010] In some embodiments of the first aspect of the present application, the metadata vector set is generated by processing an enterprise scenario data table and an industry experience data set according to a preset strong model, including:

[0011] Extract the table structure information of each of the enterprise scenario data tables;

[0012] Extract each measurable field that meets the preset measurable rules, the corresponding data type information and quantity information in each of the enterprise scenario data tables, and construct table description data corresponding to each of the enterprise scenario data tables.

[0013] Compared with the prior art, the above embodiments have the following beneficial effects: extracting the table structure information of each enterprise scenario data table enhances the understanding of the data table and provides important context information for subsequent query generation; extracting fields and data type information that meet the preset measurable rules and constructing detailed table description data provide richer data for subsequent metadata construction.

[0014] In some embodiments of the first aspect of the present application, the metadata vector set is generated by processing an enterprise scenario data table and an industry experience data set according to a preset strong model, and further includes:

[0015] Extract and summarize each data analysis type in the industry experience dataset to obtain a set of general analysis types;

[0016] According to each general analysis type in the set of general analysis types, extract the corresponding natural language query task text in each enterprise scenario data table;

[0017] According to the strong model, process each natural language query task text and enterprise scenario data table to obtain the first SQL query task statement.

[0018] Compared with the prior art, the above embodiments have the following beneficial effects: By extracting general analysis types from industry data and generating corresponding natural language query task texts in each enterprise scenario data table based on the extracted general analysis types, it is ensured that the subsequent constructed metadata vector set contains various common data analysis tasks and query patterns, and at the same time enhances the understanding ability of specific enterprise scenarios; According to the strong model (such as GPT-4, Gemini or Claude), process the natural language query task text and enterprise scenario data table to generate a high-quality first SQL query task statement, reducing syntax errors and logical deviations, and improving the accuracy when using the metadata vector set for query generation subsequently.

[0019] In some embodiments of the first aspect of the present application, the metadata vector set is generated by processing the enterprise scenario data table and the industry experience dataset according to a preset strong model, and further includes:

[0020] Merge the table structure information, table description data and the first SQL query task statement corresponding to each enterprise scenario data table to obtain metadata;

[0021] According to the preset vector model, process each metadata to obtain the metadata vector set corresponding to each metadata.

[0022] Compared with the prior art, the above embodiments have the following beneficial effects: By integrating various data and performing vectorization processing, the conversion from text to numerical values is realized, which is convenient for subsequent calculation of similarity and execution of efficient retrieval operations, and speeds up the subsequent query response speed.

[0023] In some embodiments of the first aspect of the present application, the matching of the user natural language query text with the preset metadata vector set to obtain a prompt word includes:

[0024] According to the vector model, process the user natural language query text to obtain a query embedding vector;

[0025] Calculate the similarity values between each metadata vector in the metadata vector set and the query embedding vector respectively, and filter out the metadata corresponding to the metadata vector with the maximum similarity value as the first metadata;

[0026] Construct a prompt word according to the user's natural language query text and the first metadata.

[0027] Compared with the prior art, the above embodiments have the following beneficial effects: Through query embedding processing, the user's query is transformed into a form that is easy for a computer to process, and the metadata vector is accurately matched using the similarity value and a prompt word is generated, which improves the query response speed and accuracy in practical applications and improves the user experience.

[0028] In some embodiments of the first aspect of the present application, the NL2SQL model is obtained by fine-tuning a preset weak model according to question-answer training data, including:

[0029] Summarize each natural language query task text and the corresponding first SQL query task statement to obtain question-answer training data;

[0030] Fine-tune the weak model according to the question-answer training data and a preset supervised fine-tuning algorithm to obtain a fine-tuned model.

[0031] Compared with the prior art, the above embodiments have the following beneficial effects: Using the first SQL query task statement generated by the strong model, the natural language query task text, and the enterprise scenario data table, as well as the natural language query task text, high-quality question-answer training data is obtained, and the weak model is initially fine-tuned using this training data, which not only enhances the customization ability and adaptation ability of the model, but also improves the model optimization efficiency and quickly adapts to new data and query requirements.

[0032] In some embodiments of the first aspect of the present application, the NL2SQL model is obtained by fine-tuning a preset weak model according to question-answer training data, and further includes:

[0033] Process each natural language query task text and the enterprise scenario data table according to the fine-tuned model to obtain a second SQL query task statement;

[0034] Summarize each natural language query task text and the corresponding first SQL query task statement and the second SQL query task statement to obtain triple training data;

[0035] Fine-tune the fine-tuned model according to each triple training data and a preset direct preference optimization algorithm to obtain the NL2SQL model.

[0036] Compared with the prior art, the above embodiments have the following beneficial effects: The process of using the fine-tuning model to regenerate the SQL query statement verifies the effect of model improvement and provides a basis for subsequent optimization; Using the first SQL query statement, the second SQL query statement, and the natural language query task text to construct a triple training data set further enriches the training data and helps capture more patterns and relationships during subsequent training; Using the direct preference optimization algorithm to further fine-tune the fine-tuning model, this algorithm obtains a better query generation strategy through contrastive learning, improving the performance and preference accuracy of the model in practical applications and ensuring that the generated SQL query statements better meet the expectations and needs of users.

[0037] In a second aspect, the present invention also provides a query system for a lightweight enterprise NL2SQL model, including: a query text acquisition module, a prompt word generation module, and a query module:

[0038] Among them, the query text acquisition module is used to acquire the user's natural language query text;

[0039] The prompt word generation module is used to match the user's natural language query text with a preset metadata vector set to obtain a prompt word; among them, the metadata vector set is generated by processing the enterprise scenario data table and the industry experience data set according to a preset strong model; the strong model is any one of the following models: GPT-4 model, Gemini model, or Claude model;

[0040] The query module is used to process the prompt word according to a preset NL2SQL model to obtain the SQL query statement corresponding to the user's natural language query text and complete the query; among them, the NL2SQL model is obtained by fine-tuning a preset weak model according to the question-and-answer training data; among them, the question-and-answer training data is generated by processing the enterprise scenario data table and the industry experience data set according to the strong model, and the weak model is any one of the following models: Qwen model, GLM4 model, or Llama model.

[0041] Compared with the prior art, the embodiments of the present application have the following beneficial effects: Generating a metadata vector set based on the enterprise scenario data table and the industry experience data set can make full use of the existing experience and data, enhance the understanding ability of the specific enterprise environment, and reduce the need for new data annotation; By using this metadata vector set, the enterprise data table information and query patterns related to the user's input text can be quickly located, thereby improving the speed and accuracy of query generation; At the same time, the metadata vector set is generated based on a strong model with powerful natural language understanding and generation capabilities, ensuring that the vector set can provide high-quality and highly relevant matching results; Using the question-and-answer training data generated based on the enterprise scenario data table and the industry experience data set to fine-tune the weak model to obtain the NL2SQL model can make the NL2SQL model more suitable for the actual application scenario, improve the accuracy and applicability of SQL query generation while maintaining high efficiency, and provide customized services for specific enterprise needs.

[0042] In a third aspect, the present invention also provides a query device for a lightweight enterprise NL2SQL model, including a memory, a processor, and a computer program stored on the memory and executable on the processor. When the computer program is loaded into the processor, the steps of the query method for a lightweight enterprise NL2SQL model are implemented.

[0043] In a fourth aspect, the embodiments of the present application also provide a computer-readable storage medium storing a computer program, and when the computer program is executed by a processor, the steps of the query method for a lightweight enterprise NL2SQL model are implemented. BRIEF DESCRIPTION OF THE DRAWINGS

[0044] Figure 1 : It is a schematic flowchart of a query method for a lightweight enterprise NL2SQL model provided in some embodiments of the present invention.

[0045] Figure 2 : It is a schematic structural diagram of a query system for a lightweight enterprise NL2SQL model provided in some embodiments of the present invention.

[0046] Figure 3 : It is a structural diagram of a query device for a lightweight enterprise NL2SQL model provided in some embodiments of the present invention.

[0047] Figure 4 : It is an effect diagram of a comparative experiment of a query method for a lightweight enterprise NL2SQL model provided in some embodiments of the present invention.

[0048] Figure 5 : It is an effect diagram of a comparative experiment of a query method for a lightweight enterprise NL2SQL model provided in some embodiments of the present invention. Detailed implementation mode

[0049] The following will clearly and completely describe the technical solutions in the embodiments of the present invention with reference to the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only a part of the embodiments of the present invention, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative work belong to the scope of protection of the present invention.

[0050] Embodiment 1:

[0051] Please refer to Figure 1 , a query method for a lightweight enterprise NL2SQL model provided by an embodiment of the present invention, including steps S1 to S3:

[0052] Step S1: Obtain the user's natural language query text.

[0053] Step S2: Match the user's natural language query text with a preset metadata vector set to obtain a prompt word.

[0054] Among them, the metadata vector set is generated by processing enterprise scenario data tables and industry experience data sets according to a preset strong model. In specific implementation, the strong model can be any one of the following models: GPT-4 model, Gemini model, or Claude model.

[0055] Preferably, the metadata vector set can be generated through the following preferred implementation methods, and the steps include: S11-S17, specifically as follows:

[0056] S11: Extract the table structure information of each of the enterprise scenario data tables;

[0057] S12: Extract each measurable field that meets the preset measurable rules, as well as the corresponding data type information and quantity information in each of the enterprise scenario data tables, and construct the table description data corresponding to each of the enterprise scenario data tables.

[0058] In this preferred embodiment, steps S11-S12 enhance the understanding of the data tables by extracting the table structure information of each enterprise scenario data table, providing important context information for subsequent query generation; extracting fields and data type information that meet the preset measurable rules and constructing detailed table description data provide richer data for the subsequent construction of metadata.

[0059] S13: Extract and summarize each data analysis type in the industry experience data set to obtain a general analysis type set;

[0060] In specific implementation, the extraction steps can be expressed as follows:

[0061] A = summarize_analysis_types(H), where H represents the industry experience dataset, A represents the set of general analysis types, and summarize_analysis_types() represents the extraction and summarization operation, which can be specifically implemented directly through the processing of a strong model.

[0062] S14: Extract the corresponding natural language query task texts in each of the enterprise scenario data tables according to each general analysis type in the set of general analysis types;

[0063] S15: Process each of the natural language query task texts and the enterprise scenario data tables according to the strong model to obtain the first SQL query task statement.

[0064] In this preferred embodiment, steps S13 - S15 extract general analysis types from industry data, and based on the extracted general analysis types, generate corresponding natural language query task texts in each enterprise scenario data table, ensuring that the subsequent constructed metadata vector set contains various common data analysis tasks and query patterns, while enhancing the understanding ability of specific enterprise scenarios; according to a strong model (such as GPT - 4, Gemini, or Claude), process the natural language query task texts and the enterprise scenario data tables to generate high - quality first SQL query task statements, reducing syntax errors and logical deviations, and improving the accuracy when using the metadata vector set for query generation subsequently.

[0065] S16: Merge the table structure information, table description data, and the first SQL query task statements corresponding to each enterprise scenario data table to obtain metadata;

[0066] In specific implementation, the metadata can be represented as follows:

[0067] M(T i ) = {DDL(T i ), des(T i ), SQL(T i , A j ) ∣ A j ∈ A}; where M(T i ) represents the metadata corresponding to the i - th enterprise scenario data table T i , DDL(T i ) represents the table structure information corresponding to the enterprise scenario data table T i , SQL(T i , A j ) represents the first SQL query task statement corresponding to the j - th general analysis type A j in the enterprise scenario data table T i .

[0068] S17: Process each of the metadata according to a preset vector model to obtain a set of metadata vectors corresponding to each of the metadata, where the set of metadata vectors is DB vec Each vector in is represented as follows:

[0069] V(T i ) = {E(DDL(T i ), E(des(T i ), E(SQL(T i , A j ))}; where V(T i ) represents the metadata vector corresponding to the enterprise scenario data table T i , and E() represents vectorization.

[0070] In this preferred embodiment, S16 - S17 achieve the conversion from text to numerical values by integrating various data and performing vectorization processing, facilitating subsequent calculation of similarity and execution of efficient retrieval operations, and accelerating the subsequent query response speed.

[0071] Furthermore, step S2 can be implemented through the following preferred implementation method, including steps S21 - S23, specifically as follows:

[0072] S21: Process the user's natural language query text according to the vector model to obtain a query embedding vector, represented as follows: V(Q u ) = E(Q u ); where V(Q u ) represents the query embedding vector corresponding to the user's natural language query text Q u .

[0073] S22: Calculate the similarity values between each metadata vector in the set of metadata vectors and the query embedding vector respectively, and select the metadata corresponding to the metadata vector with the maximum similarity value as the first metadata;

[0074] In specific implementation, the algorithm for calculating and selecting the similarity value is represented as follows:

[0075] where M * (Y) represents the first metadata, V(M(Y)) represents the metadata vector in the set of metadata vectors DB vec , and Sim() represents calculating the vector similarity metric (such as cosine similarity).

[0076] S23: Construct a prompt word according to the user's natural language query text and the first metadata; where the prompt word P can be represented as: P = {Q u , M* (Y)}。

[0077] In this preferred embodiment, steps S21 - S23 perform question embedding processing to convert the user's query into a form that is easy for a computer to process, use the similarity value to accurately match the metadata vector and generate prompt words, improving the query response speed and accuracy in practical applications and enhancing the user experience.

[0078] Step S3: Process the prompt words according to a preset NL2SQL model to obtain the SQL query statement corresponding to the user's natural language query text, thus completing the query.

[0079] In specific implementation, the processing of the prompt words can be expressed as: SQL gen = M ChatBi (P); where SQL gen represents the output result, and M chatBi represents the NL2SQL model.

[0080] Among them, the NL2SQL model is obtained by fine-tuning a preset weak model according to the question-and-answer training data, and the weak model is any one of the following models: Qwen model, GLM4 model, or Llama model.

[0081] Preferably, the question-and-answer training data is generated by processing the enterprise scenario data table and the industry experience data set according to the strong model.

[0082] Furthermore, in specific implementation, the question-and-answer training data and the NL2SQL model can be obtained through the following preferred implementation method, including steps S31 - S35, as follows:

[0083] S31: Summarize each natural language query task text and the corresponding first SQL query task statement to obtain the question-and-answer training data.

[0084] In specific implementation, each natural language query task text and the corresponding first SQL query task statement are obtained in the same way as in the above steps S13 - S15, or can directly construct the question-and-answer training data during the process of generating the metadata vector set, that is, when implementing steps S13 - S15, reducing duplicate work. The summary of obtaining the question-and-answer training data is as follows:

[0085] Among them, D gen represents the question-and-answer training data, U represents the union, T i represents the i-th enterprise scenario data table, T represents all enterprise scenario data tables, A irepresents the i-th general analysis type, and A represents all general analysis types, including numerical calculation, trend analysis, query details, TOPN query, comparative analysis, year-on-year and month-on-month analysis, etc. SampleGen(T i , A i ) represents the combination processing of the enterprise scenario data table T i and the general analysis type A i to output the corresponding natural language query task text and the corresponding first SQL query task statement.

[0086] S32: Fine-tune the weak model according to the Q&A training data and a preset supervised fine-tuning algorithm to obtain a fine-tuned model.

[0087] In specific implementation, the supervised fine-tuning algorithm (SFT) is used for fine-tuning, which is expressed as follows:

[0088] M fine-tuned = SFT(M w , D gen ); where M fine-tuned represents the fine-tuned model, and M w represents the weak model.

[0089] In this preferred embodiment, steps S31 - S32 use the first SQL query task statement generated by the strong model, the natural language query task text, and the enterprise scenario data table, as well as the natural language query task text, to obtain high-quality Q&A training data, and use this training data to initially fine-tune the weak model, which not only enhances the customization ability and adaptation ability of the model, but also improves the model optimization efficiency and quickly adapts to new data and query requirements.

[0090] S33: Process each natural language query task text and the enterprise scenario data table according to the fine-tuned model to obtain a second SQL query task statement;

[0091] S34: Aggregate each natural language query task text and the corresponding first SQL query task statement and second SQL query task statement to obtain triple training data;

[0092] In specific implementation, the triple data can be expressed as:

[0093] D align = {(q, s q , r q ) ∣ q ∈ Q, s q = M fine-tuned (q), r q = M s (q)}; where q represents the natural language query task text, Q represents all natural language query task texts, sq Denote the second SQL query task statement obtained by the fine-tuning model processing q, r q Denote the first SQL query task statement obtained by the strong model processing q, D align Denote the triple data.

[0094] S35: Fine-tune the fine-tuning model according to each of the triple training data and a preset direct preference optimization algorithm to obtain an NL2SQL model.

[0095] In specific implementation, fine-tuning the fine-tuning model can be expressed as:

[0096] M chatBi = DPO(M fine-tuned , D align ); where M chatBi Denote the NL2SQL model, and DPO() denotes the direct preference optimization algorithm.

[0097] In this preferred embodiment, the processes of steps S33 - S35 using the fine-tuning model to regenerate the SQL query statement verify the effect of model improvement and also provide a basis for subsequent optimization; constructing a triple training data set using the first SQL query statement, the second SQL query statement, and the natural language query task text further enriches the training data and helps capture more patterns and relationships during subsequent training; using the direct preference optimization algorithm to further fine-tune the fine-tuning model, this algorithm obtains a better query generation strategy through contrastive learning, improves the performance and preference accuracy of the model in practical applications, and ensures that the generated SQL query statement better meets the expectations and requirements of users.

[0098] In particular, as shown in the comparative experiment effect diagram Figure 4 , the model trained by the present application based on data such as a general analysis type set, in the fine-tuning stage, compared with the traditional fine-tuning method, achieves significant performance improvement in all Few-shot scenarios (3-shot, 5-shot, 7-shot, and 9-shot). Taking the ChatGLM-9B model as an example, the 5-shot accuracy rate is increased from 75.6% to 80.1%, with an increase of 4.5%. The accuracy rate of 9-shot is even increased from 77.2% to 83.1%, with an increase of 5.9%, reflecting the effectiveness of the fine-tuning method based on the general data analysis type set data of the present application, indicating that the solution of the present application can effectively improve the learning ability and generalization ability of the model for small sample tasks, and is particularly suitable for the diverse data analysis needs of enterprises.

[0099] In addition, as shown in Figure 5The comparison experiment effect diagram shown. The model of the present application trained based on data such as a general analysis type set. In the application stage, compared with the traditional Retrieval-Augmented Generation (RAG) method, taking the LLaMA-7B model as an example, the 3-shot accuracy rate is increased from 69.5% to 72.3%, with an increase of 2.8%; the 9-shot accuracy rate is increased from 72.0% to 76.9%, with an increase of 4.9%. The model of the present application combines business context and historical query task records of database tables, dynamically adjusts the input content Prompt, improves the adaptability of the model to unseen questions, and further verifies its reliability in processing complex tasks in enterprise scenarios.

[0100] In summary, compared with the prior art, the above embodiments of the present application have the following beneficial effects: generating a metadata vector set based on enterprise scenario data tables and industry experience data sets can make full use of existing experience and data, enhance the understanding ability of specific enterprise environments, and reduce the need for new data annotation; by using this metadata vector set, enterprise data table information and query patterns related to the user's input text can be quickly located, thereby improving the speed and accuracy of query generation; at the same time, the metadata vector set is generated based on a strong model with powerful natural language understanding and generation capabilities, ensuring that the vector set can provide high-quality and highly relevant matching results; using the question-and-answer training data generated based on enterprise scenario data tables and industry experience data sets to fine-tune the weak model to obtain the NL2SQL model can make the NL2SQL model more suitable for actual application scenarios, improve the accuracy and applicability of SQL query generation while maintaining high efficiency, and provide customized services for specific enterprise needs.

[0101] Embodiment 2:

[0102] Please refer to Figure 2 , based on the same inventive concept, a query system for a lightweight enterprise NL2SQL model disclosed in an embodiment of the present invention includes: a query text acquisition module M1, a prompt word generation module M2, and a query module M3:

[0103] Among them, the query text acquisition module M1 is used to acquire the user's natural language query text.

[0104] The prompt word generation module M2 is used to match the user's natural language query text with a preset metadata vector set to obtain a prompt word.

[0105] Preferably, in specific implementation, to improve the accuracy of matching, this embodiment further includes a metadata vector set generation module M21;

[0106] The metadata vector set generation module M21 is used to process the enterprise scenario data table and the industry experience data set according to a preset strong model to generate a metadata vector set; the strong model is any one of the following models: GPT-4 model, Gemini model or Claude model.

[0107] Further, the metadata vector set generation module M21 includes: a table structure extraction unit and a table description extraction unit.

[0108] Among them, the table structure extraction unit is used to extract the table structure information of each enterprise scenario data table.

[0109] The table description extraction unit is used to extract each measurable field, corresponding data type information and quantity information that meet the preset measurable rules in each enterprise scenario data table, and construct table description data corresponding to each enterprise scenario data table.

[0110] In this preferred embodiment, the metadata vector set generation module M21 enhances the understanding of the data table by extracting the table structure information of each enterprise scenario data table, providing important context information for subsequent query generation; extracting fields and data type information that meet the preset measurable rules and constructing detailed table description data provide richer data for subsequent metadata construction.

[0111] Further, the metadata vector set generation module M21 further includes: a general analysis type extraction unit, a task text extraction unit and a first statement generation unit.

[0112] Among them, the general analysis type extraction unit is used to extract and summarize each data analysis type in the industry experience data set to obtain a general analysis type set.

[0113] The task text extraction unit is used to extract corresponding natural language query task texts in each enterprise scenario data table according to each general analysis type in the general analysis type set;

[0114] The first statement generation unit is used to process each natural language query task text and the enterprise scenario data table according to the strong model to obtain a first SQL query task statement.

[0115] In this preferred embodiment, the metadata vector set generation module M21 extracts common analysis types from industry data and, based on the extracted common analysis types, generates corresponding natural language query task texts in each enterprise scenario data table, ensuring that the subsequent constructed metadata vector set contains various common data analysis tasks and query patterns, while enhancing the understanding ability of specific enterprise scenarios; according to a strong model (such as GPT-4, Gemini, or Claude), processes the natural language query task texts and enterprise scenario data tables to generate high-quality first SQL query task statements, reducing syntax errors and logical deviations, and improving the accuracy when using the metadata vector set for query generation subsequently.

[0116] Furthermore, the metadata vector set generation module M21 further includes: a merging unit and a vectorization unit.

[0117] Among them, the merging unit is used to merge the table structure information, table description data, and first SQL query task statements corresponding to each enterprise scenario data table to obtain metadata;

[0118] The vectorization unit is used to process each piece of metadata according to a preset vector model to obtain a metadata vector set corresponding to each piece of metadata.

[0119] In this preferred embodiment, the metadata vector set generation module M21 realizes the conversion from text to numerical values through integrating multiple data and performing vectorization processing, facilitating subsequent calculation of similarity and execution of efficient retrieval operations, and accelerating the subsequent query response speed.

[0120] Preferably, the prompt generation module M2 includes: an embedding generation unit, a screening unit, and a construction unit.

[0121] Among them, the embedding generation unit is used to process the user natural language query text according to the vector model to obtain a query embedding vector;

[0122] The screening unit is used to calculate the similarity values between each metadata vector in the metadata vector set and the query embedding vector respectively, and screen the metadata corresponding to the metadata vector with the maximum similarity value as the first metadata;

[0123] The construction unit is used to construct a prompt according to the user natural language query text and the first metadata.

[0124] In this preferred embodiment, the prompt generation module M2 converts the user's query into a form that is easy for the computer to process through query embedding processing, accurately matches the metadata vector and generates a prompt using the similarity value, improving the query response speed and accuracy in practical applications and enhancing the user experience.

[0125] The query module M3 is used to process the prompt according to a preset NL2SQL model, obtain an SQL query statement corresponding to the user's natural language query text, and complete the query.

[0126] Specifically, in specific implementation, to improve the accuracy of the query, this embodiment may further include a model training module M4.

[0127] The model training module M4 is used to fine-tune a preset weak model according to the question-and-answer training data to obtain the NL2SQL model; wherein, the weak model is any one of the following models: Qwen model, GLM4 model or Llama model; the question-and-answer training data is generated by processing the enterprise scenario data table and the industry experience data set according to the strong model.

[0128] Specifically, the model training module M4 includes: a question-and-answer training data generation unit and a fine-tuned model generation unit.

[0129] Among them, the question-and-answer training data generation unit is used to summarize each natural language query task text and the corresponding first SQL query task statement to obtain the question-and-answer training data.

[0130] Specifically, in the question-and-answer training data generation unit, each natural language query task text and the corresponding first SQL query task statement can be obtained through the implementation methods of the general analysis type extraction unit, the task text extraction unit and the first statement generation unit of the above metadata vector set generation module M21, which will not be elaborated here.

[0131] The fine-tuned model generation unit is used to fine-tune the weak model according to the question-and-answer training data and a preset supervised fine-tuning algorithm to obtain a fine-tuned model.

[0132] In this preferred embodiment, the model training module M4 uses the first SQL query task statements generated by the strong model, the natural language query task text and the enterprise scenario data table, and the natural language query task text to obtain high-quality question-and-answer training data, and uses this training data to initially fine-tune the weak model, which not only enhances the customization ability and adaptation ability of the model, but also improves the model optimization efficiency and quickly adapts to new data and query requirements.

[0133] Furthermore, the model training module M4 further includes: a second statement generation unit, a triple acquisition unit and an NL2SQL model generation unit.

[0134] Among them, the second statement generation unit is used to process each natural language query task text and the enterprise scenario data table according to the fine-tuned model to obtain a second SQL query task statement;

[0135] The triple acquisition unit is used to summarize each natural language query task text and the corresponding first SQL query task statement and second SQL query task statement to obtain triple training data;

[0136] The NL2SQL model generation unit is used to fine-tune the fine-tuned model according to each triple training data and a preset direct preference optimization algorithm to obtain an NL2SQL model.

[0137] In this preferred embodiment, the process of the model training module M4 using the fine-tuned model to generate SQL query statements again verifies the effect of model improvement and also provides a basis for subsequent optimization; constructing a triple training data set using the first SQL query statement, the second SQL query statement, and the natural language query task text further enriches the training data and helps capture more patterns and relationships during subsequent training; using the direct preference optimization algorithm to further fine-tune the fine-tuned model, this algorithm obtains a better query generation strategy through contrastive learning, improving the performance and preference accuracy of the model in practical applications and ensuring that the generated SQL query statements better meet the expectations and requirements of users.

[0138] Compared with the prior art, the above embodiments of the present application have the following beneficial effects: generating a metadata vector set based on enterprise scenario data tables and industry experience data sets can make full use of existing experience and data, enhance the understanding ability of a specific enterprise environment, and reduce the need for new data annotation; by using this metadata vector set, it is possible to quickly locate enterprise data table information and query patterns related to the user's input text, thereby improving the speed and accuracy of query generation; at the same time, the metadata vector set is generated based on a strong model with powerful natural language understanding and generation capabilities, ensuring that the vector set can provide high-quality and highly relevant matching results; using the question-and-answer training data generated based on enterprise scenario data tables and industry experience data sets to fine-tune the weak model to obtain an NL2SQL model can make the NL2SQL model more suitable for actual application scenarios, improving the accuracy and applicability of SQL query generation while maintaining high efficiency, and providing customized services for specific enterprise needs.

[0139] Embodiment 3:

[0140] Figure 3 The structure diagram of a query device for a lightweight enterprise NL2SQL model of the present application is shown. As Figure 3 shown, the query device for the lightweight enterprise NL2SQL model may include: a processor N1, a memory N2, a data interface N3, and a communication bus N4.

[0141] Wherein: a processor N1, a memory N2, and a data interface N3 complete mutual communication through a communication bus N4; the data interface N3 is used for data communication with other devices such as an input device or an output device; the processor N1 is configured to execute a program N5, and specifically can execute the relevant steps in the above-mentioned embodiments of the query method of a lightweight enterprise NL2SQL model.

[0142] Specifically, the program N5 may include program code, and the program code includes computer-executable instructions.

[0143] The processor N1 may be a central processing unit CPU, or a specific integrated circuit ASIC (Application Specific Integrated Circuit), or one or more integrated circuits configured to implement the embodiments of the present application. One or more processors included in the query device of the lightweight enterprise NL2SQL model may be of the same type of processor, such as one or more CPUs, or may be of different types of processors, such as one or more CPUs and one or more ASICs.

[0144] The memory N2 is used to store the program N5. The memory N2 may include a high-speed RAM memory, and may also include a non-volatile memory, such as at least one disk memory.

[0145] The algorithms or displays provided herein are not inherently related to any particular computer, virtual system, or other device. In addition, the embodiments of the present application are not directed to any specific programming language.

[0146] Embodiment 4:

[0147] The embodiments of the present invention also provide a computer-readable storage medium. The storage medium stores at least one executable instruction. When the executable instruction runs on the query device / system of the lightweight enterprise NL2SQL model, the query device / system of the lightweight enterprise NL2SQL model is enabled to execute a query method of a lightweight enterprise NL2SQL model in any of the above method embodiments.

[0148] In the specification provided herein, a large number of specific details are set forth. However, it can be understood that the embodiments of the present application can be practiced without these specific details. Similarly, in order to streamline the present application and assist in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present application, the various features of the embodiments of the present application are sometimes grouped together into a single embodiment, figure, or description thereof. Among them, the claims following the specific implementation manners are hereby expressly incorporated into the specific implementation manners, and each claim itself is used as a separate embodiment of the present application.

[0149] Those skilled in the art can understand that the modules in the devices in the embodiments can be adaptively changed and arranged in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition, they can be divided into multiple sub-modules or sub-units or sub-components. Except that at least some of such features and / or processes or units are mutually exclusive.

Claims

1. A query method for a lightweight enterprise NL2SQL model, characterized in that: include: Obtain user natural language query text; Matching the user's natural language query text with a preset metadata vector set to obtain a prompt word; wherein the metadata vector set is generated by processing an enterprise scenario data table and an industry experience data set according to a preset strong model; the strong model is any one of the following models: a GPT-4 model, a Gemini model, or a Claude model; According to the preset NL2SQL model, the prompt word is processed to obtain the SQL query statement corresponding to the user's natural language query text, and the query is completed; wherein the NL2SQL model is obtained by fine-tuning the preset weak model according to the question-and-answer training data; wherein the question-and-answer training data is generated by processing the enterprise scenario data table and the industry experience data set according to the strong model, and the weak model is any one of the following models: Qwen model, GLM4 model or Llama model.

2. A query method for a lightweight enterprise NL2SQL model as claimed in claim 1, characterized in that: The metadata vector set is generated by processing the enterprise scenario data table and the industry experience data set according to the preset strong model, including: Extracting table structure information of each enterprise scenario data table; Extract each measurable field that meets the preset measurable rules and the corresponding data type information and quantity information from each of the enterprise scenario data tables, and construct table description data corresponding to each of the enterprise scenario data tables.

3. A query method for a lightweight enterprise NL2SQL model as claimed in claim 2, characterized in that: The metadata vector set is generated by processing the enterprise scenario data table and the industry experience data set according to the preset strong model, and also includes: Extracting and summarizing various data analysis types in the industry experience data set to obtain a set of general analysis types; According to each general analysis type in the general analysis type set, extracting a corresponding natural language query task text from each enterprise scenario data table; According to the strong model, each of the natural language query task texts and the enterprise scenario data table is processed to obtain a first SQL query task statement.

4. A query method for a lightweight enterprise NL2SQL model as claimed in claim 3, characterized in that: The metadata vector set is generated by processing the enterprise scenario data table and the industry experience data set according to the preset strong model, and also includes: Merging the table structure information, table description data and the first SQL query task statement corresponding to each of the enterprise scenario data tables to obtain metadata; According to a preset vector model, each metadata is processed to obtain a metadata vector set corresponding to each metadata.

5. A query method for a lightweight enterprise NL2SQL model as claimed in claim 4, characterized in that: The step of matching the user's natural language query text with a preset metadata vector set to obtain a prompt word includes: According to the vector model, the user natural language query text is processed to obtain a question embedding vector; respectively calculating similarity values ​​between each metadata vector in the metadata vector set and the question embedding vector, and selecting metadata corresponding to the metadata vector corresponding to the maximum value of the similarity value as the first metadata; A prompt word is constructed according to the user natural language query text and the first metadata.

6. A query method for a lightweight enterprise NL2SQL model as claimed in claim 5, characterized in that: The NL2SQL model is obtained by fine-tuning a preset weak model based on question-answering training data, including: Summarize the natural language query task texts and the corresponding first SQL query task statements to obtain question-answering training data; According to the question-answering training data and a preset supervised fine-tuning algorithm, the weak model is fine-tuned to obtain a fine-tuned model.

7. A query method for a lightweight enterprise NL2SQL model as claimed in claim 6, characterized in that: The NL2SQL model is obtained by fine-tuning a preset weak model based on question-answering training data, and also includes: According to the fine-tuning model, the natural language query task texts and the enterprise scenario data table are processed to obtain a second SQL query task statement; Summarizing the natural language query task texts and the corresponding first SQL query task statements and second SQL query task statements to obtain triple training data; According to each of the triple training data and a preset direct preference optimization algorithm, the fine-tuning model is fine-tuned to obtain a NL2SQL model.

8. A lightweight enterprise NL2SQL model query system, characterized in that: include: Query text acquisition module, prompt word generation module and query module: Wherein, the query text acquisition module is used to acquire the user's natural language query text; The prompt word generation module is used to match the user natural language query text with a preset metadata vector set to obtain a prompt word; wherein the metadata vector set is generated by processing the enterprise scenario data table and the industry experience data set according to a preset strong model; the strong model is any one of the following models: GPT-4 model, Gemini model or Claude model; The query module is used to process the prompt word according to a preset NL2SQL model, obtain the SQL query statement corresponding to the user's natural language query text, and complete the query; wherein the NL2SQL model is obtained by fine-tuning a preset weak model based on question-and-answer training data; wherein the question-and-answer training data is generated by processing the enterprise scenario data table and the industry experience data set according to the strong model, and the weak model is any one of the following models: Qwen model, GLM4 model or Llama model.

9. A query device for a lightweight enterprise NL2SQL model, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the computer program is loaded into the processor, the steps of the query method of the lightweight enterprise NL2SQL model according to any one of claims 1 to 7 are implemented.

10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of a query method for a lightweight enterprise NL2SQL model according to any one of claims 1 to 7 are implemented.