Method and device for realizing text SQL (Structured Query Language) based on cue words of large language model
By adopting a method of generating SQL based on a large language model in the Text-to-SQL system, combining business data, metadata and historical examples, the accuracy and stability of the existing system in complex queries and multi-domain scenarios are solved, and the system's performance and adaptability are significantly improved.
Patent Information
- Application Number
- CN202510502791.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-22
- Publication Date
- 2025-05-23
- Estimated Expiration
- 2045-04-22
AI Technical Summary
When the existing Text-to-SQL system handles complex queries and multi-domain scenarios, it is difficult to generate accurate and stable SQL statements, and it relies on manual design rules and templates, and is suitable for simple database scenarios.
The Wensheng SQL method implemented by using the prompt words based on a large language model is generated by obtaining text data, matching data sets, determining historical examples, text parsing, generating prompt words and inputting a large language model. This method combines business data, business metadata, historical examples and text data to generate prompt words, and generates SQL statements through a large language model embedded in the OQL module.
It significantly improves the accuracy and stability of the Text-to-SQL system, reduces the difficulty of generating complex SQL statements, enhances the generalization ability and efficiency of the system, and is especially suitable for complex queries and multi-domain scenarios.
Smart Images

Figure CN120030041A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the technical field related to natural language processing, and in particular to a method and device for implementing a text-based SQL based on a large language model prompt word. Background Art
[0002] Text-to-SQL systems are designed to convert natural language queries into structured SQL queries so that users can query data in relational databases through simple language descriptions without having to understand SQL syntax. Such systems are particularly useful for end users who need to extract information from a database but do not have programming or SQL knowledge.
[0003] However, existing solutions rely on manually designed rules and templates and are suitable for simple database scenarios. Summary of the invention
[0004] In view of this, the embodiments of the present application are dedicated to providing a Wensheng SQL method and device based on a large language model prompt word implementation.
[0005] The present application provides a Wensheng SQL method based on a large language model prompt word, including: Get text data; Determine a data set that matches the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; In a preset historical example set, determining a historical example corresponding to the text data as a target historical example; Performing text parsing on the text data to determine business data and business metadata in the data set that match the text data; Combining the business data, business metadata, target historical examples and text data to obtain prompt words; The prompt word is input into a preset large language model embedded with an OQL module to obtain a Sql statement corresponding to the text data.
[0006] In some embodiments, the determining, in a preset set, a data set to which the text data matches; Obtaining a data set selection instruction input by a user; determining a data set matching the text data based on the data set selection instruction; or, Matching the content in the preset set with information associated with the text data, determining the data set to which the information belongs, and selecting the most forward data set as the data set that matches the text data according to a custom weight order; or The text data is input into a preset data set matching model to obtain a data set that matches the text data; wherein the data set matching model is a deep learning model.
[0007] In some embodiments, the historical example set includes: general examples and domain examples; The domain examples include examples marked as correct during user question-answering; The general examples are pre-stored examples.
[0008] In some embodiments, determining a historical example corresponding to the text data in a preset historical example set includes: Determining the similarity between the text data and each example in the historical example set; If there are examples with a similarity greater than a preset value, the example with the highest similarity is selected as the historical example; If there is no example with a similarity greater than a preset value, an example is randomly selected from a preset number of examples with the highest similarity as a historical example.
[0009] In some embodiments, performing text parsing on the text data to determine business data and business metadata matching the text data in the data set includes: Determine the business domain concepts in the data set that match the text data by using a character matching method; Based on the information summary corresponding to the text data and the business domain concepts, based on the similarity matching method, the business data and business metadata matching the text data in the data set are determined.
[0010] In some embodiments, the OQL module in the large language model embedded with the OQL module is used to regard a multi-table structure as a single table structure; The large language model is used to generate an Sql statement for the single-table structure, and then the single-table structure Sql statement is parsed based on a preset SQL parser to generate an Sql statement for a multi-table structure.
[0011] In some embodiments, it also includes: optimizing the Sql statement.
[0012] The present application also provides a Wensheng SQL device implemented based on a large language model prompt word, including: An acquisition module, used to acquire text data; A determination module is used to determine a data set matching the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; in a preset historical example set, determine a historical example corresponding to the text data as a target historical example; perform text parsing on the text data to determine business data and business metadata matching the text data in the data set; A combining module, used for combining the business data, business metadata, target history examples and text data to obtain prompt words; A generation module is used to input the prompt word into a preset large language model embedded with an OQL module to obtain an Sql statement corresponding to the text data.
[0013] The present application also provides an electronic device, including: A processor, and a memory for storing a program executable by the processor; The processor is used to implement the Wensheng SQL method based on the large language model prompt words as described above by running the program in the memory.
[0014] The present application also provides a computer-readable storage medium, on which a computer program is stored. When the computer program is executed by a processor, the processor executes the Wensheng SQL method based on the large language model prompt word as described above.
[0015] The present application provides a method for implementing a text-generated SQL method based on a large language model prompt word, first obtaining text data; determining a data set that matches the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; determining a historical example corresponding to the text data in a preset historical example set as a target historical example; performing text parsing on the text data to determine the business data and business metadata that match the text data in the data set; combining the business data, business metadata, target historical examples and text data to obtain prompt words; inputting the prompt words into a preset large language model embedded with an OQL module to obtain a SQL statement corresponding to the text data. In this way, by matching the data set corresponding to the user text data in the preset set, it is possible to ensure that the generated SQL statement is only based on the business field and sub-field related to the user's question, avoiding interference from irrelevant information, reducing hallucination problems, and improving the accuracy of generation. Inputting historical examples as prompt words into the large model can significantly improve the accuracy of responses to repeated questions. The historical examples contain verified correct SQL statements, and the large model can "copy answers", thereby reducing the possibility of generation errors and enhancing the stability of generation. Custom OQL module: By abstracting complex multi-table query logic into single-table query, the large model only needs to process single-table logic, which greatly reduces the difficulty of generating complex SQL statements. This design enables the large model to focus more on the core semantics of user questions, rather than being distracted by complex structures such as multi-table associations. The prompt words generated by combining business data, business metadata, and historical examples provide accurate contextual information for the large model, avoiding interference from irrelevant information in the prompt words, and further reducing the difficulty of generating complex SQL. The solution provided in this application significantly improves the accuracy and stability of the Text-to-SQL system based on a large language model through data set matching, the use of historical examples, custom OQL parsing, and precise context description, while reducing the difficulty of generating complex SQL statements and enhancing the system's generalization ability and efficiency, which is particularly suitable for complex queries and multi-domain scenarios. BRIEF DESCRIPTION OF THE DRAWINGS
[0016] By describing the embodiments of the present application in more detail in conjunction with the accompanying drawings, the above and other purposes, features and advantages of the present application will become more apparent. The accompanying drawings are used to provide a further understanding of the embodiments of the present application and constitute a part of the specification. Together with the embodiments of the present application, they are used to explain the present application and do not constitute a limitation of the present application. In the accompanying drawings, the same reference numerals generally represent the same components or steps.
[0017] Figure 1 It is a flowchart of a Wensheng SQL method implemented based on a large language model prompt word provided by an embodiment of the present application.
[0018] Figure 2 This is a data set structure diagram provided by an embodiment of the present application.
[0019] Figure 3 This is a flowchart provided by an embodiment of the present application.
[0020] Figure 4 It is a structural diagram of a Wensheng SQL device implemented based on a large language model prompt word provided by an embodiment of the present application.
[0021] Figure 5 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION
[0022] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0023] Exemplary Methods Figure 1 FIG. 1 is a flow chart of a Wensheng SQL method implemented based on a large language model prompt word provided by an embodiment of the present application. Figure 1 As shown, the method includes the following contents.
[0024] Step S110, obtaining text data; The purpose of this step is to receive the user's natural language query input as the basis for subsequent processing. The input forms include: text input (such as chat box input), voice input (need to be transcribed into text first) or other multimodal input forms. The input is uniformly converted into text data in string format (i.e.: user question) for subsequent modules to process.
[0025] Step S120, determining a data set matching the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; Find the data set related to the user input in the preset collection to ensure that the generated SQL statement is based on the correct business domain and sub-domain. Preset collection: contains data sets corresponding to different domains and sub-domains. Each data set contains metadata such as library table information and field types for a specific domain. Matching methods: including: Manual selection: The user directly selects the corresponding business field or data set.
[0026] Content retrieval: Through keyword matching or semantic analysis, find information related to the user's question from the preset collection, and select the most matching data set based on the weight.
[0027] Big model analysis: Input user questions and data set descriptions into the big model, and let the model determine the best matching data set.
[0028] Step S130, determining a historical example corresponding to the text data in a preset historical example set as a target historical example; Find examples similar to the current user's question from the historical example collection and use them as part of the prompt words to improve the accuracy of generated SQL.
[0029] Historical example collection: contains question and answer records that users have marked as correct in the past (domain examples) and system default general examples.
[0030] Step S140, performing text parsing on the text data to determine business data and business metadata matching the text data in the data set; Parse user input, match it to relevant business data and metadata, and provide accurate information for generating prompt words. Text parsing includes: Character matching: directly match the keywords in the user input with business domain concepts (such as proper nouns, terms, etc.). Similarity matching: Combined with the word vector model, calculate the similarity between the user input and the business data and metadata to find highly relevant content. Matching results: Determine the business data (such as query conditions, display fields, etc.) and business metadata (such as library table structure, field type, etc.) related to the user's question.
[0031] Step S150, combining the business data, business metadata, target history examples and text data to obtain prompt words; Integrate business data, business metadata, target history examples, and user input to generate structured prompt words as input for the big model. The prompt words include: Business data and metadata: provide database table information and field descriptions related to user questions. Target history examples: contain verified correct SQL examples to help the big model understand user intent. User input: original question text to ensure that the big model generates SQL directly for user needs.
[0032] The prompt word format can be organized into a specific text format (such as including context description, task rules, etc.) according to the requirements of the large model.
[0033] Step S160: input the prompt word into a preset large language model embedded with an OQL module to obtain a SQL statement corresponding to the text data.
[0034] Use a large language model embedded with an OQL module to generate accurate SQL statements based on prompt words. Function of the OQL module: Abstract complex multi-table query logic into single-table query, reducing the difficulty of large models to generate complex SQL. Use SQL parsers (such as sqlparse, JSqlParser) to dynamically parse the generated single-table SQL into the final multi-table SQL statement. Large model input: Input prompt words into the large model, and the model generates corresponding OQL statements based on the prompt words. SQL generation: The OQL module parses OQL statements into SQL statements that conform to the target database structure to ensure that the generated SQL statements accurately execute user intent.
[0035] This process ensures the accuracy, stability and efficiency of SQL generation through sophisticated text parsing, historical example matching and custom OQL parsing. By combining business data, metadata and historical examples, the system can adapt to a variety of complex query scenarios while reducing the difficulty of generating large models, which is particularly suitable for business scenarios that need to handle multi-table associations and complex semantics.
[0036] In some embodiments, determining the data set that matches the text data in a preset set includes: Obtain a data set selection instruction input by a user; determine the data set that matches the text data based on the data set selection instruction; or, match the information associated with the text data from the content in a preset collection, determine the data set to which the information belongs, and select the frontmost data set as the data set that matches the text data according to a custom weight order; or input the text data into a preset data set matching model to obtain the data set that matches the text data; wherein the data set matching model is a deep learning model.
[0037] Specifically, in the process of converting text data into SQL statements, you first need to determine the data set related to the text data. Here are several ways to achieve this goal: Method 1: User manually selects the dataset Users can directly select data sets related to the input text according to their needs and understanding of the business. The system will provide a friendly interface, such as a drop-down menu or radio button, so that users can easily select the corresponding business field or data set. This method is very intuitive and is particularly suitable for users who are very familiar with business data. In this way, users can ensure that the system uses the correct data set to generate SQL statements, avoiding errors that may be caused by automatic matching.
[0038] Method 2: Content retrieval matching dataset If the user is not sure which dataset to choose, or wants the system to handle it automatically, they can rely on the content retrieval function. The system will analyze the text content entered by the user, extract the key information, and find the content associated with this information in the preset dataset. For example, if the user input mentions a specific business term or keyword, the system will look for datasets containing these terms or keywords. To ensure accuracy, the system will sort the matching results according to certain weight rules, and datasets with higher weights will be selected first. This method can effectively reduce user operations and improve the intelligence level of the system, and is particularly suitable for handling complex business scenarios.
[0039] Method 3: Deep learning model matching dataset For more complex scenarios, deep learning models can be used to automatically determine the relevance of text data to datasets. This model is trained with a large amount of annotated data and can understand the semantics of text and accurately identify matching datasets. Users only need to input the input text data into this preset dataset matching model, and the model will automatically analyze and output the most relevant dataset. This method is particularly suitable for processing large-scale datasets or complex business scenarios, and can significantly improve the accuracy and efficiency of matching.
[0040] Through the above three methods, the system can flexibly determine the data set related to the user input text, thereby providing an accurate basis for subsequent SQL generation. Whether it is manual selection by the user, automatic retrieval by the system, or intelligent matching using deep learning models, each method has its unique application scenarios and advantages, ensuring the efficient operation of the system in different situations.
[0041] In some embodiments, the historical example set includes: general examples and domain examples; the domain examples include examples marked as correct during the user question and answer process; the general examples are pre-stored examples.
[0042] In the Wensheng SQL system based on large language model prompt words, the historical example collection is an important component, which helps the system better understand and generate SQL statements. This historical example collection mainly consists of two parts: general examples and domain examples.
[0043] Common examples are a set of examples pre-stored by the system. They come with the system and are used to provide basic reference and backup support. These examples are usually carefully designed and verified, covering common query scenarios and SQL structures. They act like a knowledge base, providing a reliable starting point for the system, ensuring that the system can still generate reasonable SQL statements without sufficient domain examples.
[0044] Domain examples are gradually accumulated in the process of users using the system. When users mark the SQL statements generated by the system and confirm their correctness, these verified question and answer records will be stored as domain examples. Domain examples are highly targeted, and they reflect the correspondence between common queries and correct SQL statements in specific business fields. By marking and storing these examples, the system can continuously learn and adapt to the query patterns in specific fields, thereby improving the accuracy and relevance of generated SQL statements.
[0045] General examples and domain examples together constitute the historical example set, which play different roles in the system. General examples provide broadly applicable basic support, while domain examples provide personalized guidance for specific fields. When a user enters a new query, the system first tries to find similar records from the domain examples. If no suitable match is found, it falls back to the general examples. This design ensures that the system can maintain high accuracy and stability when processing various queries.
[0046] By combining general examples and domain examples, the system can better understand the user's intention and generate SQL statements that meet the user's needs. This mechanism not only improves the intelligence level of the system, but also enhances its adaptability in different business scenarios.
[0047] In some embodiments, determining a historical example corresponding to the text data in a preset historical example set includes: Determine the degree of similarity between the text data and each example in the historical example set; if there is an example with a similarity greater than a preset value, select the example with the highest similarity as the historical example; if there is no example with a similarity greater than the preset value, randomly select an example from a preset number of examples with the highest similarity as the historical example.
[0048] In some embodiments, to determine the historical examples corresponding to the text data, the system performs the following steps: 1. Calculate similarity The system first calculates the similarity between the text data entered by the user and each example in the historical example set. This can be achieved through a variety of technologies, such as using word vector models (such as Word2Vec, GloVe) or deep learning models (such as BERT) to calculate the semantic similarity between texts. The similarity calculation results will help the system determine which historical examples are most relevant to the current input.
[0049] 2. Screening Examples The system checks whether there are examples with a similarity greater than a preset value. This preset value is a threshold used to judge the relevance of examples. If the similarity of a historical example exceeds this threshold, it is considered to be highly relevant to the current input.
[0050] 3. Select Example High similarity example: If there are examples with a similarity greater than the preset value, the system will select the example with the highest similarity as the target historical example. This example will be used as part of the prompt word to help the large model generate SQL statements more accurately.
[0051] Random selection: If there is no example with a similarity greater than a preset value, the system will not give up looking for a reference example. Instead, it will randomly select one from the preset number of examples with the highest similarity. This strategy ensures that the system can provide a certain reference even when there is no high match, thereby improving the accuracy and stability of SQL generation.
[0052] Purpose and Advantages The purpose of this method is to improve the accuracy and efficiency of SQL generation. By selecting the examples that are most relevant to the user input, the system can better understand the user's intention and generate SQL statements that meet the user's needs. At the same time, this mechanism also enhances the robustness of the system. Even if there is no perfect match, it can provide a certain reference through random selection to avoid the situation where the system cannot generate SQL due to lack of examples.
[0053] Furthermore, in the case of random selection, if the output does not meet the user's needs, it can be regenerated. When regenerating, different examples can be used to obtain different outputs, thus avoiding outputting the same erroneous result multiple times.
[0054] In some embodiments, performing text parsing on the text data to determine business data and business metadata matching the text data in the data set includes: The business domain concepts in the data set that match the text data are determined based on a character matching method; based on the information summary corresponding to the text data and the business domain concepts, the business data and business metadata that match the text data in the data set are determined based on a similarity matching method.
[0055] In the Wensheng SQL system based on large language model prompt words, parsing text data to match business data and metadata is a key step. The following is a detailed introduction to this process: 1. Character matching: Determine business domain concepts The system first uses a character-based matching method to determine the matching relationship between the text data entered by the user and the business domain concepts defined in the data set. This step is like looking up a word in a dictionary. The system directly compares the keywords in the user input with the predefined business domain concepts (such as proper nouns, terms, etc.) in the data set.
[0056] The role of character matching: This method is simple and direct, and can quickly identify business concepts that are explicitly mentioned in user input. For example, if the user input mentions "average balance", the system will directly look for this concept in the data set and associate it with the relevant query method or data field.
[0057] Advantages of character matching: Its advantages are efficiency and accuracy, especially when dealing with clear and standardized business terms, it can quickly find matches.
[0058] 2. Similarity matching: determining business data and metadata After determining the business domain concepts, the system will further use similarity matching to determine the business data and business metadata that match the user input. This step is more like understanding the meaning of a sentence rather than just looking up words.
[0059] The role of similarity matching: By calculating the semantic similarity between user input and the content in the data set, the system can identify data and metadata that are not directly in the input but are related to the user's intention. For example, if the user input mentions "sales in the last month", the system may match relevant fields such as "sales data" and "time range".
[0060] Advantages of similarity matching: This method can handle more complex queries, especially when users use non-standard expressions or implicit semantics, the system can still understand the user's needs and find relevant data.
[0061] In some embodiments, the two methods can be combined; character matching and similarity matching complement each other. Character matching provides direct and clear matching results, while similarity matching supplements semantic understanding and expansion. By first performing character matching to determine business domain concepts, and then performing similarity matching based on these concepts, the system can understand user input more comprehensively and accurately, and find relevant business data and metadata.
[0062] This combined approach ensures that the system can quickly respond to clear requests and deeply understand complex intent when processing various queries, thereby improving the accuracy and efficiency of SQL generation.
[0063] The OQL module in the large language model embedded with the OQL module is used to regard a multi-table structure as a single table structure; the large language model is used to generate an Sql statement for the single table structure, and then the single table structure Sql statement is parsed for a multi-table structure based on a preset SQL parser.
[0064] In the Wensheng SQL system implemented based on the large language model prompt words, the embedded OQL (Object QueryLanguage) module plays a vital role. The following is a detailed introduction to the OQL module and its collaboration with the large language model: The core function of the OQL module is to abstract complex multi-table structures into single-table structures. This abstraction allows large language models to focus on single-table logic when generating SQL statements, significantly reducing the difficulty of generating complex SQL statements.
[0065] Specifically, when the text data entered by the user involves multi-table queries, the OQL module abstracts these multi-table structures into a logical single-table structure. This is similar to creating a virtual "joint table" that contains all relevant fields and data. This abstraction allows large language models to focus on the core semantics of user questions without having to deal with complex multi-table association logic.
[0066] The large language model generates SQL statements for this abstract single table based on prompt words (including business data, business metadata, historical examples, and user input). Due to the simplified structure of the single table, the model can more accurately understand user intent and generate corresponding SQL statements.
[0067] The generated single-table SQL statement is then input into the preset SQL parser. The SQL parser will identify the fields and table structure in the single-table SQL and map these fields to the actual multi-table structure according to the preset database table association rules.
[0068] In this way, the final generated SQL statement can correctly execute multi-table queries and meet user needs.
[0069] With this setup, by abstracting the multi-table structure into a single-table structure, the OQL module significantly reduces the complexity of generating SQL statements for large language models, and improves the accuracy and stability of the generated statements. The OQL module enables the system to adapt to various complex business scenarios, especially those involving multi-table associations and complex queries. By simplifying the generation logic, the system can respond to user requests more quickly and improve overall efficiency.
[0070] The OQL module abstracts the multi-table structure into a single-table structure, allowing the large language model to focus on generating single-table SQL statements, while the SQL parser is responsible for converting these statements into actual multi-table SQL statements. This design not only improves the accuracy and efficiency of SQL generation, but also enhances the system's adaptability in complex business scenarios.
[0071] In some embodiments, it also includes: optimizing the Sql statement.
[0072] Since the Sql statement obtained in the above solution is converted based on the Sql statement obtained by the OQL module, there may be some unreasonable or bloated and redundant parts. Based on this, the Sql statement is optimized: After the SQL statement is generated, the system will perform a preliminary analysis to identify possible performance bottlenecks or unnecessary parts. This step is similar to proofreading a draft to find areas for improvement.
[0073] Optimization methods include: Table join optimization: Check the table join conditions in the SQL statement to ensure that the join conditions are efficient and necessary. For example, if there are redundant join conditions, the system will remove them to reduce unnecessary calculations.
[0074] Subquery optimization: Convert complex subqueries into join queries or other more efficient expressions to improve execution efficiency.
[0075] Field selection optimization: Ensure that only necessary fields are selected in the SQL statement and avoid using SELECT *, thereby reducing data transmission volume and processing time.
[0076] Sorting and grouping optimization: Optimize the ORDER BY and GROUP BY clauses and ensure that they are based on index fields to improve the efficiency of sorting and grouping.
[0077] Index optimization: Based on the query conditions and table structure, indexes are recommended or automatically added to speed up queries.
[0078] Execution plan optimization: Use a query optimizer (such as Apache Calcite) to analyze the execution plan of a SQL statement, identify areas for improvement, and generate a more efficient execution plan.
[0079] Syntax optimization: Simplify complex SQL syntax, such as converting multiple OR conditions into IN conditions, to improve readability and execution efficiency.
[0080] Constant expression optimization: Calculate constant expressions in advance to avoid repeated calculations during query execution.
[0081] Subquery optimization: Convert subqueries into join queries or other more efficient expressions.
[0082] Nested query optimization: Expand nested queries into multiple simple queries to improve execution efficiency.
[0083] After optimization, the execution efficiency of SQL statements will be significantly improved while maintaining its functionality. This is similar to refining a draft to make it more concise and efficient.
[0084] The system can also automatically perform the optimization steps by leveraging existing SQL parsing and optimization tools, such as Apache Calcite, JSqlParser, etc. These tools provide powerful functions to analyze and optimize SQL statements to ensure that they achieve optimal performance when actually executed.
[0085] Optimization can improve query efficiency: Optimized SQL statements can be executed faster, reducing query time. By removing redundant operations and optimizing execution plans, CPU, memory, and disk I / O consumption are reduced. Faster query response time improves the overall user experience, especially when processing large amounts of data.
[0086] By optimizing SQL statements, the system can ensure that the generated SQL is not only semantically correct, but also optimal in terms of execution efficiency. This step is particularly important for processing complex queries and large-scale data sets, and can significantly improve the performance and response speed of the system.
[0087] The following describes the solution provided by this application in combination with the above-mentioned preferred embodiments: This invention relates to the Text-to-SQL direction under the field of large model technology. This system aims to significantly improve the accuracy and stability of converting natural language queries into SQL statements by integrating historical question and answer example modules, data set modules, and custom OQL (Object Query Language) parsing mechanisms. It is suitable for various human language and structured data interaction scenarios, such as intelligent BI, etc.
[0088] Compared with traditional Text-to-SQL solutions, our system adopts an innovative approach to overcome many limitations of existing technologies: inability to generate complex semantic SQL, low reasoning efficiency, cost waste caused by invalid tokens, unstable SQL generation, etc.
[0089] First, the levels are divided according to business fields. Each field contains multiple information sets (ie, data and). A single information set is composed of the data sources required for a session. The ultimate goal of the system is to match the truly relevant business information to the greatest extent possible based on the user's question, put it into the large model prompt word, and generate the corresponding database query statement. This can not only eliminate the interference of useless information to the large model and reduce the occurrence of hallucinations, but also reduce the prompt word length as much as possible to avoid exceeding the large model context length.
[0090] Example management is to collect users' past historical questions and answers, allowing users to mark questions and answers that the system answers accurately. In future questions and answers, these questions and answers are provided to the big model in the form of prompt words as a reference to increase the accuracy of the system's answers.
[0091] In terms of implementation, the vector library can be used as a recall method for similarity retrieval. Similarity vectors are generated for user questions and associated with the data records of correct answers. When users ask questions, high-weight examples are retrieved through similarity and placed in the large model prompt words.
[0092] Strategically, it can be divided into two types: the same questions and different questions. When the user's question is consistent with the historical example question (the similarity is extremely high), the data can be directly used as the prompt word example. Otherwise, the first few data are sorted according to the weight and a part of them are randomly selected as the prompt word example.
[0093] The designs can be divided into two categories: Domain examples: Examples marked as correct during the user question-answering process.
[0094] General examples: refers to the default examples that come with the system, which are used as a backup.
[0095] Reference Figure 2 The data set is the data source required for a session, that is, it contains all the information that the SQL statement corresponding to a user's question depends on. The data set content needs to be generated into the system database in the form of configuration. The data set information listed below is not complete and needs to be supplemented according to different business needs.
[0096] Business domain concepts: Different business domains have their own unique concepts, including but not limited to proper nouns, terminology, customized data algorithms, etc.
[0097] Business metadata: including (environment information, library table information, etc.) Environmental information: database type, version, connection address, etc.
[0098] Database table information: The database table structure involved in a session (field code, field type, field explanation description, etc.), including the relationship between tables (join relationship between tables, join fields, etc.). Fields are stored in categories. The primary field refers to the core query field (for example, for querying the average balance, the balance field is the primary field). At the same time, the relationship between other fields and the primary field is established, such as the fields that need to be displayed (associated select fields) and the fields that need to be queried by conditions (associated where fields), to ensure that only field information related to local issues is matched in a session query. The matching strategy can be used to recall all fields associated with the primary field and the fields matched by similarity.
[0099] Business data: includes real data stored in the business database for query, dictionary reference fields (in many cases, users need a conversion relationship when querying fields in SQL queries (for example, disable and enable, which may be stored in the database as 0 or 1), etc.
[0100] Based on the natural recursive nature of the SQL syntax tree, a custom intermediate query language oql is used, that is, when calling the large model to generate database query statements, the data set as a whole is regarded as a database table. In this way, the large model only needs to generate SQL according to the logic of a single table, which greatly reduces the difficulty of SQL generation and enhances the accuracy of the large model's response. The single-table query SQL statement returned by the large model is dynamically parsed into a real SQL statement through the database table association relationship configured by the data set. Technically, a SQL parser can be used to parse it into a SQL syntax tree. There are many ready-made SQL parsing libraries that can be used for this purpose, such as sqlparse (Python), JSqlParser (Java), etc.
[0101] Reference Figure 3 , the solutions provided in this application include: Receive text input: It can be designed to support multi-modal unified entry, such as chat text input, voice input, etc. Finally, it is converted into a string.
[0102] Business domain and data set matching: Match the string content of the user's question to the corresponding data set. Currently, the commonly used methods are: Manual selection: Allows users to manually select the corresponding business field or data set for question and answering Content retrieval: Match the content in the full data set to the information related to the user's question, find out the data set to which this information belongs, and select the data set according to a custom weight order, such as the number of matched information.
[0103] Big model analysis: By providing user questions, business domain descriptions, and data set descriptions in the form of prompts, the big model can decide which data set to use.
[0104] Historical examples: Designed and implemented in an example management manner.
[0105] Text parsing: query according to the dependency order between data: First, we use user questions to match business domain concepts. The recall method is based on character matching. The concept description can be an explanation of the concept (for example, average balance: the arithmetic mean of the daily balance of an account within a specific time period), or it can be the method of querying data corresponding to this concept (for example, average balance: using the 'date' field as the unit to calculate the average using SUM() / COUNT(DISTINCT date field)). Tests have found that the latter has a higher accuracy rate.
[0106] Summarize the information of user questions and business domain concepts, and use the results of the summary to recall business data (query conditions, etc.) and business metadata (queried database tables, fields, etc.). The recall method uses the association relationship of the data itself in the database and the word vector model to perform similarity matching.
[0107] Prompt word assembly: The generation logic of large models from different manufacturers may be slightly different when used. In this module, you can define prompt words specific to different large models to ensure the stability of the large model output.
[0108] Example: (You are a database administrator who is proficient in SQL and familiar with various mainstream database management systems. Your task is to convert #users' questions into accurate SQL query statements according to different database types.
[0109] Here are some examples you can refer to: {{History Question and Answer Examples}}; The task rules and precautions are as follows: 1. The generated SQL can only contain the values specified in #schema; 2. #sideInfo contains concepts related to the business logic in the question. Please convert the SQL based on the question and these concepts. 5. When SQL performs calculations between parameters, the IFNULL function must be used to avoid calculation errors; #The relevant information of this task is as follows: #schema:{{metadata}}; #sideInfo:{{data}}; #User's question: {{question}}) Large model call: When coding, this module can abstract a unified call context and adapt to different large models.
[0110] OQL syntax analysis: Designed and implemented in the form of custom OQL parsing narrative, this module can shield the SQL differences between different databases in a hard-coded form, or parse into different dialect versions of SQL statements according to different database types in the form of dialects.
[0111] Sql optimization (optional step): The generated sql generated by the large model after oql syntax parsing can be optimized through this module to ensure execution efficiency. In the implementation, parsing libraries such as Apache Calcite (Java) can be used to generate more efficient execution plans through its optimizer.
[0112] Execute SQL and return results: Field translation: Display replacement based on the enumeration value of the data, and also develop graphical display of the page, etc.
[0113] In the solution provided by this application, a historical question and answer example module is added to greatly increase the accuracy of responses to repeated questions: the traditional way is to write or organize some examples and put them in the prompt words to facilitate the large model to generate SQL more accurately, but in actual usage scenarios, these prompt words may be quite different from user questions, and the accuracy of SQL generated by the large model cannot be guaranteed. Based on the marking of user historical questions and answers, the question and answer responses marked as correct are recorded in the vector library. When the user asks a similar question again, hitting highly similar questions and SQL from the vector library can allow the large model to complete SQL generation in the form of "copying answers", greatly increasing the accuracy and stability.
[0114] The dataset module provides accurate context description for the big model to the maximum extent: the traditional prompt word-based approach is to put the library table DDL into the prompt word, but the relevant library table fields involved in a user's question and answer may only account for a small part of the overall field content, and it is impossible to distinguish the relevance. Putting the entire amount into the prompt word contains a large number of irrelevant fields, which will cause problems such as big model hallucinations and lead to very poor results. In addition, many questions and answers contain professional terms and cannot be associated with the content of the library table fields, so that the prompt word does not have a complete business context and thus the big model cannot generate correct SQL. The design based on the dataset can eliminate information irrelevant to the user's questions and answers by distinguishing the table field types. The design based on business domain concepts can make up for the incorrect output caused by the lack of understanding of professional terms when parsing the big model.
[0115] OQL greatly reduces the complexity of large model generation: The traditional method of using prompt words to let large models directly generate SQL has a very low accuracy rate in complex business scenarios and table structures, and cannot be solved simply by improving the capabilities of the large model itself. The custom OQL parsing method allows large models to process data in a single table structure, shielding the complexity brought by multi-table structures, which can greatly reduce the difficulty of generation and enhance accuracy and stability.
[0116] In some embodiments, there are several information recall methods currently available on the market: Recall based on keyword matching: (1) Simple string matching: Directly search for content containing these keywords in the database or document set based on the keywords entered by the user. This method is simple and direct, but the precision and recall rate may be limited. (2) Inverted index: Accelerate the keyword search process by building an inverted index. Each word has a list recording all the locations where it appears, which greatly improves the query speed.
[0117] Recall based on semantic understanding: Natural language processing (NLP) technology: Use NLP technology to understand the user's query intent and convert it into more precise search conditions. For example, use methods such as named entity recognition (NER) and syntactic analysis to parse the query content. Word vector model: Such as Word2Vec, GloVe, etc., convert the text into vector form, and then calculate the similarity between vectors for recall. This method can capture the semantic relationship between words, not just the surface vocabulary matching. Pre-trained models such as BERT: Using deep learning models such as BERT, it is possible to better understand the semantic information at the sentence level, thereby improving the accuracy and relevance of recall.
[0118] Knowledge graph-based recall: Knowledge graph construction: Build a knowledge graph in the field, including entities and their relationships. When a user asks a query, relevant entities and information can be found through the links in the graph. Path query: Perform complex path queries on the knowledge graph to discover implicit relationship chains and recall those indirectly related data points.
[0119] In practical applications, a better recall method can be selected according to different business scenarios. The data recall in this application includes two modules: example management and data set management. Example management uses word vector models to perform similarity recall to improve the generalization ability of question and answer parsing. In data set management, business domain concepts often cannot be well recalled through semantic understanding due to the characteristics of proper nouns. Therefore, it is recommended to use a simple string matching method, and the business data is implemented in the form of similarity recall using word vector models.
[0120] Exemplary Devices The device embodiments of the present application can be used to execute the method embodiments of the present application. For details not disclosed in the device embodiments of the present application, please refer to the method embodiments of the present application.
[0121] Figure 4 FIG. 1 is a block diagram of a text-generated SQL device based on a large language model prompt word implementation provided by an embodiment of the present application. Figure 4 As shown, the device comprises: An acquisition module 41, used for acquiring text data; The determination module 42 is used to determine the data set matching the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; in a preset historical example set, determine the historical example corresponding to the text data as the target historical example; perform text parsing on the text data to determine the business data and business metadata matching the text data in the data set; A combining module 43, used to combine the business data, business metadata, target history examples and text data to obtain prompt words; The generating module 44 is used to input the prompt word into a preset large language model embedded with an OQL module to obtain an Sql statement corresponding to the text data.
[0122] Exemplary Electronic Devices Below, reference Figure 5 To describe an electronic device according to an embodiment of the present application. Figure 5 A block diagram of an electronic device according to an embodiment of the present application is illustrated.
[0123] like Figure 5 As shown, electronic device 500 includes one or more processors 510 and memory 520 .
[0124] The processor 510 may be a central processing unit (CPU) or other forms of processing units having data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 500 to perform desired functions.
[0125] The memory 520 may include one or more computer program products, and the computer program product may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may include, for example, random access memory (RAM) and / or cache memory (cache), etc. The non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 510 may run the program instructions to implement the Wensheng SQL method based on the large language model prompt word implementation of the various embodiments of the present application described above and / or other desired functions. Various contents such as category correspondences may also be stored in the computer-readable storage medium.
[0126] In one example, the electronic device 500 may further include: an input device 550 and an output device 540 , and these components are interconnected via a bus system and / or other forms of connection mechanisms (not shown).
[0127] In addition, the input device 530 may also include, for example, a keyboard, a mouse, an interface, etc. The output device 540 may output various information to the outside, including analysis results, etc. The output device 540 may include, for example, a display, a speaker, a printer, a communication network and a remote output device connected thereto, etc.
[0128] Of course, to simplify, Figure 5 Only some of the components in the electronic device related to the present application are shown, and components such as a bus, an input / output interface, etc. are omitted. In addition, the electronic device may further include any other appropriate components according to specific application conditions.
[0129] Exemplary computer program products and computer-readable storage media In addition to the above-mentioned methods and devices, an embodiment of the present application may also be a computer program product, which includes computer program instructions, which, when executed by a processor, enable the processor to execute the steps of the Wensheng SQL method based on large language model prompt words according to various embodiments of the present application described in the above "Exemplary Method" section of this specification.
[0130] The computer program product may be written in any combination of one or more programming languages to write program codes for performing the operations of the embodiments of the present application, including object-oriented programming languages, such as Java, C++, etc., and conventional procedural programming languages, such as "C" language or similar programming languages. The program code may be executed entirely on the user computing device, partially on the user device, as an independent software package, partially on the user computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0131] In addition, an embodiment of the present application may also be a computer-readable storage medium having computer program instructions stored thereon, and when the computer program instructions are executed by a processor, the processor executes the steps of the Wensheng SQL method implemented based on large language model prompt words according to various embodiments of the present application described in the above “Exemplary Method” section of this specification.
[0132] The computer readable storage medium can adopt any combination of one or more readable media. The readable medium can be a readable signal medium or a readable storage medium. The readable storage medium can include, for example, but is not limited to, a system, device or device of electricity, magnetism, light, electromagnetic, infrared, or semiconductor, or any combination of the above. More specific examples (non-exhaustive list) of readable storage media include: an electrical connection with one or more wires, a portable 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 disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above.
[0133] The above description has been given for the purpose of illustration and description. In addition, this description is not intended to limit the embodiments of the present application to the forms disclosed herein. Although multiple example aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, changes, additions and sub-combinations thereof.
Claims
1. A Wensheng SQL method based on a large language model prompt word, characterized in that: include: Get text data; Determine a data set that matches the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; In a preset historical example set, determining a historical example corresponding to the text data as a target historical example; Performing text parsing on the text data to determine business data and business metadata in the data set that match the text data; Combining the business data, business metadata, target historical examples and text data to obtain prompt words; The prompt word is input into a preset large language model embedded with an OQL module to obtain a Sql statement corresponding to the text data.
2. The Wensheng SQL method based on the large language model prompt word implementation according to claim 1 is characterized in that: Determining, in a preset set, a data set that matches the text data includes: Obtaining a data set selection instruction input by a user; determining a data set matching the text data based on the data set selection instruction; or, Matching the content in the preset set with information associated with the text data, determining the data set to which the information belongs, and selecting the most forward data set as the data set that matches the text data according to a custom weight order; or The text data is input into a preset data set matching model to obtain a data set that matches the text data; wherein the data set matching model is a deep learning model.
3. The Wensheng SQL method based on the large language model prompt word implementation according to claim 1 is characterized in that: The historical example collection includes: general examples and domain examples; The domain examples include examples marked as correct during user question-answering; The general examples are pre-stored examples.
4. The Wensheng SQL method based on the large language model prompt word implementation according to claim 3 is characterized in that: Determining a historical example corresponding to the text data in a preset historical example set includes: Determining the similarity between the text data and each example in the historical example set; If there are examples with a similarity greater than a preset value, the example with the highest similarity is selected as the historical example; If there is no example with a similarity greater than a preset value, an example is randomly selected from a preset number of examples with the highest similarity as a historical example.
5. The Wensheng SQL method based on the large language model prompt word implementation according to claim 1 is characterized in that: Performing text parsing on the text data to determine business data and business metadata matching the text data in the data set includes: Determine the business domain concepts in the data set that match the text data by using a character matching method; Based on the information summary corresponding to the text data and the business domain concepts, based on the similarity matching method, the business data and business metadata matching the text data in the data set are determined.
6. The Wensheng SQL method based on the large language model prompt word implementation according to claim 1 is characterized in that: The OQL module in the large language model embedded with the OQL module is used to regard a multi-table structure as a single table structure; The large language model is used to generate an Sql statement for the single-table structure, and then the single-table structure Sql statement is parsed based on a preset SQL parser to generate an Sql statement for a multi-table structure.
7. The Wensheng SQL method based on the large language model prompt word implementation according to claim 1 is characterized in that: Also includes: Optimize the Sql statement.
8. A Wensheng SQL device based on a large language model prompt word, characterized in that: include: An acquisition module, used to acquire text data; A determination module is used to determine a data set matching the text data in a preset set; wherein the preset set includes data sets corresponding to different sub-fields under different fields; in a preset historical example set, determine a historical example corresponding to the text data as a target historical example; perform text parsing on the text data to determine business data and business metadata matching the text data in the data set; A combining module, used for combining the business data, business metadata, target history examples and text data to obtain prompt words; A generation module is used to input the prompt word into a preset large language model embedded with an OQL module to obtain an Sql statement corresponding to the text data.
9. An electronic device, characterized in that: include: A processor, and a memory for storing a program executable by the processor; The processor is used to implement the Wensheng SQL method based on the large language model prompt word according to any one of claims 1 to 7 by running the program in the memory.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the processor executes the Wensheng SQL method based on the large language model prompt word implementation according to any one of claims 1 to 7.
Citation Information
Patent Citations
Database query method and device based on natural language and electronic equipment
CN118535679A
Query method and device based on natural language and data knowledge and storage medium
CN119127910A
Method, system and equipment for generating SQL (Structured Query Language) statement based on large model
CN119127913A
Language conversion method and device based on retrieval enhancement and storage medium
CN119441261A
Data labeling method and device, equipment, medium and program product
CN119669466A
Cited By
Structured query statement generation method and system
CN120780730A