SQL statement generation method and device based on large model

Through the large language model, the semantic analysis of user's natural language query and database schema information is generated, which solves the problem of low accuracy of the existing Text2SQL technology in complex query scenarios and improves the diversity of queries Processing capability.

CN118981475AActive Publication Date: 2024-11-19PINGCAP XINGCHEN (BEIJING) TECH CO LTD

Patent Information

Application Number
CN202411464018.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-18
Publication Date
2024-11-19
Estimated Expiration
2044-10-18

AI Technical Summary

Technical Problem

The existing Text2SQL technology has low accuracy when handling complex and diverse natural language queries, making it difficult to understand database semantics and process large-scale database schema information.

Method used

The SQL statement generation method based on a large language model is adopted to perform semantic analysis and analysis of user initial query statements and target database schema information, and structured SQL query statements are generated, and the database key entity information is filtered out based on semantic analysis results.

Benefits of technology

It significantly improves the accuracy and generalization ability of SQL generation, enhances the processing ability of diversified query scenarios, reduces manual intervention, and improves the automation and user experience of the system.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118981475A_ABST
    Figure CN118981475A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides an SQL (Structured Query Language) statement generation method and device based on a large model, and relates to the field of distributed technologies, for example, the field of distributed databases. The method comprises the steps of obtaining an initial query statement of a user and initial database mode information of a target database; and inputting the user initial query statement and the initial database mode information of the target database into a large language model to obtain a structured query language (SQL) query statement. According to the embodiment of the invention, the SQL generation accuracy can be improved based on the large language model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of distributed technology, and more specifically, to a method and device for generating SQL statements based on a large model. Background Art

[0002] Text-to-SQL (abbreviated as T2S, or Text2SQL) is a technology that converts natural language queries into SQL queries. Its main application scenario is in distributed database queries, where users can query in natural language without having to master the SQL language.

[0003] Currently, Text2SQL mainly relies on methods based on templates, rules, and traditional machine learning. However, these methods have many limitations when dealing with complex and diverse natural language queries, resulting in low accuracy in converting natural language queries to SQL queries. Summary of the invention

[0004] The embodiment of the present application provides a method for generating SQL statements based on a large model to solve the problem of low accuracy in converting natural language queries into SQL queries in the prior art.

[0005] Correspondingly, an embodiment of the present application also provides a SQL statement generation device based on a large model, an electronic device and a storage medium to ensure the implementation and application of the above method.

[0006] In order to solve the above problems, the embodiment of the present application discloses a method for generating SQL statements based on a large model, the method comprising: Obtaining the user's initial query statement and the initial database schema information of the target database; Inputting the user's initial query statement and the initial database schema information of the target database into a large language model to obtain a structured SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

[0007] The embodiment of the present application also discloses a SQL statement generation device based on a large model, the device comprising: An acquisition module, used to acquire the user's initial query statement and the initial database schema information of the target database; A processing module, used for inputting the user's initial query statement and the initial database mode information of the target database into a large language model to obtain an SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

[0008] An embodiment of the present application also discloses an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, one or more of the methods described in the embodiments of the present application are implemented.

[0009] The embodiments of the present application further disclose a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, one or more methods described in the embodiments of the present application are implemented.

[0010] The embodiments of the present application also disclose a computer program product, including a computer program, which, when executed by a processor, implements one or more of the methods described in the embodiments of the present application.

[0011] The beneficial effects of the technical solution provided by the embodiment of the present application are: In the embodiment of the present application, the SQL statement generation method based on the large model realizes efficient parsing and generation of natural language queries by combining the user's initial query statement and the database schema information of the target database. The large language model can not only deeply understand the semantic structure of the target database, but also perform accurate semantic analysis of user queries, thereby effectively extracting key database entity information related to the query. Compared with the existing Text2SQL method, the embodiment of the present application significantly improves the semantic understanding of database schema information, enhances the generalization ability to cope with diversified query scenarios, and greatly improves the final accuracy of SQL generation.

[0012] Additional aspects and advantages of the embodiments of the present application will be partially given in the description below, which will become apparent from the description below, or will be learned through the practice of the present application. BRIEF DESCRIPTION OF THE DRAWINGS

[0013] The above and / or additional aspects and advantages of the present application will become apparent and easily understood from the following description of the embodiments in conjunction with the accompanying drawings, in which: Figure 1 A flowchart of a method for generating SQL statements based on a large model provided in an embodiment of the present application; Figure 2 A schematic diagram of a first example provided for an embodiment of the present application; Figure 3 A flowchart of a second example provided for an embodiment of the present application; Figure 4 A flowchart of a third example provided for an embodiment of the present application; Figure 5 A flowchart of a fourth example provided for an embodiment of the present application; Figure 6 A flowchart of a fifth example provided in an embodiment of the present application; Figure 7 A schematic diagram of the structure of a SQL statement generation device based on a large model provided in an embodiment of the present application; Figure 8 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0014] The embodiments of the present application are described below in conjunction with the drawings in the present application. It should be understood that the implementation methods described below in conjunction with the drawings are exemplary descriptions for explaining the technical solutions of the embodiments of the present application and do not constitute a limitation on the technical solutions of the embodiments of the present application.

[0015] Those skilled in the art will appreciate that, unless specifically stated, the singular forms "one", "an", "said" and "the" used herein may also include plural forms. It should be further understood that the terms "including" and "comprising" used in the embodiments of the present application refer to that the corresponding features can be implemented as the presented features, information, data, steps, operations, elements and / or components, but do not exclude the implementation as other features, information, data, steps, operations, elements, components and / or combinations thereof supported by the technical field. It should be understood that when we say that an element is "connected" or "coupled" to another element, the one element may be directly connected or coupled to the other element, or it may refer to that the one element and the other element establish a connection relationship through an intermediate element. In addition, the "connection" or "coupling" used here may include wireless connection or wireless coupling. The term "multiple" refers to two or more than two. In view of this, "multiple" may also be understood as "at least two" in the embodiments of the present application. The term "and / or" describes the association relationship of associated objects, indicating that three relationships can exist. For example, A and / or B can represent three situations: A exists alone, A and B exist at the same time, and B exists alone. In addition, the character " / ", unless otherwise specified, generally indicates that the associated objects before and after are in an "or" relationship.

