IP Library Granted Patent US 12669983
Granted Patent B2
US 12669983 · App. 19/049,533 · Granted Jun 30, 2026

Code generation method and apparatus, computer device, and storage medium

Inventors: Kai Wu (Beijing, CN); Changwen Chen (Beijing, CN); Guitao Ding (Beijing, CN)
Assignee: Beijing Volcano Engine Technology Co., Ltd.
G06F8/35G06F11/3612
View Patent ↗
Loading inventors, assignments & file history…
Monitor This Case
Get email alerts when status or documents change.
Order Certified Copies
Most orders are placed with the USPTO same day — all within 24 business hours.
Order via The Patent Place →
Pre-filled with this patent's details
Quick Facts
Patent No.
US 12669983
App. No.
19/049,533
Granted
Jun 30, 2026
Kind
B2
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 (94)

1 . A code generation method, comprising:

obtaining a target generation position of a code to be generated, wherein the target generation position is a position of a code editing page stored in a memory;

extracting context code information corresponding to the target generation position, wherein the context code information comprises code information of upper and lower units corresponding to the target generation position;

generating code prompt information matching the context code information, wherein the code prompt information comprises code generation description information, code function description information, and 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;

wherein extracting the context code information corresponding to the target generation position comprises:

obtaining code texts of the upper and lower units corresponding to the target generation position, and extracting the context code 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 of the code texts;

and/or

obtaining variable information corresponding to the execution result of the code texts, and extracting process context information corresponding to the target generation position from the variable information;

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; and

wherein the method further comprises:

determining a first assembly position of the code generation description information in the information assembly template;

determining a second assembly position of the code function description information in the information assembly template, wherein the code function description information is generated in response to a code function description operation;

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.

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, 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.

4 . The method of claim 3 , further comprising:

detecting a number of errors in the execution result of the target code; 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.

5 . The method of claim 3 , 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.

6 . 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, wherein the target generation position is a position of a code editing page stored in the memory;

extract context code information corresponding to the target generation position, wherein the context code information comprises code information of upper and lower units located at the target generation position;

generate code prompt information matching the context code information, wherein the code prompt information comprises code generation description information, code function description information, and 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;

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 the upper and lower units corresponding to the target generation position, and extract the context code 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 of the code texts;

and/or

obtain variable information corresponding to the execution result of the code texts, and extract process context information corresponding to the target generation position from the variable information;

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; and

wherein the computer instructions further cause the processor to:

determine a first assembly position of the code generation description information in the information assembly template;

determine a second assembly position of the code function description information in the information assembly template, wherein the code function description information is generated in response to a code function description operation;

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.

7 . The computer device of claim 6 , 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.

8 . The computer device of claim 6 , 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.

9 . The computer device of claim 8 , wherein the computer instructions further cause the processor to:

detect a number of errors in the execution result of the target code; 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.

10 . The computer device of claim 8 , 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.

11 . 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, wherein the target generation position is a position of a code editing page stored in a memory;

extract context code information corresponding to the target generation position, wherein the context code information comprises code information of upper and lower units corresponding to the target generation position;

generate code prompt information matching the context code information, wherein the code prompt information comprises code generation description information, code function description information, and 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;

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 the upper and lower units corresponding to the target generation position, and extract the context code 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 of the code texts;

and/or

obtain variable information corresponding to the execution result of the code texts, and extract process context information corresponding to the target generation position from the variable information;

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; and

wherein the computer instructions further cause the processor to:

determine a first assembly position of the code generation description information in the information assembly template;

determine a second assembly position of the code function description information in the information assembly template, wherein the code function description information is generated in response to a code function description operation;

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.

12 . The non-transitory computer-readable storage medium of claim 11 , 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.