Method, device and equipment for converting natural language into SQL (Structured Query Language) query statement and storage medium
Through pre-trained language model coding and dependency calculation, the target semantic dimension is determined, and the semantic ambiguity problem in natural language to SQL query statement transformation is solved, achieving higher conversion accuracy and model generalization ability.
Patent Information
- Application Number
- CN202510913662.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-03
- Publication Date
- 2025-08-08
- Estimated Expiration
- 2045-07-03
AI Technical Summary
The conversion of natural language to SQL query statements has a high error rate, mainly due to the ambiguity of natural language, which leads to semantic ambiguity.
The pre-trained language model encodes natural language text to obtain context vectors, combines the feature vectors of each semantic dimension of fuzzy words and the learning projection matrix to calculate dependencies, determine the target semantic dimensions, accurately dispel semantic ambiguity, and realize the accurate conversion from natural language to SQL.
It significantly improves the accuracy of natural language to SQL conversion, reduces the high error rate caused by semantic fuzziness, and enhances the generalization ability of the model and the feasibility of engineering implementation.
Smart Images

Figure CN120448406A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing technology, and in particular to a method, apparatus, device, and storage medium for converting natural language into SQL query statements. Background Art
[0002] With the deep integration of natural language processing and database technologies, converting natural language into SQL queries has become a key technology for intelligent data interaction. In real-world applications, users issue data query instructions in natural language, and the system converts them into SQL statements for execution, enabling efficient data retrieval and analysis. However, the conversion of natural language into SQL queries has a high error rate. Summary of the Invention
[0003] The present application provides a method, apparatus, device, and storage medium for converting natural language into SQL query statements, which can reduce the error rate of conversion from natural language to SQL query statements.
[0004] To achieve the above objectives, this application adopts the following technical solutions: In a first aspect, the present application provides a method for converting natural language into SQL query statements, the method comprising: Obtaining 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 based on the first context vector, feature vectors of each semantic dimension corresponding to the ambiguous word in the first natural language text, and a learnable projection matrix; determining a target semantic dimension according to a dependency relationship between each semantic dimension and the first context vector; According to the target semantic dimension, the first natural language text is converted into a first SQL query statement.
[0005] Optionally, determining the target semantic dimension according to the dependency relationship between the respective semantic dimensions and the first context vector includes: Comparing the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold to obtain a comparison result; If the comparison result indicates that there is a semantic dimension whose dependency is greater than or equal to a preset relationship threshold, the semantic dimension whose dependency is greater than or equal to the preset relationship threshold is determined as the target semantic dimension.
[0006] Optionally, the method further includes: If the comparison result indicates that there is no semantic dimension whose dependency is greater than or equal to a preset relationship threshold, the semantic dimensions are sorted according to the strength of the dependency, and the top K semantic dimensions are determined as target semantic dimensions, where K is an integer greater than or equal to 2.
[0007] Optionally, the method further includes: After performing a query according to the first SQL query statement, obtaining the relevance of the query results, the execution efficiency of the query process, and the domain constraint compliance of the query results; determining a satisfaction level of the first SQL query statement based on the relevance, the execution efficiency, and the domain constraint compliance; The learnable projection matrix is adjusted according to the satisfaction of the first SQL query statement.
[0008] Optionally, the relevance of the query result is obtained in the following manner: The similarity between the feature vector of the query result and the feature vector of the user intention is calculated, and the similarity is used as the relevance of the query result.
[0009] Optionally, the execution efficiency of the query process is obtained by: Get the query duration and resource consumption during the query process; The execution efficiency is determined according to the query duration, the resource consumption, the query duration threshold, and the resource consumption threshold.
[0010] Optionally, the domain constraint compliance of the query result is obtained in the following manner: The domain constraint compliance of the query result is determined according to whether the first SQL query statement includes a filter condition.
[0011] In a second aspect, the present application provides a device for converting natural language into SQL query statements, the device comprising: An acquisition module, configured to acquire a first natural language text to be converted; an encoding module, 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, configured to determine, based on the first context vector, feature vectors of each semantic dimension corresponding to the ambiguous words in the first natural language text, and a learnable projection matrix, a dependency relationship between each semantic dimension and the first context vector; and determine a target semantic dimension based on the dependency relationship between each semantic dimension and the first context vector; A conversion module is used to convert the first natural language text into a first SQL query statement according to the target semantic dimension.
[0012] In a third aspect, the present application provides a computing device, including a memory and a processor; One or more computer programs are stored in the memory, and the one or more computer programs include instructions; when the instructions are executed by the processor, the computing device executes the method as described in any one of the first aspects.
[0013] In a fourth aspect, the present application provides a computer-readable storage medium for storing a computer program for executing the method as described in any one of the first aspects.
[0014] It can be seen from the above technical solution that this application has at least the following beneficial effects: This application provides a method for converting natural language into SQL query statements. The method encodes natural language text through a pre-trained language model to obtain a context vector, combines the feature vectors of each semantic dimension of fuzzy words with a learnable projection matrix to calculate the dependency to determine the target semantic dimension. The method can accurately resolve the semantic ambiguity caused by the ambiguity of natural language, so that fuzzy words such as "recent" 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 by leveraging the context representation ability and dynamic dependency calculation of the pre-trained model, reduce manual rule intervention, and the explicit quantification of dependency relationships provides a clear path for debugging and optimization, effectively enhancing the generalization ability of the model and the feasibility of engineering implementation, and fundamentally solving the high error rate problem caused by semantic ambiguity in traditional methods.
[0015] It should be understood that the description of technical features, technical solutions, beneficial effects 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 is understood that the description of a feature or beneficial effect means that a specific technical feature, technical solution or beneficial effect is included in at least one embodiment. Therefore, the description of a technical feature, technical solution or beneficial effect in this specification does not necessarily refer to the same embodiment. Furthermore, the technical features, technical solutions and beneficial effects described in the present embodiment can also be combined in any appropriate manner. Those skilled in the art will understand that the embodiment can be implemented without one or more specific technical features, technical solutions or beneficial effects of a specific embodiment. In other embodiments, additional technical features and beneficial effects can also be identified in specific embodiments that do not embody all embodiments. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] Figure 1 A flowchart of a method for converting natural language into SQL query statements provided in an embodiment of the present application; Figure 2 A schematic diagram of an apparatus for converting natural language into SQL query statements provided in an embodiment of the present application; Figure 3 A schematic diagram of a computing device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0017] The terms "first", "second" and "third" in this application specification and the accompanying drawings are used to distinguish different objects rather than to limit a specific order.
[0018] In the embodiments of this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations, or descriptions. Any embodiment or design described as "exemplary" or "for example" in the embodiments of this application should not be interpreted as being preferred or advantageous over other embodiments or designs. Rather, the use of words such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner.
[0019] Significant flaws still exist in processing ambiguous natural language semantics. Due to the ambiguity of natural language, a single statement can have multiple interpretations. For example, in the phrase "find the most recent order," the word "recent" can be interpreted as meaning either recent in time or recent in distance. Different interpretations can generate different SQL queries. As a result, natural language to SQL conversion still suffers from a high error rate in practical applications.
[0020] In order to solve the above technical problems, the present application provides a method for converting natural language into SQL query statements, which 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. The server can be a cloud server, such as a central server in a central cloud computing cluster, or an edge server in an edge cloud computing cluster. Of course, the server can also be a server in a local data center. A local data center refers to a data center directly controlled by the user.
[0021] Specifically, the processing device can first obtain a first natural language text to be converted, and then use a pre-trained language model to encode the first natural language text to obtain a first context vector. Based on the first context vector, the feature vectors of each semantic dimension corresponding to the fuzzy 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, and then the target semantic dimension is determined based on the dependency relationship. 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 can be determined which semantic dimension the first natural language text is closer to, and then converted according to the target semantic dimension, which can make the first SQL query statement closer to the true intention, thereby reducing the conversion error rate caused by the ambiguity of natural language.
[0022] In order to make the technical solution of the present application clearer and easier to understand, the technical solution of the present application is introduced below with reference to the accompanying drawings.
[0023] like Figure 1 As shown in the figure, this figure is a flowchart of a method for converting natural language into SQL query statements provided by an embodiment of the present application, the method comprising: S101: A processing device obtains a first natural language text to be converted.
[0024] The first natural language text refers to text content written or expressed in a natural language of humans. For example, the first natural language text may be a query about recent orders.
[0025] The processing device may obtain the first natural language text to be converted in various ways, such as a user inputting the first natural language text in a graphical user interface (GUI) provided by the processing device.
[0026] The embodiment of the present application does not specifically limit the manner in which the processing device obtains the first natural language text to be converted.
[0027] S102: The processing device inputs the first natural language text into the pre-trained language model for encoding to obtain a first context vector corresponding to the first natural language text.
[0028] A pre-trained language model is an artificial intelligence model based on deep learning. It learns the intrinsic structure, semantics, and grammar of a language through unsupervised or self-supervised learning on large-scale text data to acquire a general understanding and generation capability of natural language. In an embodiment of the present application, the pre-trained language model can be the Robustly Optimized BERT Pretraining Approach (RoBERTa). RoBERTa is a pre-trained language model improved based on BERT (Bidirectional Encoder Representations from Transformers). It aims to improve the performance and robustness of the model in natural language understanding tasks by optimizing training strategies and data utilization.
[0029] After obtaining the first natural language text, the processing device can input the first natural language text into a pre-trained language model for encoding, thereby obtaining a first context vector corresponding to the first natural language text.
[0030] S103. The processing device determines the dependency relationship between each semantic dimension and the 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.
[0031] In the embodiment of the present application, each fuzzy word corresponds to multiple semantic dimensions, and the semantic dimensions corresponding to each fuzzy word can be pre-set. 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, such as time dimension, and d2 represents the second semantic dimension, such as distance dimension. n represents the nth semantic dimension, d n+1 represents the n+1th semantic dimension, d N Represents the Nth semantic dimension.
[0032] For each semantic dimension, a priori feature vector can be constructed in combination with domain knowledge. The prior feature vector includes the key semantic attributes of the semantic dimension. For example, the time dimension includes features such as "time unit" and "time window", and the distance dimension includes features such as "geographic location" and "distance measurement method".
[0033] For example, we first define a dimension attribute set, for example, for each semantic dimension, we enumerate the core attributes. Taking the time dimension as an example, the attribute set ={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, if the time unit is "day", it is encoded as [1,0,0], representing [day, hour, minute] respectively; if the time directionality is past or future, it is encoded as [0] or [1], with 0 representing the past and 1 representing the future. For continuous attributes, such as time windows, they are normalized to the [0,1] interval. For example, if the time window is "30 days", it is encoded as 30 / T max (T max For enumerated attributes, such as business time type, they are mapped to integer indexes, for example, "creation time" is indexed as 1, and "update time" is indexed as 2. Then, the encoding results of all attributes are concatenated in order to obtain the prior feature vector of the semantic dimension. For example, the time dimension consists of 4 unique attributes, 1 continuous attribute, and 2 enumerated attributes. Then the prior feature vector corresponding to the time dimension is In the embodiment of the present application, the prior feature vector provides an anchor point for the semantic dimension for the subsequent context attention calculation, so that the model can identify the core differences of different dimensions, for example, the time dimension includes the time unit feature, and the distance dimension includes the latitude and longitude coordinate features.
[0034] Dependency refers to the importance of the semantic dimension in the current context and can be represented by a score.
[0035] In some examples, the processing device may determine the dependency relationship between each semantic dimension and the first context vector using the following calculation formula:
[0036] in, Indicates the importance of the i-th semantic dimension in the first context vector, such as the score; "T" represents transposition, express The transpose of The feature vector representing the i-th semantic dimension; represents the normalization function, represents the result after normalization of the first context vector C, where C represents the first context vector, is a learnable projection matrix; Indicates the total number of semantic dimensions.
[0037] In getting You can also use the softmax function to generate weights for each dimension:
[0038]
[0039] in, represents the weight of the i-th dimension, Represents the normalization function, which can be used in subsequent calculations replace , thereby improving the convenience of calculation.
[0040] In an embodiment of the present application, the processing device uses context information to dynamically adjust the priority of semantic dimensions, avoids relying on fixed rules, and improves adaptability to complex contexts.
[0041] The learnable projection matrix is used to map the context vector to the attention space. As the core parameter of the attention mechanism, the projection matrix has dimensions H × K, where H represents the dimension of the first context vector C and K represents the dimension of the attention calculation space. Before training begins, the projection matrix is initialized to a random normal distribution. This initialization method provides a reasonable starting range for the parameters, avoiding training instability caused by overly large or small parameter values.
[0042] During the training phase, the entire fuzzy semantic resolution framework is jointly optimized with downstream tasks, and the objective function is defined as a combination of cross entropy loss and reinforcement learning reward. The specific form can be: .in, represents the objective function, Represents the cross entropy loss, which is used to measure the difference between the model-generated results (SQL query statements) and the true labels to ensure 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 used to adjust the relative importance of cross entropy loss and reinforcement learning reward. Through the gradient descent algorithm (such as Adam optimizer), the objective function is continuously calculated with respect to The gradient of , and update This will make Adjustments are made based on feedback from downstream tasks to ensure that the attention score accurately reflects the relevance of semantic dimensions to the context.
[0043] Using the updated projection matrix, the first context vector is mapped from the H dimension to the K-dimensional attention space, i.e. After mapping (The first context vector is the vector representation of the attention space in K) contains the context features after projection transformation, and then performs dot product operation with the feature vector of the semantic dimension to obtain the attention score This mapping calculation enables contextual 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.
[0044] In the embodiment of the present application, the weights of each dimension Indicates the preference of the context for the semantic dimension. For example, if the attention score of "recent" on the time dimension is high, the normalized weight will also be large, indicating that the current context is more inclined to the interpretation of "recent time". When generating subsequent SQL, the weight The priority of each semantic dimension is considered, and the semantic dimensions with high scores or weights will be used first to construct SQL statements, thereby converting the fuzzy semantics of natural language into specific database query rules.
[0045] S104: The processing device determines a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector.
[0046] In the embodiment 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 may include: The processing device compares the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold 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 a dependency relationship greater than or equal to the preset relationship threshold is determined as the target semantic dimension. If the comparison result indicates that there is no semantic dimension with a dependency relationship greater than or equal to the 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 the target semantic dimensions, where K is an integer greater than or equal to 2.
[0047] Among them, the dependency relationship can be expressed through the above weights To represent, 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. If it does not exist, 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.
[0048] S105: The processing device converts the first natural language text into a first SQL query statement according to the target semantic dimension.
[0049] After obtaining the target semantic dimension, the processing device may convert the first natural language text into a first SQL query statement according to the target semantic dimension.
[0050] In the case that the target semantic dimension includes multiple semantic dimensions, multiple first SQL query statements may be generated in sequence according to the weight of each semantic dimension from large to small.
[0051] In an embodiment of the present application, the processing device can also perform a query according to the first SQL query statement. After the query, the relevance of the query result, the execution efficiency of the query, and the domain constraint compliance of the query result are obtained. Based on the relevance, execution efficiency, and domain constraint compliance, the satisfaction of the first SQL query statement is determined, and then based on the satisfaction of the first SQL query statement, the learnable projection matrix is adjusted.
[0052] The processing device can determine the satisfaction level of the first SQL query statement using the following formula:
[0053] in, Indicates the first query statement of i satisfaction, represents the first weight coefficient, represents the second weight coefficient, represents the third weight coefficient, Indicates relevance, Indicates execution efficiency, Indicates domain constraint compliance.
[0054] Specifically, the processing device may determine the relevance of the query result in the following manner: Calculate the similarity between the feature vector of the query result and the feature vector of the user intention, and use the similarity as the correlation of the query result. Specifically, it is calculated by the following formula:
[0055] in, Indicates relevance, The feature vector representing the query result, A feature vector representing user intent.
[0056] In this embodiment, the feature vector I of the user's intent embodies the deep semantics of the user's statement, while O represents the feature vector of the SQL execution result. If the similarity between O and I is high, it indicates that the SQL's semantic interpretation of "recent" (e.g., along the time dimension) meets the user's expectations; if the similarity is low, it indicates an incorrect interpretation (e.g., due to the incorrect choice of the distance dimension). This allows the correct interpretation of ambiguous language to be verified. During reinforcement learning training, high similarity rewards the model positively, while low similarity leads to negative rewards, which in turn adjusts the strategy. In this way, the model can learn from historical data and gradually optimize its ability to handle ambiguous semantics. For example, in logistics scenarios, "recent" is more closely associated with the distance dimension, while in financial scenarios, "recent" is more closely associated with the time dimension. When humans understand natural language, they subconsciously judge whether the information meets their intended intent (e.g., "find the most recent orders" would seem reasonable if they received orders sorted by time). Calculating the similarity between O and I essentially simulates this process: using I as the "expected standard" and O as the "actual output." The higher the similarity, the more the "actual output" meets the "expected standard." This allows for reasonableness verification of semantic interpretation based on context, just like humans do.
[0057] Specifically, the processing device may determine the execution efficiency of the query process in the following manner: Obtain the query duration and resource consumption during the query process; determine the execution efficiency based on the query duration, resource consumption, query duration threshold, and resource consumption threshold. This is specifically calculated using the following formula:
[0058] in, Indicates execution efficiency, Indicates the query duration during the query process. Indicates the resource consumption during the query process, where resource consumption can be represented by the number of scanned rows. Indicates the maximum number of scan lines, Indicates the maximum query duration.
[0059] Specifically, the processing device may determine the domain constraint compliance of the query result in the following manner: The domain constraint compliance of the query result is determined according to whether the first SQL query statement includes a filter condition.
[0060] Define a set of compliance rules, for example, the order table must contain a filter condition with the status of paid, and check whether the SQL includes this condition. If it meets the requirements, ,otherwise .
[0061] Based on the above description, the present application provides a method for converting natural language into SQL query statements. The method obtains a context vector by encoding natural language text through a pre-trained language model, and calculates the dependency relationship by combining the feature vectors of each semantic dimension of fuzzy words and a learnable projection matrix to determine the target semantic dimension. The method can accurately resolve the semantic ambiguity caused by the ambiguity of natural language, so that fuzzy words such as "recent" 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 context representation ability and dynamic dependency calculation of the pre-trained model, reduce manual rule intervention, and the explicit quantification of dependency relationships provides a clear path for debugging and optimization, effectively enhancing the generalization ability of the model and the feasibility of engineering implementation, and fundamentally solving the high error rate problem caused by semantic ambiguity in traditional methods.
[0062] Combined with the above Figure 1 The method for converting natural language into SQL query statements provided in the embodiment of the present application is introduced in detail. The following will introduce the apparatus and device provided in the embodiment of the present application in conjunction with the accompanying drawings.
[0063] like Figure 2 As shown in FIG, this figure is a schematic diagram of a device for converting natural language into SQL query statements provided by an embodiment of the present application, the device comprising: An acquisition module 201 is configured to acquire a first natural language text to be converted; An 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; A processing module 203 is configured to determine, based on the first context vector, feature vectors of each semantic dimension corresponding to the ambiguous word in the first natural language text, and a learnable projection matrix, a dependency relationship between each semantic dimension and the first context vector; and determine a target semantic dimension based on the dependency relationship between each semantic dimension and the first context vector. 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.
[0064] Optionally, the processing module 203 is specifically used to compare the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold to obtain a comparison result; if the comparison result indicates that there is a semantic dimension whose dependency relationship is greater than or equal to the preset relationship threshold, then the semantic dimension whose dependency relationship is greater than or equal to the preset relationship threshold is determined as the target semantic dimension.
[0065] Optionally, the processing module 203 is also used to sort the semantic dimensions according to the strength of the dependency relationship if the comparison result indicates that there is no semantic dimension whose dependency relationship is greater than or equal to a preset relationship threshold, and determine the top K semantic dimensions as target semantic dimensions, where K is an integer greater than or equal to 2.
[0066] Optionally, the processing module 203 is further used to obtain the relevance of the query results, the execution efficiency of the query process, and the domain constraint compliance of the query results after querying according to the first SQL query statement; determine the satisfaction level of the first SQL query statement based on the relevance, the execution efficiency, and the domain constraint compliance; and adjust the learnable projection matrix based on the satisfaction level of the first SQL query statement.
[0067] 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 intention, and use the similarity as the relevance of the query result.
[0068] Optionally, the processing module 203 is configured to obtain query duration and resource consumption during the query process; and determine execution efficiency according to the query duration, the resource consumption, the query duration threshold, and the resource consumption threshold.
[0069] Optionally, the processing module 203 is configured to determine the domain constraint compliance of the query result according to whether the first SQL query statement includes a filtering condition.
[0070] The apparatus for converting natural language into SQL query statements according to the embodiment of the present application may correspond to executing the method described in the embodiment of the present application, and the above-mentioned other operations and / or functions of each module / unit of the apparatus for converting natural language into SQL query statements are respectively to realize Figure 1 For the sake of brevity, the corresponding processes of the various methods in the illustrated embodiments are not described again here.
[0071] The present application also provides a computing device. Figure 3 As shown, this figure is a schematic diagram of a computing device provided by an embodiment of the present application, and 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 with each other via the bus 301.
[0072] The bus 301 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus. The bus may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 3 Only one thick line is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0073] The processor 302 may be any one or more of a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).
[0074] The communication interface 303 is used for communicating with the outside.
[0075] The memory 304 may include volatile memory, such as random access memory (RAM). The 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).
[0076] The memory 304 stores executable codes, and the processor 302 executes the executable codes to perform the aforementioned method of converting natural language into SQL query statements.
[0077] Specifically, in the implementation Figure 2 In the case of the embodiment shown, and Figure 2 When each module or unit of the device for converting natural language into SQL query statement described in the embodiment is implemented by software, the execution Figure 2 The software or program code required for the functions of each module / unit in the system may be partially or completely stored in the memory 304. The processor 302 executes the program code corresponding to each unit stored in the memory 304 to perform the aforementioned method of converting natural language into SQL query statements.
[0078] Embodiments of the present application also provide a computer-readable storage medium. The computer-readable storage medium can be any available medium capable of being stored by a computing device, or a data storage device such as a data center that contains one or more available media. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, or a magnetic tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state drive). The computer-readable storage medium includes instructions that instruct the computing device to execute the aforementioned method for converting natural language into SQL query statements.
[0079] The present application also provides a computer program product comprising one or more computer instructions that, when loaded and executed on a computing device, fully or partially generate the process or function described in the present application.
[0080] The computer instructions may 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 may be transmitted from one website, computer, or data center to another website, computer, or data center via wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) means.
[0081] When the computer program product is executed by a computer, the computer performs any of the aforementioned methods for converting natural language into SQL query statements. The computer program product may be a software installation package, and when any of the aforementioned methods for converting natural language into SQL query statements is needed, the computer program product may be downloaded and executed on the computer.
[0082] The descriptions of the processes or structures corresponding to the above figures have different emphases. For parts that are not described in detail in a certain process or structure, please refer to the relevant descriptions of other processes or structures.
[0083] The above description is only a specific implementation method of the present application, but the protection scope of the present application is not limited thereto. Any changes or replacements within the technical scope disclosed in the present application should be included in the protection scope of the present application.
Claims
1. A method for converting natural language into SQL query statements, characterized in that: The method comprises: Obtaining 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 based on the first context vector, feature vectors of each semantic dimension corresponding to the ambiguous word in the first natural language text, and a learnable projection matrix; determining a target semantic dimension according to a dependency relationship between each semantic dimension and the first context vector; According to the target semantic dimension, the first natural language text is converted into a first SQL query statement.
2. The method according to claim 1, characterized in that The determining a target semantic dimension according to the dependency relationship between each semantic dimension and the first context vector includes: Comparing the dependency relationship between each semantic dimension and the first context vector with a preset relationship threshold to obtain a comparison result; If the comparison result indicates that there is a semantic dimension whose dependency is greater than or equal to a preset relationship threshold, the semantic dimension whose dependency is greater than or equal to the preset relationship threshold is determined as the target semantic dimension.
3. The method according to claim 2, characterized in that The method further comprises: If the comparison result indicates that there is no semantic dimension whose dependency is greater than or equal to a preset relationship threshold, the semantic dimensions are sorted according to the strength of the dependency, and the top K semantic dimensions are determined as target semantic dimensions, where K is an integer greater than or equal to 2.
4. The method according to claim 1, wherein The method further comprises: After performing a query according to the first SQL query statement, obtaining the relevance of the query results, the execution efficiency of the query process, and the domain constraint compliance of the query results; determining a satisfaction level of the first SQL query statement based on the relevance, the execution efficiency, and the domain constraint compliance; The learnable projection matrix is adjusted according to the satisfaction of the first SQL query statement.
5. The method according to claim 4, characterized in that The relevance of the query results is obtained in the following way: The similarity between the feature vector of the query result and the feature vector of the user intention is calculated, and the similarity is used as the relevance of the query result.
6. The method according to claim 4, characterized in that The execution efficiency of the query process is obtained in the following way: Get the query duration and resource consumption during the query process; The execution efficiency is determined according to the query duration, the resource consumption, the query duration threshold, and the resource consumption threshold.
7. The method according to claim 4, characterized in that The domain constraint compliance of the query result is obtained in the following way: The domain constraint compliance of the query result is determined according to whether the first SQL query statement includes a filter condition.
8. A device for converting natural language into SQL query statements, characterized in that: The device comprises: An acquisition module, configured to acquire a first natural language text to be converted; an encoding module, 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, configured to determine, based on the first context vector, feature vectors of each semantic dimension corresponding to the ambiguous words in the first natural language text, and a learnable projection matrix, a dependency relationship between each semantic dimension and the first context vector; and determine a target semantic dimension based on the dependency relationship between each semantic dimension and the first context vector; A conversion module is used to convert the first natural language text into a first SQL query statement according to the target semantic dimension.
9. A computing device, characterized in that including memory and processor; One or more computer programs are stored in the memory, and the one or more computer programs include instructions; when the instructions are executed by the processor, the computing device executes the method according to 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, and the computer program is used to execute the method according to 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
Multi-round Chinese Text-to-SQL (Structured Query Language) method based on semantic rewriting
CN120162342A
Generation of Context-Aware Word Embedding Vectors for Given Semantic Properties of a Word Using Few Texts
US20240203003A1