Code generation method and device

By identifying and structuring the code requirements of the input prompt information, and generating candidate codes based on complete context information and optimizing them, the problems of low accuracy of code generation and insufficient understanding of the context in the prior art are solved, and more efficient and accurate code generation is achieved.

CN120045171AActive Publication Date: 2025-05-27BEIJING QIMIAO KINGDOM TECHNOLOGY CO LTD

Patent Information

Application Number
CN202510122639.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-01-24
Publication Date
2025-05-27
Estimated Expiration
2045-01-24

AI Technical Summary

Technical Problem

Existing code generation systems rely on predefined rules and templates, are difficult to deal with complex or dynamic needs, and have low accuracy in code generation and lack an understanding of the specific context of the project.

Method used

By identifying code requirements on the input prompt information, structured requirements identification results are generated, functional requirements and context information are clarified, candidate code is generated based on complete context information, and code optimization is carried out to improve accuracy and code quality.

Benefits of technology

It improves the accuracy of code generation, ensures that the generated code can be adapted to actual application scenarios, reduces environmental mismatch problems, and improves the execution efficiency, readability and maintainability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045171A_ABST
    Figure CN120045171A_ABST
Patent Text Reader

Abstract

The invention provides a code generation method and device. Comprising the following steps: in response to a code generation request, performing code demand identification on input prompt information to obtain a structured code demand identification result; the code demand identification result comprises a function demand and first context information; the first context information comprises context description content of the function demand in the code demand identification result; determining second context information based on the function demand and the first context information; the second context information comprises complete environment description content of the function requirement; generating a candidate code based on the function demand and the second context information; and performing code optimization on the candidate code to obtain a target code. According to the method, the target code is generated based on the second context information, and the second context information comprises the complete environment description content of the function requirement, so that the code generation system can generate the code according to the complete context information, and the code generation accuracy is 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 particularly to a code generation method and apparatus. Background Art

[0002] With the development of artificial intelligence technology, various algorithms emerge in an endless stream. With the research and development of algorithms, the amount of code for algorithms is gradually increasing, and the task of developers writing code is becoming increasingly heavy. Therefore, various intelligent algorithms for automatically generating code have emerged.

[0003] In the related art, the code automatic generation method mainly relies on a rule-based code generation system, that is, code is generated through predefined rules and templates. However, the rule-based code generation system can only handle some simple requirements, and the code generation accuracy is relatively low. Summary of the Invention

[0004] The embodiments of this application provide a code generation method and apparatus. This application generates target code based on second context information, enabling the code generation system to generate code according to complete context information, thereby improving the code generation accuracy.

[0005] The technical solution of the embodiments of this application is implemented as follows:

[0006] The embodiments of this application provide a code generation method, the method including: in response to a code generation request, performing code requirement recognition on input prompt information to obtain a structured code requirement recognition result; the code requirement recognition result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement recognition result; determining second context information based on the functional requirement and the first context information; the second context information includes the complete environment description content of the functional requirement; generating candidate code based on the functional requirement and the second context information; performing code optimization on the candidate code to obtain target code.

[0007] The embodiments of this application provide a code generation apparatus, including: an identification module, configured to perform code requirement recognition on input prompt information in response to a code generation request to obtain a structured code requirement recognition result; the code requirement recognition result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement recognition result; an extraction module, configured to determine second context information based on the functional requirement and the first context information; the second context information includes the complete environment description content of the functional requirement; a generation module, configured to generate candidate code based on the functional requirement and the second context information; an optimization module, configured to perform code optimization on the candidate code to obtain target code.

[0008] In the above solution, the determining and recognizing module is further configured to: in response to a code generation request, preprocess the input prompt information to obtain preprocessed information; perform semantic recognition on the preprocessed information to obtain key semantic features; perform structured processing on the key semantic features to obtain the structured code requirement recognition result.

[0009] In the above solution, the determining module is further configured to: determine the similarity between the functional requirement and each historical functional requirement in the preset resource library; based on the similarity, determine a target historical functional requirement from the preset resource library; extract third context information of the target historical functional requirement in the preset resource library; the third context information at least includes the historical prompt information of the historical functional requirement; perform information fusion on the third context information and the first context information to obtain the second context information.

[0010] In the above solution, the generating module is further configured to: parse the functional requirement to obtain a plurality of functional modules and the core element units of each functional module; based on the second context information and the core element units of each functional module, determine the module code of each functional module; splice the module codes of the plurality of functional modules according to a preset code logic to obtain the candidate code.

[0011] In the above solution, the core element units of each functional module include input information, processing flow, and output information; the generating module is further configured to: parse the second context information to obtain the attribute information of each functional module and the code architecture corresponding to each functional module; based on the core element units and the attribute information of each functional module, determine an input code segment corresponding to the input information, a process code segment corresponding to the processing flow, and an output code segment corresponding to the output information; add the input code segment, the process code segment, and the output code segment to the code architecture to obtain the module code of each functional module.

[0012] In the above solution, the generating module is further configured to: determine the requirement type of the functional requirement; in response to the requirement type being a target type, determine the rule content of the target type from the preset rule library; the rule content at least includes conditional statements; screen the conditional information corresponding to the conditional statements from the second context information; generate the candidate code based on the conditional information according to the rule content of the target type.

[0013] In the above solution, the generating module is further configured to: obtain the keywords in the condition information; determine a target template matching the keywords from a preset template library; generate at least one code statement according to the rule content of the target type; and add the code statement to the target template to obtain the candidate code.

[0014] In the above solution, the optimizing module is further configured to: obtain preset coding parameters; the preset coding parameters at least include code format parameters and variable naming parameters; adjust the candidate code based on the preset coding parameters to obtain an optimized code; the optimized code includes multiple sub-codes; detect redundant sub-codes from the optimized code; and delete the redundant sub-codes from the optimized code to obtain the target code.

[0015] In the above solution, the apparatus further includes a report generating module, configured to: in response to the failure of the target code to run, perform problem analysis on the target code to obtain a problem analysis result; the problem analysis result at least includes a syntax error result and a security vulnerability result of the target code; in response to inputting preset information to the target code, obtain an output result of a function in the target code, and compare the output result with a preset output result corresponding to the preset information to obtain an output test result; in response to the target code being in a running state, obtain running data of the target code, and determine a performance analysis result of the target code based on the running data; the running data of the target code at least includes the running time of the target code and the memory consumption when the target code is running; and generate a test report of the target code based on the problem analysis result, the output test result, and the performance analysis result.

[0016] An embodiment of the present application provides an electronic device, including: a memory for storing computer-executable instructions; and a processor for implementing the code generation method provided by the embodiment of the present application when executing the computer-executable instructions stored in the memory.

