Multi-function code generation method and device, electronic equipment and storage medium

By calling functions one by one and building a function dependency graph, the generation order is dynamically adjusted, which solves the problems of interface mismatch and data flow errors when generating multiple functions in LLM code generation, and achieves efficient and reliable code generation and low-cost repair.

CN121832949APending Publication Date: 2026-04-10GREE ELECTRIC APPLIANCE INC OF ZHUHAI +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202511723478.1
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2025-11-21
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Existing LLM code generation methods lack explicit dependencies when generating multiple functions, leading to interface mismatches and data flow errors, resulting in high repair costs.

Method used

By calling functions one by one and testing with test cases, the function dependency graph is determined, the generation order is adjusted to prioritize generating the functions that are most dependent on, a closed-loop system is built to explicitly represent implicit dependencies, and the generation order is dynamically adjusted to avoid test failures caused by unmet dependencies.

Benefits of technology

It significantly improves the correctness and reliability of code generation, reduces repair costs, saves computing resources and time, and achieves a qualitative leap in code generation quality and efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121832949A_ABST
    Figure CN121832949A_ABST
Patent Text Reader

Abstract

The invention discloses a multi-function code generation method and device, electronic equipment and a storage medium, and belongs to the multi-function code generation method and device, the electronic equipment and the storage medium. The functions are called one by one according to the generation sequence of the functions, the functions are tested through the test cases corresponding to the functions, and error accumulation is prevented. Under the condition that the function test fails, the call chain causing the function test failure is determined, the function dependency graph is determined according to the call chain, the function dependency graph represents the dependency relationship among the multiple functions, and the test failure caused by the fact that the dependency relationship is not met is reduced. And according to the function dependency graph, the generation sequence of the functions which are not tested is adjusted, and computing resources are saved. And according to the adjusted generation sequence, correcting the function which fails to be tested or continuing to test the next function, and changing the generation sequence from a static state to a dynamic state. Under the condition that the multiple functions meet the preset conditions, the target code is obtained according to the multiple functions, and the repair cost is remarkably reduced.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application belongs to the technical field of artificial intelligence assisted software engineering, and particularly relates to a multi-function code generation method and device, electronic equipment and a storage medium. BACKGROUND

[0002] Existing LLM (Large Language Model) code generation mainly adopts a "single function generation at a time" or "multi-function generation according to the prompt order" strategy. When there is a lack of explicit dependency between multiple functions, the large language model mechanically generates according to the input order, which may cause interface mismatching, data flow errors and other problems. The existing scheme only generates code according to natural language description or simple context window order, resulting in high repair cost after defect exposure. SUMMARY

[0003] The embodiments of the application provide a multi-function code generation method, device, electronic equipment and storage medium, which can solve the problem of high code defect repair cost.

[0004] In a first aspect, the embodiments of the application provide a multi-function code generation method, which comprises: obtaining a generation request of a target code; the generation request comprises test cases corresponding to a plurality of functions one by one and a generation order of the plurality of functions; According to the generation order of the plurality of functions, the functions are called one by one, and the functions are tested through the test cases corresponding to the functions; In the case where the function test fails, the call chain causing the function test to fail is determined, and a function dependency graph is determined according to the call chain; the function dependency graph is used to represent the dependency relationship between the plurality of functions; According to the function dependency graph, the generation order of the untested functions is adjusted; According to the adjusted generation order, the function test fails or the next function test is continued; In the case where the plurality of functions meet a preset condition, the target code is obtained according to the plurality of functions.

[0005] Optionally, the method further comprises: When it is detected that there is a logically repeated function in the plurality of functions, a common interface of the logically repeated function is determined, and the function dependency graph is updated according to the common interface of the logically repeated function.

[0006] Optionally, in the case where the function test fails, the call chain causing the function test to fail is determined, and the function dependency graph is determined according to the call chain, which comprises: In the case that the function test fails, the calling sequence, data transmission path and data change content of the function are acquired; According to the calling sequence, data transmission path and data change content of the function, the calling chain causing the function test to fail is determined; According to the calling chain, a function set on which the function test failure depends is determined, and the function dependency graph is determined according to the function set.

[0007] Optionally, the adjusting the generation order of the untested function according to the function dependency graph comprises: According to the function dependency graph, the generation order of the untested function is adjusted to minimize the cost in the case that the number of test failures of the untested function is least.

[0008] Optionally, the preset condition comprises at least one of the following: the plurality of function tests pass, the function dependency graph stops updating, and the number of times of adjusting the generation order reaches a preset number of times.

[0009] Optionally, the adjusting the generation order of the untested function according to the function dependency graph to minimize the cost in the case that the number of test failures of the untested function is least comprises: According to the function dependency graph, the generation order of the untested function is adjusted according to a preset strategy to minimize the cost in the case that the number of test failures of the untested function is least; the preset strategy comprises at least one of heuristic search, planning algorithm and learning strategy.

[0010] Optionally, the method further comprises: generating an evolution log of the target code, and performing backtracking on the target code according to the evolution log of the target code.

