Method for converting natural language into SQL (Structured Query Language) statement
By building a vector database and industry knowledge graph, combining BERT model and large language model, the problem of low accuracy of natural language to SQL statements in specific fields is solved, and SQL statement generation is achieved that is more accurate and consistent with the database structure.
Patent Information
- Application Number
- CN202510081477.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-20
- Publication Date
- 2025-04-25
AI Technical Summary
The existing natural language to SQL statement methods have low accuracy in specific fields, and are prone to inconsistent with the database, mainly because the model cannot handle database structures and terms in a specific field well and insufficient training data.
By building a vector database and industry knowledge graph, the BERT model is used to label the entity content and replace the text of natural language statements, and SQL statements are generated in combination with the large language model. The method includes converting the table description file into vectorized representations, constructing an entity dataset for training a BERT model, the entity dataset contains entity names and entity classifications, and replacing entity nouns in the statement through a knowledge graph query.
It improves the accuracy of natural language to SQL statements in specific fields, ensures that the generated SQL statements can accurately reflect the user's query intention and conform to the database structure, reducing the possibility of query errors.
Smart Images

Figure CN119884167A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of natural language processing, and in particular to a method for converting natural language into SQL statements. Background Art
[0002] In today's Internet era, all walks of life have accumulated a huge amount of data, which is stored in various databases. Users interact with this data a lot every day to obtain the knowledge and information they need. This process requires structured SQL statements. SQL is a standard programming language specifically used to manage and operate relational databases. It can help developers manually write SQL statements in related fields to query and operate data in the database in an efficient and accurate way, thereby processing various business needs expressed by users in natural language.
[0003] An existing patent with application number 202311178152.6 discloses a method for converting natural language statements into SQL statements, comprising the following steps: fine-tuning and training a large language model; obtaining a natural language statement to be converted; inputting the natural language statement into the fine-tuned and trained large language model to obtain a converted SQL statement.
[0004] The defects of the prior art are that the existing methods of converting natural language to SQL statements have low accuracy in specific fields, and it is easy for the converted SQL statements to have inconsistent table names and column names with the database. First, the database models in different fields are quite different. The database structure and query requirements in some professional fields are very different from those in general fields, resulting in the inability of models trained in these general fields to handle problems in other fields well. Secondly, databases in specific fields have unique structures and terminology, which further increases the difficulty of generating accurate SQL statements. Furthermore, the lack of training corpora in specific fields makes it difficult to obtain high-quality training data. These reasons all lead to the low accuracy of existing methods of converting natural language to SQL statements in specific fields. Summary of the invention
[0005] In view of the above-mentioned defects in the prior art, the present invention provides a method for converting natural language into SQL statements, which improves the accuracy of converting natural language into SQL statements in a specific field.
[0006] In order to achieve the above object, the technical solution adopted by the present invention is:
[0007] A method for converting natural language into SQL statements, comprising the following steps:
[0008] S1: Build vector database and industry knowledge graph based on table description file;
[0009] S2: Construct an entity dataset in the industry field for training the BERT model; the entity dataset includes entity names and entity categories;
[0010] S3: annotating the entity content of the natural sentence by the BERT model to obtain an annotated sentence; querying the knowledge graph to perform text replacement on the annotated sentence to obtain a replaced sentence;
[0011] S4: vectorizing the replaced statement to obtain a statement to be searched; searching the statement to be searched in the vector database and obtaining corresponding table structure information;
[0012] S5: Generate SQL statements through a large language model according to the table structure information.
[0013] Industry information is stored in the tables of the original database, and each table has a table description file. The table description file is a document used to describe in detail the structure, content, and purpose of each table in the database. It provides comprehensive information about the table to database administrators, developers, and data analysts, which helps to better understand and use the data in the database. The table description file usually contains: 1. Basic table information, such as the table name, a brief description of the overall function and purpose of the table. 2. Field information, such as field name, field data type, field description, default value, index information, etc. 3. Relationship information, such as foreign keys, associated tables, etc. 4. Constraint information, such as primary key constraints, unique constraints, check constraints, etc.
[0014] Convert the table description file in the table data into a vectorized representation, that is, convert the text information into a numerical vector that can be understood and processed by the computer. Store the vectorized table description file information in the vector database to facilitate subsequent efficient similarity search and retrieval operations.
[0015] An industry knowledge graph is a knowledge graph built specifically for a specific industry field. It provides support for data integration, semantic understanding, and intelligent applications in the industry by representing industry-related concepts, entities, attributes, and relationships in a structured form. Entity nouns are non-logical words and conjunctions. Entity nodes are the basic units in the knowledge graph, representing specific objects or concepts in the industry. These entity nodes can be table structure information entity nodes, field information entity nodes, relationship nodes, and so on. For example, the table structure information entity node uses the table in the original database as an entity node, and the salary table (salary) as an entity node. The node information is:
[0016] {"type":"Table",
[0017] "properties":{
[0018] "table_name":"salary",
[0019] "table_description":"Employee salary table, records the salary data of company employees"}
[0020] }
[0021] Each attribute column of the table is also regarded as an entity node, and the node information is:
[0022] {"type":"Field",
[0023] "properties":{
[0024] "field_name":"development_salary",
[0025] "field_description":"Employee salaries under R&D expenses"}
[0026] }
[0027] If the attribute relationship is also used as an entity node, the relationship between salary and development_salary is constructed:
[0028] {"type":"HAS_FIELD",
[0029] "from":"salary",
[0030] "to":"development_salary",
[0031] "properties":{}
[0032] }
[0033] According to the specific content of the industry field, the industry data in the original database is classified into different types. For example, if the entity data set L in the vertical field is constructed,<Ei,Ci>}, where Ei represents the entity name and Ci represents the entity classification, for example, L = {A company, Company}, if the natural sentence contains "A company", then "A company" is labeled as "Company". By classifying entities, the data set can be better organized and managed, making subsequent model training and application more targeted and efficient. Classification also helps the model better understand and process different types of entities, improving the accuracy and relevance of sentence extraction. The BERT model is a method model for pre-training language representation, which is widely used in various tasks such as text classification, named entity recognition, and question-answering systems. It learns rich language representation and semantic information through pre-training of a large amount of text data, and its training method is an existing technology. The entity dataset is used to train the BERT model, enabling it to recognize and extract professional entity nouns in natural sentences, and annotate the entity content of natural sentences according to the classification of entities.
[0034] Query the knowledge graph, perform text replacement on the annotated sentence, and obtain the replaced sentence. The replaced sentence can be in various forms. For example, for each entity noun that is not a logical word or a conjunction, find the corresponding table name and field name through the knowledge graph query. According to the knowledge graph query results, replace the entity noun with the corresponding table name and field name in the format of Table.Column. For example: replace "R&D staff salary" with salary.development_salary; replace "sales staff salary" with salary.sale_salary.
[0035] By vectorizing the replaced statements, we can get the vector representation of the statement to be retrieved. Then, we input this vector into the vector database for similarity search to obtain the table structure information most similar to the statement to be retrieved, and provide support for generating SQL statements.
[0036] Step S5 is to generate the final SQL statement through the large language model using the table structure information obtained through preprocessing and vectorization retrieval. The large language model (such as qwen1.5-7B) is a pre-trained deep learning model that can understand and generate natural language text. These models are pre-trained with a large amount of text data to learn rich language representation and semantic information. In the task of converting natural language to SQL, the large language model can understand the intention of the natural language query and generate the corresponding SQL statement. Table structure information refers to the table structure information retrieved from the vector database, including table name, field name, table description, etc. This information helps the large language model understand the specific data structure involved in the query, thereby generating accurate SQL statements.
[0037] Preferably, the step S1 comprises: converting the table description file into a unified JSON format.
[0038] The obtained table description file is standardized and converted into a unified JSON format. This standardization helps with subsequent data processing and analysis. First, the table description file is read from the original database, and key information is extracted from the table description file, including table name, table description, field name, and field description. This information will be used to build a standardized JSON format. Convert the extracted key information into a unified JSON format. The structure of the JSON format is set according to the actual application scenario, such as: converting in the form of JSON "Table: {(Description, Fields[Columni, Commenti], ......)}", where Table is the table name, Description is the description of the table, Fields is an array of fields of a table, where Columni of each element is the column field, and Commenti represents the meaning of the column.
[0039] Preferably, the step S3 comprises: correcting the natural sentence, and standardizing the numbers and time in the natural sentence.
[0040] Correcting natural sentences includes correcting spelling, grammar, punctuation, etc. that may appear in the input sentences. Standardize numbers, time and year to make them consistent with the storage format of the original database. By correcting the input natural sentences and standardizing numbers, time and year, the quality and consistency of the input sentences are ensured, providing high-quality input for subsequent entity recognition, knowledge graph query and SQL statement generation, and improving the accuracy and reliability of the entire system.
[0041] Preferably, the step S3 comprises: dividing the natural language into a plurality of words, and performing BIO annotation on the words.
[0042] Use word segmentation tools to segment natural language content and divide continuous text into independent vocabulary units. Word segmentation is an important step in Chinese natural language processing because there are no obvious separators between words in Chinese text. Perform BIO annotation on the text after word segmentation, which is a common sequence annotation method used to mark information such as entities and logical words in the text.
[0043] Preferably, the step S3 comprises: splitting the natural language into multiple clauses according to the logical words annotated by the BIO, and obtaining a clause set.
[0044] Splitting the annotated entities into clauses based on logical words to obtain a set of clauses helps simplify complex queries and improve the efficiency and accuracy of subsequent processing. Logical words refer to words used to connect multiple clauses in natural language queries, such as "and", "or", "but", etc. These logical words usually correspond to logical operators in SQL queries, such as "AND, OR", etc. Logical words are usually labeled as "B-Logic" or "I-Logic". Splitting the annotated statements into multiple clauses based on logical words, each clause can be processed independently, which helps simplify complex queries and makes them easier to convert into SQL statements. Collecting the split clauses into a set to form a clause set, each clause can be processed independently to generate the corresponding SQL fragment.
[0045] Preferably, the step S3 comprises: vectorizing the entity nouns in the annotated sentence to obtain a text to be matched, calculating the cosine similarity between the text to be matched and the entity nouns in the vector database, and finding the most similar entity noun for replacement.
[0046] In order to solve the problem of mismatch between Chinese input content and entities in the knowledge graph, through vectorization representation and cosine similarity calculation, the entity in the vector database that is most similar to the input entity can be found, thereby improving the accuracy of entity matching. The annotated entity nouns are vectorized to obtain the text to be matched, and the cosine similarity between the text to be matched and the entity vector in the vector database is calculated to find the most similar entity and replace it. Through the above steps, each annotated entity noun can be vectorized and represented, and its cosine similarity with the entity in the vector database can be calculated to find the most similar entity, which solves the problem of mismatch between Chinese input content and entities in the knowledge graph and improves the accuracy and reliability of entity matching.
[0047] Preferably, the step S4 comprises: extracting common parts and different parts of the entity nouns in the clauses of the replaced sentence, and forming attribute state pairs.
[0048] For each sub-statement that has been converted through the knowledge graph, it is necessary to extract the common part and the different part when these entities belong to the attribute columns of different tables. The common part refers to the entity that appears in multiple clauses, usually a table name or a common field. The different part refers to the unique entity in each clause, usually a field of a table. Attribute-state pair, a tuple containing the common part and the different part. The above steps help to generate accurate SQL statements, especially when dealing with complex queries involving multiple tables.
[0049] Preferably, the step S5 comprises: constructing a corresponding query statement pair for each table, and fine-tuning the large language model using QLora.
[0050] For the content of each table, build the corresponding query statement pair. The query statement pair includes single table field query, connection field query, etc., which are used to fine-tune the training model. Use QLora method for fine-tuning. QLora is a fine-tuning method that combines LoRA and quantization technology to reduce the memory resource consumption of the model while maintaining or improving model performance. Through the above steps, the large language model is fine-tuned and trained, and the QLora method is used to reduce the memory resource consumption of the large language model while maintaining or improving the model performance, which helps to improve the accuracy and efficiency of the model in specific fields and ensure that the generated SQL statements meet the database structure and query requirements.
[0051] Preferably, the method comprises step S6: extracting the Select statement of the large language model by regular expression, and verifying the form of the SQL text by sqlparse.
[0052] To ensure that the generated SQL statements conform to the correct SQL text format, the Select statement content in the large language model answer is extracted through regular expressions, and the sqlparse library is used for verification and checking. Since the SQL statements generated by the large language model may contain some additional content, it is necessary to extract the valid Select statement content through regular expressions. sqlparse is a Python library that can parse and format SQL statements to help check whether the syntax of SQL statements is correct. Verifying the format of SQL text helps improve the accuracy and reliability of generated SQL statements and ensure that they can be correctly executed in the database.
[0053] Compared with the prior art, the beneficial effects of the present invention are as follows:
[0054] By constructing an industry entity data set containing entity names and entity classifications, the BERT model is trained to enable it to recognize and extract professional entity nouns in natural language, thereby improving the model's ability to recognize industry terms and thus improving accuracy. Classifying and managing entities helps the model better understand and process different types of entities, ensuring that the correct tables and fields can be selected when generating SQL statements. The BERT model is used to annotate the entity content of natural language queries and identify the key entities and their classes in the query. By querying the knowledge graph, the entity nouns in the processed statements are replaced with the corresponding table names and field names to ensure that the generated SQL statements can accurately reflect the user's query intent. Through the structured information of the knowledge graph, the ambiguity in natural language is reduced, so that the model can select the most appropriate tables and fields when generating SQL statements. Through the above steps and technical means, the technical solution of the present invention can effectively improve the accuracy of natural language to SQL statements in specific fields. These links jointly ensure that the system can accurately understand the user's query intent, avoid querying the wrong industry field, and generate SQL statements that conform to the database structure. By building a knowledge graph for a specific industry and using industry-specific entity data sets to train the BERT model, the system can more accurately understand and identify terms and concepts in specific fields, thereby improving the accuracy of natural language to SQL statements in specific fields. At the same time, by leveraging the powerful semantic understanding capabilities of the large language model and combining the table structure information in the vector database, it can more accurately generate SQL statements that meet user intent and reduce ambiguity and errors. BRIEF DESCRIPTION OF THE DRAWINGS
[0055] Figure 1 is a schematic diagram of the method of Example 1;
[0056] Figure 2 is a flow chart of the method of Example 1;
[0057] Figure 3 It is a step schematic diagram of step S3 of Example 1. DETAILED DESCRIPTION
[0058] In order to make the technical means, creative features, objectives and effects of the invention easier to understand, the invention is further described with reference to specific diagrams. However, the invention is not limited to the following implementation cases.
[0059] It should be noted that the structures, proportions, sizes, etc. illustrated in the drawings in this specification are only used to match the contents disclosed in the specification so as to facilitate understanding and reading by persons familiar with this technology. They are not used to limit the conditions under which the present invention can be implemented, and therefore have no substantive technical significance. Any structural modification, change in proportion or adjustment of size, without affecting the effects and purposes that can be achieved by the present invention, should still fall within the scope of the technical contents disclosed by the present invention.
[0060] Embodiment 1:
[0061] like Figure 1 , Figure 2 A method for converting a natural language into an SQL statement is shown, comprising the following steps:
[0062] S1: Build a vector database and industry knowledge graph based on the table description file.
[0063] S1.1: When the application starts, all table structure information is obtained through code according to the original database connected, and the table description file is obtained.
[0064] Industry information is stored in the tables of the original database, and each table has a table description file. The table description file is a document used to describe in detail the structure, content, and purpose of each table in the database. It provides comprehensive information about the table to database administrators, developers, and data analysts, which helps to better understand and use the data in the database. The table description file usually contains: 1. Basic table information, such as the table name, a brief description of the overall function and purpose of the table. 2. Field information, such as field name, field data type, field description, default value, index information, etc. 3. Relationship information, such as foreign keys, associated tables, etc. 4. Constraint information, such as primary key constraints, unique constraints, check constraints, etc.
[0065] S1.2: Use the text embedding model to obtain vectorized representations of the tables for various table description files. This embodiment uses Alibaba's NLP Chinese text conversion model and sets the maximum text length to 512. It is then stored in the Milvus vector database. When storing, a Collection is created with (Id, Table, Description) as the storage field, where Table represents the table name, Description represents the structural information of the table, and "Table" is used as an index to speed up the query. Convert the table description file in the table data into a vectorized representation, that is, convert the text information into a numerical vector that can be understood and processed by the computer. Store the vectorized table description file information in the vector database to facilitate subsequent efficient similarity search and retrieval operations.
[0066] S1.3: Convert the table description file to a unified JSON format.
[0067] The obtained table description file is standardized and converted into a unified JSON format. This standardization helps with subsequent data processing and analysis. First, the table description file is read from the original database, and key information is extracted from the table description file, including table name, table description, field name, and field description. This information will be used to build a standardized JSON format. Convert the extracted key information into a unified JSON format. The structure of the JSON format is set according to the actual application scenario, such as: converting in the form of JSON "Table: {(Description, Fields[Columni, Commenti], ......)}", where Table is the table name, Description is the description of the table, Fields is an array of fields of a table, where Columni of each element is the column field, and Commenti represents the meaning of the column.
[0068] S1.4 Since there may be mismatches in the entity names in the input text, such as typos or the same meaning but different field names, the column attributes of each table are extracted and deduplicated, vectorized and represented through the text embedding model, and stored in the vector database.
[0069] S1.5 builds the corresponding industry knowledge graph based on the standardized table JSON file, takes the table structure information and field information as entity nodes, and creates relationship nodes based on the relationship between tables and fields, and the relationship between tables. There are two types of relationships, one is HAS_FIELD, which represents the relationship between tables and fields; the other is HAS_RELATION, which represents the relationship between tables. The industry knowledge graph is a knowledge graph specifically built for a specific industry field. It provides support for data integration, semantic understanding and intelligent applications in the industry by representing industry-related concepts, entities, attributes and relationships in a structured form. Entity nouns are non-logical words and conjunctions. Entity nodes are the basic units in the knowledge graph, representing specific objects or concepts in the industry. These entity nodes can be table structure information entity nodes, field information entity nodes, relationship nodes, etc. For example, the table structure information entity node takes the table in the original database as an entity node, and the salary table (salary) as an entity node, and the node information is:
[0070] {"type":"Table",
[0071] "properties":{
[0072] "table_name":"salary",
[0073] "table_description":"Employee salary table, records the salary data of company employees"}
[0074] }
[0075] Each attribute column of the table is also regarded as an entity node, and the node information is:
[0076] {"type":"Field",
[0077] "properties":{
[0078] "field_name":"development_salary",
[0079] "field_description":"Employee salaries under R&D expenses"}
[0080] }
[0081] If the attribute relationship is also used as an entity node, the relationship between salary and development_salary is constructed:
[0082] {"type":"HAS_FIELD",
[0083] "from":"salary",
[0084] "to":"development_salary",
[0085] "properties":{}
[0086] }
[0087] S2: Build an entity dataset in the industry field for training the BERT model; the entity dataset contains entity names and entity categories.
[0088] According to the specific content of the industry field, the industry data in the original database is classified into different types. For example, if the entity data set L in the vertical field is constructed,<Ei,Ci>}, where Ei represents the entity name and Ci represents the entity classification, for example, L = {A company, Company}, if the natural sentence contains "A company", then "A company" is labeled as "Company". By classifying entities, the data set can be better organized and managed, making subsequent model training and application more targeted and efficient. Classification also helps the model better understand and process different types of entities, improving the accuracy and relevance of sentence extraction. The BERT model is a method model for pre-training language representation, which is widely used in various tasks such as text classification, named entity recognition, and question-answering systems. It learns rich language representation and semantic information through pre-training of a large amount of text data, and its training method is an existing technology. The entity dataset is used to train the BERT model, enabling it to recognize and extract professional entity nouns in natural sentences, and annotate the entity content of natural sentences according to the classification of entities.
[0089] S3: Use the BERT model to annotate the entity content of natural sentences to obtain annotated sentences; query the knowledge graph and perform text replacement on the annotated sentences to obtain replaced sentences.
[0090] S3.1: Correct natural sentences and standardize numbers and times in natural sentences.
[0091] like Figure 3 As shown in the figure, correcting natural sentences includes using pycorrector to correct spelling, grammar, punctuation, etc. that may appear in the input sentences. The numbers, time and year are uniformly standardized to be consistent with the storage format of the original database. By correcting the input natural sentences and standardizing the numbers and time and year, the quality and consistency of the input sentences are ensured, which provides high-quality input for subsequent entity recognition, knowledge graph query and SQL statement generation, and improves the accuracy and reliability of the entire system. pycorrector is a Python-based text error correction library that can automatically detect and correct common spelling errors, grammatical errors and punctuation errors.
[0092] S3.2 passes the standardized natural sentences into the pre-trained first-layer BERT model to annotate the entity content.
[0093] S3.2.1: Divide the natural language into multiple words and annotate the words with BIO. Build the BERT training data set, first segment some natural language content with the HanLP word segmentation tool and annotate them with BIO, and add [CLS] and [SEP] special markers to indicate the start and end of the sentence. The BIO annotation rules are as follows:
[0094] B-Entity: indicates the beginning of the entity.
[0095] I-Entity: represents the middle part of the entity.
[0096] B-Logic: indicates the beginning of a logical word.
[0097] I-Logic: represents the middle part of a logical word.
[0098] B-Operator-Ca: represents operators (addition, subtraction, multiplication, division)
[0099] B-Operator-Co: indicates comparison operator (such as greater than, less than, equal to, etc.)
[0100] O: indicates ordinary non-entity words or non-logical words
[0101] Word segmentation is an important step in Chinese natural language processing because there are no obvious separators between words in Chinese text. The text after word segmentation is annotated with BIO, which is a common sequence annotation method used to mark information such as entities and logical words in the text.
[0102] S3.2.2 Training entity annotation based on BERT model
[0103] The model input sequence X is:
[0104] X=[[CLS],x1,x2,…,xn,[SEP],y1,y2,…,[SEP]]
[0105] Use the BERT model for pre-training and fine-tune it on the training dataset to optimize the accuracy of named entity recognition.
[0106] S3.3: Split the natural language into multiple clauses according to the logical words annotated by BIO, and obtain the clause set Q = {q1, q2, ...}.
[0107] Splitting the annotated entities into clauses based on logical words to obtain a set of clauses helps simplify complex queries and improve the efficiency and accuracy of subsequent processing. Logical words refer to words used to connect multiple clauses in natural language queries, such as "and", "or", "but", etc. These logical words usually correspond to logical operators in SQL queries, such as "AND, OR", etc. Logical words are usually labeled as "B-Logic" or "I-Logic". Splitting the annotated statements into multiple clauses based on logical words, each clause can be processed independently, which helps simplify complex queries and makes them easier to convert into SQL statements. Collecting the split clauses into a set to form a clause set, each clause can be processed independently to generate the corresponding SQL fragment.
[0108] S3.4: Vectorize the entity nouns in the annotated sentences to obtain the text to be matched, calculate the cosine similarity between the text to be matched and the entity nouns in the vector database, and find the most similar entity noun to replace. The calculation formula is as follows:
[0109]
[0110] In order to solve the problem of mismatch between Chinese input content and entities in the knowledge graph, through vectorization representation and cosine similarity calculation, the entity in the vector database that is most similar to the input entity can be found, thereby improving the accuracy of entity matching. The annotated entity nouns are vectorized to obtain the text to be matched, and the cosine similarity between the text to be matched and the entity vector in the vector database is calculated to find the most similar entity and replace it. Through the above steps, each annotated entity noun can be vectorized and represented, and its cosine similarity with the entity in the vector database can be calculated to find the most similar entity, which solves the problem of mismatch between Chinese input content and entities in the knowledge graph and improves the accuracy and reliability of entity matching.
[0111] S3.5: After word segmentation, the entity nouns of each non-logical word and conjunction are queried through the knowledge graph to replace the text and replace it into the form of Table.Column to obtain the replaced sentence.
[0112] Query the knowledge graph, perform text replacement on the annotated sentence, and obtain the replaced sentence. The replaced sentence can be in various forms. For example, for each entity noun that is not a logical word or a conjunction, find the corresponding table name and field name through the knowledge graph query. According to the knowledge graph query results, replace the entity noun with the corresponding table name and field name in the format of Table.Column. For example: replace "R&D staff salary" with salary.development_salary; replace "sales staff salary" with salary.sale_salary.
[0113] S3.6: For each clause qi in the clause set Q, if there is an entity that needs to be calculated, replace it with the required entity pair and operation symbol from the vector library through vector representation to transform it
[0114] S4: Vectorize the replaced statement to obtain the statement to be retrieved; retrieve the statement to be retrieved in the vector database and obtain the corresponding table structure information.
[0115] By vectorizing the replaced statements, we can get the vector representation of the statement to be retrieved. Then, we input this vector into the vector database for similarity search to obtain the table structure information most similar to the statement to be retrieved, and provide support for generating SQL statements.
[0116] S4.1: Vectorize each processed sub-sentence qi and calculate the cosine similarity using the Milvus vector search tool. The calculation formula is as follows:
[0117]
[0118] The corresponding database table structure information T = (Id, Table, Description) is retrieved from the constructed vector database, where Table represents the table name, and Description represents the structure information of the table, including all attribute column information. Considering the complexity of the query, this embodiment selects to query up to 10 table structures to obtain the related list D = {T1, T2, ..., T10}.
[0119] S4.2: For each entity in each sub-statement qi that has been converted through the knowledge graph, if it is an attribute column of different tables, extract the common parts and different parts according to the table name and table structure information to form an attribute state pair COL_STATE=<SHARE,{E1,E2,…,En}> SHARE represents the common part, Ei represents the unique part of the clause, and each Ei is represented as Table.Column. This embodiment uses a large language model that has been fine-tuned through pre-training to implement entity extraction and classification tasks, and combines the obtained industry knowledge graph for attribute mapping.
[0120] For each sub-statement that has been converted through the knowledge graph, it is necessary to extract the common part and the different part when these entities belong to the attribute columns of different tables. The common part refers to the entity that appears in multiple clauses, usually a table name or a common field. The different part refers to the unique entity in each clause, usually a field of a table. Attribute-state pair, a tuple containing the common part and the different part. The above steps help to generate accurate SQL statements, especially when dealing with complex queries involving multiple tables.
[0121] S5: Generate SQL statements through a large language model based on the table structure information.
[0122] Step S5 is to generate the final SQL statement through the large language model using the table structure information obtained through preprocessing and vectorization retrieval. The large language model (such as qwen1.5-7B) is a pre-trained deep learning model that can understand and generate natural language text. These models are pre-trained with a large amount of text data to learn rich language representation and semantic information. In the task of converting natural language to SQL, the large language model can understand the intention of the natural language query and generate the corresponding SQL statement. Table structure information refers to the table structure information retrieved from the vector database, including table name, field name, table description, etc. This information helps the large language model understand the specific data structure involved in the query, thereby generating accurate SQL statements.
[0123] S5.1: Construct corresponding query statement pairs for each table and use QLora to fine-tune the large language model.
[0124] For the content of each table, build the corresponding query statement pair. The query statement pair includes single table field query, connection field query, etc., which are used to fine-tune the training model. Use QLora method for fine-tuning. QLora is a fine-tuning method that combines LoRA and quantization technology to reduce the memory resource consumption of the model while maintaining or improving model performance. Through the above steps, the large language model is fine-tuned and trained, and the QLora method is used to reduce the memory resource consumption of the large language model while maintaining or improving the model performance, which helps to improve the accuracy and efficiency of the model in specific fields and ensure that the generated SQL statements meet the database structure and query requirements.
[0125] This embodiment adopts the large language model as the qwen1.5-7B model, and constructs a corresponding query statement pair SQL_TRAIN={QUESTION,ANSWER} for the content of each table. The question pair includes single table field query, connection field query, etc., to achieve fine-tuning training of the model.
[0126] Using the constructed training text, the model is fine-tuned using the QLora method. When LoRA performs weight matrix decomposition, it is further quantized to achieve smaller memory resource consumption.
[0127] Assume that the original weight matrix is W, LoRA decomposes the training matrix, and the introduced low-rank matrices are A and B, then the new weight matrix W′ can be expressed as:
[0128]
[0129] where A∈R d×r , B∈R r×k , r<<(d,k), r represents the rank of the matrix, and lora_alpha represents the scaling factor.
[0130] S5.2: Construct a prompt for the prompt word based on the queried table structure list D, including defining the role positioning of the large model (database language expert), the retrieved table structure list D = {T1, T2, ..., T10} (the structure information of the table that may be used), if there are sub-statements, transform each sub-statement and then connect it, which includes the corresponding state (AND, OR, NONE), the information to be calculated Entity = {(E1, E2, ..., Ei), OPERATION} (including the set entity attributes and operations), COL_STATE (connection part), some other specific industry professional terminology document information, etc.
[0131] S5.3: Perform Few-Shot on large models to improve the conversion success rate of the model by combining some examples.
[0132] S5.4: The large model generates a corresponding answer to the input sentence based on the prompt word template.
[0133] S6: Use regular expressions to extract the Select statements of the large language model and use sqlparse to verify the form of the SQL text.
[0134] To ensure that the generated SQL statements conform to the correct SQL text format, the Select statement content in the large language model answer is extracted through regular expressions, and the sqlparse library is used for verification and checking. Since the SQL statements generated by the large language model may contain some additional content, it is necessary to extract the valid Select statement content through regular expressions. sqlparse is a Python library that can parse and format SQL statements to help check whether the syntax of SQL statements is correct. Verifying the format of SQL text helps improve the accuracy and reliability of generated SQL statements and ensure that they can be correctly executed in the database.
[0135] The present invention constructs an industry knowledge graph, utilizes its relationship information, pre-processes sentences in advance, and uses the BERT model to perform entity annotation, and then splits the sentences, thereby solving the problem of inaccurate conversion of table fields by the model when there are too many database tables in the industry, so as to improve the success rate of the model in converting natural language into SQL statements and the overall effect.
[0136] By constructing an industry entity data set containing entity names and entity classifications, the BERT model is trained to enable it to recognize and extract professional entity nouns in natural language, thereby improving the model's ability to recognize industry terms and thus improving accuracy. Classifying and managing entities helps the model better understand and process different types of entities, ensuring that the correct tables and fields can be selected when generating SQL statements. The BERT model is used to annotate the entity content of natural language queries and identify the key entities and their classes in the query. By querying the knowledge graph, the entity nouns in the processed statements are replaced with the corresponding table names and field names to ensure that the generated SQL statements can accurately reflect the user's query intent. Through the structured information of the knowledge graph, the ambiguity in natural language is reduced, so that the model can select the most appropriate tables and fields when generating SQL statements. Through the above steps and technical means, the technical solution of the present invention can effectively improve the accuracy of natural language to SQL statements in specific fields. These links jointly ensure that the system can accurately understand the user's query intent, avoid querying the wrong industry field, and generate SQL statements that conform to the database structure. By building a knowledge graph for a specific industry and using industry-specific entity data sets to train the BERT model, the system can more accurately understand and identify terms and concepts in specific fields, thereby improving the accuracy of natural language to SQL statements in specific fields. At the same time, by leveraging the powerful semantic understanding capabilities of the large language model and combining the table structure information in the vector database, it can more accurately generate SQL statements that meet user intent and reduce ambiguity and errors.
Claims
1. A method for converting natural language into SQL statements, characterized in that: The following steps are involved: S1: Build vector database and industry knowledge graph based on table description file; S2: Construct an entity dataset in the industry field for training the BERT model; the entity dataset includes entity names and entity categories; S3: annotating the entity content of the natural sentence by the BERT model to obtain an annotated sentence; querying the knowledge graph to perform text replacement on the annotated sentence to obtain a replaced sentence; S4: vectorizing the replaced statement to obtain a statement to be searched; searching the statement to be searched in the vector database and obtaining corresponding table structure information; S5: Generate SQL statements through a large language model according to the table structure information.
2. The method for converting natural language to SQL statements according to claim 1, characterized in that: The step S1 includes: converting the table description file into a unified JSON format.
3. The method for converting natural language to SQL statements according to claim 1, characterized in that: The step S3 includes: correcting the natural sentence and standardizing the numbers and time in the natural sentence.
4. The method for converting natural language to SQL statements according to claim 3, characterized in that: The step S3 includes: dividing the natural language into a plurality of words, and performing BIO annotation on the words.
5. The method for converting natural language to SQL statements according to claim 4, characterized in that: The step S3 includes: splitting the natural language into multiple clauses according to the logical words annotated by the BIO, and obtaining a clause set.
6. The method for converting natural language to SQL statements according to claim 5, characterized in that: The step S3 includes: vectorizing the entity nouns in the annotated sentence to obtain a text to be matched, calculating the cosine similarity between the text to be matched and the entity nouns in the vector database, and finding the most similar entity noun for replacement.
7. The method for converting natural language to SQL statements according to claim 6, characterized in that: The step S4 comprises: extracting common parts and different parts of the entity nouns in the clause of the replaced sentence, and forming attribute state pairs.
8. The method for converting natural language to SQL statements according to claim 1, characterized in that: The step S5 includes: constructing a corresponding query statement pair for each table, and fine-tuning the large language model using QLora.
9. The method for converting natural language to SQL statements according to claim 1, characterized in that: The method comprises step S6: extracting the Select statement of the large language model by using a regular expression, and verifying the form of the SQL text by using sqlparse.
Citation Information
Patent Citations
Method for converting natural language statement into SQL statement
CN117194477A
Bridge management and maintenance field-oriented few-sample table question and answer method
CN116303971A
Method, system and device for converting natural language to SQL (Structured Query Language) query statement and medium
CN118779342A
Intelligent SQL query generation method and system based on large language model
CN118861081A
Method and system for converting natural language into SQL (Structured Query Language) statement
CN118916381A
Cited By
Method and device for realizing semantic search
CN120541200A
Query statement conversion method and device based on large model, equipment and medium
CN120578683A
Method and system for performing natural language question and answer directly according to dimension value
CN121144446A