A method and apparatus for constructing a code RAG library

By building a code RAG library, parsing the metadata of code projects and generating link topology diagrams, the shortcomings of RAG technology in code retrieval are solved, multi-level parsing and accurate retrieval of code entities are realized, and the accuracy of code retrieval is improved.

CN122173589APending Publication Date: 2026-06-09ASIAINFO TECH CHINA INC

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
ASIAINFO TECH CHINA INC
Filing Date
2026-03-10
Publication Date
2026-06-09

AI Technical Summary

Technical Problem

Existing RAG technology lacks effective application when processing code data with certain structural rules, and cannot achieve accurate code retrieval.

Method used

By parsing the root directory of the code project, metadata at different levels is extracted and a link topology diagram is constructed, including class inheritance relationships and method call relationships. This is then converted into a searchable index and combined with the large model to generate summary data, thus building a code RAG library.

Benefits of technology

It enables multi-level entity parsing and accurate code retrieval in code projects, filling the gap in the field where code data with certain structural rules cannot be retrieved, and improving the accuracy of code retrieval.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN122173589A_ABST
    Figure CN122173589A_ABST
Patent Text Reader

Abstract

This application discloses a method and apparatus for constructing a code RAG library. The method includes: parsing the root directory of a code project, extracting metadata at different levels from the code, and determining the relationships between code entities contained in the metadata; constructing a link topology graph covering the entire code project based on the code entities and the relationships, wherein the relationships include class inheritance relationships and method call relationships; processing the link topology graph into a searchable index, and storing the searchable index in combination with code data into a code RAG library. This solution achieves multi-level entity parsing of code in a code project, constructs a link topology graph that can represent the relationships between code entities in the code, and processes the link topology graph into a searchable index, realizing the searchability of the structure. This fills the gap in the field where code data cannot be searched, and ensures the accuracy of code retrieval based on the constructed code RAG library.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and more specifically, to a method and apparatus for constructing a code RAG library. Background Technology

[0002] RAG (Retrieval-Augmented Generation) technology enhances the dynamic knowledge capabilities of the model by combining external knowledge base retrieval with large language model generation. Its core process includes: document segmentation: dividing content according to text semantic boundaries (such as fixed length / heading level); vectorized storage: using an embedding model to convert text blocks into vectors and store them in the database; hybrid retrieval: combining keyword matching and vector similarity to recall context; and generative enhancement: injecting retrieval results into LLM prompts to generate answers.

[0003] Current RAG technology only performs well in applications involving regular text content; it has no effective application for code data with certain structural rules. Therefore, how to provide a solution that allows RAG technology to be applied to code management has become a problem that professionals in the field need to consider. Summary of the Invention

[0004] In view of the above, this application provides the following technical solution:

[0005] The first aspect of this application provides a method for constructing a code RAG library, including:

[0006] The code project root directory is parsed, metadata at different levels is extracted from the code, and the relationships between the code entities contained in the metadata are determined.

[0007] Based on the code entities and the relationships, a link topology diagram covering the entire code project is constructed, wherein the relationships include class inheritance relationships and method call relationships;

[0008] The link topology map is processed and converted into a searchable index, and the searchable index is combined with code data and stored in the code RAG library.

[0009] In one possible implementation, the different levels of metadata include module-level data, file-level data, and method-level data;

[0010] The module-level data includes module information and file package information; the file-level data includes class information, reference information, and class variable information; the method-level data includes method information, method input and output parameter information, and call relationship information.

[0011] In one possible implementation, the link topology graph is processed into a searchable index, including:

[0012] The class inheritance and method call relationships indicated in the link topology diagram are encoded as index paths.

[0013] In one possible implementation, the process of converting the link topology map into a searchable index includes:

[0014] Based on the link topology graph, a hierarchical index is obtained, including:

[0015] For the module layer nodes in the link topology diagram, create an inverted index based on keywords;

[0016] For the class-layer nodes in the link topology graph, create a vector index based on the context vector;

[0017] For the method layer nodes in the link topology graph, a graph index that integrates semantic vectors and topological relationships is created.

