Semantic matching prompt-based large model database query generation method, system and equipment and medium

By building a bidirectional LSTM model and a semantic vector similarity matching mechanism, combined with a large language model generation method, the problems of insufficient semantic understanding and poor stability in the conversion of natural language to database queries are solved, efficient and accurate SQL statement generation is achieved, and cross-domain adaptation capabilities are improved.

CN120743947APending Publication Date: 2025-10-03CHINA UNIV OF PETROLEUM (BEIJING)
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202510823391.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-06-19
Publication Date
2025-10-03

AI Technical Summary

Technical Problem

Existing technologies have problems in converting natural language to database queries, such as insufficient semantic understanding, poor generation stability, and weak cross-domain generalization capabilities. As a result, the generated SQL statements are prone to logical omissions, redundancy, or illegal structures, and the domain knowledge in the training data is not systematically mined, resulting in a waste of data value.

Method used

A large-model database query generation method based on a semantic matching prompt mechanism is adopted. Semantic vectors are generated by building a bidirectional LSTM model. Historical query texts are retrieved using the semantic vector similarity matching mechanism. Structured prompt word instructions are constructed and input into a large language model to generate SQL statements consistent with user intentions.

Benefits of technology

It significantly improves the accuracy and robustness of natural language to SQL conversion, enhances cross-domain adaptability, reduces the probability of generating illegal or redundant SQL statements, and ensures that the generated results comply with grammatical specifications and business logic.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120743947A_ABST
    Figure CN120743947A_ABST
Patent Text Reader

Abstract

The invention relates to a large model database query generation method, system and device based on semantic matching prompt and a medium, and the method comprises the following steps: constructing a database query-oriented natural language semantic representation model, and generating a semantic vector by taking a natural language query text input into a database by a user as input; based on a semantic vector similarity matching mechanism, retrieving a historical query text most similar to the semantic vector from a pre-stored training set; extracting a database query statement corresponding to the historical query text as a semantic guidance prompt, and forming a structured prompt word instruction with a natural language query text input into a database by a user; and inputting the prompt word instruction into a pre-trained large language model, calling the context reasoning capability of the large language model, and generating a target database query statement consistent with the intention of the user, thereby completing the conversion from the natural language to the SQL, and obtaining a user query result. The method can be widely applied to the crossing field of artificial intelligence and database technologies.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the intersection of artificial intelligence and database technology, and specifically relates to the fields of natural language processing, deep learning, large language models and database query generation, and in particular to a large model database query generation method, system, device and medium based on semantic matching prompts, which realizes the conversion of natural language into database query statements (such as SQL) based on the semantic matching prompt mechanism and combined with the large language model. Background Art

[0002] With the widespread adoption of natural language interfaces (NLIs) in database systems, there's a growing demand for automatically converting natural language queries into structured database queries (such as SQL). As the core link in human-computer interaction, this technology directly impacts the intelligence level of database systems and the user experience. High-quality conversion not only requires accurately capturing user query intent but also ensuring that the generated SQL statements strictly match the database schema. Its accuracy directly impacts data retrieval efficiency and the reliability of business decisions.

[0003] Traditional technical paths have evolved mainly in three directions, but all have significant limitations: First, rule-based conversion methods rely on manually predefined grammatical templates. Although they can ensure basic grammatical correctness, they are difficult to cope with complex semantic expressions or diverse natural language sentences, and their flexibility is seriously insufficient; Second, end-to-end models based on neural networks (such as Seq2Seq and Transformer) directly generate SQL through a large amount of annotated data. Although this partially alleviates the rigidity of rule-based methods, they are highly dependent on the scale and quality of training data, have weak cross-domain generalization capabilities, and often have uncontrollable deviations in the generated results; Third, prompt generation methods based on large language models (such as GPT) can utilize the model's powerful contextual understanding capabilities, but are limited by the experience-dependent nature of prompt design and the model's own language bias, which can easily lead to semantic deviations or grammatical errors, and generation stability is difficult to guarantee.

