Query method and device based on large language model, equipment and medium

Through multi-model consensus decision-making and deep fusion analysis, the problem of inaccurate SQL statement generation in Text2SQL technology is solved, the grammatical correctness and semantic accuracy of SQL statements are achieved, and the matching degree and accuracy of query results are improved.

CN120821736APending Publication Date: 2025-10-21QINGDAO HISENSE TRANS TECH

Patent Information

Application Number
CN202510712883.7
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-29
Publication Date
2025-10-21

AI Technical Summary

Technical Problem

The SQL statements generated by the existing Text2SQL technology have low accuracy and cannot be executed correctly or the execution results deviate seriously from the user's intention, resulting in the inability to effectively perform queries.

Method used

At least two lightweight first-largest language models are used to generate SQL statements, and the target elements are determined based on the number of elements in each SQL field in the SQL statement. Consensus decision-making is used to avoid the incorrect preferences or random influence of individual models. The second-largest language model with a larger number of parameters is used for querying. The similarity screening and deep fusion analysis between query keywords and database fields are combined to ensure grammatical correctness and semantic accuracy.

Benefits of technology

It improves the accuracy of SQL statement generation and the matching degree of query results, realizes the optimization of the entire process from natural language to accurate query results, and enhances the language model's ability to parse and understand complex data.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120821736A_ABST
    Figure CN120821736A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a query method and device based on a large language model, equipment and a medium, and aims to solve the problem that query cannot be effectively performed due to the fact that a generated SQL statement cannot be correctly executed or an execution result is seriously deviated from a user intention. The electronic equipment generates the SQL statement corresponding to the query text by using the at least two lightweight first large language models, and determines the target element of the SQL field according to the number of the elements in each SQL field in the SQL statement, so that the SQL statement generation accuracy can be improved, the SQL statement and the database are jointly input into the second large language model with a larger parameter quantity for query, and the query efficiency is improved. The method not only ensures the grammar correctness and semantic accuracy of SQL generation, but also ensures the high matching between the final query result and the user intention through the deep reasoning ability of the large language model, and realizes the whole process optimization from the natural language to the accurate query result.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the fields of data processing artificial intelligence (AI), large language models, and intelligent question-answering technology, and in particular to a query method, apparatus, device, and medium based on a large language model. Background Art

[0002] Text-to-Structured Query Language (Text2SQL) technology is a cutting-edge technology that automatically converts natural language queries into Structured Query Language (SQL). This technology significantly lowers the barrier to entry for database queries and is rapidly becoming a key tool. Users no longer need to master complex SQL syntax or database schemas; they can simply express their needs clearly in natural language to perform professional-level data retrieval.

[0003] Among related technologies, Text2SQL relies primarily on rule-based methods and statistical machine learning. Researchers must manually write numerous grammar rules and query templates, for example, mapping fixed sentences like "query data in a certain table" to predefined SQL templates.

[0004] However, related technologies have obvious defects in query effectiveness: the generated SQL statements are less accurate and often cannot be executed correctly, or the execution results deviate seriously from the user's intentions, making it impossible to perform queries effectively. Summary of the Invention

[0005] The embodiments of the present application provide a query method, apparatus, and device based on a large language model to solve the problem that the generated SQL statement cannot be executed correctly or the execution result deviates seriously from the user's intention, making it impossible to perform effective queries.

[0006] In a first aspect, the present application provides a query method based on a large language model, the method comprising:

[0007] receiving a query text, inputting the query text into at least two first large language models, and obtaining a Structured Query Language (SQL) statement output by each of the first large language models; wherein the at least two first large language models are of the same type or different types;

[0008] For each predefined SQL field, obtain the elements of the SQL field in each SQL statement; wherein the SQL field includes selection (SELECT), source (FROM), condition (GROUP BY), grouping (ORDER), or sorting (WHERE); determine the target element of the SQL field based on the number of identified identical elements; the target element is the element with the largest number of corresponding elements;

[0009] Concatenate the target elements of each SQL field obtained to determine the target SQL statement;

[0010] The target SQL statement and the preset database are input into the second largest language model to obtain the query result output by the second largest language model; wherein the parameter amount of each of the first largest language models is smaller than the parameter amount of the second largest language model.

[0011] The above technical solution has the following advantages or beneficial effects: the electronic device uses at least two lightweight first-largest language models to generate an SQL statement corresponding to the query text, and determines the optimal target element for each SQL field based on the number of elements in the SQL statement. The target element is the element with the largest corresponding number. The target element determined in this way is the element in the SQL statement determined by the majority of the first-largest language models. This consensus decision-making method avoids the influence of the incorrect preferences or randomness of individual first-largest language models on the results, thereby effectively eliminating the generation bias of a single large language model and integrating the advantages of multiple models, thereby improving the accuracy of SQL statement generation. The SQL statement and database are jointly input into the second-largest language model with a larger number of parameters for query, and the second-largest language model's stronger semantic understanding and logical reasoning capabilities are used to execute precise queries and determine query results. This method not only ensures the syntactic correctness and semantic accuracy of SQL generation, but also ensures that the final query results are highly consistent with user intent through the deep reasoning capabilities of the large language model, achieving full process optimization from natural language to accurate query results.

[0012] In one possible implementation, after receiving the query text, before inputting the target SQL statement and the preset database into the second language model, and before obtaining the query result output by the second language model, the method further includes:

[0013] Extracting query keywords from the query text;

[0014] Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field;

[0015] Inputting the target SQL statement and the preset database into the second language model and obtaining the query result output by the second language model includes:

[0016] For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field;

[0017] The target SQL statement, each target field, the field text corresponding to each target field, and the obtained candidate data for each target field are input into the second largest language model to obtain the query result output by the second largest language model.

[0018] The above technical solution has the following advantages or beneficial effects: the method provided in the embodiment of the present application can enhance the understanding ability of the second largest language model, which not only ensures the accurate mapping of the query intent, but also fully utilizes the language model's ability to parse complex data, thereby improving the accuracy of the query.

[0019] In a possible implementation, after receiving the query text, and before inputting the query text into at least two first language models and obtaining the SQL statements output by each first language model, the method further includes:

[0020] Extracting query keywords from the query text;

[0021] Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field;

[0022] For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field;

[0023] Inputting the query text into at least two first language models and obtaining an SQL statement output by each first language model includes:

[0024] The query text, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field are input into at least two first large language models to obtain the SQL statement output by each first large language model.

