Code knowledge graph generation, code generation methods, devices, equipment and media

By constructing a code knowledge graph, the problems of illusion and insufficient context in the intelligent understanding and generation of code in large language models are solved, achieving more efficient knowledge graph construction and more accurate information response.

CN119721222BActive Publication Date: 2025-10-31BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 3 Cites 0 Cited by

Patent Information

Application Number
CN202411775261.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-04
Publication Date
2025-10-31
Estimated Expiration
2044-12-04

AI Technical Summary

Technical Problem

Existing technologies in the field of intelligent code understanding and generation suffer from problems such as illusion and insufficient contextual information, making it difficult to efficiently evaluate the generation quality and performance of large language models.

Method used

By constructing a code knowledge graph, we can obtain the code tree and generate graph nodes and edge information. Combined with descriptive information, we can form a complete and rich knowledge graph, which can be used to assist in code understanding and generation during human-computer interaction in large language models.

Benefits of technology

It increases the completeness of the knowledge graph, reduces storage and data processing volume, improves the efficiency of knowledge graph construction, and enhances the reliability and accuracy of response information.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119721222B_ABST
    Figure CN119721222B_ABST
Patent Text Reader

Abstract

This disclosure provides a code knowledge graph generation method, apparatus, device, and medium, relating to the field of data processing, specifically to intelligent search, human-computer interaction, artificial intelligence, and large-scale model technology. The specific implementation scheme includes: obtaining a code tree, the code tree including first code elements and the structural relationships between the first code elements; wherein the code tree is generated by parsing source code; generating first graph node information based on the first code elements; generating first graph edge information between corresponding graph nodes based on the structural relationships; and generating a code knowledge graph based on the first graph node information and the first graph edge information. This disclosure can increase the completeness of the knowledge graph, reduce the storage and data processing volume of the knowledge graph, and improve the efficiency of knowledge graph construction.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This disclosure relates to the field of data processing, specifically to the fields of intelligent search, human-computer interaction, artificial intelligence and large model technology, and in particular to a code knowledge graph generation, code generation method, device, equipment and medium. Background Technology

[0002] Large Language Models (LLMs) can generate code.

[0003] With the rapid development of multimodal large models, the key issue at present is to efficiently and automatically evaluate the generation quality and performance of these models in practical applications.

[0004] In the field of intelligent code understanding and generation, Retrieval-Augmented Generation (RAG)-based reinforcement of Large Language Models (LLM) can enrich the context depth by using vector semantic retrieval and keyword full-text retrieval, while ensuring the timeliness and accuracy of relevant knowledge. This can alleviate problems such as illusion and insufficient contextual information to some extent. Summary of the Invention

[0005] This disclosure provides a code knowledge graph generation method, apparatus, device, and medium.

[0006] According to one aspect of this disclosure, a method for generating a code knowledge graph is provided, comprising:

[0007] Obtain a code tree, the code tree including first code elements and the structural relationships between the first code elements; wherein, the code tree is generated by parsing the source code.

[0008] Generate the first graph node information based on the first code element;

[0009] Based on the structural relationship, the first graph edge information between the corresponding graph nodes is generated;

[0010] A code knowledge graph is generated based on the information of each node in the first graph and the edge information of the first graph.

[0011] According to one aspect of this disclosure, a code generation method is provided, comprising:

[0012] Obtain the input question information;

[0013] Based on the problem information, a search is performed in the code knowledge graph to obtain the target code knowledge; the code knowledge graph is generated by the code knowledge graph generation method provided in any embodiment of this disclosure.

[0014] Based on the target code knowledge and the question information, a response message is generated.

[0015] According to one aspect of this disclosure, a code knowledge graph generation apparatus is provided, comprising:

[0016] A code tree acquisition module is used to acquire a code tree, which includes a first code element and the structural relationships between the first code elements; wherein, the code tree is generated by parsing the source code.

[0017] The graph node generation module is used to generate first graph node information based on the first code element;

[0018] The graph edge generation module is used to generate the first graph edge information between corresponding graph nodes based on the structural relationship.

[0019] The knowledge graph construction module is used to generate a code knowledge graph based on the information of each node in the first graph and the edge information of the first graph.

[0020] According to one aspect of this disclosure, a code generation apparatus is provided, comprising:

[0021] The problem acquisition module is used to acquire the input problem information;

[0022] The knowledge retrieval module is used to retrieve target code knowledge from the code knowledge graph based on the question information; the code knowledge graph is generated by the code knowledge graph generation method provided in any embodiment of this disclosure.

[0023] The response generation module is used to generate response information based on the target code knowledge and the question information.

[0024] According to another aspect of this disclosure, an electronic device is provided, comprising:

[0025] At least one processor; and

[0026] A memory communicatively connected to the at least one processor; wherein,

[0027] The memory stores instructions that can be executed by the at least one processor, which, when executed by the at least one processor, enables the at least one processor to perform the code knowledge graph generation method or code generation method according to any embodiment of this disclosure.

[0028] According to another aspect of this disclosure, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to cause the computer to execute the code knowledge graph generation method or code generation method described in any embodiment of this disclosure.

[0029] According to another aspect of this disclosure, a computer program product is provided, including a computer program that, when executed by a processor, implements the code knowledge graph generation method or code generation method described in any embodiment of this disclosure.

[0030] The embodiments disclosed herein can increase the completeness of knowledge graphs, reduce the storage and data processing volume of knowledge graphs, and improve the efficiency of knowledge graph construction.

[0031] It should be understood that the description in this section is not intended to identify key or essential features of the embodiments of this disclosure, nor is it intended to limit the scope of this disclosure. Other features of this disclosure will become readily apparent from the following description. Attached Figure Description

[0032] The accompanying drawings are provided to better understand this solution and do not constitute a limitation of this disclosure. Wherein:

[0033] Figure 1 This is a flowchart of a code knowledge graph generation method disclosed in an embodiment of this disclosure;

[0034] Figure 2 This is a schematic diagram of a code knowledge graph construction scenario disclosed in an embodiment of this disclosure;

[0035] Figure 3 This is a schematic diagram of a code knowledge graph disclosed according to an embodiment of the present disclosure;

[0036] Figure 4 This is a flowchart of a code generation method disclosed in an embodiment of the present disclosure;

[0037] Figure 5 This is a flowchart of another code generation method disclosed according to an embodiment of the present disclosure;

[0038] Figure 6 This is a scenario diagram of a code knowledge search method disclosed in an embodiment of this disclosure;

[0039] Figure 7 This is a scene diagram of a code generation method disclosed in an embodiment of this disclosure;

[0040] Figure 8 This is a schematic diagram of the structure of the code knowledge graph generation apparatus disclosed in the embodiments of this disclosure;

[0041] Figure 9This is a schematic diagram of the structure of the code generation apparatus disclosed in the embodiments of this disclosure;

[0042] Figure 10 This is a block diagram of an electronic device based on the code knowledge graph generation method disclosed in embodiments of this disclosure. Detailed Implementation

[0043] The exemplary embodiments of this disclosure are described below with reference to the accompanying drawings, including various details of the embodiments to aid understanding, and should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of this disclosure. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.

[0044] Figure 1 This is a flowchart of a code knowledge graph generation method disclosed in an embodiment of this disclosure. This embodiment can be applied to the construction of knowledge graphs. The method of this embodiment can be executed by a code knowledge graph generation device, which can be implemented in software and / or hardware, and specifically configured in an electronic device with certain data processing capabilities, such as a server device.

