Code generation method and apparatus

By identifying and optimizing code requirements for input information and generating structured requirement identification results, the problem of lack of context understanding in code generation systems in existing technologies is solved, and the accuracy and quality of code generation are improved.

CN120045171BActive Publication Date: 2025-10-17BEIJING QIMIAO KINGDOM TECHNOLOGY CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

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

AI Technical Summary

Technical Problem

Existing code generation systems lack understanding of the specific context or specific requirements of the project and are unable to generate personalized code. In addition, the accuracy of the generated code is low and the code quality cannot be effectively verified.

Method used

By identifying code requirements based on the input prompt information, generating structured requirement identification results, determining the second context information, generating candidate codes and optimizing them, we ensure that the code is adapted to the actual application scenario, improve accuracy, and improve quality by optimizing the code format and content.

Benefits of technology

The accuracy of code generation is improved, ensuring that the generated code is adapted to actual application scenarios, enhancing the execution efficiency, readability and maintainability of the code, and complying with preset coding standards.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120045171B_ABST
    Figure CN120045171B_ABST
Patent Text Reader

Abstract

The application provides a code generation method and device; comprising: in response to a code generation request, performing code requirement identification on input prompt information to obtain a structured code requirement identification result; the code requirement identification result comprises a functional requirement and first context information; the first context information comprises context description content of the functional requirement in the code requirement identification result; determining second context information based on the functional requirement and the first context information; the second context information comprises complete environment description content of the functional requirement; generating a candidate code based on the functional requirement and the second context information; performing code optimization on the candidate code to obtain a target code. The application generates the target code based on the second context information, which comprises the complete environment description content of the functional 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] The present application relates to the technical field of artificial intelligence, and in particular to a code generation method and device. BACKGROUND

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

[0003] In the related art, the code automatic generation method mainly depends on a rule-based code generation system, that is, the 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 low. SUMMARY

[0004] The embodiments of the present application provide a code generation method and device. The present application generates target code based on second context information, so that the code generation system can generate code according to complete context information, thereby improving the code generation accuracy.

[0005] The technical scheme of the embodiments of the present application is as follows:

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

[0007] The embodiments of the present application provide a code generation device, which comprises: an identification module configured to, in response to a code generation request, perform code requirement identification on input prompt information to obtain a structured code requirement identification result; the code requirement identification result comprises a functional requirement and first context information; the first context information comprises context description content of the functional requirement in the code requirement identification result; an extraction module configured to determine second context information based on the functional requirement and the first context information; the second context information comprises 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; and an optimization module configured to perform code optimization on the candidate code to obtain target code.

[0008] In the above scheme, the determining identification module is further configured to: in response to a code generation request, pre-process the input prompt information to obtain pre-processed information; perform semantic recognition on the pre-processed information to obtain key semantic features; and perform structured processing on the key semantic features to obtain the structured code requirement identification result.

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

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

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

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

[0013] In the scheme, the generating module is further configured to: acquire a keyword 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; and add the code statement into the target template to obtain the candidate code.

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

[0015] In the scheme, the device further includes a report generating module configured to: in response to a failure of the target code, 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 input of preset information to the target code, acquire 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, acquire 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 a running time of the target code and memory consumption when the target code runs; 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 electronic device is provided in an embodiment of the present application, and includes a memory configured to store computer executable instructions, and a processor configured to execute the computer executable instructions stored in the memory to implement a code generation method provided in an embodiment of the present application.

[0017] A computer readable storage medium is provided in an embodiment of the present application, and stores a computer program or executable instructions, and the computer program or executable instructions are executed by a processor to implement a code generation method provided in an embodiment of the present application.

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

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

[0020] The code generation method in the embodiment of the present application generates a structured requirement identification result by performing code requirement identification on the input prompt information, so that the code generation system can clarify the functional requirements and the first context information, and provide 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 accuracy of code generation, but also ensure that the generated code can adapt to the actual application scenario and reduce the environment mismatch problem; in addition, optimizing the candidate code can improve the execution efficiency, readability and maintainability of the code, ensure that the target code complies with the preset coding specifications, and thus improve the code quality. BRIEF DESCRIPTION OF THE DRAWINGS

[0021] Figure 1 This is an optional flowchart of the code generation method provided in the embodiment of the present application;

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

[0023] Figure 3 This is a flow chart of optimizing candidate codes to obtain target codes, as provided in an embodiment of the present application;

[0024] Figure 4 This is a flow chart of generating a test report for a target code according to an embodiment of the present application;

[0025] Figure 5 This is a structural block diagram of a code generation device provided in an embodiment of the present application;

[0026] Figure 6 It is a structural diagram of an electronic device provided in an embodiment of the present application. DETAILED DESCRIPTION

