Query statement generation method and device, equipment, storage medium and program product

The natural language question text is encoded through a large language model and combined with the data table information to generate an accurate structured query language, which solves the problem of low accuracy of query language generation in the existing technology and realizes efficient data processing in enterprise scenarios. Management and analysis.

CN118796872APending Publication Date: 2024-10-18HANDAN BRANCH OF CHINA MOBILE GRP HEBEI COMPANYLIMITED +1
View PDF 0 Cites 7 Cited by

Patent Information

Application Number
CN202410372853.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-03-29
Publication Date
2024-10-18

AI Technical Summary

Technical Problem

In the existing technology, the structure of deep neural networks is mainly adjusted to improve test indicators, resulting in poor performance on common test sets. Structured query language generation is difficult to achieve the expected goals and has poor accuracy.

Method used

The natural language question text submitted by the user is encoded through a large language model (LLM), and rich prompt information is constructed by combining the table annotations of the data table and the relevant information in the preset metadata database, including table annotations, field names, field annotations, Related questions and thought chains, and then generate accurate structured query language, and improve the accuracy of generation through manual verification and fine-tuning training.

Benefits of technology

It realizes the generation of accurate structured query language in enterprise scenarios, improves the accuracy and reliability of query language, and meets the needs of enterprise data management and analysis.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118796872A_ABST
    Figure CN118796872A_ABST
Patent Text Reader

Abstract

The invention provides a query statement generation method and device, equipment, a storage medium and a program product, and the method comprises the steps: responding to a first problem text submitted by a user, coding the first problem text through LLM, and obtaining a first sentence vector; based on the first sentence vector and table annotations of the data tables, selecting N data tables from a preset metadatabase, wherein the similarity of the N data tables and the first question text ranks higher; based on first table related information of each data table, first prompt information of each data table is constructed, and the first table related information comprises table annotations, field names, field annotations, related questions and thinking chains; and inputting the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM. According to the method, the constructed prompt information is richer and more efficient by utilizing related problems and thinking chains, and the accuracy of the structured query language generated by the LLM is realized by finely adjusting the LLM through prompt learning.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a query statement generation method, device, equipment, storage medium and program product. Background Art

[0002] With the rapid development of the Internet and Internet of Things technologies, as well as the popularization of various sensors and smart devices, the generation and storage of data has increased exponentially. Especially in recent years, with the advent of the slogan of enterprise digitalization, the application scenarios of big data have continued to expand, including finance, medical care, education, retail, manufacturing and other fields, involving data management, data analysis, decision support and other aspects.

[0003] At present, the general development process of enterprise data tables is: users first describe the requirements analysis content through the requirements form, then the requirements personnel analyze the requirements - SQL (Structured Query Language) developers develop SQL according to the requirements description - test the application - go online - user consumption needs. However, after the development of this application is completed, it can only respond to the query of the inherent SQL.

[0004] Among the related technologies, the NL2SQL technology in NLP (Natural Language Processing) is mainly used to directly convert natural language into directly executable SQL to achieve data query and statistics. These technologies usually focus on adjusting the deep neural network structure to improve test indicators, perform poorly on general test sets, and there is no feasible solution that can be implemented in enterprises.

[0005] Therefore, it is urgent to propose a query statement generation method, device, equipment, storage medium and program product to solve the above technical problems. Summary of the invention

[0006] The present invention provides a query statement generation method, device, equipment, storage medium and program product, which are used to solve the problem that the prior art mainly adjusts the deep neural network structure to improve the test index, performs poorly on the general test set, resulting in the structured query language generation being difficult to achieve the expected goal and the defect of poor accuracy, and realizes the accurate acquisition of structured query language.

[0007] The present invention provides a query statement generating method, comprising:

[0008] In response to a first question text submitted by a user, encoding the first question text through a large language model (LLM) to obtain a first sentence vector;

[0009] Based on the first sentence vector and the table annotations of the data table, select N data tables with the highest similarity ranking with the first question text from a preset metadata database, where N is a positive integer;

[0010] Based on the first table related information of each of the data tables, construct first prompt information of each of the data tables, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains;

