A query method, device and equipment based on a large language model and a medium
Patent Information
- Application Number
- CN202510712883.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-29
- Publication Date
- 2026-10-16
- Estimated Expiration
- 2045-05-29
AI Technical Summary
[0005]本申请实施例提供了一种基于大语言模型的查询方法、装置及设备,用以解决由于生成的SQL语句无法正确执行,或执行结果与用户意图严重偏离,进而使得无法有效地进行查询的问题
[0056] Fourthly, this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described herein.
Smart Images

Figure CN120821736B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of data processing artificial intelligence (AI), large language models, and intelligent question answering, and in particular to a query method, apparatus, device, and medium based on a large language model. Background Technology
[0002] Text-to-Structured Query Language (Text2SQL) 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 do not need to master complex SQL syntax or database schema knowledge; they only need to clearly express their needs in natural language to complete professional-level data retrieval.
[0003] In related technologies, Text2SQL primarily relies on rule-based methods and statistical machine learning. Researchers need to manually write a large number of syntax rules and query templates, such as mapping fixed phrases like "query data from a certain table" to predefined SQL templates.
[0004] However, the related technologies have obvious shortcomings in terms of query validity: the generated SQL statements are often inaccurate and cannot be executed correctly, or the execution results deviate significantly from the user's intent, thus making it impossible to perform queries effectively. Summary of the Invention
[0005] This application provides a query method, apparatus, and device based on a large language model to solve the problem that queries cannot be performed effectively because the generated SQL statements cannot be executed correctly or the execution results deviate significantly from the user's intent.
[0006] Firstly, this application provides a query method based on a large language model, the method comprising:
[0007] Receive query text, input the query text into at least two primary language models, and obtain the Structured Query Language (SQL) statement output by each primary language model; wherein, the at least two primary language models are language models of the same or different types;
[0008] For each predefined SQL field, retrieve the elements of that SQL field in each SQL statement; where the SQL field includes SELECT, FROM, GROUP BY, ORDER, or WHERE; determine the target element of the SQL field based on the number of identical elements identified; the target element is the element with the largest number of identical elements.
[0009] The target elements of each obtained SQL field are concatenated to determine the target SQL statement;
[0010] The target SQL statement and the preset database are input into the second large language model to obtain the query results output by the second large language model; wherein the number of parameters of each first large language model is less than the number of parameters of the second large language model.
[0011] The above technical solution has the following advantages or beneficial effects: The electronic device uses at least two lightweight primary language models to generate SQL statements corresponding to the query text, and determines the optimal target element of each SQL field based on the number of elements in each SQL field. The target element is the element with the largest number of elements. The target element determined in this way is the element in the SQL statement determined by most primary language models. This consensus decision-making method avoids the influence of the erroneous preferences or randomness of individual primary language models on the results, thereby effectively eliminating the generation bias of a single primary language model and integrating the advantages of multiple models. This can improve the accuracy of SQL statement generation. The SQL statement and the database are jointly input into a secondary language model with a larger number of parameters for querying. The stronger semantic understanding and logical reasoning capabilities of the secondary language model are used to execute precise queries and determine the query results. This method not only ensures the syntactic correctness and semantic accuracy of the generated SQL, but also ensures a high degree of matching between the final query results and the user's intent through the deep reasoning capabilities of the primary language model, realizing the whole-process optimization from natural language to accurate query results.
[0012] In one possible implementation, after receiving the query text and before inputting the target SQL statement and a preset database into the second language model to obtain the query results output by the second language model, the method further includes:
[0013] Extract the query keywords from the query text;
[0014] Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields;
[0015] The step of inputting the target SQL statement and a preset database into the second language model and obtaining the query results output by the second language model includes:
[0016] For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for the target field;
[0017] The target SQL statement, each target field, the field text corresponding to each target field, and the candidate data obtained for each target field are input into the second language model to obtain the query results output by the second language model.
[0018] The above-mentioned technical solution has the following advantages or beneficial effects: The method provided by the embodiments of this application can enhance the understanding ability of the second language model, which not only ensures the accurate mapping of the query intent, but also makes full use of the language model's ability to parse complex data, thereby improving the accuracy of the query.
[0019] In one possible implementation, after receiving the query text and before inputting the query text into at least two first-largest language models and obtaining the SQL statement output by each first-largest language model, the method further includes:
[0020] Extract the query keywords from the query text;
[0021] Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields;
[0022] For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for the target field;
[0023] The step of inputting the query text into at least two primary language models and obtaining the SQL statement output by each primary language model includes:
[0024] The query text, each target field, the field text corresponding to each target field, and the candidate data of each target field are input into at least two first-largest language models to obtain the SQL statement output by each first-largest language model.
[0025] The above-mentioned technical solution has the following advantages or beneficial effects: The method provided by the embodiments of this application can enhance the understanding ability of the second language model, which not only ensures the accurate mapping of the query intent, but also makes full use of the language model's ability to parse complex data, thereby improving the accuracy of the query.
[0026] In one possible implementation, determining the similarity between the query keywords and each field in the database includes:
[0027] For each field in the database, determine the number of characters in that field that are identical to the query keyword, and determine the text similarity; then determine the vector similarity based on the distance between the field vector corresponding to that field and the query vector corresponding to the query keyword; finally, determine the similarity between the query keyword and that field based on the text similarity, the vector similarity, and their respective weights.
[0028] The above-mentioned technical solution has the following advantages or beneficial effects: the similarity obtained by the method provided in the embodiments of this application can comprehensively and accurately reflect the degree of matching between query keywords and database fields, providing strong support for the sorting and filtering of database retrieval results, and effectively improving the accuracy and efficiency of queries.
[0029] In one possible implementation, each of the first major language model and the second major oracle model is fine-tuned in the following way:
[0030] For each sample SQL statement, obtain the standard query text corresponding to that sample SQL statement;
[0031] Based on each sample SQL statement and its corresponding standard query text, fine-tuning is performed on each of the first and second largest language models.
[0032] The above-mentioned technical solution has the following advantages or beneficial effects: By generating standard query text, it provides high-quality supervision signals for the fine-tuning process, enabling the first language model and the second oracle model to learn more accurate and standardized query patterns, effectively avoiding the subjectivity and errors that may be caused by manual annotation. Through fine-tuning training with a large number of sample SQL texts and their standard query texts, the first language model and the second oracle model can continuously optimize their internal knowledge representation and reasoning capabilities, thus providing high-quality query results when facing various complex and ever-changing SQL query tasks.
[0033] In one possible implementation, the fine-tuning of each first and second major 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 first evidence for generating the SQL statement from the query text, and the preset second evidence for querying based on the SQL statement, each first language model and the second oracle model are fine-tuned so that each first language model learns the first evidence and the second oracle model learns the second evidence.
[0035] The above technical solution has the following advantages or beneficial effects: the introduction of pre-defined evidence provides the large language model with richer and more professional knowledge sources, enabling the large language model to access a wider range of query logics and rules during the learning process, thereby significantly improving the model's ability to understand and process complex query scenarios.
[0036] In one possible implementation, determining the target element of the SQL field based on the number of identical elements identified includes:
[0037] For each identified element, determine the number of times that SQL field represents that element in each SQL statement;
[0038] The element with the highest number of occurrences is identified as the target element for this SQL field;
[0039] If at least two elements have the maximum quantity, then the SQL field does not contain the target element.
[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 using this as a basis to determine the target element, the error that may be caused by human judgment or simple rule matching can be minimized, ensuring that the determination result of the target element is highly accurate, thereby providing a reliable foundation for subsequent SQL statement analysis, optimization or transformation based on the target element.
[0041] Secondly, this application provides a query device based on a large language model, the device comprising:
[0042] The receiving and acquiring module is used to receive query text, input the query text into at least two primary language models, and acquire the structured query language (SQL) statements output by each primary language model; wherein, the at least two primary language models are language models of the same or different types; for each pre-saved SQL field, the elements in the SQL field of each SQL statement are acquired, and the target element of the SQL field is determined based on the number of each acquired element; the target element is the element with the largest corresponding number.
[0043] The processing module is used to concatenate the target elements of each obtained SQL field to determine the target SQL statement; input the target SQL statement and the preset database into the second large language model to obtain the query results output by the second large language model; wherein, the number of parameters of each first large language model is less than the number of parameters of the second large language model.
[0044] In an optional implementation, 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 identify fields with similarity higher than a preset value as target fields.
[0045] The processing module is specifically used to randomly retrieve a preset number of candidate data for each target field from a preset database, and retrieve the field text saved for each target field, wherein the field text includes text used to explain 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 retrieved candidate data for each target field into the second large language model, and obtain the query results output by the second large language model.
[0046] In an optional implementation, 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 identify fields with similarity higher than a preset value as target fields; for each target field, randomly obtain a preset number of candidate data for the target field from a preset database, and obtain the field text saved for the target field, wherein the field text includes text used to explain the target field and constraint information of 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 candidate data of each target field into at least two first-largest language models, and to obtain the SQL statement output by each first-largest language model.
[0048] In one optional implementation, the processing module is specifically configured to, for each field in the database, determine the number of characters in the field that are identical to the query keyword, 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 their respective weights.
[0049] In one optional implementation, the processing module is further configured to obtain the standard query text corresponding to each sample SQL statement;
[0050] Based on each sample SQL statement and its corresponding standard query text, fine-tuning is performed on each of the first and second largest language models.
[0051] In one optional implementation, the processing module is specifically used to generate a first Evidence for the SQL statement and a second Evidence for querying based on the SQL statement for each sample SQL statement, the corresponding standard query text, and the preset query text, and to fine-tune each first large language model and the second large oracle model so that each first large language model learns the first Evidence and the second large oracle model learns the second Evidence.
[0052] In one optional implementation, the processing module is specifically configured to, for each identified element, determine the number of times the SQL field in each SQL statement represents that element; 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, then determine that the SQL field does not have a target element.
[0053] Thirdly, this application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0054] Memory, used to store computer programs;
[0055] A processor, used to execute a program stored in memory, implements the method described.
[0056] Fourthly, this application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described herein.
[0057] Fifthly, this application provides a computer program product comprising an executable program that is executed by a processor to implement the method described.
[0058] In this embodiment, the electronic device uses at least two lightweight first large language models of the same or different types to generate SQL statements corresponding to the query text. Based on the number of elements in each SQL field, it determines the optimal target element for each SQL field, where the target element is the element with the largest number of elements. The target element determined in this way is an element in the SQL statement determined by a majority of the first large language models. This consensus-based decision-making method avoids the influence of erroneous preferences or randomness of individual first large language models on the results. This effectively eliminates the generation bias of a single large language model and integrates the advantages of multiple models, thereby improving the accuracy of SQL statement generation. The SQL statement and database are then input into a second large language model with a larger number of parameters for querying. The second large language model's stronger semantic understanding and logical reasoning capabilities are used to execute precise queries and determine the query results. This method ensures both the syntactic correctness and semantic accuracy of the generated SQL and guarantees a high degree of matching between the final query results and the user's intent through the deep reasoning capabilities of the large language models, achieving end-to-end optimization from natural language to accurate query results. Attached Figure Description
[0059] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0060] Figure 1 A schematic diagram illustrating the process of a query method based on a large language model provided in an embodiment of this application;
[0061] Figure 2 This application provides a schematic diagram illustrating a process for determining field similarity in an embodiment.
[0062] Figure 3 A detailed schematic diagram illustrating a query process provided in an embodiment of this 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 this application;
[0064] Figure 5 This is a schematic diagram of an electronic device structure provided in an embodiment of this application. Detailed Implementation
[0065] To make the objectives and implementation methods of this application clearer, the exemplary implementation methods of this application will be clearly and completely described below with reference to the accompanying drawings of the exemplary embodiments of this application. Obviously, the exemplary embodiments described are only some embodiments of this application, and not all embodiments.
[0066] It should be noted that the brief descriptions of terms in this application are only for the convenience of understanding the embodiments described below, and are not intended to limit the embodiments of this application. Unless otherwise stated, these terms should be understood in their ordinary and common meaning.
[0067] The terms "first," "second," "third," etc., used in the specification, claims, and accompanying drawings of this application are used to distinguish similar or related objects or entities and do not necessarily imply a specific order or sequence, unless otherwise specified. It should be understood that such terms are interchangeable where appropriate. The terms "comprising" and "having," and any variations thereof, are intended to be comprehensive but not exclusive; for example, a product or device comprising a series of components is not necessarily limited to all components explicitly listed, but may include other components not explicitly listed or inherent to such product or device.
[0068] The term "module" refers to any known or subsequently developed hardware, software, firmware, artificial intelligence, fuzzy logic, or combination of hardware and / or software code that is capable of performing the functions associated with that element.
[0069] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some or all of the technical features therein. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of this application.
[0070] For ease of explanation, the above description has been provided in conjunction with specific embodiments. However, the above exemplary discussion is not intended to be exhaustive or to limit the embodiments to the specific forms disclosed above. Various modifications and variations can be obtained based on the above teachings. The selection and description of the above embodiments are for the purpose of better explaining the principles and practical applications, thereby enabling those skilled in the art to better utilize the described embodiments and various different variations of embodiments suitable for specific use considerations.
[0071] To improve query accuracy, embodiments of this application provide a query method, apparatus, device, and medium based on a large language model.
[0072] The query method based on large language models includes: an electronic device receiving query text, inputting the query text into at least two first large language models, and obtaining the SQL statement output by each first large language model; for each predefined SQL field, obtaining the element of that SQL field in each SQL statement; wherein the SQL field includes SELECT, FROM, GROUPBY, ORDER, or WHERE; determining the target element of the SQL field based on the number of identical elements identified; determining the target SQL statement based on the target element of each obtained SQL field; inputting the target SQL statement and a preset database into a second large language model, and obtaining the query result output by the second large language model; wherein the number of parameters in each first large language model is less than the number of parameters in the second large language model.
[0073] Figure 1 A schematic diagram illustrating a query method based on a large language model provided in this application embodiment, the process including the following steps:
[0074] S101: Receive query text, input the query text into at least two primary language models, and obtain the SQL statement output by each primary language model; wherein, the at least two primary language models are language models of the same or different types.
[0075] S102: For each predefined SQL field, obtain the element of that SQL field in each SQL statement; wherein, the SQL field includes SELECT, FROM, GROUPBY, ORDER, or WHERE; determine the target element of the SQL field based on the number of identical elements identified; the target element is the element with the largest corresponding quantity.
[0076] S103: Concatenate the target elements of each obtained SQL field to determine the target SQL statement.
[0077] S104: Input the target SQL statement and the preset database into the second large language model to obtain the query results output by the second large language model; wherein, the number of parameters of each first large language model is less than the number of parameters of the second large language model.
[0078] The query method based on a large language model provided in this application is applied to an electronic device, which can be a smart device such as a PC or a server.
[0079] Electronic devices can receive user-submitted query text via multi-channel interactive interfaces, including natural language commands, semi-structured descriptions, or speech-to-text input. Query text may originate from text box submissions or command interactions within enterprise chat tools. For example, the query text might be: "Find customers with sales exceeding 1 million in 2023, sorted by sales from highest to lowest, and display the 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. For ease of distinction, these large language models can be referred to as the first large language model, which has a smaller number of parameters. Each first large language model can be of the same type or different types. In one example, the electronic device can input the query text into three first large language models, which can be head-based large language models such as GPT-4 and Claude-3. The electronic device can obtain the output of each first large language model, which is the SQL statement. To ensure that each first large language model outputs an SQL statement, the electronic device can input the query text and preset prompts into at least two of the first large language models. These prompts are used to prompt the first large language models to output the SQL statement.
[0081] After obtaining the SQL statements output by each primary language model, the electronic device can retrieve the elements of each predefined SQL field (also referred to as a region), including SELECT, FROM, GROUP BY, ORDER, WHERE, etc., within each SQL statement. The elements of the same SQL field may differ across SQL statements. To accurately determine the target element for each SQL field, the electronic device can identify the number of identical elements detected. In one example, the electronic device can identify the element with the highest number of identical elements as the target element for that SQL field; in this case, the target element is the element with the highest number of identical elements.
[0082] When processing multiple SQL statements, if a certain SQL field is found to appear repeatedly in one of the SQL statements (e.g., the same SQL field appears multiple times), the electronic device determines the exact number of occurrences of that SQL field by verifying the number of occurrences of that SQL field in other SQL statements. In one example, the electronic device can determine the exact number of occurrences of that SQL field as the minimum number of occurrences. If the exact number of occurrences of that SQL field is not one, the number of each element can be determined individually, and the element with the highest occurrence count (the standard number of occurrences) can be identified as the target element of that 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 concatenate the target elements of each SQL field according to SQL syntax rules to obtain the complete target SQL statement.
[0084] The electronic device can input the acquired target SQL statement and a preset database into a second language model and obtain the query results output by the second language model. Furthermore, to enable the second language model to output query results, the electronic device can also input preset prompts to guide the second language model in its query process.
[0085] To ensure accurate and efficient querying, the second largest language model has a larger number of parameters than each of the first largest language models. In one example, the first largest language model may have 3 bytes of parameters, and the second largest language model may have 32 bytes of parameters.
[0086] This application proposes a multi-stage SQL generation scheme that significantly improves the inaccuracy of schema linking and the low accuracy of SQL generation in Text2SQL tasks. This application selects a large language model for SQL generation, constructs multiple large language models with small parameters, and obtains staged SQL statements. These SQL statements are then input into the large language model with large parameters to obtain the final SQL query result, achieving multi-model reflective generation.
[0087] The embodiments in this application are derived from the Cloud Brain product big data platform and the traffic big language model.
[0088] With the development of machine learning and natural language processing technologies, statistical methods have gradually emerged. Researchers utilize large-scale corpus data and machine learning algorithms to learn the mapping relationship between natural language and SQL. These models can automatically extract features from large amounts of data, improving the accuracy and generalization ability of the conversion. When dealing with common query patterns, statistical models can provide relatively accurate SQL translations. In recent years, with the emergence of Large Language Models (LLMs), Text2SQL technology has made breakthrough progress. Large pre-trained language models such as GPT and BERT, trained on large-scale corpora, possess powerful language understanding and generation capabilities. By using natural language queries and corresponding SQL queries as input-output pairs, fine-tuning the LLM or directly utilizing its language understanding capabilities can achieve efficient Text2SQL conversion. LLMs significantly outperform traditional methods on Text2SQL tasks, greatly improving the efficiency and accuracy of data queries and providing users with a more convenient and intelligent database interaction experience. Current industry research focuses on Text2SQL technology based on LLM (Local Language Modeling). This process typically involves three steps: retrieving tables and fields related to the question, assembling them into a unified format such as Data Definition Language (DDL) or JavaScript Object Notation (JSON), combining the obtained format with prompts, and submitting it to a large language model for SQL statement generation. After generating the SQL statement, it is directly retrieved, executed to obtain the result, and then returned to the large language model to output the response. However, this method suffers from significant errors in field retrieval and excessive information redundancy, impacting the quality of the final generated SQL. The generated SQL is generally of poor quality and prone to producing complex, inefficient statements. In contrast, the method provided in this application provides an accurate and efficient way to obtain SQL statements, and by querying the target SQL statement using a second large language model, the query results can be obtained accurately and effectively.
[0089] In this embodiment, the electronic device utilizes at least two lightweight first-level language models to generate SQL statements corresponding to the query text. Based on the number of elements in each SQL field, it determines the optimal target element for each SQL field, where the target element is the element with the largest number of elements. This consensus-based decision-making avoids the influence of erroneous preferences or randomness of individual first-level language models on the results. This effectively eliminates the generation bias of a single language model and integrates the advantages of multiple models, thereby improving the accuracy of SQL statement generation. The SQL statement and database are then input into a second-level language model with a larger number of parameters for querying. The second-level language model's stronger semantic understanding and logical reasoning capabilities are used to execute precise queries and determine the query results. This method ensures both the syntactic correctness and semantic accuracy of the generated SQL and guarantees a high degree of matching between the final query results and the user's intent through the deep reasoning capabilities of the large language models, achieving end-to-end optimization from natural language to accurate query results.
[0090] To ensure accurate and efficient querying, based on the above embodiments, in this embodiment, after receiving the query text and before inputting the target SQL statement and a preset database into the second language model to obtain the query results output by the second language model, the method further includes:
[0091] Extract the query keywords from the query text;
[0092] Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields;
[0093] The step of inputting the target SQL statement and a preset database into the second language model and obtaining the query results output by the second language model includes:
[0094] For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for the target field;
[0095] The query text, 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 language model to obtain the query results output by the second language model.
[0096] Because the database contains a lot of information, inputting the entire database into the second language model may reduce the query capability of the second language model and result in a lower query accuracy. In order to improve query accuracy, electronic devices can identify fields in the database that are related to the query text and input the information of the relevant fields into the second language model.
[0097] Specifically, after receiving the query text, the electronic device can first extract the query keywords from the query text. Natural Language Processing (NLP) tools are used to segment and tag the query text, identifying key components such as nouns, verbs, and proper nouns. Sentence structure is analyzed to extract core components such as subject, object, and predicate. Noun phrases (such as product names, dates, and locations), verbs (such as "query" and "statistics"), and domain-specific terms are retained, while stop words (such as "of," "please," and "how") are removed.
[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, this similarity can be semantic similarity. By setting a similarity threshold, fields with high relevance are selected as target fields. After obtaining the target fields, in order to facilitate the understanding of the target fields by the second language model, the electronic device can retrieve data for each target field from the database. Specifically, it can randomly retrieve a preset number of data points for each target field. The electronic device can also retrieve the field text saved for each target field, which includes text used to explain the target field and constraint information for the target field.
[0099] The target SQL statement, each target field, the corresponding text for each target field, and the candidate data for each target field are all input into the second language model for processing. Through deep fusion analysis using the language model, accurate query results are ultimately output.
[0100] The above-mentioned technical solution has the following advantages or beneficial effects: The method provided by the embodiments of this application can enhance the understanding ability of the second language model, which not only ensures the accurate mapping of the query intent, but also makes full use of the language model's ability to parse complex data, thereby improving the accuracy of the query.
[0101] To accurately and effectively generate SQL statements, based on the above embodiments, in this embodiment, after receiving the query text and before inputting the query text into at least two first-largest language models to obtain the SQL statement output by each first-largest language model, the method further includes:
[0102] Extract the query keywords from the query text;
[0103] Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields;
[0104] For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for the target field;
[0105] The step of inputting the query text into at least two primary language models and obtaining the SQL statement output by each primary language model includes:
[0106] The query text, each target field, the field text corresponding to each target field, and the candidate data of each target field are input into at least two first-largest language models to obtain the SQL statement output by each first-largest language model.
[0107] Since simply inputting the query text into each primary language model may lead to misunderstandings of the query text, potentially reducing the output capability of the primary language model and resulting in lower accuracy of the generated SQL statements, electronic devices can identify fields in the database related to the query text and input the information of these fields into each primary language model in order to generate SQL statements accurately and effectively.
[0108] Specifically, after receiving the query text, the electronic device can first extract the query keywords from the query text. Natural Language Processing (NLP) tools are used to segment and tag the query text, identifying key components such as nouns, verbs, and proper nouns. Sentence structure is analyzed to extract core components such as subject, object, and predicate. Noun phrases (such as product names, dates, and locations), verbs (such as "query" and "statistics"), and domain-specific terms are retained, while stop words (such as "of," "please," and "how") are removed.
[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, this similarity can be semantic similarity. By setting a similarity threshold, fields with high relevance are selected as target fields. After obtaining the target fields, in order to facilitate the understanding of the target fields by the second language model, the electronic device can retrieve data for each target field from the database. Specifically, it can randomly retrieve a preset number of data points for each target field. The electronic device can also retrieve the field text saved for each target field, which includes text used to explain the target field and constraint information for the target field.
[0110] The query text, each target field obtained, the corresponding field text for each target field, and the candidate data for each target field are all input into a large language model for processing. Through deep fusion analysis using the language model, a precise SQL statement is ultimately output.
[0111] The above-mentioned technical solution has the following advantages or beneficial effects: The method provided by the embodiments of this application can enhance the understanding ability of the second language model, which not only ensures the accurate mapping of the query intent, but also makes full use of the language model's ability to parse complex data, thereby improving the accuracy of the query.
[0112] This application proposes a novel database schema representation method. The original DDL is transformed into an easy-to-understand prompt word schema format through schema transformation. The column name matching of fields is achieved through keyword extraction and semantic hybrid retrieval. Then, 3-5 data samples are sampled to facilitate the understanding of the schema content by the first and second language models.
[0113] The schema, which describes the original database tables, is usually in DDL format, as shown below, taking a corporate credit rating table as an example:
[0114]
[0115]
[0116] Research has shown that transforming the schema into a hierarchical structure can improve the final generated result. Therefore, institutions such as Beihang University proposed the MAC-SQL solution, with the following definitions for prompt words and schema:
[0117]
[0118] Where desc_str represents the following:
[0119]
[0120] However, as can be seen from the above representation, the impact of the data itself on SQL generation is not considered in practical applications. Therefore, this application embodiment redesigns 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 major language model:
[0121]
[0122]
[0123] This application defines a novel schema representation method and integrates value filtering, which is superior to DDL, MAC-SQL and other solutions, showing that this method is more superior for Text2SQL tasks.
[0124] To improve the accuracy of query results, based on the above embodiments, in this embodiment, determining the similarity between the query keywords and each field in the database includes:
[0125] For each field in the database, determine the number of characters in that field that are identical to the query keyword, and determine the text similarity; then determine the vector similarity based on the distance between the field vector corresponding to that field and the query vector corresponding to the query keyword; finally, determine the similarity between the query keyword and that field based on the text similarity, the vector similarity, and their respective 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: textual and vector levels.
[0127] From a textual perspective, electronic devices can compare fields with query keywords, counting the number of identical characters in the field. Based on the number of identical characters, a text similarity score is calculated. This similarity metric directly reflects the degree of similarity between the field and the query keywords in terms of text composition. In one example, the electronic device can determine the number of identical characters in the field compared to the fewer characters in the query keywords, and the ratio of the number of identical characters to this number is the text similarity score.
[0128] Electronic devices can also be analyzed at the vector level. Specifically, advanced natural language processing techniques are used to convert each field into a corresponding field vector, and query keywords into corresponding query vectors. Then, the vector similarity is determined by calculating the distance between the field vectors and the query vectors. Vector similarity reveals the intrinsic connection between fields and query keywords from a deeper perspective, including semantics and contextual relationships, thus compensating for the limitations of simple text comparison in semantic understanding.
[0129] After obtaining the text similarity and vector similarity, and considering the actual application scenario and requirements, appropriate weights are assigned to each. The text similarity and vector similarity are then fused using a weighted summation method to obtain the overall 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 could be 0.3, and the weight corresponding to the vector similarity could be 0.6.
[0130] In one example, after obtaining the text similarity, vector similarity, and overall similarity, the electronic device determines each field as a candidate field if the determined text similarity is greater than a preset text threshold, the determined vector similarity is greater than a preset vector threshold, and the determined overall similarity is greater than a preset threshold.
[0131] In one example, the electronic device can also sort each field from high to low similarity to a certain query keyword, and determine the top preset number of fields as candidate fields.
[0132] The above-mentioned technical solution has the following advantages or beneficial effects: the similarity obtained by the method provided in the embodiments of this application can comprehensively and accurately reflect the degree of matching between query keywords and database fields, providing strong support for the sorting and filtering of database retrieval results, and effectively improving the accuracy and efficiency of queries.
[0133] Due to the diversity of database tables, many tables have more than 20 fields, especially after data governance, where a single table may have more than 50 fields. However, when users actually retrieve data, they usually only need no more than 10 valid fields. Therefore, it is necessary to provide approximate candidate fields during schema generation. This approach can compress the length of the prompt, speeding up retrieval, and reduce the impact of irrelevant fields, allowing large language models to perform accurate queries.
[0134] Figure 2 This is a schematic diagram illustrating a process for determining field similarity, provided in an embodiment of this application.
[0135] Depend on Figure 2It can be seen that electronic devices can first obtain the query text, that is... Figure 2 In the Query, elements are extracted from the query text, such as locations, to obtain query keywords. Through word retrieval, i.e. vector retrieval, the similarity between the query keywords and fields in the field library is determined. This similarity can also be called field score. The field vector can be obtained, which can be based on the original field name or the corresponding field meaning name.
[0136] To ensure accurate and efficient querying, based on the above embodiments, each first major language model and second major oracle model in this application embodiment is fine-tuned in the following way:
[0137] For each sample SQL statement, obtain the standard query text corresponding to that sample SQL statement;
[0138] Based on each sample SQL statement and its corresponding standard query text, fine-tuning is performed on each of the first and second largest language models.
[0139] To enable the first language model to accurately generate SQL statements and the second language model to perform queries accurately and effectively, electronic devices can reverse the question and response to restore the SQL to the question, which helps to increase the understanding capabilities of the first and second language models.
[0140] Specifically, for each collected sample SQL text, the electronic device can obtain the standard query text corresponding to that sample SQL text. In one example, the electronic device can input the sample SQL statement into the third language model. With its powerful language understanding and text generation capabilities, the third language model performs in-depth analysis and reconstruction of the input sample SQL text, and then outputs the corresponding standard query text.
[0141] After obtaining 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 guide the large language models to gradually learn and master how to accurately convert the original SQL text, which may contain various irregularities or complex structures, into query text that conforms to the standard specifications by adjusting the model's parameters and structure.
[0142] The above-mentioned technical solution has the following advantages or beneficial effects: 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 caused by manual annotation. Through fine-tuning training with a large number of sample SQL texts and their standard query texts, the first and second language models can continuously optimize their internal knowledge representation and reasoning capabilities, thus providing high-quality query results when faced with various complex and ever-changing SQL query tasks.
[0143] To accurately and effectively obtain the target SQL statement, based on the above embodiments, in this embodiment, the fine-tuning of each first and 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, the preset query text, the first Evidence of the generated SQL statement, and the preset second Evidence of the query based on the SQL statement, each first large language model and the second large oracle model are fine-tuned so that each first large language model learns the first Evidence and the second large oracle 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 perform queries accurately and effectively, 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 SQL statements into the second language model.
[0146] Specifically, in addition to each sample SQL statement and its corresponding standard query text, this application embodiment also introduces a first evidence specifically for generating SQL text for querying and a second evidence based on the SQL statement for querying. These evidences contain rich knowledge information, such as query logic rules in specific business scenarios, common data table structures and field relationships, as well as some typical query patterns and examples. They are the key bridge connecting natural language query intent and SQL code implementation.
[0147] During the fine-tuning phase, the electronic device inputs each sample SQL text, its corresponding standard query text, and these preset first equivids as a complete data unit into each primary language model. Upon receiving this data, each primary language model performs deep learning and analysis through 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 needs to delve into the query logic and rules contained within the first equivids. By continuously adjusting its parameters and weights, the model gradually learns how to utilize the knowledge in the first equivids to accurately convert natural language-style query text into SQL text that conforms to business requirements and grammatical norms. Furthermore, it inputs the second equivids into the secondary language model, which then learns from these second equivids through 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 pre-defined evidence provides the large language model with richer and more professional knowledge sources, enabling the large language model to access a wider range of query logics and rules during the learning process, thereby significantly improving the model's ability to understand and process complex query scenarios.
[0149] In the embodiments of this application, evidence (i.e., key knowledge information used to assist query generation and understanding) is used as the core learning object, guiding the large language model to deeply explore the precise correspondence between SQL statements and evidence. Specifically, the electronic device fully leverages the powerful capabilities of the large language model to purposefully modify the originally accurate SQL statements. These modifications aim to simulate various situations that may occur in real-world applications, such as generating SQL statements with incorrect execution results or constructing SQL statements with low execution efficiency. Subsequently, the modified SQL statements are organically combined with the original correct SQL statements to carefully construct a discriminative dataset. Based on this, a reinforcement learning scheme is introduced to significantly enhance the SQL generation effect by continuously optimizing the model's decision-making process.
[0150] Furthermore, to further improve the accuracy and rationality of the model's field selection, electronic devices can systematically generate a field selection dataset based on field information and user-submitted questions. This dataset helps the model clearly understand the intrinsic relationship between different fields and specific questions, thereby enabling it to more accurately select appropriate fields when generating SQL statements.
[0151] Furthermore, a regeneration strategy is employed, using forged SQL statements (to simulate various possible errors or special cases) and real SQL statements to guide the large language model in regenerating SQL. This process effectively cultivates the reflective ability of the large language model, enabling it to autonomously analyze and optimize the generated SQL statements when faced with different inputs and scenarios.
[0152] To ensure the trained model has broad applicability and can adapt to most mainstream SQL databases, the dataset construction process fully integrates the syntax rules of various database types such as MySQL, Oracle, and PostgreSQL. This approach 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 tailored to different database environments in practical applications.
[0153] This application's embodiments employ multi-stage task fine-tuning of various SQL generation large language models, including 3B with small parameters and 32B with large parameters, to diversify SQL generation and improve generation 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 mixed training and multi-instance generation reflection, optimize SQL generation results and improve generation accuracy.
[0154] In one example, a Text2SQL dataset is constructed by collecting scenario datasets. The dataset content is shown below:
[0155]
[0156] As described above, this Text2SQL dataset example provides a comprehensive and accurate learning sample for the model by including question identifiers, database identifiers, query text, evidence, standard SQL statements, and difficulty indicators. This helps 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 the highest free eligibility rate for K-12 students in schools in Alameda County. The query text provides the semantic requirement for the large language model, which needs to accurately convert it into an SQL statement 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 labels the difficulty of the question, here marked as "simple." Difficulty indicators can help large language model developers understand the complexity of different questions. During the training process, data can be stratified according to difficulty level, for example, allowing the large language model to learn simple questions first and gradually transition to complex ones, improving the learning efficiency and performance of the large language model. Simultaneously, during the evaluation of the large language model, the performance can be analyzed in more detail according to the difficulty level, understanding how the large language model performs on questions of different difficulty levels.
[0157] To determine the target SQL statement, based on the above embodiments, in this embodiment, determining the target element of the SQL field according to the number of identical elements identified includes:
[0158] For each identified element, determine the number of times that SQL field represents that element in each SQL statement;
[0159] The element with the highest number of occurrences is identified as the target element for this SQL field;
[0160] If at least two elements have the maximum quantity, then the SQL field does not contain the target element.
[0161] When processing SQL statement analysis and element identification tasks, to determine the core elements associated with each SQL field, electronic devices can meticulously record the frequency of occurrence of each element corresponding to that SQL field, i.e., determine the number of times that element appears 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 occurrences are missed, thereby obtaining accurate and comprehensive statistical results.
[0162] After counting all elements, the process moves to determining the target element. The electronic device meticulously compares the counts of each element, selecting the element with the highest count and initially identifying it as the target element for the SQL field. This process aims to find the element most closely related to the SQL field and occurring most frequently, representing the field's primary semantic meaning in the SQL statement.
[0163] However, considering the potential complexity and uncertainty in real-world scenarios, if the comparison process reveals that at least two elements have the highest frequency (i.e., multiple elements are tied 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 appear with the same frequency, it is impossible to definitively determine which element best represents the core semantics of the field. To avoid subsequent processing deviations due to incorrect judgments, a determination is made 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 using this as a basis to determine the target element, the error that may be caused by human judgment or simple rule matching can be minimized, ensuring that the determination result of the target element is highly accurate, thereby providing a reliable foundation for subsequent SQL statement analysis, optimization or transformation based on the target element.
[0165] Figure 3 This is a detailed schematic diagram illustrating a query process provided in an embodiment of this application.
[0166] Figure 3 The first largest language models are Qwen2.5, Liama3.2, and Phi, respectively. Taking Qwen2.5 as the second largest language model as an example, the number of parameters of the first largest language model is 3B, 3B, and 3.8B, respectively, while the number of parameters of the second largest language model is 32B.
[0167] Depend on Figure 3 As can be seen, we can first obtain each SQL statement based on each first-largest language model, extract and merge the least common elements to obtain the target SQL statement, input the target SQL statement into the second-largest 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 in this application. The device includes:
[0169] The receiving and acquiring module 401 is used to receive query text, input the query text into at least two first major language models, and acquire the structured query language (SQL) statements output by each first major language model; wherein, the at least two first major language models are major language models of the same or different types; for each pre-saved SQL field, the elements in the SQL field in each SQL statement are acquired, and the target element of the SQL field is determined based on the number of each acquired element; the target element is the element with the largest corresponding number.
[0170] The processing module 402 is used to concatenate the target elements of each obtained SQL field to determine the target SQL statement; input the target SQL statement and the preset database into the second large language model to obtain the query results output by the second large language model; wherein, the number of parameters of each first large language model is less than the number of parameters of the second large language model.
[0171] In an optional implementation, 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 identify fields with similarity higher than a preset value as target fields.
[0172] The processing module 402 is specifically used to, for each target field, randomly retrieve a preset number of candidate data for that target field from a preset database, and retrieve the field text saved for that target field, wherein the field text contains text used to explain 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 retrieved candidate data for each target field into the second large language model, and obtain the query results output by the second large language model.
[0173] In an optional implementation, 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 identify fields with similarity higher than a preset value as target fields; for each target field, randomly obtain a preset number of candidate data for the target field from a preset database, and obtain the field text saved for the target field, wherein the field text includes text used to explain the target field and constraint information of the target field;
[0174] The receiving and acquiring module 401 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-largest language models to obtain the SQL statement output by each first-largest language model.
[0175] In an optional implementation, the processing module 402 is specifically configured to, for each field in the database, determine the number of characters in the field that are the same as the query keyword, 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 their respective weights.
[0176] In an optional implementation, the processing module 402 is further configured to obtain the standard query text corresponding to each sample SQL statement;
[0177] Based on each sample SQL statement and its corresponding standard query text, fine-tuning is performed on each of the first and second largest language models.
[0178] In one optional implementation, the processing module 402 is specifically used to generate a first Evidence for the SQL statement and a second Evidence for querying based on the SQL statement for each sample SQL statement, the corresponding standard query text, and the preset query text, and to fine-tune each first large language model and the second large oracle model so that each first large language model learns the first Evidence and the second large oracle model learns the second Evidence.
[0179] In one optional implementation, the processing module 402 is specifically configured to, for each identified element, determine the number of times the SQL field in each SQL statement is the element; 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, then determine that the SQL field does not have a target element.
[0180] Figure 5 This application provides a schematic diagram of an electronic device structure. Based on the above embodiments, this application also provides an electronic device, such as... Figure 5 As shown, it includes: processor 501, communication interface 502, memory 503 and communication bus 504, wherein processor 501, communication interface 502 and memory 503 communicate with each other through communication bus 504.
[0181] The memory 503 stores a computer program, which, when executed by the processor 501, causes the processor 501 to perform any of the above method steps.
[0182] The communication bus mentioned in the above electronic devices can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not mean that there is only one bus or one type of bus.
[0183] Communication interface 502 is used for communication between the above-mentioned 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 storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0185] The processors mentioned above can be general-purpose processors, including central processing units, network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits, field-programmable gate arrays or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.
[0186] This application also provides a computer-readable storage medium storing a computer program executable by an electronic device, which, when run on the electronic device, causes the electronic device to perform any of the above method steps.
[0187] This application provides a computer program product, which includes an executable program that, when executed by a processor, implements the method described herein.
[0188] Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.
[0189] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the spirit and scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.
Claims
1. A query method based on a large language model, characterized in that, The method includes: Receive query text, input the query text into at least two primary language models, and obtain the structured query language SQL statement output by each primary language model; wherein, the at least two primary language models are language models of the same or different types; For each predefined SQL field, retrieve the elements of that SQL field in each SQL statement; wherein, the SQL field includes SELECT, FROM, WHERE, GROUP BY, or ORDER; determine the target element of the SQL field based on the number of identical elements identified; the target element is the element with the largest corresponding number. The target elements of each obtained SQL field are concatenated to determine the target SQL statement; The target SQL statement and the preset database are input into the second large language model to obtain the query results output by the second large language model; wherein the number of parameters of each first large language model is less than the number of parameters of the second large language model.
2. The method according to claim 1, characterized in that, After receiving the query text, and before inputting the target SQL statement and the preset database into the second language model to obtain the query results output by the second language model, the method further includes: Extract the query keywords from the query text; Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields; The step of inputting the target SQL statement and a preset database into the second language model and obtaining the query results output by the second language model includes: For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for 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 language model to obtain the query results output by the second 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 primary language models and obtaining the SQL statement output by each primary language model, the method further includes: Extract the query keywords from the query text; Determine the similarity between the query keywords and each field in the database, and identify the fields with similarity scores higher than a preset value as target fields; For each target field, a preset number of candidate data for that target field are randomly retrieved from a preset database, and the field text saved for that target field is retrieved, wherein the field text contains text used to explain the target field and constraint information for the target field; The step of inputting the query text into at least two primary language models and obtaining the SQL statement output by each primary language model includes: The query text, each target field, the field text corresponding to each target field, and the candidate data of each target field are input into at least two first-largest language models to obtain the SQL statement output by each first-largest language model.
4. The method according to claim 2 or 3, characterized in that, Determining the similarity between the query keywords and each field in the database includes: For each field in the database, determine the number of characters in that field that are identical to the query keyword, and determine the text similarity; then determine the vector similarity based on the distance between the field vector corresponding to that field and the query vector corresponding to the query keyword; finally, determine the similarity between the query keyword and that field based on the text similarity, the vector similarity, and their respective weights.
5. The method according to claim 1, characterized in that, Each of the first and second largest language models is fine-tuned in the following ways: For each sample SQL statement, obtain the standard query text corresponding to that sample SQL statement; Based on each sample SQL statement and its corresponding standard query text, fine-tuning is performed on each of the first and second largest language models.
6. The method according to claim 5, characterized in that, The fine-tuning of each first and second major language model based on each sample SQL statement and its corresponding standard query text includes: Based on each sample SQL statement, the corresponding standard query text, the preset query text to generate the first evidence for the SQL statement, and the preset second evidence for querying based on the SQL statement, each first language model and the second language model are fine-tuned so that each first language model learns the first evidence and the second language model learns the second evidence.
7. The method according to claim 1, characterized in that, The step of determining the target element of the SQL field based on the number of identical elements identified includes: For each identified element, determine the number of times that SQL field represents that element in each SQL statement; The element with the highest number of occurrences is identified as the target element for this SQL field; If at least two elements have the maximum quantity, then the SQL field does not contain the target element.
8. A query device based on a large language model, characterized in that, The device includes: The receiving and acquiring module is used to receive query text, input the query text into at least two primary language models, and acquire the structured query language (SQL) statements output by each primary language model; wherein, the at least two primary language models are language models of the same or different types; for each pre-saved SQL field, the elements in the SQL field of each SQL statement are acquired, and the target element of the SQL field is determined based on the number of each acquired element; the target element is the element with the largest corresponding number. The processing module is used to concatenate the target elements of each obtained SQL field to determine the target SQL statement; input the target SQL statement and the preset database into the second large language model to obtain the query results output by the second large language model; wherein, the number of parameters of each first large language model is less than the number of parameters of the second large language model.
9. An electronic device, characterized in that, The electronic device includes at least a processor and a memory, wherein the processor is used to execute a computer program stored in the memory to implement the steps of the query method based on a large language model as described in any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, It stores a computer program that, 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-7.
Citation Information
Patent Citations
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