Code generation method, device, computer equipment and storage medium

By establishing the mapping relationship between source code and code description language in a large language model and using code description language as background knowledge, the context window restriction problem of large language models during code generation is solved, and more accurate and efficient code generation is achieved. .

CN118778939BActive Publication Date: 2024-12-24SIWEI CHUANGZHI (BEIJING) TECH DEV CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411254216.0
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-09-09
Publication Date
2024-12-24
Estimated Expiration
2044-09-09

AI Technical Summary

Technical Problem

When large language models generate code, it is difficult to generate longer code due to context window limitations, resulting in limited input content and difficult to maintain context consistency and logical coherence.

Method used

By establishing a mapping relationship between the large language model and the source code, use the code description language to compress the source code into a code description language with fewer characters, and when generating the target source code, multiple target code description languages ​​are used as background Knowledge inputs to large language models to inject more source code information into a limited context window.

Benefits of technology

Injecting more compressed source code information into a limited context window improves the accuracy and relevance of code generated by large language models, ensures that the generated code matches user instructions, and improves the efficiency and quality of code generation.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118778939B_ABST
    Figure CN118778939B_ABST
Patent Text Reader

Abstract

The present invention relates to the field of artificial intelligence technology, and discloses a code generation method, device, computer equipment and storage medium, the method comprising: determining a code description language having a mapping relationship with a source code according to a large language model, the code description language including a code function description and a code writing description; storing the code description language into a database to generate a database containing multiple code description languages; obtaining user instructions; selecting multiple target code description languages ​​matching the user instructions from the database; using multiple target code description languages ​​as background knowledge, inputting the user instructions and the multiple target code description languages ​​into a large language model, and generating a target source code according to the output result of the large language model. The present invention uses a large language model to determine a code description language mapped with a source code, realizes compression of the source code, and subsequently uses the code description language to generate code, and can inject more compressed source code into a limited context window.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

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

[0002] Code generation is an important research direction in program automation, which aims to automatically generate code through artificial intelligence technology, thereby improving development efficiency and reducing development costs. From the initial template-based code generation, to rule-based code generation, to deep learning-based code generation, this field has made a series of important progress.

[0003] Large Language Model (LLM) is a natural language processing (NLP) model with large parameter scale, a large amount of training data, strong expressiveness and generalization ability, and can handle more complex code generation tasks. However, due to the context window limitation of large language models, the content input to large language models is limited, making it difficult to generate longer codes. Summary of the invention

[0004] In view of this, the present invention provides a code generation method, apparatus, computer device and storage medium to solve the problem that code generation is easily restricted by context windows.

[0005] In a first aspect, the present invention provides a code generation method, comprising:

[0006] Determine, according to the large language model, a code description language having a mapping relationship with the source code, wherein the code description language includes a code function description and a code writing description;

[0007] The code description language is stored in a database to generate a database containing multiple code description languages;

[0008] Obtaining user instructions; the user instructions are used to describe functional information of the target source code;

[0009] Selecting a plurality of target code description languages ​​matching the user instruction from the database; matching the code function description in the target code description language with the user instruction;

[0010] Taking the multiple target code description languages ​​as background knowledge, the user instructions and the multiple target code description languages ​​are input into the large language model, and the target source code is generated according to the output result of the large language model.

[0011] In some optional implementations, determining the code description language having a mapping relationship with the source code according to the large language model includes:

[0012] Generate original information including original source code;

[0013] Inputting the original information into the large language model to output a natural language description of the original source code; the natural language description includes a code function description and a code writing description of the original source code;

[0014] Inputting the natural language description into the large language model to restore the natural language description to the corresponding pending source code;

[0015] When the pending source code is consistent with the original source code data, the natural language description is used as a code description language having a mapping relationship with the original source code.

[0016] In some optional implementations, determining the code description language having a mapping relationship with the source code according to the large language model further includes:

[0017] In the case that the pending source code is inconsistent with the original source code data, determining the inconsistency information between the pending source code and the original source code;

[0018] The natural language description of the original source code is modified according to the inconsistency information until the modified natural language description can be restored by the large language model to a pending source code that is consistent with the original source code data.

[0019] In some optional implementations, the modifying the natural language description of the original source code according to the inconsistent information includes:

[0020] The original information and the inconsistent information are input into the large language model to output a revised natural language description.

[0021] In some optional implementations, generating original information including original source code includes:

[0022] Obtain descriptive information associated with the original source code;

[0023] The original source code and the description information are knowledge-merged to generate original information including the original source code and the description information.

[0024] In some optional implementations, the selecting from the database a plurality of target code description languages ​​that match the user instruction includes:

[0025] Matching the user instruction with the code function descriptions of each code description language in the database to determine the matching degree between the user instruction and the corresponding code function description;

[0026] The code description languages ​​corresponding to the multiple code function descriptions with the highest matching degree are used as the target code description languages.

[0027] In some optional implementations, the code description languages ​​corresponding to the multiple code function descriptions with the highest matching degree are used as the target code description language, including:

[0028] When the total length of the n code description languages ​​with the highest matching degree and the user instruction does not exceed the context window limit of the large language model, and the total length of the n+1 code description languages ​​with the highest matching degree and the user instruction exceeds the context window limit of the large language model, the n code description languages ​​with the highest matching degree, or the n+1 code description languages ​​with the highest matching degree are used as the target code description language; n≥2.

[0029] In a second aspect, the present invention provides a code generation device, comprising:

[0030] A description language determination module, used to determine a code description language having a mapping relationship with the source code according to the large language model, wherein the code description language includes a code function description and a code writing description;

[0031] A database module, used for storing the code description language into a database to generate a database containing multiple code description languages;

[0032] An instruction acquisition module, used to acquire user instructions; the user instructions are used to describe the functional information of the target source code;

[0033] A matching module, used to select a plurality of target code description languages ​​matching the user instruction from the database; the code function description in the target code description language matches the user instruction;

[0034] A code generation module is used to use the multiple target code description languages ​​as background knowledge, input the user instructions and the multiple target code description languages ​​into the large language model, and generate the target source code according to the output result of the large language model.

[0035] In a third aspect, the present invention provides a computer device, comprising: a memory and a processor, the memory and the processor being communicatively connected to each other, the memory storing computer instructions, and the processor executing the code generation method of the first aspect or any corresponding embodiment thereof by executing the computer instructions.

[0036] In a fourth aspect, the present invention provides a computer-readable storage medium having computer instructions stored thereon, the computer instructions being used to enable a computer to execute the code generation method of the first aspect or any corresponding embodiment thereof.

[0037] In a fifth aspect, the present invention provides a computer program product, comprising computer instructions for causing a computer to execute the code generation method of the first aspect or any corresponding embodiment thereof.

[0038] The present invention utilizes a large language model to determine the code description language mapped to the source code, thereby realizing compression of the source code; when generating the target source code, multiple target code description languages ​​are selected from these code description languages ​​and used as the background knowledge of the large language model, so that more compressed source code can be injected into a limited context window, thereby providing the large model with more content and more accurate context information, so that the large language model can generate more accurate target source code. BRIEF DESCRIPTION OF THE DRAWINGS

[0039] In order to more clearly illustrate the specific embodiments of the present invention or the technical solutions in the related technologies, the drawings required for use in the specific embodiments or the related technical descriptions will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0040] Figure 1 is a flowchart of a code generation method according to an embodiment of the present invention;

[0041] Figure 2 is a schematic diagram of a mapping relationship between source code and a code description language according to an embodiment of the present invention;

[0042] Figure 3 is a flowchart of another code generation method according to an embodiment of the present invention;

[0043] Figure 4 is a schematic diagram of a process of determining a code description language of a source code according to an embodiment of the present invention;

[0044] Figure 5 is a structural block diagram of a code generating device according to an embodiment of the present invention;

[0045] Figure 6 It is a schematic diagram of the hardware structure of a computer device according to an embodiment of the present invention. DETAILED DESCRIPTION

[0046] In order to make the purpose, technical solution and advantages of the embodiments of the present invention clearer, the technical solution in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.

[0047] Large language models have demonstrated strong expressiveness and generalization capabilities in the field of code generation, and can handle more complex code generation tasks. However, due to the context window limitations of large language models, the length of text that can be considered and understood at the same time is limited. For example, the context window size of a large language model is usually 4096 tokens (a token can be a word, punctuation mark, etc.). When the input text exceeds this length, the large language model may not be able to effectively maintain the consistency of the context, resulting in the generated code being out of touch with the previous input or logically incoherent.

[0048] When using a large language model for code generation, it is generally necessary to input part of the source code into the large language model so that the large language model can generate other source code required by the user according to the input source code. For example, similar source code can be injected into the large language model based on RAG (Retrieval Augmented Generation) technology to assist the large language model in code generation, thereby improving the quality of code generation. However, due to the large amount of source code content, it is easy to exceed the window limit of the large language model, resulting in difficulty in generating code.

[0049] An embodiment of the present invention provides a code generation method, which establishes a mutual mapping relationship between source code and code description language that can be recognized by a large language model. When generating target source code based on user instructions, the matching code description language is input into the large language model instead of the source code, so that more source code information can be injected into a limited context window, which helps the large language model generate more accurate source code.

[0050] According to an embodiment of the present invention, a code generation method embodiment is provided. It should be noted that the steps shown in the flowchart of the accompanying drawings can be executed in a computer system such as a set of computer executable instructions, and although a logical order is shown in the flowchart, in some cases, the steps shown or described can be executed in an order different from that shown here.

[0051] In this embodiment, a code generation method is provided. Figure 1 is a flow chart of a code generation method according to an embodiment of the present invention. Figure 1 As shown, the process includes the following steps.

[0052] Step S101 : determining a code description language having a mapping relationship with a source code according to a large language model, wherein the code description language includes a code function description and a code writing description.

