Code generation method, model training method, and related product

By constructing a semantic alignment matrix and a model training method for feature extraction, the problems of low code conversion efficiency and insufficient accuracy in existing technologies are solved, achieving efficient and accurate cross-language code conversion, and the generated code conforms to the syntax and style requirements of the target language.

WO2026124298A1PCT designated stage Publication Date: 2026-06-18HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD

Patent Information

Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
HUAWEI CLOUD COMPUTING TECHNOLOGIES CO LTD
Filing Date
2025-12-02
Publication Date
2026-06-18

AI Technical Summary

Technical Problem

Existing technologies are inefficient and inaccurate in code conversion, especially when dealing with complex semantics. They cannot guarantee that the converted code conforms to the syntax rules of the target programming language and maintains the original logic and functionality.

Method used

A semantic alignment matrix is ​​constructed using model training methods. Through semantic mapping and feature extraction, the target code is generated using an encoder and a model, ensuring semantic consistency and syntactic structure preservation in cross-language code conversion.

🎯Benefits of technology

It enables efficient and accurate conversion of code from one programming language to another, with the generated code conforming to the syntax rules and style requirements of the target language, thus improving the quality and efficiency of code conversion.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2025139237_18062026_PF_FP_ABST
    Figure CN2025139237_18062026_PF_FP_ABST
Patent Text Reader

Abstract

A code generation method, a model training method, and a related product, applied to the fields of computer technology and AI. In the present application, at least one model is deployed in a code generation apparatus or the code generation apparatus has the capability to invoke at least one model. The code generation apparatus can acquire code to be converted that is written in a first programming language, perform semantic mapping on said code to obtain an intermediate representation, and use the at least one model and the intermediate representation to obtain target code in a second programming language. In the present application, at least one model is used to convert code from one programming language into another programming language, and lexical‑level semantic mapping can be achieved during conversion, thereby ensuring semantic consistency between the generated code and the original language, allowing for efficient and accurate programming language conversion of code. The present application also accordingly provides a model training method and other related products.
Need to check novelty before this filing date? Find Prior Art

Description

Code generation methods, model training methods and related products

[0001] This application claims priority to Chinese patent application filed on December 9, 2024, with application number 202411813837.8 and entitled "Code Generation Method, Model Training Method and Related Products", the entire contents of which are incorporated herein by reference. Technical Field

[0002] This application relates to the fields of computer technology and artificial intelligence technology, and in particular to code generation methods, model training methods and related products. Background Technology

[0003] In the field of software development, code translation is an important research direction, aiming to convert code from one programming language to another. Especially with the trend of globalization in software development, code translation has become a crucial and urgent need. Code translation is mainly accomplished using two methods: manual translation and algorithmic translation. Manual translation involves developers manually editing code into the target programming language, which is inefficient. Algorithmic translation relies on manually defined rules and templates, has limitations when handling complex semantics, and results in code with low accuracy.

[0004] How to efficiently and accurately convert code to a programming language is a hot topic of research in the field. Summary of the Invention

[0005] This application discloses a code generation method, a model training method, and related products, which can use a model to convert code into another programming language. During the conversion, lexical-level semantic mapping can be achieved, ensuring semantic consistency between the generated code and the original language. This application can efficiently and accurately convert code into a programming language.

[0006] Firstly, this application provides a code generation method that can be applied to a computing device, for example, implemented by a module within the computing device. This module may include software modules, hardware modules, or a combination of both. For ease of description, the following explanation uses a code generation device as the execution entity of a braking control method.

[0007] The code generation method includes: a code generation device acquiring code to be converted, the code including lexical elements in a first programming language; the code generation device obtaining an intermediate representation based on the code to be converted, the lexical elements of the intermediate representation being obtained from the lexical elements in the code to be converted based on semantic mapping, and the syntactic structure in the intermediate representation being the syntactic structure of the code to be converted; and the code conversion device using at least one model, at least based on the intermediate representation, obtaining target code, the target code being programmed in a second programming language.

[0008] In this application, the code generation device can perform lexical-level semantic mapping on the code to be converted, obtaining intermediate representation lexical elements from the lexical elements in the code to be converted based on the semantic mapping. This ensures that the semantics of the lexical elements in the intermediate representation are aligned with the semantics in the original code, guaranteeing semantic consistency between the generated code and the original language. It accurately aligns the semantic information between the code to be converted and the target code, significantly reducing logical and functional deviations between the two. Simultaneously, the syntactic structure of the intermediate representation retains the syntactic structure of the original code.

[0009] Furthermore, the code generation device utilizes at least one model to generate target code based on intermediate representations. This model can learn from training data, analyze the relationships and patterns within the data, and thus perform tasks such as prediction and / or classification, demonstrating high data processing efficiency. Analyzing the intermediate representations using a model to obtain the target code ensures that the converted code conforms to the syntax rules of the second programming language while maintaining the logic and functionality of the original code. Therefore, this application can efficiently and accurately convert the programming language of the code.

[0010] In the above scheme, at least one model can be deployed in the code generation device, or the code generation device has the ability to invoke at least one model. For example, the code generation device has a communication connection with the device that has deployed at least one model. Alternatively, at least one model has a corresponding calling interface, through which the code generation device can invoke the at least one model.

[0011] Optionally, at least one model may include one or more models. In the case of multiple models, some models may be deployed on the code generation device, and some models may be deployed on other devices, and the code generation device may be able to invoke these models deployed on other devices.

[0012] In one possible implementation of the first aspect, the lexical elements in the intermediate representation are lexical elements in the second programming language. In the above implementation, the lexical elements in the intermediate representation directly use lexical elements in the second programming language, so that the vocabulary and semantic information in the code to be converted are accurately mapped to the semantic space of the second programming language, ensuring semantic consistency in cross-language code conversion.

[0013] In another possible implementation of the first aspect, the code generation apparatus obtains an intermediate representation based on the code to be converted, including the following operations: the code generation apparatus uses a semantic alignment matrix to map lexical elements in the code to be converted to corresponding lexical elements in a second programming language, the semantic alignment matrix being used to indicate the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language.

[0014] In the above implementation, the semantic alignment matrix can accurately represent the degree of semantic alignment between words. Using the semantic alignment matrix, the words in the code to be converted can be replaced with words in the semantically aligned second programming language, ensuring the semantic consistency of cross-language code conversion and greatly reducing the logical and functional deviations between the target code and the code to be converted.

[0015] In another possible implementation of the first aspect, the method further includes: the code generation device receiving a semantic alignment matrix provided by a semantic alignment model. In the above scheme, the semantic alignment matrix is ​​provided by the semantic alignment model, and the various functional modules are decoupled, which facilitates system debugging and optimization and reduces maintenance costs.

[0016] Optionally, the code generation device is equipped with a semantic alignment model or has the ability to invoke a semantic alignment model.

[0017] In some cases, the semantic alignment matrix provided by the semantic alignment model undergoes semantic alignment optimization (or simply optimization). One optimization operation is as follows: maximizing the similarity between a first target vocabulary element and a first vocabulary element, and / or minimizing the similarity between a second target vocabulary element and a first vocabulary element. Here, the first vocabulary element belongs to a vocabulary element in a first programming language, the first target vocabulary element is a vocabulary element among multiple second vocabulary elements whose semantics are semantically aligned with the first vocabulary element, the second target vocabulary element is a vocabulary element among multiple second vocabulary elements whose semantics are not aligned with the first vocabulary element, and the multiple second vocabulary elements belong to elements in the second programming language.

[0018] Optionally, at least one model is a single model. Further, the code generation device utilizes a single model to perform feature extraction and feature-based reasoning operations.

[0019] Alternatively, at least one model may comprise multiple models. When using multiple models, each model can undertake a different task, collectively achieving a task objective, such as obtaining target code based on intermediate representations. In some cases, at least one model comprises multiple smaller models; in other cases, at least one model comprises a larger model; these two scenarios can be combined.

[0020] As a possible example, at least one model includes an encoder and a first model, the encoder being used to extract features, the features extracted by the encoder being input into the first model, the first model being based on feature target codes. Of course, at least one model may include more models, such as the optimization model shown below, etc., and this application is equally applicable to cases involving more or fewer models.

[0021] In another possible implementation of the first aspect, the code generation apparatus obtains target code using at least one model based on at least an intermediate representation, comprising the following operations: the code generation apparatus uses an encoder to obtain a feature representation based at least on the intermediate representation, and uses a first model to obtain the target code based on the feature representation. The feature representation at least includes features of the intermediate representation extracted by the encoder. The first model belongs to at least one model, and the encoder can be considered as a model. That is, at least one model includes both the encoder and the first model. Of course, the above implementation also applies to the case where the encoder is not considered a model, i.e., at least one model includes the first model.

[0022] In the above implementation, feature extraction can be implemented using a dedicated module, and the inference of the target code can also be implemented using a separate module. The code generation device first extracts features based on the intermediate representation to obtain feature representations, and then uses the first model to obtain the target code based on the extracted feature representations. In this way, feature extraction and code inference are decoupled. On the one hand, feature extraction can be implemented using a dedicated module, reducing the workload of each model. On the other hand, module decoupling makes system debugging and optimization easier and reduces maintenance costs.

[0023] In the above embodiments, the code generation device is equipped with an encoder or has the ability to invoke the encoder. The code generation device is also equipped with a first model or has the ability to invoke the first model.

[0024] Furthermore, the first model is pre-trained using training data. In one possible scenario, the input to the first model undergoes the same processing as it does during application, just as it does during training.

[0025] For example, the training data includes multiple data sets. Taking one set of training data as an example, the training data includes features extracted from training samples. These training samples are intermediate representations of training code, which is written in one programming language (referred to as programming language A). The intermediate representation of the training code is obtained by performing a lexical-level semantic mapping on the training code. The lexical elements of the intermediate representation are lexical elements of another programming language (referred to as programming language B) obtained by semantically mapping the training code. The syntactic structure of the intermediate representation is the same as the syntactic structure of the training code. Further, the intermediate representation of the training code is input into an encoder for feature extraction to obtain training data. When training the first model, the first model obtains output code based on the training data. This output code is generated by the first model and uses programming language B as the programming language. The model training device adjusts (or optimizes) the parameters of the first model based on the output code of the first model using a loss function. This process is repeated multiple times to update the parameters of the first model until the first model training is complete.

[0026] Optionally, the aforementioned first model can be a large model, which is a model with a large number of parameters and a complex structure. For example, a large model includes a large language model (LLM), which refers to a deep learning model trained using a large amount of text data and capable of generating language text or understanding the meaning of language text. For example, the first model includes one or more of the following: a convolutional neural network (CNN) model, a recurrent neural network (RNN) model, or a Transformer-based model; of course, the use of other models with different structures is not excluded.

[0027] In another possible implementation of the first aspect, the code generation apparatus obtains a feature representation based at least on an intermediate representation using an encoder, comprising the following operations: the code generation apparatus uses the encoder to obtain features at least two levels based on the intermediate representation, each level of feature corresponding to at least one structural level of the intermediate representation. The code generation apparatus obtains the feature representation based on the features at least two levels.

[0028] Since the intermediate representation has multiple structural levels, and each structural level has its own features, multi-level feature extraction using an encoder can comprehensively capture the features of the intermediate representation, improve the usability of the feature representation, and help improve the accuracy and quality of the converted target code.

[0029] In yet another possible implementation of the first aspect, the hierarchical structure of the intermediate representation includes at least two of the following: syntax, semantics, and style. The above implementations provide some possible structural hierarchies for intermediate representations.

[0030] For example, the hierarchical structure of intermediate representations includes the syntax of intermediate representations, and the feature representations include the syntactic features of intermediate representations, which can improve the syntactic accuracy of the converted target code and improve the quality of the target code.

[0031] For example, the hierarchical structure of intermediate representations includes the semantics of intermediate representations, and the feature representations include the semantic features of intermediate representations. This can improve the semantic accuracy of the converted target code, improve the matching degree between the semantics of the target code and the semantics of the code to be converted, and improve the quality of the target code.

[0032] For example, the hierarchical structure of intermediate representations includes the style of intermediate representations, and the feature representations include the style features of intermediate representations. This can improve the matching degree between the style of the target code and the style of the code to be converted, thereby improving the quality of the target code.

[0033] In another possible implementation of the first aspect, the code generation apparatus obtains a feature representation using an encoder based at least on an intermediate representation, comprising the following operations: the code generation apparatus uses the encoder to obtain features at least two levels based on the intermediate representation and the code to be converted, wherein each of the features at the at least two levels corresponds to at least one structural level of the intermediate representation or to at least one hierarchical structure of the code to be converted. The code generation apparatus obtains the feature representation based on the at least two levels of features.

[0034] In another possible implementation of the first aspect, the hierarchical structure of the code to be converted includes one of the following: syntax, semantics, and style.

[0035] In another possible implementation of the first aspect, the encoder includes a first encoder and a second encoder. The code generation apparatus uses the encoder to obtain feature representations based at least on the intermediate representation, including the following operations: the code generation apparatus uses the first encoder to obtain syntactic features of the intermediate representation, and uses the second encoder to obtain semantic features and style features of the intermediate representation. The features obtained by the code generation apparatus at least at two levels include syntactic features, semantic features, and style features of the intermediate representation.

[0036] In another possible implementation of the first aspect, the encoder includes a first encoder and a second encoder. The code generation apparatus uses the encoder to obtain feature representations based at least on intermediate representations, including the following operations: the code generation apparatus uses the first encoder to obtain syntactic features of the code to be converted based on the code to be converted. The code generation apparatus uses the second encoder to obtain semantic features and style features of the intermediate representation based on the intermediate representation and the syntactic features of the code to be converted. Wherein, the features at least two levels include syntactic features of the code to be converted, semantic features of the intermediate representation, and style features of the intermediate representation.

[0037] In yet another possible implementation of the first aspect, the first encoder includes a CNN and / or an RNN, and the second encoder is a Transformer encoder.