[0045] S101. Obtain a code tree, the code tree including first code elements and structural relationships between the first code elements; wherein, the code tree is generated by parsing the source code.

[0046] In this context, a code tree can refer to a tree describing the structural relationships between first code elements. A first code element can be the basic unit in the source code that constitutes the code structure and behavior. The structural relationships between first code elements can refer to the interactions and connections between them. Nodes in the tree are first code elements, and the relationships between nodes constitute the structural relationships between the first code elements. In some embodiments, a first code element includes at least one of the following: file, class, parent class, interface, method, field, parameter, return type, call, and type, etc. Structural relationships can include: dependency relationships, inclusion relationships, and descriptive relationships, etc. A code tree can parse the source code to obtain the code elements included in the source code, as well as the structural relationships between the code elements, and use these as the first code elements and the structural relationships between them.

[0047] It should be noted that the code tree is used to form a code knowledge graph. The source code is parsed in a coarse-grained manner to build the code tree, and then the code knowledge graph is generated based on the built code tree. In this way, the code tree serves as intermediate data for forming the code knowledge graph, which can reduce the amount of data computation at the front end.

[0048] Furthermore, the source code can be uploaded by the user to the current client, or the current client can obtain authorized open-source source code and build a code tree based on the obtained source code. Alternatively, the source code can be the client's local code. The client can process the source code, generate a code tree, and send the code tree to the current client, which will then generate a code knowledge graph. This avoids the current client directly obtaining the user's local code, thus improving the security of local code.

[0049] S102. Generate the first map node information based on the first code element.

[0050] The first graph node information may include the first graph node itself and its associated information. A first code element is used as a first graph node, and information extractable from the source code of the first code element is used as the associated information of the first graph node. The first graph node information and its associated information are then combined to generate the first graph node information. In some embodiments, the associated information may include attribute information of the first code element and its position in the source code.

[0051] S103. Based on the structural relationship, generate the first graph edge information between the corresponding graph nodes.

[0052] The first graph edge information may include the first graph node connected by the first graph edge, the direction of the first graph edge, and the relationship represented by the first graph edge. A first graph edge is created for the first graph nodes corresponding to first code elements with a structural relationship to connect the corresponding first graph nodes. The first graph edge indicates that there is a structural relationship between the first code elements represented by the first graph nodes connected by the first graph edge. The first graph edge can be a directed line. For example, the direction of the first graph edge between the first graph nodes corresponding to two first code elements with a dependency relationship is from the dependent first graph node to the other first graph node. Similarly, the direction of the first graph edge between the first graph nodes corresponding to two first code elements with an inclusion relationship is from the larger first graph node to the smaller (included) first graph node. Furthermore, the direction of the first graph edge between the first graph nodes corresponding to two first code elements with a hierarchical relationship is from the upper-level first graph node to the lower-level first graph node.

[0053] S104. Generate a code knowledge graph based on the information of each of the first graph nodes and the information of the first graph edges.

[0054] The code knowledge graph consists of first graph nodes and first graph edges representing relationships between two first graph nodes. Based on the first graph node information, a first graph node is created. Based on the first graph edge information, two connected first graph nodes are found, and a first graph edge is created between these two connected nodes. The direction of this first graph edge is determined based on the first graph node information and / or the first graph edge information. By directly extracting first code elements and structural relationships from the source code, unprocessed code information and continuous relationship information can be obtained. This avoids data loss during code processing, thus preventing information gaps in the code knowledge graph. Furthermore, extracting first code elements and structural relationships from the source code and removing redundant comments and other information can compress code content and reduce redundant data.

[0055] Furthermore, the source code also includes the first code element of other coding scopes outside the current coding scope and the structural relationships between them and the first code element of the current coding scope. For example, if a method in one source code file calls a method in another source code file, the code tree corresponding to the former source code file includes the method in the latter source code file and the relationship between the two methods. Thus, the code tree built based on the source code includes the first code element of other coding scopes. This allows the constructed code knowledge graph to complete global information, making the code tree possess complete and rich code information, thereby enriching the information in the code knowledge graph.

[0056] In this embodiment of the disclosure, the code knowledge graph is used to retrieve code knowledge based on question information during human-computer interaction with a large language model, thereby assisting the large language model in understanding the question information and generating the response information required by the user.

[0057] According to the technical solution disclosed herein, by parsing the source code, extracting the code elements and structural relationships, generating a code tree based on the extracted content, and generating a code knowledge graph based on the code tree, the processing of code elements and structural relationships can be reduced, the loss of effective data in the code knowledge graph can be reduced, redundant information in the source code can be deleted, the code knowledge graph can be simplified, the storage and data processing volume of the knowledge graph can be reduced, the construction efficiency of the knowledge graph can be improved, and code elements from other scopes involved in the source code can be obtained, adding global information, thereby increasing the completeness of the knowledge graph.

[0058] In an optional embodiment, the code knowledge graph generation method further includes: obtaining descriptive information associated with the code tree; and adding the descriptive information to the code knowledge graph.

[0059] The descriptive information can refer to the functional or business descriptions of the source code. Adding descriptive information to the code knowledge graph increases the natural language content within it. For example, the descriptive information may include at least one of the following: source code commit information, merge information, and tag information. The descriptive information can be textual information and comments related to the source code. As a representation of the natural language content of the source code, the descriptive information can aid in understanding the first graph nodes and edges in the code knowledge graph.

[0060] Description information of the source code used to generate the code tree can be obtained. This description information may include at least one first code element and / or at least one sub-information of structural relationships. Based on the first code element and / or structural relationship associated with the sub-information in the description information, the sub-information is added to the corresponding position in the corresponding code knowledge graph. In some embodiments, description information of the source code can also be obtained, and a code tree can be generated based on the description information and the source code, with the sub-information in the description information serving as code elements of the code tree. Thus, the code knowledge graph generated based on the code tree contains description information.

[0061] By adding descriptive information associated with the code tree to the code knowledge graph, business service information in the code knowledge graph can be increased, making it more in line with users' natural language and improving the convenience of retrieval.

[0062] In an optional embodiment, adding the description information to the code knowledge graph includes: generating second graph node information based on the description information; adding the second graph node information to the code knowledge graph; obtaining the second code element corresponding to the description information; generating second graph edge information between the second graph node information and the corresponding first graph node in the code knowledge graph; and adding the second graph edge information to the code knowledge graph.

[0063] The second graph node information may include the second graph node itself and its associated information. The second graph edge information may include the second graph node and the first graph node connected by the second graph edge, the direction of the second graph edge, and the relationship represented by the second graph edge. The second graph edge is configured such that the connected first graph node points to the connected second graph node. The relationship represented by the second graph edge describes the relationship of business information. The description information is used as a second graph node, and a second graph edge is established between the description information and the first graph node corresponding to the described object. The description information includes at least one sub-information. Each sub-information generates a second graph node. The first code element described by the sub-information is obtained, and a second graph edge is created between the second graph node generated by the sub-information and the first graph node generated by the described first code element. The direction of the second graph edge is such that the first graph node points to the second graph node.

[0064] By incorporating descriptive information as code elements and nodes in the code knowledge graph, the importance of descriptive information within the knowledge graph is increased, thereby improving the ease of retrieval of descriptive information.

[0065] In an optional embodiment, the code tree is generated by parsing the source code of a specified target range, which includes source files, code libraries, or directories.

[0066] The source code can be provided by the user. The source code can be the source code in a single source file, the source code in a code library, or the source code stored in a specific directory.

