A source code summary generation method and system based on enhanced prompt learning framework

By enhancing the prompt learning framework, combining code pre-trained models and large language models, more accurate code summary is generated, which solves the flexibility and accuracy of code summary generation in the existing technology, adapts to multilingual and diversified code snippets, and improves the efficiency of code understanding and document generation.

CN120045226BActive Publication Date: 2025-08-22QINGDAO UNIV OF SCI & TECH
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202510525258.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-04-25
Publication Date
2025-08-22
Estimated Expiration
2045-04-25

AI Technical Summary

Technical Problem

When generating code summary, the prior art is difficult to adapt to multilingual and diverse code snippets. The prompt word design is complex and has poor flexibility, and cannot accurately reflect the fine-grained functions and logic of the code. Especially the summary generated in non-modular code is too abstract.

Method used

The enhanced prompt learning framework is adopted, combining code pre-trained models and large language models, and the knowledge prompt vectors and structural prompt vectors are generated through mapping modules and structural proxy modules, reducing dependence on manual design prompt templates, and generating more accurate code summary.

Benefits of technology

It improves the flexibility and accuracy of code summary, and is suitable for a variety of programming languages ​​and code structure types. The generated summary is in line with actual functions and improves the efficiency of code automation understanding and document generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045226B_ABST
    Figure CN120045226B_ABST
Patent Text Reader

Abstract

The present invention discloses a source code summary generation method and system based on an enhanced prompt learning framework. Code snippets are input into the enhanced prompt learning framework for processing to generate a code summary in natural language form. The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module, and a large language model. The code pre-training model receives the code snippets and performs feature extraction to obtain code features. The code features are input into the mapping module to generate a knowledge hint vector. The code features are simultaneously input into the structure proxy module to generate a structure hint vector. The knowledge hint vector and the structure hint vector are concatenated and input into the large language model as hint information to generate a code summary in natural language form. By combining the structural understanding ability of the code pre-training model with the natural language generation ability of the large language model, a more accurate code summary is generated.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the fields of natural language processing and software engineering, and in particular to a source code summary generation method and system based on an enhanced prompt learning framework. Background Art

[0002] The statements in this section merely provide background information related to the present disclosure and do not necessarily constitute prior art.

[0003] Understanding and maintaining source code is a critical step in modern software development. Developers often need to annotate their code to improve readability and maintainability. However, manually writing code comments is a time-consuming and easily overlooked task. Incomplete or inaccurate code comments are common, especially in complex projects or rapidly iterating development. As projects evolve, existing comments often become inconsistent with the actual code functionality, further increasing the difficulty of understanding and maintaining the code. To address this issue, numerous technologies for automatically generating code summaries have been proposed, aiming to help developers quickly understand the code's functionality and logic by automatically generating natural language descriptions.

[0004] In the prior art, patent CN118152287A proposes a rich text code review method based on a large language model. This method matches code snippets in rich text through regular expressions, combines knowledge bases of different programming languages ​​with predefined code review prompt words, and uses the natural language processing capabilities of the large language model to generate review opinions. This method can effectively extract code content from rich text and conduct reviews, but its limitation is that it relies less on code understanding and mainly identifies code snippets by matching regular expressions with prompt words. Especially when dealing with complex code structures and multilingual scenarios, this method has poor flexibility and requires the design of specific prompt templates for different programming languages, which increases the complexity of prompt design and makes it difficult to cope with diverse code formats and syntax.

[0005] In addition, patent CN118550579A proposes a hierarchical code summary generation method based on a large language model thinking chain. It uses static analysis tools to extract the calling relationship between code modules, and uses heuristic algorithms to calculate the weight of each code file to generate a module-level code summary. This method performs well in processing large modular software systems and can generate high-level code structure summaries. However, this solution has limitations when processing smaller-granularity code snippets, especially in the generation of code summaries at the function level or class level, and often fails to accurately capture the detailed functions of the code. This method relies on static analysis and heuristic algorithms. For complex code logic, especially non-modular code, the generated summary is too general and lacks an in-depth understanding of the internal structure and semantics of the code.