[0017] An embodiment of the present application provides a computer-readable storage medium storing a computer program or executable instructions, and when the computer program or computer-executable instructions are executed by a processor, the code generation method provided by the embodiment of the present application is implemented.

[0018] An embodiment of the present application provides a computer program product, which includes computer-executable instructions stored in a computer-readable storage medium; wherein, when a processor of an electronic device reads the computer-executable instructions from the computer-readable storage medium and executes the computer-executable instructions, the code generation method provided by the embodiment of the present application is implemented.

[0019] The embodiments of the present application have the following beneficial effects:

[0020] In the code generation method in the embodiments of the present application, by identifying code requirements from the input prompt information, a structured requirement identification result is generated, enabling the code generation system to clarify the functional requirements and the first context information, providing context information for candidate code generation; moreover, based on the functional requirements and the first context information, the environment description is further improved to obtain the second context information. Since the second context information contains a complete environment description, when generating candidate code according to the second context information, not only can the code generation accuracy be improved, but also it can be ensured that the generated code can adapt to the actual application scenario, reducing the problem of environment mismatch; in addition, optimizing the candidate code can improve the execution efficiency, readability, and maintainability of the code, ensuring that the target code complies with the preset coding specifications, thereby improving the code quality. Description of the Drawings

[0021] Figure 1 is an optional flowchart of the code generation method provided by the embodiments of the present application;

[0022] Figure 2 is a flowchart of generating candidate code based on functional requirements and the second context information provided by the embodiments of the present application;

[0023] Figure 3 is a flowchart of optimizing the candidate code to obtain the target code provided by the embodiments of the present application;

[0024] Figure 4 is a flowchart of generating a test report of the target code provided by the embodiments of the present application;

[0025] Figure 5 is a structural block diagram of a code generation device provided by the embodiments of the present application;

[0026] Figure 6 is a structural diagram of an electronic device provided by the embodiments of the present application. Detailed Embodiments

[0027] In order to make the objectives, technical solutions, and advantages of the present application clearer, the present application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be construed as limiting the present application. All other embodiments obtained by those of ordinary skill in the art without creative efforts shall fall within the scope of protection of the present application.

[0028] In the following description, reference is made to "some embodiments", which describe a subset of all possible embodiments. However, it can be understood that "some embodiments" can be the same subset or different subsets of all possible embodiments and can be combined with each other without conflict.

[0029] If similar descriptions such as "first / second" appear in the application documents, the following explanation shall be added. In the following description, the terms "first / second / third" only distinguish similar objects and do not represent a specific order for the objects. Understandably, "first / second / third" can be interchanged in a specific order or sequence when permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.

[0030] In the embodiments of the present application, the term "module" or "unit" refers to a computer program with a predetermined function or a part of a computer program, which works together with other related parts to achieve a predetermined goal, and can be fully or partially implemented by using software, hardware (such as a processing circuit or a memory), or a combination thereof. Similarly, a processor (or multiple processors or memories) can be used to implement one or more modules or units. In addition, each module or unit can be a part of the overall module or unit that includes the function of the module or unit.

[0031] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present application have the same meaning as commonly understood by those skilled in the art to which the present application belongs. The terms used in the embodiments of the present application are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.

[0032] Before describing the code generation method provided by the embodiments of the present application, first, the professional terms involved in the embodiments of the present application will be described:

[0033] Code generation system: A tool that uses artificial intelligence technologies, such as machine learning models, deep learning models, or large language models, to automatically generate corresponding code snippets based on the input information provided by developers.

[0034] To better understand the code generation method provided by the embodiments of the present application, the code generation methods in the related technologies will be described below.

[0035] In related technologies, code generation methods mainly include the following methods: 1) Rule-based code generation system: A rule-based code generation system usually relies on predefined rules and templates to generate code, which can handle some simple requirements, but its flexibility and scalability are poor, and it often cannot cope with complex or dynamic requirements. 2) Deep learning-based generation model: In recent years, generative pre-trained models have made remarkable breakthroughs in the field of natural language processing. Generative pre-trained models can learn language patterns from large-scale data and generate relatively fluent code according to the input. However, generative pre-trained models usually lack in-depth customization and optimization for specific development scenarios or user requirements, resulting in the generated code being inaccurate or inefficient in some scenarios. 3) Existing adaptive learning technologies: Some adaptive learning models can optimize the output of the model to a certain extent through the learning of historical data. However, existing adaptive learning technologies are usually limited to improving the generalization ability of the model, and there is still insufficient personalized adjustment for specific user requirements.

[0036] However, the above code generation methods still have the following problems:

[0037] 1) Lack of context understanding: In related technologies, code generation systems based on pre-trained models often can only generate general code and lack an understanding of the specific context or specific requirements of the project. 2) Lack of personalization: Systems in related technologies usually do not have enough adaptability to the specific requirements of users and cannot generate personalized code according to different development environments, technology stacks, or project requirements. 3) The quality of the generated code cannot be guaranteed: Automated systems in related technologies cannot effectively verify whether the generated code meets quality standards and lack functions for error checking and performance optimization.

[0038] Based on the problems existing in the related technologies, an embodiment of the present application provides a code generation method. First, in response to a code generation request, code requirement recognition is performed on the input prompt information to obtain a structured code requirement recognition result; the code requirement recognition result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement recognition result; then, based on the functional requirement and the first context information, second context information is determined; the second context information includes the complete environment description content of the functional requirement; next, based on the functional requirement and the second context information, candidate code is generated; finally, the candidate code is optimized to obtain target code. In this way, by performing code requirement recognition on the input prompt information to generate a structured requirement recognition result, the code generation system can clarify the functional requirement and the first context information, providing context information for candidate code generation; moreover, based on the functional requirement and the first context information, the environment description is further improved to obtain the second context information. Since the second context information contains a complete environment description, generating candidate code according to the second context information can not only improve the code generation accuracy rate, but also ensure that the generated code can adapt to the actual application scenario and reduce the problem of environment mismatch; in addition, optimizing the candidate code can improve the execution efficiency, readability and maintainability of the code, ensuring that the target code meets the preset coding specifications, thereby improving the code quality.

[0039] The code generation method provided by the embodiment of the present application can be applied to electronic devices such as laptop computers, tablet computers, and desktop computers, and the embodiment of the present application does not impose any restrictions on the specific type of the electronic device.

[0040] The following will describe in detail the code generation method provided by the embodiment of the present application with reference to the accompanying drawings.

[0041] Figure 1 is an optional flowchart of the code generation method provided by the embodiment of the present application. As Figure 1 shown, the method includes the following steps S101 to step S104:

[0042] Step S101, in response to a code generation request, perform code requirement recognition on the input prompt information to obtain a structured code requirement recognition result; the code requirement recognition result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement recognition result.

