Instruction optimization method and device, electronic device and storage medium

By constructing an instruction correction model based on Markov decision processes, the instructions of large language models are optimized, solving the problem of low efficiency in manual optimization and enabling rapid generation of high-quality code.

CN120372299BActive Publication Date: 2025-10-28JINAN INSPUR DATA TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202510870952.7
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-06-26
Publication Date
2025-10-28
Estimated Expiration
2045-06-26

AI Technical Summary

Technical Problem

In existing technologies, manually optimizing instructions generated from large language models is inefficient and costly, and it is difficult to quickly determine instructions that meet the requirements, thus hindering the generation efficiency.

Method used

By using a pre-defined instruction correction model based on Markov decision processes, the failure information of generated failed instructions is used to optimize the instructions, generate an instruction optimization dataset, and train the instruction optimization model to automatically optimize the instructions input by the user.

Benefits of technology

It enables the rapid identification of instructions that meet the requirements, improves instruction optimization efficiency, and automatically generates high-quality code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120372299B_ABST
    Figure CN120372299B_ABST
Patent Text Reader

Abstract

This application discloses a method, apparatus, electronic device, and storage medium for instruction optimization, relating to the field of data processing technology. It includes building a model based on a Markov decision process, generating a fine-tuned dataset (i.e., an instruction optimization dataset) to adjust the instruction optimization model, and fine-tuning the instruction optimization model based on the instruction optimization dataset. This enables the instruction optimization model to optimize the instructions to be optimized, thereby helping users generate code that meets their requirements. It can automatically optimize user-input instructions. Therefore, it solves the technical problem of how to quickly determine instructions that meet requirements to improve instruction optimization efficiency, achieving the technical effect of quickly determining instructions that meet requirements and improving instruction optimization efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of data processing technology, and in particular to a method and apparatus for instruction optimization, an electronic device, and a storage medium. Background Technology

[0002] Large Language Models (LLMs) have demonstrated outstanding performance in the field of natural language processing, significantly impacting various tasks including text generation, image generation, translation, and code generation. However, despite their powerful language capabilities, the output quality of LLMs remains highly dependent on the quality of the input instructions. This is especially true in code generation tasks, where the accuracy of instructions is crucial; erroneous or ambiguous encoded instructions can easily lead to the model generating incorrect code.

[0003] In existing technologies, manual instruction optimization is a common method to improve the code quality generated by large language models. Instructions are optimized into high-quality coded instructions through repeated modifications to be represented by the large language model. However, manual instruction optimization is inefficient and costly, and it is difficult to quickly write instructions that meet the requirements, thus hindering the efficiency of large language model generation. Therefore, how to quickly determine the instructions that meet the requirements in order to improve the efficiency of instruction optimization is an urgent problem to be solved. Summary of the Invention

[0004] This application provides methods, apparatus, electronic devices, and storage media for instruction optimization, to at least address the problem in the related art of how to quickly determine instructions that meet requirements in order to improve the efficiency of instruction optimization.

[0005] This application provides a method for instruction optimization, including:

[0006] The first training dataset is input into the preset code generation model for code generation processing to obtain the generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and the failure information corresponding to each of the multiple generation failure instructions.

[0007] The instruction optimization dataset is obtained by optimizing multiple failed instructions based on the failure information using a preset instruction correction model. The instruction optimization dataset includes at least multiple failed instructions, the optimized instructions corresponding to each of the multiple failed instructions, and optimization process data. The preset instruction correction model is a model built based on Markov decision process.

[0008] The pre-set instruction optimization model is trained based on the instruction optimization dataset to obtain a trained instruction optimization model. The instruction to be optimized is then optimized using the trained instruction optimization model to obtain the target instruction.

[0009] This application also provides an instruction optimization apparatus, comprising:

[0010] The generation unit is used to input the acquired first training dataset into the preset code generation model for code generation processing to obtain the generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and the failure information corresponding to each of the multiple generation failure instructions.

[0011] The first optimization unit is used to perform instruction optimization processing on multiple failed generation instructions according to the failure information through a preset instruction correction model to obtain an instruction optimization dataset. The instruction optimization dataset includes at least multiple failed generation instructions, the optimized instructions corresponding to each of the multiple failed generation instructions, and optimization process data. The preset instruction correction model is a model built based on Markov decision process.

[0012] The training unit is used to train a pre-defined instruction optimization model based on the instruction optimization dataset to obtain a trained instruction optimization model.

[0013] The second optimization unit is used to perform instruction optimization processing on the instruction to be optimized using the trained instruction optimization model to obtain the target instruction.

[0014] This application also provides an electronic device, including: a memory for storing a computer program; and a processor for implementing the steps of any of the above-described instruction optimization methods when executing the computer program.

[0015] This application also provides a computer-readable storage medium storing a computer program, wherein the computer program, when executed by a processor, implements the steps of any of the above-described instruction optimization methods.

[0016] This application also provides a computer program product, including a computer program, wherein when the computer program is executed by a processor, it implements the steps of any of the above-described instruction optimization methods.

[0017] The instruction optimization method, apparatus, electronic device, and storage medium of this application generate a fine-tuning dataset (i.e., an instruction optimization dataset) based on a model constructed using a Markov decision process. This dataset is used to fine-tune the instruction optimization model, enabling it to optimize the instructions to be optimized. This helps users generate code that meets their requirements and allows for automatic optimization of user-input instructions. Therefore, this invention solves the technical problem of quickly identifying instructions that meet requirements to improve instruction optimization efficiency, achieving the technical effect of rapidly identifying instructions that meet requirements and improving instruction optimization efficiency. Attached Figure Description

[0018] To more clearly illustrate the embodiments of this application, the accompanying drawings used in the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0019] Figure 1 A flowchart illustrating an instruction optimization method provided in an embodiment of this application;

[0020] Figure 2 A schematic diagram illustrating the principle of an instruction optimization method provided in this application embodiment;

[0021] Figure 3 A schematic diagram of the structure of an instruction optimization device provided in an embodiment of this application;

[0022] Figure 4 A schematic diagram of another instruction-optimized device provided in an embodiment of this application. Detailed Implementation

[0023] The technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those of ordinary skill in the art without creative effort are within the protection scope of this application.

[0024] It should be noted that, in the description of this application, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. The terms "first," "second," etc., in this application are used to distinguish similar objects and are not used to describe a specific order or sequence.

[0025] To enable those skilled in the art to better understand the present application, the present application will be further described in detail below with reference to the accompanying drawings and specific embodiments.

[0026] Figure 1 This document provides a flowchart illustrating an instruction optimization method as described in an embodiment of the present application. The method is further described in detail below, taking into account the execution flow of the instruction optimization method.

[0027] like Figure 1 As shown, the method for optimizing this instruction includes:

[0028] Step 101: Input the obtained first training dataset into the preset code generation model for code generation processing to obtain the generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and the failure information corresponding to each of the multiple generation failure instructions.

[0029] In the embodiments of this application, the first training dataset refers to a high-quality code generation dataset selected from the open-source community, including but not limited to: instruction-code pairs and test cases, such as: The Mostly Basic Python Problems (MBPP) dataset that meets specific quality standards, etc. The MBPP dataset is a dataset specifically used to evaluate and improve Python code generation models. The preset code generation model is a large language model used to perform code generation tasks.

[0030] To facilitate understanding of the implementation principle of this application, a schematic diagram of an instruction optimization method is provided, as follows: Figure 2 As shown, the instruction optimization method of this application mainly includes the construction of a fine-tuning dataset (instruction optimization dataset); fine-tuning of a large model (model training of a pre-defined instruction optimization model); model testing and tool development.

[0031] The first training dataset is input into a pre-defined code generation model (e.g., a pre-trained General Language Model (GLM) or other large language models with code generation capabilities) to perform code generation processing. The code generation process refers to the pre-defined code generation model automatically outputting corresponding code snippets based on the natural language instructions (i.e., coding task descriptions) in the first training dataset. The generated results undergo a rigorous code verification process (including but not limited to semantic comparison with ground truth code and dynamic execution of test cases) to identify samples that fail verification. These failed samples constitute the generation failure dataset, which is a collection of failed instructions and their structured error information identified by the code verification process. This dataset contains two key types of information:

[0032] Failure instructions: The original natural language instructions that cause code generation errors;

[0033] Failure information: The structured record of the reasons for failure (such as: syntax error, undefined variable, logical defect, etc.), extracted through Abstract Syntax Tree (AST) analysis and exception handling mechanism, including error type, location and context code snippet.

[0034] Step 102: Based on the failure information, the multiple failed generation instructions are optimized using a preset instruction correction model to obtain an instruction optimization dataset. The instruction optimization dataset includes at least multiple failed generation instructions, the optimized instructions corresponding to each of the multiple failed generation instructions, and optimization process data. The preset instruction correction model is a model built based on Markov decision process.

[0035] In the embodiments of this application, the preset instruction correction model is a high-order large model based on a Markov decision process, used to generate instruction optimization schemes. The instruction optimization dataset is structured training data containing original failed instructions (i.e., multiple failed instructions generated), optimized instructions, and records of the optimization process.

[0036] The generated failure dataset is optimized using a pre-defined instruction correction model (an agent built on a Markov decision process). The model generates targeted modification suggestions (i.e., instruction optimization processing) based on the failure information corresponding to each failed instruction. The optimization process is iterative and evolutionary.

[0037] Status: Currently, there are multiple failed generation instructions that need to be optimized;

[0038] Actions: Instruction rewriting strategies based on failure information analysis (e.g., simplified descriptions, supplementary context, and exemplified expressions);

[0039] Reward: The percentage of code generated using the new instructions that passes tests (code pass rate) serves as a feedback signal for the optimization effect.

[0040] After multiple iterations (usually no more than five), an instruction optimization dataset is generated containing the following elements: original failed instructions, optimized instructions (high-quality new instructions that have passed verification), and optimization process data (recording intermediate instructions and their verification results during iteration, such as invalid optimized instructions that were rejected).

[0041] Step 103: Train the preset instruction optimization model based on the instruction optimization dataset to obtain a trained instruction optimization model, and then use the trained instruction optimization model to optimize the instruction to be optimized to obtain the target instruction.

[0042] In the embodiments of this application, the preset instruction optimization model is a large base model to be trained, and instruction optimization capabilities are obtained through fine-tuning. The trained instruction optimization model is the model after fine-tuning, which can automatically optimize user-input instructions. The instruction to be optimized is the original encoding task description provided by the user. The target instruction is a highly executable encoded instruction after model optimization.

[0043] Training can employ the Odds Ratio Preference Optimization (ORPO) algorithm. By comparing optimized instructions (preferred samples) with intermediate instructions (inferior samples) and generating failed instructions, the model learns the implicit rules that distinguish instruction quality, thereby acquiring instruction optimization capabilities. A well-trained instruction optimization model possesses the following core functions:

[0044] Receive user input instructions to be optimized (i.e., encoding tasks for natural language descriptions);

[0045] Automatically execute optimized instructions to generate target instructions with clearer semantics and stronger executability.

[0046] By systematically mining the value of failure samples, constructing a high-quality instruction optimization dataset, and training a dedicated model using preference optimization algorithms, the following effects can be achieved, but are not limited to:

[0047] Precise instruction defect localization: Failure information analysis based on code verification identifies ambiguity, vagueness, or logical deficiencies in instructions at their root; Closed-loop optimization capability: Markov decision processes drive iterative upgrades of instructions, ensuring that optimized instructions significantly improve code generation pass rates; Automated knowledge transfer: ORPO training enables the model to master human-preferred instruction expression paradigms, generating professional-grade optimization results without human intervention; Wide applicability: The trained model can instantly process any user-inputted instructions to be optimized, quickly outputting high-quality target instructions and significantly reducing development and debugging costs.

