Code completion method and device, equipment and medium

Through causal inference technology and backdoor adjustment technology, the code context and execution environment are adjusted, and the problem of insufficient completion code generated in the existing technology is solved, achieving higher accuracy and usability of code completion effects.

CN119440535APending Publication Date: 2025-02-14SUN YAT SEN UNIV

Patent Information

Application Number
CN202411420687.4
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-10-12
Publication Date
2025-02-14

AI Technical Summary

Technical Problem

The complete code generated by the existing code completion technology is not accurate and perfect enough to effectively make up for the lack of logic.

Method used

The causal inference technology is used combined with backdoor adjustment technology, and by adjusting the context information and execution environment, the code executor is used to guide the generation of more accurate completion code based on prompts.

Benefits of technology

Improves the accuracy and usability of generated complete code, reduces maintenance costs, and only requires maintenance of a small number of prompts.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119440535A_ABST
    Figure CN119440535A_ABST
Patent Text Reader

Abstract

The invention relates to the technical field of code completion, and discloses a code completion method, device and equipment and a medium, the method comprises the steps that a to-be-completed code is generated based on a first prompt word, the first prompt word is generated by formatting initial context information through a prompt word constructor, and the initial context information is used for representing a function requirement of the to-be-completed code; testing the to-be-complemented code by using a code executor, recording error report information in a testing process, and reserving a correct code in the testing process; generating an intermediate code based on a second cue word, wherein the second cue word is generated by formatting the intermediate context information through the cue word constructor; according to the technical scheme, the code executor is used for repeatedly testing the intermediate codes on the basis of the causal inference technology until the generated intermediate codes are all correct codes, the generated intermediate codes serve as the completion codes correspondingly, and the technical effect that the generated completion codes are high in precision is achieved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the technical field of code completion, and in particular to a code completion method, device, equipment and medium. Background Art

[0002] With the rapid development of machine learning and artificial intelligence technology, the application of code completion is becoming more and more extensive. In the related technology, code completion is performed in the following way: a syntax tree is constructed according to the syntax rules, and the corresponding missing syntax is matched on the basis of the syntax tree to complete the code. This method is relatively limited and only makes up for the missing syntax, but not the missing logic. The generated completion code is not accurate and perfect enough. Summary of the invention

[0003] The present application provides a code completion method, device, equipment and medium, which solves the technical problem that the completion code generated in the related art is not accurate and perfect enough, and achieves the technical effect of high accuracy of the generated completion code.

[0004] In order to achieve the above objectives, the main technical solutions adopted in this application include:

[0005] In a first aspect, an embodiment of the present application provides a code completion method, the method comprising:

[0006] Generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information by a prompt constructor, and the initial context information is used to represent a functional requirement of the code to be completed;

[0007] Using a code executor to test the code to be completed, and recording error information during the test, while retaining the correct code during the test;

[0008] Generate an intermediate code based on a second prompt, wherein the second prompt is generated by the prompt builder by formatting intermediate context information, and the intermediate context information includes the error message and the correct code;

[0009] The code executor is used to repeatedly test the intermediate code based on causal inference technology until the generated intermediate codes are all correct codes, and the generated intermediate codes are used as completion codes accordingly.

[0010] This application uses causal inference technology for code completion, specifically based on the backdoor adjustment technology in causal inference, by adjusting context information, to complete more accurate results based on prompt guidance, and the maintenance cost is low, only a small number of prompts need to be maintained. At the same time, this application also proposes a feedback mechanism for the code executor. Once some codes can be executed correctly, these correct codes are retained, and these correct codes are filled in in the subsequent prompt update, so that the remaining codes can be continuously optimized and completed on this basis to improve the accuracy of the generated completion code.

[0011] Optionally, the method further includes: adjusting the code executor.

[0012] Optionally, the code executor is adjusted in one of the following ways:

[0013] Adding a code framework to the code executor, wherein the code framework is used to assist the code executor in running the code;

[0014] Importing a basic library into the code executor, so that the code executor runs the code by calling the basic library.

[0015] In this way, the execution environment is adjusted by at least one of the above code framework and basic library methods. By adjusting the execution environment, the execution chain is adjusted to obtain the code result. The final code result is affected by the code context and the execution environment. Based on the backdoor adjustment technology in the causal inference technology, the execution chain factors are restricted, the code context and the execution environment are paid attention to, and the code context and the execution environment are continuously adjusted to obtain the final correct code result.

[0016] Optionally, the initial context information includes function requirement information and input and output example information.

