Search

CN-122018911-A - Code generation method, electronic device and storage medium

CN122018911ACN 122018911 ACN122018911 ACN 122018911ACN-122018911-A

Abstract

The embodiment of the application provides a code generation method, electronic equipment and a storage medium, wherein the method comprises the steps of responding to a code generation request, determining a matched target code template according to request information in the code generation request, and creating the target code template based on a preset code specification; generating a prompt text based on the target code template, and generating a target code corresponding to the code generation request based on the prompt text. The method can improve the generation quality and the generation efficiency of the target codes.

Inventors

  • WANG WEI

Assignees

  • 南京荣耀软件技术有限公司

Dates

Publication Date
20260512
Application Date
20251218

Claims (14)

  1. 1. A code generation method, the method comprising: responding to a code generation request, and determining a matched target code template according to request information in the code generation request, wherein the target code template is created based on a preset code specification; Generating a prompt text based on the object code template; and generating an object code corresponding to the code generation request based on the prompt text.
  2. 2. The method according to claim 1, wherein the method further comprises: Acquiring a code example corresponding to the preset code specification; Extracting entity relationships among a plurality of entities in the code example; And constructing a code modeling map according to the entities and the entity relationship.
  3. 3. The method of claim 2, wherein constructing a code modeling map from the plurality of entities and the entity relationship comprises: and constructing the code modeling spectrum by taking the plurality of entities as spectrum nodes and the entity relationship as spectrum edges, wherein the nodes in the code modeling spectrum correspond to a plurality of preset code templates.
  4. 4. The method of claim 2, wherein constructing a code modeling map from the plurality of entities and the entity relationship further comprises: Mapping each entity into a unified modeling language UML class, and mapping the entity attribute of each entity into an object attribute of a corresponding UML class; Mapping the entity relationship into a relationship among a plurality of UML classes; and constructing the code modeling map based on the plurality of UML classes, the object attribute and the relation among the plurality of UML classes, wherein the UML classes in the code modeling map correspond to a plurality of preset code templates.
  5. 5. The method according to any one of claims 2 to 4, further comprising: replacing dynamic codes in the code examples by using preset placeholders to obtain a plurality of preset code templates; And establishing a mapping relation between the template identifiers in the code examples and the corresponding preset code templates.
  6. 6. The method of claim 1, wherein said determining a matching target code template from request information in the code generation request comprises: Generating a file identifier corresponding to the code generation request according to the request information; and selecting the target code template from the plurality of preset code templates based on the matching degree of the file identification and the template identifications of the plurality of preset code templates.
  7. 7. The method of claim 6, wherein the determining the degree of matching comprises one or more of the following: Determining the matching degree based on the similarity between the first vector corresponding to the file identifier and the second vector corresponding to the template identifier; The method comprises the steps of identifying a file identifier, counting a first number of target characters in the file identifier, wherein the target characters represent the same characters of the file identifier and the template identifier in corresponding positions, determining a larger value of the number of characters in the file identifier and the number of characters in the template identifier as a second number, and determining the matching degree based on the first number and the second number.
  8. 8. The method of claim 6 or 7, wherein selecting the target code template from a plurality of preset code templates based on a degree of matching of the file identification with template identifications of the plurality of preset code templates, comprises: If the highest matching degree is greater than or equal to a first preset threshold value, determining a preset code template corresponding to the highest matching degree as the target code template; And if the highest matching degree is smaller than the first preset threshold value, determining a preset code template corresponding to the matching degree larger than a second preset threshold value as the target code template, wherein the first preset threshold value is larger than the second preset threshold value.
  9. 9. The method of claim 1, wherein generating hint text based on the object code template comprises: Acquiring a prompt word template; And filling the target code template into the prompt word template to obtain the prompt text.
  10. 10. The method of claim 9, wherein the hint word template includes one or more of the following information: indicating the language model as identity information of the code generation expert; Task information indicating that the language model generates code based on the object code template; And a preset quality requirement for limiting the codes generated by the language model.
  11. 11. The method of claim 1, wherein said determining a matching target code template from request information in said code generation request further comprises: Selecting an object code specification from a plurality of preset code specifications based on the request information; and determining the target code template based on the code example corresponding to the target code specification.
  12. 12. The method of claim 11, wherein selecting an object code specification from a plurality of preset code specifications based on the request information comprises: Identifying a user intent of the request information; the target code specification is selected from the plurality of preset code specifications based on a similarity of the user intent to the plurality of preset code specifications.
  13. 13. An electronic device, comprising: A memory storing computer readable instructions; a processor which, when executing the computer readable instructions, implements the code generation method of any of claims 1 to 12.
  14. 14. A computer readable storage medium, characterized in that the computer readable storage medium has stored therein computer readable instructions, which when executed by a processor in an electronic device, implement the code generation method according to any of claims 1 to 12.