[0027] In order to make the purpose, technical solutions and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limiting this application. All other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.

[0028] In the following description, reference is made to “some embodiments”, which describes a subset of all possible embodiments, but it will be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.

[0029] If the application file contains similar descriptions such as "first / second", the following description is added: in the following description, the terms "first, second, third" referred to only distinguish similar objects, not represent a specific order for the object, and it can be understood that "first, second, third" can be exchanged in a specific order or sequence as allowed, so that the embodiments 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 or a part of a computer program with a predetermined function, and works together with other related parts to achieve a predetermined target, and can be implemented entirely or partially by using software, hardware (such as processing circuitry or memory) or a combination thereof. Similarly, one processor (or multiple processors or memory) can be used to implement one or more modules or units. In addition, each module or unit can be a part of an overall module or unit that includes the functions 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 meanings as those commonly understood by a person skilled in the art. 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 the code generation method provided by the embodiments of the present application is described, the professional terms involved in the embodiments of the present application are first 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 input information provided by developers.

[0034] In order to better understand the code generation method provided by the embodiments of the present application, the code generation method in the related art will be described first.

[0035] In related technologies, code generation methods mainly include the following methods: 1) Rule-based code generation system: Rule-based code generation systems usually rely on pre-defined rules and templates to generate code, and can handle some simple requirements, but their flexibility and scalability are poor, and they often cannot cope with complex or dynamic requirements. 2) Deep learning-based generation model: In recent years, generative pre-training models have made significant breakthroughs in natural language processing. Generative pre-training models can learn language patterns from large-scale data and generate relatively fluent code according to input. However, generative pre-training models often lack deep customization and optimization for specific development scenarios or user needs, resulting in generated code that is not accurate or efficient in some scenarios. 3) Existing adaptive learning technology: Some adaptive learning models can optimize model output to some extent through the learning of historical data. However, existing adaptive learning technologies are usually limited to improving the generalization ability of the model, and still lack personalized adjustment for user-specific needs.

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

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

[0038] Based on the problems in the related art, an embodiment of the present application provides a code generation method. First, in response to a code generation request, code requirement identification is performed on 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 context description content of the functional requirement in the code requirement identification result. Then, based on the functional requirement and the first context information, second context information is determined. The second context information includes complete environment description content of the functional requirement. Next, based on the functional requirement and the second context information, candidate code is generated. Finally, code optimization is performed on the candidate code to obtain target code. In this way, by performing code requirement identification on the input prompt information, a structured requirement identification result is generated, so that the code generation system can clearly understand the functional requirement and the first context information, and provide 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 includes complete environment description, the candidate code is generated based on the second context information, which 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 environment mismatch problem. In addition, the candidate code is optimized, which can improve the execution efficiency, readability and maintainability of the code, ensure that the target code meets the preset coding specification, and thus improve the code quality.

[0039] The code generation method provided by the embodiments of the present application can be applied to electronic devices such as notebook computers, tablet computers, desktop computers, etc. The embodiments of the present application do not make any limitation on the specific type of electronic device.

[0040] The code generation method provided by the embodiments of the present application will be described in detail below with reference to the accompanying drawings.

[0041] Figure 1 is an optional flowchart of the code generation method provided by the embodiments of the present application, as shown in Figure 1 the method includes the following steps S101 to S104:

[0042] Step S101, in response to a code generation request, code requirement identification is performed on 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 context description content of the functional requirement in the code requirement identification result.

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

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

[0045] Prompt information refers to the input provided by the user, which is usually used to describe the specific task, task requirements and expected results. Prompt information can serve as the basis for the system or model to understand the task, clarify the tasks to be completed, and ensure that the system can understand and perform specific functions. In addition, prompt information can provide background information or constraints to help the system generate expected output.

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

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

[0048] Functional requirements are specific functions or tasks that users want the system to implement. They are typically requirements for system behavior, such as "implement a sorting algorithm." This application does not limit the specific content of functional requirements. Functional requirements are the core content of code generation requests and may include input conditions, execution logic, and output results.

[0049] The first context information refers to background information or context description corresponding to the functional requirement, and is used to help the system understand the specific conditions and environment of the functional requirement. The context description is used to describe the specific scene, constraint condition or external condition of the functional requirement, and the context description can include business background, implementation function, development framework, code environment and data format corresponding to the functional requirement. The present application does not limit this.

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