[0048] The instruction optimization method of this application generates a fine-tuning dataset, i.e., an instruction optimization dataset, by constructing a model based on a Markov decision process. This dataset is then used to fine-tune the instruction optimization model, enabling it to optimize the instructions to be optimized. This helps users generate code that meets their requirements and allows for automatic optimization of user-input instructions. Therefore, it solves the technical problem of how to quickly identify instructions that meet requirements to improve the efficiency of instruction optimization, achieving the technical effect of rapidly identifying instructions that meet requirements and improving the efficiency of instruction optimization.

[0049] In one possible implementation of this application embodiment, it is necessary to obtain a first training dataset in advance to complete the subsequent instruction optimization process. Regarding the acquisition of the first training dataset, the following methods may also be used, but are not limited to: performing data filtering processing from a preset database to obtain a first dataset, wherein the preset database includes multiple datasets; performing dataset partitioning processing on the first dataset according to a preset partitioning method to obtain a first training dataset and a first test dataset.

[0050] In the embodiments of this application, the preset database is a dedicated repository integrating multiple open-source code generation datasets. Multiple datasets refer to independent data sets (e.g., MBPP) stored in the preset database, each containing instructions, code, and test cases. The filtering process refers to the process of evaluating and selecting the optimal dataset based on predefined quality criteria (size, semantic relevance, etc.). The first dataset is the comprehensive optimal code generation dataset (e.g., MBPP) obtained through filtering. The preset partitioning method is a custom-defined partitioning method, such as a standard strategy of splitting the dataset according to a fixed ratio (80%-20%). The first test dataset is the independent data portion retained after partitioning for the final evaluation of the model.

[0051] By rigorously screening and partitioning, at least the following can be achieved: screening the first dataset based on multi-dimensional criteria to fundamentally ensure the reliability of input for subsequent optimization processes; enabling the system to adapt to data sources of different sizes through preset partitioning methods and supporting flexible expansion; and providing an unbiased evaluation basis for the final model performance through an independent first test dataset.

[0052] In one possible implementation of this application embodiment, when performing data filtering processing from a preset database, the following methods can also be used, but are not limited to: obtaining dataset information corresponding to each of the multiple datasets in the preset database, wherein the dataset information includes at least data size, data correlation, data complexity, testability, programming language coverage, and citation; calculating dataset scores based on data size, data correlation, data complexity, testability, programming language coverage, and citation through a first preset weight, and obtaining dataset scores corresponding to each of the multiple datasets; determining the dataset with the highest dataset score among the multiple datasets as the first dataset.

[0053] In the embodiments of this application, the first preset weight is a pre-defined set of importance coefficients for each scoring dimension. The dataset score refers to the overall quality quantification value of the dataset obtained through weighted calculation.

[0054] In data filtering and processing, a quantitative scoring mechanism can be used, but is not limited to, to select the first dataset from multiple datasets contained in the preset database. Data filtering and processing mainly involves a comprehensive evaluation of the candidate datasets through multi-dimensional dataset information. The dataset information includes the attribute information of each dataset (i.e., data size, data correlation, data complexity, testability, programming language coverage, and citation). The attribute information constitutes the basic dimensions of the scoring.

[0055] Among them, data size refers to the number of independent instruction-code pairs in the dataset, reflecting the richness of the samples. For example, the dataset size of the first dataset should be more than 100 independent instruction-code pairs to ensure the richness of the training samples. Data correlation refers to the degree of consistency between instructions and corresponding codes in terms of functional semantics, which is used to measure the semantic matching degree between natural language instructions and their corresponding truth codes. For example, the instructions and codes in the first dataset have a high degree of semantic correlation, and a Bilingual Evaluation Understudy (BLEU) score ≥ 0.4 is used as the basic threshold. Data complexity refers to the logical difficulty and structural diversity of the programming problems contained in the dataset, which is used to evaluate the diversity of programming logic covered by the dataset (such as function encapsulation, conditional branching, loop control, etc.). For example, the problem complexity of the first dataset should cover common programming logic such as function implementation, condition judgment, loop, and string processing.

[0056] Testability refers to whether the dataset provides complete and automatically executable test cases. For example, the test cases for the first dataset are complete and accurate, supporting automated verification of the correctness of the generated code. Programming language coverage refers to the range of mainstream programming languages ​​supported by the dataset (such as Python, JavaScript, etc.). For example, the first dataset covers mainstream programming languages, prioritizing task-driven languages ​​such as Python and JavaScript. Citationability refers to the extent to which the dataset is cited by authoritative literature in academic research or industrial practice. For example, the first dataset is cited in mainstream large model papers, possessing a good reputation and a broad application base.

[0057] When calculating the dataset score, a weighted calculation is performed based on the dataset's attribute information, using a first preset weight (i.e., predefined importance coefficients for each dimension). The weight allocation can follow these principles: assign higher weights to dimensions that are crucial to the code generation task (e.g., data relevance, testability); assign medium weights to dimensions that significantly affect the model's generalization ability (e.g., data complexity, programming language coverage); and assign basic weights to fundamental guarantee dimensions (e.g., data size, citation frequency).

[0058] The scores for each dimension are converted into comparable values ​​through normalization, and then a weighted sum is generated to produce a dataset score. This score objectively reflects the overall quality of the dataset in a code generation scenario. The dataset with the highest score is designated as the primary dataset (e.g., the MBPP dataset receives the highest score due to its high completeness on Python tasks). This ensures that subsequent processes are built upon the foundation of the highest quality and most representative data.

[0059] Based on a structured scoring system, subjective experience can be transformed into calculable quantitative indicators by pre-setting weights, eliminating the randomness bias of manual selection; multi-dimensional dataset information covers data availability, task adaptability, and academic reliability, ensuring the effectiveness of training data from the source; and the scoring mechanism allows for flexible adjustment of weights to adapt to different task requirements, providing a general selection paradigm for instruction optimization in other fields.

[0060] In one possible implementation of this application embodiment, when obtaining the generation failure dataset, the following methods can also be used, but are not limited to: inputting multiple first data groups from the first training dataset into a preset code generation model for code generation processing to obtain generation codes corresponding to each of the multiple first data groups, wherein each first data group includes code generation instructions, as well as ground truth code and test cases corresponding to each code generation instruction; performing code verification processing on the corresponding generation codes according to the multiple ground truth codes and multiple test cases to obtain code verification results; determining multiple verification failure codes and failure information corresponding to each of the multiple verification failure codes in the multiple generation codes according to the code verification results, and constructing a generation failure dataset through the first data groups corresponding to each of the multiple verification failure codes and the failure information.

[0061] In the embodiments of this application, the first data group is a data unit in the first training dataset. Each first data group can be organized in JavaScript Object Notation (Json) format and consists of an associated combination of code generation instructions, truth code, and test cases. The code generation instructions refer to the original natural language instructions that describe the programming task; the truth code is the correct implementation code that completely matches the instructions; and the test cases are automated test scripts used to verify the correctness of the code's functionality.

[0062] Code generation processing refers to the reasoning process by which a pre-defined code generation model outputs code fragments based on natural language instructions; generated code refers to the program code automatically generated by the pre-defined code generation model based on code generation instructions; code verification processing refers to a dual verification process that judges the correctness of the generated code through semantic comparison and test execution; the code verification result marks the verification conclusion of whether the generated code passed or failed; verified failed code is the generated code instance that failed the code verification process; failure information refers to structured data describing the reasons for code failure (at least including: error type, location, and context); the generated failure dataset centrally integrates failure instructions and their corresponding failure information.

[0063] During the code verification process, a dual verification mechanism can be used to ensure the reliability of the results:

[0064] Semantic comparison: The generated code is compared character by character with the true code. If they are completely identical, the verification is directly passed.

[0065] Dynamic test execution: When semantic comparison is inconsistent, the generated code is placed in a sandbox environment to execute the corresponding test cases. The correctness of the function is judged by exception handling and assertion (timeout, error or test failure are all regarded as verification failure). Then, the code verification results are generated, and the pass / fail status of each generated code is clearly marked.

[0066] For code generation instructions that fail verification, i.e., failed instructions, two types of key information are extracted: the failed code (the generated code instance that failed verification) and the failure information (structured records of the reasons for failure, such as: syntax error type, undefined variable location, and logical defect code segment, automatically generated through AST parsing and exception stack analysis). Finally, the original first data set (including failed instructions) and its corresponding failure information are integrated to construct a complete generation failure dataset, providing an accurate defect sample library for subsequent instruction optimization.

[0067] During the implementation of dual verification mechanisms and structured failure analysis, semantic comparison avoids misjudging code that is functionally correct but has format differences, while dynamic testing deeply examines the code's behavioral logic. The combination of these two methods ensures high accuracy in failure determination. Structured failure information not only locates the error type but also precisely points to the location and context of code defects, providing actionable diagnostic basis for subsequent instruction optimization. By eliminating the need for manual intervention throughout the entire process from code generation and verification to failure information extraction, the efficiency and scalability of generating failure datasets are significantly improved.

[0068] In one possible implementation of this application embodiment, when performing code verification processing on the generated code, the following methods can also be used, but are not limited to: performing semantic comparison processing on multiple generated codes with their respective corresponding truth codes to obtain semantic comparison results for each of the multiple generated codes; determining multiple first codes and multiple second codes based on the semantic comparison results, and determining the multiple first codes as successfully verified codes, wherein the first code is the generated code that is semantically consistent with the truth code among the multiple generated codes, and the second code is the generated code that is not semantically consistent with the truth code among the multiple generated codes; performing code testing processing on the multiple second codes through their respective corresponding test cases to obtain code test results; determining multiple failed test codes and multiple passed test codes among the multiple second codes based on the code test results, and determining the multiple passed test codes as successfully verified codes, and the multiple failed test codes as multiple verification failure codes, wherein the code test results include failure information corresponding to each of the multiple failed test codes.

[0069] In the embodiments of this application, semantic comparison processing refers to a character-by-character precise comparison process between generated code and truth code, used to quickly identify perfectly matching samples; semantic comparison result refers to a binary judgment conclusion indicating whether the generated code and truth code are completely consistent; the first code is a generated code instance confirmed to be completely correct through semantic comparison; the second code is a generated code instance that does not match the semantic comparison and needs to enter deep testing; code testing processing refers to a dynamic detection process of executing test cases to verify the code function in an isolated environment; code that fails the test refers to generated code that encounters errors, timeouts, or assertion failures during the test processing; code that passes the test refers to generated code that completely passes all test cases during the test processing.

[0070] During code verification, semantic comparison is performed first. Each generated code (i.e., the code snippet output by the preset code generation model) is compared with its corresponding ground truth code (the standard correct answer provided in the dataset). Semantic comparison can adopt the principle of character-by-character exact matching. If the two are completely consistent, it is determined that the semantics are completely matched. After semantic comparison, a semantic comparison result is generated as the basis for preliminary verification.

[0071] Based on the semantic comparison results, the generated code is divided into first code and second code. Since the first code is generated code that is completely consistent with the truth code, it is directly marked as successfully verified code; while the second code is generated code that differs from the truth code, so it needs to enter the deep verification process.

[0072] The second code can be tested using, but is not limited to, the following methods: placing the second code in a sandbox execution environment and running the corresponding test cases (automated verification scripts provided by the dataset). The testing process uses an exception monitoring and assertion checking mechanism to capture the following situations: runtime timeout (exceeding a preset safety threshold); throwing unhandled exceptions (such as syntax errors or runtime errors); and failing functional assertions in the test cases.

[0073] After the test is completed, code test results are generated, which include code that passed the test and code that failed the test. Since the code that passed the test is generated code that completely passed all test cases, it can be marked as verified successful code; while the code that failed the test is generated code that failed the test, and is therefore identified as verified failed code. The error details (such as error type, stack trace, and failure assertion location) recorded in the failure information are automatically generated through runtime diagnostics and log analysis.

[0074] By using semantic comparison in the hierarchical screening mechanism, false failure samples with different formats but equivalent functions can be quickly screened out, avoiding unnecessary test execution and significantly shortening the verification cycle; second code with inconsistent semantics but correct functions (such as variable renaming, comment addition or removal) can be accepted, and its behavioral equivalence can be verified through dynamic testing, improving the practicality of the verification mechanism; multi-dimensional failure information is generated for code that fails the test, and the defect location is accurate to the line level, providing a targeted repair basis for subsequent instruction optimization.