[0038] In the above implementation, CNNs and RNNs utilize neural networks to more accurately capture syntactic features in the code. For example, CNNs can extract n-gram features from the code, while RNNs can process sequence information and capture long-term dependencies in the code. Of course, the first encoder can include both CNNs and RNNs, resulting in richer and more comprehensive extracted features. The neural network in the CNN is multi-layered, i.e., a multi-layer CNN.

[0039] The Transformer encoder has a self-attention mechanism, which can capture global or long-term dependencies in the code, avoiding information loss and making the extracted features more detailed and accurate.

[0040] In another possible implementation of the first aspect, the code generation device obtains the syntactic features of the intermediate representation using a first encoder, including the following steps: the code generation device extracts the n-gram features of the intermediate representation using a multi-layer CNN, and the code generation device extracts the dependency features of the intermediate representation using an RNN, wherein the syntactic features include n-gram features and dependency features.

[0041] In another possible implementation of the first aspect, the first model is optimized using a loss function (e.g., during training), which evaluates the style matching and / or formatting compliance of the code obtained by the first model. The style matching degree refers to the degree of match between the style of the code obtained by the model and the target style. The formatting compliance degree describes the extent to which the code obtained by the model satisfies the target formatting constraints. This approach provides a model optimization strategy that optimizes the style and formatting performance of the generated code, ensuring the accuracy of code style conversion and formatting, and improving the quality of the generated code.

[0042] In another possible implementation of the first aspect, the encoder input further includes the code to be converted. As one possible implementation, the encoder includes a first encoder and a second encoder. The code generation apparatus uses the encoder to obtain a feature representation based on an intermediate representation, including the following operations: the code generation apparatus uses the first encoder to obtain the syntactic features of the code to be converted based on the code to be converted, and uses the second encoder to obtain the semantic features and style features of the intermediate representation based on the intermediate representation. The features obtained by the code generation apparatus at least at two levels include the syntactic features of the code to be converted, the semantic features of the intermediate representation, and the style features of the intermediate representation. In some cases, since the intermediate representation preserves the syntactic structure of the code to be converted, the input of the aforementioned first encoder can also be replaced with the intermediate representation.

[0043] In another possible implementation of the first aspect, the loss function is associated with at least one of a style loss function and a format specification loss function. The style loss function is used to evaluate the degree of style matching of the code obtained by the first model, and the format specification loss function is used to evaluate the degree of format specification of the code obtained by the first model. The style loss function corresponds to a first weight α, and the format specification loss function corresponds to a second weight β.

[0044] For example, a loss function L joint Represented as follows: L joint =α·L style +β·L format

[0045] Among them, L style Let L be the style loss function. format This is the formatting loss function. α and β are weighting coefficients used to balance style transfer and formatting requirements.

[0046] In another possible implementation of the first aspect, the code generation apparatus obtains target code based on feature representation using a first model, including the following operations: the code generation apparatus uses the first model to obtain code to be optimized based on prompt words and feature representation. Further, the code generation apparatus uses an optimization model to obtain optimized prompt words based on the code to be optimized, and uses the first model to obtain target code based on the optimized prompt words and feature representation.

[0047] In the above implementation, prompt words are used to guide the first model to generate code, and the prompt words are continuously optimized by the optimization model, so that the first model can optimize its output multiple times in order to obtain high-quality code.

[0048] In another possible implementation of the first aspect, the code generation device obtains target code based on feature representation using a first model, including the following operations: the code generation device obtains prompt words based on feature representation, obtains code to be optimized based on prompt words using the first model, optimizes prompt words using an optimization model to obtain optimized prompt words, and obtains target code based on optimized prompt words using the first model.

[0049] In another possible implementation of the first aspect, the code generation apparatus obtains target code based on feature representation using a first model, including the following operations: the code generation apparatus obtains prompt words based on feature representation; obtains search results based on feature representation; the search results are used to assist in code generation, for example, including corpus and / or code samples for generating target code. The code generation apparatus uses the first model to obtain code to be optimized based on prompt words and search results; uses an optimization model to obtain optimized prompt words based on code to be optimized; and uses the first model to obtain target code based on optimized prompt words and search results.

[0050] In the above embodiments, the input of the first model also includes retrieval results, which can retrieve corpora and code samples used to generate target code, thereby enhancing the model's code conversion capability and improving the quality and processing efficiency of the generated code.

[0051] In the above scheme, the code generation device is also equipped with an optimization model or has the ability to call the optimization model.

[0052] In another possible implementation of the first aspect, the optimization model is used to optimize the prompt word based on a reward function. The reward function is used to evaluate, under the current prompt word, at least one of the following metrics: syntactic accuracy, semantic consistency, style matching, or format conformity of the code obtained by the first model.

[0053] Among them, semantic consistency degree is used to indicate the degree of semantic matching between the code to be converted and the target code, and style matching degree is the degree of matching between the style of the code obtained by the first model and the target style.

[0054] In the above implementation, by using the prompt word optimization model, the prompt words can be optimized based on one or more of the following: syntactic correctness, semantic consistency, style matching degree, and format standardization of the output code, thereby improving the accuracy and efficiency of code generation.

[0055] In another possible implementation of the first aspect, the code generation device acquires the code to be converted by means of the following steps: the code generation device receives a code generation request from a user device, the code generation request including the code to be converted. Further, the code generation device sends a target code to the user device. In the above implementation, the code to be converted is input by the user on the user device, and the user device can send a request to the code generation device carrying the user-input code to be converted. After obtaining the target code, the code generation device sends the target code to the user device.

[0056] In another possible implementation of the first aspect, the code generation device can interact with the user, in which case the code generation device can directly receive the code to be converted input by the user.

[0057] In another possible implementation of the first aspect, the method further includes: the code generation device receiving instruction information for a second programming language input by the user. In the above implementation, the code generation device supports the use of a custom converted programming language, allowing the user to specify the desired converted programming language according to their own needs, thereby enhancing the functionality of the code generation device and improving the user experience. Optionally, the instruction information for the second programming language can be directly received by the code generation device, or it can be input by the user on their device and then sent to the code generation device by the user device.

[0058] In another possible implementation of the first aspect, the code generation apparatus obtains target code based on an intermediate representation using a first model, comprising: the code generation apparatus obtaining target code based on the intermediate representation and target style indication information using the first model.

[0059] As one possible implementation, the code generation device uses an encoder to obtain a feature representation based on an intermediate representation and / or the code to be converted, and uses a first model to obtain the target code based on the feature representation and the indication information of the target style.

[0060] In the above embodiments, the input of the first model also includes target style indication information, thereby enabling the generation of code with a specific style, enabling targeted control of the style of the generated code, improving the flexibility of code conversion, enhancing the functionality of the code generation device, and improving the user experience.

[0061] In another possible implementation of the first aspect, the method further includes: the code generation device receiving target style indication information input by the user. In the above implementation, the code generation device supports customization of the converted target style, enabling users to design the converted style according to their own needs, enhancing the functionality of the code generation device and improving the user experience. Optionally, the target style indication information can be directly received by the code generation device from the user, or it can be input by the user on their device and then sent to the code generation device by the user device.

[0062] In yet another possible implementation of the first aspect, the target code conforms to the constraints of a target format, which is predefined or user-defined.

[0063] In the above implementation, the code output by the model can meet the constraints of the target format, making the output code format standardized, highly readable, and usable, thus improving the quality of the output code.

[0064] In another possible implementation of the first aspect, the code generation apparatus obtains target code based on an intermediate representation using a first model, comprising: the code generation apparatus obtaining target code based on the intermediate representation and indication information of the target format using the first model.

[0065] As one possible implementation, the code generation device uses an encoder to obtain a feature representation based on an intermediate representation and / or the code to be converted, and uses a first model to obtain the target code based on the feature representation and the indication information of the target format.

[0066] As one possible implementation, the code generation device uses an encoder to obtain a feature representation based on an intermediate representation and / or the code to be converted, and uses a first model to obtain the target code based on the feature representation, the target format indication information, and the target format indication information.

[0067] Secondly, this application provides a code generation method. The method includes: a code generation device receiving code to be converted and code conversion requirement information input by a user on a first interface; the code to be converted including lexical elements in a first programming language; and the code conversion requirement information including instruction information in a second programming language and instruction information in a target style. The code generation device sends the code to be converted and the code conversion requirement information to another code generation device. The code generation device receives target code provided by another code generation device and presents the target code information on a second interface; the programming language of the target code is the second programming language; the lexical elements in the target code are mapped based on the lexical elements in the code to be converted; and the style of the target code is the target style.

[0068] In one possible implementation of the second aspect, the code conversion requirement information also includes indication information of the target format.

[0069] Thirdly, this application provides a model training method applied to a model training device. The method includes: the model training device constructing a semantic alignment matrix, where the rows of the semantic alignment matrix are lexical elements in a first programming language, the columns of the semantic alignment matrix are lexical elements in a second programming language, and the elements in the semantic alignment matrix represent the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language. Based on the semantics of the first lexical elements and the semantics of multiple second lexical elements, the model training device optimizes the similarity between the first lexical elements and multiple second lexical elements in the semantic alignment matrix, where the first lexical elements belong to lexical elements in the first programming language, and the multiple second lexical elements belong to elements in the second programming language.

[0070] In the above scheme, the model training device can construct and optimize a semantic alignment matrix, enabling the similarity of lexical elements in different programming languages ​​to be intuitively presented. This semantic alignment matrix describes the degree of semantic alignment between lexical elements in programming languages, which is beneficial to the accuracy of semantic mapping when mapping lexical elements between different programming languages, and helps to efficiently and accurately convert code between programming languages.

[0071] In one possible implementation of the third aspect, the model training device optimizes the similarity between the first lexical element and the multiple second lexical elements in the semantic alignment matrix based on the semantics of the first lexical element and the semantics of the multiple second lexical elements, including: the model training device maximizing the similarity between a first target lexical element and the first lexical element, wherein the first target lexical element is a lexical element among the multiple second lexical elements whose semantics are semantically aligned with the first lexical element; and / or, the model training device minimizing the similarity between a second target lexical element and the first lexical element, wherein the second target lexical element is a lexical element among the multiple second lexical elements whose semantics are not semantically aligned with the first lexical element.

[0072] In another possible implementation of the second aspect, the method further includes: the model training device providing the code generation device with an optimized semantic alignment matrix.

[0073] Fourthly, this application also provides a code generation apparatus, which includes an acquisition module and a processing module. The acquisition module is used to perform operations such as acquiring and receiving data, and the processing module is used to perform data processing. This code generation apparatus is used to implement the method described in the first aspect or any possible embodiment of the first aspect.

[0074] It should be understood that the division between the acquisition module and the processing module here is only an example. In actual implementation, the module may be divided into more or fewer modules. For example, the processing module may include a semantic alignment module, an encoding module, and a code generation module. The semantic alignment module is used to perform semantic mapping operations to obtain intermediate representations, the encoding module is used to extract features to obtain feature representations, and the code generation module is used to obtain target code based on the feature representations.

[0075] Fifthly, this application also provides a code generation apparatus, which includes a communication module and an interaction module. The communication module is used to receive external data and / or provide data to external systems, and the interaction module is used to interact with a user. This code generation apparatus is used to implement the method described in the second aspect or any possible implementation thereof.

[0076] Sixthly, this application also provides a model training apparatus, which includes a construction module and an optimization module. The construction module is used to construct a matrix, and the optimization module is used to optimize the matrix. This model training apparatus is used to implement the method described in the third aspect or any possible implementation thereof.

[0077] In a seventh aspect, this application also provides a computing device, including at least one processor and a memory, the memory storing a computer program, and at least one processor for calling the computer program stored in the memory to implement the method described in the first aspect or any possible implementation of the first aspect, or to implement the method described in the third aspect or any possible implementation of the third aspect.

[0078] Eighthly, this application provides a computing device cluster. The computing device cluster includes at least one computing device, each computing device including a processor and a memory. The processor of the at least one computing device is configured to execute instructions stored in the memory of the at least one computing device, causing the computing device cluster to perform some or all of the methods described in the first aspect and any implementation thereof, as well as some or all of the methods described in the third aspect and any implementation thereof.

[0079] In a ninth aspect, this application also provides a user equipment, including a processor and a memory, the memory storing a computer program, and at least one processor for calling the computer program stored in the memory to implement the method described in the second aspect or any possible implementation of the second aspect.

[0080] In a tenth aspect, this application also provides a code generation system, which includes a computing device and a user device. The computing device is used to implement the method described in the first aspect or any possible implementation thereof. The user device is used to implement the method described in the second aspect or any possible implementation thereof.

[0081] Eleventhly, this application also provides a computer-readable storage medium storing computer instructions that, when executed by at least one processor, implement the method described in the first aspect or any possible implementation of the first aspect, or implement the method described in the second aspect or any possible implementation of the second aspect, or implement the method described in the third aspect or any possible implementation of the third aspect.

[0082] In a twelfth aspect, this application also provides a computer program product comprising computer instructions that, when executed by at least one processor, implement the method described in the first aspect or any possible implementation of the first aspect, or implement the method described in the second aspect or any possible implementation of the second aspect, or implement the method described in the third aspect or any possible implementation of the third aspect.

[0083] Furthermore, the computer program product may be a software or program product containing instructions that can run on a computing device or be stored on any available medium. Attached Figure Description

[0084] The accompanying drawings used in the embodiments of this application are described below.

[0085] Figure 1 is a schematic diagram of the architecture of a code generation system provided in an embodiment of this application;

[0086] Figure 2 is a schematic diagram of the software architecture of a code generation system provided in an embodiment of this application;

[0087] Figure 3 is a schematic diagram of the operation process of a code generation system provided in an embodiment of this application;

[0088] Figure 4 is a schematic diagram of the reasoning and training processes of a code generation system provided in an embodiment of this application;

