Database query method and device based on natural language, equipment and medium

Through the natural language-based database query method, users can input data through natural language, generate SQL statements and execute database queries, solving the problem that users need to write SQL statements by themselves in the prior art, and achieving the effect of simplifying operation processes and improving user experience.

CN120011385APending Publication Date: 2025-05-16SHENHUA INFORMATION TECH CO LTD +1
View PDF 0 Cites 3 Cited by

Patent Information

Application Number
CN202510101490.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-22
Publication Date
2025-05-16

AI Technical Summary

Technical Problem

In the prior art, users need to write SQL statements by themselves for database query, which increases the complexity of operations and affects the user experience.

Method used

Provide a database query method based on natural language. By obtaining natural language data input by users, generating target vectors, determining the SQL corpus based on external knowledge base, using the SQL generation model built with artificial intelligence technology to generate target SQL statements, and executing it in a pre-built database to display the query results.

Benefits of technology

It simplifies the operation process of database query, so users can conduct database query without being familiar with SQL syntax, improving user experience and work efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120011385A_ABST
    Figure CN120011385A_ABST
Patent Text Reader

Abstract

The invention provides a database query method and device based on a natural language, equipment and a medium, and relates to the technical field of natural language process.The method comprises the steps that to-be-queried data input by a user is obtained, and a target vector corresponding to the to-be-queried data is generated; wherein the to-be-queried data is natural language data; according to the target vector, based on an external knowledge base, determining an SQL corpus set corresponding to the to-be-queried data; wherein the SQL corpus set comprises a plurality of target SQL corpora stored in the external knowledge base; based on the to-be-queried data and the SQL corpus set, generating a target SQL statement corresponding to the to-be-queried data through an SQL generation model constructed by using an artificial intelligence technology; and executing the target SQL statement in a pre-constructed database, and displaying a query result. According to the method and the device, the problems that the operation complexity is increased and the user experience is influenced due to the fact that a user needs to write SQL statements by himself for database query in the prior art can be effectively solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of natural language processing, and in particular to a database query method based on natural language, a database query device based on natural language, an electronic device, and a machine-readable storage medium. Background Art

[0002] In data processing and database query methods, users usually need to manually write SQL (Structured Query Language) statements to retrieve, update, delete or insert data from the database. For users who are proficient in SQL, this method provides flexibility and precise control. However, for users who lack SQL knowledge, writing correct and efficient SQL statements is often challenging. Even if they can complete the query, it may be difficult to ensure the optimal performance of the query, which in turn affects work efficiency and data processing accuracy.

[0003] For non-technical users, relying on SQL knowledge and writing ability has become a bottleneck for using certain systems or tools. Users without SQL background need to spend a lot of time learning syntax or rely on technical support, which not only consumes their precious time, but also greatly reduces work efficiency. Summary of the invention

[0004] The purpose of the embodiments of the present application is to provide a natural language-based database query method, a natural language-based database query device, equipment and medium, so as to solve the problem in the prior art that users need to write SQL statements themselves to perform database queries, which increases the complexity of operations and affects the user experience.

[0005] In order to achieve the above-mentioned object, the first aspect of the present application provides a database query method based on natural language, the method comprising:

[0006] Acquire the data to be queried input by the user, and generate a target vector corresponding to the data to be queried; wherein the data to be queried is natural language data;

[0007] According to the target vector corresponding to the data to be queried, based on the external knowledge base, determining the SQL corpus corresponding to the data to be queried; wherein the SQL corpus includes a plurality of target SQL corpora stored in the external knowledge base;

[0008] Based on the data to be queried and the SQL corpus corresponding to the data to be queried, a target SQL statement corresponding to the data to be queried is generated by a SQL generation model constructed by using artificial intelligence technology;

[0009] Execute the target SQL statement in the pre-built database and display the query results.

[0010] In the embodiment of the present application, before generating the target vector corresponding to the to-be-queried data, the method further includes:

[0011] Determine the data type of the data to be queried;

[0012] If the data type is voice data, voice recognition processing is performed on the data to be queried to determine text data corresponding to the data to be queried.

[0013] In the embodiment of the present application, generating a target vector corresponding to the data to be queried includes:

[0014] Performing semantic analysis on the data to be queried to extract a keyword set;

[0015] Performing vector conversion on each keyword in the keyword set to obtain multiple target sub-vectors;

[0016] The target vector includes various target sub-vectors.

[0017] In the embodiment of the present application, the external knowledge base stores a plurality of SQL corpora and a corpus vector corresponding to each SQL corpus;

