Context sensing code generation and real-time optimization method and system based on large language model

By building code context models and large language models fine-tuning, the problems of missing context perception and insufficient real-time optimization in code generation and optimization are solved, and the high compatibility and maintainability of code generation and project architecture are achieved, improving code quality and performance.

CN120523448APending Publication Date: 2025-08-22SHANDONG LANGCHAO YUNTOU INFORMATION TECH CO LTD

Patent Information

Application Number
CN202510593199.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-05-09
Publication Date
2025-08-22

AI Technical Summary

Technical Problem

The existing technology lacks context-awareness in code generation and optimization, poor compatibility with the project architecture, inconsistent generation code style, and lacks real-time optimization capabilities, resulting in a lot of manual adjustments and lag in optimization suggestions.

Method used

By building a code context model, combining static analysis and dynamic execution tracking, using a large language model for fine-tuning, building a context map covering syntax, semantics and runtime behavior, and real-time optimization is carried out to form a generation-optimization-verification closed loop.

Benefits of technology

It improves the compatibility and maintainability of generated code and project architecture, reduces manual adjustment and rework costs, ensures dual guarantees of code quality and performance, shortens development cycles and alleviates the pressure of shortage of technical talents.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120523448A_ABST
    Figure CN120523448A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of artificial intelligence and software development, in particular to a context-aware code generation and real-time optimization method and system based on a large language model.The method comprises the following steps that a compiler front-end technology is utilized, input codes are analyzed, and an abstract syntax tree is constructed; function definition, variable declaration and statement block structure information are extracted through an abstract syntax tree, so that syntax and semantic structures of codes are obtained, and basic data support is provided for subsequent code generation and optimization; the method has the beneficial effects that the context graph covering grammar, semantics and runtime behaviors is constructed by fusing static grammar analysis (AST construction) and dynamic execution tracking (such as variable value change and function call time sequence), and the problem of context perception deficiency in a traditional code generation technology is solved. And the compatibility, the maintainability and the expandability of the generated code and the project architecture are improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence and software development technology, and specifically to a context-aware code generation and real-time optimization method and system based on a large language model. Background Art

[0002] The current software development field faces many challenges in code generation and optimization. For example:

[0003] 1. Template-based code generation methods (such as patent CN202010000X, "A Template-Based Automatic Code Generation System") lack context-awareness and simply assemble code snippets using predefined templates. This results in poor compatibility with complex project architectures and requires extensive manual adjustments. For example, in scenarios with complex function call relationships, template methods cannot dynamically adapt to variable dependencies or logical branches, leading to runtime errors.

[0004] 2. While applications of general-purpose large-scale language models (such as the paper "CodeBERT: A Pre-Trained Model for Programming and Natural Language") can generate basic code, their training data is disconnected from specific project specifications, resulting in inconsistent code styles and a lack of closed-loop verification of execution performance. For example, the code generated by CodeBERT may ignore project-defined naming conventions or memory management policies, requiring developers to manually correct logical vulnerabilities.

[0005] 3. Static code optimization tools (such as patent CN201980000Y "A code performance optimization method based on static analysis") rely on offline analysis and cannot capture runtime performance bottlenecks (such as memory leaks and cyclic redundancy) in real time. Optimization suggestions lag behind actual demand changes, resulting in a decrease in system response speed.

[0006] None of the aforementioned technologies address the three core issues of a lack of contextual awareness, a disconnect between models and engineering practices, and insufficient real-time optimization capabilities. Therefore, a technical solution integrating multi-dimensional contextual modeling, dynamic model fine-tuning, and closed-loop real-time optimization is urgently needed to improve the accuracy and standardization of code generation and enhance system performance. Summary of the Invention

[0007] The purpose of the present invention is to provide a context-aware code generation and real-time optimization method and system based on a large language model to solve the problems raised in the above background technology.

[0008] To achieve the above objectives, the present invention provides the following technical solutions: a context-aware code generation and real-time optimization method based on a large language model, comprising the following steps: utilizing compiler front-end technology to parse input code and construct an abstract syntax tree; extracting function definitions, variable declarations, and statement block structure information through the abstract syntax tree to obtain the grammatical and semantic structure of the code, providing basic data support for subsequent code generation and optimization.

[0009] Preferably, the method further includes the following steps: embedding monitoring code in the code running environment; collecting dynamic information on function call sequence, variable value changes, and loop execution times during code execution; using dynamic information as part of the code context for subsequent code generation and optimization, such as recording function call parameters and return values, and loop variable changes.