[0006] Another existing patent, CN117873559A, proposes a code summary generation method based on a large language model and static analysis tools. This method generates an abstract syntax tree through lexical and grammatical parsing, and generates a control flow graph to describe the execution process of the code. Combined with the pseudocode block generation method, the code summary is depth-first traversed to generate a complete code summary. Although this method has certain advantages in processing complex code logic and arithmetic expressions, it relies on static analysis, control flow graphs, and pseudocode block generation, and is not as flexible as the automatic generation method based on prompt learning. In addition, this method is limited in the number of tokens of the input code and the range of programming languages ​​it supports, and has limitations in the generation and understanding of multi-language code.

[0007] In summary, existing code summary generation technologies generally have the following shortcomings when faced with multilingual and diverse code snippets: 1. The prompt word design is complex and inflexible. Many methods rely on manually designed prompt templates, which are difficult to adapt to different types of programming languages ​​and code structures; 2. Existing methods have a relatively superficial understanding of code structure, especially when generating fine-grained summaries of code snippets, which cannot accurately reflect the function and logic of the code; 3. Some existing technologies are mainly suitable for generating top-level code summaries for modular systems. For smaller-grained code snippets, the generated summaries are too abstract, which is not conducive to developers' in-depth understanding of the specific functions of the code. Summary of the Invention

[0008] To overcome the above-mentioned shortcomings of the existing technology, the present invention provides a source code summary generation method and system based on an enhanced prompt learning framework, which is used to automatically generate code summaries. By combining the structural understanding ability of the code pre-training model with the natural language generation ability of the large language model, a more accurate code summary is generated.

[0009] To achieve the above objectives, one or more embodiments of the present invention provide the following technical solutions:

[0010] In a first aspect, the present invention provides a method for generating a source code summary based on an enhanced hint learning framework, comprising:

[0011] Obtain code snippets, input them into the enhanced prompt learning framework for processing, and generate code summaries in natural language form;

[0012] The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

[0013] According to a further technical solution, the mapping module includes a left encoder and a right encoder, the left encoder includes a self-attention layer and a feedforward layer connected in sequence, and the right encoder includes a self-attention layer, a cross-attention layer and a feedforward layer connected in sequence.

[0014] According to a further technical solution, the specific steps of the mapping module generating the knowledge hint vector are as follows:

[0015] When training the mapping module, background knowledge is input into the left encoder to obtain a high-level representation of the background knowledge. Virtual tags are input into the right encoder, and code features are input into the cross-attention layer of the right encoder. After the virtual tags and code features interact in the cross-attention layer, they are sent to the feedforward layer to obtain a simulated knowledge representation, and finally a trained mapping module is obtained.

[0016] The trained mapping module only retains the right encoder, inputs the virtual label into the right encoder, and inputs the code features into the cross-attention layer of the right encoder to obtain the simulated knowledge representation and input it into the projector for feature mapping and dimension alignment to obtain the knowledge prompt vector.

[0017] A further technical solution is that when the mapping module is trained and optimized, the high-order representation of background knowledge and the simulated knowledge representation are used to design the contrast estimation loss, cross entropy loss, and two-way matching loss respectively, and the total loss is the sum of the contrast estimation loss, cross entropy loss, and two-way matching loss.

[0018] A further technical solution is that the specific steps of the structure proxy module generating a structure hint vector through a variational autoencoder are: encoding the code features through the encoder of the variational autoencoder, and optimizing the variational autoencoder using reconstruction loss in the decoder so that the variational autoencoder captures the structure hint vector.

[0019] A further technical solution is to splice the knowledge hint vector and the structure hint vector as follows: input the code snippet into the embedding layer of the large language model to obtain text embedding, splice and merge the text embedding, knowledge hint vector and structure hint vector, and input them into the large language model.