[0089] Figure 5 is a schematic diagram of a semantic alignment matrix provided in an embodiment of this application;

[0090] Figure 6 is a flowchart illustrating a code generation method provided in an embodiment of this application;

[0091] Figure 7 is a schematic diagram of the contents of four types of codes provided in the embodiments of this application;

[0092] Figure 8 is a schematic diagram of a first interface provided in an embodiment of this application;

[0093] Figure 9 is a flowchart illustrating another code generation method provided in an embodiment of this application;

[0094] Figure 10 is a flowchart illustrating a model training method provided in an embodiment of this application;

[0095] Figure 11 is a schematic diagram of a code generation device provided in an embodiment of this application;

[0096] Figure 12 is a schematic diagram of another code generation device provided in an embodiment of this application;

[0097] Figure 13 is a schematic diagram of a model training device provided in an embodiment of this application;

[0098] Figure 14 is a schematic diagram of the structure of a computing device provided in an embodiment of this application;

[0099] Figure 15 is a schematic diagram of the structure of a computing device cluster provided in an embodiment of this application;

[0100] Figure 16 is a schematic diagram of another computing device cluster provided in an embodiment of this application;

[0101] Figure 17 is a schematic diagram of the structure of a user equipment provided in an embodiment of this application. Detailed Implementation

[0102] The embodiments of this application will now be described in detail with reference to the accompanying drawings. For ease of understanding, some terms that may be used in this application will be introduced first.

[0103] A model, also known as an artificial intelligence (AI) model or machine learning model, is a set of functions and parameters learned through training on training data. It is used to perform specific tasks, such as prediction, classification, or other data processing tasks. Model training is inseparable from machine learning techniques. Machine learning is the science of training computer programs or systems to perform tasks without explicit instructions. Classified by learning method, machine learning can be divided into supervised learning, unsupervised learning, and self-learning, among other categories. Deep learning is a subset of machine learning, based on deep neural network models and methods.

[0104] Large models are models with a large number of parameters and complex structures. For example, large models include large language models (LLMs), which are deep learning models trained on large amounts of text data and capable of generating or understanding the meaning of language text. In some cases, large models adopt similar architectures and training objectives to small models. The difference between large and small models is that large models are much larger and require more training data and computational resources.

[0105] Convolutional neural networks (CNNs) are a class of feedforward neural networks that include convolutional computations and have a deep structure. They are one of the representative algorithms of deep learning.

[0106] Recurrent neural networks (RNNs) are a type of neural network that takes sequence data as input, recursively processes the data in the direction of the sequence, and connects nodes (i.e., recurrent units) in a chain-like manner. They are also one of the representative algorithms of deep learning. In some cases, RNNs include long short-term memory networks (LSTM).

[0107] Transformer is a model based on the attention mechanism.

[0108] n-gram features refer to the co-occurrence features of n adjacent words or characters in a given text sequence. Commonly used n-gram features are bigram features and trigram features, corresponding to n = 2 and 3 respectively. Of course, n can take other values ​​in the specific implementation.

[0109] Long-term dependencies refer to the fact that the current state of a system may be influenced by the state of the system a long time ago. In code transformation, the information being processed may be affected by information from a long time ago; some solutions utilize encoders to capture long-term dependencies in the code. In some cases, when processing time series data, the memory units of LSTM neural networks can effectively remember long-term dependency information, thereby achieving more accurate predictions.

[0110] An intelligent agent is a proxy capable of perceiving information and taking actions to achieve specific goals. Intelligent agents possess autonomy, adaptability, and interactivity. They perceive their environment (e.g., through sensors or data input), make judgments and decisions based on their learned knowledge and algorithms, and then execute actions to influence the environment or achieve predetermined goals.

[0111] It should be understood that the terms described above are used to illustrate certain concepts in order to facilitate understanding of this solution. The background and technical implementations defined when describing the terms are merely examples, and as technology improves, the above technologies may have other implementations in specific implementations.

[0112] With the globalization of software development, cross-language code generation has become an important and urgent need. Traditional code conversion methods rely on manually defined rules and templates, which are not only inefficient but also limited in handling complex syntax and semantic structures, making it difficult to guarantee that the converted code conforms to syntactic rules while maintaining the original logic and functionality. Furthermore, different programming languages ​​and development communities often have unique coding style conventions; how to automatically convert code into a format that conforms to the style requirements of the target language is also a major challenge facing current code conversion technologies.

[0113] In view of this, this application provides a code generation method and related products. The code generation method provided by this application can utilize a model to obtain code in a target programming language based on the code to be converted, and can efficiently and accurately convert the programming language of the code. This application also provides a model training method, as well as an apparatus (including a system) for implementing the aforementioned code generation method and model training method.

[0114] Furthermore, some embodiments of this application can convert code into a format that conforms to the style requirements of the target language, resulting in high-quality and highly usable code.

[0115] For ease of understanding, the architecture and business scenarios of the code generation system provided in the embodiments of this application are described below. It should be noted that the system architecture and business scenarios described in this application are for the purpose of more clearly illustrating the technical solutions of this application and do not constitute a limitation on the technical solutions provided in this application. It should be understood that as system architectures evolve and new business scenarios emerge, the technical solutions provided in this application are equally applicable to similar technical problems.

[0116] Please refer to Figure 1, which is a schematic diagram of the architecture of a code generation system provided in an embodiment of this application. The code generation system 100 includes a computing device 10. Optionally, it may also include one or more of the following: a user device 20, a cloud 30, or a model training device 40.

[0117] The computing device 10 is a device with computing capabilities, which may include a single computing device or a cluster of multiple computing devices. The computing device 10 may include servers, such as central servers, edge servers, or local servers in a local data center, or terminal devices such as desktop computers, laptops, or smartphones.

[0118] In this embodiment, the computing device 10 can acquire the code to be converted and use a model to obtain the code of the target programming language based on the code to be converted. The model can run within the computing device 10 or outside of the computing device 10. In the latter case, the computing device 10 calls the model deployed outside the computing device 10 through a predefined module. The computing device 10 also has other functions, such as one or more of code style conversion, formatting conversion, or quality optimization. Please refer to the description below for related information.

[0119] In some possible implementations, the computing device 10 provides an access interface through which external devices can use the services provided by the computing device 10. For example, the access interface is an application programming interface (API).

[0120] In some cases, the code generation system 100 also includes a user device 20. The user device 20 is a device capable of interacting with a user, and may include, for example, an input / output (I / O) module capable of receiving user input and / or outputting information to interact with the user. Exemplarily, the user device 20 includes, but is not limited to, a personal computer (PC), tablet computer, or smartphone. A PC may include a desktop computer or laptop computer.

[0121] User equipment 20 can communicate with computing device 10. For example, user equipment 20 can provide code to be converted to computing device 10, which then obtains code in the target programming language based on the code and returns it to user equipment 20, allowing the user of user equipment 20 to obtain the code conversion result. For instance, user equipment 20 can provide code to be converted and receive the conversion result from computing device 10 via an API provided by computing device 10.

[0122] In some possible implementations, the functions of providing the code to be converted to the server and receiving the target programming language code returned by the server can be integrated into the code generation tool. The user generates code on the user device 20 based on the code generation tool, including one or more functions such as language conversion, style conversion, format conversion, and code quality optimization. The code generation tool can be a functional module on a local or online code generator, a functional module (including tools or plugins) on a local or online integrated development environment (IDE), a locally running software tool or plugin, or an online web tool, etc. For example, the code generation tool can be integrated into computer software (such as a client), and the computer software can be installed on the user device 20, allowing the user device to generate code using the computer software. As another example, the code generation tool can be integrated into an application (APP), and an APP can be installed on the user device 20, allowing the user to generate code through the APP. As yet another example, the code generation tool can be integrated into a lightweight program (such as a mini-program or microservice). As yet another example, the code generation tool is a webpage, allowing the user to generate code through the webpage.

[0123] In some cases, the code generation system 100 also includes a cloud 30, which is a device with computing resources. The cloud 30 includes various models or tools, and the computing device 10 can perform corresponding functions by calling the models or tools in the cloud. The computing resources of the cloud 30 include physical computing resources or virtual computing resources.

[0124] As one possible implementation, the cloud 30 includes an AI hub, which comprises one or more models or tools in the field of AI that can assist the computing device 10 in completing tasks. For example, the cloud 30 may include one or more models such as a semantic alignment model, an encoder, a code generation model, or an optimization model. It may further include one or more tools such as prompt word optimization templates and knowledge bases.

[0125] Optionally, the cloud 30 and the computing device 10 can be integrated into the same physical device, or they can belong to different physical devices. For example, in the latter case, the cloud 30 and the computing device 10 belong to different cloud infrastructures. Of course, this application also applies to the case where the cloud 30 and the computing device 10 belong to the same cloud infrastructure.

[0126] In some cases, models deployed in the cloud 30 or computing device 10 are pre-trained by model training device 40. Model training device 40 is a computing-capable device capable of training models based on training data and / or algorithms. Optionally, the cloud 30 and model training device 40 can be integrated together, for example, in the same physical device. Alternatively, the cloud 30 and model training device 40 can be located in different computing instances, for example, belonging to different physical devices. Similarly, model training device 40 and computing device 10 can be integrated together, or located in different computing instances, including one or more of a single computing device (or host), a cluster of computing devices, containers, virtual machines, clouds, etc.

[0127] The hardware architecture of this application has been described above. The system-level architecture of a possible code generation system provided by an embodiment of this application is described below with reference to Figure 2. Please refer to Figure 2, which is a schematic diagram of the system architecture of a code generation system provided by an embodiment of this application. Optionally, the code generation system can be deployed in the computing device 10 shown in Figure 1, or deployed in the computing device 10 and the cloud 30, or deployed in the computing device 10, the cloud 30, and the model training device 40.

[0128] As shown in Figure 2, the code generation system includes multiple functional layers, such as a user interface layer, business service layer, model layer, data layer, and database. Optionally, the code generation system also includes a request processing layer. The following uses Figure 2 as an example to illustrate these multiple functional layers:

[0129] The user interface layer provides a channel for external access to the code generation system. For example, the user interface layer includes one or more of the following: a PC interface, a mobile client interface, or a mini-program interface. For example, in the architecture shown in Figure 2, users can access the code generation system through the PC interface, mobile client, mini-program, or other access methods (such as web pages or voice interaction systems) to access the services provided by the code generation system.

[0130] The request processing layer handles requests submitted externally through interfaces. For example, it may include one or more of the following modules: an API gateway and a load balancing module. The API gateway performs one or more tasks such as request routing, protocol conversion, caching, log maintenance, service governance, authentication and authorization, or blacklist / whitelist processing. For instance, the API gateway routes user device requests to the appropriate services. The load balancing model distributes request traffic to improve the performance and resilience of the code generation system.

[0131] The business service layer comprises the services provided by the code generation system, including code generation services. These services generate code in the target programming language based on the code to be converted. Further, the business service layer includes one or more other services, such as style transfer services, formatting services, and prompt word optimization services. The style transfer service converts the style of the code; this style can be predefined and / or specified by the user. Formatting services constrain the code's format, improving code quality; again, the code format can be predefined and / or specified by the user. Prompt words guide the model to complete the task; the prompt word optimization service optimizes the prompt words provided to the model, thereby improving the quality of the model's output code. It should be understood that when the business service layer includes multiple services, these services can be combined to accomplish the same task. For example, code generation, style transfer, and formatting services may be used during code conversion.

[0132] In some possible implementations, various services of the business service layer can be integrated into an intelligent agent, such as a transformation agent. The agent can receive input from the user interface layer or request processing layer, process the input, and obtain corresponding output. For example, the transformation agent can convert the input code to be transformed into code in the target programming language.

[0133] The model layer includes one or more models. For example, the model layer includes a first model (or code generation model) for generating code. Optionally, the first model can be a large model (such as a large language model), for which a description can be found in the introduction to large models.

[0134] Furthermore, the model layer may also include one or more of an encoder, decoder, or optimizer. The encoder and decoder are used to extract features. The optimizer is used to optimize the cue words. It should be understood that the model layer may also include more or fewer models or tools; for example, the model layer may include a semantic alignment model. The model layer shown in Figure 2 is merely an example.

[0135] The data layer manages the data generated and / or input into the code generation system. For example, the data layer includes a data caching module, a transaction module, a data synchronization module, and a storage system. The code generation system also includes a database, such as an SQL database or a Redis database.

[0136] In one possible implementation, a functional layer can be implemented using one or more modules. These modules are based on functional divisions and may also be described as "components" in some scenarios. For example, the user interface layer's functionality might be implemented using a communication module, the request processing layer's functionality using a request processing module, and so on for the remaining layers. Furthermore, modules corresponding to several functional layers can be integrated together and implemented using a single module. Alternatively, the functionality of some or all functional layers can be broken down into several modules for separate implementation.

[0137] The hardware and system-level architecture of the code generation system of this application have been described above. The following, with reference to Figure 3, describes the operation process of a possible code generation system provided by an embodiment of this application. As shown in Figure 3, the code generation system includes multiple modules, which include a transformation agent, and further include one or more of the following: a front-end, an engine, an AI center, a prompt word center, and a knowledge base. These will be described in detail below:

[0138] The front-end module is the module through which the system interacts with the user, providing access interfaces (such as APIs) for the system. The front-end can include one or more interactive forms, such as web pages, mini-programs, or mobile apps. Users can submit requests through the front-end module, and these requests may include user-inputted code to be converted. For example, requests can be submitted through one or more interfaces, such as PC interfaces, mobile client interfaces, or mini-program interfaces.

[0139] The engine module is used to process requests submitted by the front end, such as routing requests and performing load balancing operations. For example, the engine module can submit the code to be transformed in the request to the transformation agent.

