Code generation method and device based on multi-large model dynamic cooperation and conflict resolution of task complexity perception
By employing task complexity-aware dynamic routing and circular cross-validation, combined with a self-evolutionary mechanism, the problem of unbalanced capabilities and conflict resolution in code generation by large language models is solved, achieving high-accuracy, low-cost, and reliable code generation.
Patent Information
- Application Number
- CN202611318261.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-08-28
- Publication Date
- 2026-11-06
AI Technical Summary
Existing large language models suffer from problems in code generation tasks, such as unbalanced capabilities, difficulty in detecting illusions, high costs of multi-model invocation, and simplistic and crude conflict resolution mechanisms. They also lack task complexity awareness and self-evolution capabilities.
Through a task complexity-aware dynamic routing mechanism, a circular cross-validation architecture, inference chain consistency analysis, and a self-evolution mechanism, the model combination is dynamically selected and called in parallel to perform circular cross-validation and conflict resolution, and the model performance is optimized by combining self-evolution.
It significantly improves the accuracy and reliability of code generation, reduces invocation costs, and enhances the system's adaptability and compatibility with heterogeneous models.
Smart Images

Figure CN123047964A_ABST
Abstract
Description
Technical Field
[0001] This invention belongs to the field of artificial intelligence-assisted software engineering and automatic code generation technology, specifically relating to a code generation method and apparatus based on task complexity awareness, dynamic collaboration and conflict resolution of multiple models. Background Technology
[0002] With the widespread application of large language models (such as GPT, Claude, DeepSeek, GLM, Qwen, etc.) in software development, AI-assisted programming has become an important means to improve R&D efficiency. However, a single large model has the following significant limitations in code generation tasks:
[0003] (1) The models exhibit significant specializations, making it difficult for a single model to cover all scenarios. Due to differences in training corpora and alignment strategies, different large models show significant differences in performance across different programming languages and task types. For example, some models excel in Python code generation but are weak in C++ or Rust code generation; some models are good at algorithm implementation but are weak in API calls and system design. A single model cannot maintain optimal performance in all scenarios.
[0004] (2) Illusions are difficult to detect, resulting in low output credibility. Large models often exhibit code illusion problems such as fictitious APIs, incorrect parameters, missing dependency packages, and mismatched function signatures. Single-model self-checking is difficult to detect its own generated errors due to cognitive biases and lacks external cross-validation methods. When a model generates code that appears reasonable but is actually erroneous, self-correction alone cannot effectively identify and correct it.
[0005] (3) High cost of multi-model invocation and lack of scheduling strategy. Although the simple all-invocation plus voting strategy can improve accuracy, the token cost and response latency increase exponentially. Most existing solutions use fixed model combinations and invocation order, without dynamically adjusting according to task difficulty, resulting in resource misallocation. It wastes computing resources for simple tasks and cannot provide sufficient inference capabilities for complex tasks.
[0006] (4) The conflict resolution mechanism is simple and crude. When the outputs of multiple models are inconsistent, existing solutions mostly adopt majority voting or random selection without in-depth analysis of the model reasoning process, and cannot identify complex situations such as "seemingly consistent but all wrong" or "a few correct but submerged". The voting mechanism essentially assumes that the majority of models are correct, but when faced with high-difficulty reasoning tasks, the majority of models may be wrong at the same time.
[0007] (5) Lack of self-evolution capability. Existing multi-model collaborative systems are static and cannot adjust model weights and routing strategies based on feedback from the execution results of historical tasks, thus failing to continuously optimize system performance as they are used.
[0008] Therefore, there is an urgent need for a code generation method and device that can sense task complexity, dynamically schedule multiple models, and possess deep conflict resolution and self-evolution capabilities. Summary of the Invention
[0009] The purpose of this invention is to provide a code generation method and apparatus based on task complexity awareness and dynamic collaboration and conflict resolution of multiple large models, aiming to solve the problems of limited code generation capability of a single large model, uncontrolled cost of multi-model collaboration, and insufficient output reliability. By introducing a task complexity awareness dynamic routing mechanism, a circular cross-validation architecture, a conflict resolution algorithm for inference chain consistency analysis, and a self-evolution mechanism for performance backflow, the accuracy and reliability of code generation are significantly improved while ensuring low calling costs.
[0010] The technical solution to achieve the purpose of this invention is: a code generation method based on task complexity awareness for dynamic collaboration and conflict resolution of multiple models, comprising the following steps:
[0011] Step S1, Task Awareness: Extract multi-dimensional features from the input code generation task and construct a task complexity vector to represent the difficulty level of the task;
[0012] Step S2, Dynamic Routing: Based on the model capability profile matrix and task complexity vector, calculate the matching degree between each candidate large language model and the current task, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code;
[0013] Step S3, Circular Cross-Validation: When multiple models are called, a circular validation topology is constructed, allowing the models to review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chains of each model, and calculating the inference chain consistency matrix.
[0014] Step S4, Conflict Resolution: Calculate the divergence degree of the multi-model output based on the code abstract syntax tree similarity and the inference chain consistency matrix, trigger a hierarchical resolution strategy according to the divergence degree level, and output the final code;
[0015] Step S5, Execute verification: Perform unit tests and static scanning of the final code in the sandbox environment, and output the final credibility label;
[0016] Step S6, Self-evolution: Persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix based on the sliding time window and exponential weighting algorithm, and iteratively optimize the routing strategy and prompt template.
[0017] Further, in step S1, multi-dimensional feature extraction is performed on the input task to construct a task complexity vector, including:
[0018] The input task is represented as an ordered triple of natural language description. ,in For user-submitted natural language feature requests, For the code context related to the task, The set of constraints imposed on the generated result;
[0019] Syntax length features are extracted using a syntax feature extraction unit. and AST depth features The semantic feature extraction unit extracts the nesting level feature of the control flow. Features of algorithm complexity estimation Cross-file dependency features are extracted through the context dependency analysis unit. and concurrent I / O features Safety constraint features are extracted through the constraint feature parsing unit. and performance constraint characteristics The extracted features are normalized using Min-Max and then concatenated into an 8-dimensional complexity vector. ;
[0020] Overall task complexity score for:
[0021]
[0022] in, express The weighting coefficients, Represents the normalization function;
[0023] according to The task is divided into three levels:
[0024] It has low complexity.
[0025] The time complexity is medium.
[0026] This is a high-complexity time;
[0027] in, , This indicates the preset task complexity level threshold.
[0028] Further, in step S2, based on the model capability profile matrix and the task complexity vector, the matching degree between each candidate large language model and the current task is calculated, and the model combination that matches the task complexity is dynamically selected, specifically including:
[0029] right Large Model Construct capability profile matrices respectively , Each line For the model exist Ability score vector in each dimension;
[0030] For each candidate model, the matching score is calculated according to the following formula. :
[0031]
[0032] in, Capability vector With complexity vector cosine similarity, , The models are respectively Cost function and delay function These are the trade-off coefficients for capability, cost, and delay, respectively.
[0033] The number of models invoked is dynamically determined based on the task complexity level. :
[0034] For low-complexity tasks, the model with the highest matching degree is called. ;
[0035] Medium-complexity tasks call two models, namely ;
[0036] High-complexity tasks call three or more models, i.e. ;
[0037] For low-complexity and medium-complexity tasks, an early stopping optimization mechanism is employed during parallel calls. This mechanism stops when the confidence level of a certain model reaches a certain threshold. Furthermore, during the rapid syntax check, other incomplete model calls are terminated, among which... The confidence threshold for early stopping.
[0038] Furthermore, in step S2, when the number of models called... Lightweight validation of single model outputs includes:
[0039] First, let the single model generation result be... Perform the following verification:
[0040] (1) Syntax parsing verification: Use a tree-sitter, compiler, or interpreter front end to check the syntax validity and obtain a syntax verification score. ;
[0041] (2) Static analysis verification: Use lint, type checker, and security scanning tools to detect obvious errors and obtain static analysis scores. ;
[0042] (3) Dependency and API authenticity check: External libraries, function signatures, and parameter counts are checked through LSP, package manager metadata, or RAG official documentation to obtain API verification scores: ;
[0043] (4) Minimal Unit Test Generation and Execution: Automatically construct basic input and output test cases for low-complexity tasks and execute them in a sandbox environment to obtain the basic test pass rate. ;
[0044] (5) Generate confidence level calibration: The normalized overall confidence level is denoted as... ;
[0045] Secondly, in a single-model scenario, a lightweight validation weight vector is defined separately:
[0046]
[0047] in, Each component represents the weight of generation confidence, syntax validation, static analysis, API verification, and basic testing in the single-model confidence calculation, and satisfies the following:
[0048]
[0049] The single-model credibility score is defined as:
[0050]
[0051] in Indicates the overall credibility in a single-model invocation scenario; Indicates the normalized overall confidence level; Indicates the grammar validation score; This represents the static analysis score; This indicates the API authenticity verification score; This represents the minimum unit test pass rate; This represents a weight vector specifically for lightweight validation of a single model.
[0052] Finally, let the dedicated confidence threshold for lightweight single-model validation be . :
[0053] If satisfied If so, the output of the single model is accepted as a candidate final result, and the process proceeds to step S5;
[0054] If not satisfied This will automatically trigger the upgrade routing mechanism, changing the number of calling models from... Upgraded to If it is higher, proceed to step S3.
[0055] Furthermore, the circular cross-validation described in step S3 includes:
[0056] S31, Ring Verification Topology:
[0057] Suppose the call The code output generated by each model is Each of them has its own chain of reasoning. ;
[0058] Construct a circular validation relationship, model Review Model The output forms a closed loop. ;
[0059] S32. Blind Review Mechanism and Multidimensional Review Indicators:
[0060] Before review, the output is anonymized by removing signature features from code comments, standardizing formatting, and hiding source model identifiers, so that the review model cannot identify the reviewer's identity.
[0061] Each review model outputs a five-dimensional score for the code being reviewed:
[0062] ,
[0063] in These correspond to syntactic correctness, logical correctness, API authenticity, execution efficiency, and security, respectively.
[0064] Final review output Overall score for:
[0065]
[0066] in The weighting coefficients for the five-dimensional evaluation indicators;
[0067] S33, Inference Chain Consistency Measurement:
[0068] chain of reasoning Perform step-level semantic alignment: divide each inference chain into a sequence of inference steps. Calculate the step coverage and conflict rate between each pair of inference chains, and construct a consistency matrix. This is used for subsequent conflict resolution.
[0069] Furthermore, the conflict resolution described in step S4 includes:
[0070] S41. Divergence Calculation:
[0071] definition The degree of divergence in the output of multiple models This is used to measure the semantic distance between the code and the consistency of the reasoning chain.
[0072]
[0073] in, Indicates output and Based on semantic similarity of AST, AST stands for Abstract Syntax Tree; It is the number of combinations; This is the average similarity among all output pairs calculated based on code AST similarity. The average value of the consistency matrix. This is the tradeoff coefficient between the semantic distance term and the inference chain consistency term in the AST.
[0074] S42. Staged digestion strategy:
[0075] Based on the degree of divergence Different resolution strategies are triggered. , These are the low divergence threshold and the high divergence threshold, respectively.
[0076] (1) Low divergence The output with the highest overall score is used directly as the final result. The calculation method is as follows ,in This is a function to find the maximum value.
[0077] (2) Disagreement : Trigger an adversarial attack and defense strategy, assign a review model to construct counterexamples to attack the candidate outputs, eliminate the candidates that cannot pass the test, and then select the optimal output;
[0078] (3) High divergence Trigger the arbitration layer strategy, call the highest capability model to combine all candidate outputs with the inference chain to generate automatic unit test cases and execute candidate code, and retrieve external documents to verify the authenticity of the API.
[0079] Furthermore, the self-evolution described in step S6 is as follows:
[0080] S61. Results Recording: Record the complexity vector for each task. The set of models called, and the scores of each model. Final execution pass rate User acceptance or modification information is stored in the historical profile database;
[0081] S62. Capability Matrix Update: Based on a sliding time window, profile the model's capabilities. Perform index-weighted updates:
[0082]
[0083] in, , These are the model capability profile matrices before and after the update. The score represents the model's true performance in this round. The learning rate;
[0084] S63, Prompt Template Evolution: Based on the failure case library, the prompt templates of each model are iterated through an automatic Prompt optimization framework;
[0085] S64. Routing Policy Retraining: Periodically retraining routing decisions based on historical data. and Regression optimization is performed to achieve Pareto optimality for accuracy versus cost.
[0086] A code generation device based on task complexity awareness and dynamic collaboration and conflict resolution of multiple models, the device being used to implement the aforementioned code generation method, comprising:
[0087] The task awareness module is used to extract multi-dimensional features from the input code generation task and construct a task complexity vector.
[0088] The dynamic routing module is used to calculate the matching degree based on the model capability profile matrix and the task complexity vector, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code;
[0089] The circular validation module is used to build a circular validation topology when multiple models are called, allowing models to anonymously review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chain to build a consistency matrix.
[0090] The conflict resolution module is used to calculate the divergence degree based on the similarity between the code abstract syntax tree and the consistency matrix, trigger a hierarchical resolution strategy based on the divergence degree to output the final code, and perform verification in the sandbox to output the credibility label.
[0091] The self-evolution module is used to persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix, and optimize the routing strategy and prompt template.
[0092] Furthermore, the task awareness module includes:
[0093] The syntax feature extraction unit is used to jointly parse natural language prompts and code context based on the Tree-sitter multilingual parser, and output syntax length features and AST depth features.
[0094] The semantic feature extraction unit is used to vectorize the task text using the CodeBERT pre-trained model and output the control flow nesting layer feature and the algorithm complexity estimation feature.
[0095] The context dependency analysis unit is used to build call graphs and dependency graphs by accessing the code repository through the Language Server Protocol, and output cross-file dependency characteristics and concurrent I / O characteristics;
[0096] The constraint feature parsing unit is used to extract safety constraint features and performance constraint features from the constraint set using a dual-channel scheme of regular matching and LLM slot extraction.
[0097] The 8-dimensional features output by the four functional units are concatenated into a complexity vector after being normalized by Min-Max. .
[0098] An electronic device includes a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the code generation method.
[0099] Compared with the prior art, the significant advantages of this invention are:
[0100] (1) Significantly improved code generation accuracy: Based on circular cross-validation and inference chain consistency analysis, it can effectively identify single-model illusions and logical errors. On benchmarks such as HumanEval and MBPP, it can improve the pass@1 index by 10%~25% compared to the single strongest model.
[0101] (2) Controllable calling cost: Based on the dynamic routing and early stopping mechanism that is aware of task complexity, simple tasks only call a single lightweight model, while complex tasks schedule multiple models to work together. Compared with the solution of calling all models plus voting, the average token cost can be reduced by more than 40%.
[0102] (3) High depth of conflict resolution: The hierarchical arbitration strategy combines execution verification, adversarial attack and defense and fact-checking. Compared with simple majority voting, it can handle complex situations such as the minority being correct and the output credibility has a quantifiable label.
[0103] (4) The system has self-evolution capability: the dynamic update mechanism of performance feedback and capability profile makes the system more accurate the longer it is used, and can automatically adapt to the access of new models and model version iteration;
[0104] (5) Strong compatibility with heterogeneous models: The unified access layer supports a variety of mainstream large models such as GPT, Claude, DeepSeek, and Qwen-Coder, which can be flexibly combined to avoid vendor lock-in. Attached Figure Description
[0105] Figure 1 This is the overall system architecture diagram of the code generation device based on task complexity awareness and dynamic collaboration and conflict resolution of multiple models provided by the present invention.
[0106] Figure 2 This is a flowchart of task complexity awareness and vector generation.
[0107] Figure 3 This is a schematic diagram of a circular cross-validation and conflict resolution mechanism.
[0108] Figure 4 This is a schematic diagram of the self-evolutionary closed-loop mechanism of backflow. Detailed Implementation
[0109] This invention provides a method for collaboratively completing code generation tasks using multiple heterogeneous Large Language Models (LLMs). By performing complexity awareness and semantic profiling on the input task, multiple large models are dynamically scheduled for parallel inference. Combined with circular cross-validation, inference chain consistency analysis, and conflict resolution mechanisms, high-accuracy, low-cost, and reliable automatic code generation is achieved.
[0110] This invention discloses a code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness, comprising the following steps:
[0111] Step S1, Task Awareness: Extract multi-dimensional features from the input code generation task and construct a task complexity vector to represent the difficulty level of the task;
[0112] Step S2, Dynamic Routing: Based on the model capability profile matrix and task complexity vector, calculate the matching degree between each candidate large language model and the current task, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code;
[0113] Step S3, Circular Cross-Validation: When multiple models are called, a circular validation topology is constructed, allowing the models to review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chains of each model, and calculating the inference chain consistency matrix.
[0114] Step S4, Conflict Resolution: Calculate the divergence degree of the multi-model output based on the code abstract syntax tree similarity and the inference chain consistency matrix, trigger a hierarchical resolution strategy according to the divergence degree level, and output the final code;
[0115] Step S5, Execute verification: Perform unit tests and static scanning of the final code in the sandbox environment, and output the final credibility label;
[0116] Step S6, Self-evolution: Persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix based on the sliding time window and exponential weighting algorithm, and iteratively optimize the routing strategy and prompt template.
[0117] As a specific example, in step S1, multi-dimensional feature extraction is performed on the input task to construct a task complexity vector, including:
[0118] To address the issue of vastly different difficulty levels in code generation tasks, this invention does not employ a fixed model invocation strategy. Instead, it first extracts multi-dimensional features from the input task to generate a complexity vector.
[0119] Let the input task be an ordered triple of natural language description. ,in For user-submitted natural language feature requests; The code context related to the task includes, but is not limited to, existing source code snippets, class / function signatures, dependency library APIs, database schemas, and interface documentation; This refers to the set of constraints imposed on the generated results, including performance constraints, security constraints, style constraints, dependency constraints, compliance constraints, and testing constraints.
[0120] Syntax length features are extracted using a syntax feature extraction unit. and AST depth features The semantic feature extraction unit extracts the nesting level feature of the control flow. Features of algorithm complexity estimation Cross-file dependency features are extracted through the context dependency analysis unit. and concurrent I / O features Safety constraint features are extracted through the constraint feature parsing unit. and performance constraint characteristics The extracted features are normalized using Min-Max and then concatenated into an 8-dimensional complexity vector. The meanings of each dimension are as follows:
[0121] Syntax length features. Statistical natural language hints. With context code The total length of the token and the number of sentences after joint parsing reflect the volume and complexity of the task text.
[0122] AST deep features. Based on the Tree-sitter multilingual parser. Construct an abstract syntax tree and extract the maximum nesting depth and the average number of branches per node to reflect the structural complexity of the code context.
[0123] : Control flow nesting level feature. The task text is vectorized using CodeBERT, and dependency parsing is combined to identify the nesting level of control structures such as loops, conditionals, branches, and exception handling.
[0124] Algorithm complexity estimation features. Based on the maximum cosine similarity between cyclic-recursive pattern recognition and a pre-built complex task template library, the time / space complexity metric of the target algorithm is estimated.
[0125] Cross-file dependency characteristics. By accessing the code repository through the Language Server Protocol (LSP), a call graph and dependency graph are constructed, and the number of cross-file references, external dependency packages, and symbol resolution hops are counted.
[0126] Concurrent I / O characteristics. Identify the number of concurrent primitives (threads, coroutines, locks, message queues) and I / O operations (files, networks, databases) involved in the context and task description, reflecting runtime complexity.
[0127] Safety constraint characteristics. A dual-channel scheme of regular matching and LLM slot extraction is adopted, from... Extract the number and strength of security-related constraints such as authentication, encryption, injection protection, and access control.
[0128] Performance constraint characteristics. From Extract the number and stringency of performance-related constraints such as response latency, throughput, memory limit, and scalability.
[0129] The features in each dimension are normalized using Min-Max and then concatenated to form a complexity vector. .
[0130] The overall complexity score of the task is calculated as follows:
[0131]
[0132] in These are learnable weight coefficients. This is the normalization function.
[0133] according to The task is divided into three levels:
[0134] Low complexity Medium complexity ), high complexity ( ).
[0135] As a specific example, in step S2, the dynamic model routing based on capability profile matching is as follows:
[0136] To address the issues of uneven capabilities and invocation costs among different models, this invention constructs a capability profile for each candidate large model and performs matching and scheduling based on task complexity vectors.
[0137] S21. Model Capability Profile Construction
[0138] For the system access Large Model Construct capability profile matrices respectively , each line For the model exist The capability score vectors on each dimension (programming language, task type, complexity level, average latency, token price, etc.) are initially obtained from benchmark test sets (such as HumanEval, MBPP, SWE-Bench) and are continuously updated as the system runs.
[0139] S22, Routing Decision Algorithm
[0140] For each candidate model, the matching score is calculated according to the following formula. :
[0141]
[0142] in, Capability vector With complexity vector cosine similarity, , The models are respectively Cost function and delay function These are the trade-off coefficients for capability, cost, and delay, respectively.
[0143] The number of models invoked is dynamically determined based on the task complexity level. :
[0144] For low-complexity tasks, the model with the highest matching degree is called. ;
[0145] Medium-complexity tasks call two models, namely ;
[0146] High-complexity tasks call three or more models, i.e. And enable the subsequent full cross-validation process.
[0147] S23, Early Stop Optimization Mechanism
[0148] For low-complexity and medium-complexity tasks, an early stopping optimization mechanism is employed during parallel calls. The system first calculates a unified normalized confidence score based on the token-level logits entropy and model self-scoring during model generation. Let the average token entropy of the model output be... The entropy confidence score is obtained after normalization transformation:
[0149]
[0150] in It is the theoretical maximum entropy or the empirical maximum entropy obtained based on historical sample statistics.
[0151] Let the normalized result of the model's self-score be:
[0152]
[0153] The overall confidence level is defined as:
[0154]
[0155] in This is the tradeoff coefficient between entropy confidence and self-rated confidence.
[0156] If the overall confidence level of the model is returned first, it satisfies Furthermore, if its output passes a fast static syntax check, it immediately terminates calls to other models, saving token costs. The preferred value is 0.92.
[0157] It should be noted that this mechanism is only applicable to low complexity ( ) and medium complexity ( For tasks with high complexity that require cross-validation, a complete validation process should be enforced.
[0158] As a specific example, the single-model scenario credibility guarantee and upgrade mechanism in step S2 is as follows:
[0159] For low-complexity tasks, when the dynamic routing module selects only one generative model, i.e. At this time, the system does not enable circular cross-validation and multi-model divergence calculation. Instead, the system enters a single-model lightweight validation mode, which ensures the credibility of the output through non-homogeneous validators, static testing tools, and executable validation mechanisms, avoiding the use of generated model self-examination as the sole reliable basis.
[0160] First, let the single model generation result be... Perform the following verification:
[0161] (1) Syntax parsing verification: Use a tree-sitter, compiler, or interpreter front end to check the syntax validity and obtain a syntax verification score. ;
[0162] (2) Static analysis verification: Use lint, type checker, and security scanning tools to detect obvious errors and obtain static analysis scores. ;
[0163] (3) Dependency and API authenticity check: External libraries, function signatures, and parameter counts are checked through LSP, package manager metadata, or RAG official documentation to obtain API verification scores: ;
[0164] (4) Minimal Unit Test Generation and Execution: Automatically construct basic input and output test cases for low-complexity tasks and execute them in a sandbox environment to obtain the basic test pass rate. ;
[0165] (5) Generate confidence level calibration: The normalized overall confidence level is denoted as... ;
[0166] Secondly, to avoid confusion with route matching weights, complexity weights, and divergence weights, a separate lightweight validation weight vector is defined for single-model scenarios:
[0167]
[0168] in, Each component represents the weight of generation confidence, syntax validation, static analysis, API verification, and basic testing in the single-model confidence calculation, and satisfies the following:
[0169]
[0170] The single-model credibility score is defined as:
[0171]
[0172] in Indicates the overall credibility in a single-model invocation scenario; Indicates the normalized overall confidence level; Indicates the grammar validation score; This represents the static analysis score; This indicates the API authenticity verification score; This represents the minimum unit test pass rate; This represents a weight vector specifically for lightweight validation of a single model.
[0173] Finally, let the dedicated confidence threshold for lightweight single-model validation be . :
[0174] If satisfied If the output of the single model is accepted as a candidate final result, the process proceeds to step S5 to perform the verification loop closure.
[0175] If not satisfied The system will automatically trigger the upgrade routing mechanism, changing the number of calling models from... Upgraded to If the result is higher, re-enter the S3 circular cross-validation and S4 conflict resolution process.
[0176] in This is a dedicated credibility threshold for lightweight validation of a single model, which can be dynamically adjusted based on historical task execution results and user feedback.
[0177] As a specific example, to address the deviation problem in the unidirectional "generate-verify" structure in step S3, this invention designs a circular cross-validation architecture. Circular cross-validation is only used in... Enabled at [time]. When At that time, the system does not use model self-examination as the final credible basis, but instead enters a single-model lightweight verification and upgrade mechanism.
[0178] Ring cross-validation and inference chain consistency analysis, specifically including:
[0179] S31, Ring Verification Topology:
[0180] Suppose the call The code output generated by each model is Each of them has its own chain of reasoning (CoT). ;
[0181] System constructs circular verification relationship: Model Review Model The output forms a closed loop. .
[0182] S32. Blind Review Mechanism and Multidimensional Review Indicators:
[0183] To avoid style bias in the model during review, this invention anonymizes the output when it is submitted for review: removing the signature feature in the code comments, standardizing the format, and hiding the source model identifier, so that the review model cannot identify the reviewer's identity.
[0184] Each review model outputs a five-dimensional score for the code being reviewed:
[0185] ,
[0186] in These correspond to syntactic correctness, logical correctness, API authenticity, execution efficiency, and security, respectively.
[0187] Final review output Overall score for:
[0188]
[0189] in These are the weighting coefficients for the five-dimensional evaluation indicators.
[0190] S33, Inference Chain Consistency Measurement:
[0191] Further investigation of the inference chain Perform step-level semantic alignment: divide each inference chain into a sequence of inference steps. Calculate the step coverage and conflict rate between each pair of inference chains, and construct a consistency matrix. This is used for subsequent conflict resolution.
[0192] As a specific example, conflict resolution based on disagreement metric in step S4 includes:
[0193] S41. Divergence Calculation:
[0194] definition The degree of divergence in the output of multiple models This is used to measure the semantic distance between the code and the consistency of the reasoning chain.
[0195]
[0196] in, Indicates output and Based on semantic similarity of AST, AST stands for Abstract Syntax Tree; It is the number of combinations; This is the average similarity among all output pairs calculated based on code AST similarity. The average value of the consistency matrix. This is the tradeoff coefficient between the semantic distance term and the inference chain consistency term in the AST.
[0197] Divergence Only for The multi-model output set definition. When At that time, without calculating the degree of divergence, the system uses the single-model confidence level. Make a judgment; if If the number of calls falls below the threshold, the model call count will be automatically increased and the process will re-enter the divergence calculation process.
[0198] S42. Staged digestion strategy:
[0199] Based on the degree of divergence Different resolution strategies are triggered. , These are the low divergence threshold and the high divergence threshold, respectively.
[0200] (1) Low divergence ( The final result is obtained by directly using the output with the highest overall score. The calculation method is as follows: ,in This is the function for finding the maximum value.
[0201] (2) Disagreement ( ): Trigger adversarial attack and defense, that is, assign a review model to construct counterexamples (boundary use cases, abnormal inputs), attempt to break through each candidate output, eliminate the losers and then select the best.
[0202] (3) High divergence ( Triggering the arbitration layer: First, invoke the highest-capability referee model, combining all... and Perform fusion generation; if the task allows, automatically construct unit test cases and actually execute candidate code, taking the execution result as the standard; if necessary, request RAG to retrieve official documentation to fact-check the API; when there is extreme inconsistency, suspend the task and request manual intervention.
[0203] As a specific example, the verification loop execution in step S5 is as follows:
[0204] For the selected final output The system automatically performs unit tests and static scans in a sandbox, and uses the pass rate as the confidence level label in the final output. , which is added to the result.
[0205] As a specific example, the self-evolutionary mechanism of the backflow in step S6 is as follows:
[0206] To enable the system to continuously optimize, this invention introduces closed-loop feedback, with the following specific steps:
[0207] (1) Results recording: The complexity vector of each task The set of models called, and the scores of each model. Final execution pass rate User acceptance / modification information is persisted to the historical profile database.
[0208] (2) Capability matrix update: Based on the sliding time window, the model capability profile is created. Perform index-weighted updates:
[0209]
[0210] in, , These are the model capability profile matrices before and after the update. The score represents the model's true performance in this round. This is the learning rate.
[0211] (3) Prompt template evolution: Failed cases are automatically added to the database, and a set of counterexamples is built for reverse optimization of the Prompt template of each model. Iteration is carried out through automatic Prompt optimization tools such as DSPy.
[0212] (4) Routing policy retraining: Periodically retraining the weights in routing decisions based on historical data. With threshold Regression optimization is performed to achieve Pareto optimality for accuracy versus cost.
[0213] This invention also provides a code generation device based on task complexity awareness and dynamic collaboration and conflict resolution of multiple models. This device is used to implement the aforementioned code generation method, comprising:
[0214] The task awareness module is used to extract multi-dimensional features from the input code generation task and construct a task complexity vector.
[0215] The dynamic routing module is used to calculate the matching degree based on the model capability profile matrix and the task complexity vector, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code;
[0216] The circular validation module is used to build a circular validation topology when multiple models are called, allowing models to anonymously review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chain to build a consistency matrix.
[0217] The conflict resolution module is used to calculate the divergence degree based on the similarity between the code abstract syntax tree and the consistency matrix, trigger a hierarchical resolution strategy based on the divergence degree to output the final code, and perform verification in the sandbox to output the credibility label.
[0218] The self-evolution module is used to persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix, and optimize the routing strategy and prompt template.
[0219] As a specific example, the task awareness module includes:
[0220] The syntax feature extraction unit is used to jointly parse natural language prompts and code context based on the Tree-sitter multilingual parser, and output syntax length features and AST depth features.
[0221] The semantic feature extraction unit is used to vectorize the task text using the CodeBERT pre-trained model and output the control flow nesting layer feature and the algorithm complexity estimation feature.
[0222] The context dependency analysis unit is used to build call graphs and dependency graphs by accessing the code repository through the Language Server Protocol, and output cross-file dependency characteristics and concurrent I / O characteristics;
[0223] The constraint feature parsing unit is used to extract safety constraint features and performance constraint features from the constraint set using a dual-channel scheme of regular matching and LLM slot extraction.
[0224] The 8-dimensional features output by the four functional units are concatenated into a complexity vector after being normalized by Min-Max. .
[0225] The present invention also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the program to implement the code generation method described above.
[0226] The key technical parameters and adjustable hyperparameters involved in this invention are shown in Table 1:
[0227] Table 1 Key Technical Parameters and Adjustable Overparameters
[0228]
[0229] The present invention will be further described in detail below with reference to the accompanying drawings and specific embodiments.
[0230] Example
[0231] This invention provides a code generation device based on task complexity awareness for dynamic collaboration and conflict resolution of multiple models, such as... Figure 1 As shown, the device is logically divided into the following 5 core modules:
[0232] I. Task Awareness Module
[0233] like Figure 2 As shown, the task awareness module consists of four functional units:
[0234] (1) Syntactic Feature Extraction Unit: Used to extract syntactic features. Based on the Tree-sitter multilingual parser, it extracts natural language prompts. With existing code context Joint parsing outputs syntax length features. With AST depth features .
[0235] (2) Semantic Feature Extraction Unit: Used to extract semantic layer features. The CodeBERT pre-trained model is used to vectorize the task text and output the control flow nesting layer features. Features for Estimating Algorithm Complexity Based on Recurrent Pattern Recognition .
[0236] (3) Context Dependency Analysis Unit: Used to extract dependency layer features. It connects to the code repository via LSP, constructs a call graph and dependency graph, and outputs cross-file dependency features. With concurrent I / O characteristics .
[0237] (4) Constraint Feature Parsing Unit: Used to extract constraint layer features. A dual-channel scheme of regular matching and LLM slot extraction is adopted to extract features from the constraint set. Extracting security constraint features With performance constraints .
[0238] Four functional units for input tasks Multi-dimensional feature extraction is performed, and the output 8-dimensional features are concatenated into a complexity vector after Min-Max normalization. Input to the downstream model routing module.
[0239] II. Dynamic Routing Module
[0240] It includes a capability profile storage unit, a matching degree calculation unit, and a scheduling execution unit. The capability profile storage unit is maintained. Matrix. The matching degree calculation unit is based on the model capability profile matrix. For each candidate model, a Match score is calculated. The scheduling execution unit is responsible for dynamic model selection: one model is called for low complexity, two models for medium complexity, and three or more models for high complexity (e.g., for small tasks like SQL spelling, only the lightweight Qwen3-Coder is called; for distributed system design, three models—Claude Opus 4.7, GPT-5.5 Ultra, and DeepSeek V4-Pro—are used in collaboration). Inference requests are initiated in parallel through the LLM API gateway, and early stopping control logic is implemented, i.e., during parallel inference, if the confidence level of a certain model is low... Furthermore, it immediately terminates other calls through a rapid syntax check.
[0241] III. Ring Validation Module
[0242] It includes an anonymization unit, a review and distribution unit, and a consistency calculation unit. The anonymization unit formats the code and removes signatures; the review and distribution unit constructs a circular review topology, with each review model outputting a five-dimensional score (syntax, logic, API, efficiency, and security); the consistency calculation unit constructs a consistency matrix based on the semantic alignment of the AST and inference chain steps. Output the degree of divergence .
[0243] IV. Conflict Resolution Module
[0244] It includes a divergence grading unit, an adversarial attack and defense unit, a referee fusion unit, and an execution verification unit. The divergence grading unit is based on... The system categorizes conflicts into low, medium, and high levels, triggering different resolution paths. The adversarial attack and defense unit invokes a specialized attacker model to construct extreme test cases such as boundary inputs, abnormal inputs, and concurrent competition for candidate code, eliminating candidates that cannot pass. The referee fusion unit invokes the highest-capability model in high-disagreement scenarios, combining the code of all candidates with the inference chain to regenerate the code. The execution verification unit has a built-in code sandbox (such as Docker, nsjail, and gVisor isolation environments) that can automatically build dependency environments and run unit tests, outputting the test pass rate. As the final credibility label, this module also integrates the RAG search interface, which can perform official documentation verification on external API calls and identify illusory API references. Figure 3 This is a schematic diagram of a circular cross-validation and conflict resolution mechanism.
[0245] V. Self-evolution module
[0246] Responsible for the system's continuous optimization capabilities, such as Figure 4 As shown, it includes a historical profile storage unit, a capability matrix update unit, a Prompt template evolution unit, and a routing strategy retraining unit. The historical profile storage unit uses a time-series database (such as InfluxDB) to persist the complexity vector, model performance, execution results, and user feedback for each task; the capability matrix update unit refreshes periodically based on a sliding time window and an exponential weighting algorithm. Matrix; Prompt template evolution unit iterates prompt templates for each model based on a failure case library and automatic prompt optimization frameworks such as DSPy; Routing strategy retraining unit periodically re-trains routing weights. With threshold Regression optimization is performed to make the system approach Pareto optimality in the three-dimensional space of accuracy, cost, and latency.
[0247] VI. Hardware Platform
[0248] The aforementioned modules run on an independent collaborative inference server cluster, configured with: high-performance CPUs (such as Intel Xeon Platinum 8568Y+, used for syntactic analysis, AST construction, consistency matrix calculation, etc. in task awareness), AI inference accelerator cards (such as NVIDIA A100 80GB, with a built-in quantized CodeBERT encoder for millisecond-level generation of task complexity vectors), large-capacity memory (for caching model output and intermediate review results), high-speed SSDs (such as Samsung PM9A3, used for persistence of historical profile and failure case libraries), and independent code sandbox execution nodes (isolated from the main service to prevent malicious code from affecting the system). Each module communicates asynchronously via message queues (such as Kafka) and connects to external heterogeneous large model APIs through a unified LLM gateway (such as LiteLLM).
[0249] The above embodiments are merely preferred embodiments of the present invention, but the embodiments of the present invention are not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the protection scope of the present invention.
Claims
1. A code generation method based on task complexity awareness for dynamic collaboration and conflict resolution of multiple models, characterized in that, Includes the following steps: Step S1, Task Awareness: Extract multi-dimensional features from the input code generation task and construct a task complexity vector to represent the difficulty level of the task; Step S2, Dynamic Routing: Based on the model capability profile matrix and task complexity vector, calculate the matching degree between each candidate large language model and the current task, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code; Step S3, Circular Cross-Validation: When multiple models are called, a circular validation topology is constructed, allowing the models to review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chains of each model, and calculating the inference chain consistency matrix. Step S4, Conflict Resolution: Calculate the divergence degree of the multi-model output based on the code abstract syntax tree similarity and the inference chain consistency matrix, trigger a hierarchical resolution strategy according to the divergence degree level, and output the final code; Step S5, Execute verification: Perform unit tests and static scanning of the final code in the sandbox environment, and output the final credibility label; Step S6, Self-evolution: Persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix based on the sliding time window and exponential weighting algorithm, and iteratively optimize the routing strategy and prompt template.
2. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 1, characterized in that, In step S1, multi-dimensional feature extraction is performed on the input task to construct a task complexity vector, including: The input task is represented as an ordered triple of natural language description. ,in For user-submitted natural language feature requests, For the code context related to the task, The set of constraints imposed on the generated result; Syntax length features are extracted using a syntax feature extraction unit. and AST depth features The semantic feature extraction unit extracts the nesting level feature of the control flow. Features of algorithm complexity estimation Cross-file dependency features are extracted through the context dependency analysis unit. and concurrent I / O features Safety constraint features are extracted through the constraint feature parsing unit. and performance constraint characteristics The extracted features are normalized using Min-Max and then concatenated into an 8-dimensional complexity vector. ; Overall task complexity score for: in, express The weighting coefficients, Represents the normalization function; according to The task is divided into three levels: It has low complexity. The time complexity is medium. This is a high-complexity time; in, , This indicates the preset task complexity level threshold.
3. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 2, characterized in that, In step S2, based on the model capability profile matrix and the task complexity vector, the matching degree between each candidate large language model and the current task is calculated, and the model combination that matches the task complexity is dynamically selected. Specifically, this includes: right Large Model Construct capability profile matrices respectively , Each line For the model exist Ability score vector in each dimension; For each candidate model, the matching score is calculated according to the following formula. : in, Capability vector With complexity vector cosine similarity, , The models are respectively Cost function and delay function These are the trade-off coefficients for capability, cost, and delay, respectively. The number of models invoked is dynamically determined based on the task complexity level. : For low-complexity tasks, the model with the highest matching degree is called. ; Medium-complexity tasks call two models, namely ; High-complexity tasks call three or more models, i.e. ; For low-complexity and medium-complexity tasks, an early stopping optimization mechanism is employed during parallel calls. This mechanism stops when the confidence level of a certain model reaches a certain threshold. Furthermore, during the rapid syntax check, other incomplete model calls are terminated, among which... The confidence threshold for early stopping.
4. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 3, characterized in that, In step S2, when the number of models called... Lightweight validation of single model outputs includes: First, let the single model generation result be... Perform the following verification: (1) Syntax parsing verification: Use a tree-sitter, compiler, or interpreter front end to check the syntax validity and obtain a syntax verification score. ; (2) Static analysis verification: Use lint, type checker, and security scanning tools to detect obvious errors and obtain static analysis scores. ; (3) Dependency and API authenticity check: External libraries, function signatures, and parameter counts are checked through LSP, package manager metadata, or RAG official documentation to obtain API verification scores: ; (4) Minimal Unit Test Generation and Execution: Automatically construct basic input and output test cases for low-complexity tasks and execute them in a sandbox environment to obtain the basic test pass rate. ; (5) Generate confidence level calibration: The normalized overall confidence level is denoted as... ; Secondly, in a single-model scenario, a lightweight validation weight vector is defined separately: in, Each component represents the weight of generation confidence, syntax validation, static analysis, API verification, and basic testing in the single-model confidence calculation, and satisfies the following: The single-model credibility score is defined as: in Indicates the overall credibility in a single-model invocation scenario; Indicates the normalized overall confidence level; Indicates the grammar validation score; This represents the static analysis score; This indicates the API authenticity verification score; This represents the minimum unit test pass rate; This represents a weight vector specifically for lightweight validation of a single model. Finally, let the dedicated confidence threshold for lightweight single-model validation be . : If satisfied If so, the output of the single model is accepted as a candidate final result, and the process proceeds to step S5; If not satisfied This will automatically trigger the upgrade routing mechanism, changing the number of calling models from... Upgraded to If it is higher, proceed to step S3.
5. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 4, characterized in that, The circular cross-validation mentioned in step S3 includes: S31, Ring Verification Topology: Suppose the call The code output generated by each model is Each of them has its own chain of reasoning. ; Construct a circular validation relationship, model Review Model The output forms a closed loop. ; S32. Blind Review Mechanism and Multidimensional Review Indicators: Before review, the output is anonymized by removing signature features from code comments, standardizing formatting, and hiding source model identifiers, so that the review model cannot identify the reviewer's identity. Each review model outputs a five-dimensional score for the code being reviewed: , in These correspond to syntactic correctness, logical correctness, API authenticity, execution efficiency, and security, respectively. Final review output Overall score for: in The weighting coefficients for the five-dimensional evaluation indicators; S33, Inference Chain Consistency Measurement: chain of reasoning Perform step-level semantic alignment: divide each inference chain into a sequence of inference steps. Calculate the step coverage and conflict rate between each pair of inference chains, and construct a consistency matrix. This is used for subsequent conflict resolution.
6. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 5, characterized in that, The conflict resolution described in step S4 includes: S41. Divergence Calculation: definition The degree of divergence in the output of multiple models This is used to measure the semantic distance between the code and the consistency of the reasoning chain. in, Indicates output and Based on semantic similarity of AST, AST stands for Abstract Syntax Tree; It is the number of combinations; This is the average similarity among all output pairs calculated based on code AST similarity. The average value of the consistency matrix. This is the tradeoff coefficient between the semantic distance term and the inference chain consistency term in the AST. S42. Staged digestion strategy: Based on the degree of divergence Different resolution strategies are triggered. , These are the low divergence threshold and the high divergence threshold, respectively. (1) Low divergence The output with the highest overall score is used directly as the final result. The calculation method is as follows ,in This is a function to find the maximum value. (2) Disagreement : Trigger an adversarial attack and defense strategy, assign a review model to construct counterexamples to attack the candidate outputs, eliminate the candidates that cannot pass the test, and then select the optimal output; (3) High divergence Trigger the arbitration layer strategy, call the highest capability model to combine all candidate outputs with the inference chain to generate automatic unit test cases and execute candidate code, and retrieve external documents to verify the authenticity of the API.
7. The code generation method for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness as described in claim 6, characterized in that, The self-evolution described in step S6 is as follows: S61. Results Recording: Record the complexity vector for each task. The set of models called, and the scores of each model. Final execution pass rate User acceptance or modification information is stored in the historical profile database; S62. Capability Matrix Update: Based on a sliding time window, profile the model's capabilities. Perform index-weighted updates: in, , These are the model capability profile matrices before and after the update. The score represents the model's true performance in this round. The learning rate; S63, Prompt Template Evolution: Based on the failure case library, the prompt templates of each model are iterated through an automatic Prompt optimization framework; S64. Routing Policy Retraining: Periodically retraining routing decisions based on historical data. and Regression optimization is performed to achieve Pareto optimality for accuracy versus cost.
8. A code generation device for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness, characterized in that, The apparatus is used to implement the code generation method according to any one of claims 1 to 7, comprising: The task awareness module is used to extract multi-dimensional features from the input code generation task and construct a task complexity vector. The dynamic routing module is used to calculate the matching degree based on the model capability profile matrix and the task complexity vector, dynamically select the model combination that matches the task complexity, and call the selected model in parallel to generate candidate code; The circular validation module is used to build a circular validation topology when multiple models are called, allowing models to anonymously review each other's outputs, generating multi-dimensional review scores, and performing step-level semantic alignment on the inference chain to build a consistency matrix. The conflict resolution module is used to calculate the divergence degree based on the similarity between the code abstract syntax tree and the consistency matrix, trigger a hierarchical resolution strategy based on the divergence degree to output the final code, and perform verification in the sandbox to output the credibility label. The self-evolution module is used to persist the complexity vector, model performance, and execution results of each task to the historical profile library, update the model capability profile matrix, and optimize the routing strategy and prompt template.
9. The code generation device for dynamic collaboration and conflict resolution of multiple models based on task complexity awareness according to claim 8, characterized in that, The task awareness module includes: The syntax feature extraction unit is used to jointly parse natural language prompts and code context based on the Tree-sitter multilingual parser, and output syntax length features and AST depth features. The semantic feature extraction unit is used to vectorize the task text using the CodeBERT pre-trained model and output the control flow nesting layer feature and the algorithm complexity estimation feature. The context dependency analysis unit is used to build call graphs and dependency graphs by accessing the code repository through the Language Server Protocol, and output cross-file dependency characteristics and concurrent I / O characteristics; The constraint feature parsing unit is used to extract safety constraint features and performance constraint features from the constraint set using a dual-channel scheme of regular matching and LLM slot extraction. The 8-dimensional features output by the four functional units are concatenated into a complexity vector after being normalized by Min-Max. .
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, When the processor executes the program, it implements the code generation method as described in any one of claims 1 to 7.