[0010] Preferably, the method includes the following steps: integrating the code syntax and semantic structure data obtained by static analysis with the code execution information data obtained by dynamic analysis; constructing a code context model based on the integrated data, and using a graph database or a custom data structure to store function call graphs and variable dependency graph information; the code context model provides rich contextual basis for subsequent fine-tuning of the large language model, such as establishing calling relationships and variable dependencies between functions.

[0011] Preferably, the method comprises the following steps:

[0012] Dataset construction: Based on the collected code context information and corresponding high-quality code examples, a fine-tuning dataset is constructed;

[0013] Model selection and adaptation: Select a pre-trained large language model suitable for code generation tasks and adjust the model based on the model architecture and fine-tuning methods, such as adding an adapter module or performing full fine-tuning.

[0014] Fine-tuning training process: Use the constructed dataset to fine-tune the model, adopt a suitable training algorithm and loss function, and continuously adjust the model parameters so that the model can generate expected code based on the input code context.

[0015] Preferably, the method comprises the following steps:

[0016] Requirements understanding and conversion: Developers input natural language requirements descriptions or code modification intentions, and natural language processing technology is used to convert them into an input format acceptable to the model;

[0017] Optimization suggestion generation: After code generation, the code is statically analyzed and performance evaluated. If potential issues are found, optimization suggestions are generated using a large language model, such as those related to high code complexity and low memory usage efficiency.

[0018] Code modification and verification: Based on optimization suggestions, the code is modified automatically or after developer confirmation, and re-verified. The verification process includes syntax checking, unit testing, and integration testing to ensure that the modified code is functionally correct and has improved performance.

[0019] A system for context-aware code generation and real-time optimization based on a large language model, comprising:

[0020] The code parsing module is used to parse the input code and construct an abstract syntax tree using the compiler front-end technology;

[0021] The information extraction unit extracts function definitions, variable declarations, and statement block structure information through the abstract syntax tree to obtain the syntax and semantic structure of the code, providing basic data support for subsequent code generation and optimization.

[0022] Preferably, including:

[0023] A monitoring code embedding module is used to embed monitoring code in the code running environment;

[0024] Dynamic information collection unit, which collects dynamic information such as function call sequence, variable value changes, and loop execution times during code execution;

[0025] The dynamic information storage unit uses the dynamic information as part of the code context for subsequent code generation and optimization, such as recording function call parameters and return values, and loop variable changes.

[0026] Preferably, including:

[0027] The data integration module is used to integrate the code syntax and semantic structure data obtained by static analysis with the code operation information data obtained by dynamic analysis;

[0028] The model building unit builds a code context model based on the integrated data and uses a graph database or a custom data structure to store function call graphs and variable dependency graphs.

[0029] The context providing unit, the code context model provides rich contextual basis for subsequent fine-tuning of the large language model, such as establishing calling relationships and variable dependencies between functions.

[0030] Preferably, including:

[0031] The dataset construction module is used to build a fine-tuning dataset based on the collected code context information and corresponding high-quality code examples;

[0032] The model selection and adaptation module is used to select a pre-trained large language model suitable for code generation tasks and adjust the model based on the model architecture and fine-tuning method, such as adding an adapter module or performing full fine-tuning.

[0033] The fine-tuning training module is used to fine-tune the model using the constructed dataset, adopt appropriate training algorithms and loss functions, and continuously adjust model parameters so that the model can generate expected code based on the input code context.

[0034] Preferably, including:

[0035] The requirements understanding and conversion module is used to receive natural language requirements descriptions or code modification intentions input by developers and convert them into an input format acceptable to the model using natural language processing technology;

[0036] The optimization suggestion generation module is used to perform static analysis and performance evaluation of the code after code generation. If potential problems are found, the module uses a large language model to generate optimization suggestions, such as those for high code complexity and low memory usage efficiency.

[0037] The code modification and verification module is used to modify the code automatically or after developer confirmation based on optimization suggestions, and re-verify the code. The verification process includes syntax checking, unit testing, and integration testing to ensure that the modified code functions correctly and has improved performance.

[0038] Compared with the prior art, the present invention has the following beneficial effects:

[0039] The proposed method and system for context-aware code generation and real-time optimization based on a large language model addresses the lack of contextual awareness in traditional code generation techniques by integrating static syntax parsing (AST construction) with dynamic execution tracking (e.g., variable value changes and function call timing) to construct a context graph covering syntax, semantics, and runtime behavior. This improves the compatibility, maintainability, and scalability of generated code with project architecture.