[0043] Here, the code generation request is a request initiated by the user to request the server or system to perform a code generation operation. A code generation application can be run on the terminal, and the server constitutes the background server of the code generation application. The terminal receives the user's code generation operation, which can be a selection operation or an input operation input through the client of the code generation application running on the terminal. For example, the selection operation can select the prompt information for code generation, or the input operation can be that the user inputs the prompt information for code generation on the client.

[0044] In some embodiments, the code generation application can provide an input interface or an input box to allow the user to select or input the prompt information for code generation. The input interface can be in the form of a form, a text box, a drop-down menu, etc., and the specific form is not limited in this application. The user can select the prompt information for code generation from the predetermined options or manually input the prompt information for code generation. In response to the code generation operation, the terminal encapsulates the prompt information input by the user for code generation to obtain a code generation request and sends the code generation request to the server.

[0045] The prompt information refers to the input provided by the user, which is usually used to describe the specific task, task requirements, and expected results. The prompt information can be used as the basis for the system or model to understand the task, to clarify the task to be completed, to ensure that the system can understand and execute specific functions, and, the prompt information can provide background information or limiting conditions to help the system generate the output that meets the expectations.

[0046] Code requirement identification refers to identifying the actual requirements in the prompt information and understanding the functions that the user wants to implement. Code requirement identification usually relies on natural language processing techniques or machine learning algorithms, and the specific implementation method is not limited in this application.

[0047] The structured code requirement identification result refers to the identification result represented in a specific data structure (such as a table, a dictionary, JSON, XML, etc.) and is used to store the key information extracted from the prompt information. Representing the code requirement identification result in a structured form helps the system to more accurately and quickly understand the user's requirements.

[0048] The functional requirement refers to the specific function or task that the user hopes the system to implement, which is usually the requirement for the system behavior, such as "implement a sorting algorithm", and the specific content of the functional requirement is not limited in this application. The functional requirement is the core content of the code generation request, and the code requirements may include input conditions, execution logic, and output results, etc.

[0049] The first context information refers to the background information or context description corresponding to the functional requirements, which is used to help the system understand the specific conditions and environment of the functional requirements. The context description is used to describe the specific scenarios, constraints, or external conditions of the functional requirements. The context description may include the business background, implementation functions, development frameworks, code environments, and data formats corresponding to the functional requirements. This application does not make any limitations here.

[0050] In some embodiments, in step S101, in response to a code generation request, code requirement recognition is performed on the input prompt information to obtain a structured code requirement recognition result, which can be implemented by the following method: First, in response to the code generation request, preprocessing is performed on the input prompt information to obtain preprocessed information; then, semantic recognition is performed on the preprocessed information to obtain key semantic features; finally, structured processing is performed on the key semantic features to obtain a structured code requirement recognition result.

[0051] Here, preprocessing refers to the preliminary cleaning and processing of the input prompt information. Preprocessing usually includes removing noise (such as removing irrelevant words, spelling mistakes, redundant information, etc.), text standardization (such as case conversion, word form reduction, etc.), analysis, special symbol processing, etc. The specific processing operations are not limited in this application. The preprocessed information refers to the prompt information after the preprocessing operation.

[0052] Semantic recognition refers to extracting key semantic features from the preprocessed information through natural language processing technology. Among them, the key semantic features refer to the features in the preprocessed information that can help the system understand the actual needs of the user. The semantic recognition method can be implemented as a machine learning method (such as hidden Markov model, conditional random field, etc.), and can also be implemented as a deep learning method (such as convolutional neural network, long short-term memory network, etc.). The specific implementation method is not limited in this application.

[0053] Structured processing refers to converting the key semantic features into a unified and standardized data structure. The structuring process refers to converting unstructured text information into a standardized format that the system can understand, such as JSON, XML, or a dictionary, etc. The specific structured processing process is as follows: First, feature extraction is performed on the key semantic features through word embedding; then, the extracted features are added to a preset structured template to obtain a structured code requirement recognition result.

[0054] Step S102, based on the functional requirements and the first context information, determine the second context information; the second context information includes the complete environment description content of the functional requirements.

[0055] Here, the second context information refers to the more detailed and comprehensive context information obtained by further expansion and analysis on the basis of the first context information.

[0056] In some embodiments, in step S102, it can be implemented by the following method: First, determine the similarity between the functional requirement and each historical functional requirement in the preset resource library; then, based on the similarity, determine the target historical functional requirement from the preset resource library; next, extract the third context information of the target historical functional requirement in the preset resource library; the third context information at least includes the historical prompt information of the historical functional requirement; finally, perform information fusion on the third context information and the first context information to obtain the second context information.

[0057] Here, the preset resource library is a database or knowledge base for storing historical functional requirements and related context information. The historical functional requirement refers to the functional requirement proposed by the user before the current moment.

[0058] The similarity is a metric for measuring the matching degree between the current functional requirement and the historical functional requirement. Common similarity calculation methods include cosine similarity, text-based term frequency-inverse document frequency (TF-IDF), deep learning models, etc. The specific similarity calculation method is not limited in this application.

[0059] The target historical functional requirement refers to the historical functional requirement that is closest to the current functional requirement selected from multiple historical functional requirements according to the similarity calculation result. The third context information refers to the background information or context description related to the target historical function. The third context information may include the prompt information input by the user before, code snippets generated according to the historical prompt information, etc.

[0060] Information fusion refers to integrating context information from different sources to form a more comprehensive and accurate background information. Common information fusion methods include direct splicing, weighted fusion, machine learning fusion, etc. The specific information fusion method is not limited here.

[0061] As an example of step S102, assume that the functional requirement is "users redeem goods with points", and the historical functional requirements in the preset resource library include: "users use points to deduct the order amount", "users' points are automatically redeemed for coupons", and "users redeem goods or services with points"; then, by calculating the cosine similarity between the functional requirement and each historical functional requirement, the target historical requirement is determined to be "users redeem goods or services with points". After determining the target historical functional requirement, according to the identifier of the target historical functional requirement, extract the corresponding third context information in the preset resource library; finally, perform information fusion on the third context information and the first context information of the functional requirement to obtain the second context information.

[0062] Step S103: Generate candidate code based on the functional requirements and the second context information.

[0063] Here, the candidate code refers to the initial code automatically generated according to the functional requirements and the second context information, which has not been verified and optimized yet and cannot be directly used.

[0064] In some embodiments, refer to Figure 2 , Figure 2 which is a schematic flowchart of generating candidate code based on the functional requirements and the second context information provided by the embodiments of the present application; Figure 2 It shows that in step S103, generating candidate code based on the functional requirements and the second context information can be implemented through the following steps S1031 to S1033:

[0065] Step S1031: Analyze the functional requirements to obtain multiple functional modules and the core element units of each functional module.

[0066] Here, the functional module refers to an independent functional unit obtained by splitting the functional requirements, and each functional module can independently complete a specific sub-function. For example, under the functional requirement of "user management", it may be split into multiple functional modules such as "user registration", "user login", and "permission management".

[0067] The core element unit refers to the basic component of each functional module, usually including input elements (user access, interface call, etc.), processing logic (data storage, operation rules, etc.), and output elements (interface feedback, operation results, etc.). For example, the core elements of the user login module may include: input: username and password, processing logic: identity verification, output result: login success or failure prompt.

[0068] In some embodiments, the analysis process in step S1031 can be implemented by a machine learning model, such as the k-means clustering algorithm, or by a deep learning model, such as a convolutional neural network or a long short-term memory network, or by a large language model, such as the Transformer model, etc. The specific analysis method is not limited in this application.

[0069] Step S1032: Determine the module code of each functional module based on the second context information and the core element units of each functional module.

[0070] Here, the module code refers to the implementation code of each functional module, which is used to complete the specific function of each functional module.

[0071] In some embodiments, the core element units of each functional module include input information, processing procedures, and output information; step S1032 can be implemented by the following method: First, parse the second context information to obtain the attribute information of each functional module and the code architecture corresponding to each functional module; then, based on the core element units and attribute information of each functional module, determine the input code snippet corresponding to the input information, the process code snippet corresponding to the preprocessing procedure, and the output code snippet corresponding to the output information; finally, add the input code snippet, process code snippet, and output code snippet to the code architecture to obtain the module code of each functional module.

[0072] Here, the attribute information refers to the programming language, functional functions, variables, etc. that need to be used when each functional module is implemented. The code architecture refers to the organization method of the code. Common code architectures include the Model-View-Controller (MVC) architecture, layered architecture, microservices architecture, etc. The specific architecture to be used needs to be determined according to the actual situation, and this application does not make any limitations here.

[0073] The input code snippet corresponding to the input information refers to the code obtained by performing code conversion on the input information in the core element unit according to the programming language and functions determined in the attribute information. The process code snippet corresponding to the processing procedure is the code obtained by performing code conversion on the processing procedure in the core element unit according to the programming language and functions determined in the attribute information. The output code snippet corresponding to the output information refers to the code obtained by performing code conversion on the output information in the core element unit according to the programming language and functions determined in the attribute information.

[0074] As an example of step S1032, first, extract features from the second context information through a semantic recognition model to determine the programming language corresponding to each functional module and the function library to be used; then, analyze the structure and type of the input information through a data analysis tool to clarify the data format and requirements; next, based on the parsing rules and mapping logic of the programming language, map the input information into the corresponding data structure and syntax rules according to the programming language and function library specified in the attribute information to generate an input processing code snippet. The generation processes of the process code snippet and the output code snippet are the same as that of the input code snippet, and this application will not elaborate here. Finally, add the input code snippet to the data acquisition field in the code architecture, add the process code snippet to the core calculation field in the code architecture, and add the output code snippet to the data output field in the code architecture to obtain the module code of each functional module.

[0075] Step S1033: splice the module codes of multiple functional modules according to a preset code logic to obtain candidate codes.

[0076] Here, the preset code logic refers to the pre - established code organization and execution order, which stipulates how each functional module works together to ensure that the program runs efficiently and stably according to the established business process. The preset code logic usually includes the call relationship of the code, the data flow direction, the module dependency relationship, and the error handling mechanism. Common preset code logics include sequential execution, parallel processing, etc. The preset code logic needs to be determined according to the actual situation, and this application does not make any limitations here. Module code splicing refers to the process of integrating the module codes of multiple functional modules according to the preset code logic.

[0077] As an example of step S1033, assume that there are three functional modules, namely module 1, module 2, and module 3. The module code corresponding to module 1 is code 1, the module code corresponding to module 2 is code 2, and the module code corresponding to module 3 is code 3. If the preset code logic is sequential execution, then according to the preset code logic, codes 1, 2, and 3 are integrated to obtain the candidate code, and the candidate code is code 1, code 2, and code 3 connected in sequence.

[0078] In some embodiments, step S103 can also be implemented by the following method: First, determine the requirement type of the functional requirement; then, in response to the requirement type being the target type, determine the rule content of the target type from the preset rule library; the rule content at least includes conditional statements; then, screen the conditional information corresponding to the conditional statements from the second context information; finally, generate the candidate code based on the conditional information according to the rule content of the target type.

[0079] Here, the requirement type of the functional requirement is the classification of the functional requirement. The requirement type is usually divided according to the nature of the function, such as input - output processing requirements, data storage and access requirements, etc. Determining the requirement type of the functional requirement helps the system clarify the specific goal of the requirement. The target type refers to the functional type that actually needs to be implemented among multiple requirement types.

[0080] The preset rule library is a collection containing a series of rule contents. The rule content contains some specific code statements. The rule content is used to generate corresponding code snippets according to the input. The rule content includes simple rules and complex rules. Among them, the simple rule refers to a rule that only includes code statements for simple processing, such as conditional statements; the complex rule refers to a rule that involves multi - step code statements, such as including conditional statements, logical judgment statements, loop statements, etc.

[0081] A conditional statement is a code statement that determines the code execution path through conditional judgment. Common conditional statements include "if-else", "switch-case", etc. Conditional information refers to the information related to conditional judgment in the second context, such as "if the order amount is greater than 100 yuan, a discount will be given, otherwise no discount will be given".

[0082] In some embodiments, according to the rule content of the target type, candidate code can be generated based on the conditional information, which can be achieved by the following methods: First, obtain the keywords in the conditional information; then, determine the target template that matches the keywords from the preset template library; next, generate at least one code statement according to the rule content of the target type; finally, add the code statement to the target template to obtain the candidate code.

[0083] Here, the keywords in the conditional information refer to the core vocabulary extracted from the conditional information. Extracting keywords helps to quickly match the code template. The preset template library stores a series of code snippets, providing reusable code structures for different needs. The target template refers to the code structure that can meet specific needs and is obtained by matching the keywords in the preset template library.

[0084] As an example of step S103, first, analyze the requirement type of the functional requirement to obtain the target type. Assume that the target type is a data storage requirement. According to the target requirement, determine the rule content corresponding to the data storage requirement in the preset rule library. Assume that the conditional statement included in the rule content is an "if-else" statement. Then, screen out the information related to conditional judgment in the second context information, such as: if the threshold of data A is greater than 0.5, then store data A in the database, otherwise do not store. Then, perform semantic analysis on the information related to conditional judgment to determine the keywords in the information related to conditional judgment, such as "threshold", "greater than", "store", etc., and screen according to the keywords in the preset template library to obtain the target template; next, generate code statements according to the conditional statement and the information related to conditional judgment, such as:

[0085] if data_A>0.5:

[0086] store_to_database(data_A)

[0087] else:

[0088] print(“Data A does not reach the storage threshold and will not be stored”)

[0089] Among them, data_A represents data A; store_to_database represents storing in the database; print represents output.

[0090] Finally, after generating the code statements, add the code statements to the conditional judgment field in the target template to obtain the candidate code.

[0091] Step S104: Optimize the candidate code to obtain the target code.

[0092] Here, code optimization refers to adjusting the code structure to improve the readability, maintainability, and execution efficiency of the code.

[0093] In some embodiments, refer to Figure 3 , Figure 3 which is a schematic flowchart of the process of optimizing the candidate code to obtain the target code provided by the embodiments of the present application; Figure 3 It shows that in step S104, optimizing the candidate code to obtain the target code can be achieved through the following steps S1041 to S1044:

[0094] Step S1041: Obtain preset coding parameters; the preset coding parameters at least include code format parameters and variable naming parameters.

[0095] Here, the preset coding parameters refer to a set of pre-set rules or standards for ensuring the consistency, readability, and maintainability of the code. The code format parameters refer to the rules that constrain the structure and layout of the code to ensure the consistency of the code format and avoid errors or maintenance difficulties caused by format chaos, such as indentation style, rules for using spaces and line breaks, etc. The variable naming parameters refer to the rules for standardizing the naming method of variables in the code to ensure clear, standardized, and readable naming with certain maintainability. The variable naming parameters can also avoid naming conflicts and reduce understanding errors caused by naming chaos.

[0096] Step S1042: Based on the preset coding parameters, adjust the candidate code to obtain the optimized code; the optimized code includes multiple sub-codes.

[0097] Here, the optimized code refers to the candidate code after being adjusted according to the preset coding parameters. The optimized code has better readability, maintainability, and scalability. The optimized code consists of multiple sub-codes. A sub-code is a part of the optimized code, usually responsible for a specific function or module. Among them, the sub-code can be a function, a class, a module, or any code unit that can be independently compiled and run. The sub-codes communicate or cooperate through interfaces or protocols.

[0098] In some embodiments, step S1042 can be implemented by the following method: First, force-align the code format of the candidate code according to the code format parameter; then, identify the variable names of the candidate code after format adjustment, and determine the function of each variable in combination with the context information; finally, based on the variable naming parameter, adjust the variable names that do not conform to the specification according to the function of each variable to obtain the optimized code.

[0099] As an example of step S1042, assume that the code format parameters include: statements within a function are indented by four spaces, and a space is added on both sides of each operator; the variable naming parameter includes: capitalize the first letter of the variable; then, adjust the format of the candidate code according to the code format parameter, indent the statements within each function in the candidate code by four spaces, and adjust the operators lacking spaces on both sides, adding spaces at the positions lacking spaces; then, capitalize the first letter of the variable names in the candidate code after format adjustment to obtain the optimized code.

[0100] Step S1043, detect redundant sub-code from the optimized code.

[0101] Here, redundant sub-code refers to code segments that implement the same function and appear multiple times in the code. Redundant code not only increases the complexity of the code, but also introduces errors and reduces the execution efficiency of the code.

[0102] In some embodiments, step S1043 can be implemented by the following method: Scan the optimized code through a code analysis tool to detect duplicate code segments, unused variables, redundant functions or classes, etc. The code analysis tool can include a programming error detector (PMD, Programming Mistake Detector), a style checker (Checkstyle), etc. Different types of programming languages correspond to different code analysis tools, and the specific code analysis tools are not limited in this application.

[0103] Step S1044, delete the redundant sub-code from the optimized code to obtain the target code.

[0104] Here, the target code refers to the final code obtained after deleting the redundant code, and the obtained target code can be directly used.

[0105] It should be noted that the embodiments of this application can not only optimize the generated code, but also optimize the code generation system by collecting user feedback information.

[0106] In some embodiments, the user's feedback information includes: 1) Code correctness feedback: This refers to whether the user has successfully run the generated target code and whether the expected functions have been achieved. For example, errors occur during the execution of the generated target code (such as incorrect output, program crashes, etc.). 2) Function completion feedback: This refers to whether the user believes that the generated target code can meet the functional requirements or if there is a possibility of further modification. The user can feedback the feedback information to the code generation system in the form of prompt messages, such as: "Although the target code can be executed, it does not meet certain boundary conditions." 3) Performance feedback: This refers to whether the execution efficiency or performance of the target code meets the user's requirements, especially in cases of processing large data, complex operations, etc. 4) User behavior data: Click behavior: The user clicks or selects a certain code snippet. Click behavior can help the code generation system understand which code snippets are more in line with the user's needs and which parts may be unclear or irrelevant. Editing and modification: The user manually edits and modifies the target code. For example, if the user modifies certain variable names, execution logic, or comments in the generated target code, these editing and modification operations can be used as reference data for optimizing the code generation system. 5) User interaction data: Multi-round conversation history: This refers to the multi-round interaction content between the code generation system and the user, including how the user describes requirements, gives feedback, and how the model responds, etc. The conversation history data helps the code generation model understand the user's long-term needs and preferences. Evaluation of the generation result: This refers to whether the user is satisfied with the generated code result. For example, the user can give a clear satisfaction score or directly provide improvement suggestions.