[0025] The above technical solution has the following advantages or beneficial effects: the method provided in the embodiment of the present application can enhance the understanding ability of the second largest language model, which not only ensures the accurate mapping of the query intent, but also fully utilizes the language model's ability to parse complex data, thereby improving the accuracy of the query.

[0026] In a possible implementation, determining the similarity between the query keyword and each field in the database includes:

[0027] For each field in the database, the number of characters that are identical to the query keyword in the field is determined to determine the character similarity; and the vector similarity is determined based on the distance between the field vector corresponding to the field and the query vector corresponding to the query keyword; and the similarity between the query keyword and the field is determined based on the character similarity, the vector similarity, and the corresponding weights.

[0028] The above technical solution has the following advantages or beneficial effects: the similarity obtained by the method provided in the embodiment of the present application can comprehensively and accurately reflect the degree of matching between the query keywords and the fields of the database, provide strong support for the sorting and screening of database retrieval results, and effectively improve the accuracy and efficiency of the query.

[0029] In one possible implementation, each of the first language model and the second oracle model is fine-tuned in the following manner:

[0030] For each sample SQL statement, obtain the standard query text corresponding to the sample SQL statement;

[0031] Based on each sample SQL statement and the corresponding standard query text, each first language model and the second language model are fine-tuned.

[0032] The above technical solution has the following advantages and benefits: By generating standard query text, it provides high-quality supervision signals for the fine-tuning process, enabling the first language model and the second prediction model to learn more accurate and standardized query patterns, effectively avoiding the subjectivity and errors that may be introduced by manual annotation. Through fine-tuning training on a large number of sample SQL texts and standard query texts, the first language model and the second prediction model can continuously optimize their internal knowledge representation and reasoning capabilities, thereby providing high-quality query results for a variety of complex and changing SQL query tasks.

[0033] In a possible implementation, the fine-tuning each first language model and the second language model based on each sample SQL statement and the corresponding standard query text includes:

[0034] Based on each sample SQL statement, the corresponding standard query text, the preset query text to generate the first basis (Evidence) of the SQL statement, and the preset second evidence for querying based on the SQL statement, each first language model and the second prediction model are fine-tuned so that each first language model learns the first evidence and the second prediction model learns the second evidence.

[0035] The above technical solution has the following advantages or beneficial effects: the introduction of preset evidence provides a richer and more professional knowledge source for the large language model, enabling the large language model to be exposed to broader and deeper query logic and rules during the learning process, thereby significantly improving the model's understanding and processing capabilities of complex query scenarios.

[0036] In a possible implementation, determining the target element of the SQL field according to the number of identified identical elements includes:

[0037] For each identified element, determine the number of SQL fields in each SQL statement that are for that element;

[0038] The element with the highest number is determined as the target element of the SQL field;

[0039] If there are at least two elements whose quantities are both the highest quantity, it is determined that the target element does not exist in the SQL field.

[0040] The above technical solution has the following advantages or beneficial effects: by accurately counting the number of occurrences of each element in the SQL field and determining the target element based on this, it can minimize the errors that may be caused by human judgment or simple rule matching, ensure that the determination result of the target element is highly accurate, and thus provide a reliable basis for subsequent SQL statement analysis, optimization or conversion operations based on the target element.

[0041] In a second aspect, the present application provides a query device based on a large language model, the device comprising:

[0042] A receiving and obtaining module is configured to receive a query text, input the query text into at least two first large language models, and obtain a structured query language (SQL) statement output by each first large language model; wherein the at least two first large language models may be of the same type or different types; for each pre-stored SQL field, obtain elements in the SQL field in each SQL statement, and determine a target element for the SQL field based on the number of each obtained element; the target element being the element with the largest corresponding number;

[0043] The processing module is configured to concatenate the target elements of each acquired SQL field to determine a target SQL statement; input the target SQL statement and a preset database into a second language model to obtain a query result output by the second language model; wherein the number of parameters of each of the first language models is smaller than the number of parameters of the second language model.

[0044] In an optional embodiment, the processing module is further configured to extract query keywords from the query text; determine the similarity between the query keywords and each field in the database, and determine the field with a similarity higher than a preset value as the target field;

[0045] The processing module is specifically configured to randomly obtain, for each target field, a preset number of candidate data of the target field from a preset database, and obtain field text saved for the target field, wherein the field text includes text for interpreting the target field and constraint information of the target field; input the target SQL statement, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field into a second language model to obtain a query result output by the second language model.

[0046] In an optional embodiment, the processing module is further configured to extract query keywords from the query text; determine the similarity between the query keywords and each field in the database, and determine the field with a similarity higher than a preset value as a target field; for each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information for the target field;

[0047] The receiving and acquiring module is specifically used to input the query text, each target field, the field text corresponding to each target field, and the acquired candidate data of each target field into at least two first-large language models, and obtain the SQL statement output by each first-large language model.

[0048] In an optional embodiment, the processing module is specifically used to determine, for each field in the database, the number of characters that are identical to the query keyword in the field, and determine the text similarity; and determine the vector similarity based on the distance between the field vector corresponding to the field and the query vector corresponding to the query keyword; and determine the similarity between the query keyword and the field based on the text similarity, the vector similarity, and the corresponding weights.

[0049] In an optional embodiment, the processing module is further configured to obtain, for each sample SQL statement, a standard query text corresponding to the sample SQL statement;

[0050] Based on each sample SQL statement and the corresponding standard query text, each first language model and the second language model are fine-tuned.

[0051] In an optional embodiment, the processing module is specifically used to generate a first evidence of the SQL statement and a preset second evidence for querying based on the SQL statement based on each sample SQL statement, the corresponding standard query text, and the preset query text, and fine-tune each first language model and the second prediction model so that each first language model learns the first evidence and the second prediction model learns the second evidence.

[0052] In an optional embodiment, the processing module is specifically used to determine, for each identified element, the number of elements in the SQL field in each SQL statement; determine the element with the highest number as the target element of the SQL field; if there are at least two elements with the highest number, determine that there is no target element in the SQL field.

[0053] In a third aspect, the present application provides an electronic device, comprising a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other via the communication bus;

[0054] Memory for storing computer programs;

[0055] The processor is used to implement the method when executing the program stored in the memory.

[0056] In a fourth aspect, the present application provides a computer-readable storage medium, wherein the computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method described is implemented.

[0057] In a fifth aspect, the present application provides a computer program product, which includes an executable program, and the executable program is executed by a processor to implement the described method.