[0018] In one possible implementation, in the hierarchical indexing implementation within the constructed code RAG library, the cross-level retrieval route weights are determined based on a first term and a second term, wherein the first term characterizes the semantic similarity between the query data and the method name / method description, and the second term characterizes the hierarchical weight of the matching module or the importance of the method in the link topology graph.

[0019] One possible implementation also includes:

[0020] The large model is invoked to generate summary data of the code entity based on the link topology diagram and stored in the code RAG library. The summary data stored in the code RAG library is associated with the corresponding code entity.

[0021] In one possible implementation, the invocation of the large model generates summary data of the code entities based on the link topology graph and stores it in the code RAG library, including:

[0022] Based on the link topology diagram, prompt word data with the same format as the preset prompt word template is obtained. The prompt word data includes at least the code entity and its corresponding association relationship.

[0023] The large model is invoked to generate summary data based on the prompt word data. The summary data includes summary content and keyword tags extracted from the summary content.

[0024] In one possible implementation, the step of processing the link topology map to obtain prompt word data with the same format as the preset prompt word template includes:

[0025] The context data of the code entity is determined based on the association relationship, and the context data includes at least one of the following elements: parent class, child class, and method call;

[0026] The various elements contained in the context data are injected into the corresponding positions of the prompt word template to obtain the prompt word data.

[0027] A second aspect of this application provides a construction apparatus for a code RAG library, comprising:

[0028] The code parsing module is used to parse the root directory of the code project, extract metadata at different levels from the code, and determine the relationships between the code entities contained in the metadata.

[0029] The graph construction module is used to construct a link topology graph covering the entire code project based on the code entities and the relationships, wherein the relationships include class inheritance relationships and method call relationships;

[0030] The code library building module is used to process the link topology map into a searchable index, and store the searchable index in combination with code data into the code RAG library.

[0031] One possible implementation also includes:

[0032] The summary generation module is used to call the large model to generate summary data of the code entity based on the link topology diagram and store it in the code RAG library. The summary data stored in the code RAG library is associated with the corresponding code entity.

[0033] As can be seen from the above technical solutions, this application discloses a method and apparatus for constructing a code RAG library. The solution realizes multi-level entity parsing of code in code engineering. The constructed code RAG library can be used for subsequent code retrieval and matching. Compared with the traditional solution of directly vectorizing the original code text, this application solution constructs a link topology graph that can represent the relationship between code entities in the code, and processes the link topology graph into a searchable index, realizing the searchability of the structure. This not only fills the gap in the field where code data with certain structural rules cannot be retrieved, but also ensures the accuracy of code retrieval based on the constructed code RAG library. Attached Figure Description

[0034] 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 embodiments of this application. For those skilled in the art, other drawings can be obtained based on the provided drawings without creative effort.

[0035] Figure 1 This is a flowchart illustrating a method for constructing a code RAG library as disclosed in an embodiment of this application;

[0036] Figure 2 This is a flowchart illustrating the generation of digest data as disclosed in an embodiment of this application;

[0037] Figure 3 This is a schematic diagram of the overall technical structure of the code RAG library construction scheme disclosed in the embodiments of this application;

[0038] Figure 4 This is a schematic diagram of the structure of a code RAG library construction device disclosed in an embodiment of this application. Detailed Implementation

[0039] For the sake of clarity and citation, the explanations, abbreviations, or acronyms used in the following text are summarized below:

[0040] CodeBERT: A pre-trained model for bimodal understanding of programming languages ​​(PL) and natural language (NL), designed specifically for tasks involving semantic association between code and text.

[0041] HNSW Index: HNSW stands for Hierarchical Navigable Small World. It is an approximate nearest neighbor (ANN) indexing algorithm used for high-dimensional vector similarity search. It is widely used in vector databases to achieve a good balance between speed, accuracy and scalability.

[0042] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0043] Figure 1 This is a flowchart illustrating a method for constructing a code RAG library as disclosed in an embodiment of this application. See also... Figure 1 As shown, the methods for building a code RAG library can include:

[0044] Step 101: Parse the root directory of the code project, extract metadata at different levels from the code, and determine the relationships between the code entities contained in the metadata.

