Prompt word generation method and device
The method and device address the issue of truncated prompt words in complex SQL queries by selecting relevant fields and values using similarity algorithms and domain knowledge, improving SQL query accuracy.
Patent Information
- Application Number
- CN202411078930.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-07
- Publication Date
- 2024-12-10
AI Technical Summary
Existing language models face inaccuracies in SQL queries due to the length of prompt words exceeding thresholds when dealing with complex multi-table queries in vertical business areas, leading to truncated and ineffective queries.
A method and device that generate prompt words by selecting relevant fields and values using similarity algorithms, constructing dynamic prompt words that focus on target fields and values, and incorporating business domain knowledge to improve accuracy.
The solution shortens prompt word length and enhances SQL query accuracy by focusing on relevant fields and values, ensuring precise and effective SQL statement generation.
Smart Images

Figure CN119106048A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a prompt word generation method and device. Background Art
[0002] When the existing language model executes the text2sql task and performs SQL query, it is usually based on a single table SQL query, and the table structure of the data table is simple and contains a small number of fields. The common method is to build a static prompt word based on the table structure information of the data table, write the table structure of each data table into the prompt word, and control the language model to generate the corresponding SQL statement according to the prompt word.
[0003] However, for vertical business areas, SQL queries are usually multi-table queries, and the table structure of each data table is complex, with the number of fields ranging from as few as 10 to as many as hundreds. If the table structure information of each data table is written into the prompt word in the traditional way, the length of the prompt word may exceed the threshold, causing the prompt word to be truncated, which in turn leads to inaccurate SQL query results. Summary of the invention
[0004] The present application provides a prompt word generation method and device, which are used to solve the problem in the prior art that when performing SQL query, the prompt word is too long, resulting in inaccurate SQL query effect.
[0005] In a first aspect, an embodiment of the present application provides a method for generating a prompt word, the method comprising:
[0006] Get each field in the target data table to be queried;
[0007] Determine, according to a preset similarity algorithm, a first similarity between each of the fields and the target text to be processed;
[0008] A first field whose corresponding first similarity is higher than a preset first threshold is determined as a target field used when performing an SQL query on the target text, and a target prompt word for performing an SQL query on the target text is constructed based on the target field.
[0009] In a second 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 the prompt word generation method as described above when executing a computer program stored in the memory.
[0010] In the embodiment of the present application, the electronic device obtains each field in the target data table to be queried that is stored; determines the first similarity of each field with the target text to be processed according to a preset similarity algorithm; determines the first field whose corresponding first similarity is higher than the preset first threshold as the target field used when performing an SQL query on the target text, and constructs a target prompt word for performing an SQL query on the target text according to the target field. In the embodiment of the present application, the electronic device determines the first field in the target data table to be queried whose first target similarity with the target text to be processed is higher than the preset first threshold by similarity calculation, and determines the first field as the target field used when performing an SQL query on the target text, constructs a target prompt word for performing an SQL query on the target text, avoids writing all the fields in the target data table into the prompt word, shortens the length of the prompt word, and improves the accuracy of the SQL query. BRIEF DESCRIPTION OF THE DRAWINGS
[0011] 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.
[0012] Figure 1 A schematic diagram of a prompt word generation process provided in an embodiment of the present application;
[0013] Figure 2 A flow chart of dynamic prompt word construction provided in the embodiment of the present application;
[0014] Figure 3 A schematic diagram of a fine-tuning platform page provided in an embodiment of the present application;
[0015] Figure 4 A schematic diagram showing a fine-tuning platform page provided in an embodiment of the present application;
[0016] Figure 5 A schematic diagram of the structure of a prompt word generating device provided in an embodiment of the present application;
[0017] Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0018] 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.
[0019] In vertical field text2sql technology, due to the complex business database structure and diverse business scenario domain knowledge, the traditional method of fully building a static prompt in the field, when fine-tuning the vertical field text2sql language model, on the one hand, it will cause the prompt to be too long, causing the language model to be actively truncated, and on the other hand, the language model cannot accurately learn business domain knowledge and complex table structures, resulting in poor fine-tuning of the language model and difficulty in meeting project application requirements.
[0020] Based on this, in order to shorten the length of prompt words and improve the accuracy of SQL queries, an embodiment of the present application provides a prompt word generation method and device.
[0021] In an embodiment of the present application, each field in the target data table to be queried is obtained; according to a preset similarity algorithm, the first similarity of each field with the target text to be processed is determined; the first field whose corresponding first similarity is higher than a preset first threshold is determined as the target field used when performing an SQL query on the target text, and according to the target field, a target prompt word for performing an SQL query on the target text is constructed.
[0022] Figure 1 A schematic diagram of a prompt word generation process provided in an embodiment of the present application, the process includes:
[0023] S101: Acquire each field in the stored target data table to be queried; and determine the first similarity between each field and the target text to be processed according to a preset similarity algorithm.
[0024] A prompt word generation method provided in an embodiment of the present application is applied to an electronic device, which may be a PC or a server.
[0025] Since the table structure of the data table is complex for the text2sql task in the vertical business field, and the number of fields included ranges from less than 10 to several hundred, based on this, in the embodiment of the present application, in order to shorten the length of the prompt word on the one hand, and to make the residual large model focus on the fields related to the target text on the other hand, and to better understand the relationship between the problem and the field, the electronic device needs to perform field recall. That is, the electronic device can filter from each field in the target data table, obtain all fields in the target data table related to the SQL query of the target text, that is, determine the target field used when performing an SQL query on the target text.
[0026] Among them, the field recall in the embodiment of the present application is implemented by similarity threshold retrieval.
[0027] Specifically, the electronic device stores a table field index table of each data table, which contains each field written in the data table. The electronic device can obtain each field in the target data table to be queried based on the table field index table of each data table.
[0028] The electronic device determines the first similarity between each field and the target text to be processed according to a preset similarity algorithm, and determines the target field based on each first similarity. The preset similarity algorithm can be any similarity algorithm for calculating the similarity between two texts, such as a cosine vector similarity algorithm.
[0029] The target data table is a data table used for performing SQL query on the target text.
[0030] S102: Determine a first field whose corresponding first similarity is higher than a preset first threshold as a target field used when performing an SQL query on the target text, and construct a target prompt word for performing an SQL query on the target text according to the target field.
[0031] In the embodiment of the present application, a first threshold is stored in the electronic device for screening a first field having a high similarity with a target text, and a target prompt word for performing an SQL query on the target text is constructed based on the first field.
[0032] Specifically, the electronic device determines each first field whose corresponding first similarity is higher than a preset first threshold value according to the first similarity between each field in the target data table and the target text, and determines each first field as a target field used when performing an SQL query on the target text. The electronic device constructs a target prompt word for performing an SQL query on the target text according to the target field.
[0033] In this embodiment of the present application, the number of target fields is not limited and can be one or more.
[0034] When constructing a prompt word based on the target field, the electronic device can sort the text2sql task instruction, the identifier of the target data table and the target field in a preset order, and determine the sorting result as the prompt word. The text2sql task instruction describes the text2sql task description and is a static module.
[0035] In addition, in the embodiment of the present application, the prompt word may also include a task constraint, which is also a static module and describes additional constraints on the current text2sql task.
[0036] In the embodiment of the present application, by constructing a dynamic text2sql prompt word mechanism, each prompt word only covers the table structure and business knowledge of the data table required for the corresponding text when performing SQL query, and no truncation occurs. In addition, the prompt word can enable the language large model to perform SQL query on the text, and the generated SQL statement has a higher accuracy rate.
[0037] In the embodiment of the present application, the electronic device determines, by means of similarity calculation, a first field in the target data table to be queried whose first target similarity with the target text to be processed is higher than a preset first threshold, and determines the first field as the target field used when performing an SQL query on the target text, and constructs a target prompt word for performing an SQL query on the target text, thereby avoiding writing all the fields in the target data table into the prompt word, shortening the length of the prompt word, and improving the accuracy of the SQL query.
[0038] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiment, in the embodiment of the present application, after the first field whose corresponding first similarity is higher than the preset first threshold is determined as the target field used when performing an SQL query on the target text, the method further includes:
[0039] Determine each second field whose corresponding first similarity is not higher than the first threshold, and determine each third field whose field type is an enumeration type in each second field according to the field type of each field saved in advance;
[0040] For each of the third fields, obtain each enumeration value corresponding to the third field that is saved, and construct an enumeration value string according to each of the enumeration values; and determine a second similarity between the enumeration value string and the target text according to the similarity algorithm;
[0041] Determine a target third field whose corresponding second similarity is higher than a preset second threshold, add the target third field to the target field, and perform a subsequent process of constructing a target prompt word for performing an SQL query on the target text according to the target field.
[0042] Since some fields in the data table may be enumeration type fields, that is, each field corresponds to multiple enumeration values, for example, the administrative division field corresponds to multiple enumeration values, namely, provinces and cities, etc. If the field is recalled based only on the first similarity between the field and the target text, the target field may be missed, which will affect the accuracy of the prompt word generation.
[0043] Based on this, first, in order to further improve the accuracy of prompt word generation and thus improve the accuracy of SQL queries, in an embodiment of the present application, the electronic device can also screen again for each enumeration type field in the target data table whose first similarity with the target text is not higher than the first threshold.
[0044] Specifically, the electronic device determines each second field whose corresponding first similarity is not higher than the first threshold, and determines each third field whose field type is an enumeration type in each second field according to the pre-stored field type of each field.
[0045] For each third field, the electronic device obtains each enumeration value corresponding to the third field stored in the table field index table, and concatenates each enumeration value according to the order in which each enumeration value is written in the table field index table to obtain an enumeration value character string.
[0046] The electronic device determines the second similarity between the enumeration value character string corresponding to each third field and the target text according to the similarity algorithm, determines the target third field whose corresponding second similarity is higher than the preset second threshold, adds the target third field to the target field, and performs a subsequent process of constructing a target prompt word for performing an SQL query on the target text according to the target field.
[0047] Among them, the process of the electronic device determining the second similarity between the enumeration value string corresponding to each third field and the target text according to the similarity algorithm is consistent with the process of the electronic device determining the first similarity between each field and the target text, which will not be repeated here.
[0048] In addition, the second threshold used for screening the target third field is different from the first threshold used for screening the first field.
[0049]
[0050]
[0051] Table 1
[0052] Table 1 is a table field index table provided in an embodiment of the present application, which includes table name, field name, description text, field enumeration value and remarks, wherein the description text is the Chinese explanation of the field, and the remarks are special descriptions of the field.
[0053] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, the target prompt word for performing an SQL query on the target text according to the target field includes:
[0054] Get the saved prompt word template;
[0055] Determine, according to the value corresponding to each field stored in the target data table, a target value used by each target field when performing an SQL query on the target text;
[0056] The value corresponding to each target field is written into the first preset position of the prompt word template, and the prompt word template after writing is determined as the target prompt word; wherein the first preset position is used to fill in the field information used in SQL query.
[0057] In the embodiment of the present application, a prompt word template is stored in the electronic device, and the prompt word template contains a first preset position for filling in the information of the field used in the SQL query. Based on this, when the electronic device constructs the target prompt word corresponding to the target text, the electronic device can obtain the stored prompt word template; and according to the value corresponding to each field stored in the target data table, determine the target value used for each target field when performing SQL query on the target text; write the value corresponding to each target field into the first preset position of the prompt word template, and determine the prompt word template after writing as the target prompt word; wherein the first preset position is used to fill in the information of the field used in the SQL query.
[0058] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, the target value used for each target field when performing an SQL query on the target text is determined according to the value corresponding to each field stored in the target data table, including:
[0059] Acquire the field type of each field saved in advance, and determine each first target field whose field type is an enumeration type and each second target field whose field type is a non-enumeration type in the target field;
[0060] For each of the first target fields, each enumeration value corresponding to the first target field stored in the target data table is obtained; according to the similarity algorithm, a third similarity between each enumeration value of the first target field and the target text is determined; and an enumeration value whose corresponding third similarity is higher than a preset third threshold is determined as a target value used by the first target field when performing an SQL query on the target text;
[0061] For each of the second target fields, the value corresponding to the first target field stored in the target data table is obtained, and the value is determined as the target value used by the second target field when performing an SQL query on the target text.
[0062] Because among the target fields determined by the electronic device, some of the first target fields are of enumeration type, the enumeration value and its meaning of the description field displayed in the prompt word for the first target field of the enumeration type can enable the language model to understand the association between the field value and the text in SQL.
[0063] For example, if the target text is "How many male personnel registered in 2019", the SQL corresponding to the target text is "select count(*)from person where xb='0'and time like'2019%'". In order to make the language model better understand, the electronic device can add a description in the prompt word: "person table field xb-gender-enumeration field, the value range is: 0: male, 1: female".
[0064] However, since the number of enumeration values of the first target field of the enumeration type varies from 2 to 9000+, if all the enumeration values of each first target field of the enumeration type are added to the prompt word, on the one hand, the prompt word will be too long, resulting in passive truncation of the prompt word, and on the other hand, it is not conducive to the large language model to focus on the enumeration values related to the target text. Therefore, value recall is required, that is, it is necessary to determine the target value used in the SQL query of the target text in each enumeration value corresponding to the first target field.
[0065] Specifically, for each first target field whose field type is an enumeration type, the electronic device obtains each enumeration value corresponding to the first target field stored in the target data table. The electronic device determines the third similarity between each enumeration value of the first target field and the target text according to the similarity algorithm, and determines the enumeration value whose corresponding third similarity is higher than the preset third threshold. The electronic device determines the enumeration value as the target value used by the first target field when it is used to perform an SQL query on the target text.
[0066] The process of determining the third similarity by the electronic device is consistent with the process of determining the first similarity and the second similarity in the above embodiment, which will not be described in detail. In addition, the third threshold is different from the first threshold and the second threshold in the above embodiment.
[0067] In addition, in an embodiment of the present application, there is also a second target field in the target field whose field type is a non-enumeration type. For each second target field, the electronic device obtains the value corresponding to the first target field stored in the target data table, and determines the value as the target value used by the second target field when it is used to perform an SQL query on the target text.
[0068] The value recall scheme provided in the embodiment of the present application calculates the similarity values between all enumeration values in the recalled enumeration field and the question, and takes the enumeration values above the threshold as the result of the value recall and adds them to the prompt word.
[0069] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, after writing the value corresponding to each target field into the first preset position of the prompt word template, the method further includes:
[0070] Obtain the correspondence between pre-saved fields and business domain knowledge;
[0071] If it is determined that there is business domain knowledge corresponding to any target field in the corresponding relationship, the business domain knowledge is added to the second preset position of the prompt word template; wherein the second preset position is used to fill in the business domain knowledge used in SQL query.
[0072] For SQL queries in vertical business areas, it is also necessary to instill professional knowledge of the business area into the model to achieve business area tasks that cannot be achieved by general language large models.
[0073] In the embodiment of the present application, by adding the business domain knowledge involved in the target text to the prompt words, the business knowledge is instilled into the language model synchronously when performing SQL queries. On the one hand, the language model can better understand the relationship between the input and output of the target text, and on the other hand, the accuracy of the language model can be improved. If the current stage is the fine-tuning stage of the language model, the convergence effect of the language model can also be improved.
[0074] Through the analysis of text2sql tasks in this field, it is found that there is a strong correlation between business domain knowledge and the fields of the data table. Therefore, the embodiment of the present application proposes to dynamically map the business knowledge associated with the target sample by constructing a mapping table between fields and business domain knowledge and combining the results of field recall.
[0075] Specifically, in the embodiment of the present application, the electronic device obtains the correspondence between the pre-saved fields and the business domain knowledge, that is, obtains the mapping table between the pre-configured fields and the business domain knowledge. The electronic device queries the correspondence, and if the electronic device determines that there is business domain knowledge corresponding to any target field in the correspondence, the business domain knowledge is added to the second preset position of the prompt word template; wherein the second preset position is used to fill in the business domain knowledge used in the SQL query.
[0076]
[0077]
[0078] Table 2
[0079] Table 2 is a mapping table of fields and business domain knowledge provided in an embodiment of the present application, which includes table name, field name, description text and business knowledge.
[0080] Based on this, in an embodiment of the present application, when the electronic device uses business domain knowledge when performing SQL queries on the target text, it can also perform queries based on the table name of the target data table, the field name of the target field, and the description text of the target field.
[0081] Specifically, in the embodiment of the present application, the target prompt word may include four parts, namely: text2sql task instructions, table structure information of the target data table, business knowledge and task constraints. The text2sql task instruction describes the text2sql task description and is a static module; the table structure information of the target data table is a dynamic module, which needs to be based on the target text and sequentially through the three steps of table recall, field recall and value recall to obtain the target data table, target field and target value corresponding to the table structure information of the target data table; business knowledge is a dynamic module, which also needs to be based on the target text through the business knowledge recall strategy to obtain the target business knowledge corresponding to the target text; task constraints are static modules, which describe the additional constraints on the current text2sql task.
[0082] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, if the current stage is the fine-tuning stage of the language large model, the method further includes:
[0083] Obtaining a sample SQL statement corresponding to the saved target text;
[0084] The target prompt word, the target text and the sample SQL statement are input into the language macro model, and the language macro model is fine-tuned by a preset fine-tuning algorithm.
[0085] In the embodiments of the present application, the generation of prompt words is widely used, for example, it can be used to fine-tune a large language model.
[0086] Specifically, if the current stage is fine-tuning the language model, the electronic device obtains a sample SQL statement corresponding to the saved target text; the electronic device inputs the target prompt word, target text and sample SQL statement into the language model, and fine-tunes the language model through a preset fine-tuning algorithm.
[0087] The embodiment of the present application uses a dynamic prompt word construction mechanism. In the fine-tuning stage of the large language model, the table structure and business-related knowledge of the data table associated with the target text are dynamically introduced through four prompt construction modules, namely, task instructions, table structure information of the associated target data table, associated business knowledge and task constraints. In this way, during the fine-tuning process of the large language model, the prompt words corresponding to the training samples only cover the table structure and business knowledge of the data table required for the corresponding samples without truncation. Moreover, the prompt words can enable the large language model to learn the corresponding content explicitly, and the large language model is easier to converge and has a higher accuracy rate.
[0088] Figure 2 The dynamic prompt word construction flow chart provided in the embodiment of the present application is as follows: Figure 2 As shown, the process includes: obtaining the Text2sql task instruction, determining the table structure information of the associated target data table, obtaining the associated business knowledge, obtaining the task constraints, and constructing prompt words. Among them, determining the table structure information of the associated target data table includes: table recall, field recall, and value recall.
[0089] The following is a description of a specific embodiment:
[0090] If the target text is "What are the percentage increases in the registered numbers of local and non-local drivers in Shibei District compared with last month?", the target SQL statement corresponding to this target text is "SELECT LY,CASE WHEN(SELECTCOUNT(xh)FROM drivinglicense WHERE LY=t.LY AND FZJG IN('Lu B','Lu U')AND xzqh='370203'AND CCLZRQ>=TRUNC(ADD_MONTHS(SYSDATE,-2),'MM')AND CCLZRQ<TRUNC(ADD_MONTHS(SYSDATE,-1),'MM'))=0THEN 0ELSE((SELECT COUNT(xh)FROMdrivinglicense WHERE LY=t.LY AND FZJG IN('Lu B','Lu U')AND xzqh='370203'ANDCCLZRQ>=TRUNC(ADD_MONTHS(SYSDATE,-1),'MM')AND CCLZRQ<TRUNC(SYSDATE,'MM')) / (SELECT COUNT(xh)FROM drivinglicense WHERE LY=t.LY AND FZJG IN('Lu B','Lu U')AND xzqh='370203'AND CCLZRQ>=TRUNC(ADD_MONTHS(SYSDATE,-2),'MM')AND CCLZRQ<TRUNC(ADD_MONTHS(SYSDATE,-1),'MM'))-1)*100END AS percentage_change FROM(SELECT'A'AS LY FROM DUAL UNION ALL SELECT'B'AS LY FROM DUAL)t".
[0091] The target prompt corresponding to this target text includes the Text2sql task instruction, the table structure information of the target data table, business knowledge, and task constraints.
[0092] Among them, the Text2sql task instruction is "You are now a database expert, and your current task is to generate SQL for the Oracle database based on the user input."
[0093] The table structure information of the target data table is as follows:
[0094] ##drivinglicense driver table, fields are as follows:
[0095] ly, driver source, CHAR, enumeration values include: B: out of town, A: local, C: transferred in, E: foreign, D: military and police, F: Hong Kong, China, G: Macau, China, H: Taiwan, China, J: agricultural machinery, I: embassy and consulate;
[0096] xzqh, administrative division, VARCHAR2, this field is a number, the value of Qingdao starts with 3702, Chinese uses like for fuzzy matching, enumeration values include: 370203: Shibei District, Qingdao City, Shandong Province, 340611: Suburbs of Huaibei City, Anhui Province, 110100: Districts of Beijing, 340620: Urban District of Huaibei City, Anhui Province, 420605: Suburbs of Xiangfan City, Hubei Province, 140103: Taiyuan City, Shanxi Province Beicheng District, 340601: Huaibei District, Anhui Province, 130904: Suburban District, Cangzhou City, Hebei Province, 340600: Huaibei City, Anhui Province, 130106: Suburban District, Shijiazhuang City, Hebei Province, 431002: Beihu District, Chenzhou City, Hunan Province, 231181: Bei'an City, Heilongjiang Province, 232602: Bei'an City, Heilongjiang Province, 130603: Beicheng District, Baoding City, Hebei Province, 330206: Beilun District, Ningbo City, Zhejiang Province, etc.;
[0097] zt, driver's license status, VARCHAR2, enumeration values include: B: 12 points, A: normal, R: cancellation and recovery, M: overdue license renewal, I: accident unhandled, H: violation unhandled, C: transfer out, N: extension of license renewal, L: locked, U: detained, G: cancellation, K: seizure, D: temporary seizure, F: revoked, J: stop using, etc.;
[0098] sfbd, whether local, CHAR, enumeration value includes: 0: No, 1: Yes;
[0099] yzjcx, original permitted vehicle type, VARCHAR2, enumeration values include: A3: city bus, D: three-wheeled motorcycle, C1: small car, E: two-wheeled motorcycle, C5: small automatic passenger car for the disabled, C4: three-wheeled car, C2: small automatic car, M: wheeled self-propelled mechanical vehicle, B1: medium-sized passenger car, C3: low-speed truck, B2: large truck, A1: large passenger car, F: light motorcycle, P: tram, A2: tractor, etc.;
[0100] ryzt, personnel status. 0: normal; 1: with drug use record, CHAR, enumeration values include: 4: less than three years after the compulsory isolation and drug rehabilitation measures were lifted, 3: with a record of drug use or injection within three years, 1: with drug use record, 2: in drug rehabilitation treatment, 0: no drug use record;
[0101] xczg, school bus driving qualification 1 yes 0 no, CHAR;
[0102] cclzrq, date of first obtaining the certificate, DATE;
[0103] bzcs, number of re-certifications, NUMBER;
[0104] ccfzjg, initial issuing authority, VARCHAR2, enumeration values include: Beijing L: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing T: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing S: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing R: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing Q: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing O: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing P: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing N: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing M: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing K: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing J: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing H: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing G: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing F: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, Beijing D: Beijing Municipal Public Security Bureau Public Security Traffic Management Bureau Vehicle Management Office, etc.;
[0105] fzjg, issuing authority, VARCHAR2, enumeration values include: Beijing L: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing T: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing S: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing R: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing Q: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing O: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing P: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing N: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing M: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing K: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing J: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing H: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing G: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing F: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, Beijing D: Vehicle Management Office of Beijing Municipal Public Security Bureau Traffic Management Bureau, etc.;
[0106] jzqx, driving license expiration, CHAR, enumeration values include: 4: old license, 5: old expiration, 2: 10-year period, 3: long-term, 1: 6-year period;
[0107] ##frm_code dictionary mapping table, the fields are as follows:
[0108] dmz,code value,VARCHAR2;
[0109] xtlb, system class, CHAR;
[0110] dmlb,code category, VARCHAR2;
[0111] dmsm1, code attribute 1, VARCHAR2;
[0112] ##frm_codetype dictionary mapping table, the fields are as follows:
[0113] lbsm,category description,VARCHAR2;
[0114] xtlb, system category, VARCHAR2;
[0115] dmlb, code category, VARCHAR2.
[0116] Business knowledge:
[0117] The sfbd field in the Drivinglicense table is used to query the local driver information and filter.
[0118] The cclzrq field in the Drivinglicense table, the first license date, DATE, is used to count the number of driver registrations based on time. This field is used for filtering. If this field is compared with a string type date, the TO_DATE function must be used to convert the string type date into a date type format.
[0119] fzjg in the Drivinglicense table is the issuing authority field. This field is used for filtering when counting the number of changes in driving licenses by region. For example, the vehicle rules for Qingdao are as follows: fzjg in ('Lu B', 'Lu U').
[0120] The task constraints are:
[0121] ##Generate SQL statements even when there is no extra explanation / no inquiry / no supplement / no specific information missing
[0122] ##Pay special attention that the fields in the generated SQL must exist in the table
[0123] ##**Always remember that the output must be a SQL statement**
[0124] ##If the date is required, the current date is 2024-05-16 23:59:59
[0125] ##If time is needed, the current time is 2024-05-16 23:59:59
[0126] ##If the user input does not specify the time for querying data, such as last year, this year, etc., it means querying all time in the database. There is no need to set time constraints on time type fields such as sgfssj in the SQL statement.
[0127] In addition, during the fine-tuning stage of the large language model, after the electronic device determines the target prompt word corresponding to the target text, the electronic device can construct a triple corresponding to the target text, including: target text, target SQL statement and target prompt word, and based on the triple, construct the language large model fine-tuning training data format in the alpaca format.
[0128] Moreover, in an embodiment of the present application, in the fine-tuning stage of the language large model, the electronic device can also configure the annotation data set where the target text is located in a platform-based manner, build a fine-tuning platform page, and then the user can select the configured annotation data in the data set window through the fine-tuning platform page. If the selected annotation data is text2sql, then the prompt word generation method of the present application will be triggered, and the corresponding modules of table recall (quantity, recall method), field recall (quantity, recall method), value recall (quantity, recall method), and business knowledge recall (quantity, recall method) will set the default values of the parameters. After the construction is completed, the effect of the training data constructed by the default parameters can be previewed. Moreover, the user can adjust the relevant construction data parameters online according to the construction data effect, realize the real-time adjustment of the constructed training data results, and then conduct language large model training.
[0129] Figure 3 A schematic diagram of a fine-tuning platform page provided in an embodiment of the present application, such as the Figure 3 As shown in the figure, through this fine-tuning platform page, you can select the training stage, data path, dataset, learning rate, and number of training rounds, and you can also preview the currently selected dataset.
[0130] Figure 4 A schematic diagram showing a fine-tuning platform page provided in an embodiment of the present application, such as the Figure 4 As shown, if the user selects the annotated data of text2sql, the prompt word generation method of the present application will be triggered to generate prompt words and display them on the fine-tuning platform page.
[0131] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, if the current stage is the SQL query stage, the method further includes:
[0132] Inputting the target prompt word and the target text into a language macromodel;
[0133] Obtain a target SQL statement corresponding to the target text output by the language model.
[0134] In the embodiment of the present application, the generation of prompt words is widely used, for example, it can be used for SQL query.
[0135] Specifically, if the current stage is SQL query, the electronic device inputs the target prompt word and the target text into the language macro model. The language macro model performs SQL query on the target text according to the target prompt word, determines and outputs the SQL statement corresponding to the target text. The electronic device obtains the target SQL statement corresponding to the target text output by the language macro model.
[0136] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, the process of determining the target data table includes:
[0137] Input the target text into a pre-trained classification model; obtain a first identifier of a first data table output by the classification model and used when performing an SQL query on the target text, and determine the first data table of the first identifier as the target data table; or,
[0138] A sample SQL statement corresponding to the target text is obtained, and a second data table corresponding to a second identifier included in the sample SQL statement is determined as the target data table.
[0139] With the development of technology, how to filter data tables according to text and select data tables suitable for target text is a very necessary task in SQL query.
[0140] Among them, multi-table recall is an indispensable process for screening data tables. The multi-table recall is to perform associated multi-table recall in all data tables in the database, select tables related to the target text, and filter out table information of data tables that are not related to the target text. That is, the electronic device determines the target data table in the database for SQL query on the target text through multi-table recall.
[0141] Specifically, the electronic device can determine the target data table corresponding to the target text through a pre-trained classification model. That is, the electronic device inputs the target text into the pre-trained classification model; obtains the first identifier of the first data table used when performing an SQL query on the target text output by the classification model, and determines the first data table of the first identifier as the target data table. Generally, the first identifier of the first data table is the table name of the first data table.
[0142] The electronic device can also train the classification model based on the pre-saved sample text and the corresponding sample SQL statement. Specifically, the electronic device sample text and the corresponding sample SQL statement are used to extract the table name of the two-tuple corresponding to the sample text and the corresponding sample SQL statement to form a (sample text, SQL statement and table name included in the SQL statement) triple.
[0143]
[0144]
[0145] Table 3
[0146] Table 3 is a triple provided in an embodiment of the present application, which includes a sample text, an SQL statement, and a table name contained in the SQL statement.
[0147] The electronic device can construct a table recall multi-label classification data set through triples, and realize the multi-table recall function by training multiple labels. The input of the multi-label classification model is text, and the output is the table name required for SQL query of the text.
[0148] In the embodiment of the present application, the prompt word generation process can also be applied to the fine-tuning stage of the language model to prompt the convergence speed and effect of the language model. Based on this, the target text for prompt word generation may be a sample text for fine-tuning the language model, and the electronic device stores a sample SQL statement corresponding to the target text.
[0149] Therefore, in the embodiment of the present application, when the electronic device performs multi-table recall, the electronic device can also determine whether the target text is a sample text for fine-tuning the language model. If so, the electronic device obtains a sample SQL statement corresponding to the stored target text. The sample SQL statement carries a second identifier corresponding to the second data table. The electronic device reads the second identifier contained in the sample SQL and determines the second data table corresponding to the second identifier as the target data table.
[0150] In order to shorten the length of the prompt word and improve the accuracy of the SQL query, based on the above embodiments, in the embodiment of the present application, the first similarity between each field and the target text to be processed is determined according to a preset similarity algorithm, including:
[0151] For each field, obtain the saved description text corresponding to the field; determine the first vector corresponding to the description text and the second vector corresponding to the target text; use the cosine similarity algorithm to determine the cosine similarity between the first vector and the second vector, and determine the cosine similarity as the first similarity between the field and the target text.
[0152] In order to improve the accuracy of similarity calculation, in the embodiment of the present application, the electronic device can calculate the first similarity between each field and the target text based on the description text corresponding to each field, wherein the description text is the Chinese explanation corresponding to each field.
[0153] Specifically, for each field, the electronic device obtains the saved description text corresponding to the field; determines the first vector corresponding to the description text and the second vector corresponding to the target text; uses the cosine similarity algorithm to determine the cosine similarity between the first vector and the second vector, and determines the cosine similarity as the first similarity between the field and the target text.
[0154] Based on the above embodiments, Figure 5 A schematic diagram of the structure of a prompt word generating device provided in an embodiment of the present application, the device comprising:
[0155] Processing module 501 is used to obtain each field in the stored target data table to be queried; determine the first similarity between each field and the target text to be processed according to a preset similarity algorithm; determine the first field whose corresponding first similarity is higher than a preset first threshold as the target field used when performing SQL query on the target text;
[0156] The generating module 502 is used to construct a target prompt word for performing an SQL query on the target text according to the target field.
[0157] In a possible implementation, the processing module 501 is further used to determine each second field whose corresponding first similarity is not higher than the first threshold, and determine each third field whose field type is an enumeration type in each second field according to the field type of each field saved in advance; for each third field, obtain each enumeration value corresponding to the saved third field, and construct an enumeration value string according to each enumeration value; determine the second similarity between the enumeration value string and the target text according to the similarity algorithm; determine the target third field whose corresponding second similarity is higher than the preset second threshold, add the target third field to the target field, and perform a subsequent process of constructing a target prompt word for performing an SQL query on the target text according to the target field.
[0158] In a possible implementation, the generation module 502 is specifically used to obtain a saved prompt word template; determine, according to the value corresponding to each field saved in the target data table, a target value used by each target field when performing an SQL query on the target text; write the value corresponding to each target field into a first preset position of the prompt word template, and determine the prompt word template after writing as the target prompt word; wherein the first preset position is used to fill in the information of the field used in the SQL query.
[0159] In a possible implementation, the generation module 502 is specifically used to obtain the field type of each field saved in advance, determine each first target field whose field type is an enumeration type in the target field, and each second target field whose field type is a non-enumeration type; for each first target field, obtain each enumeration value corresponding to the first target field saved in the target data table; determine, according to the similarity algorithm, the third similarity between each enumeration value of the first target field and the target text; determine the enumeration value whose corresponding third similarity is higher than a preset third threshold as the target value used by the first target field when performing an SQL query on the target text; for each second target field, obtain the value corresponding to the first target field saved in the target data table, and determine the value as the target value used by the second target field when performing an SQL query on the target text.
[0160] In a possible implementation, the generation module 502 is also used to obtain the correspondence between pre-saved fields and business domain knowledge; if it is determined that there is business domain knowledge corresponding to any target field in the correspondence, the business domain knowledge is added to the second preset position of the prompt word template; wherein the second preset position is used to fill in the business domain knowledge used when filling in SQL queries.
[0161] In a possible implementation, the processing module 501 is further used to obtain a sample SQL statement corresponding to the saved target text if the current stage is the fine-tuning stage of the language model; input the target prompt word, the target text and the sample SQL statement into the language model, and fine-tune the language model through a preset fine-tuning algorithm.
[0162] In a possible implementation, the processing module 501 is further used to input the target prompt word and the target text into a large language model if the current stage is an SQL query; and obtain a target SQL statement corresponding to the target text output by the large language model.
[0163] In a possible implementation, the processing module 501 is further used to input the target text into a pre-trained multi-label classification model; obtain the first identifier of the first data table output by the multi-label classification model and used when performing an SQL query on the target text, and determine the first data table of the first identifier as the target data table; or, obtain a sample SQL statement corresponding to the saved target text, and determine the second data table corresponding to the second identifier contained in the sample SQL statement as the target data table.
[0164] In a possible implementation, the processing module 501 is specifically used to obtain, for each field, a saved description text corresponding to the field; determine a first vector corresponding to the description text, and a second vector corresponding to the target text; use a cosine similarity algorithm to determine the cosine similarity between the first vector and the second vector, and determine the cosine similarity as the first similarity between the field and the target text.
[0165] Based on the above embodiments, the present application also provides an electronic device, Figure 6 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application is shown in FIG. Figure 6 As shown, it includes: a processor 601, a communication interface 602, a memory 603 and a communication bus 604, wherein the processor 601, the communication interface 602, and the memory 603 communicate with each other through the communication bus 604;
[0166] The memory 603 stores a computer program. When the program is executed by the processor 601, the processor 601 executes the steps of the prompt word generation method provided in the above embodiments.
[0167] Since the principle of solving the problem by the above electronic device is similar to the prompt word generating method, the implementation of the above electronic device can refer to the embodiment of the method, and the repeated parts will not be repeated.
[0168] 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 602 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.
[0169] 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.
[0170] 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 prompt word generation method provided in the above embodiments.
[0171] 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.
[0172] 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.
[0173] 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 comprising 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.
[0174] 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.
[0175] 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 prompt word generation method, It is characterized in that The method comprises: Get each field in the target data table to be queried; Determine, according to a preset similarity algorithm, a first similarity between each of the fields and the target text to be processed; A first field whose corresponding first similarity is higher than a preset first threshold is determined as a target field used when performing an SQL query on the target text, and a target prompt word for performing an SQL query on the target text is constructed based on the target field.
2. The method according to claim 1, It is characterized in that After determining the first field whose corresponding first similarity is higher than a preset first threshold as the target field used when performing an SQL query on the target text, the method further includes: Determine each second field whose corresponding first similarity is not higher than the first threshold, and determine each third field whose field type is an enumeration type in each second field according to the field type of each field saved in advance; For each of the third fields, obtain each enumeration value corresponding to the third field that is saved, and construct an enumeration value string according to each of the enumeration values; and determine a second similarity between the enumeration value string and the target text according to the similarity algorithm; Determine a target third field whose corresponding second similarity is higher than a preset second threshold, add the target third field to the target field, and perform a subsequent process of constructing a target prompt word for performing an SQL query on the target text according to the target field.
3. The method according to claim 1, It is characterized in that The step of constructing a target prompt word for performing an SQL query on the target text according to the target field includes: Get the saved prompt word template; Determine, according to the value corresponding to each field stored in the target data table, a target value used by each target field when performing an SQL query on the target text; The value corresponding to each target field is written into the first preset position of the prompt word template, and the prompt word template after writing is determined as the target prompt word; wherein the first preset position is used to fill in the field information used in SQL query.
4. The method according to claim 3, It is characterized in that Determining, according to the value corresponding to each field stored in the target data table, a target value used by each target field when performing an SQL query on the target text comprises: Acquire the field type of each field saved in advance, and determine each first target field whose field type is an enumeration type and each second target field whose field type is a non-enumeration type in the target field; For each of the first target fields, each enumeration value corresponding to the first target field stored in the target data table is obtained; according to the similarity algorithm, a third similarity between each enumeration value of the first target field and the target text is determined; and an enumeration value whose corresponding third similarity is higher than a preset third threshold is determined as a target value used by the first target field when performing an SQL query on the target text; For each of the second target fields, the value corresponding to the first target field stored in the target data table is obtained, and the value is determined as the target value used by the second target field when performing an SQL query on the target text.
5. The method according to claim 3, It is characterized in that After writing the value corresponding to each target field into the first preset position of the prompt word template, the method further includes: Obtain the correspondence between pre-saved fields and business domain knowledge; If it is determined that there is business domain knowledge corresponding to any target field in the corresponding relationship, the business domain knowledge is added to the second preset position of the prompt word template; wherein the second preset position is used to fill in the business domain knowledge used in SQL query.
6. The method according to claim 1, It is characterized in that If the current stage is the fine-tuning stage of the large language model, the method further includes: Obtaining a sample SQL statement corresponding to the saved target text; The target prompt word, the target text and the sample SQL statement are input into the language macro model, and the language macro model is fine-tuned by a preset fine-tuning algorithm.
7. The method according to claim 1, It is characterized in that If the current stage is the SQL query stage, the method further includes: Inputting the target prompt word and the target text into a language macromodel; Obtain a target SQL statement corresponding to the target text output by the language model.
8. The method according to claim 1, It is characterized in that The process of determining the target data table includes: Input the target text into a pre-trained classification model; obtain a first identifier of a first data table output by the classification model and used when performing an SQL query on the target text, and determine the first data table of the first identifier as the target data table; or, A sample SQL statement corresponding to the target text is obtained, and a second data table corresponding to a second identifier included in the sample SQL statement is determined as the target data table.
9. The method according to claim 1, It is characterized in that Determining the first similarity between each field and the target text to be processed according to a preset similarity algorithm includes: For each field, obtain the saved description text corresponding to the field; determine the first vector corresponding to the description text and the second vector corresponding to the target text; use the cosine similarity algorithm to determine the cosine similarity between the first vector and the second vector, and determine the cosine similarity as the first similarity between the field and the target text.
10. An electronic device, It is 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 prompt word generation method according to any one of claims 1 to 9 when executing a computer program stored in the memory.
Citation Information
Patent Citations
Question recommendation method and device based on field similarity calculation and server
CN111553151A
Data processing method and device, medium and computing equipment
CN117539893A
SQL (Structured Query Language) statement generation method and device based on large language model, equipment and medium
CN117743371A
Training method of statement generation model, statement generation method, system and equipment
CN117992791A
Data processing method, system and device, storage medium and program product
CN118132684A