[0011] The first prompt information is input into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0012] According to a query statement generation method provided by the present invention, the related questions and the thought chain of each database table in the preset metadata database are obtained in the following manner:

[0013] Acquire a second question text from a training database, and obtain a second structured query language matching the second question text through the LLM;

[0014] Encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector;

[0015] Based on the second question text and the second structured query language, generate third prompt information, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe the generation steps of generating the second structured query language according to the second question text;

[0016] Key sentences are extracted from the description results, and the extracted key sentences are stored correspondingly in the relevant question field and the thought chain field of the data table matching the second question text in the preset metadata database, wherein the relevant question field is used to describe the relevant questions and structured query language of the data table, and the thought chain field is used to record the generation steps of the structured query language.

[0017] According to a query statement generation method provided by the present invention, the second structured query language matching the second question text is obtained through the LLM, including:

[0018] Acquire a second question text from the training database, and encode the second question text through the LLM to obtain a second sentence vector;

[0019] Based on the second sentence vector and the table annotations of the data table, M data tables with the highest similarity ranking with the second question text are selected from a preset metadata database, where M is a positive integer;

[0020] Based on the second table related information of each of the data tables, construct second prompt information of each of the data tables, wherein the second table related information includes table comments, field names and field comments;

[0021] The second prompt information is input into the LLM to obtain a second structured query language corresponding to the second question text output by the LLM.

[0022] According to a query sentence generation method provided by the present invention, encoding the second question text and the second structured query language by the LLM to obtain a third sentence vector further includes:

[0023] Performing syntax check on the second structured query language;

[0024] If the second structured query language passes the syntax check, sending the second structured query language to a manual processing terminal, so that the manual processing terminal checks whether the second structured query language meets the business requirements;

[0025] When receiving a feedback result that the manual processing terminal determines that the second structured query language meets the business requirements, the second question text and the second structured query language are encoded by the LLM to obtain a third sentence vector.

[0026] According to a query statement generation method provided by the present invention, the method further includes:

[0027] Continue to execute the step of acquiring a second question text from the training database, and obtaining a second structured query language matching the second question text through the LLM;

[0028] Continue to perform the step of encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector;

[0029] Continue to execute the step of generating third prompt information based on the second question text and the second structured query language, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe a generation step of generating the second structured query language according to the second question text;

[0030] Continue to extract key sentences from the description results, and store the extracted key sentences in the relevant question fields and thinking chain fields of the data table in the preset metadata database that matches the second question text, until the update ratio of the relevant question fields and thinking chain fields of the database table in the preset metadata database reaches a preset threshold.

[0031] According to a query statement generation method provided by the present invention, after inputting the first prompt information into the LLM to obtain the first structured query language corresponding to the first question text output by the LLM, the method further includes:

[0032] Fine-tune the LLM according to the first structured query languages ​​and the corresponding first question texts that are verified by the user to be correctly generated, to obtain a fine-tune LLM;

[0033] In response to a third question text submitted by a user, the third question text is input into the fine-tuned LLM to obtain a third structured query language output by the fine-tuned LLM.

[0034] The present invention also provides a query statement generating device, comprising:

[0035] an encoding unit, configured to, in response to a first question text submitted by a user, encode the first question text through a large language model LLM to obtain a first sentence vector;

[0036] A selection unit, configured to select N data tables with the highest similarity ranking with the first question text from a preset metadata database based on the first sentence vector and the table annotations of the data table, wherein N is a positive integer;

[0037] A construction unit, configured to construct first prompt information of each of the data tables based on first table related information of each of the data tables, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains;

[0038] A generating unit is used to input the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0039] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, any one of the query statement generation methods described above is implemented.

[0040] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the query statement generation method described in any one of the above is implemented.

[0041] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the query statement generating method described in any one of the above is implemented.

