Large model context generation method and device, electronic equipment and storage medium
By obtaining the vector of the text segment to be queried and the set of candidate text segment vectors, calculating the redundancy weight coefficient, constructing the target gain function, and using a greedy algorithm to generate context, the problem of text segment overlap and redundancy in existing technologies is solved, and the quality and consistency of generated context are improved.
Patent Information
- Application Number
- CN202610070590.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-01-20
- Publication Date
- 2026-02-17
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
In existing technologies, retrieval enhancement generation methods are prone to problems such as high overlap or semantic similarity between selected text segments under strict context budget constraints, resulting in wasted budget, duplicated evidence, insufficient coverage, single basis for generated answers, limited robustness, and high parameter tuning costs.
By obtaining the vector of the text segment to be queried and the set of candidate text segment vectors, the redundancy weight coefficient is calculated, the target gain function is constructed, and a greedy algorithm is used to select the text segment vector that maximizes the target gain function from the set of candidate text segment vectors and add it to the target text segment vector set to generate the context.
This reduces redundancy among selected text segments, improves the diversity and utilization of evidence, and ensures the quality and consistency of the generated context.
Smart Images

Figure CN121542397A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and in particular to a method, apparatus, electronic device, and storage medium for generating large model contexts. Background Technology
[0002] Retrieval-Augmented Generation (RAG) is a model that combines retrieval and generation techniques and is widely used in scenarios such as question answering, summarizing, and code generation. Its core feature is that when a user asks a question, the system first retrieves the text fragment most similar to the question from an external, static knowledge base (such as enterprise documents or web page collections). This text fragment, along with the user's question, is then provided to a large language model as context to generate the final answer.
[0003] In existing technologies, most mainstream context generation methods rely on sorting and truncating the similarity between the query and candidate text segments. Under strict context budget constraints, this can easily lead to high overlap or semantic similarity between selected text segments, resulting in wasted budget and duplicated evidence. Moreover, they typically only optimize the relevance of a single text segment to the query, easily ignoring redundant relationships in the set of selected text segments. This results in problems such as insufficient coverage (important evidence is squeezed out by "duplicate segments"), single basis for generating answers, limited robustness, and high parameter tuning costs.
[0004] Therefore, how to provide a solution to the above-mentioned technical problems is a technical problem that needs to be solved by those in the relevant technical field. Summary of the Invention
[0005] To address the aforementioned technical issues, this application provides a method, apparatus, electronic device, and storage medium for generating large model contexts, which can reduce redundancy among selected text segments, improve the diversity and utilization of evidence, and thus ensure the quality and consistency of the generated context.
[0006] To achieve the above objectives, the first aspect of this application provides a method for generating large model contexts, comprising: Obtain the vector of the text segment to be queried and the set of candidate text segment vectors; The redundancy weight coefficient is calculated based on the budget constraint and the average length of the text segments in the candidate text segment vector set; Construct the target gain function based on the similarity weight coefficient and the redundancy weight coefficient; Initialize the target text vector set to an empty set; A greedy algorithm is used to sequentially select the text vectors that maximize the target gain function from the candidate text vector set and add them to the target text vector set, thus obtaining the target text vector set after greedy selection; Based on the set of target text vectors selected by the greedy algorithm, generate the context corresponding to the text vector to be queried.
[0007] Preferably, the method further includes: Obtain the initial set of text segment vectors; Calculate the similarity between the vector of the text segment to be queried and each text segment vector in the initial set of text segment vectors, and sort the text segment vectors in the initial set of text segment vectors according to the similarity to obtain the sorting result; Based on the sorting results, candidate text vectors are selected from the initial text vector set to obtain the candidate text vector set.
[0008] Preferably, the redundancy weight coefficient is calculated based on the budget constraint and the average length of the text segments in the candidate text segment vector set, including: The number of target text segments to be selected is calculated based on the budget constraint and the average length of the text segments in the candidate text segment vector set; Calculate the average similarity between the vector of the text segment to be queried and each text segment vector in the candidate text segment vector set, as well as the average redundancy among each text segment vector in the candidate text segment vector set; The redundancy weight coefficient is calculated based on the similarity weight coefficient, the number of target text segments selected, the average similarity, and the average redundancy.
[0009] Preferably, the redundancy weight coefficient is calculated based on the similarity weight coefficient, the number of target text segments selected, the average similarity, and the average redundancy, including: The redundancy weighting coefficient is calculated using the following formula: ; ; in, Represents the redundancy weighting coefficient. Represents the weighting coefficients of intermediate variables. Represents an intermediate variable. Represents the bias coefficient. Represents the similarity weight coefficient. Represents the average similarity. This represents the number of target text sections selected. This represents the average redundancy.
[0010] Preferably, the expression for the target gain function is: ; ; ; in, Represents the target gain function. This represents the vector of the text segment to be queried. Represents the set of target text vectors. Represents the similarity weight coefficient. This represents the sum of similarities between the vector of the query text segment and each text segment vector in the target text segment vector set. Represents the redundancy weighting coefficient. This represents the sum of redundancy among the various text segment vectors in the target text segment vector set. This represents the first element in the set of the query text vector and the set of the target text vector. Cosine similarity between the vectors of each text segment The first vector in the target text vector set The set of text segment vectors and the target text segment vectors. Cosine similarity between the vectors of each text segment.
[0011] Preferably, a greedy algorithm is used to sequentially select the text vectors that maximize the target gain function from the candidate text vector set and add them to the target text vector set, resulting in the greedily selected target text vector set, including: In each iteration, the text vector that maximizes the target gain function is selected from the candidate text vector set as the target text vector. It is then determined whether the marginal gain of the target text vector is greater than zero and whether adding the target text vector to the target text vector set satisfies the budget constraint. If the above conditions are met, the target text vector is added to the target text vector set. The loop ends when all text vectors in the candidate text vector set have been traversed or the budget constraint is satisfied, resulting in the greedy selection of the target text vector set.
[0012] Preferably, the expression for the marginal gain of the target text vector is: ; in, The marginal gain represents the target text vector. Represents the target text vector. Represents the set of target text vectors. Represents the similarity weight coefficient. This represents the cosine similarity between the vector of the query text and the vector of the target text. This represents the vector of the text segment to be queried. Represents the redundancy weighting coefficient. Represents the target text vector and the first element in the target text vector set. Cosine similarity between the vectors of each text segment.
[0013] A second aspect of this application provides a large model context generation apparatus, comprising: The acquisition module is used to obtain the vector of the text segment to be queried and the set of candidate text segment vectors; The calculation module is used to calculate the redundancy weight coefficient based on the budget constraints and the average length of the text segments in the candidate text segment vector set; The construction module is used to construct the target gain function based on the similarity weight coefficient and the redundancy weight coefficient; The initialization module is used to initialize the target text vector set to an empty set; The selection module is used to select the text vectors that maximize the target gain function from the candidate text vector set using a greedy algorithm and add them to the target text vector set, thus obtaining the target text vector set after greedy selection. The generation module is used to generate the context corresponding to the target text vector after the target text vector set is selected greedily.
[0014] A third aspect of this application provides an electronic device including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to implement the steps of the above-described method for generating a large model context when executing the computer program.
[0015] A fourth aspect of this application provides a storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described method for generating a large model context.
[0016] This application has at least the following beneficial effects: This application obtains a vector of the text to be queried and a set of candidate text vectors. Based on budget constraints and the average length of texts in the candidate text vector set, a redundancy weight coefficient is calculated. A target gain function is constructed based on the similarity weight coefficient and the redundancy weight coefficient. The target text vector set is initialized to an empty set. A greedy algorithm is used to sequentially select text vectors from the candidate text vector set that maximize the target gain function and add them to the target text vector set, resulting in a greedily selected target text vector set. Based on this greedily selected target text vector set, the context corresponding to the text vector to be queried is generated. The scheme of this application can reduce redundancy among selected texts, improve the diversity and utilization of evidence, thereby ensuring the quality and consistency of the generated context. Attached Figure Description
[0017] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments recorded in this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0018] Figure 1 This is a hardware structure diagram of the computer device that performs the method for generating large model contexts in the embodiments of this application; Figure 2 This is a flowchart illustrating the method for generating large model contexts in an embodiment of this application. Figure 3 This is a schematic diagram of the structure of the large model context generation device in the embodiments of this application. Detailed Implementation
[0019] To make the objectives, technical solutions, and advantages of this application clearer, the application is described and illustrated below with reference to the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the application. All other embodiments obtained by those skilled in the art based on the embodiments provided in this application without inventive effort are within the scope of protection of this application. Furthermore, it is understood that although the efforts made in such a development process may be complex and lengthy, for those skilled in the art related to the content disclosed in this application, modifications to design, manufacturing, or production based on the technical content disclosed in this application are merely conventional technical means and should not be construed as insufficient disclosure of the content of this application.
[0020] In this application, the reference to "embodiment" means that a specific feature, structure, or characteristic described in connection with an embodiment may be included in at least one embodiment of this application. The appearance of this phrase in various places in the specification does not necessarily refer to the same embodiment, nor is it a separate or alternative embodiment that is mutually exclusive with other embodiments. It will be explicitly and implicitly understood by those skilled in the art that the embodiments described in this application may be combined with other embodiments without conflict.
[0021] Unless otherwise defined, the technical or scientific terms used in this application shall have the ordinary meaning understood by one of ordinary skill in the art to which this application pertains. The terms “a,” “an,” “an,” “the,” and similar words used in this application do not indicate quantity limitation and may indicate singular or plural. The terms “comprising,” “including,” “having,” and any variations thereof used in this application are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or device that includes a series of steps or modules (units) is not limited to the listed steps or units, but may also include steps or units not listed, or may include other steps or units inherent to these processes, methods, products, or devices. The terms “connected,” “linked,” “coupled,” and similar words used in this application are not limited to physical or mechanical connections, but may include electrical connections, whether direct or indirect. “Multiple” used in this application means two or more. “And / or” describes the relationship between related objects, indicating that three relationships may exist; for example, “A and / or B” can represent: A alone, A and B simultaneously, and B alone. The terms “first,” “second,” “third,” etc., used in this application are merely to distinguish similar objects and do not represent a specific ordering of the objects.
[0022] The method embodiments provided in this application can be executed in a terminal, computer device, or similar computing device. Taking running on a computer device as an example, Figure 1 This is a hardware structure diagram of a computer device implementing the large model context generation method in this application embodiment. For example... Figure 1 As shown, the computer device includes a processor A01, a network interface A02, a display screen A04, an input device A05, and a memory (not shown) connected via a system bus. The processor A01 provides computing and control capabilities. The memory includes internal memory A03 and a non-volatile storage medium A06. The non-volatile storage medium A06 stores an operating system B01 and a computer program B02. The internal memory A03 provides an environment for the operation of the operating system B01 and the computer program B02 stored in the non-volatile storage medium A06. The network interface A02 is used to communicate with external terminals via a network connection. When the computer program is executed by the processor A01, it implements the method for generating a large model context according to embodiments of this application. The display screen A04 can be a liquid crystal display or an e-ink display. The input device A05 can be a touch layer covering the display screen, buttons, a trackball, or a touchpad mounted on the computer device casing, or an external keyboard, touchpad, or mouse.
[0023] Those skilled in the art will understand that Figure 1 The structure shown is merely a block diagram of a portion of the structure related to the present application and does not constitute a limitation on the computer device to which the present application is applied. Specific computer devices may include more or fewer components than those shown in the figure, or combine certain components, or have different component arrangements.
[0024] This application provides a method for generating large model contexts. Please refer to... Figure 2 , Figure 2 This is a flowchart illustrating the method for generating a large model context provided in this application embodiment. The method may include the following steps: S210. Obtain the vector of the text segment to be queried and the set of candidate text segment vectors.
[0025] In this embodiment of the application, after obtaining the text segment to be queried and the candidate text segments, they can be preprocessed respectively, including using regular expressions to match the text to remove stop words, remove extra whitespace, convert to traditional Chinese characters, etc., to obtain a set of vectors of the text segment to be queried and the candidate text segments after normalization.
[0026] S220. Calculate the redundancy weight coefficient based on the budget constraint and the average length of the text segments in the candidate text segment vector set.
[0027] S230. Construct the target gain function based on the similarity weight coefficient and the redundancy weight coefficient.
[0028] In this embodiment, when constructing the target gain function, the relevance and diversity of the query segment and candidate segments are taken into account. At the set level, the query relevance is maximized while redundancy between selected segments is penalized, which can improve the coverage of effective information. At the same time, the redundancy weight coefficient is calculated based on the budget constraint and the average segment length in the candidate segment vector set, which eliminates the need for tedious parameter tuning and can adapt to different budget constraints and candidate segment sets.
[0029] S240. Initialize the target text vector set to an empty set.
[0030] S250. Using a greedy algorithm, select the text vectors that maximize the target gain function from the candidate text vector set and add them to the target text vector set to obtain the target text vector set after greedy selection.
[0031] In this embodiment, a target text vector set is defined and initialized as an empty set. Once a target text vector that meets the conditions is selected, it is added to the target text vector set. Specifically, a greedy selection is performed based on budget constraints, ensuring that the target text vector set maximizes the target gain function.
[0032] S260. Based on the set of target text vectors selected by the greedy algorithm, generate the context corresponding to the text vector to be queried.
[0033] In this embodiment of the application, after obtaining the target text vector set, the text segments in the set can be reordered according to the sentence order, and combined with the text segment to be queried, the retrieval context of the RAG model can be generated.
[0034] As described above, the method for generating large model context provided in this application obtains the vector of the text to be queried and a set of candidate text vectors. Based on budget constraints and the average length of texts in the candidate text vector set, a redundancy weight coefficient is calculated. A target gain function is constructed based on the similarity weight coefficient and the redundancy weight coefficient. The target text vector set is initialized to an empty set. A greedy algorithm is used to sequentially select text vectors from the candidate text vector set that maximize the target gain function and add them to the target text vector set, resulting in a greedily selected target text vector set. Based on this greedily selected target text vector set, the context corresponding to the text vector to be queried is generated. Using the scheme of this application can reduce redundancy among selected texts, improve the diversity and utilization of evidence, thereby ensuring the quality and consistency of the generated context.
[0035] Optionally, in the above embodiments, the method further includes the following steps: S2001. Obtain the initial set of text segment vectors.
[0036] S2002. Calculate the similarity between the vector of the text segment to be queried and each text segment vector in the initial set of text segment vectors, and sort the text segment vectors in the initial set of text segment vectors according to the similarity to obtain the sorting result.
[0037] S2003. Based on the sorting results, select candidate text vectors from the initial text vector set to obtain the candidate text vector set.
[0038] In this embodiment, the initial text vector set contains a large number of text vectors. The similarity between the vector of the text to be queried and each text vector in the initial text vector set can be calculated first. Then, several candidate text vectors with the highest similarity to the vector of the text to be queried are selected to form a candidate text vector set. Specifically, similarity can be calculated using cosine similarity, dot product, kernel function, or Jaccard-n-gram.
[0039] Optionally, in the above embodiments, the redundancy weight coefficient calculated in step S220 based on the budget constraint and the average length of the text segments in the candidate text segment vector set may include the following steps: S2201. Calculate the number of target text segments to be selected based on the budget constraint and the average length of the text segments in the candidate text segment vector set.
[0040] S2202. Calculate the average similarity between the vector of the text to be queried and each vector of the candidate text vector set, as well as the average redundancy between each vector of the candidate text vector set.
[0041] S2203. Calculate the redundancy weight coefficient based on the similarity weight coefficient, the number of target text segments selected, the average similarity value, and the average redundancy value.
[0042] Furthermore, in the above embodiments, the redundancy weight coefficient is calculated using the following formula: ; ;
[0043] in, Represents the redundancy weighting coefficient. Represents the weighting coefficients of intermediate variables. Represents an intermediate variable. Represents the bias coefficient. Represents the similarity weight coefficient. Represents the average similarity. This represents the number of target text sections selected. This represents the average redundancy.
[0044] In this embodiment of the application, the candidate segment vector set is calculated. Average length of text in Chinese Then, based on budget constraints Using the formula The number of target text segments selected can be calculated. .
[0045] Then, according to the formula , Calculate the vector of the text segment to be queried respectively With candidate text segment vector set The average similarity between the vectors of each text segment and candidate text vector set Average redundancy between the vectors of each text segment ,in, Represents the vector of the text segment to be queried With candidate text segment vector set The Middle Cosine similarity between the vectors of each text segment Represents the set of candidate text segment vectors The Middle The vector of the first text segment and the first... Cosine similarity between the vectors of each text segment.
[0046] In practice, the similarity weighting coefficient It can be set to 0.5~2.0, with the default intermediate variable weighting coefficient. Bias coefficient Based on similarity weight coefficients Number of target texts selected Average similarity and average redundancy The redundancy weight coefficient can be calculated using the above formula. .
[0047] Understandably, the redundancy weight coefficient is automatically calculated based on the statistics of the candidate text segment vector set (i.e., the average similarity and the average redundancy) and the number of target text segments selected. This allows the redundancy weight coefficient to be adaptively adjusted according to the source domain, document level, and paragraph span of the candidate text segments, without the need for cumbersome parameter tuning, and to adapt to different budget constraints and candidate text segment sets.
[0048] Optionally, in the above embodiments, the expression for the target gain function is: ; ; ;
[0049] in, Represents the target gain function. This represents the vector of the text segment to be queried. Represents the set of target text vectors. Represents the similarity weight coefficient. This represents the sum of similarities between the vector of the query text segment and each text segment vector in the target text segment vector set. Represents the redundancy weighting coefficient. This represents the sum of redundancy among the various text segment vectors in the target text segment vector set. This represents the first element in the set of the query text vector and the set of the target text vector. Cosine similarity between the vectors of each text segment The first vector in the target text vector set The set of text segment vectors and the target text segment vectors. Cosine similarity between the vectors of each text segment.
[0050] In the embodiments of this application, when constructing the target gain function, similarity and The similarity between the query segment vector and the target segment vector set is defined to ensure the relevance between each segment in the target segment vector set and the query segment; redundancy and The redundancy level of each segment vector in the target segment vector set is defined to ensure that redundant components are avoided as much as possible in each segment. In other words, it takes into account the relevance and diversity of the query segment and the candidate segments, maximizing query relevance and penalizing redundancy between selected segments at the set level, which can improve the coverage of effective information.
[0051] Optionally, in the above embodiments, step S250, which uses a greedy algorithm to sequentially select the text vectors that maximize the target gain function from the candidate text vector set and add them to the target text vector set to obtain the greedily selected target text vector set, may include the following steps: In each iteration, the text vector that maximizes the target gain function is selected from the candidate text vector set as the target text vector. It is then determined whether the marginal gain of the target text vector is greater than zero and whether adding the target text vector to the target text vector set satisfies the budget constraint. If the above conditions are met, the target text vector is added to the target text vector set.
[0052] The loop ends when all text vectors in the candidate text vector set have been traversed or the budget constraint is satisfied, resulting in the greedy selection of the target text vector set.
[0053] Furthermore, in the above embodiments, the expression for the marginal gain of the target text vector is: ;
[0054] in, The marginal gain represents the target text vector. Represents the target text vector. Represents the set of target text vectors. Represents the similarity weight coefficient. This represents the cosine similarity between the vector of the query text and the vector of the target text. This represents the vector of the text segment to be queried. Represents the redundancy weighting coefficient. Represents the target text vector and the first element in the target text vector set. Cosine similarity between the vectors of each text segment.
[0055] In this embodiment of the application, from the candidate text segment vector set The text vector that maximizes the target gain function is selected as the target text vector, defined as follows: Then, the marginal gain is calculated using the above formula, that is, the increment of the target gain function is calculated, and it is determined whether the increment of the target gain function is greater than 0. If so, it is further determined whether the target text vector is... Add to target text vector set Then, the target text vector set Is the total length of the Chinese segment less than or equal to the budget constraint? If all of the above conditions are met, then the target text vector will be... Add to target text vector set In the middle. When the candidate text vector set All text vectors have been traversed or the target text vector set has been traversed. The total length of the Chinese segment is greater than the budget constraint. The loop ends when the condition is met, resulting in the set of target text vectors after the greedy selection.
[0056] In practice, the length of the segment can be calculated using estimators such as BPE (Byte Pair Encoding) or CL100K.
[0057] It should be noted that the upper bound of the pairwise similarity of the candidates is... The maximum number of target text sections to be selected is Under the condition that the objective function is applicable to any and satisfy: ;
[0058] Right now -Approximate submodularity, combined with redundancy weighting coefficient , It can be instantiated and controlled within a small range, allowing the greedy algorithm to achieve an approximate optimal solution with an approximation ratio greater than 63%, and guaranteeing the near-optimality of the greedy solution under common embedding distributions.
[0059] This application also provides an apparatus for generating large model contexts. Please refer to... Figure 3 , Figure 3 This is a schematic diagram of the structure of the large model context generation device in an embodiment of this application. The training device may include: The acquisition module 310 is used to acquire the vector of the text segment to be queried and the set of candidate text segment vectors; The calculation module 320 is used to calculate the redundancy weight coefficient based on the budget constraint and the average length of the text segments in the candidate text segment vector set; Construction module 330 is used to construct the target gain function based on the similarity weight coefficient and the redundancy weight coefficient; Initialization module 340 is used to initialize the target text vector set to an empty set; The selection module 350 is used to select the text vectors that maximize the target gain function from the candidate text vector set using a greedy algorithm and add them to the target text vector set to obtain the target text vector set after greedy selection. The generation module 360 is used to generate the context corresponding to the target text vector to be queried based on the target text vector set selected by greed.
[0060] It should be noted that the apparatus provided in the above embodiments is only illustrated by the division of the above program modules when performing related operations. In actual applications, the above processing can be assigned to different program modules as needed, that is, the internal structure of the terminal can be divided into different program modules to complete all or part of the processing described above. In addition, the apparatus provided in the above embodiments and the method embodiments in the above embodiments belong to the same concept, and the specific implementation process can be found in the method embodiments, which will not be repeated here.
[0061] Based on the hardware implementation of the above program modules, and in order to implement the method of the embodiments of this application, the embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to implement the steps of the above-described method for generating a large model context when executing the computer program.
[0062] In an exemplary embodiment, the processor may be implemented by one or more application-specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), field-programmable gate arrays (FPGAs), general-purpose processors, controllers, microcontrollers (MCUs), microprocessors, or other electronic components to perform the aforementioned method.
[0063] It is understood that the memory in the embodiments of this application can be volatile memory or non-volatile memory, or both. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), ferromagnetic random access memory (FRAM), flash memory, magnetic surface memory, optical disc, or compact disc read-only memory (CD ROM); magnetic surface memory can be disk storage or magnetic tape storage. Volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static Random Access Memory (SRAM), Synchronous Static Random Access Memory (SSRAM), Dynamic Random Access Memory (DRAM), Synchronous Dynamic Random Access Memory (SDRAM), Double Data Rate Synchronous Dynamic Random Access Memory (DDRSDRAM), Enhanced Synchronous Dynamic Random Access Memory (ESDRAM), SyncLink Dynamic Random Access Memory (SLDRAM), and Direct Rambus Random Access Memory (DRRAM).The memories described in the embodiments of this application are intended to include, but are not limited to, these and any other suitable types of memories.
[0064] In one embodiment, this application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the steps of the above-described method for generating a large model context.
[0065] It is understood that the computer-readable storage medium in the embodiments of this application includes permanent and non-permanent, removable and non-removable media, and information storage can be implemented by any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change memory (PRAM), static random access memory (SRAM), dynamic random access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile optical disc (DVD) or other optical storage, magnetic tape, magnetic magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device. As defined herein, computer-readable storage media does not include transient media, such as modulated data signals and carrier waves.
[0066] Those skilled in the art should understand that this application is not limited to the above embodiments. The embodiments and descriptions in the specification are merely illustrative of the principles of this application. Various changes and modifications can be made to this application without departing from the spirit and scope thereof, and all such changes and modifications fall within the scope of this application as claimed. The scope of protection of this application is defined by the appended claims and their equivalents.
Claims
1. A method for generating a large model context, characterized in that, The method comprises the following steps: obtaining a query text segment vector and a candidate text segment vector set; calculating a redundancy weight coefficient according to a budget constraint and an average length of text segments in the candidate text segment vector set; constructing a target gain function according to the similarity weight coefficient and the redundancy weight coefficient; initializing a target text segment vector set as an empty set; selecting, by using a greedy algorithm, a text segment vector from the candidate text segment vector set to join the target text segment vector set to obtain a greedy selection target text segment vector set, so that the target gain function is maximized; generating a context corresponding to the query text segment vector according to the greedy selection target text segment vector set.
2. The method of Claim 1, wherein, The method further comprises the following steps: obtaining an initial text segment vector set; calculating a similarity between the query text segment vector and each text segment vector in the initial text segment vector set, and sorting the text segment vectors in the initial text segment vector set according to the similarity to obtain a sorting result; selecting a candidate text segment vector from the initial text segment vector set according to the sorting result to obtain the candidate text segment vector set.
3. The method of Claim 1, wherein, The step of calculating the redundancy weight coefficient according to the budget constraint and the average length of text segments in the candidate text segment vector set comprises the following steps: calculating a target text segment selection number according to the budget constraint and the average length of text segments in the candidate text segment vector set; calculating an average similarity between the query text segment vector and each text segment vector in the candidate text segment vector set and an average redundancy between each text segment vector in the candidate text segment vector set; calculating the redundancy weight coefficient according to the similarity weight coefficient, the target text segment selection number, the average similarity and the average redundancy.
4. The method of Claim 3, wherein, The step of calculating the redundancy weight coefficient according to the similarity weight coefficient, the target text segment selection number, the average similarity and the average redundancy comprises the following step: calculating the redundancy weight coefficient by using the following formula: ; ; wherein, represents the redundancy weight coefficient, represents the intermediate variable weight coefficient, represents the intermediate variable, represents the bias coefficient, represents the similarity weight coefficient, represents the similarity average value, represents the target text segment selection number, represents the redundancy average value.
5. The method of claim 1, wherein, The expression of the target gain function is as follows: ; ; ; wherein, represents the target gain function, represents the to-be-queried text segment vector, represents the target text segment vector set, represents the similarity weight coefficient, represents the similarity sum between the to-be-queried text segment vector and each text segment vector in the target text segment vector set, represents the redundancy weight coefficient, represents the redundancy sum between each text segment vector in the target text segment vector set, represents the cosine similarity between the to-be-queried text segment vector and the first text segment vector in the target text segment vector set, represents the cosine similarity between the first text segment vector in the target text segment vector set and the second text segment vector in the target text segment vector set.
6. The method of Claim 1, wherein, The step of selecting, by using a greedy algorithm, a text segment vector from the candidate text segment vector set to join the target text segment vector set to obtain a greedy selection target text segment vector set, so that the target gain function is maximized comprises the following steps: in each loop, selecting a text segment vector from the candidate text segment vector set to join the target text segment vector set to obtain a target text segment vector, and judging whether a marginal gain of the target text segment vector is greater than zero and whether the budget constraint is satisfied after the target text segment vector is added to the target text segment vector set, if the above conditions are satisfied, the target text segment vector is added to the target text segment vector set; ending the loop when all text segment vectors in the candidate text segment vector set are traversed or the budget constraint is satisfied, to obtain the greedy selection target text segment vector set.
7. The method of Claim 6, wherein, The expression of the marginal gain of the target text segment vector is as follows: ; in, This represents the marginal gain of the target text vector. Represents the target text vector. Represents the set of target text vectors. This represents the similarity weight coefficient. The cosine similarity represents the relationship between the query vector and the target vector. This represents the vector of the text segment to be queried. This represents the redundancy weighting coefficient. Represents the target text vector and the first element in the set of target text vectors. Cosine similarity between the vectors of each text segment.
8. A large model context generation apparatus, characterized by, The method comprises the following steps: an obtaining module, configured to obtain a query text segment vector and a candidate text segment vector set; a calculating module, configured to calculate a redundancy weight coefficient according to a budget constraint and an average length of text segments in the candidate text segment vector set; A construction module is configured to construct a target gain function according to the similarity weight coefficient and the redundancy weight coefficient; An initialization module is configured to initialize a target text segment vector set as an empty set; A selection module is configured to use a greedy algorithm to select a text segment vector from the candidate text segment vector set to join the target text segment vector set in sequence so as to maximize the target gain function, and obtain a target text segment vector set after greedy selection; A generation module is configured to generate a context corresponding to the to-be-queried text segment vector according to the target text segment vector set after greedy selection.
9. An electronic device comprising a memory and a processor, characterized in that, The memory stores a computer program, and the processor is configured to run the computer program to execute the method for generating a context of a large model according to any one of claims 1 to 7.
10. A storage medium, characterized by The storage medium stores a computer program, and the computer program is configured to run to execute the method for generating a context of a large model according to any one of claims 1 to 7.