[0004] Existing technologies have exposed multiple defects in practical applications: on the one hand, the semantic understanding of the conversion from natural language to database queries is insufficient, and the implicit intentions in natural language queries (such as nested multiple conditions and ambiguous references) are often misparsed, resulting in the generated SQL logic being missing or conflicting with the database schema; on the other hand, large language models are affected by the "hallucination" phenomenon and are prone to generating redundant clauses (such as incorrect JOIN operations) or illegal grammatical structures, requiring additional manual correction. In addition, the problem of poor generation stability of large models is prominent, further increasing the uncertainty of query generation; in addition, the domain knowledge in the training data (such as high-frequency query patterns and schema association rules) is not systematically mined, making it difficult for the model to extract generalizable features from limited samples, resulting in a waste of data value. Summary of the Invention

[0005] In response to the problems of insufficient semantic understanding and poor stability of large model generation in the conversion of natural language to database queries in the existing technology, the purpose of the present invention is to provide a large model database query generation method, system, device and medium based on a semantic matching prompt mechanism, which significantly improves the accuracy, robustness and cross-domain generalization ability of generated queries through the coordinated optimization of semantic vector retrieval and prompt mechanism.

[0006] To achieve the above object, the present invention adopts the following technical solutions:

[0007] In a first aspect, the present invention provides a method for generating a query for a large model database based on semantic matching prompts, comprising the following steps:

[0008] Build a natural language semantic representation model for database queries, taking the natural language query text entered by the user into the database as input and generating a semantic vector;

[0009] Based on the semantic vector similarity matching mechanism, the historical query text with the most similar semantic vector is retrieved from the pre-stored training set;

[0010] Extract the database query sentence corresponding to the historical query text as a semantic guidance prompt, and form a structured prompt word instruction with the natural language query text entered by the user into the database;

[0011] Input the prompt word instruction into the pre-trained large language model, call the contextual reasoning ability of the large language model, and generate the user's intention. Figure 1 The target database query statement is consistent, thereby completing the conversion from natural language to SQL and obtaining the user query results.

[0012] Furthermore, the natural language semantic representation model for database query is constructed, which takes the natural language query text entered by the user into the database as input and generates a deep semantic vector, including:

[0013] Preprocessing the natural language query text input by the user into the database, and representing the natural language query text as an integer sequence;

[0014] The integer sequence is input into a pre-built natural language semantic representation model for database queries to generate a deep semantic vector.

[0015] Furthermore, the natural language query text input by the user into the database is preprocessed to represent the natural language query text as an integer sequence, including:

[0016] Obtain the natural language query text entered by the user into the database;

[0017] Normalize the acquired natural language query text and perform word segmentation on the natural language query text;

[0018] Based on a pre-built vocabulary that covers all the words in the training set, each word obtained by word segmentation is converted into an index tensor of fixed length to obtain an integer sequence corresponding to the natural language query text entered by the user.

[0019] Furthermore, the step of inputting the integer sequence into a pre-built natural language semantic representation model for database query to generate a semantic vector includes:

[0020] Build a bidirectional LSTM model;

[0021] The bidirectional LSTM model includes:

[0022] The embedding layer is used to convert the input natural language query text into a word vector of a preset dimension through an embedding operation after mapping it to an index tensor through the vocabulary table;

[0023] A bidirectional LSTM layer with a hidden layer dimension of 512, including a forward LSTM for processing word vector sequences from left to right to capture positive contextual dependencies, and a backward LSTM for processing word vector sequences from right to left to capture reverse contextual dependencies.

[0024] Output concatenation layer, used to concatenate the forward and backward hidden states of each time step into a 1024-dimensional vector;

[0025] The fully connected layer is used to take the concatenated vector of the last time step and map it into a 256-dimensional semantic vector as the semantic feature of the natural language query text;

[0026] Use the preprocessed training set to train the constructed bidirectional LSTM model to obtain a bidirectional LSTM model that meets the preset requirements;

[0027] The integer sequence corresponding to the natural language text entered by the user is input into the trained bidirectional LSTM model to generate a semantic vector.

[0028] Furthermore, the semantic vector similarity matching mechanism is based on retrieving the historical query text that is most similar to the semantic vector from the pre-stored training set, including:

[0029] Use the semantic representation model to generate semantic vectors corresponding to all natural language query texts in the training set, and build a semantic vector library for the training set;

[0030] Use the cosine similarity method to compare the natural language query text entered by the user with the semantic vectors of each natural language query text in the training set, and calculate and obtain a similarity array;

