A text2sql semantic parsing method for a domain-oriented large language model

CN118377796BActive Publication Date: 2026-10-09HANGZHOU DIANZI UNIV +1
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202410660884.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-05-27
Publication Date
2026-10-09
Estimated Expiration
2044-05-27

AI Technical Summary

Technical Problem

然而,这些方法需要大量标注的训练数据,并且容易受到数据噪声和语义歧义的影响,例如用户在提问的过程中可能会出现拼写错误或意图模糊等问题,这些错误会导致模型生成错误的SQL语句

Benefits of technology

[0049]By employing the aforementioned technical solution, a knowledge base is constructed based on a domain database and domain background knowledge. This knowledge and rules of the professional domain are integrated into the knowledge base, enabling the large model to better utilize domain knowledge to optimize the understanding and parsing process of natural language queries. The model is fine-tuned, evaluated, and optimized using a Text-SQL dataset generated based on the SQL-to-Text model, allowing it to better adapt to the semantic and syntactic rules of the specific domain. This reduces semantic bias and errors, resulting in a highly efficient Text2Sql model. Furthermore, by optimizing the user's natural language input through a pre-set prompt and knowledge base, ambiguous information in the query is enhanced, and ambiguity and misunderstanding are reduced, ultimately improving the accuracy and robustness of Text2SQL.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118377796B_ABST
    Figure CN118377796B_ABST
Patent Text Reader

Abstract

The application discloses a Text2SQL semantic parsing method for a domain large language model, comprising the following steps: S1, connecting a domain database and storing database mode information; S2, constructing a knowledge base based on the domain database and domain background knowledge and mounting the knowledge base into a large language model; S3, generating a Text-SQL data set based on a SQL-to-Text model; S4, fine-tuning, evaluating and optimizing the large language model using the Text-SQL data set, and constructing a high-performance Text2Sql model; S5, inputting a natural language question and optimizing the question based on a Prompt and a domain knowledge base; S6, parsing semantics through the Text2Sql model and designing a Prompt reasoning result based on requirements. The method optimizes natural language question input based on a Prompt, fine-tunes a domain large model and mounts a domain knowledge base for data enhancement generation, so that complex query requirements can be realized without relying on tedious manual work, and the risk of data noise and semantic ambiguity is effectively reduced. The method can also be applied to various fields and improve the accuracy of Text2SQL.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of natural language processing technology, specifically to a Text2SQL semantic parsing method oriented towards a large domain language model. Background Technology

[0002] With the rapid development of Natural Language Processing (NLP) and Artificial Intelligence (AI) technologies, especially the widespread use of large language models since 2023, the need to access corresponding databases using natural language, generate corresponding SQL scripts, and output the results has become increasingly urgent. The aim is to simplify database operations by enabling interaction with databases through natural language, allowing more people to easily communicate and manipulate databases. In the database field, Text2SQL semantic parsing is a key technology used to convert natural language queries into Structured Query Language (SQL) queries for database manipulation and retrieval.

[0003] Currently, some methods for generating SQL statements are rule-based and template-based. However, these methods only apply to specific databases and query types, lack support for complex queries, and cannot cover all situations. These methods also require significant manual work to create and maintain rules. Another group of methods is based on machine learning and deep learning techniques, such as the Seq2Seq model framework. However, these methods require large amounts of labeled training data and are susceptible to data noise and semantic ambiguity. For example, users may make spelling errors or have unclear intent when asking questions, which can lead to the model generating incorrect SQL statements. Summary of the Invention

[0004] This invention addresses the shortcomings of existing technologies by proposing a Text2SQL semantic parsing method oriented towards domain-specific large language models. It utilizes a method that optimizes natural language input based on Prompt, pre-trains and fine-tunes a domain-specific large model, and then augments the data using a domain knowledge base. This approach enables complex query requirements without relying on tedious manual work, while effectively reducing the risks of data noise and semantic ambiguity. This method can also be applied to various domains and improve the accuracy of Text2SQL.

[0005] To solve the above-mentioned technical problems, the technical solution of the present invention is as follows:

[0006] A Text2SQL semantic parsing method oriented towards a domain-wide large language model includes the following steps:

[0007] S1. Connect to the domain database and store database schema information;

