Graph representation learning and retrieval method and system based on code warehouse graph structure
By constructing a graph representation learning method for the code repository graph structure and combining it with a graph neural network to generate and update node embedding vectors, we solve the problem of heterogeneous graph fusion across files and multi-type dependencies in the code repository, achieve efficient and scalable full-repository semantic understanding and code generation, and improve the accuracy and efficiency of code completion and question-answering.
Patent Information
- Application Number
- CN202510860225.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-25
- Publication Date
- 2025-09-26
AI Technical Summary
Existing technologies make it difficult to build heterogeneous graphs with cross-file, multi-type dependencies in code repositories and seamlessly integrate graph structures with deep learning models, resulting in insufficient accuracy, efficiency, and scalability in code completion and question-answering.
By building a graph representation learning method for the code repository graph structure and combining it with a graph neural network, we generate embedding vectors for nodes and update these vectors through self-supervised training. This enables seamless integration of heterogeneous graphs across files and multi-type dependencies in the code repository, and uses intelligent models for query and code generation.
It achieves efficient and scalable full-warehouse semantic understanding and code generation, improves the accuracy of code completion and question-answering, reduces computing costs, supports large-scale online query of code graphs, and is suitable for scenarios such as function search, defect location, code reuse, and collaborative development analysis.
Smart Images

