Code generation method and device, storage medium and electronic device

By receiving natural language information and determining structured query information, querying the target code snippet from the code database, and combining it with a large language model to generate code, the problem of inaccurate code generation by the large language model is solved, and the accuracy and practicality of code retrieval are improved.

CN120447880BActive Publication Date: 2025-09-23JINAN INSPUR DATA TECH CO LTD
View PDF 4 Cites 0 Cited by

Patent Information

Application Number
CN202510964667.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2025-07-14
Publication Date
2025-09-23
Estimated Expiration
2045-07-14

AI Technical Summary

Technical Problem

The problem of inaccurate code generation based on natural language by large language models limits the accuracy and practicality of code retrieval.

Method used

By receiving natural language information, using preset templates to determine structured query information, and querying target code snippets from the code database, it generates code in combination with a large language model.

Benefits of technology

The accuracy of large language models in generating codes based on natural language has been improved, and the accuracy and practicality of code retrieval have been enhanced.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN120447880B_ABST
    Figure CN120447880B_ABST
Patent Text Reader

Abstract

This application discloses a code generation method and device, a storage medium, and an electronic device, relating to the field of artificial intelligence application technology. The method comprises: receiving natural language information input by a target object, wherein the natural language information represents the code generation requirements of the target object described in natural language; determining structured query information based on the natural language information and a preset template; querying a code database based on the natural language information and the structured query information to obtain a target code snippet; and inputting the target code snippet and the natural language information into a large language model to obtain code generated by the large language model. This method solves the technical problem of inaccurate code generation based on natural language by the large language model, achieving the technical effect of improving the accuracy of code generation based on natural language by the large language model.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of artificial intelligence application technology, and in particular to a code generation method and device, a storage medium, and an electronic device. Background Art

[0002] With the rapid development of artificial intelligence (AI), especially the application of large language models (LLMs), code generation has become a research hotspot. Large language models learn programming patterns from massive amounts of code data and can automatically generate executable code that meets functional requirements based on natural language descriptions.

[0003] However, existing technologies still have significant limitations in practical applications. Specifically, source code is typically highly structured, grammatically standardized, and semantically precise, while natural language is characterized by flexibility, polysemy, and strong contextual dependence. These fundamental differences in expression, information organization, and semantic hierarchy make it particularly difficult to accurately understand user intent and match relevant code snippets in retrieval tasks using natural language as a query. This semantic misalignment not only limits the retrieval system's ability to understand user queries but also directly impacts the accuracy and practicality of code retrieval.

[0004] This shows that there is a problem in the related art where large language models generate inaccurate codes based on natural language. Summary of the Invention

[0005] The present application provides a code generation method and device, a storage medium, and an electronic device to at least solve the problem in the related art that large language models generate inaccurate codes based on natural language.

[0006] The present application provides a code generation method, comprising: receiving natural language information input by a target object, wherein the natural language information represents the code generation requirements of the target object described in natural language; determining structured query information based on the natural language information and a preset template; querying a code database based on the natural language information and the structured query information to obtain a target code snippet; and inputting the target code snippet and the natural language information into a large language model to obtain code generated by the large language model.

[0007] The present application also provides a code generation device, including: a receiving module for receiving natural language information input by a target object, wherein the natural language information represents the code generation requirements of the target object described in natural language; a determination module for determining structured query information based on the natural language information and a preset template; a query module for querying a code database based on the natural language information and the structured query information to obtain a target code snippet; and a generation module for inputting the target code snippet and the natural language information into a large language model to obtain code generated by the large language model.

[0008] The present application also provides an electronic device, comprising: a memory for storing a computer program; and a processor for implementing the steps of any of the above-mentioned code generation methods when executing the computer program.

[0009] The present application also provides a computer-readable storage medium, in which a computer program is stored. When the computer program is executed by a processor, the steps of any of the above-mentioned code generation methods are implemented.

[0010] The present application also provides a computer program product, including a computer program, which implements the steps of any of the above-mentioned code generation methods when executed by a processor.

[0011] Through this application, structured query information can be generated based on the natural language input by the target object, and the target code can be searched from the code library based on the natural language and the structured query information. Then, the natural language and the target code are input into the large language model together to obtain the code generated by the large language model. Therefore, the technical problem of inaccurate code generation based on natural language by the large language model can be solved, and the technical effect of improving the accuracy of code generation based on natural language by the large language model is achieved. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] In order to more clearly illustrate the embodiments of the present application, the following is a brief introduction to the drawings required for use in the embodiments. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.

[0013] Figure 1 This is a schematic diagram of an application scenario of a code generation method according to an embodiment of the present application;

[0014] Figure 2 is a flowchart of an optional code generation method according to an embodiment of the present application;

[0015] Figure 3 is a schematic diagram of an optional code generation method according to an embodiment of the present application;

[0016] Figure 4 This is a structural block diagram of an optional code generation device according to an embodiment of the present application. DETAILED DESCRIPTION

[0017] The following will be combined with the accompanying drawings in the embodiments of this application to clearly and completely describe the technical solutions in the embodiments of this application. Obviously, the embodiments described are only part of the embodiments of this application, not all of them. Based on the embodiments in this application, all other embodiments obtained by ordinary technicians in this field without making creative efforts are within the scope of protection of this application.

