An algorithm model-based code automatic generation method and system
By using RAG technology and algorithm models, the similarity is dynamically adjusted to optimize the reconstruction capability of large models, solving the problem of multiple prompt word conflicts on devices with limited hardware resources, and generating code reconstruction solutions that meet the requirements.
Patent Information
- Application Number
- CN202511658789.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-11-13
- Publication Date
- 2026-02-06
- Estimated Expiration
- 2045-11-13
AI Technical Summary
On hardware devices with limited resources, existing technologies cannot effectively support code refactoring with multiple prompt words, resulting in refactored code that cannot meet the needs of the new energy monitoring and intelligent control field.
An algorithm model based on RAG technology is adopted. By calculating the difference between the prompt words and the initial code, the reference knowledge with the highest similarity is retrieved, and the similarity is dynamically adjusted to optimize the reconstruction capability of the large model and generate the final reconstructed code.
It achieves objective quantification of multi-prompt word conflicts, enhances the ability of large models to reconstruct incompatible prompt words, and the generated reconstruction code can meet the needs of the new energy monitoring and intelligent control fields.
Smart Images

Figure CN121116382B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of data processing, in particular to a code automatic generation method and system based on an algorithm model. BACKGROUND
[0002] In the field of new energy monitoring and intelligent control, it is necessary to rely on relevant intelligent monitoring and intelligent control codes to ensure the normal operation of the relevant equipment of the new energy station. For example, the maintenance and update of wind and light power prediction, water and electricity optimization scheduling, and equipment state monitoring project codes are indispensable links for maintaining the normal operation of equipment in the field of new energy monitoring and intelligent control.
[0003] Especially, with the upgrading and replacement of hardware devices (such as embedded devices, central processing units, storage devices, or communication devices), historical codes may not be directly migrated to new hardware device environments. At this time, it is often necessary to refactor the codes to generate codes that can run in specified hardware devices.
[0004] The large model technology based on prompt words is an important technical means for code refactoring. However, in the actual use of the large model, on the one hand, the code running on hardware devices with limited hardware resources cannot simultaneously have or achieve the functions or purposes of all prompt words (i.e., different prompt words are not compatible with each other); on the other hand, in the face of all prompt words, the large model may pay more attention to the logic of the initial code itself and cannot simultaneously refactor or evolve and expand the initial code according to all prompt words. The above two aspects ultimately result in the refactored code not being required and not meeting the needs of the field of new energy monitoring and intelligent control. SUMMARY
[0005] To solve the problems of prompt word conflict and incompatibility and insufficient refactoring capability of the large model when refactoring the code running on hardware devices with limited hardware resources, the present application provides a code automatic generation method and system based on an algorithm model.
[0006] The code automatic generation method and system based on an algorithm model of the present application adopt the following technical solutions:
[0007] An embodiment of the present application provides a code automatic generation method based on an algorithm model, which includes the following steps:
[0008] The initial code and a plurality of prompt words are used to refactor the code to obtain the final refactored code by combining the RAG technology, including:
[0009] D1: input each prompt word and the initial code into the large model to obtain the first refactored code of each prompt word, and obtain the first incompatibility degree between different prompt words according to the differences between the first refactored codes of different prompt words and the initial code;
[0010] D2: retrieving several reference knowledges with the greatest similarity to each prompt word from the vector database in the RAG technology, inputting the reference knowledges, all prompt words and the initial code into the large model to obtain a second reconstruction code; wherein the similarity of each reference knowledge to each prompt word is positively correlated with the first incompatibility degree of each prompt word;
[0011] D3: obtaining the second incompatibility degree between different prompt words according to the difference between the first reconstruction code and the second reconstruction code of different prompt words; re-executing D2 once, and taking the second reconstruction code obtained after re-executing D2 as the final reconstruction code; wherein when re-executing D2, the similarity of each reference knowledge to each prompt word is recalculated according to the reduction of the second incompatibility degree relative to the first incompatibility degree.
[0012] Preferably, the first incompatibility degree between different prompt words is obtained according to the difference between the first reconstruction code of different prompt words and the initial code, comprising the following specific steps:
[0013] obtaining the AST node tree of the initial code by using the AST technology, denoted as the initial syntax tree, and obtaining the AST node tree of the first reconstruction code of each prompt word by using the AST technology, denoted as the first reconstruction tree of each prompt word;
[0014] encoding the initial syntax tree and the first reconstruction tree of each prompt word into the initial code vector and the first reconstruction code vector of each prompt word respectively; taking the difference between the first reconstruction code vector of each prompt word and the initial code vector as the code reconstruction feature of each prompt word, representing the relative difference between the first reconstruction code and the initial code; obtaining the cosine similarity x of the code reconstruction features of any two prompt words, and the first incompatibility degree between any two prompt words is negatively correlated with x.
[0015] Preferably, the similarity of each reference knowledge to each prompt word is positively correlated with the first incompatibility degree of each prompt word, comprising the following specific steps:
[0016] taking any one prompt word as a target prompt word, obtaining the mean value of the first incompatibility degree between the target prompt word and all prompt words other than the target prompt word, denoted as the first average incompatibility degree of the target prompt word;
[0017] calculating the cosine similarity between the prompt word vector of the target prompt word and the embedding vector of each reference knowledge in the vector database; taking the cosine similarity multiplied by several times as the similarity of each reference knowledge to each prompt word; wherein the multiplication times are positively correlated with the first average incompatibility degree of the target prompt word.
[0018] Preferably, the specific steps of recalculating the similarity between each reference knowledge and each prompt word based on the reduction in the second incompatibility level relative to the first incompatibility level are as follows:
[0019] The mean of the first incompatibility degree between any prompt word and other prompt words is denoted as B1, and the mean of the second incompatibility degree between any prompt word and other prompt words is denoted as B2. The mean of B1 and B2 is the second average incompatibility degree of the prompt word. The recalculated similarity is positively correlated with the reduction amount and also positively correlated with the second average incompatibility degree.
[0020] Preferably, the specific steps for retrieving the reference knowledge with the highest similarity to each prompt word from the vector database in RAG technology are as follows:
[0021] Each piece of data in the vector database serves as a piece of reference knowledge, containing a text information and an embedding vector for that text information, which comes from technical manuals and historical code snippets.
[0022] In the vector database, several reference knowledge items with the highest similarity to the prompt word vector of each prompt word are obtained. Among the reference knowledge items obtained for all prompt words, several reference knowledge items with the highest similarity are selected again as the retrieved reference knowledge items.
[0023] Preferably, the reduction in the second degree of incompatibility relative to the first degree of incompatibility is positively correlated with the difference between the mean of the first degree of incompatibility among all prompt words and the mean of the second degree of incompatibility among all prompt words.
[0024] Preferably, after obtaining the final refactored code, the following step D4 is repeated several times:
[0025] D4: Use the final refactored code as the second refactored code, and then re-execute D3.
[0026] Preferably, after each execution of D4, the second degree of incompatibility between the different prompt words obtained is recorded as the incompatibility degree set;
[0027] In each set of incompatibility levels, the average of the second incompatibility levels between any given prompt word and all other prompt words is denoted as the incompatibility coefficient of that prompt word; the prompt word with the largest incompatibility coefficient is marked as an abnormal prompt word;
[0028] The ratio of the number of times each prompt word is marked as an anomalous prompt word to the number of incompatibilities in all sets of incompatibilities obtained during all executions of D4 is called the anomalous probability of each prompt word.
[0029] The entropy value of the abnormal probability of all prompt words is calculated, and when the entropy value is less than a second preset threshold, the repeating execution of D4 is continued until the entropy value is greater than or equal to the second preset threshold, and the repeating execution of D4 is stopped; after the repeating execution of D4 is stopped, the prompt word with the maximum abnormal probability is used for early warning.
[0030] Preferably, the similarity between each reference knowledge and each prompt word is recalculated according to a reduction amount of the second incompatibility degree relative to the first incompatibility degree, and the specific formula includes:
[0031] The recalculated similarity between each reference knowledge and each prompt word is equal to r x (1+f2), wherein r represents the cosine similarity between the embedding vector of each reference knowledge and the prompt vector of each prompt word, and f2 represents the mean of the reduction amount and the second average incompatibility degree.
[0032] Another embodiment of the present application provides a code automatic generation system based on an algorithm model, which comprises a memory, a processor and a computer program stored in the memory and executable on the processor, and the processor executes all steps of the code automatic generation method based on the algorithm model when executing the computer program.
[0033] The technical scheme of the present application has the following beneficial effects:
[0034] The present application realizes objective quantification of multi-prompt word conflict incompatibility by calculating the difference (first incompatibility degree) of the first reconstructed code generated by different prompt words and the initial code, and the difference (second incompatibility degree) of the second reconstructed code, avoiding the problems of logical contradiction or unreliable generation results caused by multi-prompt word conflict in traditional RAG.
[0035] Further, the present application uses incompatibility degree change to drive retrieval strategy update, that is, dynamically adjusts the similarity of the vector database according to the reduction amount of the incompatibility degree, so that the retrieval result is more focused on reconciling the conflicting or incompatible reference knowledge, enhances the reconstruction ability of the large model to the obviously incompatible prompt words, and finally obtains the final reconstructed code by using the enhanced large model, which forms an adaptive optimization closed loop, and in the case of prompt word incompatibility and insufficient code reconstruction ability of the large model, the code reconstruction can be performed as much as possible by using all prompt words, so that the generated reconstructed code can meet the demand of the new energy monitoring and intelligent control field. BRIEF DESCRIPTION OF DRAWINGS
[0036] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings needed to be used in the embodiments or prior art description. Obviously, the drawings in the following description only constitute some embodiments of the present application, and for those skilled in the art, other drawings can also be obtained from these drawings without creative labor.
[0037] Figure 1 A step flow chart of a code automatic generation method based on an algorithm model provided by an embodiment of the present application. DETAILED DESCRIPTION
[0038] In order to further illustrate the technical means and effects adopted by the present application to achieve the predetermined invention purpose, the following will combine the drawings and preferred embodiments to specifically describe the code automatic generation method and system based on an algorithm model according to the present application, its specific implementation, structure, features and effects in detail. In the following description, different "one embodiment" or "another embodiment" do not necessarily refer to the same embodiment. In addition, the specific features, structures or characteristics in one or more embodiments can be combined in any suitable form.
[0039] Unless otherwise defined, all technical and scientific terms used herein have the same meaning as commonly understood by one of ordinary skill in the art to which the present application belongs.
[0040] The specific scheme of the code automatic generation method and system based on an algorithm model provided by the present application will be specifically described below in combination with the drawings.
[0041] Embodiment one:
[0042] Please refer to Figure 1 which shows a step flow chart of a code automatic generation method based on an algorithm model provided by an embodiment of the present application. The method comprises the following steps:
[0043] Step S101, respectively input each prompt word and initial code into the large model to obtain the first reconstruction code of each prompt word.
[0044] The initial code described in the present embodiment refers to the code that needs to be reconstructed.
[0045] In some embodiments, the initial code refers to the code written in historical project engineering (such as wind and light power prediction, hydropower optimization scheduling, equipment state monitoring, etc.); with the update of hardware devices, it is necessary to migrate the historical code to new hardware devices, at this time, it is necessary to adapt to the new hardware environment, and code reconstruction is needed.
[0046] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0047] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0048] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0049] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0050] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0051] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0052] In some embodiments, the initial code refers to simulation generated code, such as Simulink generated code, which may not be directly run on different hardware devices and needs to be reconstructed.
[0053] As an example, the acquisition method of the first incompatibility degree is:
[0054] An AST node tree (Abstract Syntax Tree) of the initial code is acquired by using the AST technology, denoted as an initial syntax tree, and an AST node tree of the first reconstruction code of each hint word is acquired by using the AST technology, denoted as a first reconstruction tree of each hint word.
[0055] The AST node tree has a tree-like node hierarchy, and each node contains a type, an attribute, and a child node. The AST node tree is a known technology, and the present embodiment will not be described in detail.
[0056] The initial syntax tree and the first reconstruction tree of each hint word are respectively encoded into vectors by using the vector embedding technology, denoted as an initial code vector and a first reconstruction code vector of each hint word.
[0057] The difference between the first reconstruction code vector of each hint word and the initial code vector is denoted as a code reconstruction feature of each hint word, indicating the distribution position of the first reconstruction code vector relative to the initial code vector, and is used to describe the relative difference between the first reconstruction code and the initial code.
[0058] The cosine similarity x of the code reconstruction features of any two hint words is acquired, and the first incompatibility degree of any two hint words is negatively correlated with x.
[0059] As an example, the first incompatibility degree is equal to exp(-x), where exp() represents an exponential function with a natural constant as the base.
[0060] The greater the cosine similarity x, the smaller the difference between the first reconstruction code of any two hint words and the initial code, and the more likely any two hint words can be used for code reconstruction at the same time without obvious reconstruction conflicts or incompatibility, that is, the first incompatibility degree is small. The smaller the cosine similarity x, the greater the difference between the first reconstruction code of any two hint words and the initial code, and the more likely there will be conflicts or incompatibility (for example, having completely opposite code logic) when two hint words are used for code reconstruction at the same time, resulting in difficulty in code reconstruction or difficulty in simultaneously implementing the functions of the two hint words in the same code, that is, the first incompatibility degree is large.
[0061] So far, in this step, the incompatibility between different prompt words is analyzed (with the first incompatibility degree between different prompt words described). For the first incompatibility degree between all prompt words, if the first incompatibility degree between all prompt words is less than or equal to the first preset threshold th1, then all subsequent steps of the embodiment are no longer executed, and code reconstruction is directly performed according to the above-mentioned conventional method. If there is a first incompatibility degree greater than th1, it means that there is a certain degree of incompatibility between the prompt words, and at this time the subsequent steps of the embodiment are continued. In the embodiment, th1 is set to 0.3, and the preferred value range of th1 is (0, 1).
[0062] In step S103, a plurality of reference knowledge with the greatest similarity to each prompt word is retrieved from the vector database in the RAG technology; the similarity of each reference knowledge to each prompt word is positively correlated with the first incompatibility degree of each prompt word.
[0063] The first incompatibility degree between different prompt words described above is obtained without considering the reconstruction ability of the large model itself. In fact, the large model may also have a weak reconstruction ability, which may cause the first incompatibility degree between different prompt words to become larger.
[0064] In the embodiment, the reconstruction ability of the large model is first improved based on the first incompatibility degree described above, and finally the large model with improved reconstruction ability is used to re-evaluate the incompatibility between different prompt words, and then the initial code is reconstructed, so that the finally reconstructed code can take into account all prompt words at the same time.
[0065] For improving the reconstruction ability of the large model, it needs to be explained that one of the reasons why the reconstruction ability of the large model is weak is that the structure and training precision of the large model are not suitable. However, the GPT4, DeepSeek and other large models used in the embodiment already have a relatively suitable structure and training precision. Therefore, the large model is not improved in the embodiment. Another reason why the reconstruction ability of the large model is weak is that the large model is not provided with enough reference knowledge, so that the large model is too dependent on or limited by the coding logic of the initial code itself, and cannot reasonably diversify and expand on the basis of the initial code.
[0066] In the embodiment, Retrieval-augmented Generation (RAG) technology is used to improve the reconstruction ability of the large model.
[0067] First, a vector database in the RAG technology is constructed, and each piece of data in the vector database includes text information and an embedding vector of the text information. In the embodiment, each piece of data is regarded as a piece of reference knowledge.
[0068] As an example, the method of constructing the vector database in the RAG technology is as follows:
[0069] The technical manual of the hardware device is divided into paragraphs, the text content in the technical manual is divided into text blocks, the text blocks are encoded into vectors by using the Sentence-BERT technology, the text blocks are stored as text information in the vector database, and the vectors are embedded vectors of the text information and are also stored in the vector database.
[0070] The code of the historical project or project is divided according to the implemented functions or function definitions, the historical code is divided into code segments, the annotation information of each code segment and the code segment are stored as text information in the vector database, the annotation information is encoded into a vector by using the Sentence-BERT technology, and the vector is an embedded vector of the text information and is also stored in the vector database.
[0071] In some embodiments, a large model can be used to generate annotation information for each code segment.
[0072] Further, each prompt word is encoded into a vector by using the Sentence-BERT technology, and the vector is recorded as a prompt word vector of each prompt word. According to the similarity between each piece of reference knowledge in the vector database and each prompt word, the topK pieces of reference knowledge with the largest similarity are retrieved from the vector database.
[0073] As a comparative example, the method of retrieving the topK pieces of reference knowledge with the largest similarity from the vector database according to the similarity between each piece of reference knowledge in the vector database and each prompt word includes the following steps:
[0074] The cosine similarity between each prompt word vector and the embedded vector of each piece of reference knowledge in the vector database is calculated, and the similarity between each piece of reference knowledge and each prompt word is recorded as topK reference knowledge with the largest similarity, which is used as the initial knowledge of each prompt word.
[0075] For all initial knowledge of the prompt words, each initial knowledge corresponds to a similarity, and the topK pieces of initial knowledge with the largest similarity are selected again from all initial knowledge of the prompt words, which are used as the retrieved topK pieces of reference knowledge. In particular, when there are repeated knowledge in the retrieved topK pieces of reference knowledge, the repeated knowledge is removed, and if the reference knowledge is less than topK, the initial knowledge with the largest similarity is obtained again from the remaining initial knowledge of all prompt words as the retrieved reference knowledge, so that the retrieved reference knowledge is equal to topK.
[0076] In this example, topK is equal to 20. In other embodiments, it can be set to other values, for example, in some embodiments, topK is equal to 4 times the number of prompt words.
[0077] As a preferred example, according to the similarity between each reference knowledge in the vector database and each prompt word, the topK reference knowledge with the largest similarity is retrieved from the vector database, including the method:
[0078] Record any one prompt word as the target prompt word, obtain the mean value of the first incompatibility degree of the target prompt word with all prompt words except the target prompt word, and record it as the first average incompatibility degree of the target prompt word.
[0079] Calculate the cosine similarity between the prompt word vector of the target prompt word and the embedding vector of each reference knowledge in the vector database; the cosine similarity after increasing by several times is used as the similarity between each reference knowledge and each prompt word. The increased multiple is positively correlated with the first average incompatibility degree of the target prompt word.
[0080] Then, the topK reference knowledge retrieved is obtained according to the method of the above comparative example.
[0081] As an example, the increased multiple is equal to the first average incompatibility degree, that is, the similarity is equal to the cosine similarity multiplied by 1 plus the first average incompatibility degree.
[0082] The reference knowledge acquisition process of all prompt words in the above comparative example is independent of each other, for example, compatible or incompatible prompt words obtain the same amount of reference knowledge. The purpose of this step is to improve the reconstruction ability of the large model, and in improving the reconstruction ability of the large model, the focus is on whether the large model can reliably reconstruct the incompatible prompt words (i.e., prompt words with a larger first average incompatibility degree) analyzed above; in this preferred example, the larger the first average incompatibility degree, the more difficult it is for the target prompt word to be compatible with other prompt words, and the more incompatible prompt words. Compared with the comparative example, the similarity corresponding to the incompatible prompt word is significantly increased, which is helpful for subsequent reconstruction of the initial code by using more reference knowledge related to the incompatible prompt word, and on the basis of improving the reconstruction ability of the large model, the large model is used. The incompatible prompt words analyzed above are reconstructed again under the condition of strong reconstruction ability.
[0083] Step S104, input the retrieved reference knowledge, all prompt words and initial code into the large model to obtain the second reconstruction code. According to the difference between the first reconstruction code and the second reconstruction code of different prompt words, the second incompatibility degree between different prompt words is obtained.
[0084] Input all the retrieved reference knowledge and all the prompt words and the initial code into the large model to obtain the second reconstruction code.
[0085] It should be noted that the above steps significantly amplify the similarity of the first large incompatible prompt word, so that all reference knowledge tends to contain more relevant knowledge of the first large incompatible prompt word. At this time, the process of obtaining the second reconstruction code is equivalent to enhancing the reconstruction ability of the large model to the obviously incompatible prompt word.
[0086] According to the difference between the first reconstruction code and the second reconstruction code of different prompt words, the second incompatible degree between different prompt words is obtained, and the specific process is the same as step S102; the second incompatible degree describes the incompatible situation that still exists after the reconstruction ability of the large model to the obviously incompatible prompt word is enhanced.
[0087] In step S105, the similarity between each reference knowledge and each prompt word is recalculated according to the reduction amount of the second incompatible degree relative to the first incompatible degree.
[0088] The reduction amount of the second incompatible degree between all prompt words relative to the first incompatible degree between all prompt words is obtained.
[0089] If the reduction amount is larger, it means that on the basis of the incompatible situation between different prompt words analyzed in step S102 (i.e., on the basis of the first incompatible degree obtained in step S102), after enhancing the reconstruction ability of the large model to the incompatible prompt word according to steps S103 and S104, the incompatible degree between all prompt words is improved (i.e., the overall incompatible degree is significantly reduced), which means that the incompatible situation analyzed in step S102 is not a serious and universal incompatible situation between different prompt words. It is only that the first incompatible degree is relatively large due to the weak reconstruction ability of the large model when step S101 is executed. Further, the incompatible situation between different prompt words can be improved by enhancing the reconstruction ability of the large model to the incompatible prompt word.
[0090] If the reduction amount is smaller, it means that on the basis of the incompatible situation between different prompt words analyzed in step S102 (i.e., on the basis of the first incompatible degree obtained in step S102), after enhancing the reconstruction ability of the large model to the incompatible prompt word according to steps S103 and S104, the incompatible degree between all prompt words is not significantly improved, or even not improved (i.e., the overall incompatible degree is not significantly reduced, or even increased), which means that there is a serious and universal incompatible situation between different prompt words, which is not caused by the relatively large first incompatible degree due to the weak reconstruction ability of the large model. Further, the incompatible prompt words cannot be combined and reconstructed by enhancing the reconstruction ability of the large model to the incompatible prompt word.
[0091] As an example, the reduction amount of the second incompatibility degree between all prompt words relative to the first incompatibility degree between all prompt words is obtained, including:
[0092] A mean value of the first incompatibility degree between all prompt words is obtained, denoted as A1; a mean value of the second incompatibility degree between all prompt words is obtained, denoted as A2;
[0093] A ratio of A2-A1 to A1 is denoted as the reduction amount.
[0094] In some other examples, a ratio of w1x A2-A1 to A1 is denoted as the reduction amount, where w1 represents a scaling coefficient, and w1=0.8 in the present example, and the preferred value range of w1 is (0, 1]. The purpose of introducing w1 is to make the reduction amount more likely to be less than 0, so as to avoid the knowledge information of incompatible prompt words from excessively interfering with the reconstruction process of other prompt words in the subsequent process (see the subsequent steps for details).
[0095] Further, the similarity between each reference knowledge and each prompt word is recalculated according to the reduction amount; a mean value of the first incompatibility degree between any one prompt word and other prompt words except the prompt word is denoted as B1, a mean value of the second incompatibility degree between any one prompt word and other prompt words except the prompt word is denoted as B2, and a mean value of B1 and B2 is the second average incompatibility degree of the prompt word. The recalculated similarity is positively correlated with the reduction amount and the second average incompatibility degree of the prompt word.
[0096] When the reduction amount is greater, it means that all prompt words can be combined together for reconstruction by improving the reconstruction ability of the large model on incompatible prompt words, where the incompatible prompt words describe prompt words with a larger second average incompatibility degree, and the similarity of the prompt words needs to be increased.
[0097] When the reduction amount is smaller, it means that all prompt words cannot be combined together for reconstruction by improving the reconstruction ability of the large model on incompatible prompt words, and the similarity of the prompt words does not need to be excessively increased. When the reduction amount is less than 0, the similarity corresponding to the prompt words with a larger second average incompatibility degree (i.e., incompatible prompt words) will be greatly reduced, which can avoid the knowledge information of incompatible prompt words from excessively interfering with the reconstruction process of other prompt words.
[0098] As an example, the recalculated similarity between each reference knowledge and each prompt word is equal to r x (1+f2), where r represents the cosine similarity between the embedding vector of each reference knowledge and the prompt vector of each prompt word, and f2 represents the mean value of the reduction amount and the second average incompatibility degree. In some other embodiments, f2 can also be set as the product of the reduction amount and the second average incompatibility degree.
[0099] Step S106, code reconstruction is performed according to the similarity of each reference knowledge and each prompt word re-calculated.
[0100] For the similarity of each reference knowledge and each prompt word re-calculated, the topK reference knowledge with the largest similarity is re-retrieved from the vector database by using the method of step S103.
[0101] All the retrieved reference knowledge, all the prompt words and the initial code are input into the large model to obtain the final reconstructed code.
[0102] The final reconstructed code is taken as the code to be generated by the user and is run in the hardware device. In some embodiments, manual modification can be performed on the final reconstructed code again to further improve its performance, and then it is run in the hardware device.
[0103] It should be noted that for the reference knowledge of all steps of the present embodiment, if the text information contained in the reference knowledge is code comment information, the code segment corresponding to the comment information is input into the large model, and the comment information is not input into the large model. In the present embodiment, the reference knowledge retrieved from the vector database is used for large model processing (i.e., the reference knowledge and prompt words are input into the large model), which is a known content of RAG technology, and the present embodiment does not perform specific description.
[0104] Embodiment two:
[0105] When the final reconstructed code is obtained, the second incompatibility degree between different prompt words is re-obtained according to the difference between the first reconstructed code and the final reconstructed code of different prompt words (specifically the same as step S102). The similarity of each reference knowledge and each prompt word is re-calculated according to the reduction amount of the second incompatibility degree relative to the first incompatibility degree, and then code reconstruction is performed according to the similarity of each reference knowledge and each prompt word re-calculated to obtain the final reconstructed code (the same as steps S103 to S106).
[0106] After repeating the above process of the present embodiment several times (for example, 5 times), the final reconstructed code obtained is run in the hardware device.
[0107] When the present embodiment is repeatedly executed, the similarity of each reference knowledge and each prompt word is dynamically adjusted. For example, when the prompt words that are incompatible and cannot be further reconstructed by improving the reconstruction ability of the large model have too large similarity, the reduction amount obtained in S105 will be less than 0, which will make the similarity of these prompt words smaller in the subsequent process.
[0108] In summary, the embodiment avoids the incompatible prompt words in Embodiment One that have too large similarity (which will cause the incompatible prompt words to pollute or interfere with the knowledge of the reconstruction process) and the compatible prompt words that can be further reconstructed by the large model reconstruction (which will avoid the large model lacking relevant knowledge and weak reconstruction ability). Finally, the reference knowledge retrieved from the vector database can assist the large model in reliable code reconstruction, taking into account all prompt words as much as possible to obtain the reconstruction code that meets the requirements.
[0109] Embodiment Three:
[0110] When Embodiment Two is repeatedly executed, the second incompatible degrees between different prompt words are continuously obtained, and all the second incompatible degrees between the prompt words obtained each time are recorded as an incompatible degree set.
[0111] The difference between this embodiment and Embodiment Two is that:
[0112] When the number of times of repeatedly executing Embodiment Two is greater than or equal to 5 times:
[0113] After each time of repeatedly executing Embodiment Two, all the incompatible degree sets obtained in the process of repeatedly executing Embodiment Two (including the incompatible degree set composed of the second incompatible degrees between all the prompt words obtained in step S104) are obtained. Each incompatible degree set contains the second incompatible degrees between all the prompt words. In each incompatible degree set, the average of the second incompatible degrees between any one prompt word and all the prompt words other than the prompt word is recorded as the incompatible coefficient of the prompt word; the prompt word with the largest incompatible coefficient in the incompatible degree set is marked as an abnormal prompt word.
[0114] In all incompatible degree sets, an abnormal prompt word is marked in each incompatible degree set, and the same prompt word may be marked as an abnormal prompt word in different incompatible degree sets.
[0115] In all incompatible degree sets, the number of times each prompt word is marked as an abnormal prompt word is obtained. The ratio of the number of times to the number of incompatible degree sets is recorded as the abnormal probability of each prompt word.
[0116] The greater the abnormal probability of each prompt word, the more often (or frequently) the prompt word calculates a larger second incompatible degree with other prompt words, indicating that the prompt word should not be used for code reconstruction.
[0117] The entropy value of the abnormal probability of all prompt words is calculated. The smaller the entropy value, the more uncertain the probability of all prompt words being labeled as abnormal prompt words, that is, all prompt words have the same or similar opportunity to be labeled as abnormal prompt words, indicating that there is no clear prompt word with incompatible characteristics in all prompt words. The larger the entropy value, the more there are some prompt words in all prompt words, which are frequently labeled as abnormal prompt words, and other prompt words are rarely labeled as abnormal prompt words, indicating that there are prompt words with incompatible characteristics in all prompt words.
[0118] When the entropy value is less than the second preset threshold th2, it indicates that the entropy value is small, and the prompt word with incompatible characteristics has not been mined in all prompt words. At this time, the embodiment two is repeatedly executed until the entropy value is greater than or equal to the second preset threshold th2, or the number of repeated execution is greater than 10, and the repeated execution is stopped. In this embodiment, th2 = 1.2, and the preferred value range of th2 is [0.1, 1.6].
[0119] When the entropy value is greater than or equal to the second preset threshold th2, it indicates that the entropy value is large, and the prompt word with incompatible characteristics has been mined in all prompt words. At this time, the repeated execution of embodiment two is stopped, and the prompt word with the maximum abnormal probability is obtained, prompting the user that the prompt word is not suitable for code refactoring.
[0120] Embodiment four:
[0121] In step S101 of embodiment one, the prompt word and the initial code are input into the large model to obtain the first refactoring code of the prompt word.
[0122] The difference between this embodiment and embodiment one is that the first refactoring code is obtained by combining the RAG technology, which includes:
[0123] The cosine similarity of the prompt word vector of the prompt word and the embedding vector of each reference knowledge in the vector database is calculated, and the topK reference knowledge with the maximum cosine similarity is retrieved. The retrieved reference knowledge, the prompt word and the initial code are input into the large model to obtain the first refactoring code of the prompt word.
[0124] Embodiment five:
[0125] As an example, the initial syntax tree and the first refactoring tree of each prompt word are respectively encoded into vectors by using vector embedding technology, which includes:
[0126] The initial syntax tree or the first refactoring tree is encoded into a vector by using self-encoding technology. The self-encoding technology uses an encoder to compress and encode the initial syntax tree or the first refactoring tree into a vector, and then uses a decoder to restore the vector. This technology is a known technology, and this embodiment will not be described in detail.
[0127] It should be noted that the initial syntax tree or the first reconstruction tree is regarded as a graph structure in this embodiment, and both the encoder and the decoder use a graph neural network.
[0128] As another example, the initial syntax tree and the first reconstruction tree of each prompt word are respectively encoded into vectors by using a vector embedding technology, including:
[0129] The initial syntax tree or the first reconstruction tree is converted into a plurality of node sequences by depth-first traversal (DFT), each node sequence is regarded as a text sentence, and then all node sequences are encoded into vectors by using a Sentence-BERT technology.
[0130] Embodiment six:
[0131] The embodiment provides a code automatic generation system based on an algorithm model, which comprises a memory, a processor, and a computer program stored in the memory and executable on the processor, and the processor executes all steps in all embodiments when executing the computer program.
[0132] The above only describes the preferred embodiments of the present application and is not intended to limit the present application. Any modification, equivalent replacement, improvement, etc. within the principles of the present application shall be included in the protection scope of the present application.
Claims
1. A method for automatic code generation based on an algorithm model, characterized in that, The method includes the following steps: By combining RAG technology with the initial code and several prompts, the final refactored code is obtained, including: D1: Input each prompt word and the initial code into the large model to obtain the first reconstructed code for each prompt word. Based on the difference between the first reconstructed code of different prompt words and the initial code, obtain the first degree of incompatibility between different prompt words. D2: Retrieve several reference knowledge sets with the highest similarity to each prompt word from the vector database in RAG technology, and input the reference knowledge sets, all prompt words, and the initial code into the large model to obtain the second reconstructed code; wherein, the similarity between each reference knowledge set and each prompt word is positively correlated with the first incompatibility degree of each prompt word; D3: Obtain the second degree of incompatibility between different prompt words based on the difference between the first and second reconstruction codes of different prompt words; re-execute D2 once, and use the second reconstruction code obtained after re-execution of D2 as the final reconstruction code; wherein, when re-executing D2, recalculate the similarity between each reference knowledge and each prompt word based on the reduction of the second degree of incompatibility relative to the first degree of incompatibility. The specific steps involved in obtaining the first incompatibility degree between different prompt words based on the differences between the first reconstructed code and the initial code for each prompt word are as follows: The AST node tree of the initial code is obtained using AST technology and is denoted as the initial syntax tree. The AST node tree of the first reconstructed code for each prompt word is obtained using AST technology and is denoted as the first reconstructed tree for each prompt word. The initial syntax tree and the first reconstruction tree of each prompt word are encoded into an initial code vector and a first reconstruction code vector of each prompt word, respectively. The difference between the first reconstruction code vector and the initial code vector of each prompt word is recorded as the code reconstruction feature of each prompt word, representing the relative difference between the first reconstruction code and the initial code. The cosine similarity x of the code reconstruction features of any two prompt words is obtained, and the degree of incompatibility between any two prompt words is negatively correlated with x. Similarly, using the same difference analysis method as the first incompatibility degree, the second incompatibility degree between different prompt words is obtained based on the difference between the first and second reconstructed codes of different prompt words; Obtain the reduction in the second incompatibility level among all prompt words relative to the first incompatibility level among all prompt words, including: Get the average of the first degree of incompatibility among all prompt words, denoted as A1; get the average of the second degree of incompatibility among all prompt words, denoted as A2; The ratio of A2-A1 to A1 is recorded as the reduction amount; The specific steps involved in recalculating the similarity between each reference knowledge and each prompt word based on the reduction in the second incompatibility level relative to the first incompatibility level are as follows: The mean of the first incompatibility degree between any prompt word and other prompt words is denoted as B1, and the mean of the second incompatibility degree between any prompt word and other prompt words is denoted as B2. The mean of B1 and B2 is the second average incompatibility degree of the prompt word. The recalculated similarity is positively correlated with the reduction amount and also positively correlated with the second average incompatibility degree.
2. The method for automatic code generation based on an algorithm model according to claim 1, characterized in that, The similarity between each piece of reference knowledge and each prompt word is positively correlated with the first degree of incompatibility of each prompt word, and the specific steps include the following: Let any one of the prompt words be the target prompt word. Then, obtain the average of the first incompatibility degree between the target prompt word and all other prompt words, and denote it as the first average incompatibility degree of the target prompt word. Calculate the cosine similarity between the target prompt word's prompt word vector and the embedding vector of each reference knowledge in the vector database; increase the cosine similarity by a certain factor and use it as the similarity between each reference knowledge and each prompt word; where the factor is positively correlated with the first average incompatibility degree of the target prompt word.
3. The method for automatic code generation based on an algorithm model according to claim 1, characterized in that, The specific steps involved in retrieving the reference knowledge with the highest similarity to each prompt word from the vector database in RAG technology are as follows: Each piece of data in the vector database serves as a piece of reference knowledge, containing a text information and an embedding vector for that text information, which comes from technical manuals and historical code snippets. In the vector database, several reference knowledge items with the highest similarity to the prompt word vector of each prompt word are obtained. Among the reference knowledge items obtained for all prompt words, several reference knowledge items with the highest similarity are selected again as the retrieved reference knowledge items.
4. The method for automatic code generation based on an algorithm model according to claim 1, characterized in that, The reduction in the second degree of incompatibility relative to the first degree of incompatibility is positively correlated with the difference between the mean of the first degree of incompatibility among all prompt words and the mean of the second degree of incompatibility among all prompt words.
5. The method for automatic code generation based on an algorithm model according to claim 1, characterized in that, After obtaining the final refactored code, repeat step D4 as follows several times: D4: Use the final refactored code as the second refactored code, and then re-execute D3.
6. The method for automatic code generation based on an algorithm model according to claim 5, characterized in that, After each execution of D4, the second degree of incompatibility between the different prompt words obtained is recorded as the incompatibility degree set; In each set of incompatibility levels, the average of the second incompatibility levels between any given prompt word and all other prompt words is denoted as the incompatibility coefficient of that prompt word; the prompt word with the largest incompatibility coefficient is marked as an abnormal prompt word; The ratio of the number of times each prompt word is marked as an anomalous prompt word to the number of incompatibilities in all sets of incompatibilities obtained during all executions of D4 is called the anomalous probability of each prompt word. Calculate the entropy value of the abnormal probability of all prompt words. When the entropy value is less than the second preset threshold, continue to execute D4 repeatedly until the entropy value is greater than or equal to the second preset threshold, then stop executing D4 repeatedly. After stopping the repeated execution of D4, use the prompt word with the highest abnormal probability to issue an alert.
7. The method for automatic code generation based on an algorithm model according to claim 1, characterized in that, The specific formula for recalculating the similarity between each reference knowledge and each prompt word based on the reduction in the second degree of incompatibility relative to the first degree of incompatibility is as follows: The recalculated similarity between each reference knowledge and each prompt word is equal to r×(1+f2), where r represents the cosine similarity between the embedding vector of each reference knowledge and the prompt word vector of each prompt word, and f2 represents the mean of the reduction amount and the second average incompatibility degree.
8. A code generation system based on an algorithm model, the system comprising: A memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that, when the processor runs the computer program, it performs all the steps of the automatic code generation method based on the algorithm model according to any one of claims 1 to 7.
Citation Information
Patent Citations
Code reconstruction method and system based on data stream slicing and large language model driving
CN119248347A
Large language model optimization generation method based on optimal cue word selection
CN119476209A