[0075] In one possible implementation of this application embodiment, when performing code testing, the following methods can also be used, but are not limited to: running multiple second codes through their respective corresponding test cases to obtain code running information; determining multiple codes among the multiple second codes that meet preset running conditions as multiple passing test codes based on the code running information, wherein the preset running conditions include at least running time, throw status, assertion pass status and / or running status; and determining multiple codes among the multiple second codes that do not meet the preset running conditions as multiple failing test codes based on the code running information.

[0076] In the embodiments of this application, the code execution process is a dynamic detection process in which second code is executed and corresponding test cases are run in an isolated environment; the code execution information refers to the four-dimensional monitoring data (running time / thrown status / assertion passed status / running status) generated during the test execution process; the preset running conditions are custom-set conditions, which at least include a set of technical standards for determining whether the code passes the test (e.g., it must simultaneously meet the time, exception, assertion and process requirements).

[0077] Runtime refers to the time taken for code to execute from startup to termination; timeout is considered a failure. Throw status refers to a key indicator that identifies whether the code throws an uncaught exception. Assertion pass status refers to the result of the functional verification assertion in the test case. Run status refers to a system-level flag that reflects whether the code execution process terminates normally. Code that passes the test is a second code instance that fully meets the preset run conditions. Code that fails the test is a second code instance that violates at least one preset run condition.

[0078] During code testing, the second piece of code requiring in-depth verification is placed in an isolated sandbox environment, and its corresponding test cases (automated verification scripts provided by the dataset) are executed. The code execution process is monitored and recorded in real time, including: execution time (code execution time; exceeding a preset safety threshold (e.g., 3 seconds) is considered a timeout); throw status (monitoring whether unhandled exceptions are thrown); assertion pass status (verifying whether all functional assertions in the test cases are satisfied); and execution status (capturing systemic errors such as abnormal process termination and memory overflow). Based on the above execution information, automatic judgment is made according to preset execution conditions (i.e., predefined pass criteria).

[0079] If the second code simultaneously meets the following conditions: execution is completed within a safe time (runtime compliance); no exceptions are thrown (throwing status is normal); all functional assertions are passed (assertion pass status is met); and the process exits normally (running status is stable), then the second code is classified as test-passed code.

[0080] If any condition is violated (such as timeout, assertion failure, runtime error, etc.), the code is deemed to have failed the test.

[0081] The system achieves a balance between security and reliability through four-fold state monitoring: a sandbox environment isolates risky code, and timeout control prevents malicious or infinite loop code from impacting the system; anomaly monitoring, assertion verification, and system state detection cover the full spectrum of code issues, from syntax errors to logical defects; and preset operating conditions transform abstract correctness standards into quantifiable technical indicators, giving verification results clear attribution basis.

[0082] Furthermore, regarding the process of obtaining the failed dataset, this application summarizes it as an example: The first dataset is divided for training and evaluating the model. Code generation based on a large model is performed on the first training dataset to obtain failed data pairs. Specifically, this is achieved as follows: The first dataset is divided into an 80% training set (first training dataset) and a 20% test set (first test dataset). Each sample (first data group) in the first training dataset is organized in JSON format, with fields including: instruction (natural language instruction, i.e., code generation instruction), solution (ground value code), and test_cases (test cases). Simultaneously, using the large language model as the baseline model, code generation is performed on each instruction in the training set; the output of the baseline model is a code string. In the code generation task, a deterministic strategy with a preset temperature of 0 can be used to ensure the stability of the baseline model's output. The generated code is saved in a preset format (e.g., py format) in a specified path, and generation logs are retained for subsequent analysis.

[0083] The generated code is semantically compared with the ground truth code. If the two codes are completely identical (character-by-character comparison), the generation is considered successful and no further testing is required. If they are inconsistent, the corresponding test cases are executed to test whether the functionality of the generated code meets the standards. Testing can be automated using Python functions combined with exception handling mechanisms, and the pass / fail status of all test cases is recorded. Specifically, each test sample is dynamically injected into a Python sandbox environment for execution, with a maximum runtime of no more than 3 seconds. Timeouts, exceptions, or failed assertions are all considered failed generation. Code that fails test cases or malfunctions will be marked as failed generation samples and used as basic data for subsequent instruction optimization.

[0084] In one possible implementation of this application embodiment, when performing instruction optimization processing on multiple failed generation instructions, the following methods can also be used, but are not limited to: The multiple failed generation instructions are optimized according to the failure information using a preset instruction correction model to obtain multiple first optimized instructions; these first optimized instructions are then input into a preset code generation model for code generation processing to obtain first optimized code corresponding to each of the multiple first optimized instructions; the multiple first optimized codes are then tested using their respective test cases to obtain a first code pass rate, where the first code pass rate is the probability that the first optimized code among the multiple first optimized codes meets the preset running conditions; if the first code pass rate is less than a preset pass rate threshold, the multiple first optimized instructions are optimized using the preset instruction correction model to obtain multiple second optimized instructions; the multiple second optimized codes generated by the multiple second optimized instructions are then tested using their respective test cases to obtain a second code pass rate; this process continues until the code pass rate of the optimized instructions corresponding to the multiple failed generation instructions is greater than or equal to the preset pass rate threshold, or the number of times the multiple failed generation instructions are optimized reaches a preset optimization number threshold, to obtain an instruction optimization dataset.

[0085] In the embodiments of this application, the first optimization instruction refers to the new instruction generated after the first optimization of the failed generation instruction; the first optimized code refers to the code segment generated driven by the first optimization instruction. The first code pass rate refers to the proportion of code generated after the first round of optimization that passes the test. The preset pass rate threshold is a custom-set threshold used to determine the minimum pass rate standard (e.g., 100%) for whether the optimization is up to standard.

[0086] The second optimization instruction is the deep optimization instruction generated when the first round fails to meet the target; the second code pass rate refers to the proportion of code that passes the test after the second round of optimization; the preset optimization number threshold is a custom-set threshold, which refers to the maximum allowed number of optimization iterations (e.g., 5 times).

[0087] When constructing the instruction optimization dataset, a preset instruction correction model is used to perform initial optimization on the generated failed instructions based on failure information (including code defect type, location, and context), generating the first optimized instruction (i.e., the initially rewritten instruction). The first optimized instruction is then input into the preset code generation model to execute code generation, resulting in the corresponding first optimized code.

[0088] Subsequently, code testing is performed on the first optimized code (running original test cases in a sandbox environment), and its first code pass rate, i.e., the percentage of first optimized code that passed the test (e.g., pass rate = number of passed samples / total number of samples). If the first code pass rate is lower than a preset pass rate threshold, it indicates insufficient optimization, and a second optimization needs to be initiated: the instruction correction model generates a second optimized instruction (a deeply improved version of the instruction) based on the previous results; the second optimized code is generated and tested again, and the second code pass rate is calculated. Iteration continues until the termination condition is met: the code pass rate of the current round of optimized instructions is ≥ the preset pass rate threshold; or the cumulative number of optimizations reaches the preset optimization number threshold (e.g., a maximum of 5 times). Finally, the results of multiple rounds of optimization are integrated to form an instruction optimization dataset.

[0089] The optimized dataset should include at least: the original failed generation instructions and the optimization instructions for each round; the generation code and test results corresponding to each round of optimization instructions; and the final optimization instructions that achieve the pass rate target or reach the iteration limit.

[0090] The process of determining the instruction optimization dataset can achieve, but is not limited to, the following effects: a pass rate threshold ensures that the instruction quality is fundamentally up to standard, avoiding shallow optimization; an upper limit on the number of iterations prevents unlimited resource consumption, balancing performance and efficiency; complex instruction defects (such as logical ambiguity and missing context) are gradually corrected through multiple rounds of optimization; each iteration utilizes information from previous failures for targeted improvement. The final optimized instruction set has a significantly higher pass rate than the original instructions; and the dataset covers the complete optimization path from failure to success, providing high-quality positive and negative samples for model training.

[0091] In one possible implementation of this application embodiment, when performing instruction optimization processing on multiple failed generation instructions to determine multiple first optimized instructions, the following methods can also be used, but are not limited to: inputting failure information into a preset instruction correction model for information extraction processing to obtain error information corresponding to each of the multiple failed generation instructions, wherein the error information includes at least error type, error location, and failure code; performing optimization information generation processing based on the error information using the preset instruction correction model to obtain instruction optimization information corresponding to each of the multiple failed generation instructions, wherein the instruction optimization information includes at least modification actions corresponding to each of the multiple failed generation instructions; performing instruction modification processing on the multiple failed generation instructions according to their respective modification actions to obtain multiple first optimized instructions, wherein the modification actions are instruction optimization methods generated based on error type, error location, and failure code.

[0092] In the embodiments of this application, information extraction processing refers to a structured analysis process that automatically extracts error types, locations, and code fragments from failure information. Error information consists of three standardized elements describing code defects (type / location / failed code). Optimization information generation processing refers to an intelligent decision-making process that derives instruction modification strategies based on error information. Instruction optimization information is a set of optimization schemes containing specific modification actions. Modification actions refer to corrective operations on instruction defects (e.g., semantic supplementation, redundancy reduction, etc.). Instruction modification processing refers to the conversion process of applying modification actions to the original instruction (generating failed instructions) to generate optimized instructions.

[0093] During the generation of the first optimization instruction, failure information (including raw diagnostic data recorded during the code verification phase) is input into the preset instruction correction model. The preset instruction correction model performs information extraction processing, extracting standardized error information from the failure information. The error information includes at least the following: error type: identifying the nature of the defect (e.g., syntax error, undefined variable, logical contradiction, etc.); error location: locating the specific line number or function block of the defect in the code; and failed code: marking the original text of the code segment containing the defect.

[0094] Based on the error information, the preset instruction correction model initiates optimization information generation and processing, analyzes the root cause of the error, and generates targeted instruction optimization information. The core of the instruction optimization information is the modification action, i.e., the correction strategy targeting the defects of the original instruction, specifically including but not limited to:

[0095] Semantic supplementation: Add explicit context to ambiguous descriptions (e.g., supplement input and output examples); Redundancy reduction: Remove irrelevant descriptions that interfere with the model's understanding; Terminology standardization: Convert colloquial expressions into standard programming terms; Logical explicitation: Convert implicit conditions into explicit constraints (e.g., explicit loop boundaries).

[0096] The modifications are applied to the corresponding failed instructions, and the instruction modification process generates the first optimized instruction. The instruction modification process is not a simple replacement, but rather a semantic reconstruction combining error type and failed code: if the error type is an undefined variable, the variable scope is explicitly declared in the instruction; if the error location points to loop logic, the description of the iteration condition is strengthened; if the failed code exposes missing boundary handling, an exception scenario description is added to the instruction.

[0097] By precisely mapping error locations to corresponding instruction descriptions, defect tracing is achieved; by driving modification actions through failed code snippets, targeted optimization is ensured. Modification actions not only fix surface-level errors but also reconstruct the underlying logic of the instructions; professional-grade optimization solutions are generated based on a programming knowledge base using a high-order model. Each optimization instruction can be traced back to its specific error type and modification action, providing developers with a transparent instruction optimization path.

[0098] In one possible implementation of this application embodiment, when performing code testing on multiple first optimized codes to determine the first code pass rate, the following methods can also be used, but are not limited to: performing code testing on multiple first optimized codes through their respective corresponding test cases to obtain a first test result; determining the first pass code among the multiple first optimized codes based on the first test result, wherein the first pass code is the first optimized code that meets the preset running conditions; obtaining a first number of first optimized codes and a second number of first pass codes, and performing a division operation between the second number and the first number to obtain the first code pass rate.

