Database query method and device based on natural language and electronic equipment
Through locally deployed large model services and prompt word projects, natural language is converted into SQL statements, which solves the problem of difficulty for non-professionals to query the database, achieves data security and query simplification, and at the same time improves the quality and accuracy of SQL statements.
Patent Information
- Application Number
- CN202410646770.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-23
- Publication Date
- 2024-08-23
AI Technical Summary
It is difficult to perform database queries through natural language with the existing technology, especially for non-professionals who lack professional knowledge of SQL language and databases. It is difficult to obtain information in the database, and the SQL statements generated by existing tools are not accurate enough, resulting in data query errors. Accuracy decreases.
By deploying local large model services, using prompt word engineering and natural language processing models to convert the natural language query text input by users into SQL statements, combined with Few-Shot learning and SQL fine-tuning models, the target SQL query statements are generated and stored in the database. Run to get query results.
It realizes effective query of database by natural language, ensures data security, simplifies query difficulty, and improves the quality and accuracy of SQL query statements through verification and visual display.
Smart Images

Figure CN118535679A
Abstract
Description
Technical Field
[0001] The present application relates to the field of data processing technology, and in particular to a database query method, device and electronic device based on natural language. Background Art
[0002] In the current information age, databases are the core tools for data storage and management. Interacting with databases usually requires the use of highly professional structured query language (SQL). Therefore, for non-professionals who lack SQL language and database expertise, although the database contains a large amount of rich data, many people find it difficult to obtain the information they need from it.
[0003] In addition, although there are some open source prototype applications such as Sql-Translator that can translate the natural language query text entered by the user into SQL statements, they only play an auxiliary role and do not guarantee the correctness of the generated SQL, which will also cause the queried data to lose its corresponding accuracy. Summary of the invention
[0004] In view of this, the purpose of this application is to provide at least a natural language-based database query method, device and electronic device, which can realize natural language query operations on the database through a large model service deployed locally, thereby ensuring data security while simplifying the query difficulty.
[0005] This application mainly includes the following aspects:
[0006] In a first aspect, an embodiment of the present application provides a database query method based on natural language, the method comprising: obtaining a query text input by a user, the query text being in natural language; performing prompt word analysis and processing on the query text using a prompt word project and a locally deployed big model service to obtain a final prompt word corresponding to the query text, the final prompt word being used to guide the big model service to generate an SQL statement corresponding to the query text; inputting the final prompt word into the big model service to determine a target SQL query statement output by the big model service corresponding to the query text; and running the target SQL query statement in the database to be queried to obtain a data query result corresponding to the query text.
[0007] In one possible implementation, the large model service includes a natural language processing model, wherein a final prompt word is determined in the following manner: a query text is input into the natural language processing model for optimization processing to obtain an optimized query text, wherein the optimization processing includes understanding, translation, and rewriting; database information corresponding to the database to be queried and basic information corresponding to each data table in the database to be queried are obtained, wherein the basic information is a context formed by a table name of the data table and a field name under the data table; the basic information corresponding to each data table and the optimized query text are input into the natural language processing model to obtain a relevant data table corresponding to the query text; at least one similar query sample corresponding to the query text is screened out from a plurality of preset natural language query samples using Few-Shot learning; historical conversations related to the query text are counted; and a final prompt word is formed by the optimized query text, basic information corresponding to the relevant data table, historical conversations, at least one similar query sample, and database information.
[0008] In a possible implementation, at least one similar query sample is determined in the following manner: vectorizing a plurality of preset natural language query samples respectively to obtain a sample feature vector corresponding to each preset natural language query sample; storing a plurality of sample feature vectors in a preset vector database; vectorizing the query text to obtain a query feature vector; performing a semantic search in the preset vector database based on the query feature vector to determine at least one target sample feature vector that is semantically close to the query feature vector from a plurality of sample feature vectors; and determining the preset natural language query sample corresponding to the target sample feature vector as a similar query sample corresponding to the query text.
[0009] In one possible implementation, the large model service includes multiple parallel SQL fine-tuning models, wherein a target SQL query statement corresponding to the query text is determined in the following manner: the final prompt word is input into each SQL fine-tuning model respectively to obtain a candidate SQL query statement output by each SQL fine-tuning model; and multiple candidate SQL query statements are screened by a preset voting algorithm to determine the target SQL query statement.
[0010] In a possible implementation, before running the target SQL query statement in the database to be queried, the method also includes: using the database to be queried to perform a validity check on the target SQL query statement to determine a check result; if the check result is a check failure, incorporating the error message as additional context into the final prompt word, and reacquiring the target SQL query statement until the check passes.
[0011] In a possible implementation, the method further includes: visually displaying the data query results using a preset visualization component according to data features corresponding to the data query results.
[0012] In the second aspect, the embodiment of the present application also provides a database query device based on natural language, the device comprising: an acquisition module, used to obtain a query text input by a user, the query text is a natural language; a prompt word determination module, used to use a prompt word project and a locally deployed big model service to perform prompt word analysis and processing on the query text, and obtain a final prompt word corresponding to the query text, and the final prompt word is used to guide the big model service to generate an SQL statement corresponding to the query text; a conversion module, used to input the final prompt word into the big model service, and determine the target SQL query statement output by the big model service corresponding to the query text; a query module, used to run the target SQL query statement in the database to be queried, and obtain a data query result corresponding to the query text.
[0013] In one possible implementation, the large model service includes a natural language processing model, wherein the prompt word determination module is further used to: input the query text into the natural language processing model for optimization processing to obtain an optimized query text, wherein the optimization processing includes understanding, translation and rewriting; obtain basic information corresponding to each data table in the database to be queried, wherein the basic information is a context formed by the data table name and the field name under the data table; input the basic information corresponding to each data table and the optimized query text into the natural language processing model to obtain a related data table corresponding to the query text; utilize Few-Shot learning to filter out at least one similar query sample corresponding to the query text from multiple preset natural language query samples; count historical conversations related to the query text; and form a final prompt word from the optimized query text, the basic information corresponding to the related data table, the historical conversation, and at least one similar query sample.
[0014] In the third aspect, an embodiment of the present application also provides an electronic device, comprising: a processor, a memory and a bus, the memory storing machine-readable instructions executable by the processor, when the electronic device is running, the processor and the memory communicate through the bus, and the machine-readable instructions are executed by the processor to execute the steps of the natural language-based database query method in the above-mentioned first aspect or any possible implementation of the first aspect.
[0015] In a fourth aspect, an embodiment of the present application further provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the natural language-based database query steps in the above-mentioned first aspect or any possible implementation of the first aspect are executed.
[0016] The embodiments of the present application provide a method, device and electronic device for querying a database based on natural language, and the method includes: obtaining a query text input by a user, the query text being a natural language; using a prompt word project and a locally deployed large model service to perform prompt word analysis and processing on the query text, and obtaining a final prompt word corresponding to the query text, and the final prompt word is used to guide the large model service to generate an SQL statement corresponding to the query text; inputting the final prompt word into the large model service, and determining a target SQL query statement corresponding to the query text output by the large model service; running the target SQL query statement in the database to be queried, and obtaining a data query result corresponding to the query text. The present application implements natural language query operations on the database through a locally deployed large model service, which ensures data security while simplifying the query difficulty.
[0017] The benefits of this application are:
[0018] (1) Using locally deployed large model services instead of OpenAI to perform database operations can effectively prevent information leakage and ensure data security.
[0019] (2) The big model is fine-tuned to adapt to the SQL scenario through the verification results of the SQL query statement, making the big model service more suitable for SQL generation.
[0020] (3) Structured and refined prompt engineering can ensure that the big model service obtains complete, accurate, and appropriately lengthened context about the database, thereby ensuring the quality of the SQL query statements generated by the big model service.
[0021] (4) A cross-validation voting mechanism is used to determine the target SQL query statement from the output results of multiple SQL fine-tuning models, further ensuring the quality of the generated SQL query statement.
[0022] (5) Automatically execute SQL and visualize the data results.
[0023] In order to make the above-mentioned objects, features and advantages of the present application more obvious and easy to understand, preferred embodiments are specifically cited below and described in detail with reference to the attached drawings. BRIEF DESCRIPTION OF THE DRAWINGS
[0024] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings required for use in the embodiments will be briefly introduced below. It should be understood that the following drawings only show certain embodiments of the present application and therefore should not be regarded as limiting the scope. For ordinary technicians in this field, other related drawings can be obtained based on these drawings without paying creative work.
[0025] Figure 1A flowchart of a natural language-based database query method provided in an embodiment of the present application is shown;
[0026] Figure 2 A flow chart of a method for determining a final prompt word provided in an embodiment of the present application is shown;
[0027] Figure 3 A schematic diagram of determining a related data table provided in an embodiment of the present application is shown;
[0028] Figure 4 A schematic diagram of a similar query sample screening process provided by an embodiment of the present application is shown;
[0029] Figure 5 A schematic diagram of a natural language-based database query process provided by an embodiment of the present application is shown;
[0030] Figure 6 A schematic diagram of the structure of a natural language-based database query device provided in an embodiment of the present application is shown;
[0031] Figure 7 A schematic structural diagram of an electronic device provided in an embodiment of the present application is shown. DETAILED DESCRIPTION
[0032] To make the purpose, technical scheme and advantages of the embodiments of the present application clearer, the technical scheme 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 drawings in the present application only serve the purpose of explanation and description and are not used to limit the scope of protection of the present application. In addition, it should be understood that the schematic drawings are not drawn in real proportion. The flowchart used in this application shows the operations implemented according to some embodiments of the present application. It should be understood that the operations of the flowchart can be implemented out of sequence, and the steps without logical context can be reversed in order or implemented simultaneously. In addition, those skilled in the art, under the guidance of the content of the present application, can add one or more other operations to the flowchart, or remove one or more operations from the flowchart.
[0033] In addition, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. The components of the embodiments of the present application described and shown in the drawings here can be arranged and designed in various configurations. Therefore, the following detailed description of the embodiments of the present application provided in the drawings is not intended to limit the scope of the application claimed for protection, but merely represents the selected embodiments of the present application. Based on the embodiments of the present application, all other embodiments obtained by those skilled in the art without making creative work belong to the scope of protection of the present application.
[0034] Big language models are AI technologies that have only appeared in recent years. They are currently mainly used in content generation fields such as knowledge question answering, text creation, and translation. The combination of big models and data products is still in the early stages of exploration. There are only sporadic prototype applications on the market, and they have the following main disadvantages:
[0035] (1) Over-reliance on OpenAI at the large model level. OpenAI’s advantage is its strong reasoning ability, but its disadvantages are also obvious. The specific disadvantages are as follows:
[0036] a. Internet access is required;
[0037] b. Every call is charged;
[0038] c. It requires exposing the company’s private data, which poses a huge risk to data security.
[0039] (2) There is a lack of sophisticated prompt engineering to provide complete context, resulting in a lack of quality assurance for the generated SQL statements.
[0040] (3) There is a lack of support for large-scale databases (such as hundreds of tables and thousands of fields). The data table context of large databases is huge, often exceeding the Token Limit that large models can accept. Even if it does not exceed the Token Limit, the excessively long context makes the reasoning of large models inefficient and time-consuming.
[0041] Based on this, the embodiments of the present application provide a database query method, device and electronic device based on natural language, which realizes the query operation of the database by natural language through a large model service deployed locally, thereby ensuring data security and simplifying the query difficulty, as follows:
[0042] See also Figure 1 , Figure 1 FIG. 1 is a flowchart of a natural language-based database query method provided in an embodiment of the present application. Figure 1 As shown, the method provided in the embodiment of the present application comprises the following steps:
[0043] S100: Obtain query text input by a user.
[0044] The query text is in natural language.
[0045] S200: Utilize the prompt word project and the locally deployed large model service to perform prompt word analysis and processing on the query text to obtain a final prompt word corresponding to the query text.
[0046] The final prompt word is used to guide the large model service to generate an SQL statement corresponding to the query text.
[0047] S300: Input the final prompt word into the big model service, and determine the target SQL query statement corresponding to the query text output by the big model service.
[0048] S400: Run the target SQL query statement in the database to be queried to obtain data query results corresponding to the query text.
[0049] Prompt Engineering is a technology used in the field of natural language processing (NLP), which aims to design and construct prompt words or guides for generating text. In steps S100 to S400 of the present application, prompt engineering and the big model service are used to jointly analyze the query text to determine the final prompt word, so that the big model service generates a target SQL query statement under the guidance of the final prompt word, and runs the target SQL query statement in the database to be queried to obtain the final data query result. In this process, the structure and content of the database are explored through natural language, that is, users do not need to master the SQL language or be familiar with the complex database table structure in advance to retrieve data, and 1) the entire query process is implemented based on the locally deployed big model service without the help of OpenAI, preventing data leakage and ensuring data security.
[0050] See also Figure 2 , Figure 2 A flowchart of a method for determining a final prompt word provided by an embodiment of the present application is shown. The large model service includes a natural language processing model and multiple parallel SQL models, such as Figure 2 As shown, step S200 includes:
[0051] S2001. Input the query text into a natural language processing model for optimization processing to obtain an optimized query text.
[0052] Optimization processing includes understanding, translation and rewriting.
[0053] S2002. Obtain database information corresponding to the database to be queried and basic information corresponding to each data table in the database to be queried.
[0054] The basic information is the context formed by the data table name and the field names under the data table.
[0055] S2003. Input the basic information corresponding to each data table and the optimized query text into a natural language processing model to obtain relevant data tables corresponding to the query text.
[0056] S2004. Utilize Few-Shot learning to select at least one similar query sample corresponding to the query text from a plurality of preset natural language query samples.
[0057] S2005. Count historical conversations related to the query text.
[0058] S2006: A final prompt word is formed from the optimized query text, basic information corresponding to the relevant data table, historical conversations, at least one similar query sample and database information.
[0059] Specifically, in step S2001, the query text input by the user is generally short, casual, and colloquial, and therefore may be non-standard and inaccurate. In the present application, the natural language processing model is first called to rewrite and optimize the query text so that the optimized query text can more accurately and richly express the user's wishes. In addition, the data table and the fields thereunder are generally named based on English words. Therefore, the optimized query text needs to be translated into English through the natural language processing model. This process can improve the matching accuracy between the query text and the context of the database to be queried.
[0060] In step S2002, the database information includes a database table structure, a plurality of sample data in each data table, a database dialect (Dialect), and a maximum number of rows corresponding to the data query result, the database dialect being, for example, MySQL or Oracle.
[0061] In step S2003, for a large database, the number of data tables and their fields is large, so the length of the full context corresponding to multiple data tables will also be very long. Generally speaking, the query text entered by the user only involves a few data tables in the database to be queried. Therefore, if the full context is sent to the SQL fine-tuning model as a prompt word, the processing performance of the SQL fine-tuning model will be reduced. At the same time, irrelevant information will also interfere with the SQL fine-tuning model and reduce the generation quality of the SQL query statement. Therefore, please refer to Figure 3 , Figure 3 FIG. 1 shows a schematic diagram of determining a related data table provided in an embodiment of the present application. Figure 3 As shown, the present application inputs the query text and the basic information corresponding to each data table into the natural language processing model in the local large model service, and with the help of the text inference capability of the natural language processing model, based on the basic information corresponding to each data table, determines the relevant data table corresponding to the query text. In this way, only the basic information of the relevant data table is extracted to participate in the formation of the final prompt word, and irrelevant context is eliminated, which can further improve the processing performance of the subsequent SQL fine-tuning model, eliminate the interference of irrelevant information to the SQL fine-tuning model, and improve the generation quality of SQL query statements of the SQL fine-tuning model.
[0062] In step S2004, Few-Shot learning provides a small group of carefully selected question-answer sample examples as part of the final prompt words to assist the SQL fine-tuning model in understanding how to convert natural language query text into SQL query statements. Few-Shot learning can significantly improve the SQL fine-tuning model's ability to generate accurate SQL commands based on query text, narrowing the gap between human language and database queries.
[0063] Although the sample size of the sample data set selected for Few-Shot learning is not large, it is not possible to send all the samples in the sample data set to the SQL fine-tuning model every time. Too much irrelevant information will reduce the quality of SQL query statements generated by the SQL fine-tuning model. Therefore, it is necessary to select relevant or similar samples for the query text output by the user.
[0064] In a preferred embodiment, see Figure 4 , Figure 4 FIG. 1 is a schematic diagram showing a similar query sample screening process provided by an embodiment of the present application. Figure 4 As shown, the sample data set includes a plurality of preset natural language query samples, each of which corresponds to a natural language question-answer pair, wherein the at least one similar query sample is determined in the following manner:
[0065] Vectorization is performed on multiple preset natural language query samples respectively to obtain a sample feature vector corresponding to each preset natural language query sample, the multiple sample feature vectors are stored in a preset vector database, the query text is vectorized to obtain a query feature vector, and a semantic search is performed in the preset vector database based on the query feature vector to determine at least one target sample feature vector that is semantically close to the query feature vector from the multiple sample feature vectors, and the preset natural language query sample corresponding to the target sample feature vector is determined as a similar query sample corresponding to the query text.
[0066] In step S2005, in the human-computer interaction of the database based on natural language, the user generally asks a series of questions to gradually mine information. These questions are related to each other and jump to each other. The subsequent questions use the entire previous conversation as the context. If the conversation context is lost, the SQL query statement generated by the SQL fine-tuning model is likely not what the user wants. Therefore, the present application also supports a conversation memory function, that is, the historical conversation related to the query text input by the user is used as part of the final prompt word, so as to help the SQL fine-tuning model better understand the accurate meaning of the query text currently input by the user and generate the corresponding SQL query statement.
[0067] In a preferred embodiment, the target SQL query statement corresponding to the query text is determined in the following manner:
[0068] The final prompt words are input into each SQL fine-tuning model respectively to obtain the candidate SQL query statements output by each SQL fine-tuning model, and multiple candidate SQL query statements are screened through a preset voting algorithm to determine the target SQL query statement.
[0069] In this application, multiple parallel SQL fine-tuning models are used, and a target SQL query statement is determined from multiple candidate SQL query statements through a preset voting algorithm, which can ensure that the target SQL query statement is the optimal solution and further improve the quality of the generated target SQL query statement.
[0070] In a preferred embodiment, before executing the target SQL query statement in the database to be queried, the method further includes:
[0071] The target SQL query statement is verified for validity using the database to be queried to determine the verification result. If the verification result is a verification failure, the error message is incorporated into the final prompt word as additional context, and the target SQL query statement is retrieved again until the verification passes.
[0072] In one example, the final prompt word of the present application is itself used to guide or stimulate the SQL fine-tuning model to generate an SQL query statement. In the present application, when the legality check of the target SQL query statement fails, the error message is used as part of the final prompt word to rewrite and correct the SQL fine-tuning model, which can further improve the controllability and accuracy of the SQL query statement output by the SQL fine-tuning model.
[0073] In a preferred embodiment, the method further comprises:
[0074] Based on the data features corresponding to the data query results, the data query results are visualized using preset visualization components.
[0075] In the present application, preset visualization components include but are not limited to bar charts, pie charts and tables. If the data query result is detailed data, it can be visualized in a table format. If the data query result is statistical data, it can be visualized in a bar chart or pie chart format. No specific restrictions are made here.
[0076] See also Figure 5 , Figure 5 FIG. 1 is a schematic diagram of a natural language-based database query process provided by an embodiment of the present application. Figure 5As shown, the local large model service includes a natural language processing model and multiple SQL fine-tuning models. First, the query text input by user A is received, and the query text is processed by using the prompt word project. Specifically, in this process, the query text is optimized by calling the natural language processing model to obtain the optimized query text, and then the relevant data table is determined by the natural language processing model, and the database information and similar query samples are extracted from the database to be queried. The final prompt word is formed by the optimized query text, the basic information corresponding to the relevant data table, the historical session, at least one similar query sample and the database information. The final prompt word is sent to each SQL fine-tuning model, and the target SQL query statement is obtained by combining the preset voting algorithm. Then, the legality of the target SQL query statement is checked. If the check passes, the target SQL query statement is run in the database to be queried through the execution engine, and the data query result fed back by the database to be queried is obtained. If the check fails, the error information is incorporated into the final prompt word, and the target SQL query statement is obtained again. After the data query result is obtained, the data query result is visualized by the rendering engine, and the corresponding report is generated and fed back to user A.
[0077] Based on the same application concept, the embodiments of the present application also provide a natural language-based database query device corresponding to the natural language-based database query method provided in the above embodiments. Since the principle of solving the problem by the device in the embodiments of the present application is similar to the natural language-based database query method in the above embodiments of the present application, the implementation of the device can refer to the implementation of the method, and the repeated parts will not be repeated.
[0078] See also Figure 6 , Figure 6 FIG. 1 shows a schematic diagram of a natural language-based database query device provided in an embodiment of the present application. Figure 6 As shown, the device comprises:
[0079] The acquisition module 500 is used to acquire a query text input by a user, where the query text is a natural language.
[0080] The prompt word determination module 510 is used to use the prompt word project and the locally deployed big model service to perform prompt word analysis on the query text to obtain the final prompt word corresponding to the query text. The final prompt word is used to guide the big model service to generate an SQL statement corresponding to the query text.
[0081] A conversion module 520, configured to input the final prompt word into the big model service, and determine a target SQL query statement output by the big model service corresponding to the query text;
[0082] The query module 530 is used to run the target SQL query statement in the database to be queried to obtain a data query result corresponding to the query text.
[0083] Based on the same application idea, please refer to Figure 7 , Figure 7 FIG. 1 is a schematic diagram showing the structure of an electronic device provided by an embodiment of the present application. Figure 7 As shown, the electronic device 600 includes: a processor 610, a memory 620 and a bus 630. The memory 620 stores machine-readable instructions executable by the processor 610. When the electronic device 600 is running, the processor 610 and the memory 620 communicate through the bus 630. The machine-readable instructions are executed by the processor 610 when it is running, such as the steps of the natural language-based database query method provided in any of the above embodiments.
[0084] Based on the same application concept, an embodiment of the present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the steps of the natural language-based database query method provided in the above embodiment are executed.
[0085] Those skilled in the art can clearly understand that, for the convenience and simplicity of description, the specific working process of the system and device described above can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here. In the several embodiments provided in the present application, it should be understood that the disclosed system, device and method can be implemented in other ways. The device embodiments described above are merely schematic. For example, the division of the units is only a logical function division. There may be other division methods in actual implementation. For example, multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some communication interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.
[0086] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.
[0087] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit.
[0088] If the function is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a non-volatile computer-readable storage medium that is executable by a processor. Based on this understanding, the technical solution of the present application, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions to enable a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present application. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM), random access memory (RAM), disk or optical disk, etc., various media that can store program codes.
[0089] The above are only specific implementations of the present application, but the protection scope of the present application is not limited thereto. Any technician familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed in the present application, which should be included in the protection scope of the present application. Therefore, the protection scope of the present application should be based on the protection scope of the claims.
Claims
1. A database query method based on natural language, characterized in that: The method comprises: Obtaining a query text input by a user, wherein the query text is a natural language; Perform prompt word analysis and processing on the query text using the prompt word project and the locally deployed big model service to obtain a final prompt word corresponding to the query text, wherein the final prompt word is used to guide the big model service to generate an SQL statement corresponding to the query text; Inputting the final prompt word into the big model service, and determining a target SQL query statement output by the big model service corresponding to the query text; The target SQL query statement is executed in the database to be queried to obtain a data query result corresponding to the query text.
2. The method according to claim 1, characterized in that The large model service includes a natural language processing model, wherein the final prompt word is determined by: Inputting the query text into the natural language processing model for optimization processing to obtain an optimized query text, wherein the optimization processing includes understanding, translation and rewriting; Obtain database information corresponding to the database to be queried and basic information corresponding to each data table in the database to be queried, wherein the basic information is a context formed by a data table name and a field name under the data table; Inputting the basic information corresponding to each data table and the optimized query text into the natural language processing model to obtain a relevant data table corresponding to the query text; Using Few-Shot learning, at least one similar query sample corresponding to the query text is screened out from a plurality of preset natural language query samples; Counting historical conversations related to the query text; The final prompt word is formed by the optimized query text, basic information corresponding to the relevant data table, the historical conversation, the at least one similar query sample and database information.
3. The method according to claim 2, characterized in that The at least one similar query sample is determined by: Performing vectorization processing on the plurality of preset natural language query samples respectively to obtain a sample feature vector corresponding to each preset natural language query sample; Storing multiple sample feature vectors in a preset vector database; Performing vectorization processing on the query text to obtain a query feature vector; According to the query feature vector, a semantic search is performed in the preset vector database to determine at least one target sample feature vector that is semantically close to the query feature vector from a plurality of sample feature vectors; The preset natural language query sample corresponding to the target sample feature vector is determined as a similar query sample corresponding to the query text.
4. The method according to claim 1, characterized in that: The large model service includes multiple parallel SQL fine-tuning models. Wherein, the target SQL query statement corresponding to the query text is determined in the following manner; Input the final prompt word into each SQL fine-tuning model respectively to obtain a candidate SQL query statement output by each SQL fine-tuning model; Multiple candidate SQL query statements are screened through a preset voting algorithm to determine the target SQL query statement.
5. The method according to claim 1, characterized in that: Before executing the target SQL query statement in the database to be queried, the method further includes: Using the database to be queried to perform a validity check on the target SQL query statement, and determining a check result; If the verification result is a verification failure, the error message is incorporated into the final prompt word as additional context, and the target SQL query statement is re-acquired until the verification passes.
6. The method according to claim 1, characterized in that The method further comprises: According to the data features corresponding to the data query results, the data query results are visualized using preset visualization components.
7. A database query device based on natural language, characterized in that: The device comprises: An acquisition module, used to acquire a query text input by a user, wherein the query text is a natural language; A prompt word determination module, used to perform prompt word analysis and processing on the query text by using the prompt word project and the locally deployed big model service to obtain a final prompt word corresponding to the query text, wherein the final prompt word is used to guide the big model service to generate an SQL statement corresponding to the query text; A conversion module, used for inputting the final prompt word into the big model service, and determining a target SQL query statement output by the big model service corresponding to the query text; The query module is used to run the target SQL query statement in the database to be queried to obtain data query results corresponding to the query text.
8. The device according to claim 7, characterized in that The large model service includes a natural language processing model, wherein the prompt word determination module is further used to: Inputting the query text into the natural language processing model for optimization processing to obtain an optimized query text, wherein the optimization processing includes understanding, translation and rewriting; Obtain basic information corresponding to each data table in the database to be queried, wherein the basic information is a context formed by the data table name and the field names under the data table; Inputting the basic information corresponding to each data table and the optimized query text into the natural language processing model to obtain a relevant data table corresponding to the query text; Using Few-Shot learning, at least one similar query sample corresponding to the query text is screened out from a plurality of preset natural language query samples; Counting historical conversations related to the query text; The final prompt word is formed by the optimized query text, basic information corresponding to the relevant data table, the historical conversation, and the at least one similar query sample.
9. An electronic device, characterized in that: include: A processor, a memory and a bus, wherein the memory stores machine-readable instructions executable by the processor, and when the electronic device is running, the processor and the memory communicate through the bus, and the machine-readable instructions are executed by the processor to execute the steps of the natural language-based database query method as described in any one of claims 1 to 6.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps of the natural language-based database query method according to any one of claims 1 to 6 are executed.
Citation Information
Cited By
Performance indicator assessment method and assessment system, electronic equipment and medium
CN118863680A
Method, system and equipment for generating SQL (Structured Query Language) statement based on large model
CN119127913A
Method and apparatus for SQL assistant, electronic device and computer program product
CN119149578A
Method and device for acquiring index data, electronic equipment and computer program product
CN119149715A
Method, device, electronic device and computer program product for obtaining indicator data
CN119149715B