[0016] In order to make the objectives, technical solutions and advantages of the present application clearer, the implementation methods of the present application will be further described in detail below with reference to the accompanying drawings.

[0017] Currently, Text2SQL technology mainly relies on template-based, rule-based and traditional machine learning methods. However, these methods have many limitations when dealing with complex and diverse natural language queries. Template-based methods often fail to cover all possible forms of language expression, resulting in insufficient generalization capabilities; rule-based methods require manual writing of a large number of rules, which is labor-intensive and difficult to maintain; traditional machine learning methods have limited performance in understanding complex semantics and long-distance dependencies, and are difficult to accurately parse the intent of natural language.

[0018] Therefore, when dealing with complex and changing query requirements, the accuracy of SQL generation by the existing Text2SQL method is low and the effect is not ideal.

[0019] The large model-based SQL statement generation method and device provided in this application are intended to solve the above technical problems in the prior art.

[0020] The technical solution of the present application and how the technical solution of the present application solves the above-mentioned technical problems are described in detail below with specific embodiments. The following specific embodiments can be combined with each other, and the same or similar concepts or processes may not be repeated in some embodiments. The embodiments of the present application will be described below in conjunction with the accompanying drawings.

[0021] The solution provided in the embodiments of the present application can be executed by any electronic device, such as a terminal device or a server, wherein the server can be an independent physical server, a server cluster or a distributed system composed of multiple physical servers, or a cloud server providing cloud computing services. The terminal can be a smart phone, a tablet computer, a laptop computer, a desktop computer, a smart speaker, a smart watch, etc., but is not limited to this. The terminal and the server can be directly or indirectly connected via wired or wireless communication, and the terminal and the server can include a database.

[0022] With regard to the technical problems existing in the prior art, the SQL statement generation method and device based on a large model provided in this application are intended to solve at least one of the technical problems in the prior art.

[0023] The present application embodiment provides a possible implementation method, such as Figure 1 As shown, a flowchart of a method for generating SQL statements based on a large model is provided. The scheme can be executed by any electronic device. Optionally, the method can be applied to a server or a terminal device. For the convenience of description, the method provided in the embodiment of the present application is described below with the server as the execution subject, wherein the execution subject can be a processing module in the database installed on the server.

[0024] The embodiment of the present application can be applied to the field of database technology, by obtaining the user's initial query statement and the initial database schema of the target database; inputting the user's initial query statement and the initial database schema information of the target database into a large language model to obtain a structured SQL query statement; wherein the large language model is used to perform the following operations: semantically parse the initial database schema information of the target database to obtain the semantic parsing result of the target database; according to the semantic parsing result of the target database, semantically analyze the user's initial query statement to obtain the user query semantics; according to the user query semantics and the semantic parsing result of the target database, filter and process the initial database schema information to obtain the database key entity information corresponding to the user query; according to the database key entity information, generate the SQL query statement. In this way, accurate parsing of the user's natural language query and automatic generation of SQL query statements can be achieved. For example, a large language model can be used to understand the semantics of the database schema information and the user's query statement to generate an SQL query statement that is highly matched with the user's needs. By combining the semantic analysis of the target database with the user query semantics, we can effectively screen out the key database entity information related to the query, ensure the accuracy and efficiency of SQL query statement generation, improve the accuracy and generalization ability of query analysis, reduce manual intervention, and enhance system automation and user experience.

[0025] Specifically, Figure 1 As shown, the SQL statement generation method based on the large model in the embodiment of the present application may include the following steps: S101, obtaining a user's initial query statement and initial database schema information of a target database.

[0026] In the field of database technology, database schema information contains the definitions of various objects and their relationships in the database. In an embodiment of the present application, the initial database schema information of the database is a structural description of the target database, which provides basic organizational information of the database. For example, the initial database schema information usually includes: database name (referring to the name of the target database), table name (the name of the table in the database), column name (the name of each column in the table), column type (the data type of each column in the table, such as integer, string, date, etc.) and column sample data (some sample values ​​in the column to help the model understand the meaning of the data). This information is an important basis for understanding the database structure and generating correct SQL queries.

[0027] Among them, the user's initial query statement refers to the query demand entered by the user using natural language. This query statement may contain the user's retrieval, screening or calculation requirements for certain data in the database. In actual business scenarios, due to differences in individual language expression habits and methods, the user's initial query statement may have a variety of different expressions. For example, a user may use different vocabulary or word order to express the same query intent, such as "find sales data for 2023" and "what is the sales volume in 2023" are different in form, but they express the same query demand.

[0028] In an embodiment of the present application, by obtaining the user's initial query statement and the initial database schema information of the target database, the system can obtain the user's query requirements and the related database structure information, providing a basis for semantic parsing and SQL generation in subsequent steps.

[0029] S102, inputting the user's initial query statement and the initial database schema information of the target database into a large language model to obtain a structured SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain the database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

[0030] Since the existing Text2SQL technology has certain limitations when processing complex and diverse natural language queries, the embodiment of the present application turns the solution of Text2SQL to a method based on a large language model (LLM) and a deep learning model architecture (Transformer), and uses the advantages of large language models in semantic analysis to improve the performance and accuracy of Text2SQL. Among them, the large language model is a natural language processing model based on deep learning, which can understand and generate complex texts (hereinafter referred to as the large model). The model has been trained with a large amount of data, has strong language understanding and semantic analysis capabilities, and can handle complex natural language queries. In the embodiment of the present application, the task of the large model is to combine the user's natural language query with the structure of the target database to generate a corresponding SQL query statement. For example, the user's initial query statement and the initial database model information of the target database are input into the large model to generate a structured SQL query statement.

[0031] Among them, the key to Text2SQL is mapping construction, that is, identifying the necessary entities required to convert user queries into SQL queries from given database schema information, such as database table names, column names, conditions, etc. However, in the embodiments of the present application, the applicant found in actual business scenarios that the Text2SQL solution based on the large model usually has the following defects, which make it difficult to implement or perform poorly.

[0032] Case 1: The database schema semantic information is weak.

