Data processing method and device, equipment and storage medium
By retrieving and disassembling the agent, the large language model is guided to extract key information and disassembly natural language text into sub-problems, and sub-SQL is gradually generated, which solves the problem of low accuracy in generating SQL in natural language and improves the accuracy and efficiency of database queries.
Patent Information
- Application Number
- CN202510420129.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-03
- Publication Date
- 2025-07-22
AI Technical Summary
In the prior art, SQL generated based on natural language has low accuracy, which affects the accuracy and efficiency of database queries. Especially when processing complex natural languages, it is impossible to completely cover the information required for search, resulting in low query accuracy and efficiency.
By searching the agent to extract key information in the natural language text, and obtain the search results of similarity satisfying the set conditions from the pre-stored database. The disassembled agent to disassemble the natural language text into multiple sub-problems, and gradually generate sub-SQL, and the last sub-SQL is used as the target SQL to gradually solve user problems.
It significantly improves the accuracy of complex SQL, reduces the technical threshold for non-professional users to use databases, ensures that even non-professional users can obtain accurate query results through natural language questions, and improves the efficiency of database queries.
Smart Images

Figure CN120353816A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of artificial intelligence, and in particular, to a data processing method, device, equipment and storage medium. Background Art
[0002] In the process of querying data using a database, generally, users need to manually write the corresponding Structured Query Language (SQL) to improve the query efficiency. However, with the increase in the amount of data and the increasing complexity of the data structure, the complexity of the corresponding SQL also increases accordingly. This undoubtedly increases the query difficulty and learning cost of ordinary users without relevant knowledge backgrounds for the database, and also increases the error rate of manual writing by technical users with relevant knowledge backgrounds.
[0003] To solve the above problems, currently, the Natural Language Processing (NPL) technology is proposed, which mainly converts the natural language input by users into SQL through a large language model. Thus, users do not need to write SQL by themselves, which helps to simplify database access, further improve the query efficiency and lower the technical threshold.
[0004] However, for complex natural languages, for example, although the natural language has only one sentence, it contains multiple pieces of information to be searched. And since the large language model parses the natural language and converts it into a single SQL during the process of processing natural language, the SQL accuracy of complex natural languages is relatively low, unable to fully cover the information to be searched in the natural language, thus affecting the query accuracy; moreover, there is also the problem of low conversion efficiency, which further affects the query efficiency. Summary of the Invention
[0005] In view of this, to improve the database query accuracy and query efficiency based on natural language texts, the purpose of the present invention is to provide a data processing method, device, equipment and storage medium.
[0006] To achieve the above purpose, the technical solutions adopted in the embodiments of the present invention are as follows:
[0007] In the first aspect of the embodiments of the present invention, a data processing method is provided, including:
[0008] Guiding a large language model by a retrieval agent to extract key information from a natural language text;
[0009] Obtaining, by the retrieval agent, a retrieval result from a pre-stored database, the similarity of which with the key information meets a set similarity condition;
[0010] The disassembly agent guides the large language model to use the retrieval result to disassemble the natural language text, obtaining a corresponding plurality of sub-questions; wherein, the solution of each sub-question depends on the query result of its previous sub-question;
[0011] The disassembly agent guides the large language model to use the retrieval result to generate sub-SQLs corresponding to the respective sub-questions; wherein, each sub-SQL is obtained by conversion based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text.
[0012] In an alternative embodiment, the step of obtaining, by the retrieval agent, a retrieval result from a pre-stored database, the similarity of which with the key information meets a set similarity condition, includes:
[0013] The retrieval agent searches the database for columns corresponding to field values whose similarity with the key information meets the similarity condition, and / or columns corresponding to field descriptions and / or table descriptions whose similarity with the natural language text meets the similarity condition, so as to obtain the retrieval result.
[0014] In an alternative embodiment, the calculation process of the similarity between the key information and the field value includes:
[0015] Calculating the similarity between the key information and the field value by edit distance;
[0016] The calculation process of the similarity between the natural language text and the field description and / or the table description includes:
[0017] Respectively converting the natural language text, and the field description and / or the table description into their respective corresponding vectors through a pre-constructed vector model;
[0018] Calculating the similarity between the vector of the natural language text and the vector of the field description, and / or the similarity between the vector of the natural language text and the vector of the table description through the cosine similarity algorithm.
[0019] In an alternative embodiment, after obtaining the retrieval result, the method further includes:
[0020] The screening agent guides the large language model to filter the retrieval result, obtaining a screening result;
[0021] Correspondingly, in the step of guiding the large language model to disassemble the natural language text by using the retrieval result through the disassembly agent, and in the step of guiding the large language model to generate sub-SQLs corresponding to the multiple sub-questions by using the retrieval result through the disassembly agent, the retrieval result is replaced with the screening result.
[0022] In an alternative embodiment, the step of guiding the large language model to filter the retrieval result through the screening agent to obtain a screening result includes:
[0023] Guiding the large language model to perform column name filtering on the retrieval result according to the natural language text through the screening agent to obtain a preliminary screening result;
[0024] Guiding the large language model to perform table filtering on the preliminary screening result according to the natural language text through the screening agent to obtain a secondary screening result;
[0025] Guiding the large language model to perform column content filtering on the secondary screening result according to the natural language text through the screening agent to obtain the screening result.
[0026] In an alternative embodiment, after obtaining the target SQL, the method further includes:
[0027] Connecting the target SQL to the database through the SQL executor for execution;
[0028] When the SQL executor returns an error message, guiding the large language model to calibrate the target SQL according to the error message and the natural language text through the calibration agent to obtain a calibrated target SQL.
[0029] In an alternative embodiment, the method further includes:
[0030] When the SQL executor returns a query result, outputting the query result.
[0031] In a second aspect of the embodiments of the present invention, a data processing device is provided, including:
[0032] An extraction module configured to: guide a large language model to extract key information in the natural language text through a retrieval agent;
[0033] A retrieval module configured to: obtain a retrieval result that satisfies a set similarity condition with the key information from a pre-stored database through the retrieval agent;
[0034] The disassembling module is configured to: guide the large language model to disassemble the natural language text by using the retrieval result through the disassembling agent to obtain a corresponding plurality of sub-questions; guide the large language model to generate a sub-SQL corresponding to each of the plurality of sub-questions by using the retrieval result through the disassembling agent; wherein, the solution of each sub-question depends on the query result of its previous sub-question, each sub-SQL is converted based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text.
[0035] The query module is configured to: obtain a target query result from the database according to the target SQL.
[0036] In a third aspect of the embodiments of the present invention, there is provided an electronic device, including a processor and a memory, the memory stores machine-executable instructions that can be executed by the processor, and the processor can execute the machine-executable instructions to implement the data processing method provided in the first aspect above.
[0037] In a fourth aspect of the embodiments of the present invention, there is provided a computer-readable storage medium, on which a computer program is stored, and when the computer program is executed by a processor, it implements the data processing method provided in the first aspect above.
[0038] The data processing method, apparatus, device, and storage medium provided by the embodiments of the present invention first use a retrieval agent to guide a large language model to extract key information from the natural language text input by the user, such as keywords. Then, based on the retrieval agent, relevant retrieval results that meet the set similarity condition are retrieved from a pre-stored database, so as to retrieve relevant field values, field descriptions, and table descriptions from the database according to the keywords in the natural language text input by the user, thereby improving the accuracy of the retrieval results. Subsequently, a disassembling agent is used to guide the large language model to disassemble the natural language text using the retrieval results to obtain multiple sub-questions, thereby realizing the disassembly of the natural language text (such as the user's question) into simple sub-questions. Even for complex questions, they can be disassembled into one simple sub-question after another, which is conducive to solving complex questions step by step. And since the solution of each sub-question depends on the query result of the previous sub-question, it is possible to solve the user's question by gradually solving each sub-question, and the solution of the last sub-question is the solution of the user's question. Based on this, the disassembling agent continues to guide the large language model to generate sub-SQLs for each sub-question using the retrieval results, and each sub-SQL is obtained by conversion based on its corresponding sub-question and the previous sub-SQL. The target SQL corresponding to the natural language text is the SQL corresponding to the last sub-question. This way of gradually generating SQL can reduce the amount of text information that needs to be concerned in one SQL generation process compared to directly generating one SQL for the entire natural language text, thereby avoiding inaccurate SQL generation or information omission caused by paying attention to too much text information at one time, and thus significantly improving the accuracy of generating complex SQL. Based on this, in the subsequent process of using the accurate SQL for database query, it is realized that even non-professional users can obtain accurate SQL through natural language questions, and then can quickly obtain accurate query results from the database based on the accurate SQL data, which also reduces the technical threshold for non-professional users to use the database.
[0039] In addition, by using the retrieval agent to intervene in the data processing process of the large model, the large language model can analyze the user's intention more accurately, and thus more accurate retrieval results can be obtained. And by using the disassembling agent to intervene in the data processing process of the large language model, the sub-questions disassembled by the large language model can be more accurate, and thus the accuracy of the SQL can be better improved.
[0040] To make the above objects, features, and advantages of the present invention more obvious and understandable, the following specifically gives preferred embodiments and, in conjunction with the accompanying drawings, makes a detailed description as follows. BRIEF DESCRIPTION OF THE DRAWINGS
[0041] To more clearly illustrate the technical solutions of the embodiments of the present invention, the accompanying drawings required for the embodiments will be briefly introduced below. It should be understood that the following drawings only show some embodiments of the present invention, and therefore should not be regarded as limiting the scope. For those of ordinary skill in the art, without creative efforts, other related drawings can also be obtained based on these drawings.
[0042] Figure 1 Shows a structural block diagram of an electronic device provided by an embodiment of the present invention;
[0043] Figure 2 Shows a flowchart of a data processing method provided by an embodiment of the present invention;
[0044] Figure 3 Shows a schematic diagram of the working process of a retrieval agent provided by an embodiment of the present invention;
[0045] Figure 4 Shows a functional module diagram of a data processing device provided by an embodiment of the present invention. Detailed Embodiments
[0046] Next, the technical solutions in the embodiments of the present invention will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, rather than all embodiments. Usually, the components of the embodiments of the present invention described and shown in the accompanying drawings here can be arranged and designed in various different configurations.
[0047] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the claimed present invention, but merely represents the selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those of ordinary skill in the art without creative efforts belong to the scope of protection of the present invention.
[0048] It should be noted that relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any actual relationship or order between these entities or operations. Moreover, the terms "comprising", "including" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements not only includes those elements, but also includes other elements not expressly listed, or also includes elements inherent to such process, method, article or device. Without further limitation, the element defined by the statement "including one..." does not exclude the existence of other identical elements in the process, method, article or device including the said element.
[0049] To solve the technical problem in the related art that the accuracy of SQL generated based on natural language is relatively low, which in turn affects the precision and efficiency of database queries based on natural language texts, the present invention provides a data processing method. First, a retrieval agent is used to guide a large language model to extract key information, such as keywords, from the natural language text input by the user. Then, based on the retrieval agent, a retrieval result that meets the set similarity condition is retrieved from the pre-stored database, so as to retrieve relevant field values, field descriptions, and table descriptions from the database according to the keywords in the natural language text input by the user, thereby improving the accuracy of the retrieval result. Subsequently, a disassembling agent is used to guide the large language model to disassemble the natural language text using the retrieval result to obtain multiple sub-questions, thereby realizing the disassembly of the natural language text (such as the user's question) into simple sub-questions. Even for complex problems, they can be disassembled into one simple sub-question after another, which is conducive to solving complex problems step by step. And since the solution of each sub-question depends on the query result of the previous sub-question, it is possible to solve the user's problem by gradually solving each sub-question, and the solution of the last sub-question is the solution of the user's problem. Based on this, the disassembling agent continues to guide the large language model to generate sub-SQLs for each sub-question using the retrieval result, and each sub-SQL is obtained by converting based on its corresponding sub-question and the previous sub-SQL. The target SQL corresponding to the natural language text is the SQL corresponding to the last sub-question. This way of gradually generating SQL can reduce the text information that needs to be concerned in one SQL generation process compared to directly generating one SQL for the entire natural language text, and thus can avoid the inaccuracy or information omission of the generated SQL caused by paying attention to too much text information at one time, thereby significantly improving the accuracy of generating complex SQL. Based on this, in the subsequent process of using the accurate SQL for database queries, it is realized that even non-professional users can obtain accurate SQL by asking questions in natural language, and then can quickly obtain accurate query results from the database based on the accurate SQL data, which also reduces the technical threshold for non-professional users to use the database.
[0050] In addition, by using the retrieval agent to intervene in the data processing process of the large model, the large language model can analyze the user's intention more accurately, and thus can obtain more accurate retrieval results. And by using the disassembling agent to intervene in the data processing process of the large language model, the sub-questions disassembled by the large language model can be more accurate, and thus can better improve the accuracy of SQL.
[0051] The data processing method provided by the present invention can be applied to electronic devices. Please refer to Figure 1, is a structural block diagram of an electronic device. The electronic device 100 includes a memory 110, a processor 120, and a communication module 130. Each of the memory 110, the processor 120, and the communication module 130 is electrically connected directly or indirectly to each other to realize data transmission or interaction. For example, these components can be electrically connected to each other through one or more communication buses or signal lines.
[0052] Among them, the memory is used to store programs or data. The memory can be, but is not limited to, a random access memory (RAM), a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), etc.
[0053] The processor is used to read / write the data or programs stored in the memory and execute corresponding functions.
[0054] The communication module is used to establish a communication connection between the electronic device and other communication terminals through a network and is used to send and receive data through the network.
[0055] It should be understood that Figure 1 The structure shown is only a schematic diagram of the structure of the electronic device. The electronic device may also include more or fewer components than those shown in Figure 1 or have a configuration different from that shown in Figure 1 . Figure 1 Each component shown in can be implemented by hardware, software, or a combination thereof.
[0056] The data processing method provided by the embodiments of the present invention can be applied to various fields that need to use SQL to retrieve data from a database, such as the kitchen appliance field, the e-commerce intelligent customer service field, but not limited thereto.
[0057] When the data processing method provided by the embodiments of the present invention is applied in the kitchen appliance field, it can be applied to links such as the research and development, sales, and service of kitchen appliance products. Based on user questions, corresponding target SQLs are generated for data retrieval to provide accurate product information for users and achieve a quick response to user needs.
[0058] In the case where the data processing method provided by the embodiment of the present invention is applied to an e-commerce intelligent customer service, during the process of a user consulting product information or after-sales service, etc., corresponding target SQL can be generated based on the user's question for data retrieval, so as to provide an accurate reply to the user in a timely manner.
[0059] The following will Figure 2 describe the data processing method provided by the embodiment of the present invention. Figure 2 It is a flowchart of a data processing method provided by the embodiment of the present invention. The data processing method includes:
[0060] In step S100, the retrieval agent guides the large language model to extract key information from the natural language text.
[0061] In step S200, the retrieval agent obtains a retrieval result from the pre-stored database, where the similarity between the retrieval result and the key information meets the set similarity condition.
[0062] In step S300, the decomposition agent guides the large language model to decompose the natural language text by using the retrieval result, and obtain corresponding multiple sub-questions; among them, the solution of each sub-question depends on the query result of its previous sub-question.
[0063] In step S400, the decomposition agent guides the large language model to generate sub-SQLs corresponding to the multiple sub-questions by using the retrieval result; among them, each sub-SQL is obtained by converting based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text.
[0064] In the case where it is necessary to obtain the required information by using the database, the user can input a corresponding user question or query statement to the execution entity of the data processing method provided by the embodiment of the present invention, and the user question or query statement can be input in the form of natural language. Based on this, the above execution entity can receive the corresponding natural language text. Since the query of the database usually needs to be based on SQL, after receiving the input natural language text, the above execution entity can execute the above steps S100 to S400 to obtain the target SQL corresponding to the input natural language text.
[0065] During the execution of step S100, the natural language text is input to both the retrieval agent and the large language model, so that the retrieval agent can guide the large language model to extract accurate key information from the natural language text. The key information may include: keywords, key phrases, and named entities in the natural language text, so as to ensure that the extracted key information can guide the large language model to understand the meaning and core composition expressed by the natural language text.
[0066] In some embodiments, the retrieval agent can guide the large language model to extract key information in the form of prompt words. The prompt words may include, but are not limited to: target description, instruction description, keyword definition, key phrase definition, the task performed by the large language model, the definition of the output result of the large model, and examples of key information extraction. The target description is used to inform the large language model of the task objective; the instruction description includes a question reading prompt for guiding the large language model on how to correctly read the question, an analysis prompt for guiding the large language model on how to extract key information, and a prompt for guiding the large language model on what form to output the key information; the keyword definition is used to inform the large language model what a keyword is; the key phrase description is used to inform the large language model what a key phrase is; the task performed by the large language model is used to instruct the large language model to perform key information extraction processing on natural language text.
[0067] As an example, the prompt words input by the retrieval agent to the large language model may include, but are not limited to, the following:
[0068] "Objective: Analyze the given question and prompt, identify and extract keywords, key phrases, and named entities. These elements are crucial for understanding the core components of the query and the guidance provided. This process involves identifying and isolating important terms and phrases that may help formulate a search or query related to the posed question.
[0069] Instruction:
[0070] Read the question carefully: Understand the main focus and specific details of the question. Look for any named entities (such as organizations, locations, etc.), technical terms, and other phrases that contain important aspects of the query.
[0071] Analyze the prompt: The prompt is designed to draw attention to certain elements relevant to answering the question. Extract any keywords, phrases, or named entities that can provide further clarity or guidance for formulating the answer.
[0072] List key phrases and entities: Combine the findings from the question and prompt into a Python list. This list should contain:
[0073] Keywords: Single words that capture the basic aspects of the question or prompt.
[0074] Key phrases: Phrases or named entities that represent specific concepts, locations, organizations, or other important details.
[0075] Ensure to maintain the original wording or terms used in the question and prompt.
[0076] {FEWSHOT_EXAMPLES};
[0077] Task: Given the following question and hint, identify and list all relevant keywords, key phrases, and named entities.
[0078] Question: {QUESTION};
[0079] Hint: {HINT};
[0080] Please provide your findings as a Python list, capturing the essence of the question and hint through the identified terms and phrases. Only output the Python list without explanation.
[0081] In the above example, the content in "{}" is a variable. Among them, FEWSHOT_EXAMPLES in {FEWSHOT_EXAMPLES} represents at least one keyword extraction example provided by the retrieval agent to the large language model so that the large language model can more accurately understand the keyword extraction task. This example can adopt pre-configured relevant examples, but is not limited to this. The content in {QUESTION} is natural language text, for example, the current user question or current user query input by the user. The content in {HINT} is additional information. The additional information can assist the large language model in understanding the natural language text, and thus more accurate keywords can be extracted from the natural language text. However, this additional information may or may not exist; if the additional information exists, it can be the content supplemented by the user after inputting the natural language text; or it can be the content obtained after analyzing and understanding the natural language text through another model.
[0082] The extraction method of the above prompt words and key information can also be expressed by a formula. For example:
[0083] q = f 检索智能体 (Q, K|M)
[0084] In the formula, q represents the key information; Q represents the currently input natural language text; K represents the above additional information; M represents the prompt words input by the retrieval agent to the large language model, such as the above prompt word example. f 检索智能体 (Q, K|M) In these two items of information Q and K, they can be obtained from the given binary tuple X’=(Q, K). This triple can be obtained based on the currently input natural language text, database, and additional information. That is, fill the currently input natural language text into the position of Q in the binary tuple X’, and fill the additional information into the position of K in the binary tuple X’. Thus, through f 检索智能体(Q, K|M), the data in the binary tuple X' can be written to the corresponding position in the prompt and input to the large language model to guide the large language model to utilize its Few-shot ability to extract the corresponding key information. Few-shot means providing a small number of examples for the specific task sentence in the prompt to allow the large language model to understand and execute new tasks through a small number of examples.
[0085] Since the retrieval agent needs to perform database reduction operations based on the key information in addition to the above-mentioned guiding operations for key information extraction, in some embodiments, to facilitate the retrieval agent to obtain the database, reduce the data acquisition steps, and thus improve the data acquisition efficiency, the above binary tuple can be adjusted to a triple that also records database information, that is, the triple X = (Q, S, K), where S represents the database pre-stored in the same field as the problem or query involved in the natural language text, or represents all pre-stored databases. This database can be pre-constructed or an existing database can be used; the meanings of other letters are the same as the relevant records above and will not be elaborated here.
[0086] Thus, during the execution of steps S100 and S200, the retrieval agent can obtain the required data from the same triple without performing multiple lookup and acquisition operations, thereby improving the data acquisition efficiency.
[0087] After extracting the key information from the natural language text, step S200 can be executed, and the key information is input to the retrieval agent so that the retrieval agent can obtain a retrieval result that satisfies the set similarity condition with the key information from the pre-stored database.
[0088] The set similarity condition can refer to that the similarity between the key information and the data in the database is greater than the set similarity threshold, and this similarity threshold can be pre-configured according to experience or experiments or user requirements.
[0089] The above retrieval result can be obtained based on the data retrieval scheme in the related art on the basis of the scheme shown in step S200. However, in order to improve the comprehensiveness of data retrieval while reducing the reference space of the database to avoid ignoring valid data, in some embodiments, the data processing method provided by the embodiments of the present invention also provides another scheme for obtaining the retrieval result, that is, in the above step S200, the step of obtaining a retrieval result that satisfies the set similarity condition with the key information from the pre-stored database through the retrieval agent may include:
[0090] In step S210, the retrieval agent searches in the database for columns corresponding to field values whose similarity to the key information meets the similarity condition, and / or columns corresponding to field descriptions whose similarity to the natural language text meets the similarity condition, and / or tables corresponding to table descriptions, so as to obtain the retrieval result.
[0091] It can be understood that field retrieval and / or context retrieval can be performed based on the key information. The field retrieval operation is the step in step S210 where the retrieval agent searches in the database for columns corresponding to field values whose similarity to the key information meets the similarity condition, while the context retrieval is the step in step S210 where the retrieval agent searches in the database for columns corresponding to field descriptions whose similarity to the natural language text meets the similarity condition and / or tables corresponding to table descriptions.
[0092] For field retrieval, since the key information can be associated with values in the database, the value most similar to the extracted key information can be searched for in the database, and the most relevant field value and the corresponding column name (the column name is also called the field name) are returned for each keyword, each key phrase, and each named entity in the key information. Correspondingly, after the field retrieval operation, the obtained retrieval result may include the corresponding column, and the column includes the field value, the column name, and the column content.
[0093] For context retrieval, in addition to the field values, when the database is built, relevant descriptions of the table may be made, such as field descriptions or table descriptions, and these description information also helps to locate the table and fields most relevant to the user's question or query statement. Correspondingly, after the context retrieval operation, the obtained retrieval result may include at least one of the corresponding column and the table.
[0094] In the process of executing step S210, only field retrieval can be performed, or only context retrieval can be performed. However, in order to obtain a more comprehensive and accurate retrieval result, field retrieval and context retrieval can be performed simultaneously, and the order of field retrieval and context retrieval can be unrestricted, and the two can also be executed in parallel. As an example, for the workflow in which the retrieval agent guides the large language model to extract key information, implements field retrieval and context retrieval, and outputs the retrieval result, reference can be made to Figure 3 , Figure 3 which is a schematic diagram of the workflow of a retrieval agent provided by an embodiment of the present invention.
[0095] Thus, after obtaining the retrieval result through any of the above embodiments, the retrieval result represents part of the data in the database, thereby realizing the reduction of the reference space of the database.
[0096] In addition, for the subsequent application of the retrieval results, the retrieval results can be used as a new database, which can be expressed as S′={T′,C′}, where S′ represents the database obtained after the processing of step S200, T′ represents a table, which can contain multiple tables. For example, T′={T1,T2,…T |t|}; T |t| represents the |t|-th table in T′. C′ represents the columns of the table, which can contain multiple columns. For example, C′={C1,C2,…C |c|}; C |c| represents the |c|-th column in C′.
[0097] The similarity calculation method involved in the above step S210 can be implemented by any one of the related technologies. However, adopting the solutions in the related technologies may exclude languages with different words but similar meanings, or may also exclude languages with spelling mistakes but actually similar meanings, or may include languages with similar meanings due to spelling mistakes, thus affecting the comprehensiveness and accuracy of the retrieval results. Therefore, to solve this technical problem and take into account the retrieval efficiency at the same time, in some embodiments, the data processing method provided by the present invention also provides a similarity calculation scheme, that is, in the above step S210, the calculation process of the similarity between the key information and the field value includes:
[0098] In step S211, calculate the similarity between the key information and the field value through the edit distance.
[0099] Thus, calculating the similarity between the key information and the field value through the edit distance can solve the problems of similar expressions and spelling mistakes in the search, and at the same time take into account the retrieval efficiency.
[0100] In addition, the calculation process of the similarity between the natural language text and the field description and / or the table description includes:
[0101] In step S212, convert the natural language text, as well as the field description and / or the table description, into their respective corresponding vectors through a pre-constructed vector model;
[0102] In step S213, calculate the similarity between the vector of the natural language text and the vector of the field description, and / or the similarity between the vector of the natural language text and the vector of the table description through the cosine similarity algorithm.
[0103] Thus, by using semantic similarity to measure the similarity between the natural language text and the field description and the table description respectively, the retrieval efficiency can be guaranteed to a certain extent.
[0104] In addition, in some embodiments, to improve the accuracy of the retrieval results, after step S213, the calculation process of the similarity between the natural language text and the field description and / or the table description may further include:
[0105] In step S214, a set number of similarities with the largest similarity values are selected from the calculated similarities, and the columns or tables corresponding to the set number of similarities are used as the retrieval results.
[0106] Thus, by selecting the most matching set number of columns or tables as the retrieval results, it is beneficial to ensure the accuracy of the retrieval results.
[0107] In some embodiments, in order to solve the problems of similar expressions and spelling mistakes in the search while taking into account the retrieval efficiency during the calculation process of the similarity between the natural language text and the field description and / or the table description, before supplementing semantics through the cosine similarity algorithm and performing semantic similarity calculation, the corresponding data in the database can be corrected for similar expressions and spelling mistakes through the edit distance first, and then semantic similarity calculation is performed.
[0108] Thus, by first filtering some data through the edit distance and then calculating the similarity of semantic similarity, not only can the problems of similar expressions and spelling mistakes in the search be solved, but also the retrieval efficiency can be improved. For the processing of long texts, the improvement of the retrieval efficiency is more obvious. Furthermore, higher retrieval real-time performance can be achieved, which is beneficial to improving the subsequent SQL generation efficiency and the SQL-based query efficiency, and improving the user experience.
[0109] After obtaining the retrieval results through any of the above embodiments, step S300 can be executed to guide the large language model to disassemble the natural language text by using the retrieval results through the disassembly agent, and obtain corresponding multiple sub-problems.
[0110] Among them, the relationship between the above multiple sub-problems is: the solution of each sub-problem depends on the query result of its previous sub-problem. It can be understood that except for the first sub-problem, the solution of other sub-problems depends on the query result of the previous sub-problem, and the query result can refer to the associated result found from the above retrieval results based on the SQL corresponding to the corresponding sub-problem. For the convenience of understanding, an example is given below for illustration:
[0111] Suppose the currently input natural language text is: "List the names of specific schools with SAT excellent rates exceeding the average level", where SAT is the full name of Scholastic Assessment Test, and its Chinese meaning is Academic Ability Assessment Test. Based on this, suppose the corresponding retrieval results include: tables or columns containing specific school names, and tables or columns containing excellent rates.
[0112] Based on the above example, after the decomposition operation in step S300, the sub-problems obtained can be:
[0113] Sub-problem 1: Obtain the average value of the SAT excellent rate of specific schools.
[0114] Sub-problem 2: List the names of specific schools whose SAT excellent rate exceeds the average level.
[0115] It can be seen that to solve sub-problem 2, sub-problem 1 needs to be solved first. The solution of sub-problem 1 can be obtained by querying the corresponding data from the retrieval results of the table or column containing the names of specific schools and the table or column containing the excellent rate based on the SQL corresponding to sub-problem 1. That is, the solution of sub-problem 2 depends on the query result of sub-problem 1. In this way, a complex problem that needs to solve multiple pieces of information can be decomposed into multiple sub-problems that progress step by step, so that each sub-problem can use the query result of the previous sub-problem, and each sub-problem only needs to focus on the solution of one or a small amount of information, thus ensuring that the problem can be solved comprehensively and accurately.
[0116] To improve the generation efficiency of SQL, in the process of decomposing natural language text, step S400 can be executed in parallel to guide the large language model to generate the sub-SQL corresponding to each sub-problem by using the retrieval results through the decomposition agent. It can be understood that after each sub-problem is decomposed, the sub-SQL corresponding to that sub-problem can be generated. Except for the first sub-problem, the sub-SQL of each other sub-problem is obtained by converting based on its corresponding sub-problem and the previous sub-SQL, so that each sub-SQL can use the previous sub-SQL for generation. This way of gradually generating SQL can ensure the accuracy of the sub-SQL corresponding to the last sub-problem. And since the solution of the last sub-problem can also solve the requirements of the user problem or query statement, therefore, the sub-SQL corresponding to the last sub-problem is the target SQL corresponding to the natural language text. Based on this target SQL, the corresponding information can be accurately queried from the retrieval results and output to answer the user query requirements in the user problem or query statement.
[0117] Next, continue to use the previous example to illustrate the relationship between sub-problems, sub-SQL, and adjacent sub-SQL:
[0118] Sub - problem 1: Obtain the average value of the SAT excellent rate for a specific school. SQL: "SELECT AVG(CAST(T2.'NumGE1500' AS REAL) / T2.'NumTstTakr') FROM frpm AS T1 INNER JOIN satscores AS T2 ON T1.'CDSCode' = T2.'cds' WHERE T1.'Charter School(Y / N)' = 1".
[0119] Sub - problem 2: List the names of specific schools whose SAT excellent rate exceeds the average level. SQL: "SELECT T2.'sname' FROM frpm AS T1 INNER JOIN satscores AS T2 ON T1.'CDSCode' = T2.'cds' WHERE T2.'sname' IS NOT NULL AND T1.'Charter School(Y / N)' = 1 AND CAST(T2.'NumGE1500' AS REAL) / T2.'NumTstTakr' > (SELECT AVG(CAST(T4.'NumGE1500' AS REAL) / T4.'NumTstTakr') FROM frpm AS T3 INNER JOIN satscores AS T4 ON T3.'CDSCode' = T4.'cds' WHERE T3.'Charter School(Y / N)' = 1)".
[0120] As can be seen from the above example, the sub - SQL corresponding to sub - problem 2 is obtained by transformation based on the SQL corresponding to sub - problem 1 and sub - problem 2. The transformation principle and the meanings expressed by each SQL in the above example can be known to those skilled in the art based on all the content recorded in this article and relevant knowledge in this field, and will not be elaborated here.
[0121] In addition, since the last sub - problem covering the user's question is sub - problem 2, the sub - SQL corresponding to sub - problem 2 is the target SQL corresponding to the user's question.
[0122] In some embodiments, the problem - disassembling agent can guide the large - language model to disassemble natural - language text and generate SQL in the form of prompt words. The prompt words can include, but are not limited to: a task description for informing the large - language model of the task content, a task - execution description for guiding the large - language model to query the retrieval results according to the disassembled sub - problems and obtain the query results, a database - schema description for informing the large - language model of the database schema, and examples of problem disassembling and SQL generation.
[0123] As an example, the prompt words input by the problem decomposition agent to the large language model may include but are not limited to the following:
[0124] "Given a [database schema] description, a knowledge [evidence], and [question], you need to use valid SQLite and understand the database and knowledge, and then break down the question into sub-questions to generate text-to-SQL. When generating SQL, we should always consider the constraints:
[0125] [Constraints]
[0126] In 'SELECT <column>In it, only select the columns required in [sub-question], without any unnecessary columns or values;
[0127] In 'FROM ’ or ‘JOIN
[0128] ’, do not include unnecessary tables; if using the max or min functions, ‘JOIN <column>)' or 'SELECT MIN( <column>)’;
[0129] If <column>’s [value example] has ‘None’ or None, use ‘JOIN< / column> < / column> < / column> ’ first, then use ‘SELECT MAX( <column>‘IS NOT NULL’ is better;
[0130] If using ‘ORDER BY <column>For 'ASC|DESC', please add 'GROUP BY' before it <column>’ to select different values;
[0131] ================
[0132] [Database Schema]
[0133] table: frpm
[0135] (CDSCode, CDSCode. Value examples: ['01100170109835', '01100170112607'].)
[0136] (Charter School (Y / N), Charter School (Y / N). Value examples: [1, 0, None]. And 0: N; 1: Y)
[0137] (Enrollment (Ages 5 - 17), Enrollment (Ages 5 - 17). Value examples: [5271.0, 4734.0].)
[0138] (Free Meal Count (Ages 5 - 17), Free Meal Count (Ages 5 - 17). Value examples: [3864.0, 2637.0]. And qualified free rate = Free Meal Count / Enrollment)
[0140] table: satscores
[0142] (cds, school. Value examples: ['10101080000000', '10101080109991'].)
[0143] (sname, school name. Value examples: ['None', 'Middle College High', 'John F. Kennedy High', 'Independence High', 'Foothill High'].)
[0144] (NumTstTakr, the number of people taking the test in this school. Value examples: [24305, 4942, 1, 0, 280]. And the number of people taking the test in each school)
[0145] (AvgScrMath, average math score. Value examples: [699, 698, 289, None, 492]. And average math score)
[0146] (NumGE1500, the number of test takers with an SAT total score of 1500 or more. Value examples: [5837, 2125, 0, None, 191]. And the number of test takers with an SAT total score of 1500 or more. Common sense evidence: Excellent rate = NumGE1500 / NumTstTakr)
[0148] [Foreign key]
[0149] frpm.‘CDSCode’ = satscores.‘cds’;
[0150] [Question]
[0151] List the names of specific schools where the SAT excellent rate exceeds the average level.
[0152] [Evidence]
[0153] Specific schools refer to 'Charter School(Y / N)' = 1 in the frpm table; Excellent rate = NumGE1500 / NumTstTakr;
[0154] Considering [constraints], break down the problem into sub - problems and generate SQL after step - by - step thinking:
[0155] Sub - problem 1: Get the average SAT excellent rate of specific schools. SQL "SELECT AVG(CAST(T2.'NumGE1500' AS REAL) / T2.'NumTstTakr') FROM frpm AS T1 INNER JOIN satscores AS T2 ON T1.'CDSCode' = T2.'cds' WHERE T1.'Charter School(Y / N)' = 1";
[0156] Sub - problem 2: List the names of specific schools where the SAT excellent rate exceeds the average level. SQL "SELECT T2.'sname' FROM frpm AS T1 INNER JOIN satscores AS T2 ON T1.'CDSCode'=T2.'cds' WHERE T2.'sname' IS NOT NULL AND T1.'Charter School(Y / N)'=1 AND CAST(T2.'NumGE1500' AS REAL) / T2.'NumTstTakr' > (SELECT AVG(CAST(T4.'NumGE1500' AS REAL) / T4.'NumTstTakr') FROM frpm AS T3 INNER JOIN satscores AS T4 ON T3.'CDSCode'=T4.'cds' WHERE T3.'Charter School(Y / N)'=1)"
[0157] Problem solved.
[0158] ==========
[0159] [Database Schema]
[0160] table: account
[0162] (account_id, ID of the account. Value examples: [11382, 11362, 2, 1, 2367].),
[0163] (district_id, location of the branch. Value examples: [77, 76, 2, 1, 39].),
[0164] (frequency, frequency of the account. Value examples: ['POPLATEK MESICNE', 'POPLATEK TYDNE', 'POPLATEK PO OBRATU'].),
[0165] (date, creation date of the account. Value examples: ['1997 - 12 - 29', '1997 - 12 - 28'].
[0167] table: client
[0169] (client_id, unique number. Value examples: [13998, 13971, 2, 1, 2839].),
[0170] (gender, gender. Value examples: ['M', 'F']. And F: female. M: male),
[0171] (birth_date, date of birth. Value examples: ['1987-09-27', '1986-08-13'].),
[0172] (district_id, location of the branch. Value examples: [77, 76, 2, 1, 39].)
[0174] table: district
[0176] (district_id, location of the branch. Value examples: [77, 76, 2, 1, 39].),
[0177] (A4, number of residents. Value examples: ['95907', '95616', '94812'].),
[0178] (A11, average salary. Value examples: [12541, 11277, 8114].)
[0180] [Foreign key]
[0181] account.'district_id' = district.'district_id'
[0182] client.'district_id' = district.'district_id'
[0183] [Question]
[0184] What is the gender of the youngest client who opened an account at the branch with the lowest average salary?
[0185] [Evidence]
[0186] A later birth date indicates a younger age; A11 refers to the average salary
[0187] Break the question into sub-questions, consider [constraints], and generate SQL after thinking step by step:
[0188] Sub-question 1: What is the district_id of the branch with the lowest average salary? SQL "SELECT 'district_id' FROM district ORDER BY 'A11' ASC LIMIT 1";
[0189] Sub-question 2: Who is the youngest client who opened an account at the branch with the lowest average salary? SQL "SELECT T1.'client_id' FROM client AS T1 INNER JOIN district AS T2 ON T1.'district_id' = T2.'district_id' ORDER BY T2.'A11' ASC, T1.'birth_date' DESC LIMIT 1";
[0190] Sub-question 3: What is the gender of the youngest client who opened an account at the branch with the lowest average salary? SQL "SELECT T1.'gender' FROM client AS T1 INNER JOIN district AS T2 ON T1.'district_id' = T2.'district_id' ORDER BY T2.'A11' ASC, T1.'birth_date' DESC LIMIT 1";
[0191] Problem solved.
[0192] ================
[0193] [Database Schema] {desc_str}
[0194] [Foreign Key] {fk_str}
[0195] [Question] {query}
[0196] [Evidence] {evidence}
[0197] Decompose the problem into sub-questions, consider [Constraints], and generate SQL after step-by-step thinking.
[0198] Thus, the problem decomposition agent can guide the large language model to utilize its in-context learning ability to gradually decompose the natural language text into multiple sub-questions based on the retrieval results and generate the corresponding sub-SQLs for each sub-question by inputting the current input natural language text, the above retrieval results, and the prompt words in the above examples, and then obtain the target SQL.
[0199] Through the above steps S300 and S400, a complex task can be decomposed into simpler subtasks, making the instruction compliance of the subtasks higher. That is, if a complex natural language text is directly converted into a complex SQL statement, it is often not easy to succeed. However, through the above solution provided by the embodiments of the present invention, the complexity of the natural language text can be evaluated in the prompt. After evaluation, for a natural language text evaluated as simple, the corresponding SQL statement can be directly generated; for a natural language text evaluated as complex, it can be decomposed into multiple simpler sub-problems to gradually generate the final SQL statement, which can significantly increase the accuracy of the generated complex SQL.
[0200] To further narrow the range of tables and columns referred to when generating SQL, to further improve the SQL generation efficiency, and to reduce interference from irrelevant data to further improve the accuracy of SQL, in some embodiments, the embodiments of the present invention provide a solution for filtering the retrieval results obtained in step S200. That is, after obtaining the retrieval results, the data processing method provided by the embodiments of the present invention may further include:
[0201] In step S3000, the large language model is guided by the filtering agent to filter the retrieval results to obtain the filtering results.
[0202] Correspondingly, in the above step 300, the retrieval results in the step of guiding the large language model to disassemble the natural language text using the retrieval results by the disassembly agent, and in the above step S400, the retrieval results in the step of guiding the large language model to generate sub-SQLs corresponding to the respective sub-problems using the retrieval results by the disassembly agent are both replaced with the filtering results in step S3000.
[0203] The main function of the filtering agent is to further narrow the range of columns and tables referred to when generating SQL based on the retrieval results output by the retrieval agent, so as to filter out the most relevant tables and columns, and the most relevant tables and columns are the filtering results. Therefore, using fewer most relevant tables and columns can improve the SQL generation efficiency, and there is less interference in the most relevant tables and columns, thereby further improving the accuracy of the generated SQL.
[0204] In some embodiments, the filtering agent can guide the large language model to filter the retrieval results in the form of prompts. The prompts may include, but are not limited to: a task description for informing the large language model of the task content, a database schema description for informing the large language model of the database schema, a task execution description for guiding the large language model to filter the retrieval results according to the natural language text to obtain the filtering results, and a filtering result output description for indicating the form in which the large language model outputs the filtering results.
[0205] Based on this, the screening results obtained in the above step S3000 and the related technical principles can be expressed by the following formula:
[0206] S * = f 筛选智能体 (Q, S′, K|M′)
[0207] In the formula, S * represents the screening result, M′ represents the prompt words input by the screening agent to the large language model, and the meanings of other letters can be seen in the relevant records above and will not be elaborated here. Thus, through f 筛选智能体 (Q, S′, K|M′), information such as Q, S′, and K can be written into the corresponding positions in the prompt words and input to the large language model to guide the large language model to filter the retrieval results to obtain the screening results.
[0208] During the process of the large language model filtering the retrieval results, at least one filtering operation of column name filtering, table filtering, or column filtering can be implemented according to the prompt words of the screening agent. It can be understood that the screening agent can include at least one of the three parts of column name filtering, table filtering, or column filtering.
[0209] In some embodiments, to better improve the generation efficiency and accuracy of SQL, the screening agent can include three parts: column name filtering, table filtering, and column filtering. Correspondingly, in the above step S3000, the step of guiding the large language model to filter the retrieval results through the screening agent to obtain the screening results can include:
[0210] In step S3100, guide the large language model to perform column name filtering on the retrieval results according to the natural language text through the screening agent to obtain a preliminary screening result;
[0211] In step S3200, guide the large language model to perform table filtering on the preliminary screening result according to the natural language text through the screening agent to obtain a secondary screening result;
[0212] In step S3300, guide the large language model to perform column content filtering on the secondary screening result according to the natural language text through the screening agent to obtain the screening result.
[0213] Since the retrieval results obtained by the retrieval agent may contain a large number of columns, and these columns may be semantically irrelevant to the currently input natural language text, it is necessary to filter out the irrelevant columns. In this regard, the embodiments of the present invention regard the relevance between the columns of the data table and the natural language text as a binary classification task, and use the large language model to distinguish whether the natural language text is relevant to the column, that is, use the large language model to complete this binary classification task.
[0214] During the execution of step S3100, the screening agent can guide the large language model to complete the above binary classification task by means of prompt words, so as to perform column name filtering on the retrieval results and obtain the preliminary screening results. The prompt words can be referred to the relevant records above and will not be elaborated here.
[0215] As an example, the prompt words for the screening agent to implement column name filtering may include but are not limited to:
[0216] "You are an expert and a very smart data analysis expert. Your task is to examine the provided database schema, understand the question posed, and use the prompts to identify the specific columns in the table that are crucial for formulating an SQL query to answer the question.
[0217] Overview of the database schema: {DATABASE_SCHEMA}
[0218] This schema provides a detailed description of the database architecture, specifying tables, columns, primary keys, foreign keys, and any relevant information about relationships or constraints. Special attention should be paid to the examples listed next to each column, as they directly imply the columns relevant to our query. For the keyword phrases mentioned in the question, we provide the values most similar to those in the columns indicated by "– example" before the corresponding column names. This is the key prompt for identifying the columns to be used in the SQL query.
[0219] Question: {QUESTION}
[0220] Hint: {HINT}
[0221] The hint is designed to draw your attention to the specific elements in the database architecture that are crucial for answering the question effectively.
[0222] Task: Based on the provided database schema, question, and hint, your task is to identify all and only the columns necessary to formulate an SQL query to answer the question. For each selected column, explain why it is necessary to answer the question. Your reasoning should be concise and clear, demonstrating the logical connection between the column and the question posed.
[0223] Hint: If you select a column to filter the values in that column, make sure the column has values as examples.
[0224] Please respond with a JSON object in the following structure:
[0225] {
[0226] "chain_of_thought_reasoning": "Your reasoning for selecting the column should be concise and clear.",
[0227] "table_name1": ["column1", "column2",...],
[0228] "table_name2": ["column1", "column2",...], ...
[0230] }
[0231] Ensure that your response includes the table names as keys, and each key is associated with a list of column names that are necessary for writing SQL queries to answer the question. For each aspect of the question, provide a clear and concise explanation of the reasoning behind your choice of columns. Take a deep breath and think logically.
[0232] Only output JSON as your return.
[0233] Similarly, some characters, instructions, and descriptions involved in the above prompt words can be obtained based on the content recorded in the present invention and relevant knowledge in the art, and will not be elaborated here.
[0234] Thus, the screening agent can input the above prompt words into the large language model, and use the large language model to filter the column names of the retrieval results based on the natural language text, thereby obtaining the preliminary screening results.
[0235] Since the above binary classification task can only filter out the obviously irrelevant columns and fails to filter the irrelevant tables in the retrieval results, in order to obtain more relevant results, after obtaining the preliminary screening results, step S3200 can be executed to further filter the preliminary screening results by guiding the large language model according to the natural language text through the screening agent, so as to obtain the secondary screening results.
[0236] During the execution of step S3200, the screening agent can guide the large language model to evaluate the relevance between each table in the preliminary screening results and the natural language text in the form of prompt words, so as to filter the tables in the preliminary screening results and obtain the secondary screening results. The prompt words can be referred to the relevant records above and will not be elaborated here.
[0237] As an example, the prompt words for the screening agent to implement table filtering may include but are not limited to:
[0238] "You are an expert and a very smart data analyst. Your task is to analyze the provided database schema, understand the question raised, and use the prompts to determine the tables required to generate SQL queries to answer the question.
[0239] Database schema overview: {DATABASE_SCHEMA}
[0240] This mode provides a detailed definition of the database structure, including tables, their columns, primary keys, foreign keys, and any relevant details about relationships or constraints. For the keyword phrases mentioned in the question, we provide the values that are most similar to those in the columns indicated by "– Example" before the corresponding column names. This is a key hint for identifying the tables to be used in SQL queries.
[0241] Question: {QUESTION}
[0242] Hint: {HINT}
[0243] The hint is designed to draw your attention to specific elements in the database schema that are crucial for answering the question effectively.
[0244] Task: Given the provided database schema, question, and hint, your task is to determine the tables to be used in formulating the SQL query. For each selected table, explain why it is necessary for answering the question. Your explanation should be logically clear and concise, demonstrating a clear understanding of the database schema, question, and hint.
[0245] Please respond with a JSON object in the following structure:
[0246] {
[0247] "chain_of_thought_reasoning": "Logical analysis explanation leading to the selection of the table.",
[0248] "table_names": ["Table1", "Table2", "Table3",... ]
[0249] }
[0250] Note that you should select all and only the tables necessary for writing a valid SQL query to answer the question. Take a deep breath and think logically.
[0251] Only output JSON as your return.
[0252] Similarly, some characters, instructions, and descriptions involved in the above prompt words can be obtained based on the content recorded in the present invention and relevant knowledge in the art, and will not be elaborated here.
[0253] Thus, the screening agent can input the above prompt words into the large language model and use the large language model to perform table filtering on the preliminary screening results based on the natural language text, thereby obtaining the secondary screening results.
[0254] By executing step S3200, relatively accurate tables and some columns required for generating SQL can be obtained. However, to obtain more accurate relevant tables and columns and better eliminate irrelevant columns to further reduce interference. After obtaining the second screening result, step S3300 can be continued to further filter the column content of the second screening result by the screening agent guiding the large language model according to the natural language text to obtain the screening result.
[0255] During the execution of step S3300, the screening agent guides the large language model to evaluate the necessity of columns in the table. The necessity of a column can be understood as whether the column must be used to generate SQL, and relevant explanations for the column are given. This is essentially a chain of thought (CoT) screening method. Similarly, the screening agent can guide the large language model to complete column content filtering through the above chain of thought screening method by means of prompt words. The prompt words can be referred to the relevant records above and will not be elaborated here.
[0256] As an example, the prompt words for the screening agent to achieve column content filtering may include but are not limited to:
[0257] "You are a detail-oriented data scientist, and your task is to evaluate the relevance of database column information to answering specific SQL query questions based on the provided prompts. Your goal is to evaluate whether the given column content (column details) is relevant to constructing an SQL query to answer the question informed by the prompt. If the column content helps in query formulation, mark it as "relevant", and if not, mark it as "irrelevant".
[0258] Procedure:
[0259] 1. Carefully check the provided column content.
[0260] 2. Understand the question about the database and its related prompts.
[0261] 3. Based on your analysis, decide whether the column content is necessary for the SQL query.
[0262] The following are examples of how to determine whether the column content is relevant or irrelevant to the question and prompt:
[0263] {FEWSHOT_EXAMPLES}
[0264] Now it's your turn to determine whether the provided column content helps in formulating an SQL query to answer the given question based on the provided prompts.
[0265] Please be sure to follow the following very important guidelines. Before making a decision, please carefully check each item:
[0266] 1. You only got the content of one column, which is not sufficient to answer the complete query by itself. Focus only on the data provided here and evaluate its relevance to the question and the hint, without considering any missing information.
[0267] 2. Carefully read the column information and understand its description, then see if the question or hint asks about or refers to the same information. If so, the column content is relevant; otherwise, it is not.
[0268] Column content: {COLUMN_PROFILE}
[0269] Question: {QUESTION}
[0270] Hint: {HINT}
[0271] Take a deep breath and provide your answer in the following JSON format:
[0272] {
[0273] "chain_of_thought_reasoning": "One line explaining why the column content is relevant or irrelevant to the question and the hint.",
[0274] "is_column_information_relevant": "Yes" or "No"
[0275] }
[0276] Only output JSON as your response.
[0277] Similarly, some characters, instructions, and descriptions involved in the above prompt words can be obtained based on the content recorded in the present invention and relevant knowledge in the art, and will not be elaborated here.
[0278] Thus, the screening agent can input the above prompt words into the large language model, and use the large language model to filter the column content of the second screening result based on the natural language text, so as to obtain the screening result - the table and column most relevant to the natural language text, and the number of the most relevant table and column can be unlimited.
[0279] It can be seen that by using the retrieval agent and the screening agent to perform screening based on key information, binary classification screening, and chain of thought screening, more accurate screening results can be obtained, which further makes the sub-questions obtained by disassembling the problem based on the screening results more effective and accurate, and also makes the SQL generated based on the screening results and sub-questions more accurate.
[0280] After obtaining the target SQL through any of the above embodiments, the target SQL can be connected to the database through an SQL executor for execution to obtain corresponding query results, which can be output to the user terminal to implement a response to the user's question or query statement. That is, after obtaining the target SQL, the data processing method provided by the embodiments of the present invention may further include:
[0281] In step S500, connect the target SQL to the database through an SQL executor for execution;
[0282] In step S610, when the SQL executor returns a query result, output the query result.
[0283] However, during the execution of the target SQL, it is possible to encounter execution errors. For example, it is possible that due to basic syntax errors in the target SQL, problems may occur during the execution of the SQL by the executor, and corresponding query results cannot be returned, thereby unable to reply to the user's question or query statement, affecting the user experience. To solve these technical problems, in some embodiments, the data processing method provided by the embodiments of the present invention also provides a solution for autonomously calibrating the target SQL. That is, after obtaining the target SQL, the data processing method provided by the embodiments of the present invention may further include:
[0284] In step S500, connect the target SQL to the database through an SQL executor for execution;
[0285] In step S620, when the SQL executor returns an error message, guide the large language model to calibrate the target SQL according to the error message and the natural language text through a calibration agent to obtain a calibrated target SQL.
[0286] It can be understood that the main function of the calibration agent is to detect and automatically correct incorrect SQL.
[0287] In some embodiments, the calibration agent can guide the large language model to calibrate the existing target SQL by means of prompt words. The prompt words may include, but are not limited to: a task description for informing the large language model of the task content, a task execution description for guiding the large language model to calibrate the incorrect target SQL according to the natural language text and the error message, and an output description for instructing the large language model to output the calibrated target SQL.
[0288] As an example, the prompt words adopted by the calibration agent to calibrate the incorrect target SQL may include, but are not limited to:
[0289] "[Instruction]
[0290] When executing the following SQL, some errors occurred. Please correct the SQL based on the query and database information. If necessary, solve the problem step by step. Use the SQL format in the code block and indicate the script type in the code block. When you find the answer, please carefully verify it. If possible, include verifiable evidence in your answer.
[0291] [Constraint]
[0292] — In 'SELECT <column>In [the problem], only select the columns required in [the problem], without any unnecessary columns or values;
[0293] — In 'FROM< / column> < / column> < / column> < / column> ’ or ‘WHERE ’ or ‘JOIN
[0294] ’, do not include unnecessary tables; — if using the max or min functions, ‘JOIN <column>)' or 'SELECT MIN( <column>)’;
[0295] — If <column>’s [value example] has ‘None’ or None, use ‘JOIN< / column> < / column> < / column> ’ first, then use ‘SELECT MAX( <column>'IS NOT NULL' is better;
[0296] — If using 'ORDER BY <column>For 'ASC|DESC', please add 'GROUP BY' before it <column>’ to select different values;
[0297] [Query]
[0298] {query}
[0299] [Evidence]
[0300] {evidence}
[0301] [Database Information]
[0302] {desc_str}
[0303] [Foreign Key]
[0304] {fk_str}
[0305] [Old SQL]
[0306] "sql{sql}"
[0307] [SQLite Error]
[0308] {sqlite_error}
[0309] [Exception Class]
[0310] {exception_class}
[0311] Now, please correct the old SQL and generate the new SQL again.
[0312] [Correct SQL]”
[0313] Thus, the error information, natural language text, incorrect target SQL, and the above prompt words for calibrating the intelligent agent can be input into the large language model to guide the large language model to calibrate the target SQL based on the error information and natural language text to obtain the calibrated target SQL. Thereby, the basic syntax errors in the generated SQL statements can be significantly reduced, and the execution of the generated SQL statements can also be ensured, achieving autonomous calibration.
[0314] In an embodiment of combining and applying the retrieval intelligent agent, screening intelligent agent, question decomposition intelligent agent, and calibration intelligent agent, multi-intelligent agents can be utilized for cooperation, thereby enabling the large language model to more accurately understand the user's intention, effectively improving the conversion efficiency and accuracy from natural language to SQL, and also improving the executability of SQL.
[0315] The large language model adopted above is not limited in the embodiments of the present invention. The data processing method provided by the embodiments of the present invention can be applied to various large language models, including but not limited to: GPT4-o, Qwen, and DeepSeek.
[0316] It should be noted that the technical features or technical solutions in any of the above embodiments of the present invention can be combined with each other as long as there is no contradiction in the combination.
[0317] To execute the corresponding steps in the above embodiments and various possible ways, an implementation manner of a data processing device is given below. Optionally, the data processing device may adopt the device structure of the electronic device shown above. Further, please refer to Figure 1 which Figure 4 is Figure 4 a functional module diagram of a data processing device provided by an embodiment of the present invention. It should be noted that for the data processing device provided in this embodiment, its basic principle and the technical effects generated are the same as those in the above embodiments. For the sake of brief description, for the parts not mentioned in this embodiment, reference may be made to the corresponding content in the above embodiments. The data processing device 400 includes:
[0318] An extraction module 410, configured to: extract key information in the natural language text through a retrieval agent guiding a large language model;
[0319] A retrieval module 420, configured to: obtain a retrieval result whose similarity to the key information meets a set similarity condition from a pre-stored database through the retrieval agent;
[0320] A disassembling module 440, configured to: disassemble the natural language text by guiding the large language model using the retrieval result through a disassembly agent to obtain a corresponding plurality of sub-questions; guide the large language model to generate sub-SQLs corresponding to the respective sub-questions by using the retrieval result through the disassembly agent; wherein the solution of each sub-question depends on the query result of its previous sub-question, each sub-SQL is obtained by conversion based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text;
[0321] A query module 450, configured to: obtain a target query result from the database according to the target SQL.
[0322] In some embodiments, the process of the retrieval module 420 obtaining a retrieval result whose similarity to the key information meets a set similarity condition from a pre-stored database through the retrieval agent is configured to:
[0323] Search for columns corresponding to field values whose similarity to the key information meets the similarity condition, and / or columns corresponding to field descriptions whose similarity to the natural language text meets the similarity condition, and / or tables corresponding to table descriptions in the database through the retrieval agent to obtain the retrieval result.
[0324] In some embodiments, the calculation process of the retrieval module 420 for calculating the similarity between the key information and the field value is configured to:
[0325] Calculate the similarity between the key information and the field value through the edit distance;
[0326] The calculation process of the similarity between the natural language text and the field description and / or the table description includes:
[0327] Convert the natural language text, as well as the field description and / or the table description, into their respective corresponding vectors through a pre-constructed vector model;
[0328] Calculate the similarity between the vector of the natural language text and the vector of the field description, and / or the similarity between the vector of the natural language text and the vector of the table description through the cosine similarity algorithm.
[0329] In some embodiments, the data processing device 400 provided by the embodiments of the present invention may further include:
[0330] A screening module 430, configured to: after the retrieval module 420 obtains the retrieval result, guide the large language model to filter the retrieval result through a screening agent to obtain a screening result;
[0331] Correspondingly, during the process of the disassembling module 440 guiding the large language model to disassemble the natural language text by using the retrieval result through a disassembling agent, and during the process of guiding the large language model to generate the sub-SQL corresponding to each of the multiple sub-questions by using the retrieval result through a disassembling agent, the retrieval result used is replaced with the screening result.
[0332] In some embodiments, the process of the screening module 430 guiding the large language model to filter the retrieval result through a screening agent to obtain a screening result is configured to:
[0333] Guide the large language model to perform column name filtering on the retrieval result according to the natural language text through a screening agent to obtain a preliminary screening result;
[0334] Guide the large language model to perform table filtering on the preliminary screening result according to the natural language text through a screening agent to obtain a secondary screening result;
[0335] Guide the large language model to perform column content filtering on the secondary screening result according to the natural language text through a screening agent to obtain the screening result.
[0336] In some embodiments, the process of the query module 450 acquiring the target query result from the database according to the target SQL is configured as follows:
[0337] Connect the target SQL to the database for execution through the SQL executor;
[0338] When the SQL executor returns a query result, the query result is output.
[0339] In some embodiments, the query module 450 is further configured to:
[0340] When the SQL executor returns an error message, the calibration agent instructs the large language model to calibrate the target SQL according to the error message and the natural language text to obtain the calibrated target SQL.
[0341] Optionally, the above modules can be stored in the form of software or firmware. Figure 1 The memory shown in the figure or solidified in the operating system (OS) of the electronic device, and can be Figure 1 Meanwhile, the data and program codes required for executing the above modules can be stored in the memory.
[0342] In several embodiments provided in the present application, it should be understood that the disclosed devices and methods can also be implemented in other ways. The device embodiments described above are merely schematic. For example, the flowcharts and block diagrams in the accompanying drawings show the possible architecture, functions and operations of the devices, methods and computer program products according to multiple embodiments of the present invention. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of a code, and the module, a program segment or a part of a code contains one or more executable instructions for implementing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flowchart, and the combination of boxes in the block diagram and / or flowchart can be implemented with a dedicated hardware-based system that performs a specified function or action, or can be implemented with a combination of dedicated hardware and computer instructions.
[0343] In addition, the functional modules in the various embodiments of the present invention may be integrated together to form an independent part, or each module may exist independently, or two or more modules may be integrated to form an independent part.
[0344] When the described function is implemented in the form of a software functional module and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or a part of this technical solution can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions for causing a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the steps of the methods described in various embodiments of the present invention. The aforementioned storage medium includes: various media such as USB flash drives, mobile hard disks, read-only memories (ROM, Read-Only Memory), random access memories (RAM, Random Access Memory), magnetic disks, or optical discs that can store program codes.
[0345] The foregoing are only the preferred embodiments of the present invention and are not used to limit the present invention. For those skilled in the art, the present invention can have various changes and modifications. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention shall be included within the protection scope of the present invention.< / column> < / column> < / column> ’ or ‘WHERE < / column>
Claims
1. A data processing method, characterized in that Including: Guided by a retrieval agent, a large language model extracts key information from natural language text; Through the retrieval agent, retrieve results that meet a set similarity condition with the key information are obtained from a pre-stored database; Guided by a disassembling agent, the large language model disassembles the natural language text using the retrieval results to obtain corresponding multiple sub-questions; among them, the solution of each sub-question depends on the query result of its previous sub-question; Guided by the disassembling agent, the large language model generates sub-SQLs corresponding to the multiple sub-questions respectively using the retrieval results; among them, each sub-SQL is converted based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text.
2. The method according to claim 1, characterized in that, The step of obtaining, through the retrieval agent, retrieve results that meet a set similarity condition with the key information from a pre-stored database includes: Through the retrieval agent, columns corresponding to field values whose similarity with the key information meets the similarity condition, and / or columns corresponding to field descriptions whose similarity with the natural language text meets the similarity condition, and / or tables corresponding to table descriptions are searched in the database to obtain the retrieval results.
3. The method according to claim 2, wherein The calculation process of the similarity between the key information and the field value includes: Calculating the similarity between the key information and the field value through the edit distance; The calculation process of the similarity between the natural language text and the field description and / or the table description includes: Converting the natural language text, as well as the field description and / or the table description into their respective corresponding vectors through a pre-constructed vector model; Calculating the similarity between the vector of the natural language text and the vector of the field description, and / or the similarity between the vector of the natural language text and the vector of the table description through the cosine similarity algorithm.
4. The method according to any one of claims 1 to 3, characterized in that After obtaining the retrieval results, the method further includes: Guided by a screening agent, the large language model filters the retrieval results to obtain a screening result; Correspondingly, the retrieval results in the step of guiding the large language model to disassemble the natural language text using the retrieval results by the disassembling agent, and the step of guiding the large language model to generate sub-SQLs corresponding to the multiple sub-questions respectively using the retrieval results by the disassembling agent are replaced with the screening result.
5. The method according to claim 4, wherein The step of guiding the large language model to filter the retrieval results by the screening agent to obtain a screening result includes: Guided by the screening agent, the large language model filters the column names of the retrieval results according to the natural language text to obtain a preliminary screening result; Guided by the screening agent, the large language model filters the preliminary screening result according to the natural language text to obtain a secondary screening result; Guided by the screening agent, the large language model filters the column contents of the secondary screening result according to the natural language text to obtain the screening result.
6. The method according to claim 1, wherein After obtaining the target SQL, the method further includes: Connecting the target SQL to the database through an SQL executor for execution; When the SQL executor returns an error message, guiding the large language model by a calibration agent to calibrate the target SQL according to the error message and the natural language text to obtain a calibrated target SQL.
7. The method according to claim 6, characterized in that, The method further includes: When the SQL executor returns a query result, outputting the query result.
8. A data processing device, characterized in that, It includes: An extraction module configured to: guide the large language model by a retrieval agent to extract key information from the natural language text; A retrieval module configured to: obtain a retrieval result that meets a set similarity condition with the key information from a pre-stored database through the retrieval agent; A disassembling module configured to: guide the large language model by a disassembling agent to disassemble the natural language text using the retrieval result to obtain corresponding multiple sub-questions; guide the large language model by the disassembling agent to generate sub-SQLs corresponding to the multiple sub-questions respectively using the retrieval result; wherein, the solution of each sub-question depends on the query result of its previous sub-question, each sub-SQL is obtained by conversion based on its corresponding sub-question and its previous sub-SQL, and the sub-SQL corresponding to the last sub-question is the target SQL corresponding to the natural language text; A query module configured to: obtain a target query result from the database according to the target SQL.
9. An electronic device, characterized in that, It includes a processor and a memory, the memory stores machine-executable instructions that can be executed by the processor, and the processor can execute the machine-executable instructions to implement the method according to any one of claims 1 to 7.
10. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the method according to any one of claims 1 to 7.
Citation Information
Cited By
Query statement generation method and device based on large model, medium and equipment
CN120541191A
A method, apparatus, medium, and device for generating query statements based on a large model.
CN120541191B
Intelligent query method and device based on large model and storage medium
CN120892545A