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

By positioning the initial nodes in the software knowledge graph, filtering seed nodes and performing graph inference, forming a set of subgraph nodes, the problem of difficulty in mapping system-level functional requirements to the knowledge graph in the existing technology is solved, and more efficient system-level code generation is achieved.

CN119886291AActive Publication Date: 2025-04-25PEKING UNIV
View PDF 8 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing system-level code generation methods are difficult to map system-level functional requirements to multiple nodes and relationships in the software knowledge graph, and focus on fine-grained code search, lacking the differential processing of different relationships and the full participation of large models in the inference process.

Method used

A software knowledge graph search method for system-level code generation is proposed. By generating initial node sets, screening seed nodes, and fusion metamodels for graph inference and expansion, forming a node set of subgraphs, and generating system-level code in combination with a large model.

Benefits of technology

It realizes searching for code matching system-level functional requirements in the software knowledge graph, helping the big model better generate software system code, and improving the effectiveness and accuracy of system-level code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119886291A_ABST
    Figure CN119886291A_ABST
Patent Text Reader

Abstract

The invention discloses a software knowledge graph searching method and system for system-level code generation, and belongs to the technical field of computer software. The method comprises the following steps: acquiring a meta-model of a software knowledge graph, and constructing a vector index for description information of nodes on the graph; aiming at system-level development requirements of users, positioning an initial node set of the knowledge graph, and screening a seed node set from the initial node set by using a large model; setting an extension rule, reasoning on a software knowledge graph, screening and extending nodes under the assistance of a large model, and finally displaying a search result in a sub-graph form to realize user interaction and prompt generation. According to the method, the large model can be helped to better generate the codes of 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 the field of artificial intelligence, and have made significant progress in many tasks including code generation. Researchers have proposed a variety of effective methods for single-line and method-level code generation tasks, and system-level code generation has also received increasing attention. A software system usually consists of multiple modules, each of which contains multiple functions. Therefore, the system has a large amount of code and a complex structure, which makes generation difficult. To meet this challenge, a feasible system-level code generation method is to collect multiple software systems in the same field, construct a software knowledge graph (knowledge base), and generate through retrieval-augmented generation (RAG). That is, before the large model generates the code for a certain function, it searches for the code that implements the same function from multiple existing software systems, integrates these contents into the prompt words of the large model, and prompts the large model to generate in a reused manner. In this process, whether the search results are comprehensive and accurate will have an important impact on the generation effect.

[0003] In order to improve the effect of system-level code generation, the mainstream idea of ​​existing work is RAG, which searches for relevant information from external knowledge bases and uses this information to guide the generation of answers. This can alleviate the problems faced by large models in practical applications to a certain extent, such as lack of knowledge in specific fields and "hallucinations". Specifically, in the code generation task, some works use vector databases as knowledge bases and search based on vector similarity; some latest works use knowledge graphs to search for structured relational information, usually constructing software codes into knowledge graphs, representing classes, methods, etc. as nodes, and representing inheritance, calling and other relationships between these code elements as relationships on the graph, so as to clearly reflect the structure of the software system. The search methods based on knowledge graphs are mostly concerned with downstream tasks such as code completion and defect repair. For the functional requirements input by users, such methods usually first locate some nodes on the knowledge graph based on the similarity of words or vectors, and then continue to reason on the graph according to some rules or algorithms, and finally integrate the obtained content into the prompt words of the large model to complete the downstream tasks.

[0004] However, since the downstream tasks focused on by the above methods are more focused on searching for internal code in a software system, they lack consideration of reusable code in other software systems in the same field. When developing software systems in specific fields, the implementation of a function may require a combination of multiple code snippets, requiring the definition of data classes, writing multiple methods, etc. The required code is not necessarily semantically similar to the functional requirements proposed by the user. Existing methods cannot 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". At the same time, existing methods do not distinguish 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 code for the software system, it is necessary to invent a method for searching the software knowledge graph. 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: 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 ; From the initial node set Filter seed nodes to get 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 ; Comprehensive seed node set and the expanded node collection , get the node set of the subgraph ; Traverse the node set After building system-level code generation prompts based on the system-level development requirements for each node in the system-level development requirements, system-level code is generated based on the large model.

[0009] 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; 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 node of the JavaMethod type include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, params attribute, returnType attribute, content attribute and description attribute; The attributes of the node of the JavaField type 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.

[0010] Furthermore, 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 a 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 .