[0040] Adapter-based fine-tuning technology, combined with project-specific code libraries and context graphs for model training, addresses the issue of code generated by general-purpose large language models not conforming to project specifications. This improves the percentage of generated code that complies with project specifications and reduces rework costs due to style discrepancies.

[0041] Optimized code is validated for functionality and performance based on an automated testing framework (unit and integration testing), forming a closed "generate-optimize-verify" loop to ensure both code quality and performance. Automated code generation and real-time optimization reduce manual coding and debugging time, and support unified code standards for collaborative teamwork. This effectively shortens development cycles and reduces collaboration conflicts within teams. This significantly improves the code quality of novice developers and alleviates the pressure of a shortage of technical talent. BRIEF DESCRIPTION OF THE DRAWINGS

[0042] Figure 1 Flow chart of the method of the present invention. DETAILED DESCRIPTION

[0043] In order to clearly and completely describe the objectives and technical solutions of the present invention and make the advantages more clearly understood, the embodiments of the present invention are further described in detail below with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are part of the embodiments of the present invention, not all of them, and are only used to explain the embodiments of the present invention, not to limit the embodiments of the present invention. All other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of the present invention.

[0044] For example 1, please refer to Figure 1 The present invention provides a technical solution: a context-aware code generation and real-time optimization method based on a large language model, comprising the following steps:

[0045] 1. Code structure analysis

[0046] Use compiler front-end technology (such as clang or javaparser, depending on the specific programming language) to parse the input code and build an abstract syntax tree (AST) to extract information such as function definition, variable declaration, statement block structure, etc., thereby obtaining the grammatical and semantic structure of the code.

[0047] 2. Dynamic Execution Analysis

[0048] Embed monitoring code in the code execution environment (such as through Java bytecode instrumentation or Python code tracking tools) to collect dynamic information such as function call order, variable value changes, and loop execution times. Take Python code as an example:

[0049]

[0050] During the execution process, the calling parameters and return value of the multiply function, as well as the changes in the loop variable i in the calculate function and the calculation process of the final result can be recorded.

[0051] 3. Data integration and modeling

[0052] Integrate the data from static parsing and dynamic analysis to build a code context model. Use a graph database (such as Neo4j) or a custom data structure to store information such as function call graphs and variable dependency graphs, providing rich context for subsequent model fine-tuning. For example, in the Java code model above, a call edge can be established from the main function to the add function, associating the passed parameters and return values. In the Python code model, the call relationship between the calculate function and the multiply function, as well as the variable dependencies, can be reflected.

[0053] 4. Fine-tuning Large Language Models

[0054] (a) Dataset Construction: Based on the collected code context information and corresponding high-quality code examples (obtained from open source code repositories, internal enterprise code repositories, or manually written examples), a fine-tuning dataset is constructed. For example, for a specific code snippet, a pair of code samples containing its context information and the correct modification or extension is prepared. For example, for a function that calculates the average of an array:

[0055]

[0056] To optimize its performance, provide sample pairs containing the original function context and an optimized version (for example, using the sum function instead of a loop for summation).

[0057] (b) Model selection and adaptation: Select a pre-trained large language model suitable for code generation tasks (such as the GPT series or CodeBERT), and adjust the model based on the model architecture and fine-tuning method (such as adapter-based fine-tuning and full fine-tuning). For example, for a model based on the Transformer architecture, add adapter modules to specific layers and train only the adapter parameters to reduce computing resource consumption and improve fine-tuning efficiency.

[0058] (c) Fine-tuning training process: Use the constructed dataset to fine-tune the model, using appropriate training algorithms (such as stochastic gradient descent, Adam optimizer, etc.) and loss functions (such as cross entropy loss, mean squared error loss, etc., depending on the task). During the training process, the model parameters are continuously adjusted so that the model can generate expected code based on the input code context. For example, after multiple rounds of training, the model can generate the correct function implementation code based on the given function requirements and context information, such as generating a function to calculate the Fibonacci sequence according to the requirements:

[0059]

[0060] 5. Code Generation and Optimization

[0061] (a) Requirements Understanding and Conversion: Developers input natural language requirements or code modification intentions, and natural language processing techniques (such as lexical analysis, syntactic analysis, and semantic understanding) are used to convert them into an input format acceptable to the model. For example, if a developer inputs "Create a function that accepts two integer parameters and returns their product," the system will parse it into a structured representation that contains information such as the operation type (multiplication), input parameter types (integer), and return value requirements.