[0008] S2. Construct a knowledge base based on the domain database and domain background knowledge and mount it onto the large model;

[0009] S3. Generate a Text-SQL dataset based on the SQL-to-Text model;

[0010] S4. Use the Text-SQL dataset to fine-tune, evaluate, and optimize the model to build a high-performance Text2Sql model;

[0011] S5. Input a natural language question and optimize it based on the prompt and domain knowledge base;

[0012] S6. Parse the semantics using the Text2Sql model and design the Prompt inference results based on the requirements;

[0013] Preferably, in step S1, the database schema information storage method is a columnar storage method that organizes and stores data in a columnar manner. During storage, the database schema S = {T1, T2, ..., T...} is defined. i , ..., T n , E}, where T i Let T be the i-th table, and E represent the primary and foreign key relationships between the tables. i Contains column C i ={C i1 C ij , ...}, where C i1 This represents the first column of the i-th data table.

[0014] Preferably, the knowledge base construction method is as follows:

[0015] S2-1. Determine the application area and collect general background knowledge of that area to form a document (Text);

[0016] S2-2. Extract table names, column names, and partial data from each table in the domain database using natural language processing techniques, perform preprocessing and annotation, and then use an NLP model based on the background knowledge from S2-1 to semantically interpret and annotate the table names, column names, and partial data from each table, storing the information in a dictionary.

[0017] remark i ={"table_name": table i "Column name": column ij "data": data ij}

[0018] S2-3. Convert the domain background knowledge document from S2-1 and the annotation dictionary from S2-2 into word vectors using a pre-trained word embedding model (Word2Vec). For each word in the knowledge document and database information, assume the input is represented as a word vector V, and the center word w. c The word vectors are Context word w o word vectors The output layer uses the Softmax function for probability normalization. The formulas for the objective function and the Softmax function are as follows:

[0019] L = -logP(w) o |w c )

[0020]

[0021] Where V b V represents all words in the vocabulary. w The word vector representing word w;

[0022] S2-4. Store the word vectors from S2-3 in a vector database to establish a domain knowledge base structure, including defining the attributes, relationships, and categories of the terms. Associate the word vectors stored in the vector database with the corresponding terms in the knowledge base to supplement relevant semantic information. Then, expand the knowledge base by adding entity relationships and attribute information to make it more comprehensively reflect domain knowledge, thereby constructing a domain knowledge base.

[0023] Preferably, the specific method in step S3 is as follows:

[0024] S3-1. Design the Prompt instruction to "generate multiple corresponding problem descriptions or explanations for a given SQL statement". The Prompt should also include contextual information, such as descriptions of the tables, fields, and values ​​involved in the SQL statement, as well as database schema information, so as to use the Prompt to build a model with SQL-to-Text interpretation capabilities, i.e., an SQL-to-Text model;

[0025] S3-2. Take the correctly generated SQL statements from the domain database application, database audit logs, and test cases, and use the SQL-to-Text model in S3-1 to transform each SQL statement into multiple similar SQL interpretations or questions.

[0026] S3-3, Define a Text-SQL dataset D = {D1, D2, ..., D...} i , ..., D nThis includes a natural language statement Q and a corresponding correct, executable SQL statement. The similarity scores between the multiple SQL interpretations generated in S3-2 and the context information in the Prompt are calculated. The natural language question with the highest similarity is selected as Q in the dataset, and the highest similarity Q and its corresponding SQL statement are combined to form a Text-SQL data pair D. i ={Q i SQL i Store it in dataset D;

[0027] Preferably, in step S3-3, the similarity score is calculated as follows:

[0028] Cosine similarity measures the angle between two vectors, and the formula is: A and B are both multidimensional vectors.

[0029] Preferably, the specific method in step S4 is as follows:

[0030] S4-1. Divide the Text-SQL dataset into training and test sets according to a certain ratio. A common ratio is TS = 0.8 * TS_train + 0.2 * TS_test. When dividing, ensure that the data distribution and feature distribution of the training and test sets are as consistent as possible.

[0031] S4-2. Fine-tune the model using the pre-defined training set, adjusting the model parameters to suit the specific Text2Sql task. Define a loss function (mean squared error) to represent the degree of difference between the model's predictions and the true values, where the true value of the i-th sample is y. i The predicted value is The sample size is n, and the formula is expressed as:

[0032]

[0033] S4-3. Calculate and evaluate the fine-tuned model using the pre-defined test set. Define true positives (TP) as the number of SQL queries generated by the Text2Sql model that match real SQL queries; false negatives (FN) as the number of queries that the Text2Sql model fails to generate that match real SQL queries; and false positives (FP) as the number of SQL queries generated by the Text2Sql model that do not match real SQL queries. Common evaluation metrics include recall, precision, and F1 score, expressed by the following formulas:

[0034]

[0035]

[0036]

[0037] S4-4. Based on the evaluation results, use an optimization algorithm (gradient descent) to minimize the loss function and find the optimal model parameters to achieve better performance and generalization ability. Define the model parameters as θ, the learning rate as α, and the loss function as MSE, expressed by the formula:

[0038]

[0039] Preferably, the specific method in step S5 is as follows:

[0040] S5-1. Define a natural language set Z = {Q1, Q2, ..., Q...} i Q n}, where Z has a total of n sentences and Q i It contains P i The Chinese character q it Let t∈[1,P] represent the word in the i-th sentence. The word q... it Sentences consisting of t∈[1,P] are first processed by the word embedding matrix w in the general text embedding model m3e-base. e Convert words into word vectors, x it =w e q it ;

