SQL (Structured Query Language) statement generation method and system based on long and short term memory of large language model
By introducing long-term and short-term memory capabilities to large language models and combining multiple rounds of dialogue to extract query elements, the problems of incomplete user query requirements and low accuracy of complex query are solved, and more efficient SQL generation is achieved.
Patent Information
- Application Number
- CN202411985275.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-31
- Publication Date
- 2025-04-25
AI Technical Summary
In the existing Text-to-SQL technology, user query requirements may be incomplete or vague, resulting in the generated SQL statements being incorrect or unable to run; at the same time, when faced with complex queries with multiple indicators and multiple indexes, the accuracy of generating SQL is reduced.
By obtaining large language model and database schema design information, long-term memory is generated, and query elements are extracted in combination with multiple rounds of dialogue between users and models, short-term memory is generated, and SQL statements are finally generated based on the introduced long-term and short-term memory.
Effectively respond to the problems of vague and incomplete user query, and improve the accuracy and availability of SQL generation, especially in multi-index complex query scenarios.
Smart Images

Figure CN119884151A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of human-computer interaction technology, and in particular to a method and system for generating SQL statements based on long short-term memory of a large language model. Background Art
[0002] Text to SQL (Text to Structured Query Language, Text-to-SQL) technology refers to the process of converting natural language query questions into Structured Query Language (SQL) that can be executed in relational databases. The core goal of Text-to-SQL is to achieve intelligent interaction between people and databases, allowing users to ask questions in a natural language and the Text-to-SQL system automatically generates the corresponding SQL statements, replacing manual SQL programming and simplifying the interaction process between users and databases.
[0003] In the related art, the following methods are usually used to implement Text-to-SQL: (1) Machine learning-based methods, typically "Sequence to Sequence (Seq2Seq)" models, including Seq2SQL and RyanSQL; this type of model is based on the "encoder-decoder" architecture. After model training, the encoder can understand the database table model and the input natural language question, and then the decoder can predict the SQL statement. (2) Large Language Model (LLM) fine-tuning methods, typically using a Text-to-SQL specific dataset, fine-tuning on the basis of the pre-trained LLM, and obtaining a fine-tuned model that focuses on the Text-to-SQL field; a specific representative is SQL Coder. (3) LLM retrieval enhancement generation framework-based methods, typically represented by Vanna; Vanna identifies background knowledge related to user questions in an unsupervised manner, such as similar table structure design; and embeds the background knowledge into the prompt word through context learning to provide background knowledge for LLM to generate SQL.
[0004] However, the above methods in the related art have the following defects: (1) The problem of incomplete user query requirements: Among the currently popular methods, such as those based on LLM fine-tuning or LLM retrieval enhancement generation, most are based on the premise that the user query requirement information is sufficiently complete, and then combined with the database model design to generate SQL statements. In reality, the query requirements proposed by the user may have incomplete or vague query elements. For example, the user may only know the target table to be queried. This situation can easily cause the generated SQL to report an error or fail to run. (2) Complex query problems with multiple indicators and multiple indexes: When facing complex queries, such as multi-index queries, directly using a large model to generate SQL may not cover all indexes, resulting in a decrease in the accuracy of the generated SQL. Summary of the invention
[0005] In view of the shortcomings of the prior art, the present application provides a method and system for generating SQL statements based on long short-term memory of a large language model, which solves the problems of incomplete query elements and low accuracy of overall SQL generation in Text-to-SQL.
[0006] To achieve the above objectives, this application is implemented through the following technical solutions:
[0007] In a first aspect, an embodiment of the present application provides a method for generating SQL statements based on long and short-term memory of a large language model, the method comprising: obtaining a preset large language model, and extracting the schema design information of the target database of the large language model through a database administrator; wherein the schema design information includes table names, table comments, columns, column types, and column comments; analyzing and completing the schema design information, generating long-term memory to introduce the large language model; wherein the long-term memory is updated when the schema of the target database is updated; extracting query elements through multiple rounds of dialogue between the user and the model, generating short-term memory to introduce the large language model; obtaining an SQL generation task, and generating an SQL statement based on the large language model after the introduction of long-term memory and short-term memory to extract information from the target database.
[0008] According to the first aspect of the embodiment of the present application, the aforementioned analysis and completion of the pattern design information to generate long-term memory for introducing a large language model may specifically include: automatically traversing the target database through a program, searching for missing table and column annotation information in the target database, obtaining the necessary information to be completed and prompting the database administrator; adding a new column Is_index to characterize whether each column is an index column, wherein Is_index=1 indicates that a column is an index column, and Is_index=0 indicates that a column is not an index column; assigning the Is_index value of a column of a typical index type to 1 through a program; wherein a typical index includes datetime and longitude and latitude; manually specifying uncovered indicator columns through a database administrator in combination with background knowledge of the target database; completing necessary information through a database administrator to summarize and complete the pattern design information; storing the completed pattern design information in an EXCEL file to obtain long-term memory and introducing the long-term memory into a large language model.
[0009] According to the first aspect of the embodiment of the present application, the aforementioned large language model is a Qwen2.5 72b large model; the aforementioned extraction of query elements through multiple rounds of dialogue between the user and the model, generating short-term memory to introduce the large language model, can specifically include: determining the first subtask through user questions to start the dialogue process, starting a preset prompt word project to design a first prompt word; wherein the first subtask is to extract query information; setting the inference parameters for the Qwen2.5 72b large model, wherein when setting the inference parameters, the generation randomness setting is reduced to ensure the uniformity of the reply; after the inference parameters are set, the first prompt word is input into the Qwen2.5 72b large model for inference to extract query elements; the query elements are stored in relevant session variables as short-term memory, and the elements that have not been extracted are left blank; wherein the relevant session variables include a target table, a target column, a target index and a target operation; judging whether all query elements are not empty, and if so, confirming that the dialogue between the user and the model is terminated.
[0010] According to the first aspect of the embodiment of the present application, the aforementioned extraction of query elements through multiple rounds of dialogue between the user and the model, generating short-term memory to introduce a large language model, may also specifically include: when there are null values in all query elements, for the second subtask of characterization judgment and query, designing a second prompt word to prompt the user that the query element is missing and giving relevant recommendations; inputting the second prompt word into the Qwen2.5 72b large model for reasoning to obtain prompt recommendation information; feeding back the prompt recommendation information to the user so that the user can supplement the relevant information, and conducting a new round of dialogue between the user and the model.
[0011] According to the first aspect of the embodiment of the present application, the first prompt word corresponds to a preset first standard, and the first standard includes: clearly describing the first subtask; and introducing the target database model information in a context learning manner to help the large model extract the elements to be queried based on the user's questions.
[0012] According to the first aspect of the embodiment of the present application, the second prompt word corresponds to a preset second standard, and the second standard includes: clearly describing the second subtask; and in the absence of a certain type of query element, combining the database model to design long-term memory, traversing the index column of Is_index=1 to make recommendations to the user.
[0013] According to the first aspect of the embodiment of the present application, the aforementioned large language model is a Qwen2.5 72b large model; the aforementioned acquisition of SQL generation tasks, and generating SQL statements based on the large language model after introducing long-term memory and short-term memory to extract information from the target database, can specifically include: acquiring the SQL generation task, and designing a third prompt word based on the SQL generation task; wherein the third prompt word meets a preset third standard, and the third standard includes clearly describing the third subtask and summarizing the complete query elements in the existing short-term memory; inputting the third prompt word into the Qwen2.572b large model for reasoning to obtain a generated SQL statement; according to the generated SQL statement, extracting corresponding target data from the target database to meet user needs.
[0014] In a second aspect, an embodiment of the present application provides an SQL statement generation system based on long and short-term memory of a large language model, the SQL statement generation system comprising an information extraction module, a long-term memory generation module, a short-term memory generation module and an SQL generation module; specifically, the information extraction module is used to obtain a preset large language model, and extract the schema design information of the target database of the large language model through a database administrator; wherein the schema design information includes table names, table comments, columns, column types and column comments; the long-term memory generation module is used to analyze and complete the schema design information, generate long-term memory to introduce a large language model; wherein the long-term memory is updated when a schema update occurs in the target database; the short-term memory generation module is used to extract query elements through multiple rounds of dialogue between the user and the model, and generate short-term memory to introduce a large language model; the SQL generation module is used to obtain SQL generation tasks, and generate SQL statements based on the large language model after the introduction of long-term memory and short-term memory to extract information from the target database.
[0015] In a third aspect, an embodiment of the present application provides an electronic device, comprising: a processor, a memory, and a program stored in the memory and executable on the processor, wherein when the program is executed by the processor, the method for generating SQL statements based on long short-term memory of a large language model in the aforementioned first aspect is implemented.
[0016] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, which stores a program or instruction. When the program or instruction is executed by a processor, the method for generating SQL statements based on long short-term memory of a large language model in the aforementioned first aspect is implemented.
[0017] This application provides a method and system for generating SQL statements based on long short-term memory of a large language model. Compared with the prior art, it has the following beneficial effects:
[0018] This application extracts the schema design information of the target database of the large language model through the database administrator; analyzes and completes the schema design information, generates long-term memory to introduce the large language model; when extracting effective query information, the long-term memory can provide sufficient background information for the large language model, and effectively deal with multi-index, multi-index complex queries. This application extracts query elements through multiple rounds of dialogue between the user and the model, generates short-term memory and introduces a large language model; generates SQL statements based on the large language model after the introduction of long-term memory and short-term memory, and the large language model conducts multiple rounds of dialogue with the user. When a certain query element is missing, it can make recommendations to the user based on long-term memory, thereby making full use of the flexible dialogue interaction capabilities of the large language model to effectively deal with problems such as fuzzy and incomplete user queries. BRIEF DESCRIPTION OF THE DRAWINGS
[0019] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0020] Figure 1 It is a flowchart of a method for generating SQL statements based on long short-term memory of a large language model provided in an embodiment of the present application;
[0021] Figure 2 It is an exemplary flow chart of another method for generating SQL statements based on long short-term memory of a large language model provided in an embodiment of the present application;
[0022] Figure 3 It is a structural diagram of a SQL statement generation system based on long short-term memory of a large language model provided in an embodiment of the present application;
[0023] Figure 4 It is a structural schematic diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention are clearly and completely described. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0025] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0026] The embodiments of the present application provide a method and system for generating SQL statements based on long short-term memory of a large language model, thereby solving the problems of incomplete query elements and low accuracy of overall SQL generation in Text-to-SQL.
[0027] The technical solution in the embodiment of the present application is to solve the above technical problems, and the overall idea is as follows:
[0028] Text to SQL (Text to Structured Query Language, Text-to-SQL) technology refers to the process of converting natural language query questions into Structured Query Language (SQL) that can be executed in relational databases. The core goal of Text-to-SQL is to achieve intelligent interaction between people and databases, allowing users to ask questions in a natural language and the Text-to-SQL system automatically generates the corresponding SQL statements, replacing manual SQL programming and simplifying the interaction process between users and databases.
[0029] In related technologies, the following methods are usually used to implement Text-to-SQL:
[0030] (1) Based on machine learning methods, the typical model is the "Sequence to Sequence (Seq2Seq)" type model, and specific representatives include Seq2SQL and RyanSQL; this type of model is based on the "encoder-decoder" architecture. After model training, the encoder can understand the database table model and the input natural language questions, and then the decoder can predict the SQL statement.
[0031] (2) Methods based on Large Language Model (LLM) fine-tuning, which typically uses a specific Text-to-SQL dataset and fine-tunes the pre-trained LLM to obtain a fine-tuned model that focuses on the Text-to-SQL field; a specific representative example is SQL Coder.
[0032] (3) Methods based on the LLM retrieval enhancement generation framework, a typical example of which is Vanna. Vanna uses an unsupervised approach to identify background knowledge related to user questions, such as similar table structure designs. In addition, it embeds the background knowledge into prompt words through contextual learning to provide background knowledge for LLM to generate SQL.
[0033] The related technologies have the following defects in Text-to-SQL: (1) The problem of incomplete user query requirements: Among the currently popular methods, such as those based on LLM fine-tuning or LLM retrieval enhancement generation, most are based on the premise that the user query requirement information is sufficiently complete, and then combined with the database model design to generate SQL statements. In reality, the query requirements proposed by users may have incomplete or vague query elements. For example, the user may only know the target table to be queried. This situation can easily cause the generated SQL to report an error or fail to run. (2) Complex query problems with multiple indicators and multiple indexes: When faced with complex queries, such as multi-index queries, directly using a large model to generate SQL may not cover all indexes, resulting in a decrease in the accuracy of the generated SQL.
[0034] In summary, in the existing Text-to-SQL technology, most of the focus is on the generation of "SQL", while less consideration is given to the query requirements proposed by users, that is, less consideration is given to the complexity of "Text" (such as colloquialism, lack of key information, etc.) and the complexity of the target database schema design (for example, the same indicator may have multiple indexes), which reduces the accuracy of the overall SQL generation.
[0035] In order to better understand the above technical solution, the above technical solution will be described in detail below in conjunction with the accompanying drawings and specific implementation methods.
[0036] The following first introduces a method for generating SQL statements based on long short-term memory of a large language model provided in an embodiment of the present application.
[0037] The present application embodiment provides a flow chart of a method for generating SQL statements based on long short-term memory of a large language model, such as Figure 1 As shown, the SQL statement generation method based on large language model long short-term memory may include the following steps S110-S140.
[0038] S110, obtaining a preset large language model, and extracting schema design information of a target database of the large language model through a database administrator; wherein the schema design information includes table name, table comment, column, column type, and column comment.
[0039] S120, analyzing and completing the pattern design information, generating long-term memory to introduce a large language model; wherein the long-term memory is updated when a pattern update occurs in the target database.
[0040] S130. Extract query elements through multiple rounds of dialogue between the user and the model, generate short-term memory to introduce a large language model.
[0041] S140, obtaining an SQL generation task, and generating an SQL statement according to the large language model after introducing long-term memory and short-term memory, so as to extract information from the target database.
[0042] The above is a specific implementation method of a SQL statement generation method based on long short-term memory of a large language model provided in the embodiment of the present application. Please refer to Figure 1 and Figure 2 It can be understood that the present application extracts the schema design information of the target database of the large language model through the database administrator; analyzes and completes the schema design information, generates long-term memory to introduce the large language model; when extracting effective query information, the long-term memory can provide sufficient background information for the large language model, and effectively respond to multi-indicator, multi-index complex queries.
[0043] Furthermore, the present application extracts query elements through multiple rounds of dialogues between the user and the model, generates short-term memory and introduces a large language model; generates SQL statements based on the large language model after the introduction of long-term memory and short-term memory, and the large language model conducts multiple rounds of dialogues with the user. When a certain query element is missing, it can make recommendations to the user based on the long-term memory, thereby making full use of the flexible dialogue interaction capabilities of the large language model to effectively deal with problems such as vague and incomplete user queries.
[0044] In some embodiments, the aforementioned analysis and completion of the pattern design information to generate long-term memory to introduce a large language model, that is, the aforementioned S120 may specifically include the following steps:
[0045] S210, automatically traverse the target database through the program, find the missing table and column annotation information in the target database, obtain the necessary information to be completed and prompt the database administrator.
[0046] S220 , adding a column Is_index to indicate whether each column is an index column, wherein Is_index=1 indicates that a column is an index column, and Is_index=0 indicates that a column is not an index column.
[0047] S230. Assign the value of Is_index of a column of a typical index type to 1 through a program; wherein the typical index includes datetime and longitude and latitude.
[0048] S240. Based on the background knowledge of the target database, the database administrator manually specifies the uncovered indicator columns.
[0049] S250. The database administrator completes necessary information to summarize and complete the schema design information.
[0050] S260, storing the completed pattern design information into an EXCEL file, obtaining long-term memory and introducing the long-term memory into the large language model.
[0051] In the embodiments of the present application, it can be understood that the present application introduces the long-term memory of the target database schema information into the large language model, and prominently marks the index columns, so as to provide sufficient background information for the large language model when extracting effective query information, and effectively respond to multi-indicator, multi-index complex queries.
[0052] In some embodiments, the aforementioned large language model can be a Qwen2.5 72b large model. A Text-to-SQL method is proposed that can take into account the complexity of user questions and the complexity of target database model design, thereby improving the accuracy of SQL statement generation. This application is developed based on the Qwen2.572b large language model, and introduces long-term and short-term memory capabilities into the model based on the Qwen2.5 72b model.
[0053] Furthermore, the aforementioned extraction of query elements through multiple rounds of dialogue between the user and the model, generating short-term memory to introduce the large language model, that is, the aforementioned S130 may specifically include the following steps:
[0054] S310, determining a first subtask by asking a question by a user to start a dialogue process, and starting a preset prompt word project to design a first prompt word; wherein the first subtask is to extract query information.
[0055] S320, setting inference parameters for the Qwen2.5 72b large model, wherein when setting inference parameters, lowering the generation randomness setting to ensure uniformity of responses.
[0056] S330: After the inference parameters are set, the first prompt word is input into the Qwen2.5 72b large model for inference to extract query elements.
[0057] S340. Store the query elements into relevant session variables as short-term memory, and leave the elements that have not been extracted blank; wherein the relevant session variables include the target table, target column, target index and target operation.
[0058] S350: Determine whether all query elements are not empty. If so, confirm that the dialogue between the user and the model is terminated.
[0059] In some other optional embodiments, the above-mentioned extraction of query elements through multiple rounds of dialogue between the user and the model, generating short-term memory to introduce into the large language model, that is, the above-mentioned S130 may specifically include the following steps:
[0060] S360: When there are null values in all query elements, for the second subtask of characterizing judgment and querying, a second prompt word is designed to prompt the user that the query element is missing and to give relevant recommendations.
[0061] S370: Input the second prompt word into the Qwen2.5 72b large model for reasoning to obtain prompt recommendation information.
[0062] S380: Feedback the prompt recommendation information to the user so that the user can supplement relevant information and conduct a new round of dialogue between the user and the model. The prompt recommendation information may include queryable indicators and indexes.
[0063] In the embodiments of the present application, it can be understood that long-term memory refers to the schema design of the target database, including the names of different tables and columns, comments, etc. Short-term memory refers to the query elements extracted from the user's questions during multiple rounds of dialogue interaction with the user, including the target table and column of the user's query. Both long-term memory and short-term memory are written into the prompt word through context learning.
[0064] It should be emphasized that the existing Text-to-SQL methods have two major defects: (1) they rarely consider that the user's query request may be ambiguous and incomplete; (2) they have low accuracy when dealing with complex queries with multiple indicators and indexes.
[0065] This application introduces four short-term memory variables, namely target table, target column, target index and target operation, into the large language model. The large language model uses whether all the above variables are not empty as the standard to conduct multiple rounds of dialogue with the user. When a certain query element is missing, it can make recommendations to the user based on long-term memory, effectively dealing with problems such as vague and incomplete user queries.
[0066] In one example, the first prompt word corresponds to a preset first standard, which includes: clearly describing the first subtask; and introducing target database model information in a context learning manner to help the large model extract the elements to be queried based on the user's question.
[0067] In another example, the second prompt word corresponds to a preset second standard, which includes: clearly describing the second subtask; and in the absence of a certain type of query element, designing long-term memory in combination with the database model, traversing the index column of Is_index=1 to make recommendations to the user.
[0068] It can be understood that this application takes improving the practicality of Text-to-SQL as its starting point. By introducing long-term and short-term memory capabilities into the large language model and learning the prompt words based on context, it fully utilizes the flexible interactive capabilities of the large language model, gradually prompts and recommends users to complete query requests, and ultimately improves the accuracy and availability of the generated SQL.
[0069] In some embodiments, the aforementioned large language model is a Qwen2.5 72b large model. The aforementioned acquisition of SQL generation tasks and generation of SQL statements based on the large language model after introducing long-term memory and short-term memory to extract information from the target database, that is, the aforementioned S140 may specifically include the following steps:
[0070] S410, obtaining an SQL generation task, and designing a third prompt word based on the SQL generation task; wherein the third prompt word meets a preset third standard, and the third standard includes clearly describing the third subtask and summarizing the complete query elements in the existing short-term memory.
[0071] S420: Input the third prompt word into the Qwen2.5 72b large model for reasoning to obtain a generated SQL statement.
[0072] S430: According to the generated SQL statement, corresponding target data is extracted from the target database to meet user needs.
[0073] In the embodiments of the present application, it can be understood that after the introduction of long-term memory and short-term memory, the Qwen2.5 72b large model can complete the following three subtasks according to the prompt word requirements: (1) extract as much valid query information as possible from the user's question in accordance with the long-term memory; (2) automatically determine which query elements are still missing as of the current dialogue round in accordance with the short-term memory, prompt the user and give the user recommendations; (3) generate SQL statements when all query elements are complete.
[0074] In other words, the existing Text-to-SQL technology cannot cope well with the imperfect user query requirements and multi-index multi-index complex query problems. This application introduces long short-term memory for the Qwen 2.5 72b large model. The prompt word engineering based on context learning enables the large model to automatically identify whether query elements are missing in multiple rounds of dialogue with users, prompt users to supplement and recommend query elements, until enough query elements are extracted, and summarized into a query sentence with complete query elements, and then extract the corresponding target data from the target database to meet user needs, effectively improving the accuracy of Text-to-SQL.
[0075] It should be noted that both long-term memory and short-term memory are introduced through contextual prompt word engineering. Compared with model fine-tuning methods such as SQL Coder, this application does not require retraining of large language models and can be deployed quickly. At the same time, benefiting from the design principle of separating long-term and short-term memory, when the target database is changed, only the relevant long-term memory files need to be changed, and it can be flexibly applied to different database queries.
[0076] In some embodiments, the present application provides a SQL statement generation system 500 based on a large language model long short-term memory, such as Figure 3 As shown, the SQL statement generation system 500 based on large language model long short-term memory may include the following modules:
[0077] The information extraction module 510 is used to obtain a preset large language model and extract the schema design information of the target database of the large language model through a database administrator; wherein the schema design information includes table name, table comment, column, column type and column comment;
[0078] A long-term memory generation module 520 is used to analyze and complete the pattern design information and generate long-term memory to introduce a large language model; wherein the long-term memory is updated when a pattern update occurs in the target database;
[0079] A short-term memory generation module 530 is used to extract query elements through multiple rounds of dialogue between the user and the model, and generate short-term memory to introduce into the large language model;
[0080] The SQL generation module 540 is used to obtain SQL generation tasks and generate SQL statements based on the large language model after introducing long-term memory and short-term memory to extract information from the target database.
[0081] According to an embodiment of the present application, any multiple modules among the information extraction module 510, the long-term memory generation module 520, the short-term memory generation module 530 and the SQL generation module 540 can be combined into one module for implementation, or any one of the modules can be split into multiple modules. Alternatively, at least part of the functions of one or more of these modules can be combined with at least part of the functions of other modules and implemented in one module.
[0082] In some embodiments, the long-term memory generation module 520 may be specifically used to:
[0083] The program automatically traverses the target database, searches for missing table and column annotation information in the target database, obtains necessary information to be completed, and prompts the database administrator;
[0084] A new column Is_index is added to indicate whether each column is an index column, where Is_index=1 indicates that a column is an index column, and Is_index=0 indicates that a column is not an index column;
[0085] The Is_index value of the column with the typical index type is assigned to 1 by the program; the typical index includes datetime and latitude and longitude;
[0086] Combined with the background knowledge of the target database, the database administrator manually specifies the uncovered indicator columns;
[0087] The database administrator completes the necessary information to summarize and complete the schema design information;
[0088] The completed pattern design information is stored in an EXCEL file to obtain long-term memory and introduce the long-term memory into the large language model.
[0089] In some embodiments, the short-term memory generation module 530 may be specifically used to:
[0090] Determine the first subtask by asking a question by the user to start the dialogue process, and start the preset prompt word project to design the first prompt word; wherein the first subtask is to extract the query information;
[0091] The inference parameters of the Qwen2.5 72b large model were set. When setting the inference parameters, the generation randomness setting was lowered to ensure the uniformity of the responses.
[0092] After the inference parameters are set, the first prompt word is input into the Qwen2.5 72b large model for inference to extract the query elements;
[0093] Store the query elements into relevant session variables as short-term memory, and leave the elements that have not been extracted blank; wherein the relevant session variables include the target table, target column, target index and target operation;
[0094] Determine whether all query elements are not empty. If so, confirm that the dialogue between the user and the model is terminated.
[0095] In some embodiments, the short-term memory generation module 530 may also be used to:
[0096] When there are null values in all query elements, for the second subtask of characterization judgment and query, a second prompt word is designed to prompt the user that the query element is missing and give relevant recommendations;
[0097] The second prompt word is input into the Qwen2.5 72b large model for reasoning to obtain prompt recommendation information;
[0098] The recommended information is fed back to the user so that the user can supplement relevant information and start a new round of dialogue between the user and the model.
[0099] In some embodiments, the SQL generation module 540 may be specifically used to:
[0100] Obtaining an SQL generation task, and designing a third prompt word based on the SQL generation task; wherein the third prompt word meets a preset third standard, and the third standard includes clearly describing the third subtask and summarizing complete query elements in the existing short-term memory;
[0101] The third prompt word is input into the Qwen2.5 72b large model for reasoning to obtain the generated SQL statement;
[0102] According to the generated SQL statements, the corresponding target data is extracted from the target database to meet user needs.
[0103] Figure 3 Each module in the system shown has the function of implementing each step in the aforementioned SQL statement generation method based on long short-term memory of a large language model, and can achieve its corresponding technical effect. For the sake of concise description, it will not be repeated here.
[0104] In some embodiments, the present application tests the aforementioned SQL statement generation system 500 based on the large language model long short-term memory, and the SQL statement generation system 500 can prompt the user to supplement the query elements.
[0105] After the user asks for a query such as "Help me check the real-time wind power output", the system can combine long-term memory to identify that the user's query request lacks the index "target date" and prompt the user to add it. At the same time, the query element "real-time wind power output" included in the user's current conversation is extracted and stored in the relevant short-term memory variable.
[0106] In the second round of dialogue, the user can enter "query the data of the whole day of XX date" based on the index prompt "target date"; according to the user's query requirements and based on the database structure design, the system can find the following information: (1) target table: t public real data target column; (2) real_wind_power; (3) target index: date time within the whole day of XX date; (4) target operation: query. At the same time, the system can summarize the query requirements into a database query requirement and query the real wind power indicators in the t public real data table within the whole day of XX date; the system can also continue to generate SQL statements to query and extract information in the target database.
[0107] In other words, in the second round of dialogue, query elements are extracted from the user's input again. And when all query elements are complete, that is, all short-term memory variables are not empty, it automatically jumps to the SQL generation stage, generates SQL statements, and reads data from the database.
[0108] Furthermore, the SQL statement generation system 500 based on long short-term memory of a large language model can also cope with complex query scenarios with multiple indicators and multiple indexes. When the user lacks background information, for example, the user initially inputs "check the weather for me" and does not know in advance what weather indicators there are. Benefiting from the introduction of long-term memory, the system can prompt and recommend indicators and indexes that can be queried to the user.
[0109] For example, the indices that can be queried include: average 2m temperature, average 2m relative humidity, average surface pressure, average 10m wind speed, downward irradiance, and total cloud cover. The indexes that can be queried include: forecast result generation date, forecast data for the next t days, day standard time period, region category, latitude, longitude, longitude and latitude, and data update time.
[0110] In some embodiments, the present application provides an electronic device, the structure diagram of the electronic device is as follows Figure 4 shown.
[0111] The electronic device may include a processor 610 and a memory 620 storing computer program instructions.
[0112] Specifically, the processor 610 may include a central processing unit (CPU), or an application specific integrated circuit (ASIC), or may be configured to implement one or more integrated circuits of the embodiments of the present application.
[0113] The memory 620 may include a large capacity memory for data or instructions. By way of example and not limitation, the memory 620 may include a hard disk drive (HDD), a floppy disk drive, a flash memory, an optical disk, a magneto-optical disk, a magnetic tape, or a universal serial bus (USB) drive or a combination of two or more of these. In appropriate cases, the memory 620 may include a removable or non-removable (or fixed) medium. In appropriate cases, the memory 620 may be inside or outside the integrated gateway disaster recovery device. In a specific embodiment, the memory 620 is a non-volatile solid-state memory.
[0114] The memory 620 may include a read-only memory (ROM), a random access memory (RAM), a disk storage medium device, an optical storage medium device, a flash memory device, an electrical, optical or other physical / tangible memory storage device. Therefore, generally, the memory 620 includes one or more tangible (non-transitory) computer-readable storage media (e.g., a memory device) encoded with software including computer executable instructions, and when the software is executed (e.g., by one or more processors), it can perform the operations described in any one of the above-mentioned embodiments of the method for generating SQL statements based on long short-term memory of a large language model.
[0115] The processor 610 implements any one of the SQL statement generation methods based on large language model long short-term memory in the above embodiments by reading and executing computer program instructions stored in the memory 620 .
[0116] In one example, the electronic device may further include a communication interface 630 and a bus 600. Figure 4 As shown, the processor 610, the memory 620, and the communication interface 630 are connected via a bus 600 and communicate with each other.
[0117] The communication interface 630 is mainly used to implement communication between various modules, devices, units and / or equipment in the embodiments of the present application.
[0118] Bus 600 includes hardware, software or both, and the parts of online data flow billing equipment are coupled to each other. For example, but not limitation, bus may include accelerated graphics port (AGP) or other graphics bus, enhanced industrial standard architecture (EISA) bus, front-end bus (FSB), hypertransport (HT) interconnection, industrial standard architecture (ISA) bus, infinite bandwidth interconnection, low pin count (LPC) bus, memory bus, micro channel architecture (MCA) bus, peripheral component interconnection (PCI) bus, PCI-Express (PCI-X) bus, serial advanced technology attachment (SATA) bus, video electronics standard association local (VLB) bus or other suitable bus or two or more of these combinations. In appropriate cases, bus 600 may include one or more buses. Although the present application embodiment describes and shows a specific bus, the present application considers any suitable bus or interconnection.
[0119] In addition, in combination with the SQL statement generation method based on long short-term memory of a large language model in the above embodiment, the embodiment of the present application can provide a computer storage medium for implementation. The computer storage medium stores computer program instructions; when the computer program instructions are executed by the processor, any one of the SQL statement generation methods based on long short-term memory of a large language model in the above embodiment is implemented.
[0120] It should be clear that the present application is not limited to the specific configuration and processing described above and shown in the figures. For the sake of simplicity, a detailed description of the known method is omitted here. In the above embodiments, several specific steps are described and shown as examples. However, the method process of the present application is not limited to the specific steps described and shown, and those skilled in the art can make various changes, modifications and additions, or change the order between the steps after understanding the spirit of the present application.
[0121] The functional blocks shown in the above block diagram can be implemented as hardware, software, firmware or a combination thereof. When implemented in hardware, it can be, for example, an electronic circuit, an application specific integrated circuit (ASIC), appropriate firmware, a plug-in, a function card, etc. When implemented in software, the elements of the present application are programs or code segments that are used to perform the required tasks. The program or code segment can be stored in a machine-readable medium, or transmitted on a transmission medium or a communication link by a data signal carried in a carrier wave. "Machine-readable medium" can include any medium capable of storing or transmitting information. Examples of machine-readable media include electronic circuits, semiconductor memory devices, ROM, flash memory, erasable ROM (EROM), floppy disks, CD-ROMs, optical disks, hard disks, optical fiber media, radio frequency (RF) links, etc. The code segment can be downloaded via a computer network such as the Internet, an intranet, etc.
[0122] It should also be noted that the exemplary embodiments mentioned in this application describe some methods or systems based on a series of steps or devices. However, this application is not limited to the order of the above steps, that is, the steps can be performed in the order mentioned in the embodiment, or in a different order from the embodiment, or several steps can be performed simultaneously.
[0123] Aspects of the present disclosure are described above with reference to the flowchart and / or block diagram of the method, device (system) and computer program product according to the embodiment of the present disclosure. It should be understood that each box in the flowchart and / or block diagram and the combination of each box in the flowchart and / or block diagram can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device to produce a machine so that these instructions executed by the processor of the computer or other programmable data processing device enable the implementation of the function / action specified in one or more boxes of the flowchart and / or block diagram. Such a processor can be, but is not limited to, a general-purpose processor, a special-purpose processor, a special application processor, or a field programmable logic circuit. It can also be understood that each box in the block diagram and / or flowchart and the combination of boxes in the block diagram and / or flowchart can also be implemented by dedicated hardware that performs a specified function or action, or can be implemented by a combination of dedicated hardware and computer instructions.
[0124] In summary, compared with the prior art, this application has the following beneficial effects:
[0125] 1. This application extracts the schema design information of the target database of the large language model through the database administrator; analyzes and completes the schema design information, generates long-term memory to introduce the large language model; when extracting effective query information, the long-term memory can provide sufficient background information for the large language model, and effectively respond to multi-indicator, multi-index complex queries.
[0126] 2. This application introduces four short-term memory variables, namely, target table, target column, target index and target operation, into the large language model. The large language model uses whether all the above variables are not empty as the standard to conduct multiple rounds of dialogue with the user. When a certain query element is missing, it can make recommendations to the user based on long-term memory, effectively dealing with problems such as vague and incomplete user queries.
[0127] 3. This application takes improving the practicality of Text-to-SQL as its starting point. By introducing long-term and short-term memory capabilities into the large language model and learning the prompt words based on context, it fully utilizes the flexible interactive capabilities of the large language model, gradually prompts and recommends users to complete query requests, and ultimately improves the accuracy and availability of the generated SQL.
[0128] 4. The context-learning-based prompt word engineering of this application enables the large model to automatically identify whether query elements are missing in multiple rounds of conversations with users, prompt users to supplement and recommend query elements, until sufficient query elements are extracted and summarized into a query statement with complete query elements, and then extract the corresponding target data from the target database to meet user needs, effectively improving the accuracy of Text-to-SQL.
[0129] 5. Compared with SQL Coder and other model-based fine-tuning methods, this application does not require retraining of large language models and can be deployed quickly. At the same time, benefiting from the design principle of separating long-term and short-term memory, when the target database is changed, only the relevant long-term memory files need to be changed, and it can be flexibly applied to different database queries.
[0130] The above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A method for generating SQL statements based on long short-term memory of a large language model, characterized in that: include: Obtaining a preset large language model, and extracting schema design information of a target database of the large language model through a database administrator; wherein the schema design information includes table names, table comments, columns, column types, and column comments; Analyzing and completing the pattern design information to generate long-term memory to introduce the large language model; wherein the long-term memory is updated when the pattern of the target database is updated; Extract query elements through multiple rounds of dialogue between the user and the model, and generate short-term memory to introduce into the large language model; An SQL generation task is acquired, and an SQL statement is generated according to the large language model after the long-term memory and the short-term memory are introduced to extract information from the target database.
2. The method for generating SQL statements based on long short-term memory of a large language model according to claim 1, characterized in that: The analyzing and completing the pattern design information to generate long-term memory for introduction into the large language model includes: Automatically traverse the target database through a program, search for missing table and column annotation information in the target database, obtain necessary information to be completed and prompt a database administrator; A new column Is_index is added to indicate whether each column is an index column, where Is_index=1 indicates that a column is an index column, and Is_index=0 indicates that a column is not an index column; The Is_index value of the column whose type is a typical index is assigned to 1 by a program; wherein the typical index includes datetime and latitude and longitude; In combination with the background knowledge of the target database, the database administrator manually specifies the uncovered indicator columns; The database administrator completes the necessary information to summarize and complete the schema design information; The completed pattern design information is stored in an EXCEL file to obtain long-term memory and introduce the long-term memory into the large language model.
3. The method for generating SQL statements based on long short-term memory of a large language model according to claim 1, characterized in that: The large language model is a Qwen2.5 72b large model; the method of extracting query elements through multiple rounds of dialogue between the user and the model and generating short-term memory to introduce the large language model includes: Determine the first subtask by asking a question by the user to start the dialogue process, and start the preset prompt word project to design the first prompt word; wherein the first subtask is to extract the query information; Setting the inference parameters for the Qwen2.5 72b large model, wherein when setting the inference parameters, the generation randomness setting is reduced to ensure the uniformity of the responses; After the inference parameters are set, the first prompt word is input into the Qwen2.572b large model for inference to extract query elements; The query elements are stored in relevant session variables as short-term memory, and the elements that have not been extracted are left blank; wherein the relevant session variables include a target table, a target column, a target index, and a target operation; Determine whether all query elements are not empty. If so, confirm that the dialogue between the user and the model is terminated.
4. The method for generating SQL statements based on long short-term memory of a large language model according to claim 3, characterized in that: The method of extracting query elements through multiple rounds of dialogue between the user and the model and generating short-term memory to introduce into the large language model also includes: When there are null values in all query elements, for the second subtask of characterization judgment and query, a second prompt word is designed to prompt the user that the query element is missing and give relevant recommendations; Inputting the second prompt word into the Qwen2.5 72b large model for reasoning to obtain prompt recommendation information; The prompt recommendation information is fed back to the user so that the user can supplement relevant information, and a new round of dialogue is carried out between the user and the model.
5. The method for generating SQL statements based on long short-term memory of a large language model as claimed in claim 3, characterized in that: The first prompt word corresponds to a preset first standard, which includes: clearly describing the first subtask; and introducing target database model information in a context learning manner to help the large model extract the elements to be queried based on the user's questions.
6. The method for generating SQL statements based on long short-term memory of a large language model according to claim 4, characterized in that: The second prompt word corresponds to a preset second standard, which includes: clearly describing the second subtask; and in the absence of a certain type of query element, designing long-term memory in combination with the database model, traversing the index column of Is_index=1 to make recommendations to the user.
7. The method for generating SQL statements based on long short-term memory of a large language model according to claim 1, characterized in that: The large language model is a Qwen2.5 72b large model; the acquiring SQL generation task and generating SQL statements according to the large language model after introducing long-term memory and short-term memory to extract information from the target database include: Acquire an SQL generation task, and design a third prompt word based on the SQL generation task; wherein the third prompt word meets a preset third standard, and the third standard includes clearly describing the third subtask and summarizing complete query elements in the existing short-term memory; Input the third prompt word into the Qwen2.5 72b large model for reasoning to obtain a generated SQL statement; According to the generated SQL statement, corresponding target data is extracted from the target database to meet user needs.
8. A SQL statement generation system based on long short-term memory of a large language model, characterized in that: include: An information extraction module, used to obtain a preset large language model, and extract the schema design information of the target database of the large language model through a database administrator; wherein the schema design information includes table name, table comment, column, column type and column comment; A long-term memory generation module, used to analyze and complete the pattern design information, generate long-term memory to introduce the large language model; wherein the long-term memory is updated when the pattern of the target database is updated; A short-term memory generation module, used to extract query elements through multiple rounds of dialogue between the user and the model, and generate short-term memory to introduce into the large language model; The SQL generation module is used to obtain an SQL generation task and generate an SQL statement according to the large language model after introducing long-term memory and short-term memory to extract information from the target database.
9. An electronic device, characterized in that: include: A processor, a memory, and a program stored in the memory and executable on the processor, wherein when the program is executed by the processor, the method for generating SQL statements based on long short-term memory of a large language model as described in any one of claims 1 to 7 is implemented.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a program or instruction, and when the program or instruction is executed by the processor, the method for generating SQL statements based on long short-term memory of a large language model according to any one of claims 1 to 7 is implemented.
Citation Information
Cited By
Vehicle structured data query and visualization method based on large model
CN121412303A
SQL statement execution control method and system and server
CN121681572A
Sql statement execution control method, system and server
CN121681572B
Long and short-term memory processing system based on large model and vector database
CN121681786A
Long short-term memory processing system based on large model and vector database
CN121681786B