[0031] According to the similarity, the historical query text corresponding to the highest similarity is retrieved from the training set.

[0032] Furthermore, the database query statement corresponding to the historical query text is extracted as a semantic guidance prompt, and together with the natural language query text input by the user into the database, constitutes a structured prompt word instruction, including:

[0033] Based on the retrieved historical query text with the highest similarity, the corresponding complete database query statement and associated database schema information are extracted to construct semantic guidance prompts;

[0034] The database query statement is spliced ​​with the natural language query text entered by the user to form a prompt word instruction.

[0035] Furthermore, the construction method of the semantic guidance prompt is:

[0036] {Semantic-Prompt}={Example1-SQL}+{Example2-SQL}+...+{Example k -SQL}+{Schema-Instruction}

[0037] Among them, Semantic-Prompt is the knowledge prompt word of the named entity; Example k -SQL is the "natural language-SQL" mapping pair format of the K-th similar sample; Schema-Instruction is the database schema constraint statement.

[0038] In a second aspect, the present invention provides a large model database query generation system based on semantic matching prompts, comprising:

[0039] The semantic modeling module is used to build a natural language semantic representation model for database queries. It takes the natural language query text entered by the user into the database as input and generates a deep semantic vector.

[0040] Similar sample retrieval module, which is used to retrieve historical query texts with the most similar semantic vectors from the pre-stored training set based on the semantic vector similarity matching mechanism;

[0041] A structured prompt word construction module is used to extract the database query sentence corresponding to the historical query text as a semantic guidance prompt, and form a structured prompt word instruction with the natural language query text input by the user into the database;

[0042] The SQL generation module is used to input the prompt word instructions into the pre-trained large language model, call the context reasoning ability of the large language model, and generate the SQL that matches the user's intention. Figure 1 The target database query statement is consistent, thereby completing the conversion from natural language to SQL and obtaining the user query results.

[0043] In a third aspect, the present invention provides a computer-readable storage medium storing one or more programs, wherein the one or more programs include instructions that, when executed by a computing device, enable the computing device to execute the large model database query generation method based on semantic matching prompts.

[0044] In a fourth aspect, the present invention provides a computing device comprising: one or more processors and a memory, wherein the memory stores one or more programs and is configured to be executed by the one or more processors, and the one or more programs include instructions for executing the large model database query generation method based on semantic matching prompts.

[0045] The present invention has the following advantages due to the adoption of the above technical solution:

[0046] 1. Improve the ability to understand natural language semantics for complex scenarios: This invention uses a bidirectional LSTM semantic encoder and a historical query pattern matching mechanism to deeply analyze the complex semantics in the user's natural language (such as nested conditions and fuzzy references), significantly improving the accuracy of SQL generation logic and effectively solving the generation bias problem caused by semantic ambiguity in traditional methods.

[0047] 2. Enhance the reliability and standardization of generated results: This invention injects domain knowledge based on a structured prompt word mechanism and combines it with a grammar verification module to significantly reduce the probability of large language models generating illegal or redundant SQL statements, ensuring that the output results strictly comply with grammatical specifications and business logic requirements.

[0048] 3. Achieve efficient and low-cost cross-domain adaptation capabilities: This invention supports rapid adaptation to new database models or business terminology changes through a lightweight semantic adaptation model, reducing the time and resource consumption caused by traditional methods due to data labeling and model reconstruction, and significantly improving system scalability.

[0049] Therefore, the present invention can be widely applied to the intersection of artificial intelligence and database technology. BRIEF DESCRIPTION OF THE DRAWINGS

[0050] Various other advantages and benefits will become apparent to those skilled in the art upon reading the detailed description of the preferred embodiment below. The accompanying drawings are for illustration purposes only and are not to be considered as limiting the present invention. Throughout the drawings, the same reference numerals are used to denote the same components. In the drawings:

[0051] Figure 1 It is a flowchart of a large model database query generation method based on semantic matching prompts provided in an embodiment of the present invention. DETAILED DESCRIPTION

[0052] To make the objectives, technical solutions, and advantages of the embodiments of the present invention more clear, the technical solutions of the embodiments of the present invention will be clearly and completely described below in conjunction with the accompanying drawings of 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 described embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field are within the scope of protection of the present invention.

