Intelligent formula and SQL (Structured Query Language) generation method and equipment based on large language model, medium and program product
By using intelligent formula and SQL generation methods based on large language models, the problems of high technical threshold for users, low domain adaptability, and poor scalability of enterprise-level business data query tools are solved. End-to-end intelligent generation and multi-turn dialogue optimization are achieved, improving the accuracy of generated results and management efficiency.
Patent Information
- Application Number
- CN202511674467.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-14
- Publication Date
- 2026-03-03
AI Technical Summary
In existing technologies, enterprise-level business data query tools have high technical barriers for users, low domain adaptability and accuracy, lack information integration and verification mechanisms, poor scalability, and cannot automatically identify and generate accurate SQL query statements and business formulas.
It adopts an intelligent formula and SQL generation method based on a large language model. By receiving natural language questions, it uses a large language model to classify intent, and combines domain recognition, historical question-and-answer query, table name matching and table structure query to generate and verify SQL or formulas. It supports multi-strategy table name matching and multi-turn dialogue mechanism to achieve end-to-end intelligent generation.
It lowers the technical threshold for users, improves the accuracy and domain adaptability of generated results, realizes automatic integration and closed-loop verification of multi-source information, provides a unified management and maintenance mechanism, supports multiple rounds of interaction and demand iteration, and improves data processing efficiency.
Smart Images