[0017] Optionally, the first prompt is generated by a prompt constructor formatting task description information and initial context information, and the format of the first prompt is set based on the code executor; wherein the task description information is obtained by collection.

[0018] Optionally, generating the code to be completed based on the first prompt includes: generating the code to be completed based on the first prompt using a large model;

[0019] The generating of the intermediate code based on the second prompt includes: generating the intermediate code based on the second prompt using a large model.

[0020] This application uses a large model to generate completion code, which can adapt to the code completion needs of multiple languages, effectively reduce the error rate of preliminary code corrections in the early stage, and ensure the availability of subsequent codes.

[0021] Optionally, the error information includes syntax error information, function call missing information, variable name undefined information, and specific line number and column number information of the error. In this way, the code executor records the error information so that the subsequent large model can correct the specific code fragment in time according to the error information.

[0022] In a second aspect, an embodiment of the present application provides a code completion device, the device comprising a first generation module, a test module, a first generation module and a completion module;

[0023] A first generating module, configured to generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information by a prompt constructor, and the initial context information is used to represent a functional requirement of the code to be completed;

[0024] A test module, used to test the code to be completed using a code executor, and record error information during the test, while retaining the correct code during the test;

[0025] A second generating module, configured to generate an intermediate code based on a second prompt, wherein the second prompt is generated by the prompt constructor formatting intermediate context information, wherein the intermediate context information includes the error message and the correct code;

[0026] The completion module is used to use the code executor to repeatedly test the intermediate code based on causal inference technology until the generated intermediate codes are all correct codes, and accordingly use the generated intermediate codes as completion codes.

[0027] In a third aspect, an embodiment of the present application provides a computer device, including:

[0028] 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 any one of the above-mentioned code completion methods by executing the computer instructions.

[0029] In a fourth aspect, an embodiment of the present application provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute any one of the above-mentioned code completion methods. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] In order to more clearly illustrate the specific implementation methods of the present application or the technical solutions in the prior art, the drawings required for use in the specific implementation methods or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some implementation methods of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative work.

[0031] Figure 1 A schematic diagram of the steps of a code completion method provided in an embodiment of the present application;

[0032] Figure 2 A schematic diagram of variables based on the backdoor adjustment technology provided in an embodiment of the present application;

[0033] Figure 3 A flowchart of a code completion method provided in an embodiment of the present application. DETAILED DESCRIPTION

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

[0035] With the rapid development of machine learning and artificial intelligence technology, the application of code completion is becoming more and more extensive. In the related technology, code completion is performed in the following way: a syntax tree is constructed according to the syntax rules, and the corresponding missing syntax is matched on the basis of the syntax tree to complete the code. This method is relatively limited and only makes up for the missing syntax, but not the missing logic. The generated completion code is not accurate and perfect enough.

[0036] Alternatively, code completion can be performed in the following ways: using a neural network to train a model based on a specific data set, and using the trained model to predict missing code snippets. This method can make up for the lack of logic to a certain extent, but the training set needs to be very rich and adapted to the corresponding programming scenarios, and the maintenance cost is high.

[0037] Reference Figure 1 As an implementation method, the present application embodiment provides a code completion method, the method comprising:

[0038] S100: Generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information through a prompt constructor, and the initial context information is used to represent functional requirements of the code to be completed.

[0039] The code to be completed may be the code preliminarily generated in this application based on the required functional requirement information, at which point some of the logic or implemented functions in the code have not yet been completed; the first prompt may include the intended function and purpose of the code to be completed, the type of result required to be returned, and details or other requirements that require special attention when implementing the code; the functional requirements may be data cleaning and conversion, implementation of mathematical calculations such as summation and maximum value, or writing algorithm path planning and image processing for specific tasks.

[0040] The prompt builder formats the initial context information to generate the first prompt. The method proposed in this embodiment may also include: performing code interpretation, error tracking and query combination processing on the initial context information. Code interpretation may be line-by-line analysis of the code to understand its functional requirements and logic; error tracking may be finding the error through abnormal context information or error prompts; query combination may be operations such as merging or associating multiple context information.

[0041] S200, using a code executor to test the code to be completed, and recording error information during the test, while retaining the correct code during the test.

[0042] Among them, the code executor is used to ensure that the code generated in the above steps is executable and will not report errors when running in the actual environment. Specifically, building a general code executor that can execute multiple programming languages ​​can be constructed through a large model, a small model, or a specific code compiler. Large models such as the GPT series of deep learning models and small models such as CodeBERT are optimized for specific tasks and focus on code understanding and generation. Compared with large models, they have faster response speeds and lower resource usage. Code compilers such as Java's jdk8 can verify the grammatical correctness of Java; such as the Lua interpreter, an efficient lightweight scripting language executor for game development and embedded systems.