[0053] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, they indicate the presence of features, steps, operations, devices, components and / or combinations thereof.

[0054] To address the problems of insufficient semantic understanding, easy deviation from the original meaning or grammatical errors, high reliance on training data, and weak generalization capabilities in traditional natural language-to-SQL conversion methods, some embodiments of the present invention propose a large-model database query generation method based on semantic matching prompts, including semantic encoding and similarity retrieval, prompt word construction, and SQL generation processes. The semantic encoding and similarity retrieval process uses a bidirectional LSTM model to semantically encode natural language sentences and generate semantic vectors. The process then searches for the most semantically similar query statements in the training set for the user-entered natural language query text. The prompt word construction and SQL generation process obtains database query statements corresponding to similar query statements as prompt words, concatenates these query statements with the user input, and then inputs them into a pre-trained large language model, guiding the large language model to generate semantically consistent SQL statements. The advantage of the present invention is that the semantic vectors generated by the bidirectional LSTM model can accurately capture the deep semantics of natural language sentences, and the semantic similarity retrieval mechanism can accurately locate the samples in the training set that are most similar to the user input semantics, providing a strong basis for the construction of prompt words; the splicing combination of prompt words and user input can effectively guide the large language model to generate accurate and semantically consistent SQL statements, thereby improving the accuracy, stability and generalization ability of natural language to SQL conversion.

[0055] Correspondingly, in other embodiments of the present invention, a large model database query generation system, device and medium based on semantic matching prompts are provided.

[0056] Example 1

[0057] like Figure 1 As shown, the present invention provides a method for generating a query for a large model database based on semantic matching prompts, comprising the following steps:

[0058] 1) Build a natural language semantic representation model for database queries, taking the natural language query text entered by the user into the database as input and generating a deep semantic vector;

[0059] 2) Based on the semantic vector similarity matching mechanism, the historical query text with the most similar semantic vector is retrieved from the pre-stored training set;

[0060] 3) Extracting the database query sentence corresponding to the historical query text as a semantic guidance prompt, and forming a structured prompt word instruction together with the natural language query text input by the user into the database;

[0061] 4) Input the prompt word instruction into the pre-trained large language model, call the contextual reasoning ability of the large language model, and generate Figure 1 The target database query statement is consistent, thereby completing the conversion from natural language to SQL and obtaining the user query results.

[0062] Furthermore, the above step 1) includes the following steps:

[0063] 1.1) Preprocessing the natural language query text input by the user into the database and representing the natural language query text as an integer sequence;

[0064] 1.2) Input the integer sequence into a pre-built natural language semantic representation model for database queries to generate a deep semantic vector.

[0065] Furthermore, the above step 1.1) includes the following steps:

[0066] 1.1.1) Obtaining natural language query text entered by the user into the database;

[0067] 1.1.2) Normalize the acquired natural language query text and perform word segmentation on the natural language query text;

[0068] In this embodiment, the text normalization of natural language query text mainly includes: lowercase conversion (English text is uniformly converted to lowercase, and Chinese text retains its original format), using regular expressions to remove non-alphanumeric characters to filter special symbols and implement word segmentation;

[0069] 1.1.3) Based on a pre-built vocabulary that covers all words in the training set, convert each word obtained from the word segmentation process into a fixed-length index tensor, resulting in a sequence of integers (tensor) corresponding to the natural language query text entered by the user.

[0070] In this embodiment, when converting the vocabulary into an index tensor of fixed length, the insufficient part is filled according to the preset rules, and the excess part is truncated. The natural language query text input by the user can be converted into a numerical form that can be processed by the model. The truncation and filling operations can also control the input sequence length to ensure the processing efficiency of the subsequent semantic representation model.

[0071] Furthermore, in step 1.2) above, this embodiment uses a bidirectional LSTM model as the semantic representation model, whose input is the word vector sequence corresponding to the natural language query text, and the output is a deep semantic vector. The bidirectional LSTM model is trained using the cross-entropy function to align the semantic vector of the natural language query text with the semantic vector of the corresponding database query statement in the vector space. That is, when training the bidirectional LSTM model, the training target / supervisory signal of the LSTM training process is the SQL statement corresponding to the natural language query text in the training set. The model learns the semantic association between the natural language query text and the SQL statement, and uses methods such as contrastive learning to optimize the generation of the semantic vector so that it can be aligned with the semantics of the SQL statement.