[0099] In the embodiments of this application, the first pass code is a first optimized code instance that fully meets the preset running conditions in the test process; the first quantity is the total number of samples of the first optimized code participating in the test; the second quantity is the number of qualified samples of the first optimized code that pass the test (i.e. the number of first pass codes); wherein, the first code pass rate = the second quantity / the first quantity.

[0100] When calculating the first code pass rate, based on the first test results, code samples that meet preset running conditions (including runtime compliance, no exceptions thrown, all assertions passing, etc.) are automatically selected and classified as the first pass code (i.e., optimized code instances with completely correct functionality). Then, the pass rate calculation process is initiated.

[0101] The total amount of code optimized in the first round is counted (first quantity); the number of code that passed the first round is counted (second quantity); the pass rate of the first code is calculated by division (i.e., the second quantity divided by the first quantity). The pass rate of the first code objectively reflects the overall success rate of the first round of instruction optimization.

[0102] The pass rate provides real-time feedback on the effectiveness of instruction optimization, avoiding subjective evaluation bias and providing an objective basis for deciding whether to initiate deep iterations. Test results directly drive pass rate calculation, forming a complete chain of generation-validation-metrics; each first pass code confirms that it provides high-quality positive samples for subsequent model training. Low pass rates expose instruction batches with insufficient optimization, guiding resources to focus on areas requiring re-optimization; high pass rate samples can terminate iterations early, improving the overall efficiency of the system.

[0103] In one possible implementation of this application embodiment, when performing instruction optimization processing on multiple first optimization instructions, it can also be implemented in the following ways, but not limited to: obtaining first test failure information, wherein the first test failure information includes at least test information of first optimization code that does not meet preset running conditions; marking the first optimization instruction corresponding to the first optimization code that does not meet preset running conditions as optimization failure instruction based on the first test failure information; and performing instruction optimization processing on the optimization failure instruction according to the first test failure information through a preset instruction correction model to obtain multiple second optimization instructions.

[0104] In the embodiments of this application, the first test failure information includes complete diagnostic data of the test code that failed in the first round of optimization (i.e., the first optimized code that does not meet the preset running conditions), specifically covering: the exception type and stack trace thrown at runtime; the content of the failed assertion and the comparison between expected and actual values; and the system log of timeout or abnormal process termination.

[0105] Based on the first test failure information, the associated first optimization instruction (the instruction corresponding to the code that failed the test) is automatically marked as an optimization failure instruction. The marking process follows the principle of causal association: if the failure information reveals an error in the variable scope, the paragraph in the instruction with ambiguous variable description is marked; if a missing boundary condition is detected, the undefined constraint condition in the instruction is located; if a logical contradiction is exposed, the ambiguous task description in the instruction is traced.

[0106] The preset instruction correction model receives optimization failure instructions and their corresponding first test failure information, and performs deep instruction optimization processing: it expands the error context by combining the first test failure information; it adjusts the aggressiveness of the modification action (e.g., it uses semantic reconstruction instead of local correction for stubborn defects); and it generates a second optimization instruction to resolve the first test failure information.

[0107] By focusing on failures, optimization resources are precisely allocated. Deep optimization is initiated only for persistent instruction defects unresolved in the first round, avoiding redundant global calculations. Failure information pinpoints specific defective sections, enabling instruction-level targeted repair. First-round test failure information reveals deep-seated problems not covered by the initial optimization (e.g., implicit logical contradictions). Instructions generated by secondary optimization significantly improve the probability of meeting code pass rate thresholds, providing a definitive solution for complex instruction defects.

[0108] In one possible implementation of this application embodiment, when obtaining the instruction optimization dataset, the following methods can also be used, but are not limited to: When the code pass rate of the optimized instructions corresponding to each of the multiple failed generation instructions is greater than or equal to a preset pass rate threshold, the dataset is constructed using a preset data format based on the multiple failed generation instructions, the optimized instructions corresponding to each of the multiple failed generation instructions, and the optimization process corresponding to each of the multiple failed generation instructions, to obtain the instruction optimization dataset. Here, the optimized instructions are all successfully optimized instructions, and the code generated by the successfully optimized instructions meets preset running conditions. When the code pass rate of the optimized instructions corresponding to each of the multiple failed generation instructions is less than the preset pass rate threshold, instruction optimization processing continues for the multiple failed generation instructions, and the number of times instruction optimization is performed on the multiple failed generation instructions is determined. When the number of times instruction optimization is performed on the multiple failed generation instructions reaches a preset optimization number threshold, the dataset is constructed using a preset data format based on the multiple failed generation instructions, the first instructions corresponding to each of the multiple failed generation instructions, and the optimization process data corresponding to each of the multiple failed generation instructions, to obtain the instruction optimization dataset. Here, the first instructions include both successfully optimized instructions and successfully optimized instructions, and the code generated by the successfully optimized instructions does not meet preset running conditions.

[0109] In the embodiments of this application, a successful optimization instruction refers to an optimization instruction that ultimately generates code that completely passes the test (meets preset running conditions); a failed optimization instruction refers to an instruction that fails to generate compliant code even after reaching the iteration limit; the first instruction is a set of intermediate instructions generated during the iteration process (including successful / failed samples); the preset data format is a custom-set data format used for structured dataset organization specifications (e.g., Alpaca format); and the optimization process data is a historical log recording instruction modification actions, test results, and error feedback.

[0110] When all failed commands have been iteratively optimized, and the code pass rate (i.e., test pass rate) of the corresponding optimized commands reaches or exceeds a preset pass rate threshold, the optimization goal is considered achieved. At this point, dataset construction is performed: data elements include the original failed commands, the finally successful optimized commands (whose generated code fully conforms to the preset running conditions), and complete optimization process data (including historical records such as modification actions and test results for each round). The data is organized according to a preset data format (e.g., Alpaca four-tuple format) to generate a high-quality command optimization dataset, where all optimized commands are verified and valid optimization results.

[0111] If, after iterative optimization, the code pass rate of the corresponding optimized instructions does not reach the preset pass rate threshold, but the number of optimizations has reached the preset number of optimizations threshold, then the iteration is forcibly terminated and a dataset is constructed: the original generated failed instructions are retained; the first instructions generated in each round are summarized (including some successfully optimized instructions and those that still failed); and the complete optimization process data is recorded (highlighting the persistent defects of the failed instructions). Similarly, an instruction optimization dataset is generated according to the preset data format. At this time, the dataset contains a complete optimization trajectory with a mixture of positive and negative samples.

[0112] The construction of instruction optimization datasets can achieve, but is not limited to, the following effects: Datasets produced in qualifying scenarios contain clean, successfully optimized instructions, providing high-quality positive samples for model training; terminated scenarios retain failed optimization samples, forming valuable negative examples to enhance the model's defect identification capabilities. Pass rate thresholds ensure timely output of high-quality instructions, avoiding excessive iteration; maximum number of iterations forcibly terminates inefficient optimizations, ensuring real-time response capabilities. Complete data recording of the instruction evolution path during the optimization process provides a basis for tracing the source of errors in subsequent model diagnosis; mixed datasets reveal optimization bottlenecks caused by complex instruction defects, guiding the direction of continuous algorithm improvement.

[0113] Furthermore, regarding the construction process of the instruction optimization dataset, this application summarizes it as an example: For each failed data pair, a failed dataset is generated. Based on a Markov decision process (preset instruction correction model), an instruction optimization dataset containing a large number of failed instructions and optimized instructions is constructed. The instruction optimization dataset can be composed of quadruples. The parameters of the preset instruction correction model are as follows:

[0114] State: Each state corresponds to a failure instruction to be optimized, representing the original natural language encoding task input to the large language model;

[0115] Action: Each action represents a suggested modification to the instruction, with the direction of modification derived from error feedback during the model generation process;

[0116] Reward: Each newly generated instruction will be used to drive the code generation task, with the code pass rate (code pass rate) used as the evaluation metric. It serves as the reward signal in the Markov decision process, and its changes drive the convergence and iteration termination of the instruction optimization strategy.

[0117] Specific instructions optimize the dataset construction process, including but not limited to the following methods:

[0118] In the first training dataset, code generation is triggered using the original failure instructions, and the reasons for failure are recorded, such as syntax errors, undefined variables, and missing logic. Error information is organized in a structured format (including error type, error location, and model output code segment) and input into a pre-defined instruction correction model, requesting it to generate rewrite suggestions. Error type information is uniformly generated using AST syntax tree analysis combined with an anomaly capture mechanism, including standardized error categories such as syntax errors (SyntaxError), indentation errors (IndentationError), name errors (NameError), type errors (TypeError), and assertion errors (AssertionError), and the deepest function and variable names in the stack are extracted as context.

[0119] The preset instruction correction model optimizes the original instruction by performing modification actions, including but not limited to adjusting verb wording, simplifying redundant descriptions, supplementing contextual semantics, and providing exemplified expressions. The generated new instruction will be used as a new state and input back into the preset code generation model for code generation and testing. If the preset instruction correction model provides multiple candidate solutions when generating a new instruction, it will default to selecting the first one as the result of the current action.

[0120] The pre-defined code generation model uses new instructions to generate code and repeats the verification process. If all the code passes the test (pass rate 1), a positive reward is given and the code is recorded in the instruction optimization dataset. If the code still fails, the optimization is iterated again. The entire optimization process continues for a maximum of a pre-defined number of optimization iterations. Each new instruction generated in each round is recorded, and the instruction optimization dataset is ultimately generated.

[0121] Finally, the instruction optimization dataset is constructed, which can be a quadruple containing four items. These items can include request information, the input instruction to be optimized (i.e., the instruction that failed to be generated), the instruction that failed to be optimized and / or the instruction that succeeded, and optimization process data.

[0122] In one possible implementation of this application embodiment, when training the preset instruction optimization model, the following methods can also be used, but are not limited to: training the preset instruction optimization model according to the instruction optimization dataset and the preset model optimization strategy to obtain the trained instruction optimization model, wherein the preset model optimization strategy includes at least a preset learning rate adjustment strategy, a preset batch size, a preset optimizer, a preset number of iterations, a preset loss function, and a preset weight allocation strategy.

[0123] In the embodiments of this application, the preset model optimization strategy is a custom-configured optimization strategy, which at least covers the configuration schemes of training hyperparameters such as learning rate, batch size, and optimizer. The preset learning rate adjustment strategy is a custom-configured adjustment strategy, which at least includes a dynamic scheduling mechanism for the learning rate, including learning rate warm-up and cosine decay. The preset batch size is the number of samples in a single forward / backward propagation (e.g., 32). The preset optimizer refers to the parameter update algorithm used for training, such as the AdamW optimizer (Adaptive Moment Estimation with Weight Decay, AdamW).

[0124] The preset number of iterations refers to the maximum training epoch threshold (e.g., 10 epochs). The preset loss function is the objective function for model training optimization (e.g., the Optimizing Rationales via Policy Optimization (ORPO) algorithm). The preset weight allocation strategy refers to the rule for dynamically adjusting the loss weights based on the sample difficulty.

[0125] When training the preset instruction optimization model, the instruction optimization dataset is used as the basic input. Multiple strategies from the preset model optimization strategy are used to collaboratively control the training process, including at least the following: The preset learning rate adjustment strategy employs a phased dynamic scheduling mechanism. In the initial phase, linear warm-up is performed to increase the learning rate from zero to an initial value (e.g., 3e-5). The warm-up period accounts for 10% of the total training steps. Subsequently, a cosine decay strategy is used to smoothly decrease the learning rate with each training round, effectively balancing the model's convergence speed and stability. The preset batch size is set to a fixed number (e.g., 32), combined with gradient accumulation technology (accumulation steps set to 4) to simulate the training effect of larger batches, improving hardware resource utilization. The optimizer uses the AdamW algorithm, which enhances regularization by decoupling the weight decay mechanism. The preset number of iterations is set to a fixed number of rounds (e.g., 10 rounds), and an early stopping mechanism is embedded, which terminates training immediately when the model's performance on the validation set does not improve for two consecutive rounds to avoid invalid computation. The preset loss function adopts the odds ratio preference optimization algorithm (ORPO algorithm), which drives the model to learn preference features by comparing the difference between successful and unsuccessful optimization instructions. The preset weight allocation strategy dynamically adjusts the loss weights according to the sample difficulty: the weights of difficult samples that caused the model to make incorrect judgments in the previous round are automatically increased, while the weights of simple samples are correspondingly decreased, forcing the model to focus on strengthening weak links.