[0042] The query statement generation method, device, equipment, storage medium and program product provided by the present invention include: in response to a first question text submitted by a user, encoding the first question text through LLM to obtain a first sentence vector; based on the first sentence vector, selecting N data tables with the highest similarity ranking with the first question text from a preset metadata database; based on the first table related information of each data table, constructing the first prompt information of each data table, wherein the first table related information includes table annotations, field names, field annotations, related questions and thought chains; inputting the first prompt information into LLM to obtain the first structured query language corresponding to the first question text output by LLM. The present invention makes the constructed prompt information richer and more efficient by using related questions and thought chains, and achieves the accuracy of the structured query language generated by LLM by fine-tuning LLM through prompt learning. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0044] Figure 1 This is one of the flow charts of the query statement generation method provided by the present invention;

[0045] Figure 2 This is the second flow chart of the query statement generation method provided by the present invention;

[0046] Figure 3 It is a structural schematic diagram of the query statement generating device provided by the present invention;

[0047] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0048] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0049] Figure 1 One of the flow charts of the query statement generation method provided by the present invention; Figure 1 As shown, the method includes:

[0050] Step 110, in response to a first question text submitted by a user, encoding the first question text through a large language model LLM to obtain a first sentence vector;

[0051] The first question text submitted by the user here refers to a question about querying a data table in a preset database under a specific business scenario of an enterprise.

[0052] This type of question text usually involves the need to extract specific information from the database or perform specific operations to support the company's decision-making and operational needs in a specific business scenario. For example, an e-commerce company has a preset database that includes data tables such as product tables, order tables, and user tables. The user will submit this question text "Please list the top ten products in terms of sales volume and their sales volume in the past month."

[0053] It should be noted that the LLM in this embodiment is deployed locally. That is, by deploying LLM locally, the LLM is fine-tuned through prompt learning to improve the effect of NL2SQL, and through text interaction, more robust and generalized data query and statistical services can be provided to customers.

[0054] In this embodiment, the first question text submitted by the user is input into LLM. LLM will encode the text through a pre-trained model and convert each word, phrase or sentence in the text into a corresponding vector representation. LLM will consider the context, grammatical structure and semantic information of the text during the encoding process to capture the deep meaning of the text. The final result is a high-dimensional vector representing the semantic information of the entire question text, i.e., the first sentence vector.

[0055] Step 120, based on the first sentence vector and the table annotations of the data table, select N data tables with the highest similarity ranking with the first question text from a preset metadata database, where N is a positive integer;

[0056] Among them, metadata is data of data. Generally speaking, the metadata database stores table information, table comments, table fields, field comments, primary key / foreign key and other information of business data tables in specific business scenarios of the enterprise.

[0057] Optionally, a cosine similarity algorithm is used to calculate the similarity between the first sentence vector and the table annotations of each data table in the preset metadata database.

[0058] For example, the first vector representation is recorded as A, and the vector representation of the table annotation in the metadata database is recorded as B. Calculate the cosine similarity between vector A and vector B. The obtained similarity value is used as a measure of the similarity between the first question text and each data table. The closer the value is to 1, the higher the similarity. According to the similarity calculation results, the data tables in the database are sorted from high to low according to the similarity with the first sentence vector. Select the top N data tables with the highest similarity from the top-ranked data tables.

[0059] Step 130, constructing first prompt information of each data table based on first table related information of each data table, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains;

[0060] It should be noted that in this embodiment, two fields, namely, related questions and thought chain, are newly added to the preset metadata database.

[0061] Among them, related questions are used to record related questions and structured query languages ​​for data tables. Thinking chains are used to record the steps for generating the structured query language for the data table. For example, for a sales data table, the related question field can contain various common queries, such as "query sales in the last month", "list the top ten products in sales", etc. These questions can help you understand how to use the data table for query and analysis more quickly. For an order data table, the thinking chain field can record how to write structured query languages ​​to meet different data analysis needs. These steps can help you understand how to build complex structured query languages, so that you can better use data tables for analysis.

[0062] Optionally, a Prompt Generator can be used to integrate table comments, field names, field comments, related questions, and thought chains. For example, use the table comment as the beginning of the prompt to describe the basic information and purpose of this data table. Integrate the field name and field comment into the prompt information to show the fields of the data table and their meaning and purpose. Integrate common questions related to the data table into the prompt information to guide the LLM to understand how to query and analyze the data table. Integrate the steps for generating the structured query language of the data table into the prompt information to help the LLM understand how to build complex query statements.