[0140] The transformation agent module provides business services and can be viewed as the service layer shown in Figure 2. The transformation agent module may include one or more of the following sub-modules: a preprocessing module (optional), a semantic alignment module, an encoding module, a prompt word concatenation module, a model inference module, a retrieval augmented generation (RAG) module, and a prompt word optimization module. The transformation agent module is the core of the code generation system. Of course, the above module division within the transformation agent module is only an example. For instance, the prompt word concatenation module, the model inference module, and the RAG retrieval module can be integrated into the code generation module. Furthermore, the encoding module, prompt word concatenation module, RAG module, and model inference module can be integrated into the same module, resulting in an integrated processing module that can perform both encoding and inference functions. The following section will use the architecture shown in Figure 3 as an example to introduce the sub-modules within the transformation agent module:

[0141] The preprocessing module is used to perform word segmentation on the code. Furthermore, the preprocessing module can identify the part-of-speech (POS) of words in the code, i.e., the element types of the words mentioned above, and perform POS tagging. That is, for example, for the code to be converted, the preprocessing module can process and obtain the element types of the words in the code. For example, the element types include one or more of the following: variable, function, operator, etc.

[0142] The semantic alignment module, also known as the cross-language semantic alignment module, is used to map the vocabulary of the source programming language (or simply the source language) in the code to be converted to the vocabulary of the target programming language (or simply the target language). Furthermore, the semantic alignment module can use a semantic alignment matrix for vocabulary mapping, which can be provided by the semantic alignment model (described below). In some cases, the output of the semantic alignment module is an intermediate representation, where the vocabulary of the intermediate representation is the mapped vocabulary of the target language, and the grammatical structure of the intermediate representation is the grammatical structure of the code to be converted.

[0143] The encoding module is used to extract features from the data input to the encoding module, obtaining a feature representation of the input data. Optionally, the input data of the encoding module may include one or more of the following: the output of the semantic alignment module (such as an intermediate representation), the output of the preprocessing module (such as the code to be converted or the word segmentation result of the code to be converted). The aforementioned feature extraction operation is implemented using encoders. Optionally, there may be multiple encoders. Further, multiple encoders are used to extract features at different levels, that is, multiple encoders are multi-level fusion encoders. Multiple encoders can be used to capture one or more of the following: syntactic features (or syntactic structure features), semantic features, and style features of the code to be converted, generating rich feature representations. For example, multiple encoders include a syntax encoder and a semantic-style encoder. The syntax encoder is used to extract syntactic features, and the semantic-style encoder is used to extract semantic features and style features. The feature representation obtained by multiple encoders includes a fusion feature of syntactic features, semantic features, and style features. Further, the output of the encoding module can be provided to the model inference module (or code generation module), which can call the code generation model to generate code in the target programming language.

[0144] As one possible implementation, referring to Figure 3, the encoder is located outside the transformation agent. The encoding module in the transformation agent can invoke the encoder to extract features from the data input to the encoding module. Alternatively, the encoder can also be located inside the transformation agent, and this application also applies to this case.

[0145] In one case, the encoder package includes a syntax encoder and a semantic style encoder, let... For the code to be converted, E syntaxFor syntax encoder, E scm-style For semantic-style encoders, This represents the intermediate representation after semantic mapping or is used to represent the semantically aligned word vector space. The multi-level fusion encoding process can be represented by the following formula:

[0146] Among them, H syntax and H scm-style These are the encoded grammatical features and the semantic and / or style features, respectively. It can be seen that the code to be converted is input into the grammar encoder to extract grammatical features. The grammatical features extracted by the grammar encoder and the intermediate representation (or the semantically aligned word vector space) are input into the semantic-style encoder to extract semantic and / or style features. Furthermore, combining the above formulas, it can be seen that the output of the grammar encoder is also input into the semantic-style encoder, making the output of the semantic-style encoder incorporate features from a multi-level structure.

[0147] The code generation module processes the input information to obtain the target code. The code generation module may include one or more of the following: a prompt word concatenation module, a RAG module, and a model inference module, where the RAG module is optional. Specifically, it may include the following:

[0148] Scenario 1, referring to Figure 3, the code generation module may include a prompt word concatenation module, a RAG module, and a model inference module. The prompt word concatenation module uses the output of the encoding module to obtain prompt words. The RAG module can retrieve search results from the knowledge base based on the output of the encoding module. The search results and prompt words are provided to the model inference module, enabling it to infer the target code based on the prompt words and the RAG search results.

[0149] Scenario 2: The code generation module includes a prompt word concatenation module and a model inference module. In this case, the RAG module can be decoupled from the code generation module. In addition, the RAG module is an optional module, that is, the code generation system may not include the RAG module.

[0150] The above examples are just illustrations. The following sections will continue to introduce each module:

[0151] The prompt word concatenation module can generate prompt words, which are used to guide the model inference module to infer the code of the target programming language. Optionally, the prompt word concatenation module can call a prompt word template from the prompt word center and generate prompt words based on the prompt word template. Alternatively, the prompt word concatenation module can receive optimized prompt words from the prompt word optimization module and output prompt words based on the optimized prompt words.

[0152] The RAG module can perform retrieval based on feature representations, and the retrieval results can be input into the model inference module to assist in generating code in the target programming language. For example, the output of the RAG module is the retrieval results based on feature representations (including part or all of the feature representations).

[0153] The model inference module is used to obtain the target code based on the input information using the model. The input information includes, but is not limited to, one or more of the following: the output of the encoder, the output of the prompt word concatenation module (prompt words), and the output of the RAG module.

[0154] Optionally, the code generation system also includes a prompt word optimization module, or a prompt word dynamic optimization module. The aforementioned prompt word-guided model inference process can be executed multiple times, and the prompt word optimization module can optimize the given prompt words once or multiple times, thereby improving the quality of the code output by the model inference module.

[0155] The prompt word center includes one or more of the following: a multilingual mapping module, example code explanation, or intent optimization. Further, the intent optimization module is used to refine the intent of the prompt words. Optionally, the optimization strategy used for intent optimization can be provided by the prompt word optimization module. As one possible implementation, during the inference process performed by the model inference module, the model inference module can perform multiple inferences. In each inference, the prompt word concatenation module retrieves optimized prompt words from the prompt word center. These optimized prompt words are obtained by the prompt word center based on the optimization strategy provided by the prompt word optimization model.

[0156] The AI ​​center is used to improve the models used by intelligent agents, including one or more models such as a semantic alignment module, a syntax encoder, a semantic-style encoder, or a code generation model. The semantic alignment module provides the semantic alignment matrix to the semantic alignment module. The encoder is used in the feature extraction process of the encoding module. The code generation model is used by the model inference module to obtain the target code.

[0157] The knowledge base includes a code abbreviation corpus and a multilingual code sample library, which are used to assist the code generation module in generating code.

[0158] The code generation system shown in Figure 3 above can be combined with the architecture of other code generation systems in this application, as well as the code generation method provided below.

[0159] To facilitate understanding, an exemplary inference and training process of a code generation system will be described below. Please refer to Figure 4, which is a schematic diagram of the inference and training process of a code generation system provided in an embodiment of this application.

[0160] Please refer to part (a) of Figure 4. During the inference phase, the input processing module of the code generation system receives input information, including the code to be converted and further conversion requirement information. For example, the conversion requirement information includes one or more of the following: indications for the target programming language, indications for the target style, and indications for the target format. The preprocessing module is used to preprocess the code to be converted to obtain the element types of the vocabulary in the code. The semantic alignment module, or cross-language semantic alignment module, is used to map the vocabulary elements in the code to be converted to the corresponding vocabulary elements in the second programming language. The input of the cross-language semantic alignment module can be the code to be converted and / or the code to be converted after processing by the preprocessing module (i.e., the output of the preprocessing module). The encoding module, or multi-level fusion encoding module, is used to use the encoder to obtain one or more of the following: syntactic structure features, semantic features, and style features of the code to be converted. These features serve as the feature representation of the code to be converted. The code generation module is used to generate preliminary code based on the feature representation using a pre-trained model.

[0161] Optionally, the code generation system also includes a prompt word dynamic optimization module (which can be viewed as the prompt word optimization module shown in Figure 3). This module is used to provide feedback and optimize the output of the code generation model. As a possible example, the prompt word dynamic optimization module is used to optimize the prompt word model based on reinforcement learning, dynamically adjusting the prompt words and using them to guide the code generation model to generate high-quality code. Finally, the code generation module outputs the target code.

[0162] Please refer to section (b) of Figure 4. During the training phase, the training data includes intentions or multiple terms from multilingual code samples or code-generated corpora. The preprocessing module during the training phase can be used to segment the training data to obtain lexical elements and identify the element category to which the lexical elements belong. Specifically, the training process for each model is as follows:

[0163] For cross-language alignment models, the role is to provide a semantic alignment matrix for a given language pair. During training, the model training device first constructs a semantic alignment matrix for a language pair, such as the semantic alignment matrix for programming language A and programming language B. As shown in Figure 5, the rows of the semantic alignment matrix are source language words (i.e., words in the programming language of the code to be converted), and the columns are target language words (i.e., words in the target programming language). The elements in the matrix are used to represent the semantic similarity between words. For example, the elements in the first row and first column are used to represent the semantic similarity between Jword1 and Iword1. Of course, the row and column settings here are only examples; in practice, the rows and columns of the matrix can be replaced.

[0164] During initial construction, the elements in the semantic alignment matrix are initialized. Further, the training mechanism optimizes the semantic similarity between word elements based on training data or training algorithms. For example, the model training mechanism can optimize the semantic alignment matrix using an iterative nearest neighbor algorithm or similar methods to reduce semantic alignment errors between words. Alternatively, the model training mechanism can optimize the alignment matrix by maximizing the semantic similarity of aligned words and / or minimizing the similarity of unaligned words.

[0165] For example, let V i V is the word vector space of the source language. j Let V be the word vector space of the target language, and M be the cross-lingual semantic alignment matrix. Then the cross-lingual semantic alignment operation can be represented as: V j =M·V i

[0166] Furthermore, the trained semantic alignment model can provide the requester with a semantic alignment matrix between the source and target languages. In some possible designs, the semantic alignment matrix may include multiple matrices; for example, one or more matrices may be generated for each element type. For instance, the operator alignment matrix represents the semantic similarity between operator-class words in the source and target languages, while the variable alignment matrix represents the semantic similarity between variable-class words in the source and target languages.

[0167] For encoders, such as syntax encoders and semantic-style encoders, training data can be used to train them, making the features extracted by the encoder more accurate. For example, the training of the encoder is completed by the encoder training module. In some cases, the encoder is a multi-level fusion encoder, where multi-level refers to the ability to capture features at multiple levels of input information. For example, the encoder includes a syntax encoder and a semantic-style encoder, which are trained by the syntax encoder training module and the semantic-style encoder training module, respectively.

[0168] For code generation models, training data can be used for training, enabling the model to generate code in the target language based on input. Furthermore, during the training of the code generation model, a joint optimization strategy can be introduced to adjust the model's parameters. This joint optimization strategy considers the style and format matching of the code obtained from the large model, resulting in code that conforms to the target style and satisfies the target format constraints, thus improving the accuracy of style transfer and formatting in the code generation model.

[0169] For example, a loss function L for adjusting large model parameters joint Represented as follows: L joint =α·L style+β·L format

[0170] Among them, L style Let L be the style loss function. format This is the formatting loss function. α and β are weighting coefficients used to balance style transfer and formatting requirements.

[0171] Optionally, the prompt word optimization model, or simply the optimization model, can be trained by the prompt word optimization model training module. During training, the optimization model can be solved using a reinforcement learning algorithm (as shown in Figure 4), optimizing the prompt words based on a reward function. The reward function is used to evaluate at least one of the following metrics for the code obtained by the first model under the current prompt word: syntactic accuracy, semantic consistency, style matching, or formatting standardization. Semantic consistency indicates the degree of semantic matching between the code to be converted and the target code, while style matching indicates the degree of matching between the style of the code obtained by the first model and the target style.

[0172] For example, let R be the reward function, π be the current prompt word policy, and P be the reward function. π The probability distribution for generating code under policy π. Let C represent the solution strategy that maximizes the value of its subsequent part, where C is a variable representation of one or more of the following: syntax, semantics, style, and format. In this case, reinforcement learning-based cue word adjustment can be expressed as:

[0173] Where, π * The optimal prompt strategy can be solved using a reinforcement learning algorithm, thereby optimizing the prompts provided by the prompt optimization module.

[0174] The system architecture and operation process provided in the embodiments of this application have been described above by way of example. The method provided in the embodiments of this application is described below.

[0175] Please refer to Figure 6, which is a flowchart illustrating a code generation method provided in an embodiment of this application. Optionally, this method can be implemented by the code generation system shown in Figures 1, 2, 3, or 4. The execution entity given here is merely an example; in actual implementation, the specific product executing the method shown in Figure 5 may have other designs. The names of the devices, apparatuses, information, and parameters shown in Figure 6 are also merely examples; in actual implementation, the names may have other designs.

[0176] Referring to Figure 6, a code generation method includes one or more steps from S601 to S607. It should be understood that, for ease of description, the method is described in the order of S601 to S607, and is not intended to limit the execution to this specific order. This application embodiment does not limit the order of execution, the execution time, or the number of executions of the above one or more steps. Steps S601 to S607 are as follows:

[0177] S601, the user equipment receives the code to be converted input by the user.

[0178] The code to be converted includes lexical elements from the first programming language, which is the source language and the programming language of the code to be converted. A programming language is a language that both computers and humans can understand, including assembly language, high-level languages, etc. For example, programming languages ​​include one or more of the following: C, Python, Java, etc.

[0179] As a possible example, see part (a) of Figure 7, where the code to be converted is encoded using Python, including Python lexical elements such as “def”, “and”, “print”, etc.