[0018] Determining, according to the target vector corresponding to the data to be queried and based on an external knowledge base, a SQL corpus corresponding to the data to be queried, including:

[0019] Determine the similarity between the target vector and each corpus vector;

[0020] Sort each similarity in descending order, determine the corpus vectors corresponding to the first N similarities in the sorting result as the pending corpus vectors, and determine the SQL corpus corresponding to the pending corpus vectors as the pending SQL corpus; where N is a preset positive integer;

[0021] The SQL corpus set is obtained by rearranging each pending SQL corpus through a pre-built rearrangement model; wherein the number of SQL corpora included in the SQL corpus set is less than N.

[0022] In an embodiment of the present application, based on the data to be queried and the SQL corpus corresponding to the data to be queried, a target SQL statement corresponding to the data to be queried is generated by using an SQL generation model constructed by using artificial intelligence technology, including:

[0023] Determining a statement generation prompt word of the SQL generation model according to the keyword set and the SQL corpus corresponding to the data to be queried;

[0024] The target SQL statement is generated according to the statement generation prompt word and the data to be queried, and the target SQL statement is generated through the SQL generation model.

[0025] In the embodiment of the present application, before executing the target SQL statement in the pre-built database, the method further includes:

[0026] The target SQL statement is logically verified by using the SQL generation model, and the verified SQL statement is replaced with the target SQL statement.

[0027] In the embodiment of the present application, before executing the target SQL statement in the pre-built database, the method further includes:

[0028] The target SQL statement is optimized by using the SQL generation model, and the optimized SQL statement is replaced with the target SQL statement.

[0029] A second aspect of the present application provides a database query device based on natural language, the device comprising:

[0030] A target vector generation module, used to obtain the to-be-queried data input by the user, and generate a target vector corresponding to the to-be-queried data; wherein the to-be-queried data is natural language data;

[0031] An SQL corpus generation module, configured to determine, based on an external knowledge base and according to a target vector corresponding to the data to be queried, an SQL corpus corresponding to the data to be queried; wherein the SQL corpus includes a plurality of target SQL corpora stored in the external knowledge base;

[0032] A target SQL statement generation module, used to generate a target SQL statement corresponding to the data to be queried based on the data to be queried and the SQL corpus corresponding to the data to be queried, by using an SQL generation model constructed using artificial intelligence technology;

[0033] The database query module is used to execute the target SQL statement in a pre-built database and display the query results.

[0034] The third aspect of the present application provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the natural language-based database query method described in the first aspect when executing the computer program.

[0035] A fourth aspect of the present application provides a machine-readable storage medium having instructions stored thereon, which, when executed by a processor, configures the processor to execute the natural language-based database query method described in the first aspect above.

[0036] The natural language-based database query method, device, equipment and medium provided in this application realize database query using natural language. After the user inputs the data to be queried using natural language, the data to be queried is converted into a target SQL statement based on an external knowledge base and an SQL generation model, the database query is executed, and the query results are displayed to the user. The entire process does not require the participation of developers, which simplifies the operation process of database query. This method is applicable to database query requirements in various business systems, and users can implement database query through natural language without being familiar with SQL syntax.

[0037] Other features and advantages of the embodiments of the present application will be described in detail in the subsequent specific implementation section. BRIEF DESCRIPTION OF THE DRAWINGS

[0038] The accompanying drawings are used to provide a further understanding of the embodiments of the present application and constitute a part of the specification. Together with the following specific implementations, they are used to explain the embodiments of the present application, but do not constitute a limitation on the embodiments of the present application. In the accompanying drawings:

[0039] Figure 1 The flowchart of the natural language-based database query method according to an embodiment of the present application is schematically shown;

[0040] Figure 2 A schematic diagram of a call link of a database query example in an embodiment of the present application is schematically shown;

[0041] Figure 3 A schematic diagram of a process flow of a database query example of an embodiment of the present application is schematically shown;

[0042] Figure 4 The structure block diagram of the natural language-based database query device according to an embodiment of the present application is schematically shown;

[0043] Figure 5 The internal structure diagram of the computer device according to the embodiment of the present application is schematically shown.

[0044] Description of Reference Numerals

[0045] A01-processor; A02-network interface; A03-internal memory; A04-display screen; A05-input device; A06-non-volatile storage medium; B01-operating system; B02-computer program. DETAILED DESCRIPTION

