SQL statement generation method and device

By acquiring and fusing multi-dimensional information of data tables in the database, it solves the ambiguity and diversity problems when converting natural language query requests into SQL statements, and improves the accuracy and stability of SQL statements.

CN118377783BActive Publication Date: 2024-09-17ZHUO SHI ZHI XING (QINGTIAN) YUAN UNIVERSE TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202410842799.2
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-06-27
Publication Date
2024-09-17
Estimated Expiration
2044-06-27

AI Technical Summary

Technical Problem

When the existing technology automatically converts natural language query requests into SQL statements, it is difficult to adapt to the ambiguity and diversity of query requests, resulting in low accuracy of the generated SQL statements.

Method used

By obtaining the description information, historical query information and historical interaction information of each data table in the database, similarity calculation is used to determine the intermediate data table and candidate interaction information, and SQL statements are generated based on structural information and contextual information to ensure accurate understanding of query requests.

Benefits of technology

It improves the accuracy and stability of SQL statements, provides comprehensive contextual information through multi-dimensional information fusion, and ensures that the generated SQL statements can accurately match the user's query intent.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118377783B
    Figure CN118377783B
Patent Text Reader

Abstract

The present invention provides a method and device for generating SQL statements, and relates to the field of artificial intelligence technology. The method comprises: obtaining a current query request, obtaining description information, historical query information, and historical interaction information of each data table; determining multiple intermediate data tables according to a first similarity and a second similarity; guiding a screening language model to determine a candidate data table from multiple intermediate data tables based on the current query request and the structural information of the intermediate data table; determining multiple candidate interaction information from the historical interaction information based on a third similarity; and generating an SQL statement corresponding to the current query request based on the candidate data table, the multiple candidate interaction information, and the current query request. The present invention comprehensively characterizes the data table based on the description dimension, the historical query dimension, and the historical interaction dimension, and can provide accurate and comprehensive context information by fusing the search results under different dimensions, thereby ensuring the accuracy of the generation of SQL statements.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a method and device for generating SQL statements. Background Art

[0002] Structured Query Language (SQL) is a database query and programming language that can be used to access data, query, update, and manage databases. Querying a database usually requires users to have certain SQL knowledge, which poses a challenge to non-technical users. In order to improve the accessibility and user experience of the database, the user's query request can be automatically converted into SQL statements, thereby lowering the threshold for users to interact with the database.

[0003] Text2sql can automatically convert natural language query requests into SQL statements. Due to the ambiguity and diversity of natural language, users can express the same query intention in a variety of different ways. Therefore, when generating SQL statements, it is necessary to accurately capture the semantic requirements in the query request and accurately find the data tables related to the query request from the database.

[0004] The study found that existing technologies usually use the structural information of data tables to match query requests to find data tables related to query requests. Due to the large difference in the feature domains of structural information and query requests, it is difficult to adapt to the ambiguity and diversity of natural language query requests, resulting in low accuracy of the generated SQL statements. Summary of the invention

[0005] In view of the above problems, the object of the present invention is to provide a method and device for generating SQL statements, which can accurately convert query requests into SQL statements.

[0006] In order to solve the above technical problems, the present invention provides the following technical solutions:

[0007] On the one hand, a method for generating a SQL statement is provided, comprising:

[0008] Get the current query request;

[0009] Obtaining information to be used for each data table in the database, wherein the information to be used includes description information, historical query information, and historical interaction information;

[0010] Determining a plurality of intermediate data tables from the database according to a first similarity and a second similarity, wherein the first similarity is a similarity between the current query request and the description information, and the second similarity is a similarity between the current query request and the historical query information;

[0011] Based on the current query request and the structural information of the multiple intermediate data tables, guiding the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables;

[0012] Determine a plurality of candidate interaction information from the historical interaction information based on a third similarity, wherein the third similarity is a similarity between the current query request and the historical interaction information;

[0013] Based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request, an SQL statement corresponding to the current query request is generated.

[0014] Optionally, the first similarity includes semantic similarity, the second similarity includes semantic similarity and text similarity, and determining a plurality of intermediate data tables from the database according to the first similarity and the second similarity includes:

[0015] Determining a plurality of target description information from the plurality of description information according to the semantic similarity between the current query request and each of the description information;

[0016] Determining a plurality of first query information from the plurality of historical query information according to the semantic similarity between the current query request and each piece of historical query information;

[0017] Determining a plurality of second query information from the plurality of historical query information according to the text similarity between the current query request and each historical query information;

[0018] In combination with the plurality of target description information, the plurality of first query information and the plurality of second query information, a plurality of intermediate data tables are determined from the database.

[0019] Optionally, the determining a plurality of intermediate data tables from the database by combining the plurality of target description information, the plurality of first query information, and the plurality of second query information includes:

[0020] Merging the target description information, the first query information, and the second query information to obtain information to be sorted;

[0021] Rearranging and filtering the information to be sorted to obtain an information sequence;

[0022] Determine a data table to be used from the database based on metadata corresponding to each information in the information sequence;

[0023] Deduplication processing is performed on the data table to be used to obtain multiple intermediate data tables.

[0024] Optionally, guiding the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables based on the current query request and the structural information of the multiple intermediate data tables includes:

[0025] Obtaining structural information corresponding to each of the intermediate data tables;

[0026] Generate a screening prompt word based on the current query request and the structural information corresponding to all the intermediate data tables;

[0027] The screening prompt words are used to guide the screening language model to screen out a preset number of candidate data tables from the multiple intermediate data tables.

[0028] Optionally, the third similarity includes semantic similarity and text similarity, and determining a plurality of candidate interaction information from the historical interaction information based on the third similarity includes:

[0029] Determining a first number of first historical interaction information from the multiple historical interaction information according to the semantic similarity between the current query request and each historical interaction information;

[0030] Determining a second number of second historical interaction information from the multiple historical interaction information according to the text similarity between the current query request and each historical interaction information;

[0031] The first number of first historical interaction information and the second number of second historical interaction information are cleaned to obtain candidate interaction information.

[0032] Optionally, the generating, based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request, a SQL statement corresponding to the current query request includes:

[0033] Obtaining a candidate interaction record corresponding to each candidate interaction information;

[0034] Generate a conversion prompt word based on the preset number of candidate data tables, the structural information corresponding to each of the candidate data tables, the multiple candidate interaction records, and the current query request;

[0035] Based on the conversion prompt word, guiding the conversion language model to convert the current query request into a SQL statement to be verified;