[0067] In some embodiments, a code development application is installed locally on the client. This application is configured with a target plugin. The user specifies the target scope through the client's target plugin. The client's target plugin processes the source code within the target scope, generates a code tree, and sends the code tree to the server. The server generates a code knowledge graph based on the code tree. The target scope can be local source files, local code repositories, or local directories.

[0068] In some embodiments, the user specifies a target scope via a web interface. The web interface processes the source code of the target scope, generates a code tree, and sends the code tree to the server. The server generates a code knowledge graph based on the code tree. The target scope can be web source files, web code repositories, or web unified resource location addresses (directories), etc.

[0069] Furthermore, the client can generate a corresponding code tree for each source file. When the user specifies multiple source files, the client can send the code trees corresponding to each source file to the current electronic device. The code tree is a file-level tree. A code repository can include multiple source files, or a directory can store multiple source files. The current electronic device generates a code knowledge graph based on these multiple code trees.

[0070] By configuring the code tree to generate source code based on a user-specified target scope, the flexibility and diversity of the code knowledge graph can be improved.

[0071] In some embodiments, Figure 2 This paper presents a scenario diagram for a code knowledge graph generation method. By analyzing tens of thousands of real user request scenarios and abstracting the structure of the source code, the generated code knowledge graph Scheme is as follows: Figure 3As shown. In the code knowledge graph construction phase, the code tree is built by leveraging the syntax analysis capabilities of the client-side IDE (Integrated Development Environment). This phase requires first defining the knowledge graph's scheme (the points and edges in the graph), and can build a codebase-level knowledge graph based on traditional vector indexes.

[0072] Unlike the traditional process of segmenting chunks for fragmented analysis, a codebase is naturally one or more "graphs," but it is not limited to a single file, class, or method. This requires the ability to parse the entire codebase in order to build a knowledge graph at the codebase level.

[0073] On the IDE side, i.e., the client, there is full syntax parsing capability, but it is heavily dependent on the performance and version of the user's terminal. High-load parsing affects the user experience. Therefore, the IDE performs initial parsing, obtaining a GraphTree as an intermediate representation for each source file, focusing on parsing dependencies such as symbolic references that the server cannot handle well. Figure 2 As shown, the tree is a code tree. In the tree, the next level of a file is a class, the next level of a class includes the parent class, interface, method and field, the next level of a method includes parameters, return type and call, and the next level of a field is the type, etc.

[0074] The server-side replaces the GraphTree with a graph-acceptable node and edge format, and further handles more complex deep relationships, such as generic dependencies, inner classes, and Git diffs (differences between versions). The resulting code knowledge graph is as follows: Figure 2 In the code tree, class A points to class E, class F, method A, and fields. Class A can implement class E, and class A extends class F. Class A includes method A (HAS_method) and fields (HAS_field). In the code tree, solid arrows represent entity hierarchy relationships, and dashed arrows represent attribute pointer relationships.

[0075] In an example of a code knowledge graph, such as Figure 3As shown, a code repository (repo) points to a module, which in turn points to a file, which in turn points to class A. Furthermore, other files in this code tree also point to class A; for example, class J points to a file that points to class A. Class A points to a different file, which points to class B. In fact, the code tree is generated based on a source file, but the classes included in that source file can have structural relationships with classes in other files, which are then reflected in the code tree. This allows for the completion of structural relationships between more other files within the code tree of a single source file. Class A points to class E, class F, and method A. Class F points to method B. Method A points to method B, method C, method D, class C, class D, and local variables. Method C points to method E, and class I points to method E. Class G points to method C. Class D points to method D, and method D points to the git message. The local variable points to class H.

[0076] In this embodiment, most of the graph nodes in the code knowledge graph constructed based on the code tree correspond to entities in the source code. This intuitive conversion facilitates the conversion of code-related natural language queries to graph queries. Git messages are obtained from the source files and graph nodes are generated, preserving the relationship between documents and code. This document portion is closer to the user's natural language. The code tree retains as many direct and deep dependencies as possible within the source code. The code tree can be calculated using OLTP (On-Line Transaction Processing) and OLAP (On-Line Analytical Processing) methods of the graph database. The code tree can be generated locally, and the code knowledge graph can be generated on the server side. This reduces the need for the server to directly obtain source code to generate the graph, enabling online generation of the code knowledge graph and reducing the load on the server side for graph construction and updates.

[0077] Figure 4 This is a flowchart of a code generation method disclosed in an embodiment of this disclosure. This embodiment can be applied to human-computer interaction based on a knowledge graph constructed in the foregoing embodiments. The method of this embodiment can be executed by a code generation device, which can be implemented in software and / or hardware and specifically configured in an electronic device with certain data processing capabilities, such as a server device.

[0078] S401. Obtain the input question information.

[0079] In human-computer interaction scenarios, users provide question information, and the current electronic device processes this information to obtain a response. The question information is in natural language, and there are no restrictions on the language or media type of the question information.

[0080] S402. Based on the problem information, a search is performed in the code knowledge graph to obtain the target code knowledge; the code knowledge graph is generated by the code knowledge graph generation method of any embodiment of this disclosure.

[0081] The code knowledge graph provides target code knowledge. Based on the problem information, relevant knowledge is retrieved from the code knowledge graph to obtain the code knowledge. This code knowledge assists the current electronic device in processing the problem information. Target code knowledge may include graph nodes, graph edges, and the structural relationships between the aforementioned elements in the code knowledge graph. Graph nodes may include the first and second graph nodes from the aforementioned embodiments, and graph edges may include the first and second graph edges from the aforementioned embodiments.

[0082] S403. Generate response information based on the target code knowledge and the problem information.

[0083] In some embodiments, target code knowledge and problem information are fused and input into a large language model for processing, outputting response information. Specifically, target code knowledge and problem information can be added to a prompt template, and the added prompt template is input into a large language model for processing, outputting response information.

[0084] In some embodiments, the application scenario of human-computer interaction is to generate code based on question information. The response information is code.

[0085] According to the technical solution disclosed herein, by retrieving target code knowledge related to the problem information in the constructed code knowledge graph, and processing the problem information in combination with the target code knowledge, the response information can be obtained. Code knowledge can be retrieved in the code knowledge graph that retains the original code elements and removes redundant information, so that the limited code knowledge includes richer key information, which helps to understand the overall code architecture and improves the reliability of the response information.

[0086] In an optional embodiment, generating response information based on the target code knowledge and the question information includes: sorting the search results included in the target code knowledge according to the question information to obtain a sorting result; and generating the response information based on the sorting result and the question information.

[0087] The target code knowledge includes at least one search result. These results can be sorted according to their relevance to the question information, resulting in a ranking. In the directly retrieved target code knowledge, the search results are arranged in random order; the ranking result refers to the sorted code knowledge. In fact, the order of the search results in the code knowledge before sorting can be the desired order, meaning the order of the search results in the re-sorted code knowledge can be the same as the order of the search results in the code knowledge before sorting. The ranking result actually includes the relevance between each search result and the question information. Generating response information based on the ranking result and the question information is equivalent to generating response information based on the target code knowledge, the relevance between the search results in the target code knowledge and the question information, and the question information itself.

[0088] In some cases, the relevance between search results and question information can be determined based on the search method used and the confidence level of the search results under that method. Alternatively, the similarity between search results and question information can be directly calculated as a measure of their relevance.

[0089] In some embodiments, the prompt template includes a question slot and a code knowledge slot. Question information is added to the question slot, and the ranking results are added to the code knowledge slot. The prompt template with the added question information and ranking results is input into the large language model, and the response information is output. The search results in the prompt template are added in the order they are listed.

