A knowledge retrieval enhancement generation method and system based on large language model
By extracting key prompts for query questions in knowledge retrieval enhanced generation technology and combining it with document retrieval, filtering and sorting text fragments, the problems of low retrieval efficiency and high resource consumption in the existing technology are solved, and more accurate and natural answers are achieved. generate.
Patent Information
- Application Number
- CN202410853156.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-28
- Publication Date
- 2024-09-13
- Estimated Expiration
- 2044-06-28
AI Technical Summary
Existing knowledge retrieval enhancement generation technology is difficult to accurately retrieve the most relevant documents and text blocks when faced with a large number of relevant documents, resulting in increased system complexity and computing resource consumption, and cannot effectively solve the problem of long tail phenomena and insufficient knowledge coverage.
By converting the query question text into a target query question, key prompt words are extracted, and the documents to be matched are retrieved in the user's private knowledge base based on the enhanced document retrieval model. The key prompt words are matched with the document to filter out relevant text fragments and sort them. Post-input text generation augmentation model to generate answer text.
It achieves more accurate and comprehensive answer generation, reduces system complexity and computing resource consumption, improves the processing efficiency of documents with high similarity, and enhances model generation capabilities.
Smart Images

Figure CN118394890B
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a knowledge retrieval enhancement generation method based on a large language model and a system thereof. Background Art
[0002] Knowledge retrieval enhanced generation is a technical framework that combines retrieval technology and generative models, aiming to improve the performance of natural language processing (NLP) tasks, especially for open domain dialogue, question-answering systems, and text generation. The core idea of knowledge retrieval enhanced generation is to use a large amount of external knowledge or information to enhance the generation ability of the model, overcoming the problems that traditional end-to-end generation models may face, such as insufficient knowledge coverage and poor handling of long-tail phenomena.
[0003] In the existing technology, knowledge retrieval enhancement generation mainly consists of a retriever, a pre-trained language model, and a generator. The retriever is used to retrieve relevant information from a large-scale document library, a knowledge base, or an embedded index of a pre-trained language model; the pre-trained language model provides a strong foundation for semantic understanding. Through pre-training, the model learns rich language representations, which can be used to calculate the similarity between documents or sentences, thereby selecting the most relevant candidate information in the retrieval stage; the generator generates coherent and accurate responses or answers based on the retrieved knowledge, rather than just copying the retrieved fragments.
[0004] The main problem that currently restricts knowledge retrieval enhancement generation is that when faced with a large number of related documents, the retriever cannot accurately retrieve the most relevant documents and text blocks by simply calculating the similarity of documents or text blocks. If text blocks with extremely high similarity are uniformly input into the pre-trained large language model and generator for processing, the system complexity and computing resource consumption will be multiplied. Therefore, how to retrieve a large number of highly similar and related documents and text blocks, and efficiently execute the generator to process text blocks with high similarity is the key to further improving the pre-trained large language model to solve problems such as hallucinations and long tail phenomena. Summary of the invention
[0005] The present application provides a knowledge retrieval enhancement generation method and system based on a large language model, which are used to provide more accurate and comprehensive answers, and the generated answer text is more natural and fluent.
[0006] The technical solution of this application to solve the above technical problems is as follows:
[0007] A knowledge retrieval enhancement generation method based on a large language model, comprising:
[0008] Convert the received query question text into a target query question;
[0009] Splitting the target query question and extracting key prompt words of the target query question;
[0010] Retrieving a set of documents to be matched in the user's private knowledge base according to the target query question based on the document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched;
[0011] Based on the relevance between each of the to-be-matched documents and the target query question, a target document is obtained; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments;
[0012] Based on matching the key prompt word with the target document, a target text segment in the target document is obtained; the matching degree between the target text segment and the key prompt word is greater than a second threshold;
[0013] Sorting the target text segments to obtain a sequence of sorted text segments;
[0014] The target query question and the sorted text fragment sequence are input as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on a large language model.
[0015] The present application also provides a knowledge retrieval enhancement generation system based on a large language model, comprising:
[0016] A conversion module, used for converting the received query text into a target query question;
[0017] A keyword extraction module is used to split the target query question and extract the key prompt words of the target query question;
[0018] A document retrieval module, configured to retrieve a set of documents to be matched in a user's private knowledge base according to the target query question based on a document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched;
[0019] A document matching module, configured to obtain a target document based on the relevance between each of the to-be-matched documents and the target query question; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments;
[0020] A text segment matching module, used for matching the target document based on the key prompt word to obtain a target text segment in the target document; the matching degree between the target text segment and the key prompt word is greater than a second threshold;
[0021] A text segment sorting module, used to sort the target text segments to obtain a sorted text segment sequence;
[0022] The answer text generation module is used to input the target query question and the sorted text fragment sequence as an input set into the text generation enhancement model to obtain the answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on a large language model.
[0023] The present application also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the knowledge retrieval enhancement generation method based on a large language model as described in any one of the above items is implemented.
[0024] The present application also provides a non-transitory computer-readable storage medium having a computer program stored thereon, and when the computer program is executed by a processor, the method for enhancing knowledge retrieval and generation based on a large language model described above is implemented.
[0025] The present application also provides a computer product having a computer program stored thereon, which, when executed by a processor, implements any of the above-mentioned knowledge retrieval enhancement generation methods based on a large language model.
[0026] The embodiment of the present application converts a query question text into a target query question; extracts key prompt words of the target query question; retrieves a set of documents to be matched in a knowledge base according to the target query question based on a document retrieval enhancement model; obtains a target document based on the degree of relevance of each document to be matched with the target query question; matches the target document based on the key prompt words to obtain a target text fragment; sorts the target text fragments to obtain a sorted text fragment sequence; inputs the target query question and the sorted text fragment sequence as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model. Therefore, combining document retrieval and language generation technology can provide more accurate and comprehensive answers. At the same time, a large language model is used to make the generated answer text more natural and fluent. BRIEF DESCRIPTION OF THE DRAWINGS
[0027] Figure 1 is a flow chart of the knowledge retrieval enhancement generation method based on a large language model provided by the present application;
[0028] Figure 2 It is a schematic diagram of the execution flow of the knowledge retrieval enhancement generation method provided in an embodiment of the present application;
[0029] Figure 3 is a structural diagram of a knowledge retrieval enhancer provided in an embodiment of the present application;
[0030] Figure 4 Schematic diagram of a text feature similarity calculation method provided in an embodiment of the present application;
[0031] Figure 5 is a structural diagram of a question prompt word disassembler provided in an embodiment of the present application;
[0032] Figure 6 It is a schematic diagram of a method for calculating similarity of question decomposition words provided in an embodiment of the present application;
[0033] Figure 7 is a structural diagram of a text generation enhancer provided in an embodiment of the present application;
[0034] Figure 8 It is a structural diagram of the knowledge retrieval enhancement generation system based on a large language model provided by this application;
[0035] Fig. 9 It is a structural schematic diagram of the electronic device provided by this application. DETAILED DESCRIPTION
[0036] The following will be combined with the drawings in the embodiments of the present application to clearly and completely describe the technical solutions in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, not all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of this application.
[0037] In the description of this application, the terms "first" and "second" are used for descriptive purposes only and should not be understood as indicating or implying relative importance or implicitly indicating the number of technical features indicated. Thus, a feature defined as "first" or "second" may explicitly or implicitly include one or more of the features. In the description of this application, "plurality" means two or more, unless otherwise clearly and specifically defined.
[0038] In the description of the present application, the term "for example" is used to mean "used as an example, illustration or description". Any embodiment described as "for example" in the present application is not necessarily to be interpreted as being more preferred or advantageous than other embodiments. In order to enable any technician in the field to implement and use the present application, the following description is given. In the following description, details are listed for the purpose of explanation. It should be understood that a person of ordinary skill in the art can recognize that the present application can be implemented without using these specific details. In other examples, well-known structures and processes will not be elaborated in detail to avoid unnecessary details to obscure the description of the present application. Therefore, the present application is not intended to be limited to the embodiments shown, but is consistent with the widest range of principles and features disclosed in the present application.
[0039] Reference Figure 1 , Figure 1 : is a flowchart of the knowledge retrieval enhancement generation method based on a large language model provided in an embodiment of the present application, specifically including steps 10 to 70:
[0040] Step 10, converting the received query question text into a target query question;
[0041] Step 20, split the target query question and extract key prompt words of the target query question;
[0042] Step 30, based on the document retrieval enhancement model, a set of documents to be matched is retrieved from the user's private knowledge base according to the target query question; the set of documents to be matched includes at least two documents to be matched;
[0043] Step 40, obtaining a target document based on the relevance between each to-be-matched document and the target query question; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments;
[0044] Step 50, matching the target document based on the key prompt word to obtain a target text segment in the target document; the matching degree between the target text segment and the key prompt word is greater than a second threshold;
[0045] Step 60, sorting the target text segments to obtain a sorted text segment sequence;
[0046] Step 70, input the target query question and the sorted text fragment sequence as an input set into the text generation enhancement model to obtain the answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on the large language model.
[0047] Optional, see Figure 2 , Figure 2 This is a schematic diagram of the execution flow of the knowledge retrieval enhancement generation method provided in the embodiment of the present application. In the input stage, the user's query question text (Input) is received and the query question text is converted into a clear question, namely the target query question (Input Question).
[0048] Furthermore, in the document retrieval stage, the document retrieval enhancement model is used to retrieve relevant documents from the user's private knowledge base according to the target query question (Input Question), and the document set to be matched.
[0049] Furthermore, a target document is obtained according to the relevance between each document to be matched and the target query question, wherein the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments, wherein the first threshold is set according to actual conditions.
[0050] Furthermore, the system splits the target query question through the question prompt word decomposition model and extracts the key prompt words of the target query question.
[0051] Furthermore, the target document is matched with the key prompt word to obtain a target text segment in the target document, and the matching degree between the target text segment and the key prompt word is greater than a second threshold, that is, the text segment in the most relevant document is further screened out, wherein the second threshold is set according to actual conditions.
[0052] Furthermore, the target text segments are sorted to obtain a sorted text segment sequence to determine the text segment most relevant to the key prompt word of the target query question.
[0053] Furthermore, in the generation stage, the target query question and the sorted text fragment sequence are input into the text generation enhancement model as an input set, and the answer text output by the text generation enhancement model is provided to the user. The text generation enhancement model is constructed based on the large language model.
[0054] The embodiment of the present application converts a query question text into a target query question; extracts key prompt words of the target query question; retrieves a set of documents to be matched in a knowledge base according to the target query question based on a document retrieval enhancement model; obtains a target document based on the degree of relevance of each document to be matched with the target query question; matches the target document based on the key prompt words to obtain a target text fragment; sorts the target text fragments to obtain a sorted text fragment sequence; inputs the target query question and the sorted text fragment sequence as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model. Therefore, combining document retrieval and language generation technology can provide more accurate and comprehensive answers. At the same time, a large language model is used to make the generated answer text more natural and fluent.
[0055] Further, refer to Figure 3 , Figure 3 This is a structural diagram of the knowledge retrieval enhancer provided in the embodiment of the present application, for the document retrieval enhancement model:
[0056] Optionally, each document to be matched is processed according to the document retrieval enhancement model to convert the document content in each document to be matched into a first document content feature vector, that is, in the document embedding stage, for each document in the document library, the document retrieval enhancement model will perform an embedding operation to convert the document content into a vector form.
[0057] Optionally, feature extraction is performed on the first document content feature vector of each document to be matched according to a preset neural network model to obtain the second document content feature vector of each document to be matched. That is, in the document feature extraction stage, after the document is embedded, the document retrieval enhancement model will use a convolutional neural network (CNN) to further process the document vector to extract more useful feature vectors.
[0058] Optionally, the target query is processed according to the document retrieval enhancement model to convert the target query into a first query feature vector, i.e., the question embedding stage. The input target query passes through an embedding layer to convert the text information of the target query into a vector form. This process usually involves word embedding technology, such as Word2Vec or GloVe.
[0059] Optionally, feature extraction is performed on the first query question feature vector according to the neural network model to obtain a second query question feature vector of the target query question. That is, after the question is embedded, the model will perform feature aggregation operations, which may include methods such as pooling and attention mechanism, in order to extract the most important feature vector from the question vector.
[0060] Optionally, a set of documents to be matched is determined based on the second document content feature vector and the second query question feature vector, specifically: based on a preset model, the similarity value between the second document content feature vector and the second query question feature vector of each document to be matched is calculated; the documents to be matched corresponding to the target similarity value are grouped together to obtain a set of documents to be matched; the target similarity value is a similarity value greater than or equal to a preset threshold, and the preset model is a multi-scale fusion feedforward network, that is, the multi-scale fusion feedforward network will compare the feature vector of the question with the feature vector of each document in the document library, and calculate the similarity between them.
[0061] Further, refer to Figure 4 , Figure 4It is a schematic diagram of the text feature similarity calculation method provided in the embodiment of the present application. The Multi-Scale Fusion Feed-forward Network (MSFN) is a network structure for feature enhancement. It consists of two connected paths: a prompt word feature calculation path and a document feature vector calculation path. The prompt word feature calculation path extracts detail information by performing conventional convolution on a specific input feature channel to generate a feature vector of the same size as the original input feature vector. The document feature vector calculation path uses a larger convolution kernel to capture more global information and generates a feature vector of the same size as the prompt word feature calculation path. The feature vectors of the two paths obtain complementary knowledge through information exchange, thereby improving features at different scales in the network.
[0062] The MSFN model is designed to extract document information based on the prompt words in user questions, and includes two paths: prompt word feature calculation and document vector feature calculation. The document vector calculation path uses conventional 3×3 depth convolution to extract detailed information and generate a feature vector of the same size as the original input feature vector. On the other hand, the prompt word feature calculation path uses a larger 5×5 convolution kernel to capture more global information and generate a feature vector of the same size as the document vector calculation. In order to integrate the prompt word feature and document feature information, the feature vectors of the two branches are fused. The design of the multi-scale fusion feedforward network allows features to be enhanced at multiple scales, which is a key advantage of MSFN over other architectures. By exchanging information between prompt word features and document vector features, MSFN is able to improve features at different scales and improve document retrieval enhancement.
[0063] Further, refer to Figure 5 , Figure 5 This is a structural diagram of the question prompt word disassembler provided in an embodiment of the present application, for the question prompt word disassembly model:
[0064] By calculating and extracting features between prompt words and document context, the question prompt words are decomposed into multiple keywords with the help of NFA mechanism.
[0065] Further, refer to Figure 6 , Figure 6It is a schematic diagram of the problem decomposition word similarity calculation method provided by the embodiment of the present application. NFA is a linear frequency-aware attention mechanism that processes the feature vector X∈R^{C×H×W} through two paths. R^{C×H×W} represents the input feature map, where C is the number of channels (feature depth), and H and W are the number of words and vector dimensions in the word vector matrix, respectively. The first path converts X to generate a query (Query) through a series of convolution operations (including 1×1 and 3×3 deep separable convolutions) from X, and the query Q∈R^{4C×H×W} means that the query vector is expanded 4 times in the channel dimension, keeping the spatial size unchanged. The second path performs a discrete wavelet transform on X, decomposes it into four frequency domains, and generates keys and values through two sets of 1×1 and 3×3 convolutional layers. The key K∈R^{16C×H / 2×W / 2} and the value V∈R^{16C×H / 2×W / 2} indicate a 16-fold expansion in the channel dimension, while the spatial dimension is halved, reflecting the compression of high-frequency information. At the same time, the local context downsampled feature Xc is subjected to an inverse wavelet transform (IDWT), which, according to wavelet theory, can retain every detail of the original input X. The query, key, and value are reshaped into ˆQ∈R^{4C×HW}, ˆK∈R^{HW×4C}, and ˆV∈R^{HW×4C} for matrix multiplication. The introduction of the DWT-convolution-IDWT process implemented in the nonlinear frequency-aware attention mechanism is to capture features at different frequency levels and enhance the model's ability to model local context and global structure. Through this mechanism, the model can more effectively integrate spatial information of different scales, achieving more powerful local context modeling and expanded receptive field.
[0066] Further, refer to Figure 7 , Figure 7 It is a structural diagram of the text generation enhancer provided in the embodiment of the present application. For the text generation enhancement model:
[0067] Question and context: A question (Q) is combined with multiple contexts (C1, C2, ..., CN). The question represents the goal or intention of generation, while the context provides relevant information required for generation. The result of this step is a series of question prompt word-context pairs (Question + context 1, Question + context 2, ..., Question + context N).
[0068] Encoder: Processes the question word-context pairs multiple times to extract deeper information.
[0069] Recurrent Connection: Allows the model to backtrack and utilize previous output information during the generation process.
[0070] Large Language Model Text Generation: These question-context pairs are processed by the encoder and then fed into a large language model, which is responsible for generating the final answer. This answer is generated based on all the information and context provided, and it should be able to answer the original question.
[0071] In one embodiment, the target query question and the sorted text segment sequence are input as an input set into the text generation enhancement model, and after obtaining the answer text output by the text generation enhancement model, the method further includes:
[0072] Get the database to be queried and the query question text for the database entered by the user.
[0073] In this step, the knowledge retrieval enhancement generation system based on the large language model may include a database module, a prompt word engineering module, a knowledge graph construction module, a large language model module, an output module and an error correction module. Among them, the database module can establish a connection with the database to be queried, that is, it can be connected to the database to be queried, and query the database by executing a query statement to obtain a query result. The prompt word engineering module is mainly used to construct the prompt word engineering of the large language model. The knowledge graph construction module is mainly used to read the table and field information in the database to construct the knowledge graph of the database. The large language model module mainly generates query statements based on the text entered by the user and the prompt word engineering. The output module is mainly used to return the query statements generated by the large language model module and the query results of the database module. The error correction module is mainly used to correct the erroneous query statements manually (i.e., professionals), that is, to correct the erroneous query statements generated by the large language model.
[0074] Based on the above description of the knowledge retrieval enhancement generation system based on the large language model, when a certain database needs to be queried, the database to be queried can be connected through the database module, and then the database to be queried can be obtained, and the relevant information of the database can be obtained at the same time. The type of the database to be queried here can be determined according to the actual situation, such as Mysql database, sql database, Oracle database, etc. The relevant information of the database to be queried here may include the relevant information of the database itself and the relevant information of the data table in the database, wherein the relevant information of the database itself may include, for example, the database name, database type, etc., and the relevant information of the data table may include, for example, the table name, table size, table structure definition and other information of the data table.
[0075] In addition, the above-mentioned knowledge retrieval enhancement generation system based on the large language model may also include an acquisition module, which can receive the query question text input by the user on the display interface of the knowledge retrieval enhancement generation system or platform based on the large language model. The query question text is mainly a question or query or search for the database to be queried. Since the database to be queried has been connected through the database module, the text content of the corresponding database can also be reduced in the query question text, that is, the query question text can be a relatively concise text, such as "Please help me query the math scores of all students", which does not need to include query statements and other content, which can reduce the technical threshold of users who use the database for query or search.
[0076] At the same time, the acquisition module can also acquire the database to be queried and related information of the database to be queried from the database module.
[0077] Construct a prompt word project based on the relevant information in the database.
[0078] In this step, after obtaining the relevant information of the database, a prompt word project can be constructed based on the relevant information of the database. The prompt word mainly guides the subsequent large language model to generate more accurate and more realistic query statements according to the prompt word project.
[0079] Here, when constructing the prompt word project through the relevant information of the database, for example, the relevant information of the database can be added to the prompt word project, or the constraint conditions for constraining the generated query statements further determined through the relevant information of the database can be added, such as the format of the query statements generated, the number of the query statements generated, etc., or the dependency relationship of the data tables in the database further determined through the relevant information of the database can be added to the prompt word project. In short, the prompt word project can be constructed through the relevant information of the database.
[0080] The query question text and the prompt word engineering are input into the large language model, and the text is converted into a query statement to determine the target query statement corresponding to the query question text; the above prompt word engineering is used to guide the large language model to generate a query statement according to the prompt word engineering.
[0081] Among them, the emergence of large language models (or large models) has brought about many revolutionary changes. By using more parameters or more complex neural networks, it has achieved amazing results in intelligent question answering, natural language understanding, image recognition, etc. Therefore, this embodiment also uses the large language model in database query to lower the user's technical threshold for database query.
[0082] The large language model in this embodiment, i.e., Large Language Model, is referred to as LLM model for short. The type of the large language model can be specifically set according to the actual situation, such as Qwen series model, Llama2 series model, etc. The large language model is mainly used to convert text into query statements, where the query statements can be, for example, sql query statements, then the function of the large language model here can be, for example, text_to_sql, that is, converting the natural language text in the database field into a structured query statement sql that can be executed in a relational database. In addition, the large language model can be pre-trained, and the training process of the large language model is not described here.
[0083] After obtaining the query text (input) input by the user and the prompt word project (prompt) constructed through relevant information of the database, the query text and the prompt word project can be input into the large language model (or large language model module). The large language model can convert the query text into a query statement based on the information included in the prompt word project, and generate a target query statement corresponding to the query text. The target query statement can be, for example, a sql query statement / sql statement.
[0084] It should be noted that the target query statement is generally a query statement for querying the question text, and the performance of the target query statement is generally optimal, such as the fastest query efficiency or the highest query accuracy.
[0085] Query the database according to the target query statement and determine the query result.
[0086] In this step, after obtaining the target query statement corresponding to the query question text through the large language model module, the target query statement can be transmitted to the output module for output, so that the user can know the currently generated target query statement. At the same time, the large language model module can also input the target query statement into the database module, and execute the target query statement in the connected database to be queried through the database module, so as to query the corresponding content in the target query statement in the database and obtain the query result.
[0087] The above query results may include query failure and query success. If the query fails, the database module may also transmit the query result of the query failure to the output module for output, so that the user can promptly know the query failure and perform the next error correction operation. If the query is successful, the query result may also include the queried data. Similarly, the database module can obtain the queried data and transmit it to the output module for output, so that the user can promptly obtain the queried data for the next data processing operation.
[0088] It should be noted that although the above embodiments are all described by taking database query as an example, in fact, the database can also be searched (similar to query), added, deleted, modified, etc. The specific implementation process of these operations is similar to the implementation process of this embodiment, and will not be described in detail here.
[0089] In this embodiment, by obtaining the database to be queried and the query question text for the database input by the user, and constructing a prompt word project according to the relevant information of the database, the query question and the prompt word project are input into the large language model for text-to-query conversion, and the target query sentence corresponding to the query question text is determined, and then the query is performed in the database according to the target query sentence to determine the query result; the prompt word project is used to guide the large language model to generate a query sentence according to the prompt word project. In this method, the user only needs to input the query question text to obtain the query sentence and query the database, and the query question text is a relatively simple text, so the user does not need to be proficient in the writing technology of the database query sentence, thereby reducing the technical threshold for the user to query the database; at the same time, the method does not require professional database operators to write database query sentences to realize the query of the database, so it can also reduce the workload of professional database operators and improve work efficiency; further, since the prompt word project that can be constructed by the relevant information of the database is added in the process of generating the query sentence by the large language model, the prompt word project can guide the large language model to generate a more accurate and satisfactory query sentence, thereby improving the accuracy of the query result obtained when the database is queried by the query sentence.
[0090] In one embodiment, the above method may further include the following steps:
[0091] If the query result is a query failure, the target query statement is corrected and a new target query statement is determined; the database is requeried based on the target query statement to determine a new query result.
[0092] In this step, if the target query statement generated by the large language model fails when querying the database, it indicates that the target query statement generated by the large language model may be wrong. In this case, the target query statement can be output to a professional who operates the database. The professional can modify the erroneous part of the target query statement, and then input the modified correct query statement into the output module through the error correction module. In this way, the output module can obtain the correct query statement, which is recorded as a new target query statement. In this way, the error correction processing of the erroneous target query statement is realized, and a new target query statement is obtained.
[0093] Afterwards, the output module can input the new target query statement into the database module, and re-execute the new target query statement in the connected database via the database module to obtain a new query result. The new query result is generally a successful query. If the query still fails, error correction can be continued until the query is successful.
[0094] In this embodiment, by correcting the wrong query statements generated by the large language model, the success rate of querying the database can be improved. In addition, since the query statements generated by the large language model will not be very wrong even if they are wrong, only a small amount of modification is needed to obtain the correct query statement during the error correction process, so that professionals do not need to write all the query statements, thereby reducing the workload of professionals and improving work efficiency.
[0095] The following embodiment describes the specific process of constructing a prompt word project of a large language model through relevant information in a database.
[0096] Get the database name of the database and the table structure definition information of the data table in the database.
[0097] In this step, the above-mentioned database module can connect to the database to be queried and ensure that the database connection is normal. After the above-mentioned database module is normally connected to the database to be queried, the database module can read the name of the database (recorded as the database name), and then can also obtain the number of data tables specifically included in the database, the table structure definition information of the data table, etc., and then use this information to build the prompt word project of the large language model. Generally, a data table is composed of three parts: the table name, the fields in the table, and the records in the table. Here, the table structure definition information of the data table is to define the data table file name, determine which fields the data table contains, the field name, field type and width of each field, and other information.
[0098] Construct a prompt word project based on the database name and table structure definition information.
[0099] In this step, after obtaining the name of the connected database and the table structure definition information of the data table, the database module can directly use these two parts of information as the prompt word project, or can also add other information to the prompt word project.
[0100] Taking the example of constructing a prompt word project through database name, table structure definition information and other information, as an optional embodiment, this step may include the following steps:
[0101] According to the data table and the table structure definition information of each data table, triple information for each data table is determined; the triple information includes entities, relations, and dependencies between entities.
[0102] In this step, after obtaining the table structure definition information of each data table, you can obtain the table name, fields in the table, field names, field types and widths of each field in each data table, and then you can create a triple for each data table. The triple can be an entity-relationship-entity triple. Assuming that a student score data table includes field names such as student name, student number, and score, you can build a triple such as score-belongs-student name, or score-belongs-student number.
[0103] By constructing triples for each data table, we can obtain the dependency or association relationship between the fields in each data table, which makes it easier for the subsequent large language model to accurately understand the data table and the fields therein and generate more accurate query statements.
[0104] Based on the triple information of each data table and the dependencies between the data tables, a knowledge graph corresponding to the database is constructed.
[0105] In this step, after obtaining the data tables of the database as described above, triples between the data tables can also be constructed to represent the association or dependency relationship between the data tables, and then the knowledge graph of the database can be generated through the dependency relationship between the data tables and the triple information of each data table. In other words, through the knowledge graph, the dependency relationship between the data tables in the database and the dependency relationship between the fields in each data table can be known, which can facilitate the subsequent large language model to accurately understand the dependency relationship between data tables and the dependency relationship between the fields in the table, and generate more accurate query statements.
[0106] Construct a prompt word project based on the database name, table structure definition information and knowledge graph.
[0107] In this step, as an optional embodiment, the database name, table structure definition information and knowledge graph can be used to determine the prior information of the prompt word project; the constraints of the prompt word project are constructed according to the table structure definition information and the knowledge graph; the above constraints are used to guide the large language model to generate query statements based on the table structure definition information and the knowledge graph.
[0108] In other words, the database name, table structure definition information (also recorded as table structure definition) and knowledge graph (also recorded as knowledge graph relationship) of the database to be queried can all be used as prior information for the prompt word project.
[0109] In addition, the above constraints are set based on the table structure definition information and the knowledge graph, and are mainly set for the number of query statements generated by the large language model, the format of the generated query statements, how to generate query statements, and how to optimize the generated query statements.
[0110] By combining the above-mentioned prior information and constraints, a constructed prompt word project can be obtained. The above-constructed prompt word project (i.e., prompt) can be in the following form:
[0111] prompt={
[0112] Database name: {db_name}
[0113] Table structure definition: {table_info}
[0114] Knowledge graph relationship: {knowledge_graph}
[0115] Constraints:
[0116] 1. Please fully understand the user's intention, understand the dependencies between tables and structures in the tables based on the knowledge graph relationship {knowledge_graph}, and use the given table structure definition to create a syntactically correct SQL statement. If SQL is not required, directly answer the user's question.
[0117] 2. Queries are limited to at most {top_k} results, unless the user specifies that {top_k} in the question.
[0118] 3. You can only use the tables provided in the table structure definition information to generate SQL statements. If you cannot generate SQL statements based on the tables provided in the table structure definition information, please say: "SQL statements cannot be generated based on the provided table structure." You are prohibited from making up your own stories.
[0119] 4. Please check the correctness of the SQL statement and optimize the performance if it is correct.
[0120] 5. Please output as follows:
[0121] {xxx,xxx,xxx
[0122] }
[0123] Among them, "understanding the dependencies between tables and structures in the tables" in the above constraint 1 refers to understanding the dependencies between data tables in the database, and understanding the dependencies between fields in each data table, etc. This constraint 1 and the third constraint are used to instruct the large language model how to generate query statements. "The query is limited to a maximum of {top_k} results, unless the user specifies the {top_k} in the question" in the above constraint 2 refers to the general default output of a set number of SQL statements. If the user specifies the number of generated SQL statements, the specified number of SQL statements can be generated according to the user's needs, that is, the number of query statements generated by the large language model is indicated. The above constraint 4 refers to the fact that when the large language model initially generates multiple accurate query statements for a query question text, it can select a query statement with the best performance for output by optimizing performance, that is, it indicates how the large language model optimizes the generated query statements. The above constraint 5 specifies the format of the generated query statement, that is, it instructs the large language model to output SQL statements in this format.
[0124] In this embodiment, a prompt word project can be constructed by using the database name of the database to be queried and the table structure definition information of the data table, so that the large language model can be guided to better understand the structure of the database and its table and generate more accurate query statements. In addition, when constructing the prompt word project, triples can be constructed through the table structure definition information of the data table to construct a knowledge graph for the database, so that the large language model can fully and accurately understand the specific dependencies between data tables and the specific dependencies between fields in the table, and improve the correctness of the generated query statements. Furthermore, when constructing the prompt word project, constraints for the table structure definition information and the knowledge graph can also be added, so that the large language model can be instructed to generate accurate query statements that meet user needs according to the constraints, avoiding the situation where the large language model generates query statements arbitrarily in some cases, and further improving the accuracy and standardization of the generated query statements.
[0125] As mentioned above, the prompt word project includes constraints on the large language model to generate query statements, and the constraints include content for optimizing the query statements. The following embodiment describes the process of optimizing the query statements by the large language model.
[0126] The query question text and prompt word project are input into the large language model, and the text is converted into query statements to generate multiple candidate query statements.
[0127] In this step, after obtaining the query text and the constructed prompt word project, the query text and the prompt word project can be input into the large language model for text-to-query conversion, generating multiple query sentences, all recorded as candidate query sentences. These multiple candidate query sentences are not output in the large language model first, but are subjected to subsequent performance optimization processing.
[0128] The performance of multiple candidate query statements is optimized according to the constraints in the prompt word project to determine the target query statement corresponding to the query question text; wherein the above constraints include constraints for performance optimization when multiple candidate query statements are correct.
[0129] In this step, the large language model adds a prompt word project built for the database. The constraint of the prompt word project includes the constraint of optimizing performance when multiple candidate query statements are correct, that is, the constraint in point 4 above. Then the large language model can first determine whether each candidate query statement is accurate based on this constraint. If they are all accurate, the performance parameters of each candidate query statement are obtained, and then the performance parameters of each candidate query statement are compared and a candidate query statement with the best performance is selected as the target query statement and output.
[0130] The performance parameters here may be, for example, the query speed and query time of the candidate query statements, etc. By selecting the candidate query statement with the best performance as the final target query statement, the efficiency and accuracy of subsequent database queries through the target query statement can be improved.
[0131] In this embodiment, the performance optimization constraints in the prompt word project are used to optimize the performance of multiple candidate query statements initially generated by the large language model to obtain the target query statement with the best performance, which can improve the efficiency and accuracy of subsequent database queries through the target query statement.
[0132] The knowledge retrieval enhancement generation system based on a large language model provided by the present application is described below. The knowledge retrieval enhancement generation system based on a large language model described below and the knowledge retrieval enhancement generation method based on a large language model described above can be referenced to each other. Figure 8 The structure diagram of the knowledge retrieval enhancement generation system based on the large language model provided in the embodiment of the present application is as follows: Figure 8 As shown, the knowledge retrieval enhancement generation system based on a large language model provided in the embodiment of the present application includes:
[0133] The conversion module 801 is used to convert the received query text into a target query;
[0134] The keyword extraction module 802 is used to split the target query question and extract the key prompt words of the target query question;
[0135] The document retrieval module 803 is used to retrieve a set of documents to be matched in the user's private knowledge base according to the target query question based on the document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched;
[0136] The document matching module 804 is used to obtain a target document based on the relevance between each of the to-be-matched documents and the target query question; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments;
[0137] A text segment matching module 805 is used to match the target document based on the key prompt word to obtain a target text segment in the target document; the matching degree between the target text segment and the key prompt word is greater than a second threshold;
[0138] A text segment sorting module 806 is used to sort the target text segments to obtain a sorted text segment sequence;
[0139] The answer text generation module 807 is used to input the target query question and the sorted text fragment sequence as an input set into the text generation enhancement model to obtain the answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on a large language model.
[0140] The embodiment of the present application converts a query question text into a target query question; extracts key prompt words of the target query question; retrieves a set of documents to be matched in a knowledge base according to the target query question based on a document retrieval enhancement model; obtains a target document based on the degree of relevance of each document to be matched with the target query question; matches the target document based on the key prompt words to obtain a target text fragment; sorts the target text fragments to obtain a sorted text fragment sequence; inputs the target query question and the sorted text fragment sequence as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model. Therefore, combining document retrieval and language generation technology can provide more accurate and comprehensive answers. At the same time, a large language model is used to make the generated answer text more natural and fluent.
[0141] See also Fig. 9 , Fig. 9 Schematic diagram of an embodiment of an electronic device provided in this application. Fig. 9As shown, the embodiment of the present application provides an electronic device 900, including a memory 910, a processor 920, and a computer program 911 stored in the memory 910 and executable on the processor 920. When the processor 920 executes the computer program 911, the following steps are implemented:
[0142] Convert the received query question text into a target query question;
[0143] Splitting the target query question and extracting key prompt words of the target query question;
[0144] Retrieving a set of documents to be matched in the user's private knowledge base according to the target query question based on the document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched;
[0145] Based on the relevance between each of the to-be-matched documents and the target query question, a target document is obtained; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments;
[0146] Based on matching the key prompt word with the target document, a target text segment in the target document is obtained; the matching degree between the target text segment and the key prompt word is greater than a second threshold;
[0147] Sorting the target text segments to obtain a sequence of sorted text segments;
[0148] The target query question and the sorted text fragment sequence are input as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on a large language model.
[0149] It should be noted that in the above embodiments, the description of each embodiment has its own emphasis, and for parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0150] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware. Moreover, the present application may adopt the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program codes.
[0151] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or block in the flowchart and / or block diagram, as well as the combination of the processes and / or blocks in the flowchart and / or block diagram, 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 computer, or other programmable data processing device to generate 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 flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A system that specifies the functions of a box or multiple boxes.
[0152] 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 a product including an instruction system, which is implemented in the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.
[0153] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 The steps for the functions specified in one or more boxes.
[0154] Although the preferred embodiments of the present application have been described, those skilled in the art may make other changes and modifications to these embodiments once they have learned the basic creative concept. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments and all changes and modifications that fall within the scope of the present application.
[0155] Obviously, those skilled in the art can make various changes and modifications to the present application without departing from the spirit and scope of the present application. Thus, if these modifications and variations of the present application fall within the scope of the claims of the present application and their equivalents, the present application is also intended to include these modifications and variations.
Claims
1. A knowledge retrieval enhancement generation method based on a large language model, characterized in that: include: Convert the received query question text into a target query question; Splitting the target query question and extracting key prompt words of the target query question; Retrieving a set of documents to be matched in the user's private knowledge base according to the target query question based on the document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched; Based on the relevance between each of the to-be-matched documents and the target query question, a target document is obtained; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments; Based on matching the key prompt word with the target document, a target text segment in the target document is obtained; the matching degree between the target text segment and the key prompt word is greater than a second threshold; Sorting the target text segments to obtain a sequence of sorted text segments; Inputting the target query question and the sorted text fragment sequence as an input set into a text generation enhancement model to obtain an answer text output by the text generation enhancement model; The text generation enhancement model is constructed based on a large language model; The method further comprises: Acquire a database to be queried and a query question text for the database input by a user; Constructing a prompt word project according to the relevant information of the database; Inputting the query text and the prompt word project into a large language model, performing text-to-query conversion processing, and determining a target query sentence corresponding to the query text; the prompt word project is used to guide the large language model to generate a query sentence according to the prompt word project; A query is performed in the database according to the target query statement to determine a query result.
2. The knowledge retrieval enhancement generation method based on a large language model according to claim 1 is characterized in that: The document retrieval enhancement model retrieves a set of documents to be matched in the user's private knowledge base according to the target query question, including: Processing each of the to-be-matched documents based on the document retrieval enhancement model to convert the document content in each of the to-be-matched documents into a first document content feature vector; Extracting features from the first document content feature vector of each of the documents to be matched based on a preset neural network model to obtain a second document content feature vector of each of the documents to be matched; Processing the target query question based on the document retrieval enhancement model to convert the target query question into a first query question feature vector; Performing feature extraction on the first query question feature vector based on the neural network model to obtain a second query question feature vector of the target query question; The to-be-matched document set is determined based on the second document content feature vector and the second query question feature vector.
3. The knowledge retrieval enhancement generation method based on a large language model according to claim 2 is characterized in that: The step of determining the set of documents to be matched based on the second document content feature vector and the second query question feature vector includes: Calculate the similarity value between the second document content feature vector of each of the to-be-matched documents and the second query question feature vector based on a preset model; The documents to be matched corresponding to the target similarity value are collected to obtain the document collection to be matched; the target similarity value is a similarity value greater than or equal to a preset threshold.
4. The knowledge retrieval enhancement generation method based on a large language model according to claim 3 is characterized in that: The preset model is a multi-scale fusion feedforward network.
5. The knowledge retrieval enhancement generation method based on a large language model according to claim 1 is characterized in that: The step of constructing a prompt word project according to the relevant information in the database includes: Obtaining the database name of the database and table structure definition information of the data table in the database; Determine triple information for each of the data tables according to the data tables and table structure definition information of each of the data tables; Constructing a knowledge graph corresponding to the database according to the triple information of each of the data tables and the dependency relationship between the data tables; A prompt word project is constructed according to the database name, the table structure definition information and the knowledge graph.
6. The knowledge retrieval enhancement generation method based on a large language model according to claim 5 is characterized in that: The step of constructing a prompt word project according to the database name, the table structure definition information and the knowledge graph includes: Determine the prior information of the prompt word project by using the database name, the table structure definition information and the knowledge graph; Constructing constraints of the prompt word project according to the table structure definition information and the knowledge graph; the constraints are used to guide the large language model to generate query statements based on the table structure definition information and the knowledge graph; The step of inputting the query text and the prompt word project into a large language model, converting the text into a query statement, and determining a target query statement corresponding to the query text includes: Inputting the query question text and the prompt word project into a large language model, converting the text into a query statement, and generating a plurality of candidate query statements; Performing performance optimization processing on the multiple candidate query statements according to the constraint conditions in the prompt word project to determine the target query statement corresponding to the query question text; The constraint conditions include constraint conditions for performing performance optimization when the multiple candidate query statements are correct.
7. A knowledge retrieval enhancement generation system based on a large language model according to any one of claims 1 to 6, characterized in that: include: A conversion module, used for converting the received query question text into a target query question; A keyword extraction module is used to split the target query question and extract the key prompt words of the target query question; A document retrieval module, configured to retrieve a set of documents to be matched in a user's private knowledge base according to the target query question based on a document retrieval enhancement model; the set of documents to be matched includes at least two documents to be matched; A document matching module, configured to obtain a target document based on the relevance between each of the to-be-matched documents and the target query question; the relevance between the target document and the target query question is greater than a first threshold, and the target document includes at least two text segments; A text segment matching module, used for matching the target document based on the key prompt word to obtain a target text segment in the target document; the matching degree between the target text segment and the key prompt word is greater than a second threshold; A text segment sorting module, used to sort the target text segments to obtain a sorted text segment sequence; The answer text generation module is used to input the target query question and the sorted text fragment sequence as an input set into the text generation enhancement model to obtain the answer text output by the text generation enhancement model; the text generation enhancement model is constructed based on a large language model.
8. An electronic device, characterized in that: The invention comprises a memory, a processor and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, the knowledge retrieval enhancement generation method based on a large language model as claimed in any one of claims 1 to 6 is implemented.
9. A non-transitory computer-readable storage medium, characterized in that: A computer program is stored thereon, and when the computer program is executed by a processor, the knowledge retrieval enhancement generation method based on a large language model according to any one of claims 1 to 6 is implemented.