[0058] In the embodiment of the present application, the electronic device uses at least two lightweight first-largest language models of the same or different types to generate an SQL statement corresponding to the query text, and determines the optimal target element for each SQL field based on the number of elements in the SQL statement, where the target element is the element with the largest corresponding number. The target element determined in this manner is the element in the SQL statement determined by the majority of the first-largest language models. This consensus decision-making approach avoids the impact of erroneous preferences or randomness of individual first-largest language models on the results, thereby effectively eliminating the generation bias of a single large language model and integrating the advantages of multiple models, thereby improving the accuracy of SQL statement generation. The SQL statement and database are jointly input into the second-largest language model with a larger number of parameters for querying, and the second-largest language model's stronger semantic understanding and logical reasoning capabilities are used to execute precise queries and determine query results. This method not only ensures the syntactic correctness and semantic accuracy of SQL generation, but also ensures a high degree of match between the final query results and user intent through the deep reasoning capabilities of the large language model, achieving full process optimization from natural language to accurate query results. BRIEF DESCRIPTION OF THE DRAWINGS

[0059] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the following briefly introduces the drawings required for use in the description of the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0060] Figure 1 A schematic diagram of a query method based on a large language model provided in an embodiment of the present application;

[0061] Figure 2 A schematic diagram of a process for determining field similarity provided in an embodiment of the present application;

[0062] Figure 3 A detailed diagram of a query process provided in an embodiment of the present application;

[0063] Figure 4 A schematic diagram of the structure of a query device based on a large language model provided in an embodiment of the present application;

[0064] Figure 5 A schematic diagram of the structure of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0065] In order to make the purpose and implementation of this application clearer, the exemplary implementation of this application will be clearly and completely described below in conjunction with the drawings in the exemplary embodiments of this application. Obviously, the described exemplary embodiments are only part of the embodiments of this application, not all of the embodiments.

[0066] It should be noted that the brief descriptions of terms in this application are only for the purpose of facilitating the understanding of the embodiments described below, and are not intended to limit the embodiments of this application. Unless otherwise specified, these terms should be understood according to their ordinary and usual meanings.

[0067] In the specification and claims of this application and the drawings, the terms "first," "second," "third," etc. are used to distinguish similar or similar objects or entities and are not necessarily intended to limit a particular order or precedence, unless otherwise noted. It should be understood that the terms used in this manner are interchangeable under appropriate circumstances. The terms "including," "comprising," and "having," and any variations thereof, are intended to cover, but not exclude, inclusion. For example, a product or device comprising a list of components is not necessarily limited to all the components expressly listed, but may include other components not expressly listed or inherent to such product or device.

[0068] The term "module" refers to any known or later developed hardware, software, firmware, artificial intelligence, fuzzy logic, or combination of hardware and / or software code that is capable of performing the functionality associated with that element.

[0069] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some or all of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the scope of the technical solutions of the embodiments of the present application.

[0070] For ease of explanation, the above description has been made with reference to specific embodiments. However, the above exemplary discussion is not intended to be exhaustive or to limit the embodiments to the specific forms disclosed above. Based on the above teachings, various modifications and variations are possible. The above embodiments are selected and described to better explain the principles and practical applications, so that those skilled in the art can better utilize the embodiments and various different variations of the embodiments suitable for specific use considerations.

[0071] In order to improve query accuracy, embodiments of the present application provide a query method, apparatus, device, and medium based on a large language model.

[0072] The query method based on a large language model includes: an electronic device receives a query text, inputs the query text into at least two first large language models, and obtains an SQL statement output by each first large language model; for each predefined SQL field, obtains the elements of the SQL field in each SQL statement; wherein the SQL field includes SELECT, FROM, GROUP BY, ORDER, or WHERE; determines the target element of the SQL field based on the number of identified identical elements; determines the target SQL statement based on the obtained target element of each SQL field; inputs the target SQL statement and a preset database into a second large language model, and obtains the query result output by the second large language model; wherein the number of parameters of each first large language model is smaller than the number of parameters of the second large language model.

[0073] Figure 1 A schematic diagram of a query method based on a large language model provided in an embodiment of the present application, the process includes the following steps:

[0074] S101: Receive a query text, input the query text into at least two first large language models, and obtain an SQL statement output by each first large language model; wherein the at least two first large language models are large language models of the same type or different types.

[0075] S102: For each predefined SQL field, obtain the elements of the SQL field in each SQL statement; wherein the SQL field includes SELECT, FROM, GROUP BY, ORDER, or WHERE; determine the target element of the SQL field based on the number of identified identical elements; the target element is the element with the largest corresponding number.

[0076] S103: Concatenate the acquired target elements of each SQL field to determine the target SQL statement.

[0077] S104: Input the target SQL statement and the preset database into the second largest language model to obtain the query result output by the second largest language model; wherein the number of parameters of each of the first largest language models is smaller than the number of parameters of the second largest language model.

[0078] The query method based on a large language model provided in an embodiment of the present application is applied to an electronic device, which may be an intelligent device such as a PC or a server.

[0079] Electronic devices can receive user-submitted query text through a multi-channel interactive interface, including natural language instructions, semi-structured descriptions, or text input converted from speech. The query text may come from a text box or a command interaction through an enterprise chat tool. For example, the query text may be "Help me find customers with sales exceeding 1 million in 2023, sorted by sales from high to low, and display customer name, sales amount, and region."

[0080] After receiving the query text, the electronic device can input the query text into at least two large language models, wherein, for the sake of easy distinction, the large language model mentioned here can be referred to as the first large language model, and the number of parameters of the first large language model is relatively small. Each first large language model can be a large language model of the same type or a large language model of a different type. In one example, the electronic device can input the query text into three first large language models, and the first large language model can be a head large language model such as GPT-4 and Claude-3. The electronic device can obtain the output of each first large language model, and the output of each first large language model is an SQL statement. In order to make each first large language model output an SQL statement, the electronic device can input the query text and a preset prompt word into at least two first large language models, and the prompt word is used to prompt the first large language model to output an SQL statement.

[0081] After obtaining the SQL statements output by each first language model, the electronic device can obtain the elements of the SQL field in each SQL statement for each predefined SQL field, where the SQL field can also be called a region, and the SQL fields include SELECT, FROM, GROUP BY, ORDER, WHERE, etc. The elements of the same SQL field in each SQL statement may be different. In order to accurately determine the target element of each SQL field, the electronic device can determine the target element of the SQL field based on the number of identical elements identified. In one example, the electronic device can determine the element with the largest number of identical elements identified as the target element of the SQL field. In this case, the target element is the element with the largest corresponding number.