[0020] A further technical solution is to freeze the large language model for training optimization, and its joint optimization loss is the weighted sum of reconstruction loss, KL loss and difference generation loss.

[0021] In a second aspect, the present invention provides a source code summary generation system based on an enhanced hint learning framework, comprising:

[0022] A code acquisition module is configured to: acquire code snippets;

[0023] A summary generation module is configured to: input the code snippet into the enhanced prompt learning framework for processing, and generate a code summary in natural language form;

[0024] The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

[0025] In a third aspect, the present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps in the method for generating a source code summary based on an enhanced prompt learning framework as described in the first aspect.

[0026] In a fourth aspect, the present invention provides a computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the program, the method for generating a source code summary based on an enhanced prompt learning framework as described in the first aspect is implemented.

[0027] One or more of the above technical solutions have the following beneficial effects:

[0028] By combining the strengths of LLM and CodePLM, this paper proposes an enhanced hint learning framework that deeply understands code structure and generates more accurate natural language summaries that align with the code's functionality. Furthermore, this approach reduces reliance on manually designed hint templates, offering greater flexibility and adaptability for code snippets of various sizes and in a variety of programming languages.

[0029] The present invention combines the code pre-training model's understanding of code structure with the natural language generation capability of the large language model, and the generated code summary is more accurate and consistent with the actual function. At the same time, by using the mapping module and the structure proxy module to generate knowledge prompt vectors and structure prompt vectors, the reliance on manually designed prompt templates is reduced, the flexibility of prompt generation is enhanced, and it is adaptable to code snippets in multiple programming languages. It supports multiple programming languages, such as Python, Java, C++, etc., and enhances the applicability in different programming environments. In addition, the present invention optimizes the alignment of the prompt vector and the large language model embedding, and does not need to waste a lot of time to fine-tune the large language model. In low-resource scenarios (such as insufficient training data or limited computing resources), it can still generate high-quality code summaries that are applicable to multiple programming languages ​​and code structure types (such as functions, classes, control flows, etc.), thereby improving the efficiency of automatic code understanding and document generation. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] The accompanying drawings, which constitute a part of the present invention, are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute improper limitations on the present invention.

[0031] Figure 1 is a flow chart of a method for generating a source code summary according to an embodiment of the present invention;

[0032] Figure 2 This is a structural diagram of the enhanced prompt learning framework according to an embodiment of the present invention;

[0033] Figure 3 This is a code summary case of an embodiment of the present invention;

[0034] Figure 4 The embodiment of the present invention is based on Figure 3 The code snippets in the code snippet are generated by different methods. DETAILED DESCRIPTION

[0035] It should be noted that the following detailed descriptions are exemplary and intended to provide further explanation of the present invention. Unless otherwise specified, all technical and scientific terms used herein have the same meaning as commonly understood by those skilled in the art to which the present invention belongs.

[0036] It should be noted that the terms used herein are only for describing specific embodiments and are not intended to limit the exemplary embodiments according to the present invention. As used herein, unless the context clearly indicates otherwise, the singular form is intended to include the plural form. In addition, it should be understood that when the terms "comprise" and / or "include" are used in this specification, they indicate the presence of features, steps, operations, devices, components and / or combinations thereof.

[0037] In the absence of conflict, the embodiments of the present invention and the features thereof may be combined with each other.

[0038] Example 1

[0039] like Figure 1 As shown, this embodiment discloses a method for generating source code summaries based on an enhanced hint learning framework, the method comprising the following steps:

[0040] Obtain code snippets, input them into the enhanced prompt learning framework for processing, and generate code summaries in natural language form;

[0041] The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

[0042] In this example, we propose an enhanced prompt learning framework (EPG4CS) that combines the strengths of large-scale language models (LLMs) and pre-trained code models (CodePLMs). The EPG4CS framework aims to leverage CodePLMs to build upon the advanced capabilities of LLMs and address code summarization tasks. This framework employs a two-stage independent training strategy. In the first stage, the parameters of the pre-trained code model (CodePLM) are frozen to ensure stable vector outputs, and the mapping module is optimized using contrastive estimation loss, cross-entropy loss, and bidirectional matching loss. In the second stage, the parameters of the large language model (LLM) are frozen, and the mapping module, projector, and structural proxy modules are optimized.