[0063] Step 140: input the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0064] In this embodiment, the first prompt information corresponding to the first question text is input into the LLM again, so as to generate the first structured query language corresponding to the first question text.

[0065] For example, there is a sales data table. The first prompt information for the question text "Query the sales volume of the most recent quarter" submitted by the user is as follows:

[0066]

Table Notes

[0067]

Field name and field comment

[0068] -Order number: uniquely identifies each sales order

[0069] - Date: The transaction date of the order

[0070] - Product Name: The name of the product being sold

[0071] -Amount: Order amount ...

[0073] Related questions:

[0074] -Query the sales volume for the most recent quarter

[0075] - List the top ten products in terms of sales ...

[0077]

Thinking Chain

[0078] 1. Write SQL statements to filter by time conditions

[0079] 2. Calculate sales based on filter conditions

[0080] 3. Sort sales and filter out the top ten products ...

[0082] Such first prompt information is input into LLM, and LLM can generate a corresponding first structured query language, for example:

[0083] SELECT SUM(amount) AS sales amount

[0084] FROM sales data table

[0085] WHERE date>='start date' AND date<='end date';

[0086] The method provided in this embodiment encodes the first question text through LLM in response to the first question text submitted by the user to obtain a first sentence vector; based on the first sentence vector, selects N data tables with the highest similarity ranking with the first question text from the preset metadata database; constructs the first prompt information of each data table based on the first table related information of each data table, wherein the first table related information includes table annotations, field names, field annotations, related questions and thought chains; inputs the first prompt information into LLM to obtain the first structured query language corresponding to the first question text output by LLM. The present invention makes the constructed prompt information richer and more efficient by using related questions and thought chains, and achieves the accuracy of the structured query language generated by LLM by fine-tuning LLM through prompt learning.

[0087] In some embodiments, the related questions and the thought chains of each database table in the preset metadata database are obtained in the following manner:

[0088] Acquire a second question text from a training database, and obtain a second structured query language matching the second question text through the LLM;

[0089] Encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector;

[0090] Based on the second question text and the second structured query language, generate third prompt information, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe the generation steps of generating the second structured query language according to the second question text;

[0091] Key sentences are extracted from the description results, and the extracted key sentences are stored correspondingly in the relevant question field and the thought chain field of the data table matching the second question text in the preset metadata database, wherein the relevant question field is used to describe the relevant questions and structured query language of the data table, and the thought chain field is used to record the generation steps of the structured query language.

[0092] In this embodiment, during the training phase of LLM, the data table in the preset metadata database only contains basic table information, such as table comments, field names, and field comments. In this phase, through ICL (In-Content-Learning), these data can be used to construct richer and more efficient prompt information, thereby achieving the purpose of small sample learning.

[0093] Specifically, a training database is constructed, which contains a second question text marked with a structured query language tag, so that the LLM can learn how to convert the question text into a query language. After obtaining the second structured query language matching the second question text using the LLM, after determining that the generated second structured query language meets the expected result using the structured query language tag of the second question text, the second question text and its corresponding structured query language are encoded using the LLM to generate a sentence vector. Based on the second question text and the corresponding structured query language, a third prompt information is generated. This prompt information is a guide that tells the LLM how to generate the corresponding second structured query language based on the input second question text. Then, the third prompt information and the third sentence vector are input into the LLM to obtain the output result of the LLM. Finally, the description result is subjected to key sentence extraction, and the extracted key sentences are stored in the relevant question field and the thinking chain field of the data table matching the second question text in the preset metadata database.

[0094] For example, when the third prompt information and the second sentence vector are input into the LLM, the output result of the LLM may be a text description, which details how to generate the corresponding second structured query language according to the second question text. For example, the output result may include the following content:

[0095] For the second question text "How to query the sales of the most recent quarter", the second structured query language can be constructed by identifying the keywords 'query', 'most recent quarter' and 'sales'. First, determine the time range according to 'most recent quarter' and calculate the sum of sales in the sales table, that is, use the SQL statement "SELECT SUM(SalesAmount)FROM Sales WHERE Quarter='most recent quarter'".

[0096] From this output, we can extract two fields: related questions and thought chain:

[0097] The Related Questions field describes the related questions and SQL statements of the data table. For example, the SQL statement corresponding to "How to query the sales amount of the most recent quarter" is "SELECT SUM(SalesAmount)FROM Sales WHERE Quarter=most recent quarter".

[0098] The thought chain field records the steps of generating SQL statements, such as: "The key words 'query', 'most recent quarter' and 'sales' are extracted from the question text, and a query statement is constructed based on these keywords. Then, based on the time range of 'most recent quarter', the total sales in the sales table is calculated."

[0099] The method provided in this embodiment adds two fields, namely related questions and thought chains, to the preset metadata database in the above manner, analyzes each training process, updates these two fields, and combines these two fields with context learning to construct prompt information, thereby achieving subsequent fine-tuning of the LLM.

[0100] In some embodiments, obtaining the second structured query language matching the second question text through the LLM includes:

[0101] Acquire a second question text from the training database, and encode the second question text through the LLM to obtain a second sentence vector;

[0102] Based on the second sentence vector and the table annotations of the data table, M data tables with the highest similarity ranking with the second question text are selected from a preset metadata database, where M is a positive integer;

[0103] Based on the second table related information of each of the data tables, construct second prompt information of each of the data tables, wherein the second table related information includes table comments, field names and field comments;

[0104] The second prompt information is input into the LLM to obtain a second structured query language corresponding to the second question text output by the LLM.

[0105] In this embodiment, the second question text is first obtained from the training database and input into the LLM. The LLM encodes the text through the pre-trained model and converts each word, phrase or sentence in the text into a corresponding vector representation. The final result is a high-dimensional vector representing the semantic information of the entire question text, i.e., the second vector.

[0106] Based on the second sentence vector and the table annotations of the data table, select the top M data tables with similarity ranking to the second question text from the preset metadata database. Optionally, the cosine similarity algorithm is used to calculate the similarity between the second sentence vector and the table annotations of each data table in the preset metadata database. According to the similarity calculation result, the data tables in the database are sorted from high to low according to the similarity with the first sentence vector. Select the top M data tables with similarity ranking from the top ranked data tables.

[0107] Next, optionally, a prompt generator can be used to integrate table comments, field names, and field comments. For example, the table comment is used as the beginning of the prompt to describe the basic information and purpose of this data table. The field name and field comment are integrated into the prompt information to show the fields of the data table and their meaning and purpose.

[0108] Finally, the second prompt information is input into the LLM to obtain the second structured query language corresponding to the second question text output by the LLM.

[0109] In some embodiments, encoding the second question text and the second structured query language by the LLM to obtain a third sentence vector further includes:

[0110] Performing syntax check on the second structured query language;

[0111] If the second structured query language passes the syntax check, sending the second structured query language to a manual processing terminal, so that the manual processing terminal checks whether the second structured query language meets the business requirements;

[0112] When receiving a feedback result that the manual processing terminal determines that the second structured query language meets the business requirements, the second question text and the second structured query language are encoded by the LLM to obtain a third sentence vector.

[0113] It should be noted that the database query language has a specific syntax format and specifications. If the generated query language does not conform to these specifications, errors will occur when executing the database query. Therefore, in this embodiment, the second structured query language is firstly syntax-checked to ensure that it conforms to the syntax rules of the specific database query language.

[0114] It should also be noted that, usually, syntax checking alone cannot fully ensure that the query language meets the actual business scenario and logic requirements. Therefore, after passing the syntax check, it is sent to the manual processing terminal, which verifies whether the structured query language meets the business requirements.

[0115] Once the manual processing terminal determines that the second structured query language meets the business requirements, we can continue to encode the second question text and the second structured query language through LLM to obtain the third sentence vector.

[0116] In some embodiments, the method further comprises:

[0117] Continue to execute the step of acquiring a second question text from the training database, and obtaining a second structured query language matching the second question text through the LLM;

[0118] Continue to perform the step of encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector;

[0119] Continue to execute the step of generating third prompt information based on the second question text and the second structured query language, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe a generation step of generating the second structured query language according to the second question text;

[0120] Continue to extract key sentences from the description results, and store the extracted key sentences in the relevant question fields and thinking chain fields of the data table in the preset metadata database that matches the second question text, until the update ratio of the relevant question fields and thinking chain fields of the database table in the preset metadata database reaches a preset threshold.

[0121] In this embodiment, in the training phase of LLM, iterative training is performed with the update ratio of the relevant question fields and the thought chain fields of the database table in the preset metadata database as the target.

[0122] Specifically, first obtain the second question text from the training database, and obtain the corresponding second structured query language through LLM. Then input the second question text and the second structured query language into LLM to obtain the third sentence vector, then generate the third prompt information based on the second question text and the second structured query language, and input the prompt information and the third sentence vector into LLM to obtain the description result output by LLM, which describes the generation steps of generating the second structured query language according to the second question text. Next, extract the key sentences from the description result, and store the extracted key sentences in the relevant question fields and thinking chain fields of the data table matching the second question text in the preset metadata database. Repeat the above steps until the update proportion of the relevant question fields and thinking chain fields of the database table in the preset metadata database reaches the preset threshold. Through these steps, the relevant question fields and thinking chain fields of the database table in the preset metadata database can be gradually accumulated and updated.

[0123] For ease of understanding, refer to Figure 2 . Figure 2 The second flowchart of the query statement generation method provided by the present invention; Figure 2 As shown, the steps of the training phase in the query statement generation method include:

[0124] Step 1: The user submits a question text, which is converted into a sentence vector after LLM embedding.

[0125] Step 2: Compare the vectorized question with the table annotations in the metadata database and calculate their similarity. The specific algorithm can be the cosine similarity algorithm;

[0126] Step 3: Select the top N tables with high similarity, and use the Prompt Generator to build a basic prompt message using table comments, field names, and field comments.

[0127] Step 4: Send the prompt generated in step 3 to LLM to generate SQL.

[0128] Step 5: Verify the syntax of the generated SQL using the test library built based on the metadata library. If the verification is successful, go to step 6; if it is unsuccessful, go to step 3.

[0129] Step 6: Manually confirm whether the generated SQL meets the business requirements. If yes, go to step 7; otherwise, go to step 3;

[0130] Step 7. The question and SQL have been generated and manually confirmed. At this time, the question and SQL are converted into sentence vectors after LLM embedding and input into LLM. A question "Please describe the process of answer generation step by step" is added to the prompt information. Finally, the results output by LLM are transferred to the metadata database in the related questions and thinking chain fields. The related questions field describes the questions and SQL statements related to the table, and the thinking chain field describes the process of SQL statement generation.

[0131] The training process in this embodiment uses the update ratio of the two fields of related issues of the data tables in the preset metadata database and the thinking chain as the standard, that is, most of the data tables in the preset metadata database already have related issues and the thinking chain content, and the update training can be stopped only when there is relevant issues.

[0132] In some embodiments, after inputting the first prompt information into the LLM and obtaining the first structured query language corresponding to the first question text output by the LLM, the method further includes:

[0133] Fine-tune the LLM according to the first structured query languages ​​and the corresponding first question texts that are verified by the user to be correctly generated, to obtain a fine-tune LLM;

[0134] In response to a third question text submitted by a user, the third question text is input into the fine-tuned LLM to obtain a third structured query language output by the fine-tuned LLM.

[0135] In this embodiment, the query statement generation method based on LLM is divided into three stages: a training stage, an application stage, and an optimization stage.

[0136] In this embodiment, after the application phase lasts for a period of time, the LLM is fine-tuned and trained based on the user's feedback on the generation of the correct first structured query language and the corresponding first question text, so that the LLM has long-term fixed memory. The fine-tuning tools include but are not limited to Lora, P-Tuning v2, etc.

