Software knowledge graph search method and system for system-level code generation

By generating meta models in the software knowledge graph, filtering and expanding nodes, and combining with large-scale model reasoning, the problem of functional requirements mapping in system-level code generation is solved, and efficient system-level code generation and optimization is achieved.

CN119886291BActive Publication Date: 2025-08-26PEKING UNIV
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510372420.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-03-27
Publication Date
2025-08-26
Estimated Expiration
2045-03-27

AI Technical Summary

Technical Problem

The existing system-level code generation methods lack consideration for reusable code in other software systems in the same field, and cannot effectively map multiple nodes and relationships of system-level functional requirements to the software knowledge graph, and fail to fully utilize the big model for inference during the search process.

Method used

By generating a meta-model of the software knowledge graph, positioning the initial node set, filtering seed nodes, combining expansion rules to perform graph inference and expansion with the assistance of large models, and generating prompt words generated by system-level code.

Benefits of technology

It realizes searching for code that matches system-level functional requirements in the software knowledge graph, improving the accuracy and efficiency of code generation, and users can interactively correct search results to optimize code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119886291B_ABST
    Figure CN119886291B_ABST
Patent Text Reader

Abstract

The present invention discloses a software knowledge graph search method and system for system-level code generation, belonging to the field of computer software technology. The method comprises: obtaining a metamodel of the software knowledge graph and constructing a vector index for the description information of the nodes on the graph; locating the initial node set of the knowledge graph based on the user's system-level development requirements, and using a large model to filter the seed node set from it; then setting expansion rules, reasoning on the software knowledge graph, filtering and expanding nodes with the assistance of the large model, and finally displaying the search results in the form of a subgraph to achieve user interaction and prompt generation. The present invention can help the large model better generate code for the software system.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention belongs to the field of computer software technology and relates to a software knowledge graph search method and system for system-level code generation. Background Art

[0002] In recent years, large models have become a research hotspot in artificial intelligence, achieving significant progress in many tasks, including code generation. While researchers have proposed a variety of effective methods for single-line and method-level code generation, system-level code generation is also receiving increasing attention. A software system typically consists of multiple modules, each of which contains multiple functions. Consequently, the system's code size is large and its structure is complex, making code generation challenging. To address this challenge, a feasible system-level code generation approach involves collecting multiple software systems in the same domain, constructing a software knowledge graph (knowledge base), and then performing retrieval-augmented generation (RAG). This involves searching for code implementing the same functionality from multiple existing software systems before generating code for a specific function. This content is then incorporated into the model's prompts, prompting the model to generate code in a reusable manner. The comprehensiveness and accuracy of the search results in this process significantly impact the generation performance.

[0003] To improve the effectiveness of system-level code generation, the mainstream approach in existing work is RAG. This approach searches for relevant information from external knowledge bases and uses this information to guide answer generation. This approach can, to a certain extent, alleviate the practical challenges faced by large models, such as a lack of domain-specific knowledge and the generation of "hallucinations." Specifically, for code generation tasks, some work uses vector databases as knowledge bases and searches based on vector similarity. Other recent work uses knowledge graphs to search for structured relational information. Software code is typically constructed into a knowledge graph, with classes and methods represented as nodes. Relationships between these code elements, such as inheritance and calls, are then represented as graph relationships, clearly reflecting the structure of the software system. Knowledge graph-based search methods primarily focus on downstream tasks such as code completion and bug fixing. For user-entered functional requirements, these methods typically first locate nodes on the knowledge graph based on lexical or vector similarity. They then continue reasoning on the graph based on rules or algorithms, ultimately integrating the resulting content into the large model's prompts to complete the downstream task.

[0004] However, because the downstream tasks focused on by the above methods are more focused on searching for code within a software system, they lack consideration of reusable code within other software systems in the same field. When developing a software system in a specific field, the implementation of a function may require a combination of multiple code snippets, requiring the definition of data classes, the writing of multiple methods, etc. The required code is not necessarily semantically similar to the functional requirements proposed by the user. Existing methods are unable to map system-level functional requirements to multiple nodes and relationships in the software knowledge graph. Moreover, existing methods focus on fine-grained code search, usually focusing on very specific tasks or implementation details, such as "determining palindrome strings", rather than a functional requirement in the software system, such as "publishing a blog post". At the same time, existing methods do not differentiate between different relationships on the graph during the search process, nor do they allow large models to fully participate in the reasoning process.

[0005] In order to search for code that better matches system-level functional requirements from the software knowledge graph, thereby helping the large model to better generate software system code, it is necessary to invent a software knowledge graph search method. Summary of the Invention

[0006] In order to overcome the shortcomings of existing system-level code generation technologies based on vector retrieval and software knowledge graph retrieval, the present invention provides a software knowledge graph search method and system for system-level code generation, and implements a tool prototype based on this, which can help large models better generate code for software systems.