[0011] In a second aspect, an embodiment of the present application provides a multi-function code generation device, which comprises: a generation request acquisition module configured to acquire a generation request of target code; the generation request comprises test cases corresponding to a plurality of functions one by one and a generation order of the plurality of functions; a function calling test module configured to call functions one by one according to the generation order of the plurality of functions, and test the functions through the test cases corresponding to the functions; a dependency relationship determination module configured to determine a calling chain causing the function test to fail in the case that the function test fails, and determine a function dependency graph according to the calling chain; the function dependency graph is used to represent a dependency relationship between the plurality of functions; a generation order adjustment module configured to adjust a generation order of an untested function according to the function dependency graph. a function correction test module, configured to correct the function that fails in the test or continue to test the next function according to the adjusted generation order; a target code generation module, configured to generate the target code according to the plurality of functions when the plurality of functions meet a preset condition.

[0012] Optionally, the apparatus further comprises: a logic duplicate function detection module, configured to determine a common interface of the logic duplicate functions when detecting that there are logic duplicate functions in the plurality of functions, and update the function dependency graph according to the common interface of the logic duplicate functions.

[0013] Optionally, the dependency relationship determination module comprises: a function information acquisition sub-module, configured to acquire a calling order, a data transmission path and data change content of the function when the function fails in the test; a calling chain determination sub-module, configured to determine a calling chain that causes the function to fail in the test according to the calling order, the data transmission path and the data change content of the function; a dependency relationship determination sub-module, configured to determine a function set that eliminates the dependency of the function that fails in the test according to the calling chain, and determine the function dependency graph according to the function set.

[0014] Optionally, the generation order adjustment module comprises: a generation order adjustment sub-module, configured to adjust the generation order of the untested function according to the function dependency graph, so as to minimize the cost in the case that the number of test failures of the untested function is least.

[0015] Optionally, the preset condition comprises at least one of the following: the plurality of functions pass the test, the function dependency graph stops updating, and the number of times of adjustment of the generation order reaches a preset number of times.

[0016] Optionally, the generation order adjustment sub-module comprises: a generation order adjustment unit, configured to adjust the generation order of the untested function according to the function dependency graph according to a preset strategy, so as to minimize the cost in the case that the number of test failures of the untested function is least; the preset strategy comprises at least one of the following: heuristic search, planning algorithm and learning strategy.

[0017] Optionally, the apparatus further comprises: an evolution log generation module, configured to generate an evolution log of the target code, and backtrack the target code according to the evolution log of the target code.

[0018] In a third aspect, an electronic device is provided, which includes a processor, a memory, and a program or instructions stored in the memory and executable on the processor, and the program or instructions, when executed by the processor, implement the steps of the method according to the first aspect.

[0019] In a fourth aspect, a readable storage medium is provided, which stores a program or instructions, and the program or instructions, when executed by a processor, implement the steps of the method according to the first aspect.

[0020] The embodiments of the present application have the following advantages: The embodiments of the present application can call functions one by one according to the generation order of the functions, and test the functions through the test cases corresponding to the functions. The functions are tested one by one during the generation process, which can locate error codes in time, prevent error accumulation, and significantly improve the correctness and reliability of code generation. In the case of function test failure, the call chain causing the function test failure is determined, and the function dependency graph is determined according to the call chain, which is used to represent the dependency relationship between the functions. By analyzing the call chain at the time of test failure, the problem source is directly located to the specific function and the calling relationship, which saves the tedious process of manual sorting and troubleshooting. By dynamically constructing the function dependency graph, the dependency relationship between the functions can be accurately analyzed, and the generation order of the subsequent functions is adjusted accordingly, so that the most dependent and basic functions are generated preferentially, thereby reducing the test failure caused by the unsatisfied dependency relationship. According to the function dependency graph, the generation order of the functions that have not been tested is adjusted. By adjusting the order, the upper function that depends on other functions is avoided to be generated first, thereby avoiding a large number of generation and test operations that are bound to fail, saving computing resources and time. According to the adjusted generation order, the function that fails the test is corrected or the next function is tested. The test failure is no longer just an error signal, but a feedback information driving the system to optimize itself, which builds a complete closed-loop system of generation-test failure-dependency analysis-adjustment order-re-generation, explicitly displays the implicit dependency, and changes the generation order from static to dynamic. In the case that the plurality of functions meet the preset condition, the target code is obtained according to the plurality of functions. Through the above process, the generation quality and efficiency of the target code have achieved a qualitative leap, and the repair cost is significantly reduced. BRIEF DESCRIPTION OF DRAWINGS

[0021] Figure 1 is a step flow chart of a multi-function code generation method according to an embodiment of the present application; Figure 2 is a step flow chart of another multi-function code generation method according to an embodiment of the present application; Figure 3 is a logic diagram of a multi-function code generation method according to an embodiment of the present application; Figure 4 is a structural block diagram of a multi-function code generation device according to an embodiment of the present application. DETAILED DESCRIPTION

[0022] The technical solutions in the embodiments of the present application will be clearly and completely described in connection with the drawings in the embodiments of the present application. Obviously, the described embodiments are only some of the embodiments of the present application, rather than all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those of ordinary skill in the art without creative work fall within the scope of protection of the present application.

[0023] The terms "first", "second", and the like in the specification and claims of the present application are used to distinguish similar objects, and are not used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged under appropriate circumstances, so that the embodiments of the present application can be implemented in an order other than those illustrated or described herein. In addition, "and / or" in the specification and claims indicates at least one of the connected objects, and the character " / ", generally indicates that the front and rear associated objects are in an "or" relationship.

[0024] The multi-function code generation method, device, electronic equipment and storage medium provided by the embodiments of the present application will be described in detail below in connection with the drawings and through specific embodiments and application scenarios.

