Code generation method and device, equipment, medium and program product
By performing multi-dimensional detection and correction on candidate code, combined with quality scoring, the problem of insufficient code generation accuracy in existing technologies has been solved, and a comprehensive improvement in code syntax, logic, and intent has been achieved.
Patent Information
- Application Number
- CN202511624697.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-07
- Publication Date
- 2026-02-10
AI Technical Summary
Existing code generation methods have bottlenecks in terms of syntax, semantics, and contextual logic, resulting in insufficient code generation accuracy and difficulty in meeting the needs of complex business scenarios.
By performing syntax checks, code logic checks, and code intent checks on the first candidate code, error types are identified and targeted corrections are made. Combined with code quality attribute scores, the final code is generated.
It improves the accuracy and reliability of code generation, ensuring that the code fully meets the standards in terms of form, logic, and intent, thereby enhancing the accuracy and adaptability of code generation.
Smart Images

Figure CN121501322A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, to the application of large models in fintech scenarios, and more specifically to a code generation method, apparatus, device, medium, and program product. Background Technology
[0002] With the rapid development of artificial intelligence technology, deep learning and AI technologies have improved the performance of automatic program generation. However, programming languages have extremely high requirements for syntactic correctness, semantic consistency, and the coherence of contextual logic. At the syntactic level, the syntactic rules of various languages must be strictly followed; any missing symbols or formatting errors will cause the code to fail. At the semantic level, it is necessary to ensure that the meaning expressed by the code is consistent with the developer's intention. Figure One To avoid issues such as variable confusion and function call errors, and at the contextual logic level, it is necessary to ensure logical coherence and mutual adaptation between different parts of the code to meet the logical association requirements in complex business scenarios. Existing code generation methods still face technical bottlenecks when dealing with these high requirements. They still have problems such as syntax omissions, difficulty in direct execution, semantic comprehension deviations, inability to accurately implement functional requirements, and gaps in contextual logic connection, making it difficult to integrate into the overall project framework. These problems directly lead to insufficient code generation accuracy and restrict the application of code generation technology. Therefore, how to improve code generation accuracy is a key technical problem that urgently needs to be solved. Summary of the Invention
[0003] In view of the above problems, this application provides a code generation method, apparatus, device, medium and program product to improve code generation accuracy.
[0004] According to a first aspect of this application, a code generation method is provided, comprising: inputting pseudocode into a code generation model to generate first candidate code; determining the error types of the first candidate code, wherein the error types include syntax errors, code logic errors, and code intent errors; correcting the first candidate code according to the error types by adopting corresponding correction strategies to obtain second candidate code; scoring the second candidate code based on code quality attributes, wherein the code quality attributes include the conciseness of the second candidate code and the adaptability of the second candidate code to the business scenario corresponding to the pseudocode; and determining the finally generated code based on the scoring result of the second candidate code.
[0005] According to an embodiment of this application, a first candidate code is corrected according to the error type to obtain a second candidate code by adopting a corresponding correction strategy, including: performing quality detection on the corrected first candidate code and re-acquiring the error type, wherein the quality detection includes syntax detection, code logic detection and code intent detection; iterating the first candidate code correction operation and the error type re-acquisition operation until a preset condition is met; and using the first candidate code that meets the preset condition as the second candidate code.
[0006] According to an embodiment of the present application, the syntax detection comprises: performing a segment-by-segment scan on the code character stream of the first candidate code; matching each character in the code character stream with a preset syntax rule library; and determining a syntax error code segment according to a matching result.
[0007] According to an embodiment of the present application, the code logic detection comprises: comparing a first node in a first logic graph with a second node in a second logic graph and comparing a first edge in the first logic graph with a second edge in the second logic graph, wherein the first logic graph is determined according to the first candidate code, the second logic graph is determined according to the pseudo code, the first node and the first edge in the first logic graph respectively represent data features in the first candidate code and a logical relationship between the data features, and the second node and the second edge in the second logic graph respectively represent key elements in the pseudo code and a logical relationship between the key elements; and taking a code segment corresponding to a redundant or missing first node as a code logic error code segment and / or taking a code segment corresponding to a first edge inconsistent with the second edge as a code logic error code segment.
[0008] According to an embodiment of the present application, the code intent detection comprises: respectively extracting semantic information of the pseudo code and abstract syntax tree features of the first candidate code; calculating a matching degree of the semantic information and the abstract syntax tree features; and taking a code segment corresponding to a matching degree less than a preset threshold as a code intent error code segment.
[0009] According to an embodiment of the present application, according to the error type, a corresponding correction strategy is adopted to correct the first candidate code, comprising at least one of the following: for a syntax error, selecting a matching character matched with the syntax error code segment from the preset syntax rule library to replace a corresponding character in the syntax error code segment; for a code logic error, taking the second logic graph as a reference to perform at least one of the following: deleting a redundant code logic error code segment, adding a missing code logic error code segment, and correcting a logical relationship between data features in the code logic error code segment.
[0010] According to an embodiment of the present application, according to the error type, a corresponding correction strategy is adopted to correct the first candidate code, further comprising: for a code intent error, performing semantic reasoning on semantic information corresponding to a matching degree less than a preset threshold from multiple dimensions to obtain multiple potential intents; inputting the multiple potential intents into a cross-modal knowledge transfer model to generate multiple new code segments; and merging the new code segments into the first candidate code.
[0011] According to an embodiment of the present application, the code generation model is trained in the following manner: inputting a current character of existing target code into a first long short-term memory network to obtain state features, wherein the state features represent time sequence features of the current character sequence of the existing target code; fusing local features and the state features through a local attention mechanism to obtain first fused features, wherein the local features are obtained by inputting vector features into a feature pyramid network, and the local features represent local information possessed by the existing pseudo code, and the vector features are obtained by performing word vector encoding and position encoding on the existing pseudo code; fusing the first fused features and global features through a global attention mechanism to obtain second fused features; wherein the global features are obtained by inputting the vector features into a second long short-term memory network, and the global features represent overall semantic information possessed by the existing pseudo code; predicting a next character in the output code by using a normalized exponential function according to the second fused features to obtain a predicted character; inputting the next character of the existing target code into the first long short-term memory network to update the state features; repeating the operations of obtaining the first fused features, obtaining the second fused features, and obtaining the predicted character until a sentence of the output code is complete.
[0012] According to an embodiment of the present application, predicting a next character in the output code by using a normalized exponential function according to the second fused features to obtain a predicted character comprises: predicting a next character in the output code by using a normalized exponential function according to the second fused features to obtain an initial predicted character; calculating a difference value between the initial predicted character and the next character in the existing target code; calculating a current loss function according to the difference value and adjusting model parameters; re-obtaining the initial predicted character based on the adjusted model parameters; repeating the operations of calculating the difference value, adjusting the model parameters, and re-obtaining the initial predicted character until the current loss function converges; and taking the initial predicted character after the current loss function converges as a final output predicted character.
[0013] The second aspect of the present application provides a code generation device, comprising: a candidate code generation module configured to input a pseudo code into a code generation model to generate a first candidate code; a code detection module configured to determine an error type of the first candidate code, wherein the error type includes a syntax error, a code logic error, and a code intent error; a code correction module configured to correct the first candidate code according to the error type by using a corresponding correction strategy to obtain a second candidate code; a code quality scoring module configured to score the second candidate code based on code quality attributes, wherein the code quality attributes include a degree of conciseness of the second candidate code and an adaptation degree of a business scenario corresponding to the second candidate code and the pseudo code; and a code generation module configured to determine a finally generated code according to a scoring result of the second candidate code.
[0014] The third aspect of the present application provides an electronic device, comprising: one or more processors; 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.
[0015] The fourth aspect of the present application further provides a computer-readable storage medium having stored thereon a computer program or instructions, which, when executed by a processor, implement the steps of the method.
[0016] The fifth aspect of the present application further provides a computer program product comprising a computer program or instructions, which, when executed by a processor, implement the steps of the method. BRIEF DESCRIPTION OF DRAWINGS
[0017] The above content of the present application and other purposes, features and advantages will be more apparent through the following description of the embodiments of the present application with reference to the accompanying drawings, in which:
[0018] Figure 1 An application scenario diagram of the code generation method, apparatus, device, medium and program product according to the embodiments of the present application is schematically shown;
[0019] Figure 2 A flowchart of the code generation method according to the embodiments of the present application is schematically shown;
[0020] Figure 3 A structural schematic diagram of the code generation method according to the embodiments of the present application is schematically shown;
[0021] Figure 4 A flowchart of the method for obtaining the second candidate code according to the embodiments of the present application is schematically shown;
[0022] Figure 5 A flowchart of the method for modifying the first candidate code according to the embodiments of the present application is schematically shown;
[0023] Figure 6 A flowchart of the training method of the code generation model according to the embodiments of the present application is schematically shown;
[0024] Figure 7 A structural block diagram of the code generation apparatus according to the embodiments of the present application is schematically shown;
[0025] Figure 8 A block diagram of an electronic device suitable for implementing the code generation method according to the embodiments of the present application is schematically shown. DETAILED DESCRIPTION
[0026] Hereinafter, embodiments of the present application will be described with reference to the accompanying drawings. It should be understood, however, that the description is merely exemplary of the present application, and is not intended to limit the scope of the present application. In the following detailed description of the embodiments of the present application, numerous specific details are set forth in order to provide a thorough understanding of the present application. However, it will be apparent to one skilled in the art that the present application can be practiced without these specific details. In other instances, well-known structures and functions have not been described in detail in order to avoid obscuring aspects of the present application.
[0027] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to be limiting of the present application. As used herein, the term "includes" and tautological expressions thereof, such as "including," "includes," "include," "contains," "containing," and so forth, shall be read expansively and without limitation. The terms "comprising," "comprise" and / or "comprised of," and tautological expressions thereof (e.g., "comprising of") will be understood to enable recitations that they do not exclude additional matter.
[0028] All terms used herein (including technical and scientific terms) have the meanings commonly understood by one of ordinary skill in the art unless otherwise defined. It should be noted that the terms used herein are defined as having a meaning that is consistent with the context of the specification in which the terms are utilized, and the terms should not be interpreted in an idealized or overly formal sense.
[0029] In instances where a term similar to "at least one of A, B, and C, etc." is used, in general, it should be understood that the meaning is that of "one or more of A, B, and C, etc." (e.g., "a system having at least one of A, B, and C" should include, but not be limited to, a system having A alone, a system having B alone, a system having C alone, a system having both A and B, a system having both A and C, a system having both B and C, and / or a system having A, B, and C, etc.).
[0030] Embodiments of the present application provide a code generation method, which identifies the type of error code and makes targeted corrections by performing multi-dimensional detection such as syntax detection, code logic detection, and code intent detection on the first candidate code, can improve correction accuracy, and further selects a more concise code that is more suitable for a business scenario by scoring the corrected code, improves code generation precision and reliability, and eliminates the reasons for possible code errors through the code correction-code scoring-final code determination method, thereby greatly improving the code generation precision.
[0031] Figure 1 An application scenario diagram of a code generation method according to an embodiment of the present application is schematically shown.
[0032] As Figure 1As shown, application scenario 100 according to this embodiment may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 serves as a medium for providing a communication link between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links, or fiber optic cables, etc.
[0033] Users can use the first terminal device 101, the second terminal device 102, and the third terminal device 103 to interact with the server 105 via the network 104 to receive or send messages, etc. Various communication client applications can be installed on the first terminal device 101, the second terminal device 102, and the third terminal device 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).
[0034] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be various electronic devices with displays and support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0035] Server 105 can be a server that provides various services, such as a backend management server that supports websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (this is just an example). The backend management server can analyze and process data such as received user requests, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal devices.
[0036] 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. The code generation method provided in this application embodiment can also be executed by a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105. Correspondingly, the code generation device provided in this application embodiment can also be located in a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105.
[0037] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0038] The following will be based on Figure 1 The described scene, through Figures 2-6 The code generation method according to the embodiments of this application will be described in detail.
[0039] Figure 2 A flowchart illustrating a code generation method according to an embodiment of this application is shown schematically.
[0040] like Figure 2 As shown, the code generation method 200 of this embodiment includes operations S210 to S250.
[0041] In operation S210, pseudocode is input into the code generation model to generate the first candidate code.
[0042] In operation S220, the error type of the first candidate code is determined, where the error type includes syntax error, code logic error and code intent error.
[0043] In operation S230, based on the error type, the corresponding correction strategy is adopted to correct the first candidate code and obtain the second candidate code.
[0044] In operation S240, the second candidate code is scored based on code quality attributes, including the conciseness of the second candidate code and the degree to which the second candidate code adapts to the business scenario corresponding to the pseudocode.
[0045] In operation S250, the final generated code is determined based on the scoring result of the second candidate code.
[0046] In some embodiments, during operation S220, syntax errors include syntax format errors, such as missing semicolons, misspelled keywords, and undefined variables. Code logic errors include: the first candidate code lacking code segments corresponding to certain nodes in the pseudocode (e.g., the pseudocode contains a node for "filtering data," but the first candidate code lacks a code segment representing this node); the first candidate code containing redundant code segments (e.g., the pseudocode does not contain "calculate the mean," but the first candidate code contains a code segment representing "calculate the mean"); and the logical relationship of a certain code segment in the first candidate code being inconsistent with the logical relationship of the corresponding part of the pseudocode (e.g., the logical relationship of the pseudocode is...). If the logic of a code segment in the first candidate code is to perform calculations first and then filter the data, then that code segment contains a code logic error. A code intent error occurs when the functionality of the first candidate code is inconsistent with the functionality intended by the pseudocode. For example, if the pseudocode's intent is to "calculate the average of two numbers," but the first candidate code ultimately "calculates the sum of two numbers," then the first candidate code contains a code intent error. Similarly, if the pseudocode aims to "calculate the average of three positive numbers," but the first candidate code ultimately "calculates the average of three negative numbers," then the first candidate code contains a code intent error.
[0047] In some embodiments, during operation S240, the simplicity of the second candidate code can refer to the number of lines of the second candidate code, code redundancy, etc. Code redundancy includes the number of redundant functions, the number of useless variables, and the degree of repetition in code logic. The adaptability of the second candidate code to the business scenario corresponding to the pseudocode can be that in some financial transaction scenarios, the interface is required to be compatible. In order to meet the requirement of interface compatibility, it is necessary to generate versionable and compatible code. Alternatively, for certain specific scenarios, such as compliance detection scenarios, the generated code needs to meet the compliance requirements.
[0048] In some embodiments, method 200 further includes: performing quality detection on the first candidate code, obtaining correct code and error code respectively, determining the type of error code, adopting a corresponding correction strategy to correct the error code according to the error type, scoring and sorting the correct code and the corrected error code based on code quality attributes, and using the code ranked first as the final generated code or the top N codes as the final generated code, where N is an integer greater than or equal to 1.
[0049] According to the embodiments of this application, by performing multi-dimensional detection on the first candidate code, the type of erroneous code can be identified and targeted corrections can be made, thereby improving the accuracy of corrections. By scoring the corrected first candidate code, more concise code that is more suitable for the business scenario can be selected, thereby improving the accuracy and reliability of code generation. Through the method of code correction-code scoring-final code determination, the possible causes of code errors are eliminated in all aspects, thereby greatly improving the accuracy of code generation.
[0050] Figure 3 The schematic diagram illustrates the structure of a code generation method according to an embodiment of this application.
[0051] like Figure 3 As shown, pseudocode is first input into the code generation model to generate first candidate code. Then, the first candidate code undergoes quality testing to obtain correct and incorrect code. Based on the quality testing results, the type of incorrect code is determined. According to the type of incorrect code, a large model is used to perform targeted code repair. The repaired code undergoes quality testing again, and the code repair process is repeated for any detected incorrect code until a preset condition is met. The repaired incorrect code that meets the preset condition is then used as the second candidate code. The correct code and the second candidate code are scored and ranked based on code quality attributes. The code with the highest score or the top N codes can be used as the final code. Furthermore, the preset condition could be that all incorrect codes pass the correctness test or that the number of iterations reaches a preset number.
[0052] According to embodiments of this application, the translation code model is a lightweight, low-resource-consumption model. Candidate code is first generated using this model, which can accurately generate code corresponding to simple pseudo-code with clear intent. Only when the code output by the code generation model is detected to have problems is the large model used to repair the erroneous code output by the code generation model. This "on-demand calling" strategy of the large model significantly optimizes the overall resource utilization, enabling high-performance code generation even with limited computing power. Furthermore, the code generation model generates code in a relatively short time. For pseudo-code with simple intent and sensitive to response time, using the code generation model not only reduces time consumption but also ensures the accuracy of code generation. Combining the code generation model with the large model allows for accurate code generation even if the semantic information of the pseudo-code is incomplete or the logical relationships are unclear, thereby improving the robustness of code generation and reducing the requirements for the precise expression of the pseudo-code.
[0053] Figure 4 A flowchart illustrating a method for obtaining a second candidate code according to an embodiment of this application is shown.
[0054] like Figure 4 As shown, the method for obtaining the second candidate code includes operations S410 to S430.
[0055] In operation S410, quality checks are performed on the corrected first candidate code to re-acquire error types. The quality checks include syntax checks, code logic checks, and code intent checks.
[0056] In operation S420, the first candidate code correction operation and the error type re-acquisition operation are iterated until the preset conditions are met.
[0057] When operating S430, the first candidate code that meets the preset conditions is used as the second candidate code.
[0058] In some embodiments, during operation S430, the preset condition may be that all error codes pass the correctness check or the number of iterations reaches a preset number.
[0059] According to embodiments of this application, an iterative correction mechanism is employed to correct the first candidate code, gradually eliminating various erroneous codes until the generated code meets the standards. This avoids the incompleteness of a single correction and dynamically addresses new problems arising during the correction process, significantly improving code correction accuracy. The correctness of the code is detected from the perspectives of code form, code logic, and code intent. This multi-dimensional detection strategy, working together, performs quality checks on the first candidate code, improving detection accuracy. This facilitates targeted correction of detected problems, thereby enhancing code generation accuracy.
[0060] In some embodiments, syntax detection includes: scanning the code character stream of a first candidate code segment by segment; matching each character in the code character stream with a preset syntax rule base; and determining the syntax error code segment based on the matching result.
[0061] According to the embodiments of this application, by scanning the code character stream of the first candidate code segment by segment and matching each scanned character with a preset syntax rule library, it is possible to accurately detect whether the syntax of each character in the code is correct, thereby quickly locating syntax problems, reducing the time developers spend troubleshooting syntax problems, and thus improving code development efficiency; by detecting syntax problems, code segments with syntax problems can be found, which is beneficial for subsequent syntax modification to avoid generating invalid code that cannot be compiled or executed.
[0062] In some embodiments, code logic detection includes: comparing a first node in a first logic graph with a second node in a second logic graph, and comparing a first edge in the first logic graph with a second edge in the second logic graph, wherein the first logic graph is determined based on a first candidate code, the second logic graph is determined based on pseudocode, the first node and the first edge in the first logic graph represent the logical relationship between data features of the first candidate code, and the second node and the second edge in the second logic graph represent the logical relationship between key elements in the pseudocode; and designating the code segment corresponding to a redundant or missing first node as a code logic error code segment, and / or designating the code segment corresponding to a first edge that is inconsistent with the second edge as a code logic error code segment.
[0063] In some embodiments, the data features of the first candidate code correspond one-to-one with the key elements in the pseudocode. The key elements may be the input information, output information, and specific operations to be performed by the code. The logical relationships between the data features correspond one-to-one with the logical relationships between the key elements. The logical relationships between the elements may be to calculate the mean first and then calculate the mean, or to filter the data first and then calculate the filtered data.
[0064] According to the embodiments of this application, by comparing the nodes and edges of the logic graph, redundant, missing, or logically inconsistent code segments can be effectively identified, ensuring that the generated code and pseudocode have consistent logic. Starting from the key elements of the pseudocode and the logical relationships between the key elements, the code logic of the first candidate code is deeply verified to see if it is consistent with the code logic of the pseudocode, accurately locating the code logic deviations in the first candidate code, which is beneficial for subsequent code logic modification of the candidate code, thereby improving the accuracy of code generation.
[0065] In some embodiments, code intent detection includes: extracting semantic information of pseudocode and abstract syntax tree features of a first candidate code, respectively; calculating the matching degree between the semantic information and the abstract syntax tree features; and identifying code segments with matching degrees less than a preset threshold as code intent error segments.
[0066] According to embodiments of this application, by extracting the semantic information of pseudocode, the true intent of pseudocode can be captured more accurately. By extracting the abstract syntax tree features of the first candidate code, the intent of the first candidate code can be accurately obtained. By calculating the matching degree between the semantic information and the abstract syntax tree features, the degree of intent deviation of the first candidate code can be determined. Based on the degree of intent deviation, the code segment with erroneous intent can be identified. This code intent detection method can accurately obtain the code segment with erroneous intent, which is beneficial for subsequent accurate modification of code segments with erroneous intent, thereby improving the quality of code generation.
[0067] Figure 5A flowchart illustrating a method for modifying a first candidate code according to an embodiment of this application is shown.
[0068] like Figure 5 As shown, the correction method for the first candidate code includes operations S510 to S520.
[0069] When operating S510, for syntax errors, a matching character that matches the syntax error code segment is selected from the preset syntax rule library, and this character replaces the corresponding character in the syntax error code segment.
[0070] When operating S520, for code logic errors, based on the second logic graph, perform at least one of the following: delete redundant code logic error segments, add missing code logic error segments, and correct the logical relationships between data features in the code logic error segments.
[0071] In some embodiments, a large model can be used to apply a correction strategy corresponding to the error type to correct the first candidate code.
[0072] In some embodiments, during operation S510, if a line in a code segment is missing a colon or a key character is misspelled, the misspelled key character in the code segment can be modified using a large model based on the correct spelling of the key character in the preset syntax rule base, and the missing colon can be added to the corresponding position in the code segment.
[0073] According to the embodiments of this application, different types of code problems are specifically fixed, avoiding the omission of key errors by a single fixing method, reducing code defects, and adopting an appropriate fixing strategy for different problems, so that the code can fully meet the standards in terms of code form, code logic and code intent, thereby directly improving the accuracy of code generation.
[0074] In some embodiments, the first candidate code is corrected by adopting a corresponding correction strategy according to the error type, and further includes: for code intent errors, performing semantic reasoning on semantic information corresponding to matching degrees less than a preset threshold from multiple dimensions to obtain multiple potential intents; inputting the multiple potential intents into a cross-modal knowledge transfer model to generate multiple new code segments; and merging the new code segments into the first candidate code.
[0075] In some embodiments, based on the semantic information of the pseudocode, a large model can be used for context association to obtain context association information. From the dimensions of context association information, knowledge graph of the business scenario corresponding to the pseudocode, and semantic extension, the large model is used to perform semantic reasoning on the semantic information corresponding to the matching degree below a preset threshold to obtain multiple potential intents. Each potential intent is translated into multiple different programming languages, and then each programming language is converted into a target programming language. In this way, each potential intent corresponds to multiple target programming languages, that is, each potential intent corresponds to multiple new code segments. There are subtle differences between the multiple new code segments corresponding to the same potential intent, which may be differences in computational precision, library functions, or memory management, etc. By obtaining multiple new code segments through this intent divergence method, the intent of the pseudocode can be expanded. By merging all new code segments into the first candidate code, multiple first candidate codes can be obtained. Among these first candidate codes, there may be some that are related to the true intent of the pseudocode. Figure One This improves the accuracy of code generation. Furthermore, by leveraging the powerful semantic understanding and semantic reasoning capabilities of the large model, multiple potential intentions of the pseudocode are obtained, effectively solving the problem of unclear pseudocode semantics.
[0076] In some embodiments, assuming the pseudocode is "c equals the maximum of the absolute values of a and b", the first candidate code output by the code generation model is "c=max(abs(a),b)". The first candidate code has an incorrect code intent. Therefore, a large model is used to perform semantic reasoning based on the semantic information of the pseudocode, generating multiple latent semantic information to understand the intent of the pseudocode from multiple perspectives. Based on the multiple latent semantic information, a new code is generated and merged into the first candidate code, finally generating the correct code "c=max(abs(a),abs(b))".
[0077] For example, suppose the pseudocode is "Input a string", where the string is represented by s. The first candidate code output by the code generation model is "gets(s)". "gets(s)" means reading the entire line until a newline character is encountered without checking the length, and the newline character is not stored. However, the true intention of this pseudocode is to read the entire line (including spaces) until a newline character is encountered or the specified length is reached. Therefore, the first candidate code has an incorrect code intent. Thus, a large model is used to perform semantic reasoning on the pseudocode, generating multiple latent semantic information to understand the intent of the pseudocode from multiple perspectives. Based on the multiple latent semantic information, a new code is generated and merged into the first candidate code, finally generating the correct code "cin >> (s)".
[0078] According to embodiments of this application, for code segments with incorrect code intent, multiple potential intents of the pseudocode are obtained through semantic reasoning, and new code is generated based on these potential intents. The new code contains elements that have the same intent as the true intent of the pseudocode.Figure One This method improves the accuracy of code repair because it avoids errors in the code.
[0079] In some embodiments, targeted fixing of different types of code problems can avoid missing critical errors with a single fixing method, reduce code defects, and adopt adaptive fixing strategies for different problems. This ensures that the code fully meets the standards in terms of code form, code logic, and code intent, thereby directly improving the accuracy of code generation. For code segments with incorrect code intent, multiple potential intents of the pseudocode are obtained through semantic reasoning, and new code is generated based on these potential intents. The new code contains elements that correspond to the true intent of the pseudocode. Figure One This method improves the accuracy of code repair because it avoids errors in the code.
[0080] Figure 6 A flowchart illustrating a training method for a code generation model according to an embodiment of this application is shown.
[0081] like Figure 6 As shown, the training method for the code generation model includes operations S610 to S660.
[0082] In operation S610, the current character of the existing target code is input into the first long short-term memory network to obtain state features, wherein the state features represent the temporal features of the current character sequence of the existing target code.
[0083] When operating the S620, local features and state features are fused through a local attention mechanism to obtain the first fused feature. The local features are obtained by inputting vector features into the feature pyramid network. The local features represent the local information of the existing pseudocode, and the vector features are obtained by performing word vector encoding and position encoding on the existing pseudocode.
[0084] When operating S630, the first fusion feature and the global feature are fused through the global attention mechanism to obtain the second fusion feature; the global feature is obtained by inputting the vector feature into the second long short-term memory network, and the global feature represents the overall semantic information of the existing pseudocode.
[0085] When operating S640, based on the second fusion feature, the normalized exponential function is used to predict the next character in the output code, and the predicted character is obtained.
[0086] When operating the S650, the next character of the existing target code is input into the first long short-term memory network to update the state features.
[0087] While operating S660, repeat the first fusion feature acquisition, the second fusion feature acquisition, and the predicted character acquisition operations until the output code statement is complete.
[0088] In some embodiments, during operation S620, local features are obtained by inputting vector features into a feature pyramid network, including: representing the vector features with the letter E; passing the vector features through a linear layer to obtain a comprehensive feature L; passing the comprehensive feature through pyramid networks with convolution kernels of 3×3, 5×5, and 7×7 respectively to obtain three feature vectors of different scales, represented as a first feature vector F1, a second feature vector F2, and a third feature vector F3 respectively; cross-fusing the second and third feature vectors to obtain a fourth feature vector; cross-fusing the second and fourth feature vectors to obtain a fifth feature vector; cross-fusing the first and fifth feature vectors to obtain a sixth feature vector; and concatenating the fourth, fifth, and sixth feature vectors and inputting them into another linear layer to obtain local features.
[0089] According to embodiments of this application, this training method enables the code generation model to acquire features at different scales of pseudocode, including fine-grained semantic information and global code logic information. By fusing features at different scales, the comprehensiveness of the information contained in each feature is improved. Based on the fused features, the final generated code is obtained, thereby improving the comprehensiveness and accuracy of the code.
[0090] In some embodiments, based on the second fusion feature, the next character in the output code is predicted using a normalized exponential function to obtain the predicted character, including: based on the second fusion feature, the next character in the output code is predicted using a normalized exponential function to obtain an initial predicted character; the difference between the initial predicted character and the next character in the existing target code is calculated; based on the difference, the current loss function is calculated and the model parameters are adjusted; based on the adjusted model parameters, the initial predicted character is re-obtained; the difference calculation, model parameter adjustment, and re-obtaining of the initial predicted character are repeated until the current loss function converges; the initial predicted character after the current loss function converges is used as the final output predicted character.
[0091] According to the embodiments of this application, by continuously calculating the difference between the predicted character and the actual character to optimize the model parameters of the code generation model, the output of the code generation model can continuously approach the correct output, which can ensure the accuracy of the predicted character and thus improve the accuracy of code generation; when the loss function converges, the final predicted character can be obtained, which can improve the generalization ability of the code generation model.
[0092] Based on the above code generation method, this application also provides a code generation apparatus. The following will combine... Figure 7 The device is described in detail.
[0093] Figure 7 A schematic block diagram of a code generation apparatus according to an embodiment of this application is shown.
[0094] like Figure 7 As shown, the code generation apparatus 700 of this embodiment includes a candidate code generation module 710, a code detection module 720, a code correction module 730, a code quality scoring module 740, and a code generation module 750.
[0095] The candidate code generation module 710 is used to input pseudocode into the code generation model and generate first candidate code. In one embodiment, the candidate code generation module 710 can be used to perform the operation S210 described above, which will not be repeated here.
[0096] The code detection module 720 is used to determine the error type of the first candidate code, wherein the error type includes syntax errors, code logic errors, and code intent errors. In one embodiment, the code detection module 720 can be used to perform the operation S220 described above, which will not be repeated here.
[0097] The code correction module 730 is used to correct the first candidate code according to the error type and adopt a corresponding correction strategy to obtain the second candidate code. In one embodiment, the code correction module 730 can be used to perform the operation S230 described above, which will not be repeated here.
[0098] The code quality scoring module 740 is used to score the second candidate code based on code quality attributes, including the conciseness of the second candidate code and the degree to which the second candidate code adapts to the business scenario corresponding to the pseudocode. In one embodiment, the code quality scoring module 740 can be used to perform the operation S240 described above, which will not be repeated here.
[0099] The code generation module 750 is used to determine the final generated code based on the scoring result of the second candidate code. In one embodiment, the code generation module 750 can be used to perform the operation S250 described above, which will not be repeated here.
[0100] According to the embodiments of this application, the code generation device 700 can perform multi-dimensional detection on the first candidate code, such as syntax detection, code logic detection, and code intent detection, to identify the type of erroneous code and make targeted corrections, thereby improving the accuracy of corrections. The corrected code is then scored, and more concise code that is more suitable for the business scenario is selected, thereby improving the accuracy and reliability of code generation, eliminating all possible causes of errors in the code, and thus greatly improving the accuracy of code generation.
[0101] In some embodiments, the candidate code generation module 710 specifically includes a code generation model training module. This module is used to input the current character of the existing target code into a first long short-term memory network to obtain state features, where the state features represent the temporal features of the current character sequence of the existing target code; fuse the local features and state features through a local attention mechanism to obtain a first fused feature, where the local features are obtained by inputting vector features into a feature pyramid network, representing the local information of the existing pseudocode, and the vector features are obtained by performing word vector encoding and position encoding on the existing pseudocode; fuse the first fused feature and global features through a global attention mechanism to obtain a second fused feature, where the global feature is obtained by inputting vector features into a second long short-term memory network, representing the overall semantic information of the existing pseudocode; based on the second fused feature, use a normalized exponential function to predict the next character in the output code, obtaining the predicted character; input the next character of the existing target code into the first long short-term memory network to update the state features; repeat the operations of obtaining the first fused feature, the second fused feature, and the predicted character until the output code is complete. The code generation model training module includes a character prediction module.
[0102] In some embodiments, the character prediction module in the candidate code generation module 710 is specifically used to: predict the next character in the output code based on the second fusion feature using a normalized exponential function to obtain an initial predicted character; calculate the difference between the initial predicted character and the next character in the existing target code; calculate the current loss function and adjust the model parameters based on the difference; re-acquire the initial predicted character based on the adjusted model parameters; repeat the difference calculation, model parameter adjustment, and re-acquisition of the initial predicted character until the current loss function converges; and use the initial predicted character after the current loss function converges as the final output predicted character.
[0103] In some embodiments, the code detection module 720 specifically includes a syntax detection module, a code logic detection module, and a code intent detection module. The syntax detection module is used to scan the code character stream of the first candidate code segment by segment, match each character in the code character stream with a preset syntax rule library, and determine the code segment with syntax errors based on the matching results. The code logic detection module is used to compare the first node in the first logic graph with the second node in the second logic graph, and the first edge in the first logic graph with the second edge in the second logic graph. The first logic graph is determined based on the first candidate code, and the second logic graph is determined based on pseudocode. The first node in the first logic graph is compared with the second edge in the second logic graph. The first side represents the logical relationship between the data features of the first candidate code and the data features of the second side. The second node and the second side in the second logic graph represent the key elements and the logical relationship between the key elements in the pseudocode and the pseudocode, respectively. The code segment corresponding to the redundant or missing first node is regarded as the code logic error code segment, and / or, the code segment corresponding to the first side that is inconsistent with the second side is regarded as the code logic error code segment. The code intent detection module is used to extract the semantic information of the pseudocode and the abstract syntax tree features of the first candidate code, respectively, calculate the matching degree between the semantic information and the abstract syntax tree features, and regard the code segment corresponding to the matching degree less than the preset threshold as the code intent error code segment.
[0104] In some embodiments, the code correction module 730 is specifically used to: for syntax errors, select matching characters from a preset syntax rule library that match the syntax error code segment, and replace the corresponding characters in the syntax error code segment; for code logic errors, based on the second logic graph, perform at least one of the following: delete redundant code logic error code segments, add missing code logic error code segments, and correct the logical relationships between data features in the code logic error code segments.
[0105] In some embodiments, the code correction module 730 is further configured to: for code intent errors, perform semantic reasoning on semantic information corresponding to matching degrees less than a preset threshold from multiple dimensions to obtain multiple potential intents; input the multiple potential intents into a cross-modal knowledge transfer model to generate multiple new code segments; and merge the new code segments into the first candidate code.
[0106] In some embodiments, the code correction module 730 is further configured to: detect the corrected first candidate code and reacquire the error type; iterate the first candidate code correction operation and the error type reacquisition operation until a preset condition is met; and use the first candidate code that meets the preset condition as the second candidate code.
[0107] According to embodiments of this application, any plurality of modules among the candidate code generation module 710, code detection module 720, code correction module 730, code quality scoring module 740, and code generation module 750 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 candidate code generation module 710, code detection module 720, code correction module 730, code quality scoring module 740, and code generation module 750 can be at least partially implemented as hardware circuitry, such as a field-programmable gate array (FPGA), a programmable logic array (PLA), a system-on-a-chip, a system-on-a-substrate, a system-on-package, an application-specific integrated circuit (ASIC), or implemented in hardware or firmware by any other reasonable means of integrating or packaging the circuitry, or implemented in software, hardware, or firmware, or in any suitable combination of any of these three implementation methods. Alternatively, at least one of the candidate code generation module 710, code detection module 720, code correction module 730, code quality scoring module 740, and code generation module 750 may be implemented at least partially as a computer program module, which can perform corresponding functions when the computer program module is run.
[0108] Figure 8 A block diagram schematically illustrates an electronic device suitable for implementing a code generation method according to an embodiment of this application.
[0109] like Figure 8 As shown, an electronic device 800 according to an embodiment of this application includes a processor 801, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 802 or a program loaded from a storage portion 808 into a random access memory (RAM) 803. The processor 801 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or an associated chipset and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 801 may also include onboard memory for caching purposes. The processor 801 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of this application.
[0110] RAM 803 stores various programs and data required for the operation of electronic device 800. Processor 801, ROM 802, and RAM 803 are interconnected via bus 804. Processor 801 executes various operations of the method flow according to embodiments of this application by executing programs in ROM 802 and / or RAM 803. It should be noted that the programs may also be stored in one or more memories other than ROM 802 and RAM 803. Processor 801 may also execute various operations of the method flow according to embodiments of this application by executing programs stored in said one or more memories.
[0111] According to embodiments of this application, the electronic device 800 may further include an input / output (I / O) interface 805, which is also connected to a bus 804. The electronic device 800 may also include one or more of the following components connected to the input / output (I / O) interface 805: an input section 806 including a keyboard, mouse, etc.; an output section 807 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 808 including a hard disk, etc.; and a communication section 809 including a network interface card such as a LAN card, modem, etc. The communication section 809 performs communication processing via a network such as the Internet. A drive 810 is also connected to the input / output (I / O) interface 805 as needed. A removable medium 811, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 810 as needed so that computer programs read from it can be installed into the storage section 808 as needed.
[0112] This application also provides 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.
[0113] 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 (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In 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 ROM 802 and / or RAM 803 and / or one or more memories other than ROM 802 and RAM 803 described above.
[0114] 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 enable the computer system to implement the code generation method provided in the embodiments of this application.
[0115] When the computer program is executed by the processor 801, it performs the functions defined in the system / apparatus of this application embodiment. According to the embodiments of this application, the systems, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0116] 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 809, and / or installed from a removable medium 811. 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.
[0117] In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 809, and / or installed from the removable medium 811. When the computer program is executed by the processor 801, it performs the functions defined in the system of this application embodiment. According to the embodiments of this application, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0118] 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. Programming languages include, but are not limited to, languages such as Java, C++, Python, "C", or similar programming 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).
[0119] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this application. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0120] 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, characterized in that, The method includes: Input the pseudocode into the code generation model to generate the first candidate code; Determine the error type of the first candidate code, wherein the error type includes syntax error, code logic error, and code intent error; Based on the error type, a corresponding correction strategy is adopted to correct the first candidate code, thereby obtaining the second candidate code; The second candidate code is scored based on code quality attributes, including the conciseness of the second candidate code and the degree of adaptability of the second candidate code to the business scenario corresponding to the pseudocode. The final generated code is determined based on the scoring result of the second candidate code.
2. The method according to claim 1, characterized in that, The step of correcting the first candidate code according to the error type and adopting a corresponding correction strategy to obtain the second candidate code includes: The corrected first candidate code is subjected to quality inspection to re-obtain the error type, wherein the quality inspection includes syntax inspection, code logic inspection and code intent inspection; Iterate through the first candidate code correction operation and the error type re-acquisition operation until the preset condition is met; The first candidate code that meets the preset conditions will be used as the second candidate code.
3. The method according to claim 2, characterized in that, The syntax detection includes: The code character stream of the first candidate code is scanned segment by segment; Match each character in the code character stream with a preset syntax rule library; Based on the matching results, identify the code segment with syntax errors.
4. The method according to claim 2, characterized in that, The code logic detection includes: By comparing the first node in the first logical graph with the second node in the second logical graph, and the first edge in the first logical graph with the second edge in the second logical graph, wherein the first logical graph is determined based on the first candidate code, and the second logical graph is determined based on the pseudocode, the first node and the first edge in the first logical graph respectively represent the logical relationship between the data features of the first candidate code and the data features, and the second node and the second edge in the second logical graph respectively represent the key elements in the pseudocode and the logical relationship between the key elements; The code segment corresponding to the first node that is redundant or missing is designated as a code logic error code segment, and / or the code segment corresponding to the first side that is inconsistent with the second side is designated as a code logic error code segment.
5. The method according to claim 2, characterized in that, The code intent detection includes: Semantic information of the pseudocode and abstract syntax tree features of the first candidate code are extracted respectively; Calculate the matching degree between the semantic information and the abstract syntax tree features; The code segment corresponding to the matching degree that is less than a preset threshold is designated as a code intent error code segment.
6. The method according to any one of claims 3 to 5, characterized in that, The step of taking corresponding correction strategies to correct the first candidate code according to the error type includes at least one of the following: For the syntax error, a matching character that matches the syntax error code segment is selected from the preset syntax rule library and used to replace the corresponding character in the syntax error code segment; For the aforementioned code logic error, based on the second logic graph, perform at least one of the following: delete redundant code logic error segments, add missing code logic error segments, and correct the logical relationships between the data features in the code logic error segments.
7. The method according to any one of claims 3 to 5, characterized in that, The step of taking corresponding correction strategies to correct the first candidate code according to the error type further includes: For the code intent error, semantic reasoning is performed on the semantic information corresponding to the matching degree that is less than a preset threshold from multiple dimensions to obtain multiple potential intents; The multiple potential intentions are input into the cross-modal knowledge transfer model to generate multiple new code snippets; The new code segment is merged into the first candidate code.
8. The method according to claim 1, characterized in that, The code generation model was trained in the following way: The current character of the existing target code is input into the first long short-term memory network to obtain state features, wherein the state features characterize the temporal features of the current character sequence of the existing target code; The first fused feature is obtained by fusing local features and state features through a local attention mechanism. The local features are obtained by inputting vector features into a feature pyramid network. The local features represent the local information of the existing pseudocode. The vector features are obtained by performing word vector encoding and position encoding on the existing pseudocode. The first fused feature and the global feature are fused through a global attention mechanism to obtain the second fused feature; wherein, the global feature is obtained by inputting the vector feature into a second long short-term memory network, and the global feature represents the overall semantic information of the existing pseudocode; Based on the second fusion feature, the next character in the output code is predicted using the normalized exponential function, and the predicted character is obtained. The next character of the existing target code is input into the first long short-term memory network to update the state features; Repeat the operations of obtaining the first fusion feature, obtaining the second fusion feature, and obtaining the predicted character until the output code is complete.
9. The method according to claim 8, characterized in that, The step of predicting the next character in the output code based on the second fusion feature using a normalized exponential function, and obtaining the predicted character, includes: Based on the second fusion feature, the next character in the output code is predicted using the normalized exponential function to obtain the initial predicted character; Calculate the difference between the initial predicted character and the next character in the existing target code; Based on the difference, calculate the current loss function and adjust the model parameters; Based on the adjusted model parameters, the initial predicted character is re-acquired; Repeat the difference calculation, the model parameter adjustment, and the initial predicted character acquisition until the current loss function converges; The initial predicted character after the current loss function converges is used as the final output predicted character.
10. A code generation device, characterized in that, The device includes: The candidate code generation module is used to input pseudocode into the code generation model and generate the first candidate code; A code detection module is used to determine the error type of the first candidate code, wherein the error type includes syntax errors, code logic errors, and code intent errors; The code correction module is used to correct the first candidate code according to the error type and adopt the corresponding correction strategy to obtain the second candidate code; The code quality scoring module is used to score the second candidate code based on code quality attributes, wherein the code quality attributes include the conciseness of the second candidate code and the degree of adaptability of the second candidate code to the business scenario corresponding to the pseudocode; The code generation module is used to determine the final generated code based on the scoring results of the second candidate code.
11. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs. The characteristic feature is that 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 9.
12. A computer-readable storage medium having a computer program or instructions stored thereon, characterized in that, 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 9.
13. A computer program product, comprising a computer program or instructions, characterized in that, 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 9.