Method for generating steel field TEXT2SQL based on LLM enhanced by RAG
Through the RAG-enhanced LLM method, the SQL statement generation problem caused by the complexity of user needs in the steel industry is solved, and non-professional personnel can efficiently operate databases through natural language to generate accurate SQL queries, and support data analysis and intelligent decision-making of steel enterprises.
Patent Information
- Application Number
- CN202510248346.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Priority Date
- 2025-02-27
- Filing Date
- 2025-03-04
- Publication Date
- 2025-07-11
AI Technical Summary
In the steel industry, the complexity of user requirements and the professionalism of database operations make it difficult for existing large language models to accurately understand and generate SQL statements that meet the requirements, especially when parsing complex intentions and contexts, where existing methods are challenged.
The LLM method based on RAG enhancement is adopted to process user requirements and database schema information vectorized, and combine RAG retrieval technology to generate accurate SQL statements. The method includes establishing a database connection, natural language input, indexing processing, boot word generation, vectorization processing and verification output, and query conversion using large language model and RAG technology.
It realizes that non-professional operators can operate the database easily through natural language, improve the accuracy and efficiency of database queries, supports data analysis and intelligent decision-making of steel enterprises, and has good scalability and adaptability.
Smart Images

Figure CN120296037A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to an industrial intelligent manufacturing technology, and in particular to a method for generating SQL statements using natural language of RAG enhanced LLM in the steel industry. Background Art
[0002] With the rapid development of industrial intelligent manufacturing, especially in heavy industries such as steel, large-scale data accumulation has become a key factor in improving production efficiency and quality. However, steel production involves complex equipment monitoring, production scheduling, and environmental control, which generates a large amount of heterogeneous data, including equipment operation data, sensor data, production line status data, and maintenance records. These data are stored in different databases, forming a huge information base. How to efficiently extract, process, and analyze information from these massive amounts of data to support the optimization and decision-making of the production process has become a core issue that needs to be solved urgently.
[0003] The traditional database operation method uses SQL statements to query data, but this requires users to have certain database knowledge and be familiar with the table structure and field meanings. This is neither convenient nor efficient for operators in many industrial fields (such as maintenance personnel, production schedulers, etc.). Therefore, how to enable these operators to operate the database through natural language without understanding the database structure has become an important requirement in the intelligent processing of industrial data.
[0004] In recent years, large language models (such as GPT) have made breakthrough progress in the field of natural language processing. They can understand and generate human language, especially in generating SQL statements, showing great potential. By training large language models, users can input requirements in natural language, and large language models automatically convert them into accurate SQL query statements, thus solving the problem of traditional SQL operation complexity.
[0005] However, with the advancement of technology and in-depth research on applications, existing methods still face some challenges, especially in understanding user questions. In the specific scenarios of the steel industry, users' expression intentions and contexts are usually complex. Although large language models can generate SQL query statements, there are still challenges in parsing these complex intentions and contexts. Therefore, how to accurately understand user needs and generate SQL statements that meet the requirements is still a difficulty at this stage. Summary of the invention
[0006] Aiming at the problem that large language models have difficulty in obtaining table names and field names when generating accurate SQL statements according to user descriptions, a RAG-enhanced LLM-based TEXT2SQL method for the steel industry is proposed. The focus is on solving the ambiguity problem of user requirements. By combining the semantic information of user requirements and database schema information, it is vectorized and stored in a vector database. Through RAG retrieval, accurate SQL statements can be generated after retrieving relevant information, accurately matching the corresponding table names and field names, thereby improving the effectiveness and accuracy of the model in specific application scenarios in the steel industry.
[0007] The technical solution of the present invention is as follows:
[0008] A RAG-enhanced LLM-based TEXT2SQL method for the steel industry, including establishing a database connection, natural language input, natural language to SQL conversion, index processing, retrieval and enhancement, generating guiding words, vectorization processing, verification and output; specifically including the following steps:
[0009] Step 1: First, a connection to the database needs to be established; this database contains various data in the steel production process. This step involves creating a connection string, which includes the type of database, IP address, port number, username, and password; the connection string is passed to a database connector, and the connector uses it to establish a connection with the database; once the connection is established, an SQL query is executed to obtain the database schema, including the names of all relevant tables and the names and data types of all columns in these tables;
[0010] Step 2: After establishing the database connection, the administrator needs to define user semantic information and database schema; this process usually needs to be defined in advance and customized according to specific industrial production requirements; first, the administrator needs to find the common equipment and parameter entity requirements of users according to the business process of steel production and the database architecture; then, the administrator defines semantic information for these entities to clarify their meanings and mutual relationships; finally, the obtained database schema is stored; after organizing the information of user semantic information and database schema, it is vectorized and stored in a vector database;
[0011] Step 3: The user inputs a natural language query, obtains the natural language query input by the user, and saves it as a string. This string then undergoes a preprocessing step, including removing redundant spaces, checking the integrity and validity of the input, and ensuring that there are no syntax errors or spelling mistakes; the preprocessed query string is passed to the subsequent natural language processing stage for parsing and understanding; during this process, the user's query statement is matched and retrieved with the predefined semantic and schema information stored in the vector database to ensure that the query can obtain an accurate response;
[0012] Step 4: Generate a set of prompts. These prompts are designed to guide the large language model to generate more accurate SQL queries. The prompts include some specialized vocabulary of databases and SQL. When generating SQL queries, these prompts are provided to the large language model as hint words to ensure that the model can correctly understand the query intention and generate SQL queries that conform to the database structure and user requirements.
[0013] Step 5: Use the large language model and RAG technology to convert natural language queries into SQL queries. First, use the large language model to vectorize the language text according to the user input requirements, and then use the RAG method to retrieve the input question in the local vector library. The RAG technology finds the predefined semantic information most similar to the query content by calculating the vector similarity between the user input and the predefined semantic information stored in the vector library. Confirm the types of these entities and their corresponding relationships in the database model by retrieving the semantic information in the vector library. Identify these entities and mark their types through the large language model. Then, considering the entity-based recognition and the relationships between them, vectorize the query context provided by the RAG technology and the database table fields, and calculate the similarity to determine the most relevant tables and fields. In this way, it is possible to accurately match the database tables and fields most relevant to the user query and automatically construct an accurate SQL query statement according to the matching results.
[0014] Step 6: When a new user query is received, retrieve the most similar historical queries and corresponding SQL queries from the vector database. The retrieved historical queries and SQL queries will be used as the input to the large language model to generate a new SQL query statement. Specifically, first convert the new query into a vector and search for the most similar historical query vector and its corresponding SQL query vector in the vector database. After finding the most similar historical query, use these historical queries and their corresponding SQL queries as context input to the large language model. Use these reference historical queries and their SQL statements as context information to help the model understand the background, requirements, and format of the query, so as to generate a more accurate SQL query.
[0015] Step 7: Send the generated SQL query to the SQL validator. The validator checks the syntactic correctness and logical rationality of the SQL query. First, the validator parses the SQL query to check whether the syntactic structure conforms to the SQL standard to ensure that there are no syntax errors in the query. Next, the validator performs a logical check to confirm whether the table names, column names, and conditions in the query are in the schema information of the database and whether the query logic conforms to the data characteristics of the steel production process. Only after the SQL validator passes these checks to ensure that the SQL query statement has neither syntax errors nor logical problems will the query statement be considered valid and output. Finally, the validated SQL query statement will be provided for the user to use. The user will perform a database retrieval based on the generated SQL query statement, obtain the required data, and perform subsequent analysis operations to support the production management and decision-making of steel enterprises.
[0016] Preferably, in step 1, the database includes production line equipment status, product quality parameters, and fault record data.
[0017] Furthermore, in step 2, the information of the database schema includes the administrator's description information of each table in the database. If the administrator encounters a new table for the first time, the new table needs to be described. If the table has been described before, there is no need to describe it again. The description mainly focuses on each field in the table to ensure that each field can be clearly understood so that the model can correctly parse and process it.
[0018] Preferably, in step 4, the guiding words also include the description of the role and the specific syntax and specifications of the SQL statement.
[0019] Preferably, the large language model includes DeepSeek.
[0020] The beneficial effects of the present invention are as follows:
[0021] The present invention vectorizes the semantic information of the user's requirements and the schema information of the database, and combines the RAG method to enhance the results of the SQL statements generated by the large language model, and can effectively generate high-quality SQL query statements. This method simplifies the use of the database by non-professional operators, enabling non-professional database operators to conveniently view the required information through natural language, thereby improving the accuracy and usage efficiency of database queries.
[0022] Through this method, users do not need to deeply understand the specific structure and table information of the database. By simply entering a natural language query, they can automatically match the relevant data tables in the steel production process and generate corresponding SQL statements for database operations. This method changes the complexity of traditional database operations and provides strong support for data analysis, production optimization, and intelligent decision-making of steel enterprises.
[0023] The method of the present invention has good scalability and adaptability. By vectorizing the semantic information of user requirements and database schema information, this method can adapt to various types and scales of databases, whether structured or unstructured data. In addition, with the upload of new data, the definition and description of new requirements can be increased to ensure the continuous high efficiency of SQL statement generation. This enables the present invention to remain efficient and accurate when processing large-scale and dynamically changing data in the steel industry, meeting the high requirements of the steel industry for data processing. Brief Description of the Drawings
[0024] Figure 1 It is a flowchart of the method for generating TEXT2SQL by the RAG-enhanced large language model of the present invention;
[0025] Figure 2 The method proposed by the present invention can deposit any database of an enterprise into any vector library and perform automated SQL statement generation and display with any locally deployed large model. Detailed Embodiment
[0026] The present invention will be described in detail below with reference to the drawings and specific embodiments. This embodiment is implemented on the premise of the technical solution of the present invention, and gives detailed implementation manners and specific operation processes, but the protection scope of the present invention is not limited to the following embodiments.
[0027] The present invention proposes a method for generating TEXT2SQL using a RAG (Retrieval-Augmented Generation)-enhanced large language model, which is applied to the steel field. Through this method, users do not need to understand the detailed information of the database, and only need to provide a vague natural language description to generate the corresponding SQL statement, thereby realizing efficient operation of the database. This method combines the natural language processing ability of the large language model and the retrieval enhancement function of RAG to solve the problem of database operation complexity and improve the operation efficiency. The main steps include establishing a database connection, natural language input, natural language to SQL conversion, index processing, retrieval and enhancement, generation of guiding words, vectorization processing, verification and output. Through this method, enterprise data analysis and intelligent decision-making become more intelligent and efficient.
[0028] The present invention mainly proposes a method for generating SQL statements from natural language using a RAG-enhanced large language model applied to the steel industry. Specifically, it includes the following steps:
[0029] Step 1: First, the present invention needs to establish a connection with the database. This database contains various data in the steel production process, including data such as the status of production line equipment, product quality parameters, and fault records. This step involves the creation of a connection string, which includes the type of database (e.g., MySQL, Oracle, SQL Server, etc.), IP address, port number, username, and password. The connection string is passed to a database connector, and the connector uses it to establish a connection with the database. Once the connection is established, an SQL query is executed to obtain the schema of the database, including the names of all relevant tables and the names and data types of all columns in these tables.
[0030] Step 2: After establishing the database connection, the administrator needs to define user semantic information and the database schema. This process usually needs to be defined in advance and customized according to specific industrial production requirements. First, the administrator needs to find the commonly used requirements of users based on the business process of steel production and the database architecture. For example, in the steel production process, there may be equipment and parameters such as "blast furnace", "converter", "temperature", and "pressure", and these concepts correspond to different tables and fields in the database. Then, the administrator defines semantic information for these entities (i.e., equipment and parameters), clarifying their meanings and relationships. For example, "blast furnace temperature" can be defined as a field that is related to the equipment "blast furnace", and the blast furnace is a table. Blast furnace temperature indicates that a field should be in the blast furnace table. Finally, the obtained database schema is stored. The information of these database schemas includes the administrator's description information of each table in the database, such as the status of equipment and process parameters in the steel production line. If the administrator encounters a new table for the first time (e.g., a newly added digital twin model table), the new table needs to be described; if the table has been described before, there is no need to describe it again. The description mainly focuses on each field in the table to ensure that each field can be clearly understood so that the model can correctly parse and process it. After organizing this information (i.e., user semantic information and database schema information), it is vectorized and stored in a vector database. At this time, the semantic and schema information (i.e., user semantic information and database schema information) has been converted into structured content that can be used for subsequent queries.
[0031] Step 3: The user inputs a natural language query, such as "Check the fault records of the steel production line in the past week.", obtain the natural language query input by the user, and save it as a string, such as "Check the fault records of the steel production line in the past week." This string then undergoes preprocessing steps, including removing extra spaces, checking the integrity and validity of the input, ensuring there are no grammar errors or spelling mistakes. The preprocessed query string is passed to the subsequent natural language processing (NLP) stage for parsing and understanding. During this process, the user's query statement is matched and retrieved with the predefined semantic and pattern information stored in the vector database to ensure that the query can obtain an accurate response. The key to this step is to accurately capture (that is, ensure that the user's query content can be recognized and received, and avoid being unable to understand due to incomplete or incorrect input), ensure the integrity and validity of the query (that is, confirm that the query has no problems in format and grammar and can be effectively parsed), so that the subsequent processing module can correctly parse and execute, thus ensuring the response accuracy of this method.
[0032] Step 4: Generate a set of prompts, which are designed to guide the large language model to generate more accurate SQL queries. The prompts may include some database and SQL specific vocabulary, such as "SELECT", "FROM", "WHERE", etc. It also covers the description of the role (such as SQL statement generator), specific syntax and specifications of SQL statements, etc., such as "You are an advanced SQL statement generator that can convert the user's natural language input into the generation of SQL statements. It is necessary to strictly follow the format and specifications of SQL statements. Common SQL statement template: SELECT [column1], [column2] FROM [table] WHERE [conditions]." When generating SQL queries, these prompts are provided to the large language model as hint words to ensure that the model can correctly understand the query intention and generate SQL queries that conform to the database structure and user needs. This step significantly improves the accuracy and efficiency of SQL query generation by providing clear context and structure information.
[0033] Step 5: This method uses large language models (such as DeepSeek) and RAG technology to convert natural language queries into SQL queries. First, the large language model is used to vectorize the language text according to the requirements input by the user, and then the RAG method is used to retrieve the input question in the local vector library. The RAG technology finds the predefined semantic information most similar to the query content by calculating the vector similarity between the user input and the predefined semantic information stored in the vector library. For example, for the entities "steel production line", "fault record", and "past week" that appear in the query, the semantic information in the vector library is retrieved to confirm the types of these entities and their corresponding relationships in the database model. These entities are identified by the large language model and their types are marked. For example, "steel production line" may be marked as "equipment", "fault record" is marked as "data type", and "past week" may be marked as "time". Then, considering the entity-based recognition and their relationships, the query context provided by the RAG technology and the database table fields are vectorized, and the similarity is calculated to determine the most relevant tables and fields. In this way, the database tables and fields most relevant to the user query can be accurately matched, and an accurate SQL query statement can be automatically constructed according to the matching results. For example, for the query "View the fault records of the steel production line in the past week", the following SQL query will be generated:
[0034] SELECT*FROM digital_twin_fault_records
[0035] WHERE production_line='steel production line'
[0036] AND record_date>=DATE_SUB(CURDATE(),INTERVAL 7DAY);
[0037] The key to this step is to accurately identify the entities and their relationships in the natural language query, retrieve and match relevant information through RAG technology, and finally convert this information into the corresponding SQL query so that the query can be correctly executed and the required information can be returned.
[0038] Step 6: When a new user query is received, the most similar historical queries and corresponding SQL queries will be retrieved from the vector database. The retrieved historical queries and SQL queries will be used as inputs to the large language model to generate a new SQL query statement. Specifically, first, the new query is converted into a vector, and the most similar historical query vectors and their corresponding SQL query vectors are searched in the vector database. After finding the most similar historical queries, these historical queries and their corresponding SQL queries are used as context inputs to the large language model. By using these reference historical queries and their SQL statements as context information, the model can understand the background, requirements, and format of the query, thereby generating a more accurate SQL query. This method not only utilizes the user's historical query data to improve the accuracy of generating new SQL queries but also speeds up the query processing speed to ensure that users obtain accurate query results.
[0039] Step 7: The generated SQL query is sent to the SQL validator, which checks the syntactic correctness and logical rationality of the SQL query, especially the data structure. The validator first parses the SQL query to check whether the syntactic structure conforms to the SQL standard to ensure that there are no syntax errors in the query. Next, the validator performs a logical check to confirm whether the table names, column names, and conditions in the query are in the database schema information and whether the query logic conforms to the data characteristics of the steel production process. Only after the SQL validator passes these checks and ensures that the SQL query statement has neither syntax errors nor logical problems will the query statement be considered valid and output. Finally, the verified SQL query statement will be provided for the user to use. The user will perform database retrieval according to the generated SQL query statement, obtain the required data, and perform subsequent analysis operations to support the production management and decision-making of steel enterprises.
[0040] Through the above steps, the present invention realizes the efficient conversion of users' natural language queries into SQL queries in the steel industry by using a RAG-enhanced large language model, greatly improving the efficiency of data query and analysis, and providing strong support for the intelligent and digital transformation of steel production.
[0041] The above-described embodiments only represent one implementation manner of the present invention, and the description is relatively specific and detailed, but it should not be construed as a limitation on the scope of the invention patent. It should be noted that for those of ordinary skill in the art, without departing from the concept of the present invention, several modifications and improvements can be made, and these all belong to the protection scope of the present invention. Therefore, the protection scope of the present invention patent shall be subject to the appended claims.
Claims
1. A TEXT2SQL method for the steel field based on RAG-enhanced LLM, characterized in that, Including establishing a database connection, natural language input, natural language to SQL conversion, index processing, retrieval and enhancement, generating guiding words, vectorization processing, verification and output; specifically including the following steps: Step 1: First, a connection to the database needs to be established; this database contains various data in the steel production process. This step involves creating a connection string, which includes the type of database, IP address, port number, username, and password; The connection string is passed to a database connector, and the connector uses it to establish a connection with the database; once the connection is established, an SQL query is executed to obtain the database schema, including the names of all relevant tables and the names and data types of all columns in these tables; Step 2: After establishing the database connection, the administrator needs to define user semantic information and the database schema; this process usually needs to be defined in advance and customized according to specific industrial production requirements; first, the administrator needs to find the entity requirements of commonly used equipment and parameters by the user according to the business process of steel production and the database architecture; then, the administrator defines semantic information for these entities to clarify their meanings and mutual relationships; finally, the obtained database schema is stored; after organizing the information of user semantic information and the database schema, it is vectorized and stored in the vector database; Step 3: The user inputs a natural language query, obtains the natural language query entered by the user, and saves it as a string. This string then undergoes preprocessing steps, including removing extra spaces, checking the integrity and validity of the input, and ensuring there are no grammar errors or spelling mistakes; The preprocessed query string is passed to the subsequent natural language processing stage for parsing and understanding; In this process, the user's query statement is matched and retrieved with the predefined semantic and schema information stored in the vector database to ensure that the query can obtain an accurate response; Step 4: Generate a set of guiding words (prompts), which are designed to guide the large language model to generate a more accurate SQL query; the guiding words include some special vocabulary of the database and SQL. When generating an SQL query, these guiding words are provided to the large language model as prompt words to ensure that the model can correctly understand the query intention and generate an SQL query that conforms to the database structure and user requirements; Step 5: Use large language models and RAG technology to convert natural language queries into SQL queries; First, use a large language model to vectorize the language text according to the user's input requirements, and then use the RAG method to retrieve the input question in the local vector library; The RAG technology finds the predefined semantic information most similar to the query content by calculating the vector similarity between the user input and the predefined semantic information stored in the vector library; Confirm the types of these entities and their corresponding relationships in the database model by retrieving the semantic information in the vector library; Identify these entities and mark their types through a large language model; Then, considering the entity-based recognition and the relationships between them, vectorize the query context provided by the RAG technology and the database table fields, and calculate the similarity to determine the most relevant tables and fields. In this way, it is possible to accurately match the database tables and fields most relevant to the user query, and automatically construct an accurate SQL query statement based on the matching results; Step 6: When a new user query is received, retrieve the most similar historical queries and corresponding SQL queries from the vector database; The retrieved historical queries and SQL queries will be used as input to the large language model to generate a new SQL query statement; Specifically, first convert the new query into a vector, and search for the most similar historical query vector and its corresponding SQL query vector in the vector database; After finding the most similar historical query, use these historical queries and their corresponding SQL queries as context input to the large language model, and use these reference historical queries and their SQL statements as context information to help the model understand the background, requirements, and format of the query, so as to generate a more accurate SQL query; Step 7: Send the generated SQL query to the SQL validator, and the validator checks the syntax correctness and logical rationality of the SQL query; The validator first parses the SQL query to check whether the syntax structure conforms to the SQL standard to ensure that the query has no syntax errors; Next, the validator performs a logical check to confirm whether the table names, column names, and conditions in the query are in the database schema information, and whether the query logic conforms to the data characteristics of the steel production process; Only after the SQL validator passes these checks to ensure that the SQL query statement has neither syntax errors nor logical problems, the query statement will be considered valid and output; Finally, the verified SQL query statement will be provided for the user to use; The user will perform database retrieval according to the generated SQL query statement, obtain the required data and perform subsequent analysis operations to support the production management and decision-making of steel enterprises.
2. The RAG-enhanced LLM-based TEXT2SQL method for the steel industry according to claim 1, characterized in that, In Step 1, the database includes production line equipment status, product quality parameters, and fault record data.
3. The TEXT2SQL method for the steel field based on RAG-enhanced LLM according to claim 1, characterized in that, In step 2, the information of the database schema includes the administrator's description information of each table in the database. If the administrator encounters a new table for the first time, the new table needs to be described; if the table has been described before, there is no need to describe it again; the description mainly focuses on each field in the table to ensure that each field can be clearly understood so that the model can correctly parse and process it.
4. The TEXT2SQL method for the steel field based on RAG-enhanced LLM according to claim 1, wherein In step 4, the guiding words also include the description of the role, specific syntax and specifications of SQL statements.
5. The TEXT2SQL method for the steel field generated by RAG-enhanced LLM according to claim 1, characterized in that The large language model includes DeepSeek.
Citation Information
Cited By
Intelligent SQL (Structured Query Language) generation system based on multistage intention recognition and generation method thereof
CN120910240A
Natural language query analysis and database field matching method based on large model
CN121009105A
Query statement generation method and electronic equipment
CN121029953A
A query statement generation method and an electronic device
CN121029953B
SQL statement generation method and device and electronic equipment
CN121070970A