[0041] S5-2. Design a Prompt based on the domain knowledge base in S2 to analyze and optimize natural language problems. A Prompt, consisting of instructions, context information, input data to be processed, and a specified output content type or format, is represented as P = Instruction + Context + Input + Output, where instructions are mandatory and the others are optional.

[0042] S5-3, When inputting natural language Q i When it is time, it will first be converted into a vector. i Then, it retrieves a set of vectors V from the knowledge base that have a high similarity to the given vector. i ={vector i0 , ...,rector ij Then, based on the matching degree between vectors, the vector set is sorted, and the top-k knowledge results with the highest matching degree are selected as context and encapsulated in the Prompt for the Text2Sql model to refer to, understand, and generate better input. The formula is expressed as:

[0043] Result Top-k =Max_Score Top-k (Sort(Match(Qi V i )))

[0044] Preferably, the specific method in step S6 is as follows:

[0045] S6-1. Based on the enhanced Q in S5-3 S Retrieve the table with the highest matching degree from the database. and column Then go to the knowledge base to find the corresponding explanation and note K. T&C Design instructions I in combination with different requirements request and integration As prompt words in the Text2Sql model.

[0046] S6-2. If the requirement is an SQL statement, define the generated SQL as {K, T, C, V, OP}, where K represents the SQL statement keywords, such as "select" and "where"; T and C represent the data tables and columns involved in the SQL statement, respectively; V represents the values ​​appearing in the SQL statement, which are related to the data type of the corresponding column; and OP represents some data operators in the SQL statement, such as ">" and "=". Use the Prompt function in S6-1 and design instruction I. request For "Recall-based Data Table" List And the explanation notes K in the knowledge base T&C The optimized query question Q S "Convert it into the corresponding SQL statement and output it as the final result." This is used as a prompt in the Text2Sql model to parse and generate the SQL statement corresponding to the question. It can also be stored as an intermediate state for subsequent SQL optimization and other operations to obtain more efficient and accurate results.

[0047] S6-3. If the requirement is the final result after executing SQL, the instructions in the Prompt remain unchanged. The SQL statement generated by parsing the Text2Sql model is input into the SQL executor environment for execution, and the result after execution is returned. At the same time, the results can be further understood and analyzed through the explanatory notes in the Prompt, and data analysis and visualization can be performed.

[0048] This invention has the following characteristics and beneficial effects:

[0049] By employing the aforementioned technical solution, a knowledge base is constructed based on a domain database and domain background knowledge. This knowledge and rules of the professional domain are integrated into the knowledge base, enabling the large model to better utilize domain knowledge to optimize the understanding and parsing process of natural language queries. The model is fine-tuned, evaluated, and optimized using a Text-SQL dataset generated based on the SQL-to-Text model, allowing it to better adapt to the semantic and syntactic rules of the specific domain. This reduces semantic bias and errors, resulting in a highly efficient Text2Sql model. Furthermore, by optimizing the user's natural language input through a pre-set prompt and knowledge base, ambiguous information in the query is enhanced, and ambiguity and misunderstanding are reduced, ultimately improving the accuracy and robustness of Text2SQL. Attached Figure Description

[0050] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0051] Figure 1 This is a flowchart of a method according to an embodiment of the present invention. Detailed Implementation