[0082] When processing multiple SQL statements, if a SQL field is found to be repeated in a particular SQL statement (for example, the same SQL field appears multiple times), the electronic device will determine the exact number of the SQL field by verifying the number of occurrences of the SQL field in other SQL statements. In one example, the electronic device may determine the minimum number of occurrences of the SQL field as the exact number of the SQL field. If the exact number of occurrences of the SQL field is not 1, the number of each element may be determined separately, and the element with the highest number of occurrences, the standard number, may be determined as the target element of the SQL field.

[0083] After obtaining the target element of each SQL field, the electronic device can determine the target SQL statement based on the obtained target element of each SQL field. Specifically, the electronic device can splice the target elements of each SQL field according to SQL grammar rules to obtain a complete target SQL statement.

[0084] The electronic device may input the acquired target SQL statement and a preset database into the second language model and obtain the query results output by the second language model. To enable the second language model to output the query results, the electronic device may also input preset prompt words used to prompt the second language model to perform a query into the second language model.

[0085] In order to perform queries accurately and efficiently, the parameter size of the second largest language model is greater than the parameter size of each first largest language model. In one example, the parameter size of the first largest language model can be 3B, and the parameter size of the second largest language model can be 32B.

[0086] This embodiment of the application proposes a multi-stage SQL generation solution that significantly improves the issues of inaccurate schema linking and low SQL generation accuracy in Text2SQL tasks. This embodiment selects a large SQL generation language model, constructs multiple large language models with small parameters, and obtains staged SQL statements. This SQL statement is then fed into the large language model with large parameters to obtain the final SQL query result, achieving multi-model reflective generation.

[0087] The embodiments of this application are derived from the cloud brain product big data platform and the traffic language model.

[0088] With the advancement of machine learning and natural language processing technologies, statistical methods have gradually gained popularity. Researchers use machine learning algorithms to learn the mapping between natural language and SQL using large corpus data. These models can automatically extract features from large amounts of data, improving the accuracy and generalization of the conversion. When processing common query patterns, statistically based models can provide relatively accurate SQL translation. In recent years, with the emergence of large language models (LLMs), Text2SQL technology has achieved breakthroughs. Large pre-trained language models such as GPT and BERT, trained on large corpora, possess powerful language understanding and generation capabilities. By using natural language queries and corresponding SQL queries as input and output pairs, LLMs can be fine-tuned or directly leveraged for their language understanding capabilities to achieve efficient Text2SQL conversion. LLMs significantly outperform traditional methods in Text2SQL tasks, significantly improving the efficiency and accuracy of data queries and providing users with a more convenient and intelligent database interaction experience. Currently, many works in the industry are focused on the LLM-based Text2SQL technology, and its process is generally divided into three steps: retrieve the tables and their fields related to the problem, and piece them together into a unified format such as Data Definition Language (DDL), JavaScript Object Notation (JSON), etc. The obtained format and prompt words are pieced together in a unified manner and submitted to the large language model for SQL statement generation. After the SQL statement is generated, it is directly retrieved and executed to obtain the result, and then handed over to the large language model to output the reply content. However, this method has large errors in the field retrieval effect and a lot of information redundancy, which affects the quality of the final SQL generation. The SQL generation quality is average, and it is easy to generate complex SQL statements with low execution efficiency. However, by adopting the method provided in the embodiment of the present application, SQL statements can be obtained accurately and effectively, and queries based on the target SQL statement based on the second largest language model can accurately and effectively obtain query results.

[0089] In the embodiment of the present application, the electronic device uses at least two lightweight first-largest language models to generate an SQL statement corresponding to the query text, and determines the optimal target element for the SQL field based on the number of elements in each SQL field in the SQL statement, where the target element is the element with the largest corresponding number. The target element determined in this manner is the element in the SQL statement determined by the majority of the first-largest language models. This consensus decision-making method avoids the impact of the incorrect preferences or randomness of individual first-largest language models on the results, thereby effectively eliminating the generation bias of a single large language model and integrating the advantages of multiple models, thereby improving the accuracy of SQL statement generation. The SQL statement and database are jointly input into the second-largest language model with a larger number of parameters for query, and the second-largest language model's stronger semantic understanding and logical reasoning capabilities are used to execute precise queries and determine query results. This method not only ensures the syntactic correctness and semantic accuracy of SQL generation, but also ensures a high degree of match between the final query results and user intent through the deep reasoning capabilities of the large language model, achieving full-process optimization from natural language to accurate query results.

[0090] In order to perform queries accurately and efficiently, based on the above embodiment, in an embodiment of the present application, after receiving the query text, before inputting the target SQL statement and the preset database into the second language model and obtaining the query results output by the second language model, the method further includes:

[0091] Extracting query keywords from the query text;

[0092] Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field;

[0093] Inputting the target SQL statement and the preset database into the second language model and obtaining the query result output by the second language model includes:

[0094] For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field;

[0095] The query text, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field are input into the second largest language model to obtain the query result output by the second largest language model.

[0096] Since the database contains a lot of content, if the entire database is input into the second largest language model, the query capability of the second largest language model may be reduced, resulting in lower query accuracy. In order to improve query accuracy, the electronic device can identify the fields in the database related to the query text and input the information of the related fields into the second largest language model.

[0097] Specifically, after receiving the query text, the electronic device can first extract the query keywords in the query text. Use Natural Language Processing (NLP) tools to segment and tag the query text, and identify key components such as nouns, verbs, and proper nouns. Analyze the sentence structure and extract core components such as subject, object, and predicate. Retain noun phrases (such as product names, dates, and places), verbs (such as "query", "statistics"), and specific field terms, and remove stop words (such as "of", "please", and "how").

[0098] After obtaining each query keyword, the electronic device can calculate the similarity between the query keyword and each field in the database. In one example, the similarity can be semantic similarity. By setting a similarity threshold, fields with higher relevance are screened out as target fields. After obtaining the target field, in order to facilitate the second largest language model to understand the target field, the electronic device can obtain the data of the target field from the database for each target field. Specifically, a preset number of target fields can be randomly obtained. The electronic device can also obtain the field text saved for the target field, which includes the text used to explain the target field and the constraint information of the target field.

[0099] The target SQL statement, each target field, the corresponding field text, and the candidate data for each target field are all input into the second language model for processing. Through deep fusion analysis of the language model, accurate query results are ultimately output.

[0100] The above technical solution has the following advantages or beneficial effects: the method provided in the embodiment of the present application can enhance the understanding ability of the second largest language model, which not only ensures the accurate mapping of the query intent, but also fully utilizes the language model's ability to parse complex data, thereby improving the accuracy of the query.