[0046] In order to make the purpose, technical solution and advantages of the embodiments of the present application clearer, the technical solution in the embodiments of the present application will be clearly and completely described below in conjunction with the drawings in the embodiments of the present application. It should be understood that the specific implementation methods described herein are only used to illustrate and explain the embodiments of the present application, and are not used to limit the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0047] It should be noted that if the embodiments of the present application involve directional indications (such as up, down, left, right, front, back, etc.), such directional indications are only used to explain the relative position relationship, movement status, etc. between the components under a certain specific posture (as shown in the accompanying drawings). If the specific posture changes, the directional indication will also change accordingly.

[0048] In addition, if there are descriptions involving "first", "second", etc. in the embodiments of the present application, the descriptions of "first", "second", etc. are only used for descriptive purposes and cannot be understood as indicating or suggesting their relative importance or implicitly indicating the number of technical features indicated. Therefore, the features defined as "first" and "second" may explicitly or implicitly include at least one of the features. In addition, the technical solutions between the various embodiments can be combined with each other, but they must be based on the ability of ordinary technicians in the field to implement them. When the combination of technical solutions is contradictory or cannot be implemented, it should be deemed that such combination of technical solutions does not exist and is not within the scope of protection required by this application.

[0049] With the rapid development of big data and artificial intelligence technologies, enterprises have an increasing demand for data processing and analysis. Especially in the face of complex and changing data models and continuously evolving business needs, traditional fixed query design has become increasingly inefficient and costly.

[0050] In a fast-iterating business environment, data models (such as database table structures) often need to be adjusted frequently to cope with new business scenarios. This dynamic demand requires data processing systems to have higher flexibility and scalability, while inherent query design cannot adapt to frequent changes in data models. Whenever the data model changes, additional development work is required, which not only increases development costs, but also reduces the convenience and agility of the system.

[0051] For scenarios that require frequent adjustments to data models, the traditional software development process (covering requirements analysis, design, coding, testing, etc.) is too cumbersome. Any small change in the data model may trigger a series of development and testing work, seriously hindering the product iteration speed and the ability to respond to market changes.

[0052] In view of the fact that users in the related art need to write SQL statements themselves to perform database queries, which increases the complexity of operations and affects the user experience, the present application provides a database query method, device, equipment and medium based on natural language. The following, in conjunction with the accompanying drawings, the database query method, device, equipment and medium based on natural language provided by the present application are described in detail through specific embodiments and implementation methods.

[0053] Figure 1 The flowchart of the natural language-based database query method of the embodiment of the present application is schematically shown. Figure 1 As shown, in one embodiment of the present application, a natural language-based database query method is provided, and the method may include the following steps.

[0054] Step 200: Obtain the to-be-queried data input by the user, and generate a target vector corresponding to the to-be-queried data.

[0055] The data to be queried input by the user is natural language data, specifically text data or voice data. In other words, the user can input the data to be queried by two methods: text input and voice input.

[0056] In an embodiment of the present application, for the acquired data to be queried, before generating a target vector corresponding to the data to be queried, the method further includes the following steps.

[0057] Step 110: determine the data type of the data to be queried.

[0058] Step 120: If the data type is voice data, perform voice recognition processing on the data to be queried to determine text data corresponding to the data to be queried.

[0059] In a specific example, step 120 may receive and parse the user input through a natural language processing module (such as using a deep learning-based speech recognition model) and convert the speech input into text for subsequent processing.

[0060] The embodiments of the present application provide a voice input method, which allows users to input the data to be queried more quickly and conveniently. Especially in specific scenarios (such as driving, walking, etc.) or for user groups with special needs, voice input provides great convenience.

[0061] In the embodiment of the present application, the target vector corresponding to the data to be queried includes a plurality of target sub-vectors, and the step 200 of generating the target vector corresponding to the data to be queried includes the following steps.

[0062] Step 210: semantically analyze the data to be queried to extract a keyword set.

[0063] In a specific example, the implementation method of step 210 is:

[0064] Based on the preset analysis prompt words, the SQL generation model constructed by using artificial intelligence technology is used to perform semantic analysis on the data to be queried, and a keyword set is extracted.

[0065] In this specific example, the specific operation of semantically analyzing the data to be queried includes steps such as word segmentation, part-of-speech tagging, and named entity recognition.

[0066] The SQL generation model may be an open source model qwen2.5-72B. The following is an example of extracting a keyword set using the model.

[0067] System prompt words of the model (i.e. preset parsing prompt words): [You are an engineer who is very familiar with database statements. There are the following tables in the system [---table structure information omitted here---]. Based on the user's question, please analyze what kind of database operation the user needs to perform, and clearly identify the user's operation intention, target database table, and specific query content].

[0068] User question (i.e. data to be queried): Please help me check the total amount of income from today's bank statements.

[0069] The model answers based on the system prompt words: [User intent: query; Target database table: bank statements; Specific query content: summary of transaction amounts].