[0053] In this embodiment, the source code is a code written in machine language, and the source code can be described in natural language. The source code description expressed in natural language is called "code description language". In addition, the source code and the code description language are mapped to each other, and a mapping relationship between the two can be established.

[0054] The code description language specifically includes "code function description" and "code writing description". As the name implies, the code function description is used to describe the function of the source code; the code writing description is used to describe the information required to write the source code, which may specifically include code steps, input and output parameters, etc. Figure 2 A schematic diagram showing the mapping relationship between source code and code description language is shown, and the content included in the code description language is schematically shown.

[0055] For example, if a source code calculates c=a+b and returns the result of a+b, that is, c, then the code function description of the source code can be: calculate the sum of two numbers; the code steps can be: pass in two parameters and calculate the sum of the two; the input and output parameters can be: input parameters a and b, and output parameter c. The code steps and input and output parameters are collectively referred to as code writing description.

[0056] In this embodiment, based on the preset large language model, a code description language having a mapping relationship with the source code is determined. Since the code description language is generated using the large language model, the large language model can identify the mapping relationship between the source code and the code description language. The source code and the code description language are mutually mapped, that is, the large language model can be used to generate a corresponding code description language based on the source code, and can also be used to generate a corresponding source code based on the code description language.

[0057] Step S102, storing the code description language into a database to generate a database containing multiple code description languages.

[0058] In this embodiment, by using the existing multiple source codes, the code description language corresponding to each source code can be determined according to the large language model, so that the mapping relationship between each source code and the corresponding code description language can be determined.

[0059] In addition, a database for storing code description languages ​​is pre-set. After the code description languages ​​of various source codes are determined using the large language model, each code description language can be entered into a database and stored in a preset database, thereby obtaining a database containing multiple code description languages.

[0060] Since the mapping relationship between source code and code description language is mainly used to represent the ability of the large language model to understand the relationship between source code and code description language, when generating code subsequently, the ability of the large language model to generate corresponding source code based on the code description language is mainly used. Therefore, only the code description language of each source code needs to be stored in the database, that is, the database may not include the source code itself.

[0061] Step S103, obtaining a user instruction; the user instruction is used to describe the functional information of the target source code.

[0062] In this embodiment, when the user needs to use the large language model to generate the required source code, the user can input a corresponding user instruction. For ease of description, the source code required by the user is referred to as the target source code.

[0063] Specifically, when generating a user instruction, the user can describe the functional information of the target source code in natural language, thereby obtaining a user instruction in natural language, and accordingly, the user instruction can describe the functional information of the target source code. For example, when generating a user instruction, one can try to describe one's own requirements in a format similar to the code functional description, thereby generating a corresponding user instruction.

[0064] For example, if the user currently needs a target source code capable of calculating the difference between two numbers, the following user instruction may be generated: calculate the difference between two numbers.

[0065] Step S104, multiple target code description languages ​​matching the user instructions are selected from the database; the code function description in the target code description language matches the user instructions.

[0066] In this embodiment, after obtaining the user instruction, a code description language matching the user instruction is selected from a preset database. For ease of description, the code description language matching the user instruction is referred to as a "target code description language"; and multiple target code description languages ​​need to be selected.

[0067] Specifically, since the user instruction is used to describe the functional information of the target source code, the user instruction can be regarded as a code functional description of the target source code; and the code functional description in each code description language in the database has basically described the functional information of the corresponding source code. Therefore, the target code description language that matches the user instruction, the source code mapped by it, also has a high correlation with the required target source code, and the target source code can be generated more accurately based on the target code description language later.

[0068] Step S105 , using multiple target code description languages ​​as background knowledge, inputting user instructions and the multiple target code description languages ​​into a large language model, and generating target source code according to the output result of the large language model.

[0069] In this embodiment, since the large language model can identify the mapping relationship between the code description language and the source code in the database, the code description language in the database and the mapped source code are equivalent to the large language model; since the code description language has fewer characters than the source code itself, the source code with longer characters can be compressed into a code description language with shorter characters; for example, for a source code of about 4,000 characters, the code description language generally does not exceed 1,000 characters, and the code description language can be used as a compressed source code, and there is basically no problem of data loss.

[0070] After selecting multiple target code description languages, code generation can be performed based on the large language model.

[0071] Specifically, in addition to inputting user instructions into the large language model, multiple selected target code description languages ​​are also input into the large language model as prompts. Specifically, these target code description languages ​​are input into the large language model as background knowledge; multiple target code description languages ​​as background knowledge can provide more accurate context information for the large language model, thereby improving the relevance and practicality of code suggestions.

[0072] Furthermore, as mentioned above, since the target code description language and the source code mapped thereto are equivalent to the large language model, inputting multiple target code description languages ​​into the large language model is equivalent to inputting the source code mapped thereto into the large language model. Since the code description language is a compressed source code, it can be used as a compressed prompt word. Therefore, under the limitation of the large language model context window, more target code description languages ​​can be input, so that the large language model can use more source code as background knowledge, thereby generating a target source code corresponding to the user instruction.