[0043] EPG4CS includes the following key components:

[0044] (1) Code pre-training model (frozen code pre-training model CodePLMs), which is pre-trained on code language tasks and generates embedding vectors for input code that are useful for downstream tasks;

[0045] The code pre-training model can be CodeBERT, CodeT5, or other code-based pre-training models, which can extract the grammatical structure and context information of the code snippet.

[0046] (2) Mapper, which aims to align code representation with knowledge information through various pre-training tasks;

[0047] (3) Structural Agent module (StructuralAgent), which aims to extract the structural features of the code using VAE;

[0048] (4) Large language model (FrozenLLM), which is responsible for generating accurate code summaries after receiving rich prompt information. Large language models can be StarcodeBase, PolyCoder, or other models that support natural language generation.

[0049] In this embodiment, the randomly initialized soft hint method often leads to unstable LLM output and reduced generalization ability. To address this issue, the present invention designs three training losses in the first stage to reduce the training difficulty in the second stage: contrastive estimation loss, cross-entropy loss, and bidirectional matching loss. The mapping module (Mapper) is pre-trained to enable it to generate optimal soft hint vectors (i.e., knowledge hint vectors containing code background knowledge) in the second stage.

[0050] like Figure 2 As shown on the left, the mapping module (mapper) consists of a left encoder (LT) and a right encoder (RT). The left encoder consists of a self-attention layer and a feedforward layer connected in sequence, and the right encoder consists of a self-attention layer, a cross-attention layer, and a feedforward layer connected in sequence. The weights are shared between the self-attention layer and the feedforward layer of the left and right encoders. The entire process of processing the pre-trained data pair <code snippet C, background knowledge K> can be described as follows: First, the frozen code pre-training model generates a representation from the input (code snippet) (i.e., code features), and then Input to the right encoder. The code snippet contains multiple codes, each of which produces a representation At the same time, the left encoder is used to calculate the background knowledge Process and extract a high-level representation Then, the right encoder integrates the virtual marker and code features , generating a simulated knowledge representation The virtual tokens are a set of randomly initialized vector parameters whose dimensions match the input dimensions of the first layer of the encoder and are used as the input to the first layer of the encoder. The formal form of these steps is as follows:

[0051]

[0052]

[0053] in, A high-level representation of background knowledge, represents the right encoder, represents the left encoder, represents the model parameters shared between the left encoder and the right encoder, represents simulated knowledge representation, represents a virtual tag, Indicates code characteristics, The cross-attention mechanism is used to realize information interaction between virtual tags and code features using the cross-attention mechanism, so that the virtual tags can effectively extract knowledge that is closely integrated with the logic and function of the code from the code features.

[0054] The steps of the mapping module during training are as follows: background knowledge is processed through the left encoder to obtain a high-order representation of background knowledge and capture the deep semantic information therein; the virtual tag is input into the right encoder, and the code features are input into the cross-attention layer of the right encoder at the same time. The virtual tag is also input into the cross-attention layer after being calculated by the self-attention layer. The virtual tag and the code features interact with each other through the cross-attention mechanism, and then the virtual tag is further sent to the forward feedback layer to obtain the simulated knowledge representation; when the mapping module is trained and optimized, the high-order representation of background knowledge and the simulated knowledge representation are used to design the contrast estimation loss, cross entropy loss and bidirectional matching loss. The final optimization model is the sum of the three losses.

[0055] The steps of applying the mapping module are as follows: After training, the trained mapping module is obtained, and only the right encoder part is retained, which is consistent with the training. The virtual tag is input into the right encoder, and the code feature is input into the cross-attention layer of the right encoder. The virtual tag is also input into the cross-attention layer after calculation by the self-attention layer. At this time, the virtual tag obtains the code information most relevant to the text from the code feature to obtain the simulated knowledge representation; the simulated knowledge representation is input into the projector for feature mapping and dimension alignment to obtain the knowledge prompt vector.

