A method and device for generating SQL based on semantic alignment and hierarchical agent
By introducing a method based on semantic alignment and hierarchical agents in Text2SQL technology, the problems of semantic understanding bias and low query efficiency in educational database queries are solved, and higher query accuracy and user experience are achieved.
Patent Information
- Application Number
- CN202411960119.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-30
- Publication Date
- 2025-05-13
- Estimated Expiration
- 2044-12-30
AI Technical Summary
The existing Text2SQL technology has problems such as user query semantic understanding deviation, high computing power cost, slow query speed and low accuracy in education database query.
Using a semantic alignment and hierarchical agent-based approach, semantic information alignment, problem disassembly, candidate tables and field selection, combined with FAQ technology for FAQ technology, structured query language SQL is generated, and reflected and processed to improve query accuracy and efficiency.
It significantly improves the accuracy of table selection and field selection, improves query speed and the accuracy of final generated results, and significantly improves the user experience.
Smart Images

Figure CN119377255B_ABST
Abstract
Description
Technical Field
[0001] The invention relates to a method for generating SQL, and relates to the technical field of database query optimization, and in particular to a method and device for generating SQL based on semantic alignment and hierarchical Agent. Background Art
[0002] Text2SQL technology is a technology that converts human natural language text into structured query language SQL (Structured Query Language). At present, it is mainly guided by carefully constructed user prompts to generate structured query language SQL in accordance with predetermined steps and sequences. However, when integrating the existing Text2SQL technology into educational database queries, the following challenges are encountered: First, since user queries usually involve terms related to the education field, the semantic understanding of the user query by the large language model LLM is biased, which leads to errors in the table and field selection process of the large language model LLM. Secondly, the query of the educational database covers a variety of tasks from simple to complex. If only a fixed process is used for a variety of Text2SQL tasks, it may increase the computing cost, affect the query speed, and reduce the accuracy of Text2SQL. The existing database query experience is usually poor, due to inaccurate understanding of user intent, complex selection of multiple tables and fields, and low query efficiency. Especially in the field of education, due to the large number of data tables and complex fields, full query often leads to excessive consumption of word tokens and inaccurate query results. Summary of the invention
[0003] In order to solve the problems existing in the background technology, the present invention provides a method and device for generating SQL based on semantic alignment and hierarchical Agent.
[0004] The technical solution adopted by the present invention is:
[0005] 1. A method for generating SQL based on semantic alignment and hierarchical agent:
[0006] Step 1) When a user makes a query, the original user query question and the education database containing several education information tables are semantically aligned, including information enhancement, semantic understanding and semantic mapping processing, to obtain a valid user query question and several related enhanced education information tables. The valid user query question is the original user query question or the user query question after semantic understanding processing.
[0007] Step 2) Call the large model agent to use the large language model to process the valid user's query questions and their related enhanced education information tables, including question decomposition, candidate table selection and candidate field selection processing, to obtain the user's query questions after secondary processing and their related candidate tables and candidate fields. The user's query questions after secondary processing are valid user's query questions or valid user's query questions after question decomposition.
[0008] Step 3) Based on the candidate tables and candidate fields related to the user's query questions after secondary processing, a structured query language SQL is generated using the FAQ (Frequently Asked Questions) method and reflected upon to obtain the final reply text and feed it back to the user.
[0009] In the step 1), when performing semantic information alignment, the definition information schema of each education information table in the education database is first enhanced, and key information and preset special tags and their descriptions are added to obtain the enhanced definition information schema of each education information table. e ; Then, the user's query questions are categorized and rewritten to obtain valid user query questions, and the enhanced education information tables are divided into business levels. The valid user's query questions are semantically mapped through a large language model and then mapped to the corresponding business level, so that several enhanced education information tables related to the valid user's query questions can be queried in the mapped business level.
[0010] When the user's query questions are classified, the user's query questions are divided into two categories. The query questions of users involving time terms are classified as time confusion questions, and the query questions of other users are classified as semantic confusion questions. When the query question belongs to the time confusion question, additional time information is injected into the query question. R t , including the current time and the start and end time in the query question; when the query question belongs to the semantic confusion problem, first build a preset ambiguous dataset D , ambiguous dataset D contains several ambiguous sentences or phrases, and then based on the ambiguous dataset D Fine-tune the conversation pre-training model ChatGLM3 to obtain the fine-tuned conversation pre-training model G t , input the user's query question into the fine-tuned dialogue pre-training model G tIt is processed in order to determine whether the user's query question is ambiguous. If so, the user's query question is rewritten until there is no ambiguity and a valid user's query question is obtained.
[0011] In the step 2), when disassembling the problem, first create a problem disassembly large model intelligent agent (Disassemble Agent), input the valid user's query problem into the large language model for processing, and the large language model queries several enhanced education information tables related to the valid user's query problem; when the valid user's query problem only queries one related enhanced education information table, the valid user's query problem is regarded as a simple problem, and the problem disassembly large model intelligent agent is not called; when the valid user's query problem queries two or more related enhanced education information tables, the valid user's query problem is regarded as a complex problem, and the problem disassembly large model intelligent agent is called to disassemble the valid user's query problem, thereby obtaining several disassembled simple problems, and then obtaining several enhanced education information tables related to each disassembled simple problem.
[0012] In the step 2), when selecting candidate tables, first create a large model agent (Table Agent) for selecting candidate tables. When the number of enhanced education information tables related to the user's query questions after secondary processing is less than or equal to a preset threshold, the large model agent for selecting candidate tables is not called, and each related enhanced education information table is directly used as a candidate table for the user's query questions after secondary processing; when the number of enhanced education information tables related to the user's query questions after secondary processing is greater than a preset threshold, then the enhanced education information tables related to the user's query questions after secondary processing and their enhanced definition information schema are used. e , use the large language model to generate a specific description of each related enhanced education information table of the user's query question after secondary processing, then process the user's query question after secondary processing and the specific description of each related enhanced education information table through the large language model, and finally select several candidate tables of the user's query question after secondary processing, and inject each candidate table into the instruction information of the large language model.
[0013] In the step 2), when selecting candidate fields, first create a large model agent (Column Agent) for selecting candidate fields. By using the large language model and the machine learning Few-Shot method, the large model agent for selecting candidate fields automatically determines and selects the most relevant candidate fields in each candidate table of the user's query question after secondary processing, and injects each candidate field into the instruction information of the large language model.
[0014] In the step 3), a FAQ method is used to create several common questions and their corresponding structured query language SQL results. During the query process, if the user's query question belongs to a common question, the structured query language SQL result of the common question is injected into the instruction of the large language model as additional information, and the user's query question and its candidate fields after secondary processing are processed by the large language model to generate a final executable structured query language SQL statement; then the generated structured query language SQL statement is reflected, the generated structured query language SQL statement is executed in the education database, and then the running result of the structured query language SQL is returned. If the structured query language SQL is executed correctly, it is considered that the structured query language SQL is correct; if the structured query language SQL fails to execute, the error message of the failure of the structured query language SQL and the query question of the current failed structured query language SQL are re-input into the large language model for processing, thereby regenerating a new structured query language; finally, the regenerated new structured query language after execution is processed by the large language model, and the text result is output and returned to the user.
[0015] 2. A SQL generation device based on semantic alignment and hierarchical agent:
[0016] The device comprises a data acquisition unit, which is used for acquiring data from original user's query questions and an education database containing a plurality of education information tables.
[0017] The device includes a data processing unit for performing semantic information alignment processing on the original user's query questions and an education database containing several education information tables, calling large model intelligent agent processing, and generating and reflecting on structured query language SQL.
[0018] The device includes a data execution unit, which executes and finally generates a structured query language SQL and obtains a final reply text, which is then fed back to the user.
[0019] 3. An electronic device comprises: a memory and a processor coupled to each other, wherein the memory stores program data, and the processor calls the program data to execute the method as described above.
[0020] 4. A computer-readable storage medium having program data stored thereon, wherein the program data implements the method described above when executed by a processor.
[0021] The beneficial effects of the present invention are:
[0022] 1) The present invention significantly improves the accuracy of table selection and field selection by accurately aligning user queries with the semantic information of the education database.
[0023] 2) The present invention introduces a large model intelligent agent agent to autonomously plan the structured query language SQL generation framework, calls different large model intelligent agent agents to realize the generation process control of simple and complex queries, thereby improving the query speed, and calls the structured query language SQL to reflect the large model intelligent agent agent on the final generation result, which can achieve a higher accuracy in the query effect. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] Figure 1 is a flow chart of the method of the present invention;
[0025] Figure 2 is a block diagram of the method of the present invention;
[0026] Figure 3 This is a flow chart of the method of the present invention to realize dynamic calling of intelligent agents;
[0027] Figure 4 is a schematic diagram of a device for implementing the method of an embodiment of the present invention;
[0028] Figure 5 It is a schematic diagram of an electronic device for implementing the method of the embodiment of the present invention. DETAILED DESCRIPTION
[0029] The present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0030] like Figure 1 and Figure 2 As shown, the method of generating SQL based on semantic alignment and hierarchical Agent of the present invention is as follows:
[0031] The database used in the specific implementation of the present invention is an education database composed of 22 tables, including a class information table, a school information table, and a teacher training information table, etc. As shown in Table 1, part of the content of the class information table is shown. The class information table contains multiple records, each record has multiple field (column) information, including class ID (class_id), organization ID (org_id), campus ID (campus_id), campus name (campus_name), class name (name) and alias (alias), etc. Each field in the original definition information schema of the class information table contains name, data type and remarks information, as shown in the table structure (CREATE TABLE) of Table 2.
[0032] Table 1
[0033]
[0034] Table 2
[0035]
[0036] Among them, cdm_dim_org_class is the name of the class information table; class_id is a field in the table; bigint(20) means that the field is of bigint type, 20 means that the data will be aligned with a width of 20 characters when displayed, varchar(200) means that the field is a variable-length string with a maximum length of 200 characters, int(11) means that the field is an integer type, indicating that the output width is 11 when queried; NOT NULL means that this field cannot be empty, and each record in this table must have a valid class_id, NULL means that the record in this column can be empty; COMMENT is to further explain the purpose of this field, pointing out that the field is used to store class ID, etc.
[0037] When users make inquiries, if all table information and field information are fully queried, it is easy to cause excessive token consumption. In addition, when users use colloquial methods to query, large language models such as GPT-4o (omni) language models may not accurately understand the user's intentions and never generate effective structured query language SQL query statements. Therefore, in order to optimize query efficiency, the original definition information Schema of each table in the database can be enhanced, semantically understood, and semantically mapped, so that the user's query questions can be transformed into query Semantic information alignment is performed with the education database, and the information schema is defined to define the organization and structure information of each table in the database.
[0038] Inquiry questions from users query When aligning with the semantic information of the education database, we first enhance the original definition information schema of each table in the education database, add key information and preset special tags in the description of the table and field, and specifically add the main information describing each table and the important fields and the meaning of the fields through the COMMENT keyword, especially explain the values of the special tags. The enhanced definition information schema of the class information table e As shown in Table 3:
[0039] Table 3
[0040]
[0041] In the original definition information schema of the education database, as shown in Table 2, there is a lack of clear description of the status of special marks during the design process. For example, the status of academic completion, special marks are stored as 0 and 1 in the record. Therefore, it is necessary to use the COMMENT field to annotate similar information and inform the large language model GPT-4o that 1 means graduation and 0 means studying, so as to obtain the enhanced definition information schema of the class information table. e .
[0042] Then we need to ask the user a query query Before semantic understanding, the education-related terms are divided into two categories: time confusion and semantic confusion. query Time confusion involves time terms, such as "this academic year", "this semester", etc., while other confusing terms, such as "qualifications", "top three in grades", etc., are defined as semantic confusion.
[0043] Upon receiving a query from a user query After that, you first need to determine the query question query The term category, when the query question query When it falls into the time confusion category, such as the user's query question query If the query question is "Name of the head teacher of Class 2, Grade 3 this semester", query Inject additional time information into R t , including the current time and the time of this semester, the large language model GPT-4o will query the enhanced database based on the time information R t Determine the current time and the start and end time of this semester, so as to query "the name of the head teacher of Class 2, Grade 3 from the start time to the end time of this semester".
[0044] When querying questions query When it belongs to the semantic confusion category, you need to first build a preset ambiguous dataset D , ambiguous dataset D There are several ambiguous sentences or phrases in the dataset. In the specific implementation, 124 ambiguous sentences or phrases were collected based on the user's historical query questions to construct an ambiguous dataset. D , according to the ambiguous dataset D Fine-tune the conversation pre-training model ChatGLM3 to obtain the fine-tuned conversation pre-training model G t , so that it can be used to determine the user's query problem query Is there any ambiguity? If so, perform the user's query. query, until there is no ambiguity, as shown in Table 4 below:
[0045] Table 4
[0046]
[0047] When a user queries "Which teachers have low salaries?", the sentence is ambiguous because "low salaries" has a vague meaning and lacks an accurate numerical definition. Combined with the existing teacher salaries, teachers with salaries below the average level are considered to have "low salaries". The user's query is rewritten as "Which teachers have salaries below the average level?". By determining the ambiguity, the ambiguity in structured query language SQL queries can be effectively reduced and avoided.
[0048] Determining the user's query query Is there an ambiguous process, the fine-tuned dialogue pre-training model G t Instruction statement I g As shown in Table 5 below:
[0049] Table 5
[0050]
[0051] So as to obtain the rewritten query problem query r , query r = G t ( I g , query ),in, query Represents the user's initial query statement.
[0052] After ensuring that the user's query questions are valid, it is also necessary to improve the user's query speed and the probability of querying the correct tables and fields. For users who are not familiar with educational databases, the query questions are diverse, and it is extremely difficult for the large language model GPT-4o to select the correct tables and fields from the enhanced database with multiple tables and fields. Therefore, it is necessary to semantically map the database. First, all tables in the database are classified into business levels, and then divided into: teacher, class, student business level and teacher scientific research, training, and teacher ethics level, as shown in Table 6. Then, the relationship between different tables and business levels in the database is constructed. All tables in the existing education database are divided into corresponding levels. For each valid query question, first determine the business level to which the valid query question belongs, map the valid query question to the corresponding business level, narrow the query scope, improve the query speed and accuracy, and then query and select the relevant tables in the corresponding business level.Tables b .
[0053] Table 6
[0054]
[0055] After semantic information alignment, user queries can be effectively aligned with the business level and field information of the education database, improving the accuracy and efficiency of queries.
[0056] After the semantic information of the user's query question and the education database are aligned, the large model agent Agents are called. After obtaining the effective user query question, the structured query language SQL generation process will be carried out next. In order to cope with the diverse query questions raised by users, the large model agent Agents can independently plan the generation of structured query language SQL. Each large model agent Agent can make independent decisions based on the current context and specific tasks, and flexibly select the various stages of structured query language SQL generation, so as to make the best behavior choice when facing different query requirements, and improve the query speed, such as Figure 3 shown.
[0057] First, create a problem disassembly big model intelligent agent (Disassemble Agent), through the problem disassembly big model intelligent agent using the big language model GPT-4o to disassemble the user's query problem. For the user's query problem, first input the valid user's query problem into the big language model GPT-4o for processing. The big language model GPT-4o first determines the business level of the enhanced database to which the user's query problem belongs, and then obtains several tables related to the user's query problem from the corresponding business level. Tables b ; When only one table is involved, the user's query question is a "simple question", such as "How many male teachers are there in the school?", then the problem decomposition large model agent will not be called; when two or more tables are involved, the user's query question is a "complex question", such as "Which teacher has multiple administrative positions?", involving the teacher table and the teacher position table, the user's query question will be decomposed into "Which teacher has multiple administrative positions, and how many administrative positions are there respectively", and the decomposition result is query d =GPT-4o( query r ,schema e ), according to the disassembly results query d Generate the structured query language SQL for each simple question and then query them separately in the database.
[0058] Then create a large model agent (Table Agent) to select the candidate table. The large model agent uses the large language model GPT-4o to query the relevant tables at the corresponding business level. Tables b Select the table that is most relevant to the user's query after decomposition Tables r When there are multiple tables in the database, including all the information in the database in each instruction will not only cause a large amount of token waste, but also easily lead the large language model GPT-4o to generate incorrect results. e First, use the large language model GPT-4o to generate a specific description of each table desp t , desp t =GPT-4o(schema e , Tables b ), each description information includes: table name, table name explanation, table specific purpose and important field values in the table, as shown in Table 7, and each different information is separated by a comma.
[0059] Table 7
[0060]
[0061] Then through the specific description of the above table desp t In the process of selecting a table for the large language model GPT-4o, the description information is directly entered without entering all the field texts of the table in detail, thereby reducing the overhead of the word Token and retaining important field information, which maximizes the guarantee that the large language model GPT-4o selects the correct candidate table. Tables r , Tables r =GPT-4o( desp t , query d ).
[0062] Generally, most users' query questions involve 2-3 tables. In order to provide a certain fault tolerance rate, the large model agent for selecting candidate tables will be called in this stage to select the top 5 most relevant tables as candidate tables. When there are fewer tables in the database, the large model agent for selecting candidate tables will not be called.
[0063] After selecting the candidate table, create a column agent to select candidate fields. The column agent uses the large language model GPT-4o and the machine learning Few-Shot method to automatically select the most relevant candidate fields in the candidate table for the generation of structured query language SQL. In the field selection process, some examples are given. example c , and combined with the enhanced table definition information schema of the candidate table e , let the large language model GPT-4o independently judge the candidate fields in the candidate table Candidate c The final candidate table and candidate fields will be injected into the instruction information. Some examples example c As shown in Table 8 below:
[0064] Table 8
[0065]
[0066] Candidate fields Candidate c =GPT-4o(schema e , example c ,[ Tables r | Tables b ],[ query | query r | query d ]), where [ ] indicates optional, | indicates selecting one, [ query | query r | query d ] indicates that the query question may be the user's original query question query , it may also be a rewritten query problem query r , or the query problem after disassembly query d .
[0067] Each large model agent called makes independent decisions based on the current context and specific tasks, flexibly selects the optimal structured query language SQL generation path, and handles complex query problems.
[0068] In the process of generating structured query language SQL, there are a large number of similar question queries when querying the education database. Therefore, the FAQ (Frequently Asked Questions) technology is used to write 20 frequently asked questions and the corresponding structured query language SQL results in advance. During the query process, if similar query questions are retrieved, the results of this part are injected into the instruction generated by the structured query language SQL as additional information, and the beginning information of the instruction is defined. start As shown in Table 9 below:
[0069] Table 9
[0070]
[0071] The FAQ questions and their corresponding SQL statements are shown in Table 10 below:
[0072] Table 10
[0073]
[0074] Among them, the query problem indicates that it is necessary to count the number of teachers and students of each grade in the campus. The corresponding structured query language SQL statement is shown in Table 11:
[0075] Table 11
[0076]
[0077] Among them, SELECT is the keyword of the query statement in the structured query language SQL, which selects the grade id (grade_id) in the class information table (cdm_dim_org_class), the teacher id (staff_id) in the faculty information table (cdm_dim_staff), and the student id (student_id) in the education and teaching information table (cdm_dim_student); COUNT() is an aggregate function used to count the number of records, the DISTINCT keyword is used to exclude duplicate values in the result, and it is used to exclude duplicate values from the result set. The AS keyword specifies an alias for the query result, and FROM indicates that the table specified for the query is cdm_dim_org_class (class information table). The corresponding structured query language SQL statement for associating classes and faculty is shown in Table 12:
[0078] Table 12
[0079]
[0080] Among them, the staff position appointment information table (staff_assignment) stores the assignment relationship between classes and teachers, and associates them through the class id (class_id) to obtain the teacher information under each class. The corresponding structured query language SQL statement for associating teacher information is shown in Table 13:
[0081] Table 13
[0082]
[0083] Here, the staff information table (cdm_dim_staff) stores detailed information about teachers, and the teacher id (staff_id) is used for association to obtain the specific information of the teacher. The corresponding SQL statement for associating student information is shown in Table 14:
[0084] Table 14
[0085]
[0086] Among them, the education and teaching information table (cdm_dim_student) is used to store detailed information of students, and is associated through the grade id (grade_id) to obtain student information under each grade. To filter a specific campus, the corresponding structured query language SQL statement is shown in Table 15:
[0087] Table 15
[0088]
[0089] Among them, the campus id is used for filtering, "1727892204627824656" is the specified campus id, and only the teacher and student data in this specific campus are counted. Group statistics, the corresponding structured query language SQL statement is shown in Table 16:
[0090] Table 16
[0091]
[0092] Among them, grouping is done by grade id (grade_id), and the number of teachers and students in each grade is finally counted.
[0093] Finally, according to the FAQ information and candidate field information Candidate c 、User query questions query , time information R t , call GPT-4o to generate the final executable structured query language SQL statement, SQL=GPT-4o(I start ,[R t ], Candidate c ,[FAQ],[ query | query r | query d ]).
[0094] Then, it is necessary to reflect on the structured query language SQL. For the generated structured query language SQL statement, it is necessary to check whether its execution result is correct or not to improve the user experience. Therefore, the generated structured query language SQL statement is executed in the education database, and then the running result of the structured query language SQL is returned. If the structured query language SQL is executed correctly, it is considered that the structured query language SQL is correct; if the structured query language SQL fails to execute, the error message E of the structured query language SQL failure is returned. info , and the failed SQL query problems are re-entered into the large language model GPT-4o to regenerate the SQL query problems. r , SQL r =GPT-4o (I start ,E info ,[ R t ], Candidate c ,[FAQ],[ query | query r | query d ]).
[0095] Only one SQL reflection is needed to correct most SQL errors. Finally, the output results are sorted through the large language model GPT-4o to obtain the final text results. Response , Response =GPT-4o(execute(SQL)), where execute(SQL) means executing the structured query language SQL and returning it to the user.
[0096] The structured query language SQL results of common query questions are written in advance through FAQ technology, combined with intelligent generation and self-reflection mechanisms to ensure that the generated structured query language SQL statements are accurate, and structured query language SQL errors are corrected and regenerated when necessary, thereby significantly improving query reliability and user experience.
[0097] Finally, based on the education dataset, 200 different evaluation datasets were manually prepared, and the results of the generated structured query language SQL statements were compared with the results in the evaluation dataset, achieving a 92% accuracy rate in query results.
[0098] like Figure 4 As shown, it is a block diagram of an apparatus for generating SQL based on semantic alignment and hierarchical agent according to an embodiment of the present disclosure, the apparatus includes a data acquisition unit, a data processing unit and a data execution unit, the data acquisition unit acquires the original user natural query statement and the target database Schema information; wherein the target database is an education database containing 22 tables of education information tables; the data processing unit performs semantic understanding and semantic mapping on the original user natural query statement, and enhances the Schema information of the education database, and at the same time, according to the query information and education database information obtained after processing, dynamically calls the intelligent agent Agents to autonomously define the process of generating structured query language SQL, calls the large model agent processing and generates and reflects the structured query language SQL, wherein the dynamically called intelligent agent Agents include a problem decomposition large model agent, a candidate table selection large model agent and a candidate field selection large model agent; the data execution unit executes the final generation of structured query language SQL, captures the generation result, obtains the final reply text, and then automatically feeds back to the user.
[0099] like Figure 5 As shown, it is a block diagram of an electronic device according to the method of generating SQL based on semantic alignment and hierarchical agent according to an embodiment of the present disclosure. The electronic device includes a memory, a computing module, a read-only memory and a random access memory. The computing module can perform various appropriate actions and processes according to a computer program stored in the read-only memory or a computer program loaded from the memory to the random access memory. The computing module includes but is not limited to a central processing module, a graphics processing module, and various modules for running machine learning model algorithms. The computing module, the read-only memory and the random access memory are interconnected through an internal bus, and the input / output interface is also connected to the internal bus. The input module, the output module, the storage module and the communication module are connected to the internal bus through the input / output interface. The input module can be a keyboard and a mouse, etc., the output module can be a display, etc., the storage module can be a disk, etc., the communication module can be a network card and a wireless communication transceiver, etc., and the communication module allows the electronic device to exchange information / data with other devices through a computer network such as the Internet.
[0100] Electronic devices are intended to represent various forms of digital computers, such as desktop computers, workstations, and servers, etc. Electronic devices may also represent various forms of mobile devices, such as smart phones and wearable devices, etc. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit implementations of the present disclosure described and / or claimed herein.
[0101] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may be provided to a general-purpose computer, a special-purpose computer, or other programmable processor or controller so that the program code, when executed by the processor, implements the functions / operations specified in the flow chart and / or block diagram. The program code may be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.
[0102] In order to provide interaction with a user, the method of the present invention can be implemented on a computer, which has: a display device (such as a liquid crystal display monitor, etc.) for displaying information to the user, and a keyboard and a pointing device (such as a mouse, etc.), and the user can provide input to the computer through the keyboard and the pointing device.
[0103] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A method for generating SQL based on semantic alignment and hierarchical agent, characterized in that: include: Step 1) When a user makes a query, semantic information alignment is performed between the original user query question and an education database containing a plurality of education information tables, including information enhancement, semantic understanding and semantic mapping processing, to obtain a valid user query question and a plurality of related enhanced education information tables, wherein the valid user query question is the original user query question or the user query question after semantic understanding processing; Step 2) calling the large model agent to use the large language model to process the valid user's query question and its related enhanced education information tables, including question decomposition, candidate table selection and candidate field selection processing, to obtain the user's query question after secondary processing and its related candidate table and candidate field, the user's query question after secondary processing is the valid user's query question or the valid user's query question after question decomposition; Step 3) Based on the candidate tables and candidate fields related to the user's query questions after secondary processing, a structured query language SQL is generated using the FAQ method and reflected upon, and the final reply text is obtained and fed back to the user; In the step 1), when performing semantic information alignment, the definition information schema of each education information table in the education database is first enhanced, and key information and preset special tags and their descriptions are added to obtain the enhanced definition information schema of each education information table. e ; Then, the user's query questions are categorized and rewritten to obtain valid user query questions, and each enhanced education information table is divided into business levels, and then the valid user query questions are semantically mapped through a large language model and mapped to the corresponding business level, so that several enhanced education information tables related to the valid user query questions can be queried in the mapped business level; When the user's query questions are classified, the user's query questions are divided into two categories: the user's query questions involving time terms are divided into time confusion questions, and the other user's query questions are divided into semantic confusion questions; When the query question is a time-confused question, inject additional time information into the query question R t , including the current time and the start and end times in the query question; When the query problem belongs to the semantic confusion problem, first build a preset ambiguous dataset D , ambiguous dataset D contains several ambiguous sentences or phrases, and then based on the ambiguous dataset D Fine-tune the conversation pre-training model ChatGLM3 to obtain the fine-tuned conversation pre-training model G t , input the user's query question into the fine-tuned dialogue pre-training model G t In the process, it is processed to determine whether the user's query question is ambiguous. If there is ambiguity, the user's query question is rewritten until there is no ambiguity, and a valid user's query question is obtained; In the step 2), when selecting a candidate table, a large model agent for selecting the candidate table is first created. When the number of enhanced education information tables related to the user's query question after secondary processing is less than or equal to a preset threshold, the large model agent for selecting the candidate table is not called, and each related enhanced education information table is directly used as a candidate table for the user's query question after secondary processing; When the number of enhanced education information tables related to the query questions of the user after the secondary processing is greater than the preset threshold, the enhanced education information tables related to the query questions of the user after the secondary processing and their enhanced definition information schema are generated. e , using the large language model to generate a specific description of each related enhanced education information table of the secondary processed user query question, and then processing the secondary processed user query question and the specific description of each related enhanced education information table through the large language model, and finally selecting a number of candidate tables of the secondary processed user query question, and injecting each candidate table into the instruction information of the large language model; In the step 2), when selecting candidate fields, firstly, a large model agent for selecting candidate fields is created, and the large model agent for selecting candidate fields uses a large language model and adopts a machine learning Few-Shot method to automatically determine and select the most relevant candidate fields in each candidate table of the user's query question after secondary processing, and each candidate field is injected into the instruction information of the large language model; In the step 3), a FAQ method is used to create several common questions and their corresponding structured query language SQL results. During the query process, if the user's query question belongs to a common question, the structured query language SQL result of the common question is injected into the instruction of the large language model as additional information, and the user's query question and its candidate fields after secondary processing are processed by the large language model to generate a final executable structured query language SQL statement; then the generated structured query language SQL statement is reflected, the generated structured query language SQL statement is executed in the education database, and then the running result of the structured query language SQL is returned. If the structured query language SQL is executed correctly, it is considered that the structured query language SQL is correct; if the structured query language SQL fails to execute, the error message of the failure of the structured query language SQL and the query question of the current failed structured query language SQL are re-input into the large language model for processing, thereby regenerating a new structured query language; finally, the regenerated new structured query language after execution is processed by the large language model, and the text result is output and returned to the user.
2. The method for generating SQL based on semantic alignment and hierarchical Agent according to claim 1, characterized in that: In the step 2), when performing problem decomposition, first create a problem decomposition large model intelligent agent, input the valid user's query problem into the large language model for processing, and the large language model queries several enhanced education information tables related to the valid user's query problem; when the valid user's query problem only queries one related enhanced education information table, the valid user's query problem is regarded as a simple problem, and the problem decomposition large model intelligent agent is not called; when the valid user's query problem queries two or more related enhanced education information tables, the valid user's query problem is regarded as a complex problem, and the problem decomposition large model intelligent agent is called to decompose the valid user's query problem, thereby obtaining several decomposed simple problems, and then obtaining several enhanced education information tables related to each decomposed simple problem.
3. A device for generating SQL based on semantic alignment and hierarchical Agent applicable to the method according to any one of claims 1 to 2, characterized in that: include: A data acquisition unit, used for acquiring data from original user query questions and an education database containing a plurality of education information tables; A data processing unit, used for performing semantic information alignment processing on the original user's query question and the education database containing several education information tables, calling large model intelligent agent processing, and generating and reflecting on the structured query language SQL; The data execution unit executes the final generated structured query language SQL and obtains the final response text, which is then fed back to the user.
4. An electronic device, characterized in that: include: A memory and a processor coupled to each other, wherein the memory stores program data, and the processor calls the program data to execute the method according to any one of claims 1-2.
5. A computer-readable storage medium having program data stored thereon, characterized in that: When the program data is executed by a processor, the method according to any one of claims 1 to 2 is implemented.
Citation Information
Patent Citations
Natural Language Question-Answering Search System for Integrated Access to Database, FAQ, and Web Site
KR1020010107111A
Devices and methods for generating an SQL query based on a natural language query
WO2024120610A1