[0137] After the LLM is fine-tuned and trained, since the LLM has a long-term fixed memory, the third question text submitted by the user can be directly input into the fine-tuned and trained LLM, and the fine-tuned and trained LLM outputs the third structured query language.

[0138] The query statement generating device provided by the present invention is described below. The query statement generating device described below and the query statement generating method described above can be referred to each other.

[0139] Figure 3 A schematic diagram of the structure of the query statement generating device provided by the present invention; Figure 3 As shown, the device comprises:

[0140] The encoding unit 310 is used to encode the first question text through a large language model LLM in response to the first question text submitted by the user to obtain a first sentence vector;

[0141] A selection unit 320 is used to select N data tables with the highest similarity to the first question text from a preset metadata database based on the first sentence vector and the table annotations of the data table, where N is a positive integer;

[0142] A construction unit 330 is used to construct first prompt information of each data table based on first table related information of each data table, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains;

[0143] The generating unit 340 is configured to input the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0144] The device provided by the present invention, in response to a first question text submitted by a user, encodes the first question text through LLM to obtain a first sentence vector; based on the first sentence vector, selects N data tables with the highest similarity ranking with the first question text from a preset metadata database; based on the first table related information of each data table, constructs the first prompt information of each data table, wherein the first table related information includes table annotations, field names, field annotations, related questions and thought chains; inputs the first prompt information into LLM to obtain the first structured query language corresponding to the first question text output by LLM. The present invention makes the constructed prompt information richer and more efficient by using related questions and thought chains, and achieves the accuracy of the structured query language generated by LLM by fine-tuning LLM through prompt learning.

[0145] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4 As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the query statement generation method, which includes: in response to a first question text submitted by a user, encoding the first question text through a large language model LLM to obtain a first sentence vector; based on the first sentence vector and the table annotations of the data table, selecting N data tables with the highest similarity ranking with the first question text from a preset metadata database, wherein N is a positive integer; based on the first table related information of each data table, constructing the first prompt information of each data table, wherein the first table related information includes table annotations, field names, field annotations, related questions and thought chains; inputting the first prompt information into the LLM to obtain the first structured query language corresponding to the first question text output by the LLM.

[0146] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the 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, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk and other media that can store program codes.

[0147] On the other hand, the present invention also provides a computer program product, which includes a computer program. The computer program can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the query statement generation method provided by the above methods, the method including: in response to a first question text submitted by a user, encoding the first question text through a large language model LLM to obtain a first sentence vector; based on the first sentence vector and the table annotations of the data table, selecting N data tables with the top similarity ranking with the first question text from a preset metadata database, wherein N is a positive integer; based on the first table related information of each of the data tables, constructing the first prompt information of each of the data tables, wherein the first table related information includes table annotations, field names, field annotations, related questions and thinking chains; inputting the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0148] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the query statement generation method provided by the above-mentioned methods, the method comprising: in response to a first question text submitted by a user, encoding the first question text through a large language model LLM to obtain a first sentence vector; based on the first sentence vector and the table annotations of the data table, selecting N data tables with the top similarity ranking with the first question text from a preset metadata database, wherein N is a positive integer; based on the first table related information of each of the data tables, constructing the first prompt information of each of the data tables, wherein the first table related information includes table annotations, field names, field annotations, related questions and thinking chains; inputting the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

[0149] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0150] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0151] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A query statement generation method, characterized in that: include: In response to a first question text submitted by a user, encoding the first question text through a large language model (LLM) to obtain a first sentence vector; Based on the first sentence vector and the table annotations of the data table, select N data tables with the highest similarity ranking with the first question text from a preset metadata database, where N is a positive integer; Based on the first table related information of each of the data tables, construct first prompt information of each of the data tables, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains; The first prompt information is input into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

