Code generation method and apparatus
By extracting class diagrams and/or sequence diagrams from the initial code as prompts, the generation quality problem of large language models under the constraints of complex code understanding and data length is solved, achieving efficient and accurate code generation.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- CHINABANK PAYMENT (BEIJING) TECH CO LTD
- Filing Date
- 2024-11-25
- Publication Date
- 2026-06-02
AI Technical Summary
Large language models struggle to accurately understand existing code when dealing with complex code, resulting in low-quality generated code. Furthermore, data length limitations prevent one-time input, hindering code integration.
By extracting class diagram code and/or sequence diagram code from the initial code as prompts, an input model is constructed to generate target code corresponding to the software development requirements.
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 code generation.
Smart Images

Figure CN122132015A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of artificial intelligence technology, and in particular to a code generation method and apparatus. Background Technology
[0002] 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
[0003] In view of this, embodiments of the present invention 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 of the input model, thereby improving the quality of the generated code.
[0004] To achieve the above objectives, according to one aspect of the present invention, a code generation method is provided.
[0005] The code generation method of this invention includes: determining initial code that has been written and is related to software development requirements; 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; and inputting the prompt information into a pre-trained code generation model to obtain target code corresponding to the software development requirements.
[0006] 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.
[0007] 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.
[0008] 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.
[0009] Optionally, the interaction between instances of the class structure includes: the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0010] Optionally, generating the prompt information based on the class structure feature code and the software development requirement information includes: 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 the software development requirement information into the 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 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 obtained, concatenating the class diagram code, the sequence diagram code, and the software development requirement information into the prompt information.
[0011] Optionally, the relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0012] To achieve the above objectives, according to another aspect of the present invention, a code generation apparatus is provided.
[0013] The code generation apparatus of this invention includes: a code preparation unit, an extraction unit, and a code acquisition unit.
[0014] 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; and input the prompt information into a pre-trained code generation model to obtain the target code corresponding to the software development requirements information.
[0015] 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.
[0016] Optionally, the extraction unit 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.
[0017] 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.
[0018] Optionally, the interaction between instances of the class structure includes: the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0019] 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 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.
[0020] Optionally, the relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0021] To achieve the above objectives, according to another aspect of the present invention, an electronic device is provided.
[0022] An electronic device according to the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the code generation method provided by the present invention.
[0023] To achieve the above objectives, according to another aspect of the present invention, a computer-readable storage medium is provided.
[0024] The present invention provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the code generation method provided by the present invention.
[0025] To achieve the above objectives, according to another aspect of the present invention, a computer program product is provided.
[0026] One computer program product of the present invention includes a computer program that, when executed by a processor, implements the code generation method provided by the present invention.
[0027] According to the technical solution of the present invention, the embodiments described above have the following advantages or beneficial effects: First, the initial code already written and 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.
[0028] The further effects of the aforementioned unconventional alternative methods will be explained below in conjunction with specific implementation methods. Attached Figure Description
[0029] The accompanying drawings are provided to better understand the invention and are not intended to unduly limit the scope of the invention. Wherein: Figure 1 This is a schematic diagram of the main steps of the code generation method in an embodiment of the present invention; Figure 2 This is a flowchart illustrating the code generation method in an embodiment of the present invention; Figure 3 This is a schematic diagram illustrating the specific execution steps of the code generation method in an embodiment of the present invention; Figure 4 This is a schematic diagram of the components of the code generation device in an embodiment of the present invention; Figure 5 This is an exemplary system architecture diagram that can be applied thereto according to embodiments of the present invention; Figure 6 This is a schematic diagram of the electronic device structure used to implement the code generation method in the embodiments of the present invention. Detailed Implementation
[0030] The following description, in conjunction with the accompanying drawings, illustrates exemplary embodiments of the present invention, including various details to aid understanding. These details 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 the invention. Similarly, for clarity and brevity, descriptions of well-known functions and structures are omitted in the following description.
[0031] It should be noted that, unless otherwise specified, the embodiments of the present invention and the technical features thereof can be combined with each other.
[0032] Figure 1 This is a schematic diagram of the main steps of the code generation method in an embodiment of the present invention.
[0033] like Figure 1 As shown, the specific execution steps of the code generation method in this embodiment of the invention are as follows: 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.
[0034] 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.
[0035] 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.
[0036] 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.
[0037] Figure 2 This is a flowchart illustrating the code generation method in an embodiment of the present invention. For example... Figure 2 As shown.
[0038] Step S201: Obtain 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 text 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.
[0039] 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.
[0040] 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.).
[0041] 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.
[0042] 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.
[0043] Figure 3 This is a schematic diagram illustrating the specific execution steps of the code generation method in an embodiment of the present invention. For example... Figure 3 As shown.
[0044] 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.
[0045] 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: class Customer { private String customerId; private String name; private String address; private List <order>orders = new ArrayList<>(); public void placeOrder(Order order) { ... } public void cancelOrder(Order order) { ... } }
[0046] 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.
[0047] 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.
[0048] 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.
[0049] 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 invention, 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: class Customer { - customerId: String - Name: String - address: String - orders: List <order> + placeOrder(Order order): void + cancelOrder(Order order): void } 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: @startuml participant "customer:Customer" as customer participant "order:Order" as order participant "payment:Payment" as payment customer -> order: createOrder() activate order order -> order : Set orderStatus = CONFIRMED deactivate order customer <-- order : Order created @enduml 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.
[0050] 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.
[0051] 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.
[0052] In some embodiments, class diagram code, sequence diagram code, software development requirements information, and information related to the current code generation task can be combined into 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, limiting the programming architecture to Model-View-Controller (MVC) architecture, etc.). For instance, class diagram code, software development requirements information, and some information related to the current code generation task can be combined into a prompt message; sequence diagram code, software development requirements information, and some information related to the current code generation task can be combined into a prompt message; and class diagram code, sequence diagram code, software development requirements information, and some information related to the current code generation task can be combined into a prompt message.
[0053] 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.
[0054] 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.
[0055] 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 invention, through the above extraction of class diagram code and / or sequence diagram code, reduces the amount of data input to the model, enabling one-time input within the model's input data length limit, thereby further improving code generation quality.
[0056] In the technical solution of this invention, in order to address the shortcomings of existing technologies in processing large project code and integrating existing code structures 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 with the generated code, and reducing the learning cost for developers and the time for assembling prompt information.
[0057] It should be noted that the technical solutions of this invention, 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.
[0058] 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 the present invention 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 the present invention.
[0059] To facilitate better implementation of the above-described solutions of the embodiments of the present invention, related apparatus for implementing the above-described solutions is also provided below.
[0060] Please see Figure 4 As shown, the code generation device 400 provided in this embodiment of the invention may include: a code preparation unit 401, an extraction unit 402, and a code acquisition unit 403.
[0061] 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.
[0062] In this embodiment of the invention, 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.
[0063] 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.
[0064] 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.
[0065] Preferably, the interaction between instances of the class structure includes: the timing relationship of message transmission between multiple objects instantiated from the class structure.
[0066] 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.
[0067] Furthermore, in this embodiment of the invention, the relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
[0068] According to the technical solution of the present invention, 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 that the input data cannot be input all at once due to the model's limitation on the length of the 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 the generated code.
[0069] Figure 5 An exemplary system architecture 500 is shown that can be applied to the code generation method or code generation apparatus of the present invention.
[0070] like Figure 5 As shown, system architecture 500 may include terminal devices 501, 502, and 503, network 504, and server 505 (this architecture is merely an example; the components included in a specific architecture may be adjusted according to the specific application). Network 504 serves as the medium for providing a communication link between terminal devices 501, 502, and 503 and server 505. Network 504 may include various connection types, such as wired or wireless communication links or fiber optic cables.
[0071] 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 can be installed on terminal devices 501, 502, and 503, such as code generation applications (for example only).
[0072] 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.
[0073] 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.
[0074] It should be noted that the code generation method provided in the embodiments of the present invention is generally executed by server 505, and correspondingly, the code generation device is generally set in server 505.
[0075] It should be understood that Figure 5 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0076] The present invention also provides an electronic device. The electronic device according to an embodiment of the present invention includes: one or more processors; and a storage device for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors implement the code generation method provided by the present invention.
[0077] The following is for reference. Figure 6 It shows a schematic diagram of the structure of a computer system 600 suitable for implementing an electronic device according to embodiments of the present invention. Figure 6 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of use of the embodiments of the present invention.
[0078] like Figure 6 As shown, 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.
[0079] 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.
[0080] In particular, according to the embodiments disclosed in this invention, the processes described in the above main step diagrams can be implemented as computer software programs. For example, embodiments of this invention 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 invention.
[0081] It should be noted that the computer-readable medium shown in this invention can be a computer-readable signal medium or a computer-readable storage medium, or any combination thereof. A computer-readable storage medium can be, for example,—but not limited to—an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of a computer-readable storage medium may include, but are not limited to: an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. In this invention, a computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. In this invention, a computer-readable signal medium can include a data signal propagated in baseband or as part of a carrier wave, carrying computer-readable program code. Such propagated data signals can take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A computer-readable signal medium may also be any computer-readable 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.
[0082] 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 the present invention. 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.
[0083] The units described in the embodiments of the present invention 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."
[0084] In another aspect, the present invention 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.
[0085] The present invention also provides a computer program product, including a computer program that, when executed by a processor, implements the code generation method provided by the present invention.
[0086] In the technical solution of this invention 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.
[0087] The specific embodiments described above do not constitute a limitation on the scope of protection of this invention. 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 invention should be included within the scope of protection of this invention.< / order> < / order>
Claims
1. A code generation method, characterized in that, include: 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; 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, characterized in that, 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, characterized in that, 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, characterized in that, The class structure includes classes and / or interfaces; If any class is included in the initial code, the static characteristics of that class include: the class name, attribute information, and method information; 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, characterized in that, 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, characterized in that, 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 requirement information are concatenated to form the prompt information; 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, characterized in that, The relationships between the class structures include at least one of the following: inheritance, implementation, aggregation, composition, association, and dependency.
8. A code generation device, characterized in that, include: 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 used to obtain the class structure feature code of the initial code; wherein the class structure feature code represents 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 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, characterized in that, include: 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, characterized in that, 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, characterized in that, When the computer program is executed by a processor, it implements the method as described in any one of claims 1-7.