[0045] Parsing the root directory of a code project can be done using existing or future code parsing tools in the field, such as using an Abstract Syntax Tree (AST) for structured parsing. The purpose of structured code parsing is to extract the syntactic elements and their relationships according to the syntax of the coding language. The parsing result is concrete data that can be stored in a relational database (such as MySQL).

[0046] This application's solution obtains the root directory of the code project, and then scans the root directory using a scanning tool such as the AST tool or other identification tools to obtain the corresponding project files and module information, extracting module-level data, file-level data, and method-level data respectively. The project files and module information contain hierarchical information, such as which modules are included in the current module layer, which class files are contained in each module, and the relationships between modules and other modules, etc.

[0047] The hierarchy, from highest to lowest, can include module level, file level, and method level. Each level contains corresponding metadata. Module-level metadata includes, but is not limited to, module name and description; file-level metadata includes, but is not limited to, file path, name, type, and content description; and method-level metadata includes, but is not limited to, method call relationships, method summaries, and method code. The relationships may include, but are not limited to, which class file the code entity inherits from and which method was called.

[0048] Step 102: Based on the code entities and the relationships, construct a link topology diagram covering the entire code project. The relationships include class inheritance relationships and method call relationships.

[0049] After obtaining the code entities and their associated relationships from the code data, it is possible to determine the multi-level topological relationships of the code entities and construct a link topology diagram covering the entire code project. For example, the solution in this application can obtain the class inheritance relationship and method call chain across files through AST parsing, map import (a keyword in programming languages ​​that represents a dependency import) dependencies to inter-module call edges; and generate a globally unique ID for each code entity (corresponding code entity): module name::class name::method name.

[0050] The link topology diagram realizes a cross-file topology map (class inheritance / call chain), making the hierarchical and relational relationships between all entities in the entire project's code structure clearer and more intuitive, providing a foundation for efficient and accurate code retrieval based on the built code RAG library.

[0051] Step 103: Convert the link topology map into a searchable index, and store the searchable index in combination with the code data into the code RAG library.

[0052] After obtaining the link topology map, it can be converted into a searchable index. This allows for accurate and efficient retrieval of code data in the code RAG library based on the converted searchable index, filling the gap in the field where code data with certain structural rules cannot be retrieved.

[0053] Traditional solutions often lack an implementation to transform AST topological relationships (class inheritance / method calls) into a searchable index. This leads to multi-hop queries (such as "find the module that called this method") relying on large language models for autonomous reasoning, resulting in a high error rate. Furthermore, the search results cannot reconstruct the code-level topology, requiring engineers to manually reassemble the logic.

[0054] The code RAG library construction method provided in this embodiment realizes multi-level entity parsing of code in code engineering. The constructed code RAG library can be used for subsequent code retrieval and matching. Compared with the traditional solution of directly vectorizing the original code text, the solution of this application constructs a link topology graph that can represent the relationship between code entities in the code, and processes the link topology graph into a searchable index, realizing the searchability of the structure. This not only fills the gap in the field where code data with certain structural rules cannot be retrieved, but also ensures the accuracy of code retrieval based on the constructed code RAG library.

[0055] In one implementation, the metadata at different levels includes module-level data, file-level data, and method-level data; the module-level data includes module information and file package information; the file-level data includes class information, reference information, and class variable information; and the method-level data includes method information, method input and output parameter information, and call relationship information.

[0056] It is understood that the method for constructing the code RAG library provided in this application is based on existing code structures. The solution is also applicable to searchable implementations of other types of data with certain structural rules, besides code data. Furthermore, if the code structure changes or is adjusted in the future as the code field develops, based on the implementation principles disclosed in this application, only minor adjustments to some configurations in the implementation scheme are needed to construct a similar code RAG library applicable to code retrieval. This application does not impose fixed limitations on the specific applicable scenarios for constructing the code RAG library.

[0057] In one implementation, converting the link topology graph processing into a searchable index may include: encoding the class inheritance and method call relationships indicated in the link topology graph into index paths. Converting the link topology graph processing into a searchable index may also include: obtaining a hierarchical index based on the link topology graph processing.