[0126] During training, the pre-defined instruction optimization model undergoes multiple iterations according to a pre-set optimization strategy. After each training round, the model performance is evaluated using an independent validation set; if no early stopping condition is triggered, the model proceeds to the next training round. Through the synergistic effect of the ORPO loss function's preference alignment capability and dynamic weight allocation, the model gradually masters the optimization patterns of natural language instructions, such as learning to convert vague verbs into precise programming terms and supplementing missing boundary condition declarations. The final trained instruction optimization model possesses automated instruction reconstruction capabilities, significantly improving the accuracy and executability of the user's original instructions.

[0127] Through multi-dimensional synergy of pre-defined model optimization strategies, training efficiency and model performance are simultaneously improved. The cosine decay learning rate strategy ensures smooth convergence during training, avoiding oscillations caused by sudden changes in the learning rate; the dynamic weight allocation mechanism significantly improves the model's generalization ability to handle complex instruction defects by strengthening the learning weights of difficult samples; the early stopping mechanism and gradient accumulation technique minimize computational consumption while ensuring accuracy. In particular, the ORPO algorithm, as the core loss function, deeply embeds human preference knowledge into the model parameters, making the generated optimization instructions more aligned with the programmer's thought process. The overall strategy system forms a closed-loop optimization chain from data to model, laying a high-performance foundation for subsequent tool deployment.

[0128] In one possible implementation of this application embodiment, when adjusting the model parameters of a preset instruction optimization model, the following methods may also be used, but are not limited to: adjusting the model parameters of the preset instruction optimization model using an instruction optimization dataset to obtain a first instruction optimization model, and performing model testing on the first instruction optimization model based on a first test dataset to obtain a first model test result; adjusting the model parameters of the first instruction optimization model based on the first model test result to obtain a second instruction optimization model, and performing model testing on the second instruction optimization model based on the first test dataset to obtain a second model test result; if the first model test result and the second model test result meet a preset stopping condition, determining the second instruction optimization model as a trained instruction optimization model; if the first model test result and the second model test result do not meet the preset stopping condition, adjusting the model parameters of the second instruction optimization model based on the second model test result until the number of model parameter adjustments reaches a preset number of iterations, thereby obtaining a trained instruction optimization model.

[0129] In the embodiments of this application, model parameter adjustment processing refers to the training process of updating model weights according to a preset model optimization strategy (e.g., gradient backpropagation). The first instruction optimization model is an intermediate model version generated after the first round of parameter adjustment. Model testing processing refers to the process of evaluating the model's ability to optimize instructions using a first test dataset. The first model test results include at least the performance quantification metrics of the first instruction optimization model on the first test dataset. The second instruction optimization model is an improved model whose parameters are further optimized based on the first model test results. The preset stopping condition is a custom-defined threshold standard used to determine performance convergence (e.g., the lower limit of the accuracy improvement on the validation set). The preset number of iterations is a defined maximum allowed number of parameter adjustment rounds (e.g., 10 rounds). The trained instruction optimization model refers to the final output model that meets the stopping condition or reaches the iteration limit.

[0130] Based on the instruction optimization dataset, the initial model parameter tuning process is performed on the pre-defined instruction optimization model. The parameter tuning process can employ preset optimization strategies such as learning rate adjustment, batch size, and ORPO loss function to generate a preliminary optimized first instruction optimization model. Subsequently, the first instruction optimization model is tested using a separately reserved first test dataset (i.e., the 20% test dataset in the first dataset partition).

[0131] The model testing process includes at least the following: inputting the original failed instruction into the first instruction optimization model (generating the failed instruction), evaluating the pass rate of the code generation after generating the optimized instruction, and recording it as the first model test result (e.g., the code pass rate of the optimized instruction is 72%).

[0132] If the first model's test results do not meet expectations, feedback-driven re-optimization is initiated. This involves adjusting training parameters (e.g., increasing the weight of difficult samples) based on weaknesses identified in the first model's test results (e.g., poor optimization of loop logic instructions), and implementing a second round of model parameter adjustment to generate a second optimized instruction model. The model is then validated again using the same first test dataset to generate the second model's test results. At this point, it is determined whether the first and second model's test results meet preset stopping conditions (e.g., the performance improvement on the validation set is less than 5% for two consecutive rounds, or the pass rate of optimized instructions reaches 100%). If the conditions are met, the training process is immediately terminated, and the second optimized instruction model is output as the final trained optimized instruction model. If not, iteration continues until the cumulative number of model parameter adjustments reaches a preset iteration limit (e.g., a maximum of 10 rounds), at which point the current optimal model is forcibly output as the training result.

[0133] Through a closed-loop training-testing feedback mechanism, model parameter optimization becomes dynamically adaptive. Each round of parameter adjustments is based on targeted improvements made to address weaknesses revealed by previous tests; for example, training weights are increased for models with weak data type handling. Intelligent determination of preset stopping conditions effectively avoids overfitting risks, automatically terminating training when model performance stabilizes, thus saving computational resources. Hard constraints on the number of iterations ensure a usable model is output with limited resources. The iterative validation strategy ensures that the final model combines superior performance with high training efficiency, providing a highly reliable core for automated instruction optimization tools.

[0134] In one possible implementation of this application embodiment, when performing model testing on the first instruction optimization model, the following methods can also be used, but are not limited to: extracting data from the first test dataset using a preset few-sample prompting strategy to obtain a first dataset, and dividing other datasets into a second dataset and a third dataset, wherein the other datasets are datasets in the first test dataset excluding the first dataset; inputting the third dataset into the first instruction optimization model for instruction optimization processing to obtain an optimized third dataset; inputting the first dataset, the second dataset, and the optimized third dataset into a preset code generation model for code generation processing, and performing code testing through test cases to obtain the first test pass rate corresponding to the first dataset, the second test pass rate corresponding to the second dataset, and the third test pass rate corresponding to the optimized third dataset; if the third test pass rate is greater than the first test pass rate and the second test pass rate, then the first model test result is determined to be a positive result; if the third test pass rate is not greater than the first test pass rate and / or the second test pass rate, then the first model test result is determined to be a negative result.

[0135] In the embodiments of this application, the preset few-shot suggestion strategy is a custom-defined strategy, referring to the methodology of selecting high-quality instruction samples to construct a benchmark control group. The first dataset is a set of high-quality instruction samples extracted using the few-shot strategy. Other datasets are the remaining samples in the first test dataset excluding the first dataset. The second dataset refers to the original unoptimized instruction subset divided from the other datasets. The third dataset refers to the experimental group subset composed of instructions to be optimized from the other datasets. Optimizing the third dataset refers to the instruction set of the third dataset after model optimization.

[0136] The first test pass rate is the code test pass rate for the few-shot hint group (dataset 1). The second test pass rate is the code test pass rate for the original instruction group (dataset 2). The third test pass rate is the code test pass rate for the optimized instruction group (optimized dataset 3). A positive result indicates that the optimized group outperforms both the few-shot group and the original group. A negative result indicates that the model performance does not meet the dominance criterion.

[0137] By employing a pre-defined few-sample suggestion strategy, high-quality instruction samples were extracted from the independently retained first test dataset to form the first dataset (e.g., selecting 10 fully functional and clearly expressed instruction-code pairs), thus constructing a reference group for code generation capabilities. The remaining test data (i.e., other datasets) were evenly divided into a second dataset (original unoptimized instructions) and a third dataset (instructions to be optimized), forming three sets of control samples.

[0138] The third dataset is input into the first instruction optimization model for instruction optimization processing to generate the semantically reconstructed optimized third dataset; the three datasets (first dataset, second dataset, and optimized third dataset) are input into the preset code generation model to execute code generation; the generated code is automatically verified through unified test cases, and the following are recorded: first test pass rate (benchmark pass rate of the few-sample prompt group); second test pass rate (comparison pass rate of the original instruction group); third test pass rate (performance metric of the optimized instruction group).

[0139] The judgment process can adopt the relative advantage principle: if the pass rate of the third test is higher than the pass rate of the first test and the pass rate of the second test, then the optimized model is confirmed to have significantly improved the code generation quality, and the result of the first test is judged as a positive result; conversely, if the optimized instructions do not exceed the baseline level of the few-sample prompt group, or are not significantly better than the original instruction group, then the result is judged as a negative result, indicating that the model needs further optimization.

[0140] By employing a three-tiered evaluation mechanism, the limitations of traditional single-indicator comparisons are overcome. An industry best practice baseline (first dataset) is constructed by introducing a few-sample prompt strategy, while a control group of original instructions (second dataset) is retained, providing a comprehensive reference system for validating the optimized model's capabilities. The optimized group must surpass both the selected prompts and the original input, eliminating the possibility of randomness in the evaluation results. When the pass rate of the optimized instruction group is higher than both the best-in-class natural language expression and the unoptimized instructions, the effectiveness and universality of the model's optimization strategy can be rigorously demonstrated, providing a reliable basis for the direction of model iteration.

[0141] In one possible implementation of this application embodiment, when adjusting the model parameters of the first instruction optimization model, the following methods can also be used, but are not limited to: determining multiple optimization anomalies in the third dataset based on the test results of the first model, wherein the code generated by the optimization anomalies does not meet preset running conditions; performing weight allocation processing in a preset loss function based on a preset weight allocation strategy, according to the multiple optimization anomalies, to obtain an updated loss function; if the test result of the first model is determined to be a positive result, adjusting the model parameters of the first instruction optimization model according to the updated loss function and the preset model optimization strategy to obtain a second instruction optimization model; if the test result of the first model is determined to be a negative result, updating the preset model optimization strategy in response to the received update information to obtain an updated preset model optimization strategy, and adjusting the model parameters of the first instruction optimization model according to the updated loss function and the updated preset model optimization strategy to obtain a second instruction optimization model.

[0142] In the embodiments of this application, optimized abnormal data refers to instruction samples whose code, even after model optimization, still does not meet the preset operating conditions. The preset weight allocation strategy is a rule system that dynamically adjusts the loss weights based on the sample defect type. The updated loss function refers to the ORPO loss function after weighted correction based on abnormal data. The model parameter adjustment process is a training process that updates the model weights based on backpropagation of the loss function. Update information refers to optimization strategy adjustment suggestions (e.g., hyperparameter changes, algorithm replacement). The strategy update process refers to a deep correction process of the preset model optimization strategy. The updated preset model optimization strategy is a training configuration scheme that incorporates new rules.

[0143] After the performance test of the first instruction optimization model is completed, a differentiated parameter adjustment strategy is implemented based on the first test results. First, the optimization abnormal data in the third dataset is located and optimized through the first test results. That is, instruction samples that still generate code that does not meet the preset running conditions after model optimization are fundamentally caused by the instruction semantics not being fully corrected or new defects being introduced.

[0144] A pre-defined weighting strategy is then implemented to dynamically increase the weight coefficients of outlier data in the pre-defined loss function (ORPO algorithm) based on the distribution characteristics of the outlier data in the training set. For example, for frequently occurring boundary condition missing errors, the loss weight of the corresponding samples is increased by 50%, resulting in a more targeted update loss function. This dynamic weighting allows the model to focus on learning the weaker areas in the next round of training.

[0145] If the first test result is positive (i.e. the optimized instruction set (optimized third dataset) surpasses both the few-sample benchmark set (first dataset) and the original instruction set (second dataset)), it proves that the current model has optimization effectiveness. The model parameter adjustment process is then performed on the first instruction optimization model by directly updating the loss function and combining it with the existing preset model optimization strategy to generate an enhanced second instruction optimization model.