[0033] In actual business scenarios, the database schema information obtained may only have the corresponding table name, column name, column type, etc. However, this basic information often cannot directly reflect the specific meaning of the table and column, the relationship between tables, and the main functions and uses of the database. The lack of this semantic information has greatly restricted the accuracy of the mapping construction of Text2SQL and the final generated SQL query statement. For example, if the specific meanings of the tables and columns in the database are not clear, the generated SQL query may deviate from the actual query requirements. For example, if the user's initial query statement is "list all customers who have placed orders in the past week", when it is not clear whether the "date" column in the database represents the order date or the delivery date, an incorrect SQL query statement may be generated. Similarly, if the relationship between tables is not clear, the generated SQL query statement may not correctly connect the tables, and thus fail to obtain the correct query results. For example, if the user's initial query statement is "list all customers who have purchased product A", when it is not clear how to connect the "order table" and "customer table" in the database, the correct SQL query statement cannot be generated.

[0034] In some embodiments, in order to solve the technical problem mentioned in the above situation 1, the embodiment of the present application uses a large model to perform semantic analysis on the initial database schema information of the target database to obtain the semantic analysis result of the target database. Specifically, the large model first performs semantic analysis on the input initial database schema information of the target database. Semantic analysis refers to the large model's understanding of the meaning behind the database structure, such as the relationship between tables in the database, the meaning of the table, the meaning of the column, etc. The results of the semantic analysis can help the large model understand the various entities in the database. For example, when parsing the database schema information, the large model can deduce that the "order table" contains information such as "order ID", "customer ID", "order amount", and understand the specific meaning and purpose of these fields.

[0035] Case 2: The semantics of the user's initial query statement is uncertain and diverse.

[0036] In actual business scenarios, due to the differences in language expression habits and methods of different users, similar query requirements may be expressed in multiple different ways. In addition, users may not be very clear about their real needs, or they may not be able to accurately express their needs, which may cause the query statements entered by users to be vague or unclear, or even completely irrelevant to the current database content. In this case, if SQL queries are blindly generated based on large models, the SQL queries may look reasonable or real, but are actually wrong or unfounded. This phenomenon is called "hallucination". The above factors will bring additional challenges to the processing of Text2SQL. If the user's query intent cannot be clearly understood, accurate mapping construction cannot be completed, which affects the accuracy and efficiency of Text2SQL.

[0037] In some embodiments, in order to solve the technical problem mentioned in the above situation 2, the embodiments of the present application use the semantic analysis results of the big model and the database to perform semantic analysis on the user's initial query statement to obtain the user's query semantics. Specifically, the semantic analysis results of the database are used to clarify the user's initial query statement, optimize the understanding of the user's query intention, and obtain the user's query semantics that can clearly express the query results expected by the user.

[0038] Case 3: Unable to handle databases with large amounts of data.

[0039] In actual business scenarios, database schema information can be very large (for example, a database contains many tables, or some tables contain many columns), and existing large models usually have context window limitations. This means that if the database schema information of the database exceeds the size of the context window of the large model, the large model cannot process the data at one time, and thus cannot identify the database key entity information required to convert user queries into SQL queries from these large amounts of database schema information. This will not only affect the final accuracy of Text2SQL, but also limit the scope of application of the Text2SQL technical solution. Therefore, how to handle super-large databases while ensuring model performance is also an important challenge facing Text2SQL.

[0040] In some embodiments, the embodiments of the present application solve the technical problems mentioned in the above situation three, and use a large model to accurately identify the database key entity information (including related tables and columns, etc.) required to convert user queries to SQL queries from the initial database schema information of the database according to the semantics of the user query and the semantic parsing results of the target database, and are suitable for processing ultra-large databases. For example, for a user query "find the total sales in 2023", the database key entity information may include the "sales date" and "sales amount" columns in the "sales table", which are the key data required for the query.

[0041] In some embodiments, after obtaining the database key entity information corresponding to the user query, the big model will generate a corresponding SQL query statement. This SQL query statement is a structured expression of the user's natural language query and can be directly executed by the database. For example, the user query "find the total sales in 2023" will be converted into the following SQL statement: SELECT SUM(sales amount) FROM sales table WHERE sales date BETWEEN '2023-01-01' AND '2023-12-31'. In some embodiments, the technical solution of step S102 is applicable to a variety of scenarios, especially when the user is not familiar with the SQL language but needs to query a complex database. For example, in a business analysis system, the user may want to use natural language to input a query, and the system needs to automatically generate SQL to retrieve data in the database. It is also applicable to data analysis platforms, intelligent customer service systems, etc.

[0042] As an example, combining Figure 2 As shown, Figure 2 The process shown in shows the basic steps and working principle of the SQL statement generation method based on the large model.

[0043] S201, target database connection.

[0044] In the embodiment of the present application, the system can obtain the database schema information of the database by connecting to the target database. The database schema information includes the structural information of the database table, column, field type, etc., which is the basis for the subsequent generation of SQL queries.

[0045] S202, database schema query and modeling.

[0046] The embodiment of the present application queries the database schema information of the target database and performs database modeling based on the query results. This process involves semantic understanding and parsing of the database structure to provide input information for the large model.

[0047] In some embodiments, the results of the database schema query will be stored in the storage service for use in subsequent steps.

[0048] S203, user query input.

[0049] In the embodiment of the present application, the user puts forward a query demand in natural language and inputs it into the system. This query is usually in natural language form, such as "find sales data for 2023".

[0050] S204, issue clarification.

[0051] In the embodiment of the present application, after the system receives the user's natural language query, it enters the question clarification stage. In this stage, the large language model will parse the user input, understand the query intent, and perform semantic analysis.

[0052] S205, mapping construction.

[0053] In the embodiment of the present application, based on understanding the semantics of user queries, the big model will match and map the user query intent with the database schema. At this time, the model needs to determine the key database entity information involved in the query, such as table and column names.

[0054] In some embodiments, the mapping results may also be stored in a storage service in the system for use in subsequent SQL generation processes.

[0055] S206, SQL generation and optimization.

[0056] In the embodiment of the present application, based on the result of the mapping construction, the big model generates the corresponding SQL query statement. During the generation process, the big model combines the database mode information and the user query semantics to generate an optimized SQL query statement to ensure efficient query execution.

[0057] In some embodiments, during the process of generating SQL query statements, the system may also optimize the SQL query statements to further improve query efficiency.