[0056] Next, the design of the pre-training task of the mapping module Mapper in the first phase will be introduced in detail.

[0057] 1) Code knowledge contrastive learning: Contrastive learning enhances the model’s ability to extract key features for distinguishing positive and negative samples by clearly distinguishing code snippets. Based on contrastive learning, the output of the left encoder is maximized. Simulated knowledge representation with right encoder output The mutual information between them is optimized, thereby optimizing the simulation effect of the virtual marker.

[0058] Specifically, first calculate and Select the pair of samples with the highest score (e.g. & ) as positive samples, while other sample pairs with low similarity or irrelevant are regarded as negative samples. In the training optimization process, the noise contrast estimation (InfoNCE) loss function is used, which is expressed as:

[0059]

[0060] in, represents the comparison estimated loss, represents a high-order representation of the virtual label, A high-level representation of background knowledge, represents the temperature parameter, represents a similarity function, such as cosine similarity or Euclidean distance.

[0061] 2) Knowledge text generation: To further enhance the text modeling capability of Mapper, text generation loss is introduced. Its training goal is to enable Mapper to receive code features. Generating text with background knowledge Consistent output. In the design of Mapper, since Mapper's architecture does not allow direct interaction between the frozen code pre-trained model and the knowledge text, the information required to generate the text must first be extracted by the query and then passed to the knowledge text through the self-attention layer. Therefore, virtual tagging Forced from code features Here, a multimodal causal self-attention mask is used to control the interaction between the left encoder and the right encoder, and the [DEC] token is used as a signal to start decoding in the left encoder, so Is the text sequence to be generated by Mapper. Finally, the Mapper is optimized by cross entropy loss:

[0062]

[0063] in, represents the cross entropy loss, represents the number of samples in the collected sample, 、 Represents an index, Indicates the The true probability distribution of knowledge texts, Indicates the The model predicts the probability distribution of knowledge text.

[0064] 3) Code-knowledge matching loss: Through the above two tasks, the relationship between virtual tags and background knowledge is aligned. However, due to the modality gap between programming language and natural language, a two-way matching loss is designed to enable Mapper to capture code snippets more accurately. and background knowledge text The specific optimization loss function is described as follows:

[0065]

[0066] in, represents the bidirectional matching loss, Indicates the Code features, Indicates the A high-level representation of background knowledge, represents the margin hyperparameter, represents the distance function, Defines the rectifier operation.

[0067] Generate learning from frozen LLM.

[0068] 1) Structural Agent: Although LLMs are widely recognized for their excellent understanding and generalization capabilities, their decoding-based training model does not fully consider the structural semantics of the code. To address this issue, we design a structural agent module based on a variational autoencoder, which models the structured representation of the code as a probability distribution through variational reasoning. Specifically, the structural agent module obtains code features from the code pre-training model. , and then generate the latent variables through the encoder of the structural agent module , the decoder from The original code is reconstructed into the latent space to ensure that the reconstructed code retains as much of the original structure and semantic features as possible. The following are the specific implementation steps:

[0069]

[0070]

[0071]

[0072] in, represents the conditional probability distribution, indicating that given the input Time latent variables distribution of represents a normal distribution; Represents latent variables The mean of and input Decide; Represents latent variables The variance of and input Decide; represents the conditional probability distribution, indicating that given the latent variable Time input distribution of Represents latent variables The mean of and Decide; Represents latent variables The variance of and input Decide; and Represents the parameters of the encoder and decoder in the structure agent module; represents the reconstruction loss, represents the KL divergence.

[0073] Compared with previous methods, the structural agent module makes the latent variables Can be trained as potential structured features, using latent variables As a structural hint vector (hint vector containing code structure information).