[0036] If the SQL statement to be verified is successfully queried in the database, the SQL statement to be verified is used as the SQL statement corresponding to the current query request;

[0037] If the SQL statement to be verified fails to query the database, the number of retries is obtained and updated to obtain the updated number of retries;

[0038] If the number of retries after the update is less than the preset number, the conversion prompt word is obtained and updated based on the failure information to obtain a new conversion prompt word, and the step of guiding the conversion language model to convert the current query request into a SQL statement to be verified based on the conversion prompt word and subsequent steps are returned to execute;

[0039] If the number of retries after the update is not less than the preset number, the SQL statement to be verified is output.

[0040] Optionally, before obtaining the to-be-used information of each data table in the database, the method further includes:

[0041] Obtain the structural information, historical query records, and historical interaction records of each data table in the database;

[0042] For each of the data tables, determining a specified number of sample data from the data table;

[0043] Based on the structural information of each data table and the sample data, construct a first database, wherein the first database includes description information of each data table;

[0044] Based on the structural information and historical query records of each data table, construct a second database, wherein the second database includes the historical query information of each data table;

[0045] Based on the structural information and historical interaction records of each data table, a third database is established, wherein the third database includes the historical interaction information of each data table.

[0046] Optionally, constructing the first database based on the structural information and sample data of each data table includes:

[0047] For each of the data tables, determining a first prompt word based on the structural information corresponding to the data table and the sample data;

[0048] Based on the first prompt word, guiding the first language model to generate description information corresponding to the data table;

[0049] The description information is associated with the identifier of the data table and stored in the first database.

[0050] Optionally, constructing the second database based on the structural information and historical query records of each data table includes:

[0051] For each data table, perform text extraction on each historical query record corresponding to the data table to obtain a historical query text corresponding to each historical query record;

[0052] Based on the historical query text, deduplication processing is performed on the historical query records corresponding to the data table to obtain cleaned query records;

[0053] For each of the cleaned query records, generating a second prompt word based on the cleaned query record and the structural information of the data table;

[0054] Based on the second prompt word, guiding the second language model to convert the cleaned query record into historical query information;

[0055] The historical query information is associated with the identifier corresponding to the data table and stored in the second database.

[0056] On the other hand, a SQL statement generating device is also provided, for implementing any of the above methods, the device comprising:

[0057] Request acquisition module, used to obtain the current query request;

[0058] An information acquisition module is used to acquire the information to be used of each data table in the database, wherein the information to be used includes description information, historical query information, and historical interaction information;

[0059] an intermediate determination module, configured to determine a plurality of intermediate data tables from the database according to a first similarity and a second similarity, wherein the first similarity is a similarity between the current query request and the description information, and the second similarity is a similarity between the current query request and the historical query information;

[0060] A candidate determination module, configured to guide the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables based on the current query request and the structural information of the multiple intermediate data tables;

[0061] An interaction determination module, configured to determine a plurality of candidate interaction information from the historical interaction information based on a third similarity, wherein the third similarity is a similarity between the current query request and the historical interaction information;

[0062] A generation module is used to generate an SQL statement corresponding to the current query request based on the preset number of candidate data tables, the multiple candidate interaction information and the current query request.

[0063] On the other hand, an electronic device is also provided, comprising a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads instructions from the memory to execute the steps in any one of the SQL statement generation methods provided by the present invention.

[0064] On the other hand, a computer-readable storage medium is also provided, wherein the computer-readable storage medium stores a plurality of instructions, wherein the instructions are suitable for being loaded by a processor to execute the steps in any one of the SQL statement generation methods provided by the present invention.

[0065] On the other hand, a computer program product is also provided, comprising a computer program / instruction, wherein when the computer program / instruction is executed by a processor, the steps in any one of the SQL statement generation methods provided by the present invention are implemented.

[0066] The beneficial effects brought about by the technical solution provided by the present invention include at least:

[0067] In an embodiment of the present invention, multiple intermediate data tables are determined from the database using information on the historical query dimension and the description dimension of the data table, and then candidate data tables that match the current query request are accurately determined based on the structural information of the intermediate data table; multiple candidate interaction information is determined using information on the historical interaction dimension of the data table; finally, the retrieval results on different dimensions are fused to provide accurate and comprehensive contextual information, so that the current query request can be accurately understood and the accuracy of the generated SQL statement can be ensured. BRIEF DESCRIPTION OF THE DRAWINGS

[0068] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, 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 invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.

[0069] Figure 1 It is a schematic diagram of an application scenario of the SQL statement generation method provided by an embodiment of the present invention;

[0070] Figure 2 It is a flowchart of a method for generating a SQL statement provided by an embodiment of the present invention;

[0071] Figure 3 is a schematic diagram of a process for determining a candidate data table provided by an embodiment of the present invention;

[0072] Figure 4 It is a schematic diagram of a process for generating an SQL statement provided by an embodiment of the present invention;

[0073] Figure 5It is a structural diagram of a SQL statement generating device provided by an embodiment of the present invention;

[0074] Figure 6 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0075] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0076] It is understandable that in the specific implementation of the present invention, data related to user information, etc., needs to obtain user permission or consent, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.

[0077] For example, see Figure 1 , shows a schematic diagram of an application scenario of the SQL statement generation method. The application scenario may include a terminal 101 and a server 102, and data can be exchanged between the terminal 101 and the server 102 via a network. The terminal 101 may be a mobile phone, a tablet computer, a smart Bluetooth device, a computer, or other devices; the server 102 may be a single server or a server cluster consisting of multiple servers.

[0078] Among them, the current query request can be sent by the terminal 101 to the server 102, so that the server 102 can obtain the current query request; the server 102 continues to obtain the description information, historical query information and historical interaction information of each data table in the database; according to the first similarity and the second similarity, multiple intermediate data tables are determined; based on the current query request and the structural information of the intermediate data table, the screening language model is guided to determine the candidate data table from the multiple intermediate data tables; based on the third similarity, multiple candidate interaction information is determined from the historical interaction information; based on the candidate data table, the candidate interaction information and the current query request, an SQL statement corresponding to the current query request is generated.

[0079] Then, the server 102 can send the SQL statement to the terminal 101 according to the corresponding rules, so as to display the generated SQL statement to the user. In this embodiment, a method for generating a SQL statement is provided, such as Figure 2 As shown, the specific process of the SQL statement generation method can be as follows:

[0080] S110: Get the current query request.