[0058] In the embodiment of the present application, the SQL statement generation method based on the large model realizes efficient parsing and generation of natural language queries by combining the user's initial query statement and the database schema information of the target database. The large language model can not only deeply understand the semantic structure of the target database, but also perform accurate semantic analysis of user queries, thereby effectively extracting key database entity information related to the query. Compared with the existing Text2SQL method, the embodiment of the present application significantly improves the semantic understanding of database schema information, enhances the generalization ability to cope with diversified query scenarios, and greatly improves the final accuracy of SQL generation.

[0059] In one embodiment of the present application, the semantic parsing of the initial database schema information of the target database to obtain the semantic parsing result of the target database includes: According to the initial database schema information of the target database, obtaining the column meaning and table meaning of each table in the target database; Determine, according to the table meaning of each table and the column meaning of each column in each table, the reference relationship between each table and other tables; Determine the core tables in the target database according to the number of reference relationships between each table and other tables, and cluster each of the core tables to obtain database core entity information; The database characteristic information of the target database is determined according to the database core entity information.

[0060] In the embodiment of the present application, the initial database schema information includes but is not limited to: database name (unique identifier of the database), table name (name of each table in the database, used to divide different business modules), column name (name of each field in the table), column type (indicates the type of field data, such as integer, string, date, etc.), column sample data (some sample data of each column, used to further infer the actual business meaning of the column). This information provides the basis of data structure for subsequent semantic analysis and identification of relationships between tables.

[0061] In an embodiment of the present application, according to the initial database schema information of the target database, the column meaning and table meaning of each table in the target database are obtained from the database schema information. Specifically, for inferring the business meaning of a column, the role of the column in the actual business can be inferred based on the column name, column type, and column sample data. For example, the "order_id" column can be inferred as the "order number", and the "created_at" column can be inferred as the "creation time".

[0062] Furthermore, the business meaning of the entire table can be further inferred based on the business meaning of each column. For example, a table containing fields such as "order_id", "order_date", and "customer_id" can be inferred to be an "order information table".

[0063] Furthermore, the meaning of the columns can be optimized inversely through the business meaning of the table. For example, if a table is inferred to be an "order information table", all columns in the table that are not related to orders may need to be readjusted or removed in terms of business meaning.

[0064] In the embodiment of the present application, the business meaning of columns and tables is optimized through multiple iterations to ensure that the business meaning of each field and table is clear and accurate, and to ensure that the business semantics of columns and tables can highly match the actual business scenarios, making subsequent reference relationship inference more reliable.

[0065] In the embodiment of the present application, based on the inferred table meaning, the system then needs to traverse each table in the database, and infer the possible reference relationship between each table and other tables based on the table meaning of each table and the column meaning of each column in each table. Specifically, the system constructs two agents, namely the first agent (agent-a) and the second agent (agent-b). Agent-a is responsible for identifying and extracting the reference relationship between tables, and agent-b is responsible for evaluating the accuracy of these reference relationships and providing feedback. For example, there is usually a foreign key reference between the order table and the customer table, indicating the association between the order and the customer.

[0066] In the embodiment of the present application, agent-b provides feedback on the extraction results of agent-a. If there are errors or omissions, agent-a will re-extract the reference relationship based on the feedback until the set number of iterations or the trigger end condition is reached. Finally, the system will filter out possible duplicate reference relationships to ensure the uniqueness and accuracy of each reference relationship. Through this step, the embodiment of the present application can not only identify the reference relationship between tables, but also lay the foundation for the subsequent extraction of core tables and database core entity information.

[0067] In the embodiment of the present application, the core tables in the target database are determined based on the reference relationships between each table and other tables, and the core tables are clustered to obtain the core entity information of the database. Specifically, the system will sort the importance of each table according to the number of reference relationships, and select the first N tables with the most reference relationships as core tables. For example, the first 20 tables with the most reference relationships are selected as core tables.

[0068] Furthermore, the third agent (agent-c) and the fourth agent (agent-d) are constructed, and these core tables are clustered by agent-c to extract the core entity information of the database. For example, the core entity information of the database can be entity information such as "customer information", "order information" or "product information", which contains specific table and core field descriptions.

[0069] In some embodiments, agent-d will evaluate the extraction effect of these database core entity information and provide feedback to agent-c to ensure that the extracted database core entity information is accurate in business semantics. Through this process, the embodiment of the present application can automatically identify key business modules or entities in the database, greatly improving the ability to understand and optimize the database.

[0070] In an embodiment of the present application, the database characteristic information of the entire database is inferred based on the extracted database core entity information. Among them, the database characteristic information includes but is not limited to the function, purpose, field and core field of the database. Among them, the functional purpose of the database, for example, the database may be used for order management, customer information management or product inventory management; the field, for example, through the analysis of tables and entities, the application field of the database can be inferred, such as e-commerce, finance, medical care, etc.; the core field, for example, is the field in the database that plays a decisive role in key business, such as order ID, customer ID, product name, etc.

[0071] It can be seen that the embodiment of the present application can accurately obtain the business meaning of each table and each column by performing multi-level and multi-dimensional analysis and inference on the initial database model information of the target database, thereby improving the system's ability to understand complex database structures. By introducing intelligent agents, the embodiment of the present application can automatically identify the reference relationship between tables, and continuously optimize the recognition accuracy of the reference relationship through an iterative feedback mechanism, avoiding the cumbersome operations of manual definition. In addition, the embodiment of the present application can automatically identify the core tables in the database, extract key entities, and improve the accuracy of entity extraction by optimizing the feedback mechanism, making the main business modules of the database more clearly visible.

[0072] In some embodiments, acquiring the column meaning and table meaning of each table in the target database according to the initial database schema information of the target database includes: Inferring and storing the business meaning of each column in each of the tables; Based on the business meanings of the columns, optimizing and storing the business meanings of each table; Based on the business meaning of each table, optimizing and storing the business meaning of each column in each table; The business meaning of each column and the business meaning of each table are optimized iteratively until a set number of iterations is met or an end condition is triggered.

[0073] In some embodiments, determining the reference relationship between each table and other tables according to the table meaning of each table includes: Constructing a first agent and a second agent; wherein the first agent is used to extract the reference relationship between each table and other tables according to the table meaning of each table, and the second agent is used to evaluate the reference relationship and feed back the evaluation result to the first agent; The first agent re-extracts according to the evaluation result, and iteratively optimizes the reference relationship between each table and other tables until a set number of iterations is met or an end condition is triggered.