[0073] The code generation method provided in this embodiment uses a large language model to determine the code description language mapped to the source code to achieve source code compression; when generating the target source code, multiple target code description languages ​​are selected from these code description languages ​​and used as background knowledge of the large language model. More compressed source code can be injected into a limited context window, thereby providing the large model with more content and more accurate context information, so that the large language model can generate more accurate target source code.

[0074] In this embodiment, a code generation method is provided. Figure 3is a flow chart of a code generation method according to an embodiment of the present invention. Figure 3 As shown, the process includes the following steps.

[0075] Step S301 : determining a code description language having a mapping relationship with a source code according to a large language model, wherein the code description language includes a code function description and a code writing description.

[0076] Specifically, the above step S301 "determining a code description language having a mapping relationship with the source code according to the large language model" includes the following steps S3011 to S3014.

[0077] Step S3011, generating original information including original source code.

[0078] In this embodiment, when determining the code description language mapped to the existing source code based on the large language model, the existing source code is referred to as the "original source code", and original information including the original source code is generated, and the code description language mapped to the original source code is subsequently determined using the original information. For example, the original source code can be used as the original information, that is, the original information only includes the original source code.

[0079] Step S3012, inputting the original information into the large language model to output a natural language description of the original source code; the natural language description includes a code function description and a code writing description of the original source code.

[0080] In this embodiment, the original information is input into the large language model, and the large language model's understanding ability can be used to output the content describing the original source code in natural language. In this embodiment, the output of the large language model at this time is called "natural language description". Among them, the natural language description is also a code description language for the original source code. Accordingly, the natural language description also includes: code function description and code writing description. The code writing description can also ensure that the large language model outputs the correct source code (such as the target source code or the subsequent pending source code).

[0081] Optionally, the above step S3011 “generating original information including original source code” may include steps A1 to A2.

[0082] Step A1, obtaining description information related to the original source code.

[0083] Step A2: Merge the original source code and the description information to generate original information including the original source code and the description information.

[0084] In this embodiment, in order to accurately generate the code description language of the original source code, description information related to the original source code may be obtained, and the description information may be, for example, comments on the original source code, or an article introducing the original source code, etc. The original source code and the related description information are merged to generate the required original information, which specifically includes the original source code and the related description information.

[0085] In this embodiment, knowledge fusion can be achieved by merging the original source code and description information, which greatly enriches the semantic information of the original source code; when the merged original information is subsequently input into the large language model, the large language model can receive more knowledge related to the original source code, thereby enhancing the large language model's understanding of the internal logic and functions of the source code, improving the accuracy and comprehensiveness of natural language conversion, making the natural language description output by the large language model more accurate, and also helping to improve the accuracy of subsequent code understanding and application.

[0086] Step S3013, inputting the natural language description into the large language model to restore the natural language description to the corresponding pending source code.

[0087] Step S3014: when the pending source code is consistent with the original source code data, the natural language description is used as a code description language having a mapping relationship with the original source code.

[0088] In this embodiment, the above natural language description is a code description language output by the large language model. It may be inappropriate to directly use the natural language description as the code description language of the original source code, or there is no strict mapping relationship between the natural language description and the original source code. In this embodiment, a process of reverse verification of the natural language description is introduced.

[0089] Specifically, for the natural language description generated based on the large language model, it is used as the input of the large language model, and the code generation capability of the large language model is used to restore the natural language description to the corresponding source code. To facilitate the distinction of the description, the source code is called "pending source code."

[0090] For the pending source code generated by the large language model, determine whether the data is consistent with the original source code. For example, the text similarity of the pending source code and the original source code can be matched. If they are exactly the same, it is determined that the data of the two are consistent. If the pending source code is consistent with the original source code data, it means that the natural language description generated by the large language model can accurately represent the original source code, and there is a mutual mapping relationship between the two. Therefore, the natural language description can be used as a code description language with a mapping relationship with the original source code, and the natural language description is subsequently stored in the database.

[0091] In this embodiment, a large language model is used to implement a closed-loop verification process from source code to natural language description and then from natural language description back to source code, thereby ensuring the accuracy and reversibility of the natural language description and the consistency between the source code and the code description language. The code description language can be used to represent a lossless compression result of the source code.

[0092] In some optional implementations, the above step S301 "determining a code description language having a mapping relationship with the source code according to the large language model" may also include the following steps B1 to B2.

[0093] Step B1: when the pending source code is inconsistent with the original source code data, determine the inconsistency information between the pending source code and the original source code.

[0094] Step B2, correcting the natural language description of the original source code according to the inconsistent information, until the corrected natural language description can be restored by the large language model to the pending source code that is consistent with the original source code data.

[0095] In this embodiment, if the pending source code generated by the large language model is inconsistent with the original source code data, it means that the natural language description used to generate the pending source code cannot accurately describe the original source code, that is, there is no strict mapping relationship between the natural language description and the original source code. In this case, the inconsistency information between the pending source code and the original source code is determined, and the inconsistency information may specifically include the location where the inconsistency exists between the pending source code and the original source code, and the difference between the two.