[0011] Further, the initial node set Filter seed nodes to get a set of seed nodes ,include: Generate a task description, wherein the task description 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; is the initial node set Each node number in and combines the fullName attribute and the description attribute to generate 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 a seed node set .

[0012] 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: 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 nodes, 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 calling the method The number of methods for the method Filter and get the method ; Get the method The corresponding node; The second extension rule includes: obtaining the method calling Methods The corresponding node; The third extended rule includes: obtaining the method Corresponding parameter types The corresponding node; The fourth extended rule includes: obtaining the method The 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, and the return value type is obtained. ; Get the return value type The corresponding node; The fifth extended rule includes: obtaining the method Belongs to the category ; By judging the class Whether it is 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 the method In this 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 Methods ; Acquisition method , interface implementation class ,method Corresponding nodes and / or methods , Interface class ,method The corresponding node; The first extended 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.

[0013] Furthermore, the method Filter and get the method ,include: Statistics by method The number of method call relationships with any JavaMethod node as the tail node and any JavaMethod node as the head 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 .

[0014] 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: For a first extended node obtained according to the first extended rule, the first extended node is extended based on the first extended rule, the second extended rule, the third extended rule, the fourth extended rule and the fifth extended rule to generate a second extended node; wherein the number of times the first extended 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, for this member variable Filter and filter the member variables obtained The corresponding node is added to the second extended node set.

[0015] Furthermore, the member variable Screening is performed, including: According to the task description and the member variables The name attribute and type attribute of the member variable are used to construct prompt words for member variable filtering. The prompt words for member variable screening are input into the large model to obtain member variables .

[0016] A software knowledge graph search system for system-level code generation, the system comprising: 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 an initial node set. ; Seed node generation module, used to generate Filter seed nodes to get 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 ; 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 system-level code generation prompts based on the system-level development requirements for each node in the system-level development requirements, system-level code is generated based on the large model.

[0017] An electronic device, comprising: a processor and a memory storing computer program instructions; when the processor executes the computer program instructions, the software knowledge graph search method for system-level code generation described in any one of the above items is implemented.

[0018] 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.

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

[0020] By taking the functional requirements of the software system level as input, users can search and reason in the software knowledge graph constructed by multiple software systems in the same field, and display the nodes and relationships in the graph that match the functional requirements in the form of a subgraph. At the same time, users can make corrections on the subgraph and delete unnecessary nodes. The corrected subgraph content will be used to construct prompt words so that the large model can generate code to achieve system-level functional requirements. BRIEF DESCRIPTION OF THE DRAWINGS

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

[0022] Figure 2 It is the metamodel of software knowledge graph.

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

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

[0025] 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

[0026] The present invention is further described in detail below in conjunction with the accompanying drawings. The examples given are only used to explain the present invention but not to limit the scope of the present invention.

[0027] 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 code generation for the big model.

[0028] 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, according to the user's system-level development needs, the initial node set of the knowledge graph is located, and the seed node set is screened therefrom using the large model; then, the expansion rules are set, reasoning is performed on the software knowledge graph, and the 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.

[0029] 1. Preprocessing stage.

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

[0031] 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 the set of 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, denoted as AttributeType, then {AttributeType} = {<Attribute.entity.type, Attribute.name>}.

[0032] Based on the above defined concepts, we traverse all nodes in the software knowledge graph in the neo4j database, record the node type, and record the attribute type of the node type; traverse all relationships in the knowledge graph, obtain the first and last node types of the relationship and the name of the relationship itself, and establish associations between the corresponding node types in the metamodel. Finally, we get a set of node types and a set of relationship types, that is, a metamodel of the software knowledge graph.

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

[0034] There are a large number of nodes in the software knowledge graph. In order to conduct more efficient searches in the future and to search for 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, which 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 functions implemented by the code in Chinese. Considering that the user input in the subsequent search stage is a relatively short development requirement, 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, so the description information description is used to build the index.

[0035] 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, which is a common embedding model in the Chinese field. Here, a model with a small number of parameters is used to improve the efficiency of embedding. After the embedding vector is generated, the vector is stored in ElasticSearch together with the vid attribute (unique identifier) ​​of the node. ElasticSearch is used as the underlying storage and search engine, combined with vector storage and nearest neighbor search (KNN) technology to achieve efficient vector indexing.

[0036] 2. Search phase.

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

