Large language model construction method and device based on NL2SQL
By preprocessing data of NL2SQL system and building training data sets, the training efficiency problem caused by system complexity is solved, and more efficient SQL statement generation is achieved.
Patent Information
- Application Number
- CN202510546407.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-28
- Publication Date
- 2025-08-08
AI Technical Summary
The existing NL2SQL system is too complex in terms of data and training methods, making it difficult to quickly train efficient large language models and perform practical applications.
By obtaining the original data set, preprocessing is performed to determine the logical relationship between paragraphs, merging paragraphs with the same logical relationship, extracting key information, building a training data set, and training the large language model based on the data set to generate SQL statements.
It improves the accuracy of the NL2SQL system, reduces the difficulty of data set construction, enhances the practicality of the system, and can generate SQL query statements more accurately.
Smart Images

Figure CN120448410A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of large language model construction, and in particular to a large language model construction method and device based on NL2SQL. Background Art
[0002] Currently, traditional database query methods typically require specialized knowledge of Structured Query Language (SQL), which not only limits data processing speed but also increases operational complexity. To more accurately understand query intent and generate corresponding SQL statements, a new solution is leveraging large language models (LLMs) to enable efficient and intuitive database access.
[0003] However, the existing NL2SQL system lacks an efficient method for combining data and training methods to improve large-scale model capabilities. The current approach is overly complex in terms of data and training methods, making it difficult to quickly train efficient large-scale language models and put them into practical use. Summary of the Invention
[0004] To address the above technical issues, the present invention is proposed. Embodiments of the present invention provide a method and apparatus for constructing a large language model based on NL2SQL, which can address the problem that the NL2SQL system is overly complex in terms of data and training methods, making it difficult to quickly train an efficient large language model and implement it in practical applications.
[0005] According to one aspect of the present invention, a method for constructing a large language model based on NL2SQL is provided, comprising:
[0006] Get the original dataset;
[0007] Preprocessing the original data set to obtain a preprocessed original data set;
[0008] Determining logical relationships between paragraphs in the preprocessed raw data set;
[0009] Merge paragraphs with the same logical relationship to obtain a merged data set;
[0010] Extracting key information from the merged data set;
[0011] Based on the key information, a training data set is constructed; wherein the training data set includes multiple questions and SQL statements corresponding to each question;
[0012] Based on the training data set, the large language model is trained to obtain a trained large language model.
[0013] In one embodiment, preprocessing the original data set to obtain a preprocessed original data set includes:
[0014] Extracting key data from the original data set;
[0015] Constructing a correspondence between the key data and its corresponding text data; wherein the key data corresponds to a first index, and the text data corresponding to the key data corresponds to a second index, and the level of the first index is higher than the level of the second index;
[0016] A preprocessed original data set containing the corresponding relationship is constructed.
[0017] In one embodiment, constructing a training data set based on the key information includes:
[0018] Converting the key information into vector text;
[0019] Encapsulating the vector text into a vector database;
[0020] Based on the vector database, a training data set is constructed.
[0021] In one embodiment, constructing a training data set based on the vector database includes:
[0022] Obtaining tables, fields, and relationships between the tables and fields in the vector database;
[0023] Determining the structure of text data in the vector database based on the table, the fields, and the relationship between the table and the fields;
[0024] analyzing the structure to obtain an analysis result;
[0025] Based on the analysis results, a training data set is constructed.
[0026] In one embodiment, constructing a training data set based on the analysis results includes:
[0027] Based on the analysis results, a dynamic prompt template is generated;
[0028] Based on the large language model, the dynamic prompt template and the analysis result, a training data set corresponding to the vector database is generated.
[0029] In one embodiment, the training of the large language model based on the training data set to obtain the trained large language model includes:
[0030] Performing semantic expansion on the multiple questions in the training data set and the SQL statements corresponding to each question to obtain an expanded data set;
[0031] Verifying the expanded questions in the expanded data set and the SQL statements corresponding to each expanded question based on preset statement verification rules;
[0032] Based on the verification result, selecting target expanded questions and SQL statements corresponding to each target expanded question from the expanded data set;
[0033] Based on the target expanded questions and the SQL statements corresponding to each target expanded question, an NL2SQL dataset is obtained;
[0034] Based on the NL2SQL dataset, the large language model is trained to obtain a trained large language model.
[0035] In one embodiment, after training the large language model based on the training dataset to obtain the trained large language model, the NL2SQL large language model construction method further includes:
[0036] Get query information;
[0037] Generate a target SQL query statement based on the query information and the trained large language model;
[0038] Execute the target SQL query statement to obtain an execution result.
[0039] In one embodiment, after executing the SQL query statement to obtain the execution result, the method for constructing a large language model based on NL2SQL further includes:
[0040] Get feedback results;
[0041] If the feedback result indicates that the SQL query statement is wrong, correct the SQL query statement.
[0042] In one embodiment, extracting key information from the merged data set includes:
[0043] Performing text segmentation on the merged data set to obtain segmented text;
[0044] Performing semantic analysis on the segmented text and determining the semantic integrity of the segmented text;
[0045] If the semantic completeness is greater than a preset completeness threshold, key information of the segmented text is extracted.
[0046] According to another aspect of the present invention, a large language model construction device based on NL2SQL is provided, comprising:
[0047] Acquisition module, used to obtain the original data set;
[0048] A preprocessing module, configured to preprocess the original data set to obtain a preprocessed original data set;
[0049] a determination module configured to determine the logical relationship between paragraphs in the preprocessed original data set; merge paragraphs with the same logical relationship to obtain a merged data set; and extract key information from the merged data set;
[0050] A construction module, configured to construct a training data set based on the key information; wherein the training data set includes a plurality of questions and an SQL statement corresponding to each question;
[0051] The training module is used to train the large language model based on the training data set to obtain a trained large language model.
[0052] The present invention provides a method and device for constructing a large language model based on NL2SQL, comprising: obtaining an original data set, preprocessing the original data set to obtain a preprocessed original data set, determining the logical relationship between paragraphs in the preprocessed original data set, merging paragraphs with the same logical relationship to obtain a merged data set, extracting key information from the merged data set, constructing a training data set based on the key information, wherein the training data set includes multiple questions and SQL statements corresponding to each question, and training a large language model based on the training data set to obtain a trained large language model. By analyzing the logical relationship between paragraphs in the original data set, the paragraphs are streamlined and the efficiency of extracting key information from the short circuit is improved. Constructing a training data set based on the key information effectively improves the accuracy of NL2SQL, reduces the difficulty of data set construction, and enhances the practicality of the system. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The above and other objects, features, and advantages of the present invention will become more apparent through a more detailed description of the embodiments of the present invention in conjunction with the accompanying drawings. The accompanying drawings are provided to provide a further understanding of the embodiments of the present invention and constitute a part of the specification. Together with the embodiments of the present invention, they are used to explain the present invention and are not intended to limit the present invention. In the drawings, the same reference numerals generally represent the same components or steps.
[0054] Figure 1 It is a flowchart of a method for building a large language model based on NL2SQL provided by an exemplary embodiment of the present invention.
[0055] Figure 2 It is a paragraph decomposition flowchart provided by an exemplary embodiment of the present invention.
[0056] Figure 3 It is a flowchart of a method for preprocessing an original data set provided by an exemplary embodiment of the present invention.
[0057] Figure 4 It is a flowchart of a method for building a large language model based on NL2SQL provided by another exemplary embodiment of the present invention.
[0058] Figure 5 1 is a schematic diagram of the structure of a large language model construction device based on NL2SQL provided by an exemplary embodiment of the present invention.
[0059] Figure 6 2 is a schematic diagram of the structure of a large language model construction device based on NL2SQL provided by another exemplary embodiment of the present invention.
[0060] Figure 7 is a structural diagram of an electronic device provided by an exemplary embodiment of the present invention. DETAILED DESCRIPTION
[0061] Below, the exemplary embodiments according to the present invention will be described in detail with reference to the accompanying drawings. Obviously, the described embodiments are only part of the embodiments of the present invention, rather than all the embodiments of the present invention, and it should be understood that the present invention is not limited to the exemplary embodiments described herein.
[0062] The NL2SQL system (Natural Language to SQL) is a natural language processing (NLP) technology used to convert natural language queries into Structured Query Language (SQL) queries.
[0063] Figure 1 It is a flowchart of a method for building a large language model based on NL2SQL provided by an exemplary embodiment of the present invention. Figure 2 This is a paragraph decomposition flow chart provided by an exemplary embodiment of the present invention. Figure 1-2 As shown in FIG, the method for building a large language model based on NL2SQL includes:
[0064] Step 110: Obtain the original data set.
[0065] In an embodiment of the present invention, the present invention summarizes and organizes Data Definition Language (DDL) or Schemas (Schema is a language used to describe and standardize the logical structure of XML documents), documents, etc. to construct an original data set.
[0066] Step 120: Preprocess the original data set to obtain a preprocessed original data set.
[0067] Step 130: Determine the logical relationship between paragraphs in the pre-processed original data set.
[0068] In an embodiment of the present invention, a natural language processing paragraph analysis tool is used to extract logical relationships between pre-processed original data sets, wherein the logical relationships include subordinate relationships, parallel relationships, etc.
[0069] When determining the logical relationship between paragraphs in the preprocessed raw data set, it can be described in detail from the following aspects:
[0070] Optionally, identify the core theme or main idea of each paragraph. Using methods like keyword extraction and topic modeling, you can clarify the main content discussed in each paragraph. Alternatively, determine the type of information contained in a paragraph, such as facts, opinions, examples, and arguments. This helps you understand the logical connections between paragraphs.
[0071] Then, based on the content and information type between paragraphs, logical relationships can be divided into several common types: Cause-and-effect relationship: The content of one paragraph is the cause or result of another paragraph. For example, paragraph A describes a certain phenomenon, and paragraph B analyzes its causes. Contrast relationship: There are opposing or contrasting contents between paragraphs, such as paragraph A and paragraph B describe two different viewpoints or situations respectively. Supplementary relationship: One paragraph provides supplementary information or details to another paragraph, usually used to further elaborate on a certain point of view. Sequential relationship: The content between paragraphs presents a chronological or logical order, for example, paragraph A describes the background of an event, and paragraph B describes the development of the event.
[0072] Analyzing the connecting words in paragraphs (such as "therefore," "however," "in addition," "first," and "secondly") can help identify the logical relationships between paragraphs. These words indicate specific types of relationships between paragraphs. They can also help identify the objects referred to in paragraphs, ensuring understanding of the connections between paragraphs. For example, "it" mentioned in one paragraph may be further discussed in a later paragraph.
[0073] Finally, analyze the structural relationships between paragraphs, their hierarchy, and paragraph sequence. Hierarchy: Analyze the hierarchical structure of paragraphs within the overall text, identifying which paragraphs are main paragraphs and which are supporting paragraphs. Main paragraphs typically present the main argument, while supporting paragraphs provide evidence or examples. Paragraph sequence: Examine the order of paragraphs and understand how they are organized within the text to grasp the logical flow and coherence of the argument.
[0074] Step 140: Merge paragraphs with the same logical relationship to obtain a merged data set.
[0075] In the embodiment of the present invention, Figure 2As shown, paragraphs with the same logical relationship are merged to obtain a merged dataset. For example, paragraphs A and B are in a subordinate relationship. Paragraphs A and B are merged into segment D, which is the merged dataset.
[0076] Specifically, you need to determine the criteria for merging paragraphs. First, you need to clarify the criteria for merging paragraphs. Merged paragraphs should generally share the same logical relationship, for example: Cause-and-effect: One paragraph describes the cause, another describes the result. Supplementary: One paragraph provides additional information or details about another. Contrast: While contrasting paragraphs are generally not merged, they can be merged in specific circumstances, such as when comparing two viewpoints to reach a common conclusion.
[0077] Before merging, first classify all paragraphs to identify the logical relationships between them. You can use the following methods: Topic analysis: Apply topic modeling algorithms (such as LDA) or keyword extraction techniques to identify the topic of each paragraph. Connective word identification: Analyze the connective words used in the paragraphs to understand the logical relationships between paragraphs.
[0078] After completing the paragraph classification, merge the paragraphs according to the logical relationship. The specific steps are as follows: Select the target paragraph: Find the paragraphs that meet the merging criteria from the dataset. For example, paragraph A and paragraph B are identified as subordinate relationships. Merge operation: Merge the selected paragraphs into a new segment. The merging can be performed in the following ways: Text splicing: Splice the contents of the paragraphs directly together, and add appropriate transition words or sentences when merging to ensure fluency. Rewrite and simplify: During the merging process, the content can be rewritten and simplified to remove redundant information and improve logical coherence. For example, if paragraphs A and B both mention the same background information, only one paragraph can be retained. Generate a merged segment: Name the merged result a new segment, for example, segment D. Segment D will contain the merged information of paragraphs A and B, and retain the logical relationship.
[0079] Step 150: Extract key information from the merged data set.
[0080] In the embodiment of the present invention, key information in the merged data set is extracted through component parsing, for example, key information E is extracted from segment D.
[0081] Step 160: Based on the key information, a training data set is constructed, wherein the training data set includes multiple questions and an SQL statement corresponding to each question. SQL (Structured Query Language) is a standard programming language for managing and operating relational databases.
[0082] Step 170: Based on the training data set, the large language model is trained to obtain a trained large language model.
[0083] In the embodiment of the present invention, since the large language model is trained by using multiple questions and their corresponding SQL statements in the training data set, the trained model can more accurately generate SQL statements corresponding to specific questions.
[0084] Figure 3 FIG. 1 is a flow chart of a method for preprocessing an original data set provided by an exemplary embodiment of the present invention. Figure 3 As shown, step 120 may include:
[0085] Step 121: Extract key data from the original data set.
[0086] Step 122: Construct a correspondence between the key data and its corresponding text data, wherein the key data corresponds to a first index, the text data corresponding to the key data corresponds to a second index, and the level of the first index is higher than the level of the second index.
[0087] In an embodiment of the present invention, the text in the original dataset is organized into a two-level index. The first-level index is key information, and the second-level index is the original text, with a one-to-one mapping between the two. Specifically, the first-level index consists of key data, representing important key information in the dataset, and its rank is higher than the second-level index. The second-level index consists of original text corresponding to each key data, providing detailed descriptions and background information of the key data. This structure not only ensures the priority of key data, but also enables the effective organization and rapid retrieval of text data, facilitating subsequent data analysis and application.
[0088] Step 123: Construct a pre-processed original data set containing corresponding relationships.
[0089] In one embodiment, step 160 may be specifically implemented as follows: converting key information into vector text; encapsulating the vector text into a vector database; and constructing a training data set based on the vector database.
[0090] In an embodiment of the present invention, an embedding model is used to convert the extracted keywords into vectors, in which each dimension represents a certain semantics or feature of a text block, and then the vectorized text is stored in the vector database Faiss. The vector database is Facebook's Faiss vector database. Faiss can quickly process large-scale data sets, improve retrieval efficiency by encapsulating the vector library into an index database, that is, a vector database, and use algorithms on the GPU to accelerate the retrieval of vectors, which helps to speed up the query process. Based on the vector database, a training data set is constructed. The embedding model is a technology that converts data (usually text, images or other types of information) into a low-dimensional vector representation. This vector representation can capture the semantics, structure and other features of the data, allowing computers to better understand and process the data. Faiss (Facebook AI Similarity Search) is an open source library developed by Facebook for efficient similarity search and clustering of dense vectors. GPU is a hardware specially designed for fast processing of graphics and images.
[0091] In one embodiment, step 160 can be specifically implemented as follows: obtaining tables, fields, and relationships between tables and fields in the vector database; determining the structure of text data in the vector database based on the tables, fields, and relationships between tables and fields; analyzing the structure to obtain analysis results; and constructing a training data set based on the analysis results.
[0092] In an embodiment of the present invention, in a vector database, it is necessary to obtain information about all tables and their fields, including the name of the table, the name and data type of the field, and the relationship between the table and the field (such as primary key, foreign key, index, etc.). By analyzing these structures, the organization of the text data can be determined, such as whether there is a nested structure, a hierarchical relationship or an association relationship. These structures are analyzed in depth to identify the important features and patterns in the text data, thereby obtaining analysis results, such as the distribution of the data, relevance, and potential feature engineering directions. Based on these analysis results, a high-quality training data set is constructed to ensure that the data set can effectively reflect the characteristics and relationships of the text data, thereby improving the performance and accuracy of the model. For example, statistical analysis tools (such as mean, median, variance, etc.) and visualization tools (such as histograms, box plots, etc.) are used to check the distribution of each field, especially numerical fields.
[0093] In one embodiment, step 160 may be specifically implemented as follows: generating a dynamic prompt template based on the analysis result; and generating a training data set corresponding to the vector database based on the large language model, the dynamic prompt template, and the analysis result.
[0094] In an embodiment of the present invention, based on the analysis results, a dynamic prompt template is generated through prompt engineering, and then a training data set corresponding to the vector database is generated based on the large language model, the dynamic prompt template and the analysis results.
[0095] Specifically, based on the preliminary analysis results, the prompting project generates dynamic prompt templates. These templates are designed to capture the structural characteristics and contextual information of the text data to guide the large language model (LLM) in generating a training dataset corresponding to the vector database. The dynamic prompt template structure includes the following components: 1) Input field description: This describes the specific content and format of the input data, such as "Please generate descriptive text based on the following data: {field 1}, {field 2}"; 2) Contextual prompt: This provides the contextual information required for text generation, such as "In this case, consider {relationship} and {constraints}"; 3) Output format description: This specifies the expected format or style of the generated text, such as "Please output in JSON format" or "Generate a concise description"; and 4) Example prompt: This includes example input and output examples to help the model better understand the expected generated results. By combining these dynamic prompt templates with the large language model, the resulting training dataset will contain rich text content corresponding to each table and field in the vector database, supporting subsequent model training and application. For example, product name: "Smartwatch"; price: 999 yuan; category: Electronics; features: heart rate monitoring, GPS navigation, waterproof design.
[0096] In one embodiment, step 170 can be specifically implemented as follows: semantically expanding multiple questions in the training data set and the SQL statements corresponding to each question to obtain an expanded data set; verifying the expanded questions in the expanded data set and the SQL statements corresponding to each expanded question based on preset statement verification rules; based on the verification results, screening out target expanded questions and the SQL statements corresponding to each target expanded question from the expanded data set; obtaining an NL2SQL data set based on the target expanded questions and the SQL statements corresponding to each target expanded question; and training a large language model based on the NL2SQL data set to obtain a trained large language model.
[0097] In an embodiment of the present invention, in order to enhance the diversity and richness of the data set, semantic expansion is further performed. By deeply analyzing each question and using the preset prompt engineering to guide the large model to generate multiple new questions related to each question and their corresponding SQL statements, an expanded data set is formed. In the expanded data set, each SQL statement needs to be strictly verified and screened. The purpose of this step is to ensure that the execution result of each SQL statement is consistent with the expected result of the corresponding question. The correctness of the SQL statement is judged by comparing the execution result of the SQL statement with the expected result of the question. Only those SQL statements whose execution results are consistent with the expected results will be retained for subsequent model training. Model training is performed using the verified and screened target NL2SQL data set. The data set is divided into a training set and a test set by random sampling. Then, the preset data processing model is trained using the training set, and the trained model is evaluated using the test set. After multiple iterations and optimizations, a target data processing model that can accurately execute query operations is obtained.
[0098] Specifically, an expanded dataset was generated by semantically expanding multiple questions and their corresponding SQL statements in the training dataset. This process involves identifying different ways of expressing the question and potential variants, while ensuring that these new questions maintain semantic consistency with the original question and attaching corresponding SQL statements. Next, based on the preset statement verification rules, each new question in the expanded dataset and its corresponding SQL statement are verified to ensure the logical correctness and execution validity of the SQL statement. This verification step is intended to exclude SQL statements that fail to return the expected results when executed. The preset statement verification rules include determining whether the expanded question matches the semantics of the original question, and whether the SQL statement corresponding to the expanded question is consistent with the SQL question corresponding to the original question. If they are consistent, they are retained. If
[0099] Based on the validation results, we screened the expanded dataset for target expanded questions and their corresponding SQL statements that met the criteria, ensuring they accurately reflected the desired query intent. Finally, we constructed a high-quality NL2SQL dataset based on these target expanded questions and their corresponding SQL statements. Using this NL2SQL dataset, we trained a large language model. After multiple rounds of iteration and fine-tuning, we ultimately achieved a trained large language model capable of more accurately converting natural language questions into SQL queries.
[0100] In one embodiment, after step 170, the method for constructing a large language model based on NL2SQL can be specifically implemented as follows: obtaining query information; generating a target SQL query statement based on the query information and the trained large language model; and executing the target SQL query statement to obtain an execution result.
[0101] In an embodiment of the present invention, a user inputs query information, and the trained large language model obtains a target SQL query statement based on the query information. The query information may include the target question input by the user.
[0102] Specifically, semantic analysis is performed on the query information to obtain a semantic result, a target question similar to the semantic result is obtained from the trained large language model, and a target SQL query statement corresponding to the target question is obtained.
[0103] In one embodiment, after step 170 , the method for constructing a large language model based on NL2SQL may be specifically implemented as follows: obtaining a feedback result; and correcting the SQL query statement if the feedback result indicates that the SQL query statement is incorrect.
[0104] In an embodiment of the present invention, obtaining feedback results means that after executing an SQL query statement, the system will return the execution result or corresponding error information. If the feedback result indicates that the SQL query statement contains an error, such as a syntax error, a misspelled table or field name, or a missing necessary condition, the query statement needs to be corrected. The correction process includes carefully analyzing the error message, identifying the problem, and correcting the relevant parts according to the SQL syntax specifications and database structure. For example, adjusting the statement format, supplementing missing elements, or modifying incorrect content to ensure that the final query statement can be successfully executed and return the correct results.
[0105] Furthermore, users can provide feedback on the generated SQL query statements and their results. If the query result is correct, the question-answer pair can be stored in the vector database, improving the accuracy of future queries. If the query result is incorrect, the user can make corrections, and the corrected SQL query statement and the corrected query result will be stored in the vector database. Through this continuous learning and iteration, the system will be able to gradually improve the accuracy of generated SQL queries.
[0106] In one embodiment, step 150 can be specifically implemented as follows: performing text segmentation on the merged data set to obtain segmented text; performing semantic analysis on the segmented text and determining the semantic completeness of the segmented text; if the semantic completeness is greater than a preset completeness threshold, extracting key information from the segmented text.
[0107] In an embodiment of the present invention, when processing the merged data set, it is first divided into multiple independent text paragraphs by text segmentation technology for subsequent analysis. The text segmentation tool will ensure that each text paragraph has relatively complete independent semantics according to preset rules, such as the maximum length of a single paragraph of text and the overlapping length between adjacent texts, thereby improving the rationality of segmentation. These segmented text paragraphs will undergo semantic analysis to evaluate their semantic integrity. If the semantic integrity of a certain text segment is higher than a set threshold, its key information will be extracted. In this way, it is ensured that each text paragraph maintains consistency in content and can clearly express the same theme or information, thereby providing a reliable basis for subsequent data processing and analysis.
[0108] Figure 4 FIG. 1 is a flow chart of a method for constructing a large language model based on NL2SQL provided by another exemplary embodiment of the present invention. Figure 4As shown, Step S1: Document Loading, Text Segmentation and Merging, and Keyword Extraction. First, the document is loaded and its content is parsed and processed. This step segments and merges the text, extracting keywords and important information from the text. Step S2: Text Vectorization and Storage in a Vector Database. The keywords and text information extracted in Step S1 are vectorized, converting the text data into vector form that can be understood by the machine learning model and then storing it in the vector database. Step S3: Generate and Optimize a Dataset Using Database Metadata. In this step, metadata information in the vector database is used to generate and optimize a dataset. Metadata information includes data characteristics, field information, and other information, which can help better organize and manage data. Step S4: The user submits a query in natural language (which can be entered as a question). The user submits a query in natural language, which can be any human language or question, and does not require specialized database knowledge. Step S5: Based on the identified intent, relevant information is retrieved from the dataset. The system uses natural language processing technology to identify the intent of the user's query and then retrieves relevant information from the dataset database generated and optimized in Step S3. This process is achieved by querying the vector database. Step S6: Submit the retrieved information to the LLM in the form of a prompt and generate SQL. Organize the retrieved information into the form of a prompt and submit it to the Large Language Model (LLM), which then generates the corresponding SQL query statement. Step S7: The generated SQL statement is executed on the database and the result is fed back to the user. The generated SQL statement will be executed on the database, and the execution result will be returned to the system, which will feed back the query result to the user. Step S8: User feedback, and the feedback result will be stored in the data set. The user can give feedback on the returned results, and the system will further optimize and update the data set based on the user's feedback information, so that the system can continue to learn and improve.
[0109] Figure 5 Schematic diagram of a large language model building device based on NL2SQL provided by an exemplary embodiment of the present invention. Figure 5As shown, the large language model construction device based on NL2SQL includes: an acquisition module 201 for acquiring an original data set; a preprocessing module 202 for preprocessing the original data set to obtain a preprocessed original data set; a determination module 203 for determining the logical relationship between paragraphs in the preprocessed original data set; merging paragraphs with the same logical relationship to obtain a merged data set; extracting key information from the merged data set; a construction module 204 for constructing a training data set based on the key information; wherein the training data set includes multiple questions and SQL statements corresponding to each question; and a training module 205 for training the large language model based on the training data set to obtain a trained large language model.
[0110] Figure 6 FIG. 1 is a schematic diagram of a large language model construction device based on NL2SQL provided by another exemplary embodiment of the present invention. Figure 6 As shown, the preprocessing module 202 may include: an extraction unit 2021, used to extract key data from the original data set; a first construction unit 2022, used to construct a correspondence between the key data and its corresponding text data; wherein the key data corresponds to a first index, and the text data corresponding to the key data corresponds to a second index, and the level of the first index is higher than the level of the second index; a second construction unit 2023, used to construct a preprocessed original data set containing the correspondence.
[0111] In one embodiment, the construction module 204 includes: a conversion unit 2041 for converting key information into vector text; an encapsulation unit 2042 for encapsulating the vector text into a vector database; and a construction subunit 2043 for constructing a training data set based on the vector database.
[0112] In one embodiment, the construction subunit 2043 can be specifically implemented as follows: obtaining tables, fields, and the relationships between tables and fields in the vector database; determining the structure of text data in the vector database based on the tables, fields, and the relationships between tables and fields; analyzing the structure to obtain analysis results; and constructing a training data set based on the analysis results.
[0113] In one embodiment, the construction subunit 2043 may be specifically implemented as follows: generating a dynamic prompt template based on the analysis result; generating a training data set corresponding to the vector database based on the large language model, the dynamic prompt template and the analysis result.
[0114] In one embodiment, the training module 205 can be specifically implemented as follows: semantically expanding multiple questions in the training data set and the SQL statements corresponding to each question to obtain an expanded data set; verifying the expanded questions in the expanded data set and the SQL statements corresponding to each expanded question based on preset statement verification rules; based on the verification results, screening out the target expanded questions and the SQL statements corresponding to each target expanded question from the expanded data set; obtaining the NL2SQL data set based on the target expanded questions and the SQL statements corresponding to each target expanded question; and training the large language model based on the NL2SQL data set to obtain a trained large language model.
[0115] In one embodiment, after the training module 205, the large language model construction device based on NL2SQL can be specifically configured to: obtain query information; generate a target SQL query statement based on the query information and the trained large language model; and execute the target SQL query statement to obtain an execution result.
[0116] In one embodiment, after the training module 205, the apparatus for constructing a large language model based on NL2SQL may be specifically configured to: obtain a feedback result; and correct the SQL query statement if the feedback result indicates that the SQL query statement is incorrect.
[0117] In one embodiment, the determination module 203 can be specifically configured as follows: performing text segmentation on the merged data set to obtain segmented text; performing semantic analysis on the segmented text and determining the semantic completeness of the segmented text; if the semantic completeness is greater than a preset completeness threshold, extracting key information of the segmented text.
[0118] Figure 7 The figure shows a block diagram of an electronic device according to an embodiment of the present application.
[0119] like Figure 7 As shown, the electronic device 10 includes one or more processors 11 and a memory 12 .
[0120] The processor 11 may be a central processing unit (CPU) or other forms of processing units having data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 10 to perform desired functions.
[0121] The memory 12 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may include, for example, random access memory (RAM) and / or cache memory. The non-volatile memory may include, for example, read-only memory (ROM), a hard disk, a flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 11 may execute the program instructions to implement the NL2SQL-based large language model construction method of each embodiment of the present application described above and / or other desired functions. Various contents such as input signals, signal components, noise components, etc. may also be stored in the computer-readable storage medium.
[0122] In one example, the electronic device 10 may further include an input device 13 and an output device 14 , and these components are interconnected via a bus system and / or other forms of connection mechanisms (not shown).
[0123] When the electronic device 10 is a stand-alone device, the input device 13 may be a communication network connector, configured to receive collected input signals from the first device and the second device.
[0124] In addition, the input device 13 may also include, for example, a keyboard, a mouse, and the like.
[0125] The output device 14 can output various information to the outside, including determined distance information, direction information, etc. The output device 14 can include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, and the like.
[0126] Of course, to simplify, Figure 7 Only some of the components related to the present application in the electronic device 10 are shown, and components such as a bus, an input / output interface, etc. are omitted. In addition, the electronic device 10 may further include any other appropriate components according to specific application scenarios.
[0127] The computer program product may be written in any combination of one or more programming languages to implement the program code for performing the operations of the embodiments of the present application, including object-oriented programming languages such as Java, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0128] The computer-readable storage medium can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can, for example, include but is not limited to a system, device or component of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination thereof. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof.
[0129] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principles of the present invention should be included in the scope of protection of the present invention.
Claims
1. A method for constructing a large language model based on NL2SQL, characterized in that: include: Get the original dataset; Preprocessing the original data set to obtain a preprocessed original data set; Determining logical relationships between paragraphs in the preprocessed raw data set; Merge paragraphs with the same logical relationship to obtain a merged data set; Extracting key information from the merged data set; Based on the key information, a training data set is constructed; wherein the training data set includes multiple questions and SQL statements corresponding to each question; Based on the training data set, the large language model is trained to obtain a trained large language model.
2. The method for constructing a large language model based on NL2SQL according to claim 1, characterized in that: The preprocessing of the original data set to obtain a preprocessed original data set includes: Extracting key data from the original data set; Constructing a correspondence between the key data and its corresponding text data; wherein the key data corresponds to a first index, and the text data corresponding to the key data corresponds to a second index, and the level of the first index is higher than the level of the second index; A preprocessed original data set containing the corresponding relationship is constructed.
3. The method for constructing a large language model based on NL2SQL according to claim 1, characterized in that: The constructing of a training data set based on the key information includes: Converting the key information into vector text; Encapsulating the vector text into a vector database; Based on the vector database, a training data set is constructed.
4. The method for constructing a large language model based on NL2SQL according to claim 3, characterized in that: The constructing of a training data set based on the vector database includes: Obtaining tables, fields, and relationships between the tables and fields in the vector database; Determining the structure of text data in the vector database based on the table, the fields, and the relationship between the table and the fields; analyzing the structure to obtain an analysis result; Based on the analysis results, a training data set is constructed.
5. The method for constructing a large language model based on NL2SQL according to claim 4, characterized in that: The constructing of a training data set based on the analysis results includes: Based on the analysis results, a dynamic prompt template is generated; Based on the large language model, the dynamic prompt template and the analysis result, a training data set corresponding to the vector database is generated.
6. The method for constructing a large language model based on NL2SQL according to claim 5, characterized in that: The step of training the large language model based on the training data set to obtain the trained large language model includes: Performing semantic expansion on the multiple questions in the training data set and the SQL statements corresponding to each question to obtain an expanded data set; Verifying the expanded questions in the expanded data set and the SQL statements corresponding to each expanded question based on preset statement verification rules; Based on the verification result, selecting target expanded questions and SQL statements corresponding to each target expanded question from the expanded data set; Based on the target expanded questions and the SQL statements corresponding to each target expanded question, an NL2SQL dataset is obtained; Based on the NL2SQL dataset, the large language model is trained to obtain a trained large language model.
7. The method for constructing a large language model based on NL2SQL according to claim 1, characterized in that: After training the large language model based on the training data set to obtain a trained large language model, the method further includes: Get query information; Generate a target SQL query statement based on the query information and the trained large language model; Execute the target SQL query statement to obtain an execution result.
8. The method for constructing a large language model based on NL2SQL according to claim 7, characterized in that: After executing the SQL query statement to obtain the execution result, the method further includes: Get feedback results; If the feedback result indicates that the SQL query statement is wrong, correct the SQL query statement.
9. The method for constructing a large language model based on NL2SQL according to claim 1, characterized in that: Extracting key information from the merged dataset includes: Performing text segmentation on the merged data set to obtain segmented text; Performing semantic analysis on the segmented text and determining the semantic integrity of the segmented text; If the semantic completeness is greater than a preset completeness threshold, key information of the segmented text is extracted.
10. A large language model construction device based on NL2SQL, characterized in that: include: Acquisition module, used to obtain the original data set; A preprocessing module, configured to preprocess the original data set to obtain a preprocessed original data set; a determination module, configured to determine the logical relationship between paragraphs in the preprocessed original data set; Merge paragraphs with the same logical relationship to obtain a merged data set; Extracting key information from the merged data set; A construction module, configured to construct a training data set based on the key information; wherein the training data set includes a plurality of questions and an SQL statement corresponding to each question; The training module is used to train the large language model based on the training data set to obtain a trained large language model.
Citation Information
Patent Citations
Data analysis method, data analysis device, terminal and storage medium
CN107273519A
NL2SQL data set construction method and device, equipment and medium
CN118503270A
Method and system for generating nl2sql training set based on large model and suitable for all industries
CN118964579A
Method for creating structured document based on deep learning model
CN119558307A