A code detection method and apparatus based on fine-tuning a large model
By building a fine-tuned large model, the problem of low efficiency in manual code detection and repair was solved, achieving efficient code detection and repair, and improving code accuracy and software quality.
Patent Information
- Application Number
- CN202510006669.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-01-03
- Publication Date
- 2025-10-31
- Estimated Expiration
- 2045-01-03
AI Technical Summary
Existing technologies for manually detecting and fixing code have low accuracy and efficiency, and cannot quickly obtain the correct code, especially as the size and complexity of software systems increase.
Construct a fine-tuned large model, including the correspondence between the detection code and the reference detection results, obtain the code to be detected, perform preprocessing, obtain the detection results through fine-tuning the model, and adjust the code fragments generated from the code to be detected based on the code generation model.
It improves the accuracy and efficiency of code inspection, reduces the possibility of human error, shortens the development cycle, and enhances software quality and maintainability.
Smart Images

Figure CN119807003B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of deep machine learning algorithm technology, and in particular to a code detection method and apparatus based on fine-tuning a large model. Background Technology
[0002] In recent years, the rise of large-scale models in the field of artificial intelligence has attracted widespread attention and discussion. With the continuous maturation of model training and fine-tuning techniques, and the increasing code capabilities of base models, developers can significantly improve development efficiency. Traditional code maintenance relies on manual review and testing, methods that are time-consuming and susceptible to human error. As software systems grow in scale and complexity, the accuracy and efficiency of manual code inspection and repair become increasingly limited, failing to quickly obtain correct code. Summary of the Invention
[0003] This invention provides a code detection method and apparatus based on fine-tuning a large model, which solves the problems of low accuracy and efficiency in manual code detection and repair in the prior art.
[0004] According to one aspect of the present invention, a code detection method based on fine-tuning a large model is provided, comprising:
[0005] Construct a fine-tuning model; wherein, the fine-tuning model includes the correspondence between detection codes and reference detection results;
[0006] Obtain the code to be detected;
[0007] The code to be detected is preprocessed to obtain the preprocessed code to be detected;
[0008] Based on the code to be detected and the fine-tuning model, obtain the detection result of the code to be detected;
[0009] Based on the code generation model, the code to be detected is adjusted according to the detection results, and code snippets are generated.
[0010] In one embodiment, the preprocessing of the code to be detected to obtain preprocessed code to be detected includes:
[0011] According to the format requirements, verify the format of the code to be detected and generate the verification result;
[0012] If the verification result indicates that the code to be detected is in a normal format, then remove the characters in the code to be detected that match the preset characters to obtain the cleaned code to be detected;
[0013] Remove characters from the code to be detected that match preset symbols to obtain the code to be detected after removal;
[0014] According to the filtering rules, key code is obtained from the removed code to be detected.
[0015] In one embodiment, the step of filtering out key code from the removed code to be detected according to the filtering rules includes:
[0016] The removed code to be detected is then classified to obtain the classified code to be detected.
[0017] Run the classified code to be detected;
[0018] Select the classified code that meets the functional requirements after running the test as the target code;
[0019] Delete the target code that has the same content as the target code to obtain the key code.
[0020] In one embodiment, constructing the fine-tuning model includes:
[0021] Obtain multiple anomaly detection codes and multiple normal detection codes; wherein, each anomaly detection code corresponds to an error type; wherein, the reference detection result includes the error type;
[0022] Based on the full-parameter fine-tuning strategy, a fine-tuning model is generated according to the multiple anomaly detection codes, multiple normal detection codes, and the error type corresponding to each anomaly detection code.
[0023] In one embodiment, after acquiring multiple anomaly detection codes and multiple normal detection codes, the code detection method based on fine-tuning a large model further includes:
[0024] Remove the comments from the multiple anomaly detection codes;
[0025] Remove the comments from the multiple normal detection codes;
[0026] The step of generating a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, multiple normal detection codes, and the error type corresponding to each anomaly detection code, includes:
[0027] Based on a full-parameter fine-tuning strategy, a fine-tuning model is generated according to the multiple removed anomaly detection codes, the multiple removed normal detection codes, and the error type corresponding to each removed anomaly detection code.
[0028] In one embodiment, the method for constructing the code generation model includes:
[0029] Obtain multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to modified codes;
[0030] Based on a full-parameter fine-tuning strategy, a code generation model is generated according to the multiple anomaly detection codes, multiple normal detection codes, and the modified code corresponding to each anomaly detection code.
[0031] In one embodiment, adjusting the code to be detected and generating a code snippet based on the detection result according to the code generation model includes:
[0032] If the detection result indicates that the code to be detected is abnormal, then the code to be detected is input into the code generation model to obtain a code fragment.
[0033] In one embodiment, before adjusting the code to be detected and generating a code snippet based on the detection result according to the code generation model, the code detection method based on fine-tuning a large model further includes:
[0034] Get the number of runs;
[0035] Based on the number of runs, the code to be tested is run and multiple running results are generated;
[0036] The step of adjusting the code to be detected and generating a code snippet based on the detection results, according to the code generation model, includes:
[0037] If it is determined that the error type of each running result is the same as the initial error type corresponding to the code to be detected, then based on the code generation model, the code to be detected is adjusted and a code snippet is generated according to the detection result.
[0038] In one embodiment, adjusting the code to be detected and generating a code snippet based on the detection result according to the code generation model includes:
[0039] If the detection result indicates that the code to be detected is abnormal, then the abnormal code of the code to be detected is extracted;
[0040] The exception code is input into the code generation model to generate a code snippet.
[0041] According to another aspect of the present invention, a code detection device based on fine-tuning a large model is provided, comprising:
[0042] A building module is used to build a fine-tuning model; wherein, the fine-tuning model includes the correspondence between detection codes and reference detection results;
[0043] The acquisition module is used to acquire the code to be detected.
[0044] A preprocessing module is used to preprocess the code to be detected to obtain preprocessed code to be detected;
[0045] The detection module is used to obtain the detection result of the code to be detected based on the code to be detected and the fine-tuning model;
[0046] The generation module is used to adjust the code to be detected and generate code snippets based on the detection results, according to the code generation model.
[0047] This invention provides a code detection method and apparatus based on a fine-tuned large model, comprising: constructing a fine-tuning model, wherein the fine-tuning model includes the correspondence between detection code and reference detection results; obtaining code to be detected; preprocessing the code to be detected to obtain preprocessed code to be detected; obtaining the detection result of the code to be detected based on the code to be detected and the fine-tuning model; and adjusting the code to be detected based on the detection result using a code generation model to generate code fragments. By constructing a fine-tuning model and inputting the code to be detected into the fine-tuning model, it is possible to determine whether the code to be detected is abnormal. Abnormal code is then input into the code generation model and modified, thereby generating code fragments that can run normally. This addresses the problems of low accuracy and efficiency in manual code detection and repair in existing technologies. Attached Figure Description
[0048] Figure 1 This is a flowchart illustrating a code detection method based on a fine-tuned large model provided in an exemplary embodiment of the present invention.
[0049] Figure 2 This is a flowchart illustrating a code preprocessing method to be detected provided in an exemplary embodiment of the present invention.
[0050] Figure 3 This is a schematic diagram of the structure of a code detection device based on a fine-tuned large model provided in an exemplary embodiment of the present invention.
[0051] Figure 4 This is a schematic diagram of the structure of a code detection device based on a fine-tuned large model provided in another exemplary embodiment of the present invention.
[0052] Figure 5 This is a structural diagram of an electronic device provided in an exemplary embodiment of the present invention. Detailed Implementation
[0053] Figure 1 This is a flowchart illustrating a code detection method based on a fine-tuned large model provided in an exemplary embodiment of the present invention. Figure 1 As shown, code detection methods based on fine-tuning large models include:
[0054] Step 110: Construct a fine-tuning model, which includes the correspondence between detection codes and reference detection results.
[0055] Step 120: Obtain the code to be detected.
[0056] In this embodiment of the invention, the method for obtaining the code to be detected may include: acquiring the code through online resources, public datasets, professional databases, and literature in specific fields obtained through web crawling technology. Specifically, this invention can develop customized crawler programs to adapt to different website structures and data formats, ensuring the diversity and comprehensiveness of the data. Based on project requirements, the crawled data undergoes preliminary screening to exclude content unrelated to programming, ensuring the relevance and accuracy of the data. Besides web crawling, data sources can also be enriched by purchasing professional datasets, collaborating to acquire data, or using data synthesis techniques.
[0057] Step 130: Preprocess the code to be detected to obtain the preprocessed code to be detected.
[0058] In this embodiment of the invention, preprocessing the code to be detected is an important step in software development, which helps improve code readability and maintainability. Preprocessing may include code formatting, eliminating unnecessary comments, optimizing variable naming, and structural adjustments. By cleaning and optimizing the code, developers can more easily identify potential errors and vulnerabilities. Furthermore, good preprocessing can improve code execution efficiency, reduce subsequent debugging time, and ensure software compatibility in different environments.
[0059] Step 140: Based on the code to be detected and the fine-tuned model, obtain the detection results of the code to be detected.
[0060] In this embodiment of the invention, the process of obtaining detection results based on the code to be detected and the fine-tuned model is crucial. First, the code to be detected needs to be preprocessed to ensure its format and structure meet the model's input requirements. Next, the code is input into the fine-tuned model, which analyzes it using existing training data to identify potential problems or vulnerabilities. Finally, the detection results can be fed back to the developers in the form of a report, pointing out errors, performance bottlenecks, or security risks in the code. This process not only improves code quality but also accelerates the development cycle, ensuring the software reaches its optimal state before release.
[0061] Step 150: Based on the code generation model, adjust the code to be detected and generate code snippets according to the detection results.
[0062] In this embodiment of the invention, the process of adjusting the code to be detected and generating code snippets based on the detection results using a code generation model is particularly important in software development. By analyzing the detection results, developers can identify defects and areas for improvement in the code, and then automatically generate optimized code snippets using the code generation model. This model, by learning from a large number of high-quality code samples, can propose solutions that conform to best practices. This not only improves development efficiency but also reduces the possibility of human error, making the final generated code more robust and maintainable. This process effectively shortens the iteration cycle and improves the overall quality of the software.
[0063] This invention provides a code detection method based on a fine-tuned large model, comprising: constructing a fine-tuning model, wherein the fine-tuning model includes the correspondence between detection code and reference detection results; obtaining the code to be detected; preprocessing the code to be detected to obtain preprocessed code to be detected; obtaining the detection result of the code to be detected based on the code to be detected and the fine-tuning model; and adjusting the code to be detected based on the detection result using a code generation model to generate code snippets. By constructing a fine-tuning model and inputting the code to be detected into it, it is possible to determine whether the code to be detected is abnormal. Abnormal code is then input into the code generation model and modified, thereby generating code snippets that can run normally. This addresses the problems of low accuracy and efficiency in manual code detection and repair in existing technologies.
[0064] Figure 2 This is a flowchart illustrating a code preprocessing method to be detected provided in an exemplary embodiment of the present invention. Figure 2 As shown, step 130 may include:
[0065] Step 131: Verify the format of the code to be tested according to the format requirements and generate the verification result.
[0066] In this embodiment of the invention, firstly, code formatting specifications need to be clearly defined, such as indentation, naming rules, and comment style. These specifications aim to improve code readability and consistency. Next, static code analysis tools are used to verify the format of the code under test; these tools automatically check whether the code conforms to preset formatting standards.
[0067] During the verification process, the tool identifies non-compliant parts, such as extra spaces, incorrect indentation, and non-standard variable naming. This process reduces communication costs caused by format inconsistencies, ultimately improving the overall quality and maintainability of the software.
[0068] Step 132: If the verification result indicates that the code to be detected is in a normal format, remove the characters in the code to be detected that match the preset characters to obtain the cleaned code to be detected.
[0069] In this embodiment of the invention, if the verification result indicates that the code to be tested has a normal format, the code can be further cleaned to improve its readability and conciseness. The first step of cleaning is to identify characters in the code that match preset characters. These characters may be redundant comments, debugging information, or unnecessary spaces, etc. Automated tools are typically used during the cleaning process, which can effectively identify and remove unnecessary characters and information. The cleaned code to be tested will be cleaner, which not only improves the code's aesthetics but also reduces potential misunderstandings and maintenance difficulties. Furthermore, the cleaned code performs better in subsequent performance testing and defect detection because unnecessary interference information is removed, making it easier to locate problems and optimize performance. Therefore, this process is crucial for ensuring code quality and improving development efficiency.
[0070] Step 133: Remove characters that match the preset symbols from the code to be detected to obtain the code to be detected after removal.
[0071] In this embodiment of the invention, removing characters from the code to be detected that match preset symbols to obtain the cleaned code is a crucial step in code optimization and cleanup. Preset symbols typically include specific characters, comment symbols, spaces, newlines, or other redundant information. These may be unnecessary in the core logic of the code and could even interfere with readability and maintainability. Identifying and removing these symbols makes the code more concise, facilitating subsequent analysis and debugging. The cleaned code will be clearer, aiding in quickly locating problems and expanding functionality. Furthermore, the optimized code usually exhibits improved performance because unnecessary characters or information no longer consume memory and processing resources. Ultimately, this process significantly improves the overall quality of the code and development efficiency.
[0072] Step 134: Based on the filtering rules, filter out the key code from the removed code to be detected.
[0073] In this embodiment of the invention, data cleaning is a crucial step in ensuring data quality. Its purpose is to remove noise and inconsistencies from the data, thereby improving its usability. Automated scripts and algorithms are used to identify and remove irrelevant characters, special symbols, and formatting errors from the data. Standardizing data formats, such as converting all code to a unified programming language format, ensures data consistency. Specifically for programming data, algorithms need to be developed to identify and process comments, because while comments are helpful for understanding code, they may introduce unnecessary information when training models.
[0074] In one embodiment, step 134 can be specifically implemented as follows: classifying the removed code to be detected to obtain classified code to be detected; running the classified code to be detected; selecting the classified code to be detected whose output meets the functional requirements as the target code; deleting the target code with the same content as the target code to obtain the key code.
[0075] In this embodiment of the invention, classifying the removed code to be tested to obtain classified code is a key step in optimizing code management and improving software quality. During the classification process, automated tools can help identify the relevance of code snippets and group them into an ordered code structure. After obtaining the classified code to be tested, the next step is to run this code. By executing the code, its functionality can be verified to meet expectations, and the performance of different categories of code in actual operation can be observed. This process helps to discover potential errors or performance issues, providing a basis for subsequent optimization. The target code should not only achieve the expected functionality but also be efficient and readable for easy maintenance. Finally, to ensure the conciseness and efficiency of the code, other target code with the same content as the target code needs to be deleted, thus obtaining the critical code. This step aims to eliminate redundancy, retain the optimal implementation scheme, and ensure that the critical code meets project requirements in terms of both functionality and performance.
[0076] In one embodiment, step 110 can be specifically implemented as follows: obtaining multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to error types; wherein, the reference detection results include error types; and generating a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, the multiple normal detection codes, and the error types corresponding to each anomaly detection code.
[0077] In this embodiment of the invention, acquiring multiple anomaly detection codes and multiple normal detection codes is the first step in building an efficient anomaly detection system. These anomaly detection codes contain different types of errors, such as syntax errors, logical errors, and runtime errors, while normal detection codes represent code implementations that conform to the expected functionality and logic. By analyzing these codes, we can gain a deeper understanding of the characteristics and manifestations of various errors. During this process, the error types corresponding to the anomaly detection codes will serve as important annotation information. These error types can help fine-tune the model to more accurately understand which code patterns are associated with specific errors. Simultaneously, the reference detection results must also include these error types to provide the model with clear learning objectives. Full-parameter fine-tuning is a machine learning technique that adjusts all parameters of the model to better adapt it to new datasets. In this process, the model learns how to distinguish between anomaly detection codes and normal detection codes, and how to identify features associated with specific error types.
[0078] By comprehensively analyzing multiple anomaly and normal detection codes, the fine-tuning model can grasp more complex code patterns, thereby enhancing its effectiveness in real-world applications. Ultimately, the generated fine-tuned model not only improves the accuracy of anomaly detection but also plays a crucial role in subsequent code reviews and automated repairs. This technical solution provides a solid foundation for achieving efficient code quality assurance, contributing to improved overall efficiency and reliability in software development.
[0079] Then, based on the two functions of the model, different training strategies are designed. For code problem detection, the focus is on training the model to identify and fix logical errors; for code generation, the focus is on training the model to understand user descriptions and generate accurate code. Appropriate loss functions are selected to measure model performance, such as cross-entropy loss and mean squared error, to ensure the model can learn effectively. Clear evaluation metrics, such as accuracy, recall, and F1 score, are defined to quantify the model's performance.
[0080] In one embodiment, step 110 may be specifically implemented as: removing comments from multiple anomaly detection codes; removing comments from multiple normal detection codes; wherein, the step of generating a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, the multiple normal detection codes, and the error type corresponding to each anomaly detection code, includes: generating a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes after removal, the multiple normal detection codes after removal, and the error type corresponding to each removed anomaly detection code.
[0081] In this embodiment of the invention, removing comments from multiple anomaly detection codes and multiple normal detection codes is a crucial step in the code cleanup and optimization process. Comments in code are typically used to explain logic, provide context, or describe complex functions; however, during data analysis and model training, redundant comments can introduce noise and affect the model's learning performance. Therefore, cleaning up comments can help improve the accuracy and effectiveness of subsequent analysis. Specifically, a systematic analysis of multiple anomaly detection codes and normal detection codes is first required to identify all commented sections. These comments may include single-line comments, multi-line comments, and docstrings. By using text processing tools or regular expressions, these comments can be quickly and effectively located and deleted, ensuring that the core logic and structure of the code are preserved. The process of removing comments also helps reduce code complexity, making the remaining code more concise and clear. Thus, in subsequent model training and anomaly detection tasks, the algorithm can focus more on the actual behavior of the code rather than human-added explanations, thereby improving the model's ability to recognize code patterns. Furthermore, the cleaned code allows for faster understanding of the code logic during code review, especially when faced with a large amount of code; the simplified code structure can significantly improve review efficiency. Ultimately, removing comments not only provides a clearer data foundation for generating fine-tuned models, but also lays the groundwork for improving code quality and maintainability, ensuring the efficiency and stability of software development.
[0082] In one embodiment, the method for constructing a code generation model can be specifically implemented as follows: obtaining multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to modified codes; and generating a code generation model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, the multiple normal detection codes, and the modified codes corresponding to each anomaly detection code.
[0083] In this embodiment of the invention, acquiring multiple anomaly detection codes and multiple normal detection codes is a crucial step in building an intelligent code generation model. In this process, the anomaly detection codes not only include code containing errors or problems, but also their corresponding modified versions. These modified codes represent fixed or optimized versions, demonstrating how erroneous code can be transformed into functional code. This provides the model with valuable learning data, enabling it to understand the sources of errors and their corrective measures.
[0084] First, the collection of anomaly detection code should cover multiple programming languages and error types to ensure the model's generalization ability. During the collection process, the development team needs to carefully label the error type of each anomaly detection code and associate it with its corresponding modified code. This association will provide important contextual information for model training, helping it understand the connection between errors and fixes.
[0085] Next, based on the full-parameter fine-tuning strategy, the team will use the collected anomaly detection code, normal detection code, and their corresponding modified code to generate a code generation model. Full-parameter fine-tuning is a powerful technique that allows the model to perform deep learning and adjustments based on existing knowledge to adapt to new data. This process will enable the model not only to identify normal and abnormal code, but also to learn the specific steps and strategies for transforming abnormal code into normal code.
[0086] During the model training phase, the algorithm analyzes a large number of code examples to identify common patterns that lead to anomalies and learns corresponding solutions. For each anomaly detection code, the model learns how to apply the logic in the modified code to generate equivalent, functional code. In this way, the model can continuously optimize its generation capabilities, ultimately forming an efficient code generation tool.
[0087] Ultimately, this code generation model can not only automatically generate code that meets functional requirements, but also provide repair suggestions for existing abnormal code during the development process. This will greatly improve the efficiency of software development, reduce code defects caused by human error, and improve code quality and maintainability.
[0088] In one embodiment, step 150 can be specifically implemented as follows: if the detection result indicates that the code to be detected is abnormal, the code to be detected is input into the code generation model to obtain a code fragment.
[0089] In this embodiment of the invention, when the detection result indicates that the code to be detected is abnormal, the next step is to input the code to be detected into a pre-trained code generation model to generate repaired or optimized code snippets. This process not only improves development efficiency but also automates error repair, reducing the need for manual intervention. First, before inputting the code, the code to be detected needs to undergo preprocessing. This may include removing irrelevant comments, formatting the code structure, and ensuring that the code meets input requirements. This step is to ensure that the code generation model can accurately understand the context and logical structure of the code to be detected. In addition, preprocessing can help the model more effectively identify abnormal parts, so that it can focus more on fixing related errors when generating code. Next, the code to be detected will be input into the code generation model. At this stage, the model will analyze the structure, syntax, and logic of the code to be detected based on its previously learned knowledge, identify existing problems, and generate corresponding code snippets.
[0090] In one embodiment, before step 150, the code detection method based on fine-tuning the large model can be specifically implemented as follows: obtaining the number of runs; running the code to be detected and generating multiple running results based on the number of runs; the code detection method based on fine-tuning the large model can be specifically implemented as follows: if it is determined that the error type of each running result is the same as the initial error type corresponding to the code to be detected, then based on the code generation model, adjusting the code to be detected and generating code fragments according to the detection results.
[0091] In this embodiment of the invention, the number of times the code is run and the results it generates are crucial for detecting and fixing errors. By obtaining the number of times the code to be tested is run, we can systematically analyze its behavior, identify potential errors, and generate corresponding repair suggestions. The system needs to record the number of times the code to be tested is run. This can be achieved by setting a counter that increments each time the code is executed. The significance of obtaining the number of runs is that it provides a sufficient data foundation for subsequent error analysis. The statistics of the number of runs can also help the development team understand the stability of the code; frequently run code segments are more likely to expose potential problems. After a certain number of runs have been counted, the system will run the code to be tested based on these runs. Each run will generate a result, which will be stored for subsequent analysis. The reason for setting multiple runs is that some errors may be intermittent, occurring only under specific conditions. Therefore, through multiple runs, we can obtain a more comprehensive dataset to help identify error patterns. If it is determined that the error type of each run result is the same as the initial error type corresponding to the code to be tested, the system will proceed to the next step. The purpose of this consistency check is to ensure that the current error analysis and detection are effective; only when the error types are consistent can subsequent code adjustments be safely made.
[0092] In one embodiment, step 150 may be specifically implemented as follows: if the detection result indicates that the code to be detected is abnormal, then the abnormal code of the code to be detected is extracted; the abnormal code is input into the code generation model to generate code fragments.
[0093] In this embodiment of the invention, the system needs to receive detection results from the running code to be detected. If the detection result indicates that the code to be detected is abnormal, the system will trigger subsequent exception handling procedures. The system will use static and dynamic analysis tools to identify the specific type of exception, such as runtime errors, logical errors, out-of-bounds access, null pointer references, etc. It will collect relevant information when the exception occurs, including exception stack traces, input parameters, runtime environment, etc., for subsequent analysis. After confirming that the code to be detected is abnormal, the system will extract the code segment related to the exception. It will analyze the exception stack information to determine the specific line of code that caused the exception. It will extract contextual information related to the abnormal code, including function call chains, variable states, execution paths, etc., to generate more contextualized repair suggestions. The extracted abnormal code will be input into a pre-trained code generation model. This model can be based on machine learning or deep learning techniques and aims to understand and generate code. It will ensure that the input abnormal code conforms to the model's input format requirements, including necessary comments, code style, etc., to improve the quality of the generated results. The extracted contextual information will also be input into the model to help the model better understand the cause and background of the exception. The model will adopt appropriate repair strategies based on the input abnormal code and its context. Common fixes include code refactoring, adding exception handling logic, and modifying conditional statements. The model may generate multiple code snippets to address different fixes for the same exception. Developers can choose the most suitable fix based on their specific needs. Unit tests are performed on the generated code snippets to verify their performance under different input conditions, ensuring the fix is effective and does not introduce new errors.
[0094] In one embodiment, after step 150, the code detection method based on the fine-tuned large model can be specifically implemented as follows: Real-time tracking of the execution status of code segments to capture code anomalies and errors. When a code error is detected, the system automatically extracts the relevant code segment for further analysis. The fine-tuned model receives the erroneous code and uses previously trained knowledge to perform logical and syntactic analysis. Based on the analysis results, the model proposes a repair plan or directly generates corrected code. Under the premise of ensuring that the repaired code will not cause other problems, the system automatically replaces the original erroneous code. After replacement, automated testing is immediately performed to verify the repair effect and ensure the stability and performance of the new code. Through continuous monitoring and repair, the system achieves self-iteration and optimization. This reduces manual intervention by operations and maintenance personnel, lowers maintenance costs, and improves system stability. The system automatically records all monitoring, analysis, and repair processes and results, facilitating problem tracking and historical analysis. Operations and maintenance personnel regularly check the logs, perform manual verification and review, and ensure that the automatic maintenance of the system does not introduce new problems.
[0095] Figure 3 This is a schematic diagram of the structure of a code detection device based on a fine-tuned large model provided in an exemplary embodiment of the present invention. Figure 3 As shown, the code detection device based on a fine-tuned large model includes: a construction module 201 for constructing a fine-tuning model, wherein the fine-tuning model includes the correspondence between the detection code and the reference detection result; an acquisition module 202 for acquiring the code to be detected; a preprocessing module 203 for preprocessing the code to be detected to obtain the preprocessed code to be detected; a detection module 204 for acquiring the detection result of the code to be detected based on the code to be detected and the fine-tuning model; and a generation module 205 for adjusting the code to be detected and generating code fragments based on the code generation model and the detection result.
[0096] This invention provides a code detection device based on a fine-tuned large model, comprising: a construction module constructing a fine-tuning model, wherein the fine-tuning model includes the correspondence between detection code and reference detection results; acquiring code to be detected; a preprocessing module preprocessing the code to be detected to obtain preprocessed code to be detected; a detection module acquiring detection results for the code to be detected based on the code to be detected and the fine-tuning model; and a generation module adjusting the code to be detected and generating code snippets based on the detection results using a code generation model. By constructing a fine-tuning model and inputting the code to be detected into it, it is possible to determine whether the code to be detected is abnormal. Abnormal code is then input into the code generation model and modified, resulting in code snippets that can run normally. This addresses the problems of low accuracy and efficiency in manual code detection and repair in existing technologies.
[0097] Figure 4 This is a schematic diagram of the structure of a code detection device based on a fine-tuned large model provided in another exemplary embodiment of the present invention. Figure 4 As shown, the preprocessing module 203 may include: a formatting unit 2031, used to verify the format of the code to be detected according to the format requirements and generate a verification result; a first removal unit 2032, used to remove characters in the code to be detected that match preset characters if the verification result indicates that the format of the code to be detected is normal, so as to obtain the cleaned code to be detected; a second removal unit 2033, used to remove characters in the code to be detected that match preset symbols, so as to obtain the removed code to be detected; and a filtering unit 2034, used to filter out key codes from the removed code to be detected according to the filtering rules.
[0098] In one embodiment, the filtering unit 2034 may be specifically configured to: classify the removed code to be detected to obtain classified code to be detected; run the classified code to be detected; select the classified code to be detected whose output meets the functional requirements as the target code; and delete the target code with the same content as the target code to obtain the key code.
[0099] In one embodiment, the construction module 201 may be specifically configured to: acquire multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to error types; wherein, the reference detection results include error types; and generate a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, the multiple normal detection codes, and the error types corresponding to each anomaly detection code.
[0100] In one embodiment, the construction module 201 may be specifically configured to: remove comments from multiple anomaly detection codes; remove comments from multiple normal detection codes; and generate a fine-tuning model based on a full-parameter fine-tuning strategy, according to the removed multiple anomaly detection codes, the removed multiple normal detection codes, and the error type corresponding to each removed anomaly detection code.
[0101] In one embodiment, the code generation model construction apparatus includes: acquiring multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to modified codes; and generating a code generation model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, the multiple normal detection codes, and the modified codes corresponding to each anomaly detection code.
[0102] In one embodiment, the generation module 205 may be specifically configured to: if the detection result indicates that the code to be detected is abnormal, then input the code to be detected into the code generation model to obtain a code fragment.
[0103] In one embodiment, before the generation module 205, the code detection device 20 based on the fine-tuning large model can be specifically configured to: obtain the number of runs; run the code to be detected according to the number of runs and generate multiple running results; wherein, the generation module 205 can be specifically configured to: if it is determined that the error type of each running result is the same as the initial error type corresponding to the code to be detected, then based on the code generation model, adjust the code to be detected according to the detection results and generate code fragments.
[0104] In one embodiment, the generation module 205 may be specifically configured to: if the detection result indicates that the code to be detected is abnormal, extract the abnormal code of the code to be detected; and input the abnormal code into the code generation model to generate code fragments.
[0105] Figure 5 A block diagram of an electronic device according to an embodiment of this application is illustrated.
[0106] like Figure 5 As shown, the electronic device 10 includes one or more processors 11 and memory 12.
[0107] The processor 11 may be a central processing unit (CPU) or other form of processing unit with data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 10 to perform desired functions.
[0108] The memory 12 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may include, for example, random access memory (RAM) and / or cache memory. The non-volatile memory may include, for example, read-only memory (ROM), hard disk, flash memory, etc. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 11 may execute the program instructions to implement the code detection method based on fine-tuning large models described in the various embodiments of this application above, and / or other desired functions. Various contents such as input signals, signal components, and noise components may also be stored in the computer-readable storage medium.
[0109] In one example, the electronic device 10 may also include an input device 13 and an output device 14, which are interconnected via a bus system and / or other forms of connection mechanism (not shown).
[0110] When the electronic device 10 is a standalone device, the input device 13 can be a communication network connector for receiving the collected input signals from the first device and the second device.
[0111] In addition, the input device 13 may also include, for example, a keyboard, a mouse, etc.
[0112] The output device 14 can output various information to the outside, including determined distance information, direction information, etc. The output device 14 may include, for example, a display, a speaker, a printer, and a communication network and its connected remote output devices, etc.
[0113] Of course, for the sake of simplicity, Figure 5 Only some of the components of the electronic device 10 relevant to this application are shown in this illustration; components such as buses, input / output interfaces, etc., are omitted. In addition, the electronic device 10 may include any other suitable components depending on the specific application.
[0114] The computer program product can be written in any combination of one or more programming languages to perform the operations of the embodiments of this application. The programming languages include object-oriented programming languages such as Java and C++, as well as conventional procedural programming languages such as C or similar languages. The program code can be executed entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0115] The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may, for example, include, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses, or devices, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: electrical connections having one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fibers, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0116] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.
Claims
1. A code detection method based on fine-tuning a large model, characterized in that, include: Construct a fine-tuning model; wherein, the fine-tuning model includes the correspondence between detection codes and reference detection results; The construction of the fine-tuning model includes: Obtain multiple anomaly detection codes and multiple normal detection codes; wherein, each anomaly detection code corresponds to an error type; wherein, the reference detection result includes the error type; Based on the full-parameter fine-tuning strategy, a fine-tuning model is generated according to the multiple anomaly detection codes, multiple normal detection codes, and the error type corresponding to each anomaly detection code; Obtain the code to be detected; The code to be detected is preprocessed to obtain the preprocessed code to be detected; Based on the preprocessed code to be detected and the fine-tuned model, the detection result of the code to be detected is obtained; Based on the code generation model, the code to be detected is adjusted according to the detection results, and code snippets are generated. The method for constructing the code generation model includes: Obtain multiple anomaly detection codes and multiple normal detection codes; wherein, the anomaly detection codes correspond to modified codes; Based on a full-parameter fine-tuning strategy, a code generation model is generated according to the multiple anomaly detection codes, multiple normal detection codes, and the modified code corresponding to each anomaly detection code. The code generation model, based on the detection results, adjusts the code to be detected and generates code snippets, including: If the detection result indicates that the code to be detected is abnormal, then the code to be detected is input into the code generation model to obtain a code fragment.
2. The code detection method based on fine-tuning a large model according to claim 1, characterized in that, The preprocessing of the code to be detected to obtain the preprocessed code to be detected includes: According to the format requirements, verify the format of the code to be detected and generate the verification result; If the verification result indicates that the code to be detected is in a normal format, then remove the characters in the code to be detected that match the preset characters to obtain the cleaned code to be detected; Remove characters from the code to be detected that match preset symbols to obtain the code to be detected after removal; According to the filtering rules, key code is obtained from the removed code to be detected.
3. The code detection method based on fine-tuning a large model according to claim 2, characterized in that, The step of filtering the key code from the removed code to be detected according to the filtering rules includes: The removed code to be detected is then classified to obtain the classified code to be detected. Run the classified code to be detected; Select the classified code that meets the functional requirements after running the test as the target code; Delete the target code that has the same content as the target code to obtain the key code.
4. The code detection method based on fine-tuning a large model according to claim 3, characterized in that, After obtaining multiple anomaly detection codes and multiple normal detection codes, the process also includes: Remove the comments from the multiple anomaly detection codes; Remove the comments from the multiple normal detection codes; The step of generating a fine-tuning model based on a full-parameter fine-tuning strategy, according to the multiple anomaly detection codes, multiple normal detection codes, and the error type corresponding to each anomaly detection code, includes: Based on a full-parameter fine-tuning strategy, a fine-tuning model is generated according to the multiple removed anomaly detection codes, the multiple removed normal detection codes, and the error type corresponding to each removed anomaly detection code.
5. The code detection method based on fine-tuning a large model according to claim 4, characterized in that, Before adjusting the code to be detected and generating a code snippet based on the detection result according to the code generation model, the method further includes: Get the number of runs; Based on the number of runs, the code to be tested is run and multiple running results are generated; The step of adjusting the code to be detected and generating code snippets based on the detection results, according to the code generation model, includes: If it is determined that the error type of each running result is the same as the initial error type corresponding to the code to be detected, then based on the code generation model, the code to be detected is adjusted and a code snippet is generated according to the detection result.
6. The code detection method based on fine-tuning a large model according to claim 5, characterized in that, The code generation model, based on the detection results, adjusts the code to be detected and generates code snippets, including: If the detection result indicates that the code to be detected is abnormal, then the abnormal code of the code to be detected is extracted; The exception code is input into the code generation model to generate a code snippet.
Citation Information
Patent Citations
AST-based source code anomaly detection method and device
CN115146282A
Code defect detection method and device based on large model and related equipment
CN117743164A