[0096] Afterwards, the natural language description of the original source code is corrected using the inconsistency information to generate a corrected natural language description, and then the above step S3013 is re-executed to determine the pending source code corresponding to the corrected natural language description, and it is judged again whether the original source code and the regenerated pending source code are consistent in data; if the two are consistent, the corrected natural language description can be used as a code description language having a mapping relationship with the original source code; if the two are still inconsistent, continue to correct again using new inconsistency information until the corrected natural language description can be restored by the large language model to a pending source code that is consistent with the original source code data.

[0097] Optionally, the above step B2 of “correcting the natural language description of the original source code according to the inconsistent information” may include step B21.

[0098] Step B21, inputting the original information and inconsistent information into the large language model to output a corrected natural language description.

[0099] In this embodiment, the natural language description is regenerated using the large language model to achieve the correction of the natural language description. Specifically, after the inconsistent information is determined, the original information and the inconsistent information are both used as inputs of the large language model, so that the large language model can regenerate a more appropriate natural language description for the inconsistent information between the original source code and the pending source code, thereby making the new pending source code generated using the corrected natural language description as consistent as possible with the original source code data.

[0100] The pending source code itself can also be input into the large language model, that is, the original information, the pending source code and the inconsistent information are input into the large language model to generate a corrected natural language description. Since this process can be an offline generation process, it is not affected by the limitation of the context window of the large language model.

[0101] Figure 4 The process of determining the code description language of the source code is shown. Figure 4 As shown, the original source code A and the corresponding description information can be merged to generate original information containing the original source code A and the corresponding description information; then the original information is input into a large language model (LLM) to output a natural language description of the original source code A; then the natural language description is input into the large language model again to restore the natural language description to the corresponding source code, i.e., the pending source code B.

[0102] Afterwards, it is determined whether the original source code A and the pending source code B are consistent in data. If the two are consistent, the natural language description is directly used as the code description language of the original source code A and is stored in the database, that is, there is a mapping relationship between the natural language description and the original source code A.

[0103] If the two are inconsistent, then the inconsistency information between the original source code A and the pending source code B is generated, and the inconsistency information and the original information are input into the large language model to generate a new natural language description. Afterwards, the new natural language description is input into the large language model to generate a new pending source code (e.g., pending source code C); if the new pending source code (e.g., pending source code C) is consistent with the original source code A data, then it can be determined that there is a mapping relationship between the new natural language description and the original source code A. If the two are still inconsistent, then the consistency information between the new pending source code (e.g., pending source code C) and the original source code A is generated, and the above process is performed again until a natural language description having a mapping relationship with the original source code A is determined.

[0104] Step S302, storing the code description language into a database to generate a database containing multiple code description languages.

[0105] For details, please see Figure 1Step S102 of the illustrated embodiment will not be described in detail here.

[0106] Step S303, obtaining a user instruction; the user instruction is used to describe the functional information of the target source code.

[0107] For details, please see Figure 1 Step S103 of the illustrated embodiment will not be described in detail here.

[0108] Step S304, multiple target code description languages ​​matching the user instructions are selected from the database; the code function description in the target code description language matches the user instructions.

[0109] For details, please see Figure 1 Step S104 of the illustrated embodiment will not be described in detail here.

[0110] In some optional implementations, the above step S304 of "selecting multiple target code description languages ​​matching the user instruction from the database" may include the following steps C1 to C2.

[0111] Step C1, matching the user instruction with the code function description of each code description language in the database, and determining the matching degree between the user instruction and the corresponding code function description.

[0112] Step C2, using the code description language corresponding to the multiple code function descriptions with the highest matching degree as the target code description language.

[0113] In this embodiment, each code description language in the database has a corresponding code function description. After obtaining the user instruction, the matching degree between the user instruction and each code function description can be determined; for example, RAG matching can be performed on the user instruction and each code function description to determine the corresponding matching degree.

[0114] It can be understood that the higher the matching degree between the code function description and the user instruction, the closer the code description language corresponding to the code function description is to the target source code, so the code description language can be used as the target code description language for generating the target source code later.

[0115] The determined matching degrees can be sorted, and the top matching degrees are the highest matching degrees, and then the code description languages ​​corresponding to these matching degrees can be used as the target code description languages. For example, the number of target code description languages ​​required can be preset, such as 3, and the three code description languages ​​with the highest matching degrees can be used as the target code description languages.

[0116] In this embodiment, when generating code, only the code function description is used for matching, which can improve the matching efficiency of user instructions, reduce unnecessary consumption of computing resources, and also reduce the token loss of large model input, thereby improving the response speed and accuracy of the code generation process.

[0117] Optionally, the above step C2 of "using the code description language corresponding to the multiple code function descriptions with the highest matching degree as the target code description language" includes the following step C21.

[0118] Step C21, when the total length of the n code description languages ​​with the highest matching degree and the user instruction does not exceed the context window limit of the large language model, and the total length of the n+1 code description languages ​​with the highest matching degree and the user instruction exceeds the context window limit of the large language model, the n code description languages ​​with the highest matching degree or the n+1 code description languages ​​with the highest matching degree are used as target code description languages; n≥2. For example, n=2, 3, 4, etc.