[0018] It should be noted that, in the description of this application, the terms "comprises," "includes," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or device comprising a series of elements includes not only those elements, but also other elements not explicitly listed, or elements inherent to such process, method, article, or device. The terms "first," "second," etc., in this application are used to distinguish similar objects, and are not used to describe a particular order or sequence.

[0019] In order to enable those skilled in the art to better understand the present application, the present application is further described in detail below with reference to the accompanying drawings and specific implementation methods.

[0020] According to one aspect of the embodiment of the present application, a code generation method is provided. Optionally, in this embodiment, the above code generation method can be applied to, but is not limited to, Figure 1 The hardware environment shown includes a terminal device 102 and a server 104. The server 104 can be connected to the terminal device 102 via a network and can be used to provide services (e.g., application services, etc.) for the terminal device 102 or a client installed on the terminal device 102. A database can be set on the server 104 or independently of the server 104 to provide data storage services for the server 104.

[0021] The aforementioned network may include, but is not limited to, at least one of the following: a wired network and a wireless network. The aforementioned wired network may include, but is not limited to, at least one of the following: a wide area network, a metropolitan area network, or a local area network. The aforementioned wireless network may include, but is not limited to, at least one of the following: Wi-Fi (Wireless Fidelity) and Bluetooth. The terminal device 102 may be, but is not limited to, a personal computer (PC), a mobile phone, a tablet computer, etc. The server 104 may be, but is not limited to, a cloud server, a server cluster, or other server types.

[0022] The code generation method of the embodiment of the present application can be executed by the server 104, or by the terminal device 102, or jointly by the server 104 and the terminal device 102. The code generation method of the embodiment of the present application can also be executed by the client installed on the terminal device 102.

[0023] Taking the execution of the code generation method in this embodiment by the terminal device 102 as an example, here, the terminal device 102 can be a physical host, and the code generation method in this embodiment is applied to the physical host. The memory that can be called by the physical host is divided into multiple memory levels, and one memory level of the multiple memory levels contains at least one type of memory, and the multiple memory levels include a first memory level corresponding to the physical memory of the physical host. Here, the physical host can be enterprise-level servers, cluster servers, office computers, embedded devices, and other physical devices that can serve as the underlying hardware support in a virtualized environment. Physical memory refers to the physical memory module directly and closely connected to the host hardware. It is the core and foundation of the memory architecture. Typically composed of dynamic random access memory (DRAM), physical memory boasts extremely fast read and write speeds, responding to processor memory access requests with nanosecond response times. This makes physical memory suitable for hosting the core code of the virtual machine's operating system, frequently called system function libraries, and data from critical, high-speed processes. For example, during the initial startup of a virtual machine, the operating system kernel needs to quickly load and initialize various hardware drivers and establish a basic system operating environment. During this time, physical memory can efficiently complete data read and write operations, ensuring the rapid and stable startup of the virtual machine. During virtual machine operation, applications with extremely demanding memory read and write performance, such as the transaction processing module of a database management system and the real-time rendering engine, also rely on physical memory to ensure efficient operation, thereby maintaining the smoothness and responsiveness of the entire virtual machine system.

[0024] Figure 2 is a flow chart of an optional code generation method according to an embodiment of the present application, such as Figure 2 As shown, the process of the method may include the following steps:

[0025] Step S202: receiving natural language information input by a target object, wherein the natural language information represents a code generation requirement of the target object described in natural language;

[0026] Optionally, in the above step S202, for example, the natural language input by the user is "help me write a reading function for a large file".

[0027] Step S204: determining structured query information based on the natural language information and a preset template;

[0028] Optionally, in the above step S204, for example, the structured query information determined based on the natural language input by the above user and the preset template is: {operation object: file, operation action: create file reading function, constraint condition: file size is greater than 1G}.

[0029] Step S206: querying a code database according to the natural language information and the structured query information to obtain a target code snippet;

[0030] Optionally, in the above step S208, the target code segment may be multiple code segments.

[0031] Step S208: input the target code snippet and the natural language information into a large language model to obtain a code generated by the large language model.

[0032] Through the embodiments provided in the present application, structured query information can be generated based on the natural language input by the target object, and the target code can be searched from the code library based on the natural language and the structured query information, and then the natural language and the target code are input into the large language model together to obtain the code generated by the large language model. Therefore, the technical problem of inaccurate code generation based on natural language by the large language model can be solved, and the technical effect of improving the accuracy of code generation based on natural language by the large language model is achieved.

[0033] In an exemplary embodiment, determining structured query information based on the natural language information and a preset template includes: performing semantic parsing on the natural language information based on the preset template to obtain a semantic parsing result, wherein the semantic parsing result includes the filled content of the preset template; and filling the preset template based on the semantic parsing result to obtain the structured query information.

[0034] In an exemplary embodiment, semantic parsing of the natural language information is performed according to the preset template to obtain a semantic parsing result, including: determining a template structure of the preset template, wherein the items to be filled in the template structure include an operation object, an operation action, and a constraint condition; determining a semantic parsing prompt word according to the template structure, wherein the semantic parsing prompt word is used to instruct the large language model to determine the items to be filled in from the natural language information; and inputting the natural language information and the semantic parsing prompt word into the large language model to obtain a semantic parsing result output by the large language model.