[0043] Specifically, you can use a code executor, such as Jupyter Notebook, to test the generated code to be completed. You can maintain a log file to record error information during the test and retain the correct code during the test. On the basis of the retained correct code, you can gradually correct the code to be completed according to the recorded error information.

[0044] S300: Generate an intermediate code based on a second prompt, wherein the second prompt is generated by formatting intermediate context information through a prompt constructor, and the intermediate context information includes error information and a correct code.

[0045] Among them, the subsequent intermediate code can be the code further generated in this application based on the above-mentioned preliminary generated code, and the error information can be one or more of input verification error, data type error, logic error or network request error; the correct code can be the code that has passed the test of all test cases and can work normally under various conditions.

[0046] The prompt builder formats the intermediate context information to generate the second prompt, and further includes: re-arranging, counting and fusing the error information and correct code in the intermediate context information. The re-arrangement may be to classify the error information and correct code by type, such as syntax, runtime or logic; the counting operation may be to analyze the quantity and frequency of the error information and count the number of occurrences of different types of error information; the fusing operation may be to combine the error information with the correct code to form an effective repair or optimization strategy to improve the next code generation process.

[0047] S400, repeatedly testing the intermediate code based on causal inference technology using a code executor until all generated intermediate codes are correct codes, and correspondingly using the generated intermediate codes as completion codes.

[0048] Specifically, this embodiment provides a piece of code. Assuming that the functional requirement is to multiply each element by 2, the code is as follows:

[0049]

[0050] Based on the test feedback from the code executor, analyze which part of the intermediate code caused the error, use the causal inference method to find the relationship between the input and output, and find out the part that needs to be completed or modified and complete it.

[0051] Test again based on the code executor and get the correct code without any error prompts.

[0052] Specifically, the intermediate code is repeatedly tested based on the backdoor adjustment technology in causal inference technology. Backdoor adjustment is a technology used to control confounding variables and thus identify causal effects. The core idea is to eliminate the interference of potential confounding factors on causal relationships by adjusting them.

[0053] Reference Figure 2 , a backdoor path can be a path from a processing variable (e.g., X) to an outcome variable (e.g., Y) in a causal graph, which is not direct but connected through other variables (confounding variables). Confounding variables can affect X and Y, causing the association between the two to not reflect the true causal relationship. Further, X can be the code context, i.e., the intermediate context information mentioned, and Y can be the code result.

[0054] Furthermore, the embodiment of the present application adjusts the code context and then adjusts the execution chain to obtain the code result by keeping other factors unchanged. Specifically, the code context is adjusted from the first prompt to the second prompt and then adjusts the execution chain.

[0055] This application uses a causal inference method, which is more accurate than the methods in related technologies. This application generates the code to be completed based on the first prompt, and the code to be completed is judged by the code executor to analyze and test which part of the code is wrong. Taking the number of function test cases as an example, only 10 out of 100 examples passed, and the application of this method increased to 30 examples, which improved the accuracy and precision.

[0056] This application uses causal inference technology for code completion, specifically based on the backdoor adjustment technology in causal inference, by adjusting context information to complete more accurate results based on prompt guidance, and has low maintenance costs, only requiring maintenance of a small number of prompts.

[0057] At the same time, the present application also proposes a feedback mechanism for the code executor. Once some codes can be executed correctly, these correct codes are retained and filled in when the subsequent prompts are updated. This overcomes the problem in the related art that only a single evaluation is made on the code completion results without improving from the perspective of the results. The present application continues to optimize and complete the remaining codes based on the updated subsequent prompts, thereby improving the accuracy of the generated completion code.

[0058] As an implementation method, refer to Figure 3 , a code completion method, comprising:

[0059] S010. The prompt constructor generates a first prompt based on the functional requirements of the code to be completed, and the first prompt is input into the large model. The large model outputs the code to be completed, and the output code to be completed is input into the code executor. The code executor tests the code to be completed, records the correct code, and returns the error information to the prompt constructor.

[0060] S020. The prompt constructor generates a second prompt based on the functional requirements of the code to be completed and the error message, that is, a new prompt different from the first prompt, and the second prompt is input into the large model. The large model outputs the first intermediate code, and the output first intermediate code is input into the code executor. The code executor tests the first intermediate code, records the correct code therein, and determines whether there is an error message in the first intermediate code, that is, whether the first intermediate code is all correct code. If there is an error message or not all correct code, the error message is returned and input into the prompt constructor.