[0119] In this embodiment, the number of target code description languages ​​required can be determined based on the length of the code description language. Specifically, after determining the matching degree between each code description language and the user instruction (i.e., the matching degree between the code function description and the user instruction), the position of the code description language that will cause the input to exceed the limit of the large language model context window can be determined.

[0120] Among them, if the total length of the n code description languages ​​and user instructions with the highest matching degree does not exceed the context window limit of the large language model, but the total length of the n+1 code description languages ​​and user instructions with the highest matching degree exceeds the context window limit of the large language model, it means that if the subsequent code description languages ​​with lower matching degrees (i.e., the n+2th and other code description languages) are also input into the large language model, under the context window limit, the large language model will not be able to remember the content input earlier; and the large language model cannot remember part of the content in the n+1th code description language.

[0121] Therefore, the n code description languages ​​with the highest matching degree can be used as the target code description language, so that the user instructions and the n target code description languages ​​can be completely injected into the large language model without exceeding the context window limit. Alternatively, in order to make full use of the context window of the large language model, the n+1 code description languages ​​with the highest matching degree can also be used as the target code description language, that is, the user instructions and the n+1 target code description languages ​​are used as inputs, so as to provide the large language model with as much relevant background knowledge as possible, and ensure the accuracy of the output target source code.

[0122] Step S305 , using multiple target code description languages ​​as background knowledge, inputting user instructions and multiple target code description languages ​​into a large language model, and generating target source code according to an output result of the large language model.

[0123] For details, please see Figure 1 Step S105 of the illustrated embodiment will not be described in detail here.

[0124] The code generation method provided in this embodiment uses a large language model to convert the collected source code into natural language, thereby compressing the source code into a code description language with a smaller data volume and a mapping relationship; using multiple code description languages ​​with the highest matching degree as background knowledge can provide richer context information for the large language model, help the large language model better understand the code knowledge, and help generate more accurate and relevant code. Injecting multiple compressed source code information into a limited context window effectively utilizes the input token of the large language model and improves the efficiency of information transmission. In addition, the reverse verification process from source code to natural language description and then to source code is adopted to ensure the accuracy and availability of the natural language description, greatly improving the credibility of the code description language.

[0125] In the present embodiment, a code generation device is also provided, and the device is used to implement the above-mentioned embodiment and preferred implementation mode, and the description that has been made will not be repeated. As used below, the term "module" can be a combination of software and / or hardware that implements a predetermined function. Although the device described in the following embodiments is preferably implemented with software, the implementation of hardware, or a combination of software and hardware is also possible and conceived.

[0126] This embodiment provides a code generation device, such as Figure 5 As shown, including:

[0127] A description language determination module 501 is used to determine a code description language having a mapping relationship with the source code according to the large language model, wherein the code description language includes a code function description and a code writing description;

[0128] A database module 502, used to store the code description language into a database and generate a database containing multiple code description languages;

[0129] The instruction acquisition module 503 is used to acquire user instructions; the user instructions are used to describe the functional information of the target source code;

[0130] A matching module 504 is used to select a plurality of target code description languages ​​that match the user instruction from the database; the code function description in the target code description language matches the user instruction;

[0131] The code generation module 505 is used to use the multiple target code description languages ​​as background knowledge, input the user instructions and the multiple target code description languages ​​into the large language model, and generate the target source code according to the output result of the large language model.

[0132] In some optional implementations, the description language determination module 501 determines the code description language having a mapping relationship with the source code according to the large language model, including:

[0133] Generate original information including original source code;

[0134] Inputting the original information into the large language model to output a natural language description of the original source code; the natural language description includes a code function description and a code writing description of the original source code;

[0135] Inputting the natural language description into the large language model to restore the natural language description to the corresponding pending source code;

[0136] When the pending source code is consistent with the original source code data, the natural language description is used as a code description language having a mapping relationship with the original source code.

[0137] In some optional implementations, the description language determination module 501 determines the code description language having a mapping relationship with the source code according to the large language model, and further includes:

[0138] In the case that the pending source code is inconsistent with the original source code data, determining the inconsistency information between the pending source code and the original source code;

[0139] The natural language description of the original source code is modified according to the inconsistency information until the modified natural language description can be restored by the large language model to a pending source code that is consistent with the original source code data.

[0140] In some optional implementations, the description language determination module 501 corrects the natural language description of the original source code according to the inconsistency information, including:

[0141] The original information and the inconsistent information are input into the large language model to output a revised natural language description.

[0142] In some optional implementations, the description language determination module 501 generates original information including original source code, including:

[0143] Obtain descriptive information associated with the original source code;

[0144] The original source code and the description information are knowledge-merged to generate original information including the original source code and the description information.

[0145] In some optional implementations, the matching module 504 selects a plurality of target code description languages ​​that match the user instruction from the database, including:

[0146] Matching the user instruction with the code function descriptions of each code description language in the database to determine the matching degree between the user instruction and the corresponding code function description;

[0147] The code description languages ​​corresponding to the multiple code function descriptions with the highest matching degree are used as the target code description languages.

[0148] In some optional implementations, the matching module 504 uses the code description languages ​​corresponding to the multiple code function descriptions with the highest matching degree as the target code description language, including:

[0149] When the total length of the n code description languages ​​with the highest matching degree and the user instruction does not exceed the context window limit of the large language model, and the total length of the n+1 code description languages ​​with the highest matching degree and the user instruction exceeds the context window limit of the large language model, the n code description languages ​​with the highest matching degree, or the n+1 code description languages ​​with the highest matching degree are used as the target code description language; n≥2.

[0150] The further functional description of each of the above modules and units is the same as that of the above corresponding embodiments and will not be repeated here.

[0151] The code generation device in this embodiment is presented in the form of a functional unit, where the unit refers to an ASIC (Application Specific Integrated Circuit) circuit, including a processor and memory that executes one or more software or fixed programs, and / or other devices that can provide the above functions.

[0152] The embodiment of the present invention also provides a computer device having the above Figure 5 The code generating device shown.

[0153] See also Figure 6 , Figure 6 is a schematic diagram of the structure of a computer device provided by an optional embodiment of the present invention, such as Figure 6As shown, the computer device includes: one or more processors 10, a memory 20, and interfaces for connecting various components, including high-speed interfaces and low-speed interfaces. Various components are connected to each other using different buses for communication, and can be installed on a common mainboard or installed in other ways as needed. The processor can process instructions executed in the computer device, including instructions stored in or on the memory to display the graphical information of the GUI on an external input / output device (such as a display device coupled to the interface). In some optional embodiments, if necessary, multiple processors and / or multiple buses can be used together with multiple memories. Similarly, multiple computer devices can be connected, and each device provides some necessary operations (for example, as a server array, a group of blade servers, or a multi-processor system). Figure 6 A processor 10 is taken as an example.

[0154] The processor 10 may be a central processing unit, a network processor or a combination thereof. The processor 10 may further include a hardware chip. The hardware chip may be a dedicated integrated circuit, a programmable logic device or a combination thereof. The programmable logic device may be a complex programmable logic device, a field programmable gate array, a general purpose array logic or any combination thereof.

[0155] The memory 20 stores instructions executable by at least one processor 10, so that the at least one processor 10 executes the method shown in the above embodiment.

[0156] The memory 20 may include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function; the data storage area may store data created according to the use of the computer device, etc. In addition, the memory 20 may include a high-speed random access memory, and may also include a non-transient memory, such as at least one disk storage device, a flash memory device, or other non-transient solid-state storage device. In some optional embodiments, the memory 20 may optionally include a memory remotely arranged relative to the processor 10, and these remote memories may be connected to the computer device via a network. Examples of the above-mentioned network include, but are not limited to, the Internet, an intranet, a local area network, a mobile communication network, and combinations thereof.

[0157] The memory 20 may include a volatile memory, such as a random access memory; the memory may also include a non-volatile memory, such as a flash memory, a hard disk or a solid state drive; the memory 20 may also include a combination of the above types of memory.

[0158] The computer device also includes an input device 30 and an output device 40. The processor 10, the memory 20, the input device 30 and the output device 40 may be connected via a bus or other means. Figure 6 The example of connecting through bus is taken in the following.

[0159] The input device 30 can receive input digital or character information, and generate key signal input related to the user settings and function control of the computer device, such as a touch screen, a keypad, a mouse, a track pad, a touch pad, an indicator bar, one or more mouse buttons, a trackball, a joystick, etc. The output device 40 may include a display device, an auxiliary lighting device (e.g., an LED) and a tactile feedback device (e.g., a vibration motor), etc. The above-mentioned display device includes but is not limited to a liquid crystal display, a light emitting diode, a display and a plasma display. In some optional embodiments, the display device may be a touch screen.

[0160] The embodiment of the present invention also provides a computer-readable storage medium. The method according to the embodiment of the present invention can be implemented in hardware, firmware, or can be implemented as a computer code that can be recorded in a storage medium, or can be implemented as a computer code that is originally stored in a remote storage medium or a non-temporary machine-readable storage medium and will be stored in a local storage medium through a network download, so that the method described herein can be stored in such software processing on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. Among them, the storage medium can be a magnetic disk, an optical disk, a read-only storage memory, a random access memory, a flash memory, a hard disk or a solid-state hard disk, etc.; further, the storage medium can also include a combination of the above types of memories. It can be understood that a computer, a processor, a microprocessor controller, or programmable hardware includes a storage component that can store or receive software or computer code. When the software or computer code is accessed and executed by a computer, a processor, or hardware, the method shown in the above embodiment is implemented.