[0035] Optionally, in the above embodiment, for example, the above preset template includes goals, actions, and conditions, then the semantic analysis of natural language information includes guiding the large language model to perform semantic understanding of the natural language information input by the user based on prompt word engineering technology, and automatically extracting the core elements contained in the natural language, such as target entities (such as functions, methods, modules, etc.), operation behaviors (such as calling, modifying, reading, etc.) and additional restrictions (such as file paths, timestamps, developer identifiers, etc.), to obtain semantic analysis results.

[0036] In an exemplary embodiment, the preset template is filled according to the semantic parsing result to obtain the structured query information, including: determining the correspondence between the filled content in the semantic parsing result and the template structure; and writing the filled content into the preset template according to the correspondence to obtain the structured query information.

[0037] Optionally, in the above embodiment, after obtaining the semantic parsing result of the natural language, a unified template mapping rule can be formulated to query the target, action, and condition in the semantic parsing result and embed them into the corresponding template to obtain structured query information.

[0038] In an exemplary embodiment, before querying a code database based on the natural language information and the structured query information to obtain a target code snippet, the method further includes: dividing the source code into code snippets based on the structural information of the source code and the semantic information of the source code to obtain multiple code snippets, wherein the code snippets include code and comments; performing annotation enhancement on each of the multiple code snippets to obtain multiple enhanced comments; determining a dual-channel vector based on the code in each code snippet and the enhanced comments corresponding to each code snippet, wherein the dual-channel vector includes code semantic information and comment semantic information; saving the code in each code snippet, the enhanced comments corresponding to each code snippet, and the dual-channel vector to the code database, wherein there is a correspondence between the dual-channel vector and the code in each code snippet and the enhanced comments corresponding to each code snippet.

[0039] Optionally, in the above embodiment, the step of enhancing the comments of the source code makes up for the problem of insufficient code comment information, and models the code snippets of the source code and their corresponding enhanced comments as a dual-channel vector of "code + natural language" for representation, providing richer and more controllable semantic alignment features for subsequent semantic retrieval. It does not provide a stable and clear alignment anchor point for the semantic mapping between natural language and code, but is a key intermediate layer for achieving bidirectional semantic connection.

[0040] It should be noted that, in the above code database, the code in each code snippet and the enhanced annotation corresponding to each code snippet constitute a code snippet, and the code snippets subsequently queried from the code database are all code snippets with enhanced annotations.

[0041] In an exemplary embodiment, dividing the source code into code fragments based on the structural information of the source code and the semantic information of the source code to obtain multiple code fragments includes: extracting an abstract syntax tree of the source code through a code analysis tool, and determining the structural information of the source code according to the structure of the abstract syntax tree; dividing the source code into multiple code blocks based on the structural information; clustering the multiple code blocks based on the semantic information of the source code, determining code blocks of the same type as one code fragment, and obtaining the multiple code fragments, wherein the semantic information of the source code includes code semantic information of the multiple code blocks.

[0042] Optionally, in the above embodiment, the abstract syntax tree (AST) of the source code can be extracted by using a code parsing tool. The AST is a tree representation of the grammatical structure of the source code, which expresses the grammatical structure of the programming language in the form of a tree. Each node on the tree represents a structure in the source code. By identifying key structural nodes (such as function definitions, control structures, exception handling statements, etc.) and their scope in the source code, the initial code block can be obtained.

[0043] The CodeBERT pre-trained model (a bimodal pre-trained model for programming languages ​​and natural languages) is used to semantically embed initial code blocks (line-level or statement-level code). This embedding combines positional information about grammatical structures (such as nesting levels and scope depth) to form a structure-aware hybrid semantic representation. Semantic density clustering is used to partition the semantic representation of code blocks, clustering similar code snippets. This yields clearly structured, appropriately granular code snippets, which serve as the basis for subsequent annotation generation and structured pattern retrieval, improving code retrieval accuracy.

[0044] Optionally, in the above embodiment, the code block division method based on structure perception and semantic aggregation can automatically identify semantically concentrated and structurally complete fine-grained code fragments, overcome the information redundancy problem brought about by traditional coarse-grained division, and improve the quality of code vector representation and the accuracy of subsequent retrieval.

[0045] In an exemplary embodiment, annotation enhancement is performed on each of the multiple code snippets to obtain multiple enhanced annotations, including: performing annotation detection on the multiple code snippets, determining a first code snippet, a second code snippet, and a third code snippet from the multiple code snippets, wherein the first code snippet represents a code snippet without any annotations, the second code snippet represents a code snippet with an incomplete annotation grammatical structure, and the third code snippet represents a code snippet with a complete annotation grammatical structure; generating a first annotation for the first code snippet, and generating a second annotation for the second code snippet; determining the first annotation, the second annotation, and the third annotation as the multiple enhanced annotations, wherein the third annotation represents an annotation of the third code snippet.

[0046] Optionally, in the above embodiment, different annotation enhancement operations can be performed based on the specific circumstances of the code snippets. Code generation can be performed for code snippets that lack annotations, and code completion can be performed for low-quality annotations (natural language annotations lacking subject, predicate, object, and other structures). For high-quality annotations, no enhancement is required, and the original annotations are retained. After annotation enhancement, consistency checks can be performed on the terms and code identifiers in the annotation text, duplicate information can be removed, and language fluency can be checked to ensure the reliability and accuracy of the annotations.