[0074] In some embodiments, determining the core tables in the target database according to the reference relationship between each table and other tables, and clustering each of the core tables to obtain database core entity information includes: Sort by the number of reference relationships of each table, and select the first N tables as the core tables; Constructing a third agent and a fourth agent; wherein the third agent is used to cluster each of the core tables to obtain database core entity information, and the fourth agent is used to evaluate and feedback the results of obtaining the database core entity information; The third agent re-extracts according to the core entity information acquisition result of the database, and iteratively optimizes the core entity information acquisition result of the database until the set number of iterations is met or the end condition is triggered.

[0075] As an example, combining Figure 3 As shown, Figure 3 The process shown in demonstrates the basic steps and working principle of semantically parsing the initial database schema information of the target database based on the big model.

[0076] S301, connecting to a target database to obtain initial database schema information.

[0077] In the embodiment of the present application, the target database is first connected to obtain the initial database schema information of the database. This information includes basic information such as database name, table name, column name, column type, and sample data of some columns. These initial data provide data structure and type reference for subsequent column and table meaning inference.

[0078] S302, infer the meaning of the column.

[0079] In the embodiment of the present application, based on the database schema information obtained in S301, the business meaning of each column of each table is inferred in this step. By analyzing the column name, column type and sample data, the system infers the specific business meaning of each column. For example, the column name "order_id" may be inferred to represent the order number. The system can also optimize the business meaning of the column through multiple iterations to ensure the accuracy of the inference results.

[0080] S303: Infer the meaning of the table.

[0081] In the embodiment of the present application, the overall business meaning of the table is further inferred based on the business meaning of each column in S302. In this process, the meaning of the table is usually determined by the columns it contains. For example, if the table contains fields such as "order_id" and "order_date", it can be inferred that the table is related to the order business.

[0082] S304: Infer table relationship.

[0083] In the embodiment of the present application, in this step, the reference relationship between the tables is inferred based on the field relationship between the tables. The current table is usually used as a reference table, and other related tables are used as referenced tables. Through the collaboration of the large model and the intelligent agent, the system identifies the foreign keys, dependencies, etc. between the tables. In this process, the system introduces a result evaluation intelligent agent to feedback and optimize the inference results, and continuously improve the accuracy of reference relationship identification.

[0084] S305: Extract core entities.

[0085] In the embodiment of the present application, the reference relationship of all tables is analyzed, the core tables of the database are identified, and these core tables are clustered and entities are extracted. For example, the core entities may include business entities such as "customer" and "order", and the accuracy of extraction and business matching are continuously improved through the feedback optimization mechanism.

[0086] S306: Infer the function and purpose of the database.

[0087] In the embodiment of the present application, the main functions, uses and fields of the entire database are inferred based on the core entities and their table relationships. According to the business meanings and entities identified in the previous steps, it is determined whether the database is used for a specific business scenario, such as an order management system, a customer relationship management system, etc. This inference provides an overall view of the business interpretation of the database.

[0088] In some embodiments, the semantic analysis of the user's initial query statement is performed according to the semantic parsing result of the target database to obtain the user query semantics, including: Determining context information required for semantic analysis of the user's initial query statement according to the semantic parsing result of the target database; The user query semantics is determined according to the context information.

[0089] In an embodiment of the present application, before semantic analysis is performed on the user's initial query statement, it is necessary to determine relevant context information that can help understand the user's initial query statement based on the semantic analysis results of the target database. Among them, context information refers to key data extracted from the semantic analysis results of the database that can help the system understand the user's query. This information may include core entities in the database, the business meaning of fields, the relationship between tables, and the functions and uses of the database.

[0090] In some embodiments, when processing a very large database, the system needs to intelligently select which context information is the most important according to the context window limit of the large model to ensure efficient use of computing resources. Specifically, when the semantic parsing result does not exceed the Context Window limit, all semantic parsing results of the target database are used as context. This can provide a complete reference for semantic analysis to ensure maximum accuracy. When the semantic parsing result exceeds the Context Window limit, the core entity and database feature information will be extracted as context. This is because core entities often reflect the key tables and data relationships in the database and can provide sufficient context information for most queries. If the sum of the core entity information and the database feature information exceeds the Context Window limit, the context is further simplified, and only key information such as the database function purpose and the description of the core fields is used. This information provides a global perspective to help the system understand the background and purpose of the user's query.

[0091] In the embodiment of the present application, the system can understand the main concepts or variables in the user's initial query statement through context information. For example, if the user's initial query statement mentions "order amount", the context information helps the system identify the specific fields and associated tables of the concept in the database. The system matches these identified concepts with the context information to ensure that the meaning of each concept in the database structure is clear and correct.

[0092] In the embodiment of the present application, when performing semantic analysis, if the system finds that certain concepts or variables in the user's initial query statement do not match the context or are ambiguous, the system will attempt to generate relevant explanations. For example, the user may enter an ambiguous field name, and the system will provide possible field explanations based on the context information. For example, if the user's query mentions "total amount", the system may recognize multiple possible fields of "total amount" in the database, such as "total order amount" or "payment amount", and provide clarifying explanations.

[0093] The embodiments of the present application can significantly improve the accuracy of queries by using context information to perform semantic analysis and clarification on user queries. The unclear parts in user queries can be clarified through the system's context inference, making the query results more accurate. In addition, the hierarchical strategy in the context selection scheme makes the system more efficient when processing very large databases. When the database information exceeds the computing power of the large model, the system can intelligently extract the core information for processing, avoiding unnecessary computing burdens while ensuring the effectiveness of the context.

[0094] In some embodiments, determining the context information required for semantic analysis of the user's initial query statement according to the semantic parsing result of the target database includes: In the case where the semantic parsing result does not exceed the context window limit of the large model, using the semantic parsing result as the context information; When the semantic parsing result exceeds the context window limit of the large model, the database core entity information and the database feature information are used as context information; If the sum of the database core entity information and the database feature information exceeds the context window limit of the large model, the database feature information is used as context information.

[0095] As an example, combining Figure 4 As shown, Figure 4 The process shown in demonstrates the basic steps and working principle of semantic analysis of the user's initial query statement based on the large model.

[0096] S401, construct relevant context based on the modeling results.

[0097] In the embodiment of the present application, based on the Context Window restrictions of the large model, intelligent selection is made of which context information is the most important to ensure efficient use of computing resources.

[0098] S402, identifying main concepts or variables in the user query.