[0072] Specifically, the method includes the following steps:

[0073] 1.2.1) Build a bidirectional LSTM model.

[0074] In this embodiment, the bidirectional LSTM model includes:

[0075] The embedding layer is used to convert the input natural language query text into a 100-dimensional word vector after mapping it to an index tensor through the vocabulary. For example, if "customer" is mapped to index 56 in the vocabulary, the corresponding word vector is embedded

[56] = [0.12, -0.45, ..., 0.78] (100 dimensions).

[0076] A bidirectional LSTM layer (nn.LSTM) is used. The hidden layer dimension is set to 512. The forward LSTM is used to process the word vector sequence from left to right to capture positive contextual dependencies. The backward LSTM is used to process the word vector sequence from right to left to capture reverse contextual dependencies.

[0077] The output concatenation layer is used to concatenate the forward and backward hidden states of each time step into a 1024-dimensional vector (512+512).

[0078] The fully connected layer is used to take the concatenated vector (lstm_out[:,-1,:]) of the last time step and map it to a 256-dimensional semantic vector through nn.Linear(1024,256) as the semantic feature of the natural language query text.

[0079] 1.2.2) Use the preprocessed training set to train the constructed bidirectional LSTM model to obtain a bidirectional LSTM model that meets the preset requirements.

[0080] The preprocessing method of the training set is the same as steps 1.1.1) to 1.1.3), and will not be described in detail in the present invention. During the training process, the bidirectional LSTM model learns to map natural language sentences to their corresponding semantic vectors.

[0081] 1.2.3) Input the integer sequence corresponding to the natural language text entered by the user into the trained bidirectional LSTM model to generate a deep semantic vector for subsequent similarity calculation.

[0082] Furthermore, the above step 2) includes the following steps:

[0083] 2.1) Use the bidirectional LSTM model to generate semantic vectors corresponding to all natural language query texts in the training set and build a semantic vector library for the training set.

[0084] Specifically, first, all natural language query texts in the training set are loaded, and steps 1.1.1) to 1.1.4) are repeated for each natural language query text to perform preprocessing and convert it into an integer sequence (tensor); then, each integer sequence is input into the trained 4-layer 512-dimensional bidirectional LSTM model to obtain the 256-dimensional semantic vector of each natural language query text, and the semantic vector library of the training set is constructed.

[0085] 2.2) Use the cosine similarity method to compare the semantic vectors of the user-entered natural language query text with the semantic vectors of each natural language query text in the training set, and calculate and obtain a similarity array. The closer the similarity value is to 1, the more similar the semantics are.

[0086] The formula used to calculate similarity is the cosine similarity formula, which is used to measure the similarity between two vectors. The specific formula is as follows:

[0087] similarity=cos(θ)=A·B / ||A||·||B||

[0088] Where A represents the semantic vector of the user input text; B represents the semantic vector of a text in the training set; A·B represents the dot product of vector A and vector B; ||A|| and ||B|| represent the modulus (i.e., the length of the vector) of vector A and vector B, respectively.

[0089] 2.3) Based on the similarity, the historical query text corresponding to the highest similarity is retrieved from the training set to provide basic data for the subsequent prompt word construction.

[0090] Furthermore, the above step 3) includes the following steps:

[0091] 3.1) Based on the retrieved historical query text with the highest similarity, extract the corresponding complete database query statement and associated database schema information, and construct semantic guidance prompts;

[0092] The database schema information refers to the database structure description related to the historical query text, including key information such as table name, field name, field type, primary key, foreign key, etc. This information is used to guide the model to generate SQL statements that conform to the actual structure and constraints of the database;

[0093] 3.2) The database query statement is concatenated with the natural language query text input by the user to form a prompt word instruction, which is used to guide the large language model to generate the final SQL statement.

[0094] Furthermore, in the above step 3.1), the construction method of the semantic guidance prompt is:

[0095] {Semantic-Prompt}={Example1-SQL}+{Example2-SQL}+...+{Example k -SQL}+{Schema-Instruction}