[0007] To achieve the above objectives, the technical solution of the present invention includes the following contents.

[0008] A software knowledge graph search method for system-level code generation, the method comprising:

[0009] Generate a software knowledge graph and a metamodel of the software knowledge graph, wherein the metamodel is a set of node types and a set of relationship types on the software knowledge graph;

[0010] Locate the nodes in the software knowledge graph according to system-level development requirements to generate an initial node set ;

[0011] From the initial node set Filter seed nodes to obtain a set of seed nodes ;

[0012] Seed node set As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ;

[0013] Comprehensive seed node set and the expanded node collection , get the node set of the subgraph ;

[0014] Traverse the node set After building a prompt word for system-level code generation based on the system-level development requirements for each node in the system-level development requirements, the system-level code is generated based on the large model.

[0015] Furthermore, the types of the node include: JavaClass type, JavaMethod type and JavaField type; wherein the JavaClass type represents a class in a Java software system, the JavaMethod type represents a method in a Java software system, and the JavaField type represents a member variable in a Java software system;

[0016] The attributes of the JavaClass type node include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, content attribute and description attribute;

[0017] The attributes of the JavaMethod type node include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, params attribute, returnType attribute, content attribute and description attribute;

[0018] The attributes of the JavaField type node include: id attribute, name attribute, fullName attribute, projectName attribute and type attribute;

[0019] Among them, the id attribute stores the identifier of the node in the graph database, the vid attribute stores the unique identifier of the node, the name attribute stores the name of the class, method or member variable corresponding to the node, the fullName attribute stores the full name, the projectName attribute stores the name of the software system to which the node belongs, the params attribute stores the parameter list of the method corresponding to the node, the returnType attribute stores the return value type of the method corresponding to the node, the content attribute stores the complete code of the class or method, the description attribute saves a brief description of the complete code, and the type attribute stores the type of the member variable.

[0020] Furthermore, the nodes in the software knowledge graph are located according to the system-level development requirements to generate an initial node set. ,include:

[0021] Convert the description attribute into an embedded vector and construct the vector index of the node;

[0022] Generate embedding vectors of system-level development requirements;

[0023] Generate the initial node set based on the cosine distance between the embedded vectors .

[0024] Furthermore, the initial node set Filter seed nodes to obtain a set of seed nodes ,include:

[0025] Generate a task description, which is used to introduce the background of the seed node screening task in detail;

[0026] Generate precautions, which are used to limit the output result format of the large model output result;

[0027] The initial node set Each node number in , and combined with the fullName attribute and the description attribute, generates an initial node information description;

[0028] The task description, the precautions and the initial node information description are embedded in the prompt word template for seed node screening, and combined with the large model to obtain the seed node set .

[0029] Furthermore, the seed node set As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ,include:

[0030] For the seed node set Any node of type JavaMethod , get all the relationships associated with the node ;

[0031] Based on the metamodel and the relationship , and use the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule to expand the node of type JavaMethod After the expansion is performed and the first extended node set is obtained, the first extended node set is added to the extended node set ;

[0032] In the first extended node set and the seed node set Based on the data class nodes in the data class, the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule are used to expand to obtain a second expansion node set, and the second expansion node set is added to the expansion node set ;

[0033] The first extension rule includes: obtaining method Calling method ; According to the call method The number of methods for the method Screening, get the method ; Get the method The corresponding node;

[0034] The second extension rule includes: obtaining the method to call Method The corresponding node;

[0035] The third extended rule includes: obtaining the method Corresponding parameter type The corresponding node;

[0036] The fourth extended rule includes: obtaining the method Corresponding return value type The corresponding node; according to the return value type The corresponding node is the number of all RETURN_TYPE relationships of the tail node to filter and get the return value type ; Get the return value type The corresponding node;

[0037] The fifth extended rule includes: obtaining the method Class ; By judging the class Is it an interface class or an interface implementation class for the class Filter and get the class ; In the class In the case of an interface class, get the interface implementation class of the interface class And this method In the implementation class Methods in , and obtain the method Calling method ; In the class In the case of an interface implementation class, get the interface class corresponding to the interface implementation class and methods In the interface class Methods defined in , and get the method called Method ; Acquisition method , interface implementation class ,method Corresponding nodes and / or methods , interface class ,method The corresponding node;

[0038] The first extension node is already in the seed node set Or the extended node set When the first expansion node is in the process of being expanded, the first expansion node is not expanded.

[0039] Furthermore, the method Screening, get the method ,include:

[0040] Statistics Methods The number of method call relationships with any JavaMethod node as the first node as the tail node;

[0041] If the number of method call relationships is greater than the set threshold, the method is filtered out. ;

[0042] If the number of method call relationships is less than or equal to the set threshold, the method As a method .

