Method for question answering
By dynamically constructing a database schema related to natural language questions, the dependency of existing intelligent question answering systems on database schemas is solved, achieving efficient and accurate SQL query generation and database structure adaptability.
Patent Information
- Application Number
- CN202511667900.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-14
- Publication Date
- 2026-02-24
- Estimated Expiration
- 2045-11-14
AI Technical Summary
In existing intelligent question answering systems, the dependence of supervised learning-based Text-to-SQL models on database schemas makes them difficult to adapt to frequently changing database environments, resulting in high computational burden and low accuracy.
By dynamically constructing a database schema related to natural language problems, and using prompt word engineering to guide the target model to generate SQL query statements, end-to-end training and fine-tuning are avoided, and only the schema related to the problem is used as input.
It significantly improves the accuracy of SQL query statements, reduces the computational burden, and can automatically adapt to changes in database structure.
Smart Images

Figure CN121144470B_ABST
Abstract
Description
Technical Field
[0001] This application generally relates to the field of intelligent interaction technology. More specifically, this application relates to a method for question answering. Background Technology
[0002] The core data of existing business systems is typically stored in highly structured databases, providing a rich data foundation for intelligent question-answering systems. However, users tend to ask questions in natural language, such as "Who had the highest sales last month?", and these unstructured queries cannot be directly understood by the database. Therefore, the system needs to accurately convert users' natural language questions into standardized structured query language (SQL) to achieve accurate retrieval of structured data and generation of answers.
[0003] The industry typically employs a supervised learning-based Text-to-SQL model. By using paired samples of "natural language questions - SQL queries" as input along with the entire schema of the database, the model is trained end-to-end to learn the complex mapping relationship between natural language questions and SQL queries, thereby achieving automatic conversion from natural language questions to SQL queries.
[0004] However, the above methods have at least the following drawbacks: model performance is heavily dependent on the database schema seen during training and the limited data samples. When the database schema structure changes, the model needs to be retrained or fine-tuned, making it difficult to adapt to the frequently changing database environments in real-world applications. The model relies on the entire database schema as input; an excessively large schema not only significantly increases the computational burden but also introduces a large amount of irrelevant information that creates noise interference, distracting the model and thus reducing the accuracy of generated SQL queries.
[0005] In view of this, there is an urgent need to provide a question-and-answer method to address the aforementioned shortcomings. Summary of the Invention
[0006] In order to at least address one or more of the technical problems mentioned above, this application proposes a method for question answering in several aspects.
[0007] In a first aspect, this application provides a method for question answering, comprising: receiving a natural language question input by a user; determining a target node set based on the correlation between the natural language question and nodes in a pre-generated question-and-answer database relationship graph, wherein the question-and-answer database relationship graph uses table names in the question-and-answer database as nodes and foreign keys as edges; mapping the target node set to a database schema; using the database schema and the natural language question as input to a target model to generate an SQL query statement; and generating a natural language answer to the natural language question based on the query results of the SQL query statement in the question-and-answer database.
[0008] In some embodiments, determining the target node set based on the relevance between the nodes in the natural language question and the question-answer database relationship graph generated in advance based on the question-answer database includes: determining initial nodes based on the similarity between the natural language question and the nodes in the question-answer database relationship graph; combining the initial nodes in pairs to obtain multiple child node sets; determining the shortest path between two initial nodes in the question-answer database relationship graph within the child node sets; and merging all intermediate nodes on the shortest path into the child node sets to obtain the target node set.
[0009] In some embodiments, determining candidate nodes based on the similarity between the natural language question and each node in the question-answering database relationship graph includes: extracting common nouns from the natural language question; and determining candidate nodes based on the similarity between the common nouns and each node in the question-answering database relationship graph.
[0010] In some embodiments, extracting general terms from the natural language problem includes: using cue word engineering to guide the target model to extract the general terms from the natural language problem; wherein the cue word engineering guides the target model to perform the following steps: extracting all terms in the natural language problem; identifying proper nouns and concrete nouns among the terms; replacing the proper nouns and concrete nouns with category nouns; and using the category nouns and other terms among the terms as the general terms.
[0011] In some embodiments, determining candidate nodes based on the similarity between the natural language question and each node in the question-answering database relationship graph includes: when performing similarity calculation, the language of the node is consistent with the language of the natural language question.
[0012] In some embodiments, mapping the target node set to a database schema includes: querying the question-and-answer database to obtain the schema of each node based on each node in the target node set, wherein the schema includes at least table names, column names, and the data types corresponding to the column names; adding semantic annotations consistent with the language of the natural language question to the table names and / or column names and / or data types in the schema of each node; and adding foreign key semantic annotations consistent with the language of the natural language question to the corresponding column names in the schema of nodes with edge relationships, so as to obtain the mapped database schema.
[0013] In some embodiments, after taking the database schema and the natural language problem as input to the target model to generate an SQL query statement, the method further includes: performing syntax validation and validity validation on the SQL query statement; if the syntax validation fails, taking the SQL query statement and the syntax validation result as input to the target model to correct the SQL query statement; if the validity validation fails, outputting a corresponding warning message.
[0014] In some embodiments, generating a natural language answer to the natural language question based on the query results of the SQL query statement in the question-and-answer database includes: executing the SQL query statement in the question-and-answer database to obtain query results; and using prompt word engineering to guide the target model to generate a natural language answer based on the query results and the natural language question.
[0015] In some embodiments, the method further includes: associating and caching the natural language question and the set of target nodes used to form a historical cache; when receiving a new natural language question from a user, determining the similarity between the new natural language question and each historical natural language question in the historical cache; if there are historical natural language questions with a similarity less than or equal to a first similarity threshold, then using the set of historical target nodes associated with the historical natural language question as the target node set corresponding to the new natural language question; if there are no historical natural language questions with a similarity less than or equal to the first similarity threshold, then performing the step of determining the target node set based on the correlation between the natural language question and each node in the question-answer database relationship graph generated in advance based on the question-answer database.
[0016] In some embodiments, associating and caching the natural language question and the set of target nodes used to form a historical cache includes: receiving user feedback information on the natural language answer;
[0017] If the feedback information indicates that the natural language answer meets the user's expectations, then the natural language question and the set of target nodes used are associated and cached to form a historical cache.
[0018] In some embodiments, associating and caching the natural language question and the set of target nodes used to form a historical cache includes: vectorizing the natural language question to obtain a vector representation of the natural language question; associating and caching the vector representation of the natural language question and the set of target nodes used to form a historical cache; and determining the similarity between the newly posed natural language question and each historical natural language question in the historical cache when a new natural language question is received from a user includes: vectorizing the newly posed natural language question to obtain a vector representation of the natural language question; and determining the similarity between the vector representation of the newly posed natural language question and the historical vector representations of each historical natural language question in the historical cache.
[0019] In a second aspect, embodiments of this application provide a processing apparatus, including: a processor configured to execute program instructions; and a memory configured to store program instructions that, when loaded and executed by the processor, cause the processor to perform the methods described in the first aspect and any of its embodiments.
[0020] In a third aspect, embodiments of this application provide a computer-readable storage medium storing program instructions that, when loaded and executed by a processor, cause the processor to perform the methods described in the first aspect and any of its embodiments.
[0021] The question-answering method described above dynamically constructs a database schema related to the natural language question input by the current user. It then uses prompt word engineering to guide the target model to directly generate SQL queries based on this schema and the natural language question. This approach eliminates the need for end-to-end training and fine-tuning of the target model, adapting to frequently changing database environments. Furthermore, instead of using the entire database schema as input, it uses a constructed schema relevant to the natural language question, removing irrelevant noise and reducing the input size of the target model. This significantly improves the accuracy of the generated SQL queries and effectively reduces the computational burden. Attached Figure Description
[0022] The above and other objects, features, and advantages of exemplary embodiments of this application will become readily understood by reading the following detailed description with reference to the accompanying drawings. In the drawings, several embodiments of this application are illustrated by way of example and not limitation, and the same or corresponding reference numerals denote the same or corresponding parts, wherein:
[0023] Figure 1 Exemplary flowcharts illustrating methods for question answering according to some embodiments of this application are shown;
[0024] Figure 2 Exemplary flowcharts illustrating methods for question answering according to some embodiments of this application are shown;
[0025] Figure 3 Exemplary flowcharts illustrating methods for question answering according to some embodiments of this application are shown;
[0026] Figure 4 Exemplary flowcharts illustrating methods for question answering according to some embodiments of this application are shown;
[0027] Figure 5 Exemplary flowcharts illustrating methods for question answering according to some embodiments of this application are shown;
[0028] Figure 6 An exemplary structural block diagram of a processing apparatus according to some embodiments of this application is shown. Detailed Implementation
[0029] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some, not all, of the embodiments of this application. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0030] It should be understood that the terms "comprising" and "including" used in the specification and claims of this application indicate the presence of the described features, integrals, steps, operations, elements and / or components, but do not exclude the presence or addition of one or more other features, integrals, steps, operations, elements, components and / or collections thereof.
[0031] It should also be understood that the terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the application. As used in this specification and claims, the singular forms “a,” “an,” and “the” are intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used in this specification and claims refers to any combination and all possible combinations of one or more of the associated listed items, and includes such combinations.
[0032] As used in this specification and claims, the term "if" may be interpreted, depending on the context, as "when," "once," "in response to determination," or "in response to detection." Similarly, the phrase "if determined" or "if [described condition or event] is detected" may be interpreted, depending on the context, as "once determined," "in response to determination," "once [described condition or event] is detected," or "in response to detection of [described condition or event]."
[0033] The specific embodiments of this application will now be described in detail with reference to the accompanying drawings.
[0034] Exemplary application scenarios
[0035] The core data of existing business systems is typically stored in highly structured databases, providing a rich data foundation for intelligent question-answering systems. However, users tend to ask questions in natural language, such as "Who had the highest sales last month?", and these unstructured queries cannot be directly understood by the database. Therefore, the system needs to accurately convert users' natural language questions into standardized structured query language (SQL) to achieve accurate retrieval of structured data and generation of answers.
[0036] The industry typically employs a supervised learning-based Text-to-SQL model. By using paired samples of "natural language questions - SQL queries" as input along with the entire schema of the database, the model is trained end-to-end to learn the complex mapping relationship between natural language questions and SQL queries, thereby achieving automatic conversion from natural language questions to SQL queries.
[0037] However, the above methods have at least the following drawbacks: model performance is heavily dependent on the database schema seen during training and the limited data samples. When the database structure changes, the model needs to be retrained or fine-tuned, making it difficult to adapt to the frequently changing database environments in real-world applications. The model relies on the entire database schema as input; an excessively large schema not only significantly increases the computational burden but also introduces a large amount of irrelevant information that creates noise interference, distracting the model and thus reducing the accuracy of generated SQL queries.
[0038] Exemplary application scheme
[0039] In view of this, embodiments of this application provide a method for question answering, which dynamically constructs a database schema related to the natural language question input by the current user, and guides the target model to directly generate SQL query statements based on the database schema and the natural language question by utilizing prompt word engineering. On the one hand, it eliminates the need for end-to-end training and fine-tuning of the target model, adapting to frequently changing database environments; on the other hand, it eliminates the need to use the entire database schema as input to the target model, instead using the constructed database schema related to the natural language question as input, removing noise interference irrelevant to the natural language question and reducing the input size of the target model, thereby significantly improving the accuracy of the generated SQL query statements and effectively reducing the computational burden.
[0040] Figure 1 An exemplary flowchart of a question-and-answer method 100 according to some embodiments of this application is shown, which includes the following steps S101 to S105.
[0041] In step S101, the user inputs a natural language question.
[0042] In step S102, a target node set is determined based on the natural language question and the correlation between nodes in the question-and-answer database relationship graph generated in advance based on the question-and-answer database, wherein the question-and-answer database relationship graph uses table names in the question-and-answer database as nodes and foreign keys as edges.
[0043] In this embodiment, the question-answering database is a relational database that stores all structured data that can be used to answer user questions. It can be understood that the question-answering database includes multiple interconnected data tables, and these tables are linked through foreign keys. A foreign key is a field in one data table that points to the primary key of another table, used to establish the relationship between the tables.
[0044] Therefore, this embodiment pre-generates a question-and-answer database relationship graph based on the question-and-answer database, with table names as nodes and foreign keys as edges, which fully describes the global topology between tables in the question-and-answer database. In this question-and-answer database relationship graph, each node corresponds to a specific data table, and each edge represents a foreign key connection between data tables. Furthermore, the question-and-answer database relationship graph can be either undirected or directed; this embodiment does not impose any specific limitations on this.
[0045] It should be noted that if the question-answering database itself is a graph database such as Neo4j or Nebula Graph, then there is no need to generate an additional question-answering database relationship graph; the graph structure built into the graph database can be used directly as the relationship graph.
[0046] Based on this, this embodiment determines the target node set according to the relevance of nodes in the natural language question and question-answering database relationship graph, recording all nodes related to the natural language question and the edge relationships between nodes. In other words, the target node set records the table names of all data tables related to the natural language question and the foreign key connections between data tables.
[0047] In step S103, the target node set is mapped to a database schema.
[0048] A database schema is a formal description of the logical structure and organization of a database, including: table definitions, relationships between tables, integrity constraints, etc.
[0049] In some embodiments, mapping a target node set to a database schema includes: obtaining the schema of each node in the question-answering database based on each node in the target node set; the schema includes at least table names, column names, and the data types corresponding to the column names; then adding semantic annotations consistent with the language of the natural language question to the table names and / or column names and / or data types in the schema of each node; and adding foreign key semantic annotations consistent with the language of the natural language question to the corresponding column names in the schema of nodes with edge relationships, so as to obtain the mapped database schema. It can be understood that the edge relationship of a node is a foreign key, therefore, the column names belonging to the foreign key can be found in the schema of nodes with edge relationships, and then foreign key semantic annotations consistent with the language of the natural language question can be added to these column names. The database schema obtained by this mapping only contains all tables, columns, and their foreign key connections related to the natural language question, as well as semantic annotations consistent with the language of the natural language question. On the one hand, this removes noise interference irrelevant to the natural language question, significantly reducing the input size of the target model; on the other hand, the semantic annotations provide the target model with clear and accurate contextual structure information. The combined effect of these two factors helps the target model accurately generate SQL query statements and effectively reduces the computational burden.
[0050] For example, if the user inputs a natural language question in Chinese, and a table in a schema is named "Department" in English, the languages are clearly different. Therefore, a semantic comment indicating "department" can be added after the table name "Department," resulting in "Department / / department." Furthermore, the table "Department" includes a column "DepartmentID," which is a foreign key pointing to the "DepartmentID" column in the table "Employee." This means "DepartmentID" is the primary key. Therefore, a foreign key semantic comment indicating "identifies the department of the employee in the employee table" can be added after "DepartmentID."
[0051] In step S104, the database schema and the natural language problem are used as inputs to the target model to generate the SQL query statement.
[0052] In this embodiment, the mapped database schema contains all tables, columns, and foreign key connections related to the natural language question, as well as semantic annotations consistent with the language of the natural language question. This provides the target model with the sufficient structured contextual information necessary to generate accurate SQL queries. Therefore, the target model does not need to memorize or process the entire large and complex question-and-answer database schema. Instead, it can directly achieve efficient and accurate conversion from natural language questions to SQL queries by dynamically injecting the mapped database schema related to the current user's input natural language question. This design not only significantly reduces the computational and cognitive burden on the target model but also gives it a natural adaptability to changes in the question-and-answer database structure. When the question-and-answer database structure changes, only the question-and-answer database relationship graph needs to be updated, without any retraining or fine-tuning of the target model itself, to continuously ensure the accuracy of generated SQL queries.
[0053] It should be noted that this embodiment does not limit the specific selection of the target model; any existing model capable of generating SQL query statements can be used. For example, a Large Language Model (LLM) can be used as the target model. To further improve the ability to generate complex queries, a 32-bit LLM with a larger number of parameters and stronger code capabilities, such as Qwen2.5-Coder:32B, can be preferred.
[0054] In step S105, a natural language answer to the natural language question is generated based on the query results of the SQL query statement in the question-and-answer database.
[0055] In this embodiment, after generating the SQL query statement corresponding to the natural language question input by the current user, the SQL query statement can be executed in the question-and-answer database to obtain the query results, which are a structured data set. To provide the user with an intuitive and easy-to-understand final answer, prompt engineering is further used to guide the target model to generate a natural language answer that conforms to human language habits based on the query results and the natural language question, and then output and display it to the user. This embodiment does not impose specific limitations on this process.
[0056] Prompt engineering is a key technology in artificial intelligence, especially in the field of large language models such as GPT. It refers to the careful design and optimization of input prompts to guide the model to generate the expected output more efficiently and accurately. Its core goal is to stimulate the model's best performance with the most appropriate input, similar to "how to ask a question to get a better answer".
[0057] In some embodiments, after performing step S105, the natural language question and its target node set, SQL query statement, natural language answer, duration of generating the natural language answer, and user feedback based on the natural language answer can be associated, saved, and displayed to the user. Furthermore, the target model can be optimized based on the saved content.
[0058] The above combination Figure 1 This application provides a detailed description of a question-answering method 100 according to some embodiments. This method determines a target node set based on the correlation between nodes in a pre-generated question-answering database relationship graph (where the database name is a node and foreign keys are edges) and the user-input natural language question. The target node set is then mapped to a database schema. Next, the database schema and the natural language question are used as input to the target model to generate an SQL query. Finally, a natural language answer to the natural language question is generated based on the query results in the question-answering database. Thus, this application embodiment dynamically constructs a database schema related to the current user-input natural language question, enabling the target model to directly understand the question-answering database structure and generate accurate SQL queries based on this schema. This eliminates the need for end-to-end training of the target model, automatically adapts to changes in the question-answering database structure, and effectively reduces computational burden.
[0059] Figure 2 The following is an exemplary flowchart of some embodiments of the question-answering method 200, which can be used as a specific implementation of step S102 in method 100 above. Therefore, the preceding text is combined with... Figure 1 The described features can be similarly applied here. For example... Figure 2 As shown, method 200 includes the following steps S201 to S205.
[0060] In step S201, candidate nodes are determined based on the similarity between the natural language question and each node in the question-answering database relationship graph.
[0061] In this embodiment, nodes with a similarity score less than or equal to a second similarity threshold are considered potentially relevant to the natural language problem and are thus identified as candidate nodes. It should be noted that, to cover as many initial nodes as possible that are actually relevant to the current natural language problem, the second similarity threshold needs to be set to a relatively large value to obtain all candidate nodes that may be relevant to the natural language problem.
[0062] In some embodiments, in step S201 above, when performing similarity calculation, the language of the node must be consistent with the language of the natural language question. That is, the similarity is calculated between nodes with the same language and the natural language question to obtain the second similarity threshold.
[0063] Specifically, in some embodiments, the table names in the question-and-answer database can be translated into a language consistent with the language of the natural language question in advance, and the table names and their corresponding translations can be associated and saved. Then, when performing the above step S201, the translations associated with the nodes (table names) are obtained, and candidate nodes are determined based on the similarity between the natural language question and the translations associated with each node, thereby ensuring that the language of the nodes is consistent with the language of the natural language question when performing similarity calculations as described above.
[0064] In some embodiments, each table name and its corresponding translation can be associated and stored in the node database, so that when performing the above step S201, the translation associated with the node (table name) can be obtained from the node database.
[0065] For example, the user inputs a natural language question in Chinese, while a table named "Department" exists in English. Clearly, the table name and the language of the natural language question are inconsistent. In this case, each table name is pre-translated into Chinese to obtain the translated table name, and then the table name and its corresponding translation are associated and stored in the node database.
[0066] In some embodiments, when performing the above step S201, candidate table names can also be determined directly based on the similarity between the translations associated with each table name stored in the node database and the natural language problem. These candidate table names are candidate nodes. This embodiment does not specifically limit this.
[0067] In step S202, the target model is guided by prompt word engineering to select initial nodes from the candidate nodes based on the natural language question.
[0068] In this embodiment, the candidate nodes that are potentially related to the natural language problem have already been screened out by setting a large second similarity threshold. Here, the target model is used to perform a second precise screening from the candidate nodes to obtain all the initial nodes that are actually related to the natural language problem. Thus, efficient and accurate screening of the initial nodes can be achieved without missing any possible candidate nodes.
[0069] In step S203, the initial nodes are combined in pairs to obtain multiple sets of child nodes. It can be understood that the set of child nodes obtained by combining the initial nodes in pairs includes two initial nodes.
[0070] In step S204, the shortest path between the two initial nodes in the child node set is determined in the question-and-answer database relationship graph.
[0071] In step S205, all intermediate nodes on the shortest path are merged into the child node set to obtain the target node set.
[0072] In this embodiment, all intermediate nodes on the shortest path are considered as related nodes to the initial node, and are also actually relevant to the natural language problem. Therefore, they are merged into the child node set, and the merged child node set constitutes the target node set. The resulting target node set records all nodes actually related to the natural language problem and the edge relationships between nodes. In other words, the target node set records all table names related to the natural language problem and the foreign key connections between the tables.
[0073] In some embodiments, after obtaining the target node set by performing the above step S205, the target node set is further deduplicated to ensure the uniqueness of the nodes in the target node set.
[0074] As an optional implementation, all intermediate nodes on the shortest path can be merged into the child node set according to edge relationships, so that the final target node set not only includes all nodes actually related to the natural language problem, but also maintains the complete and correct edge relationships between these nodes. In other words, the target node set not only includes all the necessary data tables, but also maintains the foreign key connections between these data tables.
[0075] As an example, the child node set includes node 1 and node 2. The shortest path between node 1 and node 2 is found in the question-and-answer database graph. This shortest path may also include intermediate nodes in addition to node 1 and node 2. For example, the shortest path could be: node 1-node 3-node 4-node 5-node 2. Then, intermediate nodes 3, 4, and 5 are merged into the child node set, resulting in the target node set {node 1, node 3, node 4, node 5, node 2}.
[0076] The above combination Figure 2The method 200 for question answering, according to some embodiments of this application, accurately obtains all initial nodes actually related to the natural language question by combining similarity filtering and target model filtering guided by prompt word engineering. Then, the initial nodes are paired to obtain multiple sets of child nodes. Next, the shortest path between two initial nodes in the question-answering database relationship graph is determined. Finally, all intermediate nodes on the shortest path are merged into the child node sets, and all child node sets constitute the target node set. This method efficiently mines the implicit relationships between initial nodes, fully expands the range of nodes actually related to the natural language question, ensures the comprehensiveness and accuracy of subsequent database schema mapping, and provides a solid data foundation for accurate question answering.
[0077] Figure 3 The present application shows a method 300 for question answering, which can be used as a specific implementation of step S201 in method 200 above. Therefore, the foregoing is combined with Figure 2 The described features can be similarly applied here. For example... Figure 3 As shown, method 300 includes the following steps S301 to S302.
[0078] In step S301, common nouns are extracted from the natural language problem.
[0079] In this embodiment, generic names refer to nouns that can refer to anything. They have no specific meaning and can be used to describe various different objects. For example, "item," "concept," and "event" are all generic names.
[0080] User-generated natural language questions typically contain rich instance descriptions and detailed information. This embodiment effectively removes these distractions by identifying and extracting common nouns from the questions, thereby accurately capturing their core semantic concepts. This method transforms specific, instanced question statements into abstract, categorized semantic representations, achieving precise mapping to database table names. Through semantic-level concept alignment, accurate and comprehensive initial nodes can be obtained, thereby improving the accuracy of subsequent SQL query generation.
[0081] In some embodiments, cue word engineering can be used to guide the target model to extract general nouns from natural language problems. Specifically, the cue word engineering guides the target model to perform the following steps: extract all nouns from the natural language problem; then identify proper nouns and concrete nouns among the nouns; and replace the proper nouns and concrete nouns with category nouns, using the category nouns and other nouns (excluding proper nouns and concrete nouns) as general nouns. Proper nouns are the names of specific people, places, or institutions, i.e., personal names, place names, unit names, organization names, etc. Concrete nouns refer to physical objects or substances with actual forms that can be directly perceived through the senses (sight, hearing, touch, taste, smell).
[0082] For example, if a user inputs a natural language question like "What is the sales volume of TH mobile phones in Province A?", the extracted nouns are "Province A, TH mobile phones, sales volume". Province A is a proper noun, and TH mobile phones is a specific noun. Then, Province A is replaced with "region", and TH mobile phones are replaced with "communication tools". Finally, the general nouns are "region, communication tools, sales volume".
[0083] In step S302, candidate nodes are determined based on the similarity between the common nouns and the nodes in the question-and-answer database relationship graph.
[0084] In this embodiment, nodes with a similarity score less than or equal to a second similarity threshold are considered potentially relevant to the natural language problem and are thus identified as candidate nodes. Similarly, as described above, this second similarity threshold needs to be set to a larger value to obtain all candidate nodes potentially relevant to the natural language problem.
[0085] As described above, when performing similarity calculations, the language of the nodes must be consistent with the language of the natural language problem. Therefore, step S302 involves obtaining the translations associated with the nodes (table names) and then determining candidate nodes based on the similarity between common nouns and the translations associated with each node, thereby ensuring that, as described above, the language of the nodes is consistent with the language of the natural language problem when performing similarity calculations.
[0086] In some embodiments, when performing step S302 above, candidate table names can also be determined directly based on the similarity between the common nouns and the translations associated with each table name stored in the node database. These candidate table names are candidate nodes. This embodiment does not specifically limit this.
[0087] In some embodiments, Euclidean distance can be used to represent the similarity between common terms and nodes in the question-and-answer database relationship graph. This embodiment does not specifically limit this. Based on this, the second similarity threshold can be set to [0.7, 1]. It can be understood that the larger the Euclidean distance, the lower the similarity, and the smaller the Euclidean distance, the higher the similarity.
[0088] The above combination Figure 3 The present application provides a detailed description of a question-answering method 300 according to some embodiments. This method extracts common nouns from natural language questions and then determines candidate nodes based on the similarity between the common nouns and nodes in the question-answering database relationship graph. This method can obtain all candidate nodes that may be related to the natural language question, laying a solid foundation for the subsequent determination of the target node set, thereby significantly improving the accuracy and reliability of the final generated SQL query statement.
[0089] Figure 4 The present application illustrates a method 400 for question answering, which, as an additional technical solution to the method 100 described above, can be executed after step S104. Therefore, the preceding text is combined with... Figure 1 The described features can be similarly applied here. For example... Figure 4 As shown, method 400 includes the following steps S401 to S403.
[0090] In step S401, the SQL query statement is subjected to syntax and validity checks.
[0091] In this embodiment, syntax validation refers to checking whether the structure of the SQL query statement conforms to the specifications, including basic syntax rules such as keyword usage, bracket matching, delimiters, and statement termination characters, ensuring that it is a formally valid SQL query statement. Legality validation verifies the executability of the SQL query statement in the question-and-answer database, including checking the existence of table and column names, the rationality of data type operations, the correctness of function usage, and whether permissions and constraints are met. Through these two layers of validation, runtime errors and security risks in SQL query statements can be effectively avoided, ensuring that the SQL query statement can be correctly parsed by the question-and-answer database and executed securely and efficiently.
[0092] In step S402, if the syntax check fails, the SQL query statement and the syntax check result are used as inputs to the target model to correct the SQL query statement.
[0093] In this embodiment, after correcting the SQL query statement, the above step S401 is continued.
[0094] In some systems, the number of corrections to the target model can be set. If the SQL query still fails syntax validation after exceeding this number of corrections, a corresponding warning message will be output.
[0095] In step S403, if the validity check fails, a corresponding warning message is output.
[0096] In this embodiment, because syntax errors typically have clear correction patterns, the target model can specifically correct the SQL query statement based on the syntax check result when the syntax check fails. For cases where the validity check fails, a warning message containing detailed error information is directly output to prompt the user for further review and processing, thus avoiding any impact on the integrity and security of the question-and-answer database.
[0097] The above combination Figure 4 The present application provides a detailed description of a question-and-answer method 400 in some embodiments. By performing dual checks on the SQL query statement, including syntax and validity checks, it ensures that the final executed SQL query statement conforms to the SQL syntax specification and is consistent with the actual structure and constraints of the question-and-answer database, thereby effectively improving the accuracy and reliability of the generated SQL query statement.
[0098] Figure 5 The present application illustrates a method 500 for question answering, which serves as an additional technical solution to the method 100 described above. Therefore, the preceding text is combined with... Figure 1 The described features can be similarly applied here. For example... Figure 5 As shown, method 500 includes the following steps S501 to S504.
[0099] In step S501, the natural language problem and the set of target nodes used are associated and cached to form a historical cache.
[0100] In some embodiments, a natural language problem and the set of target nodes used can be associated and cached in a cache database.
[0101] In some embodiments, before performing step S501, feedback information from the user on the natural language answer can also be received. If the feedback information indicates that the natural language answer meets the user's expectations, the natural language question and the set of target nodes used are associated and cached to form a historical cache. If the feedback information indicates that the natural language answer does not meet the user's expectations, the current natural language question and the set of target nodes used are not associated and cached to ensure that the data in the historical cache can also generate accurate SQL query statements in the future.
[0102] In some embodiments, natural language questions that take a long time to generate natural language answers and the set of target nodes used can be associated and cached to form a historical cache, thereby improving the processing efficiency of subsequent natural language questions of the same type.
[0103] In step S502, when a new natural language question is received from the user, the similarity between the new natural language question and each historical natural language question in the historical cache is determined.
[0104] In some embodiments, as can be seen from the above description, historical natural language questions and the set of historical target nodes used are associated and stored in a cache database. Therefore, when performing the above step S502, the similarity between the newly asked natural language question and the historical natural language questions stored in the cache database can be calculated to determine the similarity.
[0105] In step S503, if there are historical natural language questions with a similarity less than or equal to the first similarity threshold, then the set of historical target nodes associated with the historical natural language questions is used as the set of target nodes corresponding to the newly asked natural language question.
[0106] In step S504, if there are no historical natural language questions with a similarity less than or equal to the first similarity threshold, then the step of determining the target node set based on the correlation between the natural language questions and the nodes in the question-answer database relationship graph generated in advance based on the question-answer database is executed.
[0107] In this embodiment, when a new natural language question is received from a user, its similarity to each historical natural language question in the historical cache is first calculated. If there is a historical natural language question with a similarity less than or equal to a first similarity threshold, the set of historical target nodes associated with that historical natural language question is directly used as the target node set of the current natural language question. Therefore, after executing step S101 of method 100, step S103 can be executed directly without executing step S102, thus improving the efficiency of generating SQL query statements and also improving the efficiency of generating natural language answers. It should be noted that here, caching is determined directly based on similarity; therefore, the first similarity threshold is a relatively small value.
[0108] In some embodiments, Euclidean distance can be used to represent the similarity between a natural language problem and a historical natural language problem. In other words, the Euclidean distance between a natural language problem and a historical natural language problem can be calculated, and then the historical target nodes associated with the historical natural language problem whose Euclidean distance is less than or equal to a first similarity threshold can be combined as the target node set of the current natural language problem.
[0109] In some embodiments, the first similarity threshold may be set to 0.4.
[0110] Furthermore, to improve the hit rate of the historical cache, step S501 includes: vectorizing the natural language question to obtain a vector representation of the natural language question. Then, the vector representation of the natural language question and the set of target nodes used are associated and cached to form a historical cache. Therefore, when performing step S502, the specific steps are: when a new natural language question is received from a user, the new natural language question is vectorized to obtain a vector representation of the new natural language question. Then, the similarity between the vector representation of the new natural language question and the historical vector representations of each historical natural language question in the historical cache is determined. Thus, by introducing vector representation, this embodiment upgrades the cache matching mechanism from traditional literal matching to semantic-level matching. Semantic matching can identify synonyms, semantic associations, and intent similarities in natural language questions, thereby significantly improving the hit rate of the historical cache.
[0111] The above combination Figure 5 This application provides a detailed description of a question-answering method 500 in some embodiments. This method associates and caches natural language questions and their associated target node sets to form a historical cache. Then, when a new natural language question is received from a user, the similarity between the new natural language question and each historical natural language question in the historical cache is calculated. If a historical natural language question exists with a similarity less than or equal to a first similarity threshold, the historical target node set associated with that historical natural language question is used as the target node set corresponding to the natural language question. If no historical natural language question exists with a similarity less than or equal to the first similarity threshold, the step of determining the target node set based on the correlation between the natural language question and the nodes in a pre-generated question-answering database relationship graph is performed. This improves the efficiency of generating natural language answers by enhancing the efficiency of generating SQL query statements.
[0112] This application also provides a question-answering system, including a receiving module for receiving natural language questions input by a user; a parsing module for determining a target node set based on the natural language question and the relevance between nodes in a pre-generated question-answering database relationship graph, wherein the question-answering database relationship graph uses table names in the question-answering database as nodes and foreign keys as edges; a mapping module for mapping the target node set to a database schema; a query statement generation module for taking the database schema and the natural language question as input to the target model to generate an SQL query statement; and an answer generation module for generating a natural language answer to the natural language question based on the query results of the SQL query statement in the question-answering database.
[0113] In some embodiments, the question-answering system further includes a validation module for performing syntax and validity checks on the SQL query statement; if the syntax check fails, the SQL query statement and the syntax check result are used as input to the target model to correct the SQL query statement; if the validity check fails, the corresponding warning message is output.
[0114] In some embodiments, the question-answering system further includes a caching module, configured to associate and cache natural language questions and the set of target nodes used therein to form a historical cache; and when a new natural language question is received from a user, to determine the similarity between the new natural language question and each historical natural language question in the historical cache; if there are historical natural language questions with a similarity less than or equal to a first similarity threshold, then the historical target node set associated with the historical natural language question is used as the target node set corresponding to the new natural language question; if there are no historical natural language questions with a similarity less than or equal to the first similarity threshold, then the step of determining the target node set based on the correlation between the natural language question and each node in the question-answering database relationship graph generated in advance based on the question-answering database is performed.
[0115] To implement the method steps described above in conjunction with the accompanying drawings at the software and hardware level, embodiments of this application also provide a processing apparatus, which can be as follows: Figure 6 The processing device shown. Figure 6 An exemplary structural block diagram of the processing apparatus 60 according to an embodiment of this application is shown, such as... Figure 6 As shown, the processing device 60 of this application may include a processor 610 and a memory 620. The memory 620 stores an executable program, which the processor 610 can load and execute, enabling the processing device 60 to implement any of the method steps described above.
[0116] In one example scenario, processor 610 can be used to control memory 620. Further, processor 610 can be a central processing unit (CPU), application processor (AP), or similar integrated within processing device 60; while memory 620, as hardware implementing storage functions, can be read-only memory (ROM), dynamic RAM (DRAM), or similar.
[0117] This application also provides a computer-readable storage medium storing program instructions that, when executed by a processor of a processing device, cause the processor to perform the method steps described in any embodiment of this application.
[0118] In this application embodiment, a computer program product is also provided, including a computer program or instructions, which, when executed by a processor, implement the method described in any embodiment of this application.
[0119] While numerous embodiments of this application have been shown and described herein, it will be apparent to those skilled in the art that such embodiments are provided by way of example only. Many modifications, alterations, and alternatives will arise for those skilled in the art without departing from the spirit and intent of this application. It should be understood that various alternatives to the embodiments of this application described herein may be employed in the practice of this application. The appended claims are intended to define the scope of protection of this application and therefore cover equivalents or alternatives within the scope of these claims.
Claims
1. A method for question answering, characterized in that, include: The problem of receiving natural language input from users; Based on the natural language question and the correlation between nodes in the question-answer database relationship graph generated in advance based on the question-answer database, a target node set is determined, wherein the question-answer database relationship graph uses table names in the question-answer database as nodes and foreign keys as edges; Map the target node set to a database schema; The database schema and the natural language question are used as inputs to the target model to generate SQL query statements; Based on the query results of the SQL query in the question-and-answer database, generate a natural language answer to the natural language question; The step of determining the target node set based on the correlation between nodes in the natural language question and the pre-generated question-answer database relationship graph includes: Candidate nodes are determined based on the similarity between the natural language question and each node in the question-answering database relationship graph; The target model is guided by prompt word engineering to select initial nodes from the candidate nodes based on the natural language question. The initial nodes are combined in pairs to obtain multiple sets of child nodes; Determine the shortest path between the two initial nodes within the set of child nodes in the question-and-answer database graph; All intermediate nodes on the shortest path are merged into the set of child nodes to obtain the target node set.
2. The method according to claim 1, characterized in that, The step of determining candidate nodes based on the similarity between the natural language question and each node in the question-answering database relationship graph includes: Extract common nouns from the natural language problem; The candidate nodes are determined based on the similarity between the common nouns and the nodes in the question-and-answer database relationship graph.
3. The method according to claim 2, characterized in that, The extraction of general terms from the natural language problem includes: The target model is guided by prompt word engineering to extract the common nouns from the natural language problem; wherein, The prompt word engineering guides the target model to perform the following steps: Extract all nouns from the natural language processing problem; Identify the proper nouns and concrete nouns within the stated nouns; Replace the proper nouns and specific nouns with category nouns; The category nouns and other nouns within the category are referred to as the general nouns.
4. The method according to claim 1 or 2, characterized in that, The step of determining candidate nodes based on the similarity between the natural language question and each node in the question-answering database relationship graph includes: When performing similarity calculations, the language of the node is consistent with the language of the natural language problem.
5. The method according to claim 1, characterized in that, The step of mapping the target node set to a database schema includes: Based on each node in the target node set, the schema of each node is obtained by querying the question-and-answer database. The schema includes at least the table name, column name, and the data type corresponding to the column name. Add semantic annotations to the table names and / or column names and / or data types in the schema of each node, consistent with the language of the natural language problem. Add foreign key semantic annotations to the column names in the schema of nodes with edge relationships, which are consistent with the language of the natural language problem, to obtain the mapped database schema.
6. The method according to claim 1, characterized in that, The process of using the database schema and the natural language problem as input to the target model to generate an SQL query statement further includes: Perform syntax and validity checks on the SQL query statement; If the syntax check fails, the SQL query statement and the syntax check result are used as input to the target model to correct the SQL query statement. If the validity check fails, a corresponding warning message will be output.
7. The method according to claim 1, characterized in that, The step of generating a natural language answer to the natural language question based on the query results in the question-and-answer database using the SQL query statement includes: Execute the SQL query statement in the question-and-answer database to obtain the query results; The target model is guided by prompt word engineering to generate a natural language answer based on the query results and the natural language question.
8. The method according to claim 1, characterized in that, The method further includes: The natural language problem and the set of target nodes used are associated and cached to form a historical cache; When a new natural language question is received from a user, the similarity between the new natural language question and each historical natural language question in the historical cache is determined. If there are historical natural language questions with a similarity less than or equal to the first similarity threshold, then the set of historical target nodes associated with the historical natural language questions will be used as the set of target nodes corresponding to the newly asked natural language question. If there are no historical natural language questions with a similarity less than or equal to the first similarity threshold, then the step of determining the target node set based on the correlation between the natural language questions and the nodes in the question-answer database relationship graph generated in advance based on the question-answer database is performed.
9. The method according to claim 8, characterized in that, The step of associating and caching the natural language problem and the set of target nodes used to form a historical cache includes: Receive feedback information from the user regarding the natural language response; If the feedback information indicates that the natural language answer meets the user's expectations, then the natural language question and the set of target nodes used are associated and cached to form a historical cache.
10. The method according to claim 8, characterized in that, The step of associating and caching the natural language problem and the set of target nodes used to form a historical cache includes: The natural language problem is vectorized to obtain a vector representation of the natural language problem; The vector representation of the natural language problem and the set of target nodes used are associated and cached to form a historical cache; When receiving a new natural language question from a user, determining the similarity between the new natural language question and each historical natural language question in the historical cache includes: When a new natural language question is received from a user, the new natural language question is vectorized to obtain a vector representation of the new natural language question. Determine the similarity between the vector representation of the new natural language question and the historical vector representations of each historical natural language question in the history cache.
Citation Information
Patent Citations
Natural language query method based on deep learning
CN111522839A
Structured query statement generation method, question and answer processing method and corresponding devices
CN118427215A
Database query method and device, electronic equipment and nonvolatile storage medium
CN119226315A
Natural language query method and system for relational database
CN119917519A
Query statement conversion method and device based on large model, equipment and medium
CN120578683A