[0052] It should be noted that, unless otherwise specified, the embodiments and features described in the present invention can be combined with each other.

[0053] To make the objectives, technical solutions, and advantages of this invention clearer, the invention will be further described in detail below with reference to the accompanying drawings.

[0054] Conversely, this invention encompasses any substitutions, modifications, equivalent methods, and solutions made within the spirit and scope of the invention as defined in the claims. Furthermore, to provide a better understanding of the invention, certain specific details are described in detail below. However, those skilled in the art will fully understand the invention even without these detailed descriptions.

[0055] This invention provides a Text2SQL semantic parsing method oriented towards a large domain language model, such as... Figure 1 As shown, the energy vertical industry is used as an application case for illustration. The case includes the following steps:

[0056] S1. Connect to the energy sector database and store its pattern information. This includes the following sub-steps:

[0057] S11. Given that the database in the energy sector is a relational database MySQL, obtain the connection parameters for this database, including hostname, port number, database name, username, and password, and establish a connection.

[0058] S12. Obtain and store database schema information. When storing, define the database schema S = {T1, T2, ..., T...}. i , ..., T n , E}, where T i Let Ti be the i-th table, and E represent the primary and foreign key relationships between the tables. Table Ti contains column C. i ={C i1 C ij , ...}, where C i1 This represents the first column of the i-th data table.

[0059] S2. Construct an energy domain knowledge base based on energy domain databases and energy background knowledge, and then mount it onto the large language model. This includes the following sub-steps:

[0060] S21. Collect general background knowledge in the energy field and create a document (Text).

[0061] S22. Using NLP technology, parse and label the table names, column names, and partial data in each table of the energy database. Using an NLP model based on the energy background knowledge from S21, perform semantic interpretation and annotation of the table names, column names, and partial data in each table, and store the results in a dictionary.

[0062] remark i ={"table_name": table i "Column name": column ij "data": data ij}

[0063] S23. Convert the energy background knowledge document in S21 and the notes dictionary in S22 into word vectors using a pre-trained word embedding model (Word2Vec). For each word in the knowledge document and database information, assume the input is represented as a word vector V, and the center word w. c The word vectors are Context word w o word vectors The output layer uses the Softmax function for probability normalization. The objective function and the Softmax function are expressed as follows:

[0064] L = -logP(w) o |W c )

[0065]

[0066] Where V b V represents all words in the vocabulary. w This represents the word vector of word w.

[0067] S24. Store the word vectors from S23 in a vector database to obtain an energy domain knowledge base.

[0068] In subsequent knowledge base matching retrieval, word vectors from the vector library can be used to match the query, and the similarity can be calculated to measure the matching degree between the query and the documents in the library.

[0069] S3. Generate a Text-SQL dataset for the energy sector based on the SQL-to-Text model. Define the Text-SQL dataset D = {D1, D2, ..., D...} i , ..., D n}, where D i It includes a natural language question Q and a corresponding correct, executable SQL statement for Q. Specifically, it includes the following sub-steps:

[0070] S31. Design the Prompt instruction to "generate multiple descriptions or explanations of problems related to the energy field given an SQL statement." Construct the context information of the Prompt based on the descriptions of tables, fields, values, etc., involved in the SQL statement, as well as database schema information. Then, use this Prompt to build a model capable of SQL-to-Text interpretation in the energy field, i.e., an SQL-to-Text model.

[0071] S32. Extract some correctly generated SQL statements from energy sector database applications, database audit logs, and test cases, and use the SQL-to-Text model in S31 to transform them into multiple similar SQL interpretations or questions.

[0072] It should be noted that the energy sector database application described in S32 is provided and licensed by Party A. In this embodiment, it includes an energy management system, power dispatching system, smart grid system, and energy market management system. The database table structure specifically includes, but is not limited to: energy table, energy manufacturer table, equipment table, and customer table. The database audit log records all operations performed on the database by the energy sector database application, including but not limited to query, insert, update, and delete operations. Specific recorded information includes, but is not limited to: audit log ID, operation type, user performing the operation, operation time, executed SQL statement, and the table operated on. Test cases are designed to cover SQL statements of different operation types and their expected results, used to verify the correctness of the SQL statements. For example, energy sector test cases might verify the SQL statement for querying power generation by inputting specific power generation data, or verify the SQL statement for updating energy consumption records by inputting specific energy consumption data.