[0107] In some embodiments, after receiving the user's feedback information, the code generation system can process it based on the feedback information. The specific processing process is as follows: 1) Classify and archive the collected feedback information. For example, classify the feedback into "function feedback", "performance feedback", "error feedback", etc., and sort the feedback information according to the urgency or importance of the problem. 2) Error analysis and improvement suggestion extraction. For the error information in the feedback information (such as runtime errors or logical errors), the code generation system will extract the key error information, analyze the error type (such as type errors, dimension errors, algorithm errors, etc.), and use these errors as an important basis for improving the code generation system. And generate improvement suggestions according to the user's error feedback. For example, if the user reports an error of dimension mismatch during matrix multiplication, the code generation system will extract this error information and add a dimension matching check mechanism when generating code subsequently. 3) Model fine-tuning. Training data augmentation: Feedback information helps to augment the training data. The user's feedback (especially multi-round interaction feedback) can be used to construct more personalized training samples, which will be used to further fine-tune the code generation system to make the code generation system perform better in specific domains or tasks. 4) User preference modeling. Personalized model adjustment: By analyzing historical interaction data, the code generation system can identify the user's coding habits, preferences, commonly used programming frameworks, etc. For example, if the user always tends to use a specific library (such as NumPy, Pandas, etc.), the code generation system will give priority to these databases when generating code to conform to the user's coding habits. 5) Reinforcement learning and feedback loop. Reinforcement learning mechanism: The code generation system can also use reinforcement learning methods to optimize the output through continuous interaction with the user. Each time the user provides feedback, the code generation system will update the system's reward mechanism according to the satisfaction of the user's feedback to further improve the quality of the target code. For example, if the user modifies a variable name or function signature, the code generation system will learn these modifications and apply similar naming specifications in future code generation. 6) Long-term learning and trend prediction. Learning the user's long-term needs: By analyzing the user's long-term needs and interaction patterns, the code generation system can not only predict the user's future needs and proactively provide suggestions, but also dynamically adjust the complexity and functionality of the generated code according to different situations. Specifically, if the user frequently requests to generate code involving database operations, the code generation system will gradually strengthen its ability in generating database-related code. At the same time, the code generation system will also flexibly adjust the details and complexity of the generated code according to situational factors such as the complexity of the task and the user's habits, ensuring that the generated code meets the user's needs and can run efficiently in a specific situation. The combination of this long-term learning and situational awareness enables the code generation system to gradually adapt to the user's personalized needs, provide more accurate and practical target code, and thus improve the development efficiency and code quality.

[0108] In some embodiments, after generating the target code, the target code can also be verified and tested to generate a test report of the target code. Refer to Figure 4 , Figure 4 which is a schematic flowchart of generating a test report of the target code provided by an embodiment of the present application, Figure 4 showing that generating a test report of the target code can be implemented through the following steps S201 to step S204:

[0109] Step S201, in response to the failure of the target code to run, perform problem analysis on the target code to obtain a problem analysis result; the problem analysis result at least includes a syntax error result and a security vulnerability result of the target code.

[0110] Here, the failure of the target code to run means that an error or exception occurs during the execution of the target code, resulting in the inability to execute as expected or to complete the task normally, such as the target code crashing at startup or returning an error when processing user input.

[0111] Problem analysis refers to a detailed investigation and diagnosis of errors or exceptions in the target code to determine the cause of the failure of the target code to run. Common analysis methods include syntax checking, code quality checking, and vulnerability detection, etc. Among them, syntax checking refers to analyzing whether there are syntax errors in the target code, such as missing parentheses, spelling mistakes, etc.; code quality checking refers to identifying potential problems in the target code, such as dead code, complex conditional statements, or loops, etc.; vulnerability detection refers to analyzing whether there are potential security vulnerabilities in the target code, such as unhandled exceptions, buffer overflows, etc.

[0112] The problem analysis result refers to the detailed information about the code problem obtained after diagnosing and analyzing the target code. The problem analysis result usually includes the following key parts: 1) Problem description: Specifically describe the problem of the failure of the target code to execute, such as function abnormality or output error, etc.; 2) Error type: The type of error that causes the execution failure, such as syntax error, logical error, security vulnerability, etc.; 3) Error occurrence location: The specific location where the error appears in the target code, including file name, function name, and line number, etc., such as "the error appears on line 45 of the main.py file"; 4) Problem cause analysis: Explain in detail the reason for the problem; 5) Repair suggestion: Provide an improvement plan for the problem that appears.

[0113] The syntax error result refers to the part of the target code that does not conform to the programming language syntax rules, usually detected by the compiler or interpreter when compiling or running the target code. The security vulnerability result refers to the potential security hazard existing in the target code.

[0114] As an example of step S201, first, compile or run the target code. When the target code fails to run, the compiler or interpreter will output syntax error information. Alternatively, use a static code analysis tool to automatically scan for potential errors in the target code without running the target code. Then, a security audit tool can be used to scan the target code to obtain potential security vulnerabilities in the target code. Alternatively, simulate the vulnerabilities that an attacker might exploit through preset test cases to determine whether there are relevant security vulnerabilities in the target code. The specific syntax analysis method and security vulnerability detection method are not limited in this application.

[0115] Step S202: In response to inputting preset information into the target code, obtain the output result of the function in the target code, and compare the output result with the preset output result corresponding to the preset information to obtain an output test result.

[0116] Here, the preset information refers to pre-defined input data. The output result of the function refers to the execution result obtained by a certain function in the target code based on the pre-defined input information. The preset output result refers to the pre-defined output result corresponding to the preset information, which is used as the judgment basis for the test.

[0117] In some embodiments, step S202 can be implemented by the following method: First, obtain the preset information and input the preset information into the target code. Then, after receiving the preset information, call the function in the target code that processes the preset information, and process the preset information through this function to obtain the output result. Finally, compare the output result with the preset output result to obtain the output result. If the output result is the same as the preset output result, the output test result can be "test passed". If the output result is different from the preset output result, the output result can be "test failed + reason for failure".

[0118] As an example of step S202, the preset information is: commodity price of 100 yuan, discount rate of 10%, and the preset output result is: 90 yuan. Then, input the preset information into the target code, call the calculation function in the target code to process the preset information, and the obtained output result is 90 yuan. At this time, since the output result is the same as the preset output result, the output test result is: "test passed".

[0119] Step S203: In response to the target code being in a running state, obtain the running data of the target code, and determine the performance analysis result of the target code based on the running data; the running data of the target code at least includes the running time of the target code and the memory consumption when the target code is running.

[0120] Here, the running data refers to various metric data generated during the running of the target code, such as running time, memory consumption, resource consumption (such as CPU consumption, I / O interface usage), etc. The specific running data is not limited in this application. The running data of the target code can be obtained from the logging system.

[0121] The performance analysis result refers to the evaluation result of the code efficiency and resource usage obtained by analyzing the running data. The performance analysis result can also include whether there is a memory leak. The running time refers to the total time consumed from the start of execution to the end of execution of the target code, and can also refer to the time required for each function in the target code to complete execution. The memory consumption refers to the memory resources occupied by the target code during running, usually expressed in bytes, kilobytes or megabytes. For example, the target code occupies 50 megabytes of memory during running.

[0122] Step S204: Generate a test report for the target code based on the problem analysis result, output test result and performance analysis result.

[0123] The test report is used to uniformly display the test conditions and results of the target code in terms of functionality, performance, security, etc. In some embodiments, after obtaining the problem analysis result, output test result and performance analysis result, the problem analysis result, output test result and performance analysis result are integrated to obtain the test report of the target code.

[0124] The code generation method provided by the embodiments of this application can be widely applied to various scenarios. For example, in the scenario of intelligent software development, by using the code generation method provided by this application, the target code that meets the needs of developers can be generated, greatly reducing the workload of developers and improving development efficiency. In the scenario of automated testing, by using the code generation method provided by the embodiments of this application, according to the test requirements input by testers, the test code that meets the test requirements can be generated, which can help testers achieve efficient automated testing and reduce the workload of manually writing test code.