[0096] Among them, Semantic-Prompt is the knowledge prompt word of the named entity; Example k -SQL is the "natural language-SQL" mapping pair format of the K-th similar sample (input: [text] → SQL: [statement]); Schema-Instruction is the database schema constraint statement (e.g., using the table name customer, with fields including id, name, and address).

[0097] Furthermore, the above step 3.2) includes the following steps:

[0098] 3.2.1) Concatenate the natural language query text entered by the user to the end of the database query statement to form a structured prompt word instruction that conforms to the input specifications of the large language model;

[0099] When splicing, use the following specific format:

[0100] User question: [Natural language query text entered by the user]

[0101] Example SQL 1: [SQL statement 1 corresponding to similarity query]

[0102] Example SQL 2: [SQL statement 2 corresponding to similar query] ...

[0104] Generate SQL:

[0105] 3.2.2) Add domain expert role settings to the generated prompt word instructions based on actual needs.

[0106] In this embodiment, to further improve the accuracy and professionalism of generated SQL statements, domain expert role settings can be added to the prompt word instructions. For example, a large language model can be designated to play the role of an expert in the database field and follow specific SQL generation rules.

[0107] In this embodiment, the final prompt word instruction is expressed as:

[0108] {Final-Instruction}={User-Input}+{Semantic-Prompt}+{Expert-Constraint}

[0109] Final-Instruction is a structured prompt; User-Input is the user's original natural language query (which must be preprocessed by lowercase conversion and filtering out special symbols); and Expert-Constraint is an expert-generated constraint (for example, prohibiting the use of GROUP BY and requiring a WHERE clause).

[0110] Furthermore, the present invention further comprises the following steps:

[0111] 5) Update the training set in real time based on the verified natural language query text and the corresponding database query statement, and fine-tune the LSTM model parameters.

[0112] Example 2

[0113] To validate the effectiveness of the proposed method, this example uses a test dataset containing 1,000 natural language-SQL pairs and evaluates them based on three dimensions: generation accuracy, semantic consistency, and execution reliability. Specifically, the F1 score, semantic similarity score, and Sentence-BERT score are used for comprehensive judgment. The comparison methods are: Baseline Method 1: using a pure LSTM model to transform SQL statements; Baseline Method 2: directly using a large model to generate SQL without prompt optimization mechanisms; and the proposed method: a generation method based on bidirectional LSTM semantic retrieval and dynamic prompt injection.

[0114] The scores of the method in this embodiment and the baseline method are shown in Table 1. As can be seen from Table 1, the score generated by the method in this embodiment is higher than the scores of Baseline Method 1 and Baseline Method 2, indicating that the method in this embodiment can effectively improve the accuracy, stability, and generalization ability of large language models in database query generation tasks.

[0115] Table 1 Scoring table of the method in this embodiment and other methods

[0116]

[0117] Example 3

[0118] The above-mentioned embodiment 1 provides a large model database query generation method based on semantic matching prompts. Correspondingly, this embodiment provides a large model database query generation system based on semantic matching prompts. The system provided by this embodiment can implement the large model database query generation method based on semantic matching prompts of embodiment 1, and the system can be implemented by software, hardware, or a combination of software and hardware. For example, the system may include integrated or separate functional modules or functional units to execute the corresponding steps in each method of embodiment 1. Since the system of this embodiment is basically similar to the method embodiment, the process described in this embodiment is relatively simple, and the relevant parts can be referred to the partial description of embodiment 1. The embodiment of the system provided in this embodiment is only illustrative.

[0119] The large model database query generation system based on semantic matching prompts provided in this embodiment includes:

[0120] The semantic modeling module is used to build a natural language semantic representation model for database queries. It takes the natural language query text entered by the user into the database as input and generates a deep semantic vector.

[0121] Similar sample retrieval module, which is used to retrieve historical query texts with the most similar semantic vectors from the pre-stored training set based on the semantic vector similarity matching mechanism;

[0122] A structured prompt word construction module is used to extract the database query sentence corresponding to the historical query text as a semantic guidance prompt, and form a structured prompt word instruction with the natural language query text input by the user into the database;

[0123] The SQL generation module is used to input the prompt word instructions into the pre-trained large language model, call the context reasoning ability of the large language model, and generate the SQL that matches the user's intention. Figure 1 The target database query statement is consistent, thereby completing the conversion from natural language to SQL and obtaining the user query results.