[0099] In the embodiment of the present application, through the context information, the system can understand the main concepts or variables in the user's initial query statement.

[0100] S403, determining whether there is ambiguity or ambiguity.

[0101] In the embodiment of the present application, when performing semantic analysis, if the system finds that some concepts or variables in the user's initial query sentence do not match the context or are ambiguous, step S404 is executed, otherwise step S405 is executed. S404, generating the most relevant explanation according to the constructed context.

[0102] In the embodiment of the present application, for unclear or ambiguous concepts or variables in step S403, the most relevant explanation is generated according to the constructed context.

[0103] S405, optimizing the user query.

[0104] In the embodiment of the present application, the explanation generated in step S404 is used to optimize the user query, ensuring that the result expected by the user is clearly expressed.

[0105] In some embodiments, the initial database schema information is screened and processed according to the user query semantics and the semantic parsing result of the target database to obtain database key entity information corresponding to the user query, including: According to the semantic analysis result of the target database, determine N tables related to the user query semantics through semantic similarity search technology; In the context window of the large model, M tables and corresponding key columns are identified among the N tables that are semantically related to the user query, where M≤N; The M tables and the key columns are aggregated to obtain database key entity information corresponding to the user query.

[0106] In an embodiment of the present application, first, a semantic similarity search technique is used to determine N tables that are semantically related to the user query. Among them, the semantic similarity search technique refers to determining the database table that is most relevant to the user query by analyzing the semantic information of the text content. This technology uses natural language processing and machine learning models to understand the intention of the user query and find the best matching table in the schema of the database. For example, through the semantic similarity search technology, the top 20 tables that are semantically related to the user query are determined to narrow the search scope. After obtaining the top 20 tables, it is necessary to further accurately identify which tables are most relevant to the semantics of the user query. Specifically, an embodiment of the present application provides a decomposition processing strategy that is performed within the Context Window of a large model. Among them, the Context Window refers to the range of content that the model can take into account when processing data, ensuring that the model can accurately match and identify within a limited range.

[0107] In some embodiments, when processing a user query, the large model analyzes the content and structure of the first 20 tables to determine which tables have the most semantically appropriate query. The M tables (M ≤ 20) most relevant to the user query are selected from these 20 tables, and the key columns in these tables are identified.

[0108] In some embodiments, after determining the M most relevant tables and corresponding key columns, these data need to be screened and aggregated to determine the database key entity information that is ultimately required. Specifically, further screening is performed from the M tables to remove redundant and irrelevant tables and columns. The relevant tables and columns are summarized and integrated into the final database key entity information required for user queries. The goal of this step is to streamline and determine the minimum and necessary tables and columns to meet the user's query requirements. The embodiment of the present application maps user queries to key tables and columns in the database through semantic similarity search, context analysis, and data aggregation to generate a final SQL query. This method is particularly suitable for processing very large-scale databases because it avoids a comprehensive scan of the entire database through step-by-step screening and aggregation, thereby improving efficiency.

[0109] As an example, combining Figure 5 As shown, Figure 5 The process shown in demonstrates the basic steps and working principle of filtering and processing the initial database schema information based on the big model to obtain the database key entity information corresponding to the user query.

[0110] S501, based on the semantic analysis result of the target database, determine N tables related to the user query semantics.

[0111] In the embodiment of the present application, a semantic similarity search technology is used to determine N tables that are semantically related to the user query.

[0112] S502: Based on the large model, further identify M key tables and key columns that are most necessary for the user query.

[0113] In the embodiment of the present application, when processing a user query, the large model will analyze the content and structure of the first N tables to determine which tables have the most semantically appropriate query. The M tables (M ≤ 20) most relevant to the user query are selected from the N tables, and the key columns in these tables are identified.

[0114] S503: Aggregate the M tables and the key columns to determine the minimum and necessary tables and columns required in the end.

[0115] In the embodiment of the present application, after determining the M most relevant tables and corresponding key columns, these data need to be screened and aggregated to determine the database key entity information that is ultimately required.

[0116] In some embodiments, generating the SQL query statement according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information includes: Constructing a fifth agent and a sixth agent; wherein the fifth agent is used to generate an SQL query statement according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information, and the sixth agent is used to evaluate the SQL query statement and feed back the evaluation result to the fifth agent; The fifth agent regenerates the SQL query statement according to the evaluation result, and iteratively optimizes the SQL query statement until a set number of iterations is met or an end condition is triggered.

[0117] In the embodiment of the present application, a fifth agent (agent-e) and a sixth agent (agent-f) are constructed. Among them, agent-e is responsible for generating SQL query statements based on the database key entity information and the semantic parsing results of the target database corresponding to the database key entity information. This agent will use the semantic parsing results corresponding to the database table and column information obtained from the previous step to convert the user query into an initial SQL query statement. Agent-f is responsible for evaluating the execution results of the SQL query statement and providing feedback. The task of this agent is to check the execution of the SQL query and provide optimization suggestions based on the results. Specifically, agent-e uses the data obtained from the database key entity information to construct an SQL query statement. This is the initial version of the SQL query generation, which is written based on the mapping construction results. Agent-f executes the SQL query statement, and if the query execution fails (for example, due to syntax errors or database structure problems), the error information is submitted to agent-e as feedback. If the SQL query is successfully executed, agent-f will verify whether the SQL query meets the user query requirements. For example, check whether the returned data is accurate, whether it contains all necessary information, etc. If the SQL query does not fully meet the requirements, agent-b will make optimization suggestions.

[0118] In some embodiments, agent-e regenerates the SQL query statement based on the feedback from agent-f. The optimization process may include: if the feedback contains syntax error information, agent-e will modify the SQL query to correct these errors. Based on the feedback from the result verification, agent-e may adjust the logic of the SQL query to ensure that the returned results better meet the needs of the user.

[0119] In some embodiments, the above process is repeated until a set number of iterations is met or an end condition is triggered.

[0120] As an example, combining Figure 6 As shown, Figure 6The process shown in shows the basic steps and working principle of generating the SQL query statement based on the big model according to the key entity information of the database.

[0121] S601, generating SQL query statements based on the mapping construction results and feedback.

[0122] In the embodiment of the present application, agent-e generates an SQL query statement according to the mapping construction result, that is, the database key entity information and / or the feedback of agent-f.