[0081] A query request refers to a request actively initiated by a user in a question-and-answer scenario. For example, a question-and-answer system may provide an interactive interface through which a user may send a query request to a server. The query request may be in a variety of forms, such as voice, text, or multimodal. The current query request refers to the query request currently received, i.e., the query request that needs to be processed currently.

[0082] When the server obtains the current query request, it can convert the current query request into text form for subsequent processing. For example, when the current query request is in voice form, the current query request in text form can be obtained through voice-to-text technology. If the current query request is in multimodal form, that is, the current query request contains at least one of text, image, and voice, the corresponding conversion technology can be used to convert the current query request into text.

[0083] S120: Obtain the information to be used of each data table in the database.

[0084] Multiple data tables may be stored in a database. The information to be used refers to the information required for subsequent use obtained after data analysis and mining of the data tables in advance. The information to be used may include description information, historical query information, and historical interaction information.

[0085] The description information refers to the description of the basic content of the data table, which may include the introduction of the data table, the data contained in the data table, and the types of questions that the data table can answer. The historical query information refers to the information of all query records related to the data table in the historical time period. The historical query information may include the purpose of the query, the business that may be involved, and other information. The historical interaction information refers to the information of the successful interaction records related to the data table in the historical time period. Successful interaction means that the query request is received and the SQL statement corresponding to the query request is returned. The historical time period refers to a period of time before the current time. The length of time can be set according to actual needs, for example, one week before the current time, one month before the current time, etc.

[0086] The description information, historical query information, and historical interaction information of each data table in the database can be pre-captured and stored in the designated database so that it can be directly captured from the designated database when needed. For example, the description information of the data table can be stored in the first database in association with the identifier corresponding to the data table, the historical query information of the data table can be stored in the second database in association with the identifier corresponding to the data table, and the historical interaction information of the data table can be stored in the third database in association with the identifier of the data table.

[0087] Thus, the description information of the data table can be directly obtained from the first database, the historical query information of the data table can be obtained from the second database, and the historical interaction information of the data table can be obtained from the third database.

[0088] Before obtaining the information to be used in the data table, each data table can be analyzed and mined to construct the three databases. In some implementations, the structural information, historical query records, and historical interaction records of each data table in the database can be obtained; for each of the data tables, a specified number of sample data are determined from the data table; based on the structural information of each data table and the sample data, a first database is constructed, the first database includes description information of each data table; based on the structural information and historical query records of each data table, a second database is constructed, the second database includes historical query information of each data table; based on the structural information and historical interaction records of each data table, a third database is constructed, the third database includes historical interaction information of each data table.

[0089] The structural information of a data table refers to the schema information corresponding to the data table, that is, the rules and properties of the table structure of the data table. Historical query records refer to query records associated with the data table in the historical time period, and historical interaction records refer to successful query records associated with the data table in the historical time period. Historical query records may include historical query requests, and historical interaction records may include historical query requests and historical SQL statements corresponding to the historical query requests.

[0090] For each data table, a specified number of data can be sampled from the data table based on the sampling rules as sample data. The sampling rules and the specified number can be set according to actual needs. In an embodiment of the present invention, the specified number can be 3 and the sampling rule can be random sampling.

[0091] The first database can be constructed using the structural information of each data table and the sample data sampled therefrom. The first data table can store description information corresponding to each data table. Optionally, when constructing the first database, a first prompt word can be determined for each data table based on the structural information corresponding to the data table and the sample data; the first language model is guided to generate description information corresponding to the data table based on the first prompt word; and the description information is associated with the identifier of the data table and stored in the first database.

[0092] For each data table, the first prompt word can be determined based on the structure information and sample data of the data table. For example, a first prompt template can be obtained, and the first prompt template can include slots to be filled, such as structure information slots and sample slots. For example, {schema} is a structure information slot, {item} is a sample slot, and {schema} is a sample slot.sample} is a sample slot. Fill the structure information corresponding to the data table into the structure information slot, and fill the specified number of sample data into the sample slot to obtain the first prompt word.

[0093] Then, the first prompt word may be input into the first language model so that the first language model can generate description information corresponding to the data table. The first language model may refer to a large language model.

[0094] In order to facilitate the subsequent search for the description information corresponding to the data table, the description information and the identifier of the data table can be stored in association, wherein the identifier of the data table can be the table name, that is, the table name is stored in the first database as the metadata of the description information. As an implementation method, the description information can be stored in the first database in the form of a vector. For example, after obtaining the description information, the description information can be converted into a description vector using a vectorization model and then stored in the first database. The vectorization model can be a bag of words model, Word2Vec, BERT, etc.

[0095] Based on the structural information and historical query records of each data table, a second database is constructed, and the second database includes the historical query information of each data table. Optionally, when constructing the second database, for each data table, text extraction is performed on each historical query record corresponding to the data table to obtain the historical query text corresponding to each historical query record; based on the historical query text, the historical query record corresponding to the data table is deduplicated to obtain a cleaned query record; for each cleaned query record, a second prompt word is generated based on the cleaned query record and the structural information of the data table; based on the second prompt word, the second language model is guided to convert the cleaned query record into historical query information; the historical query information is associated with the identifier corresponding to the data table and stored in the second database.

[0096] For each data table, there may be multiple historical query records. For each historical query record of the data table, text extraction can be performed on the historical query record to obtain the corresponding historical query text. When performing text extraction, special symbols in the historical query record, such as punctuation marks, spaces, etc., can be removed to obtain the historical query text. For example, the historical query record is "hello, world", and the historical query text is "helloworld".

[0097] Based on the historical query text, the historical query records corresponding to the data table can be deduplicated to obtain cleaned query records. As an implementation method, each historical query text can be converted into a hash value using a hash algorithm. If there is an identical hash value, it can be determined that the two historical query records are duplicated, thereby achieving deduplication and obtaining cleaned query records.

[0098] For each cleaned query record, a second prompt word is generated based on the cleaned query record and the structural information of the data table. Similarly, a second prompt template can be obtained first, and the cleaned query record and the structural information of the corresponding data table can be filled into the corresponding slot in the second prompt template to generate the second prompt word. Then, the second prompt word is input into the second language model to convert the cleaned query record into historical query information. The second language model can refer to a large language model.

