Code generation method, code generation device, electronic equipment and storage medium

By parsing large language models and processing code generation instructions through masking, the problem of low efficiency in manual code writing is solved, and efficient and accurate code generation is achieved.

CN121934834APending Publication Date: 2026-04-28PING AN TECH (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
PING AN TECH (SHENZHEN) CO LTD
Filing Date
2026-01-20
Publication Date
2026-04-28

AI Technical Summary

Technical Problem

Manually writing code is inefficient, especially when dealing with complex business modules, as it requires more time to understand and design solutions.

Method used

The code generation instructions are parsed using a large language model, key information is ignored by masking, scores are calculated to enhance the results, code snippets that meet the generation requirements are selected, and target code is generated.

Benefits of technology

It improves the efficiency and accuracy of code generation, effectively combats the attention dilution effect, and maintains attention to key instruction information.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121934834A_ABST
    Figure CN121934834A_ABST
Patent Text Reader

Abstract

The embodiment of the invention provides a code generation method, a code generation device, electronic equipment and a storage medium, belongs to the technical field of artificial intelligence, and is applied to the field of financial science and technology and the field of digital medical treatment. The method comprises the steps of obtaining a code generation instruction, wherein the code generation instruction comprises a generation demand field; performing instruction analysis on the code generation instruction through a preset large language model to obtain a first score of a preset code snippet; performing mask processing on the generation demand field to obtain a mask instruction; performing instruction analysis on the mask instruction through a large language model to obtain a second score of the preset code snippet; performing score enhancement on the first score according to the second score to obtain a target score of the preset code snippet; screening the preset code snippets according to the target score to obtain selected code snippets; and generating a target code according to the code generation instruction, the mask instruction and the selected code snippet. According to the embodiment of the invention, the code generation efficiency can be improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of artificial intelligence technology, and is applied to the fields of fintech and digital healthcare, and particularly to a code generation method, code generation device, electronic device and storage medium. Background Technology

[0002] As business expands, it becomes necessary to write code to develop new business modules to adapt to ever-changing business needs. For example, in fintech scenarios, code is written for payment request processing, user authentication, transaction data encryption, and bank interface integration to support mobile payment services. Similarly, in digital healthcare scenarios, code is written to develop medical image analysis tools to analyze medical images and locate lesions.

[0003] In related technologies, business modules are developed by manually writing code. However, when faced with complex business modules, manually writing code requires more time to understand and design solutions, resulting in low code generation efficiency. Summary of the Invention

[0004] The main objective of this application is to provide a code generation method, a code generation device, an electronic device, and a storage medium, with the aim of improving the efficiency of code generation.

[0005] To achieve the above objectives, a first aspect of this application proposes a code generation method, the method comprising: Obtain code generation instructions; wherein, the code generation instructions include generating requirement fields; The code generation instructions are parsed using a pre-defined large language model to obtain the first score of the pre-defined code fragment. The generated requirement field is masked to obtain a mask instruction; The masked instructions are parsed using the large language model to obtain the second score of the preset code fragment; The first score is enhanced based on the second score to obtain the target score of the preset code snippet; The preset code snippets are filtered based on the target score to obtain the selected code snippets; Target code is generated based on the code generation instructions, the masking instructions, and the selected code fragment.

[0006] In some embodiments, the step of enhancing the first score based on the second score to obtain the target score of the preset code snippet includes: Calculate the difference score between the first score and the second score; The first score is enhanced based on the difference score to obtain the target score.

[0007] In some embodiments, the step of enhancing the first score based on the difference score to obtain the target score includes: The difference scores are amplified based on a preset anchoring coefficient to obtain candidate scores; wherein the anchoring coefficient is greater than 1. The candidate score and the first score are added together to obtain the target score.

[0008] In some embodiments, before amplifying the difference scores according to preset anchoring coefficients to obtain candidate scores, the method further includes: obtaining the anchoring coefficients. The step of obtaining the anchoring coefficient includes: Based on the first score, the preset code snippets are filtered to obtain candidate code snippets; The large language model is used to predict the code generation instructions and the candidate code fragments to obtain the prediction confidence of the preset code fragment; The anchoring coefficient is obtained based on the prediction confidence level; wherein the prediction confidence level and the anchoring coefficient are inversely proportional.

[0009] In some embodiments, obtaining the anchoring coefficient based on the predicted confidence level includes: If the prediction confidence is less than a first preset threshold or greater than a second preset threshold, then a target adjustment factor is obtained; wherein, the first preset threshold is less than the second preset threshold, and the target adjustment factor is greater than 0 and less than 1; The prediction confidence level is adjusted according to the target adjustment factor to obtain a reference confidence level; The anchoring coefficient is obtained by mapping the reference confidence level.

[0010] In some embodiments, obtaining the target adjustment factor includes: Obtain the preset initial adjustment factor, sample instructions, and sample codes; The large language model is used to predict the code based on the initial adjustment factor and the sample instructions to obtain the predicted code. Calculate the code difference loss between the sample code and the predicted code; The initial adjustment factor is updated based on the code difference loss to obtain the target adjustment factor.

[0011] In some embodiments, generating target code based on the code generation instructions, the masking instructions, and the selected code fragment includes: The code generation instructions and the selected code fragments are parsed using the large language model to obtain a third score for the preset code fragments; The masked instructions and the selected code snippets are parsed using the large language model to obtain a fourth score for the preset code snippets; The third score is enhanced based on the fourth score to obtain a reference score for the preset code snippet; The preset code snippets are filtered based on the reference scores to obtain reference code snippets; The selected code segment and the reference code segment are concatenated to obtain the target code.

[0012] To achieve the above objectives, a second aspect of this application provides a code generation apparatus, the apparatus comprising: An instruction acquisition module is used to acquire code generation instructions; wherein, the code generation instructions include generation requirement fields; The first instruction parsing module is used to parse the code generation instructions using a preset large language model to obtain the first score of the preset code fragment. The masking module is used to mask the generated requirement fields to obtain masking instructions; The second instruction parsing module is used to parse the masked instruction through the large language model to obtain the second score of the preset code segment. An enhancement module is used to enhance the first score based on the second score to obtain the target score of the preset code snippet; The filtering module is used to filter the preset code snippets according to the target score to obtain selected code snippets; The generation module is used to generate target code based on the code generation instructions, the masking instructions, and the selected code fragment.

[0013] To achieve the above objectives, a third aspect of this application provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the method described in the first aspect.

[0014] To achieve the above objectives, a fourth aspect of the present application provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the method described in the first aspect.

[0015] The code generation method, device, electronic device, and computer-readable storage medium proposed in this application obtain code generation instructions, including a generation requirement field, to generate target code that meets the generation requirements. A large language model is used to parse the code generation instructions to understand their intent and obtain a first score for a preset code segment. The generation requirement field is masked to ignore key information in the code generation instructions, resulting in a masked instruction. The large language model is then used to parse the masked instruction to obtain the code generation result after ignoring key instructions, resulting in a second score for the preset code segment. The attention dilution effect of the large language model causes the model's attention to the code generation instructions to gradually decrease, leading to the generated code deviating from the instruction intent. To maintain the model's attention to key instructions during code generation, the first score is enhanced based on the second score, enabling the model to focus on key instruction information and counteracting the attention dilution effect, resulting in a target score for the preset code segment. The preset code segments are then filtered based on the target score to obtain code segments that meet the instruction intent, resulting in selected code segments. The system automatically generates target code that meets the code generation requirements based on code generation instructions, masking instructions, and selected code snippets, thereby improving the accuracy and efficiency of code generation. Attached Figure Description

[0016] Figure 1 This is a flowchart of the code generation method provided in the embodiments of this application; Figure 2 yes Figure 1 The flowchart of step S150 in the middle; Figure 3 yes Figure 2 The flowchart of step S220 in the text; Figure 4 This is another flowchart of the code generation method provided in the embodiments of this application; Figure 5 yes Figure 4 The flowchart of step S430 in the middle; Figure 6 yes Figure 5 The flowchart of step S510 in the text; Figure 7 yes Figure 1 The flowchart of step S170 in the process; Figure 8 This is a schematic diagram of the structure of the code generation apparatus provided in the embodiments of this application; Figure 9 This is a schematic diagram of the hardware structure of the electronic device provided in the embodiments of this application. Detailed Implementation

[0017] To make the objectives, technical solutions, and advantages of this application clearer, the following detailed description is provided in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are merely illustrative and not intended to limit the scope of this application.

[0018] It should be noted that although functional modules are divided in the device schematic diagram and a logical order is shown in the flowchart, in some cases, the steps shown or described may be performed in a different order than the module division in the device or the order in the flowchart. The terms "first," "second," etc., in the specification, claims, and the aforementioned drawings are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence.

[0019] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which this application belongs. The terminology used herein is for the purpose of describing embodiments of this application only and is not intended to limit this application.

[0020] As business expands, it becomes necessary to write code to develop new business modules to adapt to ever-changing business needs. For example, in fintech scenarios, code is written for payment request processing, user authentication, transaction data encryption, and bank interface integration to support mobile payment services. Similarly, in digital healthcare scenarios, code is written to develop medical image analysis tools to analyze medical images and locate lesions.

[0021] In related technologies, business modules are developed by manually writing code. However, when faced with complex business modules, manually writing code requires more time to understand and design solutions, resulting in low code generation efficiency.

[0022] Based on this, embodiments of this application provide a code generation method, a code generation apparatus, an electronic device, and a computer-readable storage medium, aiming to improve the efficiency of code generation.

[0023] The code generation method, code generation device, electronic device, and computer-readable storage medium provided in this application are specifically described through the following embodiments. First, the code generation method in this application is described.

[0024] The code generation method provided in this application relates to the field of artificial intelligence technology. The code generation method provided in this application can be applied to a terminal, a server, or software running on either a terminal or a server. In some embodiments, the terminal can be a smartphone, tablet, laptop, desktop computer, etc.; the server can be configured as an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN, and big data and artificial intelligence platforms; the software can be an application implementing the code generation method, but is not limited to the above forms.

[0025] This application can be used in a wide variety of general-purpose or special-purpose computer system environments or configurations. Examples include: personal computers, server computers, handheld or portable devices, tablet devices, multiprocessor systems, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputers, mainframe computers, and distributed computing environments including any of the above systems or devices. This application can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc., that perform specific tasks or implement specific abstract data types. This application can also be practiced in distributed computing environments where tasks are performed by remote processing devices connected via a communication network. In distributed computing environments, program modules can reside in local and remote computer storage media, including storage devices.

[0026] Figure 1 This is an optional flowchart of the code generation method provided in the embodiments of this application. Figure 1 The method may include, but is not limited to, steps S110 to S170.

[0027] Step S110: Obtain code generation instructions; wherein, code generation instructions include generating requirement fields; Step S120: The code generation instructions are parsed using a preset large language model to obtain the first score of the preset code fragment; Step S130: Mask the generated requirement fields to obtain mask instructions; Step S140: Parse the masked instructions using a large language model to obtain the second score of the preset code segment; Step S150: Enhance the first score based on the second score to obtain the target score of the preset code snippet; Step S160: Filter the preset code snippets according to the target score to obtain the selected code snippets; Step S170: Generate target code based on code generation instructions, masking instructions, and selected code snippets.

[0028] Steps S110 to S170, as illustrated in this embodiment, automatically generate target code by leveraging the powerful natural language understanding capabilities of a large language model to parse code generation instructions. This solves the problem of time-consuming manual code writing, improves code generation efficiency, and thus enhances software development efficiency. Simultaneously, by augmenting code generation results that identify key instruction information based on those that have been ignored, the model maintains attention to key instruction information during code generation, effectively combating the attention dilution effect and improving the accuracy of code generation.

[0029] In step S110 of some embodiments, when a user needs to generate code, they input a code generation instruction through a client providing the code generation service. The client then sends the code generation instruction to the backend server, which responds to the instruction and generates the target code. The code generation instruction is a natural language command sent by the user to the client to request the generation of code for a specific function. The code generation instruction includes a generation requirement field, which defines the code's functional requirements, programming language requirements, code style requirements, performance requirements, etc., and can be used to indicate the code generation requirements. This field is key instruction information within the code generation instruction. Depending on the user's needs, the code generation instruction can be a simple code function description instruction or a more detailed, refined instruction.

[0030] Taking fintech scenarios as an example, in mobile payment services, code generation instructions can be simple code function description instructions, such as generating a Python class for handling mobile payment requests. Code generation instructions can also be instructions that describe requirements in detail, such as: function description: receive payment requests, verify the validity of payment requests, call third-party payment interfaces to complete payment, and handle payment results including success, failure, timeout, etc.; programming language: Python; coding style: conforms to PEP8 standards and includes detailed comments; performance requirements: efficiently process payment requests.

[0031] Taking digital healthcare scenarios as an example, in AI-assisted diagnosis, code generation instructions can be simple code function description instructions, such as generating a functional module for locating brain lesions. Code generation instructions can also be instructions that describe requirements in detail, such as receiving brain CT images, preprocessing the brain CT images, and calling a deep learning model to detect lesions in the preprocessed brain CT images.

[0032] In step S120 of some embodiments, the large language model adopts a transformer-based neural network architecture, which is trained on a large amount of text data and has powerful natural language understanding capabilities. It can handle complex code generation instructions and generate corresponding code. The code generation instructions are input into the large language model for instruction parsing to obtain the first score of the preset code fragment at the current time step. The large language model has a vocabulary that records all possible code tokens learned by the model during training. Code tokens are meaningful code symbols, which can be keywords (such as if, for, while, def, etc.), identifiers (such as variable names, function names, class names, etc.), operators (such as +, -, *, / , =, etc.), symbols (such as (), {}, [], etc.), literals (such as the number 123, the string hello, etc.), and special markers (such as code start markers, code end markers, etc.). The preset code fragment is a code token in the vocabulary, and the first score is the probability of the occurrence of the code token in the code to be generated.

[0033] In step S130 of some embodiments, the generation requirement field is used as anchor text, and the anchor text is masked to ignore the generation requirement field, i.e., the key instruction information, in the code generation instruction, thus obtaining a masked instruction. Specifically, the generation requirement field in the code generation instruction can be replaced with a mask character to obtain a masked instruction. The mask character is used to cover the generation requirement field and can be set according to the actual situation, such as "*". For example, if the code generation instruction is "Generate a Python class for handling mobile payment requests", and the generation requirement fields are mobile payment and Python, then the masked instruction is "Generate a * class for handling **** requests".

[0034] In step S140 of some embodiments, the mask instruction is input to the large language model for instruction parsing, and the response result of the model ignoring key instruction information is obtained to obtain the second score of the preset code fragment at the current time step. The preset code fragment is a code tag in the vocabulary, and the second score is the probability of the code tag appearing in the code to be generated. It should be noted that the preset code fragment in step S140 and the preset code fragment in step S120 are the same code tag.

[0035] Please see Figure 2 In some embodiments, step S150 may include, but is not limited to, steps S210 to S220: Step S210: Calculate the difference score between the first score and the second score; Step S220: Enhance the first score based on the difference score to obtain the target score.

[0036] In step S210 of some embodiments, the large language model generates a new token sequence by analyzing the complex relationships between code tokens, forming the target code. As the large language model generates more and more code tokens, its attention to key instruction information in the code generation instructions gradually decreases, while it pays more attention to the most recently generated code tokens, resulting in an attention dilution effect. Key instruction information is a necessary constraint for code generation; if it is not paid attention to or not paid enough attention to, the code generated by the model will deviate from the user's core intent, leading to inaccurate code. To improve the accuracy of code generation, this application embodiment compares the model's response difference when key instructions are recognized and when key instructions are ignored to calculate the influence of key instructions on the final code generation result. Specifically, the first score is subtracted from the second score to obtain the difference score. The difference score is used to quantify the difference in the model's response when key instructions are recognized and when key instructions are ignored, representing the pure semantic influence of the generation requirement field.

[0037] In step S220 of some embodiments, the first score is enhanced based on the difference score to apply the influence of the key instruction information to the model's original code generation result, so as to prompt the model not to ignore the key instruction information, so that the model always maintains a high level of attention to the key instruction throughout the code generation process, thereby significantly improving the accuracy of code generation.

[0038] By using steps S210 to S220 above, the attention dilution effect of the model can be countered without retraining the model, thus improving the accuracy and efficiency of code generation.

[0039] Please see Figure 3 In some embodiments, step S220 may include, but is not limited to, steps S310 to S320: Step S310: Amplify the difference scores according to the preset anchoring coefficient to obtain candidate scores; wherein the anchoring coefficient is greater than 1. Step S320: Add the candidate scores and the first score together to obtain the target score.

[0040] In step S310 of some embodiments, to counteract the attention dilution effect, an anchoring coefficient greater than 1 is multiplied by the difference score to amplify the semantic influence of the generation requirement field on code generation, thus obtaining a candidate score. The anchoring coefficient can be set according to the actual situation, such as 1.25, which is a general value that can achieve stable performance improvement.

[0041] It's important to note that the range of the anchoring coefficient determines how it affects the code generation result. An anchoring coefficient greater than 1 (w > 1), where w represents the anchoring coefficient, indicates that the impact of key instructions on the code generation result is amplified. An anchoring coefficient equal to 1 (w = 1) indicates that no effect is exerted on the model's original output. An anchoring coefficient greater than or equal to 0 and less than 1... This indicates a reduction in the impact of key instructions on the code generation result. If the anchoring coefficient is less than 0, i.e., w < 0, it means the model produces the opposite effect to the key instructions. In this embodiment, the anchoring coefficient is set to be greater than 1 to combat the attention dilution effect present in large language models.

[0042] In step S320 of some embodiments, the candidate score and the first score are added together to obtain the target score of the preset code fragment at the current time step. The target score is used to indicate the probability of generating the preset code fragment at the current time step. It should be noted that the larger the target score of the preset code fragment, the greater the probability of generating the preset code fragment at the current time step.

[0043] Through the above steps S310 to S320, the semantic impact of key instructions on the original output of the model can be amplified to counteract the attention dilution effect.

[0044] Please see Figure 4 In some embodiments, prior to step S310, the code generation method may include, but is not limited to, steps S410 to S430: Step S410: Filter the preset code snippets according to the first score to obtain candidate code snippets; Step S420: Use a large language model to predict the code generation instructions and candidate code fragments to obtain the prediction confidence of the preset code fragments; Step S430: Obtain the anchoring coefficient based on the prediction confidence level; wherein the prediction confidence level and the anchoring coefficient are inversely proportional.

[0045] In step S410 of some embodiments, the large language model has a vocabulary, which includes multiple preset code segments, i.e. code tags. The first score of each preset code segment at the current time step is calculated, and the preset code segment with the largest first score is selected to obtain the candidate code segment at the current time step.

[0046] In step S420 of some embodiments, the large language model generates a new token at each time step during code generation. To determine the probability of generating a token in the next time step, the code generation instruction and candidate code segments from the current time step are input into the large language model for code prediction, and the probability of each preset code segment in the vocabulary in the next time step is output. The smallest probability can be selected as the first probability, and the largest probability as the second probability. A weighted average of the first and second probabilities is calculated to obtain the prediction confidence. The prediction confidence value is in the range of [0,1] and is used to indicate the probability that the model's prediction result is correct. The prediction result is that the token generated by the large language model in the next time step is the preset code segment corresponding to the second probability.

[0047] In step S430 of some embodiments, an anchoring coefficient is calculated based on the prediction confidence level, and the prediction confidence level and the anchoring coefficient are inversely proportional. A higher prediction confidence level indicates a greater likelihood that the model's prediction is correct, allowing for smaller adjustments to the model and a smaller value for the anchoring coefficient. Conversely, a lower prediction confidence level allows for a larger value for the anchoring coefficient.

[0048] Through steps S410 to S430 above, the anchoring coefficient can be adaptively adjusted according to the prediction confidence level, so as to calculate the influence exerted on the original code generation result based on the anchoring coefficient.

[0049] Please see Figure 5 In some embodiments, step 430 may include, but is not limited to, steps S510 to S530: Step S510: If the prediction confidence is less than the first preset threshold or greater than the second preset threshold, then obtain the target adjustment factor; wherein, the first preset threshold is less than the second preset threshold, and the target adjustment factor is greater than 0 and less than 1. Step S520: Adjust the prediction confidence level according to the target adjustment factor to obtain the reference confidence level; Step S530: Map the reference confidence level to obtain the anchoring coefficient.

[0050] In step S510 of some embodiments, the prediction confidence is denoted as P, the first preset threshold is denoted as T1, and the second preset threshold is denoted as T2. If the prediction confidence is less than the first preset threshold (P < T1), it indicates that the probability of the model's prediction being correct is too low, and the model is unreliable; or if the prediction confidence is greater than the second preset threshold (P > T2), it indicates that the probability of the model's prediction being correct is too high, and the model is overfitting. In this case, a target adjustment factor is obtained. The first and second preset thresholds can be set according to actual conditions, ensuring that the first preset threshold is less than the second preset threshold. For example, the first preset threshold is set to 0.3, and the second preset threshold is set to 0.8. The target adjustment factor is used to adjust the prediction confidence; the target adjustment factor is greater than 0 and less than 1.

[0051] If the prediction confidence level is greater than or equal to the first preset threshold and less than or equal to the second preset threshold (T1≤P≤T2), it indicates that the prediction confidence level is within the normal range. Therefore, the prediction confidence level is mapped using a mapping function to obtain the anchoring coefficient. The dependent variable and independent variable of the mapping function are inversely proportional; the prediction confidence level can be used as the independent variable, and the anchoring coefficient as the dependent variable. The mapping function can be a reciprocal function, an exponential function, etc. A reciprocal function can be set in the following form: , Where confidence represents the prediction confidence level; It is a very small positive number (such as 0.01) used to avoid the unstable situation where the denominator is close to 0.

[0052] The exponential function can be set in the following form: , Where e is the base of the natural logarithm; k is a positive constant used to control the slope of the function; and confidence represents the prediction confidence level.

[0053] In step S520 of some embodiments, 1 is subtracted from the predicted confidence level to obtain a difference, the target adjustment factor is multiplied by this difference, and the product is added to the predicted confidence level to obtain a reference confidence level. The formula for calculating the reference confidence level is expressed as: , Where confidence represents the prediction confidence level; This represents the target adjustment factor, which is greater than 0 and less than 1.

[0054] In step S530 of some embodiments, the reference confidence level is mapped using the mapping function described in step S510 to obtain the anchoring coefficient.

[0055] Through the above steps S510 to S530, the anchoring coefficient can be obtained, so as to amplify the semantic influence of the anchored text based on the anchoring coefficient.

[0056] Please see Figure 6 In some embodiments, step 510 may include, but is not limited to, steps S610 to S640: Step S610: Obtain the preset initial adjustment factor, sample instruction, and sample code; Step S620: Use a large language model to predict the code of the initial adjustment factor and sample instructions to obtain the predicted code; Step S630: Calculate the code difference loss between the sample code and the predicted code; Step S640: Update the initial adjustment factor based on the code difference loss to obtain the target adjustment factor.

[0057] In step S610 of some embodiments, a preset initial adjustment factor is obtained, which is greater than 0 and less than 1. Sample instructions and corresponding sample codes are obtained from the sample dataset. The sample instructions are natural language instructions used for code generation, and may be the same as or different from the code generation instructions in step S110. The sample codes are canonical code samples that match the sample instructions.

[0058] In step S620 of some embodiments, sample instructions are input into a large language model for code prediction to obtain initial codes and first code scores for the initial codes. The initial codes include first code segments at multiple time steps, each with a first segment probability. The first code score is the product of the first segment probabilities of the first code segments generated at each time step. The maximum and minimum first code scores are obtained, and a weighted average is calculated to obtain the code confidence score. Referring to the confidence adjustment formula in step S520, the code confidence score is adjusted according to the initial adjustment factor to obtain candidate confidence scores. The candidate confidence scores are mapped using the aforementioned mapping function to obtain initial coefficients, which are greater than 1.

[0059] Following step S130, the sample instruction is masked, and the masked sample instruction is input into the large language model for code prediction, resulting in a masked code and a second code score for the masked code. The masked code includes second code segments at multiple time steps, each with a second segment probability. The second code score is the product of the second segment probabilities generated at each time step. The code difference score between the first code score and the second code score is calculated. Referring to steps S310 to S320, the code difference score is amplified based on the initial coefficients to obtain an amplified score. This amplified score is then added to the first code score to obtain the initial score. The initial code with the largest initial score is selected as the predicted code.

[0060] It should be noted that the large language model outputs the probabilities of multiple preset code snippets at each time step. The first code snippet is any preset code snippet that can be output at the current time step, thus there are multiple initial codes. The second code snippet is the preset code snippet with the highest probability at the current time step, thus there is only one mask code.

[0061] In step S630 of some embodiments, the sample code and the predicted code are vectorized respectively to obtain the sample vector of the sample code and the predicted vector of the predicted code. The Euclidean distance between the sample vector and the predicted vector is calculated to obtain the code difference loss.

[0062] In step S640 of some embodiments, the initial adjustment factor is updated with the goal of minimizing code difference loss to obtain the optimal adjustment factor and thus the target adjustment factor.

[0063] Through the above steps S610 to S640, the optimal adjustment factor can be obtained, and the prediction confidence can be adjusted based on the optimal adjustment factor.

[0064] In step S160 of some embodiments, the target score of each preset code segment in the vocabulary is calculated, and the preset code segment with the largest target score is selected as the selected code segment for the current time step.

[0065] Please see Figure 7 In some embodiments, step S170 may include, but is not limited to, steps S710 to S750: Step S710: The code generation instructions and selected code fragments are parsed using a large language model to obtain the third score of the preset code fragment; Step S720: The masked instructions and selected code snippets are parsed using a large language model to obtain the fourth score of the preset code snippet; Step S730: Enhance the score of the third score based on the fourth score to obtain a reference score for the preset code snippet; Step S740: Filter the preset code snippets according to the reference scores to obtain reference code snippets; Step S750: Concatenate the selected code snippet and the reference code snippet to obtain the target code.

[0066] In step S710 of some embodiments, the code generation instruction and the selected code fragment are input into a large language model for instruction parsing to obtain a third score for the preset code fragment in the next time step. The third score is the probability of the preset code fragment appearing in the code to be generated in the next time step under the action of the code generation instruction.

[0067] In step S720 of some embodiments, the mask instruction and the selected code fragment are input into a large language model for instruction parsing to obtain a fourth score for the preset code fragment in the next time step. The fourth score is the probability of the preset code fragment appearing in the code to be generated in the next time step under the action of the mask instruction.

[0068] In step S730 of some embodiments, the third score is subtracted from the fourth score to obtain the difference score between the third and fourth scores. The difference score is amplified according to a preset anchoring coefficient, and the amplified difference score is added to the third score to obtain a reference score for a preset code segment. The anchoring coefficient can be determined with reference to steps S510 to S530, and will not be described in detail here.

[0069] In step S740 of some embodiments, a reference score is calculated for each preset code segment, and the preset code segment with the highest reference score is selected as the reference code segment.

[0070] In step S750 of some embodiments, the selected code segment of the current time step and the reference code segment of the next time step are concatenated to obtain the target code.

[0071] By using steps S710 to S750, the user's core intent is kept in mind at each time step of code generation, thus improving the accuracy of code generation.

[0072] This application provides a code generation method, including: obtaining a code generation instruction; inputting the code generation instruction and all code fragments generated in previous time steps into a large language model for instruction parsing to obtain a first score for a preset code fragment in the current time step; masking the generation requirement field in the code generation instruction to obtain a mask instruction; inputting the mask instruction and all code fragments generated in previous time steps into a large language model for instruction parsing to obtain a second score for the preset code fragment in the current time step; calculating the difference score between the first score and the second score, and amplifying the difference score using an anchoring coefficient greater than 1 to obtain a target score; selecting the preset code fragment with the largest target score as the code fragment generated in the current time step; if the code fragment generated in the current time step is a code terminator, then concatenating all code fragments generated in previous time steps and the code fragment generated in the current time step in chronological order to obtain the target code; if the code fragment generated in the current time step is not a code terminator, then repeating the above steps.

[0073] It should be noted that if no code snippet has been generated before the current time step, the code generation instruction or mask instruction will be input into the large language model.

[0074] Please see Figure 8 This application also provides a code generation apparatus that can implement the above-described code generation method. The code generation apparatus includes: The instruction acquisition module 810 is used to acquire code generation instructions; wherein, the code generation instructions include generation requirement fields; The first instruction parsing module 820 is used to parse the code generation instructions using a preset large language model to obtain the first score of the preset code fragment. The mask module 830 is used to mask the generated requirement fields to obtain mask instructions; The second instruction parsing module 840 is used to parse the masked instructions using a large language model to obtain the second score of the preset code segment. Enhancement module 850 is used to enhance the score of the first score based on the second score to obtain the target score of the preset code snippet; The filtering module 860 is used to filter preset code snippets based on the target score to obtain the selected code snippets; The generation module 870 is used to generate target code based on code generation instructions, masking instructions, and selected code snippets.

[0075] The specific implementation of this code generation device is basically the same as the specific implementation of the code generation method described above, and will not be repeated here.

[0076] This application also provides an electronic device, which includes a memory and a processor. The memory stores a computer program, and the processor executes the computer program to implement the above-described code generation method. This electronic device can be any smart terminal, including tablet computers, in-vehicle computers, etc.

[0077] Please see Figure 9 , Figure 9 The hardware structure of an electronic device according to another embodiment is illustrated. The electronic device includes: The processor 910 can be implemented using a general-purpose central processing unit (CPU), microprocessor, application specific integrated circuit (ASIC), or one or more integrated circuits, and is used to execute relevant programs to implement the technical solutions provided in the embodiments of this application. The memory 920 can be implemented as a read-only memory (ROM), static storage device, dynamic storage device, or random access memory (RAM). The memory 920 can store the operating system and other applications. When the technical solutions provided in the embodiments of this specification are implemented through software or firmware, the relevant program code is stored in the memory 920 and is called and executed by the processor 910 using the code generation method of the embodiments of this application. The input / output interface 930 is used to implement information input and output; The communication interface 940 is used to enable communication and interaction between this device and other devices. Communication can be achieved through wired means (such as USB, network cable, etc.) or wireless means (such as mobile network, WIFI, Bluetooth, etc.). Bus 950 transmits information between various components of the device (e.g., processor 910, memory 920, input / output interface 930, and communication interface 940); The processor 910, memory 920, input / output interface 930 and communication interface 940 are connected to each other within the device via bus 950.

[0078] This application also provides a computer-readable storage medium storing a computer program that, when executed by a processor, implements the above-described code generation method.

[0079] Memory, as a non-transitory computer-readable storage medium, can be used to store non-transitory software programs and non-transitory computer-executable programs. Furthermore, memory may include high-speed random access memory, and may also include non-transitory memory, such as at least one disk storage device, flash memory device, or other non-transitory solid-state storage device. In some embodiments, memory may optionally include memory remotely located relative to the processor, and these remote memories can be connected to the processor via a network. Examples of such networks include, but are not limited to, the Internet, intranets, local area networks, mobile communication networks, and combinations thereof.

[0080] The code generation method, device, electronic device, and computer storage medium provided in this application automatically generate target code by leveraging the powerful natural language understanding capabilities of a large language model to parse code generation instructions. This solves the problem of time-consuming manual code writing and improves code generation efficiency. Furthermore, by enhancing code generation results that identify key instruction information based on those that have been ignored, the model maintains attention to key instruction information during code generation, effectively combating the attention dilution effect and improving the accuracy of code generation.

[0081] The embodiments described in this application are for the purpose of more clearly illustrating the technical solutions of the embodiments of this application, and do not constitute a limitation on the technical solutions provided by the embodiments of this application. As those skilled in the art will know, with the evolution of technology and the emergence of new application scenarios, the technical solutions provided by the embodiments of this application are also applicable to similar technical problems.

[0082] Those skilled in the art will understand that the technical solutions shown in the figures do not constitute a limitation on the embodiments of this application, and may include more or fewer steps than shown, or combine certain steps, or different steps.

[0083] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.

[0084] Those skilled in the art will understand that all or some of the steps in the methods disclosed above, as well as the functional modules / units in the systems and devices, can be implemented as software, firmware, hardware, or suitable combinations thereof.

[0085] The terms “first,” “second,” “third,” “fourth,” etc. (if present) in the specification and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such data can be interchanged where appropriate so that the embodiments of this application described herein can be implemented in orders other than those illustrated or described herein. Furthermore, the terms “comprising” and “having,” and any variations thereof, are intended to cover non-exclusive inclusion; for example, a process, method, system, product, or apparatus that comprises a series of steps or units is not necessarily limited to those steps or units explicitly listed, but may include other steps or units not explicitly listed or inherent to such processes, methods, products, or apparatus.

[0086] It should be understood that in this application, "at least one (item)" means one or more, and "more than" means two or more. "And / or" is used to describe the relationship between related objects, indicating that three relationships can exist. For example, "A and / or B" can represent three cases: only A exists, only B exists, and both A and B exist simultaneously, where A and B can be singular or plural. The character " / " generally indicates that the preceding and following related objects are in an "or" relationship. "At least one (item) of the following" or similar expressions refer to any combination of these items, including any combination of single or plural items. For example, at least one (item) of a, b, or c can represent: a, b, c, "a and b", "a and c", "b and c", or "a and b and c", where a, b, and c can be single or multiple.

[0087] In the several embodiments provided in this application, it should be understood that the disclosed apparatus and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of the units described above is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or units may be electrical, mechanical, or other forms.

[0088] The units described above as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.

[0089] Furthermore, the functional units in the various embodiments of this application can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit. The integrated unit can be implemented in hardware or as a software functional unit.

[0090] If the integrated unit is implemented as a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or all or part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes multiple instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods of the various embodiments of this application. The aforementioned storage medium includes various media capable of storing programs, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.

[0091] The preferred embodiments of the present application have been described above with reference to the accompanying drawings, but this does not limit the scope of the claims of the present application. Any modifications, equivalent substitutions, and improvements made by those skilled in the art without departing from the scope and substance of the embodiments of the present application shall be within the scope of the claims of the present application.

Claims

1. A code generation method, characterized in that, The method includes: Obtain code generation instructions; wherein, the code generation instructions include generating requirement fields; The code generation instructions are parsed using a pre-defined large language model to obtain the first score of the pre-defined code fragment. The generated requirement field is masked to obtain a mask instruction; The masked instructions are parsed using the large language model to obtain the second score of the preset code fragment; The first score is enhanced based on the second score to obtain the target score of the preset code snippet; The preset code snippets are filtered based on the target score to obtain the selected code snippets; Target code is generated based on the code generation instructions, the masking instructions, and the selected code fragment.

2. The method according to claim 1, characterized in that, The step of enhancing the first score based on the second score to obtain the target score of the preset code snippet includes: Calculate the difference score between the first score and the second score; The first score is enhanced based on the difference score to obtain the target score.

3. The method according to claim 2, characterized in that, The step of enhancing the first score based on the difference score to obtain the target score includes: The difference scores are amplified based on a preset anchoring coefficient to obtain candidate scores; wherein the anchoring coefficient is greater than 1. The candidate score and the first score are added together to obtain the target score.

4. The method according to claim 3, characterized in that, Before amplifying the difference scores according to a preset anchoring coefficient to obtain candidate scores, the method further includes: obtaining the anchoring coefficient. The step of obtaining the anchoring coefficient includes: Based on the first score, the preset code snippets are filtered to obtain candidate code snippets; The large language model is used to predict the code generation instructions and the candidate code fragments to obtain the prediction confidence of the preset code fragment; The anchoring coefficient is obtained based on the prediction confidence level; wherein the prediction confidence level and the anchoring coefficient are inversely proportional.

5. The method according to claim 4, characterized in that, The step of obtaining the anchoring coefficient based on the predicted confidence level includes: If the prediction confidence is less than a first preset threshold or greater than a second preset threshold, then a target adjustment factor is obtained; wherein, the first preset threshold is less than the second preset threshold, and the target adjustment factor is greater than 0 and less than 1; The prediction confidence level is adjusted according to the target adjustment factor to obtain a reference confidence level; The anchoring coefficient is obtained by mapping the reference confidence level.

6. The method according to claim 5, characterized in that, The acquisition of the target adjustment factor includes: Obtain the preset initial adjustment factor, sample instructions, and sample codes; The large language model is used to predict the code based on the initial adjustment factor and the sample instructions to obtain the predicted code. Calculate the code difference loss between the sample code and the predicted code; The initial adjustment factor is updated based on the code difference loss to obtain the target adjustment factor.

7. The method according to any one of claims 1 to 6, characterized in that, The step of generating target code based on the code generation instruction, the masking instruction, and the selected code fragment includes: The code generation instructions and the selected code fragments are parsed using the large language model to obtain a third score for the preset code fragments; The masked instructions and the selected code snippets are parsed using the large language model to obtain a fourth score for the preset code snippets; The third score is enhanced based on the fourth score to obtain a reference score for the preset code snippet; The preset code snippets are filtered based on the reference scores to obtain reference code snippets; The selected code segment and the reference code segment are concatenated to obtain the target code.

8. A code generation device, characterized in that, The device includes: An instruction acquisition module is used to acquire code generation instructions; wherein, the code generation instructions include generation requirement fields; The first instruction parsing module is used to parse the code generation instructions using a preset large language model to obtain the first score of the preset code fragment. The masking module is used to mask the generated requirement fields to obtain masking instructions; The second instruction parsing module is used to parse the masked instruction through the large language model to obtain the second score of the preset code segment. An enhancement module is used to enhance the first score based on the second score to obtain the target score of the preset code snippet; The filtering module is used to filter the preset code snippets according to the target score to obtain selected code snippets; The generation module is used to generate target code based on the code generation instructions, the masking instructions, and the selected code fragment.

9. An electronic device, characterized in that, The electronic device includes a memory and a processor, the memory storing a computer program, and the processor executing the computer program to implement the method according to any one of claims 1 to 7.

10. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it implements the method described in any one of claims 1 to 7.