[0025] Reference Figure 1 , a step flowchart of a multi-function code generation method according to an embodiment of the present application is shown, which can specifically include the following steps: Step 101, obtaining a generation request of target code; the generation request includes test cases corresponding to a plurality of functions one by one and a generation order of the plurality of functions; In the embodiments of the present application, each function to be generated has a dedicated test case, which is used to verify whether the function executes as expected. The generation order of the plurality of functions can be a user initial preset, a random generation or a function generation sequence obtained based on static analysis, which can be dynamically adjusted in the subsequent process, such as reordering according to a function dependency graph.

[0026] The generation request of the target code can also include a requirement description of the target code, which can be a natural language, a pseudo code or a formal specification. The mapping relationship between the test cases and the functions can be established by static scanning or semantic matching according to the requirement description, the function signature expected to be called by each test case is analyzed, and an initial mapping table is formed, so that it can be initially understood which functions need to be generated or modified, thereby laying a foundation for subsequent work.

[0027] Step 102: Call the functions one by one according to the generation order of the multiple functions, and test the functions using the test cases corresponding to the functions; In this embodiment, based on the generation order of multiple functions, the large language model can be requested to generate and load the implementation of each function to execute its logic. After the function is generated, its dedicated test cases are run to verify whether the current implementation of the function meets the expected behavior (pass / fail). The test results will serve as input for subsequent dependency discovery and rearrangement, providing basic data for subsequent closed-loop feedback. For example, the large language model can be requested to generate the source code of a single function one by one according to the generation order of multiple functions, and all relevant tests for that function can be executed immediately. If the test passes, the function is recorded as "stable"; if the test fails, failure information is recorded.

[0028] This application embodiment tests each function during the generation process, which can promptly locate erroneous code, prevent error accumulation, and significantly improve the correctness and reliability of code generation.

[0029] Step 103: If the function test fails, determine the call chain that caused the function test to fail, and determine the function dependency graph based on the call chain; the function dependency graph is used to represent the dependency relationships between the multiple functions; In this embodiment, when a function test fails, the call chain leading to the failure can be determined. Specifically, the function call relationship causing the failure is determined based on its runtime failure context, and this call relationship is incrementally updated to the function dependency graph as dependency edges. The function dependency graph represents explicit or implicit dependencies between multiple functions. Specifically, when a function's test fails, its runtime failure context can be collected, including but not limited to the call stack, variable states, exception information, and side effect logs. Based on this context, other functions necessary for the test to pass are identified, and their corresponding dependencies are derived. These dependencies are added to the global function dependency graph as directed edges. This graph dynamically represents the dependencies between all functions to be generated, including direct call dependencies, data flow dependencies, and environment side effect dependencies.

[0030] This application's embodiments analyze the call chain when tests fail, directly pinpointing the root cause of the problem to specific functions and call relationships, eliminating the tedious process of manual sorting and troubleshooting. By dynamically constructing a function dependency graph, the dependencies between functions can be accurately analyzed, and the subsequent generation order can be adjusted accordingly, prioritizing the generation of the most dependent and most basic functions, thereby reducing test failures caused by unmet dependencies.

[0031] Step 104: Adjust the generation order of untested functions according to the function dependency graph; In this embodiment, the function dependency graph first needs to be parsed to identify all functions and their direct and indirect dependencies. The function dependency graph can be represented as a directed graph, where nodes represent functions and edges represent dependencies, i.e., one function calls another. Based on the parsed function dependencies, a topological sort can be performed on the function dependency graph. For example, for each directed edge uv (representing a path from node u to node v), ensure that u appears before v in the sorting to guarantee that each function will be executed after all its dependent functions. Having obtained the topological sorting result, the generation order of untested functions can be adjusted according to this order.

[0032] In this embodiment, for each untested function, its generation timing is determined based on its position in the topological sort, ensuring that each function is only generated after all its dependent functions have been correctly generated and tested. By adjusting the order, the generation of upper-level functions that depend on other functions is avoided first, thereby avoiding a large number of generation and testing operations that are destined to fail, saving computational resources and time.

[0033] Step 105: According to the adjusted generation order, correct the functions that failed the test or continue testing the next function; In this embodiment, based on the analysis results, a set of functions that need to be pre-existing or modified to eliminate the current failure can be determined. The problem causing the test failure can be located, and the identified issue can be corrected in the code, or the functions that need to be pre-existing can be modified. Once the problem with the current function is resolved and all relevant tests are successfully passed, the testing of the next function can continue according to the previously determined topological order, repeating the above process until all functions have been tested and corrected as necessary in sequence.

[0034] In this application embodiment, test failure is no longer just an error signal, but feedback information that drives the system to perform self-optimization. It constructs a complete closed-loop system of generation-test failure-dependency analysis-order adjustment-regeneration, making implicit dependencies explicit and changing the generation order from static to dynamic.

[0035] Step 106: If the plurality of functions meet the preset conditions, obtain the target code based on the plurality of functions.

[0036] In this embodiment, if the current generation state already meets preset conditions, there is no need to continue iterating. All generated and verified functions can be integrated into complete, runnable target code. These preset conditions may include: all unit tests have passed, and functional completeness is achieved; the function dependency graph has not added any new dependency edges in N consecutive iterations, the system has converged, and no new implicit dependencies have been discovered; and the maximum number of iterations or time limit has been reached to prevent infinite loops and ensure system robustness.