[0180] In one possible implementation, the user equipment may present a first interface where the user can input a code to be converted. Accordingly, the user equipment receives the code input by the user on the first interface. For example, the user can input the code on the first interface using text, voice, image input (e.g., automatic image recognition), or by uploading a code file.

[0181] In one possible implementation, the user equipment also receives code conversion request information input by the user, which indicates the user's code conversion needs. This conversion request can be input via text or voice, among other methods.

[0182] Optionally, the code conversion requirement information includes one or more of the following: instructions for the second programming language, instructions for the target style, and instructions for the target format. These will be described in detail below:

[0183] The second programming language is the target programming language to which the code is converted, or simply the target language. For example, if the first programming language is Python, the second programming language could be C, Java, or other languages. Of course, the programming language can also be converted between different versions of the same programming language. Optionally, in some cases, the second programming language may be the same as the first programming language; for example, if only the code style and format need to be converted, the first and second programming languages ​​may be the same language.

[0184] The target style indication information is used to specify the style of the converted target code. This style includes function style, naming style, etc. In some cases, the target style also corresponds to a second programming language; for example, Python may correspond to one or more styles. When the second programming language is Python, the user can input or select the style corresponding to Python.

[0185] The target format indicator is used to indicate the format of the converted target code. Here, "format" refers to the code's formatting, such as indentation, line breaks, and spaces. Of course, the meanings of format and style may overlap; for example, the aforementioned indentation, line breaks, and spaces can also serve as indicators of the target style.

[0186] Of course, the code requirements information in a specific implementation may include many other requirements; the requirements listed here are merely examples. Furthermore, the target style and target format instructions may overlap; for instance, both style and format may include instructions on indentation, function names, and data structure formats.

[0187] As an example of code conversion requirement information, the code conversion requirement information is represented in text form or in data form, such as in the form of attributes and attribute values. Please refer to Figure 8, which is a schematic diagram of a first interface provided in an embodiment of this application. In the first interface, the user inputs the code to be converted, and the code conversion requirement information is represented in text form, specifically: "Convert the above code into C language form, with the first letter of each function capitalized." The second programming language indication is "C language," and the target style indication is "the first letter of each function capitalized."

[0188] S602, the user equipment sends the code to be converted to the code generation device.

[0189] Accordingly, the code generation device receives the code to be converted from the user equipment. Optionally, the code to be converted may be sent to the code generation device in a request, and the code generation device receives the request submitted by the user equipment and obtains the code to be converted.

[0190] Optionally, the user device can send the code to be converted to the code generation device via an API provided by the code generation device, including by modules deployed within the code generation device. For example, the user device can provide the code to be converted via a PC client API, a mobile app API, a mini-program API, or a webpage API.

[0191] Optionally, after receiving the conversion request information input by the user, the user equipment also sends code conversion request information to the code generation device. Accordingly, the code generation device receives the code conversion request information from the user equipment. For example, the request sent by the user equipment to the code generation device includes the code to be converted and the code conversion request information.

[0192] S603, the code generation device acquires the code to be converted.

[0193] For an introduction to the code to be converted, please refer to the description in step S601 above.

[0194] Optionally, the code to be converted can be obtained from the user device; for example, the code generation device receives the code to be converted from the user device. Alternatively, the code to be converted can be provided to the code generation device by another device other than the user device. In some scenarios, business and / or services may also use the code generation service. Business devices that process business and / or service devices that provide services can also provide the code to be converted to the code generation device, which processes the code and returns it.

[0195] In this embodiment, the code generation device is a computing device that may include software modules and / or hardware modules. In some cases, the code generation device deploys one or more modules of a code generation system. For example, the code generation device deploys modules capable of implementing one or more functional layers as shown in Figure 2. As another example, the code generation device includes one or more modules of the code generation system shown in Figure 3, such as deploying a conversion agent. As yet another example, the code generation device includes one or more modules of the code generation system shown in Figure 4. For example, referring to Figure 3, the code generation device includes modules implementing operations in the inference phase, namely one or more of an input processing module, a preprocessing module, a semantic alignment module, a multi-level fusion encoding module, a code generation module, a prompt word dynamic optimization module, and an output processing module. As yet another example, the code generation device also includes modules implementing operations in the training phase.

[0196] The code generation device is used here for the purpose of illustrating the scheme. The code generation device can be replaced with other devices with computing capabilities, such as computing devices, computing device clusters, code generation systems, intelligent agents (such as the aforementioned code conversion intelligent agents), etc., and the name of the device can also be designed in other ways.

[0197] In some cases, after the code generation device obtains the code to be converted, it can perform preprocessing on the code, including one or more of the following: cleaning, word segmentation, and part-of-speech tagging.

[0198] S604, the code generation device obtains an intermediate representation based on the code to be converted.

[0199] The intermediate representation is the information obtained after processing the code to be converted, which can represent the meaning expressed by the code to be converted in a new form.

[0200] In some possible implementations, the intermediate representation includes lexical elements and a syntactic structure. The lexical elements of the intermediate representation are obtained from the lexical elements of the code to be converted based on semantic mapping, and the syntactic structure of the intermediate representation is the syntactic structure of the code to be converted. That is, for the code to be converted, its lexical elements are mapped to lexical elements in a new programming language, but its syntactic structure is not changed, thus obtaining a representation of the code to be converted.

[0201] For example, please refer to section (b) of Figure 7. Using a semantic alignment matrix, “def” in the code to be converted is mapped to the semantically similar “function”, “and” is mapped to “sum” or “and”, “a” is mapped to “variable1”, “b” is mapped to “variable2”, “result” is mapped to “variable3”, and “return” is mapped to “return”. Other cases are not listed here. After mapping, the lexical elements of the intermediate representation are replaced with another semantically similar lexical element, but their syntactic structure is preserved from the syntactic structure of the code to be converted. Here, variable1, variable2, and variable3 are used to represent three exemplary variables. For example, the original variable names, namely a, b, and result, can also be retained.

[0202] In some possible implementations, the lexical elements in the intermediate representation are lexical elements in a second programming language. That is, the intermediate representation does not introduce new programming languages ​​or other representation methods, but uses lexical elements in the second programming language, so that the lexical and semantic information in the code to be converted is accurately mapped to the semantic space of the second programming language, ensuring semantic consistency in cross-language code conversion.

[0203] In other cases, the lexical elements in the intermediate representation may be other programming languages ​​or other representation methods. For example, the semantics of the code to be converted may be represented using an interim language first, and then converted to a second programming language in subsequent processing (e.g., reasoning) stages.

[0204] It should be understood that the semantic alignment matrix shown in Figure 7 is only an example.

[0205] As one possible implementation, a semantic alignment matrix is ​​used to indicate the semantic similarity between lexical elements in a first programming language and lexical elements in a second programming language. As shown in Figure 5, the rows of the semantic alignment matrix represent lexical elements in the source language (considered the first programming language), and the columns represent lexical elements in the target language (considered the second programming language). The elements in the semantic alignment matrix represent the semantic similarity between lexical elements in the first and second programming languages. For example, element a1 in the first row and first column of the matrix represents the semantic similarity between Jword1 in the target language and Iword1 in the source language, while element a2 in the first row and second column represents the semantic similarity between Jword2 and Iword1.

[0206] In one example of semantic mapping using a semantic alignment matrix, the code generation device maps lexical elements in the code to be converted to corresponding lexical elements in a second programming language based on the semantic alignment matrix. This mapping process is related to the semantic similarity between lexical elements. For example, referring to Figure 5, for the lexical element Iword1 in the source language, it is mapped to the lexical element with the highest semantic similarity to Iword1 among multiple lexical elements in the corresponding target language. For example, if the maximum similarity among multiple similarities in the row containing Iword1 is the similarity corresponding to Jword1, then Iword1 is mapped to Jword1.

[0207] In some possible implementations, the semantic alignment matrix is ​​provided by a semantic alignment model. For example, this semantic alignment model is deployed in an AI hub in the cloud, and the code generation device can invoke the semantic alignment model through the semantic alignment module and receive the semantic alignment matrices of the first and second programming languages ​​fed back by the semantic alignment model.

[0208] Optionally, the semantic alignment matrix may include multiple matrices. For example, one or more matrices may be generated for each element type. For instance, the operator alignment matrix represents the semantic similarity between operator-type words in the source language and operator-type words in the target language, while the variable alignment matrix represents the semantic similarity between variable-type words in the source language and variable-type words in the target language. Thus, words in the second programming language can be mapped based on the element type of a word element in the first programming language and the matrix corresponding to that word element.

[0209] In one possible implementation, the code generation apparatus determines the element type of the lexical elements in the code to be converted. Exemplarily, the code generation apparatus preprocesses the code to be converted, including segmenting the code to obtain lexical elements and identifying the element type corresponding to each lexical element. Optionally, the preprocessing can be performed before step S604.

[0210] Alternatively, in some cases, the feature representation obtained from feature extraction can also be regarded as an intermediate representation. For example, when performing feature extraction on the code to be converted, the resulting feature representation can be used as an intermediate representation.

[0211] S605, the code generation device uses at least one model to obtain target code based on an intermediate representation.

[0212] The model can learn from training data, analyze the relationships and patterns in the data, and perform tasks such as prediction or classification with high efficiency. Analyzing the processed intermediate representation using at least one model to obtain the target code ensures that the converted code conforms to the syntax rules of the second programming language while maintaining the logic and functionality of the original code. Furthermore, at least one part or all of the model is pre-trained.

[0213] At least one model may include one or more models. When at least one model includes one model, the code generation device utilizes that model to perform feature extraction and feature-based reasoning. When at least one model includes multiple models, each model may undertake a different task, collectively achieving a task objective, such as obtaining target code based on intermediate representations.

[0214] As one possible implementation, at least one model includes an encoder and a first model. The encoder extracts features, which are then input into the first model. The first model can obtain target code based on the features extracted by the encoder. Of course, at least one model can include more models, and this application applies equally to cases involving more or fewer models. For ease of description, the following description assumes at least one model includes an encoder and a first model. It should be understood that "model" here refers to a model capable of inferring target code based on features, and may further include one or more models. Furthermore, the name of the first model can also be designed in other ways, such as calling it a code generation model.

[0215] In some possible implementations, the code generation device uses an encoder to obtain a feature representation based on an intermediate representation, and then uses a first model to obtain the target code based on the feature representation. Thus, feature extraction can be implemented using a dedicated module, i.e., firstly, feature extraction is performed on the intermediate representation to obtain a feature representation, and then the first model is used to obtain the target code based on the extracted feature representation.

[0216] In some cases, the intermediate representation has multiple structural levels, each with its own features. To comprehensively extract the features of the intermediate representation, the code generation device can perform feature extraction hierarchically. That is, the extracted feature representation includes features from at least two levels, or the feature representation is obtained by fusing features from at least two levels. Further, the features from the at least two levels each correspond to at least one structural level of the intermediate representation.

[0217] In some possible implementations, the hierarchical structure of the intermediate representation includes at least two of the following: syntax, semantics, and style. For example, the hierarchical structure of the intermediate representation includes the syntax of the intermediate representation, and the feature representation includes the syntactic features of the intermediate representation, which can improve the syntactic accuracy of the converted target code and improve the quality of the target code. As another example, the hierarchical structure of the intermediate representation includes the semantics of the intermediate representation, and the feature representation includes the semantic features of the intermediate representation, which can improve the semantic accuracy of the converted target code, improve the semantic matching degree between the target code and the code to be converted, and improve the quality of the target code. As yet another example, the hierarchical structure of the intermediate representation includes the style of the intermediate representation, and the feature representation includes the style features of the intermediate representation, which can improve the style matching degree between the target code and the code to be converted, and improve the quality of the target code.

[0218] As one possible implementation, the code generation device uses an encoder to obtain features at least two levels, and obtains a feature representation based on these features. Further, the features at least two levels can be obtained using multiple encoders. For example, the encoder includes a first encoder and a second encoder. The code generation device uses the first encoder to obtain syntactic features of the intermediate representation, and uses the second encoder to obtain semantic features and style features of the intermediate representation. The features at least two levels obtained by the code generation device include syntactic features, semantic features, and style features of the intermediate representation. Optionally, the first encoder is, for example, the syntactic encoder shown in Figure 3, and the second encoder is, for example, the semantic-style encoder shown in Figure 3.

[0219] For example, the first encoder includes a convolutional neural network (CNN) and / or a recurrent neural network (RNN), and the second encoder is a Transformer encoder. CNNs and RNNs utilize neural networks to more accurately capture syntactic features in the code. For example, a CNN can extract n-gram features from the code, while an RNN can process sequence information and capture long-term dependencies in the code. Of course, the first encoder can include both CNNs and RNNs, making the extracted features richer and more comprehensive. Furthermore, the CNN can include multiple layers of CNNs. The Transformer encoder, with its self-attention mechanism, can capture global or long-term dependencies in the code, avoiding information loss and resulting in more detailed and accurate extracted features.

[0220] As a possible example, the code generation device uses a first encoder to obtain syntactic features of the intermediate representation, including the following steps: the code generation device uses a multi-layer CNN to extract n-gram features of the intermediate representation, and the code generation device uses an RNN to extract dependency features of the intermediate representation. The syntactic features include n-gram features and dependency features, such as long-term dependencies.

[0221] In some possible implementations, the encoder input also includes the code to be converted. The code generation device uses the encoder and the code to be converted to obtain the features of the code to be converted, and uses the encoder and the intermediate representation to obtain the features of the intermediate representation. The feature representation input to the first model includes the features of the code to be converted and the features of the intermediate representation. In this implementation, the code generation device can extract the features of the code to be converted and the features of the intermediate representation, and combine the features of both to perform reasoning to obtain the target code. This can further improve the comprehensiveness and richness of the captured features, and make the generated target code more accurate.