[0099] The historical query information and the identifier corresponding to the data table are then associated and stored in the second database, wherein the identifier corresponding to the data table may be the table name of the data table, that is, the table name is stored in the second database as metadata of the historical query information. In some embodiments, the historical query information may be stored in the second database in the form of a vector. For example, after obtaining the historical query information, the vectorization model may be used to convert the historical query information into a historical query vector, and then stored in the second database.

[0100] The historical interaction record contains the user's historical interaction requests and the SQL statements that returned successfully. For example, the historical interaction request is: How many days does Zhang San have for his annual leave? The SQL statement that returned successfully is: SELECT annual_leave_balanceFROM employees WHERE name = 'Zhang San'. When building a third database, the historical interaction requests can be directly used as historical interaction information, and the identifier of the corresponding data table and the SQL statement that returned successfully can be used as metadata and stored in the third database in association. Similarly, the vectorized model can be used to convert the historical interaction information into a historical interaction vector and then stored in the third database.

[0101] For each data table, the data table can be characterized from three dimensions: description information, historical query information, and historical interaction information. The data table can be comprehensively characterized, and the description information, historical query information, and historical interaction information have little difference from the feature domain of the current query request, which can ensure the accuracy of subsequent calculations and thus improve the accuracy of SQL statement generation.

[0102] S130: Determine a plurality of intermediate data tables from the database according to the first similarity and the second similarity.

[0103] The first similarity refers to the similarity between the current query request and the description information of the data table, and the second similarity refers to the similarity between the current query request and the historical query information of the data table. Using the first similarity and the second similarity, multiple intermediate data tables can be determined from the database. Figure 3 , shows a schematic diagram of the process of determining the candidate data table, and the following will be combined with Figure 3, for detailed explanation.

[0104] In some embodiments, the first similarity may include semantic similarity, and the second similarity may include semantic similarity and text similarity. When determining the intermediate data table, multiple target description information may be determined from the multiple description information according to the semantic similarity between the current query request and each of the description information; multiple first query information may be determined from the multiple historical query information according to the semantic similarity between the current query request and each of the historical query information; multiple second query information may be determined from the multiple historical query information according to the text similarity between the current query request and each of the historical query information; and multiple intermediate data tables may be determined from the database in combination with the multiple target description information, the multiple first query information, and the multiple second query information.

[0105] If the first database stores the description information in vector form, the description vector can be directly obtained from the first database. If the description information is not in vector form, the description information is converted into a description vector using a vectorization model, and the current query request is also converted into a current query vector using a vectorization model.

[0106] Then the cosine similarity between the current query vector and each description vector can be calculated to obtain the semantic similarity, wherein the greater the cosine similarity, the greater the semantic similarity, indicating that the two vectors are more semantically similar. The description information is sorted in descending order of semantic similarity, and then the top K1 description information with the highest order is selected to obtain multiple target description information. Among them, K1 is a positive integer, and the specific value can be set according to actual needs, and no specific limitation is made here.

[0107] Similarly, by calculating the cosine similarity between the current query vector and each historical query vector, the semantic similarity between the current query request and each historical query information can be obtained. Sort each historical query information in descending order of semantic similarity, and then select the top K2 historical query information from them to obtain multiple first query information. K2 is a positive integer, and the specific value can be set according to actual needs, and is not specifically limited here.

[0108] When calculating the text similarity between the current query request and each historical query information, the current query request and each historical query information can be preprocessed by performing word segmentation, removing stop words, etc. For each word segmentation in the current query request, the word frequency of the word segmentation in the current query request and the word frequency of the word segmentation in each historical query information are calculated; then for each word segmentation in the current query request, the inverse document frequency of the word segmentation is calculated. Finally, the text similarity is calculated according to the following formula:

[0109] ;

[0110] Among them, Score(Doc,Query) represents the text similarity between the historical query information and the current query request; n represents the number of word segments in the current query request; IDF(q i ) represents the participle q i The inverse document frequency of f(q i ,Doc) represents the participle q i The word frequency in the historical query information; k1 and b are adjustable parameters; |Doc| represents the length of the historical query information; avgdl represents the average length of all historical query information.

[0111] Based on the above method, the text similarity between each historical query information and the current query request can be calculated; the historical query information is sorted in descending order according to the text similarity score, and the top K3 historical query information with the highest order are selected to obtain multiple second query information. Among them, K3 is a positive integer, and the specific value can be set according to actual needs and is not specifically limited here.

[0112] It should be noted that the first query information is retrieved through semantic similarity, and the second query information is retrieved through text similarity. When the current query request contains some domain-specific words or uncommon English abbreviations, the first query information may not be accurately retrieved through semantic similarity, but the second query information can be retrieved based on text similarity. Therefore, combining the two retrieval methods can effectively improve the retrieval hit rate and avoid omissions.

[0113] Finally, multiple target description information, multiple first query information, and multiple second query information are combined to determine multiple intermediate data tables from the database. Optionally, the target description information, the first query information, and the second query information are combined to obtain information to be sorted; the information to be sorted is rearranged and filtered to obtain an information sequence; based on the metadata corresponding to each information in the information sequence, a data table to be used is determined from the database; and the data table to be used is deduplicated to obtain multiple intermediate data tables.

[0114] Among them, multiple target description information, multiple first query information and multiple second query information can be merged first to obtain information to be sorted. Then, the re-ranking model is used to re-rank the information to be sorted based on the correlation between the current query request and each piece of information to be sorted, wherein the greater the correlation, the higher the ranking. At the same time, the re-ranking model can also filter out the information to be sorted whose correlation is lower than the specified threshold to obtain the final information sequence. Among them, the information with greater correlation in the information sequence is ranked higher, so that the model can pay more attention to the information with high correlation during subsequent model processing. The specified threshold can be set according to actual needs. For example, if the specified threshold is set to 0.7, the re-ranking model can calculate the correlation, and then filter out the information with correlation lower than 0.7, and then sort it. Based on the re-ranking model, different retrieval results can be effectively integrated to improve the accuracy of subsequent generated SQL statements.

[0115] For each information in the information sequence, the metadata corresponding to the information can be queried from the corresponding database. For example, in the aforementioned first database, second database and third database, the metadata all contain the table name of the data table. The corresponding data table can be determined from the database through the table name, and after deduplication processing is performed on the determined data table, multiple intermediate data tables can be obtained.

[0116] S140: Based on the current query request and the structural information of the multiple intermediate data tables, guide the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables.