[0123] S602: Evaluate the generated SQL query statement.

[0124] In the embodiment of the present application, agent-f evaluates the SQL query statement generated by agent-e and provides feedback to agent-a, including S6021, S6022, S6023, S6024, and S6025.

[0125] S6021, execute SQL.

[0126] S6022: Check whether SQL reports an error. If so, execute S6023; otherwise, execute S6024.

[0127] S6023, record the error information and execute S6025.

[0128] S6024, check whether the generated SQL query statement meets the user's query requirements.

[0129] S6025, generate final feedback.

[0130] Based on the same principle as the method provided in the embodiment of the present application, the embodiment of the present application also provides a SQL statement generation device based on a large model, such as Figure 7 As shown, the device comprises: The acquisition module 701 is used to acquire the user's initial query statement and the initial database schema information of the target database; Processing module 702, used for inputting the user's initial query statement and the initial database schema information of the target database into a large language model to obtain an SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

[0131] The SQL statement generation device based on the large model provided in the embodiment of the present application can achieve Figures 1 to 6 To avoid repetition, the various processes implemented in the method embodiment are not described here.

[0132] The SQL statement generation device based on a large model provided by the present application realizes efficient parsing and generation of natural language queries by combining the user's initial query statement and the database schema information of the target database. The large language model can not only deeply understand the semantic structure of the target database, but also perform accurate semantic analysis on user queries, thereby effectively extracting key database entity information related to the query. Compared with the existing Text2SQL method, the embodiment of the present application significantly improves the semantic understanding of database schema information, enhances the generalization ability to cope with diversified query scenarios, and greatly improves the final accuracy of SQL generation.

[0133] The big model-based SQL statement generation device of the embodiment of the present application can execute the big model-based SQL statement generation method provided in the embodiment of the present application. The implementation principle is similar. The actions performed by each module and unit in the big model-based SQL statement generation device in each embodiment of the present application correspond to the steps in the big model-based SQL statement generation method in each embodiment of the present application. For the detailed functional description of each module of the big model-based SQL statement generation device, please refer to the description in the corresponding big model-based SQL statement generation method shown in the previous text, which will not be repeated here.

[0134] Based on the same principle as the method shown in the embodiment of the present application, the embodiment of the present application also provides an electronic device, which may include but is not limited to: a processor and a memory; a memory for storing a computer program; a processor for executing the SQL statement generation method based on a large model shown in any optional embodiment of the present application by calling a computer program. Compared with the prior art, the SQL statement generation method based on a large model provided by the present application realizes efficient parsing and generation of natural language queries by combining the user's initial query statement and the database model information of the target database. The large language model can not only deeply understand the semantic structure of the target database, but also perform accurate semantic analysis on the user's query, thereby effectively extracting the key entity information of the database related to the query. Compared with the existing Text2SQL method, the embodiment of the present application significantly improves the semantic understanding of database model information, enhances the generalization ability to cope with diversified query scenarios, and greatly improves the final accuracy of SQL generation.

[0135] In an optional embodiment, an electronic device is also provided, such as Figure 8 As shown, Figure 8 The electronic device 8000 shown includes: a processor 8001 and a memory 8003. The processor 8001 and the memory 8003 are connected, such as through a bus 8002. Optionally, the electronic device 8000 may also include a transceiver 8004, which may be used for data interaction between the electronic device and other electronic devices, such as data transmission and / or data reception. It should be noted that in actual applications, the transceiver 8004 is not limited to one, and the structure of the electronic device 8000 does not constitute a limitation on the embodiments of the present application.

[0136] The processor 8001 may be a CPU (Central Processing Unit), a general-purpose processor, a DSP (Digital Signal Processor), an ASIC (Application Specific Integrated Circuit), an FPGA (Field Programmable Gate Array) or other programmable logic devices, transistor logic devices, hardware components or any combination thereof. It may implement or execute various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. The processor 8001 may also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a DSP and a microprocessor, etc.

[0137] The bus 8002 may include a path to transmit information between the above components. The bus 8002 may be a PCI (Peripheral Component Interconnect) bus or an EISA (Extended Industry Standard Architecture) bus. The bus 8002 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 8 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.

[0138] The memory 8003 may be a ROM (Read Only Memory) or other types of static storage devices that can store static information and instructions, a RAM (Random Access Memory) or other types of dynamic storage devices that can store information and instructions, or an EEPROM (Electrically Erasable Programmable Read Only Memory), a CD-ROM (Compact Disc Read Only Memory) or other optical disk storage, optical disk storage (including compressed optical disk, laser disk, optical disk, digital versatile disk, Blu-ray disk, etc.), magnetic disk storage medium, other magnetic storage devices, or any other medium that can be used to carry or store computer programs and can be read by a computer, without limitation herein.

[0139] The memory 8003 is used to store the computer program for executing the embodiment of the present application, and the execution is controlled by the processor 8001. The processor 8001 is used to execute the computer program stored in the memory 8003 to implement the steps shown in the above method embodiment.

[0140] Among them, electronic devices include but are not limited to: mobile terminals such as mobile phones, laptops, digital broadcast receivers, PDAs (personal digital assistants), PADs (tablet computers), PMPs (portable multimedia players), vehicle-mounted terminals (such as vehicle-mounted navigation terminals), etc., and fixed terminals such as digital TVs, desktop computers, etc. Figure 8 The electronic device shown is merely an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.

[0141] An embodiment of the present application provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps and corresponding contents of the aforementioned method embodiment can be implemented.

[0142] The embodiment of the present application also provides a computer program product, including a computer program, which can implement the steps and corresponding contents of the aforementioned method embodiment when executed by a processor.

[0143] The terms "first", "second", "third", "fourth", "1", "2", etc. (if any) in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the numbers used in this way can be interchanged where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than that shown or described in the drawings.

[0144] It should be understood that, although each operation step is indicated by arrows in the flowchart of the embodiment of the present application, the implementation order of these steps is not limited to the order indicated by the arrows. Unless clearly stated herein, in some implementation scenarios of the embodiment of the present application, the implementation steps in each flowchart can be performed in other orders according to demand. In addition, some or all of the steps in each flowchart may include multiple sub-steps or multiple stages based on actual implementation scenarios. Some or all of these sub-steps or stages may be executed at the same time, and each sub-step or stage in these sub-steps or stages may also be executed at different times respectively. In different scenarios of execution time, the execution order of these sub-steps or stages may be flexibly configured according to demand, and the embodiment of the present application does not limit this.