[0146] If the test result is not positive, deep tuning is triggered. This involves receiving external input or algorithm-generated updates (such as changes to the learning rate decay scheme or optimizer switching suggestions), and updating the preset model optimization strategy based on these updates. For example, replacing the AdamW optimizer with the Evolving the Optimization for Deep Learning (LION) optimizer, or adjusting the learning rate warm-up ratio from 10% to 15%, generates an updated preset model optimization strategy. Subsequently, the updated loss function and the new strategy execution parameters are combined to form a second-instruction optimization model with upgraded capabilities.

[0147] By accurately capturing and redistributing weights from outlier data, the model can quickly identify its own shortcomings. Lossless optimization under positive results ensures continuous performance improvement, while policy updates triggered by non-positive results provide a systematic corrective capability. This hierarchical response mechanism leverages the efficiency advantages of automated optimization and breaks through local performance bottlenecks through dynamic policy adjustments, significantly enhancing the model's ability to correct complex instruction defects and giving the training process the vitality of self-diagnosis and continuous evolution.

[0148] Furthermore, regarding the training process of the preset instruction optimization model, this application summarizes it as an example: During the model training phase, this invention employs the Odds Ratio Preference Optimization (ORPO) algorithm and the Low-Rank Adaptation (LoRa) method for fine-tuning the large model. The training process uses a structured dataset in the format of four tuples, namely the instruction optimization dataset. The instruction optimization dataset contains at least instructions, inputs, optimized instructions (chosen), and rejected instructions, thereby accurately guiding the model on how to generate outputs that meet user needs when faced with similar code generation tasks.

[0149] The fine-tuning training process involves multiple iterations. After each round of fine-tuning, the first test dataset is input into the fine-tuned model to evaluate its performance. During testing, 20% of the first dataset is used as the test set to verify the model's ability to automatically optimize instructions through numerous test instances, including improving the accuracy of code generation and its ability to align with user needs. The evaluation involves comparative analysis: First, the pass rate of the code generated by directly inputting the initial, unoptimized instructions into the benchmark model is recorded. Second, a few-shot hint strategy is employed, selecting a small number of typical, high-quality instructions and their corresponding code as examples for the pre-defined code generation model to reference before inputting the original instructions; the pass rate of this second instance is recorded. Finally, the instructions optimized by the trained instruction optimization model are input into the pre-defined code generation model, and the pass rate of this second instance is recorded. By comparing the pass rates of these three types of instructions (initial instructions, few-shot hint instructions, and optimized instructions) on the code generation task, the performance of the trained instruction optimization model in instruction optimization can be clearly demonstrated.

[0150] If the pass rate of the optimized instruction-generated code is significantly higher than that of the initial instruction and the few-sample prompt instruction, it proves that the fine-tuning effect is good. Adjust the fine-tuning parameters based on the test results and proceed to the next round of fine-tuning. Specific fine-tuning strategies include the following aspects: Learning rate adjustment, with an initial learning rate set to 3e−5. A learning rate warm-up and cosine decay strategy is adopted. In the early stages of training, the learning rate increases linearly from 0 to the preset initial learning rate; this warm-up process accounts for 10% of the total training steps. Afterward, the learning rate uses a cosine decay strategy until the end of training. Regarding sample weight allocation, the weights of samples are dynamically adjusted based on their performance in the previous evaluation round. For difficult samples that caused model errors or poor optimization in the previous test, their weight in the loss function calculation is increased, making the model pay more attention to these difficult instances in subsequent training. For simple samples that can be handled well, their weight is reduced. The batch size is chosen to be 32. The optimizer was AdamW, the number of iterations was set to 10, and an early stopping strategy was used. After each training round, the model performance was evaluated on the validation set. If the model's performance on the validation set did not improve for two consecutive rounds, the training was terminated early, and the model that performed best on the validation set was selected.

[0151] To effectively utilize hardware resources, gradient accumulation techniques can be employed, with a cumulative step count of 4, achieving an effect equivalent to training with larger batches. Simultaneously, to prevent overfitting, a dropout rate parameter is used during training, set to 0.1. Through iterative optimization, the model can gradually improve its ability to optimize instructions. After fine-tuning, the generated model exhibits significantly enhanced instruction optimization performance in code generation tasks, providing more accurate optimization suggestions.

[0152] In one possible implementation of this application embodiment, when performing instruction optimization processing on the instruction to be optimized using a trained instruction optimization model, the following methods can also be used, but are not limited to: deploying the trained instruction optimization model to a preset instruction optimization tool, wherein the preset instruction optimization tool includes at least an interface tool and a visualization tool; receiving the instruction to be optimized based on the interface tool, and responding to the triggered instruction optimization signal, performing instruction optimization processing on the instruction to be optimized using the trained instruction optimization model to obtain the target instruction; and performing visualization processing on the instruction to be optimized and the target instruction using the visualization tool.

[0153] In the embodiments of this application, the interface tool provides an Application Programming Interface (API) service, supporting direct calls from development environments such as code editors and automation scripts; the visualization tool is a graphical user interface that allows users to perform visual operations through a browser. Both tools share the same model kernel, ensuring consistency in optimization logic. In practical applications, the trained instruction optimization model is first embedded into the tool's backend using model merging and quantization techniques. For example, based on the Large Language Model Training & Fine-tuning Factory (LLaMA Factory), model weight fusion and lightweight compression are achieved, significantly reducing deployment resource requirements.

[0154] After the tool is deployed, users can trigger the optimization process through different methods: When using the interface tool, the original instruction to be optimized (e.g., fuzzy code generation requirement processing file data) is sent to the server as an API request parameter. The tool automatically generates an instruction optimization signal to start model processing. The trained instruction optimization model parses the semantic defects of the instruction and generates the reconstructed target instruction (e.g., supplemented to read the file and calculate the average value of the second column), which is returned to the caller through the API response. When using the visualization tool, the user enters the instruction to be optimized in the web form and clicks the optimization button to trigger the same signal. The target instruction output by the model will be displayed on the interactive interface in real time. To ensure the transparency of the optimization process, the visualization tool performs additional visualization processing functions: it displays the text comparison of the original instruction to be optimized and the target instruction side by side, and uses semantic highlighting technology to mark key optimization points (e.g., newly added input format instructions, corrected action verbs, etc.) to help users intuitively understand the optimization logic.

[0155] The tool-based deployment mechanism significantly lowers the technical barrier for users to utilize the optimized model. Interface tools achieve deep integration with the development environment through standardized APIs, allowing developers to obtain optimization suggestions without interrupting their workflow. Visualization tools provide a zero-code interactive experience, helping non-technical users quickly grasp the instruction optimization methods. Semantic highlighting and comparison features establish a visual understanding of the model optimization logic for users, enhancing the interpretability and credibility of the optimization results. The entire system, while ensuring optimization effectiveness, forms a complete closed loop from model training to industrial deployment.

[0156] In summary, the embodiments of this application can achieve the following technical effects:

[0157] 1. By using a model built on Markov decision processes, a fine-tuning dataset, known as the instruction optimization dataset, is generated to adjust the instruction optimization model. Based on this dataset, the instruction optimization model is fine-tuned, enabling it to optimize instructions and thus help users generate code that meets their requirements. This allows for automatic optimization of user-input instructions. Therefore, it solves the technical problem of quickly identifying suitable instructions to improve optimization efficiency, achieving the desired technical effect of rapidly determining the appropriate instructions and enhancing optimization efficiency.

[0158] 2. Enhancing the Automation and Intelligence of Encoding Instruction Optimization: Traditional manual generation or optimization of encoding instructions is not only time-consuming and labor-intensive, but also highly dependent on the user's experience and expertise, especially in code generation tasks where the accuracy requirements for encoding instructions are extremely high. By introducing a Markov decision process, the system automatically generates optimization datasets and performs fine-tuning based on high-order large models, thereby significantly improving the automation and intelligence of the optimization process. The system continuously analyzes error feedback and adjusts the content of input instructions, reducing the time required for manual encoding instruction optimization.

[0159] 3. Improving Instruction Quality in Code Generation Tasks: For code generation tasks, the quality of the encoded instructions directly determines the accuracy and usability of the model's output code. By fine-tuning the model using the ORPO algorithm and conducting multiple rounds of generation and testing, we ensure that it can provide more accurate and effective instructions for code generation tasks. The optimized instructions effectively improve the performance of code generation tasks.

[0160] 4. Helping beginners quickly get started with code generation tasks: Writing appropriate instructions is a challenging task for beginners, especially for specific code generation needs. The automatic optimization tool provides beginners with simple and efficient support for optimizing coding instructions. Users only need to input initial instructions, and the system will automatically generate optimization suggestions to help novice users gradually master how to write efficient code generation instructions, lowering the barrier to entry.

[0161] 5. Improved Development Efficiency: The ORPO algorithm enables fine-tuning of the model, gradually aligning the large language model with preferences and outputting coding instructions that better meet user needs. Through systematic instruction optimization, developers can quickly obtain accurate code snippets in complex code generation tasks, reducing the time and manpower costs of repetitive debugging.

[0162] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods according to the above embodiments can be implemented by means of software plus necessary general-purpose hardware platforms. Of course, they can also be implemented by hardware, but in many cases the former is a better implementation method.

[0163] Embodiments of this application also provide an instruction optimization apparatus. Figure 3 A schematic diagram of an instruction-optimized device provided in this application is shown below. Figure 3 As shown, it includes:

[0164] The generation unit 31 is used to input the acquired first training dataset into the preset code generation model for code generation processing to obtain the generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and the failure information corresponding to each of the multiple generation failure instructions.

[0165] The first optimization unit 32 is used to perform instruction optimization processing on multiple failed generation instructions according to the failure information through a preset instruction correction model to obtain an instruction optimization dataset. The instruction optimization dataset includes at least multiple failed generation instructions, the optimized instructions corresponding to each of the multiple failed generation instructions, and optimization process data. The preset instruction correction model is a model built based on Markov decision process.

[0166] Training unit 33 is used to train a preset instruction optimization model based on the instruction optimization dataset to obtain a trained instruction optimization model.

[0167] The second optimization unit 34 is used to perform instruction optimization processing on the instruction to be optimized using the trained instruction optimization model to obtain the target instruction.

[0168] In one embodiment of this application, such as Figure 4 As shown, the instruction-optimized apparatus also includes:

[0169] The filtering unit 35 is used to perform data filtering processing from a preset database to obtain a first dataset, wherein the preset database includes multiple datasets;

[0170] The partitioning unit 36 ​​is used to partition the first dataset according to a preset partitioning method to obtain the first training dataset and the first test dataset.

[0171] In one embodiment of this application, the screening unit 35 is further configured to:

[0172] Obtain dataset information corresponding to each of the multiple datasets in the preset database. The dataset information includes at least the data size, data correlation, data complexity, testability, programming language coverage, and citation.

[0173] Based on data size, data correlation, data complexity, testability, programming language coverage, and citation, dataset scores are calculated using a first preset weight to obtain the dataset scores for each of the multiple datasets.

[0174] The dataset with the highest score among multiple datasets is designated as the first dataset.

[0175] In one embodiment of this application, the generation unit 31 is further configured to:

[0176] Multiple first data sets in the first training dataset are input into a preset code generation model for code generation processing to obtain the generated code corresponding to each of the multiple first data sets. Each first data set includes code generation instructions, as well as the truth code and test cases corresponding to each code generation instruction.

[0177] Based on multiple truth codes and multiple test cases, code verification is performed on the corresponding generated code to obtain code verification results;

[0178] Based on the code verification results, determine multiple failed verification codes and their corresponding failure information from the multiple generated codes, and construct a failure dataset using the first data group corresponding to each of the multiple failed verification codes and the failure information.

[0179] In one embodiment of this application, the generation unit 31 is further configured to:

[0180] The generated codes are semantically compared with their respective truth codes to obtain the semantic comparison results for each generated code.

[0181] Based on the semantic comparison results, multiple first codes and multiple second codes are determined, and the multiple first codes are determined as verification successful codes. Among them, the first code is the generated code that is consistent with the semantic comparison of the truth code among the multiple generated codes, and the second code is the generated code that is inconsistent with the semantic comparison of the truth code among the multiple generated codes.