[0037] When multiple functions meet preset conditions, the source code of all verified functions can be combined according to language specifications (such as Python, Java classes, etc.), and processed with formatting, deduplication, and interface alignment to ensure that the overall code is executable and maintains consistency with the original test set. Simultaneously, a final function dependency graph can be output, recording all explicit and implicit dependencies, as well as an evolution log of the generation process, for auditing and backtracking purposes.

[0038] This application embodiment can call functions one by one according to the generation order of multiple functions, and test the functions through the corresponding test cases. Testing functions one by one during the generation process can promptly locate erroneous code, prevent error accumulation, and significantly improve the correctness and reliability of generated code. In the event of a function test failure, the call chain causing the failure is determined, and a function dependency graph is constructed based on the call chain. The function dependency graph represents the dependencies between multiple functions. By analyzing the call chain when a test fails, the root cause of the problem is directly located to the specific function and call relationship, saving the tedious process of manual sorting and troubleshooting. By dynamically constructing the function dependency graph, the dependencies between functions can be accurately analyzed, and the subsequent generation order can be adjusted accordingly, prioritizing the generation of the most dependent and most basic functions, thereby reducing test failures caused by unmet dependencies. Based on the function dependency graph, the generation order of untested functions is adjusted. By adjusting the order, the generation of upper-level functions that depend on other functions is avoided first, thus avoiding a large number of generation and testing operations that are destined to fail, saving computational resources and time. Following the adjusted generation order, functions that fail testing are corrected or the next function is tested. Test failures are no longer merely error signals, but feedback information that drives the system to self-optimize. This constructs a complete closed-loop system of generation – test failure – dependency analysis – order adjustment – ​​regeneration, making implicit dependencies explicit and changing the generation order from static to dynamic. When multiple functions meet preset conditions, the target code is obtained from these functions. Through this process, the quality and efficiency of target code generation have achieved a qualitative leap, and the cost of repair has been significantly reduced.

[0039] Reference Figure 2The diagram illustrates a flowchart of another multi-function code generation method according to an embodiment of this application. The method may specifically include the following steps: Step 201: Obtain the target code generation request; the generation request includes test cases corresponding to multiple functions and the generation order of the multiple functions; In this embodiment, each function to be generated has a dedicated test case, which is used to verify whether the function performs as expected. The generation order of multiple functions can be initially preset by the user, randomly generated, or a function generation sequence derived from static analysis. In subsequent processes, this order can be dynamically adjusted, such as by reordering according to the function dependency graph.

[0040] The request to generate target code can also include a requirement description for the target code, which can be in natural language, pseudocode, or formal specifications. Based on the requirement description, a mapping relationship between test cases and functions can be established through static scanning or semantic matching. Analyzing the function signatures expected to be called by each test case creates an initial mapping table, providing a preliminary understanding of which functions need to be generated or modified, laying the foundation for subsequent work.

[0041] Step 202: Call the functions one by one according to the generation order of the multiple functions, and test the functions using the test cases corresponding to the functions; In this embodiment, based on the generation order of multiple functions, the large language model can be requested to generate and load the implementation of each function to execute its logic. After the function is generated, its dedicated test cases are run to verify whether the current implementation of the function meets the expected behavior (pass / fail). The test results will serve as input for subsequent dependency discovery and rearrangement, providing basic data for subsequent closed-loop feedback. For example, the large language model can be requested to generate the source code of a single function one by one according to the generation order of multiple functions, and all relevant tests for that function can be executed immediately. If the test passes, the function is recorded as "stable"; if the test fails, failure information is recorded.

[0042] This application embodiment tests each function during the generation process, which can promptly locate erroneous code, prevent error accumulation, and significantly improve the correctness and reliability of code generation.

[0043] Step 203: If the function test fails, determine the call chain that caused the function test to fail, and determine the function dependency graph based on the call chain; the function dependency graph is used to represent the dependency relationships between the multiple functions; In one embodiment, the step of determining the call chain that caused the function test to fail, and determining the function dependency graph based on the call chain, may further include the following sub-steps: Sub-step S11: If the function test fails, obtain the function call order, data transfer path, and data change content; Sub-step S12: Determine the call chain that caused the function test to fail based on the function call order, data transmission path, and data change content; Sub-step S13: Based on the call chain, determine the set of functions that the failure of the function test depends on, and determine the function dependency graph based on the set of dependent functions.

[0044] In this embodiment, when a function test fails, its runtime call stack, data flow path, and state change records can be obtained. Based on runtime information, other functions necessary to eliminate the test failure are identified, resulting in a set of dependent functions. The dependency relationships between functions and the set of dependent functions are treated as directed edges and incrementally updated in the function dependency graph. The call order, i.e., the call stack, reflects the control flow between functions; the data transmission path refers to how parameters are passed from the test entry point to the failure point, including the input / output (data flow) of intermediate functions; and the data change content refers to the modification records (side effects or state changes) of variables, object states, global resources, etc., during runtime.

[0045] Specifically, when a function test fails, its runtime context can be collected. The call stack records the function call sequence from the test entry point to the failure point; the data transfer path tracks the flow of key parameters and return values ​​between functions; and data changes record the state modifications of global variables, object properties, or external resources. Based on this information, the root cause of the test failure can be analyzed, and the set of functions required for the test to pass can be deduced. For example, if the failure stems from an undefined function, an empty return value, or an uninitialized state, the corresponding function is identified as a dependency. Subsequently, directed edges pointing from the current function to this set of dependent functions can be added to the global function dependency graph, achieving dynamic explicitness of dependencies.