Description

Code generation method, electronic device and storage medium Technical Field The present application relates to the field of artificial intelligence technologies, and in particular, to a code generation method, an electronic device, and a storage medium. Background With the development of artificial intelligence, in order to improve development efficiency, a large language model may be used to assist a developer in writing a code program. However, in the actual application process, the code generated by the universal large language model is difficult to adapt to the coding specification in the enterprise, and the enterprise needs to invest a lot of time and manpower to audit and correct the generated code. The method not only reduces the overall efficiency of code generation, but also is easily influenced by experience of developers, so that the generation quality of the code cannot be ensured. Disclosure of Invention The embodiment of the application discloses a code generation method, electronic equipment and a storage medium, which can solve the technical problems that the code generation efficiency is low and the code generation quality cannot be ensured. The first aspect of the application discloses a code generation method, which comprises the steps of responding to a code generation request, determining a matched target code template according to request information in the code generation request, creating the target code template based on a preset code specification, generating a prompt text based on the target code template, and generating a target code corresponding to the code generation request based on the prompt text. According to the technical scheme, based on the request information in the code generation request, the target code template conforming to the preset code specification can be adapted, and the prompt text is generated by using the target code template, so that the language model can accurately understand the preset code specification, and the generation quality and the generation efficiency of the target code can be improved. In a possible implementation manner, the method further comprises the steps of obtaining a code example corresponding to the preset code specification, extracting a plurality of entities in the code example and entity relations among the entities, and constructing a code modeling map according to the entities and the entity relations. Through the technical scheme, the code modeling map is constructed by utilizing the code examples corresponding to the preset code specifications, so that a plurality of preset code templates corresponding to the code modeling map can be ensured to follow the preset code specifications. In one possible implementation manner, the constructing a code modeling map according to the entities and the entity relationships includes constructing the code modeling map by using the entities as map nodes and the entity relationships as map edges, wherein the nodes in the code modeling map correspond to a plurality of preset code templates. By the technical scheme, the plurality of entities are used as map nodes, and the entity relations are used as map edges, so that the entity relations in the code examples can be displayed in a displaying mode. In one possible implementation manner, the code modeling map is constructed according to the entities and the entity relationships, and further comprises mapping each entity into a Unified Modeling Language (UML) class, mapping entity attributes of each entity into object attributes of corresponding UML classes, mapping the entity relationships into relationships among the UML classes, and constructing the code modeling map based on the UML classes, the object attributes and the relationships among the UML classes, wherein the UML classes in the code modeling map correspond to a plurality of preset code templates. Through the technical scheme, the abstract code example can be converted into a structured and standardized code modeling map based on the relationships among the plurality of UML classes obtained by mapping the plurality of entities, the object attributes of the UML classes obtained by mapping the entity attributes of each entity and the plurality of UML classes obtained by mapping the entity relationship. In one possible implementation, the method further comprises replacing dynamic codes in the plurality of code examples with preset placeholders to obtain a plurality of preset code templates, and establishing a mapping relation between template identifications in the plurality of code examples and corresponding preset code templates. According to the technical scheme, the dynamic codes in the code examples are replaced by the preset placeholders, so that a plurality of universal preset code templates can be obtained, and the target code templates can be determined by establishing the mapping relation between the template identifiers and the corresponding preset code templates. In one