[0051] Here, preprocessing refers to preliminary cleaning and processing of the input prompt information. Preprocessing usually includes removing noise (such as removing irrelevant words, spelling errors, 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 herein. 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, wherein the key semantic features refer to features in the preprocessed information that can help the system understand the actual requirements of the user. The semantic recognition method can be implemented as a machine learning method (such as a hidden Markov model, a conditional random field, etc.), and can also be implemented as a deep learning method (such as a convolutional neural network, a long short-term memory network, etc.). The specific implementation method is not limited herein.

[0053] Structured processing refers to converting the key semantic features into a unified and standardized data structure. The structured process refers to converting unstructured text information into a standardized format that the system can understand, such as JSON, XML or 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 pre-set structured template to obtain the structured code requirement identification result.

[0054] In step S102, based on the functional requirement and the first context information, the second context information is determined; the second context information includes complete environment description content of the functional requirement.

[0055] Here, the second context information refers to more detailed and comprehensive context information obtained by further expanding and analyzing the first context information.

[0056] In some embodiments, step S102 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; then, extract the 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; finally, fuse 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. Historical functional requirements refer to functional requirements proposed by users before the current moment.

[0058] Similarity is a metric used to measure the degree of match between current functional requirements and historical functional requirements. 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 based on similarity calculation results. The third context information refers to background information or context description related to the target historical functional requirement. The third context information may include prompts previously entered by the user, code snippets generated based on historical prompts, and so on.

[0060] Information fusion involves integrating contextual information from different sources to create more comprehensive and accurate background information. Common information fusion methods include direct concatenation, weighted fusion, and machine learning fusion. The specific information fusion methods themselves are not limited here.

[0061] As an example of step S102, assuming that the functional requirement is "user points redeem goods", the historical functional requirements in the preset resource library include: "user uses points to deduct order amount", "user points automatically redeem coupons" and "user points redeem goods or services"; then, by calculating the cosine similarity between the functional requirement and each historical functional requirement, the target historical requirement is determined to be "user points redeem goods or services". After determining the target historical functional requirement, according to the identifier of the target historical functional requirement, the third context information corresponding to the target historical functional requirement is extracted from the preset resource library; finally, the third context information is fused with the first context information of the functional requirement to obtain the second context information.

[0062] Step S103, generating a candidate code based on the functional requirement and the second context information.

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

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

[0065] Step S1031, the functional requirement is parsed to obtain a plurality of functional modules and the core element unit of each functional module.

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

[0067] The core element unit refers to the basic component of each functional module, which usually includes 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 parsing process in step S1031 can be implemented through a machine learning model, such as a k-means clustering algorithm, and can also be implemented through a deep learning model, such as a convolutional neural network, a long short-term memory network, and a large language model, such as a transformer model, etc. The specific parsing method is not limited herein.

[0069] Step S1032, based on the second context information and the core element unit of each functional module, determining the module code 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 unit of each function module includes input information, processing flow and output information; step S1032 can be implemented by the following method: first, the second context information is parsed to obtain the attribute information of each function module and the code architecture corresponding to each function module; then, based on the core element unit and the attribute information of each function module, the input code segment corresponding to the input information, the flow code segment corresponding to the preprocessing flow and the output code segment corresponding to the output information are determined; finally, the input code segment, the flow code segment and the output code segment are added to the code architecture to obtain the module code of each function module.

[0072] Here, the attribute information refers to the programming language, function or variable, etc. that each function module needs to use when implemented. The code architecture refers to the organization method of the code, and common code architectures include Model-View-Controller (MVC, Model-View-Controller) architecture, layered architecture and micro-service architecture, etc. The specific architecture used needs to be determined according to the actual situation, which is not limited herein.

[0073] The input code segment corresponding to the input information refers to the code obtained by code conversion of the input information in the core element unit according to the programming language and function, etc. determined in the attribute information. The flow code segment corresponding to the processing flow is the code obtained by code conversion of the processing flow in the core element unit according to the programming language and function, etc. determined in the attribute information. The output code segment corresponding to the output information refers to the code obtained by code conversion of the output information in the core element unit according to the programming language and function, etc. determined in the attribute information.

[0074] As an example of step S1032, first, the second context information is feature extracted by a semantic recognition model to determine the programming language corresponding to each function module and the function library that needs to be used; then, the structure and type of the input information are analyzed by a data analysis tool to clarify the data format and requirements; then, based on the parsing rules and mapping logic of the programming language, the input information is mapped to the corresponding data structure and syntax rules according to the programming language and function library specified in the attribute information, to generate the input processing code segment. The generation process of the flow code segment and the output code segment is the same as that of the input code segment, which will not be repeated herein. Finally, the input code segment is added to the data acquisition field in the code architecture, the flow code segment is added to the core calculation field in the code architecture, and the output code segment is added to the data output field in the code architecture, to obtain the module code of each function module.

[0075] Step S1033, the module codes of the plurality of function modules are spliced according to a preset code logic to obtain a candidate code.

[0076] Here, the preset code logic refers to the pre-prepared code organization and execution order, which specifies 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 calling relationship of the code, the data flow, the module dependency relationship, and the error handling mechanism. Common preset code logic includes sequential execution, parallel processing, etc. The preset code logic needs to be determined according to the actual situation, which is not limited in this application. 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. The preset code logic is sequential execution. According to the preset code logic, code 1, code 2, and code 3 are integrated to obtain the candidate code, which 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 a conditional statement; next, filter the condition information corresponding to the conditional statement from the second context information; finally, generate the candidate code based on the condition information according to the rule content of the target type.

[0079] Here, the requirement type of the functional requirement is a 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 to clearly understand the specific goal of the requirement. The target type refers to the actual functional type to be implemented among multiple requirement types.

[0080] The preset rule library is a collection of a series of rule contents. The rule content contains some specific code statements, and the rule content is used to generate the corresponding code fragment according to the input. The rule content includes simple rules and complex rules, wherein the simple rule refers to a rule that only includes simple processing code statements, 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] Conditional statements are code statements that determine the execution path of code through conditional judgments. Common conditional statements include "if-else" and "switch-case." Conditional information refers to information related to conditional judgments in a second context, such as "If the order amount is greater than 100 yuan, then apply a discount; otherwise, do not apply a discount."