[0101] In order to accurately and efficiently generate SQL statements, based on the above embodiments, in an embodiment of the present application, after receiving the query text, before inputting the query text into at least two first language models and obtaining the SQL statements output by each first language model, the method further includes:

[0102] Extracting query keywords from the query text;

[0103] Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field;

[0104] For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field;

[0105] Inputting the query text into at least two first language models and obtaining an SQL statement output by each first language model includes:

[0106] The query text, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field are input into at least two first large language models to obtain the SQL statement output by each first large language model.

[0107] Since only the query text is input into each first language model, the first language model may have an incorrect understanding of the query text, which may reduce the output capacity of the first language model and make the accuracy of the generated SQL statement low. In order to accurately and efficiently generate SQL statements, the electronic device can identify the fields related to the query text in the database and input the information of the related fields into each first language model.

[0108] Specifically, after receiving the query text, the electronic device can first extract the query keywords in the query text. Use Natural Language Processing (NLP) tools to segment and tag the query text, and identify key components such as nouns, verbs, and proper nouns. Analyze the sentence structure and extract core components such as subject, object, and predicate. Retain noun phrases (such as product names, dates, and places), verbs (such as "query", "statistics"), and specific field terms, and remove stop words (such as "of", "please", and "how").

[0109] After obtaining each query keyword, the electronic device can calculate the similarity between the query keyword and each field in the database. In one example, the similarity can be semantic similarity. By setting a similarity threshold, fields with higher relevance are screened out as target fields. After obtaining the target field, in order to facilitate the second largest language model to understand the target field, the electronic device can obtain the data of the target field from the database for each target field. Specifically, a preset number of target fields can be randomly obtained. The electronic device can also obtain the field text saved for the target field, which includes the text used to explain the target field and the constraint information of the target field.

[0110] The query text, each target field obtained, the corresponding field text for each target field, and the candidate data obtained for each target field are all input into a large language model for processing. Through deep fusion analysis of the language model, accurate SQL statements are ultimately output.

[0111] The above technical solution has the following advantages or beneficial effects: the method provided in the embodiment of the present application can enhance the understanding ability of the second largest language model, which not only ensures the accurate mapping of the query intent, but also fully utilizes the language model's ability to parse complex data, thereby improving the accuracy of the query.

[0112] The embodiment of the present application is equivalent to proposing a new database schema representation method, which changes the original DDL into an easy-to-understand prompt word schema format through schema conversion, wherein the column name matching of the field is achieved through keyword extraction and semantic hybrid retrieval method, and then 3-5 data are sampled to facilitate the first language model and the second language model to understand the schema content.

[0113] The description language schema of the original database table is usually DDL, which is in the following form, taking the enterprise credit evaluation table as an example:

[0114]

[0115]

[0116] Research has found that converting the schema into a hierarchical structure can improve the final generation effect. Therefore, Beihang University and other institutions proposed the MAC-SQL solution, in which the definitions of prompt words and schema are as follows:

[0117]

[0118] Where desc_str means as follows

[0119]

[0120] However, as can be seen from the above representation, the impact of the data itself on SQL generation is not considered in actual applications. Therefore, the embodiments of the present application redesign the Schema and Prompt templates based on the style of Multi-Access Control Structured Query Language (MAC-SQL). In one example, the following content can be input into the first language model:

[0121]

[0122]

[0123] The embodiment of the present application defines a new Schema representation method and integrates value screening, which is superior to DDL, MAC-SQL and other solutions, showing that this method is more superior for Text2SQL tasks.

[0124] In order to improve the accuracy of the query results, based on the above embodiments, in the embodiment of the present application, determining the similarity between the query keyword and each field in the database includes:

[0125] For each field in the database, the number of characters that are identical to the query keyword in the field is determined to determine the character similarity; and the vector similarity is determined based on the distance between the field vector corresponding to the field and the query vector corresponding to the query keyword; and the similarity between the query keyword and the field is determined based on the character similarity, the vector similarity, and the corresponding weights.

[0126] In database-related query scenarios, in order to accurately measure the similarity between query keywords and various fields in the database, electronic devices will conduct similarity analysis on each field in the database from two dimensions: text level and vector level.

[0127] From a textual perspective, the electronic device can compare the field with the query keyword and count the number of identical characters in the field. Based on the number of identical characters, the electronic device calculates the text similarity, which directly reflects the degree of similarity between the field and the query keyword in terms of text composition. In one example, the electronic device can determine the number of characters in the field and the query keyword that have fewer characters, and determine the ratio of the number of identical characters to this number as the textual similarity.

[0128] Electronic devices can also perform vector-level analysis, leveraging advanced natural language processing technology to convert each field into a corresponding field vector and the query keyword into a corresponding query vector. Vector similarity is then determined by calculating the distance between the field vector and the query vector. Vector similarity reveals the inherent connection between the field and the query keyword from a deeper perspective, including semantics and contextual associations, addressing the semantic limitations of simple text comparison.

[0129] After obtaining the text similarity and vector similarity, and combining them with the actual application scenario and requirements, we assign reasonable weights to each. We then combine the text similarity and vector similarity through weighted summation to derive the comprehensive similarity between the query keyword and the field. This similarity can also be referred to as the score between the query keyword and the field. In one example, the weight corresponding to the text similarity can be 0.3, and the weight corresponding to the vector similarity can be 0.6.

[0130] In one example, after the electronic device obtains the text similarity, vector similarity and comprehensive similarity, for each field, if the text similarity corresponding to the field is greater than the preset text threshold, the vector similarity corresponding to the field is greater than the preset vector threshold, and the comprehensive similarity corresponding to the field is greater than the preset threshold, then the field can be determined as a candidate field.

[0131] In one example, the electronic device may further sort the similarity between each field and a query keyword from high to low, and determine the first preset number of fields as candidate fields.

[0132] The above technical solution has the following advantages or beneficial effects: the similarity obtained by the method provided in the embodiment of the present application can comprehensively and accurately reflect the degree of matching between the query keywords and the fields of the database, provide strong support for the sorting and screening of database retrieval results, and effectively improve the accuracy and efficiency of the query.

[0133] Due to the diversity of database tables, many have more than 20 fields. Especially after data governance, the number of fields in a single table may exceed 50. However, when users actually retrieve data, they often only involve no more than 10 valid fields. Therefore, it is necessary to provide approximate candidate fields during schema generation. This approach can shorten the prompt length and speed up searches. It can also reduce the impact of irrelevant fields and enable accurate queries with large language models.

[0134] Figure 2 A schematic diagram of a process for determining field similarity provided in an embodiment of the present application.