Figure CN121597704A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of computer data processing and artificial intelligence, and in particular to an intelligent formula and SQL generation method, device, medium and program product based on a large language model for enterprise-level business data query scenarios. Background Technology
[0002] With the acceleration of enterprise digital transformation, the volume of business data is growing exponentially. SQL query statements (mainly used for data extraction) and business formulas (mainly used for data calculation) have become important tools for enterprise data processing. Currently, breakthroughs in large language model technology in the field of natural language processing provide the technological foundation for "natural language to data" conversion; however, existing technologies still have the following shortcomings or deficiencies:
[0003] 1. High technical threshold for users
[0004] Existing SQL / formula generation tools often require users to have basic syntax knowledge (such as SQL table join logic and formula function call rules). Non-technical users (such as finance and business personnel) find it difficult to use them independently and need to rely on IT personnel for assistance, resulting in low data processing efficiency.
[0005] 2. Low domain adaptability and accuracy.
[0006] The data table structures and business rules of different business areas of an enterprise (such as finance and supply chain) vary greatly. Existing tools lack the ability to identify and accurately adapt to different domains, and are prone to generating SQL / formulas with cross-domain errors (such as calling the financial table structure to generate supply chain query statements). At the same time, table name matching relies on single keyword retrieval and cannot handle semantic associations of table names (such as matching "customer information table" with "CUST_INFO table"), resulting in low accuracy of generated results.
[0007] 3. Lack of information integration and verification mechanisms
[0008] Existing tools cannot automatically integrate multi-source information such as table information, historical Q&A, and domain knowledge, requiring users to manually provide complete data dimensions; moreover, they lack a closed-loop mechanism for SQL syntax validation and formula logic validation, requiring users to manually check for errors in the generated results, further increasing the cost of use.
[0009] 4. Lack of unified management and maintenance capabilities
[0010] The existing tools have a high degree of coupling between their data sources (such as business knowledge and table structure) and the generation service, making it difficult for administrators to perform batch updates, version management, or anomaly investigations on the data sources. When the enterprise's data structure is adjusted, the tools need to be completely rebuilt, resulting in poor scalability. Summary of the Invention
[0011] In view of this, the present invention provides a method, apparatus, device, medium and program product for generating intelligent formulas and SQL based on a large language model, which at least partially solves the problems existing in the prior art.
[0012] To achieve the above objectives, the present invention adopts the following technical solution:
[0013] In a first aspect, the present invention provides an intelligent formula and SQL generation method based on a large language model, comprising the following steps:
[0014] Receiving natural language questions from users;
[0015] Using large language models for question intent classification;
[0016] When the intent of the question is an SQL generation task, analyze the question and list the SQL generation execution plan, including: domain identification → historical question and answer query → table name matching → table structure query → SQL generation → validation;
[0017] When the problem intent is a formula generation task, analyze the problem and list the formula generation execution plan, including: determine whether to query the database → domain identification → historical question and answer query → table name matching → table structure query → formula generation → validation;
[0018] Execute according to the execution plan, call the pre-registered tools to query the database and generate preliminary SQL; or, when it is determined that a database query is required, call the pre-registered tools to query the database and generate preliminary database formulas; when it is determined that a database query is not required, directly call the pre-registered tools to generate non-database formulas.
[0019] Validate and adjust the initial SQL or formulas;
[0020] Output the final SQL or formula.
[0021] As a further improvement of the present invention, the receiving of the user's natural language question includes: receiving the user's natural language question through interface input or API call;
[0022] The method of classifying question intent using a large language model includes: using the large language model in conjunction with keyword extraction to determine whether the question intent is an SQL generation task or a formula generation task; non-task-type intents are directly fed back to the user.
[0023] Furthermore, the pre-registered tools include: domain identification, historical question and answer query, table name matching, table structure query, SQL generation, database formula generation, and non-database formula generation;
[0024] The domain identification is used to identify the domain to which the question belongs through vector similarity queries;
[0025] The historical question-and-answer query is used to retrieve similar historical questions and answers from a vector database;
[0026] The table name matching is used to obtain the required table ID by matching table names through multiple strategies, including: matching table names using a combination of strategies such as direct matching, fuzzy matching, full-domain vector search, and limited-domain vector search to obtain the ID of the required table.
[0027] The direct matching includes precise matching of table IDs to quickly obtain accurate table information; the fuzzy matching includes using LIKE queries to fuzzy match table names and table IDs; the full-domain vector search includes using table name similarity search without domain filtering; the limited-domain vector search includes using table name similarity search with domain filtering; the hybrid method includes integrating results and querying table details to ensure that complete table information is obtained.
[0028] The table structure query includes retrieving table information from the database to provide the detailed structure and master-child relationships of the table for generating SQL queries;
[0029] The SQL generation includes automatically generating SQL based on domain recognition, historical question and answer queries, table name matching, and table structure queries.
[0030] The formula generation includes automatically generating database formulas based on domain identification, historical question and answer queries, table name matching, and table structure queries, or directly generating non-database formulas.
[0031] Furthermore, the domain identification is a cross-level domain identification;
[0032] And / or, the metadata of the pre-registered tool includes name, parameters, and maximum number of calls;
[0033] And / or, the pre-registered tool further includes an associated extended table; correspondingly, the SQL generation execution plan includes: domain identification → historical question and answer query → table name matching → table structure query → associated extended table → SQL generation → verification; the formula generation execution plan includes: determining whether to query the database → domain identification → historical question and answer query → table name matching → table structure query → associated extended table → formula generation → verification.
[0034] Furthermore, the method of calling pre-registered tools to query databases includes: relational databases and vector databases;
[0035] The relational database stores domain information, historical questions and answers, and table structures.
[0036] The vector database stores vector data in sets, including domain information, business table names, historical questions and answers, and formulas.
[0037] Furthermore, the verification and adjustment of the initial SQL or formula includes:
[0038] Perform syntax validation on the initially generated formulas or SQL statements to check whether they conform to SQL syntax rules or formula operation rules;
[0039] If the verification fails, it will be automatically corrected and adjusted. The automatic correction and adjustment includes: correcting syntax errors, supplementing missing SQL elements, optimizing statement structure, and correcting formula and function parameters. After the adjustment, the verification will be performed again until the syntax is completely correct.
[0040] Furthermore, it also includes building a multi-round dialogue mechanism to dynamically optimize SQL / formulas based on subsequent supplementary needs from users;
[0041] And / or, after generating the execution plan and before executing it, the process also includes: performing an information assessment to detect whether key information is missing; the key information includes domain ID and table structure; if sufficient, the execution plan is executed; if insufficient, the user is requested to supplement the information; after the tool calls to generate the initial SQL or formula, an assessment step is also included, requesting the user to supplement the information when there is insufficient valid information.
[0042] In a second aspect, the present invention provides a computer device, the device comprising: a processor and a memory;
[0043] The memory is used to store one or more program instructions;
[0044] The processor is configured to run one or more program instructions to perform the steps described above for generating intelligent formulas and SQL based on a large language model.
[0045] Thirdly, the present invention provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the above-described intelligent formula and SQL generation method based on a large language model.
[0046] Fourthly, the present invention provides a computer program product comprising computer program instructions that, when executed by a processor, implement the steps of the intelligent formula and SQL generation method based on a large language model as described above.
[0047] The present invention has at least the following beneficial effects:
[0048] 1. Lowered technical barrier for users: It realizes an end-to-end service of "natural language input - intelligent generation of SQL / formula", which does not require users to master the syntax knowledge and adapts to the needs of non-technical users.
[0049] 2. Improved domain adaptability and generation accuracy: Achieved accurate identification of business domains and multi-strategy table name matching (direct matching, fuzzy matching, vector search), ensuring that the generated result data uses the correct table information and conforms to domain business rules.
[0050] 3. Multi-source information integration and closed-loop verification are achieved: information such as table information, domain knowledge, and historical Q&A is automatically integrated to build a "generation-verification-adjustment" closed loop, reducing the cost of manual investigation.
[0051] 4. Provides a unified management and maintenance mechanism: The modular data source management interface and configuration system are designed to support administrators in efficiently maintaining data, monitoring service status, and improving scalability.
[0052] 5. Supports multi-round interaction and requirement iteration: Builds a multi-round dialogue mechanism that can dynamically optimize SQL / formulas based on subsequent user requests, improving interaction flexibility. Attached Figure Description
[0053] To more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings in the following description are merely exemplary, and those skilled in the art can derive other embodiments based on the provided drawings without creative effort.
[0054] Figure 1 This is a schematic diagram of the architecture of an intelligent formula and SQL generation method based on a large language model according to an embodiment of the present invention.
[0055] Figure 2 This is a flowchart of an embodiment of the intelligent formula and SQL generation method based on a large language model according to the present invention;
[0056] Figure 3 This is a flowchart of the intelligent agent workflow of an intelligent formula and SQL generation method based on a large language model according to an embodiment of the present invention. Detailed Implementation
[0057] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0058] This invention provides an intelligent formula and SQL generation method based on a large language model, primarily applicable to enterprise-level business data query scenarios, such as the following use cases:
[0059] (1) Enterprise ERP system
[0060] As an embedded auxiliary module of the ERP system, it provides users in business sectors such as finance, supply chain, and human resources with an automatic generation service from natural language to business formulas and SQL query statements. For example, if finance personnel enter "query the expense details of each department in Q1 2025", the corresponding SQL statement can be generated directly, or a business formula conforming to financial rules can be generated based on the need to "calculate the monthly accounts receivable turnover rate".
[0061] (2) Data Analysis and BI Platform
[0062] It integrates into enterprise BI (Business Intelligence) platforms, allowing non-technical data analysts to adjust query requirements through multi-round dialogues (such as "adding regional filter conditions to the existing sales data query results"), automatically optimizing SQL statements and returning analysis results, without requiring users to have knowledge of SQL syntax or data modeling.
[0063] (3) Financial and business calculations
[0064] For scenarios such as enterprise financial accounting, cost allocation, and performance evaluation, it provides functions for calling formula libraries and generating custom formulas. For example, if the human resources department enters "Calculate employee quarterly performance bonus (base salary × performance coefficient × department assessment coefficient)", it can match similar formulas in the formula library and adjust the parameters, or directly generate a new formula, while verifying the legality of the formula logic.
[0065] Figure 1 This is a schematic diagram illustrating the architecture of an intelligent formula and SQL generation method based on a large language model, according to an embodiment of the present invention.
[0066] like Figure 1As shown, this invention constructs an intelligent agent system through a three-layer architecture of "user interaction layer - core service layer - data storage layer," combined with large language model function call capabilities, multi-strategy table matching algorithms, and tool invocation mechanisms. The functions of each layer are as follows:
[0067] (a) User interaction layer: includes user interface and administrator interface.
[0068] (II) Core Service Layer: This layer comprises four core modules (Application Control, Tools & Services, AI Inference, and Content Verification), addressing the entire process from "requirement identification to tool invocation, result generation, and verification optimization." Its core functions include:
[0069] 1. Tool Registration and Management: Register tool metadata (name, parameters, maximum number of calls) to control tool call frequency and solve the problem of tool loop calls; through interaction with a large language model, select the appropriate tool call chain according to the question intent (SQL / formula); core registered tools include domain recognition, historical question and answer query, table name matching, table structure query, SQL generation, formula generation, etc., covering the entire process requirements.
[0070] 2. Validation and Adjustment: The generated formulas or SQL statements undergo rigorous syntax validation to check their compliance with SQL syntax rules and formula operation rules. Based on the results, adjustment strategies are implemented, such as correcting syntax errors, adding missing elements, and optimizing statement structure. Validation is then repeated until the syntax is completely correct.
[0071] 3. Core Tools:
[0072] (1) Domain identification: Identify the domain to which the problem belongs through vector similarity query, providing domain information for subsequent table name matching and data processing.
[0073] (2) Table name matching: Match table names using multiple strategies (direct matching, fuzzy matching, vector search) to ensure accurate acquisition of the required table ID.
[0074] Direct Match: Matches table IDs precisely, quickly obtaining accurate table information.
[0075] Fuzzy matching: Use LIKE queries to perform fuzzy matching on table names and table IDs, improving matching flexibility.
[0076] Full-domain vector search: Table name similarity search without domain filtering, expanding the search scope.
[0077] Domain-restricted vector search: Table name similarity search with domain filtering, which improves matching accuracy by combining domain information.
[0078] Mixed: Integrate results and query table details to ensure complete table information is obtained.
[0079] (3) Historical question and answer query: Query similar historical questions and answers from the vector database to improve the accuracy and efficiency of the answer by using historical data.
[0080] (4) Table structure query: Obtain table information through data storage layer tools to provide detailed table structure and master-child relationship for generating SQL queries.
[0081] (III) Data storage layer: The dual storage mode of "relational database + vector database" is adopted to solve the problems of data classification storage and efficient query.
[0082] (1) Relational database: Stores table structure, domain information, historical Q&A, etc., and supports basic data query;
[0083] (2) Vector database: It stores vector data such as business knowledge, domain information, business table names, historical Q&A, and formulas in separate sets, and improves the efficiency of domain identification, table matching, and historical Q&A through similarity query.
[0084] Figure 2 This is a flowchart of an embodiment of the intelligent formula and SQL generation method based on a large language model according to the present invention; Figure 3 This is a flowchart of the intelligent agent workflow of an intelligent formula and SQL generation method based on a large language model according to an embodiment of the present invention.
[0085] Combination Figure 2 , 3 As shown, the intelligent formula and SQL generation method based on a large language model in this embodiment includes:
[0086] Step S100: Receive the user's natural language question.
[0087] Here, user input in natural language can be received via interface input or API calls. For example, a natural language question could be: query the detailed expense expenditure of each department in Q1 2025, or calculate the monthly accounts receivable turnover rate.
[0088] Step S200: Classify the question intent using a large language model.
[0089] Specifically, a large language model can be used to extract keywords to determine the question intent as either an SQL generation task or a formula generation task, while non-task-type intents can be directly fed back to the user.
[0090] For example, in the aforementioned task "Query the detailed expense expenditure of each department in Q1 2025," the keyword extracted is "query," and the model, combined with semantic recognition, identifies the question intent as an SQL generation task. Similarly, in the task "Calculate the monthly accounts receivable turnover rate," the keyword extracted is "calculate," and the model, combined with semantic recognition, identifies the question intent as a formula generation task. If the task is identified as a non-SQL generation task or a formula generation task, the user is directly prompted to re-enter the information.
[0091] Step S300: Generate execution plan:
[0092] When the intent of the question is an SQL generation task, analyze the question and list the SQL generation execution plan. The SQL generation execution plan includes: domain identification → historical question and answer query → table name matching → table structure query → SQL generation → validation.
[0093] When the problem intent is a formula generation task, analyze the problem and list the formula generation execution plan. The formula generation execution plan includes: determining whether to query the database → domain identification → historical question and answer query → table name matching → table structure query → formula generation → validation.
[0094] Preferably, after generating the execution plan, the process further includes: performing an information assessment to detect whether any key information is missing; such as domain ID and table structure; if sufficient, executing the plan; if insufficient, requesting the user to provide additional information.
[0095] Step S400: Execute according to the execution plan, call the pre-registered tools to query the database and generate preliminary SQL.
[0096] When it is determined that a database query is required, a pre-registered tool is invoked to query the database and generate a preliminary database formula. When it is determined that a database query is not required, a pre-registered tool is directly invoked to generate a non-database formula.
[0097] In this step, tools need to be pre-registered. Pre-registered tools include: domain identification, historical question-and-answer query, table name matching, table structure query, SQL generation, database formula generation, and non-database formula generation. Through pre-registered tools, the transformation from user intent to an executable solution can be completed in one stop, including:
[0098] (1) Domain identification includes identifying the domain to which the question belongs through vector similarity query, providing domain information for subsequent table name matching and historical question and answer query; specifically, domain identification is cross-level domain identification.
[0099] (2) Historical question and answer query includes querying similar historical questions and answers from a vector database;
[0100] (3) Table name matching includes matching table names through multiple strategies to ensure accurate acquisition of the required table ID; matching table names through multiple strategies includes: matching table names through a combination of direct matching, fuzzy matching, full-domain vector search, and limited-domain vector search to obtain the table ID;
[0101] Direct matching includes exact matching of table IDs to quickly obtain accurate table information;
[0102] Fuzzy matching includes using LIKE queries to fuzzy match table names and table IDs;
[0103] Full-domain vector search includes table name similarity search without domain filtering;
[0104] Domain-limited vector search includes table name similarity search using domain filtering;
[0105] The process of combining results and querying table details ensures that complete table information is obtained.
[0106] (4) Table structure query includes obtaining table information from the database and providing detailed table structure and master-child relationship for generating SQL query;
[0107] (5) SQL generation includes automatic SQL generation based on domain identification, historical question and answer queries, table name matching, and table structure queries;
[0108] (6) Formula generation includes automatically generating database formulas based on historical question and answer queries, domain identification, and table name matching, or directly generating non-database formulas.
[0109] Additionally, preferably, the pre-registered tool may also include a relational extension table. The relational extension table is used to expand and associate other related tables based on the key fields and master-child relationships of the obtained table structure. Correspondingly, the SQL generation execution plan includes: domain identification → historical question-and-answer query → table name matching → table structure query → relational extension table → SQL generation → validation; the formula generation execution plan includes: determining whether to query the database → domain identification → historical question-and-answer query → table name matching → table structure query → relational extension table → formula generation → validation.
[0110] The metadata of the pre-registered tools includes their name, parameters, and maximum number of calls. During the tool invocation process, the corresponding tools are invoked according to the execution plan, and the number of calls is monitored to avoid excessive invocation.
[0111] The aforementioned query of databases in the database using pre-registered tools includes: relational databases and vector databases; the relational databases store domain information, historical questions and answers, and table structures;
[0112] The vector database stores vector data in sets, including domain information, business table names, historical questions and answers, and formulas.
[0113] Furthermore, after the tool generates the initial SQL or formula, an evaluation step can be included to request the user to provide additional information when there is insufficient valid information.
[0114] Step S500: Validate and adjust the initial SQL or formula.
[0115] Specifically, the initially generated formulas or SQL statements are subjected to syntax validation to check whether they conform to SQL syntax rules or formula operation rules. If the validation fails, automatic correction and adjustment are performed, including: correcting syntax errors, supplementing missing SQL elements (such as supplementing SQL WHERE conditions), optimizing statement structure, and correcting formula function parameters. After adjustment, validation is performed again until the syntax is completely correct and the result error problem is resolved.
[0116] Step S600: Output the final SQL or formula. Record the data for future use.
[0117] The above method also includes building a multi-turn dialogue mechanism that can dynamically optimize SQL / formulas based on subsequent user requests.
[0118] This invention solves the problem of "demand transformation - result optimization" through a closed-loop process of "accepting natural language input - intent classification - generating execution plan - information evaluation - tool invocation - result output - verification and adjustment".
[0119] This invention relies on multi-strategy table name matching (precise, fuzzy, vector search) and domain recognition, especially cross-level domain recognition, to solve the problems of inaccurate table name matching and cross-domain generation errors. Combined with modular tool registration management and a "generation-verification-adjustment" closed-loop mechanism, it significantly reduces the result error rate and significantly improves maintainability, scalability, and result reliability.
[0120] This invention shortens the overall response time by caching high-frequency data and asynchronously parallelizing tool calls; it also reduces resource waste and improves data query and tool collaboration efficiency by controlling tool call counts and optimizing database indexes.
[0121] This invention employs a registration-based tool management system, eliminating the need to refactor the core code when adding new tools; hierarchical storage (relational database + vector database) and batch data import simplify maintenance; and it supports dynamic adaptation of multiple models, reducing the cost of technology iteration.
[0122] This invention enables end-to-end generation of formulas / SQL from natural language, breaking through the operational barriers for non-technical users; it supports multi-turn dialogue for local process updates, improving the efficiency of requirement iteration and interaction; and it accurately identifies user intent, reducing generation deviations.
[0123] This invention adopts the intelligent agent design concept. From user needs to the final result, the entire process is driven by a large language model to autonomously analyze, make decisions and solve problems. It is more accurate in judging user intent, has a higher domain recognition accuracy, and has excellent multi-turn continuous dialogue performance.
[0124] In addition, this embodiment also provides a computer device, the device including: a processor and a memory; the memory is used to store one or more program instructions; the processor is used to run one or more program instructions to perform the steps of intelligent formula and SQL generation based on a large language model as described above.
[0125] Furthermore, this embodiment also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps of the intelligent formula and SQL generation method based on a large language model as described above.
[0126] In addition, this embodiment also provides a computer program product, which includes computer program instructions. When the computer program instructions are executed by a processor, they implement the steps of the intelligent formula and SQL generation method based on a large language model as described above.
[0127] In this embodiment of the invention, the processor can be an integrated circuit chip with signal processing capabilities. The processor can be a general-purpose processor, a digital signal processor (DSP), an application-specific integrated circuit (ASIC), a field-programmable gate array (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components. It can implement or execute the methods, steps, and logic block diagrams disclosed in this embodiment of the invention. The general-purpose processor can be a microprocessor or any conventional processor. The steps of the methods disclosed in this embodiment of the invention can be directly manifested as execution by a hardware decoding processor, or execution by a combination of hardware and software modules in the decoding processor. The software modules can be located in random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, or other mature storage media in the art. The processor reads information from the storage medium and, in conjunction with its hardware, completes the steps of the above methods. The storage medium can be memory, for example, volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static Random Access Memory (SRAM), Dynamic Random Access Memory (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDRSDRAM), Enhanced Synchronous DRAM (ESDRAM), Synchlink DRAM (SLDRAM), and Direct Rambus RAM (DRRAM).The storage media described in the embodiments of this invention are intended to include, but are not limited to, these and any other suitable types of memory. Those skilled in the art will recognize that the functions described in one or more of the above examples can be implemented using a combination of hardware and software. When applied software, the corresponding functions can be stored in a computer-readable medium or transmitted as one or more instructions or code on a computer-readable medium. Computer-readable media include computer storage media and communication media, wherein communication media include any medium that facilitates the transmission of computer programs from one place to another. Storage media can be any available medium accessible to general-purpose or special-purpose computers. Although the invention has been described in detail above with general description and specific embodiments, modifications or improvements can be made to it, which will be apparent to those skilled in the art. Therefore, all such modifications or improvements made without departing from the spirit of the invention are within the scope of protection claimed by this invention.
[0128] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Any simple modifications, equivalent changes, or alterations made by those skilled in the art using the disclosed technical content shall fall within the protection scope of the present invention.
Claims
1. A method for generating intelligent formulas and SQL based on a large language model, characterized in that, Includes the following steps: Receiving natural language questions from users; Using large language models for question intent classification; When the intent of the question is an SQL generation task, analyze the question and list the SQL generation execution plan, including: domain identification → historical question and answer query → table name matching → table structure query → SQL generation → validation; When the problem intent is a formula generation task, analyze the problem and list the formula generation execution plan, including: determine whether to query the database → domain identification → historical question and answer query → table name matching → table structure query → formula generation → validation; Execute according to the execution plan, call the pre-registered tools to query the database and generate preliminary SQL; or, when it is determined that a database query is required, call the pre-registered tools to query the database and generate preliminary database formulas; when it is determined that a database query is not required, directly call the pre-registered tools to generate non-database formulas. Validate and adjust the initial SQL or formulas; Output the final SQL or formula.
2. The intelligent formula and SQL generation method based on a large language model according to claim 1, characterized in that, The natural language questions received from users include: receiving natural language questions from users through interface input or API calls; The method of classifying question intent using a large language model includes: using the large language model in conjunction with keyword extraction to determine whether the question intent is an SQL generation task or a formula generation task; non-task-type intents are directly fed back to the user.
3. The intelligent formula and SQL generation method based on a large language model according to claim 1, characterized in that, The pre-registered tools include: domain identification, historical question and answer query, table name matching, table structure query, SQL generation, database formula generation, and non-database formula generation; The domain identification is used to identify the domain to which the question belongs through vector similarity queries; The historical question-and-answer query is used to retrieve similar historical questions and answers from a vector database; The table name matching is used to obtain the required table ID by matching table names using multiple strategies, including: matching table names using a combination of strategies such as direct matching, fuzzy matching, full-domain vector search, and limited-domain vector search to obtain the ID of the required table. The direct matching includes precise matching of table IDs to quickly obtain accurate table information; the fuzzy matching includes using LIKE queries to fuzzy match table names and table IDs; the full-domain vector search includes using table name similarity search without domain filtering; the limited-domain vector search includes using table name similarity search with domain filtering; the hybrid method includes integrating results and querying table details to ensure that complete table information is obtained. The table structure query includes retrieving table information from the database to provide the detailed structure and master-child relationships of the table for generating SQL queries; The SQL generation includes automatically generating SQL based on domain recognition, historical question and answer queries, table name matching, and table structure queries. The formula generation includes automatically generating database formulas based on domain identification, historical question and answer queries, table name matching, and table structure queries, or directly generating non-database formulas.
4. The intelligent formula and SQL generation method based on a large language model according to claim 3, characterized in that, The domain identification is a cross-level domain identification; And / or, the metadata of the pre-registered tool includes name, parameters, and maximum number of calls; And / or, the pre-registered tool further includes an associated extended table; correspondingly, the SQL generation execution plan includes: domain identification → historical question and answer query → table name matching → table structure query → associated extended table → SQL generation → verification; the formula generation execution plan includes: determining whether to query the database → domain identification → historical question and answer query → table name matching → table structure query → associated extended table → formula generation → verification.
5. The intelligent formula and SQL generation method based on a large language model according to claim 1, characterized in that, The databases mentioned in the context of calling pre-registered tools to query databases include: relational databases and vector databases; The relational database stores domain information, historical questions and answers, and table structures. The vector database stores vector data in sets, including domain information, business table names, historical questions and answers, and formulas.
6. The intelligent formula and SQL generation method based on a large language model according to claim 1, characterized in that, The initial validation and adjustment of the SQL or formula includes: Perform syntax validation on the initially generated formulas or SQL statements to check whether they conform to SQL syntax rules or formula operation rules; If the verification fails, it will be automatically corrected and adjusted. The automatic correction and adjustment includes: correcting syntax errors, supplementing missing SQL elements, optimizing statement structure, and correcting formula and function parameters. After the adjustment, the verification will be performed again until the syntax is completely correct.
7. The intelligent formula and SQL generation method based on a large language model according to claim 1, characterized in that, It also includes building a multi-round dialogue mechanism to dynamically optimize SQL / formulas based on subsequent user requests; And / or, after generating the execution plan and before executing it, the process also includes: performing an information assessment to detect whether key information is missing; the key information includes domain ID and table structure; if sufficient, the execution plan is executed; if insufficient, the user is requested to supplement the information; after the tool calls to generate the initial SQL or formula, an assessment step is also included, requesting the user to supplement the information when there is insufficient valid information.
8. A computer device, characterized in that, The device includes: a processor and a memory; The memory is used to store one or more program instructions; The processor is configured to run one or more program instructions to perform the steps of intelligent formula and SQL generation based on a large language model as described in any one of claims 1 to 7.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the steps of the intelligent formula and SQL generation method based on a large language model as described in any one of claims 1 to 7.
10. A computer program product, characterized in that, The computer program product includes computer program instructions that, when executed by a processor, implement the steps of a smart formula and SQL generation method based on a large language model as described in any one of claims 1 to 7.
Citation Information
Patent Citations
Method and device for generating SQL (Structured Query Language) statement based on large language model and terminal equipment
CN117971860A
Method and system for realizing Text2SQL (Structured Query Language)
CN120470020A
Data quality monitoring method and device based on metadata and large language model
CN120631886A
Text-to-SQL model anchor query generation
US12450272B1
System for generating domain-based user-customized database according to question and intent classification, and method for same
WO2025110705A1
Cited By
Anti-illusion NL2SQL method and device based on agent framework, equipment and medium
CN122086923A