[0070] That is, based on the questions entered by the user, the model extracts a set of keywords including query, bank statements, and summary transaction amounts.

[0071] In the embodiment of the present application, the keyword set includes user intent (such as query, update, delete, insert) and key entities and attributes involved (such as table name, field name), and the keyword set is important information for subsequently constructing the target SQL statement. Among them, user intent is an important basis for generating SQL statements and determines the type of SQL statements.

[0072] It should be understood that in the embodiment of the present application, when the data type of the data to be queried input by the user is voice data, the step 210 of performing semantic analysis on the data to be queried is specifically performing semantic analysis on the text data corresponding to the data to be queried.

[0073] Step 220 , performing vector conversion on each keyword in the keyword set to obtain multiple target sub-vectors.

[0074] In an embodiment of the present application, step 220 may select a commonly used vectorization method (such as Word2Vec, BERT) or an existing vectorization model (such as bge-large-zh) to achieve vector conversion.

[0075] The embodiment of the present application performs vector conversion on keywords so as to subsequently match the target vector corresponding to the to-be-queried data in an external knowledge base to obtain a corpus vector with high similarity, thereby generating a target SQL statement.

[0076] Step 300: Determine the SQL corpus corresponding to the data to be queried based on the target vector corresponding to the data to be queried and based on an external knowledge base.

[0077] Among them, the external knowledge base stores multiple SQL corpora and the corpus vectors corresponding to each SQL corpus, the SQL corpus includes a text description and its corresponding SQL statement, the corpus vector corresponding to the SQL corpus includes the SQL statement and the text introduction of the SQL statement, and the SQL corpus set includes multiple target SQL corpora stored in the external knowledge base. It should be understood that in the embodiment of the present application, the number of SQL corpora stored in the external knowledge base is much larger than the number of SQL corpora stored in the SQL corpus set.

[0078] In a specific example, the corpus vector corresponding to the SQL corpus is converted through an existing vectorization model, and the SQL corpus and its corresponding corpus vector are combined and stored in the search analysis engine. The vectorization model can process SQL statements of different complexities and easily adapt to new corpus types.

[0079] In the embodiment of the present application, step 300 includes the following steps.

[0080] Step 310: Determine the corresponding similarities between the target vector and each corpus vector.

[0081] The similarity between the target vector and the corpus vector corresponding to the query data can be calculated by Pearson correlation coefficient, Euclidean distance, etc.

[0082] In a specific example, step 310 acquires the corpus vector corresponding to each SQL corpus by calling the search analysis engine.

[0083] Step 320, sorting the similarities in descending order, determining the corpus vectors corresponding to the first N similarities in the sorting result as the pending corpus vectors, and determining the SQL corpus corresponding to the pending corpus vectors as the pending SQL corpus.

[0084] Wherein, N is a preset positive integer. In a specific example, N is selected to be 10.

[0085] After analyzing the semantics of the user input, the embodiment of the present application screens out the most relevant information by calculating the similarity with the existing SQL corpus in the external knowledge base. The similarity calculation mechanism can understand the user needs more intelligently and give accurate responses.

[0086] Step 330: Rearrange each pending SQL corpus using a pre-built rearrangement model to obtain the SQL corpus set.

[0087] The number of SQL corpora included in the SQL corpus is less than N. In a specific example, the number of SQL corpora included in the SQL corpus is selected to be 3, and the reranking model is selected to be bge-reranker-base. That is, after the pending SQL corpora are reranked and sorted by the reranking model, the three pending SQL corpora with the highest scores after sorting are taken as the target SQL corpora included in the SQL corpus.

[0088] In a specific example, the step 330 uses the powerful information retrieval capability of the search analysis engine to quickly find similar items from a large-scale corpus. By obtaining relevant SQL corpus from the search analysis engine as a reference for the model, a targeted example is provided for the output of the model, allowing the model to output according to the provided SQL corpus.

[0089] By combining vectorization technology with search engines, it is possible to find SQL corpora with higher similarity more flexibly and quickly, and support efficient retrieval of large-scale corpora (i.e. external knowledge bases).

[0090] The embodiment of the present application is connected to a rich external knowledge base. When a user asks a question, multiple SQL corpora that are highly relevant to the user's question are extracted from the external knowledge base as a reference for generating a target SQL statement, thereby ensuring that the generation process of the target SQL statement is based on actual examples, thereby improving the accuracy and relevance of the generated results.