[0182] Multiple second code snippets are tested using their respective test cases to obtain the code test results.

[0183] Based on the code test results, multiple failed test codes and multiple passed test codes are identified from the multiple second codes. The multiple passed test codes are identified as successfully verified codes, and the multiple failed test codes are identified as multiple failed verification codes. The code test results include the failure information corresponding to each of the multiple failed test codes.

[0184] In one embodiment of this application, the generation unit 31 is further configured to:

[0185] Multiple second code snippets are executed using their respective test cases to obtain code execution information.

[0186] Based on the code execution information, multiple codes that meet the preset execution conditions among multiple second codes are identified as multiple codes that pass the test. The preset execution conditions include at least the execution time, the throw status, the assertion pass status and / or the execution status.

[0187] Based on the code execution information, multiple codes in the secondary code that do not meet the preset execution conditions are identified as multiple failed test codes.

[0188] In one embodiment of this application, the first optimization unit 32 is further configured to:

[0189] The preset instruction correction model optimizes multiple failed instructions based on the failure information to obtain multiple first optimized instructions. These first optimized instructions are then input into the preset code generation model for code generation to obtain the first optimized code corresponding to each of the first optimized instructions.

[0190] Multiple first-optimized codes are tested using their respective test cases to obtain the first code pass rate, where the first code pass rate is the probability of the first optimized code that meets the preset running conditions among multiple first-optimized codes;

[0191] If the first code pass rate is less than the preset pass rate threshold, the preset instruction correction model is used to perform instruction optimization processing on multiple first optimized instructions to obtain multiple second optimized instructions. The multiple second optimized codes generated by the multiple second optimized instructions are then subjected to code testing processing through their respective test cases to obtain the second code pass rate.

[0192] The instruction optimization dataset is obtained when the pass rate of the optimized instructions corresponding to the multiple failed instructions is greater than or equal to a preset pass rate threshold, or when the number of times the multiple failed instructions are optimized reaches a preset optimization number threshold.

[0193] In one embodiment of this application, the first optimization unit 32 is further configured to:

[0194] The failure information is input into the preset instruction correction model for information extraction and processing to obtain the error information corresponding to each of the multiple failed instructions. The error information includes at least the error type, error location, and failure code.

[0195] Based on the preset instruction correction model, the error information is used to generate optimization information to obtain instruction optimization information corresponding to each of the multiple failed instructions. The instruction optimization information includes at least the modification actions corresponding to each of the multiple failed instructions.

[0196] Multiple failed instructions are modified according to their respective modification actions to obtain multiple first optimized instructions. The modification actions are instruction optimization methods based on error type, error location, and failure code.

[0197] In one embodiment of this application, the first optimization unit 32 is further configured to:

[0198] Multiple optimized code snippets are tested using their respective test cases to obtain the first test results.

[0199] Based on the first test results, the first pass code among multiple first optimized codes is determined, wherein the first pass code is the first optimized code that meets the preset running conditions;

[0200] Obtain the first number of optimized codes and the second number of passed codes, and perform a division between the second number and the first number to obtain the pass rate of the first code.

[0201] In one embodiment of this application, the first optimization unit 32 is further configured to:

[0202] Obtain first test failure information, wherein the first test failure information includes at least test information of the first optimized code that does not meet the preset running conditions;

[0203] Based on the first test failure information, the first optimization instruction corresponding to the first optimization code that does not meet the preset running conditions is marked as an optimization failure instruction;

[0204] By using a preset instruction correction model to optimize failed instructions based on the first test failure information, multiple second optimized instructions are obtained.

[0205] In one embodiment of this application, the first optimization unit 32 is further configured to:

[0206] When the pass rate of the optimized instructions corresponding to multiple failed instructions is greater than or equal to the preset pass rate threshold, the dataset is constructed and processed according to the multiple failed instructions, the optimized instructions corresponding to the multiple failed instructions, and the optimization process corresponding to the multiple failed instructions, in a preset data format to obtain the instruction optimization dataset. The optimized instructions are all successful instructions, and the code generated by the successful instructions meets the preset running conditions.

[0207] If the pass rate of the optimized instructions corresponding to multiple failed generation instructions is less than the preset pass rate threshold, continue to optimize the multiple failed generation instructions and determine the number of times to optimize the multiple failed generation instructions.

[0208] When the number of times the generated failed instructions are optimized reaches a preset optimization number threshold, the dataset is constructed and processed according to the multiple generated failed instructions, the first instructions corresponding to each of the multiple generated failed instructions, and the optimization process data corresponding to each of the multiple generated failed instructions through a preset data format to obtain the instruction optimization dataset. The first instructions include optimization failed instructions and optimization successful instructions. The code generated by the optimization failed instructions does not meet the preset running conditions.

[0209] In one embodiment of this application, the training unit 33 is further configured to perform model training on a preset instruction optimization model according to the instruction optimization dataset and a preset model optimization strategy to obtain a trained instruction optimization model. The preset model optimization strategy includes at least a preset learning rate adjustment strategy, a preset batch size, a preset optimizer, a preset number of iterations, a preset loss function, and a preset weight allocation strategy.

[0210] In one embodiment of this application, the training unit 33 is further configured to:

[0211] The model parameters of the preset instruction optimization model are adjusted using the instruction optimization dataset to obtain the first instruction optimization model. The first instruction optimization model is then tested using the first test dataset to obtain the first model test result.

[0212] Based on the test results of the first model, the model parameters of the first instruction optimization model are adjusted to obtain the second instruction optimization model. The second instruction optimization model is then tested based on the first test dataset to obtain the test results of the second model.

[0213] If the test results of the first model and the test results of the second model meet the preset stopping conditions, the second instruction optimization model is determined to be the trained instruction optimization model.

[0214] If the test results of the first model and the second model do not meet the preset stopping conditions, the model parameters of the second instruction optimization model are adjusted according to the test results of the second model until the number of model parameter adjustments reaches the preset number of iterations, and the trained instruction optimization model is obtained.

[0215] In one embodiment of this application, the training unit 33 is further configured to:

[0216] Data is extracted from the first test dataset using a preset few-sample prompting strategy to obtain the first dataset. The other datasets are then divided into the second dataset and the third dataset. The other datasets are the datasets in the first test dataset excluding the first dataset.

[0217] The third dataset is input into the first instruction optimization model for instruction optimization processing to obtain the optimized third dataset;

[0218] The first dataset, the second dataset, and the optimized third dataset are respectively input into the preset code generation model for code generation processing, and the code is tested through test cases to obtain the first test pass rate corresponding to the first dataset, the second test pass rate corresponding to the second dataset, and the third test pass rate corresponding to the optimized third dataset.

[0219] If the pass rate of the third test is greater than the pass rates of the first test and the second test, the test result of the first model is determined to be a positive result. If the pass rate of the third test is not greater than the pass rate of the first test and / or the pass rate of the second test, the test result of the first model is determined to be a negative result.

[0220] In one embodiment of this application, the training unit 33 is further configured to:

[0221] Based on the test results of the first model, several optimization anomalies in the third dataset were identified, among which the code generated by the optimization anomalies did not meet the preset running conditions.

[0222] Based on a preset weight allocation strategy, weights are allocated in a preset loss function according to multiple optimization anomalies to obtain an updated loss function.

[0223] If the test result of the first model is determined to be positive, the model parameters of the first instruction-optimized model are adjusted according to the updated loss function and the preset model optimization strategy to obtain the second instruction-optimized model.

[0224] If the test result of the first model is determined to be a negative result, the preset model optimization strategy is updated in response to the received update information to obtain the updated preset model optimization strategy. Then, the model parameters of the first instruction optimization model are adjusted according to the updated loss function and the updated preset model optimization strategy to obtain the second instruction optimization model.

[0225] In one embodiment of this application, the second optimization unit 34 is further configured to:

[0226] The trained instruction optimization model is deployed to a preset instruction optimization tool, which includes at least an interface tool and a visualization tool.

[0227] The interface tool receives the instruction to be optimized and responds to the triggered instruction optimization signal. The trained instruction optimization model is used to optimize the instruction to be optimized to obtain the target instruction.

[0228] Visualization tools are used to visualize the instructions to be optimized and the target instructions.

[0229] For a description of the features in the embodiment corresponding to the instruction optimization apparatus, please refer to the relevant description in the embodiment corresponding to the instruction optimization method, which will not be repeated here.

[0230] Embodiments of this application also provide an electronic device, including a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to perform the steps in any of the above-described instruction-optimized method embodiments.

[0231] Embodiments of this application also provide a computer-readable storage medium storing a computer program, wherein the computer program is configured to execute the steps in any of the above-described instruction-optimized method embodiments at runtime.

[0232] In one exemplary embodiment, the aforementioned computer-readable storage medium may include, but is not limited to, various media capable of storing computer programs, such as a USB flash drive, read-only memory (ROM), random access memory (RAM), portable hard disk, magnetic disk, or optical disk.

[0233] Embodiments of this application also provide a computer program product, which includes a computer program that, when executed by a processor, implements the steps in any of the above-described instruction optimization method embodiments.

[0234] Embodiments of this application also provide another computer program product, including a non-volatile computer-readable storage medium storing a computer program, which, when executed by a processor, implements the steps in any of the above-described instruction optimization method embodiments.

[0235] Those skilled in the art will further recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of both. To clearly illustrate the interchangeability of hardware and software, the components and steps of the various examples have been generally described in terms of functionality in the foregoing description. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0236] The foregoing has provided a detailed description of the instruction optimization method, apparatus, electronic device, and storage medium provided in this application. Specific examples have been used to illustrate the principles and implementation methods of this application. The descriptions of the embodiments above are merely for the purpose of helping to understand the method and its core ideas. It should be noted that those skilled in the art can make various improvements and modifications to this application without departing from its principles, and these improvements and modifications also fall within the protection scope of the claims of this application.

Claims

1. A method for instruction optimization, characterized in that, include: The first training dataset is input into a preset code generation model for code generation processing to obtain a generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and failure information corresponding to each of the multiple generation failure instructions. The first training dataset is a code generation dataset, which includes at least instructions, code pairs and test cases. The failure information is a structured record of failure reasons, which includes at least syntax errors, undefined variables and logical defects. The multiple failed generation instructions are optimized according to the failure information by a preset instruction correction model to obtain an instruction optimization dataset. The instruction optimization dataset includes at least the multiple failed generation instructions, the optimized instructions corresponding to each of the multiple failed generation instructions, and optimization process data. The preset instruction correction model is a model built based on Markov decision process. The preset instruction optimization model is trained based on the instruction optimization dataset to obtain a trained instruction optimization model. The instruction to be optimized is then optimized using the trained instruction optimization model to obtain the target instruction.

2. The instruction optimization method according to claim 1, characterized in that, Before inputting the acquired first training dataset into a preset code generation model for code generation processing to obtain the failed generation dataset from the first training dataset, the method further includes: Data is filtered from a preset database to obtain a first dataset, wherein the preset database includes multiple datasets; The first dataset is divided according to a preset partitioning method to obtain the first training dataset and the first test dataset.

3. The instruction optimization method according to claim 2, characterized in that, The process of filtering data from a preset database to obtain the first dataset includes: Obtain dataset information corresponding to each of the multiple datasets in the preset database, wherein the dataset information includes at least data size, data correlation, data complexity, testability, programming language coverage, and citation. Based on the data size, data correlation, data complexity, testability, programming language coverage, and citation, a dataset score is calculated using a first preset weight to obtain the dataset score corresponding to each of the multiple datasets. The dataset with the highest score among the multiple datasets is determined as the first dataset.