[0161] A part of the present invention may be applied as a computer program product, such as a computer program instruction, which, when executed by a computer, can call or provide the method and / or technical solution according to the present invention through the operation of the computer. Those skilled in the art should understand that the existence of the computer program instruction in a computer-readable medium includes, but is not limited to, a source file, an executable file, an installation package file, etc., and accordingly, the way in which the computer program instruction is executed by the computer includes, but is not limited to: the computer directly executes the instruction, or the computer compiles the instruction and then executes the corresponding compiled program, or the computer reads and executes the instruction, or the computer reads and installs the instruction and then executes the corresponding installed program. Here, the computer-readable medium may be any available computer-readable storage medium or communication medium accessible to the computer.

[0162] Although the embodiments of the present invention have been described in conjunction with the accompanying drawings, those skilled in the art may make various modifications and variations without departing from the spirit and scope of the present invention, and such modifications and variations are all within the scope defined by the appended claims.

Claims

1. A code generation method, characterized in that: The method comprises: Determine a code description language having a mutual mapping relationship with the source code according to the large language model, wherein the code description language includes a code function description and a code writing description; the code writing description includes code steps and input and output parameters; The code description language is stored in a database to generate a database containing multiple code description languages; Obtaining user instructions; the user instructions are used to describe functional information of the target source code; Selecting a plurality of target code description languages ​​matching the user instruction from the database; matching the code function description in the target code description language with the user instruction; Using the multiple target code description languages ​​as background knowledge, inputting the user instructions and the multiple target code description languages ​​into the large language model, and generating the target source code according to the output result of the large language model; The step of determining the code description language having a mapping relationship with the source code according to the large language model includes: Generate original information including original source code; Inputting the original information into the large language model to output a natural language description of the original source code; the natural language description includes a code function description and a code writing description of the original source code; Inputting the natural language description into the large language model to restore the natural language description to the corresponding pending source code; In the case that the pending source code is inconsistent with the original source code data, determining the inconsistency information between the pending source code and the original source code; Correcting the natural language description of the original source code according to the inconsistency information until the corrected natural language description can be restored by the large language model to a pending source code that is consistent with the original source code data; When the pending source code is consistent with the original source code data, the natural language description is used as a code description language having a mapping relationship with the original source code.

2. The method according to claim 1, characterized in that The step of correcting the natural language description of the original source code according to the inconsistent information includes: The original information and the inconsistent information are input into the large language model to output a revised natural language description.

3. The method according to claim 1, characterized in that The generation includes original information of the original source code, including: Obtain descriptive information associated with the original source code; The original source code and the description information are knowledge-merged to generate original information including the original source code and the description information.

4. The method according to claim 1, characterized in that: The step of selecting a plurality of target code description languages ​​matching the user instruction from the database comprises: Matching the user instruction with the code function descriptions of each code description language in the database to determine the matching degree between the user instruction and the corresponding code function description; The code description languages ​​corresponding to the multiple code function descriptions with the highest matching degree are used as the target code description languages.

5. The method according to claim 4, characterized in that The code description language corresponding to the multiple code function descriptions with the highest matching degree is used as the target code description language, including: When the total length of the n code description languages ​​with the highest matching degree and the user instruction does not exceed the context window limit of the large language model, and the total length of the n+1 code description languages ​​with the highest matching degree and the user instruction exceeds the context window limit of the large language model, the n code description languages ​​with the highest matching degree, or the n+1 code description languages ​​with the highest matching degree are used as the target code description language; n≥2.

6. A code generating device, characterized in that: The device comprises: A description language determination module is used to determine a code description language having a mutual mapping relationship with the source code according to the large language model, wherein the code description language includes a code function description and a code writing description; the code writing description includes code steps and input and output parameters; A database module, used for storing the code description language into a database to generate a database containing multiple code description languages; An instruction acquisition module, used to acquire user instructions; the user instructions are used to describe the functional information of the target source code; A matching module, used to select a plurality of target code description languages ​​matching the user instruction from the database; the code function description in the target code description language matches the user instruction; A code generation module, configured to use the multiple target code description languages ​​as background knowledge, input the user instructions and the multiple target code description languages ​​into the large language model, and generate the target source code according to an output result of the large language model; The description language determination module determines the code description language having a mapping relationship with the source code according to the large language model, including: Generate original information including original source code; Inputting the original information into the large language model to output a natural language description of the original source code; the natural language description includes a code function description and a code writing description of the original source code; Inputting the natural language description into the large language model to restore the natural language description to the corresponding pending source code; In the case that the pending source code is inconsistent with the original source code data, determining the inconsistency information between the pending source code and the original source code; Correcting the natural language description of the original source code according to the inconsistency information until the corrected natural language description can be restored by the large language model to a pending source code that is consistent with the original source code data; When the pending source code is consistent with the original source code data, the natural language description is used as a code description language having a mapping relationship with the original source code.

7. A computer device, characterized in that: include: A memory and a processor, wherein the memory and the processor are communicatively connected to each other, the memory stores computer instructions, and the processor executes the code generation method according to any one of claims 1 to 5 by executing the computer instructions.

8. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer instructions, and the computer instructions are used to enable a computer to execute the code generation method according to any one of claims 1 to 5.