[0222] In one possible example, when the encoder includes a first encoder and a second encoder, the code generation device uses the first encoder to extract the syntactic features of the code to be converted, and uses the second encoder to extract the syntactic features and / or style features of the intermediate representation. The feature representation input to the first model includes the syntactic features of the code to be converted, as well as the syntactic features and / or style features of the intermediate representation. That is, the syntactic features of the intermediate representation in the aforementioned implementation can be replaced with the syntactic features of the code to be converted, and the remaining design can be combined with the above, for example, the first encoder is a CNN and / or an RNN, and the second encoder is a Transformer encoder.

[0223] In one scenario, the encoder comprises a syntax encoder and a semantic-style encoder. The code to be converted is input into the syntax encoder to extract syntactic features. The syntactic features and intermediate representations (or semantically aligned word vector spaces) extracted by the syntax encoder are input into the semantic-style encoder to extract semantic features and / or style features. Furthermore, the output of the syntax encoder is also input into the semantic-style encoder, resulting in the semantic-style encoder's output incorporating features from a multi-level structure. A related description can be found in the formulaic representation of the encoding process shown in Figure 3.

[0224] The process of obtaining feature representations by the encoder has been explained above. The following section introduces some possible designs related to the first model.

[0225] The first model is used to obtain target code based on feature representation. In one possible implementation, the first model is optimized using a loss function, which evaluates one or more of the following: semantic accuracy, syntactic accuracy, style matching, and format conformance of the code obtained by the first model. Semantic accuracy indicates the difference between the semantics of the code obtained by the first model and the semantics of the code input to the first model; syntactic accuracy indicates the syntactic correctness of the code obtained by the first model; style matching indicates the degree of matching between the style of the code obtained by the model and the target style; and format conformance describes the degree to which the code obtained by the model satisfies the target format constraints. In this way, the code generation device can ensure the accuracy of the generated code and improve the accuracy of style transfer and format conformance.

[0226] In one possible implementation, the loss function for optimizing the first model is related to at least one of a style loss function and a format specification loss function. The style loss function is used to evaluate the style matching degree of the code obtained from the first model, and the format specification loss function is used to evaluate the format specification degree of the code obtained from the first model.

[0227] In some cases, where the loss function is correlated with both the style loss function and the format loss function, their weights can be designed. For example, the style loss function can correspond to a first weight α, and the format normalization loss function can correspond to a second weight β. For instance, the loss function can be an Ljoint loss function.

[0228] In some cases, the first model includes a prompt word concatenation module, which outputs prompt words to assist the first model in generating code. Furthermore, during the process of obtaining the target code using the model, the code generation device can call the first model multiple times to generate code. In each of these multiple calls, the prompt words input to the model can be different, thereby continuously optimizing the code output by the first model.

[0229] In one possible implementation, the code generation device obtains prompt words based on feature representations, uses a first model to obtain code to be optimized based on the prompt words, uses an optimization model to optimize the prompt words to obtain optimized prompt words, and uses the first model to obtain target code based on the optimized prompt words. That is, the code generation device uses prompt words to guide the code generation model to generate code, and uses an optimization model to continuously optimize the prompt words, enabling the first model to optimize its output multiple times in order to obtain high-quality code. The operation of obtaining prompt words using feature representations can be implemented by a prompt word concatenation module, for example, the prompt word concatenation module shown in Figure 3 provides the prompt words.

[0230] For example, please refer to parts (c) and (d) of Figure 7. In the code generation model, the generated code still needs optimization in terms of style matching and formatting, such as the omission of capitalized first letters in functions and the lack of functional comments. Further optimization through prompts can improve code quality, resulting in higher-quality output code that better meets user needs.

[0231] In some cases, when optimizing prompt words, the optimization model can evaluate the quality of the output code under the current prompt word based on the code currently input to the first model (i.e. the code to be optimized), thereby obtaining the optimized prompt word.

[0232] In some possible implementations, the optimization model is used to optimize prompt words based on a reward function. The reward function evaluates at least one of the following metrics for the code obtained by the first model under the current prompt word: syntactic accuracy, semantic consistency, style matching, or formatting standardization. For example, semantic consistency indicates the degree of semantic matching between the code to be converted and the target code, and style matching indicates the degree of matching between the style of the code obtained by the first model and the target style. Thus, by using a prompt word optimization model, the accuracy and efficiency of code generation can be improved by optimizing prompt words based on one or more of the following: syntactic correctness, semantic consistency, style matching, and formatting standardization of the output code.

[0233] In some possible implementations, the process of obtaining the target code may also utilize search results, which are used to assist the model in generating code. These search results indicate the code generation corpus and / or code samples obtained based on feature representation detection. Optionally, these search results are provided by the RAG module, as shown in Figure 3.

[0234] As a possible example, the code generation device obtains prompt words based on feature representations, and obtains search results based on feature representations. The code generation device uses a first model to obtain code to be optimized based on the prompt words and search results, uses an optimization model to optimize the prompt words to obtain optimized prompt words, and uses the first model to obtain the target code based on the optimized prompt words and search results. Optionally, when optimizing prompt words multiple times, the search result input into the first model may be the same; that is, the search module inputs a search result once, while the prompt word concatenation module may optimize the output prompt words multiple times.

[0235] In some possible implementations, the input to the first model also includes indication information of the target style, thereby enabling the generation of code in a specific style, enhancing the functionality of the code generation device, and improving the user experience. For example, the code generation device utilizes the first model to obtain target code based on the intermediate representation and the indication information of the target style. As one possible implementation, the code generation device utilizes an encoder to obtain a feature representation based on the intermediate representation and / or the code to be converted, and then utilizes the first model to obtain the target code based on the feature representation and the indication information of the target style.

[0236] Furthermore, during the process of obtaining the target code, the prompt words can be optimized to make the style of the target code more consistent with the target style.

[0237] In some possible implementations, the target code conforms to the constraints of a target format, which is predefined or user-defined. As one possible implementation, the code generation apparatus utilizes an encoder to obtain a feature representation based on an intermediate representation and / or the code to be converted, and uses a first model to obtain the target code based on the feature representation and indication information of the target format.

[0238] It should be understood that the various possible solutions provided in the embodiments of this application can be combined without mutual exclusion. As an example of combination, the code generation apparatus uses an encoder to obtain a feature representation based on an intermediate representation and / or the code to be converted, and uses a first model to obtain a target code based on the feature representation, the target format indication information, and the target format indication information.

[0239] In some cases, step S604 is optional. The code generation device can utilize at least one model to obtain target code based on the code to be converted, wherein the at least one model may include an encoder and a first model. The encoder can directly extract features of one or more hierarchical structures of the code to be converted and input the extracted features into the first model to obtain the target code. The process by which the first model obtains the target code based on features can be found in the aforementioned process of obtaining target code based on feature representation.

[0240] Optionally, the method shown in Figure 6 further includes steps S606 and S607, as follows:

[0241] S606, the code generation device sends target code to the user equipment.

[0242] Accordingly, the user equipment receives target code from the code generation device.

[0243] S607, User Equipment presents target code information.

[0244] The target code information includes one or more pieces of information such as the target code content, the target code name, and the target code attributes. For example, the user device can display a second interface to present the target code information.

[0245] In the embodiment shown in Figure 6, the code generation device can acquire code to be converted written in a first programming language, perform semantic mapping on the code to be converted to obtain an intermediate representation, and use at least one model and the intermediate representation to obtain target code using a second programming language. The code generation device uses at least one model to convert the code into another programming language, and can achieve lexical-level semantic mapping during the conversion, ensuring the semantic consistency between the generated code and the original language, and can efficiently and accurately convert the programming language of the code.

[0246] Furthermore, multi-level feature extraction is implemented during the feature extraction process, which can improve the accuracy of code conversion.

[0247] Furthermore, the first model, after being optimized using a joint optimization strategy, can further improve the quality of the converted code.

[0248] Furthermore, by optimizing the code with dynamic prompts during the reasoning process using the first module, the quality of the resulting code can be further improved.

[0249] The embodiments shown in Figure 6 above illustrate various possible implementations. It should be noted that these various possible implementations can be combined without mutual exclusion. One possible implementation is described below with reference to Figure 9. It should be understood that Figure 9 represents related concepts not explained herein, which can be found in the preceding description.

[0250] Please refer to Figure 9, which is a flowchart illustrating another code generation method provided in an embodiment of this application, including the following steps:

[0251] S901, the user equipment receives the code to be converted input by the user.

[0252] S902, the user equipment sends the code to be converted to the code generation device.

[0253] S903, the code generation device uses a semantic alignment matrix to obtain an intermediate representation based on the code to be converted.

[0254] Furthermore, the semantic alignment matrix can be provided by a semantic alignment model. As one possible implementation, the code generation device can invoke the semantic alignment model and receive the semantic alignment matrix returned by it. Based on the semantic alignment matrix provided by the semantic alignment model, an intermediate representation is obtained based on the code to be converted.

[0255] S904, the code generation device uses an encoder to obtain feature representations based on intermediate representations.

[0256] Optionally, the encoder is a multi-level encoder, as described in Figure 6.

[0257] Furthermore, the encoder's input also includes the code to be converted (including the processed code to be converted). In this case, the feature representation includes features of the intermediate representation and features of the code to be converted. For example, the feature representation includes the syntactic features of the code to be converted, the semantic features of the intermediate representation, and the style features of the intermediate representation.

[0258] Optionally, the encoder can be deployed outside the code generation device, which can invoke the encoder to obtain a feature representation based on the input.

[0259] S905, the code generation device uses a code generation model to obtain target code based on feature representation.

[0260] The code generation module may belong to at least one model in the embodiment shown in Figure 6 above. Optionally, the code generation model may be deployed outside the code generation device, and the code generation device may call the code generation model to obtain the feature representation based on the feature representation.

[0261] Optionally, during the process of obtaining the target code based on feature representation, the code generation device may execute the following process multiple times: outputting code based on the current prompt word, receiving optimized prompt words obtained by the prompt word concatenation module based on the output code, and outputting code again based on the optimized prompt words. Optionally, the prompt word concatenation module may call a prompt word optimization model to optimize the prompt words; for a related introduction to the prompt word optimization model, please refer to the foregoing.

[0262] Optionally, the code generation model is optimized using a joint optimization strategy, which refers to the optimization of one or more of the following metrics of the code obtained by the model: semantic accuracy, syntactic accuracy, style matching, and format conformance. For example, the joint optimization strategy is implemented based on a loss function, which is related to at least one of the style loss function and the format conformance loss function, as described in Figure 6.

[0263] Optionally, the code generation model is a large model, which may include a large language model.

[0264] S906, the code generation device sends target code to the user equipment.

[0265] S907, User Equipment presents target code information.

[0266] In the embodiment shown in Figure 9, the code generation device can achieve cross-language code generation, converting the code to be converted into code in the target language (e.g., a second programming language). Moreover, the code generation device achieves semantic alignment, encoding, and code generation functions by calling a model, making the generated code semantically accurate and the code generation efficiency high.

[0267] Furthermore, the code generation device also supports style conversion and formatting, which can significantly improve the quality of the converted code.

[0268] The previous section discussed the application process of the code generation service. The following section describes the training process of a semantic alignment model used in the code generation service. This semantic alignment model is used to output a semantic alignment matrix.

[0269] Please refer to Figure 10, which is a flowchart illustrating a model training method provided in an embodiment of this application. It should be understood that, for ease of description, the method is described in the order of S1001 to S1002, and is not intended to limit the execution to this specific order. This embodiment does not limit the order of execution, the execution time, or the number of executions of one or more of the above steps. Steps S1001 to S1002 are as follows:

[0270] S1001, the model training device constructs a semantic alignment matrix.

[0271] In the semantic alignment matrix, the columns represent lexical elements in the second programming language, and the elements in the semantic alignment matrix represent the semantic similarity between lexical elements in the first and second programming languages. As shown in Figure 5, the rows of the semantic alignment matrix represent lexical elements in the source language (considered the first programming language), and the columns represent lexical elements in the target language (considered the second programming language). The elements in the semantic alignment matrix represent the semantic similarity between lexical elements in the first and second programming languages. For example, element a1 in the first row and first column of the matrix represents the semantic similarity between Jword1 in the target language and Iword1 in the source language, while element a2 in the first row and second column represents the semantic similarity between Jword2 and Iword1.

[0272] S1002, the model training device optimizes the semantic alignment matrix.

[0273] The model training device updates the elements in the semantic alignment matrix based on training data or training algorithms, that is, updates the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language.

[0274] As one possible implementation, the model training device optimizes the similarity between the first lexical element and the multiple second lexical elements in the semantic alignment matrix based on the semantics of the first lexical element and the semantics of multiple second lexical elements. Here, the first lexical element belongs to a lexical element in a first programming language, and the multiple second lexical elements belong to elements in a second programming language.

[0275] As one possible implementation, the model training device maximizes the similarity between a first target vocabulary element and a first vocabulary element, where the first target vocabulary element is a vocabulary element whose semantics are semantically aligned with the first vocabulary element among a plurality of second vocabulary elements. And / or, the model training device minimizes the similarity between a second target vocabulary element and a first vocabulary element, where the second target vocabulary element is a vocabulary element whose semantics are not aligned with the first vocabulary element among a plurality of second vocabulary elements. Through the above implementation, the model training device can highlight the differences in similarity between vocabulary elements, and when using a semantic alignment matrix for semantic mapping, it can more quickly and accurately select the optimal aligned vocabulary.

[0276] Optionally, the model training device is used to provide the optimized semantic alignment matrix to the code generation device. See the foregoing for a related description.

[0277] The methods of the embodiments of this application have been described in detail above. The apparatus of the embodiments of this application is provided below. It should be understood that the module division of the apparatus (which may include a system) provided in this application is merely an exemplary illustration of one way to divide the structure of the apparatus. In practical applications, the structure of the apparatus may have other division methods. This application is equally applicable to apparatuses with other division methods but the same function. Furthermore, the apparatus name provided in this application is an exemplary name and can be replaced in specific implementations.