[0125] In the code generation method provided by the embodiments of this application, by identifying the code requirements from the input prompt information, a structured requirement identification result is generated, enabling the code generation system to clarify the functional requirements and the first context information, providing context information for candidate code generation; moreover, based on the functional requirements and the first context information, the environment description is further improved to obtain the second context information. Since the second context information contains a complete environment description, generating candidate code according to the second context information can not only improve the code generation accuracy, but also ensure that the generated code can adapt to the actual application scenario and reduce the problem of environment mismatch; in addition, optimizing the candidate code can improve the execution efficiency, readability and maintainability of the code, and ensure that the target code meets the preset coding specifications, thereby improving the code quality.

[0126] Based on the code generation method described in the above embodiments, Figure 5 FIG. 4 shows a structural block diagram of a code generation device 100 provided by an embodiment of the present application. The code generation device may be a device in an electronic device (e.g., a server). The code generation device may be implemented in a software manner and may be software in the form of a program and a plug-in, etc., including the following software modules: an identification module 101, a determination module 102, a generation module 103, and an optimization module 104. These modules are logical, and thus can be combined arbitrarily or further split according to the functions implemented.

[0127] Among them, the identification module 101 is configured to, in response to a code generation request, perform code requirement identification on the input prompt information to obtain a structured code requirement identification result; the code requirement identification result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement identification result; the extraction module 102 is configured to, in response to a code generation request, perform code requirement identification on the input prompt information to obtain a structured code requirement identification result; the code requirement identification result includes a functional requirement and first context information; the first context information includes the context description content of the functional requirement in the code requirement identification result; the generation module 103 is configured to generate candidate code based on the functional requirement and the second context information; the optimization module 104 is configured to perform code optimization on the candidate code to obtain target code.

[0128] In some embodiments, the determination and identification module is further configured to: in response to a code generation request, perform preprocessing on the input prompt information to obtain preprocessed information; perform semantic recognition on the preprocessed information to obtain key semantic features; perform structured processing on the key semantic features to obtain the structured code requirement identification result.

[0129] In some embodiments, the determination module is further configured to: determine the similarity between the functional requirement and each historical functional requirement in a preset resource library; based on the similarity, determine a target historical functional requirement from the preset resource library; extract third context information of the target historical functional requirement in the preset resource library; the third context information at least includes historical prompt information of the historical functional requirement; perform information fusion on the third context information and the first context information to obtain the second context information.

[0130] In some embodiments, the generating module is further configured to: parse the functional requirements to obtain a plurality of functional modules and the core element units of each functional module; determine the module codes of each functional module based on the second context information and the core element units of each functional module; splice the module codes of the plurality of functional modules according to a preset code logic to obtain the candidate code.

[0131] In some embodiments, the core element units of each functional module include input information, a processing flow, and output information; the generating module is further configured to: parse the second context information to obtain the attribute information of each functional module and the code architecture corresponding to each functional module; determine an input code segment corresponding to the input information, a process code segment corresponding to the processing flow, and an output code segment corresponding to the output information based on the core element units and the attribute information of each functional module; add the input code segment, the process code segment, and the output code segment to the code architecture to obtain the module code of each functional module.

[0132] In some embodiments, the generating module is further configured to: determine the requirement type of the functional requirements; in response to the requirement type being a target type, determine the rule content of the target type from a preset rule library; the rule content at least includes conditional statements; screen the conditional information corresponding to the conditional statements from the second context information; generate the candidate code based on the conditional information according to the rule content of the target type.

[0133] In some embodiments, the generating module is further configured to: obtain the keywords in the conditional information; determine a target template matching the keywords from a preset template library; generate at least one code statement according to the rule content of the target type; add the code statement to the target template to obtain the candidate code.

[0134] In some embodiments, the optimizing module is further configured to: obtain preset coding parameters; the preset coding parameters at least include code format parameters and variable naming parameters; adjust the candidate code based on the preset coding parameters to obtain an optimized code; the optimized code includes a plurality of sub-codes; detect redundant sub-codes from the optimized code; delete the redundant sub-codes from the optimized code to obtain the target code.

[0135] In some embodiments, the device further includes a report generation module, configured to: in response to the failure of the target code to run, perform problem analysis on the target code to obtain a problem analysis result; the problem analysis result at least includes a syntax error result and a security vulnerability result of the target code; in response to inputting preset information to the target code, obtain an output result of a function in the target code, and compare the output result with a preset output result corresponding to the preset information to obtain an output test result; in response to the target code being in a running state, obtain running data of the target code, and determine a performance analysis result of the target code based on the running data; the running data of the target code at least includes the running time of the target code and the memory consumption when the target code is running; generate a test report of the target code based on the problem analysis result, the output test result, and the performance analysis result.

[0136] It should be noted that the description of the device in the embodiments of the present application is similar to the description of the above method embodiments, and has similar beneficial effects as the method embodiments, so details will not be repeated. For the technical details not disclosed in the embodiments of the present device, please refer to the description of the method embodiments of the present application for understanding.

[0137] The embodiments of the present application provide an electronic device, Figure 6 which is a schematic structural diagram of the electronic device provided by the embodiments of the present application. As Figure 6 shown, the electronic device 130 includes: at least one processor 131 ( Figure 6 only one is shown), a memory 132, and computer-executable instructions 133 stored in the memory 132 and executable on at least one processor 131. When the processor 131 executes the executable instructions 133, the steps in any of the above method embodiments for code generation are implemented.

[0138] The electronic device may include, but is not limited to, the processor 131 and the memory 132. Those skilled in the art can understand that Figure 6 this is only an example of the electronic device 130, and does not constitute a limitation on the electronic device 130. It may include more or fewer components than shown in the figure, or combine certain components, or different components. For example, it may also include input / output devices, network access devices, etc.

[0139] The processor 131 may be a central processing unit (CPU). The processor 131 may also be other general-purpose processors, digital signal processors (DSPs), application specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor, etc.

[0140] In some embodiments, the memory 132 may be an internal storage unit of the electronic device 130, such as the hard disk or memory of the electronic device 130. In some other embodiments, the memory 132 may also be an external storage device of the electronic device 130, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the electronic device 130. Further, the memory 132 may also include both the internal storage unit and the external storage device of the electronic device 130. The memory 132 is used to store an operating system, application programs, a boot loader, data, and other programs, such as program codes of computer programs. The memory 132 may also be used to temporarily store data that has been output or is to be output.

[0141] An embodiment of the present application provides a computer-readable storage medium, in which computer-executable instructions are stored. When the computer-executable instructions are executed by a processor, the processor will be caused to execute the code generation method provided by the embodiment of the present application. For example, as Figure 1 the shown code generation method.