4. The instruction optimization method according to claim 2, characterized in that, The step of inputting the acquired first training dataset into a preset code generation model for code generation processing to obtain the generation failure dataset in the first training dataset includes: Multiple first data groups in the first training dataset are input into the preset code generation model for code generation processing to obtain the generated code corresponding to each of the multiple first data groups. Each first data group includes a code generation instruction, a truth code corresponding to each code generation instruction, and the test case. Based on the multiple truth codes and multiple test cases, code verification processing is performed on the corresponding generated code to obtain code verification results; Based on the code verification results, multiple failed verification codes and corresponding failure information are determined from the multiple generated codes, and the generated failure dataset is constructed using the first data group corresponding to each of the multiple failed verification codes and the failure information.

5. The instruction optimization method according to claim 4, characterized in that, The step of performing code verification processing on the corresponding generated code based on multiple truth codes and multiple test cases to obtain code verification results includes: The generated codes are semantically compared with their respective truth codes to obtain semantic comparison results for each of the generated codes. Based on the semantic comparison results, multiple first codes and multiple second codes are determined, and the multiple first codes are determined as verification success codes. The first code is the generated code that is consistent with the semantic comparison of the truth code among the multiple generated codes, and the second code is the generated code that is inconsistent with the semantic comparison of the truth code among the multiple generated codes. The plurality of second codes are respectively subjected to code testing processing through their respective corresponding test cases to obtain code test results; Based on the code test results, multiple failed test codes and multiple passed test codes are determined from the plurality of second codes. The multiple passed test codes are determined as the verification successful codes, and the multiple failed test codes are determined as the verification failed codes. The code test results include the failure information corresponding to each of the multiple failed test codes.

6. The instruction optimization method according to claim 5, characterized in that, The step of performing code testing on the plurality of second codes through their respective corresponding test cases to obtain code test results includes: The plurality of second codes are processed by running their respective corresponding test cases to obtain code execution information; Based on the code execution information, multiple codes that meet preset execution conditions among the multiple second codes are identified as the multiple codes that pass the test, wherein the preset execution conditions include at least execution time, throw status, assertion pass status and / or execution status; Based on the code execution information, multiple codes among the multiple second codes that do not meet the preset execution conditions are identified as the multiple failed test codes.

7. The instruction optimization method according to claim 4, characterized in that, The step of optimizing the multiple failed commands based on the failure information using a preset command correction model to obtain an optimized command dataset includes: The preset instruction correction model optimizes the multiple failed generation instructions according to the failure information to obtain multiple first optimized instructions. The multiple first optimized instructions are then input into the preset code generation model for code generation to obtain the first optimized code corresponding to each of the multiple first optimized instructions. Multiple first optimized codes are tested using their respective test cases to obtain a first code pass rate, wherein the first code pass rate is the probability of a first optimized code that meets the preset running conditions among multiple first optimized codes; If the first code pass rate is less than the preset pass rate threshold, the preset instruction correction model is used to perform instruction optimization processing on the plurality of first optimized instructions to obtain a plurality of second optimized instructions, and the plurality of second optimized codes generated by the plurality of second optimized instructions are respectively subjected to code testing processing through their respective test cases to obtain the second code pass rate; The instruction optimization dataset is obtained when the pass rate of the optimized instructions corresponding to the multiple failed generation instructions is greater than or equal to the preset pass rate threshold, or when the number of times the multiple failed generation instructions are optimized reaches the preset number of optimizations threshold.

8. The instruction optimization method according to claim 7, characterized in that, The step of optimizing the multiple failed generation instructions according to the failure information using a preset instruction correction model to obtain multiple first optimized instructions includes: The failure information is input into the preset instruction correction model for information extraction processing to obtain the error information corresponding to each of the multiple failed instructions. The error information includes at least the error type, error location, and failure code. Based on the preset instruction correction model, optimization information generation processing is performed according to the error information to obtain instruction optimization information corresponding to each of the plurality of failed instruction generation, wherein the instruction optimization information includes at least the modification actions corresponding to each of the plurality of failed instruction generation; The multiple failed generation instructions are modified according to their respective modification actions to obtain the multiple first optimized instructions, wherein the modification action is an instruction optimization method based on the error type, the error location, and the failed code.

9. The instruction optimization method according to claim 7, characterized in that, The step of performing code testing on multiple first optimized codes through their respective corresponding test cases to obtain the first code pass rate includes: The first optimized code is tested using its respective test cases to obtain the first test result. Based on the first test results, a first pass code is determined among a plurality of first optimized codes, wherein the first pass code is a first optimized code that meets the preset running conditions; Obtain the first number of the first optimized code and the second number of the first passed code, and perform a division operation between the second number and the first number to obtain the pass rate of the first code.

10. The instruction optimization method according to claim 7, characterized in that, The step of performing instruction optimization processing on the plurality of first optimized instructions using the preset instruction correction model to obtain a plurality of second optimized instructions includes: Obtain first test failure information, wherein the first test failure information includes at least test information of the first optimized code that does not meet the preset running conditions; Based on the first test failure information, the first optimization instruction corresponding to the first optimization code that does not meet the preset running conditions is marked as an optimization failure instruction; The preset instruction correction model performs instruction optimization processing on the optimization failure instruction based on the first test failure information to obtain the plurality of second optimization instructions.

11. The instruction optimization method according to claim 7, characterized in that, The process of obtaining the instruction optimization dataset, where the code pass rate of the optimized instructions corresponding to each of the plurality of failed generation instructions is greater than or equal to the preset pass rate threshold, or the number of times the instruction optimization is performed on the plurality of failed generation instructions reaches a preset optimization number threshold, includes: When the pass rate of the optimized instructions corresponding to each of the multiple failed instructions is greater than or equal to the preset pass rate threshold, the instruction optimization dataset is obtained by constructing a dataset using a preset data format based on the multiple failed instructions, the optimized instructions corresponding to each of the multiple failed instructions, and the optimization process corresponding to each of the multiple failed instructions. The optimized instructions are all successful instructions, and the code generated by the successful instructions meets the preset running conditions. If the pass rate of the optimized instructions corresponding to each of the multiple failed generation instructions is less than the preset pass rate threshold, the multiple failed generation instructions shall continue to be optimized, and the number of times the multiple failed generation instructions shall be optimized shall be determined. When the number of times the multiple failed generation instructions are optimized reaches the preset optimization number threshold, the instruction optimization dataset is obtained by constructing a dataset based on the multiple failed generation instructions, the first instruction corresponding to each of the multiple failed generation instructions, and the optimization process data corresponding to each of the multiple failed generation instructions through a preset data format. The first instruction includes optimization failure instructions and optimization success instructions, wherein the code generated by the optimization failure instructions does not meet the preset running conditions.

12. The instruction optimization method according to claim 4, characterized in that, The step of training the preset instruction optimization model based on the instruction optimization dataset to obtain the trained instruction optimization model includes: The preset instruction optimization model is trained according to the instruction optimization dataset and the preset model optimization strategy to obtain the trained instruction optimization model. The preset model optimization strategy includes at least a preset learning rate adjustment strategy, a preset batch size, a preset optimizer, a preset number of iterations, a preset loss function, and a preset weight allocation strategy.

13. The instruction optimization method according to claim 12, characterized in that, The step of adjusting the model parameters of the preset instruction optimization model based on the instruction optimization dataset and the preset model optimization strategy to obtain the trained instruction optimization model includes: The preset instruction optimization model is processed by adjusting the model parameters using the instruction optimization dataset to obtain a first instruction optimization model. The first instruction optimization model is then tested using the first test dataset to obtain a first model test result. Based on the test results of the first model, the model parameters of the first instruction optimization model are adjusted to obtain the second instruction optimization model. Based on the first test dataset, the second instruction optimization model is tested to obtain the test results of the second model. If the test results of the first model and the test results of the second model meet the preset stopping conditions, the second instruction optimization model is determined to be the trained instruction optimization model; If the test results of the first model and the test results of the second model do not meet the preset stopping condition, the model parameters of the second instruction optimization model are adjusted according to the test results of the second model until the number of model parameter adjustments reaches the preset number of iterations, and the trained instruction optimization model is obtained.

14. The instruction optimization method according to claim 13, characterized in that, The step of performing model testing on the first instruction optimization model based on the first test dataset to obtain the first model test result includes: Data is extracted from the first test dataset using a preset few-sample prompting strategy to obtain the first dataset, and the other datasets are divided into the second dataset and the third dataset, wherein the other datasets are the datasets in the first test dataset other than the first dataset; The third dataset is input into the first instruction optimization model for instruction optimization processing to obtain an optimized third dataset. The first dataset, the second dataset, and the optimized third dataset are respectively input into the preset code generation model for code generation processing, and the code is tested through the test cases to obtain the first test pass rate corresponding to the first dataset, the second test pass rate corresponding to the second dataset, and the third test pass rate corresponding to the optimized third dataset. If the pass rate of the third test is greater than the pass rate of the first test and the pass rate of the second test, then the test result of the first model is determined to be a positive result. If the pass rate of the third test is not greater than the pass rate of the first test and / or the pass rate of the second test, then the test result of the first model is determined to be a negative result.

15. The instruction optimization method according to claim 14, characterized in that, The step of adjusting the model parameters of the first instruction optimization model based on the test results of the first model to obtain the second instruction optimization model includes: Based on the test results of the first model, multiple optimization anomalies are determined in the third optimization dataset, wherein the code generated by the optimization anomalies does not meet the preset running conditions; Based on the preset weight allocation strategy, the weight allocation process is performed on the preset loss function according to the multiple optimization anomalies to obtain the updated loss function; If the test result of the first model is determined to be positive, the model parameters of the first instruction-optimized model are adjusted according to the updated loss function and the preset model optimization strategy to obtain the second instruction-optimized model. If the test result of the first model is determined to be a negative result, the preset model optimization strategy is updated in response to the received update information to obtain the updated preset model optimization strategy. Then, the model parameters of the first instruction optimization model are adjusted according to the updated loss function and the updated preset model optimization strategy to obtain the second instruction optimization model.

16. The instruction optimization method according to claim 1, characterized in that, The step of optimizing the instruction to be optimized using the trained instruction optimization model to obtain the target instruction includes: The trained instruction optimization model is deployed to a preset instruction optimization tool, wherein the preset instruction optimization tool includes at least an interface tool and a visualization tool; Based on the interface tool, the instruction to be optimized is received, and in response to the triggered instruction optimization signal, the instruction to be optimized is processed by the trained instruction optimization model to obtain the target instruction. The visualization tool is used to visualize the instruction to be optimized and the target instruction.

17. An instruction optimization apparatus, characterized in that, include: The generation unit is used to input the acquired first training dataset into a preset code generation model for code generation processing to obtain a generation failure dataset in the first training dataset. The generation failure dataset includes multiple generation failure instructions and failure information corresponding to each of the multiple generation failure instructions. The first training dataset is a code generation dataset, which includes at least instructions, code pairs and test cases. The failure information is a structured record of failure reasons, which includes at least syntax errors, undefined variables and logical defects. The first optimization unit is used to perform instruction optimization processing on the plurality of failed generation instructions according to the failure information through a preset instruction correction model to obtain an instruction optimization dataset. The instruction optimization dataset includes at least the plurality of failed generation instructions, the optimized instructions corresponding to each of the plurality of failed generation instructions, and optimization process data. The preset instruction correction model is a model constructed based on a Markov decision process. The training unit is used to train the preset instruction optimization model based on the instruction optimization dataset to obtain the trained instruction optimization model. The second optimization unit is used to perform instruction optimization processing on the instruction to be optimized using the trained instruction optimization model to obtain the target instruction.

18. An electronic device, characterized in that, include: memory for storing computer programs; A processor for implementing the method of instruction optimization as described in any one of claims 1 to 16 when executing the computer program.

19. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program, wherein the computer program, when executed by a processor, implements the steps of the instruction-optimized method as described in any one of claims 1 to 16.

20. A computer program product, comprising a computer program, characterized in that, When the computer program is executed by a processor, it implements the steps of the instruction optimization method as described in any one of claims 1 to 16.

Citation Information

Patent Citations

  • Method for automatically generating instruction data set based on LLM

    CN117667202A

  • Code generation optimization method and device for large language model, equipment and medium

    CN117724695A