Background knowledge augmentation-based SQL generation method and apparatus, device and medium
By introducing background knowledge extraction and statement generation modules in the NL2SQL large language model, obtaining questions to be answered and matching initial background knowledge from the background knowledge base, the problem of background knowledge enhancement in the existing technology is solved, improving the accuracy of task resolution and simplifying the process.
Patent Information
- Application Number
- PCT/CN2024/097158
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2023-09-27
- Filing Date
- 2024-06-04
- Publication Date
- 2025-04-03
AI Technical Summary
The prior art methods to enhance the background knowledge of the NL2SQL large language model are relatively complex and difficult to implement.
By obtaining the questions to be answered and matching the initial background knowledge from the background knowledge base; generating the first prompt information based on the initial background knowledge and the questions to be answered; input the first prompt information into the background knowledge to extract the large language model to obtain the target background knowledge; generating the second prompt information based on the target background knowledge and the questions to be answered; input the second prompt information into the statement to generate the large language model to obtain the SQL statement.
It improves the task resolution accuracy of the NL2SQL large language model, reduces the possibility of irrelevant knowledge, and simplifies the process of background knowledge enhancement.
Smart Images

Figure CN2024097158_03042025_PF_FP_ABST
Abstract
Description
SQL generation method, device, equipment and medium based on background knowledge enhancement
[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on September 27, 2023, with application number 202311267512.X. The entire contents of this application are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of natural language processing technology, for example, to a method, apparatus, device and medium for generating SQL based on background knowledge enhancement. Background Art
[0003] NL2SQL is a technology that converts natural language text into Structured Query Language (SQL), enabling ordinary users to query databases using natural language. With the continuous development of artificial intelligence and natural language processing technologies, NL2SQL technology has also developed rapidly.
[0004] In order to improve the capabilities of the NL2SQL large language model and effectively enhance the application of large model technology in professional fields, it is necessary to enhance the background knowledge of the NL2SQL large language model. At present, the commonly used background knowledge enhancement methods are mainly: (1) Combining knowledge graphs with models can help the model better understand the entities in the query and the relationships between them, but the implementation threshold of knowledge graphs is high and the process is complicated; (2) Using pre-trained models such as BERT and GPT to pre-train on large amounts of text data. Pre-trained models usually require a lot of computing resources and are opaque, and their decision-making process is difficult to explain. (3) The database schema (such as table names, column names, etc.) is the key to understanding SQL queries. By embedding schema information into the model, the model can be helped to better understand and generate SQL queries. However, this method is relatively complex and requires the design of specific schema embeddings for each database.
[0005] Summary of the Invention
[0006] The present application provides a method, apparatus, device and medium for generating SQL based on background knowledge enhancement, so as to solve the problem that the methods for background knowledge enhancement of the NL2SQL large language model in related technologies are relatively complex and difficult to implement.
[0007] According to one aspect of the present application, a method for generating SQL based on background knowledge enhancement is provided, comprising:
[0008] Obtaining a question to be answered and matching initial background knowledge corresponding to the question to be answered from a background knowledge base; the question to be answered is a natural language text;
[0009] generating first prompt information according to the initial background knowledge and the question to be answered;
[0010] Inputting the first prompt information into a background knowledge extraction language model to obtain target background knowledge;
[0011] generating second prompt information according to the target background knowledge and the question to be answered;
[0012] The second prompt information is input into a sentence to generate a large language model to obtain an SQL statement.
[0013] According to another aspect of the present application, there is provided a device for generating SQL based on background knowledge enhancement, comprising:
[0014] A knowledge matching module is configured to obtain a question to be answered and match initial background knowledge corresponding to the question to be answered from a background knowledge base; the question to be answered is a natural language text;
[0015] A first prompt generating module configured to generate first prompt information based on the initial background knowledge and the question to be answered;
[0016] A knowledge extraction module configured to input the first prompt information into a background knowledge extraction language model to obtain target background knowledge;
[0017] A second prompt generating module, configured to generate second prompt information according to the target background knowledge and the question to be answered;
[0018] The statement generation module is configured to input the second prompt information into a statement to generate a large language model to obtain an SQL statement.
[0019] According to another aspect of the present application, an electronic device is provided, comprising:
[0020] at least one processor; and
[0021] a memory communicatively connected to the at least one processor; wherein,
[0022] The memory stores a computer program that can be executed by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the SQL generation method based on background knowledge enhancement described in any embodiment of the present application.
[0023] According to another aspect of the present application, a computer-readable storage medium is provided, wherein the computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a processor to implement the SQL generation method based on background knowledge enhancement described in any embodiment of the present application when executed.
[0024] The technical solution of the embodiment of the present application is to obtain the question to be answered and match the initial background knowledge corresponding to the question to be answered from the background knowledge library; the question to be answered is a natural language text; a first prompt information is generated based on the initial background knowledge and the question to be answered; the first prompt information is input into the background knowledge extraction large language model to obtain the target background knowledge; the background knowledge extraction large language model is introduced to accurately match the target background knowledge needed to solve the question to be answered from the background knowledge in the background knowledge library, thereby reducing the possibility of irrelevant knowledge appearing, thereby improving the task solving accuracy of the NL2SQL large language model. At the same time, a second prompt information is generated based on the target background knowledge and the question to be answered; the second prompt information is input into the statement generation large language model to obtain an SQL statement, and the target background knowledge is written into the second prompt information to enhance the background knowledge of the statement generation large language model, thereby helping the statement generation large language model to better understand the task requirements and the solution to the problem. The method is simple and easy to implement, which solves the problem that the related art method of enhancing the background knowledge of the NL2SQL large language model is relatively complex and difficult to implement. BRIEF DESCRIPTION OF THE DRAWINGS
[0025] The following will introduce the drawings required for the description of the embodiments. The drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0026] FIG1 is a flowchart of a method for generating SQL based on background knowledge enhancement provided in an embodiment of the present application;
[0027] FIG2 is a flow chart of a method for generating second prompt information in an embodiment of the present application;
[0028] FIG3 is a flowchart of another method for generating second prompt information in an embodiment of the present application;
[0029] FIG4 is a flowchart of a method for generating SQL based on background knowledge enhancement provided in an embodiment of the present application;
[0030] FIG5 is a schematic diagram of the structure of an SQL generation device based on background knowledge enhancement provided in an embodiment of the present application;
[0031] FIG6 is a schematic structural diagram of an electronic device that implements the SQL generation method based on background knowledge enhancement according to an embodiment of the present application. DETAILED DESCRIPTION
[0032] The following describes the embodiments of the present application in conjunction with the accompanying drawings. The embodiments described are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments of the present application, all other embodiments obtained by ordinary technicians in this field without making creative efforts should fall within the scope of protection of this application.
[0033] It should be noted that the terms "first", "second", etc. in the specification and claims of the present application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the present application described herein can be implemented in an order other than those illustrated or described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, system, product or device comprising a series of steps or units is not necessarily limited to those steps or units listed, but may include other steps or units that are not listed or inherent to these processes, methods, products or devices.
[0034] FIG1 is a flowchart of a method for generating SQL based on background knowledge enhancement provided by an embodiment of the present application. This embodiment is applicable to performing background knowledge enhancement on the NL2SQL large language model that converts natural language text into structured query language SQL. The method can be performed by a device for generating SQL based on background knowledge enhancement. The device can be implemented in the form of hardware and / or software and can be configured in an electronic device. As shown in FIG1 , the method includes:
[0035] S110: Obtain a question to be answered, and match initial background knowledge corresponding to the question to be answered from a background knowledge base; the question to be answered is a natural language text.
[0036] In some embodiments, the question to be answered refers to a question waiting to be answered. In the embodiment of the present application, the question to be answered can be a natural language text, and is converted into a structured query language SQL through the SQL generation method based on background knowledge enhancement provided by the embodiment of the present application, so as to query the answer to the question based on the SQL statement.
[0037] The background knowledge base is a database for storing background knowledge. In an embodiment of the present application, it is necessary to create a background knowledge base based on the background knowledge in advance. Optionally, the background knowledge stored in the background knowledge base may include: inter-table knowledge, industry knowledge and experiential knowledge. Inter-table knowledge may include schema-type semantic information, conventional knowledge, equivalent representation of field names or values. Industry knowledge may include industry terminology, industry regulations (such as industry standards or industry guidelines, etc.), professional knowledge, etc. Experiential knowledge may include: custom logic (such as table and field definition logic and SQL writing logic, etc.), database design and usage guidelines, SQL development guidelines and best practices, etc. Optionally, the background knowledge stored in the background knowledge base is meta-knowledge. Meta-knowledge is "knowledge about information and knowledge", including knowledge learning, utilization, creation, storage, sharing, evaluation and other content, which can play a role in reducing knowledge search time, determining the priority of knowledge use, knowledge classification, macro description of knowledge items, and controlling the stimulation and operation of knowledge.
[0038] The initial background knowledge is the background knowledge that matches the question to be answered. It should be noted that in this embodiment, the initial background knowledge can be understood as background knowledge that fuzzily matches (or roughly matches) the question to be answered, that is, the initial background knowledge may contain redundant background knowledge that is not strongly relevant to the question to be answered.
[0039] In this embodiment, a question to be answered in a natural language format is obtained, the question to be answered is input into a pre-created background knowledge base, and background knowledge roughly matching the question to be answered in the background knowledge base is searched as initial background knowledge for the question to be answered.
[0040] In an optional embodiment of the embodiment of the present application, the background knowledge base is a vector database; and matching the initial background knowledge corresponding to the question to be answered from the background knowledge base includes:
[0041] Performing text vectorization on the question to be answered based on a vectorization model to obtain a question vector;
[0042] The background knowledge base is queried according to the question vector to obtain initial background knowledge matching the question vector.
[0043] In some embodiments, the background knowledge base is a vector database, which can also be understood as the background knowledge stored in the background knowledge base being vector data. Therefore, the questions to be answered also need to be converted into vector data. The question vector can be understood as the vectorized format data of the questions to be answered.
[0044] In this embodiment, the vectorization model may adopt an Embedding word vector model. The background knowledge base may be queried based on the question vector to obtain the initial background knowledge that matches the question vector by using a fuzzy matching algorithm to query the initial background knowledge that matches the question vector; or the similarity between the question vector and each piece of background knowledge in the background knowledge base may be calculated, and the background knowledge with a similarity greater than a preset threshold is determined as the initial background knowledge corresponding to the question to be answered.
[0045] S120: Generate first prompt information based on initial background knowledge and questions to be answered.
[0046] In some embodiments, the first prompt information can be understood as an instruction input into the background knowledge extraction large language model, which is used to instruct the background knowledge extraction large language model to extract the background knowledge needed to solve the question to be answered from the initial background knowledge. In the field of natural language processing, the prompt information prompt refers to the input text or question provided by the system to the large language model to guide the model to generate the corresponding answer or output. The prompt is usually a question, a description or a complete sentence that can be generated by the system. By providing a clear prompt, the model can be guided to follow specific semantics and contextual requirements when generating text, thereby better meeting the needs of users. Prompts can help the model understand the goals, context and constraints of the task, and maintain consistency and interpretability when generating text.
[0047] In this embodiment, the first prompt information can be determined based on the prompt information used by the background knowledge extraction large language model during the training phase. The first prompt information includes the question to be answered and the initial background knowledge.
[0048] S130: Input the first prompt information into a background knowledge extraction language model to obtain target background knowledge.
[0049] In some embodiments, the target background knowledge refers to the background knowledge required to solve the question to be answered. In this embodiment, the target background knowledge can be understood as the background knowledge that accurately matches the question to be answered relative to the initial background knowledge.
[0050] Large language models for background knowledge extraction are used to extract target background knowledge from initial background knowledge. Large language models (LLMs) are neural network-based natural language processing models designed to learn the linguistic features and probability distributions of large-scale text data. These models are pre-trained on large corpora to acquire rich linguistic knowledge, which can then be used to generate text, complete text tasks, or serve as foundational models for other downstream tasks.
[0051] In an optional embodiment of this embodiment, a method for obtaining a large language model for background knowledge extraction may be as follows: assuming that the large language model for background knowledge extraction is Query2bgKG LLM (full name: Query to Background Knowledge Large Language Model). A training set is obtained; the training corpus in the training set may consist of prompt information [prompt_kg] and background knowledge [kg_tag]. [prompt_kg] is the prompt information input to Query2bgKG LLM, which is used to instruct Query2bgKG LLM to extract the target background knowledge required to solve the user problem from the initial background knowledge. [prompt_kg] consists of a task instruction, a question to be answered, and meta-knowledge. For example, the format of the prompt information may be "Please play the role of a knowledge expert. You need to output the background knowledge and methods required to solve the problem based on the known knowledge and the user problem. The content of the known knowledge is: {initial background knowledge B}, the user problem is {user problem A}, based on the above known knowledge and the user problem, the background knowledge and methods required to solve the problem are:" . [kg_tag] is the target background knowledge required to solve the question to be answered, that is, the expected generation result of the large language model for background knowledge extraction. [kg_tag] can be manually written or generated from a question template. Based on the above training corpus, Query2bgKG LLM is fine-tuned to enable it to accurately extract target background knowledge based on the question to be answered, meta-knowledge, and specific prompt information.
[0052] In this embodiment, the question to be answered and the corresponding initial background knowledge are written as first prompt information in a format consistent with the training corpus used in the training process. The first prompt information is input into the trained Query2bgKG LLM, so that the target background knowledge needed to solve the question to be answered can be accurately extracted.
[0053] After roughly matching the background knowledge in the background knowledge base to obtain the initial background knowledge that may be needed to solve the unanswered question, the embodiment of the present application introduces a background knowledge extraction large language model to accurately match the target background knowledge needed to solve the unanswered question from the background knowledge in the background knowledge base. This is used to subsequently enhance the knowledge of the NL2SQL large language model based on the target background knowledge, reducing the possibility of irrelevant knowledge, ensuring that the extracted knowledge is the background knowledge required to convert the user query question into SQL, and thus improving the task-solving accuracy of the NL2SQL large language model. In addition, the background knowledge enhancement method of the NL2SQL large language model introduced by the background knowledge extraction large language model in the embodiment of the present application is simple and easy to implement compared to the knowledge graph enhancement method, pre-training enhancement method, and pattern information embedding method of related technologies.
[0054] S140: Generate second prompt information based on the target background knowledge and the question to be answered.
[0055] In some embodiments, the second prompt information can be understood as an instruction input into the sentence generation large language model, instructing the sentence generation large language model to convert the natural language question to be answered in the second prompt information into an SQL statement that can solve the question to be answered. The second prompt information includes: the question to be answered and the target background knowledge required to solve the question to be answered.
[0056] For example, assuming that the second prompt message is Prompt_nl2sql, the second prompt message may be "Prompt_nl2sql: You are now a data analysis expert, you know the data table {schema}, and you know {target background knowledge}. Please tell me what SQL statement can answer the question {user question}."
[0057] In an embodiment of the present application, by writing the target background knowledge into the second prompt information, the background knowledge of the statement generation large language model can be enhanced, thereby helping the statement generation large language model to better understand the task requirements and problem-solving methods, and improve the accuracy of SQL generation.
[0058] S150: Input the second prompt information into a statement to generate a large language model and obtain an SQL statement.
[0059] In this embodiment, assuming that the statement generation large language model is NL2SQL LLM, the constructed second prompt information Prompt_nl2sql containing the query question and target background knowledge is input into NL2SQL LLM to obtain the SQL statement corresponding to the query question output by NL2SQL LLM.
[0060] The technical solution of the embodiment of the present application obtains the question to be answered and matches the initial background knowledge corresponding to the question to be answered from the background knowledge library; the question to be answered is a natural language text; a first prompt information is generated according to the initial background knowledge and the question to be answered; the first prompt information is input into the background knowledge extraction large language model to obtain the target background knowledge; a second prompt information is generated according to the target background knowledge and the question to be answered; the second prompt information is input into the statement generation large language model to obtain an SQL statement, which can accurately enhance the background knowledge of the statement generation large language model, reduce the possibility of irrelevant knowledge, and help the statement generation large language model better understand the task requirements and problem-solving methods, thereby improving the task solving accuracy of the NL2SQL large language model, and the process is simple and easy to implement.
[0061] In an optional embodiment of the present application, FIG2 is a flow chart of a method for generating second prompt information in the embodiment of the present application. As shown in FIG2, the second prompt information is generated based on the target background knowledge and the question to be answered;
[0062] S1411. Query a historical question database based on the question vector corresponding to the question to be answered to obtain historical questions that match the question vector; the historical question database is used to store verified historical questions;
[0063] S1412. Generate second prompt information including target background knowledge, questions to be answered, and historical questions based on a preset prompt template.
[0064] In some embodiments, the historical question database is used to store verified historical questions. Historical questions can be understood as questions to be answered received at a historical time. Verification can refer to the verification of historical questions and corresponding SQL statements. This verification can be user feedback or system verification. Preset prompt templates are pre-built templates that specify the format of prompt information.
[0065] In this embodiment, in addition to building a background knowledge base to enhance the knowledge of the sentence generation large language model, a historical question library can also be built based on the received historical questions. After accurately matching the target background knowledge required to solve the question to be answered from the background knowledge base, the question vector can also be input into the historical question database to obtain historical questions that match the question vector, which means that the sentence generation large language model has received the same or similar questions in historical time; second prompt information is generated based on the target background knowledge, the question to be answered, and the historical questions, and the second prompt information is input into the sentence generation large language model, which can help the sentence generation large language model refer to the SQL generated for the historical question and use the target background knowledge to generate the SQL statement for the question to be answered.
[0066] This embodiment enhances the knowledge of the statement generation large language model through the background knowledge base and the historical question base. It can help the statement generation large language model better understand the task requirements and problem-solving methods based on the background knowledge required to solve the questions to be answered and the SQL statements corresponding to verified historical questions, thereby improving the accuracy of SQL generation.
[0067] In another optional embodiment of the present application, FIG3 is a flowchart of another method for generating second prompt information in the embodiment of the present application. As shown in FIG3, generating second prompt information based on the target background knowledge and the question to be answered includes:
[0068] S1421. Determine an equivalent question vector of the question vector corresponding to the question to be answered based on synonyms in the target background knowledge;
[0069] S1422: Generate second prompt information including target background knowledge and an equivalent question vector based on a preset prompt template.
[0070] In some embodiments, an equivalent problem vector refers to a vector that is equivalent to the problem vector.
[0071] In this embodiment, an equivalent question vector of the question vector corresponding to the question to be answered is determined based on synonyms in the target background knowledge, so that the question to be answered is equivalently rewritten based on synonym replacement in the background knowledge. For example, the professional terms in the question to be answered are replaced with equivalent names, thereby improving the understanding of the question by the sentence generation large language model.
[0072] Figure 4 is a flowchart of another SQL generation method based on background knowledge enhancement provided by an embodiment of the present application. Based on the above embodiment, before matching the initial background knowledge corresponding to the question to be answered from the background knowledge base, this embodiment also includes: obtaining background knowledge and attribute information of the background knowledge; segmenting and vectorizing the background knowledge to obtain background knowledge vectors; writing the background knowledge vectors into a knowledge base document according to the attribute information; constructing a background knowledge base based on the knowledge base document; wherein the knowledge base document is used to store background knowledge with corresponding attribute information; the attribute information includes: usage conditions and knowledge type of the background knowledge.
[0073] As shown in FIG4 , the method includes:
[0074] S210: Acquire background knowledge and attribute information of the background knowledge.
[0075] In some embodiments, the attribute information of background knowledge is information used to describe basic attributes of the background knowledge, and the attribute information of the background knowledge may include: a usage condition of the background knowledge and a knowledge type. In this embodiment, the attribute information of the background knowledge may be predefined.
[0076] In this embodiment, the usage condition of background knowledge refers to the condition under which the background knowledge is needed to solve the problem, that is, if the question to be answered meets the usage condition, then this background knowledge is needed to solve the question to be answered.
[0077] Optionally, the knowledge types include: action type and non-action type; the background knowledge of the non-action type is used to explain the question to be answered; the background knowledge of the action type is used to prompt the sentence generation large language model to perform preset operations on the question to be answered.
[0078] In some embodiments, non-action-type background knowledge is used to explain certain parts of the question being answered, such as industry terminology, equivalent expressions for field names or values, etc. Action-type background knowledge typically requires a large language model to perform corresponding preset operations based on the action-type background knowledge, such as logic refinement and field selection. This may include conventional knowledge, industry jargon, behavior descriptions specified by industry standards, human experience, and matching relationships between keywords and table fields.
[0079] S220: Segment and vectorize the background knowledge to obtain a background knowledge vector.
[0080] In some embodiments, the background knowledge vector is background knowledge represented by a text vector.
[0081] In this embodiment, it is possible to specify that a piece of background knowledge occupies one line in the knowledge description, and then the background knowledge is segmented by the line break character '\n'. The segmented background knowledge is then vectorized using the Embedding word vector model to obtain a background knowledge vector.
[0082] S230: Writing the background knowledge vector into a knowledge base document in the background knowledge base according to the attribute information.
[0083] In some embodiments, the knowledge base document is used to store background knowledge with corresponding attribute information;
[0084] In this embodiment, the background knowledge vector is written into the knowledge base document in the background knowledge base according to the attribute information. If the background knowledge needs to be modified or added, it is only necessary to modify the knowledge base document first and then perform steps S220 and S230.
[0085] This embodiment classifies knowledge through knowledge base documents, which can avoid semantic confusion and maintain the consistency of knowledge in the vector database. It not only realizes the controllable background extraction, but also can easily modify the content in the knowledge base according to different business scenarios. It has strong usability, practicality and scalability.
[0086] S240: Obtain the question to be answered, and match the initial background knowledge corresponding to the question to be answered from the background knowledge base; the question to be answered is a natural language text.
[0087] S250: Generate first prompt information based on initial background knowledge and the question to be answered.
[0088] S260: Input the first prompt information into a background knowledge extraction language model to obtain target background knowledge.
[0089] S270: Generate second prompt information based on the target background knowledge and the question to be answered.
[0090] S280: Input the second prompt information into a statement to generate a large language model and obtain an SQL statement.
[0091] In this embodiment, the execution process of S240 to S280 is the same as that of S110 to S150 in the above embodiment.
[0092] The technical solution of the embodiment of the present application obtains background knowledge and attribute information of background knowledge; segments and vectorizes the background knowledge to obtain a background knowledge vector; writes the background knowledge vector into a knowledge base document in a background knowledge base according to the attribute information; obtains a question to be answered and matches the initial background knowledge corresponding to the question to be answered from the background knowledge base; the question to be answered is a natural language text, and a first prompt information is generated based on the initial background knowledge and the question to be answered; the first prompt information is input into a background knowledge extraction large language model to obtain target background knowledge; a second prompt information is generated based on the target background knowledge and the question to be answered; the second prompt information is input into a statement generation large language model to obtain an SQL statement; the statement generation large language model can be accurately enhanced with background knowledge, reducing the possibility of irrelevant knowledge appearing, and the process is simple and easy to implement. At the same time, the knowledge required for generating SQL is extracted by retrieving the vectorized knowledge base, thereby deepening the large model's understanding of the NL2SQL task in the professional field, thereby improving the accuracy of the large model's NL2SQL task and the accuracy of SQL generation. It is also versatile and is not limited to a specific database, background knowledge or specific model structure, and has a wide range of applications.
[0093] In one embodiment, the SQL generation method based on background knowledge enhancement provided by the embodiment of this application is described in the NL2SQL application scenario based on equity incentive-related question queries. In this application scenario, through the knowledge base compilation and background knowledge extraction methods, it is possible to accurately associate the relevant equity incentive background knowledge in the knowledge base based on natural semantic questions, thereby enhancing the NL2SQL LLM's ability to understand user questions. The data table with equity incentive as the background is named t_inc_esop_info, which contains relevant information about the implementation of equity incentives by different companies. The process of background knowledge enhancement and NL2SQL SQL generation method based on this application is as follows:
[0094] Step 1: Build a background knowledge base. For NL2SQL tasks based on equity incentives, background knowledge is required for reference when generating SQL statements. The background knowledge documents are divided into two categories, as shown below: The pre_define.txt background knowledge document stores [condition-nonaction kg] conditional-non-action knowledge. The following is an example of [condition-nonaction kg]: "If the user's question contains 'Class I restricted stock', Class I restricted stock refers to stock obtained upon grant, but employees cannot freely transfer or dispose of these stocks during a certain restricted period. If the user's question contains 'return coefficient', 'return coefficient' refers to 'payment return coefficient'." The pre_action.txt background knowledge document stores [condition-action] conditional-action knowledge.
[0095] The following is an example of a [condition-action]: "When a question contains consecutive numbers, such as '334', it should be automatically converted to '30:30:40', as it may not be written exactly as 30:30:40 in actual business. If the ratio is written as '1:1', it should be automatically converted to '50:50'. When a question contains a date, time period, or point in time, such as 'the past six months', but no date field is specified, 'preplan_ann_date' is used, where the field type of the preplan_ann_date can be a string."
[0096] Step 2: Roughly match background knowledge. First, segment the background knowledge based on line breaks (\n) and vectorize it using the text2vec-large-chinese model. Then, vectorize the user question (i.e., the question to be answered) and enter it into the background knowledge database for similarity search, roughly matching relevant background knowledge.
[0097] Here’s an example of a user question roughly matching background knowledge:
[0098] User question: "Which foreign companies in Province A have implemented employee stock ownership plans?"
[0099] Meta-knowledge (i.e., initial background knowledge): "When 'employee stock ownership plan' appears in the question, the employee stock ownership plan is a mechanism that allows a company's employees to purchase or acquire company stock, thereby becoming shareholders of the company; this plan is designed to motivate and reward employees, enhance their loyalty and participation, and maintain a common interest in the company's performance and growth. When 'foreign enterprises' appear in the question, WIND_SEC_CODE = ('foreign-invested enterprises'). When 'employee stock ownership plan' appears in the question, script_type = ('employee stock ownership plan'). When 'restricted period' appears in the question, it refers to 'lock-up period (interval_months)', script_type = ('first-class restricted stock'). When 'exercise ratio' appears in the question, it refers to 'unlocking ratio in each period (inc_execpct_set)', script_type = ('stock option')".
[0100] Step 3: Exact matching of background knowledge.
[0101] Step 3.1 Query2bgKG LLM learning. For training corpus generation, you can use question templates or manually written methods. The following is a simple method for generating corpus based on question templates. First, write a generalized question template based on the questions that practitioners in the actual equity incentive business need to query. Example: What [script_type] has [wind_sec_code] of [s_info_province] done?
[0102] Among them, [s_info_province] (province), [wind_sec_code] (nature of enterprise) and [script_type] (type of equity incentive) are the field names in the equity incentive data table t_inc_esop_info. When generating questions, just replace the field names with field values. At the same time, based on the background knowledge involved in the replaced field values, generate [kg_tag] (if the filled-in field value meets a usage condition, then the generated question involves the background knowledge corresponding to the usage condition). Then perform the operation in step 1 on the generated question, extract relevant meta-knowledge, and combine the question and meta-knowledge to generate the prompt information [prompt_kg] (i.e., the first prompt information).
[0103] For example: Question template: "Which [wind_sec_code] of [info_province] has done [script_type]?"
[0104] User question: "Which [state-owned enterprises] in [Province B] have engaged in [options]?"
[0105] The first prompt message prompt_kg: "Please play the role of a knowledge expert. You need to output the background knowledge and methods needed to solve the problem based on the known knowledge and the user's question. The content of the known knowledge is: "When "stock options" appear in the question, stock options are a financial instrument that gives the holder the right to buy or sell company stock. When "state-owned enterprises" appear in the question, WIND_SEC_CODE in ('state-owned enterprises'). When "options" appear in the question, script_type = ('stock options'). When "restricted period" appears in the question, it refers to the "lock-up period (interval_months)", script_type = ('First category restricted stock'). When "exercise ratio" appears in the question, it refers to the "unlocking ratio for each period (inc_execpct_set)", script_type = ('stock options')." The user question is: "Which state-owned listed companies in Province B have exercised options?" Based on the above known knowledge and user questions, the background knowledge and methods required to solve the problem are:".
[0106] kg_tag: "When 'stock option' appears in the question, stock option is a financial instrument that gives the holder the right to buy or sell company stock. When 'state-owned enterprise' appears in the question, WIND_SEC_CODE = ('state-owned enterprise'). When 'option' appears in the question, script_type = ('stock option')".
[0107] Step 3.2: Query2bgKG Precision Extraction. When a business user asks a query, a rough background knowledge match is first performed to extract relevant meta-knowledge. The two are then combined to generate text information in the prompt_kg format described in Step 3.1 and input into the Query2bgKG LLM. The Query2bgKG LLM then outputs the precise background knowledge relevant to the user's query.
[0108] Example: User question: "Which foreign companies in Province A have implemented employee stock ownership plans?"
[0109] Query2bgKG outputs the following result: "When 'Employee Stock Ownership Plan' appears in the question, an employee stock ownership plan is a mechanism that allows a company's employees to purchase or acquire company stock, thereby becoming shareholders; this plan is designed to motivate and reward employees, enhance their loyalty and engagement, and maintain a shared interest in the company's performance and growth. When 'foreign enterprise' appears in the question, WIND_SEC_CODE = ('Foreign-invested enterprise'). When 'Employee Stock Ownership Plan' appears in the question, script_type = ('Employee Stock Ownership Plan')."
[0110] Step 4: NL2SQL generates the SQL. Based on the above prompt, NL2SQL LLM generates the following SQL: select * from t_inc_esop_info where SCRIPT_TYPE = 'Employee Stock Ownership Plan' and WIND_SEC_CODE = 'Foreign-Invested Enterprises' and S_INFO_PROVINCE like 'Province A'.
[0111] As shown above, in practical application scenarios, especially professional scenarios, the background knowledge construction and SQL generation method proposed in this application can provide the large model with the background knowledge required for NL2SQL tasks. This deepens the large model's understanding of NL2SQL tasks and improves the accuracy of SQL generation.
[0112] FIG5 is a schematic diagram of the structure of a SQL generation device based on background knowledge enhancement provided by an embodiment of the present application. As shown in FIG5 , the device includes:
[0113] The knowledge matching module 310 is configured to obtain a question to be answered and match the initial background knowledge corresponding to the question to be answered from a background knowledge base; the question to be answered is a natural language text;
[0114] A first prompt generating module 320 is configured to generate first prompt information based on the initial background knowledge and the question to be answered;
[0115] The knowledge extraction module 330 is configured to input the first prompt information into a background knowledge extraction language model to obtain target background knowledge;
[0116] A second prompt generating module 340 is configured to generate second prompt information based on the target background knowledge and the question to be answered;
[0117] The statement generation module 350 is configured to generate a large language model by inputting the second prompt information into a statement to obtain an SQL statement.
[0118] The technical solution of the embodiment of the present application obtains the question to be answered and matches the initial background knowledge corresponding to the question to be answered from the background knowledge library; the question to be answered is a natural language text; a first prompt information is generated based on the initial background knowledge and the question to be answered; the first prompt information is input into the background knowledge extraction large language model to obtain the target background knowledge; a second prompt information is generated based on the target background knowledge and the question to be answered; the second prompt information is input into the statement generation large language model to obtain an SQL statement, which can accurately enhance the background knowledge of the statement generation large language model, reduce the possibility of irrelevant knowledge appearing, thereby improving the task solving accuracy of the NL2SQL large language model, and at the same time help the statement generation large language model better understand the task requirements and problem-solving methods, improve the accuracy of SQL generation, and the process is simple and easy to implement.
[0119] Optionally, the knowledge matching module 310 is configured to:
[0120] Performing text vectorization on the question to be answered based on a vectorization model to obtain a question vector;
[0121] The background knowledge base is queried according to the question vector to obtain initial background knowledge matching the question vector; wherein the background knowledge base is a vector database.
[0122] Optionally, the second prompt generating module includes:
[0123] a historical question matching unit configured to query a historical question database according to the question vector corresponding to the question to be answered, and obtain a historical question matching the question vector; the historical question database is used to store verified historical questions;
[0124] The first information generating unit is configured to generate second prompt information including the target background knowledge, the question to be answered and the historical question based on a preset prompt template.
[0125] Optionally, the second prompt generating module is further configured to:
[0126] an equivalent determination unit configured to determine an equivalent question vector of the question vector corresponding to the question to be answered based on synonyms in the target background knowledge;
[0127] The second information generating unit is configured to generate second prompt information including the target background knowledge and the equivalent question vector based on a preset prompt template.
[0128] Optionally, the device further includes:
[0129] a knowledge acquisition module configured to acquire background knowledge and attribute information of the background knowledge before matching the initial background knowledge corresponding to the question to be answered from the background knowledge base;
[0130] A vectorization module configured to segment and vectorize the background knowledge to obtain background knowledge vectors;
[0131] The vector writing module is configured to write the background knowledge vector into a knowledge base document in a background knowledge base according to the attribute information.
[0132] Optionally, the attribute information includes: usage conditions and knowledge types of the background knowledge; the knowledge types include: action type and non-action type;
[0133] Non-action type background knowledge is used to explain the question to be answered;
[0134] The background knowledge of the action type is used to prompt the sentence generation large language model to perform a preset operation on the question to be answered.
[0135] Optionally, the background knowledge stored in the background knowledge base is meta-knowledge; the background knowledge includes: inter-table knowledge, industry knowledge and experience knowledge.
[0136] The SQL generation device based on background knowledge enhancement provided in the embodiment of the present application can execute the SQL generation method based on background knowledge enhancement provided in any embodiment of the present application, and has the corresponding functional modules and beneficial effects of the execution method.
[0137] FIG6 shows a block diagram of an electronic device 10 that can be used to implement an embodiment of the present application. The electronic device can represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices (such as helmets, glasses, watches, etc.) and other similar computing devices. The components shown herein, their connections and relationships, and their functions are only examples.
[0138] As shown in FIG6 , the electronic device 10 includes at least one processor 11 and a memory, such as a read-only memory (ROM) 12 and a random access memory (RAM) 13, that is communicatively connected to the at least one processor 11. The memory stores a computer program that can be executed by the at least one processor, and the processor 11 can perform various appropriate actions and processes according to the computer program stored in the read-only memory (ROM) 12 or the computer program loaded from the storage unit 18 into the random access memory (RAM) 13. Various programs and data required for the operation of the electronic device 10 can also be stored in the RAM 13. The processor 11, ROM 12, and RAM 13 are connected to each other via a bus 14. An input / output (I / O) interface 15 is also connected to the bus 14.
[0139] Multiple components in the electronic device 10 are connected to the I / O interface 15, including an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a magnetic disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the electronic device 10 to exchange information / data with other devices via a computer network such as the Internet and / or various telecommunication networks.
[0140] The processor 11 can be any general-purpose and / or specialized processing component with processing and computing capabilities. Some examples of the processor 11 include a central processing unit (CPU), a graphics processing unit (GPU), various specialized artificial intelligence (AI) computing chips, various processors that run machine learning model algorithms, a digital signal processor (DSP), and any other suitable processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the SQL generation method based on background knowledge enhancement.
[0141] In some embodiments, the SQL generation method based on background knowledge enhancement may be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as the storage unit 18. In some embodiments, part or all of the computer program may be loaded and / or installed on the electronic device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the SQL generation method based on background knowledge enhancement described above may be performed. Alternatively, in other embodiments, the processor 11 may be configured to execute the SQL generation method based on background knowledge enhancement by any other appropriate means (e.g., by means of firmware).
[0142] Various embodiments of the systems and techniques described above can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), system-on-chip systems (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable system that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.
[0143] Computer programs for implementing the methods of the present application may be written in any combination of one or more programming languages. These computer programs may be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable data processing device, such that when the computer program is executed by the processor, the functions / operations specified in the flowcharts and / or block diagrams are implemented. The computer program may be executed entirely on the machine, partially on the machine, as a stand-alone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0144] In the context of the present application, a computer-readable storage medium can be a tangible medium that can contain or store a computer program for use with an instruction execution system, device or equipment or used in combination with an instruction execution system, device or equipment. A computer-readable storage medium can include an electronic, magnetic, optical, electromagnetic, infrared or semiconductor system, device or equipment, or any suitable combination of the foregoing. Alternatively, a computer-readable storage medium can be a machine-readable signal medium. Examples of machine-readable storage media include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAMs), read-only memories (ROMs), erasable programmable read-only memories (EPROMs or flash memories), optical fibers, portable compact disk read-only memories (CD-ROMs), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0145] To provide interaction with a user, the systems and techniques described herein can be implemented on an electronic device having: a display device (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the electronic device. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0146] The systems and techniques described herein can be implemented in a computing system that includes back-end components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes front-end components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such back-end components, middleware components, or front-end components. The components of the system can be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: a local area network (LAN), a wide area network (WAN), a blockchain network, and the Internet.
[0147] A computing system may include clients and servers. The clients and servers are typically remote from each other and typically interact via a communication network. This client-server relationship arises through computer programs running on the respective computers, creating a client-server relationship. The server may be a cloud server, also known as a cloud computing server or cloud host. This server is a hosting product within the cloud computing service ecosystem that addresses the management difficulties and limited scalability of traditional physical hosting and VPS services.
[0148] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in this application can be performed in parallel, sequentially, or in a different order, as long as the desired results of this application can be achieved.
Claims
1. A SQL generation method based on background knowledge enhancement, comprising: Obtaining the question to be answered, and matching the initial background knowledge corresponding to the question to be answered from the background knowledge base; The question to be answered is a natural language text; Generate first prompt information according to the initial background knowledge and the question to be answered; Inputting the first prompt information into a background knowledge extraction large language model to obtain target background knowledge; Generate second prompt information according to the target background knowledge and the question to be answered; The second prompt information is input into a sentence to generate a large language model to obtain an SQL statement.
2. The method according to claim 1, wherein: The matching of the initial background knowledge corresponding to the question to be answered from the background knowledge base includes: Performing text vectorization on the question to be answered based on a vectorization model to obtain a question vector; The background knowledge base is queried according to the question vector to obtain initial background knowledge matching the question vector; wherein the background knowledge base is a vector database.
3. The method according to claim 1 or 2, wherein: The generating the second prompt information according to the target background knowledge and the question to be answered includes: According to the question vector corresponding to the question to be answered, a historical question database is searched to obtain a historical question matching the question vector; the historical question database is used to store verified historical questions; Based on a preset prompt template, second prompt information including the target background knowledge, the question to be answered and the historical question is generated.
4. The method according to claim 1 or 2, wherein: The generating the second prompt information according to the target background knowledge and the question to be answered includes: Determining an equivalent question vector of the question vector corresponding to the question to be answered according to the synonyms in the target background knowledge; Based on a preset prompt template, second prompt information including the target background knowledge and the equivalent question vector is generated.
5. The method according to claim 1, before matching the initial background knowledge corresponding to the question to be answered from the background knowledge base, the method further comprises: Acquiring background knowledge and attribute information of the background knowledge; Segmenting and vectorizing the background knowledge to obtain a background knowledge vector; The background knowledge vector is written into a knowledge base document in a background knowledge base according to the attribute information.
6. The method according to claim 5, wherein: The attribute information includes: the usage conditions and knowledge types of the background knowledge; the knowledge types include: action type and non-action type; the background knowledge of non-action type is used to explain the question to be answered; the background knowledge of action type is used to prompt the sentence generation large language model to perform preset operations on the question to be answered.
7. The method according to claim 1 or 5, wherein: The background knowledge stored in the background knowledge base is meta-knowledge; the background knowledge includes: inter-table knowledge, industry knowledge and experience knowledge.
8. A SQL generation device based on background knowledge enhancement, comprising: A knowledge matching module, configured to obtain a question to be answered and match initial background knowledge corresponding to the question to be answered from a background knowledge base; The question to be answered is a natural language text; A first prompt generating module, configured to generate first prompt information according to the initial background knowledge and the question to be answered; A knowledge extraction module, configured to input the first prompt information into a background knowledge extraction language model to obtain target background knowledge; A second prompt generating module, configured to generate second prompt information according to the target background knowledge and the question to be answered; The statement generation module is configured to input the second prompt information into a statement to generate a large language model to obtain an SQL statement.
9. An electronic device, comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores a computer program executable by the at least one processor, and the computer program is executed by the at least one processor so that the at least one processor can execute the SQL generation method based on background knowledge enhancement according to any one of claims 1 to 7.
10. A computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable a processor to implement the SQL generation method based on background knowledge enhancement according to any one of claims 1 to 7 when executed.
Citation Information
Patent Citations
Inference question and answer method based on knowledge fusion
CN115730058A
Large language model knowledge enhancement method and system, electronic equipment and medium
CN116303980A
SQL (Structured Query Language) generation method and device based on background knowledge enhancement, equipment and medium
CN117312372A
Text classifier for answer identification, background knowledge expression generator, training device therefor, and computer program
WO2022085533A1
Cited By
Prompt information generation system and prompt information generation model obtaining method
CN116756276A
Domain knowledge supplement and SQL generation optimization method and device based on large model
CN119938891A
Database query method, device and equipment based on large model retrieval enhancement generation
CN120104762A
Large model training method for query conversion, database query method, computer equipment and computer readable storage medium
CN120123367A
Large language model knowledge editing method and interaction method based on knowledge representation decoupling
CN120181205A