Code translation method and device based on large language model
Through the large language model-driven architecture understanding, task decomposition and coding debugging workflow, the problem of warehouse-level code translation in the existing technology is solved, and efficient warehouse-level code translation and cost reduction are achieved.
Patent Information
- Application Number
- CN202510477303.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-04-16
- Publication Date
- 2025-07-25
AI Technical Summary
Existing code translation tools and technologies can only effectively handle simple function-level tasks, and cannot implement warehouse-level source code translation, resulting in high repetitive development costs.
Through architecture understanding workflow, code translation task decomposition workflow and coding debugging workflow, we use large language model (LLM) to build warehouse-level code translation methods, including UML class diagram construction, task planning and code generation, and combined with the agent collaborative workflow to achieve warehouse-level code translation.
It breaks through the bottleneck of function-level translation, realizes warehouse-level code translation, and reduces the translation threshold and cost.
Smart Images

Figure BDA0005361716810000091 
Figure BDA0005361716810000101 
Figure BDA0005361716810000102
Abstract
Description
Technical Field
[0001] The present invention belongs to the field of computer technology, and particularly relates to a method and device for code translation based on a large language model. Background Art
[0002] With the development of computer technology, many excellent codes written in the early days now face the problem of language obsolescence. These codes often use programming languages that are no longer widely taught or used. Migrating them to a new language can avoid the cost of repeated development. However, existing code translation tools and technologies, such as large language models (LLMs), can only effectively handle tasks at the simple function level; binary-based code translation and assembly code translation are limited to specific application scenarios. Therefore, a new method is needed to achieve source code translation tasks at the repository level. Summary of the Invention
[0003] The purpose of the present invention is to provide a method and device for code translation based on a large language model in view of the deficiencies of the prior art.
[0004] The purpose of the present invention is achieved through the following technical solutions: A method for code translation based on a large language model includes the following steps:
[0005] An end user initiates a code translation task to a configured agent through a web front-end, driven by a large language model; the code translation task includes an architecture understanding workflow, a code translation task decomposition workflow, and a coding and debugging workflow;
[0006] The construction methods of the architecture understanding workflow, the code translation task decomposition workflow, and the coding and debugging workflow are based on the same set of frameworks; the framework includes work nodes and a workflow that links the work nodes;
[0007] The work nodes can process the input of the context and output the corresponding task results.
[0008] Further, the architecture understanding workflow is to construct a UML class diagram for the code library, abstract the core architecture information layer by layer, and form an architecture design document O1 of the code library;
[0009] The code translation task decomposition workflow is to perform code translation task decomposition according to the architecture design document O1 of the code library in combination with the requirement document P1 to form a task planning list O2;
[0010] The coding and debugging workflow is to sequentially extract the elements in the task planning list O2 and perform coding and debugging work to complete the code translation and debugging work.
[0011] Further, the UML class diagram of the code library is constructed, and the core architecture information is abstracted layer by layer to form the architecture design document O1 of the code library, specifically as follows:
[0012] (a.1) Construct the repository UML class diagram: The working node first starts the UML generation tool integration module and calls the large language model to preprocess and semantically parse the code structure, annotation information, and related metadata of the code library to provide more accurate context understanding for the UML generation tool; The UML generation tool communicates with the large language model in real time based on the code files of the code library, and the large language model provides explanations and prompts for complex logic, design patterns, etc. in the code to help the UML generation tool construct the complete UML class diagram of the code library;
[0013] (a.2) The working node calls the intelligent agent, which uses the large language model to deeply analyze the complete UML class diagram; The large language model provides semantic association analysis between classes, function relevance evaluation, and business importance judgment capabilities; The intelligent agent understands the semantic meaning of the inheritance hierarchy through the large language model and combines the call logs, test case coverage in the code library, and the language model to analyze these data to determine the usage frequency to extract the core key classes and feedback them to the working node;
[0014] (a.3) The working node calls the intelligent agent, which is based on the construction method of the complete UML class diagram and closely cooperates with the large language model, using the understanding ability of the large language model to understand the construction logic and rules of the complete UML class diagram and pass them to the intelligent agent; The intelligent agent limits the scope of the complete UML class diagram to the core key classes. During the process, the large language model performs semantic checks and optimizations on the association relationships, attributes, and methods between classes to ensure that the UML class diagram of the core key classes obtained is accurate and conforms to the business semantics; Finally, the UML class diagram of the core key classes is generated and feedback to the working node;
[0015] (a.4) The working node calls the intelligent agent, which takes the UML class diagram of the core key classes as input and combines the knowledge base and reasoning ability of the large language model; The large language model provides supplementary domain knowledge to help the intelligent agent understand the business concepts, entity relationships, and business rules in the class diagram; The intelligent agent summarizes the conceptual model and the interaction relationships of the conceptual model, summarizes the conceptual model and the interaction relationships of the conceptual model and feedbacks them to the working node;
[0016] (a.5) The working node invokes the agent. Based on the UML class diagram of the core key classes, the agent first uses a large language model to perform semantic analysis on the interfaces in the class diagram and extracts the interface functions of the core key classes. Then the agent invokes the function definition query tool, which interacts with the large language model. The large language model optimizes and semantically expands the query request to improve the accuracy of the query. After the function definition query tool obtains the specific code information of the interface functions, the agent combines this information and uses the large language model to sort out and summarize the business processes, forms a description of the core business processes, and feeds it back to the working node;
[0017] (a.6) The working node invokes the agent. The agent works in collaboration with the large language model. The large language model provides strategic suggestions for class diagram simplification. The agent simplifies the UML class diagram of the core key classes according to these suggestions, removes the detailed information, and retains the key data members and methods, forms a summary UML class diagram of the code library, and feeds it back to the working node;
[0018] (a.7) The working node invokes the agent. The agent receives the conceptual model summarized in step (a.4), the description of the core business processes formed in step (a.5), and the summary UML class diagram of the code library formed in step (a.6), and uses the large language model to perform text integration, format layout, and content optimization, and summarizes this information into a complete code library architecture design document O1.
[0019] Furthermore, combining the architecture design document O1 of the code library with the requirements document P1, perform task decomposition for the code translation task to form a task planning list O2, specifically:
[0020] (b.1) Generate a translation plan: The working node first invokes the large language model, using the code library architecture design document O1 generated by the architecture understanding workflow and the requirements document P1 as inputs. The large language model deeply analyzes the code library architecture design document O1 and the requirements document P1, identifies the mapping relationships, functional corresponding points, and potential technical difficulties between the two. At the same time, the agent in the working node starts. Based on the analysis results of the large language model, the agent combines the preset code translation rule library and industry best practices to guide the large language model to generate a preliminary framework for the code translation plan. The large language model further refines the content of the plan on the basis of the preliminary framework for the code translation plan, including translation strategies, technical selection suggestions, and risk assessments, and finally generates a complete code translation plan;
[0021] (b.2) Extract main tasks: The worker node passes the generated complete code translation plan to the agent. The agent uses natural language processing technology to parse the complete code translation plan and extract the main translation tasks described in the complete code translation plan. During the recognition process, the large language model serves as an auxiliary tool to provide semantic extension and ambiguity elimination support for the agent to ensure accurate extraction of all main translation tasks. The agent stores the extracted main translation tasks in a structured format and feeds them back to the worker node;
[0022] (b.3) Decompose the main task: The worker node calls the agent for the extracted main translation task. The agent combines the knowledge base and domain model of the large language model to carefully decompose the main translation task into multiple executable subtasks. During the decomposition process, the large language model is responsible for providing the logical relationships, dependencies, and potential boundary condition analyses between the subtasks. At the same time, the agent uses the code query tool to refine and evaluate each subtask, obtains the code details of the relevant dependent tasks, and feeds them back to the worker node;
[0023] (b.4) Task translation type evaluation: The worker node passes the decomposed subtasks to the agent. The agent uses the large language model to perform semantic analysis on each subtask, combines the preset translation type classification criteria, evaluates the translation type of each subtask, and feeds it back to the worker node. During the process, the large language model provides predictions of translation difficulty, suggestions for translation strategies, and early warnings of potential problems;
[0024] (b.5) Summarize and sort task module: After receiving the evaluation results of the translation types of each subtask, the worker node calls the agent. The agent uses the large language model to summarize and organize the evaluation results of the translation types of the subtasks, and summarizes the subtasks belonging to the same translation type or module. At the same time, the agent combines the dependencies, priorities, and resource constraint conditions between the tasks to sort the summarized tasks, forms a task planning list O2, and feeds it back to the worker node. During the process, the large language model provides optimization suggestions for the sorting algorithm, resource allocation strategies, and evaluations of potential risks.
[0025] Further, the translation type is individual translation, combined translation, or no translation.
[0026] Further, the individual translation means that for a single function or model within the capabilities of the LLM, it can be independently translated;
[0027] The combined translation means that for simple tasks, a simple task belonging to the same category, or a simple task belonging to the same module, it can be optionally combined with other tasks for translation;
[0028] For tasks where translation is not required to complete the target translation or is unnecessary, translation can be skipped.
[0029] Further, the elements in the task planning list O2 are sequentially extracted, and code translation and debugging work are performed through encoding and debugging. Specifically:
[0030] (c.1) Code Dependency Query: The large language model in the worker node serves as the core processing unit. According to the task planning list O2, it calls the code query tool to retrieve the detailed code of the specified function or module to obtain the complete content to be translated. During this process, the large language model is responsible for handling the semantic understanding related to the task planning, accurately conveying the query requirements to the code query tool, and performing preliminary semantic parsing and sorting of the query results.
[0031] (c.2) Code Generation: The large language model in the worker node generates the code details of the target language based on the task objective, the code details of the target language, and the framework structure information of the existing code library using its powerful code generation ability. Subsequently, the first intelligent agent acts as a software engineer and the second intelligent agent acts as a senior software engineer to participate in the code review process: The first intelligent agent, based on the code details generated by the large language model and combined with its own code writing experience and knowledge, conducts a preliminary review of the code and provides optimization suggestions. The second intelligent agent evaluates and provides improvement opinions on the architecture, performance, and maintainability of the code. The first intelligent agent and the second intelligent agent communicate with each other through the dialogue system built by the large language model. The large language model is responsible for understanding the intentions of both parties, integrating the dialogue content, and generating new dialogue content until complete and accurate code is generated, at which point the dialogue ends. Finally, the worker node aggregates and outputs the final translated code.
[0032] (c.3) Code Editing: The worker node writes the final translated code into the code library to obtain an updated code library. During the writing process, the third intelligent agent acts as a software engineer and calls the file editing tool to write the final translated code into the code library and provides a DIFF preview result for the fourth intelligent agent to review: If the review passes, the save modification tool is called to save the edit, that is, the final translated code is written into the code library to obtain an updated code library. If the review fails, the rollback tool is called to cancel the edit, that is, the final translated code is not written into the code library.
[0033] (c.4) Build and Debug: After writing the finally translated code into the code library, the worker node calls the build and compilation tools to compile and test the updated code library; the large language model is responsible for configuring the parameters of the build and compilation test and feeding back the results of the build and compilation test to the worker node; after the build and compilation test passes, it then calls the use cases to carry out running and debugging: If an error occurs during the compilation or debugging process, the fifth intelligent agent in the worker node plays the role of fault location and repair, locates the problem by calling the code query tool provided by the large language model and modifies the error using the file editing tool;; after the error modification is completed, the build and compilation test is carried out again until the build and compilation test of the updated code library is all completed, or the number of error modification reaches the upper limit, and the code translation and debugging work is ended; during the process, the large language model, as the core coordinator, is responsible for the interaction between intelligent agents, the invocation of tools, and the processing and transmission of information.
[0034] The present invention also includes a device for code translation based on a large language model, comprising a memory and one or more processors, wherein executable code is stored in the memory, and when the one or more processors execute the executable code, it is used for the method of code translation based on a large language model as described above.
[0035] The present invention also includes a computer-readable storage medium, on which a program is stored, and when the program is executed by a processor, it implements the method of code translation based on a large language model as described above.
[0036] The beneficial effects of the present invention are:
[0037] 1) It realizes code translation at the repository level, breaking through the bottleneck that the LLM can only translate function-level code;
[0038] 2) The application of the present invention can greatly reduce the translation threshold and the cost of code translation. BRIEF DESCRIPTION OF THE DRAWINGS
[0039] Figure 1 It is a flowchart of the architecture understanding workflow;
[0040] Figure 2 It is a flowchart of the code translation task decomposition workflow;
[0041] Figure 3 It is a flowchart of the coding and debugging workflow;
[0042] Figure 4 It is a schematic diagram of the function introduction of the pilot project;
[0043] Figure 5 It is a structural diagram of a device for code translation based on a large language model. DETAILED DESCRIPTION OF THE EMBODIMENTS
[0044] To make the objectives, technical solutions and advantages of the present invention more clearly understood, the present invention will be further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention, rather than all embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present invention without creative efforts shall fall within the protection scope of the present invention.
[0045] Embodiment 1
[0046] The present invention provides a method for code translation based on a large language model, including the following steps:
[0047] The end user initiates a code translation task to a configured agent through a web front end, driven by a large language model; the code translation task includes an architecture understanding workflow, a code translation task decomposition workflow, and a coding and debugging workflow;
[0048] The construction methods of the architecture understanding workflow, the code translation task decomposition workflow, and the coding and debugging workflow are based on the same set of frameworks; the framework includes work nodes and a workflow for linking work nodes.
[0049] The architecture understanding workflow is to construct a UML class diagram for the code library, abstract the core architecture information layer by layer, and form an architecture design document O1 of the code library.
[0050] The code translation task decomposition workflow is to perform code translation task decomposition according to the architecture design document O1 of the code library in combination with the requirements document P1, and form a task planning list O2.
[0051] The coding and debugging workflow is to sequentially extract the elements in the task planning list O2 and perform coding and debugging work to complete the code translation and debugging work.
[0052] The work node can process the input of the context and output the corresponding task result.
[0053] The work node generally consists of two or more agents (of course, there can be no agents, and only specific tools are configured to handle fixed tasks, such as file merging operations. Without agents, the LLM ability cannot be invoked).
[0054] The agent can invoke the LLM or specific tools, process the input information, and provide feedback.
[0055] Each agent undertakes a certain role. For example, Agent A plays the role of the user, and Agent B plays the role of the assistant. They have a conversation. The agents have a conversation around the task goal. The conversation ends until the user role believes that the task goal has been achieved.
[0056] After the conversation ends, the working node calls the LLM to summarize the historical conversation as the output of the current working node.
[0057] A workflow links working nodes with different task goals, and its execution order follows the topological dependency relationship of a directed acyclic graph.
[0058] As Figure 1 shown, the UML class diagram of the code library is constructed, and the core architecture information is abstracted layer by layer to form the architecture design document O1 of the code library, specifically as follows:
[0059] (a.1) Construct the repository UML class diagram: The working node first starts the UML generation tool integration module and calls the large language model to preprocess and semantically parse the code structure, annotation information, and related metadata of the code library to provide more accurate context understanding for the UML generation tool; The UML generation tool communicates with the large language model in real time based on the code files of the code library, and the large language model provides explanations and hints for complex logic, design patterns, etc. in the code to help the UML generation tool construct the complete UML class diagram of the code library.
[0060] (a.2) The working node calls an agent, which uses the large language model to deeply analyze the complete UML class diagram; The large language model provides the ability to analyze semantic associations between classes, evaluate functional relevance, and judge business importance; The agent understands the semantic meaning of the inheritance hierarchy through the large language model and combines the call logs, test case coverage in the code library, and the language model to analyze these data to determine the usage frequency to extract core key classes and feedback them to the working node.
[0061] (a.3) The working node calls an agent, which is based on the construction method of the complete UML class diagram and closely cooperates with the large language model, uses the understanding ability of the large language model to understand the construction logic and rules of the complete UML class diagram and passes them to the agent; The agent limits the scope of the complete UML class diagram to core key classes. During the process, the large language model performs semantic checks and optimizations on the association relationships, attributes, and methods between classes to ensure that the UML class diagram of the core key classes is accurate and conforms to business semantics; Finally, the UML class diagram of the core key classes is generated and feedback to the working node.
[0062] (a.4) The working node calls an agent, which uses the UML class diagram of the core key classes as input and combines the knowledge base and reasoning ability of the large language model; The large language model provides supplementary domain knowledge to help the agent understand the business concepts, entity relationships, and business rules in the class diagram; The agent summarizes the conceptual model and the interaction relationships of the conceptual model and feedbacks them to the working node.
[0063] (a.5) The working node calls the intelligent agent, which first uses the large language model to perform semantic analysis on the interface in the class diagram based on the UML class diagram of the core key class, and extracts the interface functions of the core key class; then the intelligent agent calls the function definition query tool, which interacts with the large language model. The large language model optimizes and semantically expands the query request to improve the accuracy of the query; after the function definition query tool obtains the specific code information of the interface function, the intelligent agent combines this information and uses the large language model to sort out and summarize the business process, forming a core business process description and feeding it back to the working node.
[0064] (a.6) The working node calls the intelligent agent, which works in collaboration with the large language model. The large language model provides strategic suggestions for class diagram simplification. The intelligent agent simplifies the UML class diagrams of core key classes based on these suggestions, removes detailed information, retains key data members and methods, forms a summary UML class diagram of the code base, and feeds it back to the working node.
[0065] (a.7) The working node calls the intelligent agent, which receives the conceptual model summarized in step (a.4), the core business process description formed in step (a.5), and the outline UML class diagram of the code base formed in step (a.6), and uses the large language model to perform text integration, formatting and content optimization, and summarizes this information into a complete code base architecture design document O1.
[0066] like Figure 2 As shown, the code translation task is decomposed according to the architecture design document O1 of the code base combined with the requirement document P1 to form a task planning list O2, which is specifically:
[0067] (b.1) Generate translation plan: The working node first calls the large language model and takes the code base architecture design document O1 and the requirement document P1 generated by the architecture understanding workflow as input; the large language model uses its powerful semantic understanding and context association capabilities to conduct an in-depth analysis of the code base architecture design document O1 and the requirement document P1, and identifies the mapping relationship, functional correspondence points, and potential technical difficulties between the two; at the same time, the intelligent agent in the working node is started, which guides the large language model to generate a preliminary code translation plan framework based on the analysis results of the large language model, combined with the preset code translation rule library and industry best practices; based on the preliminary code translation plan framework, the large language model further refines the plan content, including translation strategy, technology selection recommendations, and risk assessment, and finally generates a complete code translation plan.
[0068] (b.2) Extract the main tasks: The worker node passes the generated complete code translation plan to the agent. The agent uses natural language processing technology to parse the complete code translation plan and extract the main translation tasks described in the complete code translation plan. During the recognition process, the large language model serves as an auxiliary tool to provide semantic extension and ambiguity elimination support for the agent to ensure that all main translation tasks are accurately extracted. The agent stores the extracted main translation tasks in a structured format and feeds them back to the worker node.
[0069] (b.3) Decompose the main tasks: The worker node calls the agent for the extracted main translation tasks. The agent combines the knowledge base and domain model of the large language model to carefully decompose the main translation tasks into multiple executable subtasks. During the decomposition process, the large language model is responsible for providing the logical relationships, dependencies, and potential boundary condition analyses between the subtasks. At the same time, the agent uses the code query tool to refine and evaluate each subtask, obtains the code details of the relevant dependent tasks, and feeds them back to the worker node.
[0070] (b.4) Task translation type evaluation: The worker node passes the decomposed subtasks to the agent. The agent uses the large language model to perform semantic analysis on each subtask, combines the preset translation type classification criteria, evaluates the translation type of each subtask, and feeds it back to the worker node. During the process, the large language model provides predictions of translation difficulty, suggestions for translation strategies, and warnings of potential problems.
[0071] The translation types are separate translation, combined translation, or no translation.
[0072] The separate translation means that for a single function or model within the capabilities of the LLM, it can be translated independently.
[0073] The combined translation means that for simple tasks, a simple task belonging to the same category, or a simple task belonging to the same module, it can be selected to be translated in combination with other tasks.
[0074] The no translation means that for tasks that complete the target translation or have no need for translation, it can be selected not to be translated.
[0075] (b.5) Summarize and sort the task module: After the worker node receives the evaluation results of the translation types of each subtask, it calls the agent. The agent uses the large language model to summarize and organize the evaluation results of the translation types of the subtasks, and summarizes the subtasks belonging to the same translation type or module. At the same time, the agent combines the dependencies, priorities, and resource constraints between the tasks to sort the summarized tasks, forms a task planning list O2, and feeds it back to the worker node. During the process, the large language model provides optimization suggestions for the sorting algorithm, resource allocation strategies, and evaluations of potential risks.
[0076] As Figure 3 shown, the elements in the task planning list O2 are sequentially extracted, and the code translation and debugging work is completed by performing coding and debugging tasks, specifically as follows:
[0077] (c.1) Code Dependency Query: The large language model in the worker node serves as the core processing unit. According to the task planning list O2, it calls the code query tool to retrieve the detailed code of the specified function or module to obtain the complete content that needs to be translated. During the process, the large language model is responsible for processing the semantic understanding related to the task, accurately communicating the query requirements to the code query tool, and performing preliminary semantic parsing and sorting of the query results.
[0078] (c.2) Code Generation: The large language model in the worker node generates the code details of the target language based on the task objective, the code details of the target language, and the framework structure information of the existing code library using its powerful code generation ability. Subsequently, the first intelligent agent plays the role of a software engineer and the second intelligent agent plays the role of a senior software engineer to participate in the code review process: The first intelligent agent conducts a preliminary review and provides optimization suggestions for the code based on the code details generated by the large language model, combined with its own code writing experience and knowledge. The second intelligent agent evaluates and provides improvement suggestions regarding the architecture, performance, and maintainability of the code. The first intelligent agent and the second intelligent agent communicate with each other through the dialogue system built by the large language model. The large language model is responsible for understanding the intentions of both parties, integrating the dialogue content, and generating new dialogue content until complete and accurate code is generated, at which point the dialogue ends. Finally, the worker node aggregates and outputs the final translated code.
[0079] (c.3) Edit Code: The worker node writes the final translated code into the code library to obtain an updated code library. During the writing process, the third intelligent agent plays the role of a software engineer and calls the file editing tool to write the final translated code into the code library, and provides a DIFF preview result for the fourth intelligent agent to review: If the review passes, the save modification tool is called to save the edit, that is, the final translated code is written into the code library to obtain an updated code library; if the review fails, the rollback tool is called to cancel the edit, that is, the final translated code is not written into the code library.
[0080] (c.4) Build and Debug: After completing the writing of the final translated code into the code library, the worker node calls the build and compilation tools to compile and test the updated code library; the large language model is responsible for configuring the parameters of the build and compilation test and feeding back the results of the build and compilation test to the worker node; after the build and compilation test passes, it then calls the use cases to conduct running and debugging: If an error occurs during the compilation or debugging process, the fifth agent in the worker node plays the role of fault location and repair, locates the problem by calling the code query tool provided by the large language model and modifies the error using the file editing tool;; after the error modification is completed, the build and compilation test is performed again until the build and compilation test of the updated code library is completed, or the number of error modifications reaches the upper limit, and the code translation and debugging work is ended; during the process, the large language model, as the core coordinator, is responsible for the interaction between agents, the invocation of tools, and the processing and transmission of information.
[0081] Example 2
[0082] As Figure 4 shown, specifically taking the GalSim astronomical galaxy simulation as an example project, its specific implementation method is elaborated. The underlying layer of the GalSim library is implemented in C++, and the upper layer is encapsulated in Python. Its main function is to render and simulate the brightness model to generate images. The rendering methods include Real domain rendering, FFT domain rendering, and Photon Shoot rendering. Our goal is to translate the underlying C++ implementation of Photon Shoot rendering into a CUDA implementation that can be accelerated on the GPU graphics card.
[0083] To summarize in one sentence: Translate the C++ code of Photon Shoot rendering in the GalSim library into CUDA code.
[0084] The tools required for C++ to CUDA translation are shown in Table 1.
[0085] Table 1: Toolset
[0086]
[0087]
[0088] The task settings, agent role settings, and supporting tools of the worker node are shown in Table 2.
[0089] Table 2: Task settings, agent role settings, and supporting tools of the worker node
[0090]
[0091]
[0092]
[0093]
[0094] The context between the nodes in Table 2 is passed through files.
[0095] The current task prompt words in Table 2 reflect the main goal, but they have to be provided to the agent on the working node in combination with the specific background and context in order for the LLM of the agent to understand them better.
[0096] The complete task prompt word template for each working node is as follows:
[0097] msg_template = "'
[0098] ## Project Background
[0099] {background}
[0100] ## Goal
[0101] {target}
[0102] ## Task Context
[0103] {context}
[0104] ## Task
[0105] {task}
[0106] "'
[0107] Among them, background is the common content of the project, target is a specific translation task, context is provided by the dependent working node, and task is the current task prompt word.
[0108] For example, the complete task goal prompt word for the code dependency query working node is as follows:
[0109] ## Project Background
[0110] Galsim is a project for simulating astronomical optical imaging. The brightness model SBprofie is used to construct the ImageView image through optical simulation.
[0111] The underlying layer of the Galsim project is developed in C++, and is provided to users through a Python interface. There are bottlenecks in the rendering simulation, especially the photon shooting method, which has a large amount of calculation.
[0112] In order to improve performance, it is decided to expand the Galsim function and use CUDA to accelerate the photon shooting method.
[0113] Project Path: / home / code / GalSim /
[0114] Source File Path: / home / code / GalSim / src /
[0115] Header File Path: / home / code / GalSim / include /
[0116] namespace: galsim
[0117] Goal:
[0118] Migrate the galsim::PhotonArray module, which involves functions: galsim::PhotonArray::scaleFlux, galsim::PhotonArray::scaleXY, galsim::PhotonArray::convolve, galsim::PhotonArray::getTotalFlux, galsim::PhotonArray::convolveShuffle
[0119] ## Task Context
[0120] CUDA Migration Code: / home / workspace / galsim_code_edit / node2_genCode.md
[0121] ## Task
[0122] Write the generated CUDA migration code into the code library in sequence.
[0123] Configure the "architecture understanding", "task grading", and "coding and debugging" workflows according to the working node configuration in Table 2 above. At the same time, complete the code translation work according to the architecture understanding workflow, the code translation task decomposition workflow, and the coding and debugging workflow.
[0124] Example 3: This example relates to an apparatus for code translation based on a large language model, including a memory and one or more processors. Executable code is stored in the memory. When the one or more processors execute the executable code, it is used for the method of code translation based on a large language model in Example 1 above. The apparatus embodiment can be applied to any device with data processing capabilities, and the any device with data processing capabilities can be a device or apparatus such as a computer.
[0125] As Figure 5, at the hardware level, the knowledge distillation device includes a processor, an internal bus, a network interface, a memory, and a non-volatile memory. Of course, it may also include other hardware required for other services. The processor reads the corresponding computer program from the non-volatile memory into the memory and then runs it to implement the above Figure 1 method shown. Of course, in addition to the software implementation, the present invention does not exclude other implementation methods, such as logic devices or a combination of software and hardware, etc. That is to say, the execution subject of the following processing flow is not limited to each logic unit, and can also be hardware or logic devices.
[0126] Improvements to a technology can be clearly distinguished as either hardware improvements (e.g., improvements to circuit structures such as diodes, transistors, switches, etc.) or software improvements (improvements to method flows). However, with the development of technology, many improvements to method flows today can be regarded as direct improvements to hardware circuit structures. Almost all designers obtain the corresponding hardware circuit structure by programming the improved method flow into the hardware circuit. Therefore, it cannot be said that an improvement to a method flow cannot be implemented using a hardware entity module. For example, a Programmable Logic Device (PLD) (such as a Field Programmable Gate Array (FPGA)) is such an integrated circuit whose logical function is determined by the user programming the device. Designers can program themselves to "integrate" a digital system onto a single PLD, without having to ask a chip manufacturer to design and fabricate a dedicated integrated circuit chip. Moreover, nowadays, instead of manually fabricating integrated circuit chips, this programming is mostly implemented using "logic compiler" software, which is similar to the software compiler used in program development and writing. The original code before compilation also has to be written in a specific programming language, which is called a Hardware Description Language (HDL), and there is not just one type of HDL, but many types, such as ABEL (Advanced Boolean Expression Language), AHDL (Altera Hardware Description Language), Confluence, CUPL (Cornell University Programming Language), HDCal, JHDL (Java Hardware Description Language), Lava, Lola, MyHDL, PALASM, RHDL (Ruby Hardware Description Language), etc. Currently, the most commonly used are VHDL (Very-High-Speed Integrated Circuit Hardware Description Language) and Verilog. Those skilled in the art should also be aware that by simply performing some logical programming on the method flow using the above-mentioned several hardware description languages and programming it into the integrated circuit, it is easy to obtain the hardware circuit that implements the logical method flow.
[0127] The controller can be implemented in any suitable manner. For example, the controller can take the form of, for example, a microprocessor or a processor and a computer-readable medium storing computer-readable program code (such as software or firmware) executable by the (micro)processor, logic gates, switches, an application specific integrated circuit (ASIC), a programmable logic controller, and an embedded microcontroller. Examples of the controller include, but are not limited to, the following microcontrollers: ARC 625D, Atmel AT91SAM, Microchip PIC18F26K20, and Silicone Labs C8051F320. The memory controller can also be implemented as part of the control logic of the memory. Those skilled in the art also know that, in addition to implementing the controller in the form of pure computer-readable program code, it is entirely possible to logically program the method steps to enable the controller to be implemented in the form of logic gates, switches, application specific integrated circuits, programmable logic controllers, embedded microcontrollers, etc. to achieve the same function. Therefore, such a controller can be considered a hardware component, and the devices included therein for implementing various functions can also be regarded as the structures within the hardware component. Or even, the devices for implementing various functions can be regarded as either software modules for implementing the method or the structures within the hardware component.
[0128] The systems, devices, modules, or units illustrated in the above embodiments can be specifically implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer can be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or any combination of these devices.
[0129] It should also be noted that the term "comprising", "including", or any other variant thereof is intended to cover non-exclusive inclusion, such that a process, method, commodity, or device comprising a series of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such process, method, commodity, or device. Without further limitation, an element defined by the statement "comprising an..." does not exclude the existence of additional identical elements in the process, method, commodity, or device comprising the element.
[0130] Those skilled in the art should understand that the embodiments of the present invention can be provided as a method, a system, or a computer program product. Therefore, the present invention can take the form of a complete hardware embodiment, a complete software embodiment, or an embodiment combining software and hardware aspects. Moreover, the present invention can take the form of a computer program product implemented on one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that contain computer-usable program code.
[0131] The present invention can be described in the general context of computer-executable instructions executed by a computer, such as program modules. Generally, program modules include routines, programs, objects, components, data structures, etc. that perform specific tasks or implement specific abstract data types. The present invention can also be practiced in a distributed computing environment where tasks are performed by remote processing devices connected through a communication network. In a distributed computing environment, program modules can be located in local and remote computer storage media including storage devices.
[0132] Embodiment 4: The embodiment of the present invention further provides a computer-readable storage medium, on which a program is stored. When the program is executed by a processor, it implements the method of code translation based on a large language model in Embodiment 1 above.
[0133] The above are only the preferred embodiments of the present invention and are not intended to limit the present invention. Any modifications, equivalent replacements, improvements, etc. made within the spirit and principle of the present invention shall be included within the scope of protection of the present invention.
Claims
1. A method for code translation based on large language models, characterized in that, It includes the following steps: The end user initiates a code translation task to the configured agent through the web front end, driven by the large language model; the code translation task includes an architecture understanding workflow, a code translation task decomposition workflow, and a coding and debugging workflow; The construction methods of the architecture understanding workflow, the code translation task decomposition workflow, and the coding and debugging workflow are based on the same set of frameworks; the framework includes work nodes and workflows that link the work nodes; The work nodes can process the input of the context and output the corresponding task results.
2. The method for code translation based on a large language model according to claim 1, wherein The architecture understanding workflow is to build a UML class diagram for the code library, abstract the core architecture information layer by layer, and form an architecture design document O1 of the code library; The code translation task decomposition workflow is to perform code translation task decomposition according to the architecture design document O1 of the code library in combination with the requirement document P1, and form a task planning list O2; The coding and debugging workflow is to sequentially extract the elements in the task planning list O2 and perform coding and debugging work to complete the code translation and debugging work.
3. The method for code translation based on a large language model according to claim 2, wherein The method of building a UML class diagram for the code library, abstracting the core architecture information layer by layer, and forming an architecture design document O1 of the code library is specifically as follows: (a.1) Build a warehouse UML class diagram: The work node first starts the UML generation tool integration module, and calls the large language model to preprocess and semantically parse the code structure, annotation information, and related metadata of the code library to provide more accurate context understanding for the UML generation tool; the UML generation tool builds a complete UML class diagram of the code library based on the code files of the code library, and communicates with the large language model in real time during the process. The large language model provides explanations and prompts for complex logic, design patterns, etc. in the code to help the UML generation tool build a complete UML class diagram of the code library; (a.2) The work node calls an agent, and the agent uses the large language model to deeply analyze the complete UML class diagram; the large language model provides semantic association analysis between classes, function relevance evaluation, and business importance judgment capabilities; the agent understands the semantic meaning of the inheritance hierarchy through the large language model and combines the call logs, test case coverage in the code library, and the language model to analyze these data to determine the usage frequency to extract the core key classes and feedback them to the work node; (a.3) The work node calls an agent, and the agent is based on the construction method of the complete UML class diagram and closely cooperates with the large language model, uses the understanding ability of the large language model to understand the construction logic and rules of the complete UML class diagram and transmits them to the agent; the agent limits the scope of the complete UML class diagram to the core key classes. During the process, the large language model performs semantic checks and optimizations on the association relationships, attributes, and methods between classes to ensure that the UML class diagram of the core key classes is accurate and conforms to the business semantics; finally, a UML class diagram of the core key classes is generated and feedback to the work node; (a.4) The working node invokes the agent, which takes the UML class diagram of the core key classes as input and combines the knowledge base and reasoning ability of the large language model; the large language model provides supplementary domain knowledge to help the agent understand the business concepts, entity relationships, and business rules in the class diagram; the agent summarizes the conceptual model and the interaction relationships of the conceptual model based on this information, and feeds back the summary of the conceptual model and the interaction relationships of the conceptual model to the working node; (a.5) The working node invokes the agent, which is based on the UML class diagram of the core key classes. First, it uses the large language model to perform semantic analysis on the interfaces in the class diagram to extract the interface functions of the core key classes; then the agent invokes the function definition query tool, which interacts with the large language model. The large language model optimizes and semantically expands the query request to improve the accuracy of the query; after the function definition query tool obtains the specific code information of the interface function, the agent combines this information and uses the large language model to sort out and summarize the business process, forming a description of the core business process and feeding it back to the working node; (a.6) The working node invokes the agent, which works in collaboration with the large language model. The large language model provides strategy suggestions for class diagram simplification. The agent simplifies the UML class diagram of the core key classes according to these suggestions, removes the detailed information, and retains the key data members and methods, forming a summary UML class diagram of the code library and feeding it back to the working node; (a.7) The working node invokes the agent, which receives the conceptual model summarized in step (a.4), the description of the core business process formed in step (a.5), and the summary UML class diagram of the code library formed in step (a.6), and uses the large language model for text integration, format layout, and content optimization, and summarizes this information into a complete code library architecture design document O1.
4. A method for code translation based on a large language model according to claim 3, characterized in that, Combining the architecture design document O1 of the code library with the requirements document P1, perform code translation task decomposition to form a task planning list O2, specifically: (b.1) Generate a translation plan: The working node first invokes the large language model, taking the code library architecture design document O1 and the requirements document P1 generated by the architecture understanding workflow as input; the large language model deeply analyzes the code library architecture design document O1 and the requirements document P1 to identify the mapping relationships, function counterparts, and potential technical difficulties between the two; at the same time, the agent in the working node starts. Based on the analysis results of the large language model, the agent combines the preset code translation rule library and industry best practices to guide the large language model to generate a preliminary code translation plan framework; Based on the preliminary code translation plan framework, the large language model further refines the plan content, including translation strategies, technology selection suggestions, and risk assessments, and finally generates a complete code translation plan; (b.2) Extract the main tasks: The worker node passes the generated complete code translation plan to the agent. The agent uses natural language processing technology to parse the complete code translation plan and extract the main translation tasks described in the complete code translation plan. During the recognition process, the large language model serves as an auxiliary tool to provide semantic extension and ambiguity elimination support for the agent to ensure that all the main translation tasks are accurately extracted. The agent stores the extracted main translation tasks in a structured format and feeds them back to the worker node. (b.3) Decompose the main tasks: The worker node invokes the agent for the extracted main translation tasks. The agent combines the knowledge base and domain model of the large language model to carefully decompose the main translation tasks into multiple executable subtasks. During the decomposition process, the large language model is responsible for providing the logical relationships, dependencies, and potential boundary condition analyses between the subtasks. At the same time, the agent uses the code query tool to refine and evaluate each subtask, obtains the code details of the relevant dependent tasks, and feeds them back to the worker node. (b.4) Task translation type evaluation: The worker node passes the decomposed subtasks to the agent. The agent uses the large language model to perform semantic analysis on each subtask, combines the preset translation type classification criteria, evaluates the translation type of each subtask, and feeds it back to the worker node. During the process, the large language model provides predictions of translation difficulty, suggestions for translation strategies, and warnings of potential problems. (b.5) Aggregation and sorting task module: After receiving the evaluation results of the translation types of each subtask, the worker node invokes the agent. The agent uses the large language model to summarize and organize the evaluation results of the translation types of the subtasks, aggregates the subtasks belonging to the same translation type or module. At the same time, the agent combines the dependencies, priorities, and resource constraint conditions between the tasks to sort the aggregated tasks, forms a task planning list O2, and feeds it back to the worker node. During the process, the large language model provides optimization suggestions for the sorting algorithm, strategies for resource allocation, and evaluations of potential risks.
5. A method for code translation based on a large language model according to claim 4, characterized in that, The translation type is individual translation, combined translation, or no translation.
6. The method for code translation based on a large language model according to claim 5, wherein The individual translation means that for a single function or model within the capabilities of the LLM, it can be translated independently. The combined translation means that for simple tasks, a simple task belonging to the same category, or a simple task belonging to the same module, it can be selected to be translated in combination with other tasks. The no translation means that for tasks that have completed the target translation or have no need for translation, no translation can be selected.
7. A method for code translation based on a large language model according to claim 6, characterized in that Extract the elements in the task planning list O2 in sequence, perform coding and debugging work to complete the code translation and debugging work, specifically: (c.1) Code dependency query: The large language model in the worker node serves as the core processing unit, calls the code query tool according to the task planning list O2, and retrieves the detailed code of the specified function or module to obtain the complete content that needs to be translated. During the process, the large language model is responsible for processing the semantic understanding related to task planning, accurately communicating the query requirements to the code query tool, and performing preliminary semantic parsing and collation on the query results; (c.2) Code generation: The large language model in the worker node generates the code details of the target language using its powerful code generation ability based on the task objective, the code details of the target language, and the framework structure information of the existing code library; Subsequently, the first intelligent agent acts as a software engineer and the second intelligent agent acts as a senior software engineer to participate in the code review process: The first intelligent agent conducts a preliminary review and provides optimization suggestions for the code based on the code details generated by the large language model and its own code writing experience and knowledge. The second intelligent agent evaluates and provides improvement opinions on the architecture, performance, and maintainability of the code. The first intelligent agent and the second intelligent agent communicate with each other through the dialogue system built by the large language model. The large language model is responsible for understanding the intentions of both parties, integrating the dialogue content, and generating new dialogue content until complete and accurate code is generated, ending the dialogue. Finally, the worker node aggregates and outputs the final translated code; (c.3) Editing code: The worker node writes the final translated code into the code library to obtain an updated code library; During the writing process, the third intelligent agent acts as a software engineer and writes the final translated code into the code library by calling the file editing tool, and provides a DIFF preview result for the fourth intelligent agent to review: If the review is passed, the save modification tool is called to save the edit, that is, the final translated code is written into the code library to obtain an updated code library; If the review fails, the rollback tool is called to cancel the edit, that is, the final translated code is not written into the code library; (c.4) Build and debug: After writing the final translated code into the code library, the worker node calls the build and compilation tool to compile and test the updated code library. The large language model is responsible for configuring the parameters of the build and compilation test and feeding back the results of the build and compilation test to the worker node; After the compilation test passes, use cases are called to conduct running debugging: If an error occurs during the compilation or debugging process, the fifth intelligent agent in the worker node acts as a fault location and repair role, locates the problem by calling the code query tool provided by the large language model, and modifies the error using the file editing tool. After the error is modified, the compilation test is performed again until the compilation test of the updated code library is completed, or the number of error modifications reaches the upper limit, ending the code translation and debugging work. During the process, the large language model serves as the core coordinator, responsible for the interaction between intelligent agents, the invocation of tools, and the processing and transmission of information.
8. An apparatus for code translation based on a large language model, characterized in that, It includes a memory and one or more processors. Executable code is stored in the memory. When the one or more processors execute the executable code, it is used to implement the method for code translation based on a large language model according to any one of claims 1-7.
9. A computer-readable storage medium, characterized in that, A program is stored thereon. When the program is executed by a processor, it implements the method for code translation based on a large language model according to any one of claims 1-7.
Citation Information
Cited By
Intelligent agent system for graphics processor development and graphics processor development method
CN121414568A
Warehouse-level code translation agent method and device based on code graph structure
CN122331909A
A repository-level code translation intelligent agent method and device based on code graph structure
CN122331909B