[0117] In order to further reduce information redundancy, a certain number of candidate data tables can be further determined from the multiple intermediate data tables based on the current query request and the multiple intermediate data tables for subsequent use. As an implementation method, the structural information corresponding to each intermediate data table can be obtained; based on the current query request and the structural information corresponding to all the intermediate data tables, a screening prompt word is generated; and using the screening prompt word, a screening language model is guided to screen a preset number of candidate data tables from the multiple intermediate data tables.

[0118] It is understandable that the intermediate data table is a data table in the database, and the structural information of each intermediate data table can be directly obtained. Then, the current query request and the structural information of all intermediate data tables are used as context to construct a screening prompt word, and then the screening prompt word is input into the screening language model to guide the screening language model to screen out K4 data tables that are most relevant to the current query request from the intermediate data table as candidate data tables. Among them, the preset number is K4, and the specific value can be set according to actual needs, and is not specifically limited here.

[0119] S150: Determine a plurality of candidate interaction information from the historical interaction information based on the third similarity.

[0120] Among them, the third similarity refers to the similarity between the current query request and the historical interaction information, and the third similarity may include semantic similarity and text similarity. When determining multiple candidate interaction information, it can be that according to the semantic similarity between the current query request and each historical interaction information, a first number of first historical interaction information is determined from the multiple historical interaction information; according to the text similarity between the current query request and each historical interaction information, a second number of second historical interaction information is determined from the multiple historical interaction information; the first number of first historical interaction information and the second number of second historical interaction information are cleaned to obtain candidate interaction information.

[0121] The current query vector corresponding to the current query request is obtained, the historical interaction vector corresponding to each historical interaction information is obtained, and the cosine similarity between the current query vector and each historical interaction vector is calculated as the semantic similarity. Then the historical interaction information is sorted in descending order of semantic similarity, and the first number of historical interaction information with the highest order is selected as the first historical interaction information. The first number can be set according to actual needs and is not specifically limited here.

[0122] The method of calculating the text similarity between the current query request and each historical interaction information is similar to the method of calculating the text similarity described above, and will not be described here to avoid repetition. After calculating the text similarity, the historical interaction information can be sorted in descending order of text similarity, and the first second number of historical interaction information with the highest order can be selected to obtain multiple second interaction information. Among them, the second number is a positive integer, and the specific value can be set according to actual needs, and is not specifically limited here.

[0123] It should be noted that when the first historical interaction information and the second historical interaction information are determined, two sequences are essentially obtained. If the two sequences are respectively recorded as the first sequence and the second sequence, the first sequence stores multiple first historical interaction information in sequence; and the second sequence stores multiple second historical interaction information in sequence.

[0124] After merging the first historical interaction information and the second historical interaction information and removing duplicates, a data set can be obtained. For each historical interaction information in the data set, a new sorting parameter can be calculated based on its ranking in the original sequence; finally, each historical interaction information in the data set is reordered according to the new sorting parameter to obtain candidate interaction information. Optionally, the reciprocal of the ranking of the historical interaction information in the original sequence can be calculated to calculate the new sorting parameter.

[0125] S160: Determine a SQL statement corresponding to the current query request based on the preset number of candidate data tables, the plurality of candidate interaction information, and the current query request.

[0126] The candidate data table is the data table that is most relevant to the current query request, and the candidate interaction information is the historical interaction information that is most relevant to the current query request. Based on the candidate data table, candidate interaction information, and the current query request, the SQL statement corresponding to the current query request can be generated. Figure 4 , showing a schematic diagram of the process of generating SQL statements, the following will be combined Figure 4 Provide detailed explanation.

[0127] Optionally, the candidate interaction record corresponding to each candidate interaction information is obtained; based on the preset number of candidate data tables, the structural information corresponding to each candidate data table, the multiple candidate interaction records and the current query request, a conversion prompt word is generated; based on the conversion prompt word, a conversion language model is guided to convert the current query request into a SQL statement to be verified; if the SQL statement to be verified is successfully queried in the database, the SQL statement to be verified is used as the SQL statement corresponding to the current query request; if the SQL statement to be verified fails to be queried in the database, the number of retries is obtained and updated to obtain the number of retries after update; if the number of retries after update is less than the preset number, the conversion prompt word is obtained and updated based on the failure information to obtain a new conversion prompt word, and the step of guiding the conversion language model to convert the current query request into the SQL statement to be verified based on the conversion prompt word and subsequent steps are returned to execute; if the number of retries after update is not less than the preset number, the SQL statement to be verified is output.

[0128] Among them, a database query can be performed according to the table name of the candidate data table to obtain the structural information of each candidate data table. Since the historical interaction information stored in the third database is the historical query request in the historical interaction record, the metadata of the historical interaction information includes the table name of the data table and the historical SQL statement corresponding to the historical query request. The candidate interaction record corresponding to the candidate interaction information can be obtained by using the third database. For example, the candidate interaction record can be queried based on the table name and historical SQL statement of the candidate interaction information. When the historical interaction information in the third database is not a vector, the candidate interaction information and the historical SQL statement in the metadata can be directly spliced ​​to obtain the candidate interaction record.

[0129] The structural information of each data table is used as context, the candidate interaction records are used as examples, and combined with the current query request, a conversion prompt word is generated, and the conversion prompt word is input into the conversion language model to convert the current query request into the SQL to be verified. The conversion prompt word is input into the conversion language model, and the conversion language model will output the corresponding answer. Then, the SQL statement in the answer is parsed using a regular expression to obtain the SQL statement to be verified.

[0130] Then use the SQL to be verified to execute the SQL query in the database. If no error is reported during the execution of the SQL query, that is, the query is successful, it can be considered that the SQL statement to be verified is executable normally. Therefore, the SQL statement to be verified can be used as the SQL statement corresponding to the current query request, and the SQL statement corresponding to the current query request and the corresponding query results can be fed back to the user.

[0131] If an error occurs during the execution of the SQL query, the SQL query fails. At this time, the number of retries can be obtained and updated to obtain the updated number of retries. If the updated number of retries is less than the preset number, it can be considered that there is still a chance to retry, and the failure information corresponding to the query failure can be obtained, and the conversion prompt word can be updated using the failure information. Optionally, when updating the conversion prompt word, the failure information can be added to the conversion prompt word to obtain a new conversion prompt word. Then return to execute the step of inputting the conversion prompt word into the conversion language model so that the conversion language model generates the SQL statement to be verified and the subsequent steps.

[0132] If the number of retries after the update is not less than the preset number, the retry opportunities are exhausted and the SQL statement to be verified can be directly output so that the user can correct the SQL statement to be verified.

