A knowledge base document screening and question answering method and system based on context budget
By using a context-budget-based knowledge base document filtering method to dynamically select and truncate document content, the problem of context window limitations in large language model question answering systems is solved, achieving efficient use of information and improved accuracy of answers.
Patent Information
- Application Number
- CN202610809725.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-05
- Publication Date
- 2026-11-06
AI Technical Summary
Large language models in question-answering systems suffer from problems such as inflexible document selection, information loss, or budget overruns due to context window limitations. Existing technologies, such as fixed-number selection and simple truncation methods, are insufficient and cannot effectively utilize information in the knowledge base.
The knowledge base document filtering method based on context budgeting dynamically selects and truncates document content by calculating the lexical cost of user questions, expected answers, and prompt templates, as well as the maximum context length of the model. This ensures that high-value information is retained within the budget. The method uses similarity calculation and dynamic document selection strategies to generate answers.
It improved information utilization, avoided budget waste and information loss, enhanced the accuracy and completeness of answers, reduced computational overhead, and ensured real-time response speed.
Smart Images

Figure CN123048692A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of natural language processing technology, specifically to a knowledge base document filtering and question answering method and system based on context budgeting. Background Technology
[0002] In recent years, large language models have made significant progress in the field of natural language processing and have been widely used in tasks such as question answering, dialogue, and text generation. However, large language models face a key limitation in practical deployment: their context window has an upper limit, such as 2048 or 4096 words in early models. Although newer large language models support longer contexts with technological advancements, they still face problems such as truncation, performance degradation, or insufficient GPU memory when the context length is exceeded.
[0003] In retrieval-enhanced generation systems, to accurately answer user questions, the system typically needs to retrieve several document fragments related to the question from a knowledge base. These fragments are then concatenated with the user question and input into a large language model to generate the answer. However, documents in the knowledge base vary in length, and the number of relevant documents can be substantial. If all retrieved documents are indiscriminately loaded into the context and fed into the model, it can easily exceed the context length limit of the large language model, leading to a decrease in answer quality or even failure to generate an answer. Conversely, simply truncating or discarding some documents may result in the loss of crucial information.
[0004] In existing technologies, common processing methods include: fixed-quantity selection, truncation based on the total number of terms, or hard truncation relying entirely on search ranking results. These methods have the following shortcomings: First, fixed-quantity selection ignores the differences in document length, and long documents may consume too much budget, excluding other important short documents; second, simple truncation lacks flexibility, and when a single document is too long to fit entirely into the remaining budget, it must either be discarded completely or forcibly retained, resulting in exceeding the budget; third, it does not fully consider the consumption of terms by user questions, expected answers, and the prompt template itself, leading to a discrepancy between budget allocation and actual available capacity. Summary of the Invention
[0005] To overcome the technical shortcomings of existing retrieval enhancement generation systems, such as inflexible document filtering, information loss, or budget overruns due to the limitation of large language model context length, this invention provides a knowledge base document filtering and question answering method and system based on context budget.
[0006] To solve the above problems, the present invention is implemented according to the following technical solution:
[0007] In a first aspect, the present invention provides a knowledge base document filtering and question answering method based on context budgeting, comprising the following steps:
[0008] Receive user questions and obtain the maximum context length of the large language model;
[0009] The available lexical budget for retrieval enhancement generation is calculated based on the number of lexical units in the user question, the number of lexical units in the expected answer, the lexical cost of the prompt template, and the maximum context length.
[0010] The similarity between the user's question and the documents in the knowledge base is calculated to obtain a relevance score for each document. The documents are then sorted in descending order of their scores to obtain a sorted set of documents.
[0011] Calculate the number of lexical units in each document of the sorted document set;
[0012] Based on the available lexical budget and the number of lexical units in each document, dynamic document selection is performed on the sorted document set: each document is traversed sequentially; if the number of lexical units in the current document does not exceed the current remaining budget, the complete content of the document is selected as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than a preset minimum document threshold, the document is truncated, the truncated content is selected as the selected content, and the selection of subsequent documents is terminated; if the current remaining budget is less than the minimum document threshold, the current document is skipped and the traversal continues.
[0013] The selected document content is concatenated and input together with the user's question into the large language model to generate an answer to the user's question.
[0014] In conjunction with the first aspect, the present invention provides a first specific implementation of the first aspect, specifically, the step of calculating the available lexical budget for retrieval enhancement generation based on the number of lexical units in the user question, the number of lexical units in the expected answer, the lexical cost of the prompt template, and the maximum context length specifically includes:
[0015] The user question is segmented into words and its lexical count is calculated;
[0016] The number of lexical units in the expected answer is determined based on a preset default value or a statistical value based on the length of historical answers.
[0017] Calculate the lexical cost of the prompt template based on the preset prompt template string;
[0018] Subtract the number of lexical units in the user question, the number of lexical units in the expected answer, and the lexical cost of the prompt template from the maximum context length to obtain the preliminary budget value;
[0019] Obtain the system's preset knowledge base budget limit, and take the smaller of the initial budget value and the knowledge base budget limit as the final usable term budget for retrieval enhancement.
[0020] In conjunction with the first aspect, the present invention provides a second specific implementation of the first aspect. Specifically, the step of calculating the similarity between the user question and documents in the knowledge base to obtain a relevance score for each document, and sorting them in descending order of score to obtain a sorted document set, specifically includes:
[0021] The user question is converted into a question feature vector, and each document in the knowledge base is converted into a document feature vector. The cosine similarity between the question feature vector and each document feature vector is calculated as the first similarity score.
[0022] The user question is matched with each document for terms, and the BM25 score based on term frequency and inverse document frequency is calculated as the second similarity score.
[0023] The first similarity score and the second similarity score are weighted and summed to obtain the comprehensive relevance score of each document, wherein the weights of the first similarity score and the second similarity score are both preset non-negative values and their sum is 1;
[0024] All documents in the knowledge base are sorted from high to low according to the comprehensive relevance score, resulting in a sorted document set.
[0025] In conjunction with the first aspect, the present invention provides a third specific implementation of the first aspect, specifically, the calculation of the number of lexical units in each document of the sorted document set specifically includes:
[0026] The text content of each document is segmented into words, and the number of tokens obtained after segmentation is counted as the token count of the document.
[0027] Alternatively, character counts can be performed on the text content of each document, and the number of tokens in the document can be estimated based on a preset conversion ratio, wherein the conversion ratio is: for English text, each token corresponds to four characters; for Chinese text, each token corresponds to 1.5 characters.
[0028] In conjunction with the first aspect, the present invention provides a fourth specific implementation of the first aspect. Specifically, the step of concatenating all selected document contents and inputting them together with the user question into the large language model to generate an answer to the user question specifically includes:
[0029] Obtain preset system prompts, which are used to define the response roles and behavioral norms of the large language model;
[0030] Concatenate the contents of all selected documents in the order of selection to obtain the context text;
[0031] Construct user prompt text, which includes the user question and the context text, wherein the context text is placed before or after the user question;
[0032] Combine the system prompt and the user prompt text into a complete input prompt;
[0033] Using the input prompt as input, the text generation interface of the large language model is called to obtain the generated text returned by the large language model, and the generated text is used as the answer to the user's question.
[0034] In conjunction with the first aspect, the present invention provides a fifth specific implementation of the first aspect, specifically, when no document content is selected after traversing the sorted document set, the block retrieval degradation mode is activated;
[0035] In the block retrieval degradation mode, the documents in the knowledge base are divided into multiple semantic blocks according to semantic boundaries, the relevance score of each semantic block to the user's question is calculated, and the semantic blocks are arranged in descending order of the scores;
[0036] Based on the available lexical budget, semantic blocks are selected sequentially until the cumulative number of lexical units is close to but does not exceed the available lexical budget.
[0037] The selected semantic blocks are concatenated and input together with the user's question into the large language model to generate an answer to the user's question.
[0038] In conjunction with the first aspect, the present invention provides a sixth specific implementation of the first aspect, specifically, the step of performing a truncation operation on the document includes:
[0039] Based on the text structure of the current document and / or the type of the user's question, select one of the following truncation strategies to execute:
[0040] Start extracting text from the beginning of the document until the number of tokens extracted reaches the current remaining budget;
[0041] Divide the document according to its natural paragraph boundaries, and select complete paragraphs one by one until adding another complete paragraph would cause the cumulative word count to exceed the current remaining budget;
[0042] The invention first calls a large language model to compress the content of the current document, generating a content summary such that the number of terms in the summary does not exceed the current remaining budget. Secondly, the invention also provides a knowledge base document filtering and question-answering system based on context budget, comprising:
[0043] The user issue receiving module is used to receive user issues.
[0044] The model parameter acquisition module is used to obtain the maximum context length of a large language model;
[0045] The budget calculation module is used to calculate the available lexical budget for retrieval enhancement generation based on the number of lexicals in the user question, the number of lexicals in the expected answer, the lexical cost of the prompt template, and the maximum context length.
[0046] The retrieval and sorting module is used to calculate the similarity between the user's question and the documents in the knowledge base, obtain the relevance score of each document, and sort them in descending order of the scores to obtain a sorted set of documents.
[0047] The document lexical calculation module is used to calculate the number of lexical units in each document in the sorted document set;
[0048] The dynamic document selection module is used to perform dynamic document selection on the sorted document set based on the available lexical budget and the number of lexical units in each document: iterates through each document in turn; if the number of lexical units in the current document does not exceed the current remaining budget, the complete content of the document is selected as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than a preset minimum document threshold, the document is truncated, the truncated content is selected as the selected content, and the selection of subsequent documents is terminated; if the current remaining budget is less than the minimum document threshold, the current document is skipped and the traversal continues.
[0049] The question-and-answer generation module is used to concatenate the content of all selected documents and input them together with the user's question into the large language model to generate an answer to the user's question.
[0050] Compared with the prior art, the beneficial effects of the present invention are:
[0051] By comprehensively considering the lexical cost of user questions, expected answers, and suggestion templates, as well as the model's maximum context length, the lexical budget available for a document is accurately calculated, avoiding budget waste or overruns caused by simple truncation or fixed-number selection. When the remaining budget is insufficient to accommodate the entire document but there is still usable space, the document is truncated instead of being discarded directly. This preserves some highly relevant information while making full use of the remaining budget, improving information utilization. A minimum document threshold is set, and the current document is skipped when the remaining budget is too small, avoiding information fragmentation and invalid input caused by truncating too much content, ensuring the overall effectiveness of the suggestions. Documents are processed in descending order of relevance score, prioritizing the most relevant information, and subsequent traversal is terminated promptly after truncation, reducing computational overhead and improving real-time response speed. Attached Figure Description
[0052] The specific embodiments of the present invention will be further described in detail below with reference to the accompanying drawings, wherein:
[0053] Figure 1 This is a flowchart of a knowledge base document filtering and question answering method based on context budgeting according to the present invention.
[0054] Figure 2 This is a block diagram of an electronic device used to implement embodiments of the present invention;
[0055] In the picture:
[0056] 100 - Electronic device, 101 - Computing unit, 102 - ROM, 103 - RAM, 104 - Bus, 105 - I / O interface, 106 - Input unit, 107 - Output unit, 108 - Storage unit, 109 - Communication unit. Detailed Implementation
[0057] The preferred embodiments of the present invention will be described below with reference to the accompanying drawings. It should be understood that the preferred embodiments described herein are for illustration and explanation only and are not intended to limit the present invention.
[0058] like Figures 1-2 As shown, this invention provides a knowledge base document filtering and question answering method and system based on context budgeting.
[0059] Example 1
[0060] This embodiment provides a knowledge base document filtering and question-answering method based on context budgeting. This method can be executed by computing devices with large language model reasoning capabilities (such as cloud servers, local high-performance workstations, or edge AI devices). This method is particularly suitable for scenarios such as enterprise knowledge base question answering, intelligent customer service, medical literature retrieval, and legal document auxiliary analysis, where the number of knowledge base documents is enormous, their lengths vary significantly, and the context window of the large language model is limited. Through dynamic budget allocation and intelligent truncation, this invention can retain high-value information related to the user's question to the greatest extent possible without exceeding the maximum context length of the model, thereby improving the accuracy and completeness of the generated answer.
[0061] like Figure 1 As shown, the knowledge base document filtering and question answering method based on context budgeting includes the following steps:
[0062] S100: Receive user questions and obtain the maximum context length of the large language model;
[0063] S200: Calculate the available lexical budget for retrieval enhancement generation based on the number of lexical units in the user question, the number of lexical units in the expected answer, the lexical cost of the prompt template, and the maximum context length;
[0064] S300: Calculate the similarity between the user's question and the documents in the knowledge base, obtain the relevance score of each document, and sort them in descending order of the scores to obtain a sorted set of documents;
[0065] S400: Calculate the number of lexical units in each document of the sorted document set;
[0066] S500: Based on the available lexical budget and the number of lexical units in each document, perform dynamic document selection on the sorted document set: sequentially traverse each document; if the number of lexical units in the current document does not exceed the current remaining budget, select the complete content of the document as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than the preset minimum document threshold, perform a truncation operation on the document, select the truncated content as the selected content, and terminate the selection of subsequent documents; if the current remaining budget is less than the minimum document threshold, skip the current document and continue traversing.
[0067] S600: Concatenate all the selected document contents and input them together with the user question into the large language model to generate an answer to the user question.
[0068] Specifically, the present invention provides a detailed description of each of the above steps as follows.
[0069] S100: Receive user questions and obtain the maximum context length of the large language model.
[0070] Specifically, the user question is a natural language string, such as "Please summarize the key risk factors in the company's Q3 2024 financial report." It can be received via HTTP API, command-line input, or a graphical user interface.
[0071] Maximum context length of a large language model This refers to the maximum number of tokens the model can process at once; this value is determined by the model itself. For example, when using GPT-3.5-turbo, Lexicons; when using GPT-4-turbo, Lexical units; when using the open-source LLaMA-2-7B, Lexicon. In this embodiment, this value is obtained by calling the model's configuration interface or reading the local configuration file.
[0072] S200: Calculate the retrieval enhancement to generate a usable lexical budget.
[0073] Retrieval enhancement generates available lexical budget (denoted as ) This represents the number of terms that can be used to carry the content of the knowledge base document after reserving the questions, expected answers, and hint templates provided to the user. The calculation formula is as follows: ;
[0074] in, The number of lexical units for the user's question is calculated using lexicalization tools provided by large language models (such as OpenAI's tiktoken or HuggingFace's AutoTokenizer).
[0075] The expected number of lexical units in the answer can be set based on the historical average answer length or a user-specified value, such as 512 lexical units; it can also be set to a conservative upper limit (such as 1024) to ensure that answer generation is not truncated due to insufficient budget.
[0076] This refers to the fixed terminology cost of the prompt template. The prompt template contains fixed text such as system commands, document formatting, and question markers.
[0077] This step specifically includes the following sub-steps:
[0078] S201: Segment the user question into words and calculate its word count.
[0079] Using the same tokenizer as large language models, such as OpenAI's tiktoken encoder or HuggingFace's AutoTokenizer, the user's question string is converted into a sequence of tokens, and the sequence length is counted as... For example, the user question "Please summarize the main risk factors in the company's third quarter 2024 financial report" was processed by the GPT-4 word segmenter to obtain... Lexical element.
[0080] S202: Determine the number of lexical units in the expected answer based on a preset default value or a statistical value based on the length of historical answers.
[0081] lexical number of the expected answer This is a reserve to ensure that the large language model is not forced to truncate due to insufficient remaining budget when generating answers. There are two ways to determine this:
[0082] Option 1 (Fixed Default): The system presets a conservative value, such as 512 words. This value is suitable for general question-and-answer scenarios and can accommodate answers of medium length.
[0083] Method 2 (Dynamic Statistics): The system maintains records of the number of lexical units in answers generated from historically successful question-and-answer sessions, calculates their mean or median, and multiplies it by a safety factor (e.g., 1.2) to determine the expected number of lexical units in the current question-and-answer session. For example, if the historical average answer length is 380 lexical units, then... Lexical element.
[0084] If a user explicitly requests a long answer (such as "please list in detail"), the system can temporarily increase this value; conversely, for short answer scenarios (such as "yes / no" questions), it can decrease the value to free up more budget for the document.
[0085] S203: Calculate the lexical cost of the prompt template based on the preset prompt template string.
[0086] A prompt template refers to a fixed-format system instruction and document assembly framework, which does not contain user questions or dynamic document content. For example, a template string:
[0087] text
[0088] Please answer the questions based on the following document. If the relevant information is not found in the document, please simply state that you do not know.
[0089] Documents: {documents}
[0090] Question: {question}
[0091] Answer:
[0092] Replace `{documents}` and `{question}` in the template with empty strings to obtain plain, fixed text. Then, use a tokenizer to calculate its word count, denoted as . In this embodiment, the fixed cost of the template is 256 tokens. If the system supports multiple templates (such as different languages or tasks), the cost will be dynamically calculated based on the current configuration.
[0093] S204: Subtract the above three lexical numbers from the maximum context length to obtain the preliminary budget value.
[0094] Calculate the preliminary budget : ;like If the error message is not clear, it means that the model context is insufficient to support the basic question-and-answer process. The system should immediately return an error message and suggest that the user shorten the question, reduce the expected answer length, or switch to a model with a larger context.
[0095] S205: Obtain the system's preset knowledge base budget limit, and take the smaller of the preliminary budget value and the knowledge base budget limit as the final retrieval enhancement generation usable term budget.
[0096] Knowledge base budget cap (denoted as) This is a system-level configuration parameter used to prevent excessive accumulation of retrieved documents from crowding out the lexical space needed for answer generation, or to avoid excessively long prompts leading to a decrease in model generation quality. In this embodiment, the default setting is used. This means the total budget for knowledge base documents should not exceed 60% of the context length. Users can manually adjust this ratio (e.g., 0.4~0.8).
[0097] Final available lexical budget for: This value represents the total budget of terms available to carry document content during the dynamic document selection process. By introducing a knowledge base budget cap, information overload caused by excessively long documents is avoided.
[0098] S300: Calculate the similarity between the user's question and the documents in the knowledge base, obtain the relevance score of each document, and sort them in descending order of the scores to obtain a sorted document set.
[0099] This step employs a strategy that combines dense and sparse retrieval, taking into account both semantic matching and keyword matching to improve the accuracy and robustness of document retrieval. Specifically, it includes the following sub-steps:
[0100] S301: Convert the user question into a question feature vector and convert each document in the knowledge base into a document feature vector. Calculate the cosine similarity between the question feature vector and each document feature vector, and use it as the first similarity score.
[0101] Using pre-trained semantic embedding models (such as all-MiniLM-L6-v2, bge-base-zh-v1.5, or text-embedding-ada-002) for the user question q and each document in the knowledge base respectively. Encode the feature vector of the problem. and document feature vectors The vector dimension is typically 384, 768, or 1536. The cosine similarity between the two vectors is calculated as the first similarity score. The score range is: In this embodiment, the actual value is typically in the range of... Between these values, a larger value indicates a more semantically relevant meaning. To control computational overhead, document vectors in the knowledge base can be pre-calculated and stored; during online queries, only the question vector needs to be calculated and the dot product iterated.
[0102] S302: Match the user question with each document using terms, and calculate the BM25 score based on term frequency and inverse document frequency as the second similarity score;
[0103] The BM25 algorithm (Okapi BM25) is used to calculate the term matching score between the user's question and each document. BM25 is a classic probabilistic retrieval model that effectively measures whether a document contains the keywords of the question.
[0104] Specifically, for each term t in the user question q, calculate its meaning in the document. word frequency in and the frequency of inverse documents in the entire knowledge base The scores are accumulated to obtain the BM25 score: ;in, and For empirical parameters, this embodiment uses... , ; Document length (in words or lexical units); The average length of all documents in the knowledge base. The BM25 score is unaffected by vector normalization, is highly sensitive to keyword matching, and can compensate for the lack of information such as proper terms and numbers that may be missed by pure semantic retrieval.
[0105] S303: The first similarity score and the second similarity score are weighted and summed to obtain the comprehensive relevance score of each document, wherein the weights of the first similarity score and the second similarity score are both preset non-negative values and their sum is 1;
[0106] Specifically, the first similarity score and the second similarity score are weighted and summed to obtain the comprehensive relevance score for each document: ;in, It is the preset first similarity score weight, and its value range is... Furthermore, the sum of their weights is 1. In this embodiment, a default value is set based on the knowledge base type: for open, semantically rich document libraries (such as encyclopedias and news), the default value is taken as 1. (Focusing on semantics); For document libraries with dense keywords such as code, contracts, and legal clauses, select... (Emphasis on BM25). Users can also manually adjust this weight based on actual results.
[0107] Since the cosine similarity and the BM25 score have different dimensions, in practical applications, the two types of scores can be normalized separately (such as Min-Max normalization or Z-score normalization) before being weighted and summed to avoid one type of score dominating the overall result.
[0108] S304: Sort all documents in the knowledge base in descending order of the comprehensive relevance score to obtain a sorted document set.
[0109] Specifically, according to the comprehensive relevance score Sort the documents in descending order from highest to lowest to obtain the sorted collection. ,in This is the most relevant document.
[0110] S400: Calculate the number of lexical units for each document in the sorted document set.
[0111] Specifically, obtain the sorted document collection. The number of lexical units in each document is used to determine budget consumption during the dynamic document selection step. Two optional implementation methods are provided below:
[0112] Method 1 (Precise Calculation Mode): Perform word segmentation on the text content of each document and count the number of tokens obtained after word segmentation.
[0113] This method uses the same tokenizer as large language models. For example, for backends using GPT series models, the tiktoken tokenizer is used; for backends using LLaMA series models, HuggingFace's LlamaTokenizer is used. The specific steps are as follows:
[0114] Read the raw text content of a document from document storage;
[0115] Calling the tokenizer's encode method converts the text into a sequence of token IDs;
[0116] Calculate the length of the sequence, denoted as . .
[0117] The advantage of this method is its high accuracy, perfectly matching the actual number of tokens processed by the model, avoiding budget waste or overruns due to estimation errors. The disadvantage is that it requires a complete call to the tokenizer each time, incurring computational overhead for long documents or large batches of documents. This embodiment is preferentially recommended for use on the server side or in scenarios where high accuracy is required.
[0118] Method 2 (Estimation Mode): Perform character statistics on the text content of each document and estimate the number of terms in the document based on a preset conversion ratio.
[0119] This method eliminates the need for complex word segmenters, quickly estimating the number of lexical units solely through character counting and language-specific conversion ratios. The specific steps are as follows:
[0120] Perform character counting on the document text to obtain the character count. ;
[0121] The document's primary language type can be determined by checking the Unicode range of the characters: if Chinese characters (CJK Unified Ideographs) account for more than 30%, it is considered Chinese text; otherwise, it is considered English text (or a mixed text that is primarily English).
[0122] Select the conversion ratio based on the language type:
[0123] English text: Each word corresponds to 4 characters, that is ;
[0124] Chinese text: Each word character corresponds to 1.5 characters, that is... .
[0125] in, This indicates rounding up to the nearest integer.
[0126] This conversion ratio is based on empirical statistics: in English, one word element covers an average of about 4 characters (including spaces and punctuation), while in Chinese, due to the higher character information density, one word element corresponds to an average of 1.3 to 1.5 characters. In this embodiment, 1.5 is used to provide a certain safety margin.
[0127] The advantage of this method is its extremely fast computation speed, making it suitable for scenarios with extremely high real-time requirements (such as streaming question answering) or situations where the document size is extremely large and cannot withstand word segmentation one by one. The disadvantage is that there is a certain estimation error, which may result in a slight waste or slight overrun of the budget.
[0128] The default implementation of this embodiment is as follows: In a general deployment environment, method one is used to ensure the accuracy of budget allocation; only when the system is explicitly configured to fast mode or the average document length exceeds 100,000 characters will it automatically switch to method two.
[0129] S500: Perform dynamic document selection on the sorted document set based on the available lexical budget and the number of lexical units per document.
[0130] With available vocabulary budget and a collection of documents sorted in descending order of overall relevance score. and their corresponding lexical numbers The system then dynamically determines which documents to select in their entirety, which documents to truncate, and which documents to skip through a linear traversal. Its core objective is to retain as much information as possible that is highly relevant to the user's problem, while staying within budget, and to avoid information fragmentation caused by excessively short truncation.
[0131] The specific execution process for this step is as follows:
[0132] S501: Initialize variables.
[0133] Set the current remaining budget .
[0134] Initialize the selected content list .
[0135] Get the preset minimum document threshold (For example (Terminator). This threshold indicates that if the remaining budget is insufficient to accommodate a complete document, but the remaining budget is still greater than or equal to... If so, the current document can be truncated; if the remaining budget is less than If the remaining space is too small and the extracted fragment is too short, it will lose useful information. Therefore, the truncation should be abandoned and subsequent documents should be tried instead.
[0136] S502: Iterate through each document sequentially. If the number of lexical units in the current document does not exceed the current remaining budget, select the complete content of the document as the selected content. If the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than the preset minimum document threshold, perform a truncation operation on the document, select the truncated content as the selected content, and terminate the selection of subsequent documents. If the current remaining budget is less than the minimum document threshold, skip the current document and continue iterating.
[0137] Specifically, for index i ranging from 1 to m, the following judgment logic is executed:
[0138] Scenario A: The number of terms in the current document does not exceed the current remaining budget, i.e. The system then selects the entire content of the document and appends it to it. End of list. Update Then continue iterating through the next document ( ).
[0139] Scenario B: The number of terms in the current document exceeds the current remaining budget, but the current remaining budget is not less than the minimum document threshold, i.e. and The system then performs a truncation operation on the document: starting from the beginning of the document, it truncates exactly [length missing]. The content of the word units (cut as close as possible to the sentence or paragraph boundaries) is used to obtain the truncated content. .Will Add to End of list. Then immediately terminate the traversal, no longer processing any subsequent documents, regardless of their relevance scores. At this point, the remaining budget... It may be completely used up or leave very little remaining.
[0140] Scenario C: The current remaining budget is less than the minimum document threshold, i.e. At this point, the remaining budget is considered too small; even if the current document is truncated, the resulting fragment is too short to provide meaningful semantic information. Therefore, the system skips the current document and continues traversing the next document. ).
[0141] S503: Processing after traversal.
[0142] If the traversal terminates prematurely due to the truncation operation in case B, then It contains several complete documents and the last truncated document.
[0143] If the traversal ends naturally, it means that all documents have been processed and case B has never been triggered. It contains several complete documents and may also contain unused remaining budget, which is ultimately reserved for answer generation.
[0144] S500: The selected document content is concatenated and input together with the user question into the large language model to generate an answer to the user question.
[0145] Specifically, the dynamically selected document content and user questions are assembled into a prompt that conforms to the input requirements of a large language model according to a specific format, and the model is then invoked to generate the final answer text. A well-structured prompt guides the model to focus on document information and follow instruction constraints, thereby improving the accuracy of the answer. This includes the following sub-steps:
[0146] The step of concatenating all selected document content and inputting it along with the user question into the large language model to generate an answer to the user question specifically includes:
[0147] S601: Obtain a preset system prompt, the system prompt being used to define the response role and behavioral norms of the large language model;
[0148] Specifically, the system prompt is a fixed string used to define the response role, behavioral norms, and output format of the large language model. The system prompt can be pre-configured in the system and can be switched according to different application scenarios (such as customer service, legal, and medical).
[0149] S602: Concatenate the contents of all selected documents in the order of selection to obtain the context text;
[0150] Specifically, all document content is concatenated in the order listed, using specific delimiters (such as two newline characters "\n" or a separator "---") to form continuous context text.
[0151] S603: Construct user prompt text, the user prompt text including the user question and the context text, wherein the context text is placed before or after the user question;
[0152] The user prompt text combines the user's question with the context text. Depending on the model's requirements for the prompt format (e.g., chat models typically accept a list of conversation messages), there are two common combinations:
[0153] Method A (context first, question second): Suitable for scenarios where the model is required to read the document before answering. Format example:
[0154] Text
[0155] The following is the content of the relevant knowledge base document:
[0156] {context_text}
[0157] Please answer the following questions based on the above document:
[0158] {user_question}
[0159] Method B (Question first, context second): Suitable for scenarios where the model needs to understand the question before targeting the relevant documents. Format example:
[0160] Text
[0161] Question: {user_question}
[0162] Please refer to the following document for your answer:
[0163] {context_text}
[0164] This embodiment defaults to method A, placing the context before the question. This aligns better with the left-to-right attention habits of large language models, allowing the model to prioritize document information when generating answers. The {context_text} and {user_question} in the user prompt text are replaced with the context text obtained in S602 and the original user question string, respectively.
[0165] S604: Combine the system prompt and the user prompt text into a complete input prompt;
[0166] Specifically, based on the interface form of the large language model, there are two combination methods:
[0167] Method 1 (Single-string prompt): For older LLM interfaces that only accept a single string, simply concatenate the system prompt and the user prompt text, separating them with a newline character:
[0168] text
[0169] {system_prompt}{user_prompt}
[0170] Method 2 (Message List): For modern chat models (such as GPT-4, LLaMA-2-Chat, Claude), the input is a message list, typically containing two roles: system and user. This embodiment uses this method because it more clearly distinguishes between system commands and user input. The construction is as follows:
[0171] json
[0172] [{"role": "system", "content": "{system_prompt}"},
[0173] {"role": "user", "content": "{user_prompt}"}]
[0174] Where {user_prompt} is the user prompt text constructed in S603.
[0175] S605: Using the input prompt as input, call the text generation interface of the large language model to obtain the generated text returned by the large language model, and use the generated text as the answer to the user's question.
[0176] Specifically, the complete input prompt is passed as a list of messages or a string to the generation interface of the large language model. Generation parameters are set as follows:
[0177] max_tokens: Set to the expected number of tokens in the answer. .
[0178] temperature: Set to 0.2 (lower value) to reduce randomness and ensure the certainty of the answer.
[0179] top_p: set to 0.95.
[0180] stop: You can set the stop sequence, such as ["\n"] or [" "].
[0181] The generated text returned by the model is the final answer to the user's question. The system then returns this answer to the user.
[0182] In a preferred embodiment, when no document content is selected after traversing the sorted document set, the block retrieval degradation mode is activated.
[0183] Specifically, during the dynamic document selection process in step S500, the following situation may occur: after traversing the entire sorted document set, The list is still empty. Typical reasons for this include:
[0184] The number of terms in all documents is greater than the remaining budget, and for each document, the remaining budget is less than the minimum document threshold. (Therefore, the truncation operation was never triggered, and all documents were skipped.)
[0185] While the knowledge base contains relevant documents, the documents are too long; even the smallest document exceeds the available budget, and the budget is less than [amount missing]. This results in the inability to extract any valid segments;
[0186] The user questions are too broad or the relevant documents retrieved are too few, and these documents cannot be fully selected or truncated within the budget.
[0187] In the block retrieval degradation mode, the documents in the knowledge base are divided into multiple semantic blocks according to semantic boundaries, the relevance score of each semantic block to the user's question is calculated, and the semantic blocks are arranged in descending order of the scores;
[0188] Specifically, each document in the knowledge base is divided into multiple semantic blocks according to semantic boundaries. The size of each semantic block needs to be controlled within a range that can independently carry information, while also being easy to select according to budget. The segmentation strategy is as follows:
[0189] Based on paragraphs, if a paragraph is too long (more than 256 words), it needs to be divided into sentences.
[0190] Try to maintain semantic integrity: avoid cutting in the middle of a sentence; prioritize the use of line breaks, periods, question marks, exclamation marks, etc. as boundaries;
[0191] Record the source document ID and original order for each semantic block for traceability.
[0192] After segmentation, a set of semantic blocks is obtained. Each semantic block It has its text content and number of lexical terms. .
[0193] To calculate the relevance score between each semantic block and the user's question, a fusion retrieval strategy similar to that used in step S300 is employed. Specifically:
[0194] The cosine similarity between the semantic block vector and the question vector is calculated using the same dense embedding model and used as the first score;
[0195] The BM25 algorithm is used to calculate the word matching score between semantic blocks and the question, which is used as the second score.
[0196] The two are weighted and summed (with the weights consistent with those in S300) to obtain the overall relevance score for each semantic block.
[0197] Sort all semantic blocks in descending order of score to obtain the sorted set of semantic blocks. .
[0198] Based on the available lexical budget, semantic blocks are selected sequentially until the cumulative number of lexical units is close to but does not exceed the available lexical budget.
[0199] Specifically, the calculated available lexical budget Iterate through the sorted semantic blocks in sequence:
[0200] Initialize the current cumulative word element count Select list ;
[0201] For each semantic block
[0202] like Then the complete content of the semantic block is selected and appended to it. and update ;
[0203] Otherwise, if there is still room in the current remaining budget (i.e. You can try truncating the semantic block. However, to maintain the integrity of the semantic block, it is recommended not to truncate it further and terminate the selection directly. After the selection is complete, It contains several complete semantic blocks.
[0204] The selected semantic blocks are concatenated and input together with the user's question into the large language model to generate an answer to the user's question.
[0205] Specifically, All semantic blocks are concatenated in the order of selection and separated using delimiters (such as "\n") to obtain the context text. This context text, along with the user's question, is then input into the large language model as described in S600 to generate the final answer.
[0206] As an optional embodiment, performing a truncation operation on the document specifically includes:
[0207] Based on the text structure of the current document and / or the type of the user's question, select one of the following truncation strategies to execute:
[0208] Start extracting text from the beginning of the document until the number of tokens extracted reaches the current remaining budget;
[0209] Divide the document according to its natural paragraph boundaries, and select complete paragraphs one by one until adding another complete paragraph would cause the cumulative word count to exceed the current remaining budget;
[0210] The abstract large language model is invoked to compress the content of the current document and generate a content summary, ensuring that the number of terms in the summary does not exceed the current remaining budget.
[0211] Specifically, when condition B is met, the system needs to perform a truncation operation on the current document to obtain a segment not exceeding [a certain length]. The content of the terms. For scenario B, three optional truncation strategies are provided. The system automatically selects the most suitable strategy based on the current document's text structure (such as paragraph distribution and whether it contains headings) and the type of user question (such as factual, summary, or inferential). Users can also pre-configure a default strategy. The three strategies and their applicable scenarios are described below.
[0212] Strategy 1: Truncate from the beginning
[0213] Starting from the first character of the document, characters / terms are added sequentially until the cumulative term count reaches or approaches the threshold. (Typically, cut at natural boundaries such as spaces or punctuation marks to avoid truncating words or Chinese characters.) This strategy is suitable for documents with loose text structures (such as chat logs or log files) and disjointed paragraph semantics; user questions that are factual (such as "What time is mentioned in the document?"), where the answer usually appears at the beginning of the document; and where extremely high response speed is required, necessitating minimal computational overhead.
[0214] Strategy 2: Selecting entire paragraphs based on paragraph boundaries
[0215] Organize the current document by paragraph marks (such as two consecutive newlines \n, blank lines, or HTML tags). (Tags) divided into paragraph lists .
[0216] Initialize cumulative lexicon Select list .
[0217] Iterate through each paragraph in turn. :
[0218] Calculate the number of lexical units in this paragraph. .
[0219] like Then add the entire paragraph. ,renew .
[0220] Otherwise, stop traversing (without truncating the current paragraph).
[0221] Will All paragraphs are concatenated in their original order to form the truncation result.
[0222] This policy applies to documents with a clear paragraph structure (such as technical reports, news articles, and legal documents).
[0223] User questions are either general or inferential (e.g., "What is the main point of the document?"), and complete paragraphs can retain local semantics.
[0224] Strategy 3: Abstractactive Compression Based on Digest Generation
[0225] Use a lightweight summarizing large language model (such as T5-small, Pegasus, BART, or a small LLM deployed locally) to generate a summary of the current document.
[0226] Set summary generation The parameters are (Lexicon number).
[0227] The model outputs a compressed summary text with no more than [number of terms]. .
[0228] If the summary generation fails, it will be downgraded to strategy one or strategy two.
[0229] This strategy is suitable for documents that are extremely long (e.g., more than 10 times the length of the document). Direct truncation or paragraph selection will result in the loss of a large amount of context; user questions are high-level summary or comparison types (such as "What is the core conclusion of this document?"); the system has sufficient computing resources and does not have extreme real-time requirements (summary generation takes about several hundred milliseconds).
[0230] Example 2
[0231] A knowledge base document filtering and question answering system based on context budget includes: a user question receiving module for receiving user questions; a model parameter acquisition module for obtaining the maximum context length of a large language model; a budget calculation module for calculating the available lexical budget for retrieval enhancement generation based on the number of lexicals in the user question, the number of lexicals in the expected answer, the lexical cost of the prompt template, and the maximum context length; a retrieval and sorting module for calculating the similarity between the user question and documents in the knowledge base, obtaining a relevance score for each document, and sorting them in descending order of score to obtain a sorted document set; a document lexical calculation module for calculating the number of lexicals in each document in the sorted document set; and a dynamic document selection module for... Based on the available lexical budget and the number of lexical units in each document, dynamic document selection is performed on the sorted document set: each document is traversed sequentially; if the number of lexical units in the current document does not exceed the current remaining budget, the complete content of the document is selected as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than a preset minimum document threshold, the document is truncated, the truncated content is selected as the selected content, and the selection of subsequent documents is terminated; if the current remaining budget is less than the minimum document threshold, the current document is skipped and traversal continues; the question-answering generation module is used to concatenate the content of all selected documents and input them together with the user question into the large language model to generate an answer to the user question.
[0232] Example 3
[0233] According to embodiments of the present invention, the present invention also provides an electronic device, a readable storage medium, and a computer program product.
[0234] Figure 2 A schematic block diagram of an example electronic device 100 that can be used to implement embodiments of the present invention is shown. Electronic device 100 is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. Electronic device 100 may also represent various forms of mobile devices, such as personal digital assistants, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their links and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the invention described and / or claimed herein.
[0235] like Figure 2 As shown, the electronic device 100 includes a computing unit 101, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 102 or a computer program loaded from a storage unit 108 into a random access memory (RAM) 103. The RAM 103 may also store various programs and data required for the operation of the device 100. The computing unit 101, ROM 102, and RAM 103 are interconnected via a bus 104. An input / output (I / O) interface 105 is also linked to the bus 104.
[0236] Multiple components in electronic device 100 are linked to I / O interface 105, including: input unit 106, such as keyboard, mouse, etc.; output unit 107, such as various types of displays, speakers, etc.; storage unit 108, such as disk, optical disk, etc.; and communication unit 109, such as network card, modem, wireless transceiver, etc. Communication unit 109 allows electronic device 100 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.
[0237] The computing unit 101 can be a variety of general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 101 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 101 performs the various methods and processes described above, such as a context-budget-based knowledge base document filtering and question-answering method. For example, in some embodiments, a context-budget-based knowledge base document filtering and question-answering method can be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 108. In some embodiments, part or all of the computer program can be loaded and / or installed on device 100 via ROM 102 and / or communication unit 109. When the computer program is loaded into RAM 103 and executed by the computing unit 101, one or more steps of the context-budget-based knowledge base document filtering and question-answering method described above can be performed. Alternatively, in other embodiments, computing unit 101 may be configured, by any other suitable means (e.g., by means of firmware), to perform a context-budget-based knowledge base document filtering and question-answering method.
[0238] Various embodiments of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), systems-on-a-chip (SoCs), payload-programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.
[0239] The program code used to implement the methods of the present invention can be written in any combination of one or more programming languages. This program code can be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing device, such that when executed by the processor or controller, the program code causes the functions / operations specified in the flowcharts and / or block diagrams to be implemented. The program code can be executed entirely on the machine, partially on the machine, as a standalone software package partially on the machine and partially on a remote machine, or entirely on a remote machine or server.
[0240] In the context of this invention, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. Machine-readable media can include, but are not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical links based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0241] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).
[0242] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as a data server), or computing systems that include middleware components (e.g., an application server), or computing systems that include frontend components (e.g., a user computer with a graphical user interface or web browser through which a user can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., a communication network). Examples of communication networks include local area networks (LANs), wide area networks (WANs), and the Internet.
[0243] Computer systems can include clients and servers. Clients and servers are generally located far apart and typically interact via communication networks. Client-server relationships are created by computer programs running on the respective computers and having a client-server relationship with each other. Servers can be cloud servers, servers in distributed systems, or servers incorporating blockchain technology.
[0244] It should be understood that the various forms of processes shown above can be used to reorder, add, or delete steps. For example, the steps described in this invention can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution disclosed in this invention can be achieved, and this is not limited herein.
[0245] The above description is merely a preferred embodiment of the present invention and is not intended to limit the present invention in any way. Therefore, any modifications, equivalent changes, and alterations made to the above embodiments based on the technical essence of the present invention without departing from the scope of the present invention shall still fall within the scope of the present invention.
Claims
1. A knowledge base document filtering and question-answering method based on context budgeting, characterized in that, Includes the following steps: Receive user questions and obtain the maximum context length of the large language model; The available lexical budget for retrieval enhancement generation is calculated based on the number of lexical units in the user question, the number of lexical units in the expected answer, the lexical cost of the prompt template, and the maximum context length. The similarity between the user's question and the documents in the knowledge base is calculated to obtain a relevance score for each document. The documents are then sorted in descending order of their scores to obtain a sorted set of documents. Calculate the number of lexical units in each document of the sorted document set; Based on the available lexical budget and the number of lexical units in each document, dynamic document selection is performed on the sorted document set: each document is traversed sequentially; if the number of lexical units in the current document does not exceed the current remaining budget, the complete content of the document is selected as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than a preset minimum document threshold, the document is truncated, the truncated content is selected as the selected content, and the selection of subsequent documents is terminated; if the current remaining budget is less than the minimum document threshold, the current document is skipped and the traversal continues. The selected document content is concatenated and input together with the user's question into the large language model to generate an answer to the user's question.
2. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, The step of calculating the available lexical budget for retrieval enhancement generation based on the number of lexical units in the user question, the expected number of lexical units in the answer, the lexical cost of the prompt template, and the maximum context length specifically includes: The user question is segmented into words and its lexical count is calculated; The number of lexical units in the expected answer is determined based on a preset default value or a statistical value based on the length of historical answers. Calculate the lexical cost of the prompt template based on the preset prompt template string; Subtract the number of lexical units in the user question, the number of lexical units in the expected answer, and the lexical cost of the prompt template from the maximum context length to obtain the preliminary budget value; Obtain the system's preset knowledge base budget limit, and take the smaller of the initial budget value and the knowledge base budget limit as the final usable term budget for retrieval enhancement.
3. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, The step of calculating the similarity between the user's question and documents in the knowledge base to obtain a relevance score for each document, and then sorting the documents in descending order of their scores to obtain a sorted document set, specifically includes: The user question is converted into a question feature vector, and each document in the knowledge base is converted into a document feature vector. The cosine similarity between the question feature vector and each document feature vector is calculated as the first similarity score. The user question is matched with each document for terms, and the BM25 score based on term frequency and inverse document frequency is calculated as the second similarity score. The first similarity score and the second similarity score are weighted and summed to obtain the comprehensive relevance score of each document, wherein the weights of the first similarity score and the second similarity score are both preset non-negative values and their sum is 1; All documents in the knowledge base are sorted from high to low according to the comprehensive relevance score to obtain a sorted document set.
4. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, The calculation of the number of lexical units for each document in the sorted document set specifically includes: The text content of each document is segmented into words, and the number of tokens obtained after segmentation is counted as the token count of the document. Alternatively, character counts can be performed on the text content of each document, and the number of tokens in the document can be estimated based on a preset conversion ratio, wherein the conversion ratio is: for English text, each token corresponds to four characters; for Chinese text, each token corresponds to 1.5 characters.
5. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, The step of concatenating all selected document content and inputting it along with the user question into the large language model to generate an answer to the user question specifically includes: Obtain preset system prompts, which are used to define the response roles and behavioral norms of the large language model; Concatenate the contents of all selected documents in the order of selection to obtain the context text; Construct user prompt text, which includes the user question and the context text, wherein the context text is placed before or after the user question; Combine the system prompt and the user prompt text into a complete input prompt; Using the input prompt as input, the text generation interface of the large language model is called to obtain the generated text returned by the large language model, and the generated text is used as the answer to the user's question.
6. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, Also includes: If no document content is selected after traversing the sorted document set, the block retrieval degradation mode is activated. In the block retrieval degradation mode, the documents in the knowledge base are divided into multiple semantic blocks according to semantic boundaries, the relevance score of each semantic block to the user's question is calculated, and the semantic blocks are arranged in descending order of the scores; Based on the available lexical budget, semantic blocks are selected sequentially until the cumulative number of lexical units is close to but does not exceed the available lexical budget. The selected semantic blocks are concatenated and input together with the user's question into the large language model to generate an answer to the user's question.
7. The knowledge base document filtering and question answering method based on context budgeting according to claim 1, characterized in that, The truncation operation on the document specifically includes: Based on the text structure of the current document and / or the type of the user's question, select one of the following truncation strategies to execute: Start extracting text from the beginning of the document until the number of tokens extracted reaches the current remaining budget; Divide the document according to its natural paragraph boundaries, and select complete paragraphs one by one until adding another complete paragraph would cause the cumulative word count to exceed the current remaining budget; The abstract large language model is invoked to compress the content of the current document and generate a content summary, ensuring that the number of terms in the summary does not exceed the current remaining budget.
8. A knowledge base document filtering and question-answering system based on context budgeting, characterized in that, include: The user issue receiving module is used to receive user issues. The model parameter acquisition module is used to obtain the maximum context length of a large language model; The budget calculation module is used to calculate the available lexical budget for retrieval enhancement generation based on the number of lexicals in the user question, the number of lexicals in the expected answer, the lexical cost of the prompt template, and the maximum context length. The retrieval and sorting module is used to calculate the similarity between the user's question and the documents in the knowledge base, obtain the relevance score of each document, and sort them in descending order of the scores to obtain a sorted set of documents. The document lexical calculation module is used to calculate the number of lexical units in each document in the sorted document set; The dynamic document selection module is used to perform dynamic document selection on the sorted document set based on the available lexical budget and the number of lexical units in each document: iterates through each document in turn; if the number of lexical units in the current document does not exceed the current remaining budget, the complete content of the document is selected as the selected content; if the number of lexical units in the current document exceeds the current remaining budget but the current remaining budget is not less than a preset minimum document threshold, the document is truncated, the truncated content is selected as the selected content, and the selection of subsequent documents is terminated; if the current remaining budget is less than the minimum document threshold, the current document is skipped and the traversal continues. The question-and-answer generation module is used to concatenate the content of all selected documents and input them together with the user's question into the large language model to generate an answer to the user's question.
9. An electronic device, characterized in that, The electronic device includes: At least one processor; and A memory communicatively connected to the at least one processor; wherein, The memory stores a computer program that can be executed by the at least one processor, the computer program being executed by the at least one processor to enable the at least one processor to perform the knowledge base document filtering and question answering method according to any one of claims 1-7.
10. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the knowledge base document filtering and question-answering method according to any one of claims 1-7.