[0073] Similarity calculation: Cosine similarity measures the angle between two vectors. For each generated SQL interpretation, a similarity score is calculated between it and the context information in the Prompt.

[0074]

[0075] S33. Select the natural language question with the highest similarity as Q in the energy dataset, and combine the Q with the highest similarity and its corresponding SQL statement to form a Text-SQL data pair D. i ={Q i SQL i}

[0076] S4. Fine-tune, evaluate, and optimize the model using a Text-SQL (TS) dataset from the energy sector to build a high-performance Text2Sql model applicable to the energy field. This includes the following sub-steps:

[0077] S41. Divide the energy dataset TS into training and test sets according to a certain ratio. A common division ratio is TS = 0.8 * TS_train + 0.2 * TS_test. When dividing, ensure that the data distribution and feature distribution of the training and test sets are as consistent as possible.

[0078] S42. Fine-tune the model using the pre-defined training set, adjusting the model parameters to suit the specific Text2Sql task. Define a loss function (mean squared error) to represent the degree of difference between the model's predictions and the true values, where the true value of the i-th sample is y. i The predicted value is The sample size is n, and the formula is expressed as:

[0079]

[0080] S43. Calculate and evaluate metrics for the fine-tuned model using the pre-defined test set. Define true positives (TP) as the number of SQL queries generated by the Text2Sql model that match real SQL queries; false negatives (FN) as the number of queries that the Text2Sql model fails to generate that match real SQL queries; and false positives (FP) as the number of SQL queries generated by the Text2Sql model that do not match real SQL queries. Common evaluation metrics include recall, precision, and F1 score, expressed by the following formulas:

[0081]

[0082]

[0083]

[0084] S44. Based on the evaluation results, use an optimization algorithm (gradient descent) to minimize the loss function and find the optimal model parameters to achieve better performance and generalization ability. Define the model parameters as θ, the learning rate as α, and the loss function as MSE, expressed by the formula:

[0085]

[0086] S5. Input a natural language question and optimize it based on the Prompt and energy knowledge base. Define the natural language set Z = {Q1, Q2, ..., Q...} i Q n}, where Z has a total of n sentences and Q i It contains P i The Chinese character q it t∈[1,P] represents a word in the i-th sentence. Specifically, it includes the following sub-steps:

[0087] S51. From the word q it Sentences consisting of t∈[1,P] are first processed by the word embedding matrix w in the general text embedding model m3e-base. e Convert words into word vectors, x it =w e q it .

[0088] S52. Design a Prompt based on the energy domain knowledge base in S2 to analyze and optimize natural language questions. A Prompt, consisting of instructions, context information, input data to be processed, and a specified output content type or format, is represented as P = Instruction + Context + Input + Output. The analysis process requires the use of natural language processing techniques such as word segmentation, part-of-speech tagging, keyword extraction, entity recognition, and syntactic analysis. The optimization process requires designing specific instructions to allow the Text2Sql model to improve the ambiguous or vague expressions of the question based on the energy domain knowledge base, making it more accurate and specific.

[0089] It should be noted that the commands in the Prompt are mandatory, while the others are optional.

