A code generation method and device, electronic equipment and medium
By analyzing the requirements text and the code context metadata of the business system, API code is automatically generated, solving the problem of low efficiency in API code generation and achieving efficient code generation that matches the system logic and architectural style.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- BEIJING QIYI CENTURY SCI & TECH CO LTD
- Filing Date
- 2026-01-12
- Publication Date
- 2026-05-29
AI Technical Summary
Generating API code is inefficient during the business backend development process, especially when requirements change and services are iterated and updated, which requires a lot of time and repetitive work.
By acquiring the requirement text describing the target API interface, analyzing the functional, parameter, and interaction description information, and combining it with the code context metadata of the business system, API code is automatically generated, taking into account code elements and dependencies to ensure that the generated code matches the system logic and architectural style.
It enables automatic generation of API code, reducing repetitive work for developers, improving generation efficiency, and ensuring that the generated code matches the existing logic and architectural style of the business system.
Smart Images

Figure CN122111417A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of artificial intelligence technology, and in particular to a code generation method, apparatus, electronic device, and medium. Background Technology
[0002] In the development of business backend systems, it is typically necessary to build multiple services for each business function. Each service is used to implement a part of the overall business process, resulting in a large number of services involved in the development of business backend systems. For example, for the payment business in an enterprise application system, multiple services such as order creation service, authentication service, and payment service need to be built.
[0003] Within the same business system, services can call each other, requiring the use of Application Programming Interfaces (APIs). When new services are added to the business system or existing services are iterated and updated, new API code needs to be generated to implement service calls; this need for generating API code is very common.
[0004] Currently, API code is generated by having developers first outline functional requirements and then manually write the API code based on those requirements. Manually writing API code is inefficient and time-consuming, especially during requirement changes and service iterations, involving a lot of repetitive work and is extremely labor-intensive. Summary of the Invention
[0005] The purpose of this application is to provide a code generation method, apparatus, electronic device, and medium to improve the efficiency of generating API code. The specific technical solution is as follows:
[0006] A first aspect of this application provides a code generation method, the method comprising:
[0007] Obtain the requirement text describing the target API interface, wherein the target API interface is the API interface in the business system for which code needs to be written;
[0008] The requirement text is analyzed to obtain the functional description information, parameter description information, and interaction description information of the target API interface.
[0009] Based on the functional description information, parameter description information, and interaction description information, the implementation logic of the target API interface is determined. Based on the code elements and dependencies between code elements recorded in the code context metadata of the business system, the code elements used to write the implementation logic are determined. The target API code of the target API interface is generated based on the determined code elements, wherein the code context metadata is generated based on the existing code of the business system.
[0010] A second aspect of this application provides a code generation apparatus, the apparatus comprising:
[0011] The acquisition module is used to acquire the requirement text describing the target API interface, wherein the target API interface is the API interface in the business system for which code needs to be written;
[0012] The analysis module is used to analyze the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface.
[0013] The generation module is used to determine the implementation logic of the target API interface based on the functional description information, parameter description information, and interaction description information; determine the code elements for writing the implementation logic based on the code elements and dependencies between code elements recorded in the code context metadata of the business system; and generate the target API code of the target API interface based on the determined code elements, wherein the code context metadata is generated based on the existing code of the business system.
[0014] A third aspect of the embodiments of this application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0015] Memory, used to store computer programs;
[0016] A processor, when executing a program stored in memory, implements the code generation method described in the first aspect.
[0017] A fourth aspect of this application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, implements the code generation method described in the first aspect above.
[0018] A fifth aspect of this application also provides a computer program product containing instructions that, when run on a computer, cause the computer to execute the code generation method described in the first aspect.
[0019] The code generation method, apparatus, electronic device, and medium provided in this application embodiment obtain the requirement text describing the target API interface to be written in a business system, analyze the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface, and determine the implementation logic of the target API interface based on this. This application embodiment considers not only the requirement text but also code context metadata, which records the code elements of existing code in the business system and the dependencies between code elements. This reflects the existing logic and architectural style of the business system's code. Therefore, the code elements determined based on the code context metadata for writing the implementation logic are more consistent with the existing logic and architectural style of the business system's code. Thus, the target API code generated based on the determined code elements can match the existing logic and architectural style of the business system. Furthermore, as can be seen from the above description, when generating target API code using the solution provided in this application embodiment, no manual intervention is required, enabling automatic API code generation, reducing repetitive work for developers, and improving API code generation efficiency. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of this application or the prior art, the accompanying drawings used in the description of the embodiments or the prior art will be briefly introduced below.
[0021] Figure 1 A flowchart illustrating a code generation method provided in an embodiment of this application;
[0022] Figure 2 A flowchart illustrating another code generation method provided in this application embodiment;
[0023] Figure 3 A flowchart illustrating a method for constructing an annotation file, provided in an embodiment of this application;
[0024] Figure 4 An exemplary schematic diagram of a syntax tree provided for embodiments of this application;
[0025] Figure 5 An exemplary schematic diagram of the context knowledge graph provided in the embodiments of this application;
[0026] Figure 6 An exemplary schematic diagram of a code generation process provided for an embodiment of this application;
[0027] Figure 7 An exemplary schematic diagram illustrating another code generation process provided in an embodiment of this application;
[0028] Figure 8This is a schematic diagram of the structure of a code generation device provided in an embodiment of this application;
[0029] Figure 9 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0030] The technical solutions in the embodiments of this application will now be described with reference to the accompanying drawings.
[0031] To improve the efficiency of generating API code, embodiments of this application provide a code generation method. This method is applied to electronic devices, such as servers, desktop computers, or laptops, which are devices with data processing capabilities. Figure 1 As shown, the code generation method provided in this application embodiment includes S101-S103.
[0032] S101. Obtain the requirement text describing the target API interface.
[0033] The target API interface is the API interface in the business system for which code needs to be written. The requirement text is the text describing the target API interface using natural language.
[0034] The requirements document can include information describing the target API interface from multiple aspects. For example, the requirements document may include: information describing the functions that the target API interface needs to implement, information describing the input parameters that need to be transmitted when calling the target API interface, information describing the return value after calling the target API interface, and information describing the type of data interaction between the caller and the callee of the target API interface.
[0035] For example, the target API interface is the order query API interface included in the order query service. The design requirement of the order query API interface is: to query user orders by user identifier (ID) and order status, and return a list of found orders and the total number of orders. The requirement text constructed by the developers based on the design requirements of the target API interface is: "Implement the user order query interface, support filtering by user ID and order status, and return a list of orders and the total number of orders." In the requirement text, "order query" describes the function, "user ID and order status" describes the input parameters, "list of orders and total number of orders" describes the return value, and "return" describes the interaction type.
[0036] A business system may include multiple services. For example, a business system may be a school's information management system, including services for querying class information and obtaining exam information.
[0037] When a new service needs to be added to a business system, API code needs to be generated for the new service's API interface. Similarly, when an existing service in a business system is updated, API code needs to be generated for the updated service's API interface. The target API interface is the API interface of the new or updated service, and the developers construct the requirement document for the target API interface.
[0038] Then, the electronic device can receive requirement text entered by the developer, or receive requirement text sent by the developer to the electronic device through other devices.
[0039] S102. Analyze the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface.
[0040] Functional description information describes the functionality implemented by the target API interface. For example, functional description information includes order query and order creation.
[0041] Parameter description information describes the parameters required when calling the target API interface and the parameters returned after calling the target API interface. For example, parameter keywords include user ID and order status.
[0042] Interaction description information describes the type of data interaction between the caller of the target API interface and the callee of the target API interface. For example, interaction description information includes Hypertext Transfer Protocol (HTTP) method types such as GET, POST, and DELETE.
[0043] S103. Based on the functional description information, parameter description information, and interaction description information, determine the implementation logic of the target API interface. Based on the code elements and dependencies between code elements recorded in the code context metadata of the business system, determine the code elements used to write the implementation logic. Generate the target API code of the target API interface based on the determined code elements.
[0044] The code context metadata is generated based on existing code from the business system. Existing code in the business system refers to the existing API code of the services providing business functions within the business system. This existing code can be stored in the business system's code repository.
[0045] The code elements recorded in the code context metadata include: class code, method code, functions, assignment statements, and conditional statements. Dependencies between code elements include: inheritance or calling relationships between class code, containment relationships between class code and method code, and calling relationships between method code.
[0046] The implementation logic of the target API interface can be described in natural language, expressing the processing procedure after the target API interface is called. Furthermore, the attributes of code elements in the code context metadata can include descriptive information about the purpose of the code elements. Therefore, semantic matching of the implementation logic of the target API interface and the attributes of the code elements can yield code elements that match the implementation logic. These matching code elements can then be used to write the implementation logic, thereby generating the target API code for the target API interface.
[0047] Code context metadata can be structured information stored in a structured form, such as graph data. Multiple code context metadata can form a context knowledge graph, where each node in the context knowledge graph represents a code element that already exists in the business system, and the connections between nodes represent the dependencies between code elements.
[0048] Contextual knowledge graphs can be constructed by converting syntax trees into a graph structure. The syntax tree is built upon existing code from the business system, and each node in the syntax tree represents a code element within that existing code. Connections between nodes represent dependencies between these code elements. The specific construction methods for contextual knowledge graphs and syntax trees are described below.
[0049] In real-world applications, existing code within a business system may be updated. The code context metadata can be updated along with the existing code, meaning that the code context metadata includes the updated code elements of the business system and the dependencies between these updated code elements.
[0050] For example, after the target API code is generated, it becomes existing code, adding new code to the existing API codes of the business system. As another example, when a service included in the business system is deleted, the API code for that service is also deleted from the existing code of the business system accordingly.
[0051] The code generation method provided in this application embodiment obtains the requirement text describing the target API interface to be written in a business system, analyzes the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface, and determines the implementation logic of the target API interface based on this. This application embodiment considers not only the requirement text but also code context metadata, which records the code elements and dependencies between existing code in the business system. This reflects the existing logic and architectural style of the business system's code. Therefore, the code elements determined based on the code context metadata for writing the implementation logic are more consistent with the existing logic and architectural style of the business system's code. Thus, the target API code generated based on the determined code elements can match the existing logic and architectural style of the business system. Furthermore, as can be seen from the above description, when generating target API code using the solution provided in this application embodiment, no manual intervention is required, enabling automatic API code generation, reducing repetitive work for developers, and improving API code generation efficiency.
[0052] In some embodiments of this application, see Figure 2 The above-mentioned S102 analyzes the requirement text to obtain the functional description information, parameter description information and interaction description information of the target API interface, including S1021-S1023.
[0053] S1021. Perform word segmentation and part-of-speech recognition on the required text to obtain the words included in the required text and their parts of speech.
[0054] Electronic devices can first perform word segmentation on the required text to obtain multiple words included in the required text, and then perform part-of-speech recognition on each word to obtain the part of speech of each word, which includes nouns, verbs and adjectives.
[0055] S1022. Based on the part-of-speech and semantics of the obtained words, identify the words in the requirement text that describe the functions of the target API interface, the parameters of the target API interface, and the interactions supported by the target API interface.
[0056] Analyzing the part-of-speech tags of the words in the requirement text helps to analyze the grammatical structure of the requirement text and reflects the semantic relationships between the words. Therefore, by combining the obtained part-of-speech tags and semantics, it is possible to identify the words in the requirement text that describe the functions of the target API interface, the parameters of the target API interface, and the interactions supported by the target API interface.
[0057] One approach is to input the part-of-speech and semantics of each word in the requirement text into a large language model. The large language model can then identify words in the requirement text that describe the functionality of the target API interface, the parameters of the target API interface, and the interactions supported by the target API interface.
[0058] For example, the requirement text is "Implement a user order query interface, support filtering by user ID and order status, and return the order". The words describing the function of the target API interface in the requirement text include "order query", the words describing the parameters of the target API interface include "user ID and order status", and the words describing the interactions supported by the target API interface include "return".
[0059] S1023. Based on the design elements corresponding to the entities described by the identified words in the API architecture, map the identified words to code elements respectively, which serve as the functional description information, parameter description information and interactive description information of the target API interface.
[0060] The API architecture can be a Representational State Transfer (RESTful) API architecture. The design elements of a RESTful API architecture include: operation resources, request parameters, return parameters, and HTTP methods. Operation resources refer to the resources on which the operation is performed after calling the API interface; request parameters refer to the parameters required when calling the API interface; return parameters refer to the parameters returned after calling the API interface; and HTTP methods refer to the type of data interaction between the calling and called parties.
[0061] Electronic devices can determine the corresponding design elements in the API architecture based on the pre-configured correspondence between entities and design elements in the API architecture. These entities include resource entities, input parameter entities, return value entities, and interaction entities. For example, a resource entity might be a user order, input parameter entities might be user ID and order status, return value entities might be the order list, and interaction entities might be the return value. Words identified from the requirement text that describe the functionality of the target API interface correspond to resource entities; words describing the input parameters of the target API interface correspond to input parameter entities; words describing the return value of the target API interface correspond to return value entities; and words describing the interaction types supported by the target API interface correspond to interaction entities.
[0062] For example, the term describing the functionality of the target API interface is "user order query." This term describes a resource entity, and the corresponding design element is ` / api / order / query`, where ` / api / order / query` represents the Uniform Resource Locator (URL) path for the user order. The term describing the input parameters of the target API interface is "user ID and order status." This term describes the input parameter entity, and the corresponding design elements are "userId and orderStatus," where `userId` and `orderStatus` represent the parameters required when calling the API interface. The term describing the interaction types supported by the target API interface is "return," which describes an interaction entity, and the corresponding design element is "GET method."
[0063] Then, the electronic device can construct the mapped code elements according to the code element construction method corresponding to the design elements. For example, if the code element is "GET method", the corresponding code element construction method is to match HTTP with the method name in the code element, resulting in the code element "HTTP:GET".
[0064] As one implementation method, the above S1021-S1023 can be implemented through a large language model for text analysis. The implementation method includes: the electronic device fills the requirement text into the first prompt word template to obtain input information, and inputs the input information into the large language model for text analysis, so that the large language model for text analysis performs word segmentation and part-of-speech recognition on the requirement text to obtain the words included in the requirement text and their parts of speech. Based on the obtained parts of speech and semantics of the words, the model identifies the words in the requirement text that describe the function of the target API interface, the words that describe the parameters of the target API interface, and the words that describe the interactions supported by the target API interface. According to the design elements corresponding to the entities described by the identified words in the API architecture, the identified words are mapped to code elements respectively, which serve as the functional description information, parameter description information, and interaction description information of the target API interface.
[0065] For example, the first prompt word template is: "Map each keyword to a code element according to the RESTful API design elements corresponding to the entities described by the keywords in the following text."
[0066] Text: [Requirement Text].
[0067] The text within “[]” represents the content to be filled in, so when constructing the input information, you can simply fill in the required text within “[]”.
[0068] The above-mentioned first prompt word template is only an example provided by the embodiments of this application. The embodiments of this application do not specifically limit the form and content of the first prompt word template.
[0069] The training method for the large language model for text analysis includes: filling the sample requirement text into the first prompt word template as the training sample, and using manually constructed code elements based on the sample requirement text as the training labels for the training samples. The training samples are then input into the large language model for text analysis to obtain the code elements output by the model. Based on the training labels of the training samples and the output of the large language model for text analysis, a loss value is determined. The model parameters of the large language model for text analysis are adjusted using the loss value until the model meets the preset convergence conditions or reaches the preset number of iterations, at which point the training of the large language model for text analysis is considered complete. The preset convergence conditions include: the loss value calculated in the current iteration is less than the preset loss value, or the error between the loss values calculated in the most recent N iterations is less than the preset error, etc.
[0070] Through the above methods, the embodiments of this application can perform word segmentation and part-of-speech recognition on the requirement text, and further map the words in the requirement text into code elements. This realizes the conversion of requirement text described in natural language into code elements, which facilitates the subsequent generation of target API code. Based on the code elements, the user's requirements for the target API code can be understood more accurately, so as to generate more accurate target API code that better meets the user's needs.
[0071] In some embodiments of this application, the method of generating the target API code in S103 above includes steps one and two.
[0072] Step 1: Based on the functional description information, parameter description information, interaction description information, and code context metadata of the business system, generate input information for the code generation large language model.
[0073] Among them, the code generation large language model can be the Code Large Language Model (Code-LLM), etc.
[0074] In one implementation, the electronic device can construct structured information, such as JSON structure, from functional description information, parameter description information, and interaction description information. This structured information, along with the code context metadata of the business system, is then filled into a second prompt word template to obtain the input information for the code generation large language model.
[0075] For example, the second prompt template is: "Please combine your knowledge of code to generate API code that meets the user's needs."
[0076] Code knowledge: [Code context metadata of the business system];
[0077] User requirement: [Structured information].
[0078] In the above-mentioned second prompt word template, the text within "[]" represents the content to be filled in. The above-mentioned second prompt word template is only an example provided by the embodiments of this application, and the embodiments of this application do not specifically limit the form and content of the second prompt word template.
[0079] The input information for the code generation large language model can also include the code style of the target API code. For example, the second prompt word template includes a fixed code style, so that the input information constructed according to the second prompt word template includes the code style.
[0080] The code styles include RESTful, Simple Object Access Protocol (SOAP), and Graph Query Language (GraphQL).
[0081] Step 2: Input the input information into the code to generate a large language model, and obtain the target API code of the target API interface output by the code-generated large language model.
[0082] The code generation large language model determines the implementation logic of the target API interface based on functional description information, parameter description information, and interaction description information. Based on the code elements and dependencies between code elements recorded in the code context metadata, it determines the code elements used to write the implementation logic and generates the target API code based on the determined code elements.
[0083] The training method for the code-generated large language model includes: constructing input information based on the functional description information, parameter description information, and interaction description information of the sample API interface, as well as code context metadata, as training samples; using manually written API code for the sample API interface as training labels for the training samples; inputting the training samples into the code-generated large language model to obtain the API code output by the code-generated large language model; determining the loss value based on the training labels of the training samples and the output results of the code-generated large language model; adjusting the model parameters of the code-generated large language model using the loss value until the code-generated large language model meets the preset convergence condition or reaches the preset number of iterations, at which point the training of the code-generated large language model is considered complete.
[0084] In this embodiment, functional description information, parameter description information, interaction description information, and code context metadata of the business system are constructed as input information for a large code generation language model. Since the large code generation language model possesses powerful data understanding and generation capabilities, it can more deeply analyze the various descriptive information contained in the input information, thereby more accurately understanding the requirements of the target API code. It can also more deeply analyze the code context metadata, thereby more accurately understanding the implementation logic of existing code in the business system. This allows for the generation of target API code that better meets the requirements of the target API code and is more suitable for the business system.
[0085] In some embodiments of this application, the electronic device may also obtain code writing constraint information.
[0086] Constraint information can include basic constraints and personalized constraints. When generating different API code for the same business system, the basic constraints can be the same, but the personalized constraints can be different.
[0087] Basic constraints may include: the target API code's package structure must be the same as the existing code's package structure, and the target API code must only support the use of specified utility classes. Since all existing code belongs to the same business system, their package structures must be identical, and the target API code must also adopt the same package structure to ensure that the target API code can adapt to the business system.
[0088] Custom constraints can be set by developers according to actual needs. For example, custom constraints may include setting the version number format of the target API code to a preset format, and including annotation information of a specified type in the target API code, etc., without specific limitations here.
[0089] Basic constraints can be stored in a preset location, from which electronic devices can retrieve them. Personalized constraints can be set by the user before each generation of target API code. Electronic devices can receive personalized constraints input by the user or sent by the user through other devices.
[0090] Based on this, the above-mentioned method of generating the target API code of the target API interface based on the determined code elements can be implemented as follows: based on the determined code elements, generate the target API code of the target API interface according to the code writing constraint information.
[0091] Electronic devices can generate input information for a large code generation language model based on functional description information, parameter description information, interaction description information, code context metadata, and constraint information. This input information is then fed into the large code generation language model, enabling it to generate target API code according to the code writing constraints. The method for constructing the input information is similar to the description above, with the only difference being the addition of constraint information; it will not be elaborated upon here.
[0092] This application supports setting constraint information for target API code and generating target API code based on the constraint information, which improves the flexibility of generating target API code and makes the target API code more in line with actual needs.
[0093] After obtaining the target API code, the electronic device can perform preliminary syntax verification on the target API code, for example, using a Java compiler to perform a quick syntax error check. If the check result indicates that the target API code contains syntax errors, the check result can be filled into the input information of the code generation language model, and the input information can be re-entered into the code generation language model so that the code generation language model understands the syntax errors in the target API code, eliminates the syntax errors, and thus regenerates the target API code.
[0094] In some embodiments of this application, after generating the target API code for the target API interface, the electronic device can also construct annotation text for the target API code, see [link to relevant documentation]. Figure 3 The construction methods include the following S301 and S302.
[0095] S301. Based on the code of the first code element in the target API code, determine the annotation value of the annotation item in the annotation template corresponding to the type of the first code element. Based on the annotation template and the determined annotation value, add the first annotation information to the first code element in the target API code.
[0096] Different types of code elements may correspond to different annotation templates. Taking Swagger annotations as an example, different types of code elements can correspond to different types of Swagger annotation templates. For instance, class code can correspond to the `@Api` type annotation template, which includes the class name and a description of the functionality implemented by the class. Method code can correspond to both `@ApiOperation` and `@ApiParam` type annotation templates. The `@ApiOperation` type annotation template includes the method name and a description of the functionality implemented by the method; the `@ApiParam` type annotation template includes the parameter names and types of the parameters passed to the method or the parameter names and types of the parameters returned after the method is called.
[0097] Since each annotation item in the annotation template of a code element describes one aspect of the code element, determining the aspect described by the code at different locations within the code element allows us to determine the annotation item corresponding to the code at different locations. This enables us to obtain the annotation value of the annotation item from the code corresponding to the annotation item, so that, based on the annotation template and the determined annotation value, we can add the first annotation information to the first code element in the target API code.
[0098] As one implementation method, S301 can be implemented by a code generation large language model. The implementation includes: after inputting the input information into the code generation large language model, in addition to generating the target API code, the code generation large language model can determine the annotation value of the annotation item in the annotation template corresponding to the type of the first code element based on the code of the first code element in the target API code. Based on the annotation template and the determined annotation value, the first annotation information is added to the first code element in the target API code. This results in the first annotation information of the first code element being embedded in the target API code output by the code generation large language model.
[0099] For example, the first annotation information of the @ApiParam type includes: name: 11; datatype: string. Here, name is the annotation item, 11 is the annotation value corresponding to name, and name: 11 indicates that the parameter name to be passed when calling the method is 11; datatype is the annotation item, and string is the annotation value corresponding to datatype, and datatype: string indicates that the parameter type to be passed when calling the method is string.
[0100] S302. Obtain the natural language descriptor corresponding to the annotation item in the first annotation information, and add the annotation text of the first code element described in natural language to the annotation file of the target API interface according to the natural language descriptor corresponding to the annotation item and the annotation value.
[0101] The first annotation information is: the generator of the target API code is the first code element in the target API code, which provides an explanation of the code format.
[0102] The annotation text in the annotation file is provided to the user of the target API interface, and explains the first code element in the target API interface in natural language. Compared to the first annotation information, the annotation text describes the name, function, and parameters of the first code element in a more user-friendly way.
[0103] As one implementation method, electronic devices can determine the natural language descriptor corresponding to the annotation item based on the preset correspondence between each parameter item and the natural language descriptor, and generate annotation text based on the natural language descriptor corresponding to the annotation item and the annotation value.
[0104] For example, the preset correspondence includes: name corresponds to "method parameter name"; datatype corresponds to "method parameter type". So, if the first annotation information includes the annotation item name, then the natural language descriptor corresponding to the annotation item can be determined to be "method parameter name"; if the first annotation information includes the annotation item datatype, then the natural language descriptor corresponding to the annotation item can be determined to be "method parameter type".
[0105] Then, you can add a preset connector between the natural language descriptor and the annotation value to get the annotation text. For example, if the natural language descriptor corresponding to the annotation item is "method parameter name" and the annotation value corresponding to the annotation item is 11, you can add the preset connector "is" between "method parameter name" and "11" to get the annotation text: "method parameter name is 11".
[0106] After adding annotation text to the annotation file of the target API interface, the electronic device can also check whether there is any missing information in the annotation file. If the missing information is a preset required information, it can automatically fill in the missing information.
[0107] The annotation file can be a Swagger document that conforms to the Open Application Programming Interface (OpenAPI) specification. The Swagger document can be in JSON format, Yet Another Markup Language (YAML) format, or an interactive document rendered by an API documentation generator (Swagger UI).
[0108] This application embodiment can also add first annotation information to the first code element in the target API code, using the first annotation information to explain the first code element. Based on the first annotation information, annotation text describing the first code element in natural language is automatically added to the annotation file of the target API interface, realizing an automated closed loop from requirement text to target API interface and annotation file generation. Compared to manually writing annotation files, this application embodiment improves the efficiency of generating annotation files and reduces manual workload.
[0109] After obtaining the target API code and annotation files, the target API code may be updated. Updates can be made manually or by a large language model that generates the target API code. When the target API code is updated, the annotation information embedded within it is also updated accordingly.
[0110] For example, developers can manually modify the target API code, including adjusting parameter types and return value types. Another example is that when the target API interface requirements change, electronic devices can obtain the new requirement text and regenerate the target API code based on the new requirement text.
[0111] Based on this, electronic devices can also establish a correlation verification mechanism between the target API code and the annotation file, including: when the target API code is detected to be updated, identifying the second code element that has been updated, and then updating the annotation text of the second code element described in natural language in the annotation file based on the updated code of the second code element.
[0112] Electronic devices can detect whether the target API code has been updated in the following two ways:
[0113] Method 1: Based on the Integrated Development Environment (IDE) plugin, listen for modification events of the target API code. When a modification event is detected, determine that the target API code has been updated.
[0114] Method 2: Upon receiving an update event for the target API code from the file system managing the code repository, an update to the target API code is determined. The target API code, after being generated, is stored in the business system's code repository. The version control hook function of the file system managing the code repository can listen for update events of the API code in the code repository. When an update event is detected, a callback function is triggered, thereby sending the update event to the electronic device.
[0115] Upon detecting an update to the target API code, the electronic device can compare the differences between the target API code before and after the update to obtain the second code element in the target API code before the update that has been updated, and the updated code corresponding to the second code element in the target API code after the update. Then, based on the updated code, the annotation text of the second code element, described in natural language, in the annotation file is updated.
[0116] In one implementation, the electronic device can fill a third prompt word template with a second code element, the updated code of the second code element, and an annotation file to obtain third input information. Then, the third input information is used to update the large language model, enabling the updated model to locate the annotation text of the second code element in the annotation file. Based on the updated code of the second code element, the annotation text of the second code element is updated, and the updated annotation file is output.
[0117] The training method for the data-updated large language model includes: filling sample code elements, updated code of the sample code elements, and sample annotation files into a third-party prompt word template as training samples. The sample code elements are existing code elements in the business system that have been updated, and the sample annotation files are the annotation files of the existing code before the update. The manually constructed updated annotation files for the existing code are used as training labels for the training samples. The training samples are input into the data-updated large language model to obtain the updated annotation files output by the model. Based on the training labels of the training samples and the output of the data-updated large language model, a loss value is determined. The model parameters of the data-updated large language model are adjusted using the loss value until the model meets the preset convergence condition or reaches the preset number of iterations, at which point the training of the data-updated large language model is considered complete.
[0118] Because the big language model for data updates has powerful data understanding and generation capabilities, it can be used to more accurately locate and update the annotation text describing the second code element in the annotation file, thereby achieving more accurate automatic updates to the annotation file.
[0119] Through the above method, this application embodiment can establish a dynamic association between the target API code and the annotation file. When the target API code is updated, the annotation file is automatically updated, ensuring the consistency between the target API code and the annotation file, improving the timeliness and accuracy of the annotation file, and ensuring the reference value of the annotation file. Moreover, this application embodiment can automatically update the annotation file, avoiding omissions that may be caused by manual maintenance of the annotation file, and also reducing the cost of manual maintenance.
[0120] In addition, when the updated annotation file is obtained, the electronic device can also generate a change log, which includes: update timestamp, identifier of target API code, second code element, updated code of the second code element, and file identifier of the updated annotation file, so that developers can count and verify the automatic update results of the annotation file.
[0121] In some embodiments of this application, the construction of the above-mentioned contextual knowledge graph includes steps I and II.
[0122] Step 1: Generate a syntax tree based on the existing code.
[0123] Each syntax tree can represent the existing code included in the codebase, or represent a single existing code, or represent multiple classes of existing code, or represent a single class of existing code.
[0124] Existing code is stored in the business system's code repository. This existing code can be written in Java, which is a programming language. Each piece of existing code can include multiple classes, such as: Controller classes, Service classes, data access classes, entity classes, and utility classes.
[0125] The Controller class is used to receive requests sent by the calling client, send requests to the Service class so that the Service class can process the requests, receive the response generated by the Service class after processing the requests, and return the response to the requesting client.
[0126] The Service class is used to implement business logic, such as order creation logic. In the process of implementing business logic, the Service class can use data access classes to manipulate data in the database.
[0127] Entity classes are used to encapsulate data and can serve as data transfer objects (DTOs) between different classes, supporting persistent data storage.
[0128] Controller, Service, and data access classes can all call utility classes to implement specific functionalities. For example, by calling utility classes, functions such as parameter validation, data encryption, and calculating the total order price can be implemented.
[0129] Taking the example of each syntax tree representing a class of existing code, the root node of the syntax tree represents a class of existing code, each child node of the root node represents a method of that class, and the connections between the child nodes represent the calling relationships between the methods. For clarity, the specific construction method of the syntax tree will be described in detail later. The syntax tree in this embodiment can be an abstract syntax tree (AST).
[0130] Step II: Based on the nodes included in the syntax tree, generate nodes in the context knowledge graph. Based on the dependencies between code elements represented by the nodes included in the syntax tree, generate the connection relationships between nodes in the context knowledge graph.
[0131] Taking the example of each syntax tree representing a class of existing code, the root node of the syntax tree represents a class of existing code, and each child node of the root node represents a method of that class. In this case, due to the large amount of existing code and the possibility that different existing code may contain the same class, each syntax tree may contain multiple duplicate root nodes generated based on the same class. A class node in the context knowledge graph can be generated for duplicate root nodes, thereby reducing redundant nodes in the context knowledge graph. Furthermore, for each unique root node included in each syntax tree, a corresponding class node is generated in the context knowledge graph. Finally, based on the call or inheritance relationships between various types of code obtained from parsing, connection relationships between class nodes in the context knowledge graph are generated.
[0132] Similarly, due to the large amount of existing code, and the possibility that different existing code snippets may contain the same method code, each syntax tree may include multiple duplicate child nodes generated based on the same method code. A method node can be generated in the context knowledge graph for these duplicate child nodes, thereby reducing redundant nodes in the context knowledge graph. Furthermore, for each unique child node in each syntax tree, a corresponding method node is generated in the context knowledge graph. Additionally, based on the connection relationships between the root node and child nodes in the syntax tree, connection relationships between class nodes and method nodes in the context knowledge graph are generated; and based on the connection relationships between each child node in the syntax tree, connection relationships between method nodes in the context knowledge graph are generated.
[0133] The connections between the root node and its child nodes in a syntax tree include direct connections and indirect connections. For example, see... Figure 4 In syntax tree 1, root node 1 is directly connected to child node 2, and root node 1 is indirectly connected to child node 4 through child node 2. When an electronic device generates the connection relationship between the class node corresponding to the root node and the method node corresponding to the child node in the context knowledge graph, the connection relationship between the root node and the child node in the syntax tree it relies on includes the above-mentioned direct connection and indirect connection.
[0134] The following example illustrates the process of generating a contextual knowledge graph.
[0135] See Figure 4 In syntax tree 1, root node 1 connects to child nodes 2 and 3, and child node 2 connects to child node 4; in syntax tree 2, root node 5 connects to child nodes 6 and 7, and child node 7 connects to child node 8; in syntax tree 3, root node 9 connects to child nodes 10 and 11, and child node 10 connects to child node 12.
[0136] In this context, root node 1 and root node 9 both represent class code A, child nodes 2, 7, and 10 both represent method code a, child nodes 3 and 11 both represent method code b, and child nodes 4 and 12 both represent method code c; root node 5 represents class code B, child node 6 represents method code e, and child node 8 represents method code d; and class code A calls class code B.
[0137] When constructing a contextual knowledge graph, such as Figure 5 As shown, for the duplicate root node 1 and root node 9, class node A' is generated, and for the non-duplicate root node 5, class node B' is generated. Since class code A calls class code B, a connection relationship is generated between class node A' and class node B'.
[0138] For the duplicate child nodes 2, 7, and 10, generate method node a'; for the duplicate child nodes 3 and 11, generate method node b'; for the duplicate child nodes 4 and 12, generate method node c'; and based on the connection relationships between root node 1 and child nodes 2, 3, and 4, generate the connection relationships between class node A' and method nodes a', b', and c'.
[0139] For the unique child node 6, generate method node e'; for the unique child node 8, generate method node d'; and based on the connection relationships between the root node 5 and child nodes 6, 7, and 8 respectively, generate the connection relationships between class node B' and method nodes a', d', and e'; and based on the connection relationships between child node 7 and child node 8, generate the connection relationship between method code a' and method node d'.
[0140] This application first organizes the structure of each code element in the existing code to generate a syntax tree. Then, it stores the dependencies between the code elements represented by the syntax tree through a graph structure to obtain a context knowledge graph. This context knowledge graph can more comprehensively and clearly reflect the relationships between the code elements in the existing code, so that the code context metadata generated subsequently based on the context knowledge graph can help understand the current state of the business system's code, making the generated target API code more suitable for the business system.
[0141] In this embodiment of the application, the code elements and dependencies between code elements recorded in the code context metadata on which the target API code is generated in S103 can be the code elements and dependencies between code elements in the context knowledge graph.
[0142] Alternatively, the code elements and dependencies between code elements recorded in the code context metadata on which S103 is based may be selected from the context knowledge graph, including some code elements and dependencies between code elements. The selection methods include steps 1 and 2.
[0143] Step 1: Identify the relevant nodes that match the requirement text from the nodes included in the context knowledge graph.
[0144] The context knowledge graph is pre-built based on existing code in the business system and is used to represent each code element in the existing code and the dependencies between code elements.
[0145] A contextual knowledge graph can include class nodes and method nodes. Class nodes represent class code within existing code, while method nodes represent method code included within a class. The connections between nodes represent the dependencies between the code corresponding to those nodes. Each node has attributes that characterize the functionality that the code representing that node can perform.
[0146] For example, an existing codebase includes class code A and class code B. Class code A calls class code B. Class code A includes methods a through c, and method a calls method c. Class code B includes methods a, d, and e, and method a calls method d. A contextual knowledge graph built upon this would look like this: Figure 5 As shown, this includes class node A' representing class code A, class node B' representing class code B, and method nodes a'-e' representing method code a-method code e respectively.
[0147] The connection between class node A' and method nodes a'-c' indicates that class code A includes method codes a-c. The connection between class node B' and method nodes a', d', and e' indicates that class code B includes method codes a, c, and d.
[0148] The connection between class node A' and class node B' indicates that class code A calls class code B; the connection between method node a' and method node c' indicates that method code a calls method code c; and the connection between method node a' and method node d' indicates that method code a calls method code d.
[0149] Since the requirement text can represent the functionality that the target API code needs to implement, and the attributes of nodes in the context knowledge graph represent the functionality that the code element represented by that node can implement, the electronic device can match the functionality described in the requirement text with the functionality of the code element represented by the attributes of the nodes in the context knowledge graph, and designate the nodes whose attributes successfully match the requirement text as associated nodes. The functionality of the code element represented by the associated nodes obtained in this way matches the functionality required by the target API code.
[0150] As one implementation method, an electronic device can identify keywords from the demand text and match the identified keywords with the attributes of nodes in the context knowledge graph, and use the nodes whose attributes match the keywords as associated nodes.
[0151] Keywords can include various types, such as functional keywords, parameter keywords, and interaction keywords. Functional keywords describe the functionality implemented by the API code, such as "order query." Parameter keywords describe the parameters passed when calling the API interface, such as "user ID" and "order status." Interaction keywords describe the type of data interaction between the caller and the callee of the target API interface, such as "GET."
[0152] As one implementation method, if an attribute includes at least one keyword from the requirement text, it is determined that the attribute matches the keyword; if an attribute does not include any of the keywords from the requirement text, it is determined that the attribute does not match the keyword. For example, see... Figure 5 Assuming the keywords in the requirement text include "query" and "user ID", and the attribute of method node a' in the context knowledge graph includes querying order information, then the requirement text matches the attribute of node a', so node a' is designated as an associated node.
[0153] Step 2: Filter out the related nodes in the context knowledge graph and the connection relationships between the related nodes.
[0154] The connections between nodes represent the dependencies between the code elements represented by those nodes. These dependencies reflect the implementation logic of existing code, thus reflecting the architectural style and business logic of the business system. For example, the inclusion relationship between class code and method code reflects the architectural style of the business system; the calling relationship between method code reflects the execution order of the methods, thus reflecting the business logic of the business system.
[0155] The attributes of nodes in a contextual knowledge graph can include: the name of the code element represented by the node, a description of the function implemented by the code element, input parameters, and return values.
[0156] Because the attributes of associated nodes can reflect information such as the names of code elements related to the functionality required by the target API code, code function descriptions, input parameters, and return values, and the connections between associated nodes can reflect how the existing code related to the target API code is written, and what architectural style and business logic it follows, electronic devices can generate target API code based on the attributes of associated nodes and the connections between them.
[0157] In the first implementation, the electronic device can construct the input information of the code generation large language model based on the attributes of the associated nodes and the connection relationship between the associated nodes, and generate the target API code accordingly.
[0158] In the second implementation, the electronic device can input the attributes of associated nodes and the descriptive information of the connections between them into an information summarization model. The model then extracts a summary of the input information and outputs the extracted summary in natural language. Based on this summary, the input information for a large language model can be constructed, and the target API code can be generated. Here, the information summarization model can be a large language model.
[0159] For example, combining Figure 5 Method node a has attributes including its name and the ability to receive requests from clients. Method node c has attributes including its name and the ability to store order information. The connection between method nodes a and c is described as follows: method node a connects to method node c, and the connection direction is from method node a to method node c. The attributes of method node a, the attributes of method node c, and the connection description are input into an information digest model. The model extracts a summary of the input information, and the output summary, described in natural language, can be: "Method a, used to receive requests, calls method c, used to store order information."
[0160] Obtaining summary information through the above summary extraction method can reduce the amount of input data and improve the efficiency of generating target API code.
[0161] When generating summary information based on the attributes of associated nodes, you can use all the information included in the attributes of the associated nodes, or you can use only the necessary information included in the attributes of the associated nodes. For example, you can generate summary information based on the attribute names and code function descriptions included in the attributes of the associated nodes, thereby reducing the amount of input data.
[0162] In a contextual knowledge graph, the attributes of nodes can characterize the functionality of existing code elements, and the connections between nodes can reflect the dependencies between the code elements represented by the nodes, thus reflecting the implementation logic of existing code in the business system. Therefore, by identifying the associated nodes that match the requirement text from the contextual knowledge graph, and based on the attributes of these associated nodes and the connections between them, we can obtain the code elements related to the requirement text in the business system. This reflects the implementation logic of existing code related to the requirement text in the business system, and can help determine the syntax structure, architectural style, and business logic of the business system, making the generated target API code more suitable for the business system.
[0163] The above step I is based on existing code to generate a syntax tree, including steps (1) and (2).
[0164] Step (1): Parse the existing code of the business system to obtain the code elements included in the existing code.
[0165] Electronic devices can traverse and parse the existing code in the business system's codebase to obtain the various code elements contained in the existing code.
[0166] Step (2): Generate a node in the syntax tree for each code element, and generate the connection relationship between nodes in the syntax tree based on the dependency relationship between code elements.
[0167] Taking each syntax tree representing a class of existing code as an example, the electronic device generates a root node of the syntax tree for each parsed class of code, generates a child node of the syntax tree for each method of the class of code, and generates the connection relationship between the child nodes of the syntax tree according to the calling relationship between the method of the class of code.
[0168] For example, suppose class code A includes method code ac, and method code a calls method code c. Then, a root node 1 is generated for class code A, a child node 2 is generated for method code a, a child node 3 is generated for method code b, and a child node 4 is generated for method code c. Since method code a calls method code c, a connection relationship is established between child node 2 and child node 4. See the syntax tree for the constructed syntax tree. Figure 4 The syntax tree 1 in the text.
[0169] The embodiments of this application construct a syntax tree based on existing code. The syntax tree reflects the code elements included in the existing code and the dependencies between the code elements, thereby more clearly representing the code structure of the existing code and facilitating the subsequent more accurate construction of the context knowledge graph.
[0170] In some embodiments of this application, code elements may include annotation information. In this case, the electronic device may also generate the attributes of a node for each node included in the context knowledge graph, based on the annotation information of the code elements represented by that node.
[0171] Since the annotation information of code elements can explain the functionality they perform, the attributes of the corresponding nodes in the context knowledge graph can be automatically generated based on this. This avoids manually setting the attributes of nodes in the context knowledge graph and improves the efficiency of attribute generation.
[0172] See Figure 6 The following examples illustrate the code generation method provided in this application, using real-world application scenarios. The software modules involved in implementing the code generation method in an electronic device may include: a code library context parsing module, a requirements analysis and transformation module, a large model-driven API generation module, and a document synchronization and verification module.
[0173] The code repository context parsing module is used to receive code repositories submitted by developers. These repositories include multiple existing codebases. Based on the code repositories, a context knowledge graph is generated and sent to the requirements analysis and transformation module.
[0174] The requirements analysis and transformation module receives the requirement text submitted by developers, analyzes the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface; and obtains code context metadata based on the context knowledge graph, and sends the functional description information, parameter description information, interaction description information, and code context metadata to the large model-driven API generation module.
[0175] The large model-driven API generation module generates input information based on functional description information, parameter description information, interaction description information, and code context metadata. This input information is then fed into the code to generate a large language model, which outputs the target API code for the target API interface. Finally, the target API code is sent to the document synchronization and verification module.
[0176] The document synchronization and verification module is used to add annotation text to the annotation file of the target API interface based on the first annotation information included in the target API code, and to establish a correlation verification between the target API code and the annotation file. Specifically, it monitors whether the target API code has been updated; when an update is detected, it identifies the second code element that has been updated, and updates the annotation text of the second code element in the annotation file based on the updated code of the second code element. The document synchronization and verification module is also used to provide feedback on the target API code and annotation file to developers.
[0177] See Figure 7 The following provides examples illustrating the process of implementing code generation methods in various software modules of an electronic device. The software modules involved in implementing code generation methods in an electronic device may include: a code repository context resolution module, a requirements analysis and transformation module, a large model-driven API generation module, and a document synchronization and verification module.
[0178] The codebase context resolution module includes an AST traversal and parsing submodule and a context knowledge graph construction submodule. The AST traversal and parsing submodule traverses the existing code in the codebase and generates a syntax tree based on it. The context knowledge graph construction submodule generates a context knowledge graph based on the nodes in the syntax tree and the connections between them.
[0179] The requirements analysis and transformation module includes a natural language segmentation / entity recognition submodule and a requirements-to-API design mapping submodule. The natural language segmentation / entity recognition submodule performs word segmentation and part-of-speech tagging on the requirements text, obtaining the words and their parts of speech. Based on the obtained parts of speech and semantics, it identifies words describing the functionality of the target API interface, words describing the parameters of the target API interface, and words describing the interactions supported by the target API interface. The requirements-to-API design mapping submodule maps the identified words to code elements according to the design elements corresponding to the entities described by the identified words in the API architecture. These code elements serve as functional description information, parameter description information, and interaction description information for the target API interface.
[0180] The large model-driven API generation module includes: a Prompt construction submodule, a large model inference submodule, and a code syntax validation submodule. The Prompt construction submodule generates input information based on functional description information, parameter description information, interaction description information for supported interactions, and code context metadata. The large model inference submodule takes the input information and uses it to generate a large language model, resulting in the target API code for the target API interface output by the large language model. The code syntax validation submodule performs syntax validation on the target API code.
[0181] The document synchronization and validation module includes: a Swagger annotation extraction submodule, a document generation submodule, and a code-document association validation submodule. The Swagger annotation extraction submodule extracts the first annotation information of the first code element from the target API code when syntax validation passes. The document generation submodule adds the annotation text of the first code element to the annotation file of the target API interface based on the first annotation information. The code-document association validation submodule detects whether the target API code has been updated. When an update is detected, it identifies the second code element that has been updated and updates the annotation text of the second code element in the annotation file based on the updated code of the second code element. The code-document association validation submodule also sends the target API code and annotation file to the developer's terminal.
[0182] In traditional development scenarios, developers need to manually write API code and corresponding Swagger documentation to help other members of the development team understand the API's functionality and calling methods, facilitating later maintenance. Furthermore, when API requirements change or versions are iterated, developers must manually update the API code and its corresponding Swagger documentation. When API requirements are complex, such as when the API processing involves interactions between multiple modules, manually writing API code is prone to errors. Moreover, due to the numerous steps involved in manually maintaining API code, it's easy for the Swagger documentation to not be updated promptly after API code updates, leading to inconsistencies between the API code and the Swagger documentation.
[0183] This application provides a large-model-driven automatic API code generation mechanism based on code context awareness. Unlike solutions that generate API code solely based on interface requirements, this application performs deep analysis of existing code in the codebase, generating a syntax tree with a hierarchical code structure. It then constructs a context knowledge graph based on this syntax tree, thereby determining code context metadata. This allows the large language model for code generation to quickly and comprehensively understand the existing logic and architectural style of the code within the business system, making the generated API code more closely aligned with the business system. This improves the compatibility of the generated API code with other code within the business system and accelerates the delivery speed of the business system. Furthermore, this application can automatically generate annotation files based on the annotation information in the API code generated by the large language model. A correlation verification mechanism between the API code and annotation files ensures that when the API code is updated, its corresponding annotation files are updated synchronously, guaranteeing consistency between the API code and the annotation files. This application eliminates the need for manual writing of API code and annotation files, avoiding the errors that are prone to occur during manual writing and the inconsistencies that can arise from manual maintenance.
[0184] The development platform using the code generation method provided in this application can improve the automation and intelligence of API code generation, enhance the product competitiveness of the development platform, attract more users who have high requirements for the development efficiency and accuracy of the development platform, improve the user experience, and ensure user satisfaction with the development platform.
[0185] Based on the same inventive concept, corresponding to the above method embodiments, this application also provides a code generation apparatus, such as... Figure 8 As shown, the device includes: an acquisition module 801, an analysis module 802, and a generation module 803.
[0186] The acquisition module 801 is used to acquire the requirement text describing the target API interface, where the target API interface is the API interface in the business system for which code needs to be written;
[0187] Analysis module 802 is used to analyze the requirement text to obtain the functional description information, parameter description information and interaction description information of the target API interface.
[0188] The generation module 803 is used to determine the implementation logic of the target API interface based on the functional description information, parameter description information and interaction description information, determine the code elements used to write the implementation logic based on the code elements and dependencies between code elements recorded in the code context metadata of the business system, and generate the target API code of the target API interface based on the determined code elements. The code context metadata is generated based on the existing code of the business system.
[0189] In some embodiments of this application, the analysis module 802 is specifically used for:
[0190] The requirement text is segmented and part-of-speech tagging is performed to obtain the words included in the requirement text and their parts of speech;
[0191] Based on the part-of-speech and semantics of the obtained words, identify the words in the demand text that describe the functions of the target API interface, the parameters of the target API interface, and the interactions supported by the target API interface;
[0192] Based on the design elements corresponding to the entities described by the identified words in the API architecture, the identified words are mapped to code elements, which serve as the functional description information, parameter description information, and interaction description information of the target API interface.
[0193] In some embodiments of this application, the device further includes:
[0194] The determination module is used to determine the annotation value of the annotation item in the annotation template corresponding to the type of the first code element after generating the target API code of the target API interface, based on the code of the first code element in the target API code, and add the first annotation information to the first code element in the target API code based on the annotation template and the determined annotation value.
[0195] An add module is used to obtain the natural language descriptors corresponding to the annotation items in the first annotation information, and add the annotation text of the first code element described in natural language to the annotation file of the target API interface based on the natural language descriptors and annotation values.
[0196] In some embodiments of this application, the device further includes:
[0197] The determination module is used to identify the second code element that has been updated when the target API code is detected to have been updated.
[0198] The update module is used to update the annotation text of the second code element described in natural language in the annotation file based on the updated code of the second code element.
[0199] In some embodiments of this application,
[0200] The acquisition module is also used to obtain code writing constraint information;
[0201] Module 803 is specifically used for:
[0202] Based on the determined code elements and in accordance with the code writing constraints, the target API code for the target API interface is generated.
[0203] In some embodiments of this application, the generation module 803 is specifically used for:
[0204] Based on functional description information, parameter description information, interaction description information, and code context metadata of the business system, the input information for the code generation large language model is generated.
[0205] The input information is fed into the code to generate a large language model, which in turn generates the target API code for the target API interface. The code generation large language model determines the implementation logic of the target API interface based on functional description information, parameter description information, and interaction description information. Based on the code elements and dependencies between code elements recorded in the code context metadata, it determines the code elements used to write the implementation logic and generates the target API code based on the determined code elements.
[0206] This application also provides an electronic device, such as... Figure 9 As shown, it includes a processor 901, a communication interface 902, a memory 903, and a communication bus 904, wherein the processor 901, the communication interface 902, and the memory 903 communicate with each other through the communication bus 904.
[0207] Memory 903 is used to store computer programs;
[0208] When the processor 901 executes the program stored in the memory 903, it implements the method steps in the above method embodiments.
[0209] The communication bus mentioned in the above electronic devices can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. This communication bus can be divided into address bus, data bus, control bus, etc. For ease of illustration, only one thick line is used to represent it in the diagram, but this does not indicate that there is only one bus or one type of bus.
[0210] The communication interface is used for communication between the aforementioned electronic devices and other devices.
[0211] The memory may include random access memory (RAM) or non-volatile memory, such as at least one disk storage device. Optionally, the memory may also be at least one storage device located remotely from the aforementioned processor.
[0212] The processors mentioned above can be general-purpose processors, including central processing units (CPUs), network processors (NPs), etc.; they can also be digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, or discrete hardware components.
[0213] In another embodiment provided in this application, a computer-readable storage medium is also provided, wherein a computer program is stored therein, and when the computer program is executed by a processor, it implements any of the code generation methods described in the above embodiments.
[0214] In another embodiment provided in this application, a computer program product containing instructions is also provided, which, when run on a computer, causes the computer to execute any of the code generation methods described in the above embodiments.
[0215] In the above embodiments, implementation can be achieved entirely or partially through software, hardware, firmware, or any combination thereof. When implemented using software, it can be implemented entirely or partially as a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., solid state disk (SSD)).
[0216] It should be noted that, in this document, relational terms such as "first" and "second" are used only to distinguish one entity or operation from another, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Furthermore, the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitations, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0217] The various embodiments in this specification are described in a related manner. Similar or identical parts between embodiments can be referred to mutually. Each embodiment focuses on describing the differences from other embodiments. In particular, the apparatus embodiments are basically similar to the method embodiments, so the description is relatively simple; relevant parts can be referred to the descriptions of the method embodiments.
[0218] The above description is merely a preferred embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application are included within the scope of protection of this application.
Claims
1. A code generation method, characterized in that, The method includes: Obtain the requirement text describing the target API interface, wherein the target API interface is the API interface in the business system for which code needs to be written; The requirement text is analyzed to obtain the functional description information, parameter description information, and interaction description information of the target API interface. Based on the functional description information, parameter description information, and interaction description information, the implementation logic of the target API interface is determined. Based on the code elements and dependencies between code elements recorded in the code context metadata of the business system, the code elements used to write the implementation logic are determined. The target API code of the target API interface is generated based on the determined code elements, wherein the code context metadata is generated based on the existing code of the business system.
2. The method according to claim 1, characterized in that, The analysis of the requirement text yields the functional description information, parameter description information, and supported interaction description information of the target API interface, including: The requirement text is segmented and part-of-speech tagging is performed to obtain the words included in the requirement text and their parts of speech. Based on the part-of-speech and semantics of the obtained words, identify the words in the demand text that describe the functions of the target API interface, the parameters of the target API interface, and the interactions supported by the target API interface; Based on the design elements corresponding to the entities described by the identified words in the API architecture, the identified words are mapped to code elements, which serve as the functional description information, parameter description information, and interactive description information of the target API interface.
3. The method according to claim 1, characterized in that, After generating the target API code for the target API interface, the process also includes: Based on the code of the first code element in the target API code, determine the annotation value of the annotation item in the annotation template corresponding to the type of the first code element, and add first annotation information to the first code element in the target API code based on the annotation template and the determined annotation value; Obtain the natural language descriptor corresponding to the annotation item in the first annotation information, and add the annotation text of the first code element described in natural language to the annotation file of the target API interface according to the natural language descriptor corresponding to the annotation item and the annotation value.
4. The method according to claim 3, characterized in that, The method further includes: If the target API code is detected to have been updated, identify the second code element that has been updated. Based on the updated code of the second code element, update the annotation text of the second code element described in natural language in the annotation file.
5. The method according to any one of claims 1-4, characterized in that, The method further includes: Obtain code writing constraint information; The process of generating the target API code for the target API interface based on the determined code elements includes: Based on the determined code elements, and in accordance with the code writing constraint information, the target API code for the target API interface is generated.
6. The method according to any one of claims 1-4, characterized in that, The process of determining the implementation logic of the target API interface based on the functional description information, parameter description information, and interaction description information; determining the code elements used to write the implementation logic based on the code elements and dependencies between code elements recorded in the code context metadata of the business system; and generating the target API code of the target API interface based on the determined code elements includes: Based on the functional description information, parameter description information, interaction description information, and the code context metadata of the business system, input information for the code generation large language model is generated; The input information is input into the code generation language model to obtain the target API code of the target API interface output by the code generation language model; wherein, the code generation language model determines the implementation logic of the target API interface based on the functional description information, parameter description information and interaction description information, determines the code elements used to write the implementation logic based on the code elements and dependencies between code elements recorded in the code context metadata, and generates the target API code based on the determined code elements.
7. A code generation device, characterized in that, The device includes: The acquisition module is used to acquire the requirement text describing the target API interface, wherein the target API interface is the API interface in the business system for which code needs to be written; The analysis module is used to analyze the requirement text to obtain the functional description information, parameter description information, and interaction description information of the target API interface. The generation module is used to determine the implementation logic of the target API interface based on the functional description information, parameter description information, and interaction description information; determine the code elements for writing the implementation logic based on the code elements and dependencies between code elements recorded in the code context metadata of the business system; and generate the target API code of the target API interface based on the determined code elements, wherein the code context metadata is generated based on the existing code of the business system.
8. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the method described in any one of claims 1-6.
9. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores a computer program that, when executed by a processor, implements the method described in any one of claims 1-6.
10. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method according to any one of claims 1-6.