Code generation method and apparatus
By extracting class diagram code and/or sequence diagram code from the initial code as prompting information, the problem of understanding large language models in complex software systems is solved, and efficient and accurate code generation is achieved.
Patent Information
- Authority / Receiving Office
- WO · WO
- Patent Type
- Applications
- Current Assignee / Owner
- CHINABANK PAYMENT (BEIJING) TECH CO LTD
- Filing Date
- 2025-08-12
- Publication Date
- 2026-05-28
AI Technical Summary
Large language models struggle to accurately understand existing code when dealing with complex software systems, resulting in low-quality generated code. Furthermore, the limitation on the length of input data prevents the input of code for complex scenarios in one go.
By extracting class diagram code and/or sequence diagram code from the initial code, prompts for the input model are constructed to generate high-quality target code.
It improves the quality of code generation in complex scenarios, reduces the amount of input data, and enables one-time input within the model input data length limit, thereby improving the accuracy and efficiency of generated code.
Smart Images

Figure CN2025114090_28052026_PF_FP_ABST
Abstract
Description
Code generation methods and apparatus
[0001] Cross-references to related applications
[0002] This application claims priority to Chinese Patent Application No. 202411697773.X, filed on November 25, 2024, the entire contents of which are incorporated herein by reference. Technical Field
[0003] This disclosure relates to the field of artificial intelligence technology, and in particular to a code generation method and apparatus. Background Technology
[0004] With the rapid development of software development technology, automatic code generation technology has become one of the important means to improve software development efficiency and quality. In existing technologies, large language models can automatically generate code based on user needs. However, this requires users to input existing code from the scenario into the large language model so that it can understand and calculate, thereby generating the required code implementation. In practical applications, if the software system is complex or has a large amount of code, the large language model struggles to accurately understand the complex input code, resulting in low-quality generated code that cannot be integrated into existing code. Furthermore, various language models have limitations on the length of input data. In complex scenarios, existing code cannot be input as a prompt all at once; instead, existing code must be input multiple times or after deletion, further affecting the quality of the generated code. Summary of the Invention
[0005] In view of this, embodiments of the present disclosure provide a code generation method and apparatus that can extract class diagram code and / or sequence diagram code of the initial code to construct prompt information for the input model, thereby improving the quality of the generated code.
[0006] To achieve the above objectives, according to one aspect of this disclosure, a code generation method is provided.
[0007] The code generation method of this disclosure includes: determining initial code that has been written and is related to software development requirements information; wherein the initial code contains multiple class structures; obtaining class structure feature codes of the initial code; wherein the class structure feature codes represent key information in the initial code; generating prompt information based on the class structure feature codes and the software development requirements information; and inputting the prompt information into a pre-trained code generation model to obtain target code corresponding to the software development requirements information.
[0008] Optionally, the class structure feature code includes at least one of the following: class diagram code and sequence diagram code; the class diagram code represents the static features of the class structure and the relationships between class structures, and the sequence diagram code represents the interaction methods between instances of the class structure.
[0009] Optionally, obtaining the class structure feature code of the initial code includes: inputting the initial code into a pre-trained code structure extraction model to obtain the class diagram code and / or sequence diagram code of the initial code.
[0010] Optionally, the class structure includes classes and / or interfaces; if the initial code contains any class, the static characteristics of the class include: the class name, attribute information, and method information; if the initial code contains any interface, the static characteristics of the interface include: the interface name and the method information implemented by the interface.
[0011] Optionally, the interaction between instances of a class structure includes the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0012] Optionally, prompt information is generated based on class structure feature code and software development requirement information, including: when the class diagram code of the initial code is obtained but the sequence diagram code of the initial code is not obtained, concatenating the class diagram code and software development requirement information into prompt information; when the sequence diagram code of the initial code is obtained but the class diagram code of the initial code is not obtained, concatenating the sequence diagram code and software development requirement information into prompt information; when both the class diagram code and the sequence diagram code of the initial code are obtained, concatenating the class diagram code, the sequence diagram code, and the software development requirement information into prompt information.
[0013] Optionally, the relationships between class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0014] To achieve the above objectives, according to another aspect of this disclosure, a code generation apparatus is provided.
[0015] The code generation apparatus of this disclosure includes: a code preparation unit, an extraction unit, and a code acquisition unit.
[0016] The code preparation unit is used to determine the initial code that has been written and is related to the software development requirements information. The initial code contains multiple class structures. The extraction unit is used to obtain the class structure feature code of the initial code. The class structure feature code represents the key information in the initial code. The code acquisition unit is used to generate prompt information based on the class structure feature code and the software development requirements information. The prompt information is input into a pre-trained code generation model to obtain the target code corresponding to the software development requirements information.
[0017] Optionally, the class structure feature code includes at least one of the following: class diagram code and sequence diagram code; the class diagram code represents the static features of the class structure and the relationships between class structures, and the sequence diagram code represents the interaction methods between instances of the class structure.
[0018] Optionally, the extraction unit is also used to: input the initial code into a pre-trained code structure extraction model to obtain the class diagram code and / or sequence diagram code of the initial code.
[0019] Optionally, the class structure includes classes and / or interfaces; if the initial code contains any class, the static characteristics of the class include: the class name, attribute information, and method information; if the initial code contains any interface, the static characteristics of the interface include: the interface name and the method information implemented by the interface.
[0020] Optionally, the interaction between instances of a class structure includes the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0021] Optionally, the code acquisition unit is further configured to: when the class diagram code of the initial code is acquired but the sequence diagram code of the initial code is not acquired, concatenate the class diagram code and the software development requirement information into a prompt message; when the sequence diagram code of the initial code is acquired but the class diagram code of the initial code is not acquired, concatenate the sequence diagram code and the software development requirement information into a prompt message; when both the class diagram code and the sequence diagram code of the initial code are acquired, concatenate the class diagram code, the sequence diagram code, and the software development requirement information into a prompt message.
[0022] Optionally, the relationships between class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0023] To achieve the above objectives, according to another aspect of this disclosure, an electronic device is provided.
[0024] An electronic device disclosed herein includes: one or more processors; and a storage device for storing one or more programs, which, when executed by one or more processors, cause the one or more processors to implement the code generation method provided herein.
[0025] To achieve the above objectives, according to another aspect of this disclosure, a computer-readable storage medium is provided.
[0026] This disclosure provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code generation method provided in this disclosure.
[0027] To achieve the above objectives, according to another aspect of this disclosure, a computer program product is provided.
[0028] One computer program product disclosed herein includes a computer program that, when executed by a processor, implements the code generation method provided herein.
[0029] According to the technical solution of this disclosure, the embodiments of the invention described above have the following advantages or beneficial effects:
[0030] First, the existing initial code related to the software development requirements is identified. Then, the class structure feature code of the initial code is obtained (this may include class diagram code representing the static features and relationships of class structures, and / or sequence diagram code representing instance interaction methods). Based on the class structure feature code and the software development requirements, prompt information is generated. This prompt information is then input into the code generation model to obtain the target code corresponding to the software development requirements. In this way, by extracting class structure feature code representing key information from the initial code, and then using it to construct the prompt information for the input model, the model can directly obtain the basic framework and key information of the initial code, thus more accurately understanding the prompt information and improving the code generation quality in complex scenarios. Furthermore, after the above extraction of class structure feature code, the amount of data input to the model is reduced, allowing for one-time input within the model input data length limit, thereby further improving the code generation quality.
[0031] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0032] The accompanying drawings are provided to better understand this disclosure and do not constitute an undue limitation thereof. Wherein:
[0033] Figure 1 is a schematic diagram of the main steps of the code generation method in an embodiment of this disclosure;
[0034] Figure 2 is a flowchart illustrating the code generation method in an embodiment of this disclosure;
[0035] Figure 3 is a schematic diagram of the specific execution steps of the code generation method in an embodiment of this disclosure;
[0036] Figure 4 is a schematic diagram of the components of the code generation apparatus in an embodiment of this disclosure;
[0037] Figure 5 is an exemplary system architecture diagram that can be applied thereto according to embodiments of the present disclosure;
[0038] Figure 6 is a schematic diagram of the electronic device structure used to implement the code generation method in the embodiments of this disclosure. Detailed Implementation
[0039] The exemplary embodiments of this disclosure are described below with reference to the accompanying drawings, including various details of the embodiments to aid understanding, and should be considered merely exemplary. Therefore, those skilled in the art will recognize that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of this disclosure. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0040] It should be noted that, unless otherwise specified, the embodiments of this disclosure and the technical features thereof can be combined with each other.
[0041] Figure 1 is a schematic diagram of the main steps of the code generation method in an embodiment of this disclosure.
[0042] As shown in Figure 1, the specific execution steps of the code generation method in this embodiment are as follows:
[0043] Step S101: Identify the existing initial code related to the software development requirements. In this step, the initial code related to the current requirements is first identified, i.e., the existing code. Subsequent code generation will be based on the existing code and the current requirements.
[0044] Step S102: Obtain the class structure feature code of the initial code. The class structure feature code is used to represent key information in the initial code. Preferably, the class structure feature code may include one or two of the following: class diagram code and sequence diagram code. This step uses the extracted class structure feature code from the initial code to replace the initial code for subsequent model calculations.
[0045] Step S103: Generate prompt information based on class structure feature code and software development requirement information; input the prompt information into a pre-trained code generation model to obtain the target code corresponding to the software development requirement information. In this step, prompt information (i.e., the prompt word) is generated based on the class structure feature code in the initial code and combined with the software development requirement information, and then the model is used to automatically generate code.
[0046] Through the above steps, class structure feature codes representing key information in the initial code are extracted. These features are then used to construct prompts for the input model, enabling the model to directly grasp the basic framework and key information of the initial code, thus understanding the prompts more accurately and improving the code generation quality in complex scenarios. Furthermore, the extraction of class structure feature codes reduces the amount of data input to the model, allowing for one-time input within the model's input data length limit, further improving code generation quality.
[0047] Figure 2 is a flowchart illustrating the code generation method in an embodiment of this disclosure. (See Figure 2.)
[0048] Step S201: Determine the initial code. In this step, the initial code is the written code related to the software development requirements information. The software development requirements information can be textual information used to represent the development requirements under the current scenario or the current software system. In one embodiment, the initial code can be code based on a programming language such as Java, which may contain multiple class structures, including classes or interfaces.
[0049] Step S202: Extract the class structure feature code of the initial code. The class structure feature code is used to characterize the key information of the initial code. For example, the key information may include at least one of the following: static features of multiple class structures, relationships between multiple class structures, and interaction methods between instances of class structures. The class structure feature code can be based on UML (Unified Modeling Language) code and may include at least one of the following: class diagram code and sequence diagram code. The class diagram code is used to characterize the static features of multiple class structures and the relationships between multiple class structures, while the sequence diagram code is used to characterize the interaction methods between instances of class structures. In this step, extracting the class structure feature code of the initial code so that prompt information can be generated later can reduce the amount of prompt information data, thus meeting the model's limitation on the amount of data input per input.
[0050] Step S203: Generate prompt information based on class structure feature code and software development requirement information. In this step, the prompt information for the input model can be obtained by concatenating the class structure feature code and the software development requirement information, or by concatenating the class structure feature code, the software development requirement information, and other information related to the current code generation task, in order to obtain higher quality model input. For example, the information related to the current code generation task can be historical successful output results, historical failed output results (success and failure can be determined by manual detection), and manually determined empirical rules (such as limiting the number of lines of generated code, restricting the programming architecture to Model-View-Controller (MVC) architecture, etc.).
[0051] Step S204: Input the prompt information into the code generation model to obtain the target code. The code generation model can be a pre-trained large language model. In this step, the prompt information can be input into the code generation model, which will output the target code corresponding to the current software development requirements.
[0052] Through the above steps, the model can directly obtain the basic framework and key information of the initial code, thereby more accurately understanding the prompt information and improving the code generation quality in complex scenarios. Furthermore, by extracting the class structure feature code, the amount of data input to the model is reduced, enabling one-time input within the model input data length limit, thereby further improving the code generation quality.
[0053] Figure 3 is a schematic diagram illustrating the specific execution steps of the code generation method in this embodiment of the present disclosure. (See Figure 3.)
[0054] Step S301: Determine Initial Code. In this step, the initial code that has been written and is related to the software development requirements information can be determined. The software development requirements information can be textual information used to represent the software development needs of a scenario or software system. For example, if the software system is an already built e-commerce system, and a function needs to be added to it—that is, to adjust and modify the software system—then the software development requirements information could be "Add a new order fulfillment function; the order must be in a paid state." Another example is a caching framework that has already been built; if some adjustments need to be made to the caching framework, the software development requirements information could be "Add an implementation based on the above caching framework to a specific method." The following explanation continues from the previous example.
[0055] The initial code mentioned above refers to existing code, specifically code related to software development requirements—that is, code needed to meet those requirements. In this embodiment, the initial code can be based on programming languages such as Java, and may contain multiple class structures, including classes or interfaces. In one embodiment, key classes related to software development requirements can be selected from the software system's code to form the initial code. The rules for defining these key classes can be flexibly configured as needed. An example of a key class in the initial code is as follows:
[0056] The above class is the Customer (user) class. This class has attributes such as customerId (user identifier), name (user name), address (user address), and orders (order list). It has two methods: placeOrder (add order) and cancelOrder (cancel order). The specific code of the method bodies is omitted.
[0057] Step S302: Generate class diagram code for the initial code; Step S303: Generate sequence diagram code for the initial code. Steps S302 and S303 can be executed individually or both. If both are executed, they can be executed in any order or simultaneously. In one embodiment, the execution method can be determined based on the scenario and business requirements. For example, if the software development requirements are simple, one of steps S302 and S303 can be executed as needed, and prompts can be generated based on the generated class diagram code or sequence diagram code. If the software development requirements are complex, steps S302 and S303 can be executed to generate both class diagram code and sequence diagram code, and prompts can be generated based on these codes.
[0058] For example, the class diagram and sequence diagram code above can be UML-based. The class diagram code is used to represent the static characteristics of multiple class structures and the relationships between them, while the sequence diagram code is used to represent the interaction methods between instances of the class structures. For instance, the static characteristics of a class may include: the class name, attribute information, and method information; the static characteristics of an interface may include: the interface name and the methods implemented by the interface; the relationships between multiple class structures may include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency; and the interaction methods between instances of the class structures may include: the timing relationship of message transmission between multiple objects instantiated from the class structure. The above UML-based class diagram and sequence diagram content is known technology and will not be elaborated here.
[0059] In the above steps, known UML tools can be used to generate class diagram code and sequence diagram code, or machine learning models can be used to generate the above code. In one embodiment, the initial code can be input into a pre-trained code structure extraction model to obtain the class diagram code and / or sequence diagram code of the initial code. The above code structure extraction model can be a pre-trained large language model, which can be the same model as the code generation model described below, or it can be a different model. In this embodiment of the disclosure, when inputting the initial code into the code structure extraction model, if the amount of input data does not meet the input length limit of the code structure extraction model, the initial code can be input in multiple rounds using a multi-round session approach. Continuing the example above, the class diagram code (partial) obtained through the above steps can be as follows:
[0060] The above only shows the static characteristics of the Customer class in the class diagram code. In reality, the class diagram code also includes the static characteristics of other classes and interfaces, as well as the relationships between various structures. The sequence diagram code for the order creation operation obtained through the above steps can be as follows:
[0061] @startuml
[0062] participant"customer:Customer"as customer
[0063] participant"order:Order"as order
[0064] participant"payment:Payment"as payment
[0065] customer->order:createOrder()
[0066] activate order
[0067] order->order:Set orderStatus=CONFIRMED
[0068] deactivate order
[0069] customer<--order:Order created
[0070] @enduml
[0071] In this code, `@startuml` and `@enduml` represent the start and end points, respectively. Lines two, three, and four instantiate the `customer`, `order`, and `payment` classes. Lines five and six indicate that the `customer` object instructs the `order` object to execute `createOrder`, putting the order in an active state. Lines seven and eight show the `order` object updating the order to an inactive state. Line nine indicates that the `order` object returns a message to the `customer` object indicating successful order creation.
[0072] In this way, class diagram code and sequence diagram code can represent the basic framework and key information of the initial code, and reduce the amount of code data.
[0073] Step S304: Generate a prompt message based on the class diagram code and / or sequence diagram code and software development requirements information. In this step, if the class diagram code of the initial code is obtained but the sequence diagram code of the initial code is not obtained, the class diagram code and the software development requirements information are concatenated to form the prompt message; if the sequence diagram code of the initial code is obtained but the class diagram code of the initial code is not obtained, the sequence diagram code and the software development requirements information are concatenated to form the prompt message; if both the class diagram code and the sequence diagram code of the initial code are obtained, the class diagram code, the sequence diagram code, and the software development requirements information are concatenated to form the prompt message.
[0074] In some embodiments, a portion or all of the class diagram code, sequence diagram code, software development requirements information, and information related to the current code generation task can be combined to form a prompt message. For example, the information related to the current code generation task may be historical successful output results, historical failed output results (success and failure can be determined manually), or manually determined empirical rules (e.g., limiting the number of lines of generated code, restricting the programming architecture to Model-View-Controller (MVC) architecture, etc.). For instance, a prompt message can be formed by combining class diagram code, software development requirements information, and some information related to the current code generation task; a prompt message can also be formed by combining sequence diagram code, software development requirements information, and some information related to the current code generation task; or a prompt message can be formed by combining class diagram code, sequence diagram code, software development requirements information, and some information related to the current code generation task.
[0075] Step S305: Input the prompt information into the code generation model to obtain the target code. The code generation model can be a pre-trained large language model. In this step, the prompt information can be input into the code generation model, which will output the target code corresponding to the software development requirements information.
[0076] Step S306: Integrate the initial code and the target code. In this step, the initial code and the target code are integrated to realize the corresponding functions of the software development requirements information.
[0077] Through the above steps, the model can directly obtain the basic framework and key information of the initial code, thereby more accurately understanding the prompts and improving the code generation quality in complex scenarios. Furthermore, for large language models, one-time input has higher memory capacity and is more likely to yield high-quality computational results compared to multiple inputs (i.e., multi-turn conversational input). However, in existing technologies, direct code input in complex scenarios is difficult to meet the input length limitations of large language models. This embodiment of the present disclosure reduces the amount of data input to the model through the above extraction of class diagram code and / or sequence diagram code, enabling one-time input within the model input data length limit, thereby further improving the code generation quality.
[0078] In the technical solution of this disclosure embodiment, in order to solve the shortcomings of the prior art in processing large project code and integrating existing code structure and business logic, the existing initial code is transformed into UML class diagram code and sequence diagram code as a medium, and then combined with the requirements document to generate prompt information. Thus, the large language model generates implementation code that matches the requirements document. This method can enable the large model to understand the existing code more efficiently and accurately, while reducing the complexity of integrating the existing code and the generated code, reducing the learning cost for developers and the time for assembling prompt information.
[0079] It should be noted that the technical solutions disclosed herein, including the collection, updating, analysis, processing, use, transmission, and storage of user personal information, all comply with relevant laws and regulations, are used for legitimate purposes, and do not violate public order and good morals. Necessary measures are taken to prevent unauthorized access to user personal information data and to safeguard user personal information security, network security, and national security.
[0080] For the foregoing method embodiments, they are described as a series of actions for ease of description. However, those skilled in the art should understand that this disclosure is not limited to the described order of actions, and some steps may actually be performed in other orders or simultaneously. Furthermore, those skilled in the art should also understand that the embodiments described in the specification are preferred embodiments, and the actions and modules involved are not necessarily essential for implementing this disclosure.
[0081] To facilitate better implementation of the above-described solutions of the embodiments of this disclosure, related apparatus for implementing the above-described solutions is also provided below.
[0082] Please refer to Figure 4. The code generation apparatus 400 provided in this embodiment may include: a code preparation unit 401, an extraction unit 402, and a code acquisition unit 403.
[0083] The code preparation unit 401 is used to determine the initial code that has been written and is related to the software development requirements information; the initial code contains multiple class structures; the extraction unit 402 is used to obtain the class structure feature code of the initial code; the class structure feature code represents the key information in the initial code; the code acquisition unit 403 is used to generate prompt information based on the class structure feature code and the software development requirements information; and input the prompt information into a pre-trained code generation model to obtain the target code corresponding to the software development requirements information.
[0084] In this embodiment of the disclosure, the class structure feature code includes at least one of the following: class diagram code and sequence diagram code; the class diagram code represents the static features of the class structure and the relationships between class structures, and the sequence diagram code represents the interaction mode between instances of the class structure.
[0085] In one embodiment, the extraction unit 402 is further configured to: input the initial code into a pre-trained code structure extraction model to obtain class diagram code and / or sequence diagram code of the initial code.
[0086] As a preferred embodiment, the class structure includes classes and / or interfaces; if the initial code contains any class, the static characteristics of the class include: the class name, attribute information, and method information; if the initial code contains any interface, the static characteristics of the interface include: the interface name and the method information implemented by the interface.
[0087] Preferably, the interaction between instances of the class structure includes: the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0088] In an optional technical solution, the code acquisition unit 403 is further configured to: when the class diagram code of the initial code is acquired but the sequence diagram code of the initial code is not acquired, concatenate the class diagram code and the software development requirement information into the prompt information; when the sequence diagram code of the initial code is acquired but the class diagram code of the initial code is not acquired, concatenate the sequence diagram code and the software development requirement information into the prompt information; when both the class diagram code and the sequence diagram code of the initial code are acquired, concatenate the class diagram code, the sequence diagram code, and the software development requirement information into the prompt information.
[0089] Furthermore, in this embodiment of the disclosure, the relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0090] According to the technical solution of this disclosure, firstly, corresponding UML class diagram code and sequence diagram code are generated based on the initial code. Then, the requirements document, UML class diagram code, and sequence diagram code are concatenated into new prompt information. This prompt information aims to provide a sufficiently accurate and complete context for the large language model to generate code that meets specific requirements. Subsequently, the prompt information is input into the large language model, and implementation code matching the requirements document is automatically generated. This solves the problem in the prior art where input data cannot be input all at once due to the model's limitation on the length of input data. Furthermore, the input of class diagram code and sequence diagram code helps the model's understanding and calculation, thereby improving the quality of generated code.
[0091] Figure 5 illustrates an exemplary system architecture 500 to which the code generation method or code generation apparatus of the present disclosure can be applied.
[0092] As shown in Figure 5, the system architecture 500 may include terminal devices 501, 502, and 503, a network 504, and a server 505 (this architecture is merely an example; the components included in a specific architecture can be adjusted according to the specific application). The network 504 serves as the medium for providing a communication link between the terminal devices 501, 502, and 503 and the server 505. The network 504 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0093] Users can use terminal devices 501, 502, and 503 to interact with server 505 via network 504 to receive or send messages, etc. Various communication client applications, such as code generation applications (for example only), can be installed on terminal devices 501, 502, and 503.
[0094] Terminal devices 501, 502, and 503 can be various electronic devices with displays that support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0095] Server 505 can be a server that provides various services, such as a backend server (for example only) that supports code generation applications operated by users using terminal devices 501, 502, and 503. The backend server can process received code generation requests and feed back the processing results (such as object code - for example only) to terminal devices 501, 502, and 503.
[0096] It should be noted that the code generation method provided in this embodiment is generally executed by server 505, and correspondingly, the code generation device is generally located in server 505.
[0097] It should be understood that the number of terminal devices, networks, and servers shown in Figure 5 is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0098] This disclosure also provides an electronic device. The electronic device according to embodiments of this disclosure includes: one or more processors; and a storage device for storing one or more programs, which, when executed by the one or more processors, cause the one or more processors to implement the code generation method provided in this disclosure.
[0099] Referring now to FIG6, a schematic diagram of a computer system 600 suitable for implementing an electronic device according to embodiments of the present disclosure is shown. The electronic device shown in FIG6 is merely an example and should not be construed as limiting the functionality and scope of the embodiments of the present disclosure.
[0100] As shown in Figure 6, the computer system 600 includes a central processing unit (CPU) 601, which can perform various appropriate actions and processes based on programs stored in read-only memory (ROM) 602 or programs loaded from storage section 608 into random access memory (RAM) 603. The RAM 603 also stores various programs and data required for the operation of the computer system 600. The CPU 601, ROM 602, and RAM 603 are interconnected via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0101] The following components are connected to I / O interface 605: an input section 606 including a keyboard, mouse, etc.; an output section 607 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to I / O interface 605 as needed. A removable medium 611, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on drive 610 as needed so that computer programs read from it can be installed into storage section 608 as needed.
[0102] In particular, according to embodiments of this disclosure, the processes described in the main step diagrams above can be implemented as computer software programs. For example, embodiments of this disclosure include a computer program product comprising a computer program carried on a computer-readable medium, the computer program containing program code for performing the methods shown in the main step diagrams. In the above embodiments, the computer program can be downloaded and installed from a network via communication section 609, and / or installed from removable medium 611. When the computer program is executed by central processing unit 601, it performs the functions defined in the system of this disclosure.
[0103] It should be noted that the computer-readable medium disclosed herein may be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium may be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this disclosure, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in connection with an instruction execution system, apparatus, or device. In this disclosure, a computer-readable signal medium may include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable medium other than a computer-readable storage medium, which can send, propagate, or transmit a program for use by or in connection with an instruction execution system, apparatus, or device. The program code contained on the computer-readable medium may be transmitted using any suitable medium, including but not limited to: wireless, wire, optical fiber, RF, etc., or any suitable combination thereof.
[0104] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of this disclosure. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0105] The units described in the embodiments of this disclosure can be implemented in software or hardware. The described units can also be located in a processor; for example, a processor can be described as including a code preparation unit, an extraction unit, and a code acquisition unit. The names of these units do not necessarily limit the specific unit; for example, the code preparation unit can also be described as "a unit that provides initial code to the extraction unit."
[0106] In another aspect, this disclosure also provides a computer-readable medium, which may be included in the device described in the above embodiments; or it may exist independently and not assembled into the device. The computer-readable medium carries one or more programs, and when the one or more programs are executed by the device, the steps performed by the device include: determining initial code that has been written in relation to software development requirements information; wherein the initial code contains multiple class structures; obtaining class structure feature codes of the initial code; wherein the class structure feature codes characterize key information in the initial code; generating prompt information based on the class structure feature codes and the software development requirements information; and inputting the prompt information into a pre-trained code generation model to obtain target code corresponding to the software development requirements information.
[0107] This disclosure also provides a computer program product, including a computer program that, when executed by a processor, implements the code generation method provided in this disclosure.
[0108] In the technical solution of this disclosure embodiment, the initial code that has been written and is related to the software development requirements is first determined. Then, the class structure feature code of the initial code is obtained (which may include class diagram code representing the static features and relationships of the class structure in the initial code, and / or sequence diagram code representing the interaction methods of instances in the initial code). Based on the class structure feature code and the software development requirements, prompt information is generated. Subsequently, the prompt information is input into the code generation model to obtain the target code corresponding to the software development requirements. In this way, class structure feature code representing key information in the initial code is extracted and used to construct the prompt information for the input model. This allows the model to directly obtain the basic framework and key information of the initial code, thereby more accurately understanding the prompt information and improving the code generation quality in complex scenarios. Furthermore, after the above extraction of class structure feature code, the amount of data input to the model is reduced, enabling one-time input within the model input data length limit, thereby further improving the code generation quality.
[0109] The specific embodiments described above do not constitute a limitation on the scope of protection of this disclosure. Those skilled in the art should understand that various modifications, combinations, sub-combinations, and substitutions can occur depending on design requirements and other factors. Any modifications, equivalent substitutions, and improvements made within the spirit and principles of this disclosure should be included within the scope of protection of this disclosure.
Claims
1. A code generation method, comprising: Identify the initial code that has been written and is related to the software development requirements; wherein the initial code contains multiple class structures; Obtain the class structure feature code of the initial code; wherein the class structure feature code represents key information in the initial code; and Based on the class structure feature code and the software development requirements information, a prompt message is generated; the prompt message is input into a pre-trained code generation model to obtain the target code corresponding to the software development requirements information.
2. The method according to claim 1, wherein, The class structure feature code includes at least one of the following: class diagram code, sequence diagram code; The class diagram code represents the static characteristics of the class structure and the relationships between class structures, while the sequence diagram code represents the interaction methods between instances of the class structure.
3. The method according to claim 2, wherein, The step of obtaining the class structure feature code of the initial code includes: The initial code is input into a pre-trained code structure extraction model to obtain class diagram code and / or sequence diagram code of the initial code.
4. The method according to claim 2, wherein, The class structure includes classes and / or interfaces; If the initial code contains any class, the static characteristics of that class include: the class name, attribute information, and method information; and If any interface is included in the initial code, the static characteristics of that interface include: the name of the interface and the method information implemented by the interface.
5. The method according to claim 2, wherein, The interaction between instances of the class structure includes the timing relationship of message transmission between multiple objects instantiated from the class structure.
6. The method according to claim 2, wherein, The step of generating prompt information based on the class structure feature code and the software development requirements information includes: If the class diagram code of the initial code is obtained, but the sequence diagram code of the initial code is not obtained, the class diagram code and the software development requirement information are concatenated to form the prompt information; If the sequence diagram code of the initial code is obtained, but the class diagram code of the initial code is not obtained, the sequence diagram code and the software development requirements information are concatenated to form the prompt information; and If the class diagram code and sequence diagram code of the initial code are obtained, the class diagram code, the sequence diagram code, and the software development requirements information are concatenated to form the prompt information.
7. The method according to claim 2, wherein, The relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
8. A code generation apparatus, comprising: A code preparation unit is used to determine the initial code that has been written and is related to software development requirements; wherein the initial code contains multiple class structures; An extraction unit is configured to obtain the class structure feature code of the initial code; wherein the class structure feature code represents key information in the initial code; and The code acquisition unit is used to generate prompt information based on the class structure feature code and the software development requirement information; input the prompt information into a pre-trained code generation model to obtain the target code corresponding to the software development requirement information.
9. An electronic device, comprising: One or more processors; Storage device for storing one or more programs. When the one or more programs are executed by the one or more processors, the one or more processors implement the method as described in any one of claims 1-7.
10. A computer-readable storage medium having a computer program stored thereon, wherein, When the program is executed by the processor, it implements the method as described in any one of claims 1-7.
11. A computer program product comprising a computer program, wherein, When the computer program is executed by a processor, it implements the method as described in any one of claims 1-7.