[0135] Depend on Figure 2It can be seen that the electronic device can first obtain the query text, that is, Figure 2 In the query, elements are extracted from the query text, entities such as locations are extracted, and query keywords are obtained. Through word search, that is, vector search, the similarity between the query keywords and the fields in the field library is determined. This similarity can also be called field scoring, wherein the field vector of the field can be obtained. The field vector can be obtained based on the original field name or the field meaning name.

[0136] In order to perform queries accurately and efficiently, based on the above embodiments, in the embodiment of the present application, each of the first largest language model and the second largest prediction model is fine-tuned in the following manner:

[0137] For each sample SQL statement, obtain the standard query text corresponding to the sample SQL statement;

[0138] Based on each sample SQL statement and the corresponding standard query text, each first language model and the second language model are fine-tuned.

[0139] In order to enable the first language model to accurately generate SQL statements and the second language model to accurately and effectively perform queries, the electronic device can restore the SQL to the question by reversing the question and the response, which helps to increase the understanding ability of the first and second language models.

[0140] Specifically, the electronic device can obtain the standard query text corresponding to each sample SQL text collected. In one example, the electronic device can input the sample SQL statement into the third language model. The third language model, with its powerful language understanding and text generation capabilities, deeply analyzes and reconstructs the input sample SQL text, and then outputs the corresponding standard query text.

[0141] After acquiring the standard query text, the electronic device fine-tunes each of the first and second largest language models based on each sample SQL text and its corresponding standard query text. During the fine-tuning process, we adjust the model's parameters and structure to guide the large language model to gradually learn and master how to accurately convert the original SQL text, which may contain various non-standard or complex structures, into a query text that conforms to the standard specifications.

[0142] The above technical solution has the following advantages and benefits: By generating standard query text, it provides high-quality supervision signals for the fine-tuning process, enabling the first and second language models to learn more accurate and standardized query patterns, effectively avoiding the subjectivity and errors that may be introduced by manual annotation. Through fine-tuning training on a large number of sample SQL texts and standard query texts, the first and second language models are able to continuously optimize their internal knowledge representation and reasoning capabilities, thereby providing high-quality query results for a variety of complex and diverse SQL query tasks.

[0143] In order to accurately and effectively obtain the target SQL statement, based on the above embodiments, in the embodiment of the present application, the fine-tuning of each first language model and the second language model based on each sample SQL statement and the corresponding standard query text includes:

[0144] Based on each sample SQL statement, the corresponding standard query text, and the preset query text, a first evidence of the SQL statement and a preset second evidence for querying based on the SQL statement are generated, and each first language model and the second prediction model are fine-tuned so that each first language model learns the first evidence and the second prediction model learns the second evidence.

[0145] In order to enable the first language model to accurately generate SQL statements and the second language model to accurately and effectively perform queries, the electronic device can input the first evidence for generating SQL statements into the first language model and input the second evidence for querying based on the SQL statements into the second language model.

[0146] Specifically, in addition to each sample SQL statement and the corresponding standard query text, the present embodiment also introduces a preset first evidence specifically for generating SQL text from query text and a preset second evidence for querying based on SQL statements. These evidences contain rich knowledge information, such as query logic rules for specific business scenarios, common data table structures and field associations, and some typical query patterns and examples. They are the key bridge connecting natural language query intent with SQL code implementation.

[0147] During the fine-tuning phase, the electronic device treats each sample SQL text, its corresponding standard query text, and these preset first pieces of evidence as a complete data unit and inputs them into each of the first language models. After receiving this data, each first language model conducts deep learning and analysis using its complex internal neural network structure. It not only needs to understand the mapping relationship between the sample SQL text and the standard query text, but also deeply explores the query logic and rules contained in the first piece of evidence. By continuously adjusting its parameters and weights, the model gradually learns how to leverage the knowledge in the first piece of evidence to accurately convert natural language-style query text into SQL text that meets business requirements and grammatical specifications. It then inputs the second piece of evidence into the second language model, which then learns this second piece of evidence and conducts deep learning and analysis using its complex internal neural network structure.

[0148] The above technical solution has the following advantages or beneficial effects: the introduction of preset evidence provides a richer and more professional knowledge source for the large language model, enabling the large language model to be exposed to broader and deeper query logic and rules during the learning process, thereby significantly improving the model's understanding and processing capabilities of complex query scenarios.

[0149] In an embodiment of the present application, Evidence (i.e., key knowledge information used to assist query generation and understanding) is used as the core learning object to guide the large language model to deeply explore the precise correspondence between SQL statements and Evidence. Specifically, the electronic device gives full play to the powerful capabilities of the large language model and makes purposeful modifications to the originally accurate SQL statements. These modifications are intended to simulate various situations that may arise in actual applications, such as generating SQL statements with incorrect execution results, or constructing SQL statements with relatively low execution efficiency. Subsequently, the modified SQL statements are organically combined with the original correct SQL statements to carefully construct a discriminant data set. On this basis, a reinforcement learning scheme is introduced to significantly enhance the SQL generation effect by continuously optimizing the decision-making process of the model.

[0150] To further improve the accuracy and rationality of the model's field selection, the electronic device can systematically generate a field selection dataset based on field information and user questions. This dataset helps the model clearly understand the inherent relationship between different fields and specific questions, allowing it to more accurately select appropriate fields when generating SQL statements.

[0151] In addition, a regeneration strategy is employed, using forged SQL statements (to simulate various possible errors or special cases) as well as real SQL statements to guide the large language model in regenerating SQL. This process effectively cultivates the large language model's reflective ability, enabling it to autonomously analyze and optimize generated SQL statements when faced with different inputs and scenarios.

[0152] To ensure the broad applicability of the trained model and its compatibility with most mainstream SQL databases, the dataset construction fully incorporates the syntax rules of various database types, including MySQL, Oracle, and PostgreSQL. This allows the large language model to fully learn the differences and commonalities between different database syntaxes, enabling it to generate accurate and efficient SQL statements in real-world applications based on diverse database environments.

[0153] This embodiment fine-tunes a large language model for multiple SQL types through multi-stage tasks, including tasks with small parameters (3B) and large parameters (32B), to achieve diverse SQL generation and improve generalization. These tasks include Text2SQL, SQL2Text, field selection, reference information selection, SQL dialects, and SQL regeneration. Multi-stage fine-tuning and multi-instance SQL generation, combined with strategies such as multi-task hybrid training and multi-instance generation reflection, optimize SQL generation results and increase generation accuracy.