Figure CN120704664A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the fields of artificial intelligence, large-scale language models, and intelligent code analysis technology, and in particular to a method and system for graph representation learning and retrieval based on a code repository graph structure. Background Art
[0002] As software projects continue to scale, code repositories contain a large number of complex dependencies across functions, classes, and modules across multiple files. Large-scale open source projects and industrial-grade software often contain millions of lines of code, multiple programming languages, and multi-level modular structures, making it difficult for humans and traditional tools to fully understand the global context.
[0003] Existing code completion, code retrieval, and code question-and-answer systems are primarily based on text retrieval, local context pre-trained models, graph analysis tools, incremental indexing, and online learning. Text retrieval methods are susceptible to inconsistent naming, annotations, and formatting differences, resulting in high noise and low recall. Local context pre-trained models ignore cross-file call chains and modular structures, resulting in insufficient support for multi-file collaboration scenarios. They are also based on keywords and static text matching, lacking structural awareness. Graph analysis tools, on the other hand, are mostly still in the construction phase, failing to fully integrate graph structures with deep learning models. Most methods consider a single graph type (such as a function call graph), making it difficult to account for multiple relationships such as inheritance, interface implementation, and imports. Incremental indexing involves online learning, requiring high computational and storage costs when code repositories are updated, hindering the frequent iterations of continuous integration and continuous deployment processes. Furthermore, cross-language, multi-repository, and multi-version management scenarios exacerbate the limitations of existing technologies.
[0004] How to build a cross-file, multi-type dependency heterogeneous graph at the warehouse level and seamlessly integrate the graph structure with deep learning models to improve the accuracy, efficiency, and scalability of code completion and question-answering, and to provide a unified vectorized identification and efficient retrieval mechanism for the code graph structure, are technical issues that need to be urgently addressed. Summary of the Invention
[0005] In response to the above-mentioned defects, the technical problem solved by the present invention is to provide a method and system for graph representation learning and retrieval based on the code repository graph structure, combining the code repository structured representation and vector retrieval method of graph neural network, solving the technical problem of seamlessly integrating the graph structure with the deep learning model in constructing a cross-file, multi-type dependency heterogeneous graph at the warehouse level, and achieving the technical effect of efficient and scalable full-warehouse semantic understanding and code generation.
[0006] A first aspect of the present invention provides a method for graph representation learning and retrieval based on a code repository graph structure, comprising: S1: Construct a heterogeneous code graph related to the original code, the newly added code, or the modified code, and store a node set, an edge set, a node type, and an edge type related to the heterogeneous code graph.
[0007] S2: Perform graph representation learning on the heterogeneous code graph to generate a first embedding vector of the node.
[0008] S3: Taking the nodes, edges and the first embedding vector of the node in the heterogeneous graph as input, using the intelligent model to perform self-supervised training, outputting the training results and updating the first embedding vector of the node to the second embedding vector.
[0009] S4: Store the second embedding vector, and determine whether there is a new code component or modified code in the code repository. If so, return to step S1; if not, proceed to step S5.
[0010] S5: Receive a query request, generate a query vector representing the semantic features of the query according to the query request, match it with the second embedding vector, find a first node set related to the query semantics, and construct a candidate subgraph based on the first node set.
[0011] S6: Generate a target code segment according to the query request and the first node set, verify whether the target code segment complies with the structural syntax specification, and output the query result.
[0012] According to one embodiment of the present invention, constructing a heterogeneous code graph related to the code in S1 includes: Identify source code files in a code repository, generate a syntax tree AST from the source code files, and identify the code structure in the source code files, including: defining a syntax node mapping function between a first syntax tree AST node set and a second syntax tree AST node set, mapping the first syntax tree AST node set to a second syntax tree AST node set; each node in the second syntax tree AST node set is defined as an entity node, and the node has additional attributes.
[0013] A global symbol table is established for each entity node, the namespace information, entity name and node metadata corresponding to the entity node are determined, and a globally unique identifier is generated. The reference relationship of the nodes in the symbol table is traversed to output an edge set.
[0014] According to one embodiment of the present invention, the step S2 of performing graph representation learning on the heterogeneous code graph to generate a first embedding vector for the node includes: obtaining text information of the node in the heterogeneous graph, preprocessing the text information, generating a first embedding vector corresponding to the node, recording the first embedding vector as an additional attribute of the node and storing the first embedding vector. A corresponding table is established between the globally unique identifier of the node and its first embedding vector, and is updated in real time.
[0015] According to one embodiment of the present invention, in S3, nodes, edges, and first embedding vectors of the nodes in the heterogeneous graph are used as input, and self-supervised training is performed using an intelligent model, including: aggregating the different types of nodes and edges based on the node type, edge type, and first embedding vector; iteratively training a language model by randomly masking the node text to restore the original text of the node; and iteratively training the language model by randomly removing some edges and determining whether the edge exists and the connection probability of the nodes associated with the removed edge.
[0016] According to one embodiment of the present invention, outputting the training result in S3 and updating the first embedding vector of the node to the second embedding vector includes: updating the first embedding vector to the second embedding vector in real time according to the iterative training result of the intelligent model, and storing the real-time updated second embedding vector.
[0017] According to one embodiment of the present invention, the step of receiving a query request in S5, generating a query vector representing semantic features of the query based on the query request, matching the query vector with the second embedding vector, and generating a node set related to the query semantics includes: receiving input information containing a query request, wherein the query request includes a natural language description or a source code snippet.
[0018] The query request is vectorized into a query vector based on an intelligent model; the cosine similarity between the query vector and the second embedding vector is calculated; a node set matching the query vector is obtained according to the similarity value, and a candidate subgraph is constructed according to the first node set. According to one embodiment of the present invention, S6 generates a target code snippet based on the query request and the first node set, verifies whether the target code snippet conforms to structural syntax specifications, and outputs the query result, including: S601: obtaining neighbor nodes that have semantic edges with the first node set, integrating the first node set and the neighbor nodes into a second node set, extracting code snippets, comments, and signatures from the second node set, and splicing them into natural language or prompt words. S602: generating target code based on the prompt words. S603: parsing the target code, constructing a heterogeneous graph of the target code, and performing a structural comparison with the candidate subgraph to determine whether the node set and edge set in the target code heterogeneous graph are fully contained in the candidate subgraph node set and edge set, respectively. If so, proceed to step S604; otherwise, return to step S602. S604: outputting the target code.
[0019] A second aspect of the present invention provides a system for graph representation learning and retrieval based on a code repository graph structure, comprising: The construction module is used to construct a heterogeneous code graph related to the original code, the newly added code or the modified code, and store the node set, edge set, node type and edge type related to the heterogeneous code graph.
[0020] A vector representation module is used to perform graph representation learning on the heterogeneous code graph to generate a first embedding vector of the node.
[0021] The intelligent module is used to take the nodes, edges and first embedding vectors of the nodes in the heterogeneous graph as input, perform self-supervised training, output training results and update the first embedding vector of the node to a second embedding vector.
[0022] A storage module is used to store the second embedding vector, determine whether there is a new code component or modified code in the code repository, return to the construction module if there is, and enter the search module if not.
[0023] The search module receives a query request, generates a query vector representing the semantic features of the query according to the query request, matches the query vector with the second embedding vector, searches for a first node set related to the query semantics, and constructs a candidate subgraph based on the first node set.
[0024] The verification module generates a target code according to the query request and the first node set, verifies whether the target code conforms to the structural syntax specification, and outputs the query result.
[0025] According to one embodiment of the present invention, the construction module includes: a first construction unit, which is used to identify the source code file of the code library, generate a syntax tree AST from the source code file, and identify the code structure in the source code file, including defining a syntax node mapping function between a first syntax tree AST node set and a second syntax tree AST node set, and mapping the first syntax tree AST node set to a second syntax tree AST node set; each node in the second syntax tree node set is defined as an entity node, and the node has additional attributes.
[0026] The second construction unit is configured to establish a global symbol table for each entity node, determine namespace information, entity name, and node metadata corresponding to the entity node, and generate a globally unique identifier.
[0027] The third construction unit is used to traverse the reference relationship of the nodes in the symbol table and output the edge set.
[0028] According to one embodiment of the present invention, the vector representation module includes: a vector representation unit for obtaining text information of nodes in a heterogeneous graph, preprocessing the text information, generating a first embedding vector corresponding to the node, recording the first embedding vector as an additional attribute of the node, and storing the first embedding vector; and a vector correspondence unit for establishing a correspondence table between the node's globally unique identifier and its first embedding vector, and updating the table in real time.
[0029] According to one embodiment of the present invention, the training language module includes: a first training unit, configured to aggregate the different types of nodes and edges according to the node type, edge type, and the first embedding vector.
[0030] The second training unit is used to randomly mask the node text and iteratively train the language model to restore the original text of the node; randomly remove some edges and iteratively train the language model to determine whether the edge exists and the connection probability of the nodes related to the removed edge.
[0031] According to one embodiment of the present invention, the search module includes: a receiving unit configured to receive input information including a query request, the query request including a natural language description or a source code snippet; a searching unit configured to search for a node set matching the input information including the query request, and construct a candidate subgraph based on a first node set.
[0032] According to one embodiment of the present invention, the verification module includes: a first generation unit, used to obtain neighbor nodes that have semantic edges with the first node set, integrate the first node set and the neighbor nodes into a second node set, extract code snippets, comments, and signatures of the second node set, and splice them into natural language or prompt words; a second generation unit, used to generate target code based on the prompt words; a verification unit, used to parse the target code, construct a heterogeneous graph of the target code, perform structural comparison with the candidate subgraph, and determine whether the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set, if the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set, jump to the output unit, otherwise jump to the second generation unit; the output unit is used to output the target code.
[0033] The third aspect of the present invention provides an intelligent device, including a transmitter, a receiver, a memory and a processor; the memory is used to store computer instructions; the processor is used to run the computer instructions stored in the memory to implement the above-mentioned graph representation learning and retrieval method based on the code repository graph structure.
[0034] A fourth aspect of the present invention provides a storage medium, comprising: a readable storage medium and computer instructions, wherein the computer instructions are stored in the readable storage medium; the computer instructions are used to implement the above-mentioned method of graph representation learning and retrieval based on the code repository graph structure.
[0035] The beneficial effects provided by the present invention are as follows: first, the structural relationship between codes is fully modeled to realize a structure-aware retrieval method, which is superior to the traditional text matching scheme; second, GNN is used to extract cross-file and cross-version context information to improve the code semantic understanding ability; third, vectorized retrieval has high scalability and supports large-scale online query of code graphs, with wider applicability, and can be widely used in scenarios such as function search, defect location, code reuse, and collaborative development analysis; at the same time, self-supervised training and learning do not require manual data annotation, and learning objectives are automatically generated by constructing tasks. Through multiple iterative training, the model is continuously optimized, integrating the structural context information in the graph, and learning a richer context than the original semantic vector, paving the way for the code context and the integrity of subsequent verification; real-time monitoring of whether there is new code or code modification, without the need to retrain the entire model, updating and proofreading the graph structure for the new or modified code, reducing the computational cost and greatly improving the training efficiency; when matching the embedded vector in the vector database related to the query request, its neighboring nodes are also taken into account, ensuring the accuracy of the subsequent verification of the code syntax structure and the integrity of the output code. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0037] Figure 1 This is a flow chart of a graph representation learning and retrieval method based on a code repository graph structure disclosed in an embodiment of the present invention; Figure 2 This is a schematic diagram of self-supervised training of an intelligent model in a graph representation learning and retrieval method based on a code repository graph structure disclosed in an embodiment of the present invention; Figure 3 This is a schematic diagram of the framework of the graph representation learning and retrieval method based on the code repository graph structure disclosed in an embodiment of the present invention; Figure 4 This is a block diagram of a graph representation learning and retrieval system based on a code repository graph structure disclosed in an embodiment of the present invention.
[0038] The above drawings illustrate specific embodiments of the present disclosure, which will be described in more detail below. These drawings and textual descriptions are not intended to limit the scope of the present disclosure in any way, but rather to illustrate the concepts of the present disclosure to those skilled in the art by reference to specific embodiments. DETAILED DESCRIPTION
[0039] Exemplary embodiments will be described in detail herein, with examples illustrated in the accompanying drawings. In the following description, when referring to the drawings, identical numerals in different figures represent identical or similar elements, unless otherwise indicated. The embodiments described in the following exemplary embodiments are not intended to represent all possible embodiments consistent with the present disclosure. Rather, they are merely examples of apparatus and methods consistent with certain aspects of the present disclosure, as detailed in the appended claims.
[0040] This paper proposes a graph representation learning and retrieval method based on the code repository graph structure. Through the collaborative work of modules, efficient and scalable full-repository semantic understanding and code generation are achieved. Figure 1 As shown, S1: construct a heterogeneous code graph related to the original code, the newly added code or the modified code, and store the node set, edge set, node type, and edge type related to the heterogeneous code graph.
[0041] At the code repository level, a static syntax tree (AST) is parsed for each source file using a multi-language parser (such as Tree-sitter or ANTLR). The AST is an abstract representation of source code that accurately depicts the code structure and displays its hierarchical relationships, allowing precise identification of code entities (including functions, classes, interfaces, variables, and comments). Subsequently, using symbol tables and dependency analysis tools, a deep traversal of the AST nodes is performed to extract various cross-file dependencies, such as function calls, class inheritance, interface implementations, and module imports. All extracted entities are mapped as nodes in a graph, and different types of dependencies are abstracted as heterogeneous edges with attribute labels. For example, a relationship where function A calls function B might be an edge labeled "call," while a relationship where class A inherits class B might be an edge labeled "inherit." These edges carry different attribute labels, indicating the type of dependency.
[0042] This module ultimately outputs a repository-level heterogeneous graph containing multi-language and multi-type dependencies. This graph not only represents the relationship between different code entities, but also abstracts the dependencies across files and between different programming languages.
[0043] To cope with the dynamic transformation of the code base, we provide an incremental update interface for the graph to support the dynamically changing code base. We also cover the dependencies between various files in the source code.
[0044] The above automated graph construction process can parse the source code and convert its dependencies and structure into a graph, providing support for subsequent analysis, visualization, refactoring and other tasks.
[0045] First, a module is built to convert the source code in the code repository into a heterogeneous graph containing rich semantic and structural information. ,in Represents a collection of nodes, represents the edge set, and Represents the node type and edge type collection respectively.
[0046] The process begins by scanning and classifying all source files in the repository. The system identifies programming languages by file extensions (such as .java, .py) or file header features (such as Python's #! / usr / bin / env python) and filters out non-code files (such as configuration files and log files). If a script is executed using the Python interpreter, the system can determine that it is a Python script. Finally, non-code files are filtered out. In the code repository, there are many files that are not source code, such as configuration files, log files, and documents. These are not code files; only source code files are code files. Filtering out these non-code files allows analysis of the actual source code, saving computation and improving accuracy. This facilitates subsequent structural analysis of the actual source code, such as extracting an AST.
[0047] The system needs to determine whether each file is a "source code file" and what programming language it is written in. Different languages have fixed file suffixes, see Table 1, so the file extension can be used to quickly determine whether the file is source code and what programming language it is written in. For some files without extensions or with ambiguous extensions, the "file supplementary features" are used to determine whether the file is source code or what programming language it is written in. For each code file, the system calls a multi-language parser (such as Tree-sitter or ANTLR) to generate an abstract syntax tree (AST) such as For Java files, the parser will identify nodes such as class declaration (ClassDeclaration), method definition (MethodDeclaration), variable declaration (VariableDeclarator), etc.; for Python files, it will parse out structures such as function definition (FunctionDef), class definition (ClassDef), import statement (Import), etc.
[0048] Calling a multi-language parser to generate an abstract syntax tree requires the following steps: first, select a parser. Choose the appropriate parser based on the project requirements. Then, install the parser and generate an abstract syntax tree. Initialize the parser and install the required language libraries. Then, use the parser to parse the source code and generate an AST, traversing each node through the tree structure.
[0049] Once the AST is generated, the required information (e.g., function calls, class inheritance, etc.) is extracted based on the tree structure. For function calls, the calling relationships between different functions are extracted by searching for "function call" nodes in the AST. For class inheritance, the inheritance relationships between classes are extracted by searching for nodes representing classes and identifying inheritance relationships. For module imports, cross-file module dependencies are extracted by identifying statements containing "import" or "include." Finally, a graph is constructed based on the extracted information, with each entity (function, class, etc.) as a node and each relationship (e.g., call, inheritance, etc.) as an edge. The edges in the graph are labeled to indicate different dependency types.
[0050] Tree-sitter is suitable for real-time parsing and can efficiently handle incremental updates, making it suitable for use cases like editor plugins. ANTLR, on the other hand, is suitable for more complex grammatical parsing tasks and supports generating AST structures for various programming languages. These tools can be used to extract entities and dependencies from code, thereby building a dependency graph for the codebase.
[0051] Taking into account the possible grammatical differences between different languages or their versions, the system defines a grammar node mapping function: (1) The original AST node collection Mapped to a set of standard node types The purpose is to map each AST node in the original language (such as function definition, class template, etc.) to a unified standard node type. For example:
[0052]
[0053] (2)
[0054] The above formula indicates that MethodDeclaration (Java style) and FunctionDef (Python style) are both mapped to FUNC; ClassTemplate (such as C++ template class) and GenericClass are both mapped to GENERIC_CLASS; nodes representing "function" or "generic class" in different languages or versions are uniformly classified.
[0055] Based on the standardized AST, the system performs fine-grained entity extraction and dependency analysis. Each AST node is converted into a physical node in the graph , and append the property set . The fine-grained entity extraction and dependency relationships here mean that the system will perform a detailed analysis of each node in the AST, pay attention to every level of detail, and analyze each code entity (such as function, class, variable, etc.) and their dependencies. Each node in the AST (representing an entity in the code, such as a function or class) is converted into an entity node in the graph. In other words, each element in the AST corresponds to a node in the graph. A set of attributes is attached to each node (entity node), including type (function, class, variable, etc.), scope, access rights, number of lines of code, file information, etc. These attributes provide more contextual information for each node in the graph, forming a semantic graph structure with a unified representation, which is convenient for cross-language static analysis or model input for subsequent analysis and operations.
[0056] Specifically, for function nodes , its properties can be expressed as: (3) Among them, name represents the name of the function, parameters represents the parameters (usually including the name and type of the parameters), return_type represents the return type of the function, docstring represents the documentation string of the function, which is used to describe the function's function and purpose; file_path represents the path of the file where the function is located; start_line represents the starting line number of the function in the source code; end_line represents the ending line number of the function in the source code.
[0057] The system also builds a global symbol table ,in is the semantic entity name, The fully qualified path to the file. This structure is used to track cross-file dependencies and call relationships.
[0058] The global symbol table is a data structure that records all named entities in the code and their binding relationships. After building the symbol table, we know which entity each AST name is bound to. In other words, the standard AST provides structure, while the global symbol table provides semantics.
[0059] For example, when encountering the Java statement: (4) System logs Then in the analysis When querying the symbol table Establish cross-file call edges, such as the formula: (5) To solve problems such as entity ambiguity (overloading), generic template resolution (such as std::vector in C++ <t>), and complex situations such as implicit dependencies in dynamic languages. For example, classes or functions with the same name may appear in different files, names may be repeated in different scopes or inheritance structures, and dependencies in dynamic components or template languages are difficult to resolve statically. The system generates a Global Unique Identifier (GUID) for each entity, which is defined as follows: (6) Among them, filepath(v) represents the full path of the file where the semantic entity is located; line(v) represents the line number of the entity in the source code; col(v) represents the column number of the entity in the source code (accurate to the specific location); name(v) represents the name of the entity (such as function name, variable name, class name).
[0060] For example: src / main / java / com / example / Service.java:32:5:calculate. This indicates the function or method calculate at line 32 and column 5 in the Java file Service.java.
[0061] In this way, entities can be accurately connected when building call graphs, class diagrams, and dependency graphs, cross-language and cross-module call relationships can be identified, complex grammatical structures such as polymorphism, overloading, templates, and inheritance can be handled, and semantic unification can be achieved without being restricted by namespaces or language syntax.
[0062] Node types can include "File", "Class", "Function", "Variable", etc., while edge types include "Contains", "Call", "Inherits", "Imports", etc.
[0063] Construct a semantic graph with edge types including: "Contains" (e.g., a class contains a function); "Call" (e.g., a call relationship between functions); "Inherits" (e.g., an inheritance relationship between classes); "Variable" (e.g., a variable ownership); and "Imports" (e.g., a reference dependency between files / modules). Analyze the program structure based on this semantic graph.
[0064] S2: Perform graph representation learning on the heterogeneous code graph to generate the first embedding vector of the node. Specifically, first construct the heterogeneous graph node and define the symbol table. The graph vector representation module is responsible for generating vector representations of the heterogeneous graph nodes constructed above. First, after step S1, the system has constructed a global symbol table. , which stores the mapping between the unique identifiers of all code entities and the corresponding nodes in a hash structure. The key of the symbol table is composed of the entity's namespace (such as the package path in Java and the module path in Python) and the entity name. For example, for Java com.example.service.UserService, the key is com.example.service::UserService; for the Python function utils.logger.debug(), the key is utils.logger::debug. The value of the symbol table points to the metadata of the corresponding node in the graph (such as type, file path, line number), and its mathematical expression can be defined as: (7) Generate a unique identifier for each semantic entity and add it as a node in the graph.
[0065] Next, we build the graph structure and model the relationship edges. For example, the system extracts edges through various "reference relationships" in the code, such as function calls (call), inheritance (inherit), module imports (import), and variable definitions (var). These relationship edges connect two nodes and form the edge set of the graph. Dependency relationships are extracted by traversing the reference relationships in the symbol table. All dependency relationships form the edge set: (8) The predefined dependency type set {CALL, INHERIT, IMPORT, ...} covers scenarios such as function calls, class inheritance, and module imports. This process requires resolving cross-file references—for example, when parsing a C++ #include "header.h" statement, it is necessary to associate the current file with the entities declared in header.h.
[0066] Furthermore, after the graph structure is constructed, a representation is extracted for each node in the graph. The data needs to be persisted to a graph database (such as Neo4j). Its storage logic includes the following core operations: Store the graph G in the graph database and provide an incremental update interface; for each node , extract text attributes , calculate the semantic vector ; will Index into vector database and maintain mapping , supporting fast retrieval. After establishing a graph database, semantic vectors need to be generated for each code node to facilitate subsequent retrieval and pre-training. A semantic encoder (such as CodeBERT or GraphCodeBERT) can be used to encode the source code text or context associated with each node, generating a fixed-length vector representation. Specifically, for each node, the relevant source code snippet or signature (e.g., function body, class body, variable declaration, etc.) is extracted and input into a pre-trained model to obtain a first embedding vector. This generated first embedding vector can be stored directly as a node attribute in the graph database (if the database supports vector attributes) or in a dedicated vector indexing service (such as Faiss or Pinecone). The generated semantic vectors are stored in a vector index such as Faiss or Pinecone, assigning each vector a unique vector ID. In the latter case, it is necessary to maintain a mapping between graph nodes and vector records, such as establishing a table that maps node IDs to vector IDs, or a table that maps node IDs in the graph database to vector IDs in the vector database. When searching for "other nodes with similar semantics to a given node," a query to the vector database is sufficient. The correspondence between each node and its vector representation is retained, and the vector ID can be quickly located by the node ID, or vice versa.
[0067] S3: Using the nodes, edges, and first embedding vectors of the nodes in the heterogeneous graph as input, perform self-supervised training using an intelligent model, output the training results, and update the first embedding vector of the node to a second embedding vector. In addition to graph neural network models, intelligent models can also be implemented using models or methods such as graph convolutional networks and their variants, graph Transformer-like models, graph kernel methods, graph optimization methods and heuristic algorithms, probabilistic graph models, and graph-based embedding methods. This embodiment uses a graph neural network model as a detailed explanation.
[0068] like Figure 2 As shown in the figure, the graph neural network pre-training module uses a pre-built heterogeneous graph to perform self-supervised pre-training on the graph neural network model. The input is a graph containing multiple node types and edge types and the initial features of each node (i.e., the semantic vector generated by the aforementioned encoding). The model architecture usually chooses the Heterogeneous Graph Transformer (HGT), the core of which is to give independent attention mechanisms to different types of edges. The core idea of HGT is to design independent attention mechanisms for different types of edges, so as to distinguish the semantic features of different dependencies (such as function calls and class inheritance). Its message passing process is divided into the following steps: Typed feature projection of nodes and edges: for each node V and edge type , define a type-specific projection matrix to map the original features into a uniform space: , (9) in, is the semantic vector, Representation node Type (FUNC, CLASS), etc. represents the node type-related adjacency matrix, Indicates the edge type The embedding matrix of Represents edge type projection matrix, original features and Mapping to uniform space through the projection matrix .
[0069] For the edge , its attention weight Calculated as: (10) in, Edge Type Independent query (Query) and key matrix (Key), denominator Used to scale dot product structures. For example, when processing CALL edges, the model learns how to aggregate the features of the called function; when processing INHERIT edges, it focuses on the propagation of features between parent and child classes.
[0070] In the formula, u and v are two nodes in the graph, u is the target node, and v is the neighbor node. The edge type, such as "call", "reference", etc. Represents the initial feature representation of nodes u and v after type projection, Edge Type The corresponding query matrix is used to transform the target node features, Edge Type The corresponding key matrix is used to transform the neighbor node features, and d is the scaling factor used to stabilize the gradient; Indicates that the edge type is The attention weight of . Map the representation of node u to a query vector, Map the representation of node v to a key vector. The dot (or inner product) of the above two is used to get the correlation degree, which represents the "attention level" of node u to its neighbor v. The denominator d is scaled to avoid excessive values affecting training stability. The calculation method of attention weight varies with the edge type. Different models are used to achieve semantically differentiated modeling.
[0071] Attention score This is fed into the softmax function for normalization and ultimately used to weight the sum of neighboring node messages. This measures the influence of each neighbor on node u by first performing a linear transformation on the neighboring node information and then weighted summing the valid information from all neighbors. The value matrix is used to aggregate this information and output the updated representation of node u at the current layer.
[0072] Edge Type This determines which weight matrix to use during aggregation, thereby identifying the propagation patterns of different edge semantics. The neighbor contributions of each relationship type can be treated differently, rather than simply pieced together or concatenated. If there are multiple edge types, inter-class fusion can also be performed.
[0073] The above method uniformly models heterogeneous graphs with multiple edge types and uses the attention mechanism for edge type differentiation to improve the discrimination of node semantic representation.
[0074] The training task is designed as a self-supervised mode: on the one hand, the node text is randomly masked (such as masking function names or parameters), and the model is required to reconstruct the original text based on the context nodes and graph structure (mask reconstruction task); on the other hand, some real edges are randomly removed, and the model is asked to predict whether there is a specific type of dependency between nodes (edge prediction task).
[0075] In the task of mask reconstruction, we aim to make the model understand local semantic information, such as the semantic regularity of function names, variable names or parameters. The specific approach is to randomly select a part of the text attributes of the nodes in the training graph for masking, such as masking the function name as [MASK], while retaining the original text of other nodes. The goal of the model is to use the semantic context of the surrounding nodes and the connection relationship of the graph structure to restore the original text of the node. We introduced a lightweight decoder after GNN to map the node embedding vector back to a specific discrete text token. Specifically, for a node V encoded by an L-layer graph neural network, its embedding vector Sent to the decoder , predict its original text class , the formula is as follows: (11) in, and are the hidden layer and output layer parameters of the decoder respectively, is the predicted distribution over the vocabulary.
[0076] The second task is edge prediction, which is used to capture the global structural dependencies in the graph. We randomly remove some of the real edges in the graph and train the model to determine whether there is a certain type of semantic dependency (such as call, inheritance, inclusion, etc.) between node pairs. and , the model is based on its encoding vector and edge types , calculate the connection probability.
[0077] (12) in, is the connection probability between node u and node v on layer l (representing the computational level of the model). and are the feature representations of nodes u and v at layer l, respectively, and the node embeddings obtained after these representations are propagated through the graph neural network. Is the learned weight matrix. It is used to transform and combine node features. It is a Sigmoid activation function that maps the output value to the range [0,1], indicating the probability of the existence of an edge. In edge prediction, 0 indicates the absence of an edge and 1 indicates the existence of an edge.
[0078] By the characteristics of the node pair ( , ) and the weight matrix Calculates whether there are connections between nodes. By maximizing edge prediction accuracy, the model learns how to correctly determine the probability of node connections using features and weight matrices. During training, information about each node is propagated across multiple layers of the graph, and the output features of each layer influence the final prediction.
[0079] These two tasks force the model to understand both the local semantics of the code (such as variable naming conventions) and the global structure (such as the call chain between modules). During training, node representations are iteratively updated through multiple layers of message passing. The resulting second embedding vector not only contains the semantics of the code itself but also encodes its structural role in the project (such as "core utility function" or "edge interface implementation").
[0080] The benefit of self-supervision is that it eliminates the need for manual data annotation and automatically generates learning objectives through task construction. Through multiple rounds of iterative training on these two tasks, the model continuously optimizes parameters. The representation of each node not only incorporates its own semantic information (derived from text embeddings) but also incorporates its structural context within the graph (such as the nodes it has edges with and the types of edges). This allows the learned node representation to be richer in context than the raw semantic vector and more relevant to the code context.
[0081] In summary, based on the initial semantic vector and the constructed heterogeneous graph, the heterogeneous graph neural network is trained through self-supervised learning (node mask and edge prediction), enabling the model to have a deeper understanding of code semantics and structure, ultimately obtaining a more powerful node representation vector, providing more intelligent support for subsequent tasks.
[0082] S4: Store the second embedding vector, and determine whether there is a new code component or modified code in the code repository. If so, return to step S1; if not, proceed to step S5.
[0083] In this step, the node enhanced semantic vectors and the original heterogeneous graph structure obtained by graph neural network pre-training are written into the graph database and vector database respectively. , where V is the node set, E is the edge set, , The d-dimensional vector corresponding to each node v (the vector calculated after training), Node identifier (including node ID, label, attributes, etc.).
[0084] The present invention imports heterogeneous graph data containing node attributes, edge types and weights into the graph database in batches through the graph database client (such as Neo4j Bolt or Gremlin interface), wherein the node attributes include: node native attributes (such as type, category, label), node vectors obtained by training , node identification Edge attributes include: edge type, weight, and other structural information. This way of writing preserves the original graph structure while enhancing the vector features of each node. Subsequent queries can be searched by structure and embedding at the same time. At the same time, the trained node vectors can also be Store it in the vector database, so that similar nodes can be quickly found through vector miniature method.
[0085] and identify the node Node type and call edge types Fields such as __name__ and __name__ are indexed for subsequent subgraph queries and structure retrieval.
[0086] Specifically, after training, GNN will calculate an enhanced feature vector for each node and write the original graph structure into the database.
[0087] At the same time, the system calls the vector database interface (such as FAISS "add_with_ids" or Milvus "insert" method) to write the high-dimensional semantic vector of the corresponding node into the vector index and maintain the node ID between the two databases. The mapping table of vector IDs is used to ensure that the search results can be accurately mapped back to the graph structure. This module supports incremental updates. When the repository adds or modifies code, only the affected node set is updated. and adjacent edges Recalculate and store, , This allows data synchronization and vector replacement to be performed only on the modified nodes and edges, ensuring that the data in the graph database and vector database remains consistent with the source code repository. Only the transformed nodes need to be recalculated and updated in the data base and vector database, eliminating the need to retrain the entire model, improving efficiency. In other words, after the GNN model is trained, the results are written to the database and maintained consistent.
[0088] S5: Receive a query request, generate a query vector representing the semantic features of the query according to the query request, match it with the second embedding vector, find a first node set related to the query semantics, and construct a candidate subgraph based on the first node set.
[0089] After receiving the user query request in this step, the query text is first converted into (natural language or code snippets) are encoded as vector representations of the same dimension as the node semantic vector The query request here includes a natural language statement, pseudocode, or source code snippet. First, semantic encoding processing is performed on the query request to generate a query vector representing the semantic features of the query. Then, a Top-K search is performed in the vector database based on cosine similarity or inner product measurement. A similarity matching operation is performed in the vector database to find a vector set of second embedding vectors that are closest to the query vector. The vector representations of multiple semantic nodes are pre-stored in the vector database. The vector representation of each code semantic node corresponds to an entity node of the source code (such as a function, variable, class, or file snippet). By calculating the cosine similarity of each vector in the combination of the query vector and the vector, the vectors are sorted according to the similarity value to determine the vectors with the top K similarities. Specifically, the calculation is as follows: (13) The formula uses cosine similarity, where T is the retrieved node ID, representing the top-K node set related to the query vector. Then, based on the similar nodes T, a semantic subgraph consistent with the query semantics is generated, and the K node IDs with the highest similarity are returned. Based on the returned node ID, the system batch queries the corresponding node attributes and their first-order / second-order neighbor relationships in the graph database to construct a retrieval candidate subgraph. , used to enhance contextual information for subsequent completion or question-answering. This semantic subgraph can be used to drive downstream tasks such as code search result presentation, code recommendation generation, graph completion, and dependency analysis. During the retrieval process, the Top-K size and similarity threshold can be dynamically adjusted to balance recall and retrieval efficiency, ensuring sufficient relevant candidate nodes are obtained. The vector retrieval results and the structured subgraph are then passed in parallel to the next module via an asynchronous or streaming interface.
[0090] S6: Generate target code according to the query request and the first node set, verify whether the target code complies with the structural syntax specification, and output the query result.
[0091] Based on the retrieved relevant nodes, the corresponding nodes and their neighboring nodes are extracted from the graph database and integrated to generate prompts. Prompts can include code snippets, comments, signatures, and contextual information related to the query, providing rich contextual clues for the generation model. The prompts are input into a pre-trained or fine-tuned generation model (which can be a large language model or a lightweight model). The model generates code completion results or answers user questions based on the provided subgraph context. During the generation process, the format and output strategy of the prompts can be adjusted according to the specific generation task type to ensure the accuracy and relevance of the generated results. Finally, the system outputs the generated code snippet or answer, which can be matched and verified with the original query, completing the entire retrieval and generation process.
[0092] In S6, a target code snippet is generated based on the query request and the first node set, the target code snippet is verified to determine whether it conforms to the structural syntax specification, and the query result is output, including: S601: obtaining neighbor nodes that have semantic edges with the first node set, integrating the first node set and the neighbor nodes into a second node set, extracting the code snippets, comments, and signatures from the second node set, and splicing them into natural language or prompt words. S602: generating target code based on the prompt words. S603: parsing the target code, constructing a heterogeneous graph of the target code, performing a structural comparison with the candidate subgraph, and determining whether the node set and edge set in the target code heterogeneous graph are fully contained in the candidate subgraph node set and edge set, respectively. If the node set and edge set in the target code heterogeneous graph are fully contained in the candidate subgraph node set and edge set, respectively, proceed to step S604; otherwise, return to step S602. S604: outputting the target code.
[0093] Specifically, in order to achieve code completion or natural language question answering tasks. Figure 3 As shown, this step builds on the subgraph context extracted in the previous step (i.e., the code text node graphs corresponding to adjacent code are merged into the subgraph based on graph relationships) and combines it with the user query to construct a structure-aware prompt. This prompt is then fed into a generative model (such as LLaMA, CodeLLaMA, WizardCoder, or a lightweight Transformer) that has been fine-tuned or distilled in the code domain to generate the target code snippet or answer. To ensure the syntactic and structural legitimacy of the generated results, the system has designed an automatic consistency verification mechanism. By comparing graph structures, it ensures that the generated content can be seamlessly embedded into the original code base.
[0094] This step further expands upon the subgraph context extracted in S5. Specifically, based on edge structures such as call relationships and dependency relationships within the code graph, the code snippets corresponding to the target node's adjacent nodes (neighbors) are also incorporated into the current subgraph to form a more complete context. Neighbor nodes include calling functions, called functions, and closely dependent nodes within the same module. Their corresponding code text (such as function definitions, variable declarations, and comments) is incorporated into the prompt as important semantic information, enhancing context modeling capabilities.
[0095] When constructing the prompt, the system organically integrates the following information: Introductory phrase: Describes the task objective and input context (e.g., "Please complete the objective function based on the code in the subgraph and its dependencies"). Target function signature: Clearly identifies the function entry point to be completed or answered.
[0096] Call chain structure: provides the upstream and downstream paths of the target node in the function call graph.
[0097] Adjacent node code snippets: The complete code or snippet of the caller or called function extracted based on the structural adjacency relationship.
[0098] This Prompt is then input into a fine-tuned or distilled generative model M (such as LLaMA, CodeLLama, or a lightweight Transformer), which generates code completion results or natural language answers based on the context: After the result output is generated, the system can call the graph database again for consistency verification: Perform syntax parsing, generate AST and construct corresponding subgraph , and the atomic diagram Structural comparison, definition of consistency discriminant function: (14) in, express The node set in ; Representation diagram The set of edges in ; Representation diagram The node set in ; Representation diagram The set of edges in . Subset relation Representing the target graph Are the nodes and edges in If the target subgraph The node collection in Fully included in The node collection in In the target subgraph The edge set in Fully included in The edge set in , then the metric value Equal to 1, only when these two conditions are met at the same time, the subgraph yes If the above conditions do not hold, the metric value Equal to 0, indicating the target subgraph Not an atomic diagram The matching subgraph of .
[0100] Verify the validity of newly inserted nodes and dependent edges. If a conflict is detected, Trigger rollback or regeneration , ensuring that the final output conforms to both grammatical specifications and is consistent with the original warehouse structure.
[0101] The second aspect of the present invention provides a graph representation learning and retrieval system 40 based on the code repository graph structure, such as Figure 4 Shown include:
[0102] The construction module 401 is used to construct a heterogeneous code graph related to the original code, the newly added code or the modified code, and store the node set, edge set, node type and edge type related to the heterogeneous code graph.
[0103] The vector representation module 402 is configured to perform graph representation learning on the heterogeneous code graph to generate a first embedding vector for the node.
[0104] The intelligent module 403 is configured to take the nodes, edges and the first embedding vectors of the nodes in the heterogeneous graph as input, perform self-supervised training, output the training results and update the first embedding vectors of the nodes to the second embedding vectors.
[0105] The storage module 404 is configured to store the second embedding vector, determine whether there is a new code component or modified code in the code repository, and return to the construction module if yes; otherwise, enter the search module.
[0106] The search module 405 is configured to receive a query request, generate a query vector representing the semantic features of the query according to the query request, match the query vector with the second embedding vector, and search for a first node set related to the query semantics, where the first node set corresponds to source code entity nodes.
[0107] The verification module 406 generates a target code segment according to the query request and the first node set, and verifies the target code segment.
[0108] According to one embodiment of the present invention, the construction module includes: a first construction unit, which is used to identify the source code file of the code library, generate a syntax tree AST from the source code file, and identify the code structure in the source code file, including defining a syntax node mapping function between a first syntax tree AST node set and a second syntax tree AST node set, and mapping the first syntax tree AST node set to a second syntax tree AST node set; each node in the second syntax tree node set is defined as an entity node, and the node has additional attributes.
[0109] The second construction unit is configured to establish a global symbol table for each entity node, determine namespace information, entity name, and node metadata corresponding to the entity node, and generate a globally unique identifier.
[0110] The third construction unit is used to traverse the reference relationship of the nodes in the symbol table and output the edge set.
[0111] According to one embodiment of the present invention, the vector representation module includes: a vector representation unit for obtaining text information of nodes in a heterogeneous graph, preprocessing the text information, generating a first embedding vector corresponding to the node, recording the first embedding vector as an additional attribute of the node, and storing the first embedding vector; and a vector correspondence unit for establishing a correspondence table between the node's globally unique identifier and its first embedding vector, and updating the table in real time.
[0112] According to one embodiment of the present invention, the training language module includes: a first training unit, configured to aggregate the different types of nodes and edges according to the node type, edge type, and the first embedding vector.
[0113] The second training unit is used to randomly mask the node text and iteratively train the language model to restore the original text of the node; randomly remove some edges and iteratively train the language model to determine whether the edge exists and the connection probability of the nodes related to the removed edge.
[0114] According to one embodiment of the present invention, the search module includes: a receiving unit configured to receive input information including a query request, the query request including a natural language description or a source code snippet; a searching unit configured to search for a node set matching the input information including the query request, and construct a candidate subgraph based on a first node set.
[0115] According to one embodiment of the present invention, a first generation unit is used to obtain neighboring nodes that have semantic edges with the first node set, integrate the first node set and the neighboring nodes into a second node set, extract code snippets, comments, and signatures of the second node set, and splice them into natural language or prompt words; a second generation unit is used to generate target code based on the prompt words; a verification unit is used to parse the target code, construct a heterogeneous graph of the target code, perform structural comparison with the candidate subgraph, and determine whether the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set; if the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set, jump to the output unit; otherwise jump to the second generation unit; the output unit is used to output the target code.
[0116] The third aspect of the present invention provides an intelligent device, including a transmitter, a receiver, a memory and a processor; the memory is used to store computer instructions; the processor is used to run the computer instructions stored in the memory to implement the above-mentioned graph representation learning and retrieval method based on the code repository graph structure.
[0117] A fourth aspect of the present invention provides a storage medium, comprising: a readable storage medium and computer instructions, wherein the computer instructions are stored in the readable storage medium; the computer instructions are used to implement the above-mentioned method of graph representation learning and retrieval based on the code repository graph structure.
[0118] The beneficial effects provided by the present invention are as follows: first, it fully models the structural relationship between codes and realizes a structure-aware retrieval method, which is superior to the traditional text matching scheme; second, it uses GNN to extract cross-file and cross-version context information to improve the ability to understand code semantics; third, vectorized retrieval has high scalability and supports large-scale online query of code graphs, which has wider applicability and can be widely used in scenarios such as function search, defect location, code reuse, and collaborative development analysis. At the same time, self-supervised training learning does not require manual data annotation. Learning objectives are automatically generated by constructing tasks. Through multiple iterative training, the model is continuously optimized, integrating the structural context information in the graph, and learning a richer context than the original semantic vector, paving the way for the integrity of the code context and subsequent verification; it monitors in real time whether there is new code or code modification, without the need to retrain the entire model, and updates and proofreads the graph structure for the new or modified code, reducing the computational cost and greatly improving the training efficiency; when matching the embedded vector in the vector database related to the query request, its neighboring nodes are also taken into account, ensuring the accuracy of the subsequent verification of the code syntax structure and the integrity of the output code.
[0119] Obviously, the above specific implementation cases are merely examples for illustrating the application of the present method, and are not intended to limit the implementation methods. A person skilled in the art can make other variations and modifications based on the above description to study other related issues. Therefore, the scope of protection of the present invention should be based on the scope of protection of the claims.
[0120] Those skilled in the art will understand that all or part of the steps of implementing the above-mentioned method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium. When the program is executed, it executes the steps of the above-mentioned method embodiment; and the aforementioned storage medium includes: ROM, RAM, disk or optical disk, etc. Various media that can store program codes.
[0121] The embodiments of electronic devices and the like described above are merely illustrative. The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of the modules may be selected based on actual needs to achieve the objectives of the embodiments. Persons of ordinary skill in the art will be able to understand and implement the present invention without inventive effort.
[0122] Through the above description of the embodiments, those skilled in the art will clearly understand that each embodiment can be implemented using software plus a necessary general-purpose hardware platform, or of course, hardware. Based on this understanding, the essence of the above technical solution, or the portion that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a magnetic disk, or an optical disk, and includes a number of instructions for causing a computer device (such as a personal computer, server, or network device) to execute the methods described in each embodiment or certain portions of the embodiments.
[0123] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, rather than to limit them. Although the embodiments of the present invention have been described in detail with reference to the above embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the above embodiments, or replace some or all of the technical features therein with equivalents. However, these modifications or replacements do not cause the essence of the corresponding technical solutions to deviate from the scope of the technical solutions of the embodiments of the present invention.
[0124] Other embodiments of the present disclosure will readily occur to those skilled in the art after considering the specification and practicing the invention disclosed herein. The present invention is intended to cover any variations, uses, or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art not disclosed herein. The description and examples are to be considered as exemplary only, with the true scope and spirit of the present disclosure being indicated by the following claims.
[0125] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.< / t>
Claims
1. A graph representation learning and retrieval method based on the code repository graph structure, characterized by: The method comprises: S1: Construct a heterogeneous code graph related to the original code, the newly added code, or the modified code, and store the node set, edge set, node type, and edge type related to the heterogeneous code graph; S2: Perform graph representation learning on the heterogeneous code graph to generate a first embedding vector of the node; S3: Taking the nodes, edges and the first embedding vector of the node in the heterogeneous graph as input, performing self-supervised training using the intelligent model, outputting the training results and updating the first embedding vector of the node to the second embedding vector; S4: Store the second embedding vector, determine whether there is a new code component or modified code in the code repository, and if so, return to step S1; otherwise, proceed to step S5; S5: Receive a query request, generate a query vector representing the semantic features of the query according to the query request, match it with the second embedding vector, find a first node set related to the query semantics, and construct a candidate subgraph based on the first node set; S6: Generate target code according to the query request and the first node set, verify whether the target code complies with the structural syntax specification, and output the query result.
2. The method according to claim 1, characterized in that The heterogeneous code graph related to the code is constructed in S1, including: Identifying source code files in a code repository, generating a syntax tree (AST) from the source code files, and identifying a code structure in the source code files, including: defining a syntax node mapping function between a first syntax tree (AST) node set and a second syntax tree (AST) node set, mapping the first syntax tree (AST) node set to a second syntax tree (AST) node set; each node in the second syntax tree (AST) node set is defined as an entity node, and the node has additional attributes; Establishing a global symbol table for each entity node, determining namespace information, entity name and node metadata corresponding to the entity node, and generating a globally unique identifier; Traverse the reference relationship of the nodes in the symbol table and output the edge set.
3. The method according to claim 2, characterized in that The step S2 of performing graph representation learning on the heterogeneous code graph to generate a first embedding vector of the node includes: Obtaining text information of a node in a heterogeneous graph, preprocessing the text information, generating a first embedding vector corresponding to the node, recording the first embedding vector into an additional attribute of the node, and storing the first embedding vector; A corresponding table is established between the node's globally unique identifier and its first embedding vector, and is updated in real time.
4. The method according to claim 3, characterized in that In S3, the nodes, edges, and first embedding vectors of the nodes in the heterogeneous graph are used as inputs, and the intelligent model is used for self-supervised training, including: aggregating the different types of nodes and edges according to the node type, edge type, and the first embedding vector; The language model is iteratively trained on the randomly masked node text to restore the original text of the node; some edges are randomly removed to iteratively train the language model and determine whether the edge exists and the connection probability of the nodes related to the removed edge.
5. The method according to claim 4, characterized in that Outputting the training result and updating the first embedding vector of the node to the second embedding vector in S3 includes: The first embedding vector is updated in real time to a second embedding vector according to the iterative training result of the intelligent model, and the second embedding vector updated in real time is stored.
6. The method according to claim 5, characterized in that The step S5 receives a query request, generates a query vector representing the semantic features of the query according to the query request, matches the query vector with the second embedding vector, and generates a node set related to the query semantics, including: receiving input information comprising a query request, wherein the query request comprises a natural language description or a source code snippet; The query request is vectorized into a query vector based on an intelligent model; the cosine similarity between the query vector and the second embedding vector is calculated; a node set matching the query vector is obtained according to the similarity value, and a candidate subgraph is constructed according to the first node set.
7. The method according to claim 1, characterized in that The step S6 generates a target code segment according to the query request and the first node set, verifies whether the target code segment conforms to the structural syntax specification, and outputs the query result, including: S601: Obtain neighbor nodes that have semantic edges with the first node set, integrate the first node set and the neighbor nodes into a second node set, extract code snippets, comments, and signatures from the second node set, and splice them into natural language or prompt words; S602: Generate target code according to the prompt word; S603: Parse the target code, construct a heterogeneous graph of the target code, perform a structural comparison with the candidate subgraph, and determine whether the node set and edge set in the heterogeneous graph of the target code are fully contained in the node set and edge set of the candidate subgraph respectively. If the node set and edge set in the heterogeneous graph of the target code are fully contained in the node set and edge set of the candidate subgraph respectively, proceed to step S604; otherwise, return to step S602; S604: Output the target code.
8. A graph representation learning and retrieval system based on the code repository graph structure, characterized by: The system comprises: A construction module, configured to construct a heterogeneous code graph related to the original code, the newly added code, or the modified code, and store node sets, edge sets, node types, and edge types related to the heterogeneous code graph; A vector representation module, configured to perform graph representation learning on the heterogeneous code graph to generate a first embedding vector of the node; An intelligent module, configured to take nodes, edges, and first embedding vectors of the nodes in the heterogeneous graph as input, perform self-supervised training, output training results, and update the first embedding vectors of the nodes to second embedding vectors; a storage module configured to store the second embedding vector, determine whether there is a newly added code component or modified code in the code repository, and return to a construction module if so, or enter a search module if not; A search module receives a query request, generates a query vector representing the semantic features of the query according to the query request, matches the query vector with the second embedding vector, searches for a first node set related to the query semantics, and constructs a candidate subgraph based on the first node set; The verification module generates a target code according to the query request and the first node set, verifies whether the target code conforms to the structural syntax specification, and outputs the query result.
9. The system according to claim 8, characterized in that The building blocks include: A first construction unit is configured to identify a source code file of a code library, generate a syntax tree (AST) from the source code file, and identify a code structure in the source code file, including defining a syntax node mapping function between a first syntax tree (AST) node set and a second syntax tree (AST) node set, and mapping the first syntax tree (AST) node set to a second syntax tree (AST) node set; each node in the second syntax tree (AST) node set is defined as an entity node, and the node has additional attributes; A second construction unit is configured to establish a global symbol table for each entity node, determine namespace information, entity name, and node metadata corresponding to the entity node, and generate a globally unique identifier; The third construction unit is used to traverse the reference relationship of the nodes in the symbol table and output the edge set.
10. The system according to claim 9, characterized in that The vector representation module includes: A vector representation unit is used to obtain text information of nodes in the heterogeneous graph, pre-process the text information, generate a first embedding vector corresponding to the node, record the first embedding vector as an additional attribute of the node and store it; a vector correspondence unit is used to establish a correspondence table between the globally unique identifier of the node and its first embedding vector, and update it in real time; The training language module includes: a first training unit for aggregating the different types of nodes and edges according to the node type, edge type, and the first embedding vector; a second training unit for randomly masking the node text and iteratively restoring the original text of the node using the intelligent model; and randomly removing some edges for iterative training and determining whether the edge exists and the connection probability of the nodes related to the removed edge. The search module includes: a receiving unit for receiving input information including a query request, wherein the query request includes a natural language description or a source code snippet; a searching unit for a node set matching the input information including the query request, and constructing a candidate subgraph based on a first node set; The verification module includes: a first generation unit, used to obtain neighbor nodes that have semantic edges with the first node set, integrate the first node set and the neighbor nodes into a second node set, extract code snippets, comments, and signatures of the second node set, and splice them into natural language or prompt words; a second generation unit, used to generate target code according to the prompt words; a verification unit, used to parse the target code, construct a heterogeneous graph of the target code, perform structural comparison with the candidate subgraph, and determine whether the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set; if the node set and edge set in the target code heterogeneous graph are respectively fully contained in the candidate subgraph node set and edge set, jump to the output unit; otherwise, jump to the second generation unit; and an output unit, used to output the target code.
Citation Information
Cited By
Large code model training method and electronic equipment
CN121187569A
Model question-answering method and device based on code repository abstract and storage medium
CN121303364A
Method and system for generating standard drive code based on intention understanding
CN121560301A