[0090] By sorting the search results in the code knowledge according to the question information, the sorted code knowledge is obtained. This sorted code knowledge contains the order of relationships with the question information. Based on the sorted code knowledge and the question information, response information is generated. This can help the device use more relevant code knowledge to respond to the question information and obtain the response information.

[0091] In an optional embodiment, the code generation method further includes: obtaining a code tree generated based on specified local source code; and obtaining the code knowledge graph based on the code tree.

[0092] This system allows for the simultaneous acquisition of input question information and a code tree generated based on specified local source code. In other words, the user can input both the question information and source code within a specified target scope. In some embodiments, the client or network terminal generates a code tree based on the target scope source code and sends both the question information and the code tree to the current electronic device. The current electronic device receives the question information and the code tree generated from the target scope source code. The current electronic device generates a code knowledge graph based on the code tree and retrieves code knowledge from the generated code knowledge graph based on the question information. Finally, based on the question information and the code knowledge, a response is generated.

[0093] By configuring the code knowledge graph to be generated based on the source code within a user-specified target scope, the adaptability of the code knowledge graph to the question information can be improved, the representativeness of the code knowledge can be increased, and the relevance between the code knowledge graph and the question information can be increased, thereby improving the accuracy of the response.

[0094] Figure 5 This is a flowchart of another code generation method disclosed in this embodiment, which is further optimized and extended based on the above technical solution and can be combined with the above optional implementation methods. The step of retrieving code knowledge in the code knowledge graph based on the question information is specifically implemented as follows: intent recognition is performed on the question information to obtain the question intent; the retrieval type is determined based on the question intent; retrieval input information corresponding to the retrieval type is obtained; and the code knowledge is retrieved based on the retrieval input information in the code knowledge graph.

[0095] S501. Obtain the input question information.

[0096] S502. Perform intent recognition on the question information to obtain the question intent.

[0097] This involves semantic understanding of the question information to determine the question intent. In some embodiments, a large language model can be used to identify the intent of the question information. The question information is then fused with a prompt template provided by the intent recognition function and input into the large language model to obtain the question intent.

[0098] S503. Determine the search type based on the stated question intent.

[0099] The search type is used to determine the search method, and it can also include the data source to be searched. In essence, the question intent contains the knowledge needed to answer the question, the data source where the required knowledge is located, and the search method to obtain that knowledge. Based on the question intent, the data source to be searched and the search method can be obtained, and based on the data source and search method, the search type can be determined. Intent recognition helps determine how the question information needs to be searched to obtain sufficient context.

[0100] In some embodiments, the retrieval type may include: query statement retrieval, extended retrieval, or other retrieval. For example, when the question information or question intent is a precise retrieval such as query graph node retrieval, complex relationship retrieval, subgraph retrieval, or visualization retrieval, the retrieval type can be determined as query statement retrieval. Similarly, when the question information or question intent is a retrieval of descriptive content related to business services or functions, where natural language content outweighs programming language content, the retrieval type can be determined as extended retrieval. Other types can be supplementary to the retrieval types of the code knowledge graph. Other types may include vector retrieval, where graph nodes and edges in the code knowledge graph are encoded into vectors, and vector similarity is calculated in the code knowledge graph based on the vector representation corresponding to the question information and / or question intent to query related graph nodes and / or graph edges. Furthermore, other types may also include retrieval types from data sources outside the code knowledge graph.

[0101] S504. Obtain the search input information corresponding to the search type.

[0102] The retrieval input information is used to search for information within the code knowledge graph. The retrieval input information is generated according to the method corresponding to the retrieval type.

[0103] S505. Based on the retrieval input information, a retrieval is performed in the code knowledge graph to obtain the target code knowledge; the code knowledge graph is generated by the code knowledge graph generation method of any embodiment of this disclosure.

[0104] In some embodiments, the retrieval type is a query statement retrieval, and the corresponding retrieval input information is at least one query statement. Each query statement is executed, and at least one retrieval result is obtained in the code knowledge graph, generating target code knowledge. As another example, the retrieval type is an extended retrieval, and the corresponding retrieval input information is a keyword. The code knowledge graph queries for the corresponding graph node and / or graph edge in the keyword. As yet another example, the retrieval type is other retrieval, and the corresponding retrieval input information is a vector. The code knowledge graph queries for the corresponding graph node and / or graph edge in the vector; or the retrieval input information is a vector, and the code element's vector library queries for similar vectors to obtain the code element and associated information corresponding to the similar vector.

[0105] S506. Generate response information based on the target code knowledge and the problem information.

[0106] According to the technical solution of this disclosure, by identifying the intent of the question information and determining the retrieval type and corresponding retrieval input information based on the question intent, the target code knowledge is obtained by searching in the code knowledge graph based on the retrieval input information. The retrieval type required to reply to the question information can be parsed, the knowledge missing in replying to the question information can be determined, and the retrieval method for obtaining the knowledge missing in replying to the question information can be planned. Finally, the target code knowledge is obtained, realizing the autonomous retrieval and judgment of the code knowledge reply task. Based on the tool of the code knowledge graph, code knowledge retrieval is carried out, increasing the feasibility judgment of the retrieval and improving the retrieval accuracy, thereby improving the accuracy of the reply information.

[0107] In an optional embodiment, the retrieval type includes: query statement retrieval; obtaining the retrieval input information corresponding to the retrieval type includes: generating a query statement based on the question information; wherein, the retrieval input information includes the query statement.

[0108] The code knowledge graph is essentially a graph database, and queries can be performed using graph database query statements. Query statements are used to search within the code knowledge graph. Query statements can be graph database query statements. At least one query statement can be used.

[0109] In some embodiments, Text2Gremlin can be used to transform the question information into a query statement in the graph query language Gremlin. Alternatively, the graph query language could be Cypher. In some embodiments, the question information can be added to a prompt template used to generate the query statement, and the resulting prompt template can be input into a large language model for processing to output the query statement.

[0110] By generating query statements corresponding to the question information and using them as input information for query statement retrieval, the query scenarios of the knowledge graph can be expanded, enabling rapid and accurate location of code knowledge in the code knowledge graph.

[0111] In an optional embodiment, the search type includes: extended search; obtaining the search input information corresponding to the search type includes: determining key information based on the question information and the question intent; wherein the search input information includes the key information.

[0112] Extended retrieval can refer to a type of retrieval based on the attributes of nodes in a code knowledge graph. In some embodiments, key information is matched with the attribute information of nodes in the code knowledge graph. In some embodiments, keywords are extracted from question information and question intent respectively, and then fused to obtain key information. For example, the question information can be rewritten based on the question information and question intent to generate a series of related questions, and keywords can be extracted from each question. Extracting keywords from questions can involve word segmentation of the question, and supplementing, expanding, and modifying the word segmentation according to the question intent, resulting in a large number of keywords. The large number of generated keywords can also be filtered based on the question information and question intent to obtain key information. Extended retrieval provides both key information and full-text search functionality.

[0113] By obtaining key information from the question and using it as input for the extended search type, the search scope can be broadened and the content of the search results enriched. Furthermore, by searching based on the attributes of precise graph nodes in the code knowledge graph, redundant search results can be reduced and search accuracy improved.

[0114] In an optional embodiment, the step of retrieving the target code knowledge from the code knowledge graph based on the retrieval input information includes: retrieving at least two target nodes from the code knowledge graph based on the retrieval input information; and generating a subgraph based on the target nodes and the relationships between them; wherein the target code knowledge includes the target nodes and the subgraph.

