Code generation methods, apparatus, devices, media, and program products
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-03-26
- Publication Date
- 2026-08-11
AI Technical Summary
然而,目前保障代码风格一致性的作法通常是整理出相应的开发规范或标准,据此判断新代码是否满足开发规范或标准,这样仅能从开发规范或标准中预先设置的有限维度进行分析,无法全面量化新开发代码与已有代码风格的一致性
[0017]根据本申请的第四方面,还提供了一种计算机可读存储介质,其上存储有计算机程序或指令,上述计算机程序或指令被处理器执行时实现上述方法的步骤。
Smart Images

Figure CN122547327A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of artificial intelligence and big data, and can be applied to the fields of fintech or other fields. More specifically, it relates to a code generation method, apparatus, device, medium, and program product. Background Technology
[0002] With the development and accumulation of digitalization, enterprises typically develop specific code development standards and styles. New code that conforms to these standards facilitates software sharing and integration among different developers, improves code management, and enhances development efficiency, reusability, and troubleshooting. However, current practices for ensuring code style consistency often involve compiling relevant development standards or specifications to determine whether new code meets these standards. This approach only analyzes the limited dimensions pre-defined in the standards or specifications, failing to comprehensively quantify the consistency between new and existing code styles. Furthermore, since these standards or specifications are developed and refined over time, they may not be updated promptly, potentially hindering the flexibility of code development. Summary of the Invention
[0003] In view of the above problems, embodiments of this application provide a code generation method, apparatus, device, medium, and program product.
[0004] According to a first aspect of this application, a code generation method is provided. The method includes: receiving an original query statement input by a user; generating a first code fragment based on the original query statement using a code knowledge base as an external knowledge source and a large model through retrieval enhancement; extracting features from the first code fragment using a feature extraction pipeline identical to that used in the code knowledge base construction phase to form a first multi-dimensional feature representation; wherein the code knowledge base stores at least one second code fragment and a second multi-dimensional feature representation formed by extracting features from the second code fragment using the feature extraction pipeline; performing an N-way retrieval in the code knowledge base using the first multi-dimensional feature representation to obtain retrieval results, where N is an integer greater than or equal to 2; evaluating the fit between the first code fragment and the code knowledge base based on the retrieval results; and outputting the first code fragment when the fit is greater than or equal to a preset fit threshold.
[0005] According to an embodiment of this application, the method further includes: when the fit is less than a preset fit threshold, using the large model to correct the first code segment based on the search results or output correction suggestions.
[0006] According to an embodiment of this application, the step of extracting features from the first code segment using the same feature extraction pipeline as the code knowledge base construction phase to form a first multi-dimensional feature representation includes: extracting functional semantic features of the first code segment based on the text of the first code segment; extracting structural semantic features of the first code segment based on the abstract syntax tree and control flow graph of the first code segment; extracting contextual features of the first code segment based on the meta-information of the first code segment; and forming the first multi-dimensional feature representation based on the functional semantic features, structural semantic features, and contextual features of the first code segment.
[0007] According to an embodiment of this application, the multi-dimensional feature representation includes vectors and structured identifiers. The step of using the first multi-dimensional feature representation to perform N-way retrieval in the code knowledge base to obtain retrieval results includes: in one retrieval, retrieving vectors that meet preset conditions from the code knowledge base based on the vector similarity between the first multi-dimensional feature representation and the second multi-dimensional feature representation, and outputting the similar code segments corresponding to the retrieved vectors and their vector similarity values; and in another retrieval, retrieving successfully matched identification information from the code knowledge base based on the comparison between the first multi-dimensional feature representation and the structured identifiers in the second multi-dimensional feature representation.
[0008] According to an embodiment of this application, evaluating the fit between the first code segment and the code knowledge base based on the retrieval results includes: obtaining a semantic similarity score based on the vector similarity values of the similar code segments; obtaining a pattern consistency score based on the matching identifier information and the preset scoring rules in the code development pattern specification corresponding to the code knowledge base; obtaining a static compliance score based on the analysis of the first code segment by a static analysis tool; and obtaining the fit based on a weighted average of the semantic similarity score, the pattern consistency score, and the static compliance score.
[0009] According to an embodiment of this application, the step of extracting the structural semantic features of the first code segment based on the abstract syntax tree and control flow graph of the first code segment includes: generating the abstract syntax tree and control flow graph of the first code segment; extracting a first structural feature from the abstract syntax tree of the first code segment; extracting a second structural feature from the control flow graph of the first code segment; and uniformly encoding the first structural feature and the second structural feature of the first code segment through a trained encoding model to obtain the structural semantic features of the first code segment.
[0010] According to an embodiment of this application, the step of extracting the structural semantic features of the first code segment based on the abstract syntax tree and control flow graph of the first code segment further includes: extracting a third structural feature from the text of the first code segment based on a preset rule; wherein the first structural feature, the second structural feature and the third structural feature of the first code segment are uniformly encoded through the encoding model to obtain the structural semantic features of the first code segment.
[0011] According to an embodiment of this application, the step of generating a first code fragment using a large model through retrieval enhancement based on the original query statement and with a code knowledge base as the external knowledge source includes: expanding the original query statement to obtain an enhanced query statement; obtaining a recall result from the code knowledge base through M-way parallel recall based on the enhanced query statement, wherein the M-way parallel recall path is the same as or not completely the same as the N-way retrieval path, and M is an integer greater than 2; sorting the second code fragments in the recall result to form a sorting result; selecting multiple target code fragments that meet preset preference conditions according to the sorting result; generating prompt words based on the multiple target code fragments and the enhanced query statement; inputting the prompt words into the large model and obtaining the first code fragment output by the large model.
[0012] According to an embodiment of this application, sorting the second code fragments in the recall result to form a sorting result includes: matching a target application scenario from multiple application scenarios based on the enhanced query statement; obtaining a target sorting strategy corresponding to the target application scenario, wherein different application scenarios have different preset sorting strategies; and sorting the second code fragments in the recall result based on the target sorting strategy.
[0013] According to an embodiment of this application, the target ranking strategy includes various ranking factors for ranking and their weight configuration information; the ranking of the second code segments in the recall result based on the target ranking strategy includes: extracting the value of each second code segment in the recall result ranked on each ranking factor; performing weighted processing on the value of each second code segment ranked on each ranking factor according to the weight configuration information to obtain the ranking score of each second code segment in the recall result; and ranking the second code segments in the recall result based on the ranking score.
[0014] According to an embodiment of this application, obtaining the target ranking strategy corresponding to the target application scenario further includes: when there are multiple target application scenarios matched based on the enhanced query statement, weighted fusion of the preset ranking strategies of the multiple target application scenarios to obtain the target ranking strategy.
[0015] A second aspect of this application provides a code generation apparatus. The apparatus includes a code generation module, a feature extraction module, a retrieval module, and an evaluation module. The code generation module receives an original query statement input by a user and, based on the original query statement, generates a first code fragment using a large model through retrieval enhancement, with a code knowledge base as the external knowledge source. The feature extraction module uses a feature extraction pipeline identical to that used in the code knowledge base construction phase to extract features from the first code fragment, forming a first multi-dimensional feature representation; wherein the code knowledge base stores at least one second code fragment and a second multi-dimensional feature representation formed by extracting features from the second code fragment using the feature extraction pipeline. The retrieval module performs an N-way retrieval in the code knowledge base using the first multi-dimensional feature representation to obtain retrieval results, where N is an integer greater than or equal to 2. The evaluation module evaluates the fit between the first code fragment and the code knowledge base based on the retrieval results, and outputs the first code fragment when the fit is greater than or equal to a preset fit threshold.
[0016] According to a third aspect of this application, an electronic device is provided, comprising: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the method described above.
[0017] According to a fourth aspect of this application, a computer-readable storage medium is also provided, on which a computer program or instructions are stored, wherein the computer program or instructions, when executed by a processor, implement the steps of the above-described method.
[0018] According to a fifth aspect of this application, a computer program product is also provided, including a computer program or instructions that, when executed by a processor, implement the steps of the above-described method.
[0019] The above one or more embodiments have the following advantages or beneficial effects: By performing feature extraction on the first code snippet using the same feature extraction pipeline as the code knowledge base construction stage, and then performing multi-path retrieval in the code knowledge base, not only can multi-path and multi-dimensional retrieval results be generated, but also, since the feature extraction method is the same as the feature extraction method of the code snippets stored in the code knowledge base, the accuracy of the retrieval results can be improved. Based on this retrieval result, the fit between the first code snippet and the code knowledge base can be quantified, and the consistency between the first code snippet and the enterprise's existing code development specifications or standards can be more accurately grasped, thereby providing accurate guidance for adopting or improving the first code snippet. Moreover, in the embodiments of this application, when evaluating the fit of the first code snippet, it is done by matching the first code snippet with the code knowledge base, rather than comparing the first code snippet with the summarized development specifications or standards. This can improve the comprehensiveness and objectivity of the evaluation results, and on the other hand, as the code knowledge base is constantly updated, the evaluation results of the fit can also adapt to the constantly changing code development styles, avoiding the rigidity and lag problems that may exist when comparing based on summarized development specifications or standards. Attached Figure Description
[0020] The above-mentioned contents, other objects, features and advantages of this application will become clearer from the following description of embodiments with reference to the accompanying drawings, in which:
[0021] Figure 1 The illustrations depict application scenarios of the code generation method, apparatus, device, medium, and program product according to embodiments of this application.
[0022] Figure 2 A flowchart illustrating a code generation method according to an embodiment of this application is shown schematically;
[0023] Figure 3 This illustration schematically shows the processing flow of extracting features of a first code fragment in a code generation method according to an embodiment of this application;
[0024] Figure 4 This illustration schematically shows a flowchart of the N-way retrieval process in a code knowledge base in a code generation method according to an embodiment of this application;
[0025] Figure 5 The flowchart illustrating the process of evaluating the fit between a first code fragment and a code knowledge base in the code generation method of an embodiment of this application is shown in the illustration.
[0026] Figure 6 This illustration schematically shows a system architecture for generating code via retrieval enhancement according to one embodiment of the present application;
[0027] Figure 7This illustration schematically shows the offline construction process of a code knowledge base according to one embodiment of the present application;
[0028] Figure 8 The illustration schematically shows the process flow of generating code by means of retrieval enhancement according to one embodiment of this application;
[0029] Figure 9 This illustration schematically shows the process of expanding the original query statement during the code generation process by retrieval enhancement according to one embodiment of this application;
[0030] Figure 10 This illustration shows the process of sorting the recall results according to different scenarios during the code generation process by enhancing retrieval according to one embodiment of this application;
[0031] Figure 11 This schematic diagram illustrates a structural block diagram of a code generation apparatus according to an embodiment of the present application;
[0032] Figure 12 A block diagram schematically illustrates an electronic device suitable for implementing the code generation method according to embodiments of this application. Detailed Implementation
[0033] The embodiments of this application will now be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of this application. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of this application for ease of explanation. However, it will be apparent that one or more embodiments may be implemented without these specific details. Furthermore, descriptions of well-known structures and technologies are omitted in the following description to avoid unnecessarily obscuring the concepts of this application.
[0034] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the scope of this application. The terms “comprising,” “including,” etc., as used herein indicate the presence of features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0035] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0036] This application provides a code generation method, apparatus, device, medium, and program product. According to an embodiment of this application, after retrieving and enhancing a first code fragment, the first code fragment can undergo multi-dimensional feature extraction using the same feature extraction pipeline as the code knowledge base construction phase. Then, it can be searched multiple times within the code knowledge base. Based on the search results, the fit between the first code fragment and the code knowledge base is evaluated, and the first code fragment is output when the fit exceeds a threshold. This approach ensures, to a certain extent, the consistency between the generated first code fragment and existing code development specifications or standards. Furthermore, since the fit is evaluated based on search results from the code knowledge base, the evaluation results are more comprehensive than those based on summarized development specifications or standards. Moreover, the evaluation results can adapt to constantly changing code development styles when the code knowledge base is dynamically updated, thereby effectively ensuring the consistency between generated code and existing code, and improving the standardization and efficiency of code development.
[0037] It should be noted that the code generation method and apparatus determined in the embodiments of this application can be used in the field of fintech, or in any field other than fintech. This application does not limit the application field.
[0038] As used in this paper, the term "model" refers to a model that learns the relationship between inputs and outputs from training data, enabling it to generate corresponding outputs for a given input after training. Model generation can be based on machine learning techniques. Deep learning is a machine learning algorithm that processes inputs and provides corresponding outputs using multiple layers of processing units. A neural network model is an example of a deep learning-based model. In this paper, "model" may also be referred to as a "machine learning model," "learning model," "machine learning network," or "learning network," and these terms are used interchangeably.
[0039] In one or more embodiments described herein, the term "large model" can refer to a deep learning model with a large number of model parameters, which can include hundreds of millions, tens of billions, hundreds of billions, trillions, or even tens of trillions of model parameters. Large models can also be called foundational models or basic models. They are pre-trained using large-scale unlabeled corpora to produce pre-trained models with hundreds of millions of parameters. Such models can adapt to a wide range of downstream tasks and have good generalization ability, such as large language models and multimodal pre-trained models. It should be understood that in practical applications, large models only require a small number of samples to fine-tune the pre-trained model before being applied to different tasks. Large models can be widely used in natural language processing, computer vision, and other fields. Specifically, they can be applied to computer vision tasks such as visual question answering, image captioning, and image generation, as well as natural language processing tasks such as text-based sentiment classification, text summarization, and machine translation. Major application scenarios for large models can include digital assistants, intelligent robots, search, online education, office software, e-commerce, and intelligent design.
[0040] Figure 1 The illustrations depict application scenarios of the code generation method, apparatus, device, medium, and program product according to embodiments of this application.
[0041] like Figure 1 As shown, application scenario 100 according to an embodiment of this application may include a first terminal device 101, a second terminal device 102, a code knowledge base 103, a network 104, and a server 105. The network 104 is a medium used to provide a communication link between the first terminal device 101, the second terminal device 102, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links or fiber optic cables. For example, a user can use the first terminal device 101 and the second terminal device 102 to interact with the server 105 through the network 104 to receive or send information, etc. The code knowledge base 103 can be connected to the server 105 through a communication link. The server 105 can retrieve the required data from the code knowledge base 103 for processing based on requests from the first terminal device 101 and the second terminal device 102, and can also synchronously store new data generated by the operations of the first terminal device 101 and the second terminal device 102 into the code knowledge base 103.
[0042] The first terminal device 101 and the second terminal device 102 can be electronic devices such as smartphones, wearable devices, personal computers, intelligent voice interaction devices, smart home appliances, intelligent vehicles, in-vehicle terminals, aircraft, unmanned vending terminals, and extended reality devices. Extended reality devices can include virtual reality devices, augmented reality devices, and mixed reality devices. The first terminal device 101 and the second terminal device 102 can have software development tool clients (such as integrated development environment tool clients) and large model client applications installed.
[0043] Server 105 can be a server providing various services. The server can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing cloud computing services such as cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks, and basic cloud computing services such as big data. The server can also be a backend server for integrated development environment (IDE) tools, providing backend services for software development. In one embodiment, server 105 can deploy a large model. In some embodiments, the large model application can provide an interface to interact with the IDE, or the large model application can be integrated into the IDE.
[0044] Code Knowledge Base 103 is a specialized storage system for storing and managing filtered second-generation code snippets, such as a storage system used by enterprises to consolidate their high-quality code snippets. Code Knowledge Base 103 can support heterogeneous data storage types, such as vector knowledge bases, structured databases, and knowledge graphs, and correspondingly supports retrieval via different paths, such as vector similarity retrieval, keyword matching, and graph data retrieval.
[0045] It should be noted that the code generation method provided in this application embodiment can generally be executed by server 105. Correspondingly, the code generation device provided in this application embodiment can generally be located in server 105. It should be understood that... Figure 1 The number of terminal devices, networks, databases, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, databases, and servers can be included.
[0046] The following combination Figure 1 The application scenarios will be described in detail for the code generation method and apparatus of the embodiments of this application.
[0047] Figure 2 A flowchart illustrating a code generation method according to an embodiment of this application is shown schematically. Figure 2 As shown, the code generation method according to this embodiment may include steps S210 to S250.
[0048] In step S210, based on the original query statement input by the user, a first code fragment is generated using a large model through retrieval enhancement, with the code knowledge base as the external knowledge source.
[0049] In step S220, the first code segment is subjected to feature extraction using the same feature extraction pipeline as the code knowledge base 103 construction phase to form a first multi-dimensional feature representation; wherein, the code knowledge base 103 stores at least one second code segment and a second multi-dimensional feature representation formed by feature extraction of the second code segment using the feature extraction pipeline.
[0050] In this paper, the "feature extraction pipeline" belongs to feature engineering and refers to the processing flow for extracting features from code snippets, including a series of sequential or parallel processing steps. The input to the "feature extraction pipeline" is a code snippet or a preprocessed code snippet, and the output is a multi-dimensional feature representation.
[0051] In this paper, "multi-dimensional feature representation" refers to vectors, structured data (such as structured identifiers), or graph data used to comprehensively characterize the multi-dimensional features of a code segment. "Multi-dimensional features" refers to code features extracted from multiple dimensions, including but not limited to: functional semantic features, structural semantic features, and contextual features. Functional semantic features, structural semantic features, and contextual features are extracted based on information from different dimensions of the code segment, as detailed below.
[0052] In step S230, an N-way search is performed in the code knowledge base 103 using the first multi-dimensional feature representation to obtain search results, where N is an integer greater than or equal to 2. This N-way search includes, but is not limited to, vector similarity retrieval and pattern matching checks. Pattern matching checks can obtain results through keyword retrieval or graph data retrieval. Search results can also include various heterogeneous information, such as one or more second code segments (i.e., similar code segments to the first code segment) whose similarity to the first code segment meets preset conditions, or various structured identifiers (such as keywords) or graph structure information (such as call relationships) that successfully match certain identifiers (such as functions, specific classes, or meta-information such as the framework used or inheritance relationships) in the first code segment.
[0053] In step S240, based on the above search results, the fit between the first code fragment and the code knowledge base 103 is evaluated. The fit is used to quantify the stylistic consistency between the first code fragment and the code knowledge base 103 in various aspects (such as coding style, code structure, and adopted standards). For example, various heterogeneous information in the search results can be encoded and converted, such as into numerical information, and then these numerical information can be used to evaluate the fit between the first code fragment and the code knowledge base 103. For example, a score at the code text level can be obtained based on the number of similar code fragments, the degree of similarity, and the quality of the similar code fragments themselves. A corresponding score can also be derived based on the importance or frequency of use of the matched structured identifiers or graph structure information in the code knowledge base 103. These scores obtained from different dimensions are then weighted to calculate the fit.
[0054] In step S250, when the fit is greater than or equal to a preset fit threshold, the first code snippet is output. In this way, the consistency of the output first code snippet with the code style in the code knowledge base 103 can be ensured to a certain extent.
[0055] According to the embodiments of this application, by performing feature extraction on the first code fragment using the same feature extraction pipeline as the code knowledge base 103 construction phase, and then performing multi-path retrieval in the code knowledge base 103, not only can multi-path and multi-dimensional retrieval results be generated, but also the accuracy of the retrieval results can be improved because the feature extraction method is the same as the feature extraction method of the code fragments stored in the code knowledge base 103. Based on this retrieval result, the fit between the first code fragment and the code knowledge base 103 can be quantified, and the consistency between the first code fragment and the enterprise's existing code development specifications or standards can be more accurately grasped, thereby providing accurate guidance for adopting or improving the first code fragment.
[0056] Furthermore, in this embodiment, the fit of the first code segment is evaluated by matching the first code segment with the code knowledge base 103, rather than by comparing the first code segment with the summarized development specifications or standards. This improves the comprehensiveness and objectivity of the evaluation results. In addition, as the code knowledge base 103 is constantly updated, the fit evaluation results can adapt to the ever-changing code development styles, avoiding the rigidity and lag that may exist when comparing based on the summarized development specifications or standards.
[0057] In some embodiments, the first code fragment obtained in step S210 is generated by the large model using the code knowledge base 103 as an external knowledge source through retrieval enhancement. This allows for the evaluation of the fit between the code generated by the large model and the code knowledge base 103 according to the embodiments of this application. In some embodiments, when the fit is found to be less than a preset fit threshold, the large model can be used to correct the first code fragment based on the retrieval results or output correction suggestions. This can promptly detect, reduce, or avoid the problem of the large model learning bad code patterns, resulting in inconsistent style between the generated code and the enterprise's existing codebase, thus improving the quality of the code generated by retrieval enhancement. In one embodiment, the corrected code fragment from the large model can be used as the new first code fragment, then evaluated through steps S220-S240, and output only when the fit is greater than or equal to the fit threshold. In another embodiment, correction suggestions can be given simultaneously with the output of the first code fragment in step S250, allowing developers to decide whether correction is necessary.
[0058] Figure 3 The illustration schematically shows the processing flow of step S220, which involves extracting features of a first code fragment, in a code generation method according to an embodiment of this application.
[0059] like Figure 3 As shown, step S220 includes steps S221 to S224.
[0060] In step S221, the functional semantic features of the first code segment are extracted based on the text of the first code segment.
[0061] Functional semantic features are vector representations and / or structured identifiers that characterize the semantic information (such as functions, classes, parameter / variable names, key comments, etc.) that the code implements for business logic or computational tasks. They are usually generated based on identifiers, comments, and natural language descriptions in the code.
[0062] In step S222, structural semantic features of the first code segment are extracted based on the abstract syntax tree and control flow graph of the first code segment. The structural semantic features are vector representations, structured identifiers, and / or graph data that characterize the internal implementation pattern, syntax structure, or specific idioms of the code, and are typically generated based on the abstract syntax tree path, API call sequence, or control flow pattern.
[0063] In some embodiments, extracting a first structural feature from the abstract syntax tree of the first code segment can capture the code's syntactic combination patterns and application process interface (API) usage habits. Simultaneously, extracting a second structural feature from the control flow graph of the first code segment can capture the program code's logical organization and decision-making patterns. Then, the first and second structural features are uniformly encoded using a trained encoding model to obtain the structural semantic features of the first code segment.
[0064] In other embodiments, in addition to the first and second structural features, a third structural feature can be extracted from the text of the first code fragment based on preset rules to directly capture the use of a specific framework or general standard case. Then, the first, second, and third structural features of the first code fragment are uniformly encoded using an encoding model to obtain the structural semantic features of the first code fragment, thereby capturing more comprehensive structural features.
[0065] In step S223, the contextual features of the first code segment are extracted based on the meta-information of the first code segment (such as technology stack tags, the framework used, and possible API call sequences).
[0066] Contextual association features are structured data that represent the module, technology stack, and relationships between code and other code entities (such as classes and methods) in a project, such as calls and inheritance.
[0067] In step S224, a first multi-dimensional feature representation is formed based on the functional semantic features, structural semantic features, and contextual features of the first code segment.
[0068] In this way, during code generation, this embodiment of the application achieves comprehensive and multi-dimensional analysis of the first code segment by reusing the feature extraction pipeline built during the code knowledge base 103 construction. Furthermore, since the feature extraction method is the same as that used for the second code segment stored in the code knowledge base 103, it makes retrieving from the code knowledge base 103 using the first multi-dimensional feature representation more convenient, provides conditions for retrieving through different paths, and results in more accurate comparisons, thus making the evaluation results more reliable.
[0069] According to embodiments of this application, multi-dimensional feature representations may include vectors and structured identifiers. Accordingly, in one embodiment, the above-described step S230, which involves N-way retrieval in the code knowledge base 103, can be as follows: Figure 4 As shown.
[0070] Specifically, such as Figure 4 The step S230 shown may include steps S231 and S232.
[0071] In step S231, during one-way retrieval, based on the vector similarity in the first multi-dimensional feature representation and the second multi-dimensional feature representation, vectors that meet the preset conditions are retrieved from the code knowledge base 103, and the similar code segments corresponding to the retrieved vectors and their vector similarity values are output.
[0072] It should be noted that when performing vector similarity retrieval, the vectors used can be vectors corresponding to functional semantic features or structural semantic features. Therefore, the retrieved similar code fragments will tend to be fewer second code fragments that are functionally or structurally similar to the first code fragment. Alternatively, in some embodiments, the vectors used for vector similarity retrieval can be comprehensive semantic vectors resulting from the fusion of functional and structural semantic features. This results in the retrieved similar code fragments being second code fragments that are similar to the first code fragment after the fusion of functional and structural semantic features.
[0073] In some embodiments, retrieval can be performed based on the vectors corresponding to features of different dimensions (such as functional semantic features and structural semantic features) in the first multi-dimensional feature representation, and the corresponding retrieval results can be obtained.
[0074] In step S232, during one-way retrieval, based on the comparison of the structured identifiers in the first multi-dimensional feature representation and the second multi-dimensional feature representation, the successfully matched identifier information is retrieved from the code knowledge base 103.
[0075] It should be understood that Figure 4 The steps S231 and S232 shown in the diagram are performed sequentially as an illustration. Steps S231 and S232 are independent of each other and can be executed in parallel or in a different order.
[0076] In this embodiment, by performing multi-way searches in the code knowledge base 103, not only can similar code segments of the first code segment be retrieved, but also matching identifiers of key identifiers (such as function names, frames, etc.) within the first code segment that have been successfully matched can be retrieved. This allows for multi-dimensional fit evaluation and analysis, not just limited to code similarity. Since new code is often applicable to new projects, differences from existing code are inevitable. Therefore, evaluating fit solely from the perspective of similar code segments would negatively impact the scientific rigor of the new code quality assessment. This embodiment uses matching identifier information to compensate for the shortcomings of analyzing solely from a similarity perspective, effectively improving the value and guiding role of the evaluation results.
[0077] Figure 5 The flowchart illustrating step S240 of the code generation method according to an embodiment of this application, which evaluates the fit between the first code fragment and the code knowledge base 103, is shown.
[0078] like Figure 5 As shown, step S240 may include steps S241 to S244.
[0079] In step S241, a semantic similarity score is obtained based on the vector similarity values of the similar code fragments. The similar code fragments retrieved in step S231 can be multiple, or they can be retrieved based on vectors corresponding to features of different dimensions.
[0080] Therefore, in step S241, a corresponding processing strategy can be pre-set based on the specific circumstances of the similar code fragments retrieved in step S231.
[0081] For example, assuming that multiple similar code snippets are obtained based on comprehensive semantic vector retrieval, the average or maximum value of the vector similarity values of these similar code snippets can be used to calculate the semantic similarity score.
[0082] For example, after searching based on functional semantic vectors and structural semantic vectors respectively, the similar code fragments obtained by each method can be analyzed first to obtain a score, and then weighted to form a semantic similarity score.
[0083] In step S242, a pattern consistency score is obtained based on the matching identification information and the preset scoring rules in the code development pattern specification corresponding to the code knowledge base 103.
[0084] In step S243, a static compliance score is obtained based on the analysis of the first code snippet using a static analysis tool. For example, the static compliance score can be calculated by checking hard indicators such as coding standards and security performance.
[0085] In step S244, the fit is obtained by weighting the semantic similarity score, pattern consistency score, and static compliance score.
[0086] In this way, the stylistic consistency between the first code snippet and the code knowledge base 103 can be evaluated from multiple dimensions (including but not limited to: similarity, consistency of usage standards and security), which can achieve a more comprehensive and objective evaluation and improve the credibility and value of the evaluation results.
[0087] Figure 6 The illustration schematically depicts a system architecture for generating code via retrieval enhancement according to one embodiment of this application.
[0088] like Figure 6 As shown, the system architecture includes an offline knowledge base building device 300, an online code retrieval and enhancement generation device 400, and a code evaluation device 500.
[0089] The offline knowledge base building device 300 is used to filter, analyze, and index code repositories during the offline building phase to build a code knowledge base 103.
[0090] The online code retrieval enhancement generation device 400 is used for online code retrieval enhancement generation, including real-time retrieval of the code knowledge base 103 and dynamic construction of structured prompt words to assist the large model in generating code (such as the first code snippet) when developers are writing code using code writing tools.
[0091] The code evaluation device 500 is used to perform post-verification on the code generated by the online code retrieval enhancement generation device 400. The code evaluation device 500 can perform reference... Figures 2-5 The described evaluation method evaluates the code generated through retrieval enhancement to determine the degree of fit between the generated code and the code knowledge base 103.
[0092] It is understood that in some embodiments, the code evaluation device 500 can be integrated with the online code retrieval enhancement generation device 400, wherein the online code retrieval enhancement generation device 400 generates code, which is then evaluated and verified by the code evaluation device 500 before being output. For example, the code is output only when the fit is greater than a preset fit threshold, or when the fit is less than a preset fit threshold, the code is corrected by a larger model before being output.
[0093] Figure 7 The diagram illustrates the process of an offline knowledge base building device 300 building a code knowledge base 103. For example... Figure 7 As shown, the process of the offline knowledge base construction device 300 constructing the code knowledge base 103 may include steps S310 to S340.
[0094] In step S310, code acquisition and quality screening are performed.
[0095] It can collect code from one or more source code repositories and automatically filter out "high-quality code snippets" (such as functions, classes, and method blocks) through a multi-dimensional code quality assessment system.
[0096] The evaluation dimensions include, but are not limited to, static compliance analysis, maintainability indicators, business and operational indicators, and expert manual calibration, etc.
[0097] Static compliance analysis refers to using static analysis tools to check whether code conforms to preset coding standards or security rules. Maintainability metrics include, but are not limited to: cyclomatic complexity, code duplication rate, and comment density.
[0098] Business and operational metrics include, but are not limited to: associated commit history, failure rate, and performance benchmarks. Expert manual labeling can support architects in tagging specific code with labels such as "standard case" or "core logic."
[0099] For each collected code segment (or code snippet), the values of multiple evaluation dimensions are weighted according to preset rules and then processed to obtain a score for that code snippet. Code snippets with scores above a preset threshold are then considered high-quality code (i.e., the "second code snippet" mentioned above) and enter the subsequent feature extraction pipeline.
[0100] In step S320, code features are extracted using a feature extraction pipeline. This feature extraction pipeline is used in the construction phase of the code knowledge base 103. As described in step S220 above, the feature extraction pipeline in step S320 is the same as the feature extraction pipeline in step S220.
[0101] Specifically, during the construction phase of the code knowledge base 103, this feature extraction pipeline is used to perform multi-level feature extraction and analysis on the collected second code snippets. Specifically, each second code snippet can be subjected to in-depth analysis to extract functional semantic features, structural semantic features, and contextual features.
[0102] In terms of functional semantic feature extraction, text summarization can be performed first. This involves extracting or generating a natural language text describing the function of a code snippet (e.g., the function is used to query an order list based on user ID and month, and calculate the total amount). Then, semantic vectorization is performed. The text summary is input into a pre-trained text encoding model, which outputs a fixed-dimensional functional semantic vector. Code snippets with similar positions in the semantic space have similar functions. A text encoding model is a machine learning model that maps variable-length natural language text (such as words, sentences, or paragraphs) to fixed-length, dense real-valued vectors (i.e., embedding vectors). This model captures the semantic information of the text, ensuring that semantically similar texts are close in distance in the vector space. In some embodiments, in addition to outputting the functional semantic vector, functional semantic feature extraction can also extract functions, class names, and key comments from the code snippet as keywords to constitute a textual representation or structured identifier of the functional semantic features.
[0103] In terms of structural semantic feature extraction, code parsing and path extraction can be used to generate an abstract syntax tree and control flow graph of the code using a language-specific parser. Then, first structural features are extracted from the abstract syntax tree, such as the code's syntactic combination patterns and API call chains, while second structural features are extracted from the control flow graph, such as the code's organizational logic and decision-making patterns (e.g., various graph theory indicators, predefined logical patterns). These extracted first and second structural features are then converted into structural semantic vectors using another encoding model (or jointly trained with the encoding model used for functional semantic feature extraction). These vectors capture the code's implementation style and patterns. In other embodiments, in addition to structural semantic vector representations, the structural semantic features of the code can also selectively include some or all of the extracted structural information's identifiers, processed into structured identifiers represented as text.
[0104] In terms of contextual feature extraction, we can first extract the metadata of the code snippets, such as the file path, project name, technology stack tags, framework annotations, etc., and then perform lightweight static analysis to establish call relationships and inheritance relationships between code entities, forming associated data. This associated data can be encoded into vectors using the encoding model (or other encoding models) used in functional semantic feature extraction to form a vector representation of contextual features, or it can be processed into structured identifiers (such as storing them as fields or key-value pairs). Alternatively, this associated data can be stored in a knowledge graph, such as processing the relationships between entities as edges in the knowledge graph, processing project names, technology stack tags, etc., as edges in the knowledge graph, and associating them with the code snippets.
[0105] In one embodiment, structural semantic feature extraction may include three main steps: code parsing and standardization, multi-source structural feature extraction, and multi-source feature representation fusion.
[0106] First, code parsing and standardization. Input a code snippet (such as a method or function), then perform lexical and syntactic analysis using a language-specific parser. Output: Abstract syntax tree (AST): a tree-like representation of the code's syntactic structure; and a control flow graph: a directed graph representing possible paths during program execution, where nodes are basic code blocks and edges represent control flow transitions (conditional jumps, loops, function calls, etc.).
[0107] Then, multi-source structural feature extraction can extract different types of structural patterns in parallel.
[0108] Abstract syntax tree feature extraction. The goal is to capture the syntactic combination patterns of the code and API usage habits.
[0109] The process of abstract syntax tree feature extraction includes: 1.1) Critical path traversal: Instead of recording the entire abstract syntax tree, meaningful "syntactic paths" are extracted. For example, focusing on paths from the root node to leaf nodes (such as literals, identifiers) or nodes of specific types (such as method calls, loop statements, exception handling blocks); 1.2) Path patterning: Converting paths into symbol sequences. For example, the path TryStatement -> CatchClause -> MethodInvocation(LOGGER.error) in a Java method represents the pattern of "catching exceptions and logging error"; 1.3) API call chain extraction: Specifically extracting method call chains, such as stream().filter().map().collect(), which is a typical structure of functional programming in Java.
[0110] The output of Abstract Syntax Tree Feature Extraction is a set of symbols (path set) representing syntax and API usage patterns.
[0111] Control flow graph feature extraction aims to capture the logical organization and decision-making patterns of program code.
[0112] The process of control flow graph feature extraction includes: 2.1) Graph metric extraction: Calculating graph theory metrics, such as cyclomatic complexity (measuring the number of logical branches), average path length, and node in-degree / out-degree distribution. These metrics quantify the complexity of the code and the control flow structure; 2.2) Pattern subgraph matching: Identifying common predefined logical patterns in the control flow graph; for example, error handling patterns: such as the specific subgraph structure formed by try-catch-finally blocks in the CFG; resource management patterns: such as the subgraph formed by try-with-resources statements in Java or using statements in C#; loop patterns: such as the control flow structure of for, while, or do-while loops.
[0113] The output of control flow graph feature extraction is a set of numerical metrics and matched pattern identifiers.
[0114] API and idiom pattern feature extraction. The goal is to directly capture the use of specific frameworks or common standard cases.
[0115] The process of extracting features from APIs and idiom patterns can include: matching based on preset rules to identify whether the code text contains: annotations of specific frameworks, implementation elements of specific design patterns (such as the creator interface in the factory method), concurrency control primitives, or calls to internal enterprise utility classes or software development tools.
[0116] The output of API and idiom pattern feature extraction is a binary or multi-valued feature vector, where each dimension represents the presence or frequency of use of a specific pattern or API.
[0117] Next, we perform multi-source feature representation fusion. The goal is to uniformly encode the heterogeneous features (such as symbol sequences, numerical values, and identifiers) from the above sources and the output into a fixed-dimensional structural semantic vector.
[0118] The process of multi-source feature representation fusion can include: a. Sequence vectorization (for abstract syntax tree paths, etc.): Treating symbol sequences such as abstract syntax tree paths as "sentences," and using a code language model optimized for code or a lightweight encoder to convert the sequences into vectors. The model has already learned the semantics of these syntax paths during pre-training; b. Numerical feature concatenation: Normalizing numerical features such as control flow graph metrics and one-hot encoding of pattern identifiers; c. Feature fusion layer: Combining and reducing the dimensionality of the vectorized abstract syntax tree features and the above numerical features through a feature fusion network (such as a simple fully connected layer or a more complex attention mechanism layer), ultimately outputting a unified, dense structural semantic vector. This vector comprehensively encodes the syntax, logic, and data conventions of the code.
[0119] Continue to refer to Figure 7 Next, in step S330, a hybrid index is constructed. The features extracted in step S320 are stored in different index structures to form a "hybrid index". This hybrid index may include, but is not limited to: vector index, keyword inverted index, and graph database index.
[0120] Vector indexing: Functional semantic vectors and structural semantic vectors are stored in a vector database, either separately or by merging (e.g., concatenation or weighted averaging), for approximate nearest neighbor search based on semantic similarity.
[0121] Keyword inverted index: An inverted index is created for function names, class names, API names, and business terms. It is used to handle exact name matching or keyword queries. The inverted index is a widely used index data structure for full-text search. Its core idea is to establish a mapping from "terms" to a "list of documents" containing those terms. Unlike a forward index that lists terms by document, an inverted index indexes documents by term.
[0122] Graph database index: Code entities (such as classes and methods) are stored as nodes, and their calls, inheritance, and ownership relationships are stored as edges in a graph database. This is used to perform complex relationship queries and expansions.
[0123] Finally, in step S340, the code knowledge base 103 is updated. An incremental update mechanism is designed. When a new second code snippet is submitted to the code knowledge base 103, or when the coding standards change, a re-analysis and index update of specific parts of the code knowledge base 103 can be automatically or manually triggered.
[0124] Figure 8 The illustration schematically depicts the processing flow of code generation by the online code retrieval enhancement generation apparatus 400 according to an embodiment of this application. For example... Figure 8 As shown, the process of generating code by the online code retrieval enhancement generation device 400 through retrieval enhancement may include steps S410 to S470.
[0125] In step S410, the user inputs the original query statement, such as a natural language description.
[0126] In step S420, the original query statement is expanded to obtain an enhanced query statement.
[0127] In one embodiment, the process of expanding the original query statement can be found in [reference needed]. Figure 9 .
[0128] Specifically, when a developer inputs a natural language description in a code writing tool to trigger code generation, the system collects multi-source context information.
[0129] Multi-source context information includes the following aspects: explicit requirements, which refer to the natural language description entered by the developer (also known as the original query statement); implicit context, such as the content of the currently edited file, the code before and after the cursor, file paths, or project dependency files; and intent inference, such as inferring possible behaviors (e.g., implementing method bodies or writing test cases) based on the cursor position (e.g., within a function body or after a class definition).
[0130] In some embodiments, when automatically capturing multi-source context information, various key context information can be automatically and silently collected in the background through integrated development environment plugins or deep integration, without requiring any additional operations from the developer.
[0131] For explicit requirements: These are requirements described by the user in natural language within the integrated development environment (IDE) tool window (such as the sidebar) or in an input box accessed via a keyboard shortcut. This is the only content the user needs to actively input.
[0132] Implicit context can be obtained from the active document, including but not limited to automatically obtaining the complete content of the current file, the context of the cursor position, project and module metadata, or associated code patterns.
[0133] The complete content of the current file can be obtained by automatically analyzing the entire file that the developer is editing.
[0134] The context of the cursor's location can be precisely obtained by locating the code block where the cursor is positioned. For example, if the cursor is writing a function body, it indicates that the user may be implementing specific logic; if the cursor is on a method declaration line in an interface definition, it indicates that the user may need to implement the interface method; and if the cursor is at the end of a class definition, it indicates that the user may need to add a new method to this class.
[0135] Project and module metadata can include the technology stack and project structure. The technology stack can be automatically identified by parsing the project's build files, including the main frameworks, libraries, and their versions. The project structure can be identified by the module and package path of the current file.
[0136] Correlated code patterns can identify the encoding patterns of the current file or adjacent files through real-time lightweight analysis. For example, it can identify whether the current project uses a certain type or certain specific exception handling templates.
[0137] After processing the above information from multiple sources of context, an enhanced query statement is generated. For example, the original query statement is "parse JSON", and combined with the current project library name, the enhanced query becomes "parse JSON objects using the xx library". Subsequently, the enhanced query statement can be fed into a text encoding model that extracts functional semantic features to generate a query vector. This transforms the original, vague user requirement into a precise, context-rich retrieval instruction.
[0138] In step S430, based on the enhanced query statement, the recall result is obtained from the code knowledge base 103 through M-way parallel recall. The M-way parallel recall path is the same as or not exactly the same as the N-way retrieval path, where M is an integer greater than 2.
[0139] M-path parallel recall (also known as multi-path parallel recall) refers to retrieval based on different indexes executed in parallel. Retrieval can be performed separately using vector indexes, keyword indexes, and graph indexes, and the results can then be aggregated.
[0140] The vector retrieval path uses query vectors obtained based on enhanced query statement encoding to perform similarity searches in vector indexes (such as functional semantic vectors) and recall the top k semantically related code snippets.
[0141] Keyword retrieval paths can extract keywords from enhanced queries, search in the inverted index, and retrieve precisely matching segments.
[0142] Graph association retrieval path: You can start from the current file or class and explore related high-quality code nodes in the graph database.
[0143] It is understandable that the retrieval path used in the multi-path parallel recall in step S430 can be the same as the retrieval path used in evaluating code fragments in step 230, for example, both can use keyword retrieval or functional semantic vector retrieval. Alternatively, since the retrieval purposes and input data are different, the retrieval paths can also differ. For example, when performing vector indexing in step S230, structural semantic vectors can be used, or a comprehensive semantic vector fused from functional semantic vectors and structural semantic vectors can be used for retrieval, while in step S430, since there are no code fragments in the enhanced query statement, structural semantic vectors cannot be used for retrieval.
[0144] Next, in step S440, the second code fragments in the recall results are sorted to form a sorting result.
[0145] In step S450, multiple target code segments that meet preset preference criteria are selected based on the sorting results. This process involves uniformly and finely sorting all recall results to select the most relevant (e.g., Top k) and highest quality second code segment.
[0146] Here, we will use three retrieval paths—vector retrieval path, keyword retrieval path, and graph association retrieval path—as examples to illustrate the sorting process in steps S440 and S450. The sorting process can be summarized as follows: the results of the three retrieval paths are merged, input into a lightweight re-ranking model / rule engine for unified sorting, and finally output a list of code snippets sorted by comprehensive relevance.
[0147] According to an embodiment of this application, the sorting in step S440 is not performed using a fixed weight formula, but rather by: identifying the application scenario, selecting a corresponding sorting strategy based on the application scenario, and then sorting the second code fragments in the recall results according to the selected sorting strategy. The sorting strategy includes various sorting factors and sorting logic used for sorting. The sorting logic may include, but is not limited to, weight configuration information, threshold conditions, rejection rules, or procedures.
[0148] The ranking factor is derived based on the attribute information of each second code snippet. For example, the ranking factor may include, but is not limited to: vector similarity score, matching degree with the current project's technology stack, the quality score of the code snippet itself, time freshness, or the number of times it has been cited.
[0149] The weight configuration information can be a fixed weight allocation preset for the application scenario, or a dynamic weight allocation scheme set for the application scenario. For example, weights can be allocated according to the conditions satisfied by the values of each ranking factor (such as whether they are within a certain range).
[0150] like Figure 10As shown, in one embodiment, step S440 may include steps S441 to S443.
[0151] In step S441, based on the enhanced query statement, the target application scenario is matched from multiple application scenarios. The application scenario to which the current code generation task belongs can be determined by analyzing the contextual information and keywords in the enhanced query statement.
[0152] In step S442, the target ranking strategy corresponding to the target application scenario is obtained, where different application scenarios have different preset ranking strategies. Based on the identified application scenario, a set of ranking factors and their weight configurations for that application scenario are selected from the strategy library or dynamically generated, thereby obtaining the target ranking strategy. Table 1 illustrates some application scenario classifications, the judgment criteria for each scenario, and the weight configuration principles of the ranking factors.
[0153] Table 1
[0154]
[0155] Application scenario matching can be achieved in various ways. For example, keyword / pattern matching can perform lightweight natural language analysis on queries and contexts, extracting keywords and matching them with a scenario dictionary. Another example is context-based meta-information matching: it can analyze current file paths, class / method annotations, project configuration files, etc. Or, for example, it can use machine learning classifiers to collect historical interaction data, train a scenario classification model, and achieve more accurate judgments.
[0156] The following are examples of how each ranking factor is calculated in Table 1.
[0157] Functional relevance score can be considered as the raw similarity score from vector retrieval. Technology stack matching score can be considered as the explicit matching degree between the framework and library version used in the code snippet and the current project. Technology stack matching score can be further subdivided into: perfect match, indicating that the versions are the same; compatibility match, indicating that the major versions are compatible; and concept match, indicating that although the libraries are different, the design patterns are similar.
[0158] The code quality score can be taken as the overall quality score calculated in the offline phase.
[0159] Time freshness is the last update time of the code snippet.
[0160] Popularity / citation count refers to the frequency with which a code is referenced by other code within an enterprise's internal codebase.
[0161] Business domain matching degree refers to the degree of matching between the business module to which the code snippet belongs (such as "finance" or "user center") and the current project.
[0162] The security compliance label refers to whether a security scan has been passed or whether it has been marked as a "security standard case".
[0163] Performance metrics (if available), performance benchmarks or complexity metrics of the function represented by the code snippet.
[0164] Readability and maintainability refer to factors such as the completeness of comments, naming conventions, and complexity.
[0165] Expert approval rating refers to whether it has been manually marked as "recommended" by architects or technical leads.
[0166] In step S443, the second code snippets in the recall results are sorted based on the target sorting strategy. For example, the results are finally sorted according to the weights, thresholds, veto rules and sorting logic (such as weighted summation, priority cascading sorting) in the target sorting strategy, so as to a large extent ensure that the most relevant and suitable code snippets in the target application scenario are ranked first.
[0167] In one embodiment, the values of each second code segment in the recall results on each ranking factor can be extracted, and the values of each second code segment on each ranking factor can be weighted according to the weight configuration in the target ranking strategy to obtain the ranking score of each second code segment in the recall results. Then, the second code segments in the recall results are ranked based on the ranking scores.
[0168] If multiple target application scenarios are matched in step S441, it means that the system determines that a mixed scenario may be involved. In this case, in some embodiments, multiple target application scenarios can be sorted in parallel and then selected based on the best result. In other embodiments, strategy fusion can also be used, such as weighted fusion of preset sorting strategies for multiple target application scenarios to obtain the target sorting strategy in step S442. The weight settings when weighted fusion of preset sorting strategies for multiple target application scenarios can be determined according to some preset rules or formulas, such as calculation based on the probability of matching each application scenario and the attribute differences of different application scenarios (e.g., security scenarios have higher weights than others) according to preset formulas.
[0169] In step S460, prompt words are generated based on multiple target code snippets and enhanced query statements.
[0170] In step S470, the prompt word is input into the large model, and the first code snippet output by the large model is obtained.
[0171] Specifically, in this embodiment, instead of directly returning the list of code snippets [L-1, L-2, ..., Lk] generated in step S450, it uses them as "examples" and "constraints" together with the enhanced query statement to generate structured prompts. Then, the structured prompts are sent to the large language model to guide it to generate the code expected by the user and to obtain the generated code or code completion generation suggestions.
[0172] In this embodiment, after the online code retrieval enhancement generation device 400 generates a code snippet, the code evaluation device 500 can evaluate the code snippet, including evaluating the style fit of the code snippet with the code knowledge base 103 and whether there are any quality issues. If a serious violation (such as a security flaw) is found, it can be automatically corrected or a warning can be added. A feedback interface is provided, allowing developers to evaluate the search results and generated code as "useful / useless." This feedback can be used to optimize search ranking and knowledge base quality.
[0173] The core principle of the code evaluation device 500 is to reuse the feature extraction pipeline in the offline knowledge base construction device 300 to analyze the code snippets generated by the online code retrieval enhancement generation device 400, and compare their features with the feature distribution of the second code snippets already stored in the code knowledge base 103. By quantifying the degree of deviation from the "standard code pattern" in the code knowledge base 103, its intrinsic quality and style fit are evaluated.
[0174] Specifically, after acquiring the code fragments generated by the online code retrieval enhancement generation device 400, the code evaluation device 500 can use each acquired code fragment as a first code fragment, and then perform feature extraction and vectorization through the previously described step S220. Specifically, the first code fragment is fed into a feature extraction pipeline identical to that of the offline knowledge base construction device 300 for processing, including: parsing and generating its abstract syntax tree; extracting its functional semantic features and structural semantic features; identifying its technology stack tags, possible API call chains, and other contextual features; and then fusing the aforementioned functional semantic features, structural semantic features, and contextual features to generate a multi-dimensional feature representation of the first code fragment (i.e., the aforementioned first multi-dimensional feature representation).
[0175] Next, the code evaluation device 500 searches the code knowledge base 103 through step S230 described above. Based on the similarity retrieval and comparison in the code knowledge base 103, the goal is to find similar code to the first code fragment in the code knowledge base to determine whether it "looks" like the recognized code in the code knowledge base 103. N-way retrieval can be performed in the code knowledge base 103, such as vector similarity retrieval and pattern matching checks. Vector similarity retrieval can be a comprehensive semantic vector generated by fusing (e.g., concatenating or weighted averaging) functional semantic vectors representing functional semantic features and structural semantic vectors representing structural semantic features. This vector is then quickly searched in the vector index of the code knowledge base 103 to find the Top-k similar code fragments [C_ref1, C_ref2, ..., C_refk] after sorting them from high to low similarity, and their vector similarity values [S1, S2, ..., Sk] are obtained. Pattern matching checks can involve matching the structural semantic features of the first code snippet (such as specific abstract syntax paths or API call chains represented by structured identifiers or serialization) against "high-frequency high-quality patterns" or "specific pattern rule bases" stored in the code knowledge base 103 in structured identifiers or serialization. This checks whether recommended patterns are used or whether anti-patterns that should be avoided are included.
[0176] Next, the code evaluation device 500, through step S240 described above, quantifies the fit based on multi-dimensional information integrated from the search results. For example, it can calculate a comprehensive fit score based on the search results.
[0177] The fit score can be weighted by the following dimensions: semantic similarity score, pattern consistency score, contextual consistency score, and static compliance score.
[0178] The semantic similarity score, Score_semantic, can be the average or highest value of the vector similarity values [S1, S2, ..., Sk] of the top-k similar code snippets. For example, the average value can be calculated as: Score_semantic = mean(S1, S2, ..., Sk). The semantic similarity score assesses the overall similarity between the first code snippet and a second code snippet selected from code knowledge base 103 in terms of functional intent and implementation structure style. The higher the score, the more similar the snippets are.
[0179] The pattern consistency score, Score_pattern, is awarded based on the pattern matching results of the first code snippet against the code knowledge base 103. For example, a successful match of a critical business processing pattern earns +10 points, while a match of an anti-pattern earns -20 points. The pattern consistency score assesses whether the first code snippet adheres to the architectural or design patterns required by the code knowledge base 103, which is a higher-level requirement beyond basic coding standards.
[0180] The context compatibility score (Score_context) can be used to check whether the technology stack tags and imported libraries of the first code snippet are compatible with the project context in which the first code snippet is applied. The project context can be obtained from the enhanced query statement retrieval process in step S420 above. The context compatibility score can be used to assess whether the first code snippet can be seamlessly integrated into the current project, avoiding technology stack conflicts, etc.
[0181] The static compliance score, Score_static, is generated by scanning and analyzing the first code snippet using static analysis tools, mapping the severity of problems in the code text to specific scores. As a fundamental guarantee, the static compliance score can be represented by checking hard indicators such as coding standards and security performance.
[0182] The final fit score can be designed as: Score_final = w1 * Score_semantic + w2 * Score_pattern + w3 * Score_context + w4 * Score_static, where the weights w1-w4 can be pre-configured according to priority.
[0183] After obtaining a quantified fit score, the code evaluation device 500 can also provide an evaluation report and intelligent correction suggestions.
[0184] In addition, corrective suggestions can be provided. For example, automatic correction is suitable for simple, well-defined static compliance issues (such as missing the `final` keyword), and can directly provide corrected code. Another example is interactive correction, suitable for complex schema inconsistency issues. It can initiate an interactive session, using the assessment report and reference examples as new context, to re-invoke the large model and guide it to rewrite the original generated code accordingly. For example, the prompt might change to: "Please refactor based on the previously generated code, following the company's 'transaction-log-monitoring' three-stage exception handling model (see the example below)."
[0185] After the above evaluation, the code evaluation device 500 can further add a feedback loop, such as collecting the evaluation results and subsequent actions of developers as feedback information to optimize the retrieval enhancement generation in the online code retrieval enhancement generation device 400. For example, direct feedback can be collected, including developers' "acceptance," "edited acceptance," or "rejection" actions on the generated code, which are recorded and associated with the characteristics of the generated code and the retrieval results. Alternatively, passive quality signals can be collected. If the generated code is submitted directly to the code repository without modification and passes the quality gate after a period of time, the code can be automatically marked as a new "candidate high-quality code," and after review, it enters the code knowledge base 103, realizing the automatic evolution of the code knowledge base 103. The collected feedback information can be used for model optimization. For example, generated cases with "high relevance and high acceptance rate" and their complete retrieval and suggestion contexts can be used as high-quality training data for future fine-tuning of suggestion strategies or fine-tuning of the large model itself.
[0186] In this embodiment, the large model generates code through retrieval enhancement using code knowledge base 103 as an external knowledge source, achieving non-intrusive adaptation. It can output code that meets specific enterprise requirements without resource-intensive fine-tuning of the basic large language model, resulting in low cost and rapid implementation. Furthermore, it enhances knowledge interpretability and manageability; the generated code has a clear reference source (high-quality code retrieved from within the enterprise), facilitating traceability and review. Moreover, enterprise coding standards are preserved and managed as "retrievalable knowledge." The knowledge base can also evolve dynamically, continuously updating along with the development of the enterprise's codebase and standards, ensuring that code generation capabilities evolve in sync.
[0187] Furthermore, in this embodiment, the evaluation and feedback from the code evaluation device 500 enable the final code snippets to possess high accuracy and high relevance. Through multi-dimensional feature extraction and context-aware retrieval, it ensures that the provided examples are highly relevant to the current task. The evaluation feedback continuously corrects and improves the code generation style, specifications, and patterns, ensuring that the generated code is highly compatible with the enterprise's existing codebase knowledge base, thus achieving style consistency and compatibility for the enterprise's code.
[0188] Figure 11 A schematic block diagram of a code generation apparatus 600 according to an embodiment of this application is shown.
[0189] like Figure 11 As shown, the code generation apparatus 600 according to this embodiment may include a code generation module 601, a feature extraction module 602, a retrieval module 603, and an evaluation module 604.
[0190] The code generation module 601 is used to obtain the first code fragment. In one embodiment, the code generation module 601 can perform step S210 as described above. In another embodiment, the code generation module 601 can be implemented as the aforementioned online code retrieval enhancement generation device 400, as described above, and will not be repeated here.
[0191] The feature extraction module 602 is used to extract features from the first code segment using the same feature extraction pipeline as the code knowledge base 103 construction phase to form a first multi-dimensional feature representation; wherein, the code knowledge base 103 stores at least one second code segment and a second multi-dimensional feature representation formed by extracting features from the second code segment using the feature extraction pipeline. In one embodiment, the feature extraction module 602 can perform step S220 as described above.
[0192] The retrieval module 603 is used to perform an N-way retrieval in the code knowledge base 103 using the first multi-dimensional feature representation to obtain retrieval results, where N is an integer greater than or equal to 2. In one embodiment, the retrieval module 603 can execute the step S230 described above.
[0193] The evaluation module 604 is used to evaluate the fit between the first code fragment and the code knowledge base 103 based on the above search results, and to output the first code fragment when the fit is greater than or equal to a preset fit threshold. In one embodiment, the evaluation module 604 may execute steps S240 and S250 as described above.
[0194] According to embodiments of this application, any multiple modules among the code generation module 601, feature extraction module 602, retrieval module 603, and evaluation module 604 can be merged into one module, or any one of these modules can be split into multiple modules. Alternatively, at least part of the functionality of one or more of these modules can be combined with at least part of the functionality of other modules and implemented in one module. According to embodiments of this application, at least one of the code generation module 601, feature extraction module 602, retrieval module 603, and evaluation module 604 can be at least partially implemented as hardware circuitry, such as field-programmable gate arrays, programmable logic arrays, systems-on-a-chip, systems-on-a-substrate, systems-on-package, application-specific integrated circuits, or any other reasonable means of integrating or packaging circuitry, or implemented in software, hardware, or firmware, or in any appropriate combination of any of these three implementation methods. Alternatively, at least one of the code generation module 601, feature extraction module 602, retrieval module 603, and evaluation module 604 can be at least partially implemented as a computer program module, which can perform corresponding functions when the computer program module is run.
[0195] Figure 12 A block diagram schematically illustrates an electronic device suitable for implementing the code generation method according to embodiments of this application.
[0196] like Figure 12 As shown, an electronic device 1200 according to an embodiment of this application includes a processor 1201, which can perform various appropriate actions and processes according to a program stored in a read-only memory 1202 or a program loaded from a storage portion 1208 into a random access memory 1203. The processor 1201 may include, for example, a general-purpose microprocessor, an instruction set processor and / or an associated chipset and / or a dedicated microprocessor. The processor 1201 may also include onboard memory for caching purposes. The processor 1201 may include a single processing unit or multiple processing units for executing different steps of the method flow according to an embodiment of this application.
[0197] Random access memory 1203 stores various programs and data required for the steps of electronic device 1200. Processor 1201, read-only memory 1202, and random access memory 1203 are interconnected via bus 1204. Processor 1201 executes various steps of the method flow according to embodiments of this application by executing programs in read-only memory 1202 and / or random access memory 1203. It should be noted that programs may also be stored in one or more memories other than read-only memory 1202 and random access memory 1203. Processor 1201 may also execute various steps of the method flow according to embodiments of this application by executing programs stored in one or more memories.
[0198] According to embodiments of this application, the electronic device 1200 may further include an input / output interface 1205, which is also connected to the bus 1204. The electronic device 1200 may also include one or more of the following components connected to the input / output interface 1205: an input section 1206 including a keyboard, mouse, etc.; an output section 1207 including a cathode ray tube, liquid crystal display, etc., and a speaker, etc.; a storage section 1208 including a hard disk, etc.; and a communication section 1209 including a network interface card, such as a local area network card, modem, etc. The communication section 1209 performs communication processing via a network such as the Internet. A drive 1210 is also connected to the input / output interface 1205 as needed. A removable medium 1211, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 1210 as needed so that computer programs read from it can be installed into the storage section 1208 as needed.
[0199] Embodiments of this application also provide a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the method according to the embodiments of this application.
[0200] According to embodiments of this application, the computer-readable storage medium can be a non-volatile computer-readable storage medium, such as including but not limited to: portable computer disks, hard disks, random access memory, read-only memory, erasable programmable read-only memory, portable compact disk read-only memory, optical storage devices, magnetic storage devices, or any suitable combination thereof. In embodiments of this application, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to embodiments of this application, the computer-readable storage medium may include the read-only memory 1202, and / or random access memory 1203, and / or one or more memories other than read-only memory 1202 and random access memory 1203 described above.
[0201] Embodiments of this application also include a computer program product comprising a computer program containing program code for performing the methods shown in the flowchart. When the computer program product is run on a computer system, the program code is used to cause the computer system to implement the methods provided in the embodiments of this application.
[0202] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and may be downloaded and installed via the communication section 1209, and / or installed from the removable medium 1211. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0203] In embodiments of this application, the computer program can be downloaded and installed from a network via communication section 1209, and / or installed from removable medium 1211. When the computer program is executed by processor 1201, it performs the functions defined in the system of this application embodiment. According to embodiments of this application, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0204] According to embodiments of this application, program code for executing the computer programs provided in the embodiments of this application can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0205] Those skilled in the art will understand that the features described in the various embodiments of this application can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in this application. In particular, the features described in the various embodiments of this application can be combined and / or combined in various ways without departing from the spirit and teachings of this application. All such combinations and / or combinations fall within the scope of this application.
Claims
1. A code generation method, comprising: Receive the original query statement input by the user; Based on the original query statement, a first code fragment is generated using a large model with a code knowledge base as the external knowledge source through retrieval enhancement. The first code segment is subjected to feature extraction using the same feature extraction pipeline as the code knowledge base construction phase to form a first multi-dimensional feature representation; wherein, the code knowledge base stores at least one second code segment and a second multi-dimensional feature representation formed by feature extraction of the second code segment using the feature extraction pipeline; The first multi-dimensional feature representation is used to perform N-way retrieval in the code knowledge base to obtain retrieval results, where N is an integer greater than or equal to 2; Based on the search results, assess the fit between the first code snippet and the code knowledge base; and When the degree of fit is greater than or equal to a preset degree of fit threshold, the first code fragment is output.
2. The method according to claim 1, wherein, The method further includes: When the fit is less than the fit threshold, the large model is used to correct the first code segment or output correction suggestions based on the search results.
3. The method according to claim 1, wherein, The step of extracting features from the first code segment using the same feature extraction pipeline as the code knowledge base construction phase to form a first multi-dimensional feature representation includes: Functional semantic features of the first code snippet are extracted from the text of the first code snippet; Structural semantic features of the first code segment are extracted based on the abstract syntax tree and control flow graph of the first code segment; Extracting contextual features of the first code segment based on its metadata; and Based on the functional semantic features, structural semantic features, and contextual features of the first code fragment, the first multi-dimensional feature representation is formed.
4. The method according to claim 3, wherein, The multi-dimensional feature representation includes vectors and structured identifiers. The N-way retrieval using the first multi-dimensional feature representation in the code knowledge base yields retrieval results including: During the retrieval process, based on the vector similarity between the first multi-dimensional feature representation and the second multi-dimensional feature representation, vectors that meet preset conditions are retrieved from the code knowledge base, and the similar code fragments corresponding to the retrieved vectors and their vector similarity values are output; and During the search, based on the comparison between the structured identifiers in the first multi-dimensional feature representation and the second multi-dimensional feature representation, the successfully matched identifier information is retrieved from the code knowledge base.
5. The method according to claim 4, wherein, The step of evaluating the fit between the first code fragment and the code knowledge base based on the search results includes: Based on the vector similarity values of the similar code fragments, a semantic similarity score is obtained; Based on the matching identification information and the preset scoring rules in the code development pattern specification corresponding to the code knowledge base, a pattern consistency score is obtained; A static compliance score is obtained based on the analysis of the first code snippet using static analysis tools; The fit is obtained by weighting the semantic similarity score, the pattern consistency score, and the static compliance score.
6. The method according to claim 3, wherein, The extraction of structural semantic features of the first code segment based on the abstract syntax tree and control flow graph includes: Generate an abstract syntax tree and control flow graph for the first code snippet; Extract the first structural feature from the abstract syntax tree of the first code snippet; Extract the second structural features from the control flow graph of the first code segment; The first structural feature and the second structural feature of the first code segment are uniformly encoded using a trained encoding model to obtain the structural semantic feature of the first code segment.
7. The method according to claim 6, wherein, The extraction of structural semantic features of the first code segment based on the abstract syntax tree and control flow graph of the first code segment also includes: The third structural features are extracted from the text of the first code snippet based on preset rules; Specifically, the first structural feature, the second structural feature, and the third structural feature of the first code segment are uniformly encoded through the encoding model to obtain the structural semantic features of the first code segment.
8. The method according to claim 1, wherein, The step of generating the first code fragment based on the original query statement, using a code knowledge base as an external knowledge source and a large model through retrieval enhancement, includes: The original query statement is expanded to obtain an enhanced query statement; Based on the enhanced query statement, the recall results are obtained from the code knowledge base through M-way parallel recall. The M-way parallel recall path is the same as or not exactly the same as the N-way retrieval path, where M is an integer greater than 2. The second code fragment in the recall results is sorted to form a sorting result; Based on the sorting results, multiple target code fragments that meet the preset preference criteria are selected; Based on the multiple target code fragments and the enhanced query statement, prompt words are generated; Input the prompt word into the large model and obtain the first code fragment output by the large model.
9. The method according to claim 8, wherein, The sorting of the second code fragments in the recall results to form a sorting result includes: Based on the enhanced query statement, the target application scenario is matched from multiple application scenarios; Obtain the target sorting strategy corresponding to the target application scenario, wherein different application scenarios have different preset sorting strategies; The second code fragment in the recall results is sorted based on the target sorting strategy.
10. The method according to claim 9, wherein, The target ranking strategy includes various ranking factors and their weight configuration information; the ranking of the second code fragment in the recall result based on the target ranking strategy includes: Extract the values of each second code fragment in the recall results, sorted by each sorting factor; The second code segment is sorted according to the weight configuration information, and the values of each sorting factor are weighted to obtain the sorting score of each second code segment in the recall result. The second code fragment in the recall results is sorted based on the ranking score.
11. The method according to claim 9, wherein, The target ranking strategy for obtaining the target application scenario also includes: When multiple target application scenarios are matched based on the enhanced query statement, the preset sorting strategies of the multiple target application scenarios are weighted and merged to obtain the target sorting strategy.
12. A code generation apparatus, comprising: The code generation module is used to receive the original query statement input by the user, and based on the original query statement, generate the first code fragment using a large model with the code knowledge base as the external knowledge source and through retrieval enhancement. The feature extraction module is used to extract features from the first code segment using the same feature extraction pipeline as the code knowledge base construction phase to form a first multi-dimensional feature representation; wherein, the code knowledge base stores at least one second code segment and a second multi-dimensional feature representation formed by extracting features from the second code segment using the feature extraction pipeline; The retrieval module is used to perform N-way retrieval in the code knowledge base using the first multi-dimensional feature representation to obtain retrieval results, where N is an integer greater than or equal to 2; An evaluation module is used to evaluate the fit between the first code fragment and the code knowledge base based on the search results, and to output the first code fragment when the fit is greater than or equal to a preset fit threshold.
13. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs. The one or more processors execute the one or more computer programs to implement the steps of the method according to any one of claims 1 to 11.
14. A computer-readable storage medium having a computer program or instructions stored thereon, wherein, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 11.
15. A computer program product comprising a computer program or instructions, wherein, When the computer program or instructions are executed by a processor, they implement the steps of the method according to any one of claims 1 to 11.