[0082] In some embodiments, generating candidate codes based on conditional information according to the rule content of the target type can be achieved by the following method: first, obtaining keywords in the conditional information; then, determining a target template that matches the keywords from a preset template library; then, generating at least one code statement according to the rule content of the target type; finally, adding the code statement to the target template to obtain the candidate code.

[0083] Here, keywords in the conditional information refer to core terms extracted from the conditional information. Extracting keywords facilitates quick matching of code templates. The preset template library stores a series of code snippets, providing reusable code structures for different requirements. The target template is a code structure that meets specific requirements, obtained by matching keywords in the preset template library.

[0084] As an example of step S103, first, the requirement type of the functional requirement is analyzed to obtain the target type. Assuming that the target type is a data storage requirement, the rule content corresponding to the data storage requirement is determined in the preset rule library based on the target requirement. Assuming that the conditional statement contained in the rule content is an "if-else" statement, information related to the conditional judgment is filtered out from the second context information, such as: if the threshold of data A is greater than 0.5, then data A is stored in the database; otherwise, it is not stored. Then, a semantic analysis is performed on the information related to the conditional judgment to determine the keywords in the information related to the conditional judgment, such as "threshold", "greater than", "storage", etc., and the preset template library is filtered based on the keywords to obtain the target template; then, a code statement is generated based on the conditional statement and the information related to the 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 is not stored")

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

[0090] Finally, after generating the code statement, the code statement is added to the condition judgment field in the target template to obtain the candidate code.

[0091] In step S104, the candidate code is optimized 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, referring to Figure 3 , Figure 3 is a flowchart of the code optimization of the candidate code to obtain the target code provided by the embodiments of the present application; Figure 3 It is shown that in step S104, the candidate code is optimized to obtain the target code, which can be implemented by the following steps S1041 to S1044:

[0094] In step S1041, preset coding parameters are obtained; 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 rules or standards that are set in advance, which are used to ensure the consistency, readability and maintainability of the code. The code format parameter refers to the rules for constraining the structure and layout of the code, which ensures the consistency of the code in format and avoids errors or maintenance difficulties caused by format chaos, such as indentation style, space and line break usage rules, etc. The variable naming parameter refers to the rules for standardizing the naming method of variables in the code, which ensures clear, standardized and readable and maintainable naming, and the variable naming parameter can also avoid naming conflicts and reduce understanding errors caused by naming chaos.

[0096] In step S1042, the candidate code is adjusted based on the preset coding parameters to obtain the optimized code; the optimized code includes multiple sub-codes.

[0097] Here, the optimized code refers to the candidate code after adjustment according to the preset coding parameters, and the optimized code has better readability, maintainability and scalability. The optimized code is composed of multiple sub-codes. The sub-code is a part of the optimized code, which is 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, and the sub-codes communicate or cooperate with each other through interfaces or protocols.

[0098] In some embodiments, step S1042 can be implemented by the following method: first, forcibly align the code format of the candidate code according to the code format parameter; then, identify the variable name 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 name that does not conform to the specification according to the function of each variable, to obtain the optimized code.