[0061] S030. The prompt constructor generates a third prompt based on the functional requirements of the code to be completed and the error message of the first intermediate code, that is, a new prompt different from the first prompt and the second prompt, and the third prompt is input into the large model. The large model outputs the second intermediate code, and the output second intermediate code is input into the code executor. The code executor tests the second intermediate code, records the correct code therein, and judges whether there is an error message in the second intermediate code, that is, whether the second intermediate code is all correct code. If there is an error message or not all correct code, it returns to repeat step S020; if there is no error message or all are correct code, the second intermediate code is regarded as the completion code to obtain the completion code.

[0062] As an implementation manner, the method may further include: adjusting the code executor.

[0063] As an implementation, the code executor is adjusted in one of the following ways:

[0064] Add a code framework to the code executor. The code framework is used to assist the code executor in running the code.

[0065] Import the basic library into the code executor so that the code executor runs the code by calling the basic library, wherein the basic library may be a basic system library that needs to be called in the context of the entire code file. Importing the basic library into the code executor is to ensure that the system call of the code to be completed can run normally.

[0066] Reference Figure 3 The embodiment of the present application adjusts the execution environment and then adjusts the execution chain to obtain the code result while other factors remain unchanged. The execution environment is adjusted by at least one of the above-mentioned code framework and basic library methods.

[0067] It is understandable that by adjusting the code context and execution environment at the same time, and then adjusting the execution chain to obtain the code result, the code context focuses on the data and state when the code is executed, and the execution environment focuses on the external conditions and support required for the code to run. The final code result is affected by the code context and the execution environment. Based on the backdoor adjustment technology in causal inference technology, the execution chain factors are restricted, the code context and the execution environment are paid attention to, and the code context and the execution environment are continuously adjusted to obtain the final correct code result.

[0068] As an implementation manner, the initial context information includes function requirement information and input and output example information.

[0069] As an implementation mode, the first prompt is generated by a prompt constructor formatting task description information and initial context information, and the format of the first prompt is set based on the code executor; wherein the task description information is obtained by collection.

[0070] As an implementation mode, generating the code to be completed based on the first prompt includes: generating the code to be completed based on the first prompt using a large model;

[0071] Generating an intermediate code based on the second prompt includes: generating an intermediate code based on the second prompt using a large model.

[0072] Among them, the large model can be the GPT series, which handles natural language tasks such as text generation and dialogue systems; it can be BERT, which processes contextual information and can perform question answering and text classification; it can also be Vision Transformers, which converts image processing into sequence problems and uses the Transformer architecture to process visual data.

[0073] The related technologies are based on specific scenarios or construct fixed data sets to deal with single scenarios, which are not very universal: they cannot be converted when switching usage scenarios; the maintenance cost is high: because the data set is relatively simple, it needs to be updated frequently when switching usage scenarios. However, this application uses a large model to generate completion code, which can adapt to the code completion needs of multiple languages, and can effectively reduce the error rate of preliminary code corrections in the early stage, ensuring the availability of subsequent code.

[0074] As an implementation method, the error information includes syntax error information, function call missing information, variable name undefined information, and specific line and column number information of the error. The code executor records the error information so that the subsequent large model can correct the specific code snippet in time according to the error information.

[0075] Specifically, the embodiment of the present application provides a code snippet that requires code completion:

[0076] function getTheMaxNumber(){

[0077] [insert code]

[0078] …

[0079] You need to complete this code and replace the completed code with [inset code].

[0080] The following are some incorrect examples and error messages, which we hope to avoid in subsequent completion steps:

[0081] errorMessage:a is not undefined.

[0082] errorCode:

[0083] …

[0084] return a;

[0085] …

[0086] Specifically, provide an immediate class name, keyword, method name or method body task, complete the writing of this function through the method of this application, and provide a code snippet that needs code completion:

[0087] Complete the missing parts of the following code and output the result with the original code.

[0088] …

[0089] ##Please output the input in reverse order.

[0090] ##input[1,2,3]

[0091] ##output[3,2,1]

[0092] def reorder(arrs):

[0093] …

[0094] The method provided in the embodiment of the present application uses a large model for code completion, which is highly versatile and scalable, and can adapt to the code completion needs of multiple languages, multiple scenarios, and multiple fields, and can effectively reduce the error rate of preliminary code corrections in the early stage to ensure the availability of subsequent codes. The causal inference technology is used for code completion with high accuracy, specifically based on the backdoor adjustment technology in causal inference, by adjusting the context information to complete more accurate results based on prompt guidance, and has good application prospects.

[0095] As an implementation mode, an embodiment of the present application provides a code completion device, the device includes a first generation module, a test module, a first generation module and a completion module;

[0096] A first generating module, configured to generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information by a prompt constructor, and the initial context information is used to represent a functional requirement of the code to be completed;

[0097] The test module is used to test the code to be completed using the code executor, record the error information during the test, and retain the correct code during the test;

[0098] A second generating module, configured to generate an intermediate code based on a second prompt, wherein the second prompt is generated by formatting the intermediate context information through a prompt constructor, and the intermediate context information includes an error message and a correct code;

[0099] The completion module is used to use the code executor to repeatedly test the intermediate code based on the causal inference technology until the generated intermediate code is all correct code, and accordingly use the generated intermediate code as the completion code.

[0100] As an implementation mode, the test module may further include: adjusting the code executor.

[0101] As an implementation, in the test module, adjust the code executor in one of the following ways:

[0102] Add a code framework to the code executor. The code framework is used to assist the code executor in running the code.

[0103] Import the basic library into the code executor so that the code executor runs the code by calling the basic library.

[0104] As an implementation manner, in the first generation module, the initial context information includes function requirement information and input and output example information.

[0105] As an implementation mode, in the first generation module, the first prompt is generated by the prompt constructor formatting the task description information and the initial context information, and the format of the first prompt is set based on the code executor; wherein the task description information is obtained by collection.

[0106] As an implementation mode, the first generation module may further include: using the large model to generate the code to be completed based on the first prompt;

[0107] The second generation module may further include: generating an intermediate code based on the second prompt using the large model.

[0108] As an implementation method, the error information includes syntax error information, function call missing information, variable name undefined information, and specific line number and column number information of the error.

[0109] The present application provides a computer device, comprising: 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 a code completion method provided in the above embodiment by executing the computer instructions.

[0110] The computer device includes: one or more processors, memory, 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 the 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 and 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).

[0111] The processor may be a central processing unit, a network processor or a combination thereof. The processor 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.

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

[0113] The memory 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 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 storage may optionally include a memory remotely arranged relative to the processor, 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.

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

[0115] The computer device also includes a communication interface, which is used for the computer device to communicate with other devices or a communication network.

[0116] The present application provides a computer-readable storage medium, on which computer instructions are stored, and the computer instructions are used to enable a computer to execute a code completion method provided in the above embodiment.

[0117] The embodiment of the present application also provides a computer-readable storage medium. The above method according to the embodiment of the present application 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.

[0118] The embodiment of the present application provides a computer program product, which includes computer instructions, which are stored in a computer-readable storage medium. A processor of a computer device reads the computer instructions from the computer-readable storage medium, and the processor executes the computer instructions, so that the computer device executes the method of any embodiment of the present application.

[0119] Although the embodiments of the present application are 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 application, and such modifications and variations are all within the scope defined by the appended claims.

[0120] The systems, devices, modules or units described in the above embodiments may be implemented by computer chips or entities, or by products with certain functions. A typical implementation device is a computer. Specifically, the computer may be, for example, a personal computer, a laptop computer, a cellular phone, a camera phone, a smart phone, a personal digital assistant, a media player, a navigation device, an email device, a game console, a tablet computer, a wearable device, or a combination of any of these devices.

[0121] For the convenience of description, the above device is described in various units according to their functions. Of course, when implementing the present application, the functions of each unit can be implemented in the same or multiple software and / or hardware.

[0122] Those skilled in the art will appreciate that the embodiments of the present application may be provided as methods, systems, or computer program products. Therefore, the present application may adopt the form of a complete hardware embodiment, a complete software embodiment, or an embodiment in combination with software and hardware. Moreover, the present application may adopt the form of a computer program product implemented in one or more computer-usable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) that include computer-usable program code.

[0123] The present application is described with reference to the flowcharts and / or block diagrams of the methods, devices (systems), and computer program products according to the embodiments of the present application. It should be understood that each process and / or box in the flowchart and / or block diagram, as well as the combination of the processes and / or boxes in the flowchart and / or block diagram, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, a special-purpose computer, an embedded processor, or other programmable data processing device to generate a machine, so that the instructions executed by the processor of the computer or other programmable data processing device generate instructions for implementing the processes in the flowchart and / or block diagram. Figure 1 A process or multiple processes and / or boxes Figure 1 A device that provides the functions specified in a block or multiple blocks.