[0058] Specifically, the acquisition of searchable indexes includes, but is not limited to: creating keyword-based inverted indexes for module layer nodes in the link topology graph; creating vector indexes based on context vectors, such as HNSW indexes based on CodeBERT vectors, for class layer nodes in the link topology graph; and creating graph indexes that integrate semantic vectors and topological relationships for method layer nodes in the link topology graph.

[0059] In the implementation of hierarchical indexing in the constructed code RAG library, the cross-level retrieval route weight is determined based on the first and second terms, where the first term represents the semantic similarity between the query data and the method name / method description, and the second term represents the hierarchical weight of the matching module or the importance of the method in the link topology graph.

[0060] Specifically, the cross-level retrieval routing weight formula is: S = α·V_sim + β·R_weight; this formula is used in both the class and method layers. Here, α + β = 1. α + β = 1 is to ensure that the final score S is within a comparable range, allowing for fair ranking and comparison of retrieval results from different levels. This is essentially a standard constraint for linear weighted sums.

[0061] in:

[0062] α-semantic similarity weight

[0063] Representation: The degree of semantic matching between the query intent and the code entity itself;

[0064] Specifically, at the class level: the semantic similarity between the user question and the class name / class description (calculated using CodeBERT); at the method level: the semantic similarity between the user question and the method name / method description.

[0065] Physical meaning: Direct relevance, measuring the semantic distance between the user's question "what is being said" and the code entity "what is being said".

[0066] β - Hierarchical Relationship Weight

[0067] Representation: The structural importance of code entities within the system architecture;

[0068] Specifically, at the class level: R_weight = the hierarchical weight of the matching module, reflecting the inclusion relationship constraint from module to class; at the method level: R_weight = topological relationship weight × the hierarchical weight of the matching class, reflecting the inclusion relationship from class to method and the structural importance of the method in the call graph.

[0069] Physical meaning: Indirect correlation, which measures the structural position, calling relationship and architectural role of code entities in the system.

[0070] The rules for applying the above formulas are as follows:

[0071] 1. Class layer

[0072] `V_sim` = Semantic similarity between user question and class name / class description (CodeBERT vector similarity);

[0073] `R_weight` = **the hierarchical weight of the matching module** (the normalized score of this module in the module-level retrieval results).

[0074] Formula meaning: The class layer score is constrained by the module layer matching result, and only classes that match the module layer are calculated.

[0075] 2. Method layer

[0076] `V_sim` = Semantic similarity between user question and method name / method description (CodeBERT vector similarity);

[0077] `R_weight` = **topological relation weight × hierarchical weight of matching class**;

[0078] Topological relation weights: the importance of a method in the call graph (e.g., entry method = 1.0, called method = 0.7).

[0079] Hierarchical weight of the matching class: the normalized score of this class in the class-level retrieval results;

[0080] Formula meaning: The method layer score is constrained by the class layer matching result, and only methods that match the class layer are calculated.

[0081] Based on the foregoing, for user code retrieval input, a hierarchical progressive retrieval routing algorithm can be implemented based on multi-level vectorized index data, according to the code retrieval path and weight.

[0082] In implementation, an inverted index structure can be used first to establish a mapping relationship between "keywords → module IDs". When a user queries for terms containing specific terms (such as "payment" or "log"), the relevant module can be directly located. Secondly, the CodeBERT model is used to encode class and method summaries into 768-dimensional vectors, and an HNSW graph index is used to achieve approximate nearest neighbor search for the high-dimensional vectors.

[0083] In one implementation, the method for constructing the code RAG library may further include: calling a large model to generate summary data of the code entities based on the link topology diagram and storing it in the code RAG library, wherein the summary data stored in the code RAG library is associated with the corresponding code entities.

[0084] Figure 2 This is a flowchart illustrating the generation of digest data as disclosed in an embodiment of this application. See also... Figure 2 As shown, the process of calling the large model to generate summary data of the code entities based on the link topology diagram and storing it in the code RAG library may include:

[0085] Step 201: Based on the link topology map, process to obtain prompt word data with the same format as the preset prompt word template. The prompt word data includes at least the code entity and its corresponding association relationship.

[0086] In implementation, the context data of the code entity can be determined based on the association relationship. The context data includes at least one of the following elements: parent class, child class, and calling method. Then, the elements contained in the context data are injected into the corresponding positions of the prompt word template to obtain the prompt word data.

[0087] Specifically, based on the code entities and relational data parsed from the AST, class and method summary generation prompt templates can be created (which may include, but are not limited to, the three elements of functional description, input / output, and relationships). Specific code entity and relational information are then concatenated onto the prompt templates, and a large language model with good summary generation performance is selected for generating the summary data. Index data is then created in the Milvus vector library.

[0088] For example, in Java, there's a three-layer structure: "package—class—method." If we only perform semantic summarization on method code (using a large model), and simply provide the method code to the large model for understanding and summarization, then contextual information is missing. Therefore, we can design prompt word templates that assemble the method's class, package, and call chain into a complete prompt word for the large model to summarize semantic information.

[0089] Step 202: Call the large model to generate summary data based on the prompt word data. The summary data may include summary content and keyword tags extracted from the summary content.

[0090] After obtaining the prompt word data, it can be used as input to a large model. Leveraging the model's generative capabilities, summary data corresponding to each code entity is generated and stored in the code RAG library. The summary data stored in the code RAG library can then be used for retrieval and matching when the user inputs query data. The keyword tags are also used to achieve fast and accurate matching during the retrieval process.

[0091] In this implementation, entity-level semantic summaries are generated based on a large language model. The generation and storage of summary data solve the problems of insufficient semantic abstraction of code data, such as failure to distinguish similar entities (e.g., Order.calculate() vs. Cart.calculate()) and reduced upper limit of vector retrieval accuracy.

[0092] Table 1

[0093]

[0094] Table 1 shows the main technical points of the technical solution of this application. It can be seen that in the traditional solution, the topological relationship of AST parsing is not converted into a searchable index (leading to P2 / P3 problems); from the semantic level, there is a lack of entity-level abstract representation (leading to P1 / P4 problems).

[0095] This application's solution, through a three-pronged architecture of "global AST map + LLM summary + hierarchical index," achieves for the first time: structural searchability, i.e., encoding class inheritance / method call relationships into index paths; semantic distinguishability, improving the distance between entity vectors with the same name through summary generation; and cognitive alignment, with hierarchical search paths matching engineers' thinking patterns (module → class → method). Figure 3 This is a schematic diagram of the overall technical structure of the code RAG library construction scheme disclosed in the embodiments of this application, which can be combined with... Figure 3 Understand the relevant sections of this application.

[0096] In summary, the technical solution of this application can be directly applied to code structured parsing, semantic storage, and hierarchical retrieval scenarios. It enables the construction of a code RAG library that extracts code topological relationships through abstract syntax trees and generates high-quality semantic summaries by combining large language models. The solution represents an innovative application of RAG technology in code retrieval scenarios, focusing on solving the problems of vectorized representation of specific code structures (such as class / method hierarchy and call relationships), hybrid index architecture design, and hierarchical retrieval strategy optimization. It involves AST-based code structure parsing technology, LLM-driven code summarization generation methods, and graph representation of code entity relationships (class inheritance, method calls, etc.). It covers a collaborative management mechanism for unstructured code semantic vectors (stored in the Milvus library) and structured code topological relationships (AST parsed data), enabling dual-dimensional storage and joint retrieval of code knowledge.

[0097] For the foregoing method embodiments, in order to simplify the description, they are all described as a series of actions. However, those skilled in the art should understand that this application is not limited to the described order of actions, because according to this application, some steps can be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily essential to this application.

[0098] The methods described in the above-disclosed embodiments of this application are detailed in terms of the methods. The methods of this application can be implemented by various forms of apparatus. Therefore, this application also discloses an apparatus. Specific embodiments are given below for detailed description.

[0099] Figure 4 This is a schematic diagram of a structure for building a code RAG library according to an embodiment of this application. See also... Figure 4 As shown, the build apparatus 40 for the code RAG library may include:

[0100] The code parsing module 401 is used to parse the root directory of the code project, extract metadata at different levels from the code, and determine the association between the code entities contained in the metadata.

[0101] The graph construction module 402 is used to construct a link topology graph covering the entire code project based on the code entities and the association relationships, wherein the association relationships include class inheritance relationships and method call relationships.

[0102] The code library building module 403 is used to process the link topology map into a searchable index and store the searchable index in combination with code data into the code RAG library.

[0103] The code RAG library construction device provided in this embodiment realizes multi-level entity parsing of code in code engineering. The constructed code RAG library can be used for subsequent code retrieval and matching. Compared with the traditional solution of directly vectorizing the original code text, the solution of this application constructs a link topology graph that can represent the relationship between code entities in the code, and processes the link topology graph into a searchable index, realizing the searchability of the structure. This not only fills the gap in the field where code data with certain structural rules cannot be retrieved, but also ensures the accuracy of code retrieval based on the constructed code RAG library.

[0104] In one implementation, the metadata at different levels includes module-level data, file-level data, and method-level data; the module-level data includes module information and file package information; the file-level data includes class information, reference information, and class variable information; and the method-level data includes method information, method input and output parameter information, and call relationship information.

[0105] In one implementation, the codebase building module can be specifically used to encode the class inheritance and method call relationships indicated in the link topology graph into index paths.

[0106] In one implementation, the codebase building module may include: a hierarchical indexing module, used to obtain a hierarchical index based on the link topology graph, including: creating a keyword-based inverted index for module layer nodes in the link topology graph; creating a context vector-based vector index for class layer nodes in the link topology graph; and creating a graph index that integrates semantic vectors and topological relationships for method layer nodes in the link topology graph.

[0107] In one implementation, in the hierarchical indexing implementation of the constructed code RAG library, the cross-level retrieval route weight is determined based on a first term and a second term, wherein the first term represents the semantic similarity between the query data and the method name / method description, and the second term represents the hierarchical weight of the matching module or the importance of the method in the link topology graph.

[0108] In one implementation, the apparatus for building the code RAG library may further include: a summary generation module, used to call a large model to generate summary data of the code entities based on the link topology diagram and store it in the code RAG library, wherein the summary data stored in the code RAG library is associated with the corresponding code entities.

[0109] In one implementation, the summary generation module may include: a prompt word generation module, used to process the link topology map to obtain prompt word data with the same format as a preset prompt word template, wherein the prompt word data includes at least the code entity and its corresponding association relationship; and a summary generation submodule, used to call a large model to generate summary data based on the prompt word data, wherein the summary data includes summary content and keyword tags extracted from the summary content.

[0110] In one implementation, the prompt word generation module can be used to: determine the context data of the code entity based on the association relationship, wherein the context data includes at least one of the following elements: parent class, child class, and calling method; and inject each element contained in the context data into the corresponding position of the prompt word template to obtain prompt word data.

[0111] The specific implementation of the above-mentioned RAG library construction device and its various modules, as well as other possible implementations, can be found in the relevant sections of the method embodiments, and will not be repeated here.

[0112] The construction device for any of the code RAG libraries described in the above embodiments includes a processor and a memory. The code parsing module, graph construction module, code library construction module, summary generation module, prompt word generation module, and summary generation sub-module in the above embodiments are all stored as program modules in the memory, and the processor executes the above program modules stored in the memory to realize the corresponding functions.

[0113] The processor contains a kernel, which retrieves the corresponding program modules from memory. One or more kernels can be configured, and the processing of accessed data can be achieved by adjusting kernel parameters.

[0114] The memory may include non-permanent memory in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM, and the memory includes at least one memory chip.

[0115] In an exemplary embodiment, a computer-readable storage medium is also provided, which can be directly loaded into the internal memory of a computer and contains software code. After being loaded and executed by the computer, the computer program can implement the steps shown in any embodiment of the above-described method for constructing the code RAG library.