[0043] Furthermore, in the first extended node set and the seed node set Based on the data class nodes in , the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule are used to expand to obtain a second expansion node set, including:

[0044] For a first expansion node obtained according to the first expansion rule, the first expansion node is expanded based on the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule, and the fifth expansion rule to generate a second expansion node; wherein the number of times the first expansion rule is used does not exceed a set upper limit;

[0045] For a first extended node obtained according to the second extension rule, the first extended node is extended based on the third extension rule, the fourth extension rule, and the fifth extension rule to generate a second extended node;

[0046] For the first expansion node obtained according to the third expansion rule or the fourth expansion rule, obtain the parameter type or return value type All member variables included Afterwards, the member variable Filter and filter the member variables The corresponding node is added to the second extended node set.

[0047] Furthermore, the member variable Conduct screening, including:

[0048] According to the task description and the member variables The name attribute and type attribute of the , construct the prompt word for member variable screening;

[0049] Input the prompt words for member variable screening into the large model to obtain member variables .

[0050] A software knowledge graph search system for system-level code generation, the system comprising:

[0051] A metamodel construction module, used to generate a software knowledge graph and a metamodel of the software knowledge graph, wherein the metamodel is a set of node types and a set of relationship types on the software knowledge graph;

[0052] The initial node generation module is used to locate the nodes in the software knowledge graph according to the system-level development requirements to generate the initial node set ;

[0053] Seed node generation module, used to generate the seed node from the initial node set Filter seed nodes to obtain a set of seed nodes ;

[0054] Extended node generation module, used to generate seed node sets As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ;

[0055] Subgraph node generation module, used to synthesize seed node sets and the expanded node collection , get the node set of the subgraph ;

[0056] A code generation module for traversing the node set After building a prompt word for system-level code generation based on the system-level development requirements for each node in the system-level development requirements, the system-level code is generated based on the large model.

[0057] An electronic device comprising: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, it implements any of the above-mentioned software knowledge graph search methods for system-level code generation.

[0058] A computer-readable storage medium having computer program instructions stored thereon, wherein the computer program instructions, when executed by a processor, implement any of the above-mentioned software knowledge graph search methods for system-level code generation.

[0059] Compared with the prior art, the present invention has at least the following beneficial effects.

[0060] By taking system-level functional requirements as input, users can search and reason within a software knowledge graph constructed from multiple software systems in the same domain. The graph then displays nodes and relationships matching the functional requirements as subgraphs. Users can also modify the subgraphs, deleting unnecessary nodes. The modified subgraphs are then used to construct prompts, enabling the larger model to generate code that implements the system-level functional requirements. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] Figure 1 It is a schematic diagram of the execution flow of the present invention.

[0062] Figure 2 It is the meta-model of software knowledge graph.

[0063] Figure 3 This is the result display of the seed node in the example of the present invention.

[0064] Figure 4 This is the search result obtained by taking a seed node as the starting point in the example of the present invention.

[0065] Figure 5 It is a prompt word constructed according to the search results in the example of the present invention, which is used to allow the large model to generate code. DETAILED DESCRIPTION

[0066] The present invention will be described in further detail below with reference to the accompanying drawings. The examples given are only used to explain the present invention and are not used to limit the scope of the present invention.

[0067] The present invention can locate the initial node set on the software knowledge graph according to system-level development requirements, use the big model to filter and retain the seed node set that is most relevant to the requirements, set different expansion rules according to node type and relationship type, expand from the seed node set, use the big model to assist in filtering during expansion, and finally display the search results in the form of a subgraph. Users can modify the subgraph, and the modified subgraph content will be used to construct prompt words for the big model code generation.

[0068] Figure 1 The execution process of the tool of the present invention is explained: first, the metamodel of the software knowledge graph is obtained, and a vector index is constructed for the description information of the nodes on the graph; then, based on the user's system-level development needs, the initial node set of the knowledge graph is located, and the seed node set is screened from it using the large model; then, expansion rules are set, reasoning is performed on the software knowledge graph, and nodes are screened and expanded with the assistance of the large model, and finally the search results are displayed in the form of a subgraph to realize user interaction and prompt generation.

[0069] 1. Preprocessing stage.

[0070] 1.1 Obtain the metamodel of the software knowledge graph.

