Code generation method and device, storage medium and program product
By constructing multi-version knowledge graphs and generative artificial intelligence models, the compatibility issues in software system version migration were resolved, achieving efficient and accurate code generation and reducing labor costs and data dependence.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING ZITIAO NETWORK TECH CO LTD
- Filing Date
- 2026-01-29
- Publication Date
- 2026-05-12
AI Technical Summary
Existing technologies have compatibility issues during software system version migration due to changes in interfaces, syntax, semantics, or behavioral patterns. This requires developers to invest a lot of manual refactoring work, and existing tools cannot effectively handle complex cross-version semantic changes.
By constructing a multi-version knowledge graph, explicitly modeling cross-version changes, and using a generative artificial intelligence model to generate code based on relationships, the reliance on massive amounts of labeled data is reduced, ensuring that the generated code maintains semantic consistency while being syntactically compatible.
It improves the accuracy and controllability of code migration, reduces labor costs, enhances the reliability and semantic consistency of automated migration, and reduces reliance on massive amounts of labeled data.
Smart Images

Figure CN122018871A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computer technology, and in particular to a code generation method, apparatus, storage medium, and program product. Background Technology
[0002] With the rapid development of software technology, the various components upon which software systems depend, including but not limited to programming libraries, frameworks, application programming interfaces (APIs), software development kits (SDKs), and even the programming languages themselves, may be undergoing rapid iteration and evolution. This can lead to software assets (such as source code) developed based on older version dependencies potentially being incompatible with newer environments. Summary of the Invention
[0003] This disclosure provides a code generation method, apparatus, storage medium, and program product to improve the accuracy of code migration and reduce costs.
[0004] In a first aspect, embodiments of this disclosure provide a code generation method, including:
[0005] Obtain the first data corresponding to the first code; the first data includes a first association relationship; the first association relationship indicates the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version corresponding to the first code, and the second knowledge graph corresponds to the second version corresponding to the first code;
[0006] Input the first data into the first model to obtain the second code under the second version.
[0007] Secondly, embodiments of this disclosure provide a code generation device, including:
[0008] The acquisition module is used to acquire first data corresponding to the first code; the first data includes a first association relationship; the first association relationship includes a mapping relationship between nodes in a first knowledge graph and nodes in a second knowledge graph; the first knowledge graph corresponds to a first version corresponding to the first code, and the second knowledge graph corresponds to a second version corresponding to the first code;
[0009] The input module is used to input the first data into the first model to obtain the second code under the second version.
[0010] Thirdly, embodiments of this disclosure provide an electronic device, including: a processor and a memory;
[0011] The memory stores computer-executed instructions;
[0012] The processor executes computer execution instructions stored in the memory, causing the at least one processor to perform the code generation method as described in the first aspect and various possible designs of the first aspect.
[0013] Fourthly, embodiments of this disclosure provide a computer-readable storage medium storing computer-executable instructions, which, when executed by a processor, implement the code generation method described in the first aspect and various possible designs of the first aspect.
[0014] Fifthly, embodiments of this disclosure provide a computer program product, including a computer program that, when executed by a processor, implements the code generation method described in the first aspect and various possible designs of the first aspect.
[0015] This disclosure provides a code generation method, device, storage medium, and program product. The method includes obtaining first data corresponding to first code. The first data includes a first association relationship, indicating the mapping relationship between nodes in a first knowledge graph and nodes in a second knowledge graph. The first knowledge graph corresponds to a first version of the first code, and the second knowledge graph corresponds to a second version of the first code. The first data is input into a first model to obtain second code under the second version. The method provided in this disclosure, by introducing the first association relationship as a structured semantic guide and inputting the first association relationship into the generation model, can effectively improve the accuracy and controllability of version migration. It can reduce the dependence on massive amounts of labeled data. By explicitly modeling the change relationships between versions (such as splitting and adding parameters), the generated second code maintains semantic consistency while being syntactically compatible, thus reducing migration costs. Attached Figure Description
[0016] To more clearly illustrate the technical solutions in the embodiments of this disclosure or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this disclosure. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0017] Figure 1 This is a schematic diagram illustrating an application scenario of the code generation method provided in the embodiments of this disclosure;
[0018] Figure 2 Flowchart of the code generation method provided in the embodiments of this disclosure Figure 1 ;
[0019] Figure 3 This is a schematic diagram of the structure of the first association relationship in the code generation method provided in this embodiment of the disclosure;
[0020] Figure 4 A schematic diagram illustrating the principle of generating the first association relationship in the code generation method provided in this embodiment of the disclosure;
[0021] Figure 5 Flowchart of the code generation method provided in the embodiments of this disclosure Figure 2 ;
[0022] Figure 6 This is a structural block diagram of the code generation device provided in the embodiments of this disclosure;
[0023] Figure 7 This is a schematic diagram of the hardware structure of an electronic device provided in an embodiment of this disclosure. Detailed Implementation
[0024] To make the objectives, technical solutions, and advantages of the embodiments of this disclosure clearer, the technical solutions of the embodiments of this disclosure will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this disclosure, and not all embodiments. Based on the embodiments of this disclosure, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this disclosure.
[0025] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this disclosure are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with relevant laws, regulations and standards, and corresponding operation entry points are provided for users to choose to authorize or refuse.
[0026] It should be noted that the code generation method, device, storage medium and program product provided in this disclosure can be used in the field of computer technology, or in any field other than computer technology. The application field of the code generation method, device, storage medium and program product provided in this disclosure is not limited.
[0027] With the rapid development of software technology, the various fundamental components upon which software systems rely, including but not limited to programming libraries, frameworks, application programming interfaces (APIs), software development kits (SDKs), and even the programming languages themselves, are all undergoing rapid iteration and evolution. While this evolution brings new features and performance improvements, it also leads to a common technical challenge: software assets developed based on older version dependencies (such as First Code) often cannot be directly compatible with new version environments due to changes in interfaces, syntax, semantics, or behavioral patterns.
[0028] In related technologies, migrating software assets from dependencies of older versions to dependencies of newer versions (i.e., version adaptation or refactoring) can typically be achieved using example-based mapping methods. This involves analyzing a large number of annotated examples showing the correspondence between old and new versions to infer migration rules and generate code adapted to the new version. However, these methods have significant limitations: First, their effectiveness heavily relies on massive amounts of high-quality, precisely matched annotated example data, resulting in high data acquisition and annotation costs. Second, for complex and diverse code change patterns, the rules derived from limited examples lack generalization ability, leading to low accuracy and poor reliability of the generated code, making it difficult to meet the requirements of real-world production environments.
[0029] Taking APIs as an example, software development typically relies on the APIs of third-party libraries and frameworks. These libraries' APIs iterate frequently (e.g., updates from version 0.24 to 2.x), often introducing incompatible changes, including function deprecation, parameter semantic adjustments, and feature splitting / merging. This version iteration leads to build failures and runtime errors when developers migrate legacy code to new versions, requiring significant manual refactoring work and creating substantial technical debt. Related technologies often employ example-based API mapping methods, collecting a large number of version-aligned code examples (first-version code - second-version code pairs) to train models to learn API mapping patterns (such as parameter rearrangement and function replacement). However, this requires a massive number of labeled examples, and its poor generalization ability affects accuracy.
[0030] To address the aforementioned technical problems, the inventors of this disclosure have discovered that a second version of the code can be generated by constructing a second-version knowledge graph. Considering the limited knowledge provided by a single-version knowledge graph—for example, using a single-version API knowledge graph might lead to issues such as missing key steps, calling outdated APIs, and generating semantically mismatched alternative APIs—a multi-version static knowledge graph can be pre-constructed. When migrating the first code, cross-version changes (such as renaming or parameter adjustments) are explicitly modeled based on the static graph by constructing a dynamic graph corresponding to the first code. This improves the controllability and accuracy of code migration and avoids reliance on massive amounts of labeled examples, saving manpower costs. Based on this, embodiments of this disclosure provide a code generation method.
[0031] For example, the scenarios required for the code generation method provided in this disclosure may include version migration scenarios, automated migration scenarios, and version-sensitive scenarios.
[0032] For version migration scenarios, developers need to migrate the code from version 1 to version 2.
[0033] For the needs of automated migration scenarios, manual migration is inefficient and prone to missing key logic (such as parameter adjustment or function replacement), while existing tools (such as static analysis tools) cannot handle complex cross-version semantic changes.
[0034] For version-sensitive scenarios, some production environments need to strictly adhere to specific version dependencies (such as legacy enterprise systems), and it is necessary to ensure that the migrated code is syntactically compatible and behaves consistently in the second version.
[0035] Figure 1 This is a schematic diagram illustrating a scenario for the code generation method provided in an embodiment of this disclosure. For example... Figure 1 As shown, terminal device 101 is communicatively connected to server 102. Terminal device 101 can be a mobile phone, tablet computer, personal computer, or other similar device. Server 102 can be a cloud server, cluster server, local server, or other similar devices.
[0036] In the implementation process, server 102 can pre-build knowledge graphs corresponding to different software dependency versions, such as a first knowledge graph corresponding to the first version currently depended on by the first code, and a second knowledge graph corresponding to the second version to which the first code is expected to migrate. These static knowledge graphs representing version-specific semantics and structures can be stored in server 102 as a static knowledge base.
[0037] When code migration or refactoring is required, terminal device 101 receives the first code to be processed and the version information of the second version to which it is expected to be migrated, input by the user. Terminal device 101 can send the first code and the second version information to server 102. Server 102 generates second code adapted to the second version based on the received information.
[0038] It should be noted that, Figure 1 The schematic diagram shown is merely an example. The code generation method and scenario described in this disclosure are intended to more clearly illustrate the technical solutions of this disclosure and do not constitute a limitation on the technical solutions provided in this disclosure. As those skilled in the art will know, with the evolution of the system and the emergence of new business scenarios, the technical solutions provided in this disclosure are also applicable to similar technical problems.
[0039] The technical solutions of this disclosure will now be described in detail with reference to optional embodiments. These optional embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0040] Figure 2 Flowchart of the code generation method provided in the embodiments of this disclosure Figure 1 .like Figure 2 As shown, the method includes:
[0041] 201. Obtain the first data corresponding to the first code; the first data includes the first association relationship; the first association relationship indicates the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version corresponding to the first code, and the second knowledge graph corresponds to the second version corresponding to the first code.
[0042] The entity executing the embodiments of this disclosure can be a terminal device or a server.
[0043] In one scenario, the first version upon which the first code to be migrated depends and the second version to which it is expected to be migrated can be identified first. For these two versions, their corresponding knowledge graphs, namely the "first knowledge graph" and the "second knowledge graph," can be invoked or retrieved respectively. These knowledge graphs can be pre-built static knowledge bases that systematically describe the node attributes (such as name, parameters, and return type) of each interface (such as class, function, and method) in the corresponding version, as well as the semantic relationships between nodes (such as inheritance, invocation, and parameter passing), in the form of a graph structure.
[0044] The first association can be a directed semantic mapping established between nodes in the first knowledge graph and nodes in the second knowledge graph through a specific mapping algorithm, and can take the form of an association graph. These mapping relationships depict the specific types of changes that occur from the first version to the second version. For example, a source node in the first version directly corresponds to a target node in the second version (preservation or renaming), the functionality of a source node is split into multiple target nodes (node splitting), or the functionality of multiple source nodes is merged into a single target node (node merging). Furthermore, the mapping relationship may also include more granular semantic adaptation information such as adjustments to parameter lists and changes to default values. Therefore, this first association clearly indicates at the semantic level how to transform the programming logic based on the first version in the first code to the paradigm of the second version. This first association is not simply a union of two knowledge graphs to obtain this association graph; it provides a data foundation for subsequent controllable and accurate code transformation using generative models.
[0045] In a knowledge graph or primary association framework, a node is a graph vertex that represents an entity (such as a class, interface, method, function, or field) within a specific software version. An entity can be an API entity.
[0046] In the first association relationship, the mapping relationship refers to the directed edge connecting a source node in the first knowledge graph to one or more target nodes in the second knowledge graph. It is used to describe the specific change types that an entity undergoes from the first version to the second version, including but not limited to direct correspondence, renaming, splitting, merging, and parameter list adjustment.
[0047] 202. Input the first data into the first model to obtain the second code under the second version.
[0048] In one scenario, after obtaining the first data containing the first association, the generation process of a generative model (i.e., the first model) can be guided or constrained based on the first association to obtain the second code under the second version.
[0049] The first model refers to a generative artificial intelligence model that has been pre-trained on a large-scale code corpus and has the ability to understand and generate code. It can receive input containing semantic guidance information and output code that conforms to the target version specification.
[0050] Generative models are artificial intelligence models that are trained on a large amount of text and code data through self-supervised learning based on a pre-defined architecture, and are able to generate coherent and grammatically correct new sequences of characters based on a given input sequence.
[0051] Large language models refer to foundational models with a huge number of parameters that are pre-trained on a large amount of unlabeled text and code data using deep learning techniques, thereby gaining powerful language understanding and generation capabilities.
[0052] In the implementation process, initial data can be input into a first model. This first model can be a pre-trained large language model with code understanding and generation capabilities. The initial relationships in the initial data can serve as guiding information, providing structured semantic constraints for the generation process of the first model. For example, these initial relationships can indicate the mapping relationship from the first version node to the second version node (such as splitting, merging, renaming, etc.). When generating second code, the first model can adjust the code structure, calling method, or parameter list based on these relationships to ensure that the generated code conforms to the specifications of the second version.
[0053] The code generation method provided in this disclosure improves the accuracy and controllability of version migration by introducing a first association relationship as a structured semantic guide and inputting the first association relationship into the generation model. It reduces reliance on massive amounts of labeled data and, by explicitly modeling complex change relationships between versions (such as splitting and merging), ensures that the generated second code maintains semantic consistency while remaining syntactically compatible, thus reducing migration costs.
[0054] In one embodiment of this disclosure, the method further includes: obtaining a first knowledge graph of a first version corresponding to a first code and a second knowledge graph of a second version; retrieving the first knowledge graph according to the first code to determine a first subgraph; retrieving the second knowledge graph according to the first code to determine a second subgraph; and mapping the first subgraph and the second subgraph to obtain a first association relationship.
[0055] In one scenario, a first knowledge graph corresponding to the first version of the first code and a second knowledge graph corresponding to the second version can be obtained. These two knowledge graphs can be complete knowledge graphs or pruned and optimized knowledge graphs. Then, the first knowledge graph can be retrieved based on the specific content of the first code to determine the nodes actually involved in or dependent on by the first code and the relationships between these nodes, thus forming a first subgraph. Similarly, the second knowledge graph can be retrieved based on the first code (or its transformation target) to determine potentially related target nodes and the relationships between these nodes, thus forming a second subgraph. After obtaining the first and second subgraphs, mapping analysis can be performed on the retrieved first and second subgraphs to obtain a first association relationship that accurately describes the correspondence between these two specific subgraphs.
[0056] For example, taking the first knowledge graph as an example, in the construction process of the first knowledge graph, firstly, the complete code library and its metadata corresponding to the first version of the first code can be obtained from the open source repository. Through abstract syntax tree parsing combined with rule-based analysis, entities (such as API entities) are extracted, including libraries, modules, classes, functions and their parameters, and their semantic attributes are obtained, such as function signatures, default parameter values, docstrings, etc. Then, the graph topology structure can be constructed with entities as nodes and semantic relationships as edges. Each node is labeled with the entity type (e.g., "function:pandas.DataFrame.apply") and its attributes (e.g., the default value of the parameter "axis" is "0"); the edges define the semantic relationships between entities, such as "module contains class", "function has parameter", "method returns type".
[0057] In one embodiment of this disclosure, obtaining the first knowledge graph of the first version corresponding to the first code includes: obtaining the initial knowledge graph of the first version corresponding to the first code; and pruning and optimizing the initial knowledge graph to obtain the corresponding first knowledge graph.
[0058] In this embodiment of the disclosure, after constructing the knowledge graphs of various versions, the generated knowledge graphs can be structurally pruned and optimized by removing unused nodes (such as old functions that have been deprecated but not deleted) and folding long dependency chains (for example, simplifying "library → module → submodule → class" to "module → class") to control the size of the graph and avoid prompts that exceed the token budget due to the graph being too large in subsequent processing.
[0059] The method provided in this disclosure, through pruning optimization, can effectively remove redundant nodes and edges in the initial knowledge graph that are irrelevant to the current first code migration task, thereby significantly reducing the complexity and scale of the knowledge graph, reducing the computational overhead of subsequent first association construction, and improving the focus and efficiency of the first model when processing the first data, ultimately making the second code generation process faster and more accurate.
[0060] After obtaining the first and second knowledge graphs, during the generation of the first association, the system first receives the user-inputted first code Cs and second version vt, parses the APIs called in Cs (such as "pandas.DataFrame.map"), and determines the first and second versions that need to be aligned. Then, it retrieves the first version of the first knowledge graph and the second version (such as pandas 1.0) of the second knowledge graph from a pre-built multi-version static knowledge graph, extracts the actual nodes involved and their relationships based on the first code content, and constructs the first and second subgraphs.
[0061] In one embodiment of this disclosure, mapping a first subgraph and a second subgraph to obtain a first association relationship includes: determining a first matching relationship based on the similarity between the names of nodes in the first subgraph and the names of nodes in the second subgraph; determining a second matching relationship based on the matching degree between the usage patterns of nodes in the first subgraph and the usage patterns of nodes in the second subgraph; determining a third matching relationship based on the comparison result between the document embedding vectors of nodes in the first subgraph and the document embedding vectors of nodes in the second subgraph; and establishing a mapping relationship between nodes in the first subgraph and nodes in the second subgraph according to at least one of the first matching relationship, the second matching relationship, and the third matching relationship to generate the first association relationship.
[0062] In this embodiment, after obtaining the first and second subgraphs, a mapping analysis can be performed on them. Semantic mapping relationships between cross-version nodes can be established using methods such as API name similarity, usage pattern matching, and document embedding comparison. For example, if "pandas.DataFrame.map" in the first version is renamed to "pandas.DataFrame.applymap" in the second version, a "Renaming" edge is established between the two nodes. If a direct mapping is missing (e.g., the source API is split into multiple target APIs), an "empty node," i.e., an intermediate node, can be inserted to complete the migration trajectory and form a complete alignment path. Finally, a "version alignment subgraph" containing cross-version mapping relationships is generated, i.e., the first association relationship. This graph accurately describes the API change trajectory between the first and second versions, providing input for subsequent migration path planning.
[0063] The method provided in this disclosure can quickly obtain explicit renaming or approximation relationships through name similarity matching; verify the continuity of semantic functions at the code call structure level through usage pattern matching; and gain a deeper understanding of semantic connotations and design intentions at the natural language description level through document embedding vector comparison. By employing a multi-dimensional, multi-evidence fusion mapping method, the accuracy and robustness of cross-version node associations can be significantly improved, reducing mismatches caused by splitting, merging, or semantic drift due to version evolution. This provides more accurate and reliable inter-version semantic mapping guidance for subsequent code generation models, ultimately improving the quality of the generated second code in terms of syntactic correctness, functional equivalence, and semantic consistency.
[0064] For example, such as Figure 3 As shown, the relationship between the knowledge graph corresponding to the first version and the knowledge graph corresponding to the second version can include retention, deprecation, and addition. Retention indicates that nodes from the first version are retained in the second version. Deprecation indicates that nodes from the first version are deleted in the second version and are no longer used. Addition indicates that new nodes not present in the first version are added in the second version.
[0065] This embodiment of the disclosure determines relevant subgraphs by performing graph retrieval based on the first code, and then maps them, which can significantly improve processing efficiency and the accuracy of the association graph. By focusing the analysis on subsets that are truly relevant to the current migration task, the generated first association is more targeted, thereby providing more direct and effective guidance for subsequent code generation.
[0066] In one embodiment of this disclosure, mapping the first subgraph and the second subgraph to obtain a first association relationship includes: for each first node in the first subgraph, if there is a second node in the second subgraph that has a semantic equivalence relationship with the first node, then the first node and the second node are associated one-to-one; if there is a third node in the second subgraph that has a semantic transformation relationship with the first node, then an intermediate node is created, and the first node and the third node are associated through the intermediate node; the intermediate node is used to represent the semantic transformation relationship; the semantic transformation relationship includes at least one of the following: functional splitting relationship, functional merging relationship, parameter adaptation relationship.
[0067] In one scenario, the mapping process between the first and second subgraphs can be performed on each first node (source node) in the first subgraph. For each first node, if there exists a second node in the second subgraph with which it has a semantic equivalence (i.e., the functions are basically the same, possibly only renamed), then the first node and the second node can be directly associated one-to-one.
[0068] If a third node exists in the second subgraph, which is not directly equivalent to the first node but has some semantic transformation relationship (e.g., the functionality of the source node is split into the third node and another node, or the source node needs to be merged with the third node, or parameter adaptation is required when the source node calls the third node), then an intermediate node can be created. This intermediate node is used to represent this specific semantic transformation relationship. Furthermore, through this intermediate node, the first node and the third node are associated, thus clearly expressing the semantics of the association through some transformation (such as splitting, merging, or adaptation).
[0069] For example, such as Figure 4 As shown, assume version 1.0 is the first version of the API that the first code depends on, and version 2.0 is the second version of the API to be migrated to. Both versions' knowledge graphs (assuming the left side is the first subgraph and the right side is the second subgraph) include modules, classes, methods, functions, and parameters. When mapping between the two knowledge graphs, deprecated and newly added functions can be associated by adding intermediate nodes with empty functions, and deprecated and newly added parameters can be associated by adding intermediate nodes with empty parameters.
[0070] This disclosure distinguishes between semantic equivalence and semantic transformation relationships, and introduces intermediate nodes to explicitly model complex transformation logic. This enables the generated first association relationship to more accurately depict the actual changes between API versions. It provides deeper semantic guidance for subsequent code generation steps, allowing them to correctly handle refactoring operations such as feature splitting and merging, thereby improving the quality of code generation in complex migration scenarios.
[0071] In one embodiment of this disclosure, the mapping relationship includes at least one of the following: node splitting, node merging, renaming, retention, and adding parameters.
[0072] In one scenario, the mapping relationships included in the first association can encompass multiple types, used to describe the correspondence and conversion methods of interfaces or functional units between different API versions. These mapping relationships can include at least one of node splitting, node merging, renaming, retention, and adding parameters. Node splitting can indicate that a node in the first version is decomposed into multiple nodes in the second version; node merging can indicate that multiple nodes in the first version are integrated into one node in the second version; renaming can indicate that the node's name changes while the core functional semantics remain essentially unchanged; retention can indicate that the node and its calling method remain unchanged after cross-version; adding parameters can indicate that when calling a node in the second version, compared to the corresponding node in the first version, new parameters or configuration options need to be provided to meet extended functional requirements or comply with updated interface specifications. By supporting these diverse mapping relationships, it is possible to more comprehensively depict the various changes introduced by API version iterations.
[0073] This disclosure provides a structured foundation for modeling complex API semantic changes by explicitly defining mapping relationships such as node splitting, merging, renaming, retention, and adding parameters. This enables the primary association relationship to precisely guide the code generation process, ensuring that the migrated code not only passes syntax checks but also accurately reflects the design intent and functional adjustments in the version upgrade, thereby significantly improving the reliability and semantic consistency of automated migration.
[0074] In one embodiment of this disclosure, the first data further includes at least one of the following: first code and second version information.
[0075] In one scenario, the content of the first data can be further expanded to include not only the core first association but also the first code itself and / or second version information. The first code, as the original object to be migrated or transformed, contains complete contextual information (such as specific function implementations, variable definitions, control logic, etc.), providing the necessary raw materials and semantic foundation for the generation process. The second version information explicitly indicates the specific API version identifier to which the code needs to be migrated or adapted, facilitating accurate graph retrieval and mapping relationship selection when multiple possible second versions exist. Using the first code and / or second version information, along with the first association, as input can provide more comprehensive and explicit generation conditions and constraints for subsequent generative models.
[0076] This disclosure provides a more complete context for the generative model by incorporating the first code and second version information into the first data. This enables the model to generate more targeted and context-aware code under the guidance of precise version targets, combining the specific implementation details of the original code with the semantic mapping rules of the first association, thereby further improving the accuracy, practicality, and adaptability to complex migration scenarios of the generated results.
[0077] Figure 5 Flowchart of the code generation method provided in the embodiments of this disclosure Figure 2 .like Figure 5 As shown, based on the above embodiments, for example in Figure 2 Based on the illustrated embodiments, this disclosure provides an example of a method for determining path information based on a first association relationship. The method includes:
[0078] 501. Obtain the first data corresponding to the first code; the first data includes the first association relationship; the first association relationship indicates the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version corresponding to the first code, and the second knowledge graph corresponds to the second version corresponding to the first code.
[0079] Step 501 in this embodiment is similar to step 201 in the above embodiment, and will not be repeated here.
[0080] 502. Input the first association into the second model to obtain path information; the path information includes the processing order and operation type of multiple processing steps.
[0081] 503. Input the path information into the first model to obtain the second code under the second version.
[0082] In one scenario, the first association can be processed in stages. First, the first association can be input into a second model. This second model can be used to parse and plan the association graph, and its output can be path information. The path information can include multiple processing steps required to transform from the first code to the second code, as well as the logical order of these steps and the operation type corresponding to each step. Then, the path information can be input into the first model. The first model can generate the final second version of the second code based on this structured step plan. Through this two-level model collaboration, complex code migration tasks can be decomposed into a more explicit sequence of sub-steps.
[0083] The second model can be the same as the first model. Operation types can include at least one of the following: add, retain, delete, rename, shift, or modify.
[0084] In one embodiment of this disclosure, the method further includes: generating training data by simulating transition patterns through graph traversal based on a preset evolution rule table; the training data includes a source subgraph, a target subgraph, and transition steps, and the evolution rule table includes module renaming rules and parameter mandatory change rules; training an initial language model using supervised fine-tuning based on the training data (so that the initial language model learns the mapping relationship from the input subgraph to the output transition path) to obtain a supervised fine-tuned model; and optimizing the supervised fine-tuned model using reinforcement learning based on a reward function to obtain a second model; the reward function includes a structural reward term and / or a semantic reward term, wherein the structural reward term is used to evaluate the existence of entities corresponding to each step in the transition path in the dynamic subgraph, and the semantic reward term is used to evaluate the semantic alignment between each step in the transition path and the target transition.
[0085] The method provided in this disclosure significantly improves the accuracy and adaptability of code migration path generation by generating training data based on a preset evolution rule table and training the model using a combination of supervised fine-tuning and reinforcement learning. Generating training data by simulating transition patterns through graph traversal covers diverse evolution scenarios, enabling the model to learn reliable mapping relationships from source subgraphs to migration paths. The supervised fine-tuning stage allows the model to initially grasp the generation rules of migration steps, while the reinforcement learning stage ensures consistency between entities in the migration path and the dynamic subgraph structure through structural reward terms, avoiding the generation of invalid or conflicting change steps. Simultaneously, semantic reward terms promote semantic alignment between migration steps and transitions, enhancing the logical rationality and interpretability of the path. Ultimately, this method achieves efficient, structurally controllable, and semantically coherent automatic generation of code migration paths, reducing reliance on manual rule writing and improving the automation and reliability of cross-version upgrades for complex codebases.
[0086] In one scenario, during the training of the second model, the first step is to simulate various transition patterns of the API using a graph traversal algorithm based on a predefined evolution rule table (such as the API evolution rule table shown in Table 1), such as "removal followed by addition" and "renaming accompanied by parameter changes". This will automatically generate synthetic training data in the form of "source subgraph → target subgraph + migration steps", without the need for manual annotation.
[0087] Among them, the specific rules in the rule table, such as module renaming and parameter mandatory changes, are the fundamental basis for the generation of synthetic data.
[0088] Optionally, the model fine-tuning of the second model can be carried out using a dual paradigm that combines supervised fine-tuning and reinforcement learning: In the supervised fine-tuning stage, the second model can learn the mapping relationship from the input dynamically aligned subgraph (i.e., the first association relationship) to the source API (such as "pandas.DataFrame.map") to the output ordered migration path. For example, if the input contains a subgraph with the relationship "map→applymap", the model should output a sequence of steps such as "rename map to applymap and check whether the parameter axis matches".
[0089] During the reinforcement learning phase, the model can be optimized using a composite reward function (as shown in formula (1) below), where the structural reward R exist Used to determine whether the entities involved in each step of the path actually exist in the dynamic subgraph, in order to suppress illusion generation, semantic reward R process This is used to ensure that the semantics of the steps remain consistent with the evolutionary intent by calculating the cosine similarity between the path step description and the target transition type (such as "rename").
[0090] R plan =α⋅R exist +(1−α)⋅R process (1)
[0091] Among them, R plan It is the overall reward value, R exist It is a structural reward, R process It is semantic reward, α is R exist The weight.
[0092] After training, the second model can receive a dynamically aligned subgraph, the source API, and the second version as input, plan an ordered migration path from the source API to the equivalent API of the second version, and output the path information P={a1, a2,..., a...} in a structured form. k}, where each step a i Each operation type is clearly labeled, such as "a1: rename API, a2: add parameter x", thus providing clear step-by-step guidance for the subsequent generation of second code from the first model.
[0093] Table 1 API Evolution Rules
[0094]
[0095] in, These represent the sets of old and new modules, respectively. $F$ and $F'$ represent the sets of old and new classes; $P$ and $P'$ represent the sets of old and new functions; These represent individual modules, classes, functions, and parameter elements, respectively. These represent the returned name, path, module, and location attributes, respectively. These represent the optionality of the returned parameter, whether it has a default value, and the default value, respectively.
[0096] This disclosure, through the introduction of second model generation path information, transforms the abstract mapping diagram into a specific sequence of operation steps. This provides clearer and more direct guidance for the code generation process of the first model, helping to reduce the complexity and uncertainty of the generation process, thereby potentially improving the accuracy, controllability, and interpretability of the code conversion.
[0097] In one embodiment of this disclosure, inputting path information into a first model to obtain a second code under a second version includes: inputting the first code, path information, and information of the second version into the first model based on a hierarchical prompt template to obtain a second code under a second version.
[0098] In one scenario, the specific method of inputting path information into the first model can be based on a structured prompt construction approach. For example, a hierarchical prompt template can be used. Based on a predefined template, the first code, path information, and information from the second version are integrated and organized to form a structured, multi-layered input prompt. Then, this combined complete prompt is input into the first model to obtain the second code under the second version. The hierarchical prompt template can help clearly present task instructions, transformation plans, and original context to the first model.
[0099] This disclosure provides a well-organized and clearly defined input context for the generative model by integrating multi-source information using hierarchical prompt templates. This can guide the model to utilize the first code more systematically, follow the transformation path, and pay attention to the second version constraints, thereby potentially improving the semantic consistency of the generated code, its adherence to complex instructions, and the overall generation effect.
[0100] In one embodiment of this disclosure, the path information includes at least one of the following: description information of each processing step based on natural language description; description information of each processing step based on control token; and description information of each processing step based on structured description.
[0101] In one scenario, the specific form of path information can be flexible and diverse. It can include descriptions of each processing step based on natural language, such as using human-readable text to detail each step; it can also include descriptions of each processing step based on control tokens, such as using a series of predefined semantic tokens to concisely identify operations; or it can include descriptions of each processing step based on structured descriptions, such as using machine-readable data formats like JSON to precisely describe the step details. Path information can include combinations of one or more of the above forms.
[0102] This disclosure enhances the adaptability to different downstream generation models by supporting multiple forms of path description. Whether the model excels at understanding natural language or parsing structured instructions, it can effectively utilize this path information, thereby improving the flexibility and applicability of this method in practical deployment. Furthermore, the model can perform mutual authentication across multiple descriptive languages to improve understanding accuracy.
[0103] In one embodiment of this disclosure, the path information includes description information of each processing step based on a structured description; inputting the path information into a first model to obtain a second code under a second version includes: converting the path information into description information in a target format; the target format includes at least one of the following: natural language description, control token; inputting the path description in the target format into the first model to obtain the second code under a second version.
[0104] This disclosure describes an implementation method for constructing structured hints. The path information, containing descriptive information for each processing step based on a structured description, corresponds to a migration path P consisting of a series of atomic change operations (a1, a2, ..., ak). The step of converting the path information into descriptive information in the target format is specifically implemented as a structured hint construction process. This process aims to convert the formalized path P into hint information that can be effectively understood and followed by the first model (i.e., LLM).
[0105] For example, the target format includes two strategies: First, natural language description, or context summarization strategy, which focuses on summarizing key changes in the path using coherent natural language, such as explicitly stating "pandas.DataFrame.map needs to be replaced with applymap, and the parameter na_action='ignore' needs to be added," thus providing high-level semantic guidance for the model. Second, control tokens, or token-level annotation strategy, which directly inserts special markers with clear instruction meanings, such as "[RENAME:map→applymap]" and "[ADD_PARAM:na_action=ignore]", into the input prompts, forcibly guiding the model to focus on and execute the precise change operations specified by the path at the token level. Furthermore, in a more complete implementation, a hierarchical template strategy can be used to organize the prompt information into parts such as "original code," "migration plan," and "second version," ensuring that path constraints, the first code context, and version requirements are clearly separated and not confused, thereby improving the accuracy and reliability of the model-generated code.
[0106] This disclosure, by converting structured path information into target formats such as natural language descriptions or control tokens, can provide the model with clear and executable migration instructions, thereby effectively guiding the model to accurately understand and execute code changes, and improving the accuracy and reliability of generated code.
[0107] In one embodiment of this disclosure, inputting path information into a first model to obtain second code under a second version includes: inputting path information into a first model to obtain intermediate code corresponding to multiple processing steps and second code under a second version.
[0108] In one scenario, the process of inputting path information into a first model and obtaining second code can be an interpretable generative process with intermediate states. For example, after receiving path information, the first model can not only generate the final second code but also intermediate code corresponding to multiple processing steps in the path information. These intermediate codes can be viewed as stage results or partial implementations in the code conversion process. For example, for the "splitting" step, the model might first generate a code snippet calling one of the new functions; for the "parameter adaptation" step, the model might generate an intermediate form after parameter conversion. The final second code can be the result of synthesizing or iteratively evolving these intermediate code steps.
[0109] For example, in the process of the first model generating the final second code based on path information, its functional goal is to transform the planned migration path into an executable and semantically equivalent second version of code, so as to solve the common version incompatibility and syntax error problems when generating code by existing large language models.
[0110] In practical implementation, a structured hint construction method can be adopted first: inject the first code, path information, and second version information into the hint template, and three strategies can be used to ensure that path constraints are followed. First, the context summary strategy, which uses natural language to summarize the core intent of the path, such as "replace pandas.DataFrame.map with applymap and add the parameter na_action='ignore'". Second, the token-level annotation strategy, which inserts specific control tokens into the hint, such as "[RENAME:map→applymap]" and "[ADD_PARAM:na_action=ignore]", to guide the model to focus on key change points. Third, the hierarchical template strategy, which divides the overall hint into three clear parts: "original code", "migration plan", and "second version", to avoid information confusion. The original code part contains the first code, the migration plan part contains the path information (including a context summary in natural language and control tokens), and the second version part contains the version information to be migrated to.
[0111] In terms of code generation paradigms, the model can support two modes to adapt to different scenarios: the first, the step-by-step generation mode, will generate code according to the path order a1, a2...a... k Intermediate code is generated sequentially, for example, by first modifying the API name and then adding parameters. This mode facilitates debugging and process review.
[0112] The second method, single-step generation mode, can directly generate the final second code based on the complete path P, thereby improving inference efficiency.
[0113] In one embodiment of this disclosure, inputting path information into a first model to obtain second code under a second version includes: inputting path information into an initial model to obtain initial second code; fine-tuning the initial model using reinforcement learning based on a composite reward function to optimize the initial second code and obtain second code; wherein the composite reward function includes at least one of a token reward, a compilation reward, and a semantic similarity reward; the token reward is used to evaluate whether the initial second code contains a key change token specified in the migration path; the compilation reward is used to verify the syntactic correctness of the initial second code in the target version environment through an automated test script; and the semantic similarity reward is used to calculate the semantic similarity between the initial second code and a reference code, wherein the reference code is the target version code obtained through manual migration.
[0114] In this embodiment of the disclosure, the first model can also be fine-tuned and optimized through reinforcement learning during the generation of the second code. For example, a composite reward function (as shown in the following formula (1)) can be used for evaluation. This reward function can combine token reward (encourage the generated code to contain key tokens in the path), compilation reward (verify the syntactic correctness of the code in the second version environment through automated test scripts) and semantic similarity reward (calculate the semantic consistency between the generated code and the manually migrated reference code).
[0115] (2)
[0116] The sum of α1, α2, and α3 is 1, which are respectively , , The weights; It's a token reward. It's a compilation reward. It's a similarity reward.
[0117] The method provided in this disclosure, by inputting path information into an initial model and fine-tuning it using reinforcement learning based on a composite reward function, can effectively improve the accuracy and reliability of code migration. By introducing a token reward, it ensures that the generated second code accurately contains the key syntax or changes required for version migration, avoiding omissions of core modifications. Automated testing and verification of the compilation reward ensures the syntactic correctness and executableness of the generated code in the target version environment, reducing manual debugging costs. Simultaneously, by comparing the semantic similarity reward with manually migrated reference code, the generated code maintains a high degree of consistency with the expected results in terms of functional logic, improving the semantic integrity of the code. Ultimately, it enables automated, high-fidelity code version migration, significantly reducing the errors and time overhead of manual migration, and enhancing the practicality and stability of the model output.
[0118] Finally, the first model outputs second code that passes syntax checking and behavior testing, completing the entire transfer task. This structured generation process not only generates the final code but also outputs intermediate code corresponding to each processing step when needed, thereby enhancing the interpretability and controllability of the generation process.
[0119] This disclosure enhances the interpretability and controllability of the code generation process by generating intermediate code from the model. Developers can review these intermediate results to understand the model's transformation logic, and can also perform error checking or step-by-step verification based on the intermediate results, which helps improve the transparency, reliability, and debuggability of the entire migration process.
[0120] Corresponding to the code generation method in the above embodiments, Figure 6This is a structural block diagram of a code generation device provided in an embodiment of this disclosure. For ease of explanation, only the parts relevant to the embodiments of this disclosure are shown. (Refer to...) Figure 6 The code generation device includes: an acquisition module 601 and an input module 602.
[0121] The acquisition module 601 is used to acquire the first data corresponding to the first code; the first data includes a first association relationship; the first association relationship includes the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version corresponding to the first code, and the second knowledge graph corresponds to the second version corresponding to the first code.
[0122] Input module 602 is used to input the first data into the first model to obtain the second code under the second version.
[0123] The code generation apparatus provided in this disclosure improves the accuracy and controllability of version migration by introducing a first association relationship as a structured semantic guide and inputting the first association relationship into the generation model. It reduces reliance on massive amounts of labeled data and, by explicitly modeling complex change relationships between versions (such as splitting and merging), ensures that the generated second code maintains semantic consistency while remaining syntactically compatible, thus reducing migration costs.
[0124] In one embodiment of this disclosure, the mapping relationship includes at least one of the following: node splitting, node merging, renaming, and retention.
[0125] This disclosure provides a structured foundation for modeling complex API semantic changes by explicitly defining mapping relationships such as node splitting, merging, renaming, retention, and adding parameters. This enables the primary association relationship to precisely guide the code generation process, ensuring that the migrated code not only passes syntax checks but also accurately reflects the design intent and functional adjustments in the version upgrade, thereby significantly improving the reliability and semantic consistency of automated migration.
[0126] In one embodiment of this disclosure, the first data further includes at least one of the following: first code and second version information.
[0127] This disclosure provides a more complete context for the generative model by incorporating the first code and second version information into the first data. This enables the model to generate more targeted and context-aware code under the guidance of precise version targets, combining the specific implementation details of the original code with the semantic mapping rules of the first association, thereby further improving the accuracy, practicality, and adaptability to complex migration scenarios of the generated results.
[0128] In one embodiment of this disclosure, the input module 602 is specifically used to: input the first association relationship into the second model to obtain path information; the path information includes the processing order and operation type of multiple processing steps; input the path information into the first model to obtain the second code under the second version.
[0129] This disclosure, through the introduction of second model generation path information, transforms the abstract mapping diagram into a specific sequence of operation steps. This provides clearer and more direct guidance for the code generation process of the first model, helping to reduce the complexity and uncertainty of the generation process, thereby potentially improving the accuracy, controllability, and interpretability of the code conversion.
[0130] In one embodiment of this disclosure, the input module 602 is specifically used to: input the first code, path information, and information of the second version into the first model to obtain the second code under the second version.
[0131] In one embodiment of this disclosure, the input module 602 is specifically used to: input the first code, path information, and second version information into the first model based on the hierarchical prompt template, and obtain the second code under the second version.
[0132] This disclosure provides a well-organized and clearly defined input context for the generative model by integrating multi-source information using hierarchical prompt templates. This can guide the model to utilize the first code more systematically, follow the transformation path, and pay attention to the second version constraints, thereby potentially improving the semantic consistency of the generated code, its adherence to complex instructions, and the overall generation effect.
[0133] In one embodiment of this disclosure, the path information includes at least one of the following: description information of each processing step based on natural language description; description information of each processing step based on control token; and description information of each processing step based on structured description.
[0134] This disclosure enhances the adaptability to different downstream generation models by supporting multiple forms of path description. Whether the model excels at understanding natural language or parsing structured instructions, it can effectively utilize this path information, thereby improving the flexibility and applicability of this method in practical deployment. Furthermore, the model can perform mutual authentication across multiple descriptive languages to improve understanding accuracy.
[0135] In one embodiment of this disclosure, the path information includes description information of each processing step based on a structured description; the input module 602 is specifically used to: convert the path information into description information in a target format; the target format includes at least one of the following: natural language description, control token; input the path description in the target format into the first model to obtain the second code under the second version.
[0136] This disclosure, by converting structured path information into target formats such as natural language descriptions or control tokens, can provide the model with clear and executable migration instructions, thereby effectively guiding the model to accurately understand and execute code changes, and improving the accuracy and reliability of generated code.
[0137] In one embodiment of this disclosure, the input module 602 is specifically used to: input path information into the first model to obtain intermediate code and second code under the second version corresponding to multiple processing steps.
[0138] This disclosure enhances the interpretability and controllability of the code generation process by generating intermediate code from the model. Developers can review these intermediate results to understand the model's transformation logic, and can also perform error checking or step-by-step verification based on the intermediate results, which helps improve the transparency, reliability, and debuggability of the entire migration process.
[0139] In one embodiment of this disclosure, the acquisition module 601 is further configured to: acquire a first knowledge graph of a first version corresponding to the first code and a second knowledge graph of a second version; retrieve the first knowledge graph according to the first code to determine a first subgraph; retrieve the second knowledge graph according to the first code to determine a second subgraph; and map the first subgraph and the second subgraph to obtain a first association relationship.
[0140] This embodiment of the disclosure determines relevant subgraphs by performing graph retrieval based on the first code, and then maps them, which can significantly improve processing efficiency and the accuracy of the association graph. By focusing the analysis on the subset of APIs that are truly relevant to the current migration task, the generated first association is more targeted, thereby providing more direct and effective guidance for subsequent code generation.
[0141] In one embodiment of this disclosure, the acquisition module 601 is specifically used to: for each first node in the first subgraph, if there is a second node in the second subgraph that has a semantic equivalence relationship with the first node, then associate the first node with the second node in a one-to-one correspondence; if there is a third node in the second subgraph that has a semantic transformation relationship with the first node, then create an intermediate node and associate the first node with the third node through the intermediate node; the intermediate node is used to represent the semantic transformation relationship; the semantic transformation relationship includes at least one of the following: functional splitting relationship, functional merging relationship, parameter adaptation relationship.
[0142] This disclosure distinguishes between semantic equivalence and semantic transformation relationships, and introduces intermediate nodes to explicitly model complex transformation logic. This enables the generated first association relationship to more accurately depict the actual changes between API versions. It provides deeper semantic guidance for subsequent code generation steps, allowing them to correctly handle refactoring operations such as feature splitting and merging, thereby improving the quality of code generation in complex migration scenarios.
[0143] In one scenario, the acquisition module 601 is specifically used to: acquire the initial knowledge graph of the first version corresponding to the first code; and perform pruning and optimization on the initial knowledge graph to obtain the corresponding first knowledge graph.
[0144] In this embodiment of the disclosure, through pruning optimization, redundant nodes and edges in the initial knowledge graph that are irrelevant to the current first code migration task can be effectively removed, thereby significantly reducing the complexity and scale of the knowledge graph, thereby reducing the computational overhead of subsequent first association construction, and improving the focus and efficiency of the first model when processing the first data, ultimately making the generation process of the second code faster and more accurate.
[0145] In one embodiment of this disclosure, the acquisition module 601 is specifically used to: determine a first matching relationship based on the similarity between the names of nodes in the first subgraph and the names of nodes in the second subgraph; determine a second matching relationship based on the matching degree between the usage patterns of nodes in the first subgraph and the usage patterns of nodes in the second subgraph; determine a third matching relationship based on the comparison result between the document embedding vectors of nodes in the first subgraph and the document embedding vectors of nodes in the second subgraph; and establish a mapping relationship between nodes in the first subgraph and nodes in the second subgraph according to at least one of the first matching relationship, the second matching relationship and the third matching relationship, so as to generate a first association relationship.
[0146] In this embodiment, name similarity matching can quickly obtain explicit renaming or approximation relationships; usage pattern matching can verify the continuity of semantic functions at the code call structure level; and document embedding vector comparison can deeply understand semantic connotations and design intentions at the natural language description level. Through a multi-dimensional, multi-evidence fusion mapping method, the accuracy and robustness of cross-version node associations can be significantly improved, reducing mismatches caused by splitting, merging, or semantic drift due to version evolution. This provides more accurate and reliable inter-version semantic mapping guidance for subsequent code generation models, ultimately improving the quality of the generated second code in terms of syntactic correctness, functional equivalence, and semantic consistency.
[0147] In one embodiment of this disclosure, the input module 602 is further configured to: generate training data by simulating transition patterns through graph traversal based on a preset evolution rule table; the training data includes a source subgraph, a target subgraph, and transition steps, and the evolution rule table includes module renaming rules and parameter mandatory change rules; based on the training data, train the initial language model using supervised fine-tuning (e.g., to enable the initial language model to learn the mapping relationship from the input subgraph to the output transition path) to obtain a supervised fine-tuned model; and optimize the supervised fine-tuned model using reinforcement learning based on a reward function to obtain a second model; the reward function includes a structural reward term and / or a semantic reward term, the structural reward term being used to evaluate the existence of entities corresponding to each step in the transition path in the dynamic subgraph, and the semantic reward term being used to evaluate the semantic alignment between each step in the transition path and the target transition.
[0148] This embodiment of the disclosure significantly improves the accuracy and adaptability of code migration path generation by generating training data based on a preset evolution rule table and training the model using a combination of supervised fine-tuning and reinforcement learning. Generating training data by simulating transition patterns through graph traversal covers diverse evolution scenarios, enabling the model to learn reliable mapping relationships from source subgraphs to migration paths. The supervised fine-tuning stage allows the model to initially grasp the generation rules of migration steps, while the reinforcement learning stage ensures consistency between entities in the migration path and the dynamic subgraph structure through structural reward terms, avoiding the generation of invalid or conflicting change steps. Simultaneously, semantic reward terms promote semantic alignment between migration steps and transitions, enhancing the logical rationality and interpretability of the path. Ultimately, this method achieves efficient, structurally controllable, and semantically coherent automatic generation of code migration paths, reducing reliance on manual rule writing and improving the automation and reliability of cross-version upgrades for complex codebases.
[0149] In one embodiment of this disclosure, the input module 602 is further configured to: input path information into the initial model to obtain initial second code; fine-tune the initial model using reinforcement learning based on a composite reward function, optimize the initial second code, and obtain second code; wherein the composite reward function includes at least one of a token reward, a compilation reward, and a semantic similarity reward, wherein the token reward is used to evaluate whether the initial second code contains a key change token specified in the migration path; the compilation reward is used to verify the syntactic correctness of the initial second code in the target version environment through an automated test script; and the semantic similarity reward is used to calculate the semantic similarity between the initial second code and the reference code, wherein the reference code is the target version code obtained through manual migration.
[0150] This embodiment of the disclosure effectively improves the accuracy and reliability of code migration by inputting path information into the initial model and fine-tuning it using reinforcement learning based on a composite reward function. By introducing a token reward, it ensures that the generated second code accurately includes the key syntax or changes required for version migration, avoiding omissions of core modifications. Automated testing and verification of the compilation reward ensures the syntactic correctness and executableness of the generated code in the target version environment, reducing manual debugging costs. Simultaneously, by comparing the semantic similarity reward with manually migrated reference code, the generated code maintains a high degree of consistency with the expected results in terms of functional logic, improving the semantic integrity of the code. Ultimately, it achieves automated, high-fidelity code version migration, significantly reducing the errors and time overhead of manual migration, and enhancing the practicality and stability of the model output.
[0151] To implement the above embodiments, this disclosure also provides an electronic device.
[0152] refer to Figure 7 The diagram illustrates a structural schematic of an electronic device 900 suitable for implementing embodiments of the present disclosure. The electronic device 900 can be a terminal device or a server. The terminal device can include, but is not limited to, mobile terminals such as mobile phones, laptops, digital broadcast receivers, personal digital assistants (PDAs), tablet computers, portable media players (PMPs), and in-vehicle terminals (e.g., in-vehicle navigation terminals), as well as fixed terminals such as digital TVs and desktop computers. Figure 7 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments disclosed herein.
[0153] like Figure 7 As shown, the electronic device 900 may include a processing unit (e.g., a central processing unit, a graphics processing unit, etc.) 901, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 902 or a program loaded from a storage device 908 into a random access memory (RAM) 903. The RAM 903 also stores various programs and data required for the operation of the electronic device 900. The processing unit 901, ROM 902, and RAM 903 are interconnected via a bus 904. An input / output (I / O) interface 905 is also connected to the bus 904.
[0154] Typically, the following devices can be connected to I / O interface 905: input devices 906 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 907 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; storage devices 908 including, for example, magnetic tapes, hard disks, etc.; and communication devices 909. Communication device 909 allows electronic device 900 to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 7 An electronic device 900 with various devices is shown; however, it should be understood that it is not required to implement or possess all of the devices shown. More or fewer devices may be implemented or possessed alternatively.
[0155] In particular, according to embodiments of this disclosure, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable storage medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via a communication device 909, or installed from a storage device 908, or installed from a ROM 902. When the computer program is executed by a processing device 901, it performs the functions defined in the methods of embodiments of this disclosure.
[0156] It should be noted that the computer-readable storage medium described in this disclosure can be a computer-readable signal medium, a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable storage medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable storage medium can be transmitted using any suitable medium, including but not limited to: wires, optical fibers, RF (radio frequency), etc., or any suitable combination thereof.
[0157] The aforementioned computer-readable storage medium may be included in the aforementioned electronic device; or it may exist independently and not assembled into the electronic device.
[0158] The aforementioned computer-readable storage medium carries one or more programs, which, when executed by the electronic device, cause the electronic device to perform the method shown in the above embodiments.
[0159] Computer program code for performing the operations of this disclosure can be written in one or more programming languages or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, and C++, and conventional procedural programming languages such as the "C" language or similar programming languages. The program code can be executed entirely on the user's computer, partially on the user's computer, as a standalone software package, partially on the user's computer and partially on a remote computer, or entirely on a remote computer or server. In cases involving remote computers, the remote computer can be connected to the user's computer via any type of network—including a Local Area Network (LAN) or a Wide Area Network (WAN)—or can be connected to an external computer (e.g., via the Internet using an Internet service provider).
[0160] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in the block diagrams and / or flowcharts, and combinations of blocks in the block diagrams and / or flowcharts, can be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0161] The units described in the embodiments of this disclosure can be implemented in software or in hardware. The name of a unit does not necessarily limit the unit itself; for example, the first acquisition unit can also be described as "a unit that acquires at least two Internet Protocol addresses".
[0162] The functions described above in this document can be performed at least in part by one or more hardware logic components. For example, exemplary types of hardware logic components that can be used, without limitation, include: field-programmable gate arrays (FPGAs), application-specific integrated circuits (ASICs), application-specific standard products (ASSPs), system-on-a-chip (SoCs), complex programmable logic devices (CPLDs), and so on.
[0163] The electronic devices, computer-readable storage media, and computer program products provided in this disclosure, by introducing a first association relationship as a structured semantic guide and inputting the first association relationship into the generative model, can effectively improve the accuracy and controllability of version migration. This reduces reliance on massive amounts of labeled data, and by explicitly modeling complex change relationships between versions (such as splitting and merging), the generated second code maintains semantic consistency while remaining syntactically compatible, thus reducing migration costs.
[0164] The above description is merely a preferred embodiment of this disclosure and an explanation of the technical principles employed. Those skilled in the art should understand that the scope of this disclosure is not limited to technical solutions formed by specific combinations of the above-described technical features, but should also cover other technical solutions formed by arbitrary combinations of the above-described technical features or their equivalents without departing from the above-described concept. For example, technical solutions formed by substituting the above features with (but not limited to) technical features disclosed in this disclosure that have similar functions.
[0165] Furthermore, while the operations are described in a specific order, this should not be construed as requiring these operations to be performed in the specific order shown or in a sequential order. In certain environments, multitasking and parallel processing may be advantageous. Similarly, while several specific implementation details are included in the above discussion, these should not be construed as limiting the scope of this disclosure. Certain features described in the context of individual embodiments may also be implemented in combination in a single embodiment. Conversely, various features described in the context of a single embodiment may also be implemented individually or in any suitable sub-combination in multiple embodiments.
[0166] Although the subject matter has been described using language specific to structural features and / or methodological logic, it should be understood that the subject matter defined in the appended claims is not necessarily limited to the specific features or actions described above. Rather, the specific features and actions described above are merely illustrative examples of implementing the claims.
Claims
1. A code generation method, comprising: Obtain the first data corresponding to the first code; the first data includes the first association relationship; The first association indicates the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version of the first code, and the second knowledge graph corresponds to the second version of the first code; Input the first data into the first model to obtain the second code under the second version.
2. The method according to claim 1, wherein the mapping relationship includes at least one of the following: node splitting, node merging, renaming, retention, and adding parameters.
3. The method according to claim 1, wherein the first data further includes at least one of the following: first code, second version information.
4. The method according to any one of claims 1-3, further comprising: Obtain the first knowledge graph of the first version and the second knowledge graph of the second version corresponding to the first code; The first subgraph is determined by retrieving the first knowledge graph based on the first code; The second subgraph is determined by retrieving the second knowledge graph based on the first code; Map the first subgraph and the second subgraph to obtain the first association relationship.
5. The method according to claim 4, wherein mapping the first subgraph and the second subgraph to obtain the first association relationship includes: For each first node in the first subgraph, if there is a second node in the second subgraph that has a semantic equivalence relationship with the first node, then the first node and the second node are associated one-to-one. If there is a third node in the second subgraph that has a semantic transformation relationship with the first node, then an intermediate node is created, and the first node and the third node are associated through the intermediate node. The intermediate node is used to represent the semantic transformation relationship. The semantic transformation relationship includes at least one of the following: functional splitting relationship, functional merging relationship, and parameter adaptation relationship.
6. The method according to any one of claims 1-3, wherein inputting the first data into the first model to obtain the second code under the second version comprises: Input the first association relationship into the second model to obtain path information; The path information includes the processing order and operation type of multiple processing steps; The path information is input into the first model to obtain the second code under the second version.
7. The method according to claim 6, wherein the path information includes at least one of the following: Description information for each processing step based on natural language description; Description information of each processing step based on the control token; Descriptive information for each processing step based on structured description.
8. The method according to claim 6, wherein the path information includes description information of each processing step based on a structured description; the step of inputting the path information into the first model to obtain the second code under the second version includes: Convert the path information into description information in the target format; The target format includes at least one of the following: natural language description, control token; Input the path description in the target format into the first model to obtain the second code under the second version.
9. The method according to claim 6, wherein inputting the path information into the first model to obtain the second code under the second version comprises: The path information is input into the first model to obtain intermediate code corresponding to each of the processing steps and second code under the second version.
10. A code generation device, comprising: The acquisition module is used to acquire the first data corresponding to the first code; the first data includes a first association relationship; The first association relationship includes the mapping relationship between nodes in the first knowledge graph and nodes in the second knowledge graph; the first knowledge graph corresponds to the first version of the first code, and the second knowledge graph corresponds to the second version of the first code; The input module is used to input the first data into the first model to obtain the second code under the second version.
11. An electronic device, comprising: Processor and memory; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory, causing the processor to perform the code generation method as described in any one of claims 1 to 9.
12. A computer-readable storage medium storing computer-executable instructions that, when executed by a processor, implement the code generation method as described in any one of claims 1 to 9.
13. A computer program product comprising a computer program that, when executed by a processor, implements the code generation method as described in any one of claims 1 to 9.