CN118113272A - Code generation method, device, terminal and medium based on semantic thinking chain - Google Patents
Code generation method, device, terminal and medium based on semantic thinking chain Download PDFInfo
- Publication number
- CN118113272A CN118113272A CN202410487323.1A CN202410487323A CN118113272A CN 118113272 A CN118113272 A CN 118113272A CN 202410487323 A CN202410487323 A CN 202410487323A CN 118113272 A CN118113272 A CN 118113272A
- Authority
- CN
- China
- Prior art keywords
- semantic
- information
- code
- chain
- prompt
- Prior art date
- Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
- Pending
Links
- 238000000034 method Methods 0.000 title claims abstract description 42
- 239000012634 fragment Substances 0.000 claims abstract description 21
- 238000005206 flow analysis Methods 0.000 claims description 73
- 230000006870 function Effects 0.000 claims description 55
- 238000006243 chemical reaction Methods 0.000 claims description 8
- 238000010276 construction Methods 0.000 claims description 5
- 238000007781 pre-processing Methods 0.000 claims description 5
- 238000005070 sampling Methods 0.000 claims description 4
- 238000004458 analytical method Methods 0.000 description 16
- 238000004590 computer program Methods 0.000 description 5
- 238000010586 diagram Methods 0.000 description 5
- 238000005516 engineering process Methods 0.000 description 5
- 230000003340 mental effect Effects 0.000 description 5
- 238000012545 processing Methods 0.000 description 5
- 238000011161 development Methods 0.000 description 3
- 230000006872 improvement Effects 0.000 description 3
- 230000003993 interaction Effects 0.000 description 3
- 230000008569 process Effects 0.000 description 3
- 238000003491 array Methods 0.000 description 2
- 230000006399 behavior Effects 0.000 description 2
- 238000004891 communication Methods 0.000 description 2
- 230000007547 defect Effects 0.000 description 2
- 238000007689 inspection Methods 0.000 description 2
- 238000013507 mapping Methods 0.000 description 2
- 230000001360 synchronised effect Effects 0.000 description 2
- 235000002198 Annona diversifolia Nutrition 0.000 description 1
- 241000282842 Lama glama Species 0.000 description 1
- 230000009471 action Effects 0.000 description 1
- 239000008186 active pharmaceutical agent Substances 0.000 description 1
- 230000009286 beneficial effect Effects 0.000 description 1
- 238000004422 calculation algorithm Methods 0.000 description 1
- 238000004364 calculation method Methods 0.000 description 1
- 238000012937 correction Methods 0.000 description 1
- 238000013500 data storage Methods 0.000 description 1
- 235000019800 disodium phosphate Nutrition 0.000 description 1
- 230000009977 dual effect Effects 0.000 description 1
- 230000000694 effects Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 230000003068 static effect Effects 0.000 description 1
- 238000006467 substitution reaction Methods 0.000 description 1
Landscapes
- Stored Programmes (AREA)
Abstract
The invention discloses a code generation method, a device, a terminal and a medium based on a semantic thinking chain, wherein the method comprises the following steps: acquiring a plurality of code seed fragments and user demand information; determining a prompt example corresponding to each seed code segment based on a plurality of code segments and the user demand information; acquiring target demand information, and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information; and inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content. According to the invention, by introducing the prompt examples and creating the semantic thinking chain prompt information, the large language model can more accurately understand programming requirements and generate codes meeting the requirements, and the accuracy and efficiency of code generation are improved.
Description
Technical Field
The present invention relates to the field of code generation technologies, and in particular, to a code generation method, device, terminal, and medium based on a semantic thought chain.
Background
With the increasing maturity of large language model technology and theory, the large language model is widely applied in the field of automatic coding, and the traffic is in a situation of rapid development. In order to improve the coding efficiency of software developers, it is important to improve the code generation capability of a large language model, and the chain of thought technology (CoT) is one of the key means.
However, the current thought chain technology is difficult to be directly applied to the field of code generation, namely, the natural language form of the thought chain is difficult to fully express the code characteristics, and the accuracy and efficiency of code generation are seriously affected because the deep semantic information of the code is difficult to capture by a large language model.
Accordingly, there is a need for improvement and advancement in the art.
Disclosure of Invention
The invention aims to solve the technical problems that aiming at the defects in the prior art, a code generation method, a device, a terminal and a medium based on a semantic chain are provided, and aims to solve the problems that in the process of generating codes in the prior art, the technique of the chain is difficult to be directly applied to the field of code generation, and the accuracy and the efficiency of code generation are seriously influenced because deep semantic information of the codes is difficult to capture by a large language model.
In order to solve the technical problems, the technical scheme adopted by the invention is as follows:
in a first aspect, the present invention provides a code generation method based on semantic thought chains, wherein the method comprises:
acquiring a plurality of seed code fragments and user demand information;
Determining a prompt example corresponding to each seed code segment based on a plurality of seed code segments and the user demand information;
acquiring target demand information, and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information;
And inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content.
In one implementation, the obtaining a number of seed code fragments includes:
randomly sampling from the open source code library to obtain a plurality of code fragments;
and preprocessing each code segment to obtain a plurality of seed code segments.
In one implementation, the determining, based on a number of seed code segments and the user requirement information, a hint example corresponding to each of the seed code segments includes:
Determining semantic information based on the user demand information and each of the seed code segments;
And determining the prompt examples corresponding to each seed code segment based on the user demand information, the seed code segments and the semantic information.
In one implementation, the determining semantic information based on the user demand information and each of the seed code segments includes:
Based on the user demand information and each seed code segment, writing a control flow analysis example and a data flow analysis example corresponding to each seed code segment, wherein the control flow analysis example is used for reflecting logic of program execution, and the data flow analysis example is used for reflecting movement and conversion modes of data in the program;
respectively determining a control flow analysis result corresponding to the control flow analysis example and a data flow analysis result corresponding to the data flow analysis example;
And determining semantic information based on the control flow analysis result, the data flow analysis result and summary information.
In one implementation, the constructing semantic chain of thought hint information based on the hint examples and the target demand information includes:
For each prompt example, splicing the user demand information, the seed code fragments and the semantic information to obtain a prompt example splicing result corresponding to each prompt example;
splicing all the prompt example splicing results to obtain a context example;
and constructing the semantic thinking chain prompt information based on the context examples and the target demand information.
In one implementation, the building the semantic chain of thought hint information based on the contextual examples and the target demand information includes:
and respectively adding instruction information and the target demand information before and after the context example to construct the semantic thinking chain prompt information, wherein the instruction information is used for restricting the overall task of the large language model.
In one implementation, the determining the target source code based on the generated content includes:
Code formatting is carried out on the generated content to obtain formatted content;
and obtaining the target source code according to the formatted content.
In one implementation, the obtaining the target source code according to the formatted content includes:
searching for a position index of the def function according to the formatted content;
Determining a maximum range of the def function based on the position index;
deleting semantic information except the maximum range in the formatted content to obtain a source code in the def function, and taking the source code in the def function as the target source code.
In a second aspect, an embodiment of the present invention further provides a code generating device based on a semantic thought chain, where the device includes:
the code information acquisition module is used for acquiring a plurality of seed code fragments and user demand information;
The prompt example determining module is used for determining a prompt example corresponding to each seed code segment based on a plurality of seed code segments and the user demand information;
the thinking chain prompt construction module is used for acquiring target demand information and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information;
the code generation module is used for inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content.
In a third aspect, an embodiment of the present invention further provides a terminal, where the terminal includes a memory, a processor, and a code generation program based on a semantic thought chain stored in the memory and executable on the processor, and when the processor executes the code generation program based on the semantic thought chain, the processor implements the steps of the code generation method based on the semantic thought chain in any one of the above schemes.
In a fourth aspect, an embodiment of the present invention further provides a computer readable storage medium, where a code generating program based on a semantic thought chain is stored on the computer readable storage medium, where the code generating program based on the semantic thought chain implements the steps of the code generating method based on the semantic thought chain according to any one of the above schemes when the code generating program based on the semantic thought chain is executed by a processor.
The beneficial effects are that: compared with the prior art, the invention provides a code generation method based on a semantic thinking chain, which comprises the steps of firstly acquiring a plurality of seed code fragments and user demand information; and determining a prompt example corresponding to each seed code segment based on the seed code segments and the user demand information. The hint examples can help a large language model to more accurately understand programming needs and generate code that meets the needs. Then, the invention acquires target demand information, builds semantic thinking chain prompt information based on the prompt examples and the target demand information, and enables the large language model to deeply understand the internal logic and structure of the code by building the semantic thinking chain prompt information, thereby generating the code which meets the demands of logic and practical application. Finally, the semantic thinking chain prompt information is input into a preset large language model to obtain generated content, and a target source code is determined based on the generated content. Therefore, the invention introduces the prompt examples and creates the semantic thinking chain prompt information, so that the large language model can more accurately understand the programming requirement and generate codes meeting the requirement, and the accuracy and the efficiency of code generation are improved.
Drawings
Fig. 1 is a flowchart of a preferred embodiment of a code generation method based on semantic thought chains according to an embodiment of the present invention.
Fig. 2 is an algorithm flow chart of a code generation method based on semantic thought chains according to an embodiment of the present invention.
Fig. 3 is a control flow analysis example one and a data flow analysis example one in a code generation method based on semantic thought chains according to an embodiment of the present invention.
Fig. 4 is a control flow analysis example two and a data flow analysis example two in the code generation method based on semantic thought chains according to the embodiment of the present invention.
Fig. 5 is a control flow analysis example three and a data flow analysis example three in a code generation method based on semantic thought chains according to an embodiment of the present invention.
Fig. 6 is an example of semantic thinking chain prompt information in a code generation method based on semantic thinking chains according to an embodiment of the present invention.
Fig. 7 is a schematic architecture diagram of a code generating device based on semantic thought chains according to an embodiment of the present invention.
Fig. 8 is a schematic block diagram of a terminal according to an embodiment of the present invention.
Detailed Description
In order to make the objects, technical solutions and effects of the present invention clearer and more specific, the present invention will be described in further detail below with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the scope of the invention.
The code generation method based on the semantic thinking chain can enable a large language model to more accurately understand programming requirements and generate codes meeting the requirements, and accuracy and efficiency of code generation are improved. When the method is applied specifically, the embodiment firstly acquires a plurality of seed code fragments and user demand information; and determining a prompt example corresponding to each seed code segment based on the seed code segments and the user demand information. And then acquiring target demand information, and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information. And finally, inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content. In this embodiment, the hint examples can help the large language model to understand the programming requirements more accurately and generate codes meeting the requirements, and the constructed semantic thought chain hint information can enable the large language model to understand the inherent logic and structure of the codes more deeply, so as to generate codes meeting the requirements of logic and practical applications more.
The code generation method based on the semantic thinking chain is applied to terminals, wherein the terminals comprise intelligent product terminals such as computers, intelligent televisions and mobile phones. As shown in fig. 1, the code generation method based on semantic thought chains of the present embodiment includes the steps of:
Step S100, a plurality of seed code fragments and user demand information are acquired. As shown in connection with fig. 2, the present embodiment first obtains an open source code library, which may be a GitHub (hosting platform for open source and private software projects), gitLab (rapid development platform based on code generator), or the like. The open source code library covers a wide range of programming languages and application scenarios to ensure sample diversity and representativeness. In this embodiment, a certain number of code segments are randomly sampled from an open source code library, and each code segment is preprocessed to obtain a plurality of seed code segments. Preferably, the embodiment can sample 3 code segments from the open source code library in a uniformly distributed manner, and respectively preprocess the 3 code segments, wherein the preprocessing comprises any one or more of removing notes, formatting codes and eliminating dependency relations, so that 3 seed code segments are obtained, and the preprocessed seed code segments can be more suitable for subsequent analysis and use. The user requirement information obtained in this embodiment may be an actual requirement input or provided by the user, or may be a requirement corresponding to each seed code segment itself, that is, the user requirement information may be an annotation of the seed code segment.
Step 200, determining a prompt example corresponding to each seed code segment based on a plurality of seed code segments and the user demand information.
In this embodiment, after obtaining the seed code segments and the user requirement information, a hint example is written for each seed code segment. Specifically, based on the user demand information and each seed code segment, a control flow analysis example and a data flow analysis example corresponding to each seed code segment are written, wherein the control flow analysis example is used for reflecting logic of program execution. In programming, the control flow mainly comprises the following basic structure:
(1) A sequence control structure (Sequence control structure). This is the most basic structure, and the code is executed in the order in the program.
(2) A condition control structure (Conditional control structure). These include if, if_else and switch_case statements, allowing the program to choose different execution paths depending on certain conditions.
(3) A circulation control structure (Loop control structure). These include loop structures for, while, and do while, allowing the program to repeatedly execute a piece of code.
(4) A jump control structure (Jump control structure). This includes break, continue, return, etc. statements. These statements allow the program to skip parts or exit loops or functions ahead of time.
The data flow analysis example in this embodiment is used to reflect the movement and conversion manner of data inside the program. It focuses on how Data is transferred from one part to another and how Data is processed and stored, i.e. Data manipulation. The following are some key components of the data stream:
(1) Data storage and manipulation. This involves variables, constants, and data structures such as arrays, lists, and stacks that provide a way to organize, store, and manipulate data.
(2) Input and output. Data may be input to the program from an external source and output from the program to an external destination, including user input, file manipulation, and network communication.
(3) Data conversion and processing. Data often undergoes conversion, processing, or computation within a program, such as from one type to another or from one data structure to another.
(4) External interactions. This includes interactions with databases, network communications, and interactions with other software or services through APIs.
The present embodiment composes a data stream analysis example and a control stream analysis example for the sampled seed code segments according to the definition described above. Preferably, the present embodiment samples 3 seed code segments, and thus, there are 3 corresponding data flow analysis examples and control flow analysis examples. As shown in fig. 3 in detail, fig. 3 is a control flow analysis example one and a data flow analysis example one provided in the present embodiment. The control flow analysis example one and the data flow analysis example one in fig. 3 include the following:
(1) User demand information
The data stream information and the control stream information are written for the user demand information "Write a python function to MERGE THE FIRST AND LAST ELEMENTS SEPARATELY IN A LIST of lists," and the seed code fragment merge.
def merge(lst):
return [list(ele) for ele in list(zip(*lst))]
(2) Control flow analysis results
First, for the control flow, the control flow describes the execution of the function from start to end, including all logical decisions and loops. For the merge function, the control flow is relatively simple because there is no loop or obvious conditional judgment. The control flow analysis result writing flow is as follows:
Starting: the function merge (lst) is called.
Step 1: unpacking
Unpacking each subcolumn l i in the initial list lst into separate parameters using unpacking operators to pass to the zip function;
step 2, merging tuples
Merging each subcolumn L i in the unpacked initial list lst according to the corresponding element of each row using a zip function (tuple iterator) to obtain a merged i-th tuple L i, merging all columns in lst according to the corresponding element of the number of rows to obtain a merged tuple lst 1= [ L 1,L2,…Li…,Ln ];
Step 3, converting the tuples to form a new list
Lst1 is converted into a column of the list to form a new list L, which is returned.
(3) Data stream analysis results
For data flows, the data flow describes how data flows inside a function and how it is processed and converted. The writing flow of the data flow analysis result is as follows:
input: the original list lst is taken as input, where the original list contains a plurality of subcolumns.
Treatment 1: the unpacking operator is used to unpack each sub-column in lst into independent parameters to pass to the zip function.
Treatment 2: the zip function receives the unpacked sub-list and merges the sub-list according to the element position of each sub-column, for example, the first element of the first sub-column and the first elements of other sub-columns are merged according to the sequence of the sub-columns, and the last element of the first sub-column and the last element of other sub-columns are merged according to the sequence of the sub-columns to form a sequence of two tuples.
Conversion: each tuple obtained is converted into a list.
And (3) outputting: combining the two lists obtained by conversion to obtain a new list, wherein the new list comprises two lists: the first is the merging of the elements of the first position of all subcolumns and the second is the merging of the elements of the last position of all subcolumns.
As shown in fig. 4, fig. 4 is a control flow analysis example two and a data flow analysis example two provided in the present embodiment. The second control flow analysis example and the second data flow analysis example in fig. 4 include the following:
(1) User demand information
The data stream information and the control stream information are written for the user demand information 'Write a function to CHECK IF THE GIVEN INTEGER IS A PRIME number' and the seed code fragment prime_num.
def prime_num(num):
if num>= 1:
for i in range(2, num // 2):
if (num % i) == 0:
return False
else:
return True
else:
return False
(2) Control flow analysis results
First, for a control flow, the control flow describes the order or path of program execution. For the prime_num function, the control flow analysis result can be decomposed into the following steps:
Step one: starting to execute the function: when the function prime_num is called, the program starts executing.
Step two: checking whether num is 1 or more: this is a conditional determination of whether to continue executing other parts of the function.
If num is less than 1, the function returns directly to False because the negative sum 0 is not a prime number.
If num is greater than or equal to 1, the function proceeds to the next step.
Step three: iterative inspection: the function checks whether num is divisible by all integers from 2 to num/2 (not including num/2) by iterating through these integers.
If an integer is found that can divide num entirely, the function returns False, since this means num is not a prime number.
If no integer is found that can divide num entirely, the function should theoretically return True, meaning num is a prime number.
Step four: ending execution: according to the conditions, the function returns True or False, and execution ends.
(3) Data stream analysis results
For data flows, the data flows describe how data moves and changes in the program. For the prime_num function, the data stream analysis results can be summarized as:
input data: the input integer num is the starting point of the function, which determines the execution path of the function.
Iteration and inspection: num is used to determine the extent of the iteration and to check if there are integers that can divide num entirely.
Outputting data: based on the nature of num (whether or not it can be divided by other integers), the function outputs True or False.
As shown in fig. 5, fig. 5 is a control flow analysis example three and a data flow analysis example three provided in the present embodiment. The third control flow analysis example and the third data flow analysis example in fig. 5 include the following:
(1) User demand information
Data stream information and control stream information are written for the user demand information "Write a python function to calculate the sum of the numbers in a list between the indices of a specified range.", and the seed code segment sum_range_list.
def sum_Range_list(nums, m, n):
sum_range = 0
for i in range(m, n + 1, 1):
sum_range += nums[i]
return sum_range
(2) Control flow analysis results
First, for a control flow, the control flow describes the order or path of program execution. For the sum_range_list function, the control flow analysis result can be decomposed into the following steps:
Start (Start) when the sum_range_list (num, m, n) function is called, the program starts executing.
Initialization (Initialization) inside the function, the accumulator variable sum_range is first initialized to 0. This is to accumulate values within a specified range in the next cycle.
Iteration (Iteration) the function iterates through the for loop from index m to index n (containing n), each loop adding nums [ i ] (i.e., the value at the current index i) to sum_range.
Return (Return) after the loop is completed, the function returns the value in the accumulator sum_range, which is the sum of the values in the specified range.
And ending (End) the execution of the function is finished, and returning the calculation result.
(3) Data stream analysis results
For data flows, the data flows describe how data moves and changes in the program. For the sum_range_list function, the data stream analysis results can be summarized as:
Input the Input data comprises a list num and specified range indices m and n. These inputs determine from which part of the list to sum.
Processing (Process) the function iterates over a given range of indices, each iteration accumulating the value at the current index i into sum_range. This process is repeated until all indexes from m to n are covered.
Output the function ultimately outputs the value of the accumulator sum_range, which represents the sum of the values from index m to n (inclusive of n).
In this embodiment, in compiling a control flow analysis example and a data flow analysis example corresponding to each seed code segment, a control flow analysis result corresponding to the control flow analysis example and a data flow analysis result corresponding to the data flow analysis example may be determined respectively. Next, the present embodiment determines semantic information based on the control flow analysis result and the data flow analysis result, the semantic information including three parts: data flow analysis results, control flow analysis results, and summary information. The first two parts of semantic information include data stream analysis results and control stream analysis results, as shown in examples one, two, and three of the control and data streams described above. In addition, in order to enable the subsequent large language model to understand the action of the semantic information, summary information is also provided after the control flow analysis result and the data flow analysis result, such as "3. Finally, This combined graphical representation provides a clear, structured context for understanding the function's logic and behavior.", in fig. 3, that is, the semantic information further comprises the summary information, so as to remind the large language model to better understand the program logic and the behavior based on the semantic information. Examples one, two and three of the above control and data streams written in this embodiment are intended to help a large language model learn the mapping format between inputs and outputs (i.e., output generated code and semantic information according to input requirements). Second, they enable large language models to learn how to understand and generate code at a finer granularity semantic level by observing the mapping between source code and semantic information in the examples.
Further, the embodiment determines the hint examples corresponding to each of the seed code segments based on the user requirement information, the seed code segments and the semantic information, that is, forms < user requirement information, seed code segments, semantic information > hint examples. By introducing the structured prompt of the < user requirement information, the seed code fragments and the semantic information >, the large language model can more accurately understand the programming requirement and generate codes meeting the requirement. This method of structured hinting not only improves the accuracy of the code, but also helps to improve development efficiency, as it reduces the subsequent adjustments and error correction that the developer needs to make.
And step S300, acquiring target demand information, and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information.
The embodiment obtains target demand information, where the target demand information reflects an actual target demand of a user. The target demand information may be the same as the user demand information or may be different from the user demand information. Next, for each of the prompt examples, the embodiment splices the user requirement information, the seed code segment, and the semantic information in each < user requirement information, the seed code segment, and the semantic information > prompt example to obtain a prompt example splicing result corresponding to each prompt example, and splices all the prompt example splicing results to obtain a context example. And then, respectively adding Instruction information (Instruction) and the target demand information before and after the context example to construct the semantic thought chain prompt information (prompt), wherein the Instruction information is used for restricting the overall task of the large language model, namely completing demand codes (Complete the requirement code based on examples) based on the semantic thought chain prompt information. As shown in FIG. 6, FIG. 6 illustrates an example of SeCoT semantic thought chain hints. Compared with the technical defect that the traditional thinking chain technology is difficult to fully express the code characteristics in the code generation, the embodiment enables a large language model to deeply understand the inherent logic and structure of the code by combining analysis examples of data flow and control flow, so that the code which meets the requirements of logic and practical application is generated.
And step 400, inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content.
The embodiment inputs the created semantic chain prompt information into a preset large language model (such as GPT series and Llama series), and the large language model can generate corresponding codes and corresponding semantic analysis information based on the semantic chain prompt information, so that generated content is obtained. Then, the embodiment firstly carries out code formatting on the generated content to obtain formatted content, deletes semantic analysis information in the generated content according to the formatted content to obtain target source codes, so that a user can efficiently use the generated code fragments. Specifically, the embodiment searches for the position index of the def function according to the formatted contents. And then determining the maximum range of the def function based on the position index, wherein the embodiment only considers the range of the outmost def function because the generated code possibly comprises nested def functions. And deleting semantic information except the maximum range in the formatted content to obtain a source code final_code in the def function, and taking the source code final_code in the def function as the target source code.
Compared with the prior art, the functional difference of the embodiment mainly relates to deep semantic analysis of code generation and improvement of code generation accuracy, and high-efficiency adaptability in diversified programming environments. These improvements provide significant advantages in the field of automatic encoding, particularly in terms of handling complex programming tasks and improving encoding efficiency.
Based on the above embodiment, the present invention further provides a code generating device based on semantic thought chain, as shown in fig. 7, the device includes: a code information acquisition module 10, a hint example determination module 20, a mental chain hint construction module 30, and a code generation module 40. Specifically, the code information obtaining module 10 is configured to obtain a plurality of seed code segments and user requirement information. The hint example determining module 20 is configured to determine a hint example corresponding to each of the seed code fragments based on a plurality of seed code fragments and the user requirement information. The mental chain prompt construction module 30 is configured to obtain target demand information, and construct semantic mental chain prompt information based on the prompt examples and the target demand information. The code generating module 40 is configured to input the semantic thought chain prompt information into a preset large language model, obtain generated content, and determine a target source code based on the generated content.
In one implementation, the code information acquisition module 10 includes:
The code segment sampling unit is used for randomly sampling a plurality of code segments from the open source code base;
And the code preprocessing unit is used for preprocessing each code segment to obtain a plurality of seed code segments.
In one implementation, the hint example determination module 20 further includes:
The semantic information determining unit is used for determining semantic information based on the user demand information and each seed code segment;
and the prompt example determining unit is used for determining the prompt example corresponding to each seed code segment based on the user demand information, the seed code segments and the semantic information.
In one implementation, the hint example determining unit includes:
An analysis example writing subunit, configured to write, based on the user requirement information and each of the seed code segments, a control flow analysis example and a data flow analysis example corresponding to each of the seed code segments, where the control flow analysis example is used to reflect logic of program execution, and the data flow analysis example is used to reflect a movement and a conversion manner of data in the program;
an analysis result determination subunit configured to determine a control flow analysis result corresponding to the control flow analysis example and a data flow analysis result corresponding to the data flow analysis example, respectively;
and the semantic information determining subunit is used for determining semantic information based on the control flow analysis result, the data flow analysis result and the summary information.
In one implementation, the mental chain hint construction module 30 includes:
The prompt example splicing unit is used for splicing the user demand information, the seed code fragments and the semantic information aiming at each prompt example to obtain a prompt example splicing result corresponding to each prompt example;
the context example determining unit is used for splicing all the prompt example splicing results to obtain a context example;
and the thinking chain prompt creation unit is used for constructing the semantic thinking chain prompt information based on the context examples and the target demand information.
In one implementation, the mental chain hint creation unit includes:
And the information adding subunit is used for respectively adding instruction information and the target demand information before and after the context example to construct the semantic thinking chain prompt information, wherein the instruction information is used for restricting the overall task of the large language model.
In one implementation, the code generation module 40 includes:
the code formatting unit is used for carrying out code formatting on the generated content to obtain formatted content;
and the target source code determining unit is used for obtaining the target source code according to the formatted content.
In one implementation, an object source code determining unit includes:
a position determining subunit, configured to search a position index of the def function according to the formatted content;
A range determination subunit configured to determine a maximum range of the def function based on the position index;
And the source code determining subunit is used for deleting semantic information except the maximum range in the formatted content to obtain source codes in the def function, and taking the source codes in the def function as the target source codes.
The working principle of each module in the code generating device based on semantic thinking chain in this embodiment is the same as that of each step in the above method embodiment, and will not be described here again.
Based on the above embodiment, the present invention also provides a terminal, and a schematic block diagram of the terminal may be shown in fig. 8. The terminal may include one or more processors 100 (only one shown in fig. 8), a memory 101, and a computer program 102 stored in the memory 101 and executable on the one or more processors 100, e.g., a code generation program based on a semantic thought chain. The execution of the computer program 102 by one or more processors 100 may implement the various steps in an embodiment of a semantic-chain-based code generation method. Or the one or more processors 100, when executing the computer program 102, may implement the functions of the modules/units in the code generation method embodiments based on semantic thought chains, without limitation.
In one embodiment, the Processor 100 may be a central processing unit (Central Processing Unit, CPU), but may also be other general purpose processors, digital signal processors (DIGITAL SIGNAL processors, DSPs), application SPECIFIC INTEGRATED Circuits (ASICs), off-the-shelf Programmable gate arrays (Field-Programmable GATE ARRAY, FPGA) or other Programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, or the like. A general purpose processor may be a microprocessor or the processor may be any conventional processor or the like.
In one embodiment, the memory 101 may be an internal storage unit of the electronic device, such as a hard disk or a memory of the electronic device. The memory 101 may also be an external storage device of the electronic device, such as a plug-in hard disk provided on the electronic device, a smart memory card (SMART MEDIA CARD, SMC), a Secure Digital (SD) card, a flash memory card (FLASH CARD), or the like. Further, the memory 101 may also include both an internal storage unit and an external storage device of the electronic device. The memory 101 is used to store computer programs and other programs and data required by the terminal. The memory 101 may also be used to temporarily store data that has been output or is to be output.
It will be appreciated by those skilled in the art that the functional block diagram shown in fig. 8 is merely a block diagram of some of the structures associated with the present inventive arrangements and is not limiting of the terminal to which the present inventive arrangements may be applied, as a specific terminal may include more or less components than those shown, or may be combined with some components, or may have a different arrangement of components.
Those skilled in the art will appreciate that implementing all or part of the above-described methods may be accomplished by way of a computer program, which may be stored on a non-transitory computer readable storage medium, that when executed may comprise the steps of the embodiments of the methods described above. Any reference to memory, storage, operational database, or other medium used in embodiments provided herein may include non-volatile and/or volatile memory. The nonvolatile memory can include Read Only Memory (ROM), programmable ROM (PROM), electrically Programmable ROM (EPROM), electrically Erasable Programmable ROM (EEPROM), or flash memory. Volatile memory can include Random Access Memory (RAM) or external cache memory. By way of illustration and not limitation, RAM is available in a variety of forms such as Static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), dual operation data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (SYNCHLINK) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM), among others.
Finally, it should be noted that: the above embodiments are only for illustrating the technical solution of the present invention, and are not limiting; although the invention has been described in detail with reference to the foregoing embodiments, it will be understood by those of ordinary skill in the art that: the technical scheme described in the foregoing embodiments can be modified or some technical features thereof can be replaced by equivalents; such modifications and substitutions do not depart from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims (11)
1. A code generation method based on semantic thought chains, the method comprising:
acquiring a plurality of seed code fragments and user demand information;
Determining a prompt example corresponding to each seed code segment based on a plurality of seed code segments and the user demand information;
acquiring target demand information, and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information;
And inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content.
2. The semantic-chain-based code generation method according to claim 1, wherein the obtaining a plurality of seed code fragments comprises:
Randomly sampling from a preset open source code library to obtain a plurality of code fragments;
and preprocessing each code segment to obtain a plurality of seed code segments.
3. The semantic-chain-of-thought-based code generation method according to claim 1, wherein determining a hint example corresponding to each of the seed code segments based on a plurality of seed code segments and the user demand information comprises:
Determining semantic information based on the user demand information and each of the seed code segments;
And determining the prompt examples corresponding to each seed code segment based on the user demand information, the seed code segments and the semantic information.
4. A semantic chain based code generation method according to claim 3, wherein said determining semantic information based on said user demand information and each of said seed code segments comprises:
Based on the user demand information and each seed code segment, writing a control flow analysis example and a data flow analysis example corresponding to each seed code segment, wherein the control flow analysis example is used for reflecting logic of program execution, and the data flow analysis example is used for reflecting movement and conversion modes of data in the program;
respectively determining a control flow analysis result corresponding to the control flow analysis example and a data flow analysis result corresponding to the data flow analysis example;
And determining semantic information based on the control flow analysis result, the data flow analysis result and summary information.
5. A semantic-chain-based code generation method according to claim 1 or 3, wherein the constructing semantic-chain hint information based on the hint examples and the target demand information includes:
for each prompt example, splicing the user demand information, the seed code fragments and the semantic information to obtain a prompt example splicing result corresponding to each prompt example;
splicing all the prompt example splicing results to obtain a context example;
and constructing the semantic thinking chain prompt information based on the context examples and the target demand information.
6. The semantic chain-based code generation method according to claim 5, wherein the constructing the semantic chain hint information based on the context example and the target demand information comprises:
and respectively adding instruction information and the target demand information before and after the context example to construct the semantic thinking chain prompt information, wherein the instruction information is used for restricting the overall task of the large language model.
7. The semantic chain based code generation method according to claim 6, wherein the determining a target source code based on the generated content comprises:
Code formatting is carried out on the generated content to obtain formatted content;
And deleting semantic information in the formatted content to obtain the target source code.
8. The semantic-chain-based code generation method according to claim 7, wherein the deleting semantic information in the formatted content to obtain the target source code comprises:
searching for a position index of the def function according to the formatted content;
Determining a maximum range of the def function based on the position index;
deleting semantic information except the maximum range in the formatted content to obtain a source code in the def function, and taking the source code in the def function as the target source code.
9. A semantic thought chain-based code generation apparatus, the apparatus comprising:
the code information acquisition module is used for acquiring a plurality of seed code fragments and user demand information;
The prompt example determining module is used for determining a prompt example corresponding to each seed code segment based on a plurality of seed code segments and the user demand information;
the thinking chain prompt construction module is used for acquiring target demand information and constructing semantic thinking chain prompt information based on the prompt examples and the target demand information;
the code generation module is used for inputting the semantic thinking chain prompt information into a preset large language model to obtain generated content, and determining a target source code based on the generated content.
10. A terminal comprising a memory, a processor and a semantic-chain-based code generation program stored in the memory and executable on the processor, the processor implementing the steps of the semantic-chain-based code generation method of any one of claims 1-8 when executing the semantic-chain-based code generation program.
11. A computer-readable storage medium, on which a code generation program based on a semantic chain of thinking is stored, which when executed by a processor, implements the steps of the code generation method based on a semantic chain of thinking according to any of claims 1-8.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410487323.1A CN118113272A (en) | 2024-04-23 | 2024-04-23 | Code generation method, device, terminal and medium based on semantic thinking chain |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN202410487323.1A CN118113272A (en) | 2024-04-23 | 2024-04-23 | Code generation method, device, terminal and medium based on semantic thinking chain |
Publications (1)
Publication Number | Publication Date |
---|---|
CN118113272A true CN118113272A (en) | 2024-05-31 |
Family
ID=91219276
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN202410487323.1A Pending CN118113272A (en) | 2024-04-23 | 2024-04-23 | Code generation method, device, terminal and medium based on semantic thinking chain |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN118113272A (en) |
Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20150135166A1 (en) * | 2013-11-12 | 2015-05-14 | Microsoft Corporation | Source code generation, completion, checking, correction |
CN112114791A (en) * | 2020-09-08 | 2020-12-22 | 南京航空航天大学 | Code self-adaptive generation method based on meta-learning |
CN117369783A (en) * | 2023-12-06 | 2024-01-09 | 之江实验室 | Training method and device for security code generation model |
CN117828360A (en) * | 2023-12-13 | 2024-04-05 | 之江实验室 | Model training method, model training device, model code generating device, storage medium and storage medium |
-
2024
- 2024-04-23 CN CN202410487323.1A patent/CN118113272A/en active Pending
Patent Citations (4)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20150135166A1 (en) * | 2013-11-12 | 2015-05-14 | Microsoft Corporation | Source code generation, completion, checking, correction |
CN112114791A (en) * | 2020-09-08 | 2020-12-22 | 南京航空航天大学 | Code self-adaptive generation method based on meta-learning |
CN117369783A (en) * | 2023-12-06 | 2024-01-09 | 之江实验室 | Training method and device for security code generation model |
CN117828360A (en) * | 2023-12-13 | 2024-04-05 | 之江实验室 | Model training method, model training device, model code generating device, storage medium and storage medium |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US10983761B2 (en) | Deep learning enhanced code completion system | |
Smith | Probabilistic noninterference through weak probabilistic bisimulation | |
Chen et al. | Spreadsheetcoder: Formula prediction from semi-structured context | |
US11307831B2 (en) | Neural code completion via re-ranking | |
US20220236964A1 (en) | Semantic code search based on augmented programming language corpus | |
US11327722B1 (en) | Programming language corpus generation | |
US10534592B2 (en) | Template expressions for constraint-based systems | |
US10474435B2 (en) | Configuration model parsing for constraint-based systems | |
CN116302998A (en) | Multi-mode program automatic repair method, system, equipment and storage medium | |
CN112199416A (en) | Data rule generation method and device | |
US11681541B2 (en) | Methods, apparatus, and articles of manufacture to generate usage dependent code embeddings | |
CN101162480A (en) | Schedule drawing automatic recognition and comprehend method | |
CN118113272A (en) | Code generation method, device, terminal and medium based on semantic thinking chain | |
US20160140016A1 (en) | Event sequence construction of event-driven software by combinational computations | |
US11669307B2 (en) | Code injection from natural language derived intent | |
CN113076089B (en) | API (application program interface) completion method based on object type | |
Mussot et al. | Expressing and exploiting conflicts over paths in WCET analysis | |
CN115758370B (en) | Software source code defect detection method, device and storage medium | |
US20240135148A1 (en) | Semantic Representations of Mathematical Expressions in a Continuous Vector Space and Generation of Different but Mathematicallly Equivalent Expressions and Applications Thereof | |
Smelik et al. | Specification and construction of control flow semantics | |
US20230138152A1 (en) | Apparatus and method for generating valid neural network architecture based on parsing | |
CN117909505B (en) | Event argument extraction method and related equipment | |
US20240013053A1 (en) | Method and system for optimizing neural networks (nn) for on-device deployment in an electronic device | |
Plátek et al. | On pumping RP-automata controlled by complete LRG (¢, $)-grammars | |
US20230359441A1 (en) | Retrieval-augmented code completion |
Legal Events
Date | Code | Title | Description |
---|---|---|---|
PB01 | Publication | ||
PB01 | Publication | ||
SE01 | Entry into force of request for substantive examination |