[0133] In order to ensure the accuracy of SQL statement generation, after the SQL statement corresponding to the current query request and the corresponding query result are output, the user's feedback on the output can be obtained. For example, the user can express his satisfaction with the output by clicking the like or dislike button; if the user likes, it means that the current query result and SQL statement are satisfactory to the user, and the current query request and the corresponding SQL statement can be stored in the third database. It should be noted that the first database, the second database and the third database can be updated according to a fixed time period, and the results that the user considers satisfactory can be added to the third database to ensure the timeliness of the data, thereby improving the accuracy of the generated SQL statement.

[0134] The SQL statement generation method can be applied in various scenarios, and the database may include multiple data tables. In different scenarios, the data tables and the current query request can be adaptively changed according to actual needs.

[0135] The embodiment of the present invention provides a SQL statement generation method, which comprehensively characterizes the characteristics of the data table from the description dimension of the data table in the database, the historical query dimension and the historical interaction dimension, uses the current query request to search in these multiple dimensions, and integrates the search results under these multiple dimensions to determine the data table that matches the current query request, thereby providing accurate and comprehensive context information when generating SQL statements, ensuring the accuracy and stability of the generation of SQL statements.

[0136] In order to better implement the above method, an embodiment of the present invention further provides a SQL statement generation device, which can be integrated in an electronic device, and the electronic device can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers.

[0137] For example, in this embodiment, the method of the embodiment of the present invention is described in detail by taking the SQL statement generation device being specifically integrated in the server as an example.

[0138] For example, Figure 5 As shown, the SQL statement generating device 200 may include a request obtaining module 210, an information obtaining module 220, an intermediate determining module 230, a candidate determining module 240, an interactive determining module 250 and a generating module 260, as follows:

[0139] A request acquisition module 210, used to acquire the current query request;

[0140] An information acquisition module 220 is used to acquire the information to be used of each data table in the database, wherein the information to be used includes description information, historical query information, and historical interaction information;

[0141] An intermediate determination module 230, configured to determine a plurality of intermediate data tables from the database according to a first similarity and a second similarity, wherein the first similarity is a similarity between the current query request and the description information, and the second similarity is a similarity between the current query request and the historical query information;

[0142] A candidate determination module 240 is used to guide the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables based on the current query request and the structural information of the multiple intermediate data tables;

[0143] An interaction determination module 250, configured to determine a plurality of candidate interaction information from the historical interaction information based on a third similarity, wherein the third similarity is a similarity between the current query request and the historical interaction information;

[0144] The generating module 260 is configured to generate an SQL statement corresponding to the current query request based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request.

[0145] In some embodiments, the first similarity includes semantic similarity, the second similarity includes semantic similarity and text similarity, and the intermediate determination module 230 is specifically used to:

[0146] Determining a plurality of target description information from the plurality of description information according to the semantic similarity between the current query request and each of the description information;

[0147] Determining a plurality of first query information from the plurality of historical query information according to the semantic similarity between the current query request and each piece of historical query information;

[0148] Determining a plurality of second query information from the plurality of historical query information according to the text similarity between the current query request and each historical query information;

[0149] In combination with the plurality of target description information, the plurality of first query information and the plurality of second query information, a plurality of intermediate data tables are determined from the database.

[0150] In some embodiments, the intermediate determination module 230 is specifically configured to:

[0151] Merging the target description information, the first query information, and the second query information to obtain information to be sorted;

[0152] Rearranging and filtering the information to be sorted to obtain an information sequence;

[0153] Determine a data table to be used from the database based on metadata corresponding to each information in the information sequence;

[0154] Deduplication processing is performed on the data table to be used to obtain multiple intermediate data tables.

[0155] In some embodiments, the candidate determination module 240 is specifically configured to:

[0156] Obtaining structural information corresponding to each of the intermediate data tables;

[0157] Generate a screening prompt word based on the current query request and the structural information corresponding to all the intermediate data tables;

[0158] The screening prompt words are used to guide the screening language model to screen out a preset number of candidate data tables from the multiple intermediate data tables.

[0159] In some embodiments, the third similarity includes semantic similarity and text similarity, and the interaction determination module 250 is specifically used to:

[0160] Determining a first number of first historical interaction information from the multiple historical interaction information according to the semantic similarity between the current query request and each historical interaction information;

[0161] Determining a second number of second historical interaction information from the multiple historical interaction information according to the text similarity between the current query request and each historical interaction information;

[0162] The first number of first historical interaction information and the second number of second historical interaction information are cleaned to obtain candidate interaction information.

[0163] In some embodiments, the generation module 260 is specifically used to:

[0164] Obtaining a candidate interaction record corresponding to each candidate interaction information;

[0165] Generate a conversion prompt word based on the preset number of candidate data tables, the structural information corresponding to each of the candidate data tables, the multiple candidate interaction records, and the current query request;

[0166] Based on the conversion prompt word, guiding the conversion language model to convert the current query request into a SQL statement to be verified;

[0167] If the SQL statement to be verified is successfully queried in the database, the SQL statement to be verified is used as the SQL statement corresponding to the current query request;

[0168] If the SQL statement to be verified fails to query the database, the number of retries is obtained and updated to obtain the updated number of retries;

[0169] If the number of retries after the update is less than the preset number, the conversion prompt word is obtained and updated based on the failure information to obtain a new conversion prompt word, and the step of guiding the conversion language model to convert the current query request into a SQL statement to be verified based on the conversion prompt word and subsequent steps are returned to execute;

[0170] If the number of retries after the update is not less than the preset number, the SQL statement to be verified is output.

[0171] In some embodiments, the SQL statement generating device 200 further includes a construction module. Before obtaining the information to be used of each data table in the database, the construction module is specifically used to:

[0172] Obtain the structural information, historical query records, and historical interaction records of each data table in the database;

[0173] For each of the data tables, determining a specified number of sample data from the data table;

[0174] Based on the structural information of each data table and the sample data, construct a first database, wherein the first database includes description information of each data table;

[0175] Based on the structural information and historical query records of each data table, construct a second database, wherein the second database includes the historical query information of each data table;

[0176] Based on the structural information and historical interaction records of each data table, a third database is established, wherein the third database includes the historical interaction information of each data table.

[0177] In some embodiments, the building blocks are specifically used to:

[0178] For each of the data tables, determining a first prompt word based on the structural information corresponding to the data table and the sample data;

[0179] Based on the first prompt word, guiding the first language model to generate description information corresponding to the data table;

