Method, system and equipment for generating SQL (Structured Query Language) statement based on large model
Through the SQL statement generation method based on the big model, vectorized processing and full-text search technology, combined with big model inference, the problem of flexibility of the existing SQL generation solution and insufficient multilingual support is solved, and efficient and accurate SQL statements are achieved Generate and multilingual interaction.
Patent Information
- Application Number
- CN202411260716.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-09-10
- Publication Date
- 2024-12-13
AI Technical Summary
The existing SQL generation solution is based on rules, with high user learning and use costs, poor flexibility, difficult to construct complex SQL statements, poor interactivity, limited scope of application, and difficult to meet Chinese context scenarios.
Using the SQL statement generation method based on the big model, the database metadata and metadata description information are processed vectorized, combined with the full-text search library to query matching, generate the optimal SQL statement, and use the big model reasoning to generate SQL statements that meet user needs. Supports multilingual interaction.
Significantly improves the accuracy and efficiency of SQL statement generation, reduces the time cost of manual filtering and adjustment of users, enhances multilingual support capabilities, improves the ease of use and interactivity of queries, and reduces the complexity and error rate of manual operations. .
Smart Images

Figure CN119127913A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of natural language processing, and in particular to a method, system and device for generating SQL statements based on a large model. Background Art
[0002] In recent years, large model technology (LLM) has made breakthrough progress in the field of natural language processing. It has performed well in many tasks and has attracted widespread attention. With the innovation of neural network architecture and the development of dedicated hardware such as GPU, computing power has been greatly improved, making it possible to train large models with millions or even billions of parameters; at the same time, generative AI based on large model technology has also been widely used in tasks such as text generation, image generation, and code generation, bringing new opportunities for AI applications.
[0003] However, in the related technologies, especially in the industry, the commonly used SQL generation solutions are mainly based on rules, which define a series of grammar rules and templates to build SQL statements according to user input. This solution has the following defects: 1) The learning and use costs of users are high, and it is difficult to remember and understand complex SQL generation rules; 2) The flexibility is poor, the scope of application is limited, and it is difficult to build SQL statements other than rules and templates; 3) The interactivity is poor, and the appropriate SQL statements cannot be derived according to the context of user use; 4) It is unable to improve the accuracy of generated SQL statements by itself, and professionals are required to manually optimize SQL rules and templates; 5) Different rules and templates need to be configured for different types of databases, which is inefficient; 6) Most of the database table structures use English words, and there are even many abbreviations, which are difficult to meet the Chinese context scenarios. Summary of the invention
[0004] In view of the above-mentioned shortcomings of the prior art, the present application discloses a method, system and device for generating SQL statements based on a large model, which are used to solve at least one of the above-mentioned technical problems.
[0005] The first aspect of the present application discloses a method for generating SQL statements based on a big model, comprising: vectorizing and documenting metadata and metadata description text information from a database, and storing them in a vector database and a full-text search library; in response to a user's query instruction, querying and matching the vector database and the full-text search library, respectively, sorting the matching results ranked high and low according to the similarity from high to low, and determining an optimal SQL statement list and optimal metadata; based on the current language of the query instruction, generating a prompt word for the current language in combination with the optimal SQL statement list and the optimal metadata; determining a big model of a preset type according to the language type of the prompt word, calling the preset big model to infer the prompt word, and generating an SQL statement, wherein the prompt word of each language type corresponds to at least one big model.
[0006] In certain embodiments of the first aspect of the present application, after generating an SQL statement, it also includes: verifying the SQL statement according to preset rules, and if the verification passes, outputting the SQL statement to the user; wherein the preset rules include security policies and sensitive word filtering; if the verification fails, identifying defects in the SQL statement based on an intelligent SQL knowledge base, and generating SQL optimization suggestions based on a hybrid optimization mode of rules and large models.
[0007] In certain embodiments of the first aspect of the present application, the hybrid optimization mode based on rules and large models is at least one of the following: moving the filtering conditions to the bottom layer for querying, determining whether the SQL statement can be predicate pushed down; determining whether to rewrite the SQL statement, determining whether to optimize the aggregate function, and determining whether to merge multiple SQL statements.
[0008] In certain embodiments of the first aspect of the present application, calling a preset big model to infer the prompt word and generate an SQL statement includes: if it is detected that the prompt word of a certain type of language corresponds to one and only one big model, calling the big model to infer the prompt word and generate the SQL statement accordingly; or, if it is detected that the prompt word of a certain type of language corresponds to one and only one big model, jointly inputting the prompt word, the optimal SQL statement list and the optimal metadata into the big model for inference, and generating the SQL statement accordingly, wherein the big model is a multimodal big model; if it is detected that the prompt word of a certain type of language corresponds to several big models, calling each of the big models to infer the prompt word respectively, and generating multiple SQL statements accordingly, integrating the multiple SQL statements to generate the final SQL statement.
[0009] In certain embodiments of the first aspect of the present application, based on the current language of the query instruction, in combination with the optimal SQL statement list and the optimal metadata, a prompt word in the current language is generated, including: configuring a prompt word template according to template options, the template options including a prompt word template classification, a prompt word template name and a prompt word type; determining the category and name of the prompt word template based on the business scenario to which the query instruction belongs, and based on the current language of the query instruction, filling the prompt word template with user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction, and translating them into prompt words in the current language.
[0010] In certain embodiments of the first aspect of the present application, before the metadata and metadata description text information from the database are respectively vectorized and documented, it also includes: obtaining database configuration information, the database configuration information including database type, database access address, database port and account password; accessing the data source in the database according to the database configuration information, parsing the table structure of the data source based on preset metadata query rules, and determining metadata describing data features and attributes; judging whether the metadata has associated metadata description text information, the metadata description text information including custom description information; if the metadata has associated custom description information, translating the metadata and the custom description information into multilingual description information, and sending them to the intelligent SQL database for storage in a preset format; if the metadata does not have associated custom description information, translating the comments of the table or field, and if there is no comment on the table or field, translating the name of the table or field, and sending the translated multilingual description information to the intelligent SQL database for storage in a preset format.
[0011] In certain embodiments of the first aspect of the present application, the full-text retrieval library includes: determining a local knowledge base based on knowledge base classification, knowledge base name, and local file path; converting document data in the local knowledge base into text information, and segmenting the text information according to a text segmentation strategy to obtain a preset format text, wherein the text segmentation strategy includes at least one of the following: paragraphs, text lines, and words; converting the preset format text into a document vector, and storing it in a vector database that supports a vector search engine to form a full-text retrieval library.
[0012] In certain embodiments of the first aspect of the present application, the current language of the query instruction, combined with the optimal SQL statement list and the optimal metadata, generates prompt words in the current language, including: building a feature description word library, the description word library contains positive description words and negative description words that describe potential risk features; determining a prompt word template based on the description words and prompt word projects in the description word library; determining the category and name of the prompt word template based on the business scenario to which the query instruction belongs, and based on the current language of the query instruction, filling the user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction into the prompt word template, and translating them into prompt words in the current language.
[0013] The second aspect of the present application discloses a SQL statement generation method system based on a big model, comprising: a data processing module, used to vectorize and document metadata and metadata description text information from a database, and store them in a vector database and a full-text search library; an intelligent SQL module, used to respond to a user's query instruction, query and match the vector database and the full-text search library respectively, sort the matching results ranked high and low according to the similarity from high to low, and determine the optimal SQL statement list and the optimal metadata; a prompt word determination module, based on the current language of the query instruction, combined with the optimal SQL statement list and the optimal metadata to generate prompt words for the current language; an SQL generation module, used to determine a big model of a preset type according to the language type of the prompt word, call the preset big model to infer the prompt word, and generate an SQL statement, wherein the prompt word of each language corresponds to at least one big model.
[0014] The third aspect of the present application discloses an electronic device, comprising: a processor and a memory; the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device executes the above method.
[0015] The beneficial effects of the present application are as follows: by vectorizing and documenting the metadata of the database and its descriptive text information, in-depth analysis and structuring of the data are achieved. On the one hand, the potential relationship between the data is captured through vectorization processing, which is convenient for subsequent similarity calculation and rapid retrieval; on the other hand, the document processing retains the contextual information of the original text, which is convenient for full-text retrieval; the storage structure of the data is optimized, providing a solid foundation for subsequent queries. By using the vector database for similarity-based vector query matching and using the full-text retrieval library for precise or fuzzy search of text content, not only the query coverage is expanded, but also the results can be sorted according to similarity to screen out those related to the user's query. The matching results within the preset ranking that are closest to the intention significantly improve the accuracy and efficiency of the query; based on the query results, it can automatically determine the optimal SQL statement list and metadata, and recommend the SQL statement and metadata that best suits the current query needs to the user, reducing the time cost of manual screening and adjustment by the user, and can identify the current language of the user's query instructions, and generate prompt words in the corresponding language accordingly, greatly enhancing the multi-language support capability, so that users with different language backgrounds can have a smooth interactive experience; at the same time, by calling the preset large model to infer the prompt words, it can automatically generate SQL statements that meet user needs, ensuring that the generated SQL statements are both accurate and in line with language habits.
[0016] In addition, this application not only reduces the complexity and error rate of manual operations, but also improves query response speed and user satisfaction. By continuously optimizing the training data and algorithms of the big model, the intelligence level of this application will continue to improve, bringing users a more convenient and efficient database query experience. Based on the big model and generative AI technology, the accuracy of the generated SQL statements is improved, the usage threshold for non-technical personnel is lowered, and the efficiency of SQL statement queries is improved; in addition, by understanding the user's natural language input and remembering the user's usage context, the ease of use and interactivity of SQL statement queries are improved. Through the use of multiple languages combined with hybrid big models, users of different languages can use it conveniently. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] Figure 1 It is an application environment diagram of a method system for generating SQL statements based on a large model in an embodiment of the present application;
[0018] Figure 2 It is a flowchart of a method for generating SQL statements based on a large model in an embodiment of the present application;
[0019] Figure 3 It is an overall flow chart of a method for generating SQL statements based on a large model in an embodiment of the present application;
[0020] Figure 4It is a vector storage flow chart in a SQL statement generation method based on a large model in an embodiment of the present application;
[0021] Figure 5 It is a flow chart of an intelligent SQL knowledge base in a SQL statement generation method based on a large model in an embodiment of the present application;
[0022] Figure 6 It is a flow chart of SQL statement generation in a method for generating SQL statements based on a large model in an embodiment of the present application;
[0023] Figure 7 It is a structural diagram of a SQL statement generation method system based on a large model in an embodiment of the present application;
[0024] Figure 8 It is a structural schematic diagram of an electronic device in an embodiment of the present application. DETAILED DESCRIPTION
[0025] The following describes the implementation methods of the present application through specific examples, and those skilled in the art can easily understand other advantages and effects of the present application from the contents disclosed in this specification. The present application can also be implemented or applied through other different specific implementation methods, and the details in this specification can also be modified or changed in various ways based on different viewpoints and applications without departing from the spirit of the present application. It should be noted that the following embodiments and sub-samples in the embodiments can be combined with each other without conflict.
[0026] It should be noted that the illustrations provided in the following embodiments are only schematic illustrations of the basic concept of the present application, and thus the drawings only show components related to the present application rather than being drawn according to the number, shape and size of components in actual implementation. In actual implementation, the type, quantity and proportion of each component may be changed at will, and the component layout may also be more complicated.
[0027] In the following description, a large number of details are discussed to provide a more thorough explanation of the embodiments of the present application. However, it is obvious to those skilled in the art that the embodiments of the present application can be implemented without these specific details. In other embodiments, well-known structures and devices are shown in the form of block diagrams rather than in detail to avoid making the embodiments of the present application difficult to understand.
[0028] The terms "first", "second", etc. in the specification and claims of the embodiments of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the terms used in this way can be interchanged where appropriate, so as to describe the embodiments of the embodiments of the present disclosure described herein. In addition, the terms "including" and "having" and any variations thereof are intended to cover non-exclusive inclusions.
[0029] Unless otherwise stated, the term "plurality" means two or more.
[0030] In the embodiment of the present disclosure, the character " / " indicates that the preceding and following objects are in an "or" relationship. For example, A / B indicates: A or B.
[0031] The term "and / or" is a description of the association relationship between objects, indicating that three relationships can exist. For example, A and / or B means: A or B, or, A and B.
[0032] Combination Figure 1 As shown, an implementation environment diagram of a SQL statement generation method system based on a large model provided by an embodiment of the present disclosure includes a user end and a server end, wherein the user end and the server end are connected to a network and communicate with each other. In this application, the user end is the front end and the server end is the back end.
[0033] It should be understood that the SQL statement generation method system based on the large model is applied to the terminal. In some embodiments, the terminal can be at least one of a computer device, a desktop device, a smart phone, and a tablet device; the server can be configured as an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, and big data and artificial intelligence platforms; the software can be an application program based on the SQL statement generation method of the large model, etc., but is not limited to the above forms.
[0034] Combination Figure 2 As shown, the embodiment of the present disclosure provides a flow chart of a method for generating SQL statements based on a large model, including:
[0035] Step S201, vectorizing and documenting the metadata and metadata description text information from the database, and storing them in the vector database and the full-text search library;
[0036] Metadata is a type of data that describes data, including structural, descriptive, management, and usability metadata. Structural metadata mainly describes database table schema, field relationships, etc., while descriptive metadata mainly describes field names, field types, and field lengths.
[0037] Prompt words are textual content entered by users when interacting with large model systems. Prompt words are the key for humans to communicate with AI systems or complete tasks. Good prompt words can significantly affect the output results of AI systems.
[0038] Big models are an important type of technology that has emerged in the field of machine learning and natural language processing in recent years. They are large-scale neural network models trained based on deep learning, with the characteristics of huge parameter scale, strong versatility, multimodal capabilities, and continuous learning capabilities. They play a key role in many business scenarios such as natural language processing, dialogue systems, creative assistance, and AI programming.
[0039] For example, by extracting metadata (such as database table name, column name, data type, etc.) and metadata description text (such as field description, table description, etc.) in the database, using natural language processing (NLP) technology, such as word embedding (WordEmbedding) model (such as Word2Vec, BERT, etc.), the text information is converted into vectors of fixed dimension, and these vectors are stored in vector databases such as Faiss, Annoy, etc. These databases support efficient vector search and comparison.
[0040] For another example, metadata description text is stored in the form of documents, each document contains complete description information, and the indexing function of a full-text search engine (such as Elasticsearch, Solr) is used to index the documents for fast retrieval. The indexed documents are stored in the full-text search library for subsequent queries.
[0041] Step S202, in response to the user's query instruction, query and match the vector database and the full-text search database respectively, sort the matching results ranked top by a preset ranking according to the similarity from high to low, and determine the optimal SQL statement list and optimal metadata;
[0042] It should be understood that the query instruction input by the user is parsed, keywords or query conditions are extracted, and in the vector database, the vector representation of the query instruction is used to calculate the similarity (such as cosine similarity) with the vector in the database to find the vector with the highest similarity; in the full-text search library, the keywords of the query instruction are used to perform a full-text search to obtain a list of matching documents. The results are sorted according to the similarity or matching degree, and a preset number of results with high rankings are selected as candidates. The candidate results are analyzed to determine the SQL statement list and metadata that best matches the query intent, and the optimal SQL statement list and metadata are selected by evaluating the applicability and efficiency of the SQL statement.
[0043] Step S203, based on the current language of the query instruction, combining the optimal SQL statement list and the optimal metadata to generate a prompt word for the current language;
[0044] Specifically, the language of the query instruction is determined using language recognition technology (such as the language detection model in NLP). Based on the optimal SQL statement list and the optimal metadata content, combined with the language of the query instruction, easy-to-understand prompt words are generated. The prompt words should be concise and clear, and can guide users to understand or modify the SQL statement.
[0045] Step S204, determining a preset type of big model according to the language type of the prompt word, calling the preset big model to infer the prompt word, and generating an SQL statement, wherein the prompt word of each language type corresponds to at least one big model.
[0046] Specifically, according to the language of the prompt word, a large model of the corresponding language is selected from the preset large model set. These large models may be trained based on different NLP frameworks (such as Transformer) and are specifically used to generate or modify SQL statements. The prompt word is used as input and the selected large model is called for reasoning. The large model generates a grammatically and logically consistent SQL statement based on the content and context of the prompt word. If there are multiple possible SQL statements, the statement that best meets the user's intention or is most efficient can be selected as the output.
[0047] In this embodiment, by vectorizing and documenting the metadata of the database and its descriptive text information, deep analysis and structuring of the data are achieved. On the one hand, the potential relationship between the data is captured by vectorization processing, which is convenient for subsequent similarity calculation and fast retrieval; on the other hand, the document processing retains the context information of the original text, which is convenient for full-text retrieval; the storage structure of the data is optimized, providing a solid foundation for subsequent queries. By using the vector database for vector query matching based on similarity and using the full-text retrieval library for precise or fuzzy search of text content, not only the query coverage is expanded, but also the results can be sorted according to similarity to screen out those that match the user's query intent. It can automatically determine the optimal SQL statement list and metadata based on the query results, and recommend the most suitable SQL statement and metadata for the current query needs to users, reducing the time cost of manual screening and adjustment by users. It can also identify the current language of the user's query instructions and generate prompt words in the corresponding language accordingly, greatly enhancing the multi-language support capabilities, so that users with different language backgrounds can get a smooth interactive experience. At the same time, by calling the preset large model to infer the prompt words, it can automatically generate SQL statements that meet user needs, ensuring that the generated SQL statements are both accurate and in line with language habits.
[0048] In addition, this application not only reduces the complexity and error rate of manual operations, but also improves query response speed and user satisfaction. By continuously optimizing the training data and algorithms of the big model, the intelligence level of this application will continue to improve, bringing users a more convenient and efficient database query experience. Based on the big model and generative AI technology, the accuracy of the generated SQL statements is improved, the usage threshold for non-technical personnel is lowered, and the efficiency of SQL statement queries is improved; in addition, by understanding the user's natural language input and remembering the user's usage context, the ease of use and interactivity of SQL statement queries are improved. Through the use of multiple languages combined with hybrid big models, users of different languages can use it conveniently.
[0049] Optionally, in some embodiments, after generating the SQL statement, the process further includes:
[0050] Verify the SQL statement according to preset rules, and if the verification passes, output the SQL statement to the user; wherein the preset rules include security policies and sensitive word filtering;
[0051] If the verification fails, the defects of the SQL statement are identified based on the intelligent SQL knowledge base, and SQL optimization suggestions are generated based on a hybrid optimization mode of rules and large models.
[0052] It should be noted that the SQL statements entered by the user are parsed and decomposed into recognizable elements, such as table names, column names, conditional expressions, etc., through lexical analysis and syntax analysis. The parsed SQL statements are verified according to the preset rule sets, which include but are not limited to: security policies, checking whether there are structures or contents in the SQL statements that may cause security issues, such as SQL statement injection vulnerabilities (such as using unvalidated input directly spliced into the SQL statement), permission violations (trying to access unauthorized data tables or columns), etc. Sensitive word filtering: Check whether the SQL statement contains predefined sensitive words or phrases. These sensitive words may involve personal privacy, commercial secrets or other sensitive information. Once sensitive words are found, SQL optimization suggestions are generated based on the hybrid optimization mode of rules and large models.
[0053] See also Figure 3 , is a vector storage flow chart in a method for generating SQL statements based on a large model in an embodiment of the present application, which is described in detail as follows:
[0054] 1) The database metadata module automatically collects database metadata.
[0055] 2) Users maintain additional metadata description information.
[0056] 3) Users perform natural language queries
[0057] 4) The SQL generator module requests the intelligent SQL knowledge base to obtain the optimal SQL statement list and metadata
[0058] 5) Intelligent SQL knowledge base returns a list of candidate SQL statements and metadata
[0059] 6) SQL generator module generates prompt words in the corresponding language according to the user's language
[0060] 7) Submit the prompt word to the hybrid large model query
[0061] 8) The big model integrates the query according to the prompt word and returns it to Songhu
[0062] 9) The big model rewrites the integrated and processed SQL statements into the intelligent SQL knowledge base
[0063] It should be noted that the overall architecture of the SQL statement generation system based on the big model is as follows:
[0064] Database metadata management module: manages different types of databases and automatically obtains metadata information such as all table structures in different types of databases through specific rules. In addition, it allows users to add additional multilingual description information to metadata, avoiding the situation where the database table structure is usually in English words and abbreviations, which leads to the inability to accurately identify user logic in a multilingual environment.
[0065] Intelligent SQL knowledge base module: used to store and retrieve SQL statements and historical query records for various business scenarios. In order to improve the accuracy of the SQL knowledge base and facilitate historical record queries, this module adopts a hybrid mode of vector database plus full-text retrieval. First, the data stored in the data metadata management module is vectorized and stored in the vector database. Secondly, business scenario-based SQL statements in multilingual contexts are entered and stored in the vector database after vectorization. Then, the query records and results of the user's historical scenario-based SQL statements are documented and stored in the full-text retrieval database. Finally, for all SQL statements stored in the database, SQL statement defects can be automatically checked, and SQL optimization suggestions and cases can be provided.
[0066] SQL generator module: receives multilingual query requests from users and generates SQL statements. This module processes the user's natural language query and hands it over to the intelligent SQL knowledge base module. After processing, the intelligent SQL knowledge base module returns multiple candidate SQL statements in descending order of similarity. After receiving the candidate SQL statements, it generates prompt words in a specific language according to the rules based on the user's natural language type, and hands them over to the designated large model for integration processing. Since different large models are trained in different language environments, this module innovatively adopts the form of mixed processing of multiple large models, and hands prompt words in different languages to the corresponding large models for processing, thereby improving the accuracy in multilingual environments.
[0067] In this embodiment, the SQL statement generation method based on the large model has the following technical effects:
[0068] First, it improves efficiency by automatically converting user queries expressed in natural language into corresponding SQL statements.
[0069] Second, it lowers the threshold, allowing non-technical personnel to easily query data without professional SQL syntax knowledge;
[0070] Third, the scope of application has been expanded. Through unified SQL statements, data queries can be performed on different types of databases, solving the SQL syntax differences between different databases.
[0071] Fourth, it reduces human errors and generates correct SQL statements based on user semantic logic, avoiding syntax errors that may occur during manual writing;
[0072] Fifth, accuracy is improved by accumulating training data to generate more accurate SQL statements.
[0073] Sixth, the ability to interact in multiple language contexts such as Chinese has been improved.
[0074] Optionally, in some embodiments, before the metadata and metadata description text information from the database are respectively vectorized and documented, the following steps are further included:
[0075] Obtain database configuration information, including database type, database access address, database port, and account password;
[0076] Accessing a data source in a database according to the database configuration information, parsing a table structure of the data source based on a preset metadata query rule, and determining metadata describing data features and attributes;
[0077] Determine whether the metadata has associated metadata description text information, where the metadata description text information includes custom description information;
[0078] If the metadata is associated with the custom description information, the metadata and the custom description information are translated into multilingual description information, and sent to the intelligent SQL database for storage in a preset format;
[0079] If the metadata does not have the associated custom description information, the annotation of the table or field is translated; if the annotation of the table or field does not exist, the name of the table or field is translated, and the translated multilingual description information is sent to the intelligent SQL database for storage in a preset format.
[0080] For example, see Figure 4 , is a flow chart of an intelligent SQL knowledge base in a SQL statement generation method based on a large model in an embodiment of the present application, which is described in detail as follows:
[0081] Enter database type, database connection IP, port, account password and other database information;
[0082] Automatically parse metadata such as table structures in the database. The specific collection strategy is: First, maintain metadata query rules for different types of databases (such as MySQL, PostgreSQL, etc.) in this module. Then, execute the corresponding metadata query rules according to the type of database that actually needs to be connected (such as MySQL, PostgreSQL, etc.). Then try to automatically translate the metadata into multiple languages, such as: the customer table is automatically translated into "customer table", and the name column is automatically translated into "name list"; if the table or field has annotation information, the annotation is translated, such as: the annotation "customer info" is translated into "customer information"; finally, the metadata information and the automatically translated information are structured and stored in the database. The translation strategy of metadata is: First, construct a prompt word specifically for translation, such as: please translate "customer" into Chinese, English, and French; only give the translation results, and separate different languages with line breaks. Then, send the prompt word to the big model. Then, extract the response of the big model and store it in the database after associating it with the original metadata. The specific frequency of metadata collection can be configured by yourself, such as every 5 minutes, every 1 hour, every early morning, etc.
[0083] In this embodiment, additional custom description information is added to the metadata to help the vector database and the large model better understand the role of metadata, such as the custom description of the customer table is "customer information table". In addition, for some tables or fields whose English abbreviations and names cannot be translated and annotation information, users can also manually maintain their custom description information, such as the area_sls_info table can add a "regional sales information column" description.
[0084] The described metadata is handed over to the intelligent SQL knowledge base module for processing. Based on the metadata defined by the user, additional multilingual description information will be automatically added. The specific strategy is: first, determine whether a specific metadata has a "user-defined description"; if there is a custom description, translate the description into multiple languages supported by the module, such as "user information table" is translated into English "user information table" and French "tableau d'informationsutilisateur"; then, if there is no user-defined description information, translate the comments of the table or field; finally, if the table or field has no comment information, try to translate the table or field name. Submit the automatically translated metadata information and data table DDL to the intelligent SQL knowledge base in a specific format, such as: user information table, table name is user; contains name, age, sex fields, the corresponding meanings of the fields are name, age, gender; table DDL is create table user(xxxx).
[0085] Optionally, in some embodiments, the hybrid optimization mode based on rules and large models is at least one of the following:
[0086] Move the filtering conditions to the bottom layer for query to determine whether the SQL statement can be pushed down;
[0087] Determine whether to rewrite the SQL statement, determine whether to optimize the aggregate function, and determine whether to merge multiple SQL statements.
[0088] See also Figure 5 , is a flow chart of an intelligent SQL knowledge base in a SQL statement generation method based on a large model in an embodiment of the present application, which is described in detail as follows:
[0089] The database metadata and metadata natural language description information are vectorized and stored in the vector database;
[0090] The database metadata and metadata natural language description information are documented and stored in the full-text search library;
[0091] According to the user's natural language query, the optimal SQL statement list and metadata are handed over to the SQL generator for processing; the optimal means sorting the results returned by the vector database and the full-text retrieval library from high to low based on the similarity, and taking the top 10 data.
[0092] Receive the SQL statements generated by the SQL generator, re-vectorize and re-process the documents before storing them in the database;
[0093] Users can manually maintain SQL statements to improve the quality of SQL statements in the SQL knowledge base. The intelligent SQL knowledge base automatically identifies the defects of SQL statements and gives modification and optimization suggestions. The specific optimization method is a hybrid optimization mode based on rules and big models. The specific strategy is: first, check whether the SQL statement can push down the predicate, such as moving the filter condition to the bottom-level subquery; then, determine whether the SQL statement can rewrite the subquery; then, determine whether the aggregate function can be optimized, such as: sum(a)+sum(b) is optimized to sum(a+b); then, determine whether the union all statement can be merged into a single query. Finally, submit the SQL statement rewritten by the previous strategy to the big model, so that the big model can give better SQL optimization suggestions. According to the optimization suggestions, the user re-enters the SQL statement into the intelligent SQL knowledge base.
[0094] Through the above methods, based on large models and generative AI technology, the accuracy of generated SQL statements can be improved, the threshold for use by non-technical personnel can be lowered, and the efficiency of SQL statement queries can be improved. In addition, by understanding the user's natural language input and remembering the user's usage context, the ease of use and interactivity of SQL statement queries are improved. This solution also supports multiple languages, and combined with the hybrid large model solution, users of different languages can use it conveniently.
[0095] Optionally, in some embodiments, calling a preset large model to infer the prompt word and generate an SQL statement includes:
[0096] If it is detected that the prompt word of a certain language corresponds to one and only one big model, the big model is called to infer the prompt word, and the SQL statement is generated accordingly; or, if it is detected that the prompt word of a certain language corresponds to one and only one big model, the prompt word, the optimal SQL statement list and the optimal metadata are jointly input into the big model for inference, and the SQL statement is generated accordingly, wherein the big model is a multimodal big model;
[0097] If it is detected that the prompt word of a certain language corresponds to several large models, each of the large models is called to infer the prompt word, and a plurality of SQL statements are generated accordingly. The plurality of SQL statements are integrated and processed to generate a final SQL statement.
[0098] Among them, a multimodal large model refers to a deep learning model that can process and fuse data from different modalities (such as text, images, audio, etc.). In this context, it may mainly process text (prompt words) and structured data (such as metadata) to generate SQL statements.
[0099] For example, the ability to identify and classify the language of the input natural language (NL) prompt word is first used, and the language of the prompt word is identified through natural language processing (NLP) technology. Then, the preset large model library is queried to find out whether there is and only one large model corresponding to the prompt word based on the language and corresponding content field. Here, "large model" refers to a deep learning model that has been trained to handle specific tasks (such as SQL statement generation), which can be based on the Transformer structure.
[0100] If the large model directly generates SQL statements for the prompt words, the prompt words are directly input into the large model, and the corresponding SQL statements are generated through the internal logic of the model and the training data.
[0101] Alternatively, if the big model is designed to require additional information (such as the optimal SQL statement list and optimal metadata) to optimize the generated results, the prompt words, the optimal SQL statement list (that is, a series of historical or preset high-quality SQL statement lists) and the optimal metadata (such as database structure, table field information, etc.) are jointly input into the big model. The big model uses this additional information to generate more accurate SQL statements through multimodal fusion technology (the ability to process different types of data).
[0102] For example, when it is detected that there are several large models corresponding to a prompt word in a certain language, it means that there are multiple SQL generation models that may be applicable to the prompt word. To improve efficiency, these large models can be called at the same time to reason about the same prompt word. Each model generates its own SQL statement based on its specific training data and optimization goals.
[0103] In this embodiment, first, the SQL statements generated by each large model are evaluated, including but not limited to grammatical correctness, execution efficiency, expected result accuracy, etc. Based on the evaluation results, an optimal SQL statement can be selected as the final result; or, if multiple statements have their own advantages, a comprehensive and more comprehensive SQL statement can be generated through logical merging, result merging, etc.
[0104] Optionally, in some embodiments, based on the current language of the query instruction, the prompt words for the current language are generated in combination with the optimal SQL statement list and the optimal metadata, including:
[0105] Configuring the prompt word template according to template options, wherein the template options include prompt word template classification, prompt word template name, and prompt word type;
[0106] Based on the business scenario to which the query instruction belongs, the category and name of the prompt word template are determined; based on the current language of the query instruction, the user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction are filled into the prompt word template and translated into prompt words in the current language.
[0107] It should be understood that by defining a configuration interface or data structure containing multiple template options, the template options include but are not limited to "prompt word template category", "prompt word template name" and "prompt word type". According to actual needs, specific template option instances are created for different business scenarios and languages through interface operations or programming. For example, a template classified as "user query" can be created, named "product information query", and its language is specified as Chinese. Based on the above template options, constructing the specific content of the prompt word template usually involves writing or designing a structured or semi-structured text format for subsequent filling in user question information and database related information.
[0108] For example, after receiving the query command input by the user, it is first parsed to identify the business scenario to which the command belongs. The parsed business scenario is implemented through pattern matching, keyword recognition or natural language processing technology, and the matching template category and name are found in the predefined template options.
[0109] In this embodiment, user question information (such as the user's specific query requirements), the optimal SQL statement list (i.e., a high-quality SQL statement list derived from historical data or an optimization algorithm), and text description information of the optimal metadata (such as a text description of a database table structure, field names, etc.) are extracted from the query instruction; the extracted information is filled in according to the predefined prompt word template format, and the information is converted into a specific format or structure required by the template. If the language of the query instruction is different from the default language of the prompt word template, translation processing is required, which is achieved by calling a translation API, using translation software, or a built-in translation module. After the translation is completed, the prompt word in the current language is obtained.
[0110] Optionally, in some embodiments, the full-text search library includes:
[0111] Determine the local knowledge base based on the knowledge base classification, knowledge base name, and local file path;
[0112] Converting the document data in the local knowledge base into text information, and segmenting the text information according to a text segmentation strategy to obtain a preset format text, wherein the text segmentation strategy includes at least one of the following: paragraph, text line, word;
[0113] The preset format text is converted into a document vector and stored in a vector database that supports a vector search engine to form a full-text retrieval library.
[0114] It should be noted that inputs are received from users or configuration files, including knowledge base classification (such as technical field, project name, etc.), knowledge base name (unique identifier), and local file path (pointing to the directory or file where the knowledge base documents are stored). Based on the provided local file path, the file system operation API (such as Python's os module or Java's java.io.File class) is used to locate and verify whether the file or directory under the path exists and is accessible. Once the corresponding file or directory is found, it will be further verified whether its content conforms to the expected knowledge base structure (such as file type, naming rules, etc.) and whether it contains valid knowledge base classification and name information. Determine the knowledge base: If all verifications pass, the system confirms the content under the path as the local knowledge base and prepares for subsequent processing.
[0115] For another example, use an appropriate file reading method (such as Python's open() function or Java's FileReader class) to read each document in the local knowledge base as binary data or a string. If the document is not in a plain text format (such as PDF, Word, etc.), use the corresponding parsing library (such as Python's PyPDF2, python-docx, etc.) to convert it into plain text information. According to the preset text segmentation strategy (paragraph, text line, word), use regular expressions, string segmentation functions, or natural language processing (NLP) libraries (such as Python's nltk or spaCy) to segment the text. Each strategy may require different processing logic. For example, paragraph segmentation may be based on line breaks or specific tags, and word segmentation depends on language-specific word segmentation algorithms. The segmented text is organized according to a preset format (such as JSON, XML, etc.) for subsequent processing.
[0116] In this embodiment, text embedding technology (such as Word2Vec, BERT, etc.) is used to convert each document or document fragment in a preset format text into a high-dimensional vector. These vectors can capture the semantic information in the text, which is convenient for subsequent calculation and retrieval. According to the system requirements, a suitable vector database is selected, which needs to support efficient vector search and retrieval operations, such as Faiss, Milvus, VectorDB, etc. The converted document vector is stored in the selected vector database. This usually involves operations such as database connection, index creation, and data insertion. Through index and query optimization, ensure that the vector database can support fast full-text retrieval. This may require configuring the query parameters of the database, optimizing the index structure, etc. JSON, XML, etc.) for subsequent processing.
[0117] Optionally, in some embodiments, the generating of prompt words for the current language based on the current language of the query instruction in combination with the optimal SQL statement list and the optimal metadata includes:
[0118] Constructing a feature description word library, wherein the description word library includes positive description words and negative description words describing potential risk features;
[0119] Determine the prompt word template according to the description words and prompt word projects in the description word library;
[0120] Based on the business scenario to which the query instruction belongs, the category and name of the prompt word template are determined; based on the current language of the query instruction, the user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction are filled into the prompt word template and translated into prompt words in the current language.
[0121] Specifically, first, collect vocabulary related to potential risks from multiple channels (such as historical documents, industry reports, expert interviews, etc.). The vocabulary should cover a wide range of risk types, such as technical risks, market risks, legal risks, etc. Classify the collected vocabulary and distinguish between positive descriptors (such as "high return" corresponding to "high risk") and negative descriptors (such as "safe" and "stable") that describe the same risk characteristics. Label the polarity of each word (positive or negative) and the type of risk it belongs to, and integrate the classified and labeled vocabulary into a database to form a feature description vocabulary, which should support efficient retrieval and query functions for use in subsequent steps.
[0122] For example, we can deeply understand the words in the description word library and their classification, polarity and other information, and design one or more prompt word templates based on the content of the description word library. The template should be able to flexibly embed the words in the description word library to generate prompt words for different risk scenarios; prompt word engineering is a natural language processing technology used to optimize the quality and effect of prompt words. In this step, the technology can be used to tune the templates designed initially to ensure that the generated prompt words are both accurate and easy to understand.
[0123] In this embodiment, the query instruction is parsed to determine the business scenario to which it belongs (such as financial risk assessment, supply chain management, etc.). According to the business scenario, a suitable template category and name are selected from the preset prompt word template library, and user question information, the optimal SQL statement list (i.e., a set of SQL statements used to query the database, which are optimized to provide the best query results) and the text description information of the optimal metadata (such as database table structure, field description, etc.) are extracted from the query instruction. This information is filled in according to the structure of the template, and according to the current language of the query instruction, the filled template is translated into the target language using machine translation technology or manual translation; finally, prompt words that meet the current business scenario and language are generated to guide subsequent operations or remind users to pay attention to potential risks.
[0124] See also Figure 6 , is a flow chart of SQL statement generation in a method for generating SQL statements based on a large model in an embodiment of the present application, which is described in detail as follows:
[0125] 1) Receive user natural language query requests;
[0126] 2) After determining the language, request the intelligent SQL knowledge base to return the optimal SQL statement list and the corresponding language metadata description information;
[0127] 3) Generate prompt words in a specific language based on the language, combined with the optimal SQL statement list and metadata description information. This module has built-in prompt word templates for common languages, such as the Chinese prompt word template: "You are a SQL expert, provide SQL for the user question. You only need to reply with SQL code and no other explanations are required. The user question information is: {0}, the relevant metadata description information is: {1}, and the SQL statements with high similarity are: {2}"; for example, the English prompt word template is: "You are a SQL expert, provide SQL for the user question. You will only respond with SQL code and not with any explanations. The user question is: {0}. The metadata descriptions are: {1}. The similar sqls: {2}". Fill in the form according to the template above. For example, in the Chinese context, the prompt words after filling in are: "You are a SQL expert, please provide SQL for the user question. You only need to reply with SQL code and no other explanations are required. The user's question is: query the average salary level of each region. The relevant metadata description information is: Area salary table, table name is area_salary, including columns area_name and salary fields, representing the region name and salary respectively. The SQL statement with high similarity is: select avg(salary)fromsalary_table" In the English context, the prompt words after filling in are: "You are a SQL expert, provide SQL for the user question. You will only respond with SQL code and not with any explanations. The metadata descriptions are: Area salary table, table name is area_salary, it includes area_name, salary columns, these columns represent areaname, salary. The similar sqls: select avg(salary)from salary_table";
[0128] 4) Calling a relatively good large model in a specific language. This step is to automatically select the optimal large model according to the language. The specific strategy is as follows: First, the module has multiple large models built in, such as ChatGLM3, GPT3.5, LLama 2, etc. Then, select a specific model according to the language. For example, in the Chinese context, ChatGLM3 is preferred; in the English context, GPT3.5 is preferred. Finally, the prompt words generated in the previous step are submitted to the large model.
[0129] 5) Parse the large model response and extract SQL statements. For example, the SQL generated according to step 3 is: select avg(salary) from area_salary group by area_name;
[0130] 6) Syntax check the extracted SQL statements and return them to the user;
[0131] 7) Based on the accuracy of the SQL statements fed back by the user, decide whether to write the SQL statements back to the intelligent SQL knowledge base.
[0132] In this embodiment, based on the big model and generative AI technology, the accuracy of the generated SQL statements can be improved, the threshold for non-technical personnel to use can be lowered, and the efficiency of SQL statement queries can be improved. In addition, the present invention can understand the user's natural language input and remember the user's usage context, thereby improving the ease of use and interactivity of SQL statement queries. This solution also supports multiple languages, and combined with the solution of the hybrid big model, users of different languages can use it conveniently.
[0133] See also Figure 7 , is a structural diagram of a SQL statement generation method system based on a large model in an embodiment of the present application, including:
[0134] The data processing module 701 is used to vectorize and document the metadata and metadata description text information from the database, and store them in the vector database and the full-text search library;
[0135] The intelligent SQL module 702 is used to respond to the user's query instruction, perform query matching on the vector database and the full-text search library respectively, sort the matching results ranked top as a preset ranking according to the similarity from high to low, and determine the optimal SQL statement list and optimal metadata;
[0136] A prompt word determination module 703 generates a prompt word of the current language based on the current language of the query instruction in combination with the optimal SQL statement list and the optimal metadata;
[0137] The SQL generation module 704 is used to determine a preset type of big model according to the language type of the prompt word, call the preset big model to infer the prompt word, and generate an SQL statement, wherein the prompt word of each language type corresponds to at least one big model.
[0138] It should be noted that the SQL statement generation method based on a big model in the present application corresponds one-to-one to the SQL statement generation method based on a big model. The corresponding technical details, i.e., the technical solutions, refer to the above-mentioned SQL statement generation method based on a big model, and will not be repeated here one by one.
[0139] Through the above method, by vectorizing and documenting the metadata of the database and its descriptive text information, deep analysis and structuring of the data are achieved. On the one hand, the potential relationship between the data is captured through vectorization processing, which is convenient for subsequent similarity calculation and rapid retrieval; on the other hand, the document processing retains the context information of the original text, which is convenient for full-text retrieval; the storage structure of the data is optimized, providing a solid foundation for subsequent queries. By using the vector database for vector query matching based on similarity, and using the full-text retrieval library for precise or fuzzy search of text content, not only the query coverage is expanded, but also the results can be sorted according to similarity to screen out those that match the user's query intention. It can automatically determine the optimal SQL statement list and metadata based on the query results, and recommend the most suitable SQL statement and metadata for the current query needs to users, reducing the time cost of manual screening and adjustment by users. It can also identify the current language of the user's query instructions and generate prompt words in the corresponding language accordingly, greatly enhancing the multi-language support capabilities, so that users with different language backgrounds can get a smooth interactive experience. At the same time, by calling the preset large model to infer the prompt words, it can automatically generate SQL statements that meet user needs, ensuring that the generated SQL statements are both accurate and in line with language habits.
[0140] In addition, this application not only reduces the complexity and error rate of manual operations, but also improves query response speed and user satisfaction. By continuously optimizing the training data and algorithms of the big model, the intelligence level of this application will continue to improve, bringing users a more convenient and efficient database query experience. Based on the big model and generative AI technology, the accuracy of the generated SQL statements is improved, the usage threshold for non-technical personnel is lowered, and the efficiency of SQL statement queries is improved; in addition, by understanding the user's natural language input and remembering the user's usage context, the ease of use and interactivity of SQL statement queries are improved. Through the use of multiple languages combined with hybrid big models, users of different languages can use it conveniently.
[0141] In other embodiments, the present disclosure also provides an electronic device, including: a processor and a memory; the memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device executes the above method.
[0142] Figure 8 The structure diagram of the computer system suitable for implementing the electronic device of the embodiment of the present application is shown. It should be noted that: Figure 8 The computer system 800 of the electronic device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present application.
[0143] like Figure 8 As shown, the computer system 800 includes a central processing unit (CPU) 801, which can perform various appropriate actions and processes according to the program stored in the read-only memory (ROM) 802 or the program loaded from the storage part 808 to the random access memory (RAM) 803, such as executing the method in the above embodiment. In the random access memory 803, various programs and data required for system operation are also stored. The central processing unit 801, the read-only memory 802 and the random access memory 803 are connected to each other through a bus 804. The input / output (I / O) interface 805 is also connected to the bus 804.
[0144] The following components are connected to the input / output interface 805: an input section 806 including a keyboard, a mouse, etc.; an output section 807 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN (Local Area Network) card, a modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the input / output interface 805 as needed. A removable medium 811, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 810 as needed so that a computer program read therefrom is installed into the storage section 808 as needed.
[0145] In particular, according to an embodiment of the present application, the process described above with reference to the flowchart can be implemented as a computer software program. For example, an embodiment of the present application includes a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program includes a computer program for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network through a communication part 809, and / or installed from a removable medium 811. When the computer program is executed by the central processing unit 801, various functions defined in the system of the present application are executed.
[0146] The above description and the accompanying drawings fully illustrate the embodiments of the present disclosure so that those skilled in the art can practice them. Other embodiments may include structural, logical, electrical, process and other changes. The embodiments represent possible changes only. Unless explicitly required, separate components and functions are optional, and the order of operation may vary. Parts and subsamples of some embodiments may be included in or replace parts and subsamples of other embodiments. Moreover, the words used in this application are only used to describe the embodiments and are not used to limit the claims. As used in the description of the embodiments and the claims, unless the context clearly indicates, the singular forms of "a", "an" and "the" are intended to include plural forms as well. Similarly, the term "and / or" used in this application refers to any and all possible combinations of listings containing one or more associated ones. In addition, when used in the present application, the term "comprise" and its variants "comprises" and / or comprising refer to the presence of a stated sub-sample, whole, step, operation, element, and / or component, but do not exclude the presence or addition of one or more other sub-samples, wholes, steps, operations, elements, components and / or groups of these. In the absence of further restrictions, the elements defined by the sentence "comprising a ..." do not exclude the presence of other identical elements in the process, method or device comprising the elements. In this article, each embodiment may focus on the differences from other embodiments, and the same and similar parts between the various embodiments may refer to each other. For the methods, products, etc. disclosed in the embodiments, if they correspond to the method part disclosed in the embodiments, then the relevant parts can refer to the description of the method part.
[0147] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software may depend on the specific application and design constraints of the technical solution. Technicians can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the embodiments of the present disclosure. Technicians can clearly understand that for the convenience and simplicity of description, the specific working process of the above-described system, device and unit can refer to the corresponding process in the aforementioned method embodiment, and will not be repeated here.
[0148] In the embodiments disclosed herein, the disclosed methods and products (including but not limited to devices, equipment, etc.) can be implemented in other ways. For example, the device embodiments described above are only schematic. For example, the division of units can be only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some sub-samples can be ignored or not executed. In addition, the coupling or direct coupling or communication connection between each other shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms. The units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units may be selected according to actual needs to implement this embodiment. In addition, each functional unit in the embodiment of the present disclosure may be integrated in a processing unit, or each unit may exist physically alone, or two or more units may be integrated in one unit.
[0149] The flowchart and block diagram in the accompanying drawings show the possible architecture, function and operation of the method, system and computer program product according to the embodiment of the present disclosure. In this regard, each box in the flowchart or block diagram can represent a module, a program segment or a part of the code, and a part of the module, program segment or code contains one or more executable instructions for realizing the specified logical function. In some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two consecutive boxes can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, which can depend on the functions involved. In the description corresponding to the flowchart and block diagram in the accompanying drawings, the operations or steps corresponding to different boxes can also occur in a different order from the order disclosed in the description, and sometimes there is no specific order between different operations or steps. For example, two consecutive operations or steps can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, which can depend on the functions involved. Each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, may be implemented by a dedicated hardware-based system that performs the specified functions or actions, or may be implemented by a combination of dedicated hardware and computer instructions.
Claims
1. A method for generating SQL statements based on a large model, characterized in that: include: The metadata and metadata description text information from the database are vectorized and documented respectively, and stored in the vector database and full-text retrieval library; In response to the user's query instruction, query and match the vector database and the full-text search library respectively, sort the matching results ranked by a preset ranking from high to low according to the similarity, and determine the optimal SQL statement list and optimal metadata; Based on the current language of the query instruction, generate a prompt word for the current language in combination with the optimal SQL statement list and the optimal metadata; A preset type of big model is determined according to the language type of the prompt word, and the preset big model is called to infer the prompt word to generate an SQL statement, wherein the prompt word of each language type corresponds to at least one big model.
2. The method according to claim 1, characterized in that After generating the SQL statement, it also includes: Verify the SQL statement according to preset rules, and if the verification passes, output the SQL statement to the user; wherein the preset rules include security policies and sensitive word filtering; If the verification fails, the defects of the SQL statement are identified based on the intelligent SQL knowledge base, and SQL optimization suggestions are generated based on a hybrid optimization mode of rules and large models.
3. The method according to claim 2, characterized in that The hybrid optimization mode based on rules and large models is at least one of the following: Move the filtering conditions to the bottom layer for query to determine whether the SQL statement can be pushed down; Determine whether to rewrite the SQL statement, determine whether to optimize the aggregate function, and determine whether to merge multiple SQL statements.
4. The method according to claim 1, characterized in that The preset large model is called to infer the prompt word and generate SQL statements, including: If it is detected that the prompt word of a certain language corresponds to one and only one big model, the big model is called to infer the prompt word, and the SQL statement is generated accordingly; or, if it is detected that the prompt word of a certain language corresponds to one and only one big model, the prompt word, the optimal SQL statement list and the optimal metadata are jointly input into the big model for inference, and the SQL statement is generated accordingly, wherein the big model is a multimodal big model; If it is detected that the prompt word of a certain language corresponds to several large models, each of the large models is called to infer the prompt word, and a plurality of SQL statements are generated accordingly. The plurality of SQL statements are integrated and processed to generate a final SQL statement.
5. The method according to claim 1, characterized in that Based on the current language of the query instruction, the prompt words for the current language are generated in combination with the optimal SQL statement list and the optimal metadata, including: Configuring the prompt word template according to template options, wherein the template options include prompt word template classification, prompt word template name, and prompt word type; Based on the business scenario to which the query instruction belongs, the category and name of the prompt word template are determined; based on the current language of the query instruction, the user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction are filled into the prompt word template and translated into prompt words in the current language.
6. The method according to any one of claims 1 to 5, characterized in that: Before vectorizing and documenting the metadata and metadata description text information from the database, the following steps are also included: Obtain database configuration information, including database type, database access address, database port, and account password; Accessing a data source in a database according to the database configuration information, parsing a table structure of the data source based on a preset metadata query rule, and determining metadata describing data features and attributes; Determine whether the metadata has associated metadata description text information, where the metadata description text information includes custom description information; If the metadata is associated with the custom description information, the metadata and the custom description information are translated into multilingual description information, and sent to the intelligent SQL database for storage in a preset format; If the metadata does not have the associated custom description information, the annotation of the table or field is translated; if the annotation of the table or field does not exist, the name of the table or field is translated, and the translated multilingual description information is sent to the intelligent SQL database for storage in a preset format.
7. The method according to any one of claims 1 to 5, characterized in that: The full-text search library includes: Determine the local knowledge base based on the knowledge base classification, knowledge base name, and local file path; Converting the document data in the local knowledge base into text information, and segmenting the text information according to a text segmentation strategy to obtain a preset format text, wherein the text segmentation strategy includes at least one of the following: paragraph, text line, word; The preset format text is converted into a document vector and stored in a vector database that supports a vector search engine to form a full-text retrieval library.
8. The method according to claim 1, characterized in that The current language of the query instruction is generated based on the optimal SQL statement list and the optimal metadata as a prompt word of the current language, including: Constructing a feature description word library, wherein the description word library includes positive description words and negative description words describing potential risk features; Determine the prompt word template according to the description words and prompt word projects in the description word library; Based on the business scenario to which the query instruction belongs, the category and name of the prompt word template are determined; based on the current language of the query instruction, the user question information, the optimal SQL statement list and the text description information of the optimal metadata corresponding to the query instruction are filled into the prompt word template and translated into prompt words in the current language.
9. A SQL statement generation method system based on a large model, characterized in that: include: The data processing module is used to vectorize and document the metadata and metadata description text information from the database, and store them in the vector database and the full-text search library; An intelligent SQL module is used to respond to a user's query instruction, query and match the vector database and the full-text search library respectively, sort the matching results ranked top as a preset ranking according to the similarity from high to low, and determine the optimal SQL statement list and optimal metadata; A prompt word determination module, based on the current language of the query instruction, combines the optimal SQL statement list and the optimal metadata to generate a prompt word for the current language; The SQL generation module is used to determine a preset type of big model according to the language type of the prompt word, call the preset big model to infer the prompt word, and generate an SQL statement, wherein the prompt word of each language type corresponds to at least one big model.
10. An electronic device, characterized in that: include: Processor and memory; The memory is used to store a computer program, and the processor is used to execute the computer program stored in the memory, so that the electronic device executes the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
SQL statement generation method and device
CN118377783A
Database query method and device based on natural language and electronic equipment
CN118535679A
Cited By
Method for increasing accuracy of generating complex SQL by natural language through SQL example
CN119357217A
Method for increasing the accuracy of complex SQL generated by natural language using SQL examples
CN119357217B
Text generation method and device, electronic equipment and storage medium
CN119719356A
Text generation method, device, electronic device and storage medium
CN119719356B
Database query optimization method and system based on cue word guidance and application
CN119782351A