[0124] Example 4

[0125] This embodiment provides a processing device corresponding to the large model database query generation method based on semantic matching prompts provided in this embodiment 1. The processing device can be a processing device for a client, such as a mobile phone, laptop computer, tablet computer, desktop computer, etc., to execute the method of embodiment 1.

[0126] The processing device includes a processor, memory, a communication interface, and a bus. The processor, memory, and communication interface are connected via the bus to facilitate communication between them. The memory stores a computer program executable on the processor. When the processor executes the computer program, it executes the method for generating a large model database query based on semantic matching prompts provided in Example 1.

[0127] Preferably, the memory may be a high-speed random access memory (RAM), and may also include a non-volatile memory, such as at least one disk memory.

[0128] Preferably, the processor may be a central processing unit (CPU), a digital signal processor (DSP), or other general-purpose processors of various types, which are not limited here.

[0129] Example 5

[0130] The large model database query generation method based on semantic matching prompts of this embodiment 1 can be specifically implemented as a computer program product. The computer program product may include a computer-readable storage medium on which computer-readable program instructions are loaded for executing the large model database query generation method based on semantic matching prompts described in this embodiment 1.

[0131] Computer readable storage media can be tangible devices that hold and store instructions used by instruction execution devices. Computer readable storage media can be, for example, but not limited to, electronic storage devices, magnetic storage devices, optical storage devices, electromagnetic storage devices, semiconductor storage devices, or any combination thereof.

[0132] It will be understood by those skilled in the art that embodiments of the present invention may be provided as methods, systems, or computer program products. Thus, the present invention may take the form of an entirely hardware embodiment, an entirely software embodiment, or an embodiment combining software and hardware. Furthermore, the present invention may take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to magnetic disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.

[0133] The present invention is described with reference to flowcharts and / or block diagrams of methods, devices (systems), and computer program products according to embodiments of the present invention. It should be understood that each process and / or block in the flowcharts and / or block diagrams, as well as combinations of processes and / or blocks in the flowcharts and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to produce a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowcharts and / or block diagrams. Figure 1 a process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0134] These computer program instructions may also be stored in a computer readable memory that can direct a computer or other programmable data processing device to work in a specific manner, so that the instructions stored in the computer readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 a process or multiple processes and / or boxes Figure 1 The function specified in one or more boxes.

[0135] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operational steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing the instructions executed on the computer or other programmable device for implementing the process. Figure 1 a process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0136] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and not to limit it. Although the present invention has been described in detail with reference to the above embodiments, ordinary technicians in the field should understand that the specific implementation methods of the present invention can still be modified or replaced by equivalents. Any modification or equivalent replacement that does not depart from the spirit and scope of the present invention should be covered by the scope of protection of the claims of the present invention.

Claims

1. A method for generating queries for a large model database based on semantic matching prompts, characterized in that: The following steps are involved: Build a natural language semantic representation model for database queries, taking the natural language query text entered by the user into the database as input and generating a semantic vector; Based on the semantic vector similarity matching mechanism, the historical query text with the most similar semantic vector is retrieved from the pre-stored training set; Extract the database query sentence corresponding to the historical query text as a semantic guidance prompt, and form a structured prompt word instruction with the natural language query text entered by the user into the database; The prompt word instruction is input into the pre-trained large language model, and the contextual reasoning capability of the large language model is called to generate a target database query statement consistent with the user's intention, thereby completing the conversion from natural language to SQL and obtaining the user query result.

2. A method for generating queries for a large model database based on semantic matching prompts according to claim 1, characterized in that: The natural language semantic representation model for database query is constructed, which takes the natural language query text entered by the user into the database as input and generates a deep semantic vector, including: Preprocessing the natural language query text input by the user into the database, and representing the natural language query text as an integer sequence; The integer sequence is input into a pre-built natural language semantic representation model for database queries to generate a deep semantic vector.

3. A method for generating queries for a large model database based on semantic matching prompts as claimed in claim 2, characterized in that: The preprocessing of the natural language query text input by the user into the database, and representing the natural language query text as an integer sequence, includes: Obtain the natural language query text entered by the user into the database; Normalize the acquired natural language query text and perform word segmentation on the natural language query text; Based on a pre-built vocabulary that covers all the words in the training set, each word obtained by word segmentation is converted into an index tensor of fixed length to obtain an integer sequence corresponding to the natural language query text entered by the user.