[0154] In one example, a Text2SQL dataset is constructed by collecting scene datasets. The dataset contents are as follows:

[0155]

[0156] As can be seen above, this Text2SQL dataset, by including question identifiers, database identifiers, query text, evidence, standard SQL statements, and difficulty indicators, provides a comprehensive and accurate learning sample for the model, helping the large language model better understand and master the task of converting natural language to SQL statements. In this example, the user wants to know what the highest free eligibility rate is for K-12 students in Alameda County schools. The query text provides the semantic requirements for the large language model, which must accurately convert it into SQL statements to retrieve the corresponding answer from the database. The evidence clarifies how the free eligibility rate for K-12 students is calculated. The difficulty indicator marks the difficulty of the question, here marked as "simple." The difficulty indicator helps large language model developers understand the complexity of different questions. During large language model training, data can be stratified according to difficulty level, for example, starting with simple questions and gradually transitioning to complex questions, improving the model's learning efficiency and performance. Furthermore, during large language model evaluation, the model's performance can be analyzed in more detail based on difficulty level to understand its performance on questions of varying difficulty levels.

[0157] In order to determine the target SQL statement, based on the above embodiments, in the embodiment of the present application, determining the target element of the SQL field according to the number of identified identical elements includes:

[0158] For each identified element, determine the number of SQL fields in each SQL statement that are for that element;

[0159] The element with the highest number is determined as the target element of the SQL field;

[0160] If there are at least two elements whose quantities are both the highest quantity, it is determined that the target element does not exist in the SQL field.

[0161] When processing SQL statement analysis and element identification tasks, to determine the core elements associated with each SQL field, the electronic device can record the frequency of occurrence of each element corresponding to each SQL field in the SQL statement in detail, that is, determine the number of occurrences of the element in each SQL field. This statistical process needs to cover all parts of the SQL statement, including but not limited to field references in the SELECT clause and conditional judgments in the WHERE clause, to ensure that no possible element occurrence locations are missed, thereby obtaining accurate and comprehensive quantitative statistical results.

[0162] After counting all elements, the target element is determined. The electronic device carefully compares the counts of each element, selects the element with the highest count, and preliminarily identifies it as the target element for the SQL field. This process aims to identify the element with the closest correlation and highest frequency of occurrence to the SQL field, representing the primary semantic focus of the field in the SQL statement.

[0163] However, given the complexity and uncertainty that can exist in real-world scenarios, if the comparison reveals that at least two elements share the highest number, i.e., multiple elements tie for first place, the electronic device will determine that the SQL field does not have a unique target element. This is because when multiple elements occur with the same frequency, it's impossible to clearly determine which element best represents the core semantics of the field. To avoid subsequent processing errors caused by incorrect judgments, the electronic device determines that the target element does not exist.

[0164] The above technical solution has the following advantages or beneficial effects: by accurately counting the number of occurrences of each element in the SQL field and determining the target element based on this, it can minimize the errors that may be caused by human judgment or simple rule matching, ensure that the determination result of the target element is highly accurate, and thus provide a reliable basis for subsequent SQL statement analysis, optimization or conversion operations based on the target element.

[0165] Figure 3 A detailed process diagram of a query provided in an embodiment of the present application.

[0166] Figure 3 The largest language models are Qwen2.5, Liama3.2, and Phi, and the second largest language model is Qwen2.5. For example, the number of parameters of the largest language model is 3B, 3B, and 3.8B, respectively, and the number of parameters of the second largest language model is 32B.

[0167] Depend on Figure 3 It can be seen that each SQL statement can be obtained based on each first language model, and the target SQL statement can be obtained based on the minimum common element extraction and merging. The target SQL statement is input into the second language model to obtain the final SQL output, which is the query result.

[0168] Figure 4 This is a schematic diagram of the structure of a query device based on a large language model provided by this application, the device comprising:

[0169] Receiving and obtaining module 401 is configured to receive a query text, input the query text into at least two first large language models, and obtain a structured query language (SQL) statement output by each first large language model; wherein the at least two first large language models may be of the same type or different types; for each pre-stored SQL field, obtain the elements in the SQL field in each SQL statement, and determine a target element for the SQL field based on the number of each obtained element; the target element being the element with the largest number of corresponding elements;

[0170] Processing module 402 is used to splice the target elements of each acquired SQL field to determine a target SQL statement; input the target SQL statement and a preset database into a second language model to obtain a query result output by the second language model; wherein the parameter quantity of each first language model is smaller than the parameter quantity of the second language model.

[0171] In an optional embodiment, the processing module 402 is further configured to extract query keywords from the query text; determine the similarity between the query keywords and each field in the database, and determine the field with a similarity higher than a preset value as a target field;

[0172] The processing module 402 is specifically configured to randomly obtain, for each target field, a preset number of candidate data for the target field from a preset database, and obtain field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information for the target field; input the target SQL statement, each target field, the field text corresponding to each target field, and the obtained candidate data for each target field into a second language model to obtain a query result output by the second language model.

[0173] In an optional embodiment, the processing module 402 is further configured to extract query keywords from the query text; determine the similarity between the query keywords and each field in the database, and determine the field with a similarity higher than a preset value as a target field; for each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information for the target field;

[0174] The receiving and obtaining module 401 is specifically used to input the query text, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field into at least two first-large language models, and obtain the SQL statement output by each first-large language model.

[0175] In an optional embodiment, the processing module 402 is specifically used to determine, for each field in the database, the number of characters that are identical to the query keyword in the field, and determine the text similarity; and determine the vector similarity based on the distance between the field vector corresponding to the field and the query vector corresponding to the query keyword; and determine the similarity between the query keyword and the field based on the text similarity, the vector similarity and the corresponding weights.

[0176] In an optional implementation, the processing module 402 is further configured to obtain, for each sample SQL statement, a standard query text corresponding to the sample SQL statement;

[0177] Based on each sample SQL statement and the corresponding standard query text, each first language model and the second language model are fine-tuned.

[0178] In an optional embodiment, the processing module 402 is specifically used to generate a first evidence of an SQL statement and a preset second evidence for querying based on the SQL statement based on each sample SQL statement, the corresponding standard query text, and the preset query text, and fine-tune each first language model and the second prediction model so that each first language model learns the first evidence and the second prediction model learns the second evidence.

[0179] In an optional embodiment, the processing module 402 is specifically used to determine, for each identified element, the number of elements in the SQL field in each SQL statement; determine the element with the highest number as the target element of the SQL field; if there are at least two elements with the highest number, determine that there is no target element in the SQL field.