[0180] The description information is associated with the identifier of the data table and stored in the first database.

[0181] In some embodiments, the building blocks are specifically used to:

[0182] For each data table, perform text extraction on each historical query record corresponding to the data table to obtain a historical query text corresponding to each historical query record;

[0183] Based on the historical query text, deduplication processing is performed on the historical query records corresponding to the data table to obtain cleaned query records;

[0184] For each of the cleaned query records, generating a second prompt word based on the cleaned query record and the structural information of the data table;

[0185] Based on the second prompt word, guiding the second language model to convert the cleaned query record into historical query information;

[0186] The historical query information is associated with the identifier corresponding to the data table and stored in the second database.

[0187] In specific implementation, the above modules can be implemented as independent entities, or can be arbitrarily combined and implemented as the same or several entities. The specific implementation of the above modules can be found in the previous method embodiments, which will not be repeated here.

[0188] From the above, it can be seen that the SQL statement generation device of this embodiment uses the information of the data table in the historical query dimension and the description dimension to determine multiple intermediate data tables from the database, and then accurately determines the candidate data tables that match the current query request based on the structural information of the intermediate data table; uses the information of the data table in the historical interaction dimension to determine multiple candidate interaction information; finally, the retrieval results in different dimensions are integrated to provide accurate and comprehensive contextual information, so as to accurately understand the current query request and ensure the accuracy of the generated SQL statement.

[0189] The embodiment of the present invention further provides an electronic device, which may be a terminal, a server, or the like. The terminal may be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, or the like; the server may be a single server or a server cluster composed of multiple servers, or the like.

[0190] In some embodiments, the SQL statement generating device may also be integrated into multiple electronic devices. For example, the SQL statement generating device may be integrated into multiple servers, and the SQL statement generating method of the present invention may be implemented by multiple servers.

[0191] In this embodiment, the electronic device of this embodiment is a server as an example for detailed description, for example, Figure 6 As shown, it shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present invention, specifically:

[0192] The electronic device may include components such as a processor 310 with one or more processing cores, a memory 320 with one or more computer-readable storage media, a power supply 330, an input module 340, and a communication module 350. Those skilled in the art will appreciate that Figure 6 The electronic device structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.

[0193] The processor 310 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. It executes various functions of the electronic device and processes data by running or executing software programs and / or modules stored in the memory 320, and calling data stored in the memory 320. In some embodiments, the processor 310 may include one or more processing cores; in some embodiments, the processor 310 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 310.

[0194] The memory 320 can be used to store software programs and modules. The processor 310 executes various functional applications and data processing by running the software programs and modules stored in the memory 320. The memory 320 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 320 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 320 may also include a memory controller to provide the processor 310 with access to the memory 320.

[0195] The electronic device also includes a power supply 330 for supplying power to various components. In some embodiments, the power supply 330 can be logically connected to the processor 310 through a power management system, so as to manage charging, discharging, and power consumption through the power management system. The power supply 330 can also include any components such as one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, and power status indicators.

[0196] The electronic device may further include an input module 340, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.

[0197] The electronic device may further include a communication module 350. In some embodiments, the communication module 350 may include a wireless module. The electronic device may perform short-range wireless transmission through the wireless module of the communication module 350, thereby providing the user with wireless broadband Internet access. For example, the communication module 350 may be used to help the user send and receive emails, browse web pages, and access streaming media.

[0198] Although not shown, the electronic device may further include a display unit, etc., which will not be described in detail herein. Specifically in this embodiment, the processor 310 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 320 according to the following instructions, and the processor 310 will run the application programs stored in the memory 320, thereby implementing the steps in the methods of the embodiments of the present invention.

[0199] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.

[0200] From the above, it can be seen that the electronic device provided by the embodiment of the present invention can use the information of the data table in the historical query dimension and the description dimension to determine multiple intermediate data tables from the database, and then accurately determine the candidate data table that matches the current query request based on the structural information of the intermediate data table; use the information of the data table in the historical interaction dimension to determine multiple candidate interaction information; finally, merge the retrieval results in different dimensions to provide accurate and comprehensive contextual information, so as to accurately understand the current query request and ensure the accuracy of the generated SQL statement.

[0201] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.

[0202] To this end, an embodiment of the present invention provides a computer-readable storage medium, in which a plurality of instructions are stored. The instructions can be loaded by a processor to execute the steps in any one of the SQL statement generation methods provided in the embodiments of the present invention.

[0203] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.

[0204] According to one aspect of the present invention, a computer program product or computer program is provided, the computer program product or computer program including a computer program / instruction, the computer program / instruction being stored in a computer-readable storage medium. A processor of an electronic device reads the computer program / instruction from the computer-readable storage medium, and the processor executes the computer program / instruction, so that the electronic device executes the methods provided in various optional implementations of the SQL statement generation aspect and the database construction aspect provided in the above-mentioned embodiments.

[0205] Since the instructions stored in the storage medium can execute the steps in any SQL statement generation method provided in the embodiments of the present invention, the beneficial effects that can be achieved by any SQL statement generation method provided in the embodiments of the present invention can be achieved. Please refer to the previous embodiments for details and will not be repeated here.

[0206] The above is a detailed introduction to a method and device for generating SQL statements provided in an embodiment of the present invention. Specific examples are used herein to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method and core idea of ​​the present invention. At the same time, for those skilled in the art, according to the idea of ​​the present invention, there may be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present invention.

Claims

1. A method for generating a SQL statement, characterized in that: The method comprises: Get the current query request; Acquire information to be used for each data table in the database, the information to be used includes description information, historical query information and historical interaction information, wherein the historical query information is converted from historical query records of the data table in a historical time period, the historical query records include historical query requests, the historical interaction information is historical interaction records related to the data table in the historical time period, the historical interaction records include historical query requests and historical SQL statements corresponding to the historical query requests; Determining a plurality of intermediate data tables from the database according to a first similarity and a second similarity, wherein the first similarity is a similarity between the current query request and the description information, and the second similarity is a similarity between the current query request and the historical query information; Based on the current query request and the structural information of the multiple intermediate data tables, guiding the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables, including: obtaining structural information corresponding to each of the intermediate data tables; generating screening prompt words based on the current query request and the structural information corresponding to all the intermediate data tables; using the screening prompt words, guiding the screening language model to screen out a preset number of candidate data tables from the multiple intermediate data tables; Determine a plurality of candidate interaction information from the historical interaction information based on a third similarity, wherein the third similarity is a similarity between the current query request and the historical interaction information; Based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request, generating an SQL statement corresponding to the current query request; The description information is generated by the following steps: obtaining structural information of each data table in a database; for each of the data tables, determining a specified number of sample data from the data table; for each of the data tables, determining a first prompt word based on the structural information corresponding to the data table and the sample data; and guiding a first language model to generate description information corresponding to the data table based on the first prompt word.

