Statement generation method and device based on large language model, equipment and medium
By inputting the target text and classification prompt words into the large language model, determining the target category as the data table identifier, and combining SQL statements to generate prompt words to generate SQL statements, the problem that the existing technology cannot generate SQL statements based on multiple data tables is solved, and high-accuracy SQL statement generation is achieved.
Patent Information
- Application Number
- CN202411577969.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-06
- Publication Date
- 2025-02-28
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The prior art cannot effectively generate SQL statements based on multiple data tables, and cannot meet the needs of complex text to SQL.
By inputting the pending target text and pre-save classification prompt words into the large language model, the target categories corresponding to the target text are obtained, and each category is used as the identification of a data table, and then the target text, target data table and SQL statement generation prompt words are input into the large language model to generate the target SQL statement.
The ability to generate SQL statements based on multiple data tables is realized, the accuracy of SQL statement generation is improved, and the need to convert complex text into SQL is met.
Smart Images

Figure CN119537415A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a sentence generation method, device, equipment and medium based on a large language model. Background Art
[0002] With the development of big language model technology, big language models have been applied to various industries and fields, and converting text into structured query language (SQL) statements is one of its aspects.
[0003] The existing method of converting text into SQL statements based on a large language model is for single-table queries, that is, only one data table is used to generate SQL statements. However, the application of large language models is becoming more and more extensive, and the problems that need to be processed by large language models are becoming more and more complex. One data table can no longer meet the existing text-to-SQL statement needs, and how to generate SQL statements based on multiple data tables has become a problem that needs to be solved. Summary of the invention
[0004] The present application provides a statement generation method, apparatus, device and medium based on a large language model, which is used to solve the problem that the prior art cannot generate SQL statements based on multiple data tables.
[0005] In a first aspect, an embodiment of the present application provides a sentence generation method based on a large language model, the method comprising:
[0006] Inputting the target text to be processed and the pre-saved classification prompt words into the large language model, and obtaining at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table;
[0007] The target text, table information of each target data table identified as the target category, and pre-saved SQL statement generation prompt words are input into the large language model, and a target SQL statement corresponding to the target text output by the large language model is obtained.
[0008] The above technical solution has the following advantages or beneficial effects: the large language model has the ability to classify text, and the classification ability can determine the classification of the target text, thereby determining which target data tables are needed to generate SQL statements, and then allowing the large language model to generate SQL statements based on the target data tables, thereby realizing the generation of SQL statements based on multiple data tables and ensuring the accuracy of SQL statement generation.
[0009] In some embodiments of the present application, inputting the target text to be processed and the pre-saved classification prompt words into the large language model includes:
[0010] Acquire a pre-saved classification prompt template, wherein the classification prompt template carries the classification prompt word;
[0011] The target text is added to the first preset field of the classification prompt template, and the classification prompt template after adding is input into the large language model.
[0012] The above technical solution has the following advantages or beneficial effects: by presetting the classification prompt template, only the target text needs to be added each time the input of the large language model is determined, thereby improving the processing efficiency.
[0013] In some embodiments of the present application, inputting the target text, table information of each target data table identified as the target category, and pre-saved SQL statement generation prompt words into the large language model includes:
[0014] Acquire a pre-saved SQL statement generation prompt template, wherein the SQL statement generation prompt template carries the SQL statement generation prompt word;
[0015] Adding the target text to a second preset field of the SQL statement generation prompt template, and adding table information of each target data table identified as the target category to a third preset field of the SQL statement generation prompt template;
[0016] The added SQL statement generation prompt template is input into the large language model.
[0017] The above technical solution has the following advantages or beneficial effects: by presetting the SQL statement to generate the prompt template, each time the input of the large language model is determined, only the target text and the corresponding target data table information need to be added, thereby improving the processing efficiency.
[0018] In some embodiments of the present application, the fine-tuning process of the large language model includes:
[0019] Acquire a first sample text and at least one corresponding sample category that are saved in advance for classification, and use the first sample text, the sample category and the classification prompt word as a first sample for fine-tuning the large language model;
[0020] Acquire a pre-saved second sample text for generating an SQL statement and corresponding sample table information of at least one sample data table and a sample SQL statement, and use the second sample text, the sample table information of the at least one sample data table, the sample SQL statement and the SQL statement generation prompt word as a second sample for fine-tuning the large language model;
[0021] The first sample and the second sample are used as fine-tuning data sets, and the large language model is fine-tuned according to the fine-tuning data sets and a preset lora fine-tuning algorithm.
[0022] The above technical solution has the following advantages or beneficial effects: fine-tuning the large language model enables the large language model to have classification capabilities and text-to-SQL capabilities, so that the large model does not need to input the entire table structure every time, avoiding prompt truncation or incorrect judgment when judging the required table, resulting in SQL errors.
[0023] In some embodiments of the present application, before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes:
[0024] Receive an input SQL statement generation instruction carrying text to be recognized;
[0025] The text to be recognized is cleaned according to a preset text cleaning rule, and the cleaned text to be recognized is determined as the target text.
[0026] The above technical solution has the following advantages or beneficial effects: by cleaning the text to be recognized and removing some garbled characters, the interference of these characters on the large language model is reduced.
[0027] In some embodiments of the present application, before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes:
[0028] A pre-saved uniform resource locator URL of a large language model for generating SQL statements is obtained, and the large language model is called according to the URL.
[0029] The above technical solution has the following advantages or beneficial effects: calling a large language model is realized through a URL.
[0030] In some embodiments of the present application, the large language model is a Qwen model.
[0031] The above technical solution has the following advantages or beneficial effects: by fine-tuning the Qwen model, the Qwen model has classification capabilities and text-to-SQL capabilities.
[0032] In a second aspect, an embodiment of the present application provides a sentence generation device based on a large language model, the device comprising:
[0033] A classification module, used to input the target text to be processed and the pre-saved classification prompt words into the large language model, and obtain at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table;
[0034] The processing module is used to input the target text, the table information of each target data table identified as the target category, and the pre-saved SQL statement generation prompt words into the large language model, and obtain the target SQL statement corresponding to the target text output by the large language model.
[0035] In a third aspect, an embodiment of the present application further provides an electronic device, which includes at least a processor and a memory, and the processor is used to implement the steps of any of the above-mentioned sentence generation methods based on a large language model when executing a computer program stored in the memory.
[0036] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of any of the above-mentioned sentence generation methods based on a large language model.
[0037] In an embodiment of the present application, the target text to be processed and the pre-saved classification prompt words are input into the large language model, and at least one target data table corresponding to the target text output by the large language model is obtained; the target text, at least one target data table, and the prompt words generated by the pre-saved SQL statement are input into the large language model, and the target SQL statement corresponding to the target text output by the large language model is obtained. In an embodiment of the present application, the large language model has the ability to classify text, and the classification ability can determine the classification of the target text, thereby determining which target data tables are needed to generate SQL statements, and then the large language model generates SQL statements according to the target data tables, thereby realizing the generation of SQL statements based on multiple data tables and ensuring the accuracy of SQL statement generation. BRIEF DESCRIPTION OF THE DRAWINGS
[0038] In order to more clearly illustrate the technical solution of the present application, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.
[0039] Figure 1A schematic diagram of a sentence generation process based on a large language model provided in an embodiment of the present application;
[0040] Figure 2 A flow chart for fine-tuning a large language model provided in an embodiment of the present application;
[0041] Figure 3 A schematic diagram of the loss function of a large language model provided in an embodiment of the present application;
[0042] Figure 4 A flow chart of reasoning of a large language model provided in an embodiment of the present application;
[0043] Figure 5 A schematic diagram of a front-end question-and-answer page provided for an embodiment of the present application;
[0044] Figure 6 A schematic diagram of the structure of a sentence generation device based on a large language model provided in an embodiment of the present application;
[0045] Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0046] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of the present application.
[0047] In order to realize SQL statement generation based on multiple data tables, the embodiments of the present application provide a statement generation method, apparatus, device and medium based on a large language model.
[0048] In an embodiment of the present application, the electronic device inputs the target text to be processed and the pre-saved classification prompt words into the large language model, obtains the target identifier of at least one data table output by the large language model for outputting SQL statements based on the target text; inputs the target text, the target data table of the target identifier, and the pre-saved SQL statement generation prompt words into the large language model, and obtains the target SQL statement corresponding to the target text output by the large language model.
[0049] Figure 1 A schematic diagram of a sentence generation process based on a large language model provided in an embodiment of the present application, the process includes:
[0050] S101: Inputting a target text to be processed and pre-saved classification prompt words into a large language model, and obtaining at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table.
[0051] A sentence generation method based on a large language model provided in an embodiment of the present application is applied to an electronic device, which may be a PC or a server.
[0052] In related applications, a large number of data tables are stored in electronic devices. When electronic devices perform text2SQL tasks based on the data tables, they rarely use all the data tables. If the information of all the data tables is input into the large language model, it will cause a large load pressure on the large language model, and the input will also exceed the maximum character length that the large language model can process.
[0053] Based on this, in an embodiment of the present application, the electronic device fine-tunes the large language model so that the large language model has classification capabilities. The classification capability is to determine at least one category corresponding to the target text according to the input target text, wherein a category is an identifier of a data table. The target category output by the large language model is the information of at least one data table required to generate sentences for the target text determined by the large language model.
[0054] Specifically, the electronic device fine-tunes the large language model so that the large language model has classification capabilities. In the inference application stage, the electronic device inputs the target text to be processed and the pre-saved classification prompt words into the large language model, so that the large language model uses the classification capability to determine at least one target category corresponding to the target text according to the classification prompt words.
[0055] Among them, in the embodiment of the present application, the classification prompt word includes a prompt word for prompting the large language model to classify the text, and all the categories that the large language model can recognize. For example, the classification prompt word can be "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All the categories are ["student", "score", "teacher", "adress", "subject", "information"]", among which "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question" is the prompt word, and "All the categories are ["student", "score", "teacher", "adress", "subject", "information"]" are all the categories that the large language model can recognize.
[0056] It should be noted that in the embodiment of the present application, each category that can be distinguished by the large language model is respectively identified as an identifier of a data table. Based on this, the electronic device can search for the target data table corresponding to each category based on at least one target category output by the large language model, and use the found target data table as the target data table for sentence generation for the target text.
[0057] Specifically, the large language model can output one target category or multiple target categories. If the large language model outputs one target category, the electronic device determines that only one data table needs to be used when generating SQL statements for the target text, that is, the corresponding target data table identified as the target category; if the large language model outputs multiple target categories, the electronic device determines that multiple data tables need to be used when generating SQL statements for the target text, and the number of data tables used is consistent with the number of target categories output by the large language model, and the data tables used are each target data table identified as each target category.
[0058] Generally, the identifier of a data table is the table name of the data table, and may also be other information used to represent the data table, such as a serial number, etc., which is not limited here.
[0059] S102: Input the target text, table information of each target data table identified as the target category, and pre-saved SQL statement generation prompt words into the large language model, and obtain a target SQL statement corresponding to the target text output by the large language model.
[0060] In an embodiment of the present application, after the electronic device determines the target data table corresponding to the target identifier, the electronic device generates a prompt word based on the target text, the target data table and a pre-saved SQL statement and inputs it into the large language model, so that the large language model converts the target text into a target SQL statement according to the target data table and outputs it.
[0061] The SQL statement generation prompt word is a prompt word used to prompt the large language model to convert text into SQL. For example, the SQL statement generation prompt word may be "You are now a database expert, and you now need to generate a query statement based on the problem."
[0062] The electronic device obtains a target SQL statement corresponding to the target text output by the large language model.
[0063] In an embodiment of the present application, the large language model has the ability to classify text. This classification ability can determine the classification of the target text, thereby determining which target data tables are needed to generate SQL statements, and then allowing the large language model to generate SQL statements based on the target data tables, thereby realizing the generation of SQL statements based on multiple data tables and ensuring the accuracy of SQL statement generation.
[0064] In order to improve the accuracy of SQL statement generation, based on the above embodiment, in the embodiment of the present application, the inputting of the target text to be processed and the pre-saved classification prompt words into the large language model includes:
[0065] Acquire a pre-saved classification prompt template, wherein the classification prompt template carries the classification prompt word;
[0066] The target text is added to the first preset field of the classification prompt template, and the classification prompt template after adding is input into the large language model.
[0067] In an embodiment of the present application, a classification prompt template is pre-stored in the electronic device, and the classification prompt template carries classification prompt words. The electronic device can input the target text into the large language model based on the classification prompt template. In addition, the classification prompt template also includes a first preset field for filling in the target text.
[0068] Specifically, when the electronic device inputs the target text to be processed and the pre-saved classification prompt words into the large language model, the electronic device obtains the pre-saved classification prompt template, adds the target text to the first preset field of the classification prompt template, and inputs the added classification prompt template into the large language model.
[0069] In addition, in order to enable the large language model to more accurately classify the target text, in an embodiment of the present application, the classification prompt template may also carry example text and corresponding example categories.
[0070] For example, if the target text is "Query Lucy's math scores", the completed classification prompt template is: "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"], example: input: 'Query Xiao Ming's Chinese scores', output: ['student', 'score'], input: 'Query Lucy's math scores'". Among them, "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"]" is the classification prompt word; "Example: Input: 'Query Xiao Ming's Chinese scores', output: ['student', 'score']" is the example text and the corresponding example category; "Query Lucy's math scores" is the target text.
[0071] For another example, if the target text is "What is the teaching score of Teacher Han Moumou", the completed classification prompt template is "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"], example: input: 'Query Xiao Ming's Chinese score', output: ['student', 'score'], input: 'What is the teaching score of Teacher Han Moumou'".
[0072] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, the target text, the target data table of each target identifier, and the pre-saved SQL statement generation prompt words are input into the large language model, including:
[0073] Acquire a pre-saved SQL statement generation prompt template, wherein the SQL statement generation prompt template carries the SQL statement generation prompt word;
[0074] Adding the target text to a second preset field of the SQL statement generation prompt template, and adding table information of each target data table identified as the target category to a third preset field of the SQL statement generation prompt template;
[0075] The added SQL statement generation prompt template is input into the large language model.
[0076] In an embodiment of the present application, a SQL statement generation prompt template is also pre-saved in the electronic device, and the SQL statement generation prompt template includes an SQL statement generation prompt word for prompting the large language model to generate an SQL statement, a second preset field for filling in the text to be generated for the SQL statement, and a third preset field for filling in the information of the data table.
[0077] Based on this, the electronic device can generate a prompt template through an SQL statement to input the target text into the large language model and generate a data table used by the target text.
[0078] Specifically, in an embodiment of the present application, the electronic device obtains a pre-saved SQL statement generation prompt template. The electronic device adds the target text to the second preset field of the SQL statement generation prompt template, and adds each target data table identified as a target category to the third preset field of the SQL statement generation prompt template. The electronic device inputs the added SQL statement generation prompt template into the large language model.
[0079] It should be noted that the electronic device adds the table information of each target data table to the third preset field. The table information includes but is not limited to the identifier of the target data table, the fields contained in the target data table, and the value corresponding to each field, etc., which is not limited here.
[0080] In addition, in order to enable the large language model to generate SQL statements more accurately, in an embodiment of the present application, the classification prompt template may also carry sample text and corresponding sample SQL statements.
[0081] For example, the target text is "query Lucy's math scores", and the tables recognized by the large language model are the "student" table and the "score" table. Based on the target text and the table structures of the "student" table and the "score" table, the SQL statement generation prompt template is filled in. The filled-in SQL statement generation prompt template is "You are now a database expert, and you now need to generate query statements based on the problem. The tables and table structures you need are as follows: student table fields: {id student id, name student name}; score table fields: {id identity identifier id, score score, subject subject}, example: input: query Xiao Ming's Chinese score, output: select t1.name, t2.score from student t1 left join score t2 on t1.id=t2.id where t1.name='Xiao Ming and t2.subject='Chinese', input: query Lucy's math score". The SQL statement output by the large language model is: select t1.name, t2.score from student t1 left join score t2 on t1.id = t2.id where t1.name = 'lucy' and t2.subject = 'mathematics'.
[0082] Among them, "You are now a database expert, and you now need to generate query statements based on the problem" generates prompt words for SQL statements; "The tables and table structures you need are as follows: student table fields: {id student id, name student name}; score table fields: {id identity identifier id, score grade, subject subject}" is the table information of each target data table identified as the target category; "Example: Input: Query Xiao Ming's Chinese score, output: select t1.name, t2.score from student t1 left join score t2 on t1.id=t2.id where t1.name='Xiao Ming and t2.subject='Chinese'" is the sample text and the corresponding sample SQL statement.
[0083] For another example, if the target text is "Query the teaching performance of Teacher Liu this year", and the tables identified by the large language model are the "teacher" table and the "score" table, then the prompt template for generating the SQL statement is as follows: "You are now a database expert. You need to generate a query statement based on the question. The tables and their structures you need are as follows: teacher table fields: {id teacher ID, name teacher name}; score table fields: {id identity identification ID, score score, subject subject}. Example: Input: Query Xiaoming's Chinese score, Output: select t1.name, t2.score from student t1 left join score t2 on t1.id = t2.id where t1.name = 'Xiaoming' and t2.subject = 'Chinese', Input: Query the teaching performance of Teacher Liu this year". Based on this, the SQL statement output by the large language model is: select t1.name, t2.score from teacher t1 left join score t2 on t1.id = t2.id where t1.name = 'Liu' and t2.subject = 'Math'.
[0084] To further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiments of the present application, the fine-tuning process of the large language model includes:
[0085] Obtain the pre-saved first sample text for classification and the corresponding at least one sample category, and use the first sample text, the sample category, and the classification prompt word as the first sample for fine-tuning the large language model;
[0086] Obtain the pre-saved second sample text for SQL statement generation, the sample table information of at least one sample data table, and the sample SQL statement, and use the second sample text, the sample table information of the at least one sample data table, the sample SQL statement, and the SQL statement generation prompt word as the second sample for fine-tuning the large language model;
[0087] Use the first sample and the second sample as the fine-tuning data set, and fine-tune the large language model according to the fine-tuning data set and the preset LoRA fine-tuning algorithm.
[0088] With the development of big language model technology, big language models have been applied to various industries and fields, and text-to-SQL is one of them. However, if the big language model is fine-tuned based on open source data sets, it will be difficult to apply to the specific domain data of the enterprise, and the accuracy will be greatly reduced. Therefore, it is necessary to fine-tune the big language model for the internal data of the enterprise, so that the big language model can be well applied to the internal data of the enterprise and improve the accuracy of the big language model querying the internal data of the enterprise. At the same time, each enterprise's data will have many tables, so the big language model must first know which tables should be queried for the current problem, so as to generate the query SQL for the problem and ensure the accuracy of the SQL.
[0089] Based on this, in an embodiment of the present application, a process of fine-tuning a large language model is proposed. Specifically, the purpose of the fine-tuning stage is to enable the large language model to have the ability to perform multi-table classification of data in a specific field based on the fine-tuning corpus that has been annotated. The large language model itself has the ability to classify daily general data, but in a specific field, such as "querying student names and grades", the large language model can know that it is to check grades, but it cannot know which categories of ["student", "score", "teacher", "adress", "subject", "information"] the question belongs to, because these categories are not seen by the large language model, and its classification criteria are also unique to the data in this field, so it is necessary to fine-tune the processed fine-tuning data, so that the large language model can have the ability to classify data of specific categories in specific fields.
[0090] The electronic device stores a first sample text and at least one corresponding sample category for fine-tuning the classification capability of the large language model, and a second sample text and at least one corresponding sample data table and sample SQL statement for fine-tuning the SQL statement generation capability of the large language model. The electronic device can construct a fine-tuning data set for fine-tuning the large language model based on the second sample text and the second sample text.
[0091] Specifically, the electronic device uses the first sample text, sample category and classification prompt words as the first sample for fine-tuning the large language model, and uses the second sample text, sample table information of at least one sample data table, sample SQL statements and SQL statement generation prompt words as the second sample for fine-tuning the large language model.
[0092] The electronic device uses the first sample and the second sample as fine-tuning data sets, and fine-tunes the large language model according to the fine-tuning data sets and a preset lora fine-tuning algorithm.
[0093] Figure 2A flowchart of fine-tuning a large language model provided in an embodiment of the present application, the fine-tuning process includes: constructing text-to-SQL data (second sample) and multi-label classification data (first sample), performing mixed task fine-tuning according to the first sample and the second sample, and deploying the fine-tuned large language model.
[0094] Among them, multi-label text classification data and text-to-sql data are completed through manual annotation. Manual annotation needs to be performed according to the company's business data and query data scenarios. Multi-label text classification data is used to fine-tune the text classification ability of the large language model. The classification category is the table name. Each table name represents a category, so that the large language model knows which tables should be queried. In order to let the large language model know which tables should be queried, all query questions need to be classified. Each query question is divided into multiple categories. For example, the categories corresponding to the text "query student name and score" are "student" and "score", so the tables that need to be queried for this text are the student table and the score table, and these two tables are associated through id. For example, "query the teacher's teaching score", the corresponding categories of this text are "teacher" and "score", so the tables that need to be queried for this text are the teacher table and the score table, and these two tables are associated through id.
[0095] Question text Category (table) What is the teaching score of teacher Han? teacher, score What is Xiao Ming's math score? student,score …… ……
[0096] Table 1
[0097] Table 1 shows the correspondence between question texts and categories provided in the embodiment of the present application. Figure 1 As shown, the categories corresponding to the question text "What is the teaching score of Teacher Han" are teacher and score; the categories corresponding to the question text "What is Xiao Ming's math score" are student and score.
[0098] In an embodiment of the present application, the corpus of the large language model is a data group that is manually or other methods annotated, the purpose of which is to let the large language model know which tables should be queried for which type of questions. These corpora are annotated in the format required by the llamafactory framework. For example: the first sample text is "Query Xiao Ming's Chinese score", and the corresponding sample categories are "student" and "score" as examples. The first sample constructed is: {"instruction":"You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"]","input":"Query Xiao Ming's Chinese score","output":"['student','score']"}. Another example: {"instruction":"You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"]","input":"Query the teaching score of teacher Han","output":"['teacher,'score']"}. Data in this format is the processed multi-label text classification corpus, that is, the first sample.
[0099] Among them, "instruction": "You are now a multi-label text classification expert. You now need to identify the category corresponding to the question based on the question. All categories are ["student", "score", "teacher", "adress", "subject", "information"]" as the classification prompt words.
[0100] The corpus of multi-label text classification enables the large language model to have the ability to perform multi-label text classification for data in this field. It is also necessary to enable the large language model to have the ability to generate SQ for these multiple tables, so it is also necessary to process the corpus from text to SQ, and these corpuses are also annotated by the data group. The manually annotated data contains questions and corresponding correct SQ statements, such as "query Xiaoming's Chinese score", the corresponding SQ is "select t1.name, t2.score from student t1 left join score t2 on t1.id=t2.id where t1.name='Xiaoming'andt2.subject='Chinese'", and for another example, "what is the teaching score of teacher Han this year", the corresponding SQ is "selectt1.name, t2.score from teacher t1 left join score t2 on t1.id=t2.id wheret1.name='Han Moumou'and t2.time='2024'". The "instruction" in the fine-tuning corpus is a prompt word for generating SQL statements. This part is a template with a fixed format, in which the variables in the template are table names and table structures. You only need to replace the variables with the corresponding table names and table structures to get the prompt words of the large language model. The format of the fine-tuning corpus processed according to the above questions, table names and SQL is: {"instruction":"You are now a database expert. You now need to generate query statements based on the questions. The tables and table structures you need are as follows: student table fields: {id student id, name student name}; score table fields: {id identity identifier id, score score, subject subject}", "input":"Query Xiao Ming's Chinese score", "output":"select t1.name, t2.score from student t1 left join score t2 ont1.id=t2.id where t1.name='Xiao Ming'and t2.subject='Chinese'"}, thus constructing a series of text-to-SQL fine-tuning statements, i.e. the second sample.
[0101] The electronic device puts the multi-label text classification corpus and the text-to-SQL corpus marked above together, and uses the llama-factory framework to perform mixed task fine-tuning based on the qwen model. By setting different learning rates and other parameters, it iterates repeatedly until the loss function is fine-tuned to a very small value, thereby obtaining a fine-tuned large language model.
[0102] Figure 3A schematic diagram of the loss function of the large language model provided in the embodiment of the present application, such as Figure 3 As shown in the figure, during the fine-tuning process of the large language model, the loss function will gradually converge with the number of iterations. When the loss function reaches a very small value, the fine-tuning of the large language model is considered to be completed.
[0103] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes:
[0104] Receive an input SQL statement generation instruction carrying text to be recognized;
[0105] The text to be recognized is cleaned according to a preset text cleaning rule, and the cleaned text to be recognized is determined as the target text.
[0106] In an embodiment of the present application, the electronic device may receive an input SQL statement generation instruction carrying text to be recognized during operation. In order to improve the accuracy of converting text to SQL, after the electronic device receives the SQL statement generation instruction, the electronic device will clean the text to be recognized carried in the SQL statement generation instruction, and determine the cleaned text to be recognized as the target text, and then execute subsequent text-to-SQL tasks.
[0107] Specifically, the electronic device stores preset text cleaning rules, which may include removing garbled characters, capitalizing the first letter, deleting duplicate text, etc. The electronic device may clean the text to be recognized based on the text cleaning rules to obtain the target text.
[0108] In order to further improve the accuracy of SQL statement generation, based on the above embodiments, in the embodiment of the present application, before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes:
[0109] A pre-saved uniform resource locator URL of a large language model for generating SQL statements is obtained, and the large language model is called according to the URL.
[0110] In the embodiment of the present application, after the electronic device fine-tunes the large language model, the large language model has the multi-label text classification capability and text-to-SQL capability for data in a specific field, and the large language model can be deployed to generate corresponding SQL statements based on the target text of the query. Specifically, the large language model can be deployed through the llama-factory framework. After the deployment is completed, the URL requesting the large language model service will be exposed to the outside world. The URL carries the IP and port of the large language model, so that the large language model can be directly called through the URL to perform the text-to-SQL task.
[0111] Based on this, in an embodiment of the present application, when calling a large language model, the URL of the large language model for generating SQL statements that is saved in advance can be obtained first, and the large language model can be called according to the URL. Wherein, when calling the large language model, an SQL statement generation instruction for the text to be recognized is carried, and the SQL statement generation instruction for the text to be recognized carries the text to be recognized. After receiving the text to be recognized, the electronic device performs standardization processing on the text to be recognized. The standardization processing mainly removes some garbled characters and reduces the interference of these characters on the large language model.
[0112] In order to further improve the accuracy of SQL statement generation, on the basis of the above embodiments, in the embodiment of the present application, the large language model is the Qwen model.
[0113] The large language model used in the sentence generation method provided in the embodiment of the present application is the Qwen model, and may also be other large language models, which is not limited here.
[0114] Figure 4 The reasoning flow chart of the large language model provided in the embodiment of the present application is as follows: Figure 4 As shown, the process includes: inputting question text; calling a large language model to perform multi-label text classification to determine the table name corresponding to the question text; calling a large language model to generate an SQL statement based on the table name.
[0115] In addition, the sentence generation method provided in the embodiment of the present application can be applied to the front-end question and answer page. Specifically, the user enters the question text on the front-end question and answer page, and the electronic device converts the question text into an SQL statement through the sentence generation method of the present application, and then determines the reply text corresponding to the question text based on the SQL statement, and displays the reply text on the front-end question and answer page.
[0116] Figure 5 A schematic diagram of a front-end question-and-answer page provided in an embodiment of the present application, such as the Figure 5As shown, the user enters the question text "How much did the TV sell for" on the front-end question and answer page, and the electronic device determines that the reply text corresponding to the question text is "The sales volume of the TV is 200 million yuan" based on the sentence generation method provided in this application.
[0117] Among them, if the questions asked on the front-end question and answer page cover many areas, such as asking about sales data, inventory data, and employee information, then it is likely that multiple table queries will be required. In this case, the classification process of this application can be used to determine multiple tables to be queried.
[0118] The embodiment of the present application proposes a fine-tuning method for a large language model to perform multi-table cascade query. By allowing the large language model to determine which tables need to be queried for dynamic query, it is not necessary for the large language model to input the entire table structure every time, thereby avoiding prompt truncation or errors in determining the required table, resulting in SQL errors. Traditional fine-tuning methods for large language models to generate SQL are all based on open source data sets or English data sets, which cannot meet the company's special business scenarios well. In response to this situation, the embodiment of the present application proposes a fine-tuning method for the company's special business scenarios, which only requires a small amount of data to fine-tune the large model to a higher accuracy rate.
[0119] Based on the above embodiments, Figure 6 A schematic diagram of the structure of a sentence generation device based on a large language model provided in an embodiment of the present application, the device comprising:
[0120] The classification module 601 is used to input the target text to be processed and the pre-saved classification prompt words into the large language model, and obtain at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table;
[0121] Processing module 602 is used to input the target text, table information of each target data table identified as the target category, and pre-saved SQL statement generation prompt words into the large language model, and obtain the target SQL statement corresponding to the target text output by the large language model.
[0122] In a possible implementation, the classification module 601 is specifically used to obtain a pre-saved classification prompt template, wherein the classification prompt template carries the classification prompt word; add the target text to the first preset field of the classification prompt template, and input the added classification prompt template into the large language model.
[0123] In a possible implementation, the processing module 602 is specifically used to obtain a pre-saved SQL statement generation prompt template, wherein the SQL statement generation prompt template carries the SQL statement generation prompt word; add the target text to the second preset field of the SQL statement generation prompt template, add the table information of each target data table identified as the target category to the third preset field of the SQL statement generation prompt template; and input the added SQL statement generation prompt template into the large language model.
[0124] In a possible implementation, the device further includes:
[0125] The fine-tuning module 603 is specifically used to obtain a pre-saved first sample text for classification and at least one corresponding sample category, and use the first sample text, the sample category and the classification prompt word as a first sample for fine-tuning the large language model; obtain a pre-saved second sample text for SQL statement generation and sample table information of at least one corresponding sample data table and a sample SQL statement, and use the second sample text, the sample table information of at least one sample data table, the sample SQL statement and the SQL statement generation prompt word as a second sample for fine-tuning the large language model; use the first sample and the second sample as a fine-tuning data set, and fine-tune the large language model according to the fine-tuning data set and a preset lora fine-tuning algorithm.
[0126] In a possible implementation, the processing module 602 is further used to receive an input SQL statement generation instruction carrying text to be recognized; clean the text to be recognized according to a preset text cleaning rule, and determine the cleaned text to be recognized as the target text.
[0127] In a possible implementation, the processing module 602 is further configured to obtain a pre-saved uniform resource locator URL of a large language model for generating SQL statements, and call the large language model according to the URL.
[0128] In a possible implementation, the large language model is a Qwen model.
[0129] Based on the above embodiments, the present application also provides an electronic device, Figure 7 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 7 As shown, it includes: a processor 701, a communication interface 702, a memory 703 and a communication bus 704, wherein the processor 701, the communication interface 702, and the memory 703 communicate with each other through the communication bus 704;
[0130] The memory 703 stores a computer program. When the program is executed by the processor 701, the processor 701 executes the steps of the sentence generation method based on the large language model provided in the above embodiments.
[0131] Since the principle of solving the problem by the above electronic device is similar to the sentence generation method based on the large language model, the implementation of the above electronic device can refer to the embodiment of the method, and the repeated parts will not be repeated.
[0132] The communication bus mentioned in the above electronic device can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus. The communication interface 702 is used for communication between the above electronic device and other devices. The memory may include a random access memory (RAM) and may also include a non-volatile memory (NVM), such as at least one disk storage. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0133] The above-mentioned processor can be a general-purpose processor, including a central processing unit, a network processor (Network Processor, NP), etc.; it can also be a digital signal processing processor (Digital Signal Processing, DSP), an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc.
[0134] On the basis of the above embodiments, an embodiment of the present invention further provides a computer-readable storage medium, which stores a computer program executable by a processor. When the program runs on the processor, the processor implements the steps of the sentence generation method based on the large language model provided in the above embodiments.
[0135] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.
[0136] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.
[0137] These computer program instructions may also be stored in a computer readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture including an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0138] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0139] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A sentence generation method based on a large language model, characterized in that: The method comprises: Inputting the target text to be processed and the pre-saved classification prompt words into the large language model, and obtaining at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table; The target text, table information of each target data table identified as the target category, and pre-saved SQL statement generation prompt words are input into the large language model, and a target SQL statement corresponding to the target text output by the large language model is obtained.
2. The method according to claim 1, characterized in that The step of inputting the target text to be processed and the pre-saved classification prompt words into the large language model includes: Acquire a pre-saved classification prompt template, wherein the classification prompt template carries the classification prompt word; The target text is added to the first preset field of the classification prompt template, and the classification prompt template after adding is input into the large language model.
3. The method according to claim 1, characterized in that The step of inputting the target text, the table information of each target data table identified as the target category, and the pre-saved SQL statement generation prompt words into the large language model comprises: Acquire a pre-saved SQL statement generation prompt template, wherein the SQL statement generation prompt template carries the SQL statement generation prompt word; Adding the target text to a second preset field of the SQL statement generation prompt template, and adding table information of each target data table identified as the target category to a third preset field of the SQL statement generation prompt template; The added SQL statement generation prompt template is input into the large language model.
4. The method according to claim 1, characterized in that: The fine-tuning process of the large language model includes: Acquire a first sample text and at least one corresponding sample category that are saved in advance for classification, and use the first sample text, the sample category and the classification prompt word as a first sample for fine-tuning the large language model; Acquire a pre-saved second sample text for generating an SQL statement and corresponding sample table information of at least one sample data table and a sample SQL statement, and use the second sample text, the sample table information of the at least one sample data table, the sample SQL statement and the SQL statement generation prompt word as a second sample for fine-tuning the large language model; The first sample and the second sample are used as fine-tuning data sets, and the large language model is fine-tuned according to the fine-tuning data sets and a preset lora fine-tuning algorithm.
5. The method according to claim 1, characterized in that Before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes: Receive an input SQL statement generation instruction carrying text to be recognized; The text to be recognized is cleaned according to a preset text cleaning rule, and the cleaned text to be recognized is determined as the target text.
6. The method according to claim 1, characterized in that Before inputting the target text to be processed and the pre-saved classification prompt words into the large language model, the method further includes: A pre-saved uniform resource locator URL of a large language model for generating SQL statements is obtained, and the large language model is called according to the URL.
7. The method according to any one of claims 1 to 6, characterized in that: The large language model is the Qwen model.
8. A sentence generation device based on a large language model, characterized in that: The device comprises: A classification module, used to input the target text to be processed and the pre-saved classification prompt words into the large language model, and obtain at least one target category corresponding to the target text output by the large language model; wherein each category is an identifier of a data table; The processing module is used to input the target text, the table information of each target data table identified as the target category, and the pre-saved SQL statement generation prompt words into the large language model, and obtain the target SQL statement corresponding to the target text output by the large language model.
9. An electronic device, characterized in that: The electronic device comprises at least a processor and a memory, and the processor is used to implement the steps of the sentence generation method based on a large language model as described in any one of claims 1 to 7 when executing a computer program stored in the memory.
10. A computer-readable storage medium, characterized in that: It stores a computer program, which, when executed by a processor, implements the steps of the sentence generation method based on a large language model as described in any one of claims 1 to 7.