[0115] In this context, target nodes are graph nodes retrieved based on the input information. Associations refer to the connections between target nodes, which can be structural relationships between the code elements represented by the target nodes. A subgraph consists of retrieved target nodes and the connections between them. The subgraph and target nodes are considered as target code knowledge.

[0116] In some embodiments, a subgraph is generated based on at least two target nodes and the edges that exist between these target nodes. In some embodiments, the target nodes can be expanded in the code knowledge graph to obtain more graph nodes, and the expanded graph nodes and these target nodes are used together to generate a subgraph.

[0117] In some embodiments, if there is a connectable path between at least two target nodes, the graph nodes traversed by the connectable path between the two target nodes are obtained. A subgraph is generated by combining the target nodes and all graph nodes traversed by the obtained connectable path. Furthermore, the direction of the connectable path can be defined; based on the direction of the directed edges of the target nodes, graph nodes traversed in the direction of the specified connectable path are obtained.

[0118] In one example, target node A and target node D are retrieved. Target node A can reach target node D by passing through graph nodes B and C in sequence. A subgraph can be generated from target node A, graph nodes B, graph nodes C, and target node D.

[0119] In one example, the outgoing edges from target node A lead to target node D via graph nodes B and C. The incoming edges from target node A lead to graph node E, and the outgoing edges from graph node E lead to target node F. A subgraph can be generated using only target node A, graph nodes B, C, and D, while graph node E on the incoming edge path is not included in the subgraph generation. Here, incoming edges are graph edges pointing to graph nodes, and outgoing edges are graph edges that graph nodes point to other graph nodes.

[0120] In some embodiments, for each target node, a preset number of graph nodes along the connected path in a specified direction of the target node can be obtained and added to a subgraph.

[0121] By generating subgraphs from the target nodes and existing relationships retrieved from the code knowledge graph, we can enrich the deeper relationships between code elements in the search results, increase the code structure relationships in the target code knowledge, and fill in missing paths in the search results. This allows us to provide responses based on the target code knowledge. In scenarios where large language models are used for human-computer interaction, this provides the large language model with richer context and structural relationships, which can improve the large language model's understanding of code structure relationships and increase the accuracy of the response information.

[0122] In an optional embodiment, the code generation method further includes: outputting the question intent, the target code knowledge, and the response information.

[0123] Specifically, when generating a subgraph, the target code knowledge includes the subgraph, thus outputting the target code knowledge, i.e., the target node and the subgraph. By providing the user with the problem intent, target code knowledge, and response information, the intermediate processing steps of the problem information are presented to the user. This helps the user understand the response processing, increases their understanding of code structure relationships, improves the reliability of response information, and enhances the user experience.

[0124] In an optional embodiment, the step of retrieving target code knowledge in the code knowledge graph based on the retrieval input information includes: matching the retrieval input information with the graph node identifiers of the code knowledge graph to obtain the target code knowledge, wherein the graph node identifiers are the identification information of code elements in the source code used to construct the code knowledge graph.

[0125] Node identifiers uniquely identify graph nodes in a code knowledge graph. A node identifier can be the identification information of the code element that generated the identified graph node. The identification information can be a name. Matching search input information with node identifiers allows retrieval of graph nodes corresponding to the node identifiers in the search input information. In some embodiments, the search input information is a query statement, which can be used to search for node identifiers that meet the query conditions. For example, if the search input information is an extended search, it can be used to search for node identifiers corresponding to key information.

[0126] Existing keyword searches are typically based on text matching. Text-matching searches can easily retrieve results from scripts or text files that are unrelated to the source code.

[0127] By setting the node identifier of the graph node to be consistent with the identifier information of the code element, retrieval based on the code identifier information can avoid retrieving some script fragments, achieve accurate retrieval of code elements from the source code, improve retrieval accuracy, and reduce redundant retrieval results.

[0128] In some embodiments, such as Figure 6 The diagram shows a search scenario.

[0129] During the retrieval phase, this embodiment integrates graph retrieval and traditional RAG retrieval components, thus compensating for the shortcomings of vector retrieval in handling more complex and context-sensitive queries. For different question-and-answer and code generation scenarios, the intent recognition model determines the user's request and how to perform the retrieval to obtain sufficient context. The intent recognition model outputs the question intent, and the corresponding retrieval type can be determined based on the question intent.

[0130] 1. For retrieval types that are query statements, Text2Gremlin (a large language model) can be used to convert natural language question information into query statements in specific graph query languages ​​(such as Gremlin and Cypher).

[0131] 2. For extended search types, a large language model can be used to perform entity recognition on the question information, rewrite, expand, and translate the question information to obtain the corresponding key information. Based on the attributes of graph nodes, keyword and full-text search functions can be implemented according to the key information.

[0132] 3. For search types of "other" searches, which can include server-side searches, client-side searches, and hybrid searches between the client and server, the following applies: Specifically, server-side searches can include traditional RAG-based searches, specifically vector searches or keyword searches. Client-side searches can include local file queries. Hybrid searches can include searches where the server provides the search scope or criteria, and the client performs a local search based on those criteria.

[0133] The first two graph retrieval methods can serve as secondary filtering or supplements to traditional embedding (vector retrieval) and keyword retrieval. The graph retrieval provided in this disclosure can achieve dependency retrieval at the file, class, and method levels, graphical retrieval results (subgraphs), and Symbol (node ​​identifier) ​​retrieval.

[0134] The server can integrate all search results and sort them according to the relevance between the question information and each search result to obtain the target code knowledge. This target code knowledge is then provided to a large language model, which generates the response information.

[0135] Furthermore, graph retrieval capabilities can serve as a standalone supplement to the agent capabilities of a large language model. Specifically, the graph retrieval implementation code can be encapsulated to a certain extent, allowing the large language model to use it autonomously through tools or function calls. The main encapsulations include:

[0136] 1. search_method_in_class(method_name: str, class_name: str)

[0137] 2. search_method_in_file(method_name: str, file_path: str)

[0138] 3. Search_method(method_name: str)

[0139] 4. Search_class_in_file(self, class_name, file_name: str)

[0140] In an application scenario, such as Figure 7The scenario diagram illustrating a code generation method based on a code knowledge graph illustrates the following: The question-answering process refers to the user inputting a question, the current electronic device processing it, and outputting a response. The retrieval process refers to the process of the retrieval model learning its retrieval capabilities and retrieving information from the code knowledge graph. The graph construction process refers to the process of building the code knowledge graph. It's important to note that the question-answering process, graph construction process, and retrieval process are not entirely unrelated; there is a connection between them.

[0141] In the knowledge graph construction process, the IDE obtains the local source code within the target scope specified by the user, generates a code tree, and sends the code tree to the server. The server then constructs the knowledge graph based on the code tree and updates the graph.

[0142] In the question-and-answer process, the user inputs a question (query), which is then fed into an intent recognition model for processing to determine the question intent. The intent recognition model adapts its graph retrieval capabilities based on the question intent to determine the retrieval type. The retrieval process is then executed according to the retrieval type. The retrieval type can include other retrieval methods, which include local retrieval and hybrid retrieval.

[0143] In the retrieval process, based on the retrieval input information corresponding to the retrieval type, the retrieval model searches the constructed code knowledge graph to obtain the retrieval results. A question-answering process is then executed based on the retrieval results.

[0144] In the question-and-answer process, the IDE performs a local search based on the search type to obtain local code, or performs a hybrid search in cooperation with the server. The search results from both the hybrid and local searches are sent to the server. The server integrates the search results; specifically, it integrates the results from local searches, hybrid searches, and graph searches to form target code knowledge. This target code knowledge, along with the question information, is then input into the response model to generate the code corresponding to the question information.