2. The method according to claim 1, characterized in that The first similarity includes semantic similarity, the second similarity includes semantic similarity and text similarity, and the determining of a plurality of intermediate data tables from the database according to the first similarity and the second similarity includes: Determining a plurality of target description information from the plurality of description information according to the semantic similarity between the current query request and each of the description information; Determining a plurality of first query information from the plurality of historical query information according to the semantic similarity between the current query request and each piece of historical query information; Determining a plurality of second query information from the plurality of historical query information according to the text similarity between the current query request and each historical query information; In combination with the plurality of target description information, the plurality of first query information and the plurality of second query information, a plurality of intermediate data tables are determined from the database.

3. The method according to claim 2, characterized in that The combining the plurality of target description information, the plurality of first query information and the plurality of second query information to determine a plurality of intermediate data tables from the database comprises: Merging the target description information, the first query information, and the second query information to obtain information to be sorted; Rearranging and filtering the information to be sorted to obtain an information sequence; Determine a data table to be used from the database based on metadata corresponding to each information in the information sequence; Deduplication processing is performed on the data table to be used to obtain multiple intermediate data tables.

4. The method according to claim 1, characterized in that: The step of guiding the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables based on the current query request and the structural information of the multiple intermediate data tables includes: Obtaining structural information corresponding to each of the intermediate data tables; Generate a screening prompt word based on the current query request and the structural information corresponding to all the intermediate data tables; The screening prompt words are used to guide the screening language model to screen out a preset number of candidate data tables from the multiple intermediate data tables.

5. The method according to claim 1, characterized in that The third similarity includes semantic similarity and text similarity, and determining a plurality of candidate interaction information from the historical interaction information based on the third similarity includes: Determining a first number of first historical interaction information from the multiple historical interaction information according to the semantic similarity between the current query request and each historical interaction information; Determining a second number of second historical interaction information from the multiple historical interaction information according to the text similarity between the current query request and each historical interaction information; The first number of first historical interaction information and the second number of second historical interaction information are cleaned to obtain candidate interaction information.

6. The method according to claim 1, characterized in that The generating, based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request, a SQL statement corresponding to the current query request includes: Obtaining a candidate interaction record corresponding to each candidate interaction information; Generate a conversion prompt word based on the preset number of candidate data tables, the structural information corresponding to each of the candidate data tables, the multiple candidate interaction records, and the current query request; Based on the conversion prompt word, guiding the conversion language model to convert the current query request into a SQL statement to be verified; If the SQL statement to be verified is successfully queried in the database, the SQL statement to be verified is used as the SQL statement corresponding to the current query request; If the SQL statement to be verified fails to query the database, the number of retries is obtained and updated to obtain the updated number of retries; If the number of retries after the update is less than the preset number, the conversion prompt word is obtained and updated based on the failure information to obtain a new conversion prompt word, and the step of guiding the conversion language model to convert the current query request into a SQL statement to be verified based on the conversion prompt word and subsequent steps are returned to execute; If the number of retries after the update is not less than the preset number, the SQL statement to be verified is output.

7. The method according to claim 1, characterized in that Before obtaining the to-be-used information of each data table in the database, the method further includes: Obtain the structural information, historical query records, and historical interaction records of each data table in the database; storing the description information in association with the identifier of the data table in a first database; Based on the structural information and historical query records of each data table, construct a second database, wherein the second database includes the historical query information of each data table; Based on the structural information and historical interaction records of each data table, a third database is established, wherein the third database includes the historical interaction information of each data table.

8. The method according to claim 7, characterized in that The second database is constructed based on the structural information and historical query records of each data table, including: For each data table, perform text extraction on each historical query record corresponding to the data table to obtain a historical query text corresponding to each historical query record; Based on the historical query text, deduplication processing is performed on the historical query records corresponding to the data table to obtain cleaned query records; For each of the cleaned query records, generating a second prompt word based on the cleaned query record and the structural information of the data table; Based on the second prompt word, guiding the second language model to convert the cleaned query record into historical query information; The historical query information is associated with the identifier corresponding to the data table and stored in the second database.

9. A SQL statement generating device, used to implement the method according to any one of claims 1 to 8, characterized in that: The device comprises: Request acquisition module, used to obtain the current query request; An information acquisition module, used to acquire the to-be-used information of each data table in the database, wherein the to-be-used information includes description information, historical query information, and historical interaction information, wherein the historical query information is converted from the historical query records of the data table in the historical time period, wherein the historical query records include historical query requests, and the historical interaction information is the historical interaction records related to the data table in the historical time period, wherein the historical interaction records include historical query requests and historical SQL statements corresponding to the historical query requests; an intermediate determination module, configured to determine a plurality of intermediate data tables from the database according to a first similarity and a second similarity, wherein the first similarity is a similarity between the current query request and the description information, and the second similarity is a similarity between the current query request and the historical query information; A candidate determination module, for guiding the screening language model to determine a preset number of candidate data tables from the multiple intermediate data tables based on the current query request and the structural information of the multiple intermediate data tables, comprising: obtaining structural information corresponding to each of the intermediate data tables; generating screening prompt words based on the current query request and the structural information corresponding to all the intermediate data tables; and guiding the screening language model to screen out a preset number of candidate data tables from the multiple intermediate data tables using the screening prompt words; An interaction determination module, configured to determine a plurality of candidate interaction information from the historical interaction information based on a third similarity, wherein the third similarity is a similarity between the current query request and the historical interaction information; A generating module, configured to generate an SQL statement corresponding to the current query request based on the preset number of candidate data tables, the plurality of candidate interaction information and the current query request; The description information is generated by the following steps: obtaining structural information of each data table in a database; for each of the data tables, determining a specified number of sample data from the data table; for each of the data tables, determining a first prompt word based on the structural information corresponding to the data table and the sample data; and guiding a first language model to generate description information corresponding to the data table based on the first prompt word.

Citation Information

Cited By

  • Database query generation method and system based on multi-selection optimization and storage medium

    CN119690987A