Code generation method and corresponding device
By building a knowledge base and utilizing the mapping relationship between structured views and code segments, the gap between natural language and programming language in large models is resolved, the accuracy of code generation is improved, and more efficient code generation is achieved.
Patent Information
- Application Number
- CN202410378030.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-03-28
- Publication Date
- 2025-10-03
AI Technical Summary
Large models have a gap between natural language and programming language, resulting in insufficient accuracy in code generation.
By building a knowledge base and utilizing the mapping relationship between structured views and code segments, we determine the index to query the knowledge base, and combine prompt information to stimulate the context learning ability of the code generation model to improve the accuracy of code generation.
The accuracy of code generation has been improved. Through inter-modal and intra-modal alignment technology, the deviation of the generated code is corrected, the grammatical alignment and retention of structured information between different languages are improved, and the accuracy of the generated code is enhanced.
Smart Images

Figure CN120743239A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of computer technology, and in particular to a code generation method and corresponding device. Background Art
[0002] Recently, big models have demonstrated their influence across various fields, sweeping across industry and academia. This has led to a surge in research on using big models for automated code generation. When using big models for code generation, the intended code functionality is typically described in natural language. This natural language is then fed into the big model, which then generates code based on the natural language.
[0003] At present, although large models have achieved success in natural language understanding and generation, there is still a gap between natural language and programming language. This gap makes general large models have certain inherent limitations when performing code generation tasks, affecting the accuracy of generated code. Summary of the Invention
[0004] The present application provides a code generation method for improving the accuracy of generated code, and also provides a corresponding device, a computer-readable storage medium, and a computer program product.
[0005] The first aspect of the present application provides a code generation method, comprising: determining a first index for accessing a knowledge base based on a target problem, wherein the target problem is a function to be implemented by a target code described in natural language, and the knowledge base includes a mapping relationship between code segments and structured views, and the structured views are used to represent the semantics and logical relationships of the code segments; querying the knowledge base based on the first index to obtain a first mapping relationship associated with the first index; inputting the target problem and prompt information into a code generation model to obtain a target code for the target problem, wherein the prompt information is associated with the first mapping relationship.
[0006] In this application, the target problem can be the function that the target code is to implement, which is described by the user in natural language, such as: a function is required to input two numbers and return their sum.
[0007] In this application, the knowledge base may include mapping relationships between multiple code segments and structured views. Each mapping relationship may be obtained by constructing a corresponding structured view using a different code segment. Different mapping relationships are marked by different indexes, such as: code segment c i The corresponding structured view g i The index of is i.
[0008] In this application, code segments generally refer to multiple segments of program code to implement a certain function. A structured view refers to a view or graph that represents the semantics and logical relationships of code segments in the form of nodes and edges.
[0009] In the present application, the prompt information may be the first mapping relationship or obtained through the first mapping relationship.
[0010] In this application, the code generation model may be a large language model (LLM).
[0011] In the first aspect described above, a first mapping relationship related to the target problem can be obtained through the knowledge base. This first mapping relationship includes a first structured view and a corresponding code segment. Thus, when generating target code for the target problem, not only the target problem but also prompt information associated with the first mapping relationship can be input into the code generation model. This prompt information can stimulate the contextual learning capabilities of the code generation model, improve the code generation effect, and enhance the accuracy of the generated target code.
[0012] In one possible implementation, the prompt information is the meta-graph information of the first structured view in the first mapping relationship, the first structured view in the first mapping relationship, or the first mapping relationship; wherein the meta-graph information includes node information and / or edge information in the first structured view.
[0013] In this possible implementation, regardless of whether the prompt information is the meta-graph information of the first structured view, the first structured view, or the first mapping relationship, the prompt information includes structured information on the target code requirements. This is beneficial for the code generation model to correct deviations based on the structured information when generating code according to the target problem, thereby improving the accuracy of the generated target code.
[0014] In one possible implementation, the above steps of: determining the first index for accessing the knowledge base based on the target problem, include: extracting key information from the target problem; determining the first index based on the key information and the knowledge base, the first index being the index of the code segment and / or structured view in the knowledge base that has the highest similarity to the key information.
[0015] In this application, key information may be the core sentences, words or characters in the target problem. For example, if the target problem is "a function is required, which takes two numbers as input and returns their sum.", the key information may include: "function", "two numbers" and "sum".
[0016] In this possible implementation, by comparing the similarity between the key information and the code segments and / or structured views in the knowledge base, the code segment and / or structured view with the highest similarity can be determined, and the index of the code segment and / or structured view with the highest similarity is determined as the first index. In this way, during code generation, this first index can be used to query a first mapping relationship suitable for the target problem, thereby improving the accuracy of the generated target code.
[0017] In a possible implementation, extracting key information from a target problem includes encoding problem description information and function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0018] In this application, the problem description information may describe the task to be completed by the target code (function); the function description information may be a function declaration, such as giving the function name and input variables.
[0019] In this possible implementation, the first vector of key information can be obtained by encoding the target problem's problem description and function description. This vector facilitates alignment of different modalities, such as natural language and programming language, or natural language and structured views, and facilitates accurate determination of the first index.
[0020] In one possible implementation, the above steps of determining the first index based on the key information and the knowledge base include: performing a first inter-modal alignment on the key information and the first code segment in the first mapping relationship, performing a second inter-modal alignment on the first code segment and the first structured view, and performing intra-modal alignment on the first structured view to determine the first index.
[0021] In this possible implementation, inter-modal alignment can generalize grammatical differences between languages, allowing the code generation model to generate code in different programming languages. Intra-modal alignment can generalize the structural information of structured views, further improving the accuracy of the generated target code.
[0022] In one possible implementation, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0023] In this possible implementation, natural language and programming language, as well as programming language and structured views, are aligned through vectors. This can effectively generalize the differences between different languages and align information described in different languages, thereby improving the accuracy of the generated target code.
[0024] In a possible implementation, the intra-modal alignment is to preserve the structural information of the first structured view by comparing the first structured view and the second structured view, and the second structured loss is to obtain a damaged view by destroying the edge of the first structured view.
[0025] In this possible implementation, by comparing and learning the first structured view and its corresponding damaged view, the structural information of the first structured view can be effectively preserved.
[0026] In one possible implementation, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0027] In this possible implementation, the first index is determined by calculating the distance between vectors, which can increase the speed of determining the first index.
[0028] In a possible implementation, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0029] In this possible implementation, the first encoder is trained by using positive and negative training samples, which can improve the encoding accuracy of the first encoder and further improve the accuracy of encoding the code segment.
[0030] In one possible implementation, the view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is trained using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0031] In this possible implementation, the second encoder is trained by using positive and negative training samples, which can improve the encoding accuracy of the second encoder and further improve the accuracy of encoding the structured view.
[0032] In one possible implementation, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, which is obtained using the third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0033] In this possible implementation, the self-supervisory model in the second encoder is trained by using positive and negative training samples, which can improve the accuracy of the self-supervisory model and thus improve the accuracy of encoding the structured view.
[0034] In one possible implementation, the structured view in the knowledge base is obtained by integrating the data flow graph and control flow graph of the corresponding code segment. The nodes in the data flow graph represent the operations in the code segment, and the edges represent the data flow. The nodes in the control flow graph are the operations in the code segment, and the edges represent the execution order of the operations.
[0035] In this possible implementation, code segments are represented by means of data flow graphs and control flow graphs, and the structured views obtained by integrating the data flow graphs and control flow graphs can more effectively retain the valid information of the code language at both the semantic and logical levels, thereby improving the accuracy of subsequent code generation.
[0036] The second aspect of the present application provides a search method, comprising:
[0037] Get the target problem, which is the function to be achieved by the target code described in natural language;
[0038] Extract key information from the target question;
[0039] According to the key information and the knowledge base, a first index is retrieved from the knowledge base. The first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity with the key information. The knowledge base includes a mapping relationship between the code segment and the structured view. The structured view is used to represent the semantic and logical relationship of the code segment.
[0040] In this possible implementation, the key information of the target question can be used to retrieve the first index with the highest similarity to the target question from the knowledge base. This way, when subsequently generating the target code for the target question, the first index can be used to search the knowledge base, obtain the corresponding first mapping relationship, and then obtain prompt information for generating the target code. This retrieval method can help improve the accuracy of generated code.
[0041] In one possible implementation, the above step of extracting key information from the target problem includes encoding the problem description information and function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0042] In one possible implementation, the above steps of: retrieving a first index from the knowledge base based on the key information and the knowledge base, include: performing a first inter-modal alignment on the key information and the first code segment in the first mapping relationship, performing a second inter-modal alignment on the first code segment and the first structured view, and performing intra-modal alignment on the first structured view to determine the first index.
[0043] In one possible implementation, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0044] In a possible implementation, the intra-modal alignment is to preserve the structural information of the first structured view by comparing the first structured view and the second structured view, and the second structured loss is to obtain a damaged view by destroying the edge of the first structured view.
[0045] In one possible implementation, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0046] In a possible implementation, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0047] In one possible implementation, the view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is trained using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0048] In one possible implementation, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, which is obtained using the third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0049] The third aspect of the present application provides a method for constructing a knowledge base, including: generating a data flow graph and a control flow graph of a first code segment; wherein, the nodes in the data flow graph represent the operations in the code segment, and the edges represent the data flow, and the nodes in the control flow graph are the operations in the code segment, and the edges represent the execution order of the operations; integrating the data flow graph and the control flow graph to obtain a first structured view of the first code segment; establishing a first mapping relationship between the first code segment and the first structured view through a first index; adding the first mapping relationship to the knowledge base, and different mapping relationships in the knowledge base are marked by different indexes.
[0050] A fourth aspect of the present application provides a computer device, comprising:
[0051] a first processing unit, configured to determine a first index for accessing a knowledge base based on a target question, wherein the target question is a function to be implemented by a target code described in a natural language, the knowledge base including a mapping relationship between code segments and structured views, the structured views being used to represent semantic and logical relationships between the code segments;
[0052] A second processing unit is configured to query a knowledge base according to the first index to obtain a first mapping relationship associated with the first index;
[0053] The third processing unit is configured to input the target problem and the prompt information into a code generation model to obtain a target code for the target problem, wherein the prompt information is associated with the first mapping relationship.
[0054] In one possible implementation, the prompt information is the meta-graph information of the first structured view in the first mapping relationship, the first structured view in the first mapping relationship, or the first mapping relationship; wherein the meta-graph information includes node information and / or edge information in the first structured view.
[0055] In one possible implementation, the first processing unit is specifically used to extract key information from the target problem; determine a first index based on the key information and the knowledge base, where the first index is the index of the code segment and / or structured view in the knowledge base that has the highest similarity to the key information.
[0056] In a possible implementation, the first processing unit is specifically configured to encode the problem description information and the function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0057] In one possible implementation, the second processing unit is used to perform first inter-modality alignment on the key information and the first code segment in the first mapping relationship, perform second inter-modality alignment on the first code segment and the first structured view, and perform intra-modality alignment on the first structured view to determine the first index.
[0058] In one possible implementation, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0059] In a possible implementation, the intra-modal alignment is to preserve the structural information of the first structured view by comparing the first structured view and the second structured view, and the second structured loss is to obtain a damaged view by destroying the edge of the first structured view.
[0060] In one possible implementation, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0061] In a possible implementation, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0062] In one possible implementation, the view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is trained using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0063] In one possible implementation, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, which is obtained using the third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, gi ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0064] In one possible implementation, the structured view in the knowledge base is obtained by integrating the data flow graph and control flow graph of the corresponding code segment. The nodes in the data flow graph represent the operations in the code segment, and the edges represent the data flow. The nodes in the control flow graph are the operations in the code segment, and the edges represent the execution order of the operations.
[0065] A fifth aspect of the present application provides a computer device, comprising:
[0066] A first processing unit is configured to obtain a target problem, where the target problem is a function to be implemented by a target code described in a natural language;
[0067] a second processing unit, configured to extract key information from the target question;
[0068] A third processing unit is used to retrieve a first index from the knowledge base based on the key information and the knowledge base, where the first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity to the key information. The knowledge base includes a mapping relationship between code segments and structured views, and the structured view is used to represent the semantics and logical relationships of the code segments.
[0069] In a possible implementation, the second processing unit is configured to encode the problem description information and the function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0070] In one possible implementation, the third processing unit is used to perform first inter-modality alignment on the key information and the first code segment in the first mapping relationship, perform second inter-modality alignment on the first code segment and the first structured view, and perform intra-modality alignment on the first structured view to determine the first index.
[0071] In one possible implementation, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0072] In a possible implementation, the intra-modal alignment is to preserve the structural information of the first structured view by comparing the first structured view and the second structured view, and the second structured loss is to obtain a damaged view by destroying the edge of the first structured view.
[0073] In one possible implementation, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0074] In a possible implementation, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0075] In one possible implementation, the view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is trained using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0076] In one possible implementation, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, which is obtained using the third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, gi ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0077] A sixth aspect of the present application provides a computer device, comprising:
[0078] a first processing unit, configured to generate a data flow graph and a control flow graph for the first code segment; wherein the nodes in the data flow graph represent operations in the code segment and the edges represent data flow; and wherein the nodes in the control flow graph represent operations in the code segment and the edges represent the order in which the operations are executed;
[0079] a second processing unit, configured to integrate the data flow graph and the control flow graph to obtain a first structured view of the first code segment;
[0080] The third processing unit is configured to establish a first mapping relationship between the first code segment and the first structured view through the first index; and add the first mapping relationship to the knowledge base, wherein different mapping relationships in the knowledge base are marked by different indexes.
[0081] In a seventh aspect, the present application provides a computer device comprising a processor and a computer-readable storage medium storing a computer program; the processor is coupled to the computer-readable storage medium, and when the computer program is executed by the processor, the method of the first aspect or any possible implementation method is implemented.
[0082] In an eighth aspect, the present application provides a computer device comprising a processor and a computer-readable storage medium storing a computer program; the processor is coupled to the computer-readable storage medium, and when the computer program is executed by the processor, the method of the second aspect or any possible implementation method is implemented.
[0083] In a ninth aspect of the present application, a computer device is provided, which includes a processor and a computer-readable storage medium storing a computer program; the processor is coupled to the computer-readable storage medium, and when the computer program is executed by the processor, the method of the third aspect or any possible implementation method is implemented.
[0084] In the tenth aspect, the present application provides a computer-readable storage medium storing one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes a method as described in the first aspect or any possible implementation of the first aspect.
[0085] In the eleventh aspect of the present application, a computer-readable storage medium is provided for storing one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes a method as described in the second aspect or any possible implementation of the second aspect.
[0086] The twelfth aspect of the present application provides a computer-readable storage medium storing one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes a method as described in the third aspect or any possible implementation of the third aspect.
[0087] The thirteenth aspect of the present application provides a computer program product that stores one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes the method of the above-mentioned first aspect or any possible implementation of the first aspect.
[0088] In the fourteenth aspect, the present application provides a computer program product that stores one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes the method of the above-mentioned second aspect or any possible implementation of the second aspect.
[0089] The fifteenth aspect of the present application provides a computer program product that stores one or more computer-executable instructions. When the computer-executable instructions are executed by a processor, the processor executes the method of the above-mentioned third aspect or any possible implementation of the third aspect.
[0090] A sixteenth aspect of the present application provides a chip system, which includes a processor for supporting a computer device to implement the functions involved in the above-mentioned first aspect or any possible implementation of the first aspect. In one possible design, the chip system may also include a memory for storing program instructions and data necessary for training nodes. The chip system may be composed of a chip or may include a chip and other discrete devices.
[0091] In aspect 17 of the present application, a chip system is provided, which includes a processor for supporting a computer device to implement the functions involved in the above-mentioned second aspect or any possible implementation of the second aspect. In one possible design, the chip system may also include a memory for storing program instructions and data necessary for training nodes. The chip system may be composed of a chip or may include a chip and other discrete devices.
[0092] In aspect 18 of the present application, a chip system is provided, which includes a processor for supporting a computer device to implement the functions involved in the third aspect or any possible implementation of the third aspect. In one possible design, the chip system may also include a memory for storing program instructions and data necessary for training nodes. The chip system may be composed of a chip or may include a chip and other discrete devices.
[0093] The nineteenth aspect of the present application provides a cloud system, comprising: receiving a target problem, and executing a code generation process according to the method in the above-mentioned first aspect or any possible implementation method of the first aspect, and returning a target code.
[0094] The twentieth aspect of the present application provides a cloud system, comprising: receiving a target question, and executing a retrieval process according to the method in the above-mentioned second aspect or any possible implementation of the second aspect.
[0095] Among them, the technical effects brought about by the second to twentieth aspects, and any possible implementation methods of the second to twentieth aspects can refer to the technical effects brought about by the first aspect or different possible implementation methods of the first aspect, and will not be repeated here. BRIEF DESCRIPTION OF THE DRAWINGS
[0096] Figure 1 An example schematic diagram of the code generation process provided in an embodiment of the present application;
[0097] Figure 2A A schematic diagram of the structure of the cloud system provided in an embodiment of the present application;
[0098] Figure 2B Another structural diagram of the cloud system provided in an embodiment of the present application;
[0099] Figure 2C A schematic diagram of the structure of a data center provided in an embodiment of the present application;
[0100] Figure 3 An example schematic diagram of constructing a structured view provided in an embodiment of the present application;
[0101] Figure 4 An exemplary schematic diagram of inter-modal and intra-modal alignment provided in an embodiment of the present application;
[0102] Figure 5 An exemplary schematic diagram of a structured view is provided for an embodiment of the present application;
[0103] Figure 6 A schematic diagram of an embodiment of a code generation method provided in an embodiment of the present application;
[0104] Figure 7 An exemplary schematic diagram of a code generation method provided in an embodiment of the present application;
[0105] Figure 8 An example diagram of the experimental results provided in the embodiments of the present application;
[0106] Figure 9 Another example diagram of the experimental results provided in the embodiments of the present application;
[0107] 10A to 10C A plurality of example diagrams of experimental results provided for the embodiments of the present application;
[0108] Figure 11 A schematic diagram of the structure of a computer device provided for the implementation of this application;
[0109] Figure 12 A schematic diagram of the structure of a computer device provided for the implementation of this application. DETAILED DESCRIPTION
[0110] The following describes the embodiments of the present application in conjunction with the accompanying drawings. Obviously, the embodiments described are only part of the embodiments of the present application, rather than all the embodiments. Those skilled in the art will appreciate that with the development of technology and the emergence of new scenarios, the technical solutions provided in the embodiments of the present application are also applicable to similar technical problems.
[0111] The terms "first," "second," and the like in the specification and claims of this application and in the accompanying drawings are used to distinguish similar objects and are not necessarily used to describe a particular order or precedence. It should be understood that the terms used in this manner are interchangeable where appropriate so that the embodiments described herein can be implemented in an order other than that illustrated or described herein. In addition, the terms "including" and "having," as well as any variations thereof, are intended to cover non-exclusive inclusions, e.g., a process, method, system, product, or apparatus comprising a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units that are not explicitly listed or that are inherent to these processes, methods, products, or apparatus.
[0112] The present application provides a method for code generation to improve the accuracy of generated code. The present application also provides a corresponding apparatus, a computer-readable storage medium, and a computer program product. These are described in detail below.
[0113] To facilitate understanding, the following briefly introduces the technical terms involved in the embodiments of this application:
[0114] 1. Artificial Intelligence (AI): AI is the theory, methods, techniques, and application systems that use digital computers or machines controlled by digital computers to simulate, extend, and expand human intelligence, to perceive the environment, acquire knowledge, and use that knowledge to achieve optimal results. In other words, AI is a branch of computer science that seeks to understand the essence of intelligence and produce new intelligent machines that can respond in a manner similar to human intelligence. AI also studies the design principles and implementation methods of various intelligent machines, enabling them to perceive, reason, and make decisions. Research in the field of AI includes robotics, natural language processing, computer vision, decision-making and reasoning, human-computer interaction, recommendation and search, and basic AI theory. AI applications typically involve pre-designing AI models and then training them with large amounts of data to produce reasoning models suitable for different scenarios.
[0115] 2. AI Framework: The core function of an AI framework is to provide developers with an interface (mathematical operations) for building neural networks. It automatically performs reverse derivation of neural network training, approximating the optimal value, and obtaining a neural network model (approximation function) that can be used to solve classification, regression, and fitting problems, enabling application scenarios such as object classification and speech recognition. Mainstream AI frameworks in the industry include Mindspore, TensorFlow, and PyTorch. AI frameworks generally use computational graphs to represent network structures. This AI framework can be a deep learning framework or another machine learning (ML) framework.
[0116] 3. Deep learning (DL): A branch of machine learning, DL is an algorithm that uses artificial neural networks as a framework to learn and represent data. Deep learning frameworks include deep neural networks, convolutional neural networks, deep belief networks, and recurrent neural networks.
[0117] 4. Language model (LM): refers to a model for modeling natural language. Usually, given a natural language x 1~t Under the premise of , LM will predict the probability of the next word appearing according to the above context, that is, the modeling distribution P(x t+1 |x 1~t A variety of machine learning models can be used to model natural language, forming so-called language models, such as hidden Markov chains, naive Bayesian models, and neural networks. With the advent of the deep learning era, neural network models have gradually become mainstream. In particular, after the introduction of the transformer architecture, this paradigm of neural networks has gradually become the mainstream of LM.
[0118] 5. Large language models (LLMs): Compared to traditional language models (LMs), LLMs are distinguished by their massive parameter count, often reaching tens of billions, hundreds of billions, or even trillions. Current LLMs often use transformers as their underlying architecture and are pre-trained using massive amounts of text data, resulting in powerful language modeling capabilities. This allows for fine-tuning of trained large models, resulting in larger models with varying fine-tuning parameters to suit different domains or scenarios.
[0119] 6. Code generation: This is an important basic capability of the large language model. The large language model can directly generate target code based on the user's natural language. Figure 1 As shown, the user enters the natural language "input two strings a and b consisting of 0s and 1s, perform a binary operation on these inputs, and return the result." This natural language description can be called a "question" or "request." The LLM converts this natural language description into a programming language and outputs the corresponding code. The input code can also be called a code snippet.
[0120] The code generation method or retrieval method provided in the embodiments of the present application can be executed on a cloud system, an independent server or a terminal device. The following is an introduction using the execution of the code generation method or retrieval method on a cloud system as an example.
[0121] Figure 2A A schematic diagram of the structure of the cloud system provided in an embodiment of the present application.
[0122] like Figure 2A As shown, the cloud system includes a scheduling device and a centralized resource pool. The centralized resource pool can be a resource collection of multiple resource nodes, and the centralized resource pool runs LLM. The scheduling device can communicate with the tenant's client, receive the tenant's target problem, and schedule the target problem to the centralized resource pool. The centralized resource pool runs LLM according to similar Figure 1 The process converts the target problem described in natural language into the target code described in programming language, and then returns the target code to the scheduling device, which then transmits the target code to the client.
[0123] It should be noted that if LLM is running on a scheduling device, the scheduling device runs LLM in a similar manner. Figure 1 The process of converting the target problem described in natural language into the target code described in programming language, and then transmitting the target code to the client.
[0124] The functions of the scheduling device can be implemented by software or hardware.
[0125] As an example of a software functional unit, the scheduling device may include code running on a computing instance. The computing instance may include at least one of a physical host (computing device), a virtual machine, and a container. Furthermore, the computing instance may be one or more. For example, the scheduling device may include code running on multiple hosts / virtual machines / containers. It should be noted that the multiple hosts / virtual machines / containers used to run the code may be distributed in the same region or in different regions. Furthermore, the multiple hosts / virtual machines / containers used to run the code may be distributed in the same availability zone (AZ) or in different AZs, each AZ including one data center or multiple geographically close data centers. Typically, a region may include multiple AZs.
[0126] Similarly, multiple hosts / virtual machines / containers running the code can be distributed within the same virtual private cloud (VPC) or across multiple VPCs. Typically, a VPC is set up within a region. Inter-region communication between two VPCs within the same region, or between VPCs in different regions, requires a communication gateway within each VPC to interconnect the VPCs.
[0127] As an example of a hardware functional unit, the scheduling device may include at least one computer device, such as a server. Alternatively, the scheduling device may be implemented using an application-specific integrated circuit (ASIC) or a programmable logic device (PLD). The PLD may be a complex programmable logical device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof.
[0128] The multiple computer devices included in the scheduling device can be distributed in the same area or in different areas. The multiple computer devices included in the scheduling device can be distributed in the same AZ or in different AZs. Similarly, the multiple computer devices included in the scheduling device can be distributed in the same VPC or in multiple VPCs. The multiple computer devices can be any combination of computer devices such as servers, ASICs, PLDs, CPLDs, FPGAs, and GALs.
[0129] The cloud system provided in the embodiment of the present application may be a cloud service system. In the cloud service system, for example Figure 2B As shown in Figure 1, the cloud service system includes a cloud platform and basic resources. The cloud platform includes a cloud platform manager, Figure 2A The scheduling device in can be Figure 2B The cloud platform manager in the cloud platform manager. The basic resources may include multiple servers, each of which may include multiple resource nodes.
[0130] exist Figure 2A and Figure 2B The resource node in the example may be a computing device card or a virtual machine (VM). The computing device card may be at least one of a central processing unit (CPU), a graphics processing unit (GPU), and a neural network processing unit (NPU).
[0131] The cloud platform manager can schedule the target issue and return the target code to the tenant.
[0132] The cloud system provided in the embodiment of the present application may be a data center. In the data center, for example Figure 2C As shown, a data center includes a data center management platform, an internal data center network, and multiple servers. Each server consists of a hardware layer and a software layer. The hardware layer includes memory, network interface cards (NICs), processors, and disks, all connected via a bus. The hardware layer provides the hardware resources necessary for the virtual machines in the software layer to run. The software layer includes a host operating system and multiple virtual machines. The host operating system may include a data center management platform client, which can interact with the data center management platform.
[0133] Virtualization technology, which mainly consists of computing virtualization and input / output (I / O) virtualization, shares a physical server with multiple tenants at the granularity of virtual machines, allowing tenants to use physical resources conveniently and flexibly under the premise of secure isolation, and can greatly improve the utilization of physical resources.
[0134] Compute virtualization is the process of making a server's computing resources, such as processors and memory, available to virtual instances. For example, virtual machines (VMs) are virtual instances. In other scenarios, these virtual instances are containers or bare metal servers.
[0135] Figure 2C Each server obtains multiple virtual machines through virtualization technology, and each virtual machine can be understood as a resource node. Figure 2A The scheduling device in can be Figure 2C Data center management platform in .
[0136] Among them, virtual machines can also be called cloud servers (Elastic Compute Service, ECS) or elastic instances (different cloud service providers have different names).
[0137] The data center management platform provides an access interface (such as a user interface or application programming interface (API)). Tenants can use a client to remotely access the access interface, register an account and password on the data center management platform, and log in to the data center management platform. After the data center management platform successfully authenticates the account and password, the tenant can send a target problem to the data center management platform through the client. The data center management platform then dispatches the target problem and returns the target code to the tenant.
[0138] above Figure 1 The code generated by the illustrated code generation process is affected by the language gap between natural language and programming language, and its accuracy is not high. Based on this, an embodiment of the present application provides a prompt-based code generation method. In the process of generating target code in programming language based on a target problem in natural language, prompt information of the target problem can be obtained by searching the knowledge base and the knowledge enhancement module, and then the prompt information and the target problem are used to generate the target code, which can improve the accuracy of the target code. Among them, the search knowledge base can use a retrieval model.
[0139] The solutions provided in the embodiments of this application involve a knowledge base, a retrieval model, and code generation, which are introduced below respectively.
[0140] 1. Knowledge base (KB);
[0141] In this application, the knowledge base may include mapping relationships between multiple code segments and structured views. Each mapping relationship may be obtained by constructing a corresponding structured view using a different code segment. Different mapping relationships are marked by different indexes, such as: code segment c i The corresponding structured view g iThe index of is i.
[0142] The knowledge base construction process can be a process of using code segments to generate corresponding structured views, then establishing a mapping relationship between the code segments and the corresponding structured views, and then adding the mapping relationship to the knowledge base. For example, using a code generation tool (CodeGRAG) to analyze the syntax and control information of a code segment and extract its structured view. The relationship can be expressed as: g i ←GraphExtractor(c i ), where c i Represents the i-th code segment, g i Represents the corresponding structured view. Then create a tuple for the code segment and the corresponding structured view <c i ,g i >, the tuple can also be changed to a triple<i,c,g> . <c i ,g i >Inserting into a knowledge base can be expressed as: KB.append( <c i ,g i >).
[0143] Among them, the structured view can be that CodeGRAG analyzes the code based on the abstract syntax tree, data flow graph, and control flow graph, and uses read and write signals to integrate the data flow graph and control flow graph to obtain a structured view, while retaining the unique information of the code from a semantic and logical perspective.
[0144] For the process of obtaining data flow graphs and control flow graphs based on code segments and integrating them to obtain structured views, please refer to Figure 3 301 to 303 for understanding.
[0145] 301. The process of obtaining a data flow diagram based on a code segment: Figure 3 As shown, CodeGRAG analyzes code segments, abstracting "nodes" from them. Then, based on data flow relationships, it connects these nodes, essentially adding "edges" to the nodes, resulting in a data flow graph. In a data flow graph, nodes represent operations, and edges represent data flows. There are two main types of edges in a data flow graph: operation edges and function edges. Operation edges connect the node to be operated on and the node that receives the result of the operation; function edges indicate the data flow of function calls and returns. These edges connect both non-temporary operations and temporary operations, which reference variables and constants explicitly present in the source code and variables present during execution, respectively.
[0146] 302. The process of obtaining the control flow graph based on the code segment: Figure 3As shown in the figure, CodeGRAG analyzes the code segment, abstracts "nodes" from the code segment, and then establishes connections between the nodes according to the order of operation execution, that is, adding "edges" to the nodes to obtain a control flow graph. In the control flow graph, the nodes are the operations in the source code, including standard operations, function calls and returns. The edges represent the execution order of the operations. There are several types of edges in the control flow graph, namely: (1) conditional edges, which are used to represent the conditions for loops, branches, iterations, etc. in the code segment; (2) iteration edges, which are used to represent the relationship between the starting point and the end point of an iteration in the code segment; (3) function edges, which are used to represent the relationship between function calls and returns; (4) execution edges, which are used to represent the order in which operations are executed.
[0147] 303. Integrating data flow graphs and control flow graphs to obtain a structured view: Based on the above data flow graphs and control flow graphs, CodeGRAG uses read and write signals to integrate the two types of graphs, simultaneously modeling the characteristics of the code from a semantic and logical perspective to obtain a structured view of the code segment. This structured view can be used as expert knowledge in the code domain and injected into the LLM.
[0148] The solution provided in the embodiment of the present application represents code segments by means of data flow graphs and control flow graphs, and then integrates the structured views obtained by the data flow graphs and the control flow graphs, which can more effectively retain the effective information of the code language at both the semantic and logical levels, thereby improving the accuracy of subsequent code generation.
[0149] 2. Retrieval model (knowledge retriever);
[0150] The purpose of a retrieval model is to retrieve the most similar mappings from a knowledge base to a target question described in natural language. Because the code snippets in the knowledge base are written in programming languages, and they can be of different types (e.g., C, C++, Python, etc.), a crucial task for the retrieval model is to retrieve programming languages and / or structured views from the knowledge base using natural language to obtain optimal search results.
[0151] Therefore, the retrieval model provided in the embodiments of the present application has the following capabilities: (1) the ability to process target questions in natural language; (2) the ability to align modalities in different languages; and (3) the ability to align modalities within structured views. These are described below:
[0152] (1) Ability to process target questions in natural language;
[0153] The retrieval model can extract key information from the target problem. This key information can include the problem description and function description of the target problem. The problem description can describe the task to be completed by the target code, while the function description can be a function declaration, such as the function name and input variables. The retrieval model can extract the problem description of the target problem through a regularized approach and then concatenate it with the function declaration as the query content (also called key information), which can include the function and input format of the expected code segment.
[0154] (2) the ability to align modalities across different languages;
[0155] In the embodiment of the present application, the alignment between modalities of different languages is achieved by designing the retrieval function in the retrieval model. The present application uses a first encoder (which can be a model encoder) and a second encoder (which can be a graph neural network) To construct the retrieval function, the retrieval function can be expressed as:
[0156]
[0157]
[0158]
[0159] Among them, Q p Q is the question description information in the query content, c To query the functional description information in the content, V c is the code segment in the knowledge base, V g is the structured view in the knowledge base, V c and V g Belong to the same mapping relationship. h Q Indicates that the first encoder is used to Q p and Q c The first vector obtained by vectorized encoding. Indicates that the first encoder is used to V c The code vector obtained by vectorized encoding, Indicates the use of the second encoder to V g The view vector obtained by vectorized encoding, h V Represents the code vector and / or view vector The second vector obtained by the transformation. A smaller Distance value indicates a better alignment effect.
[0160] Among them, the first encoder Pre-trained models can be used to align natural language and programming language, first encoder It can be any encoder that can perform vectorized encoding of natural languages and programming languages, such as CodeT5+ or UnixCoder.
[0161] CodeT5+ is an encoder-decoder model tailored for various code comprehension and generation tasks, capable of running in different modes (encoder-only, decoder-only, and encoder-decoder). It utilizes a range of pre-training tasks, including span denoising, code causal language modeling, text-code contrastive learning, matching, and causal language modeling (CLM). The CLM task requires predicting the next token in a sequence given the previous token. Its goal is to teach the model to understand the basic structure of language and generate coherent, natural text. CodeT5+ can effectively align text and code data to improve retrieval performance.
[0162] UnixCoder is a cross-modal pre-trained model optimized for code comprehension and generation tasks, featuring a multi-layer Transformer structure with masked attention matrices. It outperforms traditional unified encoder-decoder models in autoregressive tasks such as code completion. UniXcoder uses multimodal input, including code comments and abstract syntax trees (ASTs), to enhance its code representation capabilities. It employs three types of language modeling tasks: masked language modeling, unidirectional language modeling, and denoising objectives. In addition, it introduces two advanced pre-training tasks: multimodal contrastive learning and cross-modal generation, which improve the semantic representation of code snippets.
[0163] Second encoder The semantic and logical information of the code segment can be saved. The semantic and structural information of the code segment can be integrated into each node representation of the structured view. The node representation refers to the result of node vectorization. Each node and the edge The representation is first expressed by the corresponding The vectors of encoded node text and edge text are initialized. Then the initialized and the edge The following series of relationships determine The specific process may include:
[0164]
[0165]
[0166]
[0167]
[0168]
[0169] Then a global attention-based pooling mechanism is applied to obtain a structured representation:
[0170]
[0171] in It can represent the amount of information (message) on the edge from node i to node j in layer l, W (l) Can represent the lth layer node Jingbian The weight matrix for passing messages, represents the vector of node i at the (l-1) layer, represents the vector of the edge from node i to node j in the (l-1) layer, represents the vector of node j at layer (l-1). In a graph neural network, the information of layer l comes from layer (l-1). The above multiple relations represent multiple intermediate quantities, such as: Determine the vector of node j in layer l process, get The vector g of the structured view can be obtained through the pooling mechanism. The structured view can be understood as simplifying a fully connected network layer to a transformation function, where the gated network f gate Represents a gated network that can be used to calculate the attention coefficient, f feat It is a feature conversion network that can convert node information into the same feature space.
[0172] For more information about intermodal alignment, see Figure 4 As shown, it may include 401. First inter-modal alignment: alignment of query content with code segments, 402. Second inter-modal alignment: alignment of code segments with structured views.
[0173] 401. Alignment of query content and code snippet;
[0174] The first inter-modal alignment is to align the first vector of the key information with the code vector of the code segment; the key information here is the query content.
[0175] The ability to align the first modality can be obtained by training the first encoder using the first training sample, where the first training sample has a positive pair of Negative pair , where D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0176] 402. Alignment of code snippets with structured views;
[0177] The second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view. The second inter-modal alignment can be obtained by training the second encoder using the second training sample, and the positive alignment of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0178] In the embodiments of the present application, natural language and programming language, as well as programming language and structured view are aligned through vectors, which can effectively generalize the differences between different languages and align the information described in different languages, thereby improving the accuracy of the generated target code.
[0179] (3) Intra-modal alignment capability for structured views;
[0180] For more information about alignment within the modal, see Figure 4 As shown in step 403, intra-modal alignment refers to internal alignment of structured views. A specific solution may be: by comparing the first structured view with the second structured view, the structural information of the first structured view is preserved, and the second structured view is a damaged view obtained by destroying the edge of the first structured view.
[0181] For the relationship between the first structured view and the second structured view, please refer to Figure 5 Understand. Figure 5 As shown, the second structured view can be obtained by destroying the edge from the rectified linear unit (ReLU) node to the Add node in the first structured view. Of course, Figure 5 In the example, only one edge is destroyed. In fact, multiple edges can also be destroyed.
[0182] The intra-modal alignment is implemented based on the self-supervised model in the second encoder, which is obtained using the third training sample. The positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0183] In the embodiment of the present application, the structural information of the first structured view can be effectively retained through intra-modal alignment.
[0184] 3. Code generation;
[0185] The code generation process provided in the embodiment of this application can be referred to Figure 6 Understand. Figure 6 As shown in Figure 1, a knowledge base (KB), a retrieval model, a knowledge enhancement module, and a code generation model can be configured in the cloud system. The knowledge base can include multiple tuples. <c i ,g i >, the value of index i in each tuple is different.
[0186] During the code generation process, the retrieval model can obtain the target problem, which is the function to be achieved by the target code described in natural language; the process of the retrieval model obtaining the target problem can be that the scheduling device in the cloud system receives the target problem and then schedules the target problem to the retrieval model.
[0187] After the retrieval model obtains the target question, it can perform the following process:
[0188] 601. Extract key information from the target problem.
[0189] The key information may include the problem description information Q of the target problem. p and functional description information Q c .
[0190] 602. Retrieve a first index from the knowledge base based on the key information and the knowledge base.
[0191] The step 602 may include: vectorizing the problem description information and the function description information. The vectorization process may be performed using a first encoder. Problem description information Q p and functional description information Q cProcessing is performed to obtain the first vector
[0192] Get bigrams from the knowledge base <c i ,g i >Corresponding code segment V c and V g , for V c Using the first encoder Perform vectorized encoding to obtain code vector and / or use a second encoder Pair and V g Perform vectorized encoding to obtain the view vector Then the code vector and / or view vector Transform to get the second vector Further use the distance relationship The distance value is calculated, and the index corresponding to the two-tuple of the first distance is determined as the first index. The first distance is less than the second distance, and the second distance is the distance obtained by the first vector and the mapping relationship in the knowledge base other than the first mapping relationship (the mapping relationship that obtains the first distance).
[0193] The process can also be expressed as:
[0194] q←QueryExtractor(x)
[0195]
[0196] Where x represents the target question, q represents the key information / query content, and QueryExtractor(x) extracts the key information from the target question. Retriever(q,KB) uses the query content and the knowledge base to determine the index of the first mapping relationship with the highest similarity (Top 1) to the query content, i.e., the first index.
[0197] 603. Output the first index to the knowledge enhancement module.
[0198] The knowledge enhancement module can use the first index to query the knowledge base and obtain the first mapping relationship corresponding to the first index from the knowledge base. This process can be described as: i ,g i ←KB[i], which means using index i to obtain a binary from the knowledge base <c i ,g i >.
[0199] The knowledge enhancement module can determine prompt information based on the first mapping relationship, and the prompt information can be the meta-graph information of the first structured view in the first mapping relationship, the first structured view in the first mapping relationship, or the first mapping relationship; wherein the meta-graph information includes the node information and / or edge information in the first structured view.
[0200] The knowledge enhancement module inputs the prompt information into the code generation model (LLM), and the retrieval model can input the target question / key information into the code generation model (LLM). In this way, the code generation model (LLM) can generate the target code based on the target question / key information + prompt information. The process can be expressed as: result←LLM(x,g i ), or, result←LLM(q,g i ) where x represents the target question, q represents the key information, and g i Indicates prompt information, which is consistent with the structured view g i Related, it can be from the structured view g i The meta-graph information extracted from the structured view g i itself, or contains the structured view g i The first mapping relationship is represented by "result", and the target code is represented by "result". Regardless of whether the prompt information is the meta-graph information of the first structured view, the first structured view, or the first mapping relationship, the prompt information includes structured information about the target code requirements. This helps the code generation model correct deviations based on the structured information when generating code for the target problem, thereby improving the accuracy of the generated target code.
[0201] For more information about target issues, key information, prompt information, and corresponding target codes, please refer to Figure 7 The examples shown are for understanding.
[0202] like Figure 7 As shown, the target problem can be: a function is required, which takes two numbers as input and returns their sum. Key information can include problem description information (such as: two numbers, sum), and functional description information can be a function declaration, such as: def add. The prompt information of the target problem, taking the metagraph information of the structured view as an example, can be two nodes, 2 function edges, and 4 execution edges. In addition to the prompt information introduced here, system prompt information can also be input to LLM. The system prompt information can specify the programming language, such as according to the task requirements and function declaration, please use Python language to complete the function continuation. In this way, LLM can generate the target code shown below according to the target problem and prompt information.
[0203] ```Python
[0204] def add_numbers(num1,num2):
[0205] return num1+num2
[0206] ```
[0207] As can be seen from the above description, the code generation solution provided in the embodiment of the present application can obtain a first mapping relationship related to the target problem through the knowledge base, and the first mapping relationship includes a first structured view and a corresponding code segment. In this way, when generating the target code for the target problem, it is possible to input not only the target problem into the code generation model, but also prompt information associated with the first mapping relationship. This prompt information can stimulate the contextual learning ability of the code generation model, improve the code generation effect, and improve the accuracy of the generated target code.
[0208] It should be noted that, in the above code generation process, if the retrieval model can directly obtain the first mapping relationship, the knowledge enhancement module may not be required, and the retrieval model determines the prompt information according to the first mapping relationship and inputs it to the LLM.
[0209] The solution provided in the embodiment of the present application is retrieval augmented generation technology. This technology combines the advantages of information retrieval and code generation models, aiming to improve the efficiency and output quality of the code generation model. By introducing a retrieval mechanism, the code generation model can obtain more accurate and relevant information from large-scale data, thereby effectively guiding the code generation process. This integrated approach can not only speed up the generation speed, but also improve the overall performance and user experience of the system, bringing more flexible and intelligent solutions to various application scenarios. Retrieval augmented generation technology has shown a huge performance improvement in promoting large models to handle complex and time-related tasks, and has made good use of the context learning ability of large models to guide large models to perform enhanced generation.
[0210] Under this architecture, given an input request / target problem x, the process of generating the output target code y is as follows:
[0211] y=G(z,x)
[0212] Among them, G is the code generation model, and z is the relevant information obtained from retrieval and knowledge enhancement, such as prompt information.
[0213] The retrieval and knowledge enhancement process can be expressed as:
[0214] z=R(x)
[0215] Among them, R is the retrieval model + knowledge enhancement module, which converts the input request x into the retrieval and knowledge enhancement result z.
[0216] In summary, retrieval-enhanced generation can be expressed as:
[0217] y=G(R(x),x)
[0218] The main application scenario of this application is code generation based on a large model from natural language to generated code segments. When using a large model based on natural language to generate code segments for code generation, users can simply describe the functions or logic they want to implement, and the model will automatically generate the corresponding code. This technology makes programming more intuitive and easy. Even without in-depth programming knowledge, code can be generated quickly. Moreover, the model can generate code in different types of programming languages, and the code accuracy is very high.
[0219] To verify the superiority of the solution provided in this application over the existing technology, the developers used the widely used HumanEval-X dataset to evaluate the code generation tool (CodeGRAG) proposed in this application. HumanEval-X is a multi-language code benchmark dataset.
[0220] In the experiment, a greedy decoding strategy is used for generation, and the evaluation indicator is Pass@1.
[0221] Where c is the number of tests that passed in the generated code. During the experiment, the developers evaluated the following data:
[0222] Multi-language code generation capabilities for LLMs with less than 10 billion parameters: GPT-J, CodeGen-Multi, and InCoder;
[0223] 10-20 billion parameter LLMs: CodeGeeX, CodeGen-Multi, CodeGen-Mono, StarCoder, WizardCoder, Pangu-Coder2;
[0224] Closed-source GPT-3.5LLM.
[0225] By evaluating the LLM with different parameter scales, we can get the following results: Figure 8 The experimental results shown are from Figure 8 From the comparison of the experimental results, it can be seen that the pass rate is 64.02 when using C++ programming language, and the pass rate is 77.44 when using Python programming language. The pass rate of CodeGRAG in the last row is higher than the pass rate of other existing models in the same column when using C++ programming language or Python programming language.
[0226] It can be seen that CodeGRAG can provide performance gains based on the base model, which also verifies the effectiveness of the proposed framework for retrieval-enhanced generation based on structured views of code for code generation.
[0227] In addition, the model enhanced with the structured view proposed by CodeGRAG can outperform the model generated by enhancing the original code snippet. The results show that the structured view can summarize useful structural grammatical information, filter out noise, and provide more informative knowledge hints than the original code snippet.
[0228] CodeGRAG can provide cross-language enhancements, i.e., it can use Python's knowledge base to enhance C++ code generation, and it can use C++'s knowledge base to enhance Python code generation. This verifies that the proposed structured view can abstract high-level and inherent information of code blocks (e.g., control flow and data flow for solving specific problems), which can be shared across different programming languages to solve the same problem.
[0229] In addition, the experiments to verify the results also involve using CodeT5 or UnixCoder as the first encoder introduced above. CodeT5 or UnixCoder have been trained with multilingual data and used for multilingual search. The experimental results obtained through the experiments can be found in Figure 9 Understand.
[0230] from Figure 9 Comparing the last row of data of CodeT5 and the last row of data of UnixCoder, we can see that when using C++ programming language, the pass rate of CodeT5 is 62.20, while the pass rate of UnixCoder is 64.02, and the pass rate of UnixCoder is higher than that of CodeT5; when using Python programming language, the pass rate of CodeT5 is 76.83, while the pass rate of UnixCoder is 77.44, and the pass rate of UnixCoder is higher than that of CodeT5.
[0231] from Figure 9 The experimental results shown here demonstrate that different first encoders offer different enhancement performance. Specifically, cross-language enhancement performance improves when using UnixCoder as the encoder compared to CodeT5. This is because UnixCoder captures the structure of the abstract syntax tree, while CodeT5 does not. This further validates the effectiveness of including structural information. Furthermore, a comparison with rows containing NA reveals that, while cross-language retrieval accuracy is higher, directly using raw code as a cross-language hint can bias model generation, while providing a structured view of the code improves model performance.
[0232] In addition, the developers also conducted experiments on the impact of different components of the structured view (nodes, edges) on the results. The developers adjusted the content of the components of the structured view and input them into the LLM as external knowledge. Specifically, the developers studied the information contained in the node names, edge names, and topology structure. The results are as follows: Figure 10A As shown in Figure 2. Where edge type refers to the flow type between operands (read, write, etc.), node type refers to the type of operand (DeclStmt, temp, etc.), node name refers to the name of the intermediate variable, and topological information refers to the specific number of edges of different types. The experimental results show that: (1) edge features are the most important in constructing the structural view of the code block, (2) node type is the most expressive in representing operand information, and (3) the topological structure of the structured view also provides additional information.
[0233] Discussion on the application scope of structured views. Although the proposed structured views can effectively represent the information of the code, the flexibility and convenience of applying structured views are crucial for the wide application of the proposed method. Therefore, the developers further verified the difficulty of the CodeGRAG structured view extraction process. First, the extraction process of the structured view is a front-end process. Therefore, this extraction process is applicable to a wide range of codes, even erroneous codes. Developers can also use convenient tools to reformat the code and improve the pass rate of the extraction process. In addition, the developers gave the rate (Extraction Rate) at which the code generation results of the large model can pass the structured view extraction process, such as Figure 10B and Figure 10C shown.
[0234] from Figure 10B and Figure 10C The experimental results show that even when the Pass@1 ratio of the generated code is low, the code extraction rate through the structured view is still high. This demonstrates the wide applicability of this method. Furthermore, since directly using code as RAG knowledge can still provide performance improvements for LLM code generation, multiple views (including original code and structured representations) can be used as retrieval knowledge bases.
[0235] It's also important to note that this application describes the use of LLM combined with structured information to improve code generation performance. For other generation tasks, as long as some structured information is available, meta-graph information can also be extracted to enhance performance. This means that the solutions and ideas presented in this application are still applicable to other tasks with graph structures.
[0236] The above introduces the code generation scheme, the knowledge retrieval scheme during the code generation process, and the knowledge base construction scheme. The following introduces the computer device provided in the embodiment of the present application in conjunction with the accompanying drawings.
[0237] like Figure 11 As shown, the computer device 110 provided in this embodiment of the application includes:
[0238] A first processing unit 1101 is configured to determine a first index for accessing a knowledge base based on a target question, wherein the target question is a function to be implemented by a target code described in a natural language, and the knowledge base includes a mapping relationship between code segments and structured views, wherein the structured views are used to represent semantic and logical relationships between the code segments;
[0239] The second processing unit 1102 is configured to query the knowledge base according to the first index to obtain a first mapping relationship associated with the first index;
[0240] The third processing unit 1103 is configured to input the target problem and the prompt information into the code generation model to obtain a target code for the target problem, wherein the prompt information is associated with the first mapping relationship.
[0241] Optionally, the prompt information is meta-graph information of the first structured view in the first mapping relationship, the first structured view in the first mapping relationship, or the first mapping relationship; wherein the meta-graph information includes node information and / or edge information in the first structured view.
[0242] Optionally, the first processing unit 1101 is specifically configured to extract key information from the target problem; determine a first index based on the key information and the knowledge base, where the first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity to the key information.
[0243] Optionally, the first processing unit 1101 is specifically configured to encode the problem description information and the function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0244] Optionally, the second processing unit 1102 is used to perform first inter-modality alignment on the key information and the first code segment in the first mapping relationship, perform second inter-modality alignment on the first code segment and the first structured view, and perform intra-modality alignment on the first structured view to determine the first index.
[0245] Optionally, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0246] Optionally, the intra-modal alignment is performed by comparing the first structured view and the second structured view to preserve the structural information of the first structured view, and the second structured loss is a damaged view obtained by destroying the edge of the first structured view.
[0247] Optionally, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0248] Optionally, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0249] Optionally, the view vector of the first structured view is obtained by encoding the first structured view using a second encoder, and the second encoder is obtained by training using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0250] Optionally, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, the self-supervised model is obtained using a third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoderi The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
[0251] Optionally, the structured view in the knowledge base is obtained by integrating the data flow graph and control flow graph of the corresponding code segment, where the nodes in the data flow graph represent the operations in the code segment and the edges represent the data flow; the nodes in the control flow graph are the operations in the code segment and the edges represent the execution order of the operations.
[0252] During the knowledge retrieval process, each unit of the computer device 110 may further perform the following steps:
[0253] The first processing unit 1101 is configured to obtain a target problem, where the target problem is a function to be implemented by a target code described in a natural language;
[0254] A second processing unit 1102 is configured to extract key information from the target question;
[0255] The third processing unit 1103 is used to retrieve a first index from the knowledge base based on the key information and the knowledge base, where the first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity to the key information. The knowledge base includes a mapping relationship between code segments and structured views, and the structured view is used to represent the semantics and logical relationships of the code segments.
[0256] Optionally, the second processing unit 1102 is configured to encode the problem description information and the function description information of the target problem to obtain a first vector of key information; wherein the function description information is a constraint condition for extracting the key information.
[0257] Optionally, the third processing unit 1103 is used to perform first inter-modality alignment on the key information and the first code segment in the first mapping relationship, perform second inter-modality alignment on the first code segment and the first structured view, and perform intra-modality alignment on the first structured view to determine the first index.
[0258] Optionally, the first inter-modal alignment is to align the first vector of the key information with the code vector of the first code segment; the second inter-modal alignment is to align the code vector of the first code segment with the view vector of the first structured view.
[0259] Optionally, the intra-modal alignment is performed by comparing the first structured view and the second structured view to preserve the structural information of the first structured view, and the second structured loss is a damaged view obtained by destroying the edge of the first structured view.
[0260] Optionally, the first index is determined by a first vector and a second vector, a first distance obtained by a distance relationship between the first vector and the second vector is less than a second distance, and the second distance is a distance obtained by the first vector and a mapping relationship in the knowledge base other than the first mapping relationship; wherein the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
[0261] Optionally, the code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
[0262] Optionally, the view vector of the first structured view is obtained by encoding the first structured view using a second encoder, and the second encoder is obtained by training using a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
[0263] Optionally, the intra-modal alignment is implemented based on a self-supervised model in the second encoder, the self-supervised model is obtained using a third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j′ is the damaged structured view of the j-th sample.
[0264] During the knowledge base construction process, each unit of the computer device 110 may further perform the following steps:
[0265] A first processing unit 1101 is configured to generate a data flow graph and a control flow graph for a first code segment; wherein the nodes in the data flow graph represent operations in the code segment and the edges represent data flow; and the nodes in the control flow graph represent operations in the code segment and the edges represent the execution order of the operations;
[0266] The second processing unit 1102 is configured to integrate the data flow graph and the control flow graph to obtain a first structured view of the first code segment;
[0267] The third processing unit 1103 is configured to establish a first mapping relationship between the first code segment and the first structured view through the first index; and add the first mapping relationship to the knowledge base, where different mapping relationships in the knowledge base are marked by different indexes.
[0268] The computer device provided in the embodiment of the present application can be a scheduling node, a resource node, a collection of resource nodes, or a computing device card in a resource node in a cloud system, such as a CPU, GPU, or NPU. Of course, the computer device can also be an independent server or terminal device.
[0269] The functions of the various units of the computer device 110 introduced above can be understood by referring to the introduction of the previous method embodiment part, and will not be repeated here.
[0270] Figure 12 A possible logical structure diagram of a computer device provided in an embodiment of the present application. Figure 12 As shown, the computer device 120 provided in an embodiment of the present application includes: a processor 1201, a communication interface 1202, a memory 1203 and a bus 1204. The processor 1201, the communication interface 1202 and the memory 1203 are interconnected via the bus 1204. In an embodiment of the present application, the processor 1201 is used to control and manage the actions of the computer device 120. For example, the processor 1201 is used to execute the process of code generation, knowledge retrieval or knowledge base construction. The communication interface 1202 is used to support the computer device 120 to communicate. For example, the communication interface 1202 can execute the process of obtaining the target problem. The memory 1203 is used to store the program code and data of the computer device 120.
[0271] Among them, the processor 1201 can be a central processing unit, a general-purpose processor, a digital signal processor, an application-specific integrated circuit, a field programmable gate array or other programmable logic device, a transistor logic device, a hardware component or any combination thereof. It can implement or execute the various exemplary logic blocks, modules and circuits described in conjunction with the disclosure of this application. The processor can also be a combination that implements computing functions, such as a combination of one or more microprocessors, a combination of a digital signal processor and a microprocessor, and so on. The bus 1204 can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 12 Only one thick line is used in the diagram, but this does not mean that there is only one bus or one type of bus.
[0272] In another embodiment of the present application, a computer-readable storage medium is further provided. The computer-readable storage medium stores computer-executable instructions. When the processor of the computer device executes the computer-executable instructions, the computer device executes the above-mentioned Figures 3 to 10C Steps executed by a computer device in the process.
[0273] In another embodiment of the present application, a computer program product is further provided. The computer program product includes a computer program code. When the computer program code is executed on a computer, the computer device executes the above Figures 3 to 10C Steps executed by a computer device in the process.
[0274] In another embodiment of the present application, a chip system is further provided, which includes one or more interface circuits and one or more processors; the interface circuits and the processors are interconnected via lines; the interface circuits are used to receive signals from the memory of the computer device and send signals to the processors, the signals including computer instructions stored in the memory; when the processors execute the computer instructions, the computer device executes the aforementioned Figures 3 to 10C The steps executed by the computer device in the chip. In one possible design, the chip system may also include a memory for storing program instructions and data necessary for controlling the device. The chip system may be composed of a chip or may include a chip and other discrete devices.
[0275] In the several embodiments provided in this application, it should be understood that the disclosed systems, devices and methods can be implemented in other ways. For example, the device embodiments described above are merely schematic. For example, the division of units is only a logical function division. In actual implementation, there may be other division methods, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be an indirect coupling or communication connection through some interface, device or unit, which can be electrical, mechanical or other forms.
[0276] The units described as separate components may or may not be physically separate, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed across multiple network units. Some or all of these units may be selected to achieve the purpose of this embodiment according to actual needs.
[0277] In addition, the functional units in the various embodiments of the present application may be integrated into a single processing unit, or each unit may exist physically separately, or two or more units may be integrated into a single unit. The aforementioned integrated units may be implemented in whole or in part through software, hardware, firmware, or any combination thereof.
[0278] When software is used to implement the integrated unit, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present application is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from one website, computer, server or data center to another website, computer, server or data center via a wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) method. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrations. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid state drive (SSD)).
Claims
1. A code generation method, characterized in that: include: Determining a first index for accessing a knowledge base based on a target question, wherein the target question is a function to be implemented by a target code described in a natural language, the knowledge base including a mapping relationship between code segments and structured views, the structured views being used to represent semantics and logical relationships between the code segments; querying the knowledge base according to the first index to obtain a first mapping relationship associated with the first index; The target problem and prompt information are input into a code generation model to obtain the target code of the target problem, and the prompt information is associated with the first mapping relationship.
2. The method according to claim 1, characterized in that The prompt information is the meta-graph information of the first structured view in the first mapping relationship, the first structured view in the first mapping relationship, or the first mapping relationship; wherein the meta-graph information includes node information and / or edge information in the first structured view.
3. The method according to claim 1 or 2, characterized in that Determining a first index for accessing a knowledge base according to the target question includes: Extract key information from the target problem; The first index is determined according to the key information and the knowledge base, where the first index is an index of the code segment and / or structured view in the knowledge base that has the highest similarity to the key information.
4. The method according to claim 3, characterized in that The extracting key information from the target problem includes: The problem description information and the function description information of the target problem are encoded to obtain a first vector of the key information; wherein the function description information is a constraint condition for extracting the key information.
5. The method according to claim 4, characterized in that The determining the first index according to the key information and the knowledge base includes: A first inter-modality alignment is performed on the key information and the first code segment in the first mapping relationship, a second inter-modality alignment is performed on the first code segment and the first structured view, and an intra-modality alignment is performed on the first structured view to determine the first index.
6. The method according to claim 5, characterized in that The first inter-modality alignment is to align the first vector of the key information with the code vector of the first code segment; The second inter-modality alignment is that the code vector of the first code segment is aligned with the view vector of the first structured view.
7. The method according to claim 5 or 6, characterized in that The intra-modal alignment is performed by comparing the first structured view and the second structured view to preserve the structural information of the first structured view, and the second structured loss is a damaged view obtained by destroying the edge of the first structured view.
8. The method according to any one of claims 4 to 7, characterized in that: The first index is determined by the first vector and the second vector, the first distance obtained by the distance relationship between the first vector and the second vector is less than the second distance, and the second distance is the distance obtained between the first vector and the mapping relationship in the knowledge base other than the first mapping relationship; wherein, the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
9. The method according to any one of claims 6 to 8, characterized in that: The code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
10. The method according to any one of claims 6 to 9, characterized in that: The view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is obtained by training with a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
11. The method according to claim 7, characterized in that The intra-modality alignment is implemented based on a self-supervisory model in the second encoder, wherein the self-supervisory model is obtained using a third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
12. The method according to any one of claims 1 to 11, characterized in that The structured view in the knowledge base is obtained by integrating the data flow graph and control flow graph of the corresponding code segment, wherein the nodes in the data flow graph represent the operations in the code segment and the edges represent the data flow; the nodes in the control flow graph are the operations in the code segment and the edges represent the execution order of the operations.
13. A retrieval method, characterized in that: include: Obtain a target problem, where the target problem is a function to be implemented by a target code described in a natural language; Extract key information from the target problem; Based on the key information and the knowledge base, a first index is retrieved from the knowledge base, where the first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity to the key information. The knowledge base includes a mapping relationship between code segments and structured views, and the structured view is used to represent the semantics and logical relationships of the code segments.
14. The method according to claim 13, characterized in that The extracting key information from the target problem includes: The problem description information and the function description information of the target problem are encoded to obtain a first vector of the key information; wherein the function description information is a constraint condition for extracting the key information.
15. The method according to claim 14, characterized in that The step of retrieving a first index from a knowledge base according to the key information and the knowledge base includes: A first inter-modality alignment is performed on the key information and the first code segment in the first mapping relationship, a second inter-modality alignment is performed on the first code segment and the first structured view, and an intra-modality alignment is performed on the first structured view to determine the first index.
16. The method according to claim 15, characterized in that The first inter-modality alignment is to align the first vector of the key information with the code vector of the first code segment; The second inter-modality alignment is that the code vector of the first code segment is aligned with the view vector of the first structured view.
17. The method according to claim 15 or 16, characterized in that The intra-modal alignment is performed by comparing the first structured view and the second structured view to preserve the structural information of the first structured view, and the second structured loss is a damaged view obtained by destroying the edge of the first structured view.
18. The method according to any one of claims 14 to 17, characterized in that: The first index is determined by the first vector and the second vector, the first distance obtained by the distance relationship between the first vector and the second vector is less than the second distance, and the second distance is the distance obtained between the first vector and the mapping relationship in the knowledge base other than the first mapping relationship; wherein, the second vector is obtained by transforming the code vector of the first code segment and / or the view vector of the first structured view.
19. The method according to any one of claims 16 to 18, characterized in that: The code vector of the first code segment is obtained by encoding the first code segment by a first encoder, and the first encoder is trained using a first training sample based on a pre-trained model, and the positive pair of the first training sample is Negative pair Among them, D train is the training set, is the vector of the natural language description of the problem of the i-th sample, h i V is the vector of the code segment of the i-th sample, is the vector of the code segment of the jth sample.
20. The method according to any one of claims 16 to 19, characterized in that: The view vector of the first structured view is obtained by encoding the first structured view with a second encoder, and the second encoder is obtained by training with a second training sample based on a graph neural network, and the positive pair of the training sample is Negative pair Among them, D train is the training set, c i is the code segment in the i-th sample, To encode c using the first encoder i The vector obtained after g i is the structured view in the i-th sample, To encode g using the second encoder i The vector obtained after g j is the structured view in the jth sample, To encode g using the second encoder j The vector obtained after .
21. The method according to claim 17, wherein The intra-modality alignment is implemented based on a self-supervisory model in the second encoder, wherein the self-supervisory model is obtained using a third training sample, and the positive pair of the third training sample is Negative pair Among them, D train is the training set, To encode g using the second encoder i The vector obtained after g i is the structured view in the i-th sample, g i ′ is the damaged structured view of the i-th sample, g j ′ is the damaged structured view of the j-th sample.
22. A computer device, characterized in that: include: a first processing unit, configured to determine a first index for accessing a knowledge base based on a target question, wherein the target question is a function to be implemented by a target code described in a natural language, the knowledge base including a mapping relationship between code segments and a structured view, the structured view being used to represent semantics and logical relationships between the code segments; a second processing unit, configured to query the knowledge base according to the first index to obtain a first mapping relationship associated with the first index; A third processing unit is configured to input the target problem and the prompt information into a code generation model to obtain the target code for the target problem, wherein the prompt information is associated with the first mapping relationship.
23. A computer device, characterized in that: include: A first processing unit is configured to obtain a target problem, where the target problem is a function to be implemented by a target code described in a natural language; a second processing unit, configured to extract key information from the target question; A third processing unit is used to retrieve a first index from the knowledge base based on the key information and the knowledge base, where the first index is an index of a code segment and / or structured view in the knowledge base that has the highest similarity to the key information. The knowledge base includes a mapping relationship between code segments and structured views, and the structured view is used to represent the semantics and logical relationships of the code segments.
24. A computing device, characterized in that comprising a processor and a computer-readable storage medium storing a computer program; The processor is coupled to the computer-readable storage medium, and when the computer program is executed by the processor, the method according to any one of claims 1 to 12 or 13 to 21 is implemented.
25. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 12 or 13 to 21 is implemented.
26. A computer program product, characterized in that The computer program product comprises a computer program code, which, when executed on a computer device, causes the computer device to perform the method according to any one of claims 1 to 12 or 13 to 21.
Citation Information
Cited By
Code semantic understanding and mixed retrieval question and answer method and system oriented to AI programming
CN122064775A