Code generation method and apparatus, computer device, and storage medium
Abstract
The present disclosure relates to the field of computer technology, and discloses a code generation method and apparatus, a computer device, and a storage medium. The method includes: obtaining a target generation position of a code to be generated; extracting context code information corresponding to the target generation position, and generating code prompt information matching the context code information; and inputting the code prompt information into a preset code generation model, and generating, by the preset code generation model, target code corresponding to the target generation position. The preset code generation model is pre-trained based on a prompt information sample.
Claims
exact text as granted — not AI-modifiedI/we claim:
1 . A code generation method, comprising:
obtaining a target generation position of a code to be generated; extracting context code information corresponding to the target generation position, and generating code prompt information matching the context code information; and inputting the code prompt information into a preset code generation model, and generating, by the preset code generation model, a target code corresponding to the target generation position, wherein the preset code generation model is pre-trained based on a prompt information sample.
2 . The method of claim 1 , wherein obtaining the target generation position of the code to be generated comprises:
detecting whether a code generation instruction is triggered at a current code editing position; and in response to the code generation instruction being triggered at the current code editing position, determining the current code editing position as the target generation position.
3 . The method of claim 1 , wherein extracting the context code information corresponding to the target generation position comprises:
obtaining code texts of upper and lower units corresponding to the target generation position, and extracting code context information corresponding to the target generation position from the code texts of the upper and lower units; and/or, obtaining an execution result of the code texts of the upper and lower units, and extracting execution context information corresponding to the target generation position from the execution result; and/or, obtaining variable information corresponding to the execution result, and extracting process context information corresponding to the target generation position from the variable information.
4 . The method of claim 3 , wherein generating the code prompt information matching the context code information comprises:
obtaining a preset information assembly template, and determining a padding position of the context code information in the information assembly template; and padding the context code information into the information assembly template according to the padding position, and assembling the context code information according to the information assembly template to generate the code prompt information.
5 . The method of claim 4 , further comprising:
obtaining code generation description information, and determining a first assembly position of the code generation description information in the information assembly template; generating code function description information in response to a code function description operation, and determining a second assembly position of the code function description information in the information assembly template; padding the code generation description information, the code function description information, and the context code information into the information assembly template according to the first assembly position, the second assembly position, and the padding position; and assembling the code generation description information, the code function description information, and the context code information according to the information assembly template to generate the code prompt information.
6 . The method of claim 1 , wherein, after generating, by the preset code generation model, the target code corresponding to the target generation position, the method comprises:
detecting whether an execution result of the target code is correct; in response to the execution result of the target code being incorrect, extracting execution error information of the target code; obtaining updated target prompt information by updating the code prompt information based on the execution error information; and regenerating, by the preset code generation model, the target code according to the target prompt information.
7 . The method of claim 6 , further comprising:
detecting a number of errors in the execution result; and in response to the number of errors reaching a preset number, generating the adjusted target code in response to an adjustment operation on the target code.
8 . The method of claim 6 , wherein, after generating, by the preset code generation model, the target code corresponding to the target generation position, the method further comprises:
padding the target code generated by the preset code generation model to the target generation position; and/or, creating a new code generation position, and padding the target code generated by the preset code generation model to the created new code generation position.
9 . 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 computer instructions, when executed by the processor, cause the processor to: obtain a target generation position of a code to be generated; extract context code information corresponding to the target generation position, and generate code prompt information matching the context code information; and input the code prompt information into a preset code generation model, and generate, by the preset code generation model, a target code corresponding to the target generation position, wherein the preset code generation model is pre-trained based on a prompt information sample.
10 . The computer device of claim 9 , wherein the computer instructions for obtaining the target generation position of the code to be generated, further cause the processor to:
detect whether a code generation instruction is triggered at a current code editing position; and in response to the code generation instruction being triggered at the current code editing position, determine the current code editing position as the target generation position.
11 . The computer device of claim 9 , wherein the computer instructions for extracting the context code information corresponding to the target generation position, further cause the processor to:
obtain code texts of upper and lower units corresponding to the target generation position, and extract code context information corresponding to the target generation position from the code texts of the upper and lower units; and/or, obtain an execution result of the code texts of the upper and lower units, and extract execution context information corresponding to the target generation position from the execution result; and/or, obtain variable information corresponding to the execution result, and extract process context information corresponding to the target generation position from the variable information.
12 . The computer device of claim 11 , wherein the computer instructions for generating the code prompt information matching the context code information, further cause the processor to:
obtain a preset information assembly template, and determining a padding position of the context code information in the information assembly template; and pad the context code information into the information assembly template according to the padding position, and assemble the context code information according to the information assembly template to generate the code prompt information.
13 . The computer device of claim 12 , wherein the computer instructions further cause the processor to:
obtain code generation description information, and determine a first assembly position of the code generation description information in the information assembly template; generate code function description information in response to a code function description operation, and determine a second assembly position of the code function description information in the information assembly template; pad the code generation description information, the code function description information, and the context code information into the information assembly template according to the first assembly position, the second assembly position, and the padding position; and assemble the code generation description information, the code function description information, and the context code information according to the information assembly template to generate the code prompt information.
14 . The computer device of claim 9 , wherein, after generating, by the preset code generation model, the target code corresponding to the target generation position, the computer instructions further cause the processor to:
detect whether an execution result of the target code is correct; in response to the execution result of the target code being incorrect, extract execution error information of the target code; obtaining updated target prompt information by updating the code prompt information based on the execution error information; and regenerate, by the preset code generation model, the target code according to the target prompt information.
15 . The computer device of claim 14 , wherein the computer instructions further cause the processor to:
detect a number of errors in the execution result; and in response to the number of errors reaching a preset number, generate the adjusted target code in response to an adjustment operation on the target code.
16 . The computer device of claim 14 , wherein, after generating, by the preset code generation model, the target code corresponding to the target generation position, the computer instructions further cause the processor to:
pad the target code generated by the preset code generation model to the target generation position; and/or, create a new code generation position, and pad the target code generated by the preset code generation model to the created new code generation position.
17 . A non-transitory computer-readable storage medium, storing computer instructions thereon, wherein the computer instructions, when executed by a processor, cause the processor to:
obtain a target generation position of a code to be generated; extract context code information corresponding to the target generation position, and generate code prompt information matching the context code information; and input the code prompt information into a preset code generation model, and generate, by the preset code generation model, a target code corresponding to the target generation position, wherein the preset code generation model is pre-trained based on a prompt information sample.
18 . The non-transitory computer-readable storage medium of claim 17 , wherein the computer instructions for obtaining the target generation position of the code to be generated, further cause the processor to:
detect whether a code generation instruction is triggered at a current code editing position; and in response to the code generation instruction being triggered at the current code editing position, determine the current code editing position as the target generation position.
19 . The non-transitory computer-readable storage medium of claim 17 , wherein the computer instructions for extracting the context code information corresponding to the target generation position, further cause the processor to:
obtain code texts of upper and lower units corresponding to the target generation position, and extract code context information corresponding to the target generation position from the code texts of the upper and lower units; and/or, obtain an execution result of the code texts of the upper and lower units, and extract execution context information corresponding to the target generation position from the execution result; and/or, obtain variable information corresponding to the execution result, and extract process context information corresponding to the target generation position from the variable information.
20 . The non-transitory computer-readable storage medium of claim 19 , wherein the computer instructions for generating the code prompt information matching the context code information, further cause the processor to:
obtain a preset information assembly template, and determining a padding position of the context code information in the information assembly template; and pad the context code information into the information assembly template according to the padding position, and assemble the context code information according to the information assembly template to generate the code prompt information.Join the waitlist — get patent alerts
Track US2025265048A1 — get alerts on status changes and closely related new filings.
We store only your email — no account needed. See our privacy policy.