A method, device and equipment for converting natural language into SQL query statement and storage medium
By calculating dependencies using a pre-trained language model and a learnable projection matrix, the target semantic dimension is determined, solving the ambiguity problem in the conversion of natural language to SQL query statements and achieving higher conversion accuracy and model adaptability.
Patent Information
- Application Number
- CN202510913662.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-03
- Publication Date
- 2025-12-09
- Estimated Expiration
- 2045-07-03
AI Technical Summary
The conversion from natural language to SQL query statements has a high error rate, mainly because the ambiguity of natural language makes it difficult to accurately map semantic ambiguities.
By encoding natural language text using a pre-trained language model to obtain context vectors, and combining the feature vectors of each semantic dimension of fuzzy words with the learnable projection matrix to calculate dependencies, the target semantic dimension is determined, and semantic ambiguity caused by fuzziness is accurately resolved. The contextual representation ability and dynamic dependency calculation of the pre-trained model are used to adapt to the language habits of different domains.
It significantly improves the accuracy of natural language to SQL conversion, reduces the high error rate caused by semantic ambiguity, and enhances the model's generalization ability and engineering feasibility.
Smart Images

Figure CN120448406B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of natural language processing, and particularly relates to a method and device for converting natural language into SQL query statements, equipment and a storage medium. BACKGROUND
[0002] With the deep integration of natural language processing and database technology, converting natural language into SQL query statements has become a key technology for intelligent data interaction. In actual application scenarios, users issue data query instructions through natural language, and the system converts them into SQL statements for execution, thereby realizing efficient data retrieval and analysis. However, the conversion of natural language into SQL query statements has a high error rate. SUMMARY
[0003] The present application provides a method, device, equipment and storage medium for converting natural language into SQL query statements, which can reduce the error rate of the conversion of natural language into SQL query statements.
[0004] To achieve the above purpose, the present application adopts the following technical solutions:
[0005] In a first aspect, the present application provides a method for converting natural language into SQL query statements, which comprises:
[0006] obtaining a first natural language text to be converted;
[0007] inputting the first natural language text into a pre-trained language model for encoding to obtain a first context vector corresponding to the first natural language text;
[0008] determining the dependency relationship between each semantic dimension and the first context vector according to the first context vector, the feature vector of each semantic dimension corresponding to the fuzzy word in the first natural language text, and a learnable projection matrix;
[0009] determining a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector;
[0010] converting the first natural language text into a first SQL query statement according to the target semantic dimension.
[0011] Optionally, the method for determining the target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector comprises:
[0012] comparing the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold respectively to obtain a comparison result;
[0013] If the comparison result represents that there is a semantic dimension with a dependency relationship greater than or equal to a preset relationship threshold, the semantic dimension with the dependency relationship greater than or equal to the preset relationship threshold is determined as a target semantic dimension.
[0014] Optionally, the method further comprises:
[0015] If the comparison result represents that there is no semantic dimension with a dependency relationship greater than or equal to a preset relationship threshold, the semantic dimensions are sorted according to the strength of the dependency relationship, and the top K semantic dimensions are determined as target semantic dimensions, where K is an integer greater than or equal to 2.
[0016] Optionally, the method further comprises:
[0017] After querying according to the first SQL query statement, a relevance of a query result, an execution efficiency of a query process, and a domain constraint compliance degree of the query result are obtained.
[0018] According to the relevance, the execution efficiency, and the domain constraint compliance degree, a satisfaction degree of the first SQL query statement is determined.
[0019] According to the satisfaction degree of the first SQL query statement, the learnable projection matrix is adjusted.
[0020] Optionally, the relevance of the query result is obtained in the following manner:
[0021] A similarity between a feature vector of a query result and a feature vector of the user intent is calculated, and the similarity is taken as the relevance of the query result.
[0022] Optionally, the execution efficiency of the query process is obtained in the following manner:
[0023] A query duration and resource consumption in a query process are obtained.
[0024] According to the query duration, the resource consumption, a query duration threshold, and a resource consumption threshold, an execution efficiency is determined.
[0025] Optionally, the domain constraint compliance degree of the query result is obtained in the following manner:
[0026] According to whether the first SQL query statement includes a filter condition, the domain constraint compliance degree of the query result is determined.
[0027] In a second aspect, the present application provides a device for converting a natural language into an SQL query statement, the device comprising:
[0028] An obtaining module is configured to obtain a first natural language text to be converted.
[0029] The encoding module is configured to encode the first natural language text input into a pre-trained language model to obtain a first context vector corresponding to the first natural language text.
[0030] The processing module is configured to determine a dependency relationship between each semantic dimension and the first context vector according to the first context vector, a feature vector of each semantic dimension corresponding to the ambiguous word in the first natural language text, and a learnable projection matrix; and determine a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector.
[0031] The conversion module is configured to convert the first natural language text into a first SQL query statement according to the target semantic dimension.
[0032] In a third aspect, the present application provides a computing device, comprising a memory and a processor.
[0033] The memory stores one or more computer programs, and the one or more computer programs comprise instructions; when the instructions are executed by the processor, the computing device performs the method according to any one of the first aspect.
[0034] In a fourth aspect, the present application provides a computer readable storage medium for storing a computer program, wherein the computer program is used to perform the method according to any one of the first aspect.
[0035] According to the above technical solution, the present application has at least the following beneficial effects:
[0036] The present application provides a method for converting natural language into SQL query statements, which encodes natural language text by a pre-trained language model to obtain a context vector, calculates a dependency relationship by combining a feature vector of each semantic dimension of an ambiguous word and a learnable projection matrix to determine a target semantic dimension, can accurately resolve semantic ambiguity caused by natural language ambiguity, and can accurately map ambiguous words such as "recent" to target semantic dimensions such as time and distance in specific contexts, thereby significantly improving the accuracy of natural language to SQL conversion. At the same time, this mechanism can adapt to language habits in different fields with the help of the context representation ability of the pre-trained model and dynamic dependency calculation, reduce manual rule intervention, and the explicit quantization of the dependency relationship provides a clear path for debugging and optimization, effectively enhances the generalization ability and engineering landing feasibility of the model, and fundamentally solves the high error rate problem caused by semantic ambiguity in traditional methods.
[0037] It should be understood that the description of technical features, technical solutions, advantages or similar language in this application does not imply that all features and advantages can be realized in any single embodiment. On the contrary, it can be understood that the description of a feature or advantage means that the specific technical feature, technical solution or advantage is included in at least one embodiment. Therefore, the description of technical features, technical solutions or advantages in this specification does not necessarily refer to the same embodiment. Further, the technical features, technical solutions and advantages described in this embodiment can be combined in any appropriate manner. Those skilled in the art will understand that the embodiments can be implemented without one or more specific technical features, technical solutions or advantages of a specific embodiment. In other embodiments, additional technical features and advantages can be identified in specific embodiments that do not embody all embodiments. BRIEF DESCRIPTION OF DRAWINGS
[0038] Figure 1 A flowchart of a method for converting natural language into an SQL query statement provided by an embodiment of the application;
[0039] Figure 2 A schematic diagram of a device for converting natural language into an SQL query statement provided by an embodiment of the application;
[0040] Figure 3 A schematic diagram of a computing device provided by an embodiment of the application. DETAILED DESCRIPTION
[0041] The terms "first", "second" and "third" and the like in the specification and the drawings of this application are used to distinguish different objects, not to limit a specific order.
[0042] In the embodiments of the present application, the words "exemplary" or "for example" are used to mean serving as an example, instance, or illustration, and not necessarily to imply any preference or superiority. In fact, the use of the words "exemplary" or "for example" is intended to present concepts in a particular manner.
[0043] There are still significant defects in processing natural language ambiguous semantics. Due to the ambiguity of natural language, one expression can have multiple understandings. For example, "find the nearest order", the word "nearest" can be understood as the nearest in time, or the nearest in distance, different understandings will produce different SQL query statements. This leads to a high error rate in the conversion of natural language to SQL in practical applications.
[0044] To address the aforementioned technical problems, this application provides a method for converting natural language into SQL query statements. This method can be executed by a processing device, which can be a terminal or a server. Terminals include, but are not limited to, smartphones, tablets, laptops, personal digital assistants, or smart wearable devices. Servers can be cloud servers, such as central servers in a central cloud computing cluster or edge servers in an edge cloud computing cluster. Alternatively, servers can be located in a local data center. A local data center refers to a data center directly controlled by the user.
[0045] Specifically, the processing device first acquires the first natural language text to be converted, then uses a pre-trained language model to encode the first natural language text, obtaining a first context vector. Based on the first context vector, the feature vectors of each semantic dimension corresponding to the ambiguous words in the first natural language text, and the learnable projection matrix, the dependency relationship between each semantic dimension and the first context vector is determined. Then, based on this dependency relationship, the target semantic dimension is determined. Finally, the first natural language text is converted according to the target semantic dimension to obtain a first SQL query statement. Since the target semantic dimension is determined based on the dependency relationship between each semantic dimension and the first context vector, it is possible to determine which semantic dimension the first natural language text is closer to. Therefore, conversion according to the target semantic dimension makes the first SQL query statement closer to the true intent, thereby reducing the conversion error rate caused by the ambiguity of natural language.
[0046] To make the technical solution of this application clearer and easier to understand, the technical solution of this application will be described below with reference to the accompanying drawings.
[0047] like Figure 1 As shown, this figure is a flowchart of a method for converting natural language into SQL query statements according to an embodiment of this application. The method includes:
[0048] S101, The processing device acquires the first natural language text to be converted.
[0049] First natural language text refers to text content written or expressed in human natural language. For example, first natural language text could be a query for recent orders.
[0050] The processing device can acquire the first natural language text to be converted in a variety of ways, such as through the graphical user interface (GUI) provided by the processing device, or by the user inputting the first natural language text in the GUI.
[0051] This application does not specifically limit the method by which the processing device acquires the first natural language text to be converted.
[0052] S102, the processing device encodes the first natural language text into a pre-trained language model to obtain a first context vector corresponding to the first natural language text.
[0053] The pre-trained language model is an artificial intelligence model based on deep learning, which learns the internal structure, semantics and grammar of language through unsupervised or self-supervised learning on large-scale text data, to obtain general understanding and generation ability of natural language. In the embodiments of the present application, the pre-trained language model can be (Robustly Optimized BERT Pretraining Approach, RoBERTa), which is a pre-trained language model based on BERT (Bidirectional Encoder Representations from Transformers) improved, aiming to optimize the training strategy and data utilization, and improve the performance and robustness of the model in natural language understanding tasks.
[0054] After obtaining the first natural language text, the processing device can input the first natural language text into the pre-trained language model for encoding, and then obtain the first context vector corresponding to the first natural language text.
[0055] S103, the processing device determines the dependency relationship between each semantic dimension and the first context vector according to the first context vector, the feature vector of each semantic dimension corresponding to the fuzzy word in the first natural language text, and the learnable projection matrix.
[0056] In the embodiments of the present application, each fuzzy word corresponds to multiple semantic dimensions, and the semantic dimensions corresponding to each fuzzy word can be preset. Taking the fuzzy word "recently" as an example, the set of semantic dimensions of the fuzzy word is D={d1, d2…d n , d n+1 , …, d N}, where d1 represents the first semantic dimension, for example, time dimension, d2 represents the second semantic dimension, for example, distance dimension, d n represents the nth semantic dimension, d n+1 represents the n+1th semantic dimension, and d N represents the Nth semantic dimension.
[0057] For each semantic dimension, a prior feature vector can be constructed in combination with domain knowledge, which includes key semantic attributes of the semantic dimension, for example, the time dimension includes "time unit", "time window" and other features, and the distance dimension includes "geographical location", "distance measurement method" and other features.
[0058] For example, a set of dimension attributes is defined first, such as enumerating core attributes for each semantic dimension. Taking the time dimension as an example, the attribute set is ={time unit, time window, time directionality (past / future), business time type (creation time / update time)}. For discrete attributes, such as time unit and time directionality, one-hot encoding is used, for example, time unit is “day” and is encoded as [1, 0, 0], which respectively represents [day, hour, minute]; for example, time directionality is past or future, and is encoded as [0] or [1], 0 represents past and 1 represents future. For continuous attributes, such as time window, normalization is performed in the interval [0, 1], for example, time window is “30 days” and is encoded as 30 / T max (T max is the maximum time window defined). For enumerated attributes, such as business time type, mapping is performed to an integer index, for example, “creation time” is indexed as 1 and “update time” is indexed as 2. Then, the encoding results of all attributes are spliced in order to obtain the prior feature vector of the semantic dimension. For example, the time dimension includes 4 one-hot attributes, 1 continuous attribute, and 2 enumerated attributes, and the prior feature vector corresponding to the time dimension is In an embodiment of the present application, the prior feature vector provides an anchor point for the subsequent context attention calculation of the semantic dimension, so that the model can identify the core differences of different dimensions, such as the time dimension including time unit features and the distance dimension including latitude and longitude coordinate features.
[0059] The dependency relationship refers to the importance of the semantic dimension in the current context, which can be represented by a score.
[0060] In some examples, the processing device can determine the dependency relationship between each semantic dimension and the first context vector by the following calculation formula:
[0061]
[0062] wherein, represents the importance of the i-th semantic dimension in the first context vector, such as a score; “T” represents transposition, represents the transposition of , represents the feature vector of the i-th semantic dimension; represents a normalization function, represents the result of normalization for the first context vector C, C represents the first context vector, is a learnable projection matrix; represents the total number of semantic dimensions.
[0063] After obtaining The softmax function can also be used to generate the weight of each dimension:
[0064]
[0065]
[0066] wherein, represents the weight of the i-th dimension, represents a normalization function, and then in the subsequent calculation process, the can be used instead of , thereby improving the convenience of calculation.
[0067] In the embodiments of the present application, the processing device dynamically adjusts the priority of the semantic dimension by using the context information, avoids relying on fixed rules, and improves the adaptability to complex contexts.
[0068] wherein the learnable projection matrix is used to map the context vector to the attention space. The projection matrix, as the core parameter of the attention mechanism, has a dimension of HxK, H represents the dimension of the first context vector C, and K represents the dimension of the attention calculation space. Before the training starts, the projection matrix is initialized as a random normal distribution. This initialization method can provide a reasonable starting range for the parameters, avoiding unstable training caused by excessively large or small parameter values.
[0069] In the training stage, the entire fuzzy semantic resolution framework is jointly optimized with the downstream task, and the objective function is defined as the combination of cross-entropy loss and reinforcement learning reward. The specific form can be: wherein, represents the objective function, represents the cross-entropy loss, which is used to measure the difference between the generated result (SQL query statement) of the model and the true label, and ensures the accuracy of the model on known samples; represents the reinforcement learning reward, which is used to reflect the effect of the generated SQL query statement in actual execution; is a balance coefficient, which is used to adjust the relative importance of the cross-entropy loss and the reinforcement learning reward. Through the gradient descent algorithm (such as the adam optimizer), the gradient of the objective function with respect to is calculated continuously, and the parameter value of is updated. In this way, can be adjusted according to the feedback of the downstream task, ensuring that the attention score can accurately reflect the relevance of the semantic dimension and the context.
[0070] The updated projection matrix is used to map the first context vector from H dimensions to a K-dimensional attention space, i.e. . After mapping, The first context vector (the vector representation of K is an attention space) contains the context features after the projection transformation, and then the dot product operation is performed with the feature vector of the semantic dimension to obtain the attention score This mapping calculation can enable the context information to participate in the determination of the weight of the semantic dimension in the form of suitable attention calculation, so that the projection matrix can adaptively capture the deep relationship between the context and the semantic dimension, thereby improving the accuracy of fuzzy semantic resolution.
[0071] In the embodiments of the present application, the weight of each dimension represents the preference degree of the context to the semantic dimension, for example, if the attention score of "recent" to the time dimension is high, the normalized weight will also be large, indicating that the current context is more inclined to "time recent" interpretation. In subsequent generation of subsequent SQL, the weight feels the priority of each semantic dimension, and the semantic dimension with a high score or weight will be prioritized for constructing the SQL statement, thereby converting the fuzzy semantics of natural language into specific database query rules.
[0072] S104, the processing device determines the target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector.
[0073] In the embodiments of the present application, the processing device determines the target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector, which can include:
[0074] The processing device compares the dependency relationship between each semantic dimension and the first context vector with the preset relationship threshold respectively to obtain a comparison result; if the comparison result represents that there is a semantic dimension with a dependency relationship greater than or equal to the preset relationship threshold, the semantic dimension with the dependency relationship greater than or equal to the preset relationship threshold is determined as the target semantic dimension. If the comparison result represents that there is no semantic dimension with a dependency relationship greater than or equal to the preset relationship threshold, the top K semantic dimensions are determined as the target semantic dimensions according to the strength of the dependency relationship, where K is an integer greater than or equal to 2.
[0075] The dependency relationship can be represented by the weight The preset relationship threshold can be 0.6, when there is a semantic dimension with a weight greater than or equal to 0.6, the semantic dimension is determined as the target semantic dimension, and if there is not, the weights of all semantic dimensions are sorted, and the top K semantic dimensions are selected according to the size of the weight, and the top K semantic dimensions are determined as the target semantic dimensions.
[0076] S105, the processing device converts the first natural language text into a first SQL query statement according to the target semantic dimension.
[0077] After obtaining the target semantic dimension, the processing device can convert the first natural language text into a first SQL query statement according to the target semantic dimension.
[0078] When the target semantic dimension includes multiple semantic dimensions, the processing device can generate multiple first SQL query statements in order of the weights of the respective semantic dimensions from large to small.
[0079] In the embodiments of the present application, the processing device can also query according to the first SQL query statement, and after the query, obtain the relevance of the query result, the execution efficiency of the query, and the field constraint compliance of the query result, determine the satisfaction degree of the first SQL query statement according to the relevance, the execution efficiency, and the field constraint compliance, and then adjust the learnable projection matrix based on the satisfaction degree of the first SQL query statement.
[0080] The processing device can determine the satisfaction degree of the first SQL query statement by the following formula:
[0081]
[0082] wherein, represents the satisfaction degree of the i th first query statement, represents a first weight coefficient, represents a second weight coefficient, represents a third weight coefficient, represents the relevance, represents the execution efficiency, represents the field constraint compliance.
[0083] Specifically, the processing device can determine the relevance of the query result by the following method:
[0084] Calculate the similarity between the feature vector of the query result and the feature vector of the user intent, and take the similarity as the relevance of the query result. The similarity is calculated by the following formula:
[0085]
[0086] wherein, represents the relevance, represents the feature vector of the query result, represents the feature vector of the user intent.
[0087] In the embodiments of the present application, the feature vector I of the user intention contains the deep semantics of the user statement, and O is the feature vector of the SQL execution result. If the similarity of O and I is high, it indicates that the semantic interpretation of "recent" (for example, the time dimension) by the SQL meets the user's expectation; if the similarity is low, it indicates that the interpretation is incorrect (if the distance dimension is incorrectly selected), so that the correct interpretation direction of the identified ambiguous language can be verified. In the reinforcement learning training process, if the similarity is high, the model will receive a positive reward, and if the similarity is low, the model will receive a negative reward, and then the strategy is adjusted. In this way, the model can learn from historical data and gradually optimize the processing capability of ambiguous semantics. For example, in the logistics scenario, "recent" is more related to the distance dimension, and in the financial scenario, "recent" is more related to the time dimension. When humans understand natural language, they will subconsciously judge whether the information meets the expected intention (such as "find the nearest order", and if an order sorted by time is received, it will be considered reasonable). The essence of calculating the similarity of O and I is to simulate this process: taking I as the "expected standard" and O as the "actual output", the higher the similarity, the more the "actual output" meets the "expected standard". In this way, the semantic interpretation can be reasonably checked based on the context, like humans.
[0088] Specifically, the processing device can determine the execution efficiency of the query process in the following manner:
[0089] obtaining the query duration and resource consumption in the query process; and determining the execution efficiency according to the query duration, the resource consumption, a query duration threshold and a resource consumption threshold. The execution efficiency is calculated by the following formula:
[0090]
[0091] wherein, represents the execution efficiency, represents the query duration in the query process, represents the resource consumption in the query process, which can be represented by the number of scanned rows, represents the maximum number of scanned rows, represents the maximum query duration.
[0092] Specifically, the processing device can determine the domain constraint compliance degree of the query result in the following manner:
[0093] determining the domain constraint compliance degree of the query result according to whether the first SQL query statement includes a filtering condition.
[0094] defining a compliance rule set, for example, an order table must include a filtering condition of a state of paid, checking whether the SQL includes the condition, and if yes, , otherwise .
[0095] Based on the above description, the application provides a method for converting natural language into a SQL query statement. The method encodes natural language text through a pre-trained language model to obtain a context vector, combines feature vectors of each semantic dimension of a fuzzy word and a learnable projection matrix to calculate a dependency relationship to determine a target semantic dimension. The method can accurately resolve semantic ambiguity caused by natural language fuzziness, so that fuzzy words such as "nearest" can be accurately mapped to target semantic dimensions such as time and distance in a specific context, thereby significantly improving the accuracy of natural language to SQL conversion. At the same time, the mechanism can adapt to language habits in different fields with the help of the contextual representation ability of the pre-trained model and dynamic dependency calculation, reduce manual rule intervention, and the explicit quantization of the dependency relationship provides a clear path for debugging and optimization, effectively enhances the generalization ability and engineering landing feasibility of the model, and fundamentally solves the high error rate problem caused by semantic ambiguity in traditional methods.
[0096] The above Figure 1 The method for converting natural language into a SQL query statement provided by the embodiments of the application is described in detail, and the device and the apparatus provided by the embodiments of the application will be introduced below with reference to the accompanying drawings.
[0097] As Figure 2 shown, the figure is a schematic diagram of a device for converting natural language into a SQL query statement provided by an embodiment of the application. The device comprises:
[0098] The obtaining module 201 is configured to obtain a first natural language text to be converted.
[0099] The encoding module 202 is configured to input the first natural language text into a pre-trained language model for encoding to obtain a first context vector corresponding to the first natural language text.
[0100] The processing module 203 is configured to determine a dependency relationship between each semantic dimension and the first context vector according to the first context vector, the feature vectors of each semantic dimension corresponding to the fuzzy word in the first natural language text, and a learnable projection matrix, and determine a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector.
[0101] The conversion module 204 is configured to convert the first natural language text into a first SQL query statement according to the target semantic dimension.
[0102] Optionally, the processing module 203 is specifically configured to compare the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold respectively to obtain a comparison result; if the comparison result indicates that there is a semantic dimension with a dependency relationship greater than or equal to the preset relationship threshold, the semantic dimension with the dependency relationship greater than or equal to the preset relationship threshold is determined as a target semantic dimension.
[0103] Optionally, the processing module 203 is further configured to, if the comparison result indicates that there is no semantic dimension with a dependency relationship greater than or equal to the preset relationship threshold, sort the semantic dimensions according to the strength of the dependency relationship, and determine the top K semantic dimensions as the target semantic dimensions, where K is an integer greater than or equal to 2.
[0104] Optionally, the processing module 203 is further configured to, after querying according to the first SQL query statement, obtain the relevance of the query result, the execution efficiency of the query process, and the domain constraint compliance degree of the query result; determine the satisfaction degree of the first SQL query statement according to the relevance, the execution efficiency, and the domain constraint compliance degree; and adjust the learnable projection matrix according to the satisfaction degree of the first SQL query statement.
[0105] Optionally, the processing module 203 is configured to calculate the similarity between the feature vector of the query result and the feature vector of the user intent, and take the similarity as the relevance of the query result.
[0106] Optionally, the processing module 203 is configured to obtain the query duration and resource consumption in the query process; and determine the execution efficiency according to the query duration, the resource consumption, a query duration threshold, and a resource consumption threshold.
[0107] Optionally, the processing module 203 is configured to determine the domain constraint compliance degree of the query result according to whether the first SQL query statement includes a filtering condition.
[0108] The device for converting natural language into an SQL query statement according to the embodiments of the present application can correspond to performing the method described in the embodiments of the present application, and the above-mentioned other operations and / or functions of each module / unit of the device for converting natural language into an SQL query statement are respectively implemented to realize the corresponding flow of each method in the embodiments of the present application. For brevity, they will not be described here again. Figure 1 The above-mentioned other operations and / or functions of each module / unit of the device for converting natural language into an SQL query statement are respectively implemented to realize the corresponding flow of each method in the embodiments of the present application. For brevity, they will not be described here again.
[0109] The embodiments of the present application also provide a computing device. As shown in the Figure 3 The figure is a schematic diagram of a computing device provided by the embodiments of the present application. The computing device 300 includes a bus 301, a processor 302, a communication interface 303, and a memory 304. The processor 302, the memory 304, and the communication interface 303 communicate through the bus 301.
[0110] Bus 301 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. Buses can be categorized as address buses, data buses, control buses, etc. For ease of representation, Figure 3 The bus is represented by a single thick line, but this does not mean that there is only one bus or one type of bus.
[0111] The processor 302 can be any one or more of the following processors: central processing unit (CPU), graphics processing unit (GPU), microprocessor (MP), or digital signal processor (DSP).
[0112] Communication interface 303 is used for communication with external devices.
[0113] Memory 304 may include volatile memory, such as random access memory (RAM). Memory 304 may also include non-volatile memory, such as read-only memory (ROM), flash memory, hard disk drive (HDD), or solid state drive (SSD).
[0114] The memory 304 stores executable code, which the processor 302 executes to perform the aforementioned method of converting natural language into SQL query statements.
[0115] Specifically, in achieving Figure 2 In the case of the illustrated embodiment, and Figure 2 When the modules or units of the apparatus for converting natural language into SQL query statements described in the embodiments are implemented by software, the execution... Figure 2 The software or program code required for the functions of each module / unit can be partially or wholly stored in memory 304. Processor 302 executes the program code corresponding to each unit stored in memory 304, and executes the aforementioned method of converting natural language into SQL query statements.
[0116] The embodiments of the present application further provide a computer readable storage medium. The computer readable storage medium can be any available medium or data storage device that can be used to store instructions that can be executed by a computing device. The computer readable storage medium can be a magnetic-based medium (e.g., a floppy diskette, a hard disk drive, a magnetic tape), an optical-based medium (e.g., a compact disc, a DVD), or a semiconductor-based medium (e.g., a solid-state drive), etc. The computer readable storage medium includes instructions that are executable by a computing device to perform the method of converting natural language into a SQL query statement.
[0117] The embodiments of the present application further provide a computer program product including one or more computer instructions. When the computer instructions are loaded and executed on a computing device, the computer instructions generate, in whole or in part, the processes or functions described in the embodiments of the present application.
[0118] The computer instructions can be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another computer readable storage medium, for example, the computer instructions can be transmitted from one website, computer or data center to another website, computer or data center through wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) means.
[0119] The computer program product is executed by a computer, and the computer executes any of the methods of converting natural language into a SQL query statement. The computer program product can be a software installation package, and when any of the methods of converting natural language into a SQL query statement is needed, the computer program product can be downloaded and executed on the computer.
[0120] The descriptions of the processes or structures corresponding to the above respective figures are each focused on, and the parts not described in detail in a certain process or structure can be referred to the related descriptions of other processes or structures.
[0121] The above is merely specific embodiments of the present application, and the protection scope of the present application is not limited thereto, and any changes or replacements within the technical scope disclosed in the present application should be covered within the protection scope of the present application.
Claims
1. A method of converting natural language into SQL query statements, characterized by, The method comprises: acquiring a first natural language text to be converted; inputting the first natural language text into a pre-trained language model for encoding to obtain a first context vector corresponding to the first natural language text; determining a dependency relationship between each semantic dimension and the first context vector according to the first context vector, a feature vector of each semantic dimension corresponding to an ambiguous word in the first natural language text, and a learnable projection matrix, wherein each ambiguous word corresponds to a plurality of semantic dimensions, for each semantic dimension, a core attribute is enumerated, and the encoding results of all attributes are spliced in order to obtain a prior feature vector of the semantic dimension, which provides an anchor point of the semantic dimension for subsequent context attention calculation, so that the model can identify the core difference of different dimensions; the dependency relationship refers to the importance of the semantic dimension in the current context, and the dependency relationship between each semantic dimension and the first context vector can be determined by the following calculation formula: wherein, represents the importance of the i-th semantic dimension in the first context vector, e.g. a score; "T" represents the transpose, represents the transpose of represents the feature vector of the i-th semantic dimension; represents a normalization function, represents the result of the normalization for the first context vector C, C represents the first context vector, is a learnable projection matrix; represents the total number of semantic dimensions; determining a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector; converting the first natural language text into a first SQL query statement according to the target semantic dimension.
2. The method of claim 1, wherein, The method further comprises: if the comparison result indicates that there is no semantic dimension with a dependency relationship greater than or equal to the preset relationship threshold, ranking the semantic dimensions according to the strength of the dependency relationship, and determining the top K semantic dimensions as the target semantic dimensions, wherein K is an integer greater than or equal to 2. The method further comprises:
3. The method of claim 2, wherein, after querying according to the first SQL query statement, acquiring the relevance of the query result, the execution efficiency of the query process, and the domain constraint compliance degree of the query result; determining the satisfaction degree of the first SQL query statement according to the relevance, the execution efficiency and the domain constraint compliance degree; 4. The method of claim 1, wherein, adjusting the learnable projection matrix according to the satisfaction degree of the first SQL query statement. The relevance of the query result is obtained by: calculating the similarity between the feature vector of the query result and the feature vector of the user's intention, and taking the similarity as the relevance of the query result. The execution efficiency of the query process is obtained by:
5. The method of claim 4, wherein, acquiring the query duration and resource consumption in the query process; determining the execution efficiency according to the query duration, the resource consumption, a query duration threshold and a resource consumption threshold.
6. The method of claim 4, wherein, The domain constraint compliance degree of the query result is obtained by: determining the domain constraint compliance degree of the query result according to whether the first SQL query statement includes a filtering condition. The device comprises:
7. The method of claim 4, wherein, 8. An apparatus for converting natural language into SQL query statements, the apparatus comprising: An acquisition module is configured to acquire a first natural language text to be converted; An encoding module is configured to input the first natural language text into a pre-trained language model for encoding to obtain a first context vector corresponding to the first natural language text; A processing module is configured to determine a dependency relationship between each semantic dimension and the first context vector according to the first context vector, a feature vector of each semantic dimension corresponding to an ambiguous word in the first natural language text, and a learnable projection matrix, and determine a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector. Each ambiguous word corresponds to a plurality of semantic dimensions. For each semantic dimension, a core attribute is enumerated, and the encoding results of all attributes are concatenated in order to obtain a prior feature vector of the semantic dimension. The prior feature vector provides an anchor point of the semantic dimension for subsequent context attention calculation, so that the model can identify the core difference of different dimensions. The dependency relationship refers to the importance of the semantic dimension in the current context, and the dependency relationship between each semantic dimension and the first context vector can be determined by the following calculation formula: wherein, denotes the importance of the i-th semantic dimension in the first context vector, e.g. a score; "T" denotes the transpose, denotes the transpose of denotes the feature vector of the i-th semantic dimension; denotes a normalization function, denotes the result of the normalization for the first context vector C, C denotes the first context vector, is a learnable projection matrix; denotes the total number of semantic dimensions; A conversion module is configured to convert the first natural language text into a first SQL query statement according to the target semantic dimension.
9. A computing device, comprising: comprises a memory and a processor; wherein one or more computer programs are stored in the memory, and the one or more computer programs comprise instructions; when the instructions are executed by the processor, the computing device performs the method of any one of claims 1 to 7.
10. A computer-readable storage medium, characterized in that, The computer readable storage medium is used to store a computer program for executing the method of any one of claims 1 to 7.
Citation Information
Patent Citations
Query statement generation method and device based on large language model, equipment and medium
CN118964560A
Data enhancement method and device, electronic equipment and storage medium
CN119293160A
Spoken text generation method and device, equipment and storage medium
CN119358539A