2. The query statement generation method according to claim 1, characterized in that: The related questions and the thought chains of each database table in the preset metadata database are obtained in the following manner: Acquire a second question text from a training database, and obtain a second structured query language matching the second question text through the LLM; Encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector; Based on the second question text and the second structured query language, generate third prompt information, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe the generation steps of generating the second structured query language according to the second question text; Key sentences are extracted from the description results, and the extracted key sentences are stored correspondingly in the relevant question field and the thought chain field of the data table matching the second question text in the preset metadata database, wherein the relevant question field is used to describe the relevant questions and structured query language of the data table, and the thought chain field is used to record the generation steps of the structured query language.

3. The query statement generation method according to claim 2, characterized in that: The step of obtaining the second structured query language matching the second question text through the LLM includes: Acquire a second question text from the training database, and encode the second question text through the LLM to obtain a second sentence vector; Based on the second sentence vector and the table annotations of the data table, M data tables with the highest similarity ranking with the second question text are selected from a preset metadata database, where M is a positive integer; Based on the second table related information of each of the data tables, construct second prompt information of each of the data tables, wherein the second table related information includes table comments, field names and field comments; The second prompt information is input into the LLM to obtain a second structured query language corresponding to the second question text output by the LLM.

4. The query statement generation method according to claim 2, characterized in that: The encoding of the second question text and the second structured query language by the LLM to obtain a third sentence vector further includes: Performing syntax check on the second structured query language; If the second structured query language passes the syntax check, sending the second structured query language to a manual processing terminal, so that the manual processing terminal checks whether the second structured query language meets the business requirements; When receiving a feedback result that the manual processing terminal determines that the second structured query language meets the business requirements, the second question text and the second structured query language are encoded by the LLM to obtain a third sentence vector.

5. The query statement generating method according to claim 2, characterized in that: The method further comprises: Continue to execute the step of acquiring a second question text from the training database, and obtaining a second structured query language matching the second question text through the LLM; Continue to perform the step of encoding the second question text and the second structured query language by using the LLM to obtain a third sentence vector; Continue to execute the step of generating third prompt information based on the second question text and the second structured query language, and input the third prompt information and the third sentence vector into the LLM to obtain a description result output by the LLM, wherein the third prompt information is used to instruct the LLM to describe a generation step of generating the second structured query language according to the second question text; Continue to extract key sentences from the description results, and store the extracted key sentences in the relevant question fields and thinking chain fields of the data table in the preset metadata database that matches the second question text, until the update ratio of the relevant question fields and thinking chain fields of the database table in the preset metadata database reaches a preset threshold.

6. The query statement generation method according to claim 1, characterized in that: After inputting the first prompt information into the LLM and obtaining the first structured query language corresponding to the first question text output by the LLM, the method further includes: Fine-tune the LLM according to the first structured query languages ​​and the corresponding first question texts that are verified by the user to be correctly generated, to obtain a fine-tune LLM; In response to a third question text submitted by a user, the third question text is input into the fine-tuned LLM to obtain a third structured query language output by the fine-tuned LLM.

7. A query statement generating device, characterized in that: include: an encoding unit, configured to, in response to a first question text submitted by a user, encode the first question text through a large language model LLM to obtain a first sentence vector; A selection unit, configured to select N data tables with the highest similarity ranking with the first question text from a preset metadata database based on the first sentence vector and the table annotations of the data table, wherein N is a positive integer; A construction unit, configured to construct first prompt information of each of the data tables based on first table related information of each of the data tables, wherein the first table related information includes table comments, field names, field comments, related questions and thought chains; A generating unit is used to input the first prompt information into the LLM to obtain a first structured query language corresponding to the first question text output by the LLM.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the query statement generating method according to any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the query statement generating method according to any one of claims 1 to 6 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the query statement generating method according to any one of claims 1 to 6 is implemented.

Citation Information

Cited By

  • Structured query language generation method and device, electronic equipment and storage medium

    CN119226318A

  • Structured Query Language Generation Method, Apparatus, Electronic Device, and Storage Medium

    CN119226318B

  • Futures content generation method and system based on large language model

    CN119248909A

  • Method and device for generating SQL (Structured Query Language) based on semantic alignment and hierarchical Agent

    CN119377255A

  • Method and device for generating network security rule command based on LLM

    CN121309099A