[0099] As an example of step S1042, it is assumed that the code format parameter includes: four spaces are used for indentation within a function, and spaces are added on both sides of each operator; and the variable naming parameter includes: the first letter of the variable is capitalized. Then, the candidate code is adjusted in format according to the code format parameter, that is, the statements within each function in the candidate code are indented with four spaces, and the operators that lack spaces on both sides are adjusted, and spaces are added at the positions where the spaces are lacking. Next, the first letter of the variable name in the candidate code after format adjustment is adjusted to be in capital, to obtain the optimized code.

[0100] Step S1043 detects redundant sub-codes from the optimized code.

[0101] Here, the redundant sub-code refers to a code segment that implements the same function and appears 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: scanning the optimized code by a code analysis tool to detect repeated code segments, unused variables, redundant functions or classes, etc. The code analysis tool can include a programming mistake detector (PMD, Programming Mistake Detector), a style checking tool (Checkstyle), etc. Different types of programming languages correspond to different code analysis tools, and the specific code analysis tool is not limited in the present application.

[0103] Step S1044 deletes 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 the present application can not only optimize the generated code, but also optimize the code generation system by collecting user feedback information.

[0106] In some embodiments, user feedback includes: 1) Code correctness feedback: This refers to whether the user successfully runs the generated target code and whether it achieves the expected functionality. For example, errors may occur during the execution of the generated target code (such as incorrect output or program crashes). 2) Functional completeness feedback: This refers to whether the user believes the generated target code meets the functional requirements or whether further modification is possible. Users can provide feedback to the code generation system in the form of prompts, such as: "Although the target code can be executed, it does not meet certain boundary conditions." 3) Performance feedback: This refers to whether the efficiency or performance of the target code execution meets user requirements, especially when processing big data or complex calculations. 4) User behavior data: Click behavior: Users click or select specific code snippets. Click behavior can help the code generation system understand which code snippets are more in line with user requirements and which parts may contain unclear or irrelevant content. Editing and modification: Users manually edit and modify the target code. For example, if a user changes certain variable names, execution logic, or comments in the generated target code, these edits and modifications can serve as reference data for optimizing the code generation system. 5) User Interaction Data: Multi-turn conversation history: This refers to the multi-turn interactions between the code generation system and the user, including how the user describes their needs, provides feedback, and how the model responds. This conversation history data helps the code generation model understand the user's long-term needs and preferences. Evaluation of the generated results: This refers to whether the user is satisfied with the generated code. For example, users can provide explicit satisfaction ratings or directly provide suggestions for improvement.

[0107] In some embodiments, after receiving the feedback information of the user, the code generation system can process based on the feedback information, the specific processing process is as follows: 1) classify and archive the collected feedback information. For example: divide the feedback into "function feedback", "performance feedback", "error feedback" and so on, 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 (such as runtime error or logic error) in the feedback information, the code generation system will extract the key error information, analyze the error type (such as type error, dimension error, algorithm error, 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 reflects that there is a dimension mismatch error when multiplying matrices, the code generation system will extract this error information, and add a dimension matching check mechanism when generating code in the future. 3) Model fine-tuning (Fine-tuning). Training data enhancement: feedback information helps to enhance training data, user feedback (especially multi-round interaction feedback) can be used to build more personalized training samples, which will be used to further fine-tune the code generation system, so that the code generation system performs better in specific fields 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 certain database (such as NumPy, Pandas, etc.), the code generation system will preferentially consider these databases when generating code to meet 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 user's feedback satisfaction, further improving 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 conventions in future code generation. 6) Long-term learning and trend prediction. Learn the user's long-term needs: the code generation system analyzes the user's long-term needs and interaction patterns, not only to predict the user's future needs and actively provide suggestions, but also to dynamically adjust the complexity and functionality of the generated code according to different situations. Specifically, if the user frequently requests to generate code related to database operations, the code generation system will gradually strengthen its ability to generate 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 the complexity of the task and the user's habits and other situational factors, ensuring that the generated code meets the user's needs and runs efficiently in specific situations. The combination of long-term learning and situational awareness enables the code generation system to gradually adapt to the user's individualized needs, providing more accurate and practical target code, thereby improving 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. Referring to Figure 4 , Figure 4 is a flowchart of generating a test report of the target code provided by the embodiments of the present application, Figure 4 shows that the test report of the target code can be generated by the following steps S201 to S204:

[0109] Step S201, in response to the failure of the target code running, performing 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 running refers to the error or exception in the execution process of the target code, which leads to the failure to perform as expected or the failure to complete the task normally, such as the crash of the target code at startup or the return of an error when processing user input.

[0111] Problem analysis refers to detailed investigation and diagnosis of errors or exceptions in the target code to determine the cause of the failure of the target code running. Common analysis methods include syntax checking, code quality checking and vulnerability detection, etc., wherein syntax checking refers to analyzing whether there is a syntax error in the target code, such as missing parentheses, spelling errors, 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 is a potential security vulnerability in the target code, such as unhandled exceptions, buffer overflow, etc.