[0180] Figure 5 This is a schematic diagram of the structure of an electronic device provided in an embodiment of the present application. Based on the above embodiments, the embodiment of the present application further provides an electronic device, such as Figure 5 As shown, it includes: a processor 501, a communication interface 502, a memory 503 and a communication bus 504, wherein the processor 501, the communication interface 502, and the memory 503 communicate with each other through the communication bus 504;

[0181] The memory 503 stores a computer program, and when the program is executed by the processor 501 , the processor 501 performs any of the above method steps.

[0182] The communication bus mentioned in the electronic device mentioned above can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. This communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is used in the figure, but this does not mean that there is only one bus or only one type of bus.

[0183] The communication interface 502 is used for communication between the electronic device and other devices.

[0184] The memory may include random access memory (RAM) or non-volatile memory (NVM), such as at least one disk memory. Alternatively, the memory may be at least one storage device located away from the processor.

[0185] The above-mentioned processor can be a general-purpose processor, including a central processing unit, a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component, etc.

[0186] The present application also provides a computer storage readable storage medium, which stores a computer program that can be executed by an electronic device. When the program runs on the electronic device, the electronic device implements any of the above method steps when executing.

[0187] The present application provides a computer program product, which includes an executable program. When the executable program is executed by a processor, the method described above is implemented.

[0188] Although the preferred embodiments of the present application have been described, those skilled in the art may make additional changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present application.

[0189] Obviously, those skilled in the art may make various changes and modifications to this application without departing from the spirit and scope of this application. Thus, if these modifications and variations of this application fall within the scope of the claims of this application and their equivalents, this application is intended to include these modifications and variations.

Claims

1. A query method based on a large language model, characterized in that: The method comprises: receiving a query text, inputting the query text into at least two first large language models, and obtaining a structured query language (SQL) statement output by each first large language model; wherein the at least two first large language models are large language models of the same type or different types; For each predefined SQL field, obtain the elements of the SQL field in each SQL statement; wherein the SQL field includes SELECT, FROM, GROUP BY, ORDER, or WHERE; determine the target element of the SQL field based on the number of identified identical elements; the target element is the element with the largest number of corresponding elements; Concatenate the target elements of each SQL field obtained to determine the target SQL statement; The target SQL statement and the preset database are input into the second largest language model to obtain the query result output by the second largest language model; wherein the parameter amount of each of the first largest language models is smaller than the parameter amount of the second largest language model.

2. The method according to claim 1, characterized in that After receiving the query text, inputting the target SQL statement and the preset database into the second language model, and before obtaining the query result output by the second language model, the method further includes: Extracting query keywords from the query text; Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field; Inputting the target SQL statement and the preset database into the second language model and obtaining the query result output by the second language model includes: For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field; The target SQL statement, each target field, the field text corresponding to each target field, and the obtained candidate data for each target field are input into the second largest language model to obtain the query result output by the second largest language model.

3. The method according to claim 1, characterized in that After receiving the query text, and before inputting the query text into at least two first language models and obtaining the SQL statements output by each first language model, the method further includes: Extracting query keywords from the query text; Determine the similarity between the query keyword and each field in the database, and determine the field with a similarity higher than a preset value as the target field; For each target field, randomly obtain a preset number of candidate data of the target field from a preset database, and obtain the field text stored for the target field, wherein the field text includes text for explaining the target field and constraint information of the target field; Inputting the query text into at least two first language models and obtaining an SQL statement output by each first language model includes: The query text, each target field, the field text corresponding to each target field, and the obtained candidate data of each target field are input into at least two first large language models to obtain the SQL statement output by each first large language model.

4. The method according to claim 2 or 3, characterized in that Determining the similarity between the query keyword and each field in the database includes: For each field in the database, the number of characters that are identical to the query keyword in the field is determined to determine the character similarity; and the vector similarity is determined based on the distance between the field vector corresponding to the field and the query vector corresponding to the query keyword; and the similarity between the query keyword and the field is determined based on the character similarity, the vector similarity, and the corresponding weights.

5. The method according to claim 1, wherein Each of the first language model and the second oracle model is fine-tuned in the following way: For each sample SQL statement, obtain the standard query text corresponding to the sample SQL statement; Based on each sample SQL statement and the corresponding standard query text, each first language model and the second language model are fine-tuned.

6. The method according to claim 5, characterized in that The fine-tuning of each first language model and the second language model based on each sample SQL statement and the corresponding standard query text includes: Based on each sample SQL statement, the corresponding standard query text, the preset query text to generate the first basis evidence of the SQL statement, and the preset second basis evidence for querying based on the SQL statement, each first language model and the second prediction model are fine-tuned so that each first language model learns the first evidence and the second prediction model learns the second evidence.

7. The method according to claim 1, characterized in that Determining the target element of the SQL field according to the number of identified identical elements includes: For each identified element, determine the number of SQL fields in each SQL statement that are for that element; The element with the highest number is determined as the target element of the SQL field; If there are at least two elements whose quantities are both the highest quantity, it is determined that the target element does not exist in the SQL field.

8. A query device based on a large language model, characterized in that: The device comprises: A receiving and obtaining module is configured to receive a query text, input the query text into at least two first large language models, and obtain a structured query language (SQL) statement output by each first large language model; wherein the at least two first large language models may be of the same type or different types; for each pre-stored SQL field, obtain elements in the SQL field in each SQL statement, and determine a target element for the SQL field based on the number of each obtained element; the target element being the element with the largest corresponding number; The processing module is configured to concatenate the target elements of each acquired SQL field to determine a target SQL statement; input the target SQL statement and a preset database into a second language model to obtain a query result output by the second language model; wherein the number of parameters of each of the first language models is smaller than the number of parameters of the second language model.

9. An electronic device, characterized in that: The electronic device includes at least a processor and a memory, and the processor is configured to implement the steps of the query method based on a large language model as described in any one of claims 1 to 7 above when executing a computer program stored in the memory.

10. A computer-readable storage medium, characterized in that It stores a computer program, which, when executed by a processor, implements the steps of the query method based on a large language model as described in any one of claims 1 to 7.

Citation Information

Patent Citations

  • Database query statement generation method and device, equipment and storage medium

    CN116737758A

  • Information processing method and device based on large language model, equipment and storage medium

    CN119690981A

  • User question processing method and system and related products

    CN119691112A

  • Intelligent session method and server based on table data retrieval

    EP4339799A1

  • Voice-based performance query with non-semantic databases

    US20240104092A1

Cited By

  • Information determination method, electronic equipment, storage medium and product

    CN121579517A