Code generation method and system based on large language model
By combining a large language model and a sample code library, and utilizing multi-turn dialogue and quality assessment, high-quality code is generated, solving the problem of insufficient intelligence in existing technologies and achieving more intelligent and efficient code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- ZHENGZHOU UNIV
- Filing Date
- 2026-02-02
- Publication Date
- 2026-05-19
AI Technical Summary
Existing code generation technologies based on large language models are insufficient in terms of intelligence and code quality. In particular, they are unable to meet the needs of software development when dealing with complex logic and diverse data types, and they lack multi-round interaction and optimization capabilities.
Structured basic code is generated through a large language model, the expansion strategy is adjusted based on user feedback, multi-turn dialogue is optimized by calling the sample code library, and high-quality code is generated based on preset quality evaluation standards, including chained context storage for multi-turn dialogue and semantic vector retrieval from the sample code library.
It enables smarter, higher-quality code generation, improves code accuracy and readability, adapts to the needs of complex logic and diverse data types, reduces labor costs, and increases generation efficiency.
Smart Images

Figure CN122064346A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to a code generation method and system based on a large language model, belonging to the field of computer technology. Background Technology
[0002] As software development projects become increasingly complex, the demand for code generation technology continues to rise. While traditional code generation methods can meet basic data processing needs to a certain extent, their limitations become increasingly apparent in scenarios involving large datasets, high real-time requirements, and complex and variable data types. On one hand, traditional methods rely on professionals manually writing code, which is labor-intensive and error-prone, especially when frequently updating and maintaining the codebase, resulting in high labor costs. On the other hand, manually writing rules is inefficient when handling complex tasks, making it difficult to guarantee code accuracy and affecting the timeliness of data processing. Furthermore, traditional methods require significant computing resources and storage space, leading to high hardware costs and potentially negatively impacting system performance and stability. In addition, their lack of flexibility makes it difficult to adapt to changes in diverse data types and application scenarios.
[0003] With the rapid development of artificial intelligence (AI) technology, automatically generating code using machine learning or AI techniques has become a key approach to improving code generation quality. By learning code structure and function and generating code based on input instructions or requirements, the accuracy and efficiency of code generation can be significantly improved. This approach reduces reliance on manual labor, lowers labor costs, and avoids errors and inconsistencies caused by human factors. AI technology can quickly adapt to new data types and application scenarios, providing flexible code generation solutions to meet the ever-changing needs of software development projects. For example, Chinese patent application CN119556937A discloses a method, device, electronic device, and storage medium for automatic code generation using accelerated thinking. This method first generates basic code for a dialogue model based on a custom code generation template and user-input code parameters. Then, it inputs this basic code into a large language model for processing to obtain suggestions for expanding and improving the basic code. Based on the user's actions in response to these suggestions, it generates first code, optimizes and verifies the first code to obtain second code, thereby improving code generation efficiency. However, this document has weak interactivity, generating code only through "dialog box-style basic code + one-time LLM suggestion". Users can only accept or reject suggestions, lacking the ability to have multiple rounds of interaction and gradually refine requirements. This results in a rigid code generation process, making it difficult to handle complex logic or user changes in requirements midway. Furthermore, this document only optimizes code through LLM and does not introduce external sample code libraries or best practice libraries. As a result, optimization suggestions are limited by the breadth and timeliness of the model itself and cannot dynamically absorb high-quality code patterns from real projects.
[0004] Therefore, existing intelligent code generation technologies still have room for improvement in practical applications. For example, the accuracy and readability of generated code still need further optimization. Furthermore, how to better integrate user input and contextual information to achieve more intelligent code generation and optimization is also an urgent problem to be solved. Especially for Python code generation, existing technologies still fall short in understanding Python's unique syntax structure, dynamic typing characteristics, and its specific needs in fields such as data science and web development. Summary of the Invention
[0005] The purpose of this invention is to provide a code generation method and system based on a large language model, so as to solve the problems of low intelligence and low quality in current code generation based on large language models.
[0006] To address the aforementioned technical problems, this invention provides a code generation method based on a large language model, which includes the following steps: 1) Utilize a large language model to obtain intelligent expansion suggestions for structured basic code, and generate a first version of code based on the user's actions on the intelligent expansion suggestions; 2) Based on the generated first version of the code, it engages in dialogue with the large language model to provide more detailed and specific input, enabling the large language model to generate corresponding subroutines and construct complete code logic; 3) Further optimize the initial code based on multi-turn dialogue by calling the sample code stored in the sample code library, and generate the second version of the code; 4) Perform a comprehensive quality assessment on the generated second version of the code based on the preset code quality assessment criteria, automatically generate repair rules based on the assessment results, and use the repair rules to generate the target code.
[0007] Furthermore, the structured base code in step 1) is generated by the user through a code template defined by a graphical interface.
[0008] Furthermore, the graphical interface includes a component selection area, a canvas area, and a code preview area. The component selection area is used to display class components, method components, and logic components. The canvas area is an operation area for users to build code structures so that users can perform related operations on the components. The code preview area is used to display the corresponding code content generated in real time based on the code structure built on the current canvas area.
[0009] Furthermore, the process of generating the first version of code based on the user's actions regarding the intelligent expansion suggestion in step 1) includes: The user's actions on the intelligent expansion suggestions are sent as feedback vectors to the large language model. The user's actions on the intelligent expansion suggestions include accepting suggestions and rejecting suggestions. When the large language model receives user feedback, it adjusts the weight of the corresponding suggestion type according to the operation type in the feedback vector. If the user accepts a certain type of suggestion, the corresponding weight value increases; if the user rejects it, the weight value decreases.
[0010] Furthermore, step 2), which involves engaging in dialogue with a large language model based on the generated first version of the code to provide more detailed and specific input, includes: When a user initiates a dialogue based on the first version of the code, a corresponding node is created, the user's input text is stored in the node, the large language model processing module is called to generate subroutine code, the tool is used to analyze the existing components that the generated subroutine code depends on, and the identification information of these components is recorded in the dependent component list section of the corresponding node. After each round of dialogue, the state information of the current dialogue round node is updated based on the user's feedback on the generated subroutine code, thereby establishing the dependency relationship between the dialogue round nodes and analyzing the dependency of the subroutine code in the later round of dialogue on the code or components in the previous round of dialogue.
[0011] Furthermore, a chained context storage structure is used to store the history of each round of dialogue. The chained context storage structure includes: the user's original input, the subroutine code generated by the system for the input, the user input encoded into a vector through a semantic model, and the existing components that the generated subroutine code depends on. The input prompts for dynamically constructing a large language model include: the current code snippet, the dialogue history, new user requests, and instructions.
[0012] Furthermore, the process of generating the second version of code based on the initial code in step 3) includes: using the semantic vector of the initial code as the query vector to perform vectorized retrieval in the semantic vector database of the example code library; matching similar cases based on the similarity between the query vector and the semantic vector of each example code fragment in the database; using a large language model to analyze and summarize the example code based on the similar cases; extracting the common patterns and best practices to optimize the initial code and generate the second version of code.
[0013] Furthermore, the similar cases refer to the top N example codes with high similarity, and the similarity is calculated using the cosine similarity matching algorithm.
[0014] Furthermore, the implementation process of step 4) includes: A multi-dimensional code quality assessment standard was adopted to conduct a comprehensive quality assessment of the generated second version of the code. The evaluation results from each dimension are integrated to generate an evaluation report, which includes detailed test data for each dimension, analysis index values, a list of existing problems, and corresponding severity assessments. The code issues identified in the assessment report are matched against a predefined code fix rule library to find the corresponding fix rules; The corresponding repair rules and the second version of the code generated in step 3) are input into the large language model. The large language model optimizes the second version of the code by locally rewriting or globally refactoring in combination with the repair rules, and finally generates the target code.
[0015] The present invention also provides a code generation system based on a large language model, including a processor, the processor being used to execute computer program instructions to implement the above-described code generation method based on a large language model.
[0016] The beneficial effects of this invention are as follows: Based on the generated first-version code, this invention engages in dialogue with a large language model to provide more detailed and specific input, enabling the large language model to generate corresponding subroutines and construct complete code logic. It then calls example code stored in the example code library to further optimize the initial code based on multi-turn dialogue, generating a second-version code. A comprehensive quality assessment of the generated second-version code is performed based on preset code quality evaluation standards. Repair rules are automatically generated based on the evaluation results, and the target code is generated using these repair rules. This invention utilizes the evaluation results of a comprehensive quality assessment of the second-version code to obtain repair rules, and repairs the second-version code accordingly to obtain high-quality target code. Furthermore, the entire generation process is more intelligent. Attached Figure Description
[0017] Figure 1 This is a flowchart of the code generation method based on a large language model according to the present invention; Figure 2 This is a schematic diagram of the structure of the code generation system based on a large language model according to the present invention; Figure 3 This is a schematic diagram of the graphical interface design used in the embodiments of the present invention. Detailed Implementation
[0018] The specific embodiments of the present invention will be further described below with reference to the accompanying drawings.
[0019] Implementation of code generation methods based on large language models First, users define code templates through a graphical interface to generate structured basic code. Second, the generated structured basic code is input into a large language model for processing, yielding intelligent expansion suggestions. Based on the user's actions in response to these suggestions, a first version of code is generated. Then, the user engages in multi-turn dialogue with the large language model based on the first version of code, providing more detailed and specific input to enable the model to generate corresponding subroutines, gradually building complete code logic. Next, the system calls sample code stored in the sample code library to further optimize the initial code based on the multi-turn dialogue, generating a second version of code. Finally, based on preset code quality evaluation standards, a comprehensive quality assessment is performed on the generated second version of code. Based on the assessment results, a repair plan is automatically generated, calling the large language model for partial rewriting or global refactoring, ultimately outputting high-quality target code. The implementation flow of this method is as follows: Figure 1 As shown below, a detailed explanation will follow.
[0020] 1. Generate structured basic code.
[0021] This invention employs component-based code skeleton construction technology. Users build code structures through visual operations, and the system automatically parses the dependencies between components, generating a code framework with a complete semantic structure. It supports user-defined code templates (including classes, methods, and logic components) via a unique drag-and-drop graphical user interface (GUI), and generates structured basic code in real time. Specifically, users can drag required components onto a central canvas to build the code structure, click on components on the canvas to edit component parameters, drag logic components into the method body, connect components with arrows to establish the execution order, and the preview window on the right displays the code generated by the current template in real time. Clicking the save button generates structured basic code. The specific process is as follows: 1) Interface initialization and component display This invention first designs a three-region graphical interface, such as... Figure 3As shown, the left side is the component selection area, the middle is the canvas area, and the right side is the code preview area. The left-side component selection area displays various components in list or icon format, including class components (such as different types of class templates, which can be defined according to different programming language specifications), method components (covering common function types, such as input / output functions, calculation functions, etc.), and logic components (such as conditional statements, loop statements, etc.). The middle canvas area serves as the main operating area for users to build code structures, providing ample space for dragging, placing, and connecting components. The right-side code preview area displays the corresponding code content generated in real time based on the code structure built on the current canvas. Each component has pre-set attribute parameters, such as the class name, attribute variable type and name for class components; the method name, return value type, and parameter list for method components; and the condition format and loop variable for logic components, allowing users to accurately configure these parameters when editing components later.
[0022] 2) Component selection and drag-and-drop operation Users browse various components in the left-hand component selection area using the mouse, and select the desired component type by clicking. The selected component will then be highlighted or otherwise visually emphasized to clearly indicate the user's selection. After selecting a component, the user drags it to a designated location in the central canvas area by pressing the left mouse button and moving the mouse. During dragging, the mouse movement is tracked in real-time, and a semi-transparent component icon is displayed on the canvas that follows the mouse movement to indicate the component's placement. When the user releases the mouse button, the component is placed in the corresponding position on the canvas, and a unique identifier is automatically assigned to it for subsequent operations and management.
[0023] 3) Component parameter editing Users can trigger the component parameter editing function by double-clicking any component on the canvas or right-clicking the component and selecting the "Edit" menu item. A parameter editing dialog box or panel corresponding to that component will pop up, displaying the editable attribute parameters of the component. In the parameter editing dialog box or panel, users can configure the component's various attribute parameters in detail using various appropriate input controls such as input boxes, drop-down lists, and checkboxes. For example, for class components, users can enter the class name, add or modify class attribute variables and their data types; for method components, users can edit the method name, set the return value type, and define the parameter names and types in the parameter list; for logic components, users can set the conditional expressions of conditional statements, the loop variables and their initial values, termination conditions, and other related parameters of loop statements. After the user completes the parameter editing, clicking the "OK" or "Save" button will save the modified parameter information and update the component's display content on the canvas (such as component name, brief description, etc.) according to the new parameter values to reflect the component's latest state.
[0024] 4) Component connection establishment execution order After placing and configuring the various components on the canvas, users can drag and drop logic components (such as conditional logic and loop logic) into the method component body to build the logical structure of the code. At this point, visual cues (such as dashed boxes and highlights) on the canvas clearly indicate the scope of the method component body, guiding users to place the logic components accurately in the appropriate positions within the method.
[0025] To establish the execution order between components, users can drag an arrow from a specific connection point (such as an output) of one component and connect it to a corresponding connection point (such as an input) of another component. During the dragging process, real-time alignment guides and connection hints are provided to help users accurately establish connections between components. Once an arrow successfully connects two components, the execution order relationship between them is automatically recorded, and the corresponding statement order in the generated code is updated in real-time in the code preview area on the right to ensure the correctness and consistency of the code logic.
[0026] 5) Real-time code preview and saving During user actions such as dragging and dropping components, editing parameters, and connecting components, the code generation module listens for these events in real time. Based on the type, parameter settings, and connection relationships of each component on the current canvas, it automatically generates the corresponding structured basic code in the code preview area on the right, following predefined code generation rules and templates. The code preview area displays the code content in a text editor format with syntax highlighting to enhance readability and allow users to easily view and understand the code implementation corresponding to the currently constructed code structure.
[0027] Once the user has completed building the entire code template and confirmed that the code displayed in the preview window on the right is correct, they can click the "Save" button on the interface. The saved code will be the structured basic code.
[0028] 2. Generate the first version of the code.
[0029] The structured base code is input into a large language model for processing. The large language model performs context-aware intelligent expansion of the structured base code, and the user's actions (accepting or rejecting suggestions) are fed back to the large language model in real time. The model's expansion strategy is dynamically adjusted, and the first version of the code is generated based on the user's actions in response to the expansion suggestions. The specific process is as follows: 1) Code Input and Preprocessing After the user clicks the "Generate Suggestions" button in the graphical interface, the generated structured basic code is passed to the large language model processing module via the internal API. The integrated code formatting tool (such as Black) is invoked to format the structured basic code, including code indentation, bracket matching, variable naming conventions, etc., to ensure that the code can be accurately parsed by the large language model.
[0030] 2) Input large language model and initial processing By calling the large language model deployed in the cloud via API, the model analyzes the contextual information of the input structured basic code to understand the semantics and logical structure of the code, including the definition and use of variables, function call relationships, and the nesting level of code blocks.
[0031] Based on its understanding of the code context, the model generates intelligent code expansion suggestions, including adding comments (generating natural language descriptions based on the code context and inserting them into specified locations), code completion (generating specific logic that conforms to parameter constraints for unimplemented method bodies), and optimizing structure (analyzing redundant or inefficient parts of the code and proposing refactoring suggestions). For example, if the model detects a complex logical calculation in the code but lacks corresponding comments, it will generate detailed comment suggestions explaining the function and calculation process of the code segment; if the code contains an incomplete functional module, the model will infer the implementation logic of the function based on the context and generate corresponding code completion suggestions; for structures in the code that can be optimized, such as loops or conditional statements with excessive nesting levels, the model will provide more efficient structural optimization suggestions.
[0032] 3) User feedback and dynamic model adjustment User actions related to model-generated suggestions include accepting or rejecting suggestions. When a user accepts or rejects a suggestion, the user's action is converted into a JSON-formatted feedback vector, which contains the following fields: "operation_type": Used to indicate the type of user operation, such as "accept" or "reject".
[0033] "suggestion_id": A unique identifier that identifies the corresponding suggestion content and is used to accurately identify the specific suggestion addressed in the user feedback.
[0034] "code_position": This indicates the suggested position in the code. It can be described by line number or the start and end positions of a code block, so that the model can accurately locate specific parts of the code for strategy adjustment.
[0035] The model adjusts its expansion strategy based on user feedback vectors. It maintains a suggestion type weight table, recording the weight values for different suggestion types (such as adding comments, completing features, optimizing structures, etc.). Upon receiving user feedback, the model adjusts the weight of the corresponding suggestion type based on the action type in the feedback vector. If the user accepts a suggestion, the corresponding weight increases; if they reject it, the weight decreases. The weight update formula can be expressed as: new_weight = old_weight + learning_rate (1 if operation_type == "accept" else -1) Here, learning_rate is the learning rate parameter, which controls the magnitude of weight updates. It can be adjusted according to the actual situation, for example, set to 0.1.
[0036] If a user frequently accepts a certain type of suggestion, the weight of that type of suggestion is increased in subsequent expansions; conversely, if a user frequently rejects a certain type of suggestion, its weight is decreased. Simultaneously, the model analyzes user preferences and styles based on feedback vectors, such as whether the user prefers a concise coding style or prioritizes code readability, thereby generating code suggestions that better match the user's style during subsequent expansions.
[0037] 4) Suggested output results The suggested code and comments generated by the model are integrated with the location information defined by the user in the graphical interface to generate the first version of optimized code. Then, a pre-defined third-party code formatting tool is invoked to format the integrated code according to the standard formatting specifications of the target programming language. Specifically, the formatting tool automatically adjusts formatting details such as code indentation, bracket matching, keyword alignment, and whitespace usage to ensure the code conforms to the general specifications of the programming language, thereby improving code readability and consistency and facilitating subsequent reading and use by the user. Finally, the formatted code is displayed to the user in the preview area on the right side of the user interface for viewing and further operation.
[0038] 3. Based on the first version of the code, conduct multiple rounds of dialogue to form corresponding subroutines, and gradually build up the complete code logic.
[0039] In the first two steps, users obtain a basic framework and initial code through template definition and intelligent expansion. The code at this stage often only has a skeletal structure and basic functionality, unable to fully cover complex business requirements. Therefore, to gradually refine and improve the code logic, a multi-round dialogue mechanism is needed. Users can gradually raise more specific and detailed requirements, and the large language model, with the support of the context manager, generates corresponding subroutines. In this way, the code gradually evolves from the initial framework into a fully functional and logically clear implementation.
[0040] This invention employs a dialogue context manager to store the complete dialogue history. Based on the generated initial code, the user engages in multiple rounds of dialogue with the model by providing more detailed and specific input, generating corresponding subroutines and gradually building the complete code logic. The specific process is as follows: 1) Definition of linked context storage structure A doubly linked list data structure is used to store the history of multiple rounds of dialogue. Each node contains the following fields: User input text: raw natural language description Subroutine code: generated code snippets Semantic vectors: These are generated by using a deep learning-based semantic understanding model (such as BERT) to process user-input text and obtain its semantic vector representation. Component dependency list: Use general-purpose multilingual tools (such as SonarQube) to perform static analysis on the subroutine code and extract the existing components that it depends on. Node status: Initialized as "Pending," can be later marked as "Confirmed" or "Pending Modification." 2) Initial node creation When a user initiates the first round of dialogue based on the first version of the code, an initial node is created, and the user's input text (such as "implement user login function") is stored in the node. The large language model processing module is called to generate subroutine code. The input text is encoded using a deep learning-based semantic understanding model to generate semantic vectors and store them. Tools are used to analyze the existing components (such as existing class components, method components, etc.) that the generated subroutine code depends on, and the identification information of these components is recorded in the dependency component list section of the initial node.
[0041] 3) Context management and input prompt construction during the dialogue process In each subsequent round of dialogue, when the user provides new detailed input, a new node is first created and added to the linked context storage structure, becoming the tail node of the current linked list.
[0042] The user's current new input text is saved in the user's original input text section of the new node. At the same time, the semantic model is invoked to perform semantic analysis on the new input text, generate its semantic vector representation, and store it in the semantic vector section of the new node.
[0043] To construct input suggestions for a large language model, information is integrated according to the following format: (1) Current code snippet: Displays the overall code after integrating the latest generated subroutine code in the form of a code block.
[0044] (2) Dialogue history: List the original user input text and generated subroutine code for each round of dialogue in chronological order.
[0045] (3) New user requirements: Clearly describe the new requirements raised by users in the current round.
[0046] (4) Related instructions: including specific requirements of users for code functionality, style specifications, etc.
[0047] The constructed input prompt is sent to the large language model processing module. The model returns the generated subroutine code based on the prompt. The newly generated subroutine code is saved in the subroutine code section of the current new node, and its dependent existing components are analyzed to update the list of dependent components of the node.
[0048] 4) Updating and maintaining the dialogue context After each round of dialogue, the state information of the current dialogue round node is updated based on the user's feedback on the newly generated subroutine code. (1) If the user is satisfied with the newly generated subroutine code and confirms to continue to the next step, mark the status of the node as "confirmed".
[0049] (2) If the user makes a suggestion for modification or has new supplementary requirements, mark the status of the node as "to be modified" and roll back the code to the previous confirmed node version, while prompting the user to re-enter or manually edit the code.
[0050] As the dialogue progresses, dependencies between nodes in each round of dialogue are gradually established. By analyzing the dependencies of subroutine code in later rounds of dialogue on code or components in earlier rounds, the dependency information in the chained context storage structure is improved. This helps to more accurately understand the evolution of code logic and the relationships between different parts in subsequent dialogues, providing support for more precise code generation and optimization.
[0051] In each round of dialogue, if the user's input is ambiguous or the code generated by the model does not meet the requirements, the user will be prompted to re-enter the information or provide a more detailed description. For example, if the semantic vector entered by the user has a low similarity to existing semantic vectors, the user will be prompted to re-enter the description of their needs to ensure the validity and accuracy of the input.
[0052] 4. Generate the second version of the code.
[0053] The multi-turn dialogue phase in step 3 only generates "initial complete logic." While the logic is complete, it may have suboptimal implementation methods or lack of best practices. Step 4 requires calling the example code library to further optimize the code generated through multi-turn interactions, refining mature programming patterns and general paradigms. This ensures the code not only runs but also meets high-quality industry standards. Therefore, step 3 provides the completeness of the original logic, while step 4 aims to improve its optimization and professionalism.
[0054] Vectorized retrieval is used to match similar cases from the example code library. A large language model is then used to analyze and summarize these similar cases, extracting common patterns and best practices. This further optimizes the initial code based on multi-turn dialogue, generating a second version of the code. Here, the "initial code" refers to the result generated after the user's multi-turn dialogue with the large language model in step 3. During this process, the user continuously provides more detailed and specific input requirements, and the model generates corresponding subroutines. These subroutines are then gradually pieced together and integrated into the existing code framework. Through multiple rounds of interaction and context management, a logically complete and functionally comprehensive code implementation is finally formed. Step 4, based on this, uses the example code library and semantic retrieval to further adjust the structure of this initial code, transfer patterns, and incorporate best practices to improve its performance, standardization, and readability. The specific process is as follows: 1) Building and preprocessing the sample codebase We collected high-quality sample code snippets from multiple open-source code repositories, code sharing platforms, and real-world projects using web scraping tools. Then, through a combination of automated scripts and manual review, we selected representative sample code that was of high quality (following coding standards, with clear logic), and included detailed comments. Automated scripts were used to calculate code complexity and eliminate low-quality code, while manual review further eliminated code with logical flaws, poor readability, or that did not conform to mainstream development standards.
[0055] 2) Implementation and application of semantic vector encoding engine Building upon the CodeBERT model, we further fine-tuned the model using code snippets and their corresponding detailed comments (as text descriptions) from our collected example code library. The fine-tuning tasks included code-comment matching prediction and code semantic similarity judgment to enhance the model's ability to understand the deep semantics of code and make it more suitable for the semantic vector encoding requirements in this study.
[0056] Semantic vector encoding of code snippets or initial code based on multi-turn dialogues in the example codebase requires standardization. The standardization steps include: (1) Code formatting: Call the integrated code formatting tool to unify code indentation (e.g., unify to 4 spaces), bracket position (e.g., the left bracket in the function definition is immediately after the function name, and the right bracket is aligned with the end of the function body), variable naming style (e.g., unify camelCase naming to underscore naming), and other formatting details to eliminate interference caused by style differences, so that the model can focus on the semantics of the code rather than the format.
[0057] (2) Annotation extraction and integration: The tokenize module in Python is used to extract the annotation content in the code, the ast module is used to parse the code, and a custom class is used to combine the annotation with the code logic structure as supplementary semantic information input into the pre-trained model. For code snippets without annotations, the large language model processing module is called to automatically generate temporary annotations.
[0058] The standardized code snippets and their annotations (if any) are input sequences into the fine-tuned CodeBERT model. The model calculates and outputs a fixed-dimensional (e.g., 512-dimensional or 768-dimensional) numerical vector, which is the semantic vector representation of the code snippet. The generated semantic vectors are associated with the corresponding code snippets and stored in the database of the example code repository to build a code semantic vector database for subsequent fast semantic similarity retrieval.
[0059] 3) Implementation of Vector Retrieval When initial code is received from the multi-round interaction module, the same standardized processing procedure described above is performed to ensure that the code format is consistent with the code style in the example code library and that the syntax is clear. The processed initial code is then input into the semantic vector encoding engine (a fine-tuned CodeBERT model) to generate its semantic vector representation.
[0060] Using the semantic vector of the initial code as the query vector, a vectorized retrieval is performed in the semantic vector database of the example code repository. The cosine similarity matching algorithm is used to calculate the similarity between the query vector and the semantic vector of each example code snippet in the database. The formula for calculating the similarity is: Here, A and B are the query vector and the semantic vector of the example code snippet, respectively. The example code snippets are sorted based on the calculated similarity, and the top N most similar examples are selected as similar cases. Typically, the value of N can be set between 5 and 10 depending on the actual needs.
[0061] 4) Code optimization based on similar cases The retrieved similar cases are fed into the large language model processing module, which performs in-depth analysis and summarization on these cases. It extracts common programming patterns, such as common algorithm implementations, application scenarios of design patterns, and code structure organization, as well as best practices in specific scenarios, including code performance optimization techniques, readability improvement methods, and reliability assurance measures.
[0062] Based on common patterns and best practices extracted from similar cases, and combined with the characteristics of the initial code and user needs, the large language model processing module optimizes the initial code. The optimization process includes: (1) Structural adjustment: Adjust the code structure to better conform to standard design patterns; (2) Algorithm optimization: Optimize the algorithm to improve the execution efficiency of the code; (3) Enhanced comments: Add or modify comments to enhance code readability; (4) Error handling: Add error handling mechanism to improve code reliability.
[0063] Specifically, the optimization of large language models includes: 1. Obtain similar cases and perform pattern abstraction: First, obtain multiple cases most similar to the "initial code" from the example code library through semantic vector retrieval; then submit these similar cases to the large language model, which analyzes and summarizes the cases to extract general programming patterns and best practices (such as common algorithm implementations, design patterns, code organization forms, performance / readability / reliability methods, etc.).
[0064] 2. Migrating Abstract Results to Initial Code: After mastering general patterns and best practices, the large language model, combined with the specific structure and functional requirements of the initial code, performs targeted modifications and improvements to achieve the transition from "runnable" to "engineering-grade high-quality". This specifically includes: Adjust the code structure (to conform to standard design patterns and have clearer module boundaries); Algorithm optimization (improving execution efficiency and resource utilization); Enhance comments (to improve readability and maintainability); Improve error handling (enhance robustness and fault tolerance).
[0065] The input data required for a large language model includes: 1. Initial code body: This refers to the code text generated in step 3 through multiple rounds of dialogue, which has complete business logic but has not yet been optimized; this is the direct object and constraint boundary for optimization.
[0066] 2. Multi-turn dialogue context and user requirements: The current code snippet is based on user input, new user requirements, and related instructions from the history of multi-turn dialogues, to ensure that the context is consistent and the requirements are clear during generation / optimization.
[0067] 3. Similar Case Material Package: Top-N similar code snippets and their annotations obtained from semantic vector retrieval, which are used by the model for pattern abstraction and best practice transfer.
[0068] After optimization, a second version of the code is generated and displayed in the preview area on the right side of the user interface for users to view and operate.
[0069] 5. Conduct a quality assessment and fix of the second version of the code, and generate the target code.
[0070] After multiple rounds of dialogue and example optimization, the code's logic and structure are relatively complete. However, whether it can truly meet engineering quality requirements still requires a systematic quality assessment. Step 5 conducts a comprehensive test across four dimensions: functionality, reliability, performance, and maintainability. Based on the assessment results, a remediation plan is generated, ultimately outputting standard-compliant target code. Therefore, the logical foundation generated in Step 3 is the object of subsequent quality assessment; without the complete logical support of Step 3, the quality assessment cannot proceed.
[0071] A multi-dimensional code quality assessment standard is used to conduct a comprehensive quality assessment of the generated second version of the code. Based on the assessment results, a repair plan is automatically generated. The specific process is as follows: 1) Refinement and implementation of multi-dimensional code quality assessment standards (1) Functional dimension assessment: Using Python's natural language processing libraries (such as NLTK or spaCy), the functional description text provided by users during multi-turn interactions is segmented. A BiLSTM-CRF (Bidirectional Long Short-Term Memory Network-Conditional Random Field) model is invoked to segment the words in the text and identify key semantic units. Next, a Transformer-based dependency parsing model is used to predict dependency relations in the segmented functional description text. The model predicts the dependency relation type and corresponding parent node word for each word. Based on these dependency relations, a syntax tree is gradually constructed to parse the specific functional requirements.
[0072] Based on these functional requirements, corresponding test cases are automatically generated using Python test case generation tools (such as pytest) to ensure coverage of input / output scenarios, boundary conditions, and exception handling for each functional module. The generated test cases are then applied to the second version of the code, and functional tests are executed using an automated testing framework (such as pytest). Test results are collected, including the pass / fail status, failure reason, and exception error message for each test case. The overall pass rate of the functional tests is calculated; if it falls below a preset threshold (such as 90%), a significant functional defect is identified, requiring further fixing.
[0073] (2) Reliability dimension assessment: Using Python fault injection libraries (such as Chaos Toolkit), various possible hardware failures (e.g., insufficient memory, disk I / O errors), software anomalies (e.g., network interruptions, database connection failures), and data anomalies (e.g., illegal input data, incorrect data format) are simulated in the code runtime environment. The code's behavior under these abnormal scenarios is observed, and its ability to correctly capture exceptions, perform reasonable error handling, and ensure stability is recorded.
[0074] By running code for extended periods and continuously monitoring its operational status using Python monitoring tools (such as psutil), reliability metrics such as mean time between failures (MTBF) and fault recovery time can be calculated.
[0075] (3) Performance dimension evaluation: A comprehensive performance evaluation of the Python code was conducted using multiple performance analysis tools. First, the built-in Python module `cProfile` was used for performance analysis. `cProfile` can accurately measure key performance indicators such as execution time and function call count, and generate detailed performance analysis data. This data can be parsed and displayed using the `pstats` module to help identify performance bottlenecks in the code.
[0076] Secondly, the line_profiler tool is used for line-by-line performance analysis. Unlike the function-level performance data provided by cProfile, line_profiler can provide the execution time of each line of code, allowing developers to more accurately pinpoint performance issues. This tool is attached to the target function as a decorator and collects the runtime data of each line of code during function execution.
[0077] The `memory_profiler` tool is used to monitor the memory usage of code. It can track memory allocation and deallocation in real time, playing a crucial role in detecting memory leaks and other issues. This tool is also applied to the target function using decorators.
[0078] To present performance analysis results more intuitively, performance visualization tools have also been integrated. For example, SnakeViz is a web-based tool that can convert cProfile performance analysis data into interactive flame graphs, allowing developers to view the performance distribution of their code at a glance.
[0079] In addition, the Py-Spy sampling analyzer can perform non-intrusive performance analysis on Python programs, generating flame graphs of the call stack to help developers understand the code's performance from a macro perspective.
[0080] By continuously running code and monitoring its performance under different loads and scenarios, a wealth of performance data can be collected. Using the performance metric comparison and analysis capabilities provided by these tools, the performance of the current code can be compared with baseline performance data or industry standards, thereby identifying key areas for performance optimization.
[0081] (4) Maintainability dimension assessment: Use Python static code analysis tools (such as flake8 and pylint) to perform static scanning on the code, check whether the code follows coding standards (such as variable naming conventions, indentation rules, commenting conventions, etc.), whether there are code smells (such as excessively long functions, too many parameters, complex conditional statements, etc.), and analyze maintainability-related indicators such as cyclomatic complexity, class complexity, and file complexity. Define scoring rules: for each code style violation or code smell, assign corresponding deduction points; for code structure and comments that conform to standards or are excellent, assign bonus points. For example: Incorrect variable naming: Deduct 2 points Class complexity is too high: deduct 4 points Clear code structure: +5 points Based on the scan results from the static code analysis tool, the maintainability of the code is quantitatively scored according to predefined scoring rules. If the score is lower than the set maintainability standard threshold (e.g., below 70 points), the code needs to be fixed.
[0082] 2) Automatic generation of remediation plans based on assessment results The evaluation results across four dimensions—functionality, reliability, performance, and maintainability—are integrated, including detailed test data, analytical metrics, a list of existing issues, and corresponding severity assessments for each dimension. The code evaluation report is structured to include basic code information (such as code version and generation time), an evaluation overview (briefly explaining the purpose and scope of the evaluation), detailed evaluation information for each dimension (presenting the evaluation methods, results, and problem analysis for each dimension in detail, divided into chapters), and a comprehensive evaluation conclusion (providing a summary evaluation of the overall code quality). Following the designed report structure, populate the collected evaluation results data into the corresponding positions. Use a Python report generation library (such as the Jinja2 template engine) to format the report content, presenting the evaluation results in an intuitive and easy-to-read manner.
[0083] We build a predefined code fix rule library, collecting common Python code issues from open-source communities and technical forums, including functional bugs, performance bottlenecks, and poor maintainability. For each issue, we define corresponding fix rules. The fix rules include the following parts: Issue Identifier: Uniquely identifies a code issue, facilitating quick searching within the rule base. Problem Description: Describe in detail the nature of the problem and its potential impact. Repair steps: Provide specific repair steps or code examples to guide you on how to resolve the problem. Related examples: Provide code examples containing the issues and code examples after the fixes to enhance understandability. For example, to address the array out-of-bounds error in a functional bug, the following repair rule is defined: Problem identifier: ARRAY_OUT_OF_BOUNDS Problem Description: When accessing an array element, if the index is outside the array's valid range, it may cause the program to crash or exhibit undefined behavior. Repair steps: Add bounds checking code for array indices before accessing array elements. If an index is out of range, throw an exception or return a default value. Related examples: # Problem Code Example def get_element(arr, index): return arr[index] # Example of the fixed code: def get_element(arr, index): if 0 <= index <len(arr): return arr[index] else: raise IndexError("Array index out of range") The defined remediation rules are stored in a rule base. The rule base can be a database, a configuration file, or a dedicated rule engine. Each remediation rule should have a unique identifier to facilitate quick matching and retrieval during the evaluation process.
[0084] The code issues identified in the assessment report are matched against a predefined code fix rule library. For each issue, the corresponding fix rule in the rule library is searched.
[0085] 3) Target code output The found repair rules and the original second-version code are input into the large language model processing module. The module, based on the repair suggestions, optimizes the second-version code through partial rewriting or global refactoring, ultimately generating the target code. The target code then undergoes a comprehensive quality check (including evaluation across the four dimensions mentioned above) to ensure all issues have been properly resolved and the code quality meets the expected standards. This entire process may require multiple repairs, with each repair checking whether the expected standards have been met, until the standards are achieved.
[0086] Therefore, the innovation of this invention compared with the prior art lies in: This approach utilizes a unique drag-and-drop graphical user interface (GUI) to support user-defined code templates (including classes, methods, and logic components) and generates structured basic code in real time. Unlike existing technologies, this method employs component-based code skeleton construction technology. Users build code structures through visual operations, and the system automatically parses the dependencies between components, generating a code framework with a complete semantic structure. This significantly lowers the barrier to entry and enhances intuitiveness and flexibility.
[0087] After generating the structured base code, a large language model is used to intelligently expand the code in a context-aware manner, providing dynamic suggestions such as adding comments, completing functions, and optimizing the structure. The system feeds back user actions (accepting or rejecting suggestions) to the model in real time, forming an incremental learning loop. User action data is encoded into feedback vectors, used to dynamically adjust the model's expansion strategy, making subsequent suggestions more aligned with the user's coding style and project requirements. Compared to the single expansion method in existing technologies, this invention is more intelligent and personalized.
[0088] For multi-turn dialogue scenarios, this invention proposes a chained context storage structure and employs multimodal coding technology to store user input, generated subroutine code, semantic vectors, and component dependencies in a time-series manner, ensuring complete backtracking and accurate positioning of the dialogue history. By introducing a chained context storage structure and semantic vector management, this invention guarantees the integrity of code logic evolution and dependencies during multi-turn interactions. This capability allows complex functions to be built through step-by-step interactions, significantly improving the system's ability to support complex requirements.
[0089] This invention standardizes and performs deep semantic analysis on the example codebase using a pre-trained model, mapping code snippets to a high-dimensional vector space. A cosine similarity matching algorithm is then used to retrieve the case most semantically similar to the current code. A large language model processing module is invoked to perform pattern abstraction and transfer optimization on the retrieval results, extracting general programming paradigms and domain best practices, and dynamically adapting them to the current code logic. This invention utilizes semantic vector retrieval to match example code and further optimizes the initial code by abstracting general patterns and best practices through a large language model. This method overcomes the limitations of existing technologies that rely solely on syntax and performance optimization, achieving experience transfer and advanced optimization.
[0090] This invention establishes a four-dimensional evaluation standard covering functionality, reliability, performance, and maintainability. It performs comprehensive quality testing on the generated code and generates a quality assessment report. Based on the report, it automatically generates a repair plan and calls the large language model processing module to perform partial rewriting or global refactoring of the code. Compared to existing technologies, this invention, through this complete quality assurance mechanism, directly improves the reliability and practical value of the generated code.
[0091] Implementation of a code generation system based on a large language model The present invention also provides a code generation system based on a large language model, including a processor, which is used to execute computer program instructions to implement the above-mentioned code generation method based on a large language model. The specific implementation process has been described in detail in the implementation of the code generation method based on a large language model, and will not be repeated here.
[0092] This code generation system based on a large language model can also employ... Figure 2 The system is structured as follows: a user-defined template module, a large language model processing module, a multi-turn interaction module, a sample code library and code optimization module, and a quality assessment and feedback module. The user-defined template module generates structured basic code based on user needs, allowing users to define code templates through a graphical interface, which the system then uses to generate the structured basic code. The large language model processing module expands upon the structured basic code, inputting it into the large language model to obtain expansion suggestions, which users can then use to generate the first version of the code. The multi-turn interaction module gradually refines the code generation requirements through multiple rounds of dialogue, with the output of each round serving as the input for the next, progressively building complete code logic. The sample code library and code optimization module further optimizes the initial code based on multi-turn interaction using sample code, referencing high-quality code in the sample code library and modifying it according to specifications to generate the second version of the code. The quality assessment and feedback module performs a comprehensive quality assessment of the generated second version of the code, outputting the assessment results and providing optimization suggestions, ultimately optimizing the second version of the code to generate the target code.
Claims
1. A code generation method based on a large language model, characterized in that, The method includes the following steps: 1) Utilize a large language model to obtain intelligent expansion suggestions for structured basic code, and generate a first version of code based on the user's actions on the intelligent expansion suggestions; 2) Based on the generated first version of the code, it engages in dialogue with the large language model to provide more detailed and specific input, enabling the large language model to generate corresponding subroutines and construct complete code logic; 3) Further optimize the initial code based on multi-turn dialogue by calling the sample code stored in the sample code library, and generate the second version of the code; 4) Perform a comprehensive quality assessment on the generated second version of the code based on the preset code quality assessment criteria, automatically generate repair rules based on the assessment results, and use the repair rules to generate the target code.
2. The code generation method based on a large language model according to claim 1, characterized in that, The structured base code in step 1) is generated by the user through a code template defined by the graphical interface.
3. The code generation method based on a large language model according to claim 2, characterized in that, The graphical interface includes a component selection area, a canvas area, and a code preview area. The component selection area is used to display class components, method components, and logic components. The canvas area is an operation area for users to build code structures so that users can perform related operations on the components. The code preview area is used to display the corresponding code content generated in real time based on the code structure built on the current canvas area.
4. The code generation method based on a large language model according to claim 1, characterized in that, Step 1) involves generating the first version of code based on the user's actions regarding the intelligent expansion suggestion, including: The user's actions on the intelligent expansion suggestions are sent as feedback vectors to the large language model. The user's actions on the intelligent expansion suggestions include accepting suggestions and rejecting suggestions. When the large language model receives user feedback, it adjusts the weight of the corresponding suggestion type according to the operation type in the feedback vector. If the user accepts a certain type of suggestion, the corresponding weight value increases; if the user rejects it, the weight value decreases.
5. The code generation method based on a large language model according to claim 1, characterized in that, Step 2), which involves engaging in dialogue with the large language model based on the generated first version of the code to provide more detailed and specific input, includes: When a user initiates a dialogue based on the first version of the code, a corresponding node is created, the user's input text is stored in the node, the large language model processing module is called to generate subroutine code, the tool is used to analyze the existing components that the generated subroutine code depends on, and the identification information of these components is recorded in the dependent component list section of the corresponding node. After each round of dialogue, the state information of the current dialogue round node is updated based on the user's feedback on the generated subroutine code, thereby establishing the dependency relationship between the dialogue round nodes and analyzing the dependency of the subroutine code in the later round of dialogue on the code or components in the previous round of dialogue.
6. The code generation method based on a large language model according to claim 1, characterized in that, A chained context storage structure is used to store the history of each round of dialogue. The chained context storage structure includes: the user's original input, the subroutine code generated by the system for the input, the user input encoded into a vector through a semantic model, and the existing components that the generated subroutine code depends on. The input prompts for dynamically constructing a large language model include: the current code snippet, the dialogue history, new user requests, and instructions.
7. The code generation method based on a large language model according to claim 1, characterized in that, The process of generating the second version of code from the initial code based on multi-turn dialogue in step 3) includes: using the semantic vector of the initial code as the query vector to perform vectorized retrieval in the semantic vector database of the example code library; matching similar cases based on the similarity between the query vector and the semantic vector of each example code fragment in the database; using a large language model to analyze and summarize the example code based on the similar cases; extracting the common patterns and best practices to optimize the initial code and generate the second version of code.
8. The code generation method based on a large language model according to claim 7, characterized in that, The similar cases mentioned refer to the top N example codes with the highest similarity, and the similarity is calculated using the cosine similarity matching algorithm.
9. The code generation method based on a large language model according to claim 1, characterized in that, The implementation process of step 4) includes: A multi-dimensional code quality assessment standard was adopted to conduct a comprehensive quality assessment of the generated second version of the code. The evaluation results from each dimension are integrated to generate an evaluation report, which includes detailed test data for each dimension, analysis index values, a list of existing problems, and corresponding severity assessments. The code issues identified in the assessment report are matched against a predefined code fix rule library to find the corresponding fix rules; The corresponding repair rules and the second version of the code generated in step 3) are input into the large language model. The large language model optimizes the second version of the code by locally rewriting or globally refactoring in combination with the repair rules, and finally generates the target code.
10. A code generation system based on a large language model, comprising a processor, characterized in that, The processor is used to execute computer program instructions to implement the code generation method based on a large language model as described in any one of claims 1-9.