[0090] S53. When inputting natural language Q i When it is time, it will first be converted into a vector. i Then, it retrieves a set of vectors V from the domain knowledge base that have a high similarity to the vector. i ={vector i0 ... vector ij Then, based on the matching degree between vectors, the vector set is sorted, and the top-k knowledge results with the highest matching degree are selected as context and encapsulated in the Prompt for the Text2Sql model to refer to, understand, and generate better input. The formula is expressed as:

[0091] Result Top-k =Max-Score Top-k (Sort(Match(Q i V i )))

[0092] S6. Use a Text2Sql model from the energy domain to parse the semantics and generate results based on the padded Prompt inference. This includes the following sub-steps:

[0093] S61. Based on the enhanced problem Q in S53 S Retrieve the table with the highest matching degree from the energy sector database. and column Then go to the energy knowledge base to find the corresponding explanation and note K. T&C Design corresponding instruction I based on user needs request Finally, fill and integrate.

[0094] As prompt words in the Text2Sql model.

[0095] S62. If the user wants an SQL statement, define the generated SQL as {K, T, C, V, OP}, where K represents the SQL statement keywords, such as "select" and "where"; T and C represent the data tables and columns involved in the SQL statement, respectively; V represents the values ​​appearing in the SQL statement, which are related to the data type of the corresponding column; and OP represents some data operators in the SQL statement, such as ">" and "=". Use the Prompt function in S61 and design instruction I. request .

[0096] In this embodiment, instruction I request This process transforms optimized query questions into corresponding SQL statements based on recalled data tables, columns, and explanatory notes in the knowledge base, and outputs these as the final results. The results are then used as prompts in the Text2Sql model to parse and generate SQL statements corresponding to the query. Furthermore, these prompts can be stored as intermediate states for subsequent SQL optimization operations to achieve more efficient and accurate results.

[0097] S63. If the user wants the result after executing the SQL, the instructions in the Prompt remain unchanged. The SQL statement generated by parsing the Text2Sql model is input into the SQL executor environment for execution, and the result is returned. At the same time, the results can be further understood and analyzed through the explanatory notes in the Prompt, and data analysis and visualization can be performed.

[0098] The embodiments of the present invention have been described in detail above with reference to the accompanying drawings, but the present invention is not limited to the described embodiments. For those skilled in the art, various changes, modifications, substitutions, and variations can be made to these embodiments, including components, without departing from the principles and spirit of the present invention, and these variations still fall within the protection scope of the present invention.

Claims

1. A Text2SQL semantic parsing method oriented towards a domain-specific large language model, characterized in that, Includes the following steps: Step 1: Connect to the domain database and store the schema information of the domain database; In step 1, the database schema information storage method is a columnar storage method that organizes and stores data in a columnar manner. During storage, a database schema is defined. ,in Let E be the i-th table, and E be the primary and foreign key relationships between the tables. Contains columns ,in This represents the first column of the i-th data table. Step 2: Construct a domain knowledge base based on the domain database and the domain background knowledge of the domain database, and mount the domain knowledge base into the large language model; Step 3: Generate a Text-SQL dataset based on the SQL-to-Text model; Step 4: Divide the Text-SQL dataset into training and testing sets according to the proportions. Use the training and testing sets respectively to fine-tune and evaluate the large language model, and optimize it according to the evaluation results. The optimized model is the Text2Sql model. Step 5: Input the natural language question and optimize it based on the Prompt and the domain knowledge base; The specific method for step 5 is as follows: Step 5.1: Define the natural language set Z has a total of n sentences and Includes One Chinese character, Representing the The words in the sentence, among which , from words The sentences are first formed by using the word embedding matrix in the general text embedding model m3e-base. Convert words into word vectors. ; Step 5.2: Construct a Prompt based on the domain knowledge base, wherein the Prompt includes instructions; Step 5.3, when inputting natural language When, it will first be converted into a vector. Then, search the domain knowledge base for the vector. Highly similar vector sets Then, the vector set is sorted according to the matching degree between the vectors, and the top-k knowledge results with the highest matching degree are selected as context and encapsulated in the Prompt for the Text2Sql model to refer to, understand, and generate better input. The formula is expressed as: ; Step 6: Parse the semantics using the Text2Sql model and design the Prompt inference results based on the requirements. The specific method for step 6 is as follows: Step 6.1: Input the optimized problem. Retrieve the table with the highest matching degree from the database. and column Then go to the domain knowledge base to find the corresponding explanation and notes. Design instructions based on requirements Integration As prompt words in the Text2Sql model; Step 6.2: If the requirement is an SQL statement, use the Prompt from Step 6.1 and design the instructions. This is used as a prompt in the Text2Sql model to parse and generate an SQL statement corresponding to the question. Step 6.3: If the requirement is the final result after executing the SQL, the instructions in the Prompt... Keeping the process unchanged, the SQL statement generated by parsing the Text2Sql model is input into the SQL executor's environment for execution, and the result is returned; simultaneously, the explanation comments in the Prompt are used. The results can be further understood and analyzed, and data analysis and visualization can be performed.

2. The Text2SQL semantic parsing method for a domain-oriented large language model according to claim 1, characterized in that, In step 2, the method for constructing the domain knowledge base is as follows: Step 2.1: Determine the application domain based on the domain database, and collect background knowledge of that domain to form a background knowledge document for that domain; Step 2.2: Extract table names, column names, and data from each table in the domain database using natural language processing technology, preprocess and label them, and then use an NLP model to perform semantic interpretation and annotation of the table names, column names, and data based on domain background knowledge, and store them in a dictionary; Step 2.3: Input the background knowledge document and dictionary into the pre-trained word embedding model, convert them into word vectors, and store them in the vector database; Step 2.4: Establish the domain knowledge base structure, including defining the attributes, relationships, and categories of terms, associating word vectors stored in the vector database with the corresponding terms in the knowledge base, and supplementing relevant semantic information; then expand the knowledge base by adding entity relationships and attribute information to make it more comprehensively reflect domain knowledge, thereby constructing the domain knowledge base.

3. The Text2SQL semantic parsing method for a domain-oriented large language model according to claim 2, characterized in that, In step 2.3, the method of converting pre-trained word embedding models into word vectors is as follows: First, the background knowledge document and dictionary are cleaned, including removing special characters, punctuation marks and stop words, as well as word form restoration; the pre-trained word embedding model Word2Vec is selected and loaded using the Gensim library; each word is traversed and converted into the corresponding word vector through the Word2Vec model.

4. The Text2SQL semantic parsing method for a domain-specific large language model according to claim 1, characterized in that, The specific method for step 3 is as follows: Step 3.1: Design the Prompt instruction. The Prompt instruction generates multiple corresponding problem descriptions or explanations for a given SQL statement. The context information of the Prompt is constructed based on the tables, fields, numerical descriptions and database schema information involved in the SQL statement. Thus, a model with SQL-to-Text interpretation function is constructed using the Prompt, namely the SQL-to-Text model. Step 3.2: Filter and extract the correct SQL statements from the local domain database application, database audit logs, and test cases, and use the SQL-to-Text model to convert each SQL statement into multiple similar SQL interpretations or questions; Step 3.3: Calculate the similarity scores between the generated SQL interpretations or questions and the context information in the Prompt. Select the natural language question with the highest similarity as Q in the dataset, and combine the Q with the corresponding SQL statement to form a Text-SQL data pair. Store the dataset In the process, we obtain the Text-SQL dataset. .

5. The Text2SQL semantic parsing method for a domain-specific large language model according to claim 4, characterized in that, In step 3.3, the similarity score is calculated by applying cosine similarity to measure the angle between two vectors.

6. The Text2SQL semantic parsing method for a domain-oriented large language model according to claim 1, characterized in that, The Text-SQL dataset is categorized as follows: Divide into sections.

7. The Text2SQL semantic parsing method for a domain-oriented large language model according to claim 1, characterized in that, In step 4, the model fine-tuning method is as follows: define the mean squared error loss function to represent the degree of difference between the model's prediction results and the true values.

8. The Text2SQL semantic parsing method for a domain-oriented large language model according to claim 1, characterized in that, In step 4, the evaluation method for the fine-tuned model is as follows: The fine-tuned model is evaluated and its metrics are calculated using a pre-defined test set. True positives are defined as the number of SQL queries generated by the Text2Sql model that match real SQL queries; false negatives are the number of queries that the Text2Sql model fails to generate that match real SQL queries; and false positives are the number of SQL queries generated by the Text2Sql model that do not match real SQL queries. The formula is as follows: In this context, TP represents true positives, FN represents false negatives, and FP represents false positives.

9. A Text2SQL semantic parsing method for a domain-oriented large language model according to claim 8, characterized in that, In step 4, the evaluation metrics for the Text2Sql model include recall, accuracy, and F1 score.

10. A Text2SQL semantic parsing method for a domain-oriented large language model according to claim 8, characterized in that, In step 4, gradient descent is applied to minimize the loss function based on the evaluation results.

Citation Information

Patent Citations

  • Method for converting natural statement into SQL (Structured Query Language) statement based on large language model

    CN117827886A

  • Large language model Text2SQL (Structured Query Language) chart generation method and device

    CN117851445A