[0047] Optionally, in the above embodiment, through the comment generation mechanism of context enhancement and syntactic structure completion, high-quality, semantically rich natural language comments can be generated for fine-grained code blocks, which makes up for the problem of missing or low-quality code comments, establishes an effective semantic bridge from code to natural language, and enhances the natural language interpretability of the code.

[0048] In an exemplary embodiment, generating a first annotation for the first code snippet includes: determining similar code snippets of each first code snippet from a high-quality code sample library, wherein the similar code snippets include code and annotations, and the similarity between the code semantic information of each first code snippet and the code semantic information of the similar code snippet is greater than a first preset similarity; and determining the annotation of the similar code snippet as the first annotation.

[0049] Alternatively, in the above embodiment, code snippets and inline annotation data pairs at the internal level of the function can be extracted from the open source code, and high-quality code can be screened out based on heuristic rules as a high-quality code sample library. Based on similarity retrieval of code semantic information, approximate annotation migration is implemented to generate annotations for unannotated code snippets.

[0050] In an exemplary embodiment, generating a first comment for the first code snippet further includes: inputting each first code snippet into a comment generation model to obtain a comment output by the comment generation model, wherein the comment generation model is trained based on high-quality code samples; and determining the comment output by the comment generation model as the first comment.

[0051] In an exemplary embodiment, the annotation generation model is obtained by training in the following manner: dividing the high-quality code sample into code fragments based on the structural information of the high-quality code sample and the semantic information of the high-quality code sample to obtain multiple high-quality code fragments; using the codes in the multiple high-quality code fragments as input samples and the annotations in the multiple high-quality code fragments as output samples to train the initial model to obtain the annotation generation model.

[0052] Optionally, in the above embodiment, annotations can also be generated for unannotated code snippets through an annotation generation model. The training process of the annotation generation model is as follows: first, high-quality code samples are divided into code snippets, and the division method is the same as the method of dividing the above-mentioned source code into code snippets. Then, the code in the code snippet is used as input data, and the annotation corresponding to the code is used as output data to train the neural network model to obtain the annotation generation model.

[0053] In an exemplary embodiment, generating a second annotation for the second code snippet includes: inputting each of the second code snippets into an annotation completion model to obtain an annotation output by the annotation completion model, wherein the annotation completion model is trained based on annotations of high-quality code samples; and determining the annotation output by the annotation completion model as the second annotation.

[0054] In an exemplary embodiment, the comment completion model is trained by: splitting the comments of the high-quality code sample to obtain multiple split comments; training an initial model with at least one of the split comments as an input sample and the comments of the high-quality code sample as an output sample to obtain the comment completion model.

[0055] Optionally, in the above embodiment, for code snippets with poor annotation quality (incomplete syntactic structure, lack of natural language annotations with subject, predicate, object and other structures), the annotations can be completed by the annotation completion model. The training process of the annotation completion model is as follows: first, the high-quality code samples are divided into code snippets, the annotation information in the code snippets is extracted, and then the annotation information is split into multiple annotation snippets with incomplete syntactic structure. For example, a comment is split into three annotation snippets, and then one or two of the annotation snippets are used as input data, and the complete annotation is used as output data to train the neural network model to obtain the annotation completion model.

[0056] In an exemplary embodiment, a dual-channel vector is determined based on the code in each code snippet and the enhanced annotation corresponding to each code snippet, including: vectorizing the code in each code snippet to obtain a first vector; vectorizing the high-quality annotation corresponding to each code snippet to obtain a second vector; and concatenating the first vector and the second vector to obtain the dual-channel vector.

[0057] In an exemplary embodiment, after saving the code in each code snippet, the enhanced annotation corresponding to each code snippet, and the dual-channel vector into the code database, the method further includes: constructing template information for each code snippet based on the enhanced annotation corresponding to each code snippet, wherein the template information for each code snippet includes the operation object, operation action, and constraint conditions corresponding to each code snippet.

[0058] In an exemplary embodiment, a code database is queried based on the natural language information and the structured query information to obtain a target code snippet; the method includes: vectorizing the natural language information to obtain a third vector; traversing the code database to determine a target dual-channel vector from the code database, wherein the similarity between the target dual-channel vector and the third vector is greater than a second preset similarity; determining a fourth code snippet based on the code and enhanced annotation corresponding to the target dual-channel vector; matching the structured query information with template information in the code database to determine a fifth code snippet from the code database; and determining the target code snippet based on the fourth and fifth code snippets.

[0059] Optionally, in the above embodiment, for natural query statements that do not have clear structural semantics, a pure vector retrieval method can be used for fragment recall. For natural query statements that have completed the construction of a structured paradigm, their vector expression and structured paradigm can be integrated, and the paradigm query and semantic similarity retrieval can be performed separately through a dual-channel retrieval mechanism to generate two candidate sets to achieve dual recall capabilities.

[0060] In an exemplary embodiment, the target code snippet and the natural language information are input into a large language model to obtain code generated by the large language model, including: sorting the target code snippet in descending order based on the semantic similarity between the enhanced annotation corresponding to the target code snippet and the natural language information to obtain sorted target code snippets; and inputting the sorted target code snippet and the natural language information into a large language model to obtain code generated by the large language model.

[0061] Optionally, in the above embodiment, vector search and structured pattern search can be combined. For each candidate code snippet obtained by either search method, the semantic similarity (cosine similarity) or structural consistency score (pattern hit rate) between the snippet and the natural language query can be calculated. Based on this, a multi-dimensional ranking feature vector is constructed. Based on this, a comprehensive scoring strategy is used to fusedly rank all candidate snippets, ultimately selecting the top-ranked snippets as the input context for the large language model.