[0278] Please refer to Figure 11, which is a schematic diagram of a code generation apparatus provided in an embodiment of this application. The code generation apparatus can be a standalone device (e.g., computing device 10) or a module within a standalone device, including hardware and / or software modules. This code generation apparatus 110 is used to implement the aforementioned code generation method, such as the method executed by the code generation apparatus in the code generation methods shown in Figures 6 and 9.

[0279] In one possible design, the code generation device 110 includes an acquisition module 1101 and a processing module 1102. The acquisition module 1101 is used to acquire and receive data, for example, by executing step S603. The processing module 1102 is used to process the data, for example, by executing the aforementioned steps S604 and S605, or by executing steps S903-S905. It should be understood that the module division here is merely an example; the acquisition module 1101 and processing module 1102 may be divided into more modules, or the acquisition module 1101 and processing module 1102 may be merged into one module. In yet another possible design, the code generation device deploys some or all of the modules of the aforementioned code generation system, for example, deploying some or all of the modules in the embodiments shown in Figures 2, 3, or 4, wherein the acquisition module 1101 can perform the functions of the user interface layer or front-end module, while the processing module 1102 is used to perform the functions of other modules.

[0280] In one possible implementation, the acquisition module 1101 is used to acquire the code to be converted. The processing module 1102 is used to obtain an intermediate representation based on the code to be converted, and to obtain the target code based on the intermediate representation using at least one model. For an explanation of the foregoing information, please refer to the description in the foregoing embodiments.

[0281] In another possible implementation, the processing module 1102 is used to map lexical elements in the code to be converted to corresponding lexical elements in the second programming language based on the semantic alignment matrix.

[0282] In another possible implementation, the acquisition module 1101 is also used to receive a semantic alignment matrix provided by the semantic alignment model.

[0283] In another possible implementation, the processing module 1102 is used to obtain a feature representation using the encoder at least based on the intermediate representation, and to obtain a target code using the first model based on the feature representation.

[0284] In another possible implementation, the processing module 1102 is used to obtain features at least two levels based on at least the intermediate representation using the encoder, and to obtain feature representations based on the features at least two levels.

[0285] In another possible implementation, the processing module 1102 is used to obtain features at least two levels based on the intermediate representation and the code to be converted using the encoder, and to obtain feature representations based on the features at least two levels.

[0286] In another possible implementation, the processing module 1102 is used to obtain the syntactic features of the intermediate representation using the first encoder, and to obtain the semantic features and style features of the intermediate representation using the second encoder.

[0287] In another possible implementation, the processing module 1102 is used to obtain the syntactic features of the code to be converted based on the first encoder, and to obtain the semantic features and style features of the intermediate representation based on the intermediate representation and the syntactic features of the code to be converted based on the second encoder.

[0288] In another possible implementation, the processing module 1102 is used to extract n-gram features of intermediate representations using a multi-layer CNN, and to extract dependency features of intermediate representations using an RNN, the grammatical features including n-gram features and dependency features.

[0289] In another possible implementation, processing module 1102 is used to obtain the code to be optimized based on the prompt words and feature representation using the first model. Further, processing module 1102 is used to obtain optimized prompt words based on the code to be optimized using an optimization model, and to obtain the target code based on the optimized prompt words and feature representation using the first model.

[0290] In another possible implementation, the processing module 1102 is used to obtain prompt words based on feature representation, obtain search results based on feature representation, and use the search results to assist in code generation. Further, the processing module 1102 is also used to obtain code to be optimized based on prompt words and search results using a first model, obtain optimized prompt words based on code to be optimized using an optimization model, and obtain target code based on optimized prompt words and search results using the first model.

[0291] In another possible implementation, the processing module 1102 is used to: obtain prompt words based on feature representation, obtain code to be optimized based on the prompt words using a first model, optimize the prompt words using an optimization model to obtain optimized prompt words, and obtain target code based on the optimized prompt words using the first model.

[0292] In another possible implementation, the acquisition module 1101 is used to receive a code generation request from a user device, the code generation request including code to be converted.

[0293] In another possible implementation, the code generation apparatus further includes a feedback module (not shown) for sending target code to the user equipment.

[0294] In another possible implementation, the acquisition module 1101 is also configured to receive instruction information for a second programming language input by the user.

[0295] In another possible implementation, the processing module 1102 is used to obtain target code using the first model based on intermediate representation and target style indication information.

[0296] In another possible implementation, the acquisition module 1101 is also configured to receive indication information of the target style input by the user.

[0297] In another possible implementation, processing module 1102 is used to obtain target code using the indication information of the first model based on intermediate representation and target format. The target format is predefined, or the target format is user-defined.

[0298] For details of the aforementioned possible implementations, please refer to the descriptions in the code generation system and method embodiments above.

[0299] Please refer to Figure 12, which is a schematic diagram of another code generation apparatus provided in an embodiment of this application. The code generation apparatus 120 can be a standalone device (e.g., user equipment 20) or a module within a standalone device, including hardware and / or software modules. This code generation apparatus 120 is used to implement the aforementioned code generation method, such as the method executed by the user equipment in the code generation methods shown in Figures 6 and 9.

[0300] In one possible design, the code generation device 120 includes a communication module 1201 and an interaction module 1202. The communication module 1201 is used to receive external data and / or provide data to external systems, such as executing steps S602, S902, etc. The interaction module 1202 is used to interact with the user, such as executing steps S601, S607, S901, S907, etc. This code generation device 120 is used to implement the aforementioned code generation method, such as the code generation methods shown in Figures 6 and 9. It should be understood that the module division here is only an example; the communication module 1201 and interaction module 1202 may be divided into more modules, or the communication module 1201 and interaction module 1202 may be combined into one module.

[0301] In one possible implementation, the interaction module 1202 is used to receive the code to be converted and the code conversion requirement information input by the user on the first interface. The communication module 1201 is used to send the code to be converted and the code conversion requirement information to the code generation device. The communication module 1201 is also used to receive the target code provided by the code generation device, and the interaction module 1202 is also used to present the target code information on the second interface.

[0302] For details of the aforementioned possible implementations, please refer to the descriptions in the code generation system and method embodiments above.

[0303] Please refer to Figure 13, which is a schematic diagram of a model training device provided in an embodiment of this application. The model training device 40 can be a standalone device or a module within a standalone device, including hardware and / or software modules. This model training device 40 is used to implement the aforementioned model training methods, including the method for training a model as described in the relevant section of Figure 4, and the model training method shown in Figure 10, etc.

[0304] In one possible design, the model training device 40 includes a construction module 401 and an optimization module 402. The construction module 401 is used to construct the matrix, and the optimization module 402 is used to optimize the matrix. This model training device 40 is used to implement the aforementioned model training method, such as the model training method shown in Figure 10.

[0305] In one possible design, the construction module 401 is used to construct a semantic alignment matrix. The rows of the semantic alignment matrix are lexical elements in a first programming language, and the columns are lexical elements in a second programming language. The elements in the semantic alignment matrix represent the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language. The optimization module 402 is used to optimize the similarity between the first lexical elements and the multiple second lexical elements in the semantic alignment matrix based on the semantics of the first lexical elements and the semantics of the multiple second lexical elements.

[0306] For details of the aforementioned possible implementations, please refer to the descriptions in the code generation system and method embodiments above.

[0307] This application also provides a computing device. This computing device is a device with computing capabilities, including a server or terminal device (including user equipment). Figure 14 exemplarily illustrates a schematic diagram of the structure of the computing device provided in this application. As shown in Figure 13, the computing device 300 includes a connection line 301, a processor 302, a memory 303, and a communication interface 304, wherein the processor 302, the memory 303, and the communication interface 304 communicate with each other via the connection line 301. It should be understood that this application does not limit the number of processors 302 and memory 303 in the computing device 300.

[0308] Connection line 301 is used to connect various modules in the device to enable data transmission between different modules. Exemplarily, connection line 301 includes a bus, such as a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus. In some cases, connection lines can be categorized as address connection lines, data connection lines, control connection lines, etc. For ease of illustration, only one line is used in Figure 14, but this does not mean that the computing device 300 has only one connection line or only one type of connection line. Connection line 301 can include pathways for transmitting information between various components of the computing device 300 (e.g., processor 302, memory 303, and communication interface 304).

[0309] Processor 302 may include any one or more processors such as a central processing unit (CPU), a graphics processing unit (GPU), a microprocessor (MP), or a digital signal processor (DSP).

[0310] Memory 303 may include volatile memory, such as random access memory (RAM). Memory 303 may also include non-volatile memory, such as flash memory, read-only memory (ROM), hard disk drive (HDD), or solid state drive (SSD).

[0311] The memory 303 stores executable code. The processor 302 executes the code stored in the memory 303 to implement the code generation system, code generation method, or model training method described above, or the processor 302 is used to implement the functions of one or more of the aforementioned code generation apparatus 110, code generation apparatus 120, or model training apparatus 40. That is to say, the memory 303 stores instructions for executing the aforementioned code generation system, code generation method, or model training method.

[0312] The communication interface 304 uses transceiver modules such as, but not limited to, network interface cards and transceivers to enable communication between the computing device 300 and other devices or communication networks. For example, the computing device 300 communicates with a user equipment through the communication interface 304.

[0313] This application also provides a computing device cluster. The computing device cluster includes at least one computing device, which may be a server, such as a central server, an edge server, or a local server in a local data center. In some embodiments, the computing device in the computing device cluster may also be a terminal device such as a desktop computer, a laptop computer, or a smartphone.

[0314] Figure 15 exemplarily illustrates the structural schematic diagram of the computing device cluster provided in this application. As shown in Figure 15, the computing device cluster 400 includes at least one computing device 300. The memory 303 of one or more computing devices 300 in the computing device cluster 400 may store instructions for executing the above-described code generation system, code generation method, or model training method.

[0315] In some possible implementations, the memory 303 of one or more computing devices 300 in the computing device cluster 400 may also store partial instructions for executing the aforementioned code generation system, code generation method, or model training method. In other words, a combination of one or more computing devices 300 can jointly execute the aforementioned code generation system, code generation method, or model training method. It should be noted that the memory 303 of different computing devices 300 in the computing device cluster 400 may store different instructions, each used to execute a partial function of the code generation system.

[0316] In some possible implementations, one or more computing devices 300 in the computing device cluster 400 can be connected via a network, which can be a wide area network (WAN) or a local area network (LAN). Figure 16 illustrates one possible implementation. As shown in Figure 16, computing device 300A and computing device 300B are connected via a network.

[0317] It should be understood that the functions of computing device 300A shown in Figure 16 can also be performed by multiple computing devices 300, and the functions of computing device 300B can also be performed by multiple computing devices 300. The specific deployment method can depend on business needs and the computing power of the computing devices.

[0318] Optionally, the computing device 300 (or 300A, 300B, etc.) shown in FIG1 may include the computing device 300 shown in FIG14 and / or the computing device cluster 400 shown in FIG15 (or FIG16).

[0319] This application also provides a user equipment 500, as shown in FIG17, which includes a processor 502 and a memory 503. Further, the user equipment 500 also includes a connection line 301, a communication interface 504, and an I / O module 505, and the processor 502, memory 503, and communication interface 504 communicate with each other via the connection line 301. It should be understood that this application does not limit the number of processors 502 and memory 503 in the user equipment 500.

[0320] The connection line 501, processor 502, memory 503, and communication interface 504 can be found in the foregoing description of the connection line, processor, memory, and communication interface. The I / O module 505 includes modules capable of receiving user input and / or outputting information to the user. For example, the I / O module 505 includes one or more of the following: a screen (including a screen capable of receiving input), a keyboard, an audio input device, an audio output device, and a video input device.

[0321] This application also provides a computer program product containing instructions. This computer program product can be a software or program product containing instructions, capable of running on a computing device or stored on any available medium. When the computer program product runs on a computing device, it causes the computing device to execute the model, code generation method, and model training method described above.

[0322] This application also provides a computer-readable storage medium. The computer-readable storage medium can be any available medium capable of being stored by a computing device, or a data storage device such as a data center containing one or more available media. The aforementioned available media can be magnetic media (e.g., floppy disks, hard disks, magnetic tapes), optical media (e.g., DVDs), or semiconductor media (e.g., solid-state drives), etc. The computer-readable storage medium includes instructions that instruct the computing device to execute the model, code generation method, and model training method described above.

[0323] In addition, a few additional points need to be made regarding this application:

[0324] I. The above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit it. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the protection scope of the technical solutions of the embodiments of this application.

[0325] 2. Unless otherwise stated, “multiple” means two or more.

[0326] 3. Unless otherwise specified or in case of logical conflict, the terms and / or descriptions in different embodiments of this application are consistent and can be referenced by each other. Technical features in different embodiments can be combined to form new embodiments based on their inherent logical relationships.

[0327] IV. The various numerical designations used in this application are merely for descriptive convenience and are not intended to limit the scope of protection of this application. The magnitude of the serial numbers used in this application does not imply a sequential order of execution; the execution order of each process should be determined by its function and internal logic. For example, the terms "first," "second," "third," "fourth," and other various terminology (if present) in the specification, claims, and drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. Such data can be interchanged where appropriate so that the embodiments described herein can be implemented in a sequence other than that illustrated or described herein.

[0328] Furthermore, any embodiment or design described in this application as "exemplary" or "for example" should not be construed as being more preferred or advantageous than other embodiments or designs. Specifically, the use of terms such as "exemplary" or "for example" is intended to present the relevant concepts in a concrete manner for ease of understanding.

[0329] V. The terms “comprising” and “having” and any variations thereof are intended to cover non-exclusive inclusion, for example, a process, method, system, product or device that includes a series of steps or modules is not necessarily limited to those steps or modules that are expressly listed, but may include other steps or modules that are not expressly listed or that are inherent to such process, method, product or device.

