A large model-based code generation method and device
Patent Information
- Application Number
- CN202511034369.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-25
- Publication Date
- 2026-09-04
- Estimated Expiration
- 2045-07-25
AI Technical Summary
[0005]本申请实施例提供一种基于大模型的代码生成方法及装置,以解决传统生成方法无法满足用户需求的问题
[0016] As can be seen from the above, embodiments of this application provide a code generation method and apparatus based on a large model. The method includes: receiving a user's code generation request for a target project through one or more rounds of dialogue; inputting the code generation request and initial prompts into a large code generation model, so that the large code generation model generates initial code and descriptive information based on the code generation request and initial prompts, wherein the descriptive information includes at least annotations of classes and functions in the initial code; generating optimization prompts based on the initial code and code library files; wherein the code library files are files in a preset code library specified by the user; inputting the optimization prompts, initial code, descriptive information, and code library files into the large code generation model through one or more rounds, so that the large code generation model replaces the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, thereby obtaining the target project code. It is evident that the method provided by embodiments of this application can reuse a preset code library and can take over tasks such as reading from a specified library, slicing, uploading through a multi-round mechanism, and interacting with a large model, without manual intervention, simplifying manual operations.
Smart Images

Figure CN120929061B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the fields of artificial intelligence and software development technology, and in particular to a code generation method and apparatus based on a large model. Background Technology
[0002] With the rapid development of artificial intelligence technology, large models are becoming increasingly popular in software development. One of the advantages of large models is their ability to generate code directly from a user's text description. For example, if a user enters "write a web crawler using Python," a large model can automatically generate a complete code framework, improving development efficiency and saving a significant amount of time and effort.
[0003] Many businesses and individuals typically need to reuse previously developed class libraries and function libraries to implement software development for specific projects. This allows for better management of the company's or individual's software library, avoids adding repetitive code, and enhances code maintainability. However, large models generally only reuse general-purpose code when generating code, which cannot meet actual needs.
[0004] It is evident that while large models offer many conveniences in software development, they still have numerous shortcomings in practical applications. How to enable large models to better understand and adapt to the technical specifications and existing codebases of enterprises or individuals, avoiding the generation of redundant code and improving code quality and maintainability, remains a pressing issue to be addressed. Summary of the Invention
[0005] This application provides a code generation method and apparatus based on a large model to solve the problem that traditional generation methods cannot meet user needs.
[0006] In a first aspect, embodiments of this application provide a code generation method based on a large model. The method includes: receiving a user's code generation requirements for a target project through one or more rounds of dialogue; inputting the code generation requirements and initial prompts into a large code generation model, so that the large code generation model generates initial code and descriptive information based on the code generation requirements and initial prompts, wherein the descriptive information includes at least annotations of classes and functions in the initial code; generating optimization prompts based on the initial code and code library files; wherein the code library files are files in a preset code library specified by the user; inputting the optimization prompts, initial code, descriptive information, and code library files into the large code generation model through one or more rounds, so that the large code generation model replaces the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, thereby obtaining the target project code.
[0007] In one possible implementation, optimization prompts are generated based on the initial code and codebase files, including: extracting all code units from the initial code to obtain a sequence of code units; a code unit refers to a class or function in the initial code, and the number of code units is M; generating a corresponding optimization prompt for each code unit in the code unit sequence, the optimization prompt is used to guide the code generation model to replace specific code units included in the initial code, the specific code unit refers to the code unit specified by the optimization prompt.
[0008] In one possible implementation, before inputting the optimized prompts, initial code, description information, and codebase file into the code generation model through one or more rounds, the method further includes: obtaining the maximum input length of the code generation model, where the maximum input length refers to the maximum token length of the text input into the code generation model in a single round; counting the total number of tokens in the initial code, description information, and codebase file; if the total number of tokens is greater than a preset length threshold, dividing the codebase file into N code slices to form a code slice sequence; where the preset length threshold is less than or equal to the maximum input length.
[0009] In one possible implementation, the optimized hint words, initial code, description information, and code library file are input into the code generation model through one or more rounds, including: when the total number of tokens is less than or equal to a preset length threshold, traversing the code unit sequence and inputting the input data into the code generation model through M rounds; wherein, the input data of the first round includes the optimized hint words, code library file, initial code, and description information corresponding to the first code unit, the input data of the i-th round includes the optimized hint words, code library file, generation result of the (i-1)-th round, and description information corresponding to the i-th code unit, the i-th code unit is the i-th one in the code unit sequence, M>1, 1<i≤M.
[0010] In one possible implementation, the optimized prompts, initial code, description information, and codebase files are input into the large code generation model through one or more rounds. This further includes: if the total number of tokens exceeds a preset length threshold, traversing the code unit sequence and inputting the input data into the large code generation model through M*N rounds; wherein the input data in the j-th round includes the optimized prompts, the j-th target code slice, and description information corresponding to the j-th target code unit; 1≤j≤M*N, and the j-th target code unit is the k-th code unit in the code unit sequence. The floor operator is the j-th target code slice, which is the m-th code slice in the code slice sequence, where m = (j-1)%N+1, and % is the modulo operator; and when j = 1, the input data for the first round also includes the initial code, and when j > 1, the input data for the j-th round also includes the generated result of the (j-1)-th round.
[0011] In one possible implementation, after inputting optimized prompts, initial code, description information, and codebase files into the code generation model through one or more rounds, the method further includes: feeding back the generation result of the last round of the code generation model as the target project code to the user.
[0012] In one possible implementation, after feeding back the result of the last round of code generation from the large-scale code generation model as the target project code to the user, the method further includes: receiving the user's modification suggestions for the target project code through one or more rounds of dialogue; generating modification prompts for at least one round based on the modification suggestions; and inputting the modification prompts and the target project code into the large-scale code generation model according to the rounds, so that the large-scale code generation model modifies the target project code based on the modification prompts.
[0013] In one possible implementation, before generating the prompt based on the initial code, description information, and code library files, the method further includes: obtaining the code library files based on the code library information; wherein, the code library information is information about a preset code library specified by the user for the target project, and the code library information includes the library file directory, library file name, and / or library file URL, and the number of preset code libraries is one or more.
[0014] In one possible implementation, the descriptive information also includes annotations of input parameters, output parameters, interfaces, and / or return values in the initial code.
[0015] Secondly, embodiments of this application provide a code generation device based on a large model. The device includes: an interaction module configured to receive a user's code generation request for a target project through one or more rounds of dialogue; a code generation module configured to input the code generation request and initial prompts into a large code generation model, so that the large code generation model generates initial code and descriptive information based on the code generation request and initial prompts, wherein the descriptive information includes at least annotations of classes and functions in the initial code; a prompt generation module configured to generate optimized prompts based on the initial code and code library files; wherein the code library files are files in a preset code library specified by the user; and a code optimization module configured to input the optimized prompts, initial code, descriptive information, and code library files into the large code generation model through one or more rounds, so that the large code generation model replaces the classes and functions in the initial code with the classes and functions in the code library files based on the optimized prompts, thereby obtaining the target project code.
[0016] As can be seen from the above, embodiments of this application provide a code generation method and apparatus based on a large model. The method includes: receiving a user's code generation request for a target project through one or more rounds of dialogue; inputting the code generation request and initial prompts into a large code generation model, so that the large code generation model generates initial code and descriptive information based on the code generation request and initial prompts, wherein the descriptive information includes at least annotations of classes and functions in the initial code; generating optimization prompts based on the initial code and code library files; wherein the code library files are files in a preset code library specified by the user; inputting the optimization prompts, initial code, descriptive information, and code library files into the large code generation model through one or more rounds, so that the large code generation model replaces the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, thereby obtaining the target project code. It is evident that the method provided by embodiments of this application can reuse a preset code library and can take over tasks such as reading from a specified library, slicing, uploading through a multi-round mechanism, and interacting with a large model, without manual intervention, simplifying manual operations. Attached Figure Description
[0017] Figure 1 This is a schematic diagram of the structure of a code generation system based on a large model provided in an embodiment of this application;
[0018] Figure 2 This is a first flowchart illustrating the code generation method based on a large model provided in an embodiment of this application;
[0019] Figure 3 A second flowchart illustrating the code generation method based on a large model provided in this application embodiment;
[0020] Figure 4 A schematic diagram of the configuration interface provided in an embodiment of this application;
[0021] Figure 5 This is a schematic diagram of the first process of the code generation optimization stage provided in an embodiment of this application;
[0022] Figure 6 A second flowchart illustrating the code generation and optimization stage provided in this application embodiment;
[0023] Figure 7 A schematic diagram of the structure of the code generation device based on a large model provided in the embodiments of this application. Detailed Implementation
[0024] To enable those skilled in the art to better understand the technical solutions in this application, the technical solutions in the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those of ordinary skill in the art without creative effort should fall within the scope of protection of this application.
[0025] Figure 1 This is a schematic diagram of the structure of a code generation system based on a large model provided in an embodiment of this application.
[0026] like Figure 1 As shown, this application embodiment provides a code generation system based on a large model, which may include a code generation module, a human-computer interaction module, and a large code generation model. The code generation module can also be called a code generation assistant or software development agent assistant. It can call the human-computer interaction module to interact with the user, such as a software development engineer, to obtain user requirements. Furthermore, it can call the large code generation model to generate code and provide the code back to the user. Further, in order to generate code that meets user needs and improve the reusability of software libraries for enterprises or individuals, the code generation module provided in this application embodiment can be used to execute the following code generation method based on the large model.
[0027] Figure 2 This is a first flowchart illustrating the code generation method based on a large model provided in an embodiment of this application.
[0028] Figure 3 This is a second flowchart illustrating the code generation method based on a large model provided in an embodiment of this application.
[0029] like Figure 2 and Figure 3 As shown, this application provides a code generation method based on a large model, which is applied to a code generation system based on a large model, including the following steps S100-S400.
[0030] S100: Receives user code generation requests for target projects through one or more rounds of dialogue.
[0031] This step is the initial stage of code generation, at which point the code generation assistant can be in dialogue mode. This embodiment of the application can display a dialogue interface, obtaining the user's code generation requirements for the target project through dialogue. The user may be an enterprise or individual, and the target project may be a software system, application, tool, or module, etc. The dialogue content can be generated either through predefined dialogue content or dynamically generated dialogue content.
[0032] In a pre-defined dialogue format, a fixed dialogue script or template can be used to communicate with the user. For example, in this application, a fixed dialogue script can be pre-written. For instance, to address a user's need to generate web crawler code, a series of fixed questions and answers can be pre-set, such as "Which website's data do you want to crawl?" or "What specific data fields do you need to crawl?". In practical applications, each question can correspond to a state node, and the user's answer can trigger a state switch until sufficient demand information is collected.
[0033] This application embodiment can also pre-create dialogue templates according to different code generation requirement types. For example, for the requirement to generate data processing code, a dialogue template can be created. This dialogue template can be a structured framework including replaceable items. For example, the data processing code generation dialogue template can be as follows:
[0034] 1. Data Source: What is your data source [file / database / API / other]?
[0035] -If you select a file: Is the file format [CSV / JSON / Excel / Other]?
[0036] -If you select a database: Is the database type [MySQL / PostgreSQL / Oracle / SQL Server / Other]?
[0037] - Database connection information: Host address? Port number? Username? Password? Database name?
[0038] -If you choose API: What is the API address? What is the authentication method [No Authentication / API Key / OAuth / Other]?
[0039] 2. Data processing requirements:
[0040] - What operations do you need to perform on the data? [Cleansing / Transformation / Analysis / Visualization / Other];
[0041] -If you choose cleaning: What issues need to be addressed? [Missing values / Duplicate values / Outliers / Formatting errors / Other];
[0042] -If conversion is selected: What type of conversion is required? [Data type conversion / format conversion / calculation derivation / other];
[0043] 3. Output requirements:
[0044] Where should the processed data be output? [File / Database / API / Other];
[0045] - What are the output format requirements? [CSV / JSON / Excel / specific format / other];
[0046] - Is it necessary to generate a data processing report? [Yes / No]
[0047] Dynamically generating dialogue content can be achieved through natural language processing techniques, such as using large language models to perform semantic understanding and intent analysis on user input, thereby generating reasonable dialogue content.
[0048] In some implementations, embodiments of this application may provide an interactive interface, which can be configured within a human-computer interaction module. This allows the code generation module to invoke the human-computer interaction module to engage in one or more rounds of dialogue with the user to obtain code generation requirements. Furthermore, the interactive interface may include various interactive controls and elements such as text dialog boxes, buttons, and drop-down menus. Users can use these controls to input specific requirements, select relevant parameters, or configure various options through natural language dialogue, parameter form filling, etc., thereby accurately obtaining user intent and providing more precise input to the code generation module, thus improving the accuracy of code generation and its ability to meet personalized user needs.
[0049] For example, the interactive interface may include a main menu where users can create a project for the target project and configure it accordingly. In this mode, the code generation assistant is essentially in configuration mode. The interactive interface may also include a private library configuration interface, through which users can specify a preset code library for the target project. The preset code library can be a user-specified code library for the target project, and it includes library files, such as class libraries and function libraries. Users can configure the preset code library through the private library configuration interface at the initial stage of the project, or at stages where editing or modifying the specified preset code library is required. In this way, embodiments of this application can reuse classes and functions in the preset code library to generate code for the target project. One or more preset code libraries can be specified for the same target project.
[0050] Figure 4 This is a schematic diagram of the configuration interface provided in an embodiment of this application.
[0051] like Figure 4As shown, the interactive interface (also known as the private library configuration interface) includes controls such as "New," "Edit," "Browse," and "Delete." Through this interface, users can configure library file directories, library file names, and Uniform Resource Locator (URL) addresses for preset code repositories. Furthermore, this embodiment can store the code repository information in a database for later use. The library file directory refers to the path or folder where library files are stored, used to organize and manage them. The library file name refers to the name of a specific library file, used to uniquely identify it. After adding a new library file, the code repository information can be displayed as entries in the interactive interface. This allows users to freely input multiple entries related to preset code repositories for a target project to add constraints on the preset code repository.
[0052] S200: Input the code generation requirements and initial prompts into the code generation model, so that the code generation model can generate initial code and descriptive information based on the code generation requirements and initial prompts. The descriptive information includes at least annotations of classes and functions in the initial code.
[0053] This step belongs to the initial stage of code generation. In this application embodiment, the dialogue between the person (user) and the large model can be taken over, simplifying the operation between software developers and the large model.
[0054] In this step, the embodiments of this application can generate corresponding initial prompts for the user based on code generation requirements, and input the initial prompts and code generation requirements into the large code generation model, requiring the large code generation model to generate initial code based on the code generation requirements and the initial prompts. A prompt is the input text used to guide the model to generate specific content.
[0055] Furthermore, the initial code can be an abstraction of the code's functionality, or it can be pseudocode or general-purpose code. Pseudocode is a natural language expression of the algorithm's logic, a representation method between natural language and programming language, used to describe the logical flow of an algorithm or program. General-purpose code refers to executable code with a complete syntactic structure. General-purpose code is implemented based on general-purpose functions and can be widely used in different projects or environments, possessing a certain degree of versatility and portability.
[0056] Furthermore, embodiments of this application can, based on initial prompts, request the code generation model to describe in detail the functions, input parameters (hereinafter referred to as input parameters), output parameters (hereinafter referred to as output parameters), interfaces, and return values of the classes and functions called in the initial code, forming descriptive information to facilitate subsequent optimization of the initial code. In other words, initial prompts are used to request the large model to generate initial code or pseudo-code according to user requirements, and require the large model to describe in detail the functions, input parameters, output parameters, interfaces, and return values of the classes and functions called in the initial code, so that in subsequent stages the large model can continue to search and match code calling libraries based on the functions, input parameters, output parameters, interfaces, and return values of these classes and functions.
[0057] For example, the initial prompt could be: "Write a Python function that calculates the average of all numbers in a given list and returns the result; and describe in detail the classes, methods, input parameters, output parameters, interfaces, and return values called in the function." It is understood that this initial prompt is merely an example, and its actual content is generated based on actual needs.
[0058] Furthermore, embodiments of this application can establish a large-scale code generation model based on the following steps: First, code data is collected as pre-training data. Then, a basic model framework is built based on architectures such as Transformer, and pre-trained using alignment with the pre-training data. After pre-training, supervised fine-tuning is performed using labeled datasets such as "requirement description-code implementation" to optimize the model's understanding of specific development tasks.
[0059] S300: Generates optimized prompts based on the initial code and codebase files; where the codebase files are files in the user-specified preset codebase.
[0060] This step belongs to the code generation optimization phase. The preset code library, also known as the code call library, can be specified by the user in the configuration phase before step S100, and there can be one or more preset code libraries.
[0061] Before generating optimization suggestions, code library files can be obtained based on code library information. The code library information is the information of the preset code library specified by the user for the target project, including the library file directory, library file name and / or library file URL.
[0062] Furthermore, optimization prompts can be used to guide large models to optimize the initial code, so that the code generation large model can replace the classes and functions in the initial code with the classes and functions in the code library file based on the optimization prompts. The steps for generating optimization prompts will be detailed below and will not be repeated here.
[0063] S400: Input optimization prompts, initial code, description information, and code library files into the code generation model through one or more rounds, so that the code generation model can replace the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, and obtain the target project code.
[0064] In this way, target project code that meets the code standardization management requirements of enterprises or individuals can be generated. It is evident that the method provided in this application can replace manual multi-round dialogue with large models, automatically searching, matching, and replacing classes and functions without human intervention, simplifying manual operations and shielding users from interference.
[0065] As can be seen from the above, this application provides a code generation method based on a large model. The method includes: receiving a user's code generation requirements for a target project through one or more rounds of dialogue; inputting the code generation requirements into a large code generation model, so that the large code generation model generates initial code and descriptive information based on the code generation requirements, wherein the descriptive information includes at least annotations of classes and functions in the initial code; generating optimization prompts based on the initial code and code library files; wherein the code library files are files in a preset code library specified by the user; inputting the optimization prompts, initial code, descriptive information, and code library files into the large code generation model through one or more rounds, so that the large code generation model replaces the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, thereby obtaining the target project code. As can be seen, based on the method provided in the embodiments of this application, a dual-mode configuration mode and a dialogue mode can be formed in the code generation stage to obtain the constraints of the code library and the user's code generation requirements. Furthermore, the method provided in the embodiments of this application can intelligently decompose code generation into multiple stages: initial stage, optimization stage, and completion stage. In each stage, especially the optimization stage, it takes over the reading of the specified library, slicing, uploading through a multi-round mechanism, and interacting with the code of the large model, etc., without manual intervention, thus simplifying manual operations.
[0066] Furthermore, after step S200, this embodiment of the application can also add the initial code, description information, and other results returned by the code generation model to the dialogue history list. This is because the long-term memory capacity of the large model is limited, and the dialogue history list can serve as a context carrier for multi-turn interactions, providing key semantic associations and iterative basis for subsequent optimization.
[0067] Figure 5 This is a schematic diagram of the first process of the code generation and optimization stage provided in an embodiment of this application.
[0068] like Figure 5 As shown, step S300 may include the following steps S301-S302.
[0069] S301: Extract all code units from the initial code to obtain a sequence of code units; a code unit refers to a class or function in the initial code, and the number of code units is M.
[0070] This step involves analyzing and breaking down the initial code based on the descriptive information to ultimately determine all the classes and functions contained within it. It's understandable that the specific number of code units is determined by the actual structure of the initial code.
[0071] S302: Generate corresponding optimization prompts for each code unit in the code unit sequence. The optimization prompts are used to guide the code generation model to replace specific code units included in the initial code. The specific code units refer to the code units specified by the optimization prompts.
[0072] This application embodiment can generate a corresponding optimization suggestion word for each type and function. For example, the initial code may include a read_file function, and the optimization suggestion word corresponding to this code unit may be: "Please optimize the read_file function, replace it with a function with the same function in the xx code library file, and output the replaced code.", where "xx" can refer to the name of the code library file.
[0073] This application provides a prompt word template library, which may include function optimization templates and class optimization templates. A function optimization template might be, for example, "Please optimize function [function name] and replace it with a function from [code library name]", and a class optimization template might be, for example, "Please optimize function [class name] and replace it with a class from [code library name]". The content within the brackets [] can be filled in based on actual code units. This allows for direct extraction and filling of prompt word templates when generating optimization prompt words, simplifying the prompt word generation process and reducing its difficulty.
[0074] In some implementations, embodiments of this application may also limit the length of the optimized prompt words so that the total number of tokens for the optimized prompt words does not exceed a prompt word threshold, such as 30 tokens, to improve generation efficiency and accuracy.
[0075] Figure 6 This is a second flowchart illustrating the code generation and optimization stage provided in an embodiment of this application.
[0076] like Figure 6 As shown, the steps S501-S503 may be included before step S300.
[0077] S501: Get the maximum input length of the code to generate a large model. The maximum input length refers to the maximum token length of the text generated from a single input code to generate a large model.
[0078] A token is the basic unit of text processing in a model. It is formed by tokenizing the text and other content input to the large model. Tokens can be words, characters, punctuation marks, etc. The maximum input length can also refer to the maximum number of tokens that can be generated from a single input code to the large model. Obtaining the maximum input length is a prerequisite for ensuring that the code or text can be completely passed to the large model, preventing the input information from being truncated, which would affect the output quality.
[0079] S502: Count the total number of tokens in the initial code, description information, and code library files.
[0080] This application embodiment can use the same tokenization method as the code generation large model to tokenize the initial code, description information, and code library files to obtain the total number of tokens. Counting the total number of tokens is to ensure that subsequent inputs will not exceed the maximum input limit of the model.
[0081] S503: If the total number of tokens exceeds the preset length threshold, the code library file is divided into N code slices to form a code slice sequence.
[0082] The preset length threshold is less than or equal to the maximum input length. For example, it can be equal to 512 tokens, 1024 tokens, or 2048 tokens. The specific value can be adjusted based on the actual situation, and this application embodiment does not impose a specific limitation on it.
[0083] Furthermore, the code slice sequence consists of N code slices obtained by slicing the code library file. That is, this embodiment of the application can divide a large code library file into N smaller code slices. In practical applications, this embodiment of the application can divide the code library file into multiple code slices of a preset code length. The preset code length is, for example, equal to 256 tokens, and the specific number of N depends on the size of the code library file.
[0084] The preset code length can be slightly less than the preset length threshold, for example, 50 tokens less than the preset length threshold, to ensure that the total number of tokens input to the description information of the code generation large model, the code library file (e.g., the unsegmented code library file or code slice), and the code to be optimized (e.g., the initial code) does not exceed the maximum input length of the code generation large model.
[0085] Understandably, if the total number of tokens is less than or equal to the total number of tokens, there is no need to perform a splitting operation.
[0086] Furthermore, step S300 may also include the following steps S303-S304.
[0087] S303: When the total number of tokens is less than or equal to the preset length threshold, traverse the code unit sequence and input the input data into the code generation model through M rounds; wherein, the input data of the first round includes the optimization hint word, code library file, initial code and description information corresponding to the first code unit, and the input data of the i-th round includes the optimization hint word, code library file, generation result and description information of the i-th round corresponding to the i-th code unit, the i-th code unit is the i-th one in the code unit sequence, M>1, 1<i≤M.
[0088] Understandably, if the total number of tokens is less than or equal to a preset length threshold, the codebase file is not sliced. In this case, this embodiment can traverse the code unit sequence and replace each code unit (class or function) sequentially. During a single replacement process, the prompt word corresponding to the code unit, the codebase file, and the code to be replaced can be input into the large code generation model. In this case, the replacement process can be completed in M rounds, where M equals the total number of classes and functions in the initial code.
[0089] For example, the number of code units can be three, i.e., M=3, namely Unit1, Unit2, and Unit3. In the first round, the input data consists of the optimization hints, description information, and initial code corresponding to Unit1. The output of this round includes the code obtained by replacing Unit1 in the initial code. In the second round, i=2, the input data consists of the optimization hints, description information, and the generated large model from the first round of code generation. The output of this round includes the code obtained by replacing Unit2 in the first round's output. In the third round, i=3, the input data consists of the optimization hints, description information, and the generated large model from the second round of code generation. The output includes the code obtained by replacing Unit3 in the second round's output, which is the final target generated code.
[0090] In some cases, if M=1, then the optimization hints, code library files, initial code, and description information corresponding to the code unit can be input into the large code generation model in one round.
[0091] S304: If the total number of tokens exceeds a preset length threshold, traverse the code unit sequence and input the input data into the code generation model through M*N rounds; wherein, the input data of the j-th round includes the optimization hint word corresponding to the j-th target code unit, the j-th target code slice, and description information; 1≤j≤M*N, and the j-th target code unit is the k-th code unit in the code unit sequence. The % operator is the floor operator. The j-th target code slice is the m-th code slice in the code slice sequence, where m = (j-1)%N+1, and % is the modulo operator.
[0092] Furthermore, when j=1, the input data for the first round also includes the initial code, and when j>1, the input data for the j-th round also includes the generation result of the (j-1)th round.
[0093] In this case, the replacement process can be completed in M*N rounds, where M equals the total number of classes and functions in the initial code, and N equals the total number of code slices.
[0094] It's worth noting that j represents the round number of the input round; for example, j=1 indicates the first input round. k represents the index of the code unit in the code unit sequence within the input data. This is equivalent to the general formula for the sequence number of the code unit in the input data corresponding to each round, where m represents the sequence number of the code slice in the input data in the code slice sequence, and m = (j-1)%N+1 is equivalent to the general formula for the sequence number of the code slice in the input data corresponding to each round.
[0095] For example, with 2 code slices (N=2) and 3 code units (M=3), traversing the code unit sequence can form 2*3=6 input rounds, as follows:
[0096] In the first round, input the optimization hints, the first code slice, and the initial code corresponding to the first code unit;
[0097] In the second round, input the optimization hint words corresponding to the first code unit, the second code slice, and the generated results of the first round;
[0098] In the third round, input the optimization hints for the second code unit, the first code slice, and the generated results from the second round;
[0099] In the fourth round, input the optimization hints for the second code unit, the second code slice, and the generated results from the third round;
[0100] In the 5th round, input the optimization hint words corresponding to the 3rd code unit, the 1st code slice, and the generated results from the 4th round;
[0101] In the 6th round, input the optimization hint words corresponding to the 3rd code unit, the 2nd code slice, and the generated results from the 5th round.
[0102] The following examples will be used to verify the general formula for the sequence number of code units and the general formula for the sequence number of code slices.
[0103] For example, when j=1, that is, for the first input round, m = (1-1)%2+1 = 1. The input data for the first round includes the optimization hint word corresponding to the first code unit in the code unit sequence, the first code slice in the code slice sequence, the initial code, and the description information; consistent with the previous example.
[0104] For example, when j=2, that is, for the second input round, m = (2-1)%2+1 =1+1 =2. The input data for the second round includes the optimization hint word corresponding to the first code unit in the code unit sequence, the second code slice in the code slice sequence, the generation result and description information of the first round; consistent with the previous example.
[0105] For example, when j=5, that is, for the 5th input round, m = (5-1)%2+1 = 0+1 = 1. The input data for the 5th round includes the optimization hint word corresponding to the 3rd code unit in the code unit sequence, the 1st code slice in the code slice sequence, the generation result and description information of the 4th round; consistent with the previous example.
[0106] The remaining rounds will not be elaborated here. Based on the general formula, the response time of each round's input can be simplified, and each round can be executed automatically without manual intervention, improving the performance of the code development proxy assistant and enhancing the user experience.
[0107] In this way, the initial code can be optimized through one or more rounds of dialogue.
[0108] Based on the foregoing, the large code generation model can match classes and functions with the same functionality in the codebase file based on the optimized prompt words for the classes and functions in the initial code. If a matching class or function is identified in the codebase file, the class or function in the initial code can be replaced with the class or function in the codebase file, and the replaced code can be output.
[0109] In some implementations, if no matching class or function is identified in the codebase file, no replacement can be performed and the generated result without replacement can be output. This step can be achieved by adjusting and optimizing the prompt words.
[0110] In some implementations, if the generated results for each round are too long, exceeding the token capacity of the code-generated large model, then the code-generated large model can output the results in two or more separate rounds. It's understandable that splitting the output does not affect the number of rounds in the input rounds; that is, the maximum value of the round number j is still equal to M or M*N. A single input round can correspond to one or more output rounds, and the generated results for a single input round can include the content output by one or more output rounds.
[0111] Furthermore, after step S400, the following steps S601-S604 may also be included.
[0112] S601: The generated result of the last round of code generation of the large model is fed back to the user as the target project code.
[0113] This step marks the completion of code generation, transitioning from interacting with the large code generation model to interacting with the user in order to provide feedback on the target project code to the user.
[0114] S602: Receive user feedback on the target project code through one or more rounds of dialogue.
[0115] Dialogue content can be generated either through predefined methods or dynamically. For details, please refer to the preceding content; further explanation is omitted here.
[0116] In practical applications, users can interact with the code generation assistant through an interactive interface, as shown in the following dialogue example:
[0117] User: The code comments are too simple and I can't understand them. Detailed functional descriptions are needed.
[0118] Assistant: Which parts of the comments would you like more detailed explanations for? Is it about the function's purpose, parameter descriptions, or the specific implementation logic? This way, I can more effectively supplement and improve the comments for you.
[0119] User: I need a more detailed explanation of the comments in the main function, especially the purpose of the parameters and the function's return value.
[0120] Assistant: Okay, I understand. I will add more detailed comments to the main function, including the specific purpose of each parameter and a description of the function's return value.
[0121] S603: Generate modification prompts for at least one round based on modification suggestions.
[0122] The modification prompts are generated from a prompt template library. Specifically, the prompt template library can include modification templates, such as those for modifying [main function / class / conditional statement / loop statement / structure / comment].
[0123] In the example above, the user's suggestion was "enhance the main function code comments." The suggested modification was, for example, to modify the [main function comments].
[0124] S604: Input the modification prompts and target project code into the code generation model in rounds so that the code generation model can modify the target project code based on the modification prompts.
[0125] In this step, the total number of tokens in the target project code can be counted, and it can be determined whether the total number of tokens in the target project code exceeds a preset length threshold. If it exceeds the preset length threshold, the target project code can be segmented, and then the segmented target project code can be input into the code generation model through multiple rounds.
[0126] Finally, the modified target project code can be fed back to the user.
[0127] It should also be noted that, in this embodiment of the application, the generation results received from the code generation model in each round can be saved as dialogue history information to the dialogue history list. When it is necessary to input the initial code or the generation results of each round, or other dialogue history information, into the code generation model, for example, when executing step S400, the dialogue history information can be obtained from the dialogue history list.
[0128] Based on the code generation method based on a large model provided in this application, a code generation package can be generated. This application embodiment allows the package to be installed on a client, enabling users to directly utilize the code generation function through the client. This application embodiment can also integrate the package into an Integrated Development Environment (IED) or a server. This application embodiment can further encapsulate the package as an interactive intelligent agent to achieve automated code generation. In some implementations, the package can also be conveniently implemented as a plugin, other client forms, or a server proxy, thus enabling a code generation assistant with multiple operating modes and improving the user experience.
[0129] As can be seen from the above, the code generation method based on a large model provided in this application can achieve the following beneficial effects:
[0130] 1. Code Repository Reuse and Management: Through an easy-to-use interface, solve the problems of code repository reuse and management for enterprises or individuals, and simplify the management of code repository constraints.
[0131] 2. Dual-mode switching: Set configuration mode and dialogue mode to reuse code library constraints and upload user software requirements to the large model for matching and replacement.
[0132] 3. Intelligent Assistant: The method provided in the embodiments of this application can realize an intelligent development agent assistant, which can take over most of the tedious code generation and management operations, freeing up software developers.
[0133] 4. Multi-stage code generation: The code generation function is divided into multiple stages (initial stage, optimization stage, and completion stage). During the optimization stage, the reading, replacement, and uploading of the code library are handled automatically, reducing manual intervention.
[0134] 5. Dynamic prompt word generation: The method provided in this application embodiment can automatically generate and match relevant prompt words according to different stages of code generation, simplifying the development process.
[0135] 6. Code Standardization: Through intelligent management methods, the generated code meets the code standardization requirements of the enterprise or individual.
[0136] 7. Multi-turn dialogue management: During the interaction with the large model, multi-turn dialogues are automatically managed to dynamically match and replace class libraries and function libraries in the codebase, reducing user intervention.
[0137] In summary, the embodiments of this application can provide users with an intelligent, automated, and simplified code generation method, improving software development efficiency, ensuring standardized management and reuse of code repositories, and adapting to the needs of different enterprises or individuals. Furthermore, it simplifies software development into an end-to-end, one-stop code generation model, freeing software engineers from the tedious process of human-machine interaction with large models. It minimizes the need for software engineers to frequently write and adjust different prompts at different stages of code generation, making it more engineer-friendly. It provides a user-friendly human-computer interface, enabling software developers to easily configure private library constraints. It transfers human workload to machines, using a code generation assistant to take over the tedious human-machine interaction process with large models.
[0138] Figure 7 A schematic diagram of the structure of the code generation device based on a large model provided in the embodiments of this application.
[0139] like Figure 7 As shown in the figure, this application provides a code generation apparatus based on a large model, the apparatus comprising:
[0140] Interaction module 1001 is configured to receive user code generation requests for a target project through one or more rounds of dialogue.
[0141] The code generation module 1002 is configured to: input code generation requirements and initial prompts into the code generation model, so that the code generation model generates initial code and description information based on the code generation requirements and initial prompts, and the description information includes at least annotations of classes and functions in the initial code;
[0142] The prompt word generation module 1003 is configured to generate optimized prompt words based on the initial code and code library files; wherein, the code library files are files in a preset code library specified by the user.
[0143] The code optimization module 1004 is configured to input optimization prompts, initial code, description information, and code library files into the code generation model through one or more rounds, so that the code generation model can replace the classes and functions in the initial code with the classes and functions in the code library files based on the optimization prompts, thereby obtaining the target project code.
[0144] In one possible implementation, the code optimization module 1004 is specifically configured to: extract all code units in the initial code to obtain a code unit sequence; a code unit refers to a class and function in the initial code, and the number of code units is M; generate a corresponding optimization prompt word for each code unit in the code unit sequence, and the optimization prompt word is used to guide the code generation model to replace specific code units included in the initial code, and the specific code unit refers to the code unit limited by the optimization prompt word.
[0145] In one possible implementation, the code optimization module 1004 is specifically configured to: obtain the maximum input length of the code generation large model, where the maximum input length refers to the maximum token length of the text generated from a single input code to the large model; count the total number of tokens in the initial code, description information, and code library file; if the total number of tokens is greater than a preset length threshold, divide the code library file into N code slices to form a code slice sequence; the preset length threshold is less than or equal to the maximum input length.
[0146] In one possible implementation, the code optimization module 1004 is specifically configured as follows: when the total number of tokens is less than or equal to a preset length threshold, the code unit sequence is traversed, and the input data is input into the code generation large model through M rounds; wherein, the input data of the first round includes the optimization prompt word, code library file, initial code and description information corresponding to the first code unit, and the input data of the i-th round includes the optimization prompt word, code library file, generation result and description information of the i-th round corresponding to the i-th code unit, the i-th code unit is the i-th one in the code unit sequence, M>1, 1<i≤M.
[0147] In one possible implementation, the code optimization module 1004 is specifically configured as follows: when the total number of tokens exceeds a preset length threshold, the code unit sequence is traversed, and the input data is fed into the code generation model through M*N rounds; wherein, the input data of the j-th round includes the optimization hint word corresponding to the j-th target code unit, the j-th target code slice, and description information; 1≤j≤M*N, and the j-th target code unit is the k-th code unit in the code unit sequence. The floor operator is the j-th target code slice, which is the m-th code slice in the code slice sequence, where m = (j-1)%N+1, and % is the modulo operator; and when j = 1, the input data for the first round also includes the initial code, and when j > 1, the input data for the j-th round also includes the generated result of the (j-1)-th round.
[0148] In one possible implementation, the code optimization module 1004 is specifically configured to: feed back the generation result of the last round of the code generation model as the target project code to the user; receive the user's modification suggestions for the target project code through one or more rounds of dialogue; generate modification prompts for at least one round based on the modification suggestions; and input the modification prompts and the target project code into the code generation model according to the rounds, so that the code generation model modifies the target project code based on the modification prompts.
[0149] In one possible implementation, the prompt word generation module 1003 is specifically configured to: obtain code library files based on code library information; wherein, the code library information is information about a preset code library specified by the user for the target project, and the code library information includes the library file directory, library file name and / or library file URL, and the number of preset code libraries is one or more.
[0150] In one possible implementation, the descriptive information also includes annotations of input parameters, output parameters, interfaces, and / or return values in the initial code.
[0151] In a specific implementation, the present invention also provides a computer storage medium, wherein the computer storage medium may store a program, and the program, when executed, may include some or all of the steps of the code generation method based on a large model provided by the present invention. The storage medium may be a magnetic disk, optical disk, read-only memory (ROM), or random access memory (RAM), etc.
[0152] It is readily understood that, based on the several embodiments provided in this application, those skilled in the art can combine, split, or reorganize the embodiments of this application to obtain other embodiments, none of which exceed the protection scope of this application.
[0153] The above detailed embodiments further illustrate the purpose, technical solution, and beneficial effects of the embodiments of this application. It should be understood that the above are merely specific embodiments of the embodiments of this application and are not intended to limit the protection scope of the embodiments of this application. Any modifications, equivalent substitutions, improvements, etc., made on the basis of the technical solutions of the embodiments of this application should be included within the protection scope of the embodiments of this application.
Claims
1. A code generation method based on a large model, characterized in that, The method includes: Through one or more rounds of dialogue, receive users' code generation requirements for the target project; The code generation requirements and initial prompts are input into the code generation model, so that the code generation model generates initial code and description information based on the code generation requirements and initial prompts. The description information includes at least annotations of classes and functions in the initial code. Optimized suggestion words are generated based on the initial code and codebase file; wherein, the codebase file is a file in a preset codebase specified by the user; The generation of optimized suggestion words based on the initial code and code library files includes: Extract all code units from the initial code to obtain a code unit sequence; the code unit refers to the class and function in the initial code, and the number of code units is M. For each code unit in the code unit sequence, a corresponding optimization prompt word is generated. The optimization prompt word is used to guide the code generation model to replace specific code units included in the initial code. The specific code unit refers to the code unit defined by the optimization prompt word. The optimization prompts, the initial code, the description information, and the code library file are input into the code generation model through one or more rounds, so that the code generation model replaces the classes and functions in the initial code with the classes and functions in the code library file based on the optimization prompts, thereby obtaining the target project code. Before inputting the optimized prompts, the initial code, the description information, and the code library file into the large code generation model through one or more rounds, the method further includes: Obtain the maximum input length of the code-generated large model, where the maximum input length refers to the maximum token length of the text input into the code-generated large model in a single instance; Count the total number of tokens in the initial code, the description information, and the code library files; If the total number of tokens is greater than a preset length threshold, the code library file is divided into N code slices to form a code slice sequence; the preset length threshold is less than or equal to the maximum input length.
2. The code generation method based on a large model according to claim 1, characterized in that, The step of inputting the optimized prompts, the initial code, the description information, and the code library file into the large code generation model through one or more rounds includes: If the total number of tokens is less than or equal to the preset length threshold, the code unit sequence is traversed, and the input data is input into the code generation model through M rounds; wherein, the input data of the first round includes the optimization prompt word corresponding to the first code unit, the code library file, the initial code, and the description information, the input data of the i-th round includes the optimization prompt word corresponding to the i-th code unit, the code library file, the generation result of the (i-1)-th round, and the description information, the i-th code unit is the i-th one in the code unit sequence, M>1, 1<i≤M.
3. The code generation method based on a large model according to claim 1, characterized in that, The step of inputting the optimized prompt words, the initial code, the description information, and the code library file into the code generation model through one or more rounds further includes: If the total number of tokens exceeds the preset length threshold, the code unit sequence is traversed, and the input data is processed through M. N rounds of input are fed into the code generation model; wherein, the input data of the j-th round includes the optimization prompt word corresponding to the j-th target code unit, the j-th target code slice, and the description information; 1≤j≤M N, where the j-th target code unit is the k-th code unit in the sequence of code units. , The % operator is the floor operator, where the j-th target code slice is the m-th code slice in the code slice sequence, and m = (j-1)%N+1, where % is the modulo operator. Furthermore, when j=1, the input data for the first round also includes the initial code, and when j>1, the input data for the j-th round also includes the generation result of the (j-1)-th round.
4. The code generation method based on a large model according to claim 1, characterized in that, After inputting the optimized prompts, the initial code, the description information, and the code library file into the large code generation model through one or more rounds, the method further includes: The generated result of the code in the last round is used as the target project code and fed back to the user.
5. The code generation method based on a large model according to claim 4, characterized in that, After the generation result of the large model from the last round of code generation is fed back to the user as the target project code, the process also includes: Through one or more rounds of dialogue, receive user feedback on modifications to the target project code; Based on the proposed modifications, at least one round of modification prompts will be generated; The modification prompts and the target project code are input into the code generation model in rounds, so that the code generation model modifies the target project code based on the modification prompts.
6. The code generation method based on a large model according to claim 1, characterized in that, Before generating the prompt words based on the initial code, the description information, and the code library file, the method further includes: The code library file is obtained based on the code library information; wherein, the code library information is the information of the preset code library specified by the user for the target project, and the code library information includes the library file directory, library file name and / or library file URL, and the number of preset code libraries is one or more.
7. The code generation method based on a large model according to claim 1, characterized in that, The descriptive information also includes annotations of the input parameters, output parameters, interfaces, and / or return values in the initial code.
8. A code generation device based on a large model, characterized in that, The device includes: The interaction module is configured to receive the user's code generation requirements for the target project through one or more rounds of dialogue. The code generation module is configured to: input the code generation requirements and initial prompts into the code generation model, so that the code generation model generates initial code and description information based on the code generation requirements and initial prompts, wherein the description information includes at least annotations of classes and functions in the initial code; The prompt word generation module is configured to generate optimized prompt words based on the initial code and the code library file; wherein the code library file is a file in a preset code library specified by the user; The prompt word generation module is specifically configured as follows: Extract all code units from the initial code to obtain a code unit sequence; the code unit refers to the class and function in the initial code, and the number of code units is M. For each code unit in the code unit sequence, a corresponding optimization prompt word is generated. The optimization prompt word is used to guide the code generation model to replace specific code units included in the initial code. The specific code unit refers to the code unit defined by the optimization prompt word. The code optimization module is configured to: input the optimization prompts, the initial code, the description information, and the code library file into the code generation model through one or more rounds, so that the code generation model replaces the classes and functions in the initial code with the classes and functions in the code library file based on the optimization prompts, thereby obtaining the target project code; The code optimization module is also configured to: obtain the maximum input length of the code generation model, wherein the maximum input length refers to the maximum token length of the text input into the code generation model in a single instance; Count the total number of tokens in the initial code, the description information, and the code library files; If the total number of tokens is greater than a preset length threshold, the code library file is divided into N code slices to form a code slice sequence; the preset length threshold is less than or equal to the maximum input length.
Citation Information
Patent Citations
Intelligent malicious code detection method for long script file
CN119026124A
A large model prompt word optimization method and related device
CN119783685A
Intelligent contract generation method and device and computer readable storage medium
CN120179212A