[0046] This application's embodiments analyze the call chain when tests fail, directly pinpointing the root cause of the problem to specific functions and call relationships, eliminating the tedious process of manual sorting and troubleshooting. By dynamically constructing a function dependency graph, the dependencies between functions can be accurately analyzed, and the subsequent generation order can be adjusted accordingly, prioritizing the generation of the most dependent and most basic functions, thereby reducing test failures caused by unmet dependencies.

[0047] Step 204: According to the function dependency graph, adjust the generation order of the untested functions to minimize the cost while minimizing the number of test failures of the untested functions; In this embodiment of the application, the generation order of untested functions can be rearranged according to the function dependency graph, so that the expected test failure risk (i.e., the number of test failures) and execution cost corresponding to the selected order are minimized under the premise of satisfying the dependency constraints; wherein, the execution cost includes at least one of the following: interface change amount, side effect propagation scope or computational complexity.

[0048] In this embodiment, for each untested function, its generation timing is determined based on its position in the topological sort, ensuring that each function is only generated after all its dependent functions have been correctly generated and tested. By adjusting the order, the generation of upper-level functions that depend on other functions is avoided first, thereby avoiding a large number of generation and testing operations that are destined to fail, saving computational resources and time.

[0049] In one embodiment, the step of adjusting the generation order of the untested functions according to the function dependency graph to minimize the cost while minimizing the number of test failures of the untested functions may further include the following sub-steps: Sub-step S21: Based on the function dependency graph, adjust the generation order of the untested functions according to a preset strategy to minimize the cost while minimizing the number of test failures of the untested functions; the preset strategy includes one of heuristic search, planning algorithm, and learning strategy.

[0050] In the embodiments of this application, heuristic search, such as a greedy strategy, prioritizes generating functions with the least dependencies and the lowest risk; planning algorithms, such as a constraint satisfaction problem (CSP) solver, search for the optimal scheduling path on the dependency graph; and learning strategies, such as reinforcement learning agents, learn a strategy of "high success rate generation order" based on historical project data.

[0051] The cost of minimizing the number of test failures for untested functions is the lowest, i.e., the expected risk of test failure is the lowest. By using a preset strategy, the generation order that minimizes the expected risk of test failure can be searched in the feasible scheduling space, thereby improving the overall convergence efficiency and code stability.

[0052] Step 205: According to the adjusted generation order, correct the functions that failed the test or continue testing the next function; In this embodiment, based on the analysis results, a set of functions that need to be pre-existing or modified to eliminate the current failure can be determined. The problem causing the test failure can be located, and the identified issue can be corrected in the code, or the functions that need to be pre-existing can be modified. Once the problem with the current function is resolved and all relevant tests are successfully passed, the testing of the next function can continue according to the previously determined topological order, repeating the above process until all functions have been tested and corrected as necessary in sequence.

[0053] In this application embodiment, test failure is no longer just an error signal, but feedback information that drives the system to perform self-optimization. It constructs a complete closed-loop system of generation-test failure-dependency analysis-order adjustment-regeneration, making implicit dependencies explicit and changing the generation order from static to dynamic.

[0054] Step 206: If the plurality of functions meet the preset conditions, obtain the target code based on the plurality of functions; the preset conditions include: the plurality of functions pass the test, the function dependency graph stops updating, and the number of times the generation order is adjusted reaches a preset number, at least one of the following: In this embodiment of the application, if the current generation state already meets preset conditions, there is no need to continue iterating. All generated and verified functions can be integrated into complete, runnable target code. The preset conditions may include: all unit tests have passed, and functional completeness is met; the function dependency graph has not added any new dependency edges in N consecutive iterations, the system has converged, and no new implicit dependencies have been discovered; the maximum number of iterations or time limit has been reached to prevent infinite loops and ensure system robustness.

[0055] When multiple functions meet the preset conditions, the source code of all verified functions can be combined according to language specifications (such as Python, Java classes, etc.), and processed by formatting, deduplication, interface alignment, etc., to ensure that the code as a whole is executable and maintains consistency with the original test set.

[0056] Step 207: When logically repeated functions are detected among the multiple functions, the common interface of the logically repeated functions is determined, and the function dependency graph is updated according to the common interface of the logically repeated functions. In this embodiment, the common interface can be the input parameters, output results, and possible operational behaviors shared among these duplicate functions. Functions with repeated logic can be detected using static code analysis tools or by comparing the similarity of function bodies to identify their common interface. For example, when duplicate logic or shared states are detected among multiple functions, an "interface abstraction" action can be triggered to extract the common interface or data structure and update the dependency graph, allowing subsequent generation to directly reuse this interface. Optimizing the function interface and updating the function dependency graph when duplicate logic is detected among multiple functions ensures code efficiency and maintainability.

[0057] Step 208: Generate the evolution log of the target code, and backtrack the target code based on the evolution log.

[0058] In this embodiment, the evolution log records the timing information of function generation, test results, dependency graph changes, and reordering decisions. It can record the entire history of changes to the target code from its initial state to its final state, including, for example, which functions were generated in each round, the test results, the reasons for failure, dependency graph updates, reordering decisions, and interface abstraction events. The evolution log is stored in time-series or graph database format and supports querying by function name, round, dependency relationship, and other dimensions. Users can trace the complete evolution path of any function through the log, including its initial version, failure reasons, dependency context, and the basis for final correction, thereby achieving transparent auditing and debugging support for the target code.