[0330] VI. In this application, "for indicating" can be understood as "enabling". "Enabling" can include direct enabling and indirect enabling. When describing information for enabling A, it can include whether the information directly enables A or indirectly enables A, but does not necessarily mean that the information carries A.

[0331] The information that enables the information is called the information to be enabled. In the specific implementation process, there are many ways to enable the information to be enabled, such as, but not limited to, directly enabling the information to be enabled, such as the information to be enabled itself or its index. It can also be indirectly enabled by enabling other information, where there is a relationship between the other information and the information to be enabled. It can also enable only a part of the information to be enabled, while the other parts are known or pre-agreed upon. For example, enabling specific information can be achieved by using a pre-agreed (e.g., protocol-defined) arrangement of various pieces of information, thereby reducing enabling overhead to some extent. Simultaneously, common parts of various pieces of information can be identified and enabled uniformly to reduce the enabling overhead caused by individually enabling the same information.

[0332] VII. In this application, "predefined" may include preconfiguration. For example, predefining certain information means that the information is calculated or received in advance before performing an action that uses the information. The "predefined" can be implemented by pre-storing corresponding codes, tables, or other means that can be used to indicate relevant information in the device (e.g., controller or vehicle). This application does not limit the specific implementation method.

[0333] 8. The term "storage" or "preservation" in this application can refer to storage in one or more memory devices. These memory devices can be separately configured or integrated into an encoder, decoder, processor, or communication device. Alternatively, some memory devices can be separately configured, while others can be integrated into a decoder, processor, or communication device. The type of memory can be any form of storage medium, and this is not limited.

[0334] 9. The arrows or boxes indicated by dashed lines in the schematic diagrams in the accompanying drawings of this application represent optional steps or optional modules.

[0335] 10. Unless otherwise stated, " / " indicates that the objects before and after are in an "or" relationship. For example, A / B can mean A or B. In this application, "and / or" is merely a description of the relationship between the related objects, indicating that there can be three relationships. For example, A and / or B can mean: A exists alone, A and B exist simultaneously, and B exists alone. A and B can be singular or plural.

Claims

1. A code generation method, characterized in that, The code generation device is applied to a code generation device, wherein the code generation device is equipped with an encoder or has the ability to invoke the encoder, and the code generation device is also equipped with a first model or has the ability to invoke the first model. The code generation method includes: Obtain the code to be converted, which includes lexical elements in a first programming language; An intermediate representation is obtained based on the code to be converted. The lexical elements in the intermediate representation belong to the lexical elements of the second programming language. The lexical elements of the intermediate representation are obtained from the lexical elements in the code to be converted based on semantic mapping. The syntax structure of the intermediate representation is the syntax structure of the code to be converted. Using the encoder, a feature representation is obtained at least based on the intermediate representation, the feature representation including at least the features of the intermediate representation extracted by the encoder; Using the first model, target code is obtained based on the feature representation. The programming language of the target code is the second programming language. The first model is pre-trained using training data.

2. The method according to claim 1, characterized in that, The process of obtaining an intermediate representation based on the code to be converted includes: Using a semantic alignment matrix, lexical elements in the code to be converted are mapped to corresponding lexical elements in the second programming language. The semantic alignment matrix is ​​used to indicate the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language.

3. The method according to claim 2, characterized in that, The code generation device also deploys a semantic alignment model, or the code generation device has the ability to invoke a semantic alignment model; the method further includes: The semantic alignment model provides the semantic alignment matrix, which is semantically optimized, including: Maximize the similarity between the first target word element and the first word element, and / or minimize the similarity between the second target word element and the first word element; Wherein, the first lexical element belongs to the lexical element in the first programming language, the first target lexical element is a lexical element whose semantics are aligned with the semantics of the first lexical element among a plurality of second lexical elements, the second target lexical element is a lexical element whose semantics are not aligned with the semantics of the first lexical element among the plurality of second lexical elements, and the plurality of second lexical elements belong to the elements in the second programming language.

4. The method according to any one of claims 1-3, characterized in that, The process of obtaining feature representations using the encoder, at least based on the intermediate representations, includes: Using the encoder, at least two levels of features are obtained based on the intermediate representation, wherein each level of features corresponds to at least one structural level of the intermediate representation; The feature representation is obtained based on the at least two hierarchical features.

5. The method according to any one of claims 1-3, characterized in that, The process of obtaining feature representations using the encoder, at least based on the intermediate representations, includes: Using the encoder, at least two levels of features are obtained based on the intermediate representation and the code to be converted. Each of the at least two levels of features corresponds to at least one structural level of the intermediate representation or to at least one hierarchical structure of the code to be converted. The feature representation is obtained based on the at least two hierarchical features.

6. The method according to claim 5, characterized in that, The encoder includes a first encoder and a second encoder; the step of obtaining a feature representation based on the intermediate representation using the encoder includes: Using the first encoder, the syntactic features of the code to be converted are obtained based on the code to be converted; Using the second encoder, the semantic features and style features of the intermediate representation are obtained based on the syntactic features of the intermediate representation and the code to be converted; The features at least two levels include the syntactic features of the code to be converted, the semantic features of the intermediate representation, and the style features of the intermediate representation.

7. The method according to any one of claims 1-6, characterized in that, The first model is optimized using a loss function, which is used to evaluate the style matching degree and / or format conformity degree of the code obtained by the first model. The style matching degree refers to the degree of matching between the style of the code obtained by the first model and the target style.

8. The method according to claim 7, characterized in that, The loss function is related to at least one of the style loss function and the format specification loss function; The style loss function is used to evaluate the style matching degree of the code obtained by the first model, and the format specification loss function is used to evaluate the format specification degree of the code obtained by the first model.

9. The method according to any one of claims 1-8, characterized in that, The code generation device is further equipped with an optimization model or has the ability to invoke an optimization model. The step of obtaining the target code based on the feature representation using the first model includes: Based on the aforementioned feature representation, prompt words are obtained; Using the first model, the code to be optimized is obtained based on the prompt words; Using the optimization model, the prompt words are optimized to obtain the optimized prompt words; Using the first model, the target code is obtained based on the optimized prompt words.

10. The method according to any one of claims 1-8, characterized in that, The code generation device is further equipped with an optimization model or has the ability to invoke an optimization model. The step of obtaining the target code based on the feature representation using the first model includes: Based on the aforementioned feature representation, prompt words are obtained; The search results are obtained based on the feature representation, and the search results are used to assist in code generation; Using the first model, the code to be optimized is obtained based on the prompt words and the search results; Using the optimization model, optimized prompt words are obtained based on the code to be optimized; Using the first model, the target code is obtained based on the optimized prompts and the search results.

11. The method according to claim 9 or 10, characterized in that, The optimization model is used to optimize prompt words based on a reward function. The reward function is used to evaluate at least one of the following metrics for the code obtained by the first model under the current prompt word: Grammatical accuracy, semantic consistency, style matching, or formatting compliance; The semantic consistency level is used to indicate the degree of semantic matching between the code to be converted and the target code. The style matching degree refers to the degree of matching between the style of the code obtained by the first model and the target style.

12. The method according to any one of claims 1-11, characterized in that, The process of obtaining the code to be converted includes: Receive a code generation request from a user device, the code generation request including the code to be converted; The method further includes sending the target code to the user equipment.

13. The method according to any one of claims 1-12, characterized in that, The method further includes: Receive instruction information in the second programming language input by the user.

14. The method according to any one of claims 1-13, characterized in that, The step of obtaining the target code based on the feature representation using the first model includes: Using the first model, the target code is obtained based on the feature representation and the indication information of the target style.

15. The method according to claim 14, characterized in that, The method further includes: Receive the target style instruction information input by the user.

16. A code generation device, characterized in that, The code generation device is equipped with an encoder or has the ability to invoke the encoder; the code generation device is also equipped with a first model or has the ability to invoke the first model. The code generation device includes an acquisition module, a semantic alignment module, an encoding module, and a processing module, wherein: The acquisition module is used to acquire the code to be converted, which includes lexical elements in the first programming language; The processing module is used for: An intermediate representation is obtained based on the code to be converted. The lexical elements in the intermediate representation belong to the lexical elements of the second programming language. The lexical elements of the intermediate representation are obtained from the lexical elements in the code to be converted based on semantic mapping. The syntax structure of the intermediate representation is the syntax structure of the code to be converted. Using the encoder, a feature representation is obtained at least based on the intermediate representation, the feature representation including at least the features of the intermediate representation extracted by the encoder; Using the first model, target code is obtained based on the feature representation. The programming language of the target code is the second programming language. The first model is pre-trained using training data.

17. The code generation apparatus according to claim 15, characterized in that, The processing module is used for: Using a semantic alignment matrix, lexical elements in the code to be converted are mapped to corresponding lexical elements in the second programming language. The semantic alignment matrix is ​​used to indicate the semantic similarity between lexical elements in the first programming language and lexical elements in the second programming language.

18. The code generation apparatus according to claim 17, characterized in that, The code generation device is further equipped with a semantic alignment model, or the code generation device has the ability to invoke a semantic alignment model. The processing module is further configured to: The semantic alignment model provides the semantic alignment matrix, which is semantically optimized, including: Maximize the similarity between the first target word element and the first word element, and / or minimize the similarity between the second target word element and the first word element; Wherein, the first lexical element belongs to the lexical element in the first programming language, the first target lexical element is a lexical element whose semantics are aligned with the semantics of the first lexical element among a plurality of second lexical elements, the second target lexical element is a lexical element whose semantics are not aligned with the semantics of the first lexical element among the plurality of second lexical elements, and the plurality of second lexical elements belong to the elements in the second programming language.

19. The code generation apparatus according to any one of claims 16-18, characterized in that, The processing module is used for: Using the encoder, at least two levels of features are obtained based on the intermediate representation, wherein each level of features corresponds to at least one structural level of the intermediate representation; The feature representation is obtained based on the at least two hierarchical features.

20. The code generation apparatus according to any one of claims 16-19, characterized in that, The processing module is used for: Using the encoder, at least two levels of features are obtained based on the intermediate representation and the code to be converted. Each of the at least two levels of features corresponds to at least one structural level of the intermediate representation or to at least one hierarchical structure of the code to be converted. The feature representation is obtained based on the at least two hierarchical features.

21. The code generation apparatus according to claim 20, characterized in that, The processing module is used for: Using the first encoder, the syntactic features of the code to be converted are obtained based on the code to be converted; Using the second encoder, the semantic features and style features of the intermediate representation are obtained based on the syntactic features of the intermediate representation and the code to be converted; The features at least two levels include the syntactic features of the code to be converted, the semantic features of the intermediate representation, and the style features of the intermediate representation.

22. The code generation apparatus according to any one of claims 16-21, characterized in that, The first model is optimized using a loss function, which is used to evaluate the style matching degree and / or format conformity degree of the code obtained by the first model. The style matching degree refers to the degree of matching between the style of the code obtained by the first model and the target style.

23. The code generation apparatus according to claim 22, characterized in that, The loss function is related to at least one of the style loss function and the format specification loss function; The style loss function is used to evaluate the style matching degree of the code obtained by the first model, and the format normalization loss function is used to evaluate the format normalization degree of the code obtained by the first model. The style loss function corresponds to the first weight. The format specification loss function corresponds to the second weight.

24. The code generation apparatus according to any one of claims 16-23, characterized in that, The code generation device is also equipped with an optimization model or the code generation device has the ability to invoke the optimization model; The processing module is used for: Based on the aforementioned feature representation, prompt words are obtained; Using the first model, the code to be optimized is obtained based on the prompt words; Using the optimization model, the prompt words are optimized to obtain the optimized prompt words; Using the first model, the target code is obtained based on the optimized prompt words.

25. The code generation apparatus according to any one of claims 16-24, characterized in that, The code generation device is also equipped with an optimization model or the code generation device has the ability to invoke the optimization model; The processing module is used for: Based on the aforementioned feature representation, prompt words are obtained; The search results are obtained based on the feature representation, and the search results are used to assist in code generation; Using the first model, the code to be optimized is obtained based on the prompt words and the search results; Using the optimization model, optimized prompt words are obtained based on the code to be optimized; Using the first model, the target code is obtained based on the optimized prompts and the search results.

26. The code generation apparatus according to claim 24 or 25, characterized in that, The optimization model is used to optimize prompt words based on a reward function. The reward function is used to evaluate at least one of the following metrics for the code obtained by the first model under the current prompt word: Grammatical accuracy, semantic consistency, style matching, or formatting compliance; The semantic consistency level is used to indicate the degree of semantic matching between the code to be converted and the target code. The style matching degree refers to the degree of matching between the style of the code obtained by the first model and the target style.

27. The code generation apparatus according to any one of claims 16-26, characterized in that, The code generation device also includes a feedback module; The acquisition module is used to receive a code generation request from a user device, the code generation request including the code to be converted; The feedback module is used to send the target code to the user equipment.

28. The code generation apparatus according to any one of claims 16-27, characterized in that, The acquisition module is also used to receive instruction information of the second programming language input by the user.

29. The code generation apparatus according to any one of claims 16-28, characterized in that, The processing module is further configured to: use the first model to obtain the target code based on the feature representation and the indication information of the target style.

30. The code generation apparatus according to claim 29, characterized in that, The acquisition module is also used to receive indication information of the target style input by the user.

31. A computing device, characterized in that, The computing device includes a processor and a memory, the memory storing a computer program, and the processor being used to invoke the computer program stored in the memory to implement the method according to any one of claims 1-15.

32. A code generation system, characterized in that, It includes a computing device and a user device, the computing device and the user device having a communication connection, the user device being used to send a code generation request to the computing device, the code generation request including the code to be converted; The computing device is used to implement the method according to any one of claims 1-15.

33. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions that, when executed by at least one processor, implement the method according to any one of claims 1-15.

34. A computer program product, characterized in that, The computer program product includes computer instructions that, when executed by at least one processor, implement the method of any one of claims 1-15.