[0038] The user inputs the system-level development requirements, which are also converted into embedding vectors using the bge-small-zh-v1.5 model. The vector search function provided by ElasticSearch is used to match the query embedding with all stored embedding vectors and calculate the cosine similarity. 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 contain the vid attribute of the node. The vid attribute is used to query the corresponding graph nodes in the neo4j database, and these nodes are used as the initial node set, recorded as .

[0039] 2.2 Using large models Filter the seed node set .

[0040] 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, so further The nodes in the set are screened, and the most representative node set that best matches the development requirements is selected and recorded as the seed node set. .

[0041] Due to the powerful capabilities of the big model in natural language understanding and code understanding, prompt words are constructed and the big model is used to complete the screening process. The prompt words contain three parts: task description, precautions, and initial node information description. The task description section 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 section emphasizes the restrictions on the format of the output results of the big model, requiring the big model to output a list with the numbers of the selected code snippets to facilitate subsequent parsing of the output. In the initial node information description section, Each node in the collection is numbered and the information of the fullName attribute and the 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.

[0042] 3. Reasoning stage.

[0043] In the software knowledge graph, the seed node set As the starting point of reasoning, the metamodel 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 set of relations is denoted as , then the final subgraph node set is , the relationship set is .

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

[0045] (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. .

[0046] (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 a software system wants to operate on, so The node joins the extended node set, .for , continue to process according to (2.3) rules.

[0047] (1.4) For the return value type relationship ( :JavaMethod)-[ :RETURN_TYPE]->( :JavaClass), It is 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 are Add to the extended node collection .for , continue to process according to (2.3) rules.

[0048] (1.5) For the inclusion method relation ( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), As a class, containing methods ,according to Different situations require different treatments: (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 obtains the method The judgment method is to match the following path in the knowledge graph: ( :JavaMethod)<-[ :HAVE_METHOD]-( :JavaClass)-[ :IMPLEMENT] ->( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), if the match is successful, then the matched path Node represents interface The implementation class of 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), .

[0049] (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: ( :JavaMethod)<-[ :HAVE_METHOD]-( :JavaClass)<-[ :IMPLEMENT]-( :JavaClass)-[ :HAVE_METHOD]->( :JavaMethod), if the match is successful, then Node Representative The implemented interface class, Node Representative Method Definition in the interface class, .and A JavaMethod node is usually called by another method, so The node starts to expand again, that is ( :JavaMethod)-[ :METHOD_CALL]->( :JavaMethod), .

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

[0051] (2) For some nodes expanded from rule (1) and The data class nodes in the need for further iterative expansion. However, 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: (2.1) For nodes expanded according to rule (1.1), they can continue to be expanded according to rule (1.1), but an upper limit on the number of expansions according to rule (1.1) must be set. (The value in this embodiment is 3), so as to prevent too many nodes from being expanded for too deep method calls, resulting in poor readability of search results. For the nodes expanded by rule (1.1), they can be further expanded according to rules (1.3), (1.4) and (1.5).

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

[0053] (2.3) For the nodes expanded according to the rules (1.3) and (1.4) and , and It is a data class whose member variables are important for system-level function development, so 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. Therefore, add a large model screening operation, describe the task in detail in the prompt word, and then and All related 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 an upper limit is set for the number of times rule (2.3) is used. (The value in this embodiment is 2), that is, it can only be A JavaClass node expands into a JavaField node.

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

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

[0056] 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 thinks that some nodes are irrelevant, redundant, and not very helpful for the code generation stage, they can right-click on the interface and delete these nodes one by one. Through this interactive subgraph correction process, users can delete unnecessary information in the search results, so that they can control the contextual information used by the large model when generating system-level code and optimize the effect of code generation.

[0057] 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 requirements and the content of the subgraph. Finally, the large model can generate system-level code.

[0058] In order to verify the effect 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.

[0059] After constructing the software knowledge graph for the four projects, the meta-model of the knowledge graph is first obtained in the preprocessing 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.

[0060] In the search phase, for the input 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, whose fullName and description attributes are shown in Table 1.

[0061] 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 Then use the large model from Filter 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 the attribute information of the node on the right.

[0062] In the inference stage, The nodes in are used as the starting point for reasoning on the knowledge graph. Figure 4 The subgraph obtained by reasoning with seed node 1, com.dimple.system.service.service.impl.SysUserServiceImpl.updateUserProfile (abbreviated as SysUserServiceImpl.updateUserProfile), as the starting point is shown (the reasoning process of seed node 0 is similar). Specifically, it is a node of type JavaMethod. All its associated relationships are obtained. There are two method call relationships (METHOD_CALL) with it as the first node. The tail nodes are updateUser and convert, both of which are nodes of type JavaMethod. According to rule (1.1), it is necessary to determine whether these two methods appear frequently in the software system. The number of all METHOD_CALL relationships with them as tail nodes is obtained respectively. It is found 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 tail 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 tail 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, and the corresponding relationships are also added The rule (1.5.2) also requires that the updateUserProfile node be expanded again, and the updateProfile node is obtained from the METHOD_CALL relationship.

[0063] 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, which 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 according to 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.

[0064] For the two nodes SysUserBO and SysUser obtained by expanding according to rule (1.3), several JavaField nodes can be obtained by expanding 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 to development requirements.

[0065] Finally get Figure 4 After the search results are displayed, users can interactively modify the subgraph and delete unnecessary nodes according to their system-level development requirements. Each node of the modified subgraph represents reusable code in other software systems in the same field, which can help large models complete system-level code generation tasks. Figure 5 A kind of prompt word that can be used is shown, in which the part surrounded by "{}" is replaceable content, for example, "{systemName}" can be replaced with "personal blog system" in this example, and "{query}" can be replaced with "update user information". This prompt word particularly emphasizes that when developing the current software system, we should not only pay attention to the context of the current system, but also refer to and reuse the codeSamples obtained from the search, instead of 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 ; From the initial node set Filter seed nodes to get 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 ; Comprehensive seed node set and the expanded node collection , get the node set of the subgraph ; Traverse the node set For each node in the system, and after building the prompt words for system-level code generation in combination with 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 types of the nodes 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; 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 node of the JavaMethod type include: id attribute, vid attribute, name attribute, fullName attribute, projectName attribute, params attribute, returnType attribute, content attribute and description attribute; The attributes of the node of the JavaField type 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 a 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 get a set of seed nodes ,include: Generate a task description, wherein the task description 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; is the initial node set Each node number in and combines the fullName attribute and the description attribute to generate 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 a seed node set .

5. The method according to claim 2, characterized in that: 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 nodes, 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 calling the method The number of methods for the method Filter and get the method ; Get the method The corresponding node; The second extension rule includes: obtaining the method calling Methods The corresponding node; The third extended rule includes: obtaining the method Corresponding parameter types The corresponding node; The fourth extended rule includes: obtaining the method The 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, and the return value type is obtained. ; Get the return value type The corresponding node; The fifth extended rule includes: obtaining the method Belongs to the category ; By judging the class Whether it is an interface class or an interface implementation class for the class Filter and get the class In the category In the case of an interface class, get the interface implementation class of the interface class And the method In this 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 Methods ; Acquisition method , interface implementation class ,method Corresponding nodes and / or methods , Interface class ,method The corresponding node; The first extended 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.

6. The method according to claim 5, characterized in that The method Filter and get the method ,include: Statistics by method The number of method call relationships with any JavaMethod node as the tail node and any JavaMethod node as the head 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 .

7. The method according to claim 5, 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 extended node obtained according to the first extended rule, the first extended node is extended based on the first extended rule, the second extended rule, the third extended rule, the fourth extended rule and the fifth extended rule to generate a second extended node; wherein the number of times the first extended 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, for this member variable Filter and filter the member variables obtained The corresponding node is added to the second extended node set.

8. The method according to claim 7, characterized in that The member variable Screening is performed, including: According to the task description and the member variables The name attribute and type attribute of the member variable are used to construct prompt words for member variable filtering. The prompt words for member variable screening are input into the large model to obtain member variables .

9. A software knowledge graph search system for system-level code generation, characterized in that: 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 an initial node set. ; Seed node generation module, used to generate Filter seed nodes to get 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 ; 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 For each node in the system, and after building the prompt words for system-level code generation in combination with the system-level development requirements, the system-level code is generated based on the large model.

10. 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, the software knowledge graph search method for system-level code generation as described in any one of claims 1-8 is implemented.

Citation Information

Patent Citations

  • Software code knowledge graph construction method and tool

    CN114035783A

  • Intelligent auxiliary method and platform for software development

    CN117369786A

  • Hardware security vulnerability knowledge graph assisted Verilog security code generation method and device

    CN119558238A

  • Warehouse level code problem solving system based on large language model

    CN119576384A

  • Knowledge-enriched item set expansion system and method

    US11734365B1