[0074] 2) Fusion embedding generation: In order to provide the necessary background knowledge for LLM during the decoding process, code snippets are extracted from the mapping module Simulated Knowledge Representation , which is then fed into the projector to obtain the knowledge hint vector . After that, replace the code snippet Input into LLM to get text embedding .Will 、 、 These three elements are combined and regularized to address the negative impact of distribution differences on the model. The specific steps are as follows:

[0075]

[0076] in, represents the KL loss, represents the KL divergence.

[0077] Knowledge Hint Vector After regularization, we get the regularized knowledge hint vector , After regularization, we get the regularized structure hint vector , 、 is effectively used to precisely influence the output of LLM. In addition, by comparing the difference between the generated code summary and the actual summary, the loss minimization process is guided, and the loss function is modeled as:

[0078]

[0079] in, represents the difference generation loss, represents the trainable parameters of the model, represents the number of tokens in the vocabulary, Indicates the The probability of predicting the label, Indicates each The probability of the true label, .

[0080] 3) Joint Optimization: In this paper, a two-stage independent training strategy is adopted. In the first stage, we focus on optimizing the left and right encoders in the mapping module and virtual labeling, while freezing the CodePLM to ensure stable vector output. The total loss in the first stage includes the sum of contrastive estimation loss, cross entropy loss, and bidirectional matching loss. In the second stage, to generate code summaries, the LLM is set to a frozen state and the mapping module, projector, and structure proxy module are further optimized. The joint optimization loss function in this stage is defined as follows:

[0081]

[0082] in, 、 and Represents the corresponding weight of the loss, which is used to balance the impact of each loss on model training. In this embodiment, they are set to 1.0, 0.5, and 0.1 respectively.

[0083] By combining code structure information and natural language background knowledge, the present invention significantly improves the accuracy and efficiency of code summary generation. Especially under low-resource conditions, the enhanced prompt learning method proposed in the present invention can effectively reduce training time and resource consumption. Compared with existing discrete prompt learning methods, the summaries generated by the present invention are more accurate and more context-relevant, and are suitable for scenarios such as automated code annotation, code understanding, and code retrieval.

[0084] Example 2

[0085] This embodiment discloses a source code summary generation system based on an enhanced prompt learning framework, including:

[0086] A code acquisition module is configured to: acquire code snippets;

[0087] A summary generation module is configured to: input the code snippet into the enhanced prompt learning framework for processing, and generate a code summary in natural language form;

[0088] The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

[0089] Example 3

[0090] The purpose of this embodiment is to provide a computing device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the steps of the method of embodiment 1 when executing the program.

[0091] Example 4

[0092] The purpose of this embodiment is to provide a computer-readable storage medium, a computer-readable storage medium having a computer program stored thereon, which performs the steps of the method of embodiment 1 when executed by a processor.

[0093] The steps involved in the apparatuses of Examples 3 and 4 above correspond to those of Method Example 1. For detailed implementation, please refer to the relevant description of Example 1. The term "computer-readable storage medium" should be understood to mean a single medium or multiple media containing one or more instruction sets; it should also be understood to include any medium capable of storing, encoding, or carrying an instruction set for execution by a processor and causing the processor to perform any of the methods of the present invention.

[0094] Those skilled in the art will appreciate that the modules or steps of the present invention described above can be implemented using a general-purpose computer device. Alternatively, they can be implemented using program code executable by a computing device, which can then be stored in a storage device and executed by the computing device. Alternatively, they can be fabricated into separate integrated circuit modules, or multiple modules or steps can be fabricated into a single integrated circuit module for implementation. The present invention is not limited to any specific combination of hardware and software.

[0095] The foregoing description is merely a preferred embodiment of the present invention and is not intended to limit the present invention. Those skilled in the art will readily appreciate that various modifications and variations of the present invention are possible. Any modifications, equivalent substitutions, or improvements made within the spirit and principles of the present invention are intended to be within the scope of protection of the present invention.