[0091] In addition, the external knowledge base not only contains historical data and SQL corpus of typical use cases, but also continuously adds and adjusts content based on user interaction records and feedback, ensuring that the model can always face the latest query context and needs, thereby improving the efficiency, accuracy and flexibility of the model in generating target SQL statements. At the same time, it also reduces the dependence on large-scale training data when using the model to generate target SQL statements, and can meet the query needs of different users more quickly.

[0092] Specifically, the external knowledge base can record and display relevant queries and their results, thereby providing a clear reference source for generating target SQL statements. This transparency improves interpretability, and users can more easily understand and verify the logic and basis behind the generated SQL statements. Since some knowledge and rules are already included in the external knowledge base, the introduction of the external knowledge base can reduce the requirements for model training. The model can obtain expertise in a specific field by querying the external knowledge base, rather than having to learn and remember all the details during the training process.

[0093] Step 400: Based on the data to be queried and the SQL corpus corresponding to the data to be queried, a target SQL statement corresponding to the data to be queried is generated by using an SQL generation model constructed using artificial intelligence technology.

[0094] In the embodiment of the present application, step 400 includes the following steps.

[0095] Step 410: Determine a statement generation prompt word of the SQL generation model according to the keyword set and the SQL corpus corresponding to the data to be queried.

[0096] The statement generation prompt word includes at least one variable, wherein the value of one variable is the keyword set and the SQL corpus. It is understandable that when the statement generation prompt word includes multiple variables, the values ​​of other variables may be relevant information of the database.

[0097] Step 420: Generate the target SQL statement through the SQL generation model according to the statement generation prompt word and the data to be queried.

[0098] The following is an example of using the SQL generation model to generate a target SQL statement.

[0099] System prompt words of the model (i.e. preset generated prompt words): You are a database expert, and you can accurately generate corresponding SQL based on user questions. The table structure in the system is as follows: [---table structure information omitted here---]. You can refer to these contents [{extracted key information (i.e. keyword set)}, {recalled SQL corpus (i.e. SQL corpus set): The SQL statement to query accounts in the system is select*from...\The SQL statement to query account flows in the system is select*from...}].

[0100] User question (i.e. data to be queried): Please help me check how many accounts there are in the system.

[0101] Optionally, when generating a target SQL statement, the system prompts of the model can also include the chat history between the user and the model, so that the model can combine context information for semantic analysis and generate a more accurate target SQL statement. By gradually optimizing the parsing strategy based on the user's query history and behavior, the model can perform more intelligently when facing changing query requirements.

[0102] The embodiment of the present application determines a statement generation prompt word according to the keyword set and the SQL corpus corresponding to the data to be queried, so that the model uses the generated statement generation prompt word and the reference information of the data to be queried input by the user, combined with its own powerful context understanding and semantic analysis capabilities, to generate an SQL statement that meets the user's needs. This combination method not only relies on an external knowledge base, but also gives full play to the advantages of the model in processing complex natural language tasks.

[0103] Step 500: execute the target SQL statement in the pre-built database and display the query result.

[0104] The database contains multiple tables, each of which has multiple field names. By executing the generated target SQL statement in the database, the query result can be obtained, and the query result is parsed and processed to be converted into a form that is easy for users to understand and displayed, including text, graphics, and tables.

[0105] Optionally, in the embodiment of the present application, before executing step 500, the method may further include the following steps.

[0106] Step 430: perform logic verification on the target SQL statement through the SQL generation model, and replace the verified SQL statement with the target SQL statement.

[0107] The SQL generation model has the ability to think independently, possesses skills and knowledge in all industry verticals, and can play various expert roles.

[0108] In a specific example, the specific operation of step 430 is: first, set the role of a model as a verification expert, and then submit the target SQL statement generated by the model to the verification expert model for review, that is, the verification expert model analyzes the correctness of the SQL statement. If it is correct, the SQL statement is returned as a verified SQL statement. If it is incorrect, the SQL statement is modified and the modified SQL statement (i.e., the verified SQL statement) is returned.

[0109] After completing the logic check, step 500 performs a database query operation according to the SQL statement returned by the verification expert model. If the database query operation is successfully executed, the query result will be displayed. If the database query operation fails, the generated exception information and the SQL statement will be submitted to the verification expert model for correction, the corrected result will be obtained and the database query operation will be executed again. If the number of failed database query operations exceeds the preset number of repetitions (which can be set to 3), the query failure result will be fed back to the user.

[0110] Before performing a query operation on the database, the embodiment of the present application performs a logical check on the SQL statement generated by the model to ensure that no conflict or error occurs during the operation of the database.

[0111] Optionally, based on the above embodiment, before executing step 500, the method may further include the following steps.

