SQL generation method and apparatus based on large language model, device and storage medium
By generating and formatting the slot information of SQL database, the problem of insufficient understanding and calculation of large language models in NL2SQL tasks is solved, and SQL statements that meet user needs are generated.
Patent Information
- Application Number
- PCT/CN2024/117443
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- Priority Date
- 2024-02-26
- Filing Date
- 2024-09-06
- Publication Date
- 2025-09-04
AI Technical Summary
When converting natural language to SQL, large language models have problems such as insufficient understanding ability, poor computing ability, redundant information and unclear implicit information, resulting in poor generation results.
By generating data definition language DDL prompt slot information and data sample slot information, combined with user configuration operations and natural language query requests, complete prompt information that is easy to understand by large language models, including parsing, filtering, completing and reconstructing the SQL database, and unified formatting processing.
Improve the understanding ability and calculation accuracy of large language models in NL2SQL tasks, reduce information redundancy, and ensure that the generated SQL statements meet user needs.
Smart Images

Figure CN2024117443_04092025_PF_FP_ABST
Abstract
Description
SQL generation method, device, equipment and storage medium based on large language model
[0001] This application claims priority to the Chinese patent application filed with the China Patent Office on February 26, 2024, with application number 202410206850.0. The entire contents of the above application are incorporated by reference into this application. Technical Field
[0002] The present application relates to the field of software engineering technology, and for example, to a method, apparatus, device, and storage medium for generating SQL based on a large language model. Background Art
[0003] With the release of the fourth-generation Generative Pre-trained Transformer 4 (GPT4), the powerful language understanding and generation capabilities of large language models have quickly garnered significant attention. Natural Language to SQL (NL2SQL) is a technology used in large language models that converts a user's natural language into executable Structured Query Language (SQL). This allows users to query databases and obtain results without mastering SQL syntax, significantly improving the way users interact with databases.
[0004] Although NL2SQL has great development potential based on the capabilities of large language models, the SQL generation effect of large models is highly dependent on the prompt format and content, making it more challenging to understand Chinese natural language questions. Large models are not good at handling complex and precise calculations, so it is difficult to accurately complete related tasks in one go when there is a lot of hidden information. Large models cannot directly obtain implicit business information, and what users consider to be clear language expressions may not be the expression required by the large model, making it impossible to obtain SQL that meets their needs or is effective.
[0005] Summary of the Invention
[0006] According to one aspect of the present application, a method for generating SQL based on a large language model is provided, comprising:
[0007] Generate data definition language (DDL) prompt slot information and data example slot information based on the user's configuration operation on the SQL database, wherein the DDL prompt slot information includes standard format information of the physical table, and the data example slot information includes standard content information of the physical table;
[0008] Receive a natural language query request input by a user, and transcribe the natural language query request to obtain question transcription slot information;
[0009] Obtaining complete prompt information adapted to a large language model according to the question transcription slot information, the DDL prompt slot information, and the data example slot information;
[0010] The complete prompt information is input into the large language model to generate an executable SQL statement that matches the natural language query request.
[0011] According to another aspect of the present application, a SQL generation device based on a large language model is provided, comprising:
[0012] a user configuration module configured to generate data definition language (DDL) prompt slot information and data example slot information based on a user's configuration operation on the SQL database, wherein the DDL prompt slot information includes standard format information of a physical table, and the data example slot information includes standard content information of a physical table;
[0013] A request transcription module is configured to receive a natural language query request input by a user and transcribe the natural language query request to obtain question transcription slot information;
[0014] a complete prompt information acquisition module, configured to acquire complete prompt information adapted to a large language model based on the question transcription slot information, the DDL prompt slot information, and the data example slot information;
[0015] The executable SQL statement generation module is configured to input the complete prompt information into the large language model to generate an executable SQL statement that matches the natural language query request.
[0016] According to another aspect of the present application, a computer device is provided, comprising:
[0017] one or more processors;
[0018] a storage device for storing one or more programs,
[0019] When the one or more programs are executed by the one or more processors, the one or more processors execute the method described in any embodiment of the present application.
[0020] According to another aspect of the present application, a storage medium of computer-executable instructions is provided, on which a computer program is stored. When the program is executed by a processor, the method described in any one of the embodiments of the present application is implemented. BRIEF DESCRIPTION OF THE DRAWINGS
[0021] FIG1 is a flowchart of a method for generating SQL based on a large language model according to an embodiment of the present application;
[0022] FIG2 is a flowchart of a method for generating SQL based on a large language model according to an embodiment of the present application;
[0023] FIG3 is a schematic diagram of the structure of an SQL generation device based on a large language model according to an embodiment of the present application;
[0024] FIG4 is a structural block diagram of a computer device provided according to an embodiment of the present application. DETAILED DESCRIPTION
[0025] The terms "first", "second", etc. in the specification and claims of this application and the above-mentioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequential order. It should be understood that the data used in this way can be interchangeable where appropriate, so that the embodiments of the application described herein can be implemented in an order other than those illustrated or described herein. In addition, "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions, for example, a process, method, device, product or terminal device that includes a series of steps or units is not necessarily limited to those steps or units clearly listed, but may include other steps or units that are not clearly listed or inherent to these processes, methods, products or terminal devices.
[0026] FIG1 is a flowchart of a method for generating SQL based on a large language model, according to an embodiment of the present application. This embodiment is applicable to situations where SQL is generated based on a large language model. The method can be performed by a device for generating SQL based on a large language model. The device can be implemented in the form of hardware and / or software, and can be integrated into a computer device. As shown in FIG1 , the method includes:
[0027] Step S101 : Generate Data Definition Language (DDL) prompt slot information and data example slot information based on the user's configuration operation on the SQL database.
[0028] This implementation uses a structured slot protocol and dynamic composition mechanism to effectively supplement, replace, and reconstruct information in natural language query requests. This unifies all prompt information into a more standardized information structure that is easier for large models to understand. This addresses issues such as insufficient comprehension, weak computational power, redundant information, and unclear implicit information that often plague large models in NL2SQL tasks.
[0029] Optionally, data definition language DDL prompt slot information and data example slot information are generated based on the user's configuration operation on the SQL database, including: performing DDL parsing on the SQL database to obtain database parsing information, and filtering and completing the database parsing information to obtain database standard information; reconstructing the abstract layer DDL based on the database standard information to generate DDL prompt slot information; scanning the fields of a specified type in the physical table in the SQL data to obtain the scan results corresponding to each scanned field; when it is determined according to the scan result that the field is a non-discrete type, obtaining a specified number of field data filtered out from the scan result, and generating data example slot information based on the filtered field data.
[0030] Optionally, DDL parsing is performed on the SQL database to obtain database parsing information, including: parsing the physical table in the SQL database to obtain first constraint information associated with SQL statement generation, wherein the first constraint information includes the table name, engine, and character set; parsing the field information in the SQL database to obtain second constraint information associated with SQL statement generation, wherein the second constraint information includes the field name, field class, and field comment; parsing the index information in the SQL database to obtain indexes of a specified type, wherein the specified type includes all types except unique indexes; and using the first constraint information, the second constraint information, and the index of the specified type as database parsing information.
[0031] Exemplarily, the SQL generation process in this embodiment primarily includes a user configuration phase, a user questioning phase, and an error post-processing phase. During the user configuration phase, data definition language prompt slot information and data example slot information are obtained by parsing and scanning the SQL database. The DDL prompt slot information includes standard format information for physical tables, and the data example slot information includes standard content information for physical tables. Therefore, during the user configuration phase, valid data is obtained by establishing a connection with the target database and performing format and content standardization on the information in the database. Based on the valid data obtained from the database, the natural language query request entered by the user is converted.
[0032] When parsing SQL data, specifically, the original DDL in the SQL database is parsed. Parsing the original data dictionary (DD) includes parsing the physical tables, fields, and indexes, and then performing filtering, completion, and reconstruction operations to obtain DDL prompt slot information. Furthermore, when parsing the original DDL, it includes: parsing according to the physical table selected by the user, retaining the first constraint information that affects the generation of SQL statements, including the table name, engine, and character set, storing the required information in a structured manner, and compressing and storing other information for future use; parsing field information, retaining the second constraint information that affects the generation of SQL, including the field name, field type, whether NULL is allowed, and field comments, etc., storing the required information in a structured manner, and compressing and storing other information for future use; parsing index information, parsing all types of index information except unique indexes, that is, obtaining indexes of specified types, and storing them in a structured manner after the required information is stored. Thus, the first constraint information, the second constraint information, and the index of specified types are used as database parsing information. After obtaining database parsing information, the system also completes missing field definitions in physical tables and non-foreign key associations in relational tables. It also optionally completes synonym dictionary and field enumeration value information to obtain database standard information. The synonym dictionary format is one-to-many, i.e., "actual word: synonym 1, synonym 2." Actual words and synonyms must be unique within the dictionary, while enumeration information is only allowed on fields with normalized numeric and character types. After obtaining database standard information, the abstract layer DDL is reconstructed and normalized to generate DDL hint slot information.
[0033] Reconstructing the abstract layer DDL includes, but is not limited to, normalizing the data types of similar fields; unifying the database mapping dialect; unifying the database character set; correcting SQL statements to a standard format, such as standardizing uppercase and lowercase, delimiters, paragraph indentation, and line breaks; and further improving DDL slot prompt information. For example, segment types are normalized to numeric, character, and time; engines are normalized to MySQL8 (MySQL8 database dialect), SQLSERVER (SQL Server database dialect), DM8 (DaMeng 8 database dialect), Oracle (Oracle database dialect), PG (PostgreSQL database dialect), Hive (Hive database dialect), etc.; character sets are normalized to UTF8 (UTF-8 character set) and GBK (GBK character set); all SQL keywords are capitalized; all delimiters are represented with a single space; all SQL statement indents are represented with two spaces, and all line breaks are converted to symbols before line breaks. This completes the DDL slot prompt information. Of course, this embodiment is merely illustrative and does not limit the specific method of reconstructing the abstract layer DDL.
[0034] For example, this implementation also uses GROUP BY <scan field> ORDER BY COUNT(*) LIMIT <m>The syntax queries all numeric and character fields in a specified table one by one, and obtains the scan results corresponding to each scanned field. If the scan results determine that the actual number of retrieved fields is equal to M, the field is inferred to be of a discrete type, and the inference for this field ends. If the actual number of retrieved fields is less than M, the field is of an enumeration type, and the first N of M fields are taken as examples of available field data. For example, when scanning the "City" field, there are 1000 data entries in the physical table, but only 30 different cities are found under the City field. That is, there are multiple "City" fields with the same content among the 1000 data entries. If M = 100 and N = 10, since 30 is less than 100, it can be determined that the "City" field is of an enumeration type. Since the 30 retrieved data entries are arranged in a certain order, the first 10 data entries are taken as examples of available field data. Of course, this embodiment is merely an example and does not limit the total number of data contained in the physical table or the specific number of data scanned under each field. After filtering out the field data, the field data examples are converted to text format like "<Field 1>:[Example 1a, Example 1b...]". Repeat the above steps for all physical tables in the SQL database until all field data examples are complete. All data examples are then combined to generate data example slot information. Furthermore, since data may change after each scan, the latest results need to be cached in the database for storage.
[0035] Step S102: receiving a natural language query request input by a user, and transcribing the natural language query request to obtain question transcription slot information.
[0036] Optionally, the natural language query request is transcribed to obtain problem transcription slot information, including: obtaining a pre-specified transcription type and the priority and transcription method corresponding to each transcription type, wherein the transcription type includes synonyms, field definitions, enumeration values and data examples, indicators and functions; obtaining the content to be transcribed corresponding to the transcription type in the natural language query request; determining the transcription order according to the priority of the transcription type matched by each content to be transcribed; transcribing each content to be transcribed according to the transcription method of the matched transcription type according to the transcription order to obtain problem transcription slot information.
[0037] For example, after receiving the natural language query request input by the user, whether the expression quality of the question in the natural language query request is high enough will directly affect the generation effect. When the meaning of the question remains unchanged, slight differences in the description method, grammar, modal particles, etc. will make the generation result completely different, resulting in an erroneous result. Therefore, in this embodiment, the obtained natural language query request will be pre-processed, that is, it will be transcribed into question transcription slot information that is easy for a large language model to recognize and process. Among them, when transcribing, the pre-specified writing type and the priority and transcription method corresponding to each transcription type will be obtained. For example, the transcription types in this embodiment include synonyms, field definitions, enumeration values and data examples, indicator functions and time, etc. The specific types of transcription types are not limited in this embodiment. Among them, the transcription method corresponding to synonyms is based on the synonym dictionary provided by the user, which converts the part of the synonyms hit in the question into actual words, thereby strengthening the big model's understanding of abbreviations, abbreviations, and business knowledge; the transcription method corresponding to field definitions is to convert the field definitions hit in the question into the text form of "``", thereby strengthening the big model's understanding of field information; the transcription method corresponding to enumeration values and data examples is to convert the words of the field enumeration values or data examples hit in the question into the text form of "``=<enumeration value content / example content>", thereby strengthening the big model's understanding of the actual data situation; the transcription method corresponding to indicator functions is based on the aggregation indicator function provided by the user, which converts the words hit in the question into the text form of "``=<aggregation indicator function>", thereby strengthening the big model's understanding of functions and mathematical operations; the transcription method corresponding to time is based on the content analysis in the user's question, converting the Chinese description containing time into yyyy-MM-dd The start and end times in the HH:mm:ss format are interval-style, and the minimum unit of the end time is -1, forming a time interval that opens on the left and opens on the right, which strengthens the large model's understanding of time. Of course, this embodiment is only an example and does not limit the transcription method corresponding to each transcription type.
[0038] Among them, this implementation will detect whether there is content to be transcribed corresponding to the above-mentioned transcription types in the natural language query request. Since the priority of each transcription type has been predetermined, the above-mentioned transcribed content will be replaced one by one in order from left to right and from long to short according to the priority of the replacement string length. The replaced part will not be replaced again to avoid ambiguity caused by repeated conversion, and the final transcription result will be used as the problem transcription slot information.
[0039] Step S103: Acquire complete prompt information adapted to the large language model based on the question transcription slot information, the DDL prompt slot information, and the data example slot information.
[0040] Optionally, complete prompt information adapted to the large language model is obtained based on the question transcription slot information, DDL prompt slot information and data example slot information, including: concatenating the question transcription slot information, DDL prompt slot information and data example slot information with line breaks to generate integrated prompt information, wherein the integrated prompt information is in the form of character text; role definition, format and symbol rewriting of the integrated prompt information to generate complete prompt information.
[0041] For example, in this embodiment, after obtaining the question transcription slot information, the obtained question transcription slot information is merged with the DDL prompt slot information related to the SQL database format and the content-related data example slot information. For example, the merging can be performed by splicing line breaks to obtain the format and symbol rewriting of the character text form. The format and symbol rewriting are then processed according to the role definition and symbol rewriting to generate the complete prompt information. Among them, role definition refers to changing the beginning of the integrated prompt information to define the role identity for the large model, such as starting with "You are a SQL builder..."; format rewriting refers to formatting multiple consecutive line breaks in each slot information into a single line break, and using two to three consecutive line breaks between multiple slots in the template to clearly indicate the slot relationship; symbol rewriting refers to deleting various encoded characters such as "?" and "!" and appending a UTF-8 encoded "?" character to clearly indicate the content of the question when the question slot information does not end with "?". Of course, this embodiment is merely illustrative and does not limit the specific method of rewriting the integrated prompt information.
[0042] Step S104: input the complete prompt information into a large language model to generate an executable SQL statement that matches the natural language query request.
[0043] Optionally, the complete prompt information is input into a large language model to generate an executable SQL statement that matches the natural language query request, including: inputting the complete prompt information into the large language model to obtain an initial SQL statement; parsing the initial SQL statement to obtain a syntax tree, and detecting the syntax tree to obtain syntax tree detection information; executing the initial SQL statement to obtain an execution result, and detecting the execution result to obtain execution result detection information; generating an executable SQL statement that matches the natural language query request based on the initial SQL statement, the syntax tree detection information, and the execution result detection information.
[0044] This implementation transcribes the query request entered by the user to obtain more concise transcription slot information, and then merges the transcription slot information with the pre-configured physical table standard format information and standard content information to generate a more standardized and complete prompt information that is easier for large models to understand. This solves the problems of insufficient understanding ability, weak computing power, information redundancy, and unclear implicit information that large models have in NL2SQL tasks.
[0045] Figure 2 is a flow chart of a method for generating SQL statements based on a large language model, provided in an embodiment of the present application. This embodiment, based on the above embodiment, specifically describes how to input complete prompt information into a large language model to generate executable SQL statements that match natural language query requests. As shown in Figure 2, the method includes:
[0046] Step S201 : generating data definition language DDL prompt slot information and data example slot information based on a user's configuration operation on an SQL database.
[0047] Optionally, data definition language DDL prompt slot information and data example slot information are generated based on the user's configuration operation on the SQL database, including: performing DDL parsing on the SQL database to obtain database parsing information, and filtering and completing the database parsing information to obtain database standard information; reconstructing the abstract layer DDL based on the database standard information to generate DDL prompt slot information; scanning the fields of a specified type in the physical table in the SQL data to obtain the scan results corresponding to each scanned field; when it is determined according to the scan result that the field is a non-discrete type, obtaining a specified number of field data filtered out from the scan result, and generating data example slot information based on the filtered field data.
[0048] Optionally, perform DDL parsing on the SQL database to obtain database parsing information, including:
[0049] Parse the physical table in the SQL database to obtain first constraint information associated with SQL statement generation, where the first constraint information includes the table name, engine, and character set; parse the field information in the SQL database to obtain second constraint information associated with SQL statement generation, where the second constraint information includes the field name, field class, and field comment; parse the index information in the SQL database to obtain indexes of a specified type, where the specified type includes all types except unique indexes; and use the first constraint information, the second constraint information, and the indexes of the specified type as database parsing information.
[0050] Step S202: receiving a natural language query request input by a user, and transcribing the natural language query request to obtain question transcription slot information.
[0051] Optionally, the natural language query request is transcribed to obtain problem transcription slot information, including: obtaining a pre-specified transcription type and the priority and transcription method corresponding to each transcription type, wherein the transcription type includes synonyms, field definitions, enumeration values and data examples, indicators and functions; obtaining the content to be transcribed corresponding to the transcription type in the natural language query request; determining the transcription order according to the priority of the transcription type matched by each content to be transcribed; transcribing each content to be transcribed according to the transcription method of the matched transcription type according to the transcription order to obtain problem transcription slot information.
[0052] Step S203 : Acquire complete prompt information adapted to the large language model based on the question transcription slot information, the DDL prompt slot information, and the data example slot information.
[0053] Optionally, complete prompt information adapted to the large language model is obtained based on the question transcription slot information, DDL prompt slot information and data example slot information, including: concatenating the question transcription slot information, DDL prompt slot information and data example slot information with line breaks to generate integrated prompt information, wherein the integrated prompt information is in the form of character text; role definition, format and symbol rewriting of the integrated prompt information to generate complete prompt information.
[0054] Step S204: input the complete prompt information into the large language model to obtain an initial SQL statement.
[0055] Among them, in this embodiment, after obtaining the complete prompt information and inputting it into the large language model, the large language model will output the initial SQL statement, but whether the initial SQL statement is executable at this time cannot be determined, so the initial SQL statement needs to be tested later.
[0056] Step S205: parse the initial SQL statement to obtain a syntax tree, and detect the syntax tree to obtain syntax tree detection information.
[0057] Among them, in this implementation, the initial SQL statement will be parsed to obtain a syntax tree to verify whether it contains task comprehension errors or syntax errors; the syntax tree will be traversed to compare the problem transcription slot information, DDL prompt slot information and data example slot information respectively to verify whether there are entity errors caused by obvious omissions or inconsistencies; the generated SQL statement will be detected to see whether there are potential security risks, including dangerous keywords such as KILL, DELETE, no LIMIT conditions, nested WHERE conditions and repeated calculations of subqueries, etc., and syntax tree detection information will be obtained based on the above detection results.
[0058] Step S206: execute the initial SQL statement to obtain an execution result, and detect the execution result to obtain execution result detection information.
[0059] Among them, this embodiment will also execute the initial SQL statement and detect the execution result, specifically to detect whether there is an error that the SQL does not match the actual library table configuration. Of course, this embodiment is only an example and does not limit the specific content of the execution result detection information.
[0060] Step S207 : generating an executable SQL statement that matches the natural language query request according to the initial SQL statement, the syntax tree detection information, and the execution result detection information.
[0061] Optionally, an executable SQL statement that matches the natural language query request is generated based on the initial SQL statement, syntax tree detection information and execution result detection information, including: determining whether there is error content information in the syntax data detection information and the execution result detection information; if so, generating error correction slot prompt information based on the syntax tree detection information and the execution result detection information, and generating an executable SQL statement based on the complete prompt information and the error correction slot prompt information through a large language model; otherwise, determining that the initial SQL statement has passed the detection, and using the initial SQL statement as an executable SQL statement.
[0062] This implementation determines whether the syntax tree detection information and execution result detection information contain any error content. If so, the error content is integrated into three parts: a fixed prompt question, content details, and SQL error message, to form a correction slot prompt. The correction slot prompt and the complete prompt are then re-input into the large language model for the next round of SQL statement generation to obtain an executable SQL statement. If no error content information is determined, the initial SQL statement, which has no problems, is directly used as the executable SQL statement.
[0063] This implementation transcribes the query request entered by the user to obtain more concise transcription slot information, and then merges the transcription slot information with the pre-configured physical table standard format information and standard content information to generate a more standardized and complete prompt information that is easier for large models to understand. This solves the problems of insufficient understanding ability, weak computing power, information redundancy, and unclear implicit information that large models have in NL2SQL tasks.
[0064] Figure 3 is a schematic diagram of the structure of a large-scale language model-based SQL generation device provided in an embodiment of the present application. As shown in Figure 3, the device includes: a user configuration module 310, a request transcription module 320, a complete prompt information acquisition module 330, and an executable SQL statement generation module 340.
[0065] A user configuration module 310 is configured to generate data definition language (DDL) prompt slot information and data example slot information based on a user's configuration operation on the SQL database, wherein the DDL prompt slot information includes standard format information of a physical table, and the data example slot information includes standard content information of a physical table;
[0066] The request transcription module 320 is configured to receive a natural language query request input by a user and transcribe the natural language query request to obtain question transcription slot information;
[0067] A complete prompt information acquisition module 330 is configured to acquire complete prompt information adapted to the large language model based on the question transcription slot information, the DDL prompt slot information, and the data example slot information;
[0068] The executable SQL statement generation module 340 is configured to input the complete prompt information into a large language model to generate an executable SQL statement that matches the natural language query request.
[0069] Optionally, a user configuration module is configured to parse the DDL of the SQL database to obtain database parsing information, and to filter and complete the database parsing information to obtain database standard information;
[0070] Reconstruct the abstract layer DDL based on database standard information to generate DDL prompt slot information;
[0071] Scan the fields of the specified type in the physical table in the SQL data and obtain the scan results corresponding to each scanned field;
[0072] When the field is determined to be a non-discrete type based on the scan results, a specified number of field data is filtered out from the scan results, and data sample slot information is generated based on the filtered field data.
[0073] Optionally, the user configuration module is further configured to parse a physical table in the SQL database to obtain first constraint information associated with SQL statement generation, wherein the first constraint information includes a table name, an engine, and a character set;
[0074] Parsing the field information in the SQL database to obtain second constraint information associated with SQL statement generation, wherein the second constraint information includes a field name, a field class, and a field comment;
[0075] Parse the index information in the SQL database to obtain indexes of a specified type, where the specified type includes all types except unique indexes;
[0076] The first constraint information, the second constraint information, and the index of the specified type are used as database parsing information.
[0077] Optionally, a request transcription module is configured to obtain pre-specified transcription types and the priority and transcription method corresponding to each transcription type, wherein the transcription types include synonyms, field definitions, enumeration values and data examples, indicator functions, and time;
[0078] Obtain the content to be transcribed corresponding to the transcription type in the natural language query request;
[0079] Determine the transcription order according to the priority of the transcription type matched by each to-be-transcribed content;
[0080] Each content to be transcribed is transcribed according to the transcription method of the matched transcription type and the transcription order to obtain the problem transcription slot information.
[0081] Optionally, a complete prompt information acquisition module is configured to concatenate the problem transcription slot information, the DDL prompt slot information, and the data example slot information with line breaks to generate integrated prompt information, wherein the integrated prompt information is in a character text format;
[0082] The role definition, format and symbol rewriting of the integrated prompt information are performed to generate complete prompt information.
[0083] Optionally, an executable SQL statement generation module is configured to input the complete prompt information into a large language model to obtain an initial SQL statement;
[0084] Parse the initial SQL statement to obtain a syntax tree, and then detect the syntax tree to obtain syntax tree detection information;
[0085] Execute the initial SQL statement to obtain the execution result, and test the execution result to obtain the execution result test information;
[0086] Generate an executable SQL statement that matches the natural language query request based on the initial SQL statement, syntax tree detection information, and execution result detection information.
[0087] Optionally, the executable SQL statement generation module is further configured to determine whether there is error content information in the syntax data detection information and the execution result detection information. If so, it generates error correction slot prompt information based on the syntax tree detection information and the execution result detection information, and generates an executable SQL statement based on the complete prompt information and the error correction slot prompt information through a large language model.
[0088] Otherwise, it is determined that the initial SQL statement has passed the detection, and the initial SQL statement is used as an executable SQL statement.
[0089] An SQL generation device based on a large language model provided in an embodiment of the present application can execute an SQL generation method based on a large language model provided in any embodiment of the present application, and has functional modules and beneficial effects corresponding to the execution method.
[0090] FIG4 shows a block diagram of a computer device 10 that can be used to implement an embodiment of the present application. The computer device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The computer device can also represent various forms of mobile devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present application described and / or required herein.
[0091] The components shown herein, their connections and relationships, and their functions, are examples only, and are not meant to limit implementations of the application described and / or claimed herein.
[0092] As shown in FIG4 , computer device 10 includes at least one processor 11 and memory, such as a read-only memory (ROM) 12 and a random access memory (RAM) 13, communicatively connected to at least one processor 11. The memory stores computer programs that can be executed by the at least one processor. Processor 11 can perform various appropriate actions and processes based on the computer program stored in ROM 12 or loaded from storage unit 18 into RAM 13. RAM 13 can also store various programs and data required for the operation of computer device 10. Processor 11, ROM 12, and RAM 13 are interconnected via bus 14. An input / output (I / O) interface 15 is also connected to bus 14.
[0093] Various components in the computer device 10 are connected to the I / O interface 15, including an input unit 16, such as a keyboard, a mouse, etc.; an output unit 17, such as various types of displays, speakers, etc.; a storage unit 18, such as a magnetic disk, an optical disk, etc.; and a communication unit 19, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 19 allows the computer device 10 to exchange information / data with other servers via a computer network such as the Internet and / or various telecommunication networks.
[0094] The processor 11 can be any general-purpose and / or specialized processing component with processing and computing capabilities. Some examples of the processor 11 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various processors that run machine learning model algorithms, a digital signal processor (DSP), and any appropriate processor, controller, microcontroller, etc. The processor 11 executes the various methods and processes described above, such as the SQL generation method based on a large language model.
[0095] In some embodiments, the SQL generation method based on a large language model can be implemented as a computer program, which is tangibly contained in a computer-readable storage medium, such as the storage unit 18. In some embodiments, part or all of the computer program can be loaded and / or installed on the computer device 10 via the ROM 12 and / or the communication unit 19. When the computer program is loaded into the RAM 13 and executed by the processor 11, one or more steps of the SQL generation method based on a large language model described above can be performed. Alternatively, in other embodiments, the processor 11 can be configured to use the SQL generation method based on a large language model in any other appropriate manner (for example, by means of firmware).
[0096] Various embodiments of the devices and techniques described herein can be implemented in digital electronic circuit devices, integrated circuit devices, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard parts (ASSPs), system on chip devices (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments can include being implemented in one or more computer programs that are executable and / or interpreted on a programmable device that includes at least one programmable processor, which can be a special purpose or general purpose programmable processor that can receive data and instructions from a storage device, at least one input device, and at least one output device, and transmit data and instructions to the storage device, the at least one input device, and the at least one output device.
[0097] Computer programs for implementing the large-scale language model-based SQL generation method of the present application can be written in any combination of one or more programming languages. These computer programs can be provided to a processor of a general-purpose computer, a special-purpose computer, or other programmable overhead crane operation alarm device, so that when executed by the processor, the computer programs implement the functions / operations specified in the flowcharts and / or block diagrams. The computer programs can be executed entirely on the machine, partially on the machine, as a standalone software package, partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0098] In the context of the present application, computer-readable storage medium can be a tangible medium that can contain or store a computer program for use by an instruction execution device, device or computer equipment or used in combination with an instruction execution device, device or computer equipment. Computer-readable storage medium can include but is not limited to electronic, magnetic, optical, electromagnetic, infrared or semiconductor devices, devices or computer equipment, or any suitable combination of the foregoing. Alternatively, computer-readable storage medium can be a machine-readable signal medium. A more specific example of a machine-readable storage medium can include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disc read-only memory (CD-ROM), an optical storage terminal device, a magnetic storage terminal device or any suitable combination of the foregoing.
[0099] To provide interaction with a user, the apparatus and techniques described herein can be implemented on a computer device having: a display device (e.g., a touch screen) for displaying information to the user; and keys through which the user can provide input to the computer device. Other types of apparatuses can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).
[0100] It should be understood that the various forms of the processes shown above can be used to reorder, add, or delete steps. For example, the steps described in this application can be performed in parallel, sequentially, or in a different order, as long as the desired results of the technical solution of this application can be achieved. This is not limited herein.< / m>
Claims
1. A SQL generation method based on a large language model, comprising: Generate data definition language (DDL) prompt slot information and data example slot information based on the user's configuration operation on the SQL database, wherein the DDL prompt slot information includes standard format information of the physical table, and the data example slot information includes standard content information of the physical table; Receive a natural language query request input by a user, and transcribe the natural language query request to obtain question transcription slot information; Acquire complete prompt information adapted to a large language model according to the question transcription slot information, the DDL prompt slot information, and the data example slot information; and The complete prompt information is input into the large language model to generate an executable SQL statement that matches the natural language query request.
2. The method according to claim 1, wherein The generating of data definition language DDL prompt slot information and data example slot information based on the user's configuration operation on the SQL database includes: Performing DDL analysis on the SQL database to obtain database analysis information, and performing filtering and completion operations on the database analysis information to obtain database standard information; Reconstructing the abstract layer DDL based on the database standard information to generate the DDL prompt slot information; Scanning the fields of a specified type in the physical table in the SQL data to obtain scanning results corresponding to each scanned field; When it is determined according to the scanning result that the field is of a non-discrete type, a specified number of field data is filtered out from the scanning result, and the data example slot information is generated according to the filtered field data.
3. The method according to claim 2, wherein: The performing DDL parsing on the SQL database to obtain database parsing information includes: Parsing the physical table in the SQL database to obtain first constraint information associated with SQL statement generation, wherein the first constraint information includes a table name, an engine, and a character set; Parsing the field information in the SQL database to obtain second constraint information associated with SQL statement generation, wherein the second constraint information includes a field name, a field class, and a field comment; Parsing the index information in the SQL database to obtain indexes of a specified type, wherein the specified type includes all types except unique indexes; The first constraint information, the second constraint information and the index of the specified type are used as the database parsing information.
4. The method according to claim 1, wherein The step of transcribing the natural language query request to obtain question transcription slot information includes: Obtaining pre-specified transcription types and the priority and transcription method corresponding to each transcription type, wherein the transcription types include synonyms, field definitions, enumeration values and data examples, indicator functions, and time; Obtaining content to be transcribed corresponding to the transcription type in the natural language query request; Determining a transcription order according to the priority of the transcription type matched by each of the contents to be transcribed; Each of the contents to be transcribed is transcribed according to the transcription method of the matched transcription type and the transcription order to obtain the problematic transcription slot information.
5. The method according to claim 1, wherein The obtaining of complete prompt information adapted to the large language model according to the question transcription slot information, the DDL prompt slot information, and the data example slot information includes: Splicing the problem transcription slot information, the DDL prompt slot information, and the data example slot information with line breaks to generate integrated prompt information, wherein the integrated prompt information is in a character text format; The integrated prompt information is subjected to role definition, format, and symbol rewriting to generate the complete prompt information.
6. The method according to claim 1, wherein Inputting the complete prompt information into the large language model to generate an executable SQL statement that matches the natural language query request includes: Inputting the complete prompt information into the large language model to obtain an initial SQL statement; Parsing the initial SQL statement to obtain a syntax tree, and detecting the syntax tree to obtain syntax tree detection information; Executing the initial SQL statement to obtain an execution result, and detecting the execution result to obtain execution result detection information; An executable SQL statement matching the natural language query request is generated according to the initial SQL statement, the syntax tree detection information, and the execution result detection information.
7. The method according to claim 6, wherein: The generating an executable SQL statement matching the natural language query request according to the initial SQL statement, the syntax tree detection information, and the execution result detection information includes: Determine whether there is error content information in the syntax data detection information and the execution result detection information; if so, generate error correction slot prompt information based on the syntax tree detection information and the execution result detection information, and generate the executable SQL statement based on the complete prompt information and the error correction slot prompt information through the large language model; Otherwise, it is determined that the initial SQL statement passes the detection, and the initial SQL statement is used as the executable SQL statement.
8. A SQL generation device based on a large language model, comprising: a user configuration module configured to generate data definition language (DDL) prompt slot information and data example slot information based on a user's configuration operation on the SQL database, wherein the DDL prompt slot information includes standard format information of a physical table, and the data example slot information includes standard content information of a physical table; A request transcription module is configured to receive a natural language query request input by a user and transcribe the natural language query request to obtain question transcription slot information; a complete prompt information acquisition module, configured to acquire complete prompt information adapted to a large language model based on the question transcription slot information, the DDL prompt slot information, and the data example slot information; and The executable SQL statement generation module is configured to input the complete prompt information into the large language model to generate an executable SQL statement that matches the natural language query request.
9. A computer device, comprising: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 7.
10. A computer executable instruction storage medium having a computer program stored thereon, wherein the computer program, when executed by a processor, implements the method according to any one of claims 1 to 7.
Citation Information
Patent Citations
Natural language processing method and device, equipment and storage medium
CN114186026A
Method for interactively generating data report and computing equipment
CN116756169A
Method and device for converting natural language into database statement, equipment and medium
CN117370378A
Data processing method and device, medium and computing equipment
CN117539893A
SQL (Structured Query Language) generation method, device and equipment based on large language model and storage medium
CN118093622A
Cited By
SQL statement review method, system and equipment based on large model and storage medium
CN120763929A
SQL generation method based on large language model and storage medium
CN120950527A
Multistage security isolation system and method for data library gateway
CN120956518A
Data query method and system for converting natural language into database query language
CN121210495A
Data query method and system for converting natural language into database query language
CN121210495B