[0062] (b) Optimization suggestion generation: After code generation, the code is statically analyzed (using tools such as Pylint and Checkstyle) and performance evaluated (through runtime performance monitoring or rule-based performance analysis). If potential issues are found, such as high code complexity or inefficient memory usage, the large language model is again used to generate optimization suggestions. For example, for a code snippet with multiple nested loops, the model may recommend unrolling some loops or using more efficient data structures to improve performance.

[0063] (c) Code Modification and Verification: Based on optimization suggestions, the code is modified automatically or after developer confirmation, and then re-verified. The verification process includes syntax checking, unit testing (using testing frameworks such as Pytest and JUnit), and integration testing (running in the actual project environment or a simulated environment) to ensure that the modified code functions correctly and has improved performance. For example, for an optimized sorting function, data sets of different sizes are input for testing to verify that the correctness of the sorting results and the execution time meet expectations.

[0064] Example 2, based on Example 1, proposes a system for context-aware code generation and real-time optimization method based on a large language model, including: a code parsing module, which uses compiler front-end technology to parse the input code and construct an abstract syntax tree; an information extraction unit, which extracts function definitions, variable declarations, and statement block structure information through the abstract syntax tree to obtain the syntax and semantic structure of the code, providing basic data support for subsequent code generation and optimization.

[0065] It also includes: a monitoring code embedding module for embedding monitoring code in the code running environment; a dynamic information collection unit for collecting dynamic information such as function call sequence, variable value changes, and loop execution times during code execution; and a dynamic information storage unit for using the dynamic information as part of the code context for subsequent code generation and optimization, such as recording function call parameters and return values, and loop variable changes.

[0066] It also includes: a data integration module, which is used to integrate the code syntax and semantic structure data obtained by static analysis with the code execution information data obtained by dynamic analysis; a model construction unit, which builds a code context model based on the integrated data, and uses a graph database or a custom data structure to store function call graphs and variable dependency graph information; a context provision unit, in which the code context model provides rich contextual basis for subsequent fine-tuning of the large language model, such as establishing calling relationships and variable dependencies between functions.

[0067] It also includes: a dataset construction module, which is used to build a fine-tuning dataset based on the collected code context information and corresponding high-quality code examples; a model selection and adaptation module, which is used to select a pre-trained large language model suitable for the code generation task and adjust the model according to the model architecture and fine-tuning method, such as adding an adapter module or performing full fine-tuning; a fine-tuning training module, which is used to fine-tune the model using the constructed dataset, adopting appropriate training algorithms and loss functions, and continuously adjusting model parameters so that the model can generate expected code based on the input code context.

[0068] It also includes: a requirements understanding and conversion module, which is used to receive natural language requirements descriptions or code modification intentions input by developers, and use natural language processing technology to convert them into an input format acceptable to the model; an optimization suggestion generation module, which is used to perform static analysis and performance evaluation on the code after code generation. If potential problems are found, a large language model is used to generate optimization suggestions, such as for problems with high code complexity and low memory usage efficiency; a code modification and verification module, which is used to modify the code automatically or after confirmation by the developer according to the optimization suggestions, and re-verify the code. The verification process includes syntax checking, unit testing and integration testing to ensure that the modified code functions correctly and has improved performance.

[0069] While embodiments of the present invention have been shown and described, it will be appreciated by those skilled in the art that various changes, modifications, substitutions, and variations may be made to these embodiments without departing from the principles and spirit of the invention, and that the scope of the invention is defined by the appended claims and their equivalents.

Claims

1. A context-aware code generation and real-time optimization method based on a large language model, characterized by: The following steps are involved: Use compiler front-end technology to parse the input code and build an abstract syntax tree; Through the abstract syntax tree, function definitions, variable declarations, and statement block structure information are extracted to obtain the syntax and semantic structure of the code, providing basic data support for subsequent code generation and optimization.

2. The context-aware code generation and real-time optimization method based on a large language model according to claim 1, characterized in that: The following steps are also included: Embed monitoring code in the code execution environment; During code execution, dynamic information about function call order, variable value changes, and loop execution times is collected; Dynamic information is used as part of the code context for subsequent code generation and optimization, such as recording function call parameters and return values, and loop variable changes.

