Retrieval enhancement generation method and system based on maximum context window
By optimizing the context construction of the RAG system through dynamic selection and structured prompt templates, the problems of insufficient information utilization and noise caused by fixed strategies are solved, and more efficient information utilization and answer generation are achieved.
Patent Information
- Application Number
- CN202511777850.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-28
- Publication Date
- 2026-02-27
AI Technical Summary
Existing RAG systems employ a fixed-size strategy when constructing contexts, which prevents them from fully utilizing the long context processing capabilities of large language models or introduces noisy information, affecting the comprehensiveness and accuracy of the generated answers.
We adopt a dynamic selection strategy based on the maximum context window, iteratively select documents by information value score, construct a context set with the maximum information content and optimal diversity, combine sparse retrieval and dense vector retrieval, use a re-ranking model for fine ranking, and optimize the input through structured prompt templates.
It significantly improves the comprehensiveness, accuracy, and robustness of the generated answers, avoids redundancy and emptiness in contextual information, and improves the efficiency of information utilization and the quality of generated answers.
Smart Images

Figure CN121579645A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of language processing, and in particular to a retrieval augmented generation method and system based on a maximum context window. BACKGROUND
[0002] Retrieval-Augmented Generation (RAG) technology effectively solves the inherent defects such as model hallucination and knowledge lag by combining large language models (LLM) with external knowledge bases, and has become the mainstream architecture for building trustworthy and knowledge-intensive artificial intelligence applications. A typical RAG system includes three core components: retriever, re-ranker, and generator. With the rapid development of LLM technology, the length of its context window has been continuously broken, expanding from tens of thousands of tokens in the early days to hundreds of thousands or even millions of tokens, which provides an unprecedented hardware foundation for injecting massive relevant knowledge in a single request. However, how to dynamically, intelligently, and fully utilize this huge context capacity has become a new bottleneck restricting the further improvement of RAG system performance.
[0003] In the prior art, the mainstream optimization RAG scheme focuses on improving the relevance of the retrieved documents, such as using query rewriting combined with a re-ranking model to filter out the most relevant documents. However, when constructing the context for the final input LLM, these schemes usually adopt a simple and fixed strategy: either preset a document quantity upper limit K, or set a total token number threshold, and use it to truncate the top K documents in the re-ranked list. This fixed context size scheme has significant defects: first, when the preset value is too small, the powerful long context processing capability of the LLM cannot be fully utilized, and a large amount of potentially relevant auxiliary information is discarded, resulting in incomplete and in-depth generated answers; second, when the preset value is too large or close to the model context upper limit, irrelevant or redundant noise documents are easily introduced, which not only may interfere with the model's judgment, but also may cause the request to fail due to exceeding the context limit. SUMMARY
[0004] Therefore, the present application proposes a retrieval augmented generation method and system based on a maximum context window, which dynamically determines the context size according to the maximum capacity of the model, and uses an iterative selection strategy based on information value scores, which can intelligently assemble a context set with the maximum amount of information and optimal diversity for the current query without exceeding the model's processing capacity, thereby significantly improving the comprehensiveness, accuracy, and robustness of the generated answers.
[0005] The technical scheme of the present application is implemented as follows: On the one hand, the present application provides a retrieval augmented generation method based on a maximum context window, comprising: S1 receives user query data; S2 retrieves according to the user query data to obtain a candidate document list sorted by relevance; S3 determines the available context capacity according to the maximum context window of the target large language model; S4 establishes an intermediate set, and in an iterative manner, moves the documents in the candidate document list to the intermediate set according to the information value score until the cumulative text length of the intermediate set reaches the available context capacity, to obtain a target document set, the information value score is used to describe the relevance of the document to the user query data and the novelty of the document relative to the current intermediate set; S5 combines the user query data and the target document set into a prompt information; S6 inputs the prompt information into the target large language model and obtains the answer generated by the target large language model.
[0006] On the basis of the above technical scheme, preferably, step S2 comprises the following sub-steps: S21 adopts a hybrid retrieval method to perform preliminary retrieval from the knowledge base according to the user query data to obtain an initial document set; S22 uses a reordering model to sort the initial document set to calculate the relevance score of each document to the user query data; S23 arranges the documents in descending order according to the relevance score to generate a candidate document list sorted by relevance.
[0007] Further preferably, the hybrid retrieval method is a combination of sparse retrieval and dense vector retrieval.
[0008] On the basis of the above technical scheme, preferably, step S3 comprises the following sub-steps: S31 obtains the maximum context token length of the target large language model; S32 calculates the sum of the token length of the system preset prompt template and the user query data; S33 determines the available context capacity according to the maximum context token length and the sum of the token length and sets the answer reserved space.
[0009] On the basis of the above technical scheme, preferably, step S4 comprises the following sub-steps: S41 initializes an empty intermediate set; S42 adds the first document in the candidate document list to the intermediate set; S43 calculates the information value score of the remaining documents in the candidate document list, and in an iterative manner, adds the document with the highest information value score to the intermediate set in turn until the cumulative text length of the intermediate set reaches the available context capacity; S44 outputs the intermediate set as the target document set.
[0010] Further preferably, information value scores of the remaining documents in the candidate document list are calculated, and the document with the highest information value score is added to the intermediate set in turn by iteration until the cumulative text length of the intermediate set reaches the available context capacity, including: S431 calculating information value scores of the remaining documents in the candidate document list; S432 selecting the document with the highest information value score to add to the intermediate set; S433 calculating the cumulative text length of the intermediate set after adding the new document; S434 if the cumulative text length does not reach the available context capacity, returning to S431, and if it does, proceeding to step S44.
[0011] On the basis of the above technical solutions, preferably, the information value score IV-Score(D i ) is calculated as follows:
[0012] wherein IV-Score(D i ) is the information value score of the document D i to be evaluated, R(D i , Q) is the relevance score of the document D i to the user query Q, C is the set of documents currently selected into the intermediate set, is the number of documents currently selected into the intermediate set, D c is one document in the set C, sim(D i , D c ) is the semantic similarity of the document D i to D c , E(Di) is the information entropy of the document D i , and α, β, γ are preset weight coefficients, and satisfy α+β+γ=1.
[0013] On the basis of the above technical solutions, preferably, the combination of the user query data and the target document set into the prompt information includes constructing a structured prompt template, and filling the documents in the target document set and the user query data into the prompt template to generate the prompt information.
[0014] Further preferably, the structured prompt template comprises a system instruction part, a context document part, a user query part and an answer format instruction part, wherein the system instruction part is used to define the behavior specification of the target large language model when answering, the context document part is used to accommodate the document content in the target document set, the user query part is used to place the user query data, and the answer format instruction part is used to constrain the format of the answer generated by the target large language model.
[0015] In another aspect, the present application provides a maximum context window-based retrieval enhancement generation system embedded with the above-mentioned maximum context window-based retrieval enhancement generation method.
[0016] The maximum context window-based retrieval enhancement generation method and system of the present application have the following beneficial effects relative to the prior art: 1. By dynamically determining the context size according to the maximum capacity of the model and using an iterative selection strategy based on information value score, an information amount maximum and diversity optimal context set can be intelligently assembled for the current query without exceeding the processing capacity of the model, thereby significantly improving the comprehensiveness, accuracy and robustness of the generated answer; 2. By introducing an information value score formula that fuses relevance, novelty and information abundance, the document selection process not only focuses on the direct relevance to the query, but also takes into account the information complementarity between documents and the richness of the content itself. This multi-dimensional evaluation mechanism can effectively avoid the redundancy and emptiness of context information, ensuring that each piece of context sent into the large language model has a high information density, thereby greatly improving the efficiency of the model in utilizing information and the quality of the generated answer. BRIEF DESCRIPTION OF DRAWINGS
[0017] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or prior art description. Obviously, the drawings in the following description are only some embodiments of the present application, and for those skilled in the art, other drawings can also be obtained from these drawings without creative labor.
[0018] Figure 1 The figure shows the steps of the maximum context window-based retrieval enhancement generation method of the present application. DETAILED DESCRIPTION
[0019] In order to make the objects, technical solutions and advantages of the present application clearer, the following will clearly and completely describe the technical solutions in the present application with reference to the drawings in the present application. Obviously, the described embodiments are only a part of embodiments of the present application, and not all embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the protection scope of the present application.
[0020] In the description of the embodiments of the present application, it should be noted that, unless otherwise explicitly specified and limited, the terms "connected", "connected" should be understood in a broad sense, for example, can be fixedly connected, can also be detachably connected, or integrally connected, can be mechanically connected, can also be electrically connected, can be directly connected, or indirectly connected through an intermediate medium. For those of ordinary skill in the art, the specific meanings of the above terms in the embodiments of the present application can be understood according to specific circumstances.
[0021] In the description of the embodiments of the present application, it should be noted that the terms "center", "longitudinal", "transverse", "upper", "lower", "front", "rear", "left", "right", "vertical", "horizontal", "top", "bottom", "inner", "outer" and the like indicate the orientation or positional relationship based on the orientation or positional relationship shown in the drawings, and are only for the convenience of describing the embodiments of the present application and simplifying the description, and do not indicate or imply that the devices or elements indicated must have a particular orientation, be constructed and operated in a particular orientation, and therefore cannot be understood as a limitation on the embodiments of the present application.
[0022] In addition, the terms "first", "second" are only for descriptive purposes, and cannot be understood as indicating or implying relative importance or implicitly indicating the number of the indicated technical features. Therefore, the features limited by "first", "second" can explicitly or implicitly include one or more of the features. In the description of the present application, the meaning of "multiple" is two or more, unless otherwise explicitly specified and limited.
[0023] The embodiments of the present application will be described in detail below, and examples of the embodiments are shown in the drawings, wherein the same or similar reference numerals represent the same or similar elements or elements having the same or similar functions throughout. The embodiments described below by reference to the drawings are exemplary and are intended to explain the present application, and cannot be understood as a limitation on the present application.
[0024] The disclosure below provides many different embodiments or examples for implementing different structures of the application. For the sake of simplicity, the description below refers to specific examples in which features can be present together. Features can be used in combinations other than the ones described in this disclosure. This disclosure is not limited to the examples described below, but rather the intention is for this disclosure to apply to any embodiments falling within the scope of the application. Moreover, the application can refer to different examples using different reference numerals. Such repetition is for the sake of simplicity and clarity and does not indicate a relationship between the various embodiments and / or arrangements discussed. Further, the application provides examples of various specific processes and materials, but one of ordinary skill in the art can realize the applicability of other processes and / or the use of other materials.
[0025] As shown in Figure 1 The maximum context window-based retrieval enhancement generation method of the application, the core is to provide a dynamic, intelligent adaptation and maximize the use of large language model context window retrieval enhancement generation scheme, specifically using steps S1-S6 to express.
[0026] Step S1: receiving user query data.
[0027] In this step, the system receives and parses the user input query data. User query data is the starting point and core basis of the retrieval and generation process. In practical applications, its input form has diversity, which can be natural language text directly input by the user through the graphical user interface, such as a complex question "compare neural network and symbolic artificial intelligence in the pros and cons of automatic driving decision"; It can also be text data converted by voice interaction module through automatic speech recognition technology; In addition, in batch processing or system integration scenarios, the query data can also be a structured or semi-structured request message received from the upstream business system through the application programming interface, and the system will extract the core query intent from it and standardize it into a unified text format, providing a processable input target for subsequent retrieval and context construction.
[0028] Step S2: retrieving according to the user query data to obtain a candidate document list sorted by relevance.
[0029] In step S2, the system retrieves and reorders from the pre-set external knowledge base based on the user query data, aiming to obtain a highly relevant candidate document list sorted by priority. This step is the basis for subsequent dynamic context construction, and its core goal is not simply to recall a large number of documents, but to provide a document pool that has been preliminarily filtered and sorted for intelligent selection in step S4. By combining retrieval models of different principles and reordering the results, this step effectively ensures the accuracy of the top documents in the candidate document list, while taking into account the recall rate of the entire list, providing the first information guarantee for the final generation of a comprehensive and accurate answer.
[0030] Step S21: Using a hybrid retrieval method, a preliminary retrieval is performed from the knowledge base based on the user's query data to obtain an initial document set.
[0031] This step is used to broadly recall documents that may be relevant to the user's query from a large-scale knowledge base, balancing recall and precision. Specifically, a hybrid retrieval strategy can be adopted, executing two retrieval methods based on different principles in parallel.
[0032] In one specific embodiment, the hybrid retrieval method includes a combination of sparse retrieval and dense vector retrieval. Sparse retrieval utilizes the BM25 algorithm, which is based on keyword matching, represents queries and documents as high-dimensional sparse vectors, and calculates their overlap. This effectively captures documents with exact keyword matches and has high accuracy for queries containing specific terms, entities, or proper nouns. Dense vector retrieval uses a dense retriever based on a pre-trained language model. This model maps queries and documents into a low-dimensional and continuous vector space, and measures semantic relevance by calculating the cosine similarity between vectors. This effectively understands the semantic association between queries and documents, recalling documents that are semantically related even without common keywords.
[0033] The result sets obtained from the two retrieval methods are merged to form an initial document set with broader coverage.
[0034] Step S22: Sort the initial document set using a re-ranking model and calculate the relevance score of each document to the user query data.
[0035] Due to the large number and varying quality of the initial document set obtained from the preliminary retrieval, this step introduces a more powerful but computationally expensive re-ranking model to refine the scoring and ranking of the initial document set. This model employs a cross-encoder architecture, enabling deep attention interaction computation between the user query and a document, resulting in an accurate relevance score. Compared to fast but independent vector similarity calculations, the cross-encoder better understands context and subtle semantic differences, and its output relevance score more accurately reflects the degree of matching between the document and the query. The relevance score is set as a floating-point number between 0 and 1.
[0036] Step S23: Sort the documents in descending order according to their relevance scores to generate a list of candidate documents sorted by relevance.
[0037] After obtaining the exact relevance score of each document, all documents are ranked in descending order according to the score to generate a final candidate document list. In this list, the higher the ranking of the document, the higher the semantic relevance of the document to the user query. This list sorted by relevance is the core input of all subsequent processes of the application, ensuring that the dynamic selection process starts with the most relevant information and always operates in a high-quality information pool.
[0038] Step S3: Determine the available context capacity according to the maximum context window of the target large language model.
[0039] In step S3, the context capacity available for filling related documents is accurately calculated according to the inherent capabilities of the selected target large language model. This step is the key prerequisite for the entire method to realize dynamic adaptation, and its purpose is to convert abstract model capabilities into a quantifiable resource budget. By accurately measuring the inherent overhead of the system and reserving the necessary space for the model, this step ensures that the subsequent dynamic filling process can efficiently maximize the use of its full context potential without exceeding the model processing limit, thereby fundamentally avoiding the risk of request failure or truncated generated content due to excessive context. Specifically, steps S31-S33 can be implemented.
[0040] Step S31: Obtain the maximum context token length of the target large language model.
[0041] This step is the basis for capacity calculation. The system first needs to specify the target large language model to be used, such as GPT-4, Claude-3, etc., and the maximum context window size it supports, which is usually measured in tokens. This information is an inherent property of the model and can be obtained by consulting the model's technical specification document or through its application programming interface. For example, if the target model is a version that supports a 128K context length, the maximum context token length L max here is 128000. Obtaining this parameter sets an explicit upper limit for the entire dynamic filling process.
[0042] Step S32: Calculate the sum of the token lengths of the system preset prompt template and the user query data.
[0043] This step is used to quantify the fixed overhead required to generate an answer. The system will define a prompt template that typically includes system instructions, role settings, output format requirements, and other fixed text. The system uses a tokenization tool compatible with the target large language model to calculate the token length L prompt of the preset prompt template and the token length L query of the user query data, and then adds them together to obtain the total token length L base of the fixed overheadprompt + L query This part of the capacity will be used to carry task instructions and the problem itself, and cannot be used to fill the retrieved documents.
[0044] Step S33: Determine the available context capacity according to the maximum context token length and the sum of token lengths, and set the answer reservation space.
[0045] This step is the final link of capacity calculation, and its core is to reserve enough space for the model to generate answers. The system subtracts the maximum context token length L max from the aforementioned fixed overhead L base , and further subtracts a preset reserved space L reserve for the model to generate answers.
[0046] The calculation formula of the available context capacity L available is: L available = L max - L base - L reserve Among them, the setting of the reserved space L reserve is crucial, which is a configurable empirical value, usually set to a certain percentage of the maximum context length according to the complexity of the query and the length of the expected answer, for example, 10%-20%, this design ensures that the generated answer will not be truncated accidentally, and guarantees the integrity and reliability of the output result. The final calculation of L available is the maximum token capacity that can be used to dynamically fill the retrieved documents in step S4.
[0047] Step S4: Establish an intermediate set, and use an iterative method to move the documents in the candidate document list to the intermediate set according to the information value score, until the cumulative text length of the intermediate set reaches the available context capacity, to obtain the target document set, the information value score is used to describe the relevance of the document to the user query data, and the novelty of the document relative to the current intermediate set.
[0048] In step S4, the system performs the most core dynamic diversity context construction process of the present application. Based on the high-quality candidate document list and the accurately calculated available context capacity produced by the previous steps, the purpose of this step is to intelligently assemble a final context with the maximum amount of information and the most optimal diversity, rather than simply cutting or filling. By initializing an intermediate set and using an iterative selection strategy based on information value scores, this step simulates the behavior of humans when reading documents: first grasp the most core points, and then constantly look for supplementary materials that can provide new perspectives, new evidence or new explanations until the resources are exhausted. This method ensures that the context set sent to the large language model under the given capacity limit realizes the optimal balance between relevance and information diversity, thereby providing the most solid information foundation for generating comprehensive, accurate and in-depth answers. Specifically, it can be represented as steps S41-S44.
[0049] Step S41: Initialize an empty intermediate set.
[0050] This step is the preparation stage of the dynamic construction process. By creating an empty container in the system memory, called the intermediate set. This set is used to temporarily store and accumulate selected documents during the iterative process, and its final content will constitute the target document set sent to the large language model. Initializing an empty set ensures that each time a new user query is processed, it starts from a clean state.
[0051] Step S42: Add the first-ranked document in the candidate document list to the intermediate set.
[0052] This step sets a high-quality starting point for the iterative process. Specifically, it directly selects the first-ranked document in the sorted candidate document list, which is the most relevant to the user query, and adds it to the intermediate set. This ensures that the final context will always contain the most critical evidence for answering the question, ensuring the basic accuracy and relevance of the generated answer from the source.
[0053] Step S43: Calculate the information value scores of the remaining documents in the candidate document list, and add the document with the highest information value score to the intermediate set in an iterative manner until the cumulative text length of the intermediate set reaches the available context capacity.
[0054] This step is the core loop of the present application for document selection. Specifically, it can be further divided into steps S431-S434.
[0055] Step S431: Calculate the information value scores of the remaining documents in the candidate document list.
[0056] The first step of each iteration loop is the primary computation task. The system traverses all the documents in the current candidate document list that have not been selected into the intermediate set, i.e. the remaining documents, and computes the information value score for each of them. The information value score is a comprehensive metric that balances the relevance of the document itself and its novelty with respect to the content of the current intermediate set. Through this computation, the system is able to quantify how much incremental value each remaining document would bring to the existing context if added.
[0057] In one specific embodiment, the information value score IV-Score(D i ) is computed as follows:
[0058] where IV-Score(D i ) is the information value score of the document D i under evaluation, R(D i , Q) is the relevance score of the document D i to the user query Q, C is the set of documents that have been selected into the intermediate set so far, is the number of documents in the set C, D c is a document in the set C, sim(D i , D c ) is the semantic similarity between the documents D i and D c , E(Di) is the information entropy of the document D i , and a, b, g are preset weight coefficients that satisfy a + b + g = 1.
[0059] Step S432: Select the document with the highest information value score and add it to the intermediate set.
[0060] After the score computation of all the remaining documents is completed, the system executes a greedy selection strategy. From all the remaining documents, the unique document with the highest information value score is found and added to the intermediate set. This strategy ensures that at each step, the system makes the locally optimal choice and includes the document that would bring the most marginal benefit into the intermediate set, thereby gradually improving the information value density of the entire set.
[0061] Step S433: Compute the cumulative text length of the intermediate set after adding the new document.
[0062] After the new document is selected and added, the monitoring of resource consumption needs to be updated. That is, the sum of the text lengths of all the documents in the intermediate set at this time is recomputed, which is represented by the cumulative text length here. This length is usually measured in token numbers for accurate measurement. This step is the direct basis for determining whether the loop should be terminated.
[0063] Step S434: If the accumulated text length does not reach the available context capacity, return to S431, if it reaches, enter step S44.
[0064] This sub-step is the termination condition judgment of the loop. The current accumulated text length calculated in step S433 is compared with the available context capacity determined in step S3. If it does not reach, it indicates that there is still remaining context space to accommodate more valuable documents, and the flow returns to step S431 to start a new round of iteration and continue to select from the updated remaining document pool. If it reaches or exceeds, it indicates that the available context capacity has been fully utilized or slightly exceeded, and the loop is immediately terminated, the system exits the current iteration process, and enters step S44.
[0065] Step S44: Output the intermediate set as the target document set.
[0066] When the iteration loop is terminated due to the capacity being full, the state of the intermediate set at this time is output, and this set is defined as the target document set, which is the final product of all the aforementioned optimization steps of the present application. This target document set will be directly used in the subsequent prompt construction step.
[0067] Step S5: Combine the user query data and the target document set into a prompt information.
[0068] In step S5, the system performs structured assembly of the prompt information, integrating the results of the previous steps: user query and optimized target document set, into a standardized input that can be efficiently processed by large language models. This step is not simply text splicing, but through a pre-defined and functionally explicit structured prompt template, the original information is transformed into instructions that can precisely guide the model behavior. This process ensures that the high-quality content in the target document set can be fully perceived, understood and utilized by the large language model, so that the context information selected through dynamic optimization is finally transformed into the knowledge base for generating answers.
[0069] In one specific embodiment, it is necessary to first construct a structured prompt template, and then fill the documents in the target document set and the user query data into the prompt template to finally generate the prompt information.
[0070] The core is to replace simple text splicing with a structured prompt template, which can significantly improve the efficiency of large language models in understanding and utilizing context. By predefining a template and defining the skeleton of the final prompt information and the logical position of each component, the system fills the specified positions of the template with the contents of all documents in the target document set and the original user query data as variables during runtime. This structured assembly method can provide clear context boundaries and task instructions for the model, effectively avoiding the confusion between document content, user questions, and system instructions, thereby guiding the model to generate more accurate and more compliant answers.
[0071] Specifically, the structured prompt template includes a system instruction part, a context document part, a user query part, and an answer format instruction part.
[0072] The system instruction part is located at the beginning of the prompt information, which is used to set the role and behavior specification of the large language model. For example, the instruction can be "You are a rigorous academic assistant, please answer the question strictly according to the provided information. If the information does not contain the answer to the question, please clearly indicate 'the information cannot answer the question according to the known information.' This sets the tone for the model's answer style and boundaries; the context document part is a dedicated area for filling the contents of the target document set. By using clear markers such as "### Reference Document:" at the beginning, and arranging the documents in the set in order or a specific format, the model can help distinguish external knowledge from its internal knowledge, and strengthen its reliance on the provided context; the user query part is used to clearly present the original user query data, which can be introduced by "### User Question:" and other identifiers to ensure that the model can accurately identify the core task to be solved; the answer format instruction part is used to explicitly constrain the format of the generated content, for example, requiring "please use a point list to explain" or "please give the conclusion first, then provide detailed explanation." Through format constraints, the model can be forced to think and organize more systematically, thereby producing standardized output with better readability and better compliance with application requirements.
[0073] Step S6: input the prompt information into the target large language model, and obtain the generated answer.
[0074] This step performs answer generation and output, which is the final stage of the entire retrieval enhancement generation process. This step submits the structured prompt information carefully constructed in the previous steps to the selected target large language model through the application programming interface for reasoning and calculation. The model generates a coherent, accurate, and format-compliant natural language answer based on the system instructions and the injected optimized context and user query. The system receives the answer and performs necessary post-processing, and finally returns the answer to the user through the user interface or system interface, thereby completing a complete intelligent question and answer interaction.
[0075] The retrieval enhancement generation system based on the maximum context window of the present application embeds the retrieval enhancement generation method based on the maximum context window described above.
[0076] The system realizes each step of the method in the form of hardware and / or software through the cooperative work of internal functional modules, and the specific functional modules include but are not limited to a query interface module, an intelligent retrieval module, a capacity planning module, a dynamic selection engine, a prompt constructor, and a generation and scheduling module.
[0077] The query interface module is an entrance for the system to interact with users or upstream applications, and is responsible for performing the method step S1; the intelligent retrieval module is responsible for efficiently and comprehensively recalling relevant documents from the knowledge base; the capacity planning module is responsible for dynamically calculating available context resources; the dynamic selection engine is used to receive a candidate list from the intelligent retrieval module and a capacity budget from the capacity planning module, can initialize a working set, and run an iterative algorithm; the prompt constructor is responsible for assembling an unordered document set into a structured model instruction; and the generation and scheduling module is responsible for interacting with a large language model and delivering the final result.
[0078] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principles of the present application shall be included in the protection scope of the present application.
Claims
1. A retrieval enhancement generation method based on the maximum context window, characterized in that, include: S1 receives user query data; S2 retrieves candidate documents based on user query data and sorts them by relevance. S3 determines the available context capacity based on the maximum context window of the target large language model; S4 establishes an intermediate set and iteratively moves documents from the candidate document list to the intermediate set based on their information value scores until the cumulative text length of the intermediate set reaches the available context capacity, thus obtaining the target document set. The information value scores are used to describe the relevance of the documents to the user's query data and the novelty of the documents relative to the current intermediate set. S5 combines user query data with the target document set to create a prompt message; S6 inputs the prompt information into the target large language model and obtains its generated answer.
2. The retrieval enhancement generation method based on the maximum context window as described in claim 1, characterized in that, Step S2 includes the following sub-steps: S21 employs a hybrid retrieval method, performing a preliminary retrieval from the knowledge base based on user query data to obtain an initial document set; S22 uses a reordering model to sort the initial document set and calculates the relevance score of each document to the user's query data. S23 sorts the documents in descending order based on their relevance scores, generating a candidate document list sorted by relevance.
3. The retrieval enhancement generation method based on the maximum context window as described in claim 2, characterized in that, The hybrid retrieval method combines sparse retrieval with dense vector retrieval.
4. The retrieval enhancement generation method based on the maximum context window as described in claim 1, characterized in that, Step S3 includes the following sub-steps: S31 obtains the maximum context token length of the target large language model; The sum of the S32 computing system's preset prompt template and the token length for user data queries; S33 determines the available context capacity based on the sum of the maximum context token length and the token length, and sets the answer reservation space.
5. The retrieval enhancement generation method based on the maximum context window as described in claim 1, characterized in that, Step S4 includes the following sub-steps: S41 initializes an empty intermediate set; S42 adds the first-ranked document in the candidate document list to the intermediate set; S43 calculates the information value score of the remaining documents in the candidate document list and iteratively adds the document with the highest information value score to the intermediate set until the cumulative text length of the intermediate set reaches the available context capacity. S44 outputs an intermediate set, which is then used as the target document set.
6. The retrieval enhancement generation method based on the maximum context window as described in claim 5, characterized in that, The process involves calculating the information value score of the remaining documents in the candidate document list and iteratively adding the documents with the highest information value scores to the intermediate set until the cumulative text length of the intermediate set reaches the available context capacity. This includes: S431 calculates the information value score of the remaining documents in the candidate document list; S432 selects the document with the highest information value score and adds it to the intermediate set; S433 calculates the cumulative text length of the intermediate set after adding a new document; If the cumulative text length has not reached the available context capacity, return to step S431; otherwise, proceed to step S44.
7. The retrieval enhancement generation method based on the maximum context window as described in claim 1, characterized in that, The information value score IV-Score (D) i The calculation method is as follows: Among them, IV-Score(D i Document D to be evaluated i Information value score, R(D) i Q) is document D i The relevance score to user query Q, where C is the set of documents currently selected into the intermediate set. D represents the number of documents currently selected into the intermediate set. c For a document in set C, sim(D) i D c ) for document D i With D c The semantic similarity, E(Di) is the semantic similarity of document D. i The information entropy, α, β, γ are all preset weight coefficients, and satisfy α+β+γ=1.
8. The retrieval enhancement generation method based on the maximum context window as described in claim 1, characterized in that, The step of combining user query data with the target document set to form a prompt message includes constructing a structured prompt template, filling the prompt template with documents from the target document set and user query data, and generating the prompt message.
9. The retrieval enhancement generation method based on the maximum context window as described in claim 8, characterized in that, The structured prompt template includes a system instruction section, a context document section, a user query section, and an answer format instruction section. The system instruction section is used to define the behavioral specifications of the target large language model when answering, the context document section is used to contain the document content in the target document set, the user query section is used to place user query data, and the answer format instruction section is used to constrain the format of the answer generated by the target large language model.
10. A retrieval enhancement generation system based on a maximum context window, characterized in that, The retrieval enhancement generation method based on the maximum context window, as described in any one of claims 1-9, is embedded.