[0096] Although the above describes the specific embodiments of the present invention in conjunction with the accompanying drawings, it is not intended to limit the scope of protection of the present invention. Those skilled in the art should understand that various modifications or variations that can be made by those skilled in the art on the basis of the technical solution of the present invention without any creative work are still within the scope of protection of the present invention.

Claims

1. A source code summary generation method based on an enhanced hint learning framework, characterized in that: include: Obtain code snippets, input them into the enhanced prompt learning framework for processing, and generate code summaries in natural language form; The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

2. A source code summary generation method based on an enhanced hint learning framework as claimed in claim 1, characterized in that: The mapping module includes a left encoder and a right encoder, the left encoder includes a self-attention layer and a feedforward layer connected in sequence, and the right encoder includes a self-attention layer, a cross-attention layer and a feedforward layer connected in sequence.

3. The method for generating source code summaries based on an enhanced hint learning framework according to claim 2, wherein: The specific steps of the mapping module generating the knowledge hint vector are: When training the mapping module, background knowledge is input into the left encoder to obtain a high-level representation of the background knowledge. Virtual tags are input into the right encoder, and code features are input into the cross-attention layer of the right encoder. After the virtual tags and code features interact in the cross-attention layer, they are sent to the feedforward layer to obtain a simulated knowledge representation, and finally a trained mapping module is obtained. The trained mapping module only retains the right encoder, inputs the virtual label into the right encoder, and inputs the code features into the cross-attention layer of the right encoder to obtain the simulated knowledge representation and input it into the projector for feature mapping and dimension alignment to obtain the knowledge prompt vector.

4. The method for generating source code summaries based on an enhanced hint learning framework according to claim 2, wherein: When the mapping module is trained and optimized, the high-order representation of background knowledge and the simulated knowledge representation are used to respectively design the contrast estimation loss, cross entropy loss, and bidirectional matching loss, and the total loss is the sum of the contrast estimation loss, the cross entropy loss, and the bidirectional matching loss.

5. The method for generating source code summaries based on an enhanced hint learning framework according to claim 1, wherein: The specific steps of the structure proxy module generating a structure hint vector through a variational autoencoder are as follows: encoding the code features through the encoder of the variational autoencoder, and optimizing the variational autoencoder using reconstruction loss in the decoder so that the variational autoencoder captures the structure hint vector.

6. The method for generating source code summaries based on an enhanced hint learning framework according to claim 1, wherein: The knowledge hint vector and the structure hint vector are specifically spliced ​​as follows: the code snippet is input into the embedding layer of the large language model to obtain text embedding, the text embedding, the knowledge hint vector and the structure hint vector are spliced ​​and merged, and then input into the large language model.

7. The method for generating source code summaries based on an enhanced hint learning framework according to claim 6, wherein: The large language model is frozen for training optimization, and its joint optimization loss is the weighted sum of reconstruction loss, KL loss and difference generation loss.

8. A source code summary generation system based on an enhanced hint learning framework, characterized in that: include: A code acquisition module is configured to: acquire code snippets; A summary generation module is configured to: input the code snippet into the enhanced prompt learning framework for processing, and generate a code summary in natural language form; The enhanced prompt learning framework includes a code pre-training model, a mapping module, a structure proxy module and a large language model; the code pre-training model receives code snippets and performs feature extraction to obtain code features, and the code features are input into the mapping module to generate a knowledge prompt vector; at the same time, the code features are input into the structure proxy module to generate a structure prompt vector; the knowledge prompt vector and the structure prompt vector are spliced ​​and input into the large language model as prompt information to generate a code summary in natural language form.

9. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the steps of the method for generating a source code summary based on an enhanced prompt learning framework according to any one of claims 1 to 7 are implemented.

10. A computer device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the program, the steps of the method for generating a source code summary based on an enhanced prompt learning framework according to any one of claims 1 to 7 are implemented.

Citation Information

Patent Citations

  • Label generation method and device of electronic book and electronic equipment

    CN114398854A