[0071] After the existing software knowledge graph construction tool constructs the corresponding knowledge graph and stores it in the neo4j database, traverse the knowledge graph to obtain its metamodel. The so-called metamodel schema refers to the set of node types and relationship types on the knowledge graph, that is, schema = <{EntityType}, {RelationType}>. Each node Entity in the knowledge graph has a type, {EntityType} = {Entity.type}, and the type of each relationship Relation is determined by the name of the relationship itself and the type of the first and last nodes of the relationship, that is, {RelationType} = {<Relation.start.type, Relation.end.type, Relation.name> In addition, each node type in the knowledge graph has some attributes, and these attributes also have types, which are recorded as AttributeType. Then {AttributeType}= {<Attribute.entity.type, Attribute.name>}.

[0072] Based on the concepts defined above, we traverse all nodes in the software knowledge graph in the neo4j database, record the node type and the attribute types of each node type. We then traverse all relationships in the knowledge graph, obtain the types of the first and last nodes of each relationship, and the names of the relationships themselves, and establish associations between the corresponding node types in the metamodel. Ultimately, we obtain a set of node types and a set of relationship types, which in turn form the metamodel of the software knowledge graph.

[0073] 1.2 Embed the description information of the software knowledge graph nodes and build a vector index.

[0074] There are a large number of nodes in the software knowledge graph. In order to conduct more efficient searches in the future and to find nodes that are closely related to the semantics of system-level development requirements, we first build an index for the nodes of the knowledge graph. The JavaClass and JavaMethod type nodes of the code knowledge graph represent a class or a method in the Java software system, respectively, and are usually important parts of implementing a certain function. The content attribute of these two types of nodes stores the complete code of the class or method, and the description attribute stores a brief description of the code, describing the function implemented by the code in Chinese. Considering that the user input in the subsequent search stage is relatively short development requirements, if the relatively complex code content is converted into a vector to build an index, it may be difficult to match the user's requirement description. Therefore, the description information is used to build the index.

[0075] For each JavaClass and JavaMethod node obtained by traversing the software knowledge graph, the description attribute is obtained and converted into an embedding vector using the bge-small-zh-v1.5 model, a common embedding model for the Chinese language. This model, with its smaller parameter count, improves embedding efficiency. After generating the embedding vector, it is stored in ElasticSearch along with the node's vid attribute (a unique identifier). Using ElasticSearch as the underlying storage and search engine, this approach combines vector storage with KNN (Known Nearest Neighbor) search technology to achieve efficient vector indexing.

[0076] 2. Search phase.

[0077] 2.1 Locating the initial node set of the software knowledge graph based on system-level development requirements .

[0078] The user inputs the system-level development requirements, which are also converted into embedding vectors using the bge-small-zh-v1.5 model. Using the vector search function provided by ElasticSearch, the query embedding is matched with all stored embedding vectors, and the cosine similarity is calculated. In essence, for the user's development needs, semantically similar nodes are selected from all JavaClass and JavaMethod nodes in the software knowledge graph according to the functional description of the code corresponding to the node. For the query results, they are sorted from high to low according to the cosine similarity, and the top 5 most relevant results are returned. The results include the vid attribute of the node, which is used in the neo4j database to query the corresponding graph nodes, and these nodes are used as the initial node set, recorded as .

[0079] 2.2 Using large models Filtering the seed node set .

[0080] for Although the description attributes of the nodes in the collection have a very high semantic similarity with the system-level development requirements, they are not necessarily directly related to the development requirements. It may be that only some keywords in the sentence are relatively consistent, but the functions actually expressed are not exactly the same. In order to accurately search and reason on the software knowledge graph, it is necessary to ensure that the expansion starts from the appropriate node on the graph. Therefore, further Filter the nodes in the set and select the most representative node set that best matches the development requirements, which is recorded as the seed node set .

[0081] Due to the powerful ability of the large model in natural language understanding and code understanding, a prompt word is constructed and the large model is used to complete the screening process. The prompt word consists of three parts: task description, precautions, and initial node information description. The task description part introduces the background of the task in detail, that is, when developing a software system, you search for codes that implement a certain function in other software systems in the same field, and you need to select helpful code snippets from them. The precautions part emphasizes the restrictions on the format of the output results of the large model, requiring the large model to output a list with the numbers of the selected code snippets to facilitate subsequent analysis of the output. In the initial node information description part, for Each node in the collection is numbered and the information of the fullName attribute and description attribute is listed. The fullName attribute is the full name of the class or method corresponding to the node, with complete path information, and the description attribute is the description information of the corresponding code. Construct the prompt words as described above to make the large model Filter out the seed node set ,make sure Each node in corresponds to a method or data class and serves as the starting point for the reasoning phase.

[0082] 3. Reasoning stage.

[0083] On the software knowledge graph, the seed node set As the starting point of reasoning, the meta-model of the knowledge graph is integrated to perform graph reasoning and expansion. Different expansion rules are set for different types of nodes and relationships, with the goal of expanding as many nodes as possible that are related to system-level development requirements. The expanded node set is recorded as , the extended relation set is denoted as , then the final subgraph node set is , the relationship set is .

[0084] The extension rules are centered around the JavaMethod node and are described below:

[0085] (1) For JavaMethod type nodes , get all its associated relationships, for each relationship :

[0086] (1.1) For method call relations ( :JavaMethod)-[ :METHOD_CALL]->( :JavaMethod), As The method of calling is more important for the realization of the requirements, so it needs to be expanded. However, if If it is a method that appears frequently in a software system, it means that it is not related to a specific functional requirement, but a general tool method and should not be used as a search result. The tail node of the METHOD_CALL relationship of the first node , further obtain The number of METHOD_CALL relationships with any JavaMethod node as the first node and based on a threshold (The value in this embodiment is 10) After screening, the nodes after screening Add to the extended node set, which needs further processing. .

[0087] (1.2) For method call relations ( :JavaMethod)-[ :METHOD_CALL]->( :JavaMethod), the direction of this relationship is opposite to (1.1), indicating that the node By another method Call. Add to the extended node set, which needs further processing. .

[0088] (1.3) For parameter type relations ( :JavaMethod)-[ :PARAM_TYPE]->( :JavaClass), yes The parameter type of a method usually represents the data type that a function in the software system wants to operate on, so The node joins the extended node set, .for , continue to process according to (2.3) rules.

[0089] (1.4) For the return value type relationship ( :JavaMethod)-[ :RETURN_TYPE]->( :JavaClass), It's a method The return value type. You need to first determine Is it a class that appears frequently in a software system? If so, it means that it is common in the entire system rather than related to specific functional requirements. The number of all RETURN_TYPE relationships of the tail node, based on a threshold (The value in this embodiment is 10) After screening, the nodes after screening Add to the extended node collection .for , continue to process according to (2.3) rules.

[0090] (1.5) For the included method relationship ( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), As a class, containing methods ,according to Different situations require different treatments:

[0091] (1.5.1) Judgment Is it an interface? If so, you should further get the implementation class of the interface and get the method The specific implementation in the implementation class finally gets the method The complete call chain. The judgment method is to match the following path in the knowledge graph:

[0092] ( :JavaMethod)<-[ :HAVE_METHOD]-( :JavaClass)-[ :IMPLEMENT] ->( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), if the match is successful, then the path matched Node represents the interface The implementation class, Node Representative Method In the specific implementation of the interface implementation class, .and The same JavaMethod node usually calls another method, so The node starts to expand again, that is ( :JavaMethod)-[ :METHOD_CALL]->( :JavaMethod), there is .

[0093] (1.5.2) Judgment Is it an implementation class of an interface? If so, you should further get the interface and get the method The definition in the interface finally gets the method The judgment method is similar to (1.5.1), and the following path needs to be matched in the knowledge graph:

[0094] ( :JavaMethod)<-[ :HAVE_METHOD]-( :JavaClass)<-[ :IMPLEMENT]-( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), if the match is successful, then Node Representative Implemented interface class, Node Representative Method Definition in the interface class, .and It is also a JavaMethod node, which is usually called by another method, so The node starts to expand again, that is ( :JavaMethod)-[ :METHOD_CALL]->( :JavaMethod), there is .

[0095] If the above two judgment results are both negative, it means May contain many methods, but except The methods other than are used to implement other functions, so they will not be expanded.

[0096] (2) For some nodes expanded from rule (1) and The data class nodes in the need for further iterative expansion. But special attention should be paid to the fact that if any node expanded in any step is already in or , then no further expansion will be started from it. The specific rules are as follows:

[0097] (2.1) For nodes expanded according to the rule (1.1), they can continue to be expanded according to the rule (1.1), but an upper limit on the number of expansions according to the rule (1.1) must be set. (In this example, the value is 3.) This prevents excessive expansion of nodes for deep method calls, which can lead to poor readability of search results. Nodes expanded by rule (1.1) can be further expanded according to rules (1.3), (1.4), and (1.5).

[0098] (2.2) For the nodes expanded according to the rule (1.2), they can also be further expanded according to the rules (1.3), (1.4) and (1.5).

[0099] (2.3) For the nodes expanded according to the rules (1.3) and (1.4) and , and It is a data class, and the member variables it contains are important for system-level function development, so obtaining and All associated member variable relationships ( :JavaClass)-[ :HAVE_FIELD]->( :JavaField) and ( :JavaClass)-[ :HAVE_FIELD]->( :JavaField). However, and The number of member variables may be large, and not every member variable is required for the development function, so add a large model screening operation, describe the task in detail in the prompt word, and then and All associated The name attribute and type attribute of the node are listed, where the name attribute is The variable name of the corresponding member variable, and the type attribute is the type of the member variable. After the prompt word is built, the large model will filter the relevant JavaField nodes , for each , It is important to note that there is an upper limit on the number of times the rule (2.3) can be used. (The value in this embodiment is 2), that is, it can only be A JavaClass node expands into a JavaField node.

[0100] In the present invention, only the relationships involved in the above rules are processed, and other relationships are ignored.

[0101] 4. User interaction and prompt generation stage.

[0102] In order to intuitively display the search results obtained in the above steps, a subgraph is used for visualization, and the user can modify the subgraph. Specifically, the D3.js library is used to draw an interactive force-directed graph. The user can click on each node of the subgraph to view the detailed attribute information of the node, especially the content attribute and the description attribute, so as to evaluate the role of the node in generating the specified functional code for the large model. If the user believes that some nodes are irrelevant, redundant, and not very helpful in the code generation stage, they can right-click on the interface and delete these nodes in turn. Through this interactive subgraph correction process, the user can delete unnecessary information in the search results, thereby being able to control the contextual information used by the large model when generating system-level code and optimize the code generation effect.

[0103] After the subgraph is modified, each node of the subgraph is traversed, the content is analyzed one by one, and the prompt words of the large model are constructed according to the user's development needs and the content of the subgraph. Finally, the large model can generate system-level code.

[0104] In order to verify the effectiveness of the present invention, that is, the correctness of searching and reasoning on the software knowledge graph according to system-level development requirements, the present invention selected 4 Java language software projects for experimental verification, all of which are high-star open source projects on GitHub and all belong to personal blog systems.

[0105] After constructing the software knowledge graph for the four projects, the meta-model of the knowledge graph is first obtained in the pre-processing stage, such as Figure 2 As shown in the figure, the metamodel defines the entities, relationships, and attributes of the entities in the knowledge graph; this stage also includes embedding the description information of JavaClass and JavaMethod nodes, building a vector index, and saving it in ElasticSearch.

[0106] In the search phase, for the system-level development requirement of "update user information", we first use the vector search function of ElasticSearch to locate the initial node set of the software knowledge graph. , contains 5 nodes, their fullName and description attributes are shown in Table 1.

[0107] node fullName description Node 0 top.naccl.mapper.UserMapper.updateUserByUsername( String username, User user ) Update user information for the specified username Node 1 com.dimple.system.service.service.impl.SysUserServiceImpl.updateUserProfile(SysUserBO user ) Provides user information modification function, updates user basic information by receiving user data objects Node 2 top.naccl.mapper.FriendMapper.updateFriend( top.naccl.model.dto.Friend friend ) Provide friend information update function Node 3 top.naccl.service.impl.UserServiceImpl.changeAccount( User user, String jwt ) Provides user account information change functions, including updating user passwords and other information Node 4 top.naccl.service.FriendService.updateFriend( top.naccl.model.dto.Friend friend ) Provide friend information update function, including modifying friend's detailed information

[0108] Then use the large model from Filtering the seed node set , the output of the large model is "[0, 1]", that is, nodes 0 and 1 are selected as seed nodes, such as Figure 3 As shown, two seed nodes are displayed on the left. Select a node to view its attribute information on the right.

[0109] In the inference stage, The nodes in are used as the starting point for reasoning on the knowledge graph. Figure 4This diagram shows the subgraph derived from reasoning with seed node 1, com.dimple.system.service.service.impl.SysUserServiceImpl.updateUserProfile (abbreviated as SysUserServiceImpl.updateUserProfile), as the starting point. (The reasoning process for seed node 0 is similar.) Specifically, it is a JavaMethod type node. Retrieving all its associated relationships reveals two method call relationships (METHOD_CALL) with it as the first node, and two tail nodes, updateUser and convert, both of which are JavaMethod type nodes. According to rule (1.1), we need to determine whether these two methods appear frequently in the software system. We retrieve the number of all METHOD_CALL relationships with these methods as tail nodes, and find that the number corresponding to convert exceeds the threshold. , updateUser does not exceed, so add the updateUser node , relationship join The METHOD_CALL relationship with seed node 1 as the end node does not exist, so there is no need to use rule (1.2). There is one parameter type relationship (PARAM_TYPE) with seed node 1 as the first node, and the end node is SysUserBO. According to rule (1.3), add it , relationship join . The return value type relationship (RETURN_TYPE) with seed node 1 as the first node does not exist, so there is no need to use rule (1.4). There is one included method relationship (HAVE_METHOD) with seed node 1 as the last node. Different processing is required according to the different situations of the first node m. At this time, according to rules (1.5.1) and (1.5.2), the path described in the rules is matched in the knowledge graph to determine whether m is an interface or an implementation class. The path matching of rule (1.5.2) is successful, so all nodes on the path are added. , including SysUserServiceImpl, SysUserService and updateUserProfile, the corresponding relationships are also added The rule (1.5.2) also requires that the updateUserProfile node be expanded again, and the updateProfile node be obtained through the METHOD_CALL relationship.

[0110] The updateUser node expanded according to rule (1.1) can be further expanded according to rule (2.1). Specifically, according to rule (1.3), the JavaClass node SysUser can be expanded. It is a data class. In addition, according to rule (1.5), the HAVE_METHOD relationship with updateUser as the tail node needs to be handled differently based on the situation of the first node m. The paths described in (1.5.1) and (1.5.2) are not matched successfully, so they are not expanded further.

[0111] For the two nodes SysUserBO and SysUser obtained by expanding according to rule (1.3), several JavaField nodes can be further expanded from these JavaClass nodes according to rule (2.3). The two nodes do not exceed Therefore, the (2.3) rule can be applied to both of them. The large model filters the JavaField nodes associated with the two JavaClass nodes, retaining only the member variables that are more helpful for development needs.

[0112] Finally got Figure 4 After viewing the search results, users can interactively modify the subgraph based on their system-level development needs, deleting unnecessary nodes. Each node in the modified subgraph represents reusable code from other software systems in the same domain, helping large models complete system-level code generation tasks. Figure 5 This example demonstrates a useful prompt, where the parts enclosed by "{}" are replaceable. For example, "{systemName}" can be replaced with "Personal Blog System" in this example, and "{query}" can be replaced with "Update User Information." This prompt emphasizes the importance of not only paying attention to the current system context when developing a software system, but also of referencing and reusing code samples found in searches, rather than directly generating all the code.

Claims

1. A software knowledge graph search method for system-level code generation, characterized in that: The method comprises: Generate a software knowledge graph and a metamodel of the software knowledge graph, wherein the metamodel is a set of node types and a set of relationship types on the software knowledge graph; Locate the nodes in the software knowledge graph according to system-level development requirements to generate an initial node set ; Wherein, the types of the nodes include: JavaClass type, JavaMethod type and JavaField type; From the initial node set Filter seed nodes to obtain a set of seed nodes ; Seed node set As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ; Among them, the seed node set As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ,include: For the seed node set Any node of type JavaMethod , get all the relationships associated with the node ; Based on the metamodel and the relationship , and use the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule to expand the node of type JavaMethod After the expansion is performed and the first extended node set is obtained, the first extended node set is added to the extended node set ; In the first extended node set and the seed node set Based on the data class nodes in the data class, the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule are used to expand to obtain a second expansion node set, and the second expansion node set is added to the expansion node set ; The first extension rule includes: obtaining method Calling method ; According to the call method The number of methods for the method Screening, get the method ; Get the method The corresponding node; The second extension rule includes: obtaining the method to call Method The corresponding node; The third extended rule includes: obtaining the method Corresponding parameter type The corresponding node; The fourth extended rule includes: obtaining the method Corresponding return value type The corresponding node; according to the return value type The corresponding node is the number of all RETURN_TYPE relationships of the tail node to filter and get the return value type ; Get the return value type The corresponding node; The fifth extended rule includes: obtaining the method Class ; By judging the class Is it an interface class or an interface implementation class for the class Filter and get the class ; In the class In the case of an interface class, get the interface implementation class of the interface class And this method In the implementation class Methods in , and obtain the method Calling method ; In the class In the case of an interface implementation class, get the interface class corresponding to the interface implementation class and methods In the interface class Methods defined in , and get the method called Method ; Acquisition method , interface implementation class ,method Corresponding nodes and / or methods , interface class ,method The corresponding node; The first extension node is already in the seed node set Or the extended node set When the first expansion node is not expanded; Comprehensive seed node set and the expanded node collection , get the node set of the subgraph ; Traverse the node set After building a prompt word for system-level code generation based on the system-level development requirements for each node in the system-level development requirements, the system-level code is generated based on the large model.

2. The method according to claim 1, characterized in that The JavaClass type represents a class in a Java software system, the JavaMethod type represents a method in a Java software system, and the JavaField type represents a member variable in a Java software system; The attributes of the JavaClass type node include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, content attribute and description attribute; The attributes of the JavaMethod type node include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, params attribute, returnType attribute, content attribute and description attribute; The attributes of the JavaField type node include: id attribute, name attribute, fullName attribute, projectName attribute and type attribute; Among them, the id attribute stores the identifier of the node in the graph database, the vid attribute stores the unique identifier of the node, the name attribute stores the name of the class, method or member variable corresponding to the node, the fullName attribute stores the full name, the projectName attribute stores the name of the software system to which the node belongs, the params attribute stores the parameter list of the method corresponding to the node, the returnType attribute stores the return value type of the method corresponding to the node, the content attribute stores the complete code of the class or method, the description attribute saves a brief description of the complete code, and the type attribute stores the type of the member variable.

3. The method according to claim 2, characterized in that The nodes in the software knowledge graph are located according to the system-level development requirements to generate an initial node set. ,include: Convert the description attribute into an embedded vector and construct the vector index of the node; Generate embedding vectors of system-level development requirements; Generate the initial node set based on the cosine distance between the embedded vectors .

4. The method according to claim 2, characterized in that The initial node set Filter seed nodes to obtain a set of seed nodes ,include: Generate a task description, which is used to introduce the background of the seed node screening task in detail; Generate precautions, which are used to limit the output result format of the large model output result; The initial node set Each node number in , and combined with the fullName attribute and the description attribute, generates an initial node information description; The task description, the precautions and the initial node information description are embedded in the prompt word template for seed node screening, and combined with the large model to obtain the seed node set .

5. The method according to claim 1, wherein The method Screening, get the method ,include: Statistics Methods The number of method call relationships with any JavaMethod node as the first node as the tail node; If the number of method call relationships is greater than the set threshold, the method is filtered out. ; If the number of method call relationships is less than or equal to the set threshold, the method As a method .

6. The method according to claim 1, characterized in that In the first extended node set and the seed node set Based on the data class nodes in , the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule are used to expand to obtain a second expansion node set, including: For a first expansion node obtained according to the first expansion rule, the first expansion node is expanded based on the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule, and the fifth expansion rule to generate a second expansion node; wherein the number of times the first expansion rule is used does not exceed a set upper limit; For a first extended node obtained according to the second extension rule, the first extended node is extended based on the third extension rule, the fourth extension rule, and the fifth extension rule to generate a second extended node; For the first expansion node obtained according to the third expansion rule or the fourth expansion rule, obtain the parameter type or return value type All member variables included Afterwards, the member variable Filter and filter the member variables The corresponding node is added to the second extended node set.

7. The method according to claim 6, characterized in that The member variable Conduct screening, including: According to the task description and the member variables The name attribute and type attribute of the , construct the prompt word for member variable screening; Input the prompt words for member variable screening into the large model to obtain member variables .

8. A software knowledge graph search system for system-level code generation, characterized by: The system comprises: A metamodel construction module, used to generate a software knowledge graph and a metamodel of the software knowledge graph, wherein the metamodel is a set of node types and a set of relationship types on the software knowledge graph; The initial node generation module is used to locate the nodes in the software knowledge graph according to the system-level development requirements to generate the initial node set ; Wherein, the types of the nodes include: JavaClass type, JavaMethod type and JavaField type; Seed node generation module, used to generate the seed node from the initial node set Filter seed nodes to obtain a set of seed nodes ; Extended node generation module, used to generate seed node sets As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ; Among them, the seed node set As the starting point of reasoning, the meta-model is integrated to perform graph reasoning and expansion to obtain the extended node set ,include: For the seed node set Any node of type JavaMethod , get all the relationships associated with the node ; Based on the metamodel and the relationship , and use the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule to expand the node of type JavaMethod After the expansion is performed and the first extended node set is obtained, the first extended node set is added to the extended node set ; In the first extended node set and the seed node set Based on the data class nodes in the data class, the first expansion rule, the second expansion rule, the third expansion rule, the fourth expansion rule and the fifth expansion rule are used to expand to obtain a second expansion node set, and the second expansion node set is added to the expansion node set ; The first extension rule includes: obtaining method Calling method ; According to the call method The number of methods for the method Screening, get the method ; Get the method The corresponding node; The second extension rule includes: obtaining the method to call Method The corresponding node; The third extended rule includes: obtaining the method Corresponding parameter type The corresponding node; The fourth extended rule includes: obtaining the method Corresponding return value type The corresponding node; according to the return value type The corresponding node is the number of all RETURN_TYPE relationships of the tail node to filter and get the return value type ; Get the return value type The corresponding node; The fifth extended rule includes: obtaining the method Class ; By judging the class Is it an interface class or an interface implementation class for the class Filter and get the class ; In the class In the case of an interface class, get the interface implementation class of the interface class And this method In the implementation class Methods in , and obtain the method Calling method ; In the class In the case of an interface implementation class, get the interface class corresponding to the interface implementation class and methods In the interface class Methods defined in , and get the method called Method ; Acquisition method , interface implementation class ,method Corresponding nodes and / or methods , interface class ,method The corresponding node; The first extension node is already in the seed node set Or the extended node set When the first expansion node is not expanded Subgraph node generation module, used to synthesize seed node sets and the expanded node collection , get the node set of the subgraph ; A code generation module for traversing the node set After building a prompt word for system-level code generation based on the system-level development requirements for each node in the system-level development requirements, the system-level code is generated based on the large model.

9. An electronic device, characterized in that: The electronic device includes: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, it implements the software knowledge graph search method for system-level code generation as described in any one of claims 1-7.

Citation Information

Patent Citations

  • Software code knowledge graph construction method and tool

    CN114035783A

  • Intelligent auxiliary method and platform for software development

    CN117369786A