3. The context-aware code generation and real-time optimization method based on a large language model according to claim 2, characterized in that: The following steps are involved: Integrate the code syntax and semantic structure data obtained from static analysis with the code execution information data obtained from dynamic analysis; Based on the integrated data, a code context model is constructed, using a graph database or custom data structure to store function call graphs and variable dependency graph information. The code context model provides rich contextual basis for subsequent fine-tuning of the large language model, such as establishing calling relationships and variable dependencies between functions.

4. The context-aware code generation and real-time optimization method based on a large language model according to claim 3, characterized in that: The following steps are involved: Dataset construction: Based on the collected code context information and corresponding high-quality code examples, a fine-tuning dataset is constructed; Model selection and adaptation: Select a pre-trained large language model suitable for code generation tasks and adjust the model based on the model architecture and fine-tuning methods, such as adding an adapter module or performing full fine-tuning. Fine-tuning training process: Use the constructed dataset to fine-tune the model, adopt a suitable training algorithm and loss function, and continuously adjust the model parameters so that the model can generate expected code based on the input code context.

5. The context-aware code generation and real-time optimization method based on a large language model according to claim 4, characterized in that: The following steps are involved: Requirements understanding and conversion: Developers input natural language requirements descriptions or code modification intentions, and natural language processing technology is used to convert them into an input format acceptable to the model; Optimization suggestion generation: After code generation, the code is statically analyzed and performance evaluated. If potential issues are found, optimization suggestions are generated using a large language model, such as those related to high code complexity and low memory usage efficiency. Code modification and verification: Based on optimization suggestions, the code is modified automatically or after developer confirmation, and re-verified. The verification process includes syntax checking, unit testing, and integration testing to ensure that the modified code is functionally correct and has improved performance.

6. A system for the context-aware code generation and real-time optimization method based on a large language model according to claim 5, characterized in that: include: The code parsing module is used to parse the input code and construct an abstract syntax tree using the compiler front-end technology; The information extraction unit extracts function definitions, variable declarations, and statement block structure information through the abstract syntax tree to obtain the syntax and semantic structure of the code, providing basic data support for subsequent code generation and optimization.

7. A system according to claim 6, characterized in that: include: A monitoring code embedding module is used to embed monitoring code in the code running environment; Dynamic information collection unit, which collects dynamic information such as function call sequence, variable value changes, and loop execution times during code execution; The dynamic information storage unit uses the dynamic information as part of the code context for subsequent code generation and optimization, such as recording function call parameters and return values, and loop variable changes.

8. A system according to claim 7, characterized in that: include: The data integration module is used to integrate the code syntax and semantic structure data obtained by static analysis with the code operation information data obtained by dynamic analysis; The model building unit builds a code context model based on the integrated data and uses a graph database or a custom data structure to store function call graphs and variable dependency graphs. The context providing unit, the code context model provides rich contextual basis for subsequent fine-tuning of the large language model, such as establishing calling relationships and variable dependencies between functions.

9. A system according to claim 8, characterized in that: include: The dataset construction module is used to build a fine-tuning dataset based on the collected code context information and corresponding high-quality code examples; The model selection and adaptation module is used to select a pre-trained large language model suitable for code generation tasks and adjust the model based on the model architecture and fine-tuning method, such as adding an adapter module or performing full fine-tuning. The fine-tuning training module is used to fine-tune the model using the constructed dataset, adopt appropriate training algorithms and loss functions, and continuously adjust model parameters so that the model can generate expected code based on the input code context.

10. A system according to claim 9, characterized in that: include: The requirements understanding and conversion module is used to receive natural language requirements descriptions or code modification intentions input by developers and convert them into an input format acceptable to the model using natural language processing technology; The optimization suggestion generation module is used to perform static analysis and performance evaluation of the code after code generation. If potential problems are found, the module uses a large language model to generate optimization suggestions, such as those for high code complexity and low memory usage efficiency. The code modification and verification module is used to modify the code automatically or after developer confirmation based on optimization suggestions, and re-verify the code. The verification process includes syntax checking, unit testing, and integration testing to ensure that the modified code functions correctly and has improved performance.

Citation Information

Patent Citations

  • Art box for students

    CN201980000U

  • Hand pulling type candle / firework disc rack capable of rotating left and right

    CN202010000U

Cited By

  • Structured text code generation and review method

    CN120743231A

  • Large model function call optimization method based on code generation, electronic equipment, computer readable storage medium and computer program product

    CN121455563A

  • Automatic Verilog code generation method and system based on AST grammar semantic enhancement

    CN121807317A