[0112] Step 440: Optimize the target SQL statement using the SQL generation model, and replace the optimized SQL statement with the target SQL statement.

[0113] Before performing a query operation on a database, the embodiment of the present application improves the efficiency of query execution by optimizing SQL statements according to the characteristics of the database, such as index query and performance prompt.

[0114] The natural language-based database query method provided in the embodiment of the present application is explained below through an application example.

[0115] An application system includes an intelligent middle platform, an independent client, an AI call engine, a model layer, and a business system. Among them, the intelligent middle platform provides knowledge base management functions, manages SQL corpus data, implements approximate search of SQL corpus, connects to the AI ​​call engine to implement AI capabilities, manages independent client user information, and is a carrier for SQL corpus recall and external knowledge base. The independent client provides a chat interface for users to use. It is a carrier for users to communicate with large models (i.e., SQL generation models). It calls the functional interface of the intelligent middle platform and can be embedded in the business system for use. The AI ​​call engine is used to orchestrate the execution process of AI agents, calls the model capabilities of the model layer according to the process, and returns the execution results of the AI ​​agents to the intelligent middle platform. The model layer is used to run large language models, vectorized models, and rearrangement models, provides the basic capabilities of AI models, and provides a model call interface for the AI ​​call engine. The call link diagram between the various components of the application system is shown in the following figure. Figure 2 When the user inputs voice data, the application system completes the entire process of database query as shown in Figure 3 shown.

[0116] It can be seen that the database query method based on natural language provided in the embodiment of the present application realizes database query using natural language. Specifically, after the user inputs the data to be queried using natural language, the data to be queried is converted into a target SQL statement based on an external knowledge base and an SQL generation model, the database query is executed, and the query result is returned to the user. The entire process does not require the participation of developers, which simplifies the operation process of database query. This method is applicable to database query requirements in various business systems, and users can implement database queries through natural language without being familiar with SQL syntax.

[0117] Specifically, this method uses natural language processing technology to automatically convert the user's requirements in natural language into SQL statements, which avoids the tedious process of users directly writing SQL statements and debugging SQL statements, thereby lowering the technical threshold for business queries, reducing query time, and improving user experience. In other words, users do not need to master SQL syntax, they only need to use natural language to express their requirements and can easily perform complex business query operations.

[0118] The SQL generation model in this method can learn from a large number of examples and gradually improve its ability to understand and parse natural language intent. It can flexibly parse various forms of natural language input to generate corresponding SQL queries. It has strong adaptability and extensibility. It does not need to pre-define SQL templates and can dynamically generate SQL queries based on actual user input, and can handle diverse query requirements.

[0119] Figure 1 FIG. 1 is a flow chart of a natural language-based database query method in one embodiment. It should be understood that although Figure 1 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 1 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.

[0120] Figure 4 The structure block diagram of the database query device based on natural language in the embodiment of the present application is schematically shown. Figure 4 As shown, in one embodiment of the present application, a natural language-based database query device is provided, and the database query device may include the following functional modules.

[0121] The target vector generation module is used to obtain the data to be queried input by the user and generate a target vector corresponding to the data to be queried, wherein the data to be queried is natural language data.

[0122] The SQL corpus generation module is used to determine the SQL corpus corresponding to the data to be queried based on the target vector corresponding to the data to be queried and based on the external knowledge base, wherein the SQL corpus includes multiple target SQL corpora stored in the external knowledge base.

[0123] The target SQL statement generation module is used to generate a target SQL statement corresponding to the data to be queried based on the data to be queried and the SQL corpus corresponding to the data to be queried, by using an SQL generation model constructed using artificial intelligence technology.

[0124] The database query module is used to execute the target SQL statement in a pre-built database and display the query results.

[0125] In the embodiment of the present application, the device may further include:

[0126] The input type determination module is used to determine the data type of the data to be queried.

[0127] The type conversion module is used to perform voice recognition processing on the data to be queried if the data type is voice data, so as to determine the text data corresponding to the data to be queried.

[0128] In an embodiment of the present application, the target vector corresponding to the to-be-queried data includes a plurality of target sub-vectors, and the target vector generation module includes:

[0129] The semantic analysis unit is used to perform semantic analysis on the data to be queried and extract a keyword set.

[0130] The vector conversion unit is used to perform vector conversion on each keyword in the keyword set to obtain multiple target sub-vectors.

[0131] In the embodiment of the present application, the external knowledge base stores a plurality of SQL corpora and a corpus vector corresponding to each SQL corpus, and the SQL corpus generation module includes:

[0132] The similarity calculation unit is used to determine the similarities between the target vector and each corpus vector.