[0145] The intent recognition model, response model, and retrieval model can all be large language models. Corresponding prompt templates are input into these models to achieve intent recognition, question answering, and retrieval functions. Furthermore, the retrieval model and intent recognition model can be independent deep learning models. The retrieval model can continuously enrich its graph retrieval capabilities based on question information. For example, the retrieval model can implement query statement retrieval, node-level retrieval, or graph relationship retrieval. For instance, query statement retrieval can include retrieval based on search rules. Node-level retrieval can include: current file, code repository, folder, source file, and document. Graph relationship retrieval can include: graph dependency and graph caller relationships.

[0146] The response model can visualize the results. Specifically, during the model generation stage, this embodiment compresses and visualizes the retrieval results, improving the quality of code generation based on richer contextual and relational information from the model. Visualizing the retrieval results involves concatenating the retrieval results from the graph into a subgraph. This subgraph serves two purposes: firstly, it fills in missing paths in the retrieval results, enriching dependency information; secondly, visualizing the subgraph for the model and the user enhances understanding of code dependencies and improves the user experience.

[0147] Furthermore, the graph-based retrieval results in this embodiment achieve information compression. In generalized question answering and deep dependency handling, the graph preserves the original file, class, and method signatures and call information while removing redundant comments and source code. This results in a richer amount of information received by the same window model, a more complete understanding of the overall code architecture, and further improved executable code generation.

[0148] In the field of code intelligence, traditional RAG ignores the structural relationships and deep dependencies between source code elements, cannot effectively handle structural redundancy in source code, and the plain text expression method cannot fully show the structure and dependencies of files in the code library. In practical applications, the code generated by the model needs a lot of manual repair and adjustment before it can be correctly applied to the source file.

[0149] Based on this, this disclosure provides a method for improving the quality of LLM-generated code based on a code knowledge graph. Since a codebase is inherently a "graph" of interconnected code elements, this method parses the source code to construct a knowledge graph of the entire codebase. Then, based on intent recognition Router LLM and the knowledge graph, it provides more structured, precise, and scenario-based dependency relationships and structure definition retrieval capabilities, integrating them into the traditional RAG pipeline (automated process). This compensates for the shortcomings of vector retrieval and keyword retrieval, and addresses the limitations of existing technologies in handling complex programming tasks.

[0150] According to embodiments of this disclosure, Figure 8 This is a structural diagram of a code knowledge graph generation device according to an embodiment of this disclosure. This embodiment is applicable to the construction of knowledge graphs. The device is implemented in software and / or hardware and is specifically configured in an electronic device with certain data processing capabilities.

[0151] like Figure 8 The code knowledge graph generation device 800 shown includes: a code tree acquisition module 801, a graph node generation module 802, a graph edge generation module 803, and a knowledge graph construction module 804.

[0152] The code tree acquisition module 801 is used to acquire a code tree, which includes code elements and the structural relationships between code elements; wherein, the code tree is generated by parsing the source code.

[0153] The graph node generation module 802 is used to generate at least one graph node based on the code elements in the code tree;

[0154] The graph edge generation module 803 is used to generate edges between corresponding graph nodes based on the structural relationships between code elements in the code tree.

[0155] The knowledge graph construction module 804 is used to generate a code knowledge graph based on each graph node and the edges between graph nodes.

[0156] According to the technical solution disclosed herein, by parsing the source code, extracting the code elements and structural relationships, generating a code tree based on the extracted content, and generating a code knowledge graph based on the code tree, the processing of code elements and structural relationships can be reduced, the loss of effective data in the code knowledge graph can be reduced, redundant information in the source code can be deleted, the code knowledge graph can be simplified, the storage and data processing volume of the knowledge graph can be reduced, the construction efficiency of the knowledge graph can be improved, and code elements from other scopes involved in the source code can be obtained, adding global information, thereby increasing the completeness of the knowledge graph.

[0157] Optionally, the code knowledge graph generation device further includes:

[0158] The description information acquisition module is used to acquire the description information associated with the code tree;

[0159] The knowledge graph generation module is used to add the description information to the code knowledge graph.

[0160] Optionally, the description information acquisition module includes:

[0161] The description node adding unit is used to generate graph nodes based on the description information and add them to the code knowledge graph;

[0162] The description element corresponding unit is used to obtain the code element corresponding to the description information;

[0163] The edge generation unit is used to generate edges between graph nodes generated by the description information and graph nodes generated by the corresponding code elements in the code knowledge graph.

[0164] Optionally, the code tree is generated by parsing the source code of a target scope specified by the user, where the target scope includes source files, code libraries, or directories.

[0165] The above-described code knowledge graph generation device can execute the code knowledge graph generation method provided in any embodiment of this disclosure, and has the corresponding functional modules and beneficial effects of executing the code knowledge graph generation method.

[0166] According to embodiments of this disclosure, Figure 9 This is a structural diagram of the code generation device in this embodiment of the disclosure. This embodiment is applicable to human-computer interaction based on the knowledge graph constructed in the foregoing embodiments. The device is implemented in software and / or hardware and is specifically configured in an electronic device with certain data processing capabilities.

[0167] like Figure 9 The code generation device 900 shown includes: a question acquisition module 901, a knowledge retrieval module 902, and a response generation module 903. Among them,

[0168] Problem retrieval module 901 is used to retrieve problem information;

[0169] The knowledge retrieval module 902 is used to retrieve code knowledge from the code knowledge graph based on the question information; the code knowledge graph is generated by the code knowledge graph generation method described in any embodiment of this disclosure.

[0170] The response generation module 903 is used to generate response information based on the target code knowledge and the question information.

[0171] According to the technical solution disclosed herein, by retrieving code knowledge related to the problem information from the constructed code knowledge graph and processing the problem information in combination with the code knowledge, the response information can be obtained. Code knowledge can be retrieved from the code knowledge graph that retains the original code elements and removes redundant information, so that the limited code knowledge includes richer key information, which helps to understand the overall code architecture and improves the reliability of the response information.

[0172] Optionally, the knowledge retrieval module 902 includes:

[0173] The question intent acquisition unit is used to perform intent recognition on the question information to obtain the question intent;

[0174] The retrieval type determination unit is used to determine the retrieval type based on the intent of the question;

[0175] A retrieval information determination unit is used to obtain retrieval input information corresponding to the retrieval type;

[0176] The code knowledge retrieval unit is used to retrieve code knowledge from the code knowledge graph based on the retrieval input information.

[0177] Optionally, the retrieval type includes: query statement retrieval;

[0178] The retrieval information determination unit includes:

[0179] The query statement generation subunit is used to generate a query statement based on the question information.

[0180] The first input determination subunit is used to determine the query statement as the retrieval input information corresponding to the retrieval type.

[0181] Optionally, the search type includes: extended search;

[0182] The retrieval information determination unit includes:

[0183] The key information determination subunit is used to obtain the key information corresponding to the problem information and the problem intent;

[0184] The second input determination subunit is used to determine the key information as the retrieval input information corresponding to the retrieval type.

[0185] Optionally, the code knowledge retrieval unit includes:

[0186] The target node query subunit is used to search the code knowledge graph based on the search input information to obtain multiple target nodes.

[0187] The node subgraph generation subunit is used to generate a subgraph based on the target node and the relationships between the target nodes;

[0188] Sub-units are added to the subgraph to identify each target node and the subgraph as code knowledge.

[0189] Optionally, the code generation apparatus further includes:

[0190] The problem feedback module is used to provide the user with the problem intent, code knowledge, and response information.

[0191] Optionally, the code knowledge retrieval unit includes:

[0192] The identification matching subunit is used to match the retrieval input information with the graph node identifiers of the code knowledge graph to obtain the target code knowledge. The graph node identifiers are the identification information of code elements in the source code used to construct the code knowledge graph.

[0193] Optionally, the response generation module 903 includes:

[0194] The search result sorting unit is used to sort the search results included in the target code knowledge according to the question information to obtain the sorting result;

[0195] The response information generation unit is used to generate response information based on the sorting result and the question information.

[0196] Optionally, the code generation apparatus further includes:

[0197] The specified code tree retrieval module is used to retrieve the code tree generated based on the specified local source code while simultaneously obtaining the user's input question information;

[0198] The knowledge graph generation module is used to obtain the code knowledge graph based on the code tree.

[0199] The above-described code generation apparatus can execute the code generation method provided in any embodiment of this disclosure, and has the corresponding functional modules and beneficial effects for executing the code generation method.

[0200] The collection, storage, use, processing, transmission, provision, and disclosure of user personal information involved in the technical solution disclosed herein comply with the provisions of relevant laws and regulations and do not violate public order and good morals.

[0201] According to embodiments of this disclosure, this disclosure also provides an electronic device, a readable storage medium, and a computer program product.

[0202] Figure 10A schematic area diagram of an example electronic device 1000 that can be used to implement embodiments of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device may also represent various forms of mobile devices, such as personal digital processors, cellular phones, smartphones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely illustrative and are not intended to limit the implementation of the present disclosure described and / or claimed herein.

[0203] like Figure 10 As shown, device 1000 includes a computing unit 1001, which can perform various appropriate actions and processes according to a computer program stored in read-only memory (ROM) 1002 or a computer program loaded into random access memory (RAM) 1003 from storage unit 1008. The RAM 1003 may also store various programs and data required for the instructions of device 1000. The computing unit 1001, ROM 1002, and RAM 1003 are interconnected via bus 1004. Input / output (I / O) interface 1005 is also connected to bus 1004.

[0204] Multiple components in device 1000 are connected to I / O interface 1005, including: input unit 1006, such as keyboard, mouse, etc.; output unit 1007, such as various types of monitors, speakers, etc.; storage unit 1008, such as disk, optical disk, etc.; and communication unit 1009, such as network card, modem, wireless transceiver, etc. Communication unit 1009 allows device 1000 to exchange information / data with other devices through computer networks such as the Internet and / or various telecommunications networks.

[0205] The computing unit 1001 can be various general-purpose and / or special-purpose processing components with processing and computing capabilities. Some examples of the computing unit 1001 include, but are not limited to, a central processing unit (CPU), a graphics processing unit (GPU), various special-purpose artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, a digital signal processor (DSP), and any suitable processor, controller, microcontroller, etc. The computing unit 1001 performs the various methods and processes described above, such as code knowledge graph generation methods or code generation methods. For example, in some embodiments, the code knowledge graph generation method or code generation method can be implemented as a computer software program tangibly contained in a machine-readable medium, such as storage unit 1008. In some embodiments, part or all of the computer program can be loaded and / or installed on device 1000 via ROM 1002 and / or communication unit 1009. When the computer program is loaded into RAM 1003 and executed by the computing unit 1001, one or more steps of the code knowledge graph generation method or code generation method described above can be performed. Alternatively, in other embodiments, the computing unit 1001 may be configured to execute a code knowledge graph generation method or a code generation method by any other suitable means (e.g., by means of firmware).

[0206] Various implementations of the systems and techniques described above herein can be implemented in digital electronic circuit systems, integrated circuit systems, field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard objects (ASSPs), systems-on-a-chip (SoCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations may include: implementations in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which may be a dedicated or general-purpose programmable processor, capable of receiving data and instructions from a storage system, at least one input device, and at least one output device, and transmitting data and instructions to the storage system, the at least one input device, and the at least one output device.

[0207] The program code used to implement the methods of this disclosure may be written in any combination of one or more programming languages. This program code may be provided to a processor or controller of a general-purpose computer, special-purpose computer, or other programmable data processing apparatus, such that when executed by the processor or controller, the program code causes the functions / instructions specified in the flowcharts and / or area diagrams to be implemented. The program code may be executed entirely on a machine, partially on a machine, as a standalone software package partially on a machine and partially on a remote machine, or entirely on a remote machine or server.

[0208] In the context of this disclosure, a machine-readable medium can be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, apparatus, or device. A machine-readable medium can be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium can be, but is not limited to, electronic, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatus, or devices, or any suitable combination of the foregoing. More specific examples of machine-readable storage media include electrical connections based on one or more wires, portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0209] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device for displaying information to the user (e.g., a CRT (cathode ray tube) or LCD (liquid crystal display) monitor); and a keyboard and pointing device (e.g., a mouse or trackball) through which the user provides input to the computer. Other types of devices can also be used to provide interaction with the user; for example, feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including sound input, voice input, or tactile input).

[0210] The systems and technologies described herein can be implemented in computing systems that include backend components (e.g., as data servers), or middleware components (e.g., application servers), or frontend components (e.g., user computers with graphical user interfaces or web browsers through which users can interact with implementations of the systems and technologies described herein), or any combination of such backend, middleware, or frontend components. The components of the system can be interconnected via digital data communication of any form or medium (e.g., communication networks). Examples of communication networks include local area networks (LANs), wide area networks (WANs), blockchain networks, and the Internet.

[0211] Computer systems can include clients and servers. Clients and servers are generally geographically separated and typically interact via communication networks. The client-server relationship is established by computer programs running on the respective computers and having a client-server relationship with each other. A server can be a cloud server, also known as a cloud computing server or cloud host, a hosting product within the cloud computing service ecosystem that addresses the management difficulties and weak business scalability inherent in traditional physical hosting and VPS services. Servers can also be servers for distributed systems or servers integrated with blockchain technology.

[0212] Artificial intelligence (AI) is the study of enabling computers to simulate certain human thought processes and intelligent behaviors (such as learning, reasoning, thinking, and planning). It encompasses both hardware and software technologies. AI hardware technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, and big data processing. AI software technologies mainly include computer vision, speech recognition, natural language processing, machine learning / deep learning, big data processing, and knowledge graph technologies.

[0213] Cloud computing refers to a technology system that enables access to a shared pool of physical or virtual resources via a network. These resources can include servers, instruction sets, networks, software, applications, and storage devices, and can be deployed and managed on demand and in a self-service manner. Cloud computing technology can provide efficient and powerful data processing capabilities for applications such as artificial intelligence and blockchain, as well as for model training.

[0214] It should be understood that the various forms of processes shown above can be used to reorder, add, or delete steps. For example, the steps described in this disclosure can be executed in parallel, sequentially, or in different orders, as long as the desired result of the technical solution provided in this disclosure can be achieved, and this is not limited herein.

[0215] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.

Claims

1. A method for generating a code knowledge graph, comprising: Obtain a code tree, the code tree including first code elements and the structural relationships between the first code elements; wherein, the code tree is generated by parsing the source code. Generate the first graph node information based on the first code element; Based on the structural relationship, the first graph edge information between the corresponding graph nodes is generated; Generate a code knowledge graph based on the information of each node in the first graph and the edge information of the first graph; The code tree is generated by parsing the source code of a specified target scope, which includes source files, code libraries, or directories. The specification of the target scope occurs simultaneously with the input of the question information. The code tree generated from the source code of the target scope is used to generate a code knowledge graph and retrieve the question information in the code knowledge graph. The source code also includes the first code element of other coding scopes outside the current coding scope and the structural relationship between the first code element of the current coding scope and the first code element of the current coding scope.