[0116] In an exemplary embodiment, a computer program product is also provided, which can be directly loaded into the internal memory of a computer and contains software code. After being loaded and executed by the computer, the computer program can implement the steps shown in any embodiment of the code RAG library construction method described above.

[0117] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For the apparatus disclosed in the embodiments, since they correspond to the methods disclosed in the embodiments, the description is relatively simple; relevant parts can be referred to the method section.

[0118] It should also be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.

[0119] The steps of the methods or algorithms described in conjunction with the embodiments disclosed herein can be implemented directly by hardware, a software module executed by a processor, or a combination of both. The software module can be located in random access memory (RAM), main memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium known in the art.

[0120] The above description of the disclosed embodiments enables those skilled in the art to make or use this application. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of this application. Therefore, this application is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1. A method for constructing a code RAG library, characterized in that, include: The code project root directory is parsed, metadata at different levels is extracted from the code, and the relationships between the code entities contained in the metadata are determined. Based on the code entities and the relationships, a link topology diagram covering the entire code project is constructed, wherein the relationships include class inheritance relationships and method call relationships; The link topology map is processed and converted into a searchable index, and the searchable index is combined with code data and stored in the code RAG library.

2. The method for constructing a code RAG library according to claim 1, characterized in that, The metadata at different levels includes module-level data, file-level data, and method-level data; The module-level data includes module information and file package information; the file-level data includes class information, reference information, and class variable information; the method-level data includes method information, method input and output parameter information, and call relationship information.

3. The method for constructing a code RAG library according to claim 1, characterized in that, The link topology map is processed and converted into a searchable index, including: The class inheritance and method call relationships indicated in the link topology diagram are encoded as index paths.

4. The method for constructing a code RAG library according to claim 3, characterized in that, The process of converting the link topology map into a searchable index includes: Based on the link topology graph, a hierarchical index is obtained, including: For the module layer nodes in the link topology diagram, create an inverted index based on keywords; For the class-layer nodes in the link topology graph, create a vector index based on the context vector; For the method layer nodes in the link topology graph, a graph index that integrates semantic vectors and topological relationships is created.

5. The method for constructing a code RAG library according to claim 4, characterized in that, In the implementation of hierarchical indexing in the constructed code RAG library, the cross-level retrieval route weight is determined based on the first and second terms, where the first term represents the semantic similarity between the query data and the method name / method description, and the second term represents the hierarchical weight of the matching module or the importance of the method in the link topology graph.

6. The method for constructing a code RAG library according to claim 1, characterized in that, Also includes: The large model is invoked to generate summary data of the code entity based on the link topology diagram and stored in the code RAG library. The summary data stored in the code RAG library is associated with the corresponding code entity.

7. The method for constructing a code RAG library according to claim 6, characterized in that, The large model that invokes the link topology graph generates summary data of the code entities and stores it in the code RAG library, including: Based on the link topology diagram, prompt word data with the same format as the preset prompt word template is obtained. The prompt word data includes at least the code entity and its corresponding association relationship. The large model is invoked to generate summary data based on the prompt word data. The summary data includes summary content and keyword tags extracted from the summary content.

8. The method for constructing a code RAG library according to claim 7, characterized in that, The process of obtaining prompt word data with the same format as the preset prompt word template based on the link topology map includes: The context data of the code entity is determined based on the association relationship, and the context data includes at least one of the following elements: parent class, child class, and method call; The various elements contained in the context data are injected into the corresponding positions of the prompt word template to obtain the prompt word data.

9. A device for building a code RAG library, characterized in that, include: The code parsing module is used to parse the root directory of the code project, extract metadata at different levels from the code, and determine the relationships between the code entities contained in the metadata. The graph construction module is used to construct a link topology graph covering the entire code project based on the code entities and the relationships, wherein the relationships include class inheritance relationships and method call relationships; The code library building module is used to process the link topology map into a searchable index, and store the searchable index in combination with code data into the code RAG library.

10. The apparatus for building a code RAG library according to claim 9, characterized in that, Also includes: The summary generation module is used to call the large model to generate summary data of the code entity based on the link topology diagram and store it in the code RAG library. The summary data stored in the code RAG library is associated with the corresponding code entity.