[0133] The pending corpus determination unit is used to sort the similarities in descending order, determine the corpus vectors corresponding to the first N similarities in the sorting result as the pending corpus vectors, and determine the SQL corpus corresponding to the pending corpus vectors as the pending SQL corpus, where N is a preset positive integer.

[0134] The corpus determination unit is used to rearrange each pending SQL corpus through a pre-built rearrangement model to obtain the SQL corpus; wherein the number of SQL corpora included in the SQL corpus is less than N.

[0135] In the embodiment of the present application, the target SQL statement generation module includes:

[0136] A prompt word generation unit is used to determine a prompt word for sentence generation of the SQL generation model according to the keyword set and the SQL corpus corresponding to the data to be queried.

[0137] A target SQL statement generating unit is used to generate the target SQL statement according to the statement generating prompt words and the data to be queried, and to generate the target SQL statement through the SQL generating model.

[0138] In the embodiment of the present application, the device may further include:

[0139] The statement verification module is used to perform logic verification on the target SQL statement through the SQL generation model, and replace the verified SQL statement with the target SQL statement.

[0140] In the embodiment of the present application, the device may further include:

[0141] A statement optimization module is used to optimize the target SQL statement through the SQL generation model and replace the optimized SQL statement with the target SQL statement.

[0142] Since the natural language-based database query device provided in the embodiment of the present application is a virtual device corresponding to the natural language-based database query method of the above-mentioned embodiment, it can also solve the problem in the prior art that users need to write SQL statements themselves to perform database queries, which increases the complexity of operations and affects the user experience.

[0143] An embodiment of the present application provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the natural language-based database query method described in the above embodiment when executing the computer program.

[0144] The electronic device provided in the embodiment of the present application includes a processor capable of running the natural language-based database query method of the aforementioned embodiment, and can therefore also solve the problem in the prior art that users need to write SQL statements themselves to perform database queries, which increases the complexity of operations and affects the user experience.

[0145] An embodiment of the present application provides a machine-readable storage medium having instructions stored thereon. When the instructions are executed by a processor, the processor is configured to execute the natural language-based database query method described in the above embodiment.

[0146] The machine-readable storage medium provided in the embodiment of the present application stores instructions for enabling a machine to execute the natural language-based database query method of the above embodiment. Therefore, it can also solve the problem in the prior art that users need to write SQL statements themselves to perform database queries, which increases the complexity of operations and affects the user experience.

[0147] Figure 5 The internal structure diagram of the computer device of the embodiment of the present application is schematically shown. Figure 5 As shown, in one embodiment of the present application, a computer device is provided, which may be a terminal. The computer device includes a processor A01, a network interface A02, a display screen A04, an input device A05 and a memory (not shown in the figure) connected via a system bus. Among them, the processor A01 of the computer device is used to provide computing and control capabilities. The memory of the computer device includes an internal memory A03 and a non-volatile storage medium A06. The non-volatile storage medium A06 stores an operating system B01 and a computer program B02. The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 in the non-volatile storage medium A06. The network interface A02 of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor A01, a database query method based on natural language is implemented. The display screen A04 of the computer device may be a liquid crystal display or an electronic ink display, and the input device A05 of the computer device may be a touch layer covered on the display screen, or a key, trackball or touchpad provided on the housing of the computer device, or an external keyboard, touchpad or mouse, etc.

[0148] Those skilled in the art will understand that Figure 5 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0149] In one embodiment, the natural language-based database query device provided by the present application can be implemented in the form of a computer program. The computer program can be used in Figure 5 The computer device is run on the computer device shown. The memory of the computer device can store various program modules constituting the database query device, and the computer program composed of various program modules enables the processor to execute the steps of the natural language-based database query method of each embodiment of the present application described in this specification.

[0150] Figure 5 The computer device shown can be Figure 4 In the natural language-based database query device shown, the target vector generation module executes step 200 , the SQL corpus generation module executes step 300 , the target SQL statement generation module executes step 400 , and the database query module executes step 500 .

[0151] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0152] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0153] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0154] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.

[0155] In a typical configuration, a computing device includes one or more processors (CPU), input / output interfaces, network interfaces, and memory.

[0156] The memory may include non-permanent memory in a computer-readable medium, random access memory (RAM) and / or non-volatile memory in the form of read-only memory (ROM) or flash RAM. The memory is an example of a computer-readable medium.