2. The method according to claim 1, further comprising: Obtain the description information associated with the code tree; Add the description information to the code knowledge graph.

3. The method according to claim 2, wherein, Adding the description information to the code knowledge graph includes: Based on the description information, generate the second map node information; Add the second graph node information to the code knowledge graph; Obtain the second code element corresponding to the description information; In the code knowledge graph, second graph edge information is generated between the second graph node information and the corresponding first graph node; Add the second graph edge information to the code knowledge graph.

4. A code generation method, comprising: Obtain the input question information; Based on the question information, a search is performed in the code knowledge graph to obtain the target code knowledge; The code knowledge graph is generated using any one of the code knowledge graph generation methods of claims 1-3; Based on the target code knowledge and the question information, a response message is generated; The code generation method also includes: Get the code tree generated based on the specified local source code; Based on the code tree, obtain the code knowledge graph.

5. The method according to claim 4, wherein, The step of retrieving target code knowledge from the code knowledge graph based on the question information includes: The intent of the question is obtained by performing intent recognition on the question information; Determine the search type based on the stated question intent; Obtain the search input information corresponding to the search type; Based on the retrieval input information, the target code knowledge is obtained by searching the code knowledge graph.

6. The method according to claim 5, wherein, The search types include: query statement search; The step of obtaining the search input information corresponding to the search type includes: Based on the question information, a query statement is generated; wherein, the retrieval input information includes the query statement.

7. The method according to claim 5, wherein, The search types include: extended search; The step of obtaining the search input information corresponding to the search type includes: Based on the question information and the question intent, key information is determined; wherein, the retrieval input information includes the key information.

8. The method according to claim 7, wherein, The step of retrieving the target code knowledge from the code knowledge graph based on the retrieval input information includes: Based on the retrieval input information, a search is performed in the code knowledge graph to obtain at least two target nodes; A subgraph is generated based on the target nodes and the relationships between them; The target code knowledge includes the target node and the subgraph.

9. The method according to claim 7, further comprising: Output the intent of the question, the knowledge of the target code, and the response information.

10. The method according to claim 5, wherein the step of retrieving the target code knowledge from the code knowledge graph based on the retrieval input information includes: The search input information is matched with the graph node identifiers of the code knowledge graph to obtain the target code knowledge. The graph node identifiers are the identification information of code elements in the source code used to construct the code knowledge graph.

11. The method according to claim 4, wherein, The step of generating response information based on the target code knowledge and the question information includes: Based on the question information, the search results included in the target code knowledge are sorted to obtain the sorting result; The response information is generated based on the sorting results and the question information.

12. A code knowledge graph generation device, comprising: A code tree acquisition module is used to acquire a code tree, which includes a first code element and the structural relationships between the first code elements; wherein, the code tree is generated by parsing the source code. The graph node generation module is used to generate first graph node information based on the first code element; The graph edge generation module is used to generate the first graph edge information between corresponding graph nodes based on the structural relationship. The knowledge graph construction module is used to generate a code knowledge graph based on the information of each node in the first graph and the edge information of the first graph. The code tree is generated by parsing the source code of a specified target scope, which includes source files, code libraries, or directories. The specification of the target scope occurs simultaneously with the input of the question information. The code tree generated from the source code of the target scope is used to generate a code knowledge graph and retrieve the question information in the code knowledge graph. The source code also includes the first code element of other coding scopes outside the current coding scope and the structural relationship between the first code element of the current coding scope and the first code element of the current coding scope.

13. The apparatus of claim 12, further comprising: The description information acquisition module is used to acquire the description information associated with the code tree; The knowledge graph generation module is used to add the description information to the code knowledge graph.

14. The apparatus according to claim 13, wherein, The description information acquisition module includes: The description node generation unit is used to generate second map node information based on the description information; The description node adding unit is used to add the second graph node information to the code knowledge graph; The description element corresponding unit is used to obtain the second code element corresponding to the description information; The edge generation unit is used to generate second graph edge information between the second graph node information and the corresponding first graph node in the code knowledge graph. The description edge-adding unit is used to add the edge information of the second graph to the code knowledge graph.

15. A code generation apparatus, comprising: The problem acquisition module is used to acquire the input problem information; The knowledge retrieval module is used to retrieve target code knowledge from the code knowledge graph based on the question information. The code knowledge graph is generated using any one of the code knowledge graph generation methods of claims 1-3; The response generation module is used to generate response information based on the target code knowledge and the question information; The device further includes: Specify the code tree retrieval module, which is used to retrieve the code tree generated based on the specified local source code; The knowledge graph generation module is used to obtain the code knowledge graph based on the code tree.

16. The apparatus according to claim 15, wherein, The knowledge retrieval module includes: The question intent acquisition unit is used to perform intent recognition on the question information to obtain the question intent; The retrieval type determination unit is used to determine the retrieval type based on the intent of the question; A retrieval information determination unit is used to obtain retrieval input information corresponding to the retrieval type; The code knowledge retrieval unit is used to retrieve the target code knowledge from the code knowledge graph based on the retrieval input information.

17. The apparatus according to claim 16, wherein, The search types include: query statement search; The retrieval information determination unit includes: The query statement generation subunit is used to generate a query statement based on the question information; wherein, the retrieval input information includes the query statement.

18. The apparatus according to claim 16, wherein, The search types include: extended search; The retrieval information determination unit includes: The key information determination subunit is used to determine key information based on the question information and the question intent; wherein the retrieval input information includes the key information.

19. The apparatus according to claim 18, wherein, The code knowledge retrieval unit includes: The target node query subunit is used to perform a search in the code knowledge graph based on the search input information to obtain at least two target nodes. A node subgraph generation subunit is used to generate a subgraph based on the target node and the relationships between the target nodes; wherein, the target code knowledge includes: the target node and the subgraph.

20. The apparatus of claim 18, further comprising: The problem feedback module is used to output the problem intent, the target code knowledge, and the response information.

21. The apparatus according to claim 16, wherein, The code knowledge retrieval unit includes: The identification matching subunit is used to match the retrieval input information with the graph node identifiers of the code knowledge graph to obtain the target code knowledge. The graph node identifiers are the identification information of code elements in the source code used to construct the code knowledge graph.

22. The apparatus according to claim 15, wherein, The response generation module includes: The search result sorting unit is used to sort the search results included in the target code knowledge according to the question information to obtain the sorting result; The response information generation unit is used to generate the response information based on the sorting result and the question information.

23. An electronic device, comprising: At least one processor; as well as A memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, the instructions being executed by the at least one processor to enable the at least one processor to perform the code knowledge graph generation method of any one of claims 1-3, or the code generation method of any one of claims 4-11.

24. A non-transitory computer-readable storage medium storing computer instructions, wherein, The computer instructions are used to cause the computer to execute the code knowledge graph generation method according to any one of claims 1-3, or the code generation method according to any one of claims 4-11.

25. A computer program product comprising a computer program that, when executed by a processor, implements the code knowledge graph generation method according to any one of claims 1-3, or the code generation method according to any one of claims 4-11.

Citation Information

Patent Citations

  • Software knowledge graph incremental updating method based on code submission

    CN115543402A

  • Defect positioning method and system based on code knowledge graph

    CN115629760A

  • Code retrieval processing method and device

    CN117933395A