4. A method for generating queries for a large model database based on semantic matching prompts as claimed in claim 2, characterized in that: The step of inputting the integer sequence into a pre-built natural language semantic representation model for database query to generate a semantic vector includes: Build a bidirectional LSTM model; The bidirectional LSTM model includes: The embedding layer is used to convert the input natural language query text into a word vector of a preset dimension through an embedding operation after mapping it to an index tensor through the vocabulary table; A bidirectional LSTM layer with a hidden layer dimension of 512, including a forward LSTM for processing word vector sequences from left to right to capture positive contextual dependencies, and a backward LSTM for processing word vector sequences from right to left to capture reverse contextual dependencies. Output concatenation layer, used to concatenate the forward and backward hidden states of each time step into a 1024-dimensional vector; The fully connected layer is used to take the concatenated vector of the last time step and map it into a 256-dimensional semantic vector as the semantic feature of the natural language query text; Use the preprocessed training set to train the constructed bidirectional LSTM model to obtain a bidirectional LSTM model that meets the preset requirements; The integer sequence corresponding to the natural language text entered by the user is input into the trained bidirectional LSTM model to generate a semantic vector.

5. The method for generating a query for a large model database based on semantic matching prompts according to claim 1, characterized in that: The semantic vector similarity matching mechanism is based on retrieving the historical query text with the most similar semantic vector from the pre-stored training set, including: Use the semantic representation model to generate semantic vectors corresponding to all natural language query texts in the training set, and build a semantic vector library for the training set; Use the cosine similarity method to compare the natural language query text entered by the user with the semantic vectors of each natural language query text in the training set, and calculate and obtain a similarity array; According to the similarity, the historical query text corresponding to the highest similarity is retrieved from the training set.

6. The method for generating a query for a large model database based on semantic matching prompts according to claim 1, characterized in that: The database query statement corresponding to the historical query text is extracted as a semantic guidance prompt, and together with the natural language query text input by the user into the database, a structured prompt word instruction is formed, including: Based on the retrieved historical query text with the highest similarity, the corresponding complete database query statement and associated database schema information are extracted to construct semantic guidance prompts; The database query statement is spliced ​​with the natural language query text entered by the user to form a prompt word instruction.

7. A method for generating queries for a large model database based on semantic matching prompts as claimed in claim 6, characterized in that: The construction method of the semantic guidance prompt is: {Semantic-Prompt}={Example1-SQL}+{Example2-SQL}+...+{Example k -SQL}+{Schema-Instruction} Among them, Semantic-Prompt is the knowledge prompt word of the named entity; Example k -SQL is the "natural language-SQL" mapping pair format of the K-th similar sample; Schema-Instruction is the database schema constraint statement.

8. A large model database query generation system based on semantic matching prompts, characterized in that: include: The semantic modeling module is used to build a natural language semantic representation model for database queries. It takes the natural language query text entered by the user into the database as input and generates a deep semantic vector. Similar sample retrieval module, which is used to retrieve historical query texts with the most similar semantic vectors from the pre-stored training set based on the semantic vector similarity matching mechanism; A structured prompt word construction module is used to extract the database query sentence corresponding to the historical query text as a semantic guidance prompt, and form a structured prompt word instruction with the natural language query text input by the user into the database; The SQL generation module is used to input prompt word instructions into the pre-trained large language model, call the contextual reasoning capability of the large language model, and generate target database query statements consistent with user intentions, thereby completing the conversion from natural language to SQL and obtaining user query results.

9. A computer-readable storage medium storing one or more programs, characterized in that: The one or more programs include instructions that, when executed by a computing device, cause the computing device to perform any one of the methods of claims 1 to 7 .

10. A computing device, characterized in that include: One or more processors and a memory, wherein the memory stores one or more programs and is configured to be executed by the one or more processors, wherein the one or more programs include instructions for executing any one of the methods according to claims 1 to 7.

Citation Information

Cited By

  • Method, system and device for generating reinsurance contract text based on large language model and medium

    CN121032687A