[0157] Computer readable media include permanent and non-permanent, removable and non-removable media, and can be implemented by any method or technology to store information. Information can be computer readable instructions, data structures, program modules or other data. Examples of computer storage media include, but are not limited to, phase change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technology, compact disk read-only memory (CD-ROM), digital versatile disk (DVD) or other optical storage, magnetic cassettes, magnetic disk storage or other magnetic storage devices or any other non-transmission media that can be used to store information that can be accessed by a computing device. As defined herein, computer readable media does not include temporary computer readable media (transitory media), such as modulated data signals and carrier waves.

[0158] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0159] The above are only embodiments of the present application and are not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included within the scope of the claims of the present application.

Claims

1. A database query method based on natural language, characterized in that: The method comprises: Acquire the data to be queried input by the user, and generate a target vector corresponding to the data to be queried; wherein the data to be queried is natural language data; According to the target vector corresponding to the data to be queried, based on the external knowledge base, determining the SQL corpus corresponding to the data to be queried; wherein the SQL corpus includes a plurality of target SQL corpora stored in the external knowledge base; Based on the data to be queried and the SQL corpus corresponding to the data to be queried, a target SQL statement corresponding to the data to be queried is generated by a SQL generation model constructed by using artificial intelligence technology; Execute the target SQL statement in the pre-built database and display the query results.

2. The method according to claim 1, characterized in that Before generating the target vector corresponding to the to-be-queried data, the method further includes: Determine the data type of the data to be queried; If the data type is voice data, voice recognition processing is performed on the data to be queried to determine text data corresponding to the data to be queried.

3. The method according to claim 1, characterized in that Generating a target vector corresponding to the data to be queried includes: Performing semantic analysis on the data to be queried to extract a keyword set; Performing vector conversion on each keyword in the keyword set to obtain multiple target sub-vectors; The target vector includes various target sub-vectors.

4. The method according to claim 1, characterized in that: The external knowledge base stores a plurality of SQL corpora and a corpus vector corresponding to each SQL corpus; According to the target vector corresponding to the data to be queried, based on an external knowledge base, determining the SQL corpus corresponding to the data to be queried, including: Determine the similarity between the target vector and each corpus vector; Sort each similarity in descending order, determine the corpus vectors corresponding to the first N similarities in the sorting result as the pending corpus vectors, and determine the SQL corpus corresponding to the pending corpus vectors as the pending SQL corpus; where N is a preset positive integer; The SQL corpus set is obtained by rearranging each pending SQL corpus through a pre-built rearrangement model; wherein the number of SQL corpora included in the SQL corpus set is less than N.

5. The method according to claim 3, characterized in that: Based on the data to be queried and the SQL corpus corresponding to the data to be queried, a target SQL statement corresponding to the data to be queried is generated by using an SQL generation model constructed by using artificial intelligence technology, including: Determining a statement generation prompt word of the SQL generation model according to the keyword set and the SQL corpus corresponding to the data to be queried; The target SQL statement is generated according to the statement generation prompt word and the data to be queried, and the target SQL statement is generated through the SQL generation model.

6. The method according to any one of claims 1 to 5, characterized in that Before executing the target SQL statement in the pre-built database, the method further includes: The target SQL statement is logically verified by using the SQL generation model, and the verified SQL statement is replaced with the target SQL statement.

7. The method according to any one of claims 1 to 5, characterized in that Before executing the target SQL statement in the pre-built database, the method further includes: The target SQL statement is optimized by using the SQL generation model, and the optimized SQL statement is replaced with the target SQL statement.

8. A database query device based on natural language, characterized in that: The device comprises: A target vector generation module, used to obtain the to-be-queried data input by the user, and generate a target vector corresponding to the to-be-queried data; wherein the to-be-queried data is natural language data; An SQL corpus generation module, configured to determine, based on an external knowledge base and according to a target vector corresponding to the data to be queried, an SQL corpus corresponding to the data to be queried; wherein the SQL corpus includes a plurality of target SQL corpora stored in the external knowledge base; A target SQL statement generation module, used to generate a target SQL statement corresponding to the data to be queried based on the data to be queried and the SQL corpus corresponding to the data to be queried, by using an SQL generation model constructed using artificial intelligence technology; The database query module is used to execute the target SQL statement in a pre-built database and display the query results.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the natural language-based database query method according to any one of claims 1 to 7 is implemented.

10. A machine-readable storage medium having instructions stored thereon, characterized in that: When the instruction is executed by a processor, the processor is configured to execute the natural language-based database query method according to any one of claims 1 to 7.

Citation Information

Cited By

  • Query statement conversion method and device based on large model, equipment and medium

    CN120578683A

  • Multi-modal grain data fusion and decision-making method based on artificial intelligence

    CN120743941A

  • An artificial intelligence agent-based multi-modal grain data fusion and decision-making method

    CN120743941B