[0059] This application embodiment can call functions one by one according to the generation order of multiple functions, and test the functions through the corresponding test cases. Testing functions one by one during the generation process can promptly locate erroneous code, prevent error accumulation, and significantly improve the correctness and reliability of generated code. In the event of a function test failure, the call chain causing the failure is determined, and a function dependency graph is constructed based on the call chain. The function dependency graph represents the dependencies between multiple functions. By analyzing the call chain when a test fails, the root cause of the problem is directly located to the specific function and call relationship, saving the tedious process of manual sorting and troubleshooting. By dynamically constructing the function dependency graph, the dependencies between functions can be accurately analyzed, and the subsequent generation order can be adjusted accordingly, prioritizing the generation of the most dependent and most basic functions, thereby reducing test failures caused by unmet dependencies. Based on the function dependency graph, the generation order of untested functions is adjusted. By adjusting the order, the generation of upper-level functions that depend on other functions is avoided first, thus avoiding a large number of generation and testing operations that are destined to fail, saving computational resources and time. Following the adjusted generation order, functions that fail testing are corrected or the next function is tested. Test failures are no longer merely error signals, but feedback information that drives the system to self-optimize. This constructs a complete closed-loop system of generation – test failure – dependency analysis – order adjustment – ​​regeneration, making implicit dependencies explicit and changing the generation order from static to dynamic. When multiple functions meet preset conditions, the target code is obtained from these functions. Through this process, the quality and efficiency of target code generation have achieved a qualitative leap, and the cost of repair has been significantly reduced.

[0060] Reference Figure 3 The diagram illustrates a logic diagram of a multi-function code generation method provided in an embodiment of this application. To enable those skilled in the art to better understand the embodiments of this invention, the following explanation is provided... Figure 3 The embodiments of this application are described below: Step 301, Requirements Input: The system's sole input consists of "requirements description + a set of unit tests". The requirements description can be in natural language, pseudocode, or formal specifications; the unit tests only need to "show pass / fail results after execution".

[0061] Step 302, Initial Mapping: The system first establishes a "test-function" mapping relationship: it analyzes the function signature expected to be called by each test case to form an initial mapping table. This step does not require code execution, only static scanning or semantic matching.

[0062] Step 303, Single Function Generation and Verification: The system requests the source code of a single function from the large model one by one according to a certain initial strategy (e.g., random or simple sorting), and immediately executes all relevant tests. If the test passes, the function is recorded as "stable"; if the test fails, the failure information is recorded, and the process proceeds to the next step.

[0063] Step 304, Failure-Driven Dependency Discovery: For each test failure, the system compares the execution trajectory (call stack, data flow, side effects) before and after the failure, extracting "the set of functions that must exist or be modified in advance to eliminate this failure." These function sets are abstracted into new dependency edges and added in real time to an evolving Function Dependency Graph (FDG). This process does not rely on specific tracing techniques and can employ any runtime, static, or hybrid analysis methods.

[0064] Step 305, Dynamic Reordering: After the dependency graph is updated, the system reorders the functions that have not yet been generated using the Min-GreenScheduler, based on the general objective function of "maximizing the number of tests that pass with the minimum cost". The cost can be any measurable metric such as "number of failed tests", "number of interface changes", or "scope of side effect propagation"; the reordering algorithm can be a heuristic search, planning algorithm, or learning strategy.

[0065] Step 306, Incremental Generation: The rearranged results are returned to the large model as a new task order; the model only performs incremental generation or correction for the current highest priority function. After generation, the test is executed again, and steps 303-306 are repeated in a closed loop until: a) all tests pass; b) the dependency graph no longer adds new edges in a series of rounds; c) the preset iteration limit is reached.

[0066] Step 307, Evolution Interface Refinement: When the system detects that multiple functions have duplicate logic or shared states, it can trigger an "interface abstraction" action: refine the common interface or data structure; update the dependency graph so that subsequent generation can directly reuse the interface; keep the test set unchanged and verify that the abstracted whole still passes.

[0067] Step 308, output products: final source code set; a weighted functional dependency graph recording all explicit and implicit dependencies; evolution log of the generation process for auditing and backtracking.

[0068] This application's embodiments, after each test failure, utilize runtime tracing and AST (Abstract Syntax Tree) differences to capture the cross-function call chain leading to the failure; this call chain is then deposited as weighted directed edges, and the Function Dependency Graph (FDG) is updated in real time. For the first time, unit test failure signals are used as direct inputs to the function implementation order in LLM planning. Implicit dependencies can be discovered early in development, avoiding interface misalignment; the defect discovery phase is moved from "after all functions are generated" to "after each function is generated," resulting in an average defect rate reduction of over 40% (internal experimental data).

[0069] With the objective of "maximizing test pass rates with minimal code changes," a heuristic search is used to dynamically rearrange the remaining function implementation order; it supports pluggable strategies such as prioritizing side effect isolation and interface stability. The manual "red-green-refactoring" process of Test-Driven Development (TDD) and the Min-Green Scheduler are automated and embedded into the LLM inference loop. Compared to baseline order generation, it reduces redundant code in open-source multi-function tasks; and increases the proportion of generated code that passes all tests on the first attempt.