[0112] The problem analysis result refers to the detailed information about the code problem obtained after the problem diagnosis and analysis of the target code. The problem analysis result usually includes the following key parts: 1) problem description: specifically explaining the problem of the target code execution failure, such as functional exception or output error, etc.; 2) error type: the error type leading to the execution failure, such as syntax error, logic error, security vulnerability, etc.; 3) error occurrence position: the specific position of the error in the target code, including file name, function name and line number, etc., such as "error occurs at line 45 of main.py"; 4) problem cause analysis: detailed explanation of the cause of the problem; 5) repair suggestion: improvement scheme for the problem.

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

[0114] As an example of step S201, first, the target code is compiled or run, and the compiler or interpreter outputs syntax error information when the target code fails to run, or a static code analysis tool is used to automatically scan the target code for potential errors without running the target code. Then, a security audit tool can be used to scan the target code for potential security vulnerabilities, or a test case can be used to simulate an attacker exploiting a vulnerability to determine whether the target code has related security vulnerabilities. The specific syntax analysis method and security vulnerability detection method are not limited herein.

[0115] Step S202, in response to inputting the preset information into the target code, obtaining the output result of the function in the target code, and comparing the output result with the preset output result corresponding to the preset information, obtaining the output test result.

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

[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, process the preset information through the 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 + failure reason".

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

[0119] Step S203, in response to the target code being in a running state, obtaining the running data of the target code, and determining 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 runs.

[0120] Here, the running data refers to various index data generated in the running process of the target code, such as running time, memory consumption, resource consumption (such as CPU consumption, I / O interface usage), and the like. The specific running data is not limited herein. The running data of the target code can be obtained in a log system.

[0121] The performance analysis result refers to an evaluation result about 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 the execution of each function in the target code to be completed. The memory consumption refers to the memory resources occupied by the target code during running, which is usually expressed in bytes, kilobytes or megabytes, such as 50 megabytes of memory occupied by the target code during running.

[0122] In step S204, a test report of the target code is generated based on the problem analysis result, the output test result and the performance analysis result.

[0123] The test report is used to uniformly show the test situation and result of the target code in terms of function, performance, security and the like. In some embodiments, after obtaining the problem analysis result, the output test result and the performance analysis result, the problem analysis result, the output test result and the performance analysis result are integrated to obtain the test report of the target code.

[0124] The code generation method provided by the embodiments of the present application can be widely applied to various scenes. For example, in the intelligent software development scene, the code generation method provided by the present application is used to generate the target code meeting the requirements of the developers, thereby greatly reducing the workload of the developers and improving the development efficiency. In the automatic testing scene, the code generation method provided by the embodiments of the present application is used to generate the test code meeting the test requirements according to the test requirements input by the testers, thereby helping the testers to realize efficient automatic testing and reducing the workload of manually writing the test code.

[0125] The code generation method in the embodiments of the present application performs code requirement recognition on the input prompt information to generate a structured requirement recognition result, so that the code generation system can clearly understand the functional requirements and the first context information, and provide the 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 complete environment description, the candidate code is generated according to the second context information, which not only improves the code generation accuracy, but also ensures that the generated code can adapt to the actual application scene and reduce the environment mismatch problem. In addition, the candidate code is optimized, which can improve the execution efficiency, readability and maintainability of the code, ensure that the target code meets the preset coding specification, and thus improve the code quality.

[0126] The code generation method based on the above-mentioned embodiments, Figure 5 A structural block diagram of a code generation apparatus 100 provided by an embodiment of the present application is shown. The code generation apparatus can be an apparatus in an electronic device (for example, a server). The code generation apparatus can be implemented in a software manner, and can be software in the form of a program and a plug-in, and the like. The code generation apparatus includes 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 or further split according to the implemented functions.

[0127] The identification module 101 is configured to perform code requirement identification on input prompt information in response to a code generation request, 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 context description content of the functional requirement in the code requirement identification result. The extraction module 102 is configured to perform code requirement identification on input prompt information in response to a code generation request, 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 context description content of the functional requirement in the code requirement identification result. The generation module 103 is configured to generate a 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 a target code.

[0128] In some embodiments, the determination identification module is further configured to: perform preprocessing on input prompt information in response to a code generation request, to obtain preprocessed information; perform semantic identification on the preprocessed information, to obtain key semantic features; and 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 a similarity between the functional requirement and each historical functional requirement in a preset resource library; determine a target historical functional requirement from the preset resource library based on the similarity; extract 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; and 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 requirement to obtain a plurality of functional modules and a core element unit of each functional module; determine a module code of each functional module based on the second context information and the core element unit of each functional module; and 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 unit of each functional module includes input information, a processing flow and output information; the generating module is further configured to: parse the second context information to obtain attribute information of each functional module and a code architecture corresponding to each functional module; determine an input code segment corresponding to the input information, a flow code segment corresponding to the processing flow and an output code segment corresponding to the output information based on the core element unit of each functional module and the attribute information; and add the input code segment, the flow code segment and the output code segment to the code architecture to obtain a module code of each functional module.

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

[0133] In some embodiments, the generating module is further configured to: obtain a keyword 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; and add the code statement to the target template to obtain the candidate code.

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

[0135] In some embodiments, the apparatus further comprises a report generation module configured to: in response to the target code failing to run, perform problem analysis on the target code to obtain a problem analysis result, wherein the problem analysis result comprises at least a syntax error result and a security vulnerability result of the target code; in response to inputting preset information into 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, wherein the running data of the target code comprises at least running time of the target code and memory consumption when the target code runs; and 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 apparatus embodiments of the present application is similar to the description of the method embodiments described above, and has similar beneficial effects as the method embodiments, and thus will not be described again. For technical details not disclosed in the apparatus embodiments, please refer to the description of the method embodiments for understanding.

[0137] The present application provides an electronic device, Figure 6 is a structural schematic diagram of an electronic device provided by the present application. As Figure 6 shown, the electronic device 130 comprises at least one processor 131 (only one is shown in the figure), a memory 132, and computer executable instructions 133 stored in the memory 132 and executable on the at least one processor 131, and the processor 131 executes the executable instructions 133 to implement the steps in any of the code generation method embodiments described above. Figure 6

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

[0139] ​The processor 131 can be a central processing unit (CPU), and can also be other general-purpose processors, a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic device, discrete gate or transistor logic, discrete hardware components, etc. The general-purpose processor can be a microprocessor or the processor can also be any conventional processor.

[0140] The memory 132 can be an internal storage unit of the electronic device 130, such as a hard disk or a memory of the electronic device 130 in some embodiments. The memory 132 can 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 in other embodiments. Further, the memory 132 can 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, an application program, a boot loader, data, and other programs, such as program codes of computer programs, etc. The memory 132 can also be used to temporarily store data that has been output or will be output.

[0141] The embodiment of the present application provides a computer readable storage medium, wherein computer executable instructions are stored. When the computer executable instructions are executed by a processor, the processor executes the code generation method provided by the embodiment of the present application, for example, the code generation method shown in the embodiment of the present application. Figure 1 The embodiment of the present application provides a computer readable storage medium, wherein computer executable instructions are stored. When the computer executable instructions are executed by a processor, the processor executes the code generation method provided by the embodiment of the present application, for example, the code generation method shown in the embodiment of the present application.

[0142] The 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 provided by the embodiment of the present application.

[0143] In some embodiments, the computer readable storage medium can be a RAM, a ROM, a flash memory, a magnetic surface memory, an optical disc, or a CD-ROM, etc. The computer readable storage medium can also be various devices including one or any combination of the above storage devices.

[0144] In some embodiments, the computer-executable instructions can be in the form of programs, procedures, modules, scripts, or code, and can be written in any programming language, including compiled or interpreted languages, or declarative or procedural languages; and can be deployed in any form, including as a stand-alone program or as a module, component, subroutine, or other unit suitable for use in a computing environment.

[0145] By way of example, computer-executable instructions can be, but are not limited to, programs, procedures, modules, scripts, or code, which can be stored in files in a file system, can be stored in a portion of a file that holds other programs or data, can be stored as one or more scripts stored in a markup language such as Hyper Text Markup Language (HTML), can be stored as a single file dedicated to the program in question, or can be stored in multiple files in different formats, including file servers, databases, arrays, or anywhere that can be used to hold instructions and their necessary data.

[0146] By way of example, the computer-executable instructions can be deployed to be executed on one electronic device, or on multiple electronic devices that are located at one site, or that are distributed across multiple sites and that are interconnected by a communication network.

[0147] The above description is provided as an example only and is not for the purpose of limiting the scope of the application. Any modification, equivalent replacement and improvement made within the spirit and the scope of the application shall fall within the scope of the protection of the 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 of the functional requirement in the code requirement recognition result; Determining the similarity between the functional requirement and each historical functional requirement in a preset resource library; determining a target historical functional requirement from the preset resource library based on the similarity; extracting third context information of the target historical functional requirement from the preset resource library; fusing the third context information with the first context information to obtain second context information; the second context information including a complete description of the environment of the functional requirement; Generating candidate codes based on the functional requirement and the second context information includes: Parsing the functional requirements to obtain multiple functional modules and core element units of each functional module; parsing the second context information to obtain attribute information and a corresponding code architecture of each functional module; based on the core element units and the attribute information, determining code snippets corresponding to input information, processing flow, and output information, respectively, and adding them to the corresponding code architecture; splicing the module codes of the multiple functional modules according to a preset code logic to obtain candidate codes; The generating of candidate codes based on the functional requirement and the second context information further includes: Determining rule content of a target type from a preset rule library based on the requirement type of the functional requirement; the rule content includes at least a conditional statement; filtering condition information corresponding to the conditional statement from the second context information; obtaining keywords from the condition information, and determining a target template matching the keywords from a preset template library; generating at least one code statement according to the rule content, and adding the code statement to the target template to obtain candidate code; Obtaining preset encoding parameters, including at least a code format parameter and a variable naming parameter; adjusting the candidate code based on the preset encoding parameters to obtain an optimized code including a plurality of subcodes; detecting and deleting redundant subcodes in the optimized code to obtain a target code; When the target code fails to run, the target code is analyzed to obtain a problem analysis result; the output result of the function in the target code is compared with the preset output result corresponding to the preset information to obtain an output test result; the running time and memory consumption running data are obtained to form a performance analysis result of the target code; A test report of the target code is generated based on the problem analysis result, the output test result and the performance analysis result.

2. The method according to claim 1, characterized in that In response to the code generation request, the code requirement identification is performed on the input prompt information to obtain a structured code requirement identification 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 core element units of each functional module include input information, processing flow and output information; The code snippets corresponding to the input information, processing flow, and output information are determined based on the core element unit and the attribute information, and added to the corresponding code architecture, including: Based on the core element unit and the attribute information of each functional module, 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.

4. The method according to claim 1, wherein When the target code fails to run, the target code is analyzed to obtain a problem analysis result; the output result of the function in the target code is compared with the preset output result corresponding to the preset information to obtain an output test result; Obtaining runtime data such as runtime and memory consumption and forming performance analysis results of the target code, including: In response to the target code failing to run, performing 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 target code being in a running state, the running data of the target code is obtained, and the performance analysis result of the target code is determined based on the running data; the running data of the target code includes at least the running time of the target code and the memory consumption when the target code is running.

5. A code generating device, characterized in that: include: The recognition module is used to respond to the code generation request, identify the code requirements of 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 a context description of the functional requirement in the code requirement identification result; a determination module, configured to determine a 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; fusing the third context information with the first context information to obtain second context information; the second context information including a complete environmental description of the functional requirement; A generation module is configured to parse the functional requirements to obtain a plurality of functional modules and a core element unit of each functional module; and parse the second context information to obtain attribute information of each functional module and a corresponding code architecture; Based on the core element unit and the attribute information, code snippets corresponding to the input information, processing flow, and output information are determined respectively, and added to the corresponding code architecture, and the module codes of the multiple functional modules are spliced ​​according to the preset code logic to obtain candidate codes; or, Determining the rule content of the target type from a preset rule library based on the requirement type of the functional requirement; the rule content includes at least a conditional statement; and filtering condition information corresponding to the conditional statement from the second context information; Obtaining keywords in the condition information, and determining a target template matching the keywords from a preset template library; Generate at least one code statement according to the rule content, and add it to the target template to obtain candidate code; An optimization module is configured to obtain preset encoding parameters, including at least a code format parameter and a variable naming parameter; and adjust the candidate code based on the preset encoding parameters to obtain an optimized code including a plurality of sub-codes; Detecting and deleting redundant subcodes in the optimized code to obtain a target code; A reporting module, configured to analyze the problem of the target code and obtain a problem analysis result when the target code fails to run; Compare the output result of the function in the target code with the preset output result corresponding to the preset information to obtain the output test result; Obtaining runtime and memory consumption data and forming performance analysis results of the target code; A test report of the target code is generated based on the problem analysis result, the output test result and the performance analysis result.

6. An electronic device, characterized in that: The electronic device comprises: a memory for storing computer-executable instructions; A processor, configured to implement the method according to any one of claims 1 to 4 when executing the computer-executable instructions stored in the memory.

7. A computer-readable storage medium, characterized in that Executable instructions are stored, and when executed by a processor, they are used to implement the method described in any one of claims 1 to 4.

8. A computer program product comprising computer executable instructions or a computer program, characterized in that When the computer executable instructions or computer program are executed by a processor, the method according to any one of claims 1 to 4 is implemented.

Citation Information

Patent Citations

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

    CN117667054A

  • Code generation method and system based on sql query statement

    CN117873443A