[0142] An embodiment of the present application provides a computer program product, which includes computer-executable instructions stored in a computer-readable storage medium. The processor of the electronic device reads the computer-executable instructions from the computer-readable storage medium, and the processor executes the computer-executable instructions, so that the electronic device executes the code generation method described above in the embodiment of the present application.

[0143] In some embodiments, the computer-readable storage medium may be a memory such as RAM, ROM, flash memory, magnetic surface memory, optical disc, or CD-ROM; it may also be various devices including one or any combination of the above memories.

[0144] In some embodiments, the computer-executable instructions may be in the form of a program, software, software module, script, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including being deployed as a stand-alone program or being deployed as a module, component, subroutine, or other unit suitable for use in a computing environment.

[0145] As an example, the computer-executable instructions may or may not correspond to a file in a file system, may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, stored in a single file dedicated to the program being discussed, or, stored in multiple cooperating files (e.g., files that store one or more modules, subroutines, or portions of code).

[0146] As an example, the computer-executable instructions may be deployed to execute on one electronic device, or on multiple electronic devices located at one location, or, on multiple electronic devices distributed across multiple locations and interconnected by a communication network.

[0147] As described above, the above are only embodiments of the present application and are not intended to limit the protection scope of the present application. Any modifications, equivalent replacements, and improvements made within the spirit and scope of the present application are all included within the protection scope of the present application.

Claims

1. A code generation method, characterized in that: The method comprises: In response to the code generation request, code requirement recognition is performed on the input prompt information to obtain a structured code requirement recognition result; the code requirement recognition result includes a functional requirement and first context information; the first context information includes a context description content of the functional requirement in the code requirement recognition result; Determine second context information based on the functional requirement and the first context information; the second context information includes a complete environment description content of the functional requirement; generating candidate codes based on the functional requirement and the second context information; The candidate codes are optimized to obtain target codes.

2. The method according to claim 1, characterized in that In response to the code generation request, the code requirement recognition is performed on the input prompt information to obtain a structured code requirement recognition result, including: In response to the code generation request, preprocessing the input prompt information to obtain preprocessing information; Performing semantic recognition on the preprocessed information to obtain key semantic features; The key semantic features are structured to obtain the structured code requirement recognition result.

3. The method according to claim 1, characterized in that The determining, based on the functional requirement and the first context information, second context information includes: Determining the similarity between the functional requirement and each historical functional requirement in a preset resource library; Based on the similarity, determining a target historical functional requirement from the preset resource library; Extracting third context information of the target historical functional requirement from the preset resource library; the third context information at least includes historical prompt information of the historical functional requirement; The third context information is fused with the first context information to obtain the second context information.

4. The method according to claim 1, characterized in that The generating a candidate code based on the functional requirement and the second context information includes: Analyze the functional requirements to obtain multiple functional modules and core element units of each functional module; Determine a module code of each functional module based on the second context information and a core element unit of each functional module; The module codes of multiple functional modules are spliced ​​according to the preset code logic to obtain the candidate code.

5. The method according to claim 4, characterized in that The core element units of each functional module include input information, processing flow and output information; The determining the module code of each functional module based on the second context information and the core element unit of each functional module includes: Parsing the second context information to obtain attribute information of each functional module and a code architecture corresponding to each functional module; Based on the core element unit of each functional module and the attribute information, determining an input code segment corresponding to the input information, a process code segment corresponding to the processing process, and an output code segment corresponding to the output information; The input code snippet, the process code snippet and the output code snippet are added to the code architecture to obtain the module code of each functional module.

6. The method according to claim 1, characterized in that The generating a candidate code based on the functional requirement and the second context information includes: Determine the requirement type of the functional requirement; In response to the requirement type being a target type, determining a rule content of the target type from a preset rule library; the rule content at least includes a conditional statement; Filtering condition information corresponding to the conditional statement from the second context information; The candidate codes are generated based on the condition information in accordance with the rule content of the target type.

7. The method according to claim 6, characterized in that The generating the candidate code based on the condition information according to the rule content of the target type includes: Obtaining keywords in the condition information; Determine a target template matching the keyword from a preset template library; Generate at least one code statement according to the rule content of the target type; The code statement is added to the target template to obtain the candidate code.

8. The method according to any one of claims 1 to 7, characterized in that: The step of optimizing the candidate code to obtain the target code includes: Obtaining preset encoding parameters; the preset encoding parameters at least include code format parameters and variable naming parameters; Based on the preset encoding parameters, the candidate code is adjusted to obtain an optimized code; the optimized code includes a plurality of sub-codes; Detecting redundant subcodes from the optimized code; The redundant subcode is deleted from the optimized code to obtain the target code.

9. The method according to any one of claims 1 to 7, characterized in that: The method further comprises: In response to the target code failing to run, performing a problem analysis on the target code to obtain a problem analysis result; the problem analysis result at least includes a syntax error result and a security vulnerability result of the target code; In response to inputting preset information into the target code, obtaining an output result of a function in the target code, and comparing the output result with a preset output result corresponding to the preset information to obtain an output test result; In response to the object code being in a running state, acquiring running data of the object code, and determining a performance analysis result of the object code based on the running data; the running data of the object code at least includes a running time of the object code and a memory consumption when the object code is running; A test report of the target code is generated based on the problem analysis result, the output test result and the performance analysis result.

10. A code generating device, characterized in that: include: The recognition module is used to respond to the code generation request, identify the code requirement for the input prompt information, and obtain a structured code requirement recognition result; The code requirement identification result includes a functional requirement and first context information; The first context information includes the context description content of the functional requirement in the code requirement identification result; A determination module, configured to determine second context information based on the functional requirement and the first context information; The second context information includes a complete environment description of the functional requirement; A generating module, configured to generate candidate codes based on the functional requirement and the second context information; The optimization module is used to optimize the candidate code to obtain the target code.

Citation Information

Patent Citations

  • Code generation method capable of meeting customization requirements

    CN116893805A

  • Code construction method, model fine tuning method, equipment and storage medium

    CN117369796A

  • Visual programming method based on large language model, medium, device and equipment

    CN117667054A

  • Code generation method and system based on sql query statement

    CN117873443A

  • Code generation method and device, equipment and storage medium

    CN119225729A

Cited By

  • Code generation method and device based on natural language and medium

    CN120469666A

  • Code generation method and device, equipment and medium

    CN121658503A

  • System and method for multi-layer memory-based dynamic context generation

    KR102892588B1

  • Method and system for building repair recommendation solutions using generative language models and few training examples

    US20250182064A1

  • Application generation in a wireless communication system

    WO2026073591A1