[0070] It should be noted that the multi-function code generation method provided in this application embodiment can be executed by a multi-function code generation device, or a control module within the multi-function code generation device for executing the loading multi-function code generation method. This application embodiment uses the execution of the loading multi-function code generation method by a multi-function code generation device as an example to illustrate the multi-function code generation method provided in this application embodiment.

[0071] Reference Figure 4 The diagram illustrates a structural block diagram of a multi-function code generation apparatus according to an embodiment of this application. The apparatus includes: The generation request acquisition module 401 is used to acquire the generation request of the target code; the generation request includes test cases corresponding one-to-one with multiple functions and the generation order of the multiple functions; The function call testing module 402 is used to call the functions one by one according to the generation order of the multiple functions, and to test the functions through the test cases corresponding to the functions. The dependency determination module 403 is used to determine the call chain that caused the function test to fail when the function test fails, and to determine the function dependency graph based on the call chain; the function dependency graph is used to characterize the dependency relationships between the multiple functions. The generation order adjustment module 404 is used to adjust the generation order of untested functions according to the function dependency graph. The function correction test module 405 is used to correct functions that fail to pass the test or continue testing the next function according to the adjusted generation order. The target code generation module 406 is used to obtain the target code based on the plurality of functions when the plurality of functions meet preset conditions.

[0072] Optionally, the device further includes: The logical duplicate function detection module is used to determine the common interface of the logically duplicated functions when logical duplicate functions are detected among the plurality of functions, and update the function dependency graph according to the common interface of the logically duplicated functions.

[0073] Optionally, the dependency determination module includes: The function information acquisition submodule is used to acquire the function's call order, data transfer path, and data change content when the function test fails. The call chain determination submodule is used to determine the call chain that caused the function test to fail based on the function call order, data transmission path and data change content; The dependency determination submodule is used to determine, based on the call chain, the set of functions that the function test failure depends on, and to determine the function dependency graph based on the set of dependent functions.

[0074] Optionally, the generation order adjustment module includes: The generation order adjustment submodule is used to adjust the generation order of the untested functions according to the function dependency graph, so as to minimize the cost while minimizing the number of test failures of the untested functions.

[0075] Optionally, the preset conditions include at least one of the following: the plurality of functions pass the test, the function dependency graph stops updating, and the generation order is adjusted a preset number of times.

[0076] Optionally, the generation order adjustment submodule includes: The generation order adjustment unit is used to adjust the generation order of the untested functions according to the function dependency graph and a preset strategy, so as to minimize the cost while minimizing the number of test failures of the untested functions; the preset strategy includes one of heuristic search, planning algorithm, and learning strategy.

[0077] Optionally, the device further includes: An evolution log generation module is used to generate an evolution log for the target code and to backtrack the target code based on the evolution log.

[0078] This application embodiment can call functions one by one according to the generation order of multiple functions, and test the functions through the corresponding test cases. Testing functions one by one during the generation process can promptly locate erroneous code, prevent error accumulation, and significantly improve the correctness and reliability of generated code. In the event of a function test failure, the call chain causing the failure is determined, and a function dependency graph is constructed based on the call chain. The function dependency graph represents the dependencies between multiple functions. By analyzing the call chain when a test fails, the root cause of the problem is directly located to the specific function and call relationship, saving the tedious process of manual sorting and troubleshooting. By dynamically constructing the function dependency graph, the dependencies between functions can be accurately analyzed, and the subsequent generation order can be adjusted accordingly, prioritizing the generation of the most dependent and most basic functions, thereby reducing test failures caused by unmet dependencies. Based on the function dependency graph, the generation order of untested functions is adjusted. By adjusting the order, the generation of upper-level functions that depend on other functions is avoided first, thus avoiding a large number of generation and testing operations that are destined to fail, saving computational resources and time. Following the adjusted generation order, functions that fail testing are corrected or the next function is tested. Test failures are no longer merely error signals, but feedback information that drives the system to self-optimize. This constructs a complete closed-loop system of generation – test failure – dependency analysis – order adjustment – ​​regeneration, making implicit dependencies explicit and changing the generation order from static to dynamic. When multiple functions meet preset conditions, the target code is obtained from these functions. Through this process, the quality and efficiency of target code generation have achieved a qualitative leap, and the cost of repair has been significantly reduced.

[0079] The multi-function code generation device in this application embodiment can be a device, or a component, integrated circuit, or chip in a terminal. The device can be a mobile electronic device or a non-mobile electronic device. For example, mobile electronic devices can be mobile phones, tablets, laptops, PDAs, in-vehicle electronic devices, wearable devices, ultra-mobile personal computers (UMPCs), netbooks, or personal digital assistants (PDAs), etc., while non-mobile electronic devices can be servers, network-attached storage (NAS), personal computers (PCs), televisions (TVs), ATMs, or self-service machines, etc. This application embodiment does not impose specific limitations.

[0080] The multi-function code generation device in this application embodiment can be a device with an operating system. This operating system can be Android, iOS, or other possible operating systems; this application embodiment does not specifically limit it.

[0081] The multi-function code generation device provided in this application embodiment can achieve... Figures 1 to 3 The various processes implemented by the multi-function code generation device in the method embodiment will not be described again here to avoid repetition.

[0082] Optionally, embodiments of this application also provide an electronic device, including a processor, a memory, and a program or instructions stored in the memory and executable on the processor. When the program or instructions are executed by the processor, they implement the various processes of the above-described multi-function code generation method embodiments and achieve the same technical effects. To avoid repetition, they will not be described again here.