[0124] These computer program instructions may also be stored in a computer-readable memory capable of directing a computer or other programmable data processing device to operate in a specific manner, so that the instructions stored in the computer-readable memory produce an article of manufacture comprising an instruction device, which implements the process Figure 1 A process or multiple processes and / or boxes Figure 1 A function specified in one or more boxes.

[0125] These computer program instructions can also be loaded onto a computer or other programmable data processing device so that a series of operating steps are executed on the computer or other programmable device to produce a computer-implemented process, thereby providing instructions for implementing the process. Figure 1 A process or multiple processes and / or boxes Figure 1 A step that specifies a function in one or more boxes.

[0126] It should also be noted that the terms "include", "comprises" or any other variations thereof are intended to cover non-exclusive inclusion, so that a process, method, commodity or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, commodity or device. In the absence of more restrictions, the elements defined by the sentence "comprises a ..." do not exclude the existence of other identical elements in the process, method, commodity or device including the elements.

[0127] Each embodiment in this specification is described in a progressive manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.

[0128] The above is only an embodiment of the present application and is not intended to limit the present application. For those skilled in the art, the present application may have various changes and variations. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application should be included in the scope of the claims of the present application.

[0129] Although the embodiments of the present application 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 application, and such modifications and variations are all within the scope defined by the appended claims.

Claims

1. A code completion method, characterized in that: The method comprises: Generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information by a prompt constructor, and the initial context information is used to represent a functional requirement of the code to be completed; Using a code executor to test the code to be completed, and recording error information during the test, while retaining the correct code during the test; Generate an intermediate code based on a second prompt, wherein the second prompt is generated by the prompt builder by formatting intermediate context information, and the intermediate context information includes the error message and the correct code; The code executor is used to repeatedly test the intermediate code based on causal inference technology until the generated intermediate codes are all correct codes, and the generated intermediate codes are used as completion codes accordingly.

2. The method according to claim 1, characterized in that The method further comprises: Adjust the code executor.

3. The method according to claim 2, characterized in that Adjust the code executor in one of the following ways: Adding a code framework to the code executor, wherein the code framework is used to assist the code executor in running the code; Importing a basic library into the code executor, so that the code executor runs the code by calling the basic library.

4. The method according to claim 1, characterized in that: The initial context information includes function requirement information and input and output example information.

5. The method according to claim 1, characterized in that The first prompt is generated by a prompt constructor formatting task description information and initial context information, and the format of the first prompt is set based on the code executor; wherein the task description information is obtained by collection.

6. The method according to claim 1, characterized in that The generating the code to be completed based on the first prompt includes: generating the code to be completed based on the first prompt using the large model; The generating of the intermediate code based on the second prompt includes: generating the intermediate code based on the second prompt using a large model.

7. The method according to claim 1, characterized in that The error information includes syntax error information, function call missing information, variable name undefined information, and specific line number and column number information of the error.

8. A code completion device, characterized in that: The device includes a first generation module, a test module, a first generation module and a completion module; A first generating module, configured to generate a code to be completed based on a first prompt, wherein the first prompt is generated by formatting initial context information by a prompt constructor, and the initial context information is used to represent a functional requirement of the code to be completed; A test module, used to test the code to be completed using a code executor, and record error information during the test, while retaining the correct code during the test; A second generating module, configured to generate an intermediate code based on a second prompt, wherein the second prompt is generated by the prompt constructor formatting intermediate context information, and the intermediate context information includes the error message and the correct code; The completion module is used to use the code executor to repeatedly test the intermediate code based on causal inference technology until the generated intermediate codes are all correct codes, and accordingly use the generated intermediate codes as completion codes.

9. 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 a code completion method according to any one of claims 1 to 7 by executing the computer instructions.

10. 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 a code completion method according to any one of claims 1 to 7.

Citation Information

Patent Citations

  • Code completion method based on code intermediate representation, device and storage medium

    CN108595165A

  • Method and device for converting natural language to SQL (Structured Query Language) query statement based on causal inference

    CN118093641A

  • Source code patch generation with retrieval-augmented transformer

    US20240134614A1

  • Testing Benchmark Generation Method and Testing Benchmark Generation System Capable of Adjusting Prompt Data

    US20240311273A1

Cited By

  • Embedded code-oriented large model completion method and system

    CN121560330A