Method and system for building a language conversion assistant based on generative model
By preprocessing the meta-information of the initial data table and generating a training sample set through a generative model, the problem of unclear table and column descriptions and missing context in the NL2SQL method is solved, and efficient and accurate natural language to structured query conversion is achieved. Improved data retrieval efficiency and accuracy.
Patent Information
- Application Number
- CN202410749675.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-12
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-06-12
AI Technical Summary
The traditional NL2SQL method has unclear descriptions of tables and columns, and lack of context and metadata, resulting in inaccurate SQL retrieval statements and slow response speeds, making it difficult to quickly extract data information and affecting decision-making efficiency.
By preprocessing the meta-information of the initial data table, a generative model is used to generate a training sample set of structured language retrieval statements containing table information, current date, and natural language query questions, and the language conversion assistant is trained to convert the natural language query questions into Structured language search statements.
It improves the quality and efficiency of generated training sample sets, allows users without structured query language knowledge to perform complex data retrieval, and improves retrieval efficiency and accuracy.
Smart Images

Figure CN118332000B
Abstract
Description
Technical Field
[0001] The present application relates to the field of enterprise-level data warehouse technology, and in particular to a method and system, electronic device, and storage medium for building a language conversion assistant based on a generative model. Background Art
[0002] With the continuous development of big data technology, enterprise-level data warehouses have accumulated a large amount of data resources. In order to quickly extract information from data and improve decision-making efficiency, NL2SQL (Natural Language to Structured Query Language) technology has become a key technology. However, the traditional NL2SQL method faces many challenges. For example, the lack of a clear description of tables and columns leads to inaccurate generated SQL search statements, the lack of context and metadata makes it difficult to understand the problem, and there are problems such as response speed. Therefore, these technical problems need to be solved urgently. Summary of the invention
[0003] In view of the above problems, the present application is proposed to provide a method and system, electronic device and storage medium for building a language conversion assistant based on a generative model that overcomes the above problems or at least partially solves the above problems. The technical solution is as follows:
[0004] In a first aspect, a method for building a language conversion assistant based on a generative model is provided, comprising:
[0005] Preprocessing the meta information of each of the at least one initial data table to obtain at least one preprocessed data table;
[0006] Using a generative model, in combination with the meta information of the at least one preprocessed data table and the current date, a training sample set including table information, the current date, a natural language query question, and a structured language retrieval statement corresponding to the natural language query question is generated;
[0007] The training sample set is used to train the model to be trained, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
[0008] In a possible implementation, the meta information includes one or more of a table name, a table description, a field name, a field alias, a field description, a dimension column, an indicator column, whether it is a cube table, a drill-down column, a drill-down column default value or a completion value; the table information includes a table name and / or a table description.
[0009] In a possible implementation, the generative model is used to combine the meta information of the at least one preprocessed data table and the current date to generate a training sample set including table information, the current date, a natural language query question, and a structured language retrieval statement corresponding to the natural language query question, including:
[0010] According to the meta information of the at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as first prompt information;
[0011] Designing second prompt information according to the training sample set to be generated;
[0012] The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model;
[0013] The target prompt project content is provided to the generative model to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
[0014] In a possible implementation, according to the meta information of the at least one preprocessed data table and the current date, after taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as the first prompt information, the method further includes:
[0015] Adjust one or more of the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the first prompt information to obtain adjusted first prompt information;
[0016] The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model, including:
[0017] The adjusted first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model.
[0018] In a possible implementation, the second prompt information includes prompt content for generating table information, current date, natural language query question, and structured language search sentence corresponding to the natural language query question in a preset format.
[0019] In a possible implementation, the training sample set is used to train a to-be-trained model, and a language conversion assistant is constructed to convert natural language query questions into structured language search statements, including:
[0020] Using the sample pairs of natural language query questions and table information in the training sample set, training the initial table recall model in the model to be trained to obtain a trained table recall model;
[0021] Using the sample pairs of natural language query questions, table information, and structured language retrieval sentences corresponding to the natural language query questions in the training sample set, the initial table field recall model in the training model is trained to obtain a trained table field recall model;
[0022] Using the table information, current date, natural language query question, and sample pairs of structured language retrieval sentences corresponding to the natural language query question in the training sample set, an initial language conversion model in the training model is trained to obtain a trained language conversion model;
[0023] Combining the trained table recall model, the trained table field recall model and the trained language conversion model, a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
[0024] In a possible implementation, before using the training sample set to train the to-be-trained model and constructing a language conversion assistant for converting natural language query questions into structured language search statements, the method further includes:
[0025] Performing optimization processing and / or enhancement processing on the training sample set to obtain a processed training sample set;
[0026] The training sample set is used to train the model to be trained, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements, including:
[0027] The processed training sample set is used to train the to-be-trained model, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
[0028] In a possible implementation, after constructing a language conversion assistant for converting a natural language query question into a structured language search statement, the method further includes:
[0029] Receiving a target natural language query question from a user;
[0030] The target natural language query question is converted into a target structured language search statement using the language conversion assistant, and the statement is output.
[0031] In a second aspect, a system for building a language conversion assistant based on a generative model is provided, including:
[0032] A data preprocessing unit, used for preprocessing the meta information of each initial data table in at least one initial data table to obtain at least one preprocessed data table;
[0033] A sample generating unit, configured to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions by using a generative model and combining the meta information of the at least one preprocessed data table and the current date;
[0034] A construction unit is used to train a to-be-trained model using the training sample set, and to construct a language conversion assistant for converting natural language query questions into structured language retrieval statements.
[0035] In a possible implementation, the meta information includes one or more of a table name, a table description, a field name, a field alias, a field description, a dimension column, an indicator column, whether it is a cube table, a drill-down column, a drill-down column default value or a completion value; the table information includes a table name and / or a table description.
[0036] In a possible implementation manner, the sample generating unit is further configured to:
[0037] According to the meta information of the at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as first prompt information;
[0038] Designing second prompt information according to the training sample set to be generated;
[0039] The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model;
[0040] The target prompt project content is provided to the generative model to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
[0041] In a possible implementation manner, the sample generating unit is further configured to:
[0042] Adjust one or more of the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the first prompt information to obtain adjusted first prompt information;
[0043] The adjusted first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model.
[0044] In a possible implementation, the second prompt information includes prompt content for generating table information, current date, natural language query question, and structured language search sentence corresponding to the natural language query question in a preset format.
[0045] In a possible implementation, the construction unit is further used to:
[0046] Using the sample pairs of natural language query questions and table information in the training sample set, training the initial table recall model in the model to be trained to obtain a trained table recall model;
[0047] Using the sample pairs of natural language query questions, table information, and structured language retrieval sentences corresponding to the natural language query questions in the training sample set, the initial table field recall model in the training model is trained to obtain a trained table field recall model;
[0048] Using the table information, current date, natural language query question, and sample pairs of structured language retrieval sentences corresponding to the natural language query question in the training sample set, an initial language conversion model in the training model is trained to obtain a trained language conversion model;
[0049] Combining the trained table recall model, the trained table field recall model and the trained language conversion model, a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
[0050] In a possible implementation, the system further includes a sample processing unit, configured to:
[0051] Before the construction unit uses the training sample set to train the to-be-trained model and constructs a language conversion assistant that converts natural language query questions into structured language retrieval statements, the training sample set is optimized and / or enhanced to obtain a processed training sample set;
[0052] The construction unit is also used to: use the processed training sample set to train the to-be-trained model, and construct a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0053] In a possible implementation, the system further includes an online reasoning unit, configured to:
[0054] Receiving a target natural language query question from a user;
[0055] The target natural language query question is converted into a target structured language search statement using the language conversion assistant, and the statement is output.
[0056] According to a third aspect, an electronic device is provided, comprising a processor and a memory, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute any of the above-described methods for building a language conversion assistant based on a generative model.
[0057] In a fourth aspect, a storage medium is provided, wherein the storage medium stores a computer program, wherein the computer program is configured to execute any of the above-mentioned methods for building a language conversion assistant based on a generative model when running.
[0058] By means of the above technical scheme, the embodiment of the present application provides a method and system for constructing a language conversion assistant based on a generative model, an electronic device and a storage medium. The method can preprocess the meta-information of each initial data table in at least one initial data table to obtain at least one preprocessed data table; utilize the generative model, combine the meta-information of at least one preprocessed data table and the current date, and generate a training sample set including table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions; use the training sample set to train the model to be trained, and construct a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0059] It can be seen that the present embodiment first preprocesses the meta information of each initial data table. This process ensures that the subsequent generative model can accurately understand the structure and semantics of the data table, thereby improving the quality of the generated training sample set; and the present embodiment uses the generative model, combined with the meta information of at least one preprocessed data table and the current date, to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions. The training sample set can be constructed efficiently and accurately without manually writing or constructing the training sample set, thereby improving the generation efficiency of the training sample set; in addition, the present embodiment uses the training sample set to train the training model and constructs a language conversion assistant that converts natural language query questions into structured language retrieval statements. The subsequent language conversion assistant can help users convert natural language query questions into effective structured language retrieval statements for data retrieval and query operations, allowing users without structured query language knowledge to perform complex data retrieval, thereby improving retrieval efficiency and accuracy. BRIEF DESCRIPTION OF THE DRAWINGS
[0060] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in describing the embodiments of the present application are briefly introduced below.
[0061] Figure 1 A flowchart of a method for constructing a language conversion assistant based on a generative model provided in an embodiment of the present application is shown;
[0062] Figure 2 A structural diagram of a system for building a language conversion assistant based on a generative model provided in an embodiment of the present application is shown;
[0063] Figure 3 A structural diagram of a system for building a language conversion assistant based on a generative model provided by another embodiment of the present application is shown;
[0064] Figure 4 A structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0065] The exemplary embodiments of the present application will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present application are shown in the accompanying drawings, it should be understood that the present application can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided in order to enable a more thorough understanding of the present application and to fully convey the scope of the present application to those skilled in the art.
[0066] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that such use is interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the term "including" and its variants are to be interpreted as open-ended terms meaning "including but not limited to".
[0067] Before introducing the embodiments of the present application, the following technical terms are first introduced in detail.
[0068] A generative model is a model or large model that has the capabilities of natural language processing, image processing, or speech processing, and the ability to generate content such as text, images, audio, and video. A generative model that combines pre-training methods with deep learning is called a GPT (Generative Pre-trained Transformer) model.
[0069] The large model, also known as the Large Language Model (LLM), is, first of all, large in scale, as the name suggests, with network parameters reaching tens of billions, hundreds of billions or even more; second, universal, meaning it is not limited to specific problems or fields; third, emergent, that is, it generates unexpected new capabilities.
[0070] A prompt is a piece of text or question provided to a generative model to guide the generative model to generate a specific type of text or answer. A prompt can be a sentence, a question, an article, or a topic, etc. It can guide the generative model to generate information or answers related to the prompt. For example, when given the question "What is artificial intelligence?", the generative model can generate an article or answer about artificial intelligence.
[0071] A natural language query is a question or request for information from a computer system using a natural language (such as Chinese, English, etc.) expressed orally or in written form. This form of query is usually constructed using the grammatical and semantic structure of a natural language, allowing users to communicate and interact directly with the computer in their own language.
[0072] The present application embodiment provides a method for constructing a language conversion assistant based on a generative model, such as Figure 1 As shown, the method for constructing a language conversion assistant based on a generative model may include the following steps S101 to S103:
[0073] Step S101 , preprocessing the meta information of each initial data table in at least one initial data table to obtain at least one preprocessed data table.
[0074] In this step, the meta information may include one or more of the table name, table description, field name, field alias, field description, dimension column, indicator column, whether it is a cube table, drill-down column, drill-down column default value or completion value, and this embodiment does not limit this.
[0075] For example, the initial data table B1 is named "sales_data" and its description is "a data table containing sales records of each store"; the field name Z1 is "date", its alias is "sales date", and its description is "the date on which the transaction occurred"; the field name Z2 is "store_id", its alias is "store number", and its description is "the unique identification code of the store"; the field name Z3 is "sales_amount", its alias is "sales revenue", and its description is "the total amount of sales", and so on.
[0076] Dimension columns can be columns that can be grouped, such as "date" and "store_id"; indicator columns are numeric columns that can be summed or calculated, such as "sales_amount".
[0077] The initial data table may be designed as a data cube. In this case, it is necessary to indicate whether the initial data table is a data cube, that is, whether it is a cube table, so that the model knows that this table can be used for multidimensional analysis.
[0078] If the initial data table supports drill-down, you need to specify the fields that can be drilled down and the default values. For example, if you drill down from annual sales to quarterly sales, assuming that the user wants to analyze the total sales in 2022, the user initially views the summary data of all sales, that is, total sales; using the drill-down function, the user can view the quarterly data that makes up the annual sales, that is, first quarter sales, second quarter sales, third quarter sales, and fourth quarter sales.
[0079] If the user wants to know the sales situation in the second quarter in more detail, he can continue to use the drill-down function to view the sales of each month in the second quarter, that is, the sales in April, May, and June.
[0080] The default value can be the default value when the field has no value; the completion value can be the value that needs to be filled in for possible empty values in the data to ensure the integrity of the data.
[0081] It should be noted that the table names, table descriptions, field names, field aliases, field descriptions, etc. listed above are only illustrative and do not limit this embodiment.
[0082] In addition, the metadata of each initial data table in at least one initial data table is preprocessed. The preprocessing here may include data cleaning, such as removing duplicate values, processing missing values, repairing erroneous data, etc.; it may include data standardization, such as converting data units, unifying data formats, etc. For example, if the initial data table contains data in multiple currencies, currency unit conversion can be performed to convert all data into units of the same currency; it may also include data normalization, such as normalizing the data so that the data is in the same magnitude range, for example, normalizing numerical data and scaling it to between 0 and 1, etc. This embodiment does not limit this.
[0083] Step S102, using a generative model, combining the meta information of at least one preprocessed data table and the current date, to generate a training sample set including table information, the current date, a natural language query question, and a structured language retrieval statement corresponding to the natural language query question.
[0084] In this step, the table information may be a table name, a table description, or a table name and a table description, etc. This embodiment does not limit this.
[0085] Step S103: Use the training sample set to train the to-be-trained model, and construct a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0086] This embodiment first preprocesses the meta information of each initial data table. This process ensures that the subsequent generative model can accurately understand the structure and semantics of the data table, thereby improving the quality of the generated training sample set; and, this embodiment uses the generative model, combined with the meta information of at least one preprocessed data table and the current date, to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions. The training sample set can be constructed efficiently and accurately without manually writing or constructing the training sample set, thereby improving the generation efficiency of the training sample set; in addition, this embodiment uses the training sample set to train the training model and constructs a language conversion assistant that converts natural language query questions into structured language retrieval statements. The subsequent language conversion assistant can help users convert natural language query questions into effective structured language retrieval statements for data retrieval and query operations, allowing users without structured query language knowledge to perform complex data retrieval, thereby improving retrieval efficiency and accuracy.
[0087] In the embodiment of the present application, a possible implementation method is provided. In the above step S102, a generative model is used to combine the meta information of at least one preprocessed data table and the current date to generate a training sample set including table information, the current date, a natural language query question, and a structured language search statement corresponding to the natural language query question. Specifically, the following steps A1 to A4 may be included:
[0088] Step A1, according to the meta information of at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in at least one preprocessed data table as first prompt information;
[0089] Step A2, designing second prompt information according to the training sample set to be generated;
[0090] Step A3, fusing the first prompt information and the second prompt information to obtain target prompt engineering content that interacts with the generative model;
[0091] Step A4, providing the target prompt project content to the generative model, generating a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
[0092] This embodiment analyzes the meta information (such as table name, table description, field name, field alias, field description, etc.) of the preprocessed data table, and generates first prompt information in combination with the current date and the grammatical scope and date expression scope of the structured language (such as SQL), which can help the generative model understand the context of the data and how to generate queries for data in a specific format; by designing the second prompt information related to the training sample set to be generated, the purpose is to let the generative model understand the content and structure that the training sample should include, such as what type of query questions and structured retrieval statements it needs to generate, so as to direct the output of the generative model and ensure that the generated training sample set meets the training needs of the model to be trained; and, by fusing the first prompt information and the second prompt information, the target prompt engineering content that interacts with the generative model is obtained, so that the generative model can produce output that is both related to the data table and adapted to the training sample requirements, more accurately guide the generative model, and improve the quality of the training sample set output by the generative model.
[0093] Optionally, the target prompt engineering content for interacting with the generative model provided in the embodiment of the present application is as follows.
[0094] Given the following SQL table creation statement:
[0095] CREATE TABLE IF NOT EXISTS sales_data (
[0097] date 'sale date',
[0098] store_id'store number',
[0099] sales_amount'sales amount', )
[0101] Table Description: A data table containing sales records for each store.
[0102] Please follow the following requirements to generate the NL2SQL instruction set for GPT training:
[0103] The output instruction set format is as follows:
[0104] {"table":"Table information","date":"Current date","query":"User's natural language query command","sql":"Retrieval statement used for database query and chart generation"}.
[0105] The expressions of user questions should be as rich and varied as possible, avoiding repetition. They need to be specific queries, and no vague references should appear. User questions do not necessarily strictly follow field descriptions or enumeration values, and can be appropriately rewritten with synonyms. Try to simulate real user questions, and avoid punctuation marks such as "-" and "_".
[0106] If the SQL involves filtering conditions such as "=" and "in", and the current field is completely enumerated, the filtering conditions of the generated SQL must be within the provided enumeration value range. If no enumeration value is specified or the enumeration value is incomplete, you can use it freely.
[0107] The current date is 20230308. If the date value in the user's question is a relative time (today, yesterday, this month, last week, last quarter, etc.), please calculate based on the current date.
[0108] The SQL keywords allowed in the generated instruction set are enumerated as follows:
[0109] SELECT, FROM, WHERE, DISTINCT, IN, AND, OR, BETWEEN, LIMIT, GROUP BY, ORDER BY, AVG, COUNT, MAX, MIN, SUM. Do not use other SQL built-in functions, such as time functions, string functions, and mathematical functions.
[0110] Please include the keywords GROUP BY, ORDER BY, and BETWEEN as much as possible.
[0111] The questions must include the following time examples: the day before yesterday, yesterday, today, the past n days, this week, last week, the past n weeks this month, last month, the past n months, this quarter, last quarter, the xth quarter, etc.
[0112] When columns contain city names, etc., please try to include these fields in the question as much as possible and do not rename the columns.
[0113] ## Fields should be clearly indicated as much as possible instead of using *##.
[0114] Please do not rename columns.
[0115] ##Please generate ##10 sets of ##instructions##, one json per line, with keys such as table, date, query and sql. The json should be concise and marked with serial numbers. No line breaks are required, and no additional information needs to be output.
[0116] In the above target prompt engineering content that interacts with the generative model, CREATE TABLE IF NOT EXISTSsales_data means that if the data table "sales_data" does not exist, create the data table "sales_data"; "data table containing sales records of each store" is the table description; "20230308" is the current date; "SELECT, FROM, WHERE, DISTINCT, IN, AND, OR, BETWEEN, LIMIT, GROUP BY, ORDER BY, AVG, COUNT, MAX, MIN, SUM" are keywords used in SQL to interact with the database. These keywords can also be used in combination to build complex queries to retrieve, sort, group and calculate data in the database; "GROUP BY, ORDER BY, BETWEEN" is the structured language syntax range; "the day before yesterday, yesterday, today, nearly n days, this week, last week, nearly n weeks, this month, last month, nearly n month, this quarter, last quarter, xth quarter" is the date expression range.
[0117] "The output instruction set format is as follows: {"table":"table information","date":"current date","query":"user natural language query instruction","sql":"search statement for database query and chart generation}" is designed according to the training sample set to be generated; json is a lightweight data exchange format that is easy for people to read and write, and is also easy for machines to parse and generate. It can be seen that the second prompt information includes prompt content for generating table information, current date, natural language query questions, and structured language search statements corresponding to natural language query questions in a preset format.
[0118] It can be seen that the target prompt engineering content interacting with the generative model shown above integrates the first prompt information and the second prompt information, which can guide the generative model more accurately and improve the quality of the training sample set output by the generative model.
[0119] Optionally, the training sample set provided in the embodiment of the present application is as follows:
[0120] 1. {"table":"sales_data","date":"20230308","query":"What was the total sales of each store yesterday?","sql":"SELECT store_id,SUM(sales_amount) FROM sales_data WHEREdate ='20230307' GROUP BY store_id"};
[0121] 2. {"table":"sales_data","date":"20230308","query":"Please tell me the sales of store number 1001 the day before yesterday","sql":"SELECT sales_amount FROM sales_data WHERE date ='20230306' AND store_id ='1001'"};
[0122] 3. {"table":"sales_data","date":"20230308","query":"How is today's sales?","sql":"SELECT store_id,sales_amount FROM sales_data WHERE date ='20230308'"};
[0123] 4. {"table":"sales_data","date":"20230308","query":"What is the average sales of all stores this week?","sql":"SELECT AVG(sales_amount) FROM sales_data WHERE dateBETWEEN '20230306' AND '20230308'"};
[0124] 5. {"table":"sales_data","date":"20230308","query":"Which store had the highest sales last month?","sql":"SELECT store_id,MAX(sales_amount) FROM sales_data WHEREdate BETWEEN '20230201' AND '20230228' GROUP BY store_id ORDER BY MAX(sales_amount) DESC LIMIT 1"};
[0125] 6. {"table":"sales_data","date":"20230308","query":"What are the top three store numbers in terms of sales in the past 7 days?","sql":"SELECT store_id,SUM(sales_amount) FROM sales_dataWHERE date BETWEEN '20230301'AND '20230307' GROUP BY store_id ORDER BY SUM(sales_amount) DESC LIMIT 3"};
[0126] 7. {"table":"sales_data","date":"20230308","query":"What was the total sales of each store last quarter?","sql":"SELECT store_id,SUM(sales_amount) FROM sales_dataWHERE date BETWEEN '20221001' AND '20221231' GROUP BY store_id"};
[0127] 8. {"table":"sales_data","date":"20230308","query":"How do the total sales of stores numbered 1002 and 1003 compare this quarter?","sql:"SELECT store_id,SUM(sales_amount)FROM sales_data WHERE store_id IN ('1002','1003') AND date BETWEEN '20230101'AND '20230331' GROUP BY store_id"};
[0128] 9. {"table":"sales_data","date":"20230308","query":"Count the total sales of each day last week","sql:"SELECT date SUM(sales_amount) FROM sales_data WHERE date BETWEEN'20230227' AND '20230305' GROUP BY date ORDER BY date"};
[0129] 10. {'table":"sales_data","date":"20230308","query":"What are the sales statistics of each store so far this month?","sql":"SELECT store_id,SUM(sales_amount) FROM sales_dataWHERE date BETWEEN '20230301' AND '20230308' GROUP BY store_id"}.
[0130] The training sample set provided above illustrates that the above target prompt engineering content is provided to the generative model, and a training sample set including table information, current date, natural language query question, and structured language retrieval statement corresponding to the natural language query question is generated. The training sample set includes 10 training samples. Specifically, "table" is the table information, "date" is the current date, "query" is the natural language query question, and "sql" is the structured language retrieval statement corresponding to the natural language query question is generated.
[0131] It should be noted that the above examples of target prompting engineering content and training sample sets are only illustrative and do not limit this embodiment.
[0132] The present application provides a possible implementation method. In the above step A1, after the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in at least one preprocessed data table are used as the first prompt information according to the metadata of at least one preprocessed data table and the current date, the following step A11 may also be included:
[0133] Step A11, adjusting one or more of the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the first prompt information to obtain the adjusted first prompt information.
[0134] Thus, the above step A3 combines the first prompt information and the second prompt information to obtain the target prompt engineering content that interacts with the generative model, which may include the following steps A31:
[0135] Step A31, fusing the adjusted first prompt information and the second prompt information to obtain target prompt engineering content that interacts with the generative model.
[0136] In this embodiment, the first prompt information can be adjusted according to actual needs to obtain the adjusted first prompt information, for example, the table name and table description can be adjusted, the table name "sales_data" can be adjusted to "customers", and the table description "data table containing sales records of each store" can be adjusted to "data table storing basic customer information"; for another example, the current date can be adjusted, the current date "20230308" can be adjusted to "20240308", etc. It should be noted that the examples here are only illustrative and do not limit this embodiment.
[0137] The present application provides a possible implementation method. In the above step S103, a training sample set is used to train a to-be-trained model, and a language conversion assistant is constructed to convert a natural language query question into a structured language search statement. Specifically, the following steps B1 to B4 may be included:
[0138] Step B1, using the sample pairs of natural language query questions and table information in the training sample set to train the initial table recall model in the training model to obtain a trained table recall model;
[0139] Step B2, using the sample pairs of natural language query questions, table information, and structured language retrieval sentences corresponding to the natural language query questions in the training sample set, to train the initial table field recall model in the training model to obtain a trained table field recall model;
[0140] Step B3, using the table information, current date, natural language query question, and sample pairs of structured language retrieval sentences corresponding to the natural language query question in the training sample set to train the initial language conversion model in the training model to obtain a trained language conversion model;
[0141] Step B4, combining the trained table recall model, the trained table field recall model and the trained language conversion model to construct a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0142] In this embodiment, the table recall model is responsible for identifying and recalling database tables related to natural language query questions, the table field recall model is responsible for identifying and recalling specific fields of the database table related to the natural language query questions, and the language conversion model is responsible for converting the natural language query questions into corresponding structured language retrieval statements (such as SQL); this embodiment combines the trained table recall model, the trained table field recall model and the trained language conversion model to construct a language conversion assistant that converts natural language query questions into structured language retrieval statements, which can quickly convert natural language query questions into structured language retrieval statements, thereby improving the query response speed and making the data retrieval process smoother; and the language conversion assistant can understand and process more complex natural language query questions, such as processing ambiguous statements, context-related questions or time-related queries, and users can interact with the database in the natural language they are familiar with without having to adapt to the language of the machine, which greatly improves the user experience.
[0143] The present application provides a possible implementation method. In step S103, before using the training sample set to train the to-be-trained model and constructing a language conversion assistant for converting natural language query questions into structured language search statements, the following steps C1 may also be included:
[0144] Step C1, optimizing and / or enhancing the training sample set to obtain a processed training sample set.
[0145] In this step, the training sample set can be corrected by predefined rules, including correcting SQL syntax errors, correcting time range representation, etc., to ensure the correctness and logical accuracy of the generated SQL statements. In addition, the processed training samples can be submitted to domain experts for manual review and annotation to further improve the quality of training samples and the accuracy of model training.
[0146] The enhancement processing of the training sample set may include:
[0147] 1) Query enhancement: GPT is used to rewrite the query multiple times without changing the semantics.
[0148] 2) Table name enhancement: Because this is a single-table NL2SQL scenario, the table name in the generated SQL and the table name in the DDL (Data Definition Language) must be the same. To avoid model overfitting, the input and output table names are randomly replaced with random strings.
[0149] 3) Field enhancement: The translation model of field descriptions is trained through the existing table metadata, with a certain probability of changing either the field name or the field description to increase the robustness of the model.
[0150] 4) Field shuffling: Shuffle the order of fields (change their positions) to prevent the model from remembering the order and to prevent overfitting.
[0151] In this way, step S103 uses the training sample set to train the to-be-trained model, and constructs a language conversion assistant that converts natural language query questions into structured language search statements, which may specifically include the following steps C2:
[0152] Step C2, using the processed training sample set to train the to-be-trained model, and constructing a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0153] This embodiment can increase the diversity of training samples and improve the overall quality of data by optimizing and / or enhancing the training sample set, which helps to train a more accurate model.
[0154] The present application provides a possible implementation method. After the training model is trained using the training sample set in step S103 and a language conversion assistant is constructed to convert the natural language query question into a structured language search statement, the following steps D1 and D2 may be further included:
[0155] Step D1, receiving a target natural language query question from a user;
[0156] Step D2, using a language conversion assistant, converts the target natural language query question into a target structured language retrieval statement and outputs it.
[0157] In this step, the table recall model of the language conversion assistant can be used to quickly lock the most relevant data table according to the target natural language query question (query) raised by the user; then, the table field recall model of the language conversion assistant can be used to recall a preset number (such as 10, etc.) of related fields in combination with the target natural language query question and the metadata of the data table. On the one hand, it reduces the impact of redundant fields on the language conversion model's understanding of the query, and on the other hand, it can save the length of the subsequent splicing prompt; then, the target natural language query question, the recalled table name and description, the metadata of the related fields, and the current date, etc., are passed as prompt input to the language conversion model of the language conversion assistant, and the language conversion model generates an SQL search statement based on this information.
[0158] This embodiment can realize online reasoning, can receive target natural language query questions from users, use the language conversion assistant to convert the target natural language query questions into target structured language retrieval statements, and output them for use in data retrieval and query operations, allowing users who have no knowledge of structured query language to perform complex data retrieval, thereby improving retrieval efficiency and accuracy.
[0159] A possible implementation method is provided in an embodiment of the present application, and syntax checking can also be performed on the SQL search statement generated by the language conversion model of the language conversion assistant to correct obvious syntax errors and time representation problems therein, thereby ensuring the correctness of the execution of the SQL search statement.
[0160] A possible implementation method is provided in an embodiment of the present application, and the generated SQL search statement can also be converted into a specified DSL (Domain Specific Language) language so that the engineering end can uniformly connect and execute on different database computing engines.
[0161] It should be noted that the size of the sequence number of each step in the above embodiment does not mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present application. In practical applications, all the above possible implementation methods can be combined in any way to form possible embodiments of the present application, which will not be described one by one here.
[0162] Based on the methods for constructing a language conversion assistant based on a generative model provided in the above embodiments, and based on the same inventive concept, an embodiment of the present application also provides a system for constructing a language conversion assistant based on a generative model.
[0163] Figure 2 is a structural diagram of a system for building a language conversion assistant based on a generative model provided in an embodiment of the present application. Figure 2 As shown, the system for constructing a language conversion assistant based on a generative model may specifically include a data preprocessing unit 210 , a sample generating unit 220 , and a constructing unit 230 .
[0164] A data preprocessing unit 210 is used to preprocess the meta information of each of the at least one initial data table to obtain at least one preprocessed data table;
[0165] The sample generating unit 220 is used to generate a training sample set including table information, current date, natural language query question, and structured language search sentence corresponding to the natural language query question by combining the meta information of the at least one preprocessed data table and the current date using a generative model;
[0166] The construction unit 230 is used to train the to-be-trained model using the training sample set, and to construct a language conversion assistant for converting natural language query questions into structured language retrieval statements.
[0167] A possible implementation method is provided in an embodiment of the present application, wherein the meta-information includes one or more of a table name, a table description, a field name, a field alias, a field description, a dimension column, an indicator column, whether it is a cube table, a drill-down column, and a drill-down column default value or a completion value; and the table information includes a table name and / or a table description.
[0168] A possible implementation method is provided in an embodiment of the present application, wherein the sample generation unit 220 is further configured to:
[0169] According to the meta information of the at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as first prompt information;
[0170] Designing second prompt information according to the training sample set to be generated;
[0171] The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model;
[0172] The target prompt project content is provided to the generative model to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
[0173] A possible implementation method is provided in an embodiment of the present application, wherein the sample generation unit 220 is further configured to:
[0174] Adjust one or more of the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the first prompt information to obtain adjusted first prompt information;
[0175] The adjusted first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model.
[0176] A possible implementation method is provided in an embodiment of the present application, wherein the second prompt information includes prompt content for generating table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions in a preset format.
[0177] A possible implementation method is provided in an embodiment of the present application, wherein the construction unit 230 is further configured to:
[0178] Using the sample pairs of natural language query questions and table information in the training sample set, training the initial table recall model in the model to be trained to obtain a trained table recall model;
[0179] Using the sample pairs of natural language query questions, table information, and structured language retrieval sentences corresponding to the natural language query questions in the training sample set, the initial table field recall model in the training model is trained to obtain a trained table field recall model;
[0180] Using the table information, current date, natural language query question, and sample pairs of structured language retrieval sentences corresponding to the natural language query question in the training sample set, an initial language conversion model in the training model is trained to obtain a trained language conversion model;
[0181] Combining the trained table recall model, the trained table field recall model and the trained language conversion model, a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
[0182] A possible implementation method is provided in the embodiment of the present application, such as Figure 3 As shown above Figure 2 The system shown may also include a sample processing unit 310 for:
[0183] Before the construction unit 230 uses the training sample set to train the model to be trained and constructs a language conversion assistant that converts natural language query questions into structured language retrieval statements, the training sample set is optimized and / or enhanced to obtain a processed training sample set;
[0184] The construction unit 230 is further used to: use the processed training sample set to train the to-be-trained model, and construct a language conversion assistant that converts natural language query questions into structured language retrieval statements.
[0185] A possible implementation method is provided in the embodiment of the present application, such as Figure 3 As shown above Figure 2 The system shown may also include an online reasoning unit 320 for:
[0186] Receiving a target natural language query question from a user;
[0187] The target natural language query question is converted into a target structured language search statement using the language conversion assistant, and the statement is output.
[0188] Based on the same inventive concept, an embodiment of the present application also provides an electronic device, including a processor and a memory, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the method of building a language conversion assistant based on a generative model in any of the above embodiments.
[0189] In an exemplary embodiment, an electronic device is provided, such as Figure 4 As shown, Figure 4 The electronic device 400 shown includes: a processor 401 and a memory 403. The processor 401 and the memory 403 are connected, such as through a bus 402. Optionally, the electronic device 400 may also include a transceiver 404. It should be noted that in actual applications, the transceiver 404 is not limited to one, and the structure of the electronic device 400 does not constitute a limitation on the embodiments of the present application.
[0190] Processor 401 may be a CPU (Central Processing Unit), a GPU (Graphics Processing Unit), a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array), or other programmable logic devices, transistor logic devices, hardware components, or any combination thereof. It may implement or execute various exemplary logic blocks, modules, and circuits described in conjunction with the disclosure of this application. Processor 401 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, and the like.
[0191] The bus 402 may include a path to transmit information between the above components. The bus 402 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 402 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 4 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0192] The memory 403 may be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disk, optical disk, digital versatile disk, Blu-ray disk, etc.), a magnetic disk storage medium or other magnetic storage device, or any other medium that can be used to carry or store the desired program code in the form of instructions or data structures and can be accessed by a computer, but is not limited thereto.
[0193] The memory 403 is used to store the computer program code for executing the solution of the present application, and the execution is controlled by the processor 401. The processor 401 is used to execute the computer program code stored in the memory 403 to implement the contents shown in the above method embodiment.
[0194] Among them, electronic devices include but are not limited to: mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 4 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0195] Based on the same inventive concept, an embodiment of the present application further provides a storage medium, in which a computer program is stored, wherein the computer program is configured to execute the method of building a language conversion assistant based on a generative model in any of the above embodiments when running.
[0196] Those skilled in the art can clearly understand that the specific working processes of the systems, devices, and modules described above can refer to the corresponding processes in the aforementioned method embodiments, and for the sake of brevity, they are not further described here.
[0197] Those skilled in the art can understand that the technical solution of the present application can be essentially or all or part of the technical solution can be embodied in the form of a software product, and the computer software product is stored in a storage medium, which includes a number of program instructions, so that an electronic device (such as a personal computer, a server, or a network device, etc.) executes all or part of the steps of the method described in each embodiment of the present application when running the program instructions. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store program codes.
[0198] Alternatively, all or part of the steps of implementing the aforementioned method embodiments may be accomplished by hardware associated with program instructions (such as electronic devices such as personal computers, servers, or network devices), and the program instructions may be stored in a computer-readable storage medium. When the program instructions are executed by a processor of an electronic device, the electronic device executes all or part of the steps of the methods described in the embodiments of the present application.
[0199] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that within the spirit and principles of the present application, they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not deviate from the protection scope of the present application.
Claims
1. A method for constructing a language conversion assistant based on a generative model, characterized in that: include: Preprocessing the meta information of each of the at least one initial data table to obtain at least one preprocessed data table; Using a generative model, in combination with the meta information of the at least one preprocessed data table and the current date, a training sample set including table information, the current date, a natural language query question, and a structured language retrieval statement corresponding to the natural language query question is generated; Using the training sample set to train the model to be trained, and constructing a language conversion assistant that converts natural language query questions into structured language retrieval statements; The meta information includes one or more of table name, table description, field name, field alias, field description, dimension column, indicator column, whether it is a cube table, drill-down column, drill-down column default value or completion value; the table information includes table name and / or table description; The generative model is used to combine the meta information of the at least one preprocessed data table and the current date to generate a training sample set including table information, the current date, a natural language query question, and a structured language search statement corresponding to the natural language query question, including: According to the meta information of the at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as first prompt information; Designing second prompt information according to the training sample set to be generated; The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model; The target prompt project content is provided to the generative model to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
2. The method according to claim 1, characterized in that According to the meta information of the at least one preprocessed data table and the current date, after taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as the first prompt information, the method further comprises: Adjust one or more of the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the first prompt information to obtain adjusted first prompt information; The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model, including: The adjusted first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model.
3. The method according to claim 1, characterized in that The second prompt information includes prompt content for generating table information, current date, natural language query question, and structured language search sentence corresponding to the natural language query question in a preset format.
4. The method according to any one of claims 1 to 3, characterized in that The training sample set is used to train the model to be trained, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements, including: Using the sample pairs of natural language query questions and table information in the training sample set, training the initial table recall model in the model to be trained to obtain a trained table recall model; Using the sample pairs of natural language query questions, table information, and structured language retrieval sentences corresponding to the natural language query questions in the training sample set, the initial table field recall model in the training model is trained to obtain a trained table field recall model; Using the table information, current date, natural language query question, and sample pairs of structured language retrieval sentences corresponding to the natural language query question in the training sample set, an initial language conversion model in the training model is trained to obtain a trained language conversion model; Combining the trained table recall model, the trained table field recall model and the trained language conversion model, a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
5. The method according to any one of claims 1 to 3, characterized in that Before using the training sample set to train the to-be-trained model and constructing a language conversion assistant for converting natural language query questions into structured language retrieval statements, the method further includes: Performing optimization processing and / or enhancement processing on the training sample set to obtain a processed training sample set; The training sample set is used to train the model to be trained, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements, including: The processed training sample set is used to train the to-be-trained model, and a language conversion assistant is constructed to convert natural language query questions into structured language retrieval statements.
6. The method according to any one of claims 1 to 3, characterized in that After constructing a language conversion assistant for converting natural language query questions into structured language search statements, the method further includes: Receiving a target natural language query question from a user; The target natural language query question is converted into a target structured language search statement using the language conversion assistant, and the statement is output.
7. A system for building a language conversion assistant based on a generative model, characterized in that: include: A data preprocessing unit, used for preprocessing the meta information of each initial data table in at least one initial data table to obtain at least one preprocessed data table; A sample generating unit, configured to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval statements corresponding to the natural language query questions by using a generative model and combining the meta information of the at least one preprocessed data table and the current date; A construction unit, used to train a to-be-trained model using the training sample set, and to construct a language conversion assistant for converting natural language query questions into structured language retrieval statements; The meta information includes one or more of table name, table description, field name, field alias, field description, dimension column, indicator column, whether it is a cube table, drill-down column, drill-down column default value or completion value; the table information includes table name and / or table description; The sample generation unit is also used for: According to the meta information of the at least one preprocessed data table and the current date, taking the table name, table description, field name, field alias, field description, current date, structured language grammar range, and date expression range of each preprocessed data table in the at least one preprocessed data table as first prompt information; Designing second prompt information according to the training sample set to be generated; The first prompt information and the second prompt information are integrated to obtain target prompt engineering content that interacts with the generative model; The target prompt project content is provided to the generative model to generate a training sample set including table information, current date, natural language query questions, and structured language retrieval sentences corresponding to the natural language query questions.
8. An electronic device, characterized in that: The invention comprises a processor and a memory, wherein a computer program is stored in the memory, and the processor is configured to run the computer program to execute the method for building a language conversion assistant based on a generative model according to any one of claims 1 to 6.
9. A storage medium, characterized in that: The storage medium stores a computer program, wherein the computer program is configured to execute the method for constructing a language conversion assistant based on a generative model according to any one of claims 1 to 6 when running.
Citation Information
Patent Citations
Improved NL2SQL method based on generative large language model
CN116821168A
Data processing method and device, electronic equipment and storage medium
CN117370373A
Method and device for converting natural language into database statement, equipment and medium
CN117370378A
Data query statement generation method and device, equipment and storage medium
CN117453717A
Natural language-based multi-table data query and prediction method
CN117725078A