[0083] It should be noted that the electronic devices in the embodiments of this application include the mobile electronic devices and non-mobile electronic devices described above.

[0084] This application also provides a readable storage medium storing a program or instructions. When the program or instructions are executed by a processor, they implement the various processes of the above-described multi-function code generation method embodiments and achieve the same technical effect. To avoid repetition, they will not be described again here.

[0085] The processor is the processor in the electronic device described in the above embodiments. The readable storage medium includes computer-readable storage media, such as computer read-only memory (ROM), random access memory (RAM), magnetic disk, or optical disk.

[0086] It should be noted that, in this document, 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. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes that element. Furthermore, it should be noted that the scope of the methods and apparatuses in the embodiments of this application is not limited to performing functions in the order shown or discussed, but may also include performing functions substantially simultaneously or in the reverse order, depending on the functions involved. For example, the described methods may be performed in a different order than described, and various steps may be added, omitted, or combined. Additionally, features described with reference to certain examples may be combined in other examples.

[0087] Through the above description of the embodiments, those skilled in the art can clearly understand that the methods of 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. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product is stored in a storage medium (such as ROM / RAM, magnetic disk, optical disk) and includes several instructions to cause a terminal (which may be a mobile phone, computer, server, air conditioner, or network device, etc.) to execute the methods described in the various embodiments of this application.

[0088] The embodiments of this application have been described above with reference to the accompanying drawings. However, this application is not limited to the specific embodiments described above. The specific embodiments described above are merely illustrative and not restrictive. Those skilled in the art can make many other forms under the guidance of this application without departing from the spirit and scope of the claims, and all of these forms are within the protection scope of this application.

Claims

1. A method for generating multi-function code, characterized in that, include: Obtain the request to generate the target code; The generation request includes test cases corresponding one-to-one with multiple functions and the generation order of the multiple functions; According to the generation order of the multiple functions, the functions are called one by one, and the functions are tested by the test cases corresponding to the functions. If the function test fails, the call chain that caused the function test to fail is determined, and a function dependency graph is determined based on the call chain; the function dependency graph is used to represent the dependency relationships between the multiple functions. Based on the function dependency graph, adjust the generation order of untested functions; According to the adjusted generation order, functions that fail to pass the test are corrected or the next function is tested. When the multiple functions meet the preset conditions, the target code is obtained based on the multiple functions.

2. The multi-function code generation method according to claim 1, characterized in that, The method further includes: When logically duplicated functions are detected among the plurality of functions, the common interface of the logically duplicated functions is determined, and the function dependency graph is updated based on the common interface of the logically duplicated functions.

3. The multi-function code generation method according to claim 1, characterized in that, In the event that the function test fails, determining the call chain that caused the function test to fail, and determining the function dependency graph based on the call chain, includes: If the function test fails, obtain the function's call order, data transfer path, and data changes. Based on the function call order, data transfer path, and data change content, determine the call chain that caused the function test to fail; Based on the call chain, determine the set of functions that the failure of the function test depends on, and determine the function dependency graph based on the set of dependent functions.

4. The multi-function code generation method according to claim 1, characterized in that, The step of adjusting the generation order of untested functions according to the function dependency graph includes: Based on the function dependency graph, the generation order of the untested functions is adjusted to minimize the cost while minimizing the number of test failures for the untested functions.

5. The multi-function code generation method according to claim 1, characterized in that, The preset conditions include at least one of the following: the multiple functions pass the test, the function dependency graph stops updating, and the generation order is adjusted a preset number of times.

6. The multi-function code generation method according to claim 4, characterized in that, The step of adjusting the generation order of the untested functions according to the function dependency graph to minimize the cost while minimizing the number of test failures of the untested functions includes: Based on the function dependency graph, the generation order of the untested functions is adjusted according to a preset strategy to minimize the cost while minimizing the number of test failures of the untested functions; the preset strategy includes one of heuristic search, planning algorithm, and learning strategy.

7. The multi-function code generation method according to claim 1, characterized in that, The method further includes: An evolution log of the target code is generated, and the target code is backtracked based on the evolution log.

8. A multi-function code generation device, characterized in that, include: The request generation module is used to obtain the generation request for the target code; The generation request includes test cases corresponding one-to-one with multiple functions and the generation order of the multiple functions; The function call testing module is used to call the functions one by one according to the generation order of the multiple functions, and to test the functions through the test cases corresponding to the functions; A dependency determination module is used to determine the call chain that caused the function test to fail when the function test fails, and to determine a function dependency graph based on the call chain; the function dependency graph is used to characterize the dependency relationships between the multiple functions. A generation order adjustment module is used to adjust the generation order of untested functions according to the function dependency graph; The function correction test module is used to correct functions that fail to pass the test or continue testing the next function according to the adjusted generation order. The target code generation module is used to obtain the target code based on the plurality of functions when the plurality of functions meet preset conditions.

9. An electronic device, characterized in that, It includes a processor, a memory, and a program or instructions stored in the memory and executable on the processor, wherein the program or instructions, when executed by the processor, implement the steps of the multi-function code generation method as described in claims 1-7.

10. A readable storage medium, characterized in that, The program or instructions are stored on the readable storage medium, and when the program or instructions are executed by a processor, they implement the steps of the multi-function code generation method as described in claims 1-7.