[0145] The above is only an optional implementation method for some implementation scenarios of the present application. It should be pointed out that for ordinary technicians in this technical field, without departing from the technical concept of the solution of the present application, other similar implementation methods based on the technical ideas of the present application are also within the protection scope of the embodiments of the present application.

Claims

1. A method for generating SQL statements based on a large model, characterized in that: include: Obtaining the user's initial query statement and the initial database schema information of the target database; Inputting the user's initial query statement and the initial database schema information of the target database into a large language model to obtain a structured SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

2. The method for generating SQL statements based on a large model according to claim 1, characterized in that: The performing semantic parsing on the initial database schema information of the target database to obtain the semantic parsing result of the target database includes: According to the initial database schema information of the target database, obtaining the column meaning and table meaning of each table in the target database; Determine, according to the table meaning of each table and the column meaning of each column in each table, the reference relationship between each table and other tables; Determine the core tables in the target database according to the number of reference relationships between each table and other tables, and cluster each of the core tables to obtain database core entity information; The database characteristic information of the target database is determined according to the database core entity information.

3. The method for generating SQL statements based on a large model according to claim 2, characterized in that: The acquiring, according to the initial database schema information of the target database, the column meaning and table meaning of each table in the target database comprises: Inferring and storing the business meaning of each column in each of the tables; Based on the business meanings of the columns, optimizing and storing the business meanings of each table; Based on the business meaning of each table, optimizing and storing the business meaning of each column in each table; The business meaning of each column and the business meaning of each table are optimized iteratively until a set number of iterations is met or an end condition is triggered.

4. The method for generating SQL statements based on a large model according to claim 2, characterized in that: Determining the reference relationship between each table and other tables according to the table meaning of each table includes: Constructing a first agent and a second agent; wherein the first agent is used to extract the reference relationship between each table and other tables according to the table meaning of each table, and the second agent is used to evaluate the reference relationship and feed back the evaluation result to the first agent; The first agent re-extracts according to the evaluation result, and iteratively optimizes the reference relationship between each table and other tables until a set number of iterations is met or an end condition is triggered.

5. The method for generating SQL statements based on a large model according to claim 2, characterized in that: The step of determining the core tables in the target database according to the reference relationship between each table and other tables, clustering the core tables, and obtaining the core entity information of the database includes: Sort by the number of reference relationships of each table, and select the first N tables as the core tables; Constructing a third agent and a fourth agent; wherein the third agent is used to cluster each of the core tables to obtain database core entity information, and the fourth agent is used to evaluate the database core entity information and feed back the evaluation result to the third agent; The third agent re-extracts according to the evaluation result and iteratively optimizes the core entity information of the database until the set number of iterations is met or the end condition is triggered.

6. The method for generating SQL statements based on a large model according to claim 2, characterized in that: The semantic analysis of the user's initial query statement is performed according to the semantic parsing result of the target database to obtain the user's query semantics, including: Determining context information required for semantic analysis of the user's initial query statement according to the semantic parsing result of the target database; The user query semantics is determined according to the context information.

7. The method for generating SQL statements based on a large model according to claim 6, characterized in that: The determining, based on the semantic parsing result of the target database, the context information required for semantic analysis of the user's initial query statement includes: In the case where the semantic parsing result does not exceed the context window limit of the large model, using the semantic parsing result as the context information; When the semantic parsing result exceeds the context window limit of the large model, the database core entity information and the database feature information are used as context information; If the sum of the database core entity information and the database feature information exceeds the context window limit of the large model, the database feature information is used as context information.

8. The method for generating SQL statements based on a large model according to claim 1, characterized in that: The initial database schema information is screened and processed according to the user query semantics and the semantic parsing result of the target database to obtain database key entity information corresponding to the user query, including: According to the semantic analysis result of the target database, determine N tables related to the user query semantics through semantic similarity search technology; In the context window of the large model, M tables and corresponding key columns are identified among the N tables that are semantically related to the user query, where M≤N; The M tables and the key columns are aggregated to obtain database key entity information corresponding to the user query.

9. The method for generating SQL statements based on a large model according to claim 1, characterized in that: The generating the SQL query statement according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information includes: Constructing a fifth agent and a sixth agent; wherein the fifth agent is used to generate an SQL query statement according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information, and the sixth agent is used to evaluate the SQL query statement and feed back the evaluation result to the fifth agent; The fifth agent regenerates the SQL query statement according to the evaluation result, and iteratively optimizes the SQL query statement until a set number of iterations is met or an end condition is triggered.

10. A SQL statement generation device based on a large model, characterized in that: include: An acquisition module, used to acquire the user's initial query statement and the initial database schema information of the target database; A processing module, used for inputting the user's initial query statement and the initial database mode information of the target database into a large language model to obtain an SQL query statement; The large language model is used to perform the following operations: Performing semantic analysis on the initial database schema information of the target database to obtain a semantic analysis result of the target database; According to the semantic parsing result of the target database, semantic analysis is performed on the user's initial query statement to obtain the user's query semantics; According to the user query semantics and the semantic parsing result of the target database, the initial database schema information is screened and processed to obtain database key entity information corresponding to the user query; The SQL query statement is generated according to the database key entity information and the semantic parsing result of the target database corresponding to the database key entity information.

11. An electronic device, characterized in that: The method comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the method according to any one of claims 1 to 9 is implemented when the processor executes the program.

12. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.

13. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method according to any one of claims 1 to 9 is implemented.

Citation Information

Patent Citations

  • Query statement generation method, device and equipment based on Text2SQL (Structured Query Language)

    CN117520375A

  • SQL (Structured Query Language) statement generation method and device based on large language model, equipment and medium

    CN117743371A

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

    CN118643050A

  • Database Query Generation from Natural Language Statements

    US20240273090A1

Cited By

  • Multi-agent-based SQL statement end-to-end batch generation method

    CN119179711A

  • Report generation method and device based on large model and electronic equipment

    CN119474108A

  • A report generation method, device and electronic device based on a large model

    CN119474108B

  • Query statement model fine tuning method, device and equipment based on large model

    CN119599131A

  • Automatic method and device for converting relational data into attribute graph

    CN119782405A