[0062] Optionally, in the above embodiment, by constructing a structured query paradigm for natural language, fuzzy and ambiguous natural language queries are converted into structured expressions, and combined with vector retrieval to achieve mixed matching, the system's understanding of complex query intent and the accuracy and recall of code retrieval are significantly improved.

[0063] In an optional embodiment, combined with Figure 3 The above code generation method is explained as follows: Figure 3 As shown in the figure, after the source code is input, it enters the context-enhanced annotation generation process through the structured code block partitioning module, and then the annotations and code are jointly vectorized, and the vectorization results are stored in the knowledge base. When natural language queries are input, embedded vectorization is performed simultaneously, and paradigm construction is performed based on semantic intent recognition. When querying code snippets from the knowledge base, a mixed matching retrieval of vectors and paradigms is used. The retrieval results are input into the large language model after passing through the re-ranking model, and then the large language model is used to generate accurate code that meets user needs.

[0064] In an optional embodiment, the embodiment of the present application further provides a code generation system, specifically comprising:

[0065] The code block division module is used to divide the source code into code blocks based on structure perception and semantic aggregation to obtain fine-grained code snippets;

[0066] An annotation generation module is used to generate annotations for the fine-grained code snippets based on context enhancement and syntactic structure completion to obtain high-quality natural language annotations for the code snippets;

[0067] A knowledge base construction and storage module is used to vectorize the code snippets and their corresponding natural language annotations and store them in a knowledge base;

[0068] Input interface, used to receive natural language queries input by users;

[0069] A structured paradigm construction module, configured to construct a natural language-oriented structured paradigm for the natural language query to obtain a structured query expression;

[0070] a hybrid retrieval module, configured to combine the semantic vector representation of the natural language query and / or the structured query expression to perform a hybrid retrieval from the knowledge base and re-rank the candidate code snippets;

[0071] A code generation module, configured to use the reordered candidate code snippets as context information, combine the natural language query, and generate target code using a large language model;

[0072] Output interface, used to output the generated code.

[0073] Optionally, to evaluate the accuracy and practicality of the generated code, mainstream generation task evaluation metrics from the field of natural language processing, such as fluency, relevance, and accuracy, can be used to quantitatively assess the linguistic quality of the annotations. This can be combined with human expert review to ensure the rationality of the evaluation results. For retrieval enhancement tasks, multiple evaluation metrics can be used based on open-source benchmark datasets to reflect the model's ranking accuracy, semantic matching ability, and recall coverage in actual development queries.

[0074] Through the above embodiments, the semantic gap problems in both the code-to-natural language and natural language-to-code directions are systematically solved. By providing more accurate and relevant contextual information, the output of the large language model is effectively constrained, the generation of erroneous codes is reduced, and the reliability of the generated code is improved.

[0075] It should be noted that for the aforementioned method embodiments, for the sake of simplicity, they are all expressed as a series of action combinations, but those skilled in the art should be aware that this application is not limited by the order of the actions described, because according to this application, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions and modules involved are not necessarily required by this application.

[0076] Through the description of the above implementation methods, those skilled in the art can clearly understand that the method according to the above embodiment can be implemented by means of software plus the necessary general hardware platform, and of course it can also be implemented by hardware, but in many cases the former is a better implementation method. Based on this understanding, the technical solution of the present application is essentially or the part that contributes to the existing technology can be embodied in the form of a software product, which is stored in a storage medium (for example, read-only memory (ROM) / random access memory (RAM), a magnetic disk, or an optical disk), and includes a number of instructions for enabling a terminal device (which can be a mobile phone, a computer, a server, or a network device, etc.) to execute the methods of each embodiment of the present application.

[0077] According to another aspect of the embodiments of the present application, a code generation device is also provided, which can be used to implement the code generation method provided in the above-mentioned embodiments, and will not be repeated hereafter. As used below, the term "module" can implement a combination of software and / or hardware for a predetermined function. Although the devices described in the following embodiments are preferably implemented in software, the implementation of hardware, or a combination of software and hardware, is also possible and contemplated.

[0078] Figure 4 is a structural block diagram of an optional code generation device according to an embodiment of the present application, such as Figure 4 As shown in , the code generating device includes:

[0079] A receiving module 42 is configured to receive natural language information input by a target object, wherein the natural language information represents a code generation requirement of the target object described in natural language;

[0080] A determination module 44, configured to determine structured query information based on the natural language information and a preset template;

[0081] A query module 46 is configured to query a code database based on the natural language information and the structured query information to obtain a target code snippet;

[0082] The generation module 48 is configured to input the target code snippet and the natural language information into a large language model to obtain code generated by the large language model.

[0083] Through the embodiments provided in the present application, structured query information can be generated based on the natural language input by the target object, and the target code can be searched from the code library based on the natural language and the structured query information, and then the natural language and the target code are input into the large language model together to obtain the code generated by the large language model. Therefore, the technical problem of inaccurate code generation based on natural language by the large language model can be solved, and the technical effect of improving the accuracy of code generation based on natural language by the large language model is achieved.

[0084] In an exemplary embodiment, the determination module 44 is used to perform semantic parsing on the natural language information according to the preset template to obtain a semantic parsing result, wherein the semantic parsing result includes the filled content of the preset template; and fill the preset template according to the semantic parsing result to obtain the structured query information.

[0085] In an exemplary embodiment, the determination module 44 is used to determine the template structure of the preset template, wherein the items to be filled in the template structure include operation objects, operation actions and constraints; determine semantic parsing prompt words based on the template structure, wherein the semantic parsing prompt words are used to instruct the large language model to determine the items to be filled in from the natural language information; and input the natural language information and the semantic parsing prompt words into the large language model to obtain the semantic parsing results output by the large language model.

[0086] In an exemplary embodiment, the determination module 44 is configured to determine a correspondence between the filling content in the semantic parsing result and the template structure; and write the filling content into the preset template according to the correspondence to obtain the structured query information.

[0087] In an exemplary embodiment, the query module 46 is used to divide the source code into code fragments based on the structural information of the source code and the semantic information of the source code to obtain multiple code fragments, wherein the code fragments include code and comments; perform annotation enhancement on each code fragment in the multiple code fragments to obtain multiple enhanced annotations; determine a dual-channel vector based on the code in each code fragment and the enhanced annotation corresponding to each code fragment, wherein the dual-channel vector includes code semantic information and comment semantic information; save the code in each code fragment, the enhanced annotation corresponding to each code fragment, and the dual-channel vector to the code database, wherein there is a correspondence between the dual-channel vector and the code in each code fragment and the enhanced annotation corresponding to each code fragment.

[0088] In an exemplary embodiment, the query module 46 is configured to extract an abstract syntax tree of the source code through a code analysis tool, determine structural information of the source code based on the structure of the abstract syntax tree, divide the source code into multiple code blocks based on the structural information, and cluster the multiple code blocks based on semantic information of the source code, determine code blocks of the same type as a code fragment, and obtain the multiple code fragments, wherein the semantic information of the source code includes code semantic information of the multiple code blocks.

[0089] In an exemplary embodiment, the query module 46 is used to perform annotation detection on the multiple code snippets, and determine a first code snippet, a second code snippet, and a third code snippet from the multiple code snippets, wherein the first code snippet represents a code snippet without any annotation, the second code snippet represents a code snippet with an incomplete annotation grammatical structure, and the third code snippet represents a code snippet with a complete annotation grammatical structure; generate a first annotation for the first code snippet, and generate a second annotation for the second code snippet; determine the first annotation, the second annotation, and the third annotation as the multiple enhanced annotations, wherein the third annotation represents the annotation of the third code snippet.

[0090] In an exemplary embodiment, the query module 46 is used to determine similar code snippets of each first code snippet from a high-quality code sample library, wherein the similar code snippets include code and comments, and the similarity between the code semantic information of each first code snippet and the code semantic information of the similar code snippet is greater than a first preset similarity; and the comment of the similar code snippet is determined as the first comment.

[0091] In an exemplary embodiment, the query module 46 is used to input each first code snippet into a comment generation model to obtain a comment output by the comment generation model, wherein the comment generation model is trained based on high-quality code samples; and determine the comment output by the comment generation model as the first comment.

[0092] In an exemplary embodiment, the query module 46 is used to train the annotation generation model in the following manner: dividing the high-quality code sample into code fragments based on the structural information of the high-quality code sample and the semantic information of the high-quality code sample to obtain multiple high-quality code fragments; using the codes in the multiple high-quality code fragments as input samples and the annotations in the multiple high-quality code fragments as output samples to train the initial model to obtain the annotation generation model.

[0093] In an exemplary embodiment, the query module 46 is used to input each of the second code snippets into a comment completion model to obtain a comment output by the comment completion model, wherein the comment completion model is trained based on the comments of high-quality code samples; and determine the comment output by the comment completion model as the second comment.

[0094] In an exemplary embodiment, the query module 46 is used to train the comment completion model in the following manner: splitting the comments of the high-quality code sample to obtain multiple split comments; training the initial model with at least one of the split comments as an input sample and the comments of the high-quality code sample as an output sample to obtain the comment completion model.

[0095] In an exemplary embodiment, the query module 46 is used to vectorize the code in each code snippet to obtain a first vector; vectorize the high-quality annotation corresponding to each code snippet to obtain a second vector; and concatenate the first vector and the second vector to obtain the dual-channel vector.

[0096] In an exemplary embodiment, the query module 46 is used to construct template information of each code snippet based on the enhanced annotation corresponding to each code snippet, wherein the template information of each code snippet includes the operation object, operation action and constraint condition corresponding to each code snippet.

[0097] In an exemplary embodiment, the query module 46 is configured to vectorize the natural language information to obtain a third vector; traverse the code database to determine a target dual-channel vector from the code database, wherein a similarity between the target dual-channel vector and the third vector is greater than a second preset similarity; determine a fourth code snippet based on the code and enhanced annotation corresponding to the target dual-channel vector; determine a fifth code snippet from the code database by matching the structured query information with template information in the code database; and determine the target code snippet based on the fourth and fifth code snippets.

[0098] In an exemplary embodiment, the generation module 48 is used to sort the target code snippets in descending order based on the semantic similarity between the enhanced annotations corresponding to the target code snippets and the natural language information to obtain sorted target code snippets; and input the sorted target code snippets and the natural language information into a large language model to obtain code generated by the large language model.

[0099] For the description of the features in the embodiment corresponding to the code generation device, please refer to the relevant description of the embodiment corresponding to the code generation method, and will not be repeated here.

[0100] An embodiment of the present application further provides an electronic device, comprising a memory and a processor, wherein the memory stores a computer program, and the processor is configured to run the computer program to execute the steps in any of the above code generation method embodiments.

[0101] An embodiment of the present application further provides a computer-readable storage medium, in which a computer program is stored. The computer program is configured to execute the steps of any of the above-mentioned code generation method embodiments when running.

[0102] In an exemplary embodiment, the computer-readable storage medium may include, but is not limited to, various media that can store computer programs, such as a USB flash drive, a read-only memory (ROM), a random access memory (RAM), a mobile hard disk, a magnetic disk, or an optical disk.

[0103] An embodiment of the present application further provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps in any one of the above code generation method embodiments are implemented.

[0104] An embodiment of the present application further provides another computer program product, including a non-volatile computer-readable storage medium, wherein the non-volatile computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the steps in any of the above-mentioned code generation method embodiments are implemented.

[0105] Professionals may further appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the above description has generally described the components and steps of each example according to their functions. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professionals and technicians may use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.

[0106] The above is a detailed introduction to the method and device, storage medium and electronic device of a distributed storage system provided by the present application. Specific examples are used herein to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core ideas of the present application. It should be pointed out that for ordinary technicians in this technical field, without departing from the principles of the present application, several improvements and modifications can be made to the present application, and these improvements and modifications also fall within the scope of protection of the claims of the present application.

Claims

1. A code generation method, characterized in that: include: Receiving natural language information input by a target object, wherein the natural language information represents a code generation requirement of the target object described in natural language; Determining structured query information based on the natural language information and a preset template; Querying a code database according to the natural language information and the structured query information to obtain a target code snippet; Inputting the target code snippet and the natural language information into a large language model to obtain a code generated by the large language model; Wherein, before querying the code database according to the natural language information and the structured query information to obtain the target code snippet, the method further includes: dividing the source code into code snippets based on the structural information of the source code and the semantic information of the source code to obtain multiple code snippets, wherein the code snippets include code and comments; performing annotation enhancement on each of the multiple code snippets to obtain multiple enhanced comments; determining a dual-channel vector according to the code in each code snippet and the enhanced comments corresponding to each code snippet, wherein the dual-channel vector includes code semantic information and comment semantic information; saving the code in each code snippet, the enhanced comments corresponding to each code snippet, and the dual-channel vector to the code database, wherein there is a correspondence between the dual-channel vector and the code in each code snippet and the enhanced comments corresponding to each code snippet; Among them, annotation enhancement is performed on each of the multiple code snippets to obtain multiple enhanced annotations, including: performing annotation detection on the multiple code snippets, determining a first code snippet, a second code snippet, and a third code snippet from the multiple code snippets, wherein the first code snippet represents a code snippet without annotations, the second code snippet represents a code snippet with an incomplete annotation grammatical structure, and the third code snippet represents a code snippet with a complete annotation grammatical structure; generating a first annotation for the first code snippet, and generating a second annotation for the second code snippet; determining the first annotation, the second annotation, and the third annotation as the multiple enhanced annotations, wherein the third annotation represents the annotation of the third code snippet.

2. The code generation method according to claim 1, characterized in that Determining structured query information based on the natural language information and a preset template includes: Performing semantic parsing on the natural language information according to the preset template to obtain a semantic parsing result, wherein the semantic parsing result includes the filled content of the preset template; The preset template is filled according to the semantic parsing result to obtain the structured query information.

3. The code generation method according to claim 2, characterized in that Performing semantic parsing on the natural language information according to the preset template to obtain a semantic parsing result includes: Determining a template structure of the preset template, wherein the items to be filled in the template structure include an operation object, an operation action, and a constraint condition; Determining a semantic parsing prompt word according to the template structure, wherein the semantic parsing prompt word is used to instruct the large language model to determine the item to be filled in from the natural language information; The natural language information and the semantic parsing prompt words are input into the large language model to obtain a semantic parsing result output by the large language model.

4. The code generation method according to claim 3, characterized in that Filling the preset template according to the semantic parsing result to obtain the structured query information includes: Determining a correspondence between the filling content in the semantic parsing result and the template structure; The filling content is written into the preset template according to the corresponding relationship to obtain the structured query information.

5. The code generation method according to claim 1, characterized in that: The source code is divided into code segments based on the structural information of the source code and the semantic information of the source code to obtain multiple code segments, including: Extracting an abstract syntax tree of the source code using a code analysis tool, and determining structural information of the source code according to the structure of the abstract syntax tree; dividing the source code into a plurality of code blocks based on the structural information; The multiple code blocks are clustered based on the semantic information of the source code, and code blocks of the same type are determined as a code fragment to obtain the multiple code fragments, wherein the semantic information of the source code includes code semantic information of the multiple code blocks.

6. The code generation method according to claim 1, characterized in that Generating a first comment for the first code snippet includes: Determining a similar code fragment to each first code fragment from a high-quality code sample library, wherein the similar code fragment includes code and comments, and the similarity between code semantic information of each first code fragment and code semantic information of the similar code fragment is greater than a first preset similarity; The annotation of the similar code fragment is determined as the first annotation.

7. The code generation method according to claim 1, characterized in that Generating a first comment for the first code snippet further includes: Inputting each first code snippet into an annotation generation model to obtain an annotation output by the annotation generation model, wherein the annotation generation model is trained based on high-quality code samples; The annotation output by the annotation generation model is determined as the first annotation.

8. The code generation method according to claim 7, characterized in that: The annotation generation model is trained in the following way: Dividing the high-quality code sample into code segments based on the structural information of the high-quality code sample and the semantic information of the high-quality code sample to obtain a plurality of high-quality code segments; The initial model is trained using the codes in the multiple high-quality code snippets as input samples and the comments in the multiple high-quality code snippets as output samples to obtain the comment generation model.

9. The code generation method according to claim 1, characterized in that: Generating a second comment for the second code snippet, including: Inputting each second code snippet into a comment completion model to obtain comments output by the comment completion model, wherein the comment completion model is trained based on comments of high-quality code samples; The annotation output by the annotation completion model is determined as the second annotation.

10. The code generation method according to claim 9, characterized in that: The annotation completion model is trained in the following way: Splitting the annotations of the high-quality code sample to obtain multiple split annotations; An initial model is trained using at least one of the split comments as an input sample and the comments of the high-quality code sample as an output sample to obtain the comment completion model.

11. The code generation method according to claim 1, wherein: Determining a dual-channel vector according to the code in each code snippet and the enhanced annotation corresponding to each code snippet includes: Vectorizing the code in each code snippet to obtain a first vector; Vectorizing the high-quality comments corresponding to each code snippet to obtain a second vector; The first vector and the second vector are concatenated to obtain the dual-channel vector.

12. The code generation method according to claim 1, wherein: After saving the code in each code snippet, the enhanced annotation corresponding to each code snippet, and the dual-channel vector into the code database, the method further includes: Template information of each code snippet is constructed according to the enhanced annotation corresponding to each code snippet, wherein the template information of each code snippet includes an operation object, an operation action, and a constraint condition corresponding to each code snippet.

13. The code generation method according to claim 12, characterized in that: Querying a code database according to the natural language information and the structured query information to obtain a target code snippet; comprising: Vectorizing the natural language information to obtain a third vector; Traversing the code database, determining a target dual-channel vector from the code database, wherein a similarity between the target dual-channel vector and the third vector is greater than a second preset similarity; Determining a fourth code segment according to the code and enhanced annotation corresponding to the target dual-channel vector; matching the structured query information with the template information in the code database, and determining a fifth code snippet from the code database; The target code segment is determined according to the fourth code segment and the fifth code segment.

14. The code generation method according to claim 1, characterized in that: Inputting the target code snippet and the natural language information into a large language model to obtain code generated by the large language model includes: sorting the target code snippets in descending order based on semantic similarity between the enhanced annotations corresponding to the target code snippets and the natural language information to obtain sorted target code snippets; The sorted target code fragments and the natural language information are input into a large language model to obtain a code generated by the large language model.

15. A code generating device, characterized in that: include: A receiving module, configured to receive natural language information input by a target object, wherein the natural language information represents a code generation requirement of the target object described in natural language; A determination module, configured to determine structured query information based on the natural language information and a preset template; A query module, configured to query a code database based on the natural language information and the structured query information to obtain a target code snippet; A generation module, configured to input the target code snippet and the natural language information into a large language model to obtain code generated by the large language model; The query module is further configured to divide the source code into code segments based on the structural information of the source code and the semantic information of the source code to obtain a plurality of code segments, wherein the code segments include code and comments; perform annotation enhancement on each of the plurality of code segments to obtain a plurality of enhanced comments; determine a dual-channel vector based on the code in each code segment and the enhanced comments corresponding to each code segment, wherein the dual-channel vector includes code semantic information and comment semantic information; save the code in each code segment, the enhanced comments corresponding to each code segment, and the dual-channel vector to the code database, wherein a corresponding relationship exists between the dual-channel vector and the code in each code segment and the enhanced comments corresponding to each code segment; The query module is also used to perform annotation detection on the multiple code snippets, determine a first code snippet, a second code snippet, and a third code snippet from the multiple code snippets, wherein the first code snippet represents a code snippet without any annotations, the second code snippet represents a code snippet with an incomplete annotation grammatical structure, and the third code snippet represents a code snippet with a complete annotation grammatical structure; generate a first annotation for the first code snippet, and generate a second annotation for the second code snippet; determine the first annotation, the second annotation, and the third annotation as the multiple enhanced annotations, wherein the third annotation represents the annotation of the third code snippet.

16. An electronic device, characterized in that: include: Memory for storing computer programs; A processor, configured to implement the steps of the code generation method according to any one of claims 1 to 14 when executing the computer program.

17. A computer-readable storage medium, characterized in that The computer-readable storage medium stores a computer program, wherein when the computer program is executed by a processor, the steps of the code generation method according to any one of claims 1 to 14 are implemented.

18. A computer program product comprising a computer program, characterized in that When the computer program is executed by a processor, the steps of the code generation method according to any one of claims 1 to 14 are implemented.

Citation Information

Patent Citations

  • Highly accurate code snippet recommendation method

    CN108717470A

  • Question rewriting method, question and answer processing method and corresponding devices

    CN118733735A

  • Data query method and system based on large language model

    CN119557330A

  • Code generation method and related equipment

    CN120029897A