A method, apparatus, device and storage medium for generating code

By combining a large language model with knowledge graphs and syntax trees, code for model monitoring requirements is automatically generated, solving the problem of low efficiency in existing technologies and achieving efficient and accurate code generation.

CN119690394BActive Publication Date: 2025-11-21WEBANK (CHINA)
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202411750100.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-12-02
Publication Date
2025-11-21
Estimated Expiration
2044-12-02

AI Technical Summary

Technical Problem

Existing model monitoring methods rely on developers to manually write code when business requirements change, which is inefficient and cannot quickly respond to new monitoring needs.

Method used

By using a large language model, code is generated from the longest inference path with the highest semantic similarity to the new monitoring requirements. Combined with knowledge graphs and syntax trees, code that meets the new monitoring requirements is automatically generated.

Benefits of technology

It improves the automation and accuracy of code generation, reduces the workload of developers, and ensures the rationality and accuracy of the generated code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119690394B_ABST
    Figure CN119690394B_ABST
Patent Text Reader

Abstract

The application provides a method, device, equipment and storage medium for generating code, which comprises: obtaining an added monitoring requirement for a model and a syntax tree corresponding to source code of a code repository of the model; determining each sub-requirement corresponding to the added monitoring requirement according to the added monitoring requirement, and determining code information corresponding to each sub-requirement according to the syntax tree; determining, by a large language model, a longest reasoning path with the highest semantic similarity to the added monitoring requirement from a knowledge graph corresponding to each sub-requirement; inputting the longest reasoning path, code information corresponding to the longest reasoning path and a target reasoning path into the large language model to obtain code information corresponding to the target reasoning path as code corresponding to the added monitoring requirement; wherein the code information corresponding to the longest reasoning path is determined according to the code information corresponding to each sub-requirement, and the target reasoning path comprises each sub-requirement. The method can improve the generation efficiency and accuracy of the code corresponding to the added monitoring requirement.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, and in particular to a method, apparatus, device and storage medium for generating code. Background Technology

[0002] The purpose of model monitoring is to ensure that the performance of machine learning models in practical applications meets expectations. It provides model and strategy personnel with real-time analysis and monitoring of models and rules, and offers data support for subsequent iterative optimization of models and rules. This allows model and strategy personnel to determine whether adjustments to model parameters are necessary. For example, consider a user credit scoring financial model. This model outputs a corresponding credit score based on a user's transaction history. Financial institution staff can use the credit score output by the model to determine whether to grant a loan to a user. Suppose that after inputting user A's transaction history into the model, the output shows that user A has a high credit score, leading to a loan decision. However, if user A fails to repay on time, monitoring the model will reveal its low accuracy, allowing model personnel to optimize the model promptly.

[0003] In practice, business needs or requirements are often adjusted frequently. Correspondingly, the calculation of model monitoring metrics and monitoring reports will also change with these changes. For example, initially, the business may only require monitoring the stability of the model, so the monitoring report will only include metrics or data related to model stability. As the business develops, after a period of time, it may require monitoring not only the model's stability but also its accuracy. Therefore, the monitoring report will need to add accuracy-related content to reflect these changes in business needs.

[0004] In existing model monitoring methods, when business requirements change, developers mainly rely on analyzing the new requirements and writing code for them, which is labor-intensive and inefficient. Summary of the Invention

[0005] This application provides a method, apparatus, device, and storage medium for generating code, which improves the efficiency and accuracy of generating code corresponding to new monitoring requirements in the model.

[0006] Firstly, this application provides a method for generating code, the method comprising:

[0007] Obtain new monitoring requirements for the model and the syntax tree corresponding to the source code of the model's code repository;

[0008] Based on the newly added monitoring requirements, determine the sub-requirements corresponding to the newly added monitoring requirements, and based on the syntax tree, determine the code information corresponding to each sub-requirement;

[0009] The longest reasoning path with the highest semantic similarity to the newly added monitoring requirement is determined from the knowledge graph corresponding to each sub-requirement using a large language model.

[0010] The longest inference path, the code information corresponding to the longest inference path, and the target inference path are input into the large language model to obtain the code information corresponding to the target inference path, which is used as the code corresponding to the new monitoring requirement; wherein, the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

[0011] The code generation method provided in this application, after obtaining new monitoring requirements, first breaks down the new monitoring requirements into sub-requirements and determines the code information and knowledge graph corresponding to each sub-requirement. A large language model performs reasoning based on the knowledge graph corresponding to each sub-requirement, and determines the longest reasoning path with the highest semantic similarity to the new monitoring requirement from the final reasoning results. Then, the longest reasoning path, the code information corresponding to the longest reasoning path, and the target reasoning path are input into the large language model to obtain the code corresponding to the new monitoring requirement. Compared with existing methods that rely on manual development based on new monitoring requirements, this application obtains the code corresponding to new monitoring requirements through a large language model, reducing the workload of developers and improving the automation and efficiency of code generation. Furthermore, the longest reasoning path is determined by the large language model based on the knowledge graph corresponding to each requirement, improving the rationality and accuracy of the longest reasoning path, thereby improving the rationality and accuracy of the code corresponding to the new monitoring requirement.

[0012] In one possible design, determining the longest inference path with the highest semantic similarity to the newly added monitoring requirement from the knowledge graph corresponding to each sub-requirement using the large language model includes:

[0013] In the first round of reasoning, the knowledge graphs corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the first round of reasoning;

[0014] During the i-th round of reasoning, the reasoning result obtained after the (i-1)-th round of reasoning and the knowledge graph corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the i-th round of reasoning; wherein, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning; i is a positive integer and i is greater than 1.

[0015] From at least one longest inference path obtained from the last round of inference, determine the longest inference path with the highest semantic similarity to the newly added monitoring requirement.

[0016] In the first round of reasoning, the large language model bases its reasoning on the knowledge graphs corresponding to each sub-requirement, which improves the accuracy of the first round of reasoning results. In each subsequent round of reasoning, the large language model bases its reasoning on the reasoning results of the previous round and the knowledge graphs corresponding to each sub-requirement, which improves the accuracy of the reasoning results of each round, and thus improves the accuracy of at least one reasoning path obtained in the last round of reasoning.

[0017] In one possible design, any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning, and the correlation between the first reasoning path and the second reasoning path is greater than a preset correlation threshold.

[0018] When the correlation between the first inference path and the second inference path is greater than a preset correlation threshold, the first inference path and the second inference path can be merged to obtain a new inference path, thereby improving the rationality of the determination of the new inference path.

[0019] In one possible design, the process of determining the knowledge graph corresponding to each sub-requirement is as follows:

[0020] Determine the set of keywords corresponding to any sub-requirement;

[0021] Based on the set of keywords corresponding to the sub-requirement, at least one triplet corresponding to the sub-requirement is obtained, which serves as the knowledge graph corresponding to the sub-requirement.

[0022] Any triple includes a first keyword as the head entity, the relationship between the head entity and the tail entity, and a second keyword as the tail entity; the first keyword and the second keyword are two keywords from the keyword set corresponding to the sub-requirement.

[0023] This application determines at least one triple corresponding to each sub-requirement based on the keyword set corresponding to each sub-requirement, which serves as the knowledge graph for each sub-requirement. Any triple includes a head entity, the relationship between the head entity and the tail entity, and the tail entity. Therefore, based on the knowledge graph corresponding to the sub-requirement, the association between each keyword in the keyword set corresponding to the sub-requirement can be determined. Furthermore, the keyword set here is a filtered set of keywords, all of which are highly relevant to the sub-requirement. Thus, the knowledge graph determined based on the filtered keyword set can more accurately represent the characteristics of the sub-requirement, meaning that the knowledge graph corresponding to each sub-requirement in this application has high accuracy.

[0024] In one possible design, the set of keywords corresponding to any sub-requirement is determined, including:

[0025] For any one of the sub-requirements, input the sub-requirement into a preset word segmentation component to obtain the keyword set corresponding to the sub-requirement;

[0026] The keyword set corresponding to the sub-requirement is filtered based on the degree of relevance between the comment information corresponding to the code information of each keyword in the keyword set corresponding to the sub-requirement and the function to be implemented by the sub-requirement, thereby obtaining the filtered keyword set.

[0027] In this application, the keyword set corresponding to each sub-requirement can be obtained through a preset word segmentation component, which improves the efficiency of obtaining the keyword set, thereby improving the efficiency of determining the knowledge graph and code information corresponding to each subsequent sub-requirement, which is equivalent to improving the efficiency of generating code corresponding to new monitoring requirements.

[0028] In one possible design, the syntax tree consists of code files with ownership relationships, classes included in the code files, and functions included in the classes;

[0029] The step of determining the code information corresponding to each sub-requirement based on the syntax tree includes:

[0030] For any given sub-requirement, determine the set of keywords corresponding to that sub-requirement;

[0031] Based on the syntax tree, determine the class and function corresponding to each keyword in the keyword set corresponding to the sub-requirement;

[0032] Based on the classes and functions corresponding to each keyword in the sub-requirement, determine the code information corresponding to the sub-requirement.

[0033] Syntax trees can present complex source code in an organized manner and are easy to process. Compared with directly determining the code information corresponding to each sub-requirement based on the repository source code, determining the code information corresponding to each sub-requirement based on the syntax tree reduces the difficulty of determining the code information and improves the accuracy of the code information corresponding to each sub-requirement.

[0034] In one possible design, after obtaining the code corresponding to the newly added monitoring requirement, the following is also included:

[0035] Design unit test cases for the code corresponding to the new monitoring requirements and test them. Adjust and optimize the code corresponding to the new monitoring requirements based on the test results.

[0036] The code corresponding to the new monitoring requirements directly output by the large language model may have some code implementation effects or performance that do not meet expectations. Therefore, test cases can be designed for the code corresponding to the new monitoring requirements to test it, and the code that does not meet the test requirements can be modified until the test requirements are met, so as to obtain the final implementation code corresponding to the new monitoring requirements.

[0037] Secondly, this application also provides an apparatus for generating code, the apparatus comprising: a processing unit and a transceiver unit;

[0038] The transceiver unit is used to obtain new monitoring requirements for the model and the syntax tree corresponding to the source code of the model's code repository.

[0039] The processing unit is configured to determine each sub-requirement corresponding to the new monitoring requirement based on the new monitoring requirement, and to determine the code information corresponding to each sub-requirement based on the syntax tree;

[0040] The processing unit is also used to determine the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement from the knowledge graph corresponding to each sub-requirement through a large language model;

[0041] The transceiver unit is further configured to input the longest inference path, the code information corresponding to the longest inference path, and the target inference path into the large language model to obtain the code information corresponding to the target inference path, which serves as the code corresponding to the newly added monitoring requirement; wherein, the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

[0042] In one possible design, the transceiver unit is specifically used to: input the knowledge graph corresponding to each sub-requirement into the large language model during the first round of reasoning, and determine the reasoning result corresponding to the first round of reasoning;

[0043] During the i-th round of reasoning, the reasoning result obtained after the (i-1)-th round of reasoning and the knowledge graph corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the i-th round of reasoning; wherein, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning; i is a positive integer and i is greater than 1.

[0044] From at least one longest inference path obtained from the last round of inference, determine the longest inference path with the highest semantic similarity to the newly added monitoring requirement.

[0045] In one possible design, any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning, and the correlation between the first reasoning path and the second reasoning path is greater than a preset correlation threshold.

[0046] In one possible design, the processing unit, when determining the knowledge graph corresponding to each sub-requirement:

[0047] Determine the set of keywords corresponding to any sub-requirement;

[0048] Based on the set of keywords corresponding to the sub-requirement, at least one triplet corresponding to the sub-requirement is obtained, which serves as the knowledge graph corresponding to the sub-requirement.

[0049] Any triple includes a first keyword as the head entity, the relationship between the head entity and the tail entity, and a second keyword as the tail entity; the first keyword and the second keyword are two keywords from the keyword set corresponding to the sub-requirement.

[0050] In one possible design, the transceiver unit is specifically used to input any one of the sub-requirements into a preset word segmentation component to obtain a set of keywords corresponding to the sub-requirement; the processing unit is specifically used to filter the set of keywords corresponding to the sub-requirement based on the degree of relevance between the annotation information corresponding to the code information of each keyword in the set of keywords corresponding to the sub-requirement and the function to be implemented by the sub-requirement, thereby obtaining a filtered set of keywords.

[0051] In one possible design, the syntax tree consists of code files with hierarchical relationships, classes included in the code files, and functions included in the classes; the processing unit is specifically used to determine the set of keywords corresponding to any sub-requirement; determine the classes and functions corresponding to each keyword in the set of keywords corresponding to the sub-requirement according to the syntax tree; and determine the code information corresponding to the sub-requirement according to the classes and functions corresponding to each keyword in the sub-requirement.

[0052] In one possible design, the processing unit is further configured to design unit test cases for the code corresponding to the new monitoring requirement, and to adjust and optimize the code corresponding to the new monitoring requirement based on the test results.

[0053] Thirdly, this application also provides a code generation device, the device comprising: a processor, and a memory communicatively connected to the processor;

[0054] The memory stores computer-executed instructions;

[0055] The processor executes computer execution instructions stored in the memory to implement the method described in the first aspect above.

[0056] Fourthly, this application also provides a computer-readable storage medium comprising a program that, when executed on a device, causes the device to perform the method as described in any one of the first aspects above.

[0057] Fifthly, this application also provides a computer program product, the computer program product comprising a computer program that, when executed by a processor, implements the method described in the first aspect above. Attached Figure Description

[0058] To more clearly illustrate the technical solutions in the embodiments of this application, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.

[0059] Figure 1 This is a schematic diagram of the method for generating code provided in an embodiment of this application;

[0060] Figure 2 A schematic diagram of a syntax tree provided for an embodiment of this application;

[0061] Figure 3 This is a schematic diagram of the knowledge graph corresponding to the sub-requirements provided in the embodiments of this application;

[0062] Figure 4 Schematic diagram of the reasoning path corresponding to the sub-requirements provided in the embodiments of this application Figure 1 ;

[0063] Figure 5 Schematic diagram of the reasoning path corresponding to the sub-requirements provided in the embodiments of this application Figure 2 ;

[0064] Figure 6 Schematic diagram of the device structure for generating code provided in the embodiments of this application Figure 1 ;

[0065] Figure 7 Schematic diagram of the device structure for generating code provided in the embodiments of this application Figure 2 . Detailed Implementation

[0066] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, and not all embodiments. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0067] The application scenarios described in this application are for the purpose of more clearly illustrating the technical solutions of this application, and do not constitute a limitation on the technical solutions provided in this application. Those skilled in the art will understand that with the emergence of new application scenarios, the technical solutions provided in this application are also applicable to similar technical problems. In the description of this application, unless otherwise stated, "multiple" means two or more.

[0068] To address the issue of low code efficiency in existing acquisition models for handling new monitoring requirements, this application proposes the following... Figure 1 The method shown can be executed by a server, a chip within the server, or a functional module within the server; this application does not limit this. The following explanation uses a server as the executing entity. The method includes:

[0069] Step 101: Obtain the new monitoring requirements for the model and the syntax tree corresponding to the model's source code repository.

[0070] For example, with the rapid development of large-scale model technology, various large-scale models have a wide range of applications in work and life. For instance, as mentioned in the background section, in financial institutions, large-scale models can be used to predict users' credit scores based on their transaction history to formulate business strategies, or to perform related data analysis, greatly reducing the workload of relevant staff. To ensure that the model performs as expected, it is usually monitored for performance, stability, accuracy, and other dimensions, and model parameters are adjusted promptly based on the monitoring results. As actual business continues to grow, the demand for model monitoring will also continue to increase. Relevant staff can fill out new monitoring requests on the server according to a preset template and submit them; accordingly, the server will obtain the new monitoring requests for the model.

[0071] For example, suppose the server receives a new monitoring request for the model that is "to calculate the performance monitoring predicted failure rate and the actual failure rate in the model monitoring module, and display a line graph with time as the dimension".

[0072] For example, the server also needs to parse the source code of the code repository corresponding to the model to obtain a syntax tree. A syntax tree is an abstract representation of the source code's syntactic structure, representing the syntactic structure of a programming language in a tree-like form. Each node in the syntax tree represents a structure in the source code, such as a statement, an expression, or a declaration. The generation of a syntax tree typically involves three stages: lexical analysis, syntax analysis, and semantic analysis. Lexical analysis is the first stage of the code compilation process. It takes the source code as a character stream, scans and analyzes the input character sequence, and finally converts it into a sequence of words for output. Lexical analysis provides symbol nodes for the generation of the syntax tree. Syntax analysis is the second stage of the code compilation process, mainly performing logical analysis on the code. Its main task is to analyze the basic syntactic structure of the code based on the word sequence generated in the lexical analysis stage, according to the syntactic rules of the language being analyzed. Examples include methods, variables, expressions, and other ways of composing code. Semantic analysis is the third stage of the code compilation process, also mainly performing logical analysis on the code. The main analysis method is to perform context-dependent property checks and type checks on the source code to determine whether the source code is semantically accurate.

[0073] For example, if a faster syntax tree is needed to obtain the source code of a code repository, a syntax analyzer tool can be installed on a server to obtain the syntax tree. This application does not limit the type of syntax analyzer tool used; for example, the syntax tree can be obtained by installing the ANTLR tool. The syntax tree corresponding to the repository source code in this application is as follows: Figure 2 As shown, a model consists of code files with hierarchical relationships, the classes contained within those code files, and the functions contained within those classes. A code file contains at least one class (also called a Class), and a class contains at least one function (also called a function or method). When writing the source code for a model, developers typically add corresponding comments to the code files, classes, and functions to explain the code. For example, comments explain the functionality the code is meant to perform, thereby improving code readability.

[0074] Step 102: Determine the sub-requirements corresponding to the new monitoring requirements based on the new monitoring requirements, and determine the code information corresponding to each sub-requirement based on the syntax tree.

[0075] For example, after the server obtains the new monitoring requirement of the model through step 101, it first splits the new monitoring requirement to obtain the sub-requirements corresponding to the new monitoring requirement. This splitting process can be completed by a large language model. The new monitoring requirement is input into the large language model, the large language model splits the new monitoring requirement, and outputs the multiple sub-requirements obtained after splitting. The large language model can also be called an LLM model. This application does not limit the type of large language model used. For example, it can be a GPT series or a BERT series.

[0076] For example, taking the new monitoring requirement "calculate the predicted failure rate and actual failure rate in the model monitoring module, and display a line graph with time as the dimension" as an example, assuming that after this new monitoring requirement is input into the large language model, the sub-requirements output by the large language model include: implementing the performance monitoring indicator calculation function in the model monitoring module, implementing data processing and chart display functions, comparing the actual failure rate with the predicted failure rate, and generating a line graph comparison with time as the dimension. For ease of description, the above sub-requirements are denoted as sub-requirement A to sub-requirement D respectively.

[0077] For example, after obtaining each sub-requirement corresponding to the new monitoring requirement, the keyword set corresponding to each sub-requirement is then determined; specifically, for any one of the sub-requirements, the sub-requirement is input into a preset word segmentation component to obtain the keyword set corresponding to the sub-requirement; it should be noted that this application does not limit the type of word segmentation component used.

[0078] For example, the preset word segmentation component can be an Elasticsearch component (referred to as the ES component). Sub-requirements A through D are input into the ES component to obtain the keyword set corresponding to each sub-requirement. Suppose that the keyword set corresponding to sub-requirement A output by the ES component is {model monitoring, performance monitoring, function implementation}, the keyword set corresponding to sub-requirement B is {metric calculation, data processing, chart display}, the keyword set corresponding to sub-requirement C is {actual defect rate, predicted defect rate, difference comparison}, and the keyword set corresponding to sub-requirement D is {time dimension, line chart, trend comparison}.

[0079] Further exemplarily, after obtaining the keyword set corresponding to each sub-requirement output by the preset analysis component, the class and function corresponding to each keyword in the keyword set are determined according to the syntax tree, and used as the code information corresponding to each keyword. For example, the syntax tree can determine the class and function corresponding to each keyword in the keyword set {model monitoring, performance monitoring, function implementation} corresponding to sub-requirement A, and use it as the code information corresponding to each keyword; similarly, the syntax tree can determine the class and function corresponding to each keyword in the keyword set {metric calculation, data processing, chart display} corresponding to sub-requirement B, and so on, and the syntax tree can determine the class and function corresponding to each keyword in the keyword set {time dimension, line chart, trend comparison} corresponding to sub-requirement D, and use it as the code information corresponding to each keyword.

[0080] Then, based on the degree of relevance between the comment information corresponding to the code information of each keyword in the keyword set corresponding to the sub-requirement and the function to be implemented by the sub-requirement, the keyword set corresponding to the sub-requirement is filtered to obtain the filtered keyword set; wherein, the degree of relevance between the comment information corresponding to the code information of each keyword and the function to be implemented by the sub-requirement can be determined by calculating the semantic similarity between the comment information corresponding to the code information and the function to be implemented by the sub-requirement.

[0081] For example, assuming a semantic similarity threshold of 0.7, taking sub-requirement A as an example, if the semantic similarity between the keyword "function implementation" in the keyword set corresponding to sub-requirement A and the function to be implemented by sub-requirement A is 0.6, then the keyword "function implementation" will be removed from the keyword set corresponding to sub-requirement A; if the semantic similarity between the keyword "function implementation" in the keyword set corresponding to sub-requirement A and the function to be implemented by sub-requirement A is 0.74, then the keyword "function implementation" will be retained. Through the above filtering process, the filtered keyword set corresponding to each sub-requirement can be determined. Assuming the filtered keyword set corresponding to sub-requirement A is {model monitoring, performance monitoring}, the filtered keyword set corresponding to sub-requirement B is {metric calculation, data processing}, the filtered keyword set corresponding to sub-requirement C is {actual defect rate, predicted defect rate}, and the filtered keyword set corresponding to sub-requirement D is {time dimension, line chart, trend comparison}.

[0082] Finally, based on the classes and functions corresponding to each keyword in the keyword set corresponding to each sub-requirement, the code information corresponding to each sub-requirement is determined. It should be noted that the keyword set corresponding to each sub-requirement here is the filtered keyword set (in the following text, the keyword set corresponding to each sub-requirement refers to the filtered keyword set corresponding to each sub-requirement). That is to say, based on the classes and functions corresponding to each keyword in the filtered keyword set corresponding to each sub-requirement, for example, by merging and deduplicating the classes and functions corresponding to each keyword in the filtered keyword set, the code information corresponding to each sub-requirement is obtained.

[0083] For example, taking sub-requirement A as an example, the set of keywords corresponding to sub-requirement A after filtering is {model monitoring, performance monitoring}. The classes and functions corresponding to the keywords "model monitoring" and "performance monitoring" can be merged and deduplicated to obtain the code information corresponding to sub-requirement A. The process of determining the code information corresponding to sub-requirements B to D is the same as the process of determining the code information corresponding to sub-requirement A, and will not be repeated here.

[0084] The keyword set corresponding to each sub-requirement obtained by the preset word segmentation component may contain some keywords with low relevance to the sub-requirement. Therefore, more preferably, this application further filters the keyword set corresponding to each sub-requirement output by the preset word segmentation component to obtain a filtered keyword set, ensuring that the keywords in the filtered keyword set have a high degree of relevance to the sub-requirement, thereby improving the accuracy of determining the code information and knowledge graph corresponding to the subsequent sub-requirement.

[0085] Step 103: Using a large language model, determine the longest inference path with the highest semantic similarity to the newly added monitoring requirement from the knowledge graph corresponding to each sub-requirement.

[0086] For example, after obtaining the keyword set corresponding to each sub-requirement, the knowledge graph corresponding to each sub-requirement can be determined based on the keyword set corresponding to each sub-requirement. Specifically, for any one of the sub-requirements, at least one triple corresponding to the sub-requirement is obtained based on the keyword set corresponding to the sub-requirement, which serves as the knowledge graph corresponding to the sub-requirement. Any triple includes a first keyword as the head entity and the relationship between the head entity and the tail entity as the second keyword. The first keyword and the second keyword are two keywords in the keyword set corresponding to the sub-requirement, that is, the triple is represented in the form of (head entity, relationship between the head entity and the tail entity, tail entity).

[0087] Natural Language Processing (NLP) models have wide applications in information extraction. NLP models can extract information of interest to users from specified documents or massive amounts of documents, such as entities, relationships, and events, for further analysis and processing. Therefore, this application can use NLP models to obtain at least one triple corresponding to each sub-requirement, that is, the knowledge graph corresponding to each sub-requirement.

[0088] For example, taking sub-demand D as an example, assuming that the keyword set {time dimension, line chart, trend comparison} corresponding to sub-demand D is input into a pre-trained NLP model, the resulting at least one triplet corresponding to sub-demand D is shown in the table below:

[0089] Triple group ① (Time dimension, the relationship between the time dimension and line chart, line chart) Triplet ② (Time dimension, the relationship between the time dimension and trend comparison, trend comparison) Triple group ③ (Line chart, the relationship between line chart and trend comparison, trend comparison)

[0090] Table 1

[0091] For a graphical representation of the triples shown in Table 1, please refer to [reference needed]. Figure 3 , Figure 3 A line connecting any two nodes in a graph represents the relationship between the two nodes.

[0092] For example, after obtaining the knowledge graph corresponding to each sub-requirement, a large language model is used for multiple rounds of reasoning to determine the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement. The number of rounds of reasoning is determined by the number of sub-requirements. If there are n sub-requirements, the large language model needs to perform n-1 rounds of reasoning. For example, taking sub-requirements A to D as an example, there are a total of 4 sub-requirements, therefore, the large language model needs to perform 3 rounds of reasoning.

[0093] The reasoning process of the large language model is explained below:

[0094] In the first round of reasoning, the knowledge graphs corresponding to each sub-requirement are input into the large language model to determine the reasoning result. Each reasoning path in the first round's result includes only a single sub-requirement; in other words, the number of reasoning paths in the first round's result is equal to the number of sub-requirements. For example, after the large language model performs the first round of reasoning on sub-requirements A through D, the resulting reasoning result includes four reasoning paths: the reasoning path corresponding to sub-requirement A, sub-requirement B, sub-requirement C, and sub-requirement D. Figure 4 The image shows one possible example of these four reasoning paths.

[0095] Each subsequent round of reasoning is related to the reasoning result of the previous round. In the i-th round of reasoning, the reasoning result obtained after the (i-1)-th round of reasoning and the knowledge graph corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the i-th round of reasoning. Any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning. The correlation between the first reasoning path and the second reasoning path is greater than the preset correlation threshold. Therefore, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning. i is a positive integer and i is greater than 1.

[0096] As a further example, during the inference process, the large language model traverses and processes each inference path of the input, calculates the correlation between the triples corresponding to the last node of each inference path and the triples corresponding to the first node of other inference paths. If any of the correlation results among the multiple triples is greater than a preset correlation threshold, the two inference paths can be merged.

[0097] For example, in the second round of reasoning, the reasoning results from the first round and the knowledge graphs corresponding to each sub-requirement are input into the large language model. The large language model has four reasoning paths corresponding to sub-requirements A to D. When processing the reasoning path corresponding to sub-requirement A, it is necessary to calculate the correlation between the triples corresponding to the last node of the reasoning path corresponding to sub-requirement A and the triples corresponding to the first node of the reasoning path corresponding to sub-requirement B; it is also necessary to calculate the correlation between the triples corresponding to the last node of the reasoning path corresponding to sub-requirement A and the triples corresponding to the first node of the reasoning path corresponding to sub-requirement C; and it is also necessary to calculate the correlation between the triples corresponding to the last node of the reasoning path corresponding to sub-requirement A and the triples corresponding to the first node of the reasoning path corresponding to sub-requirement D.

[0098] by Figure 4Taking the inference paths corresponding to each sub-requirement as an example, the last node in the inference path corresponding to sub-requirement A is "performance monitoring," and the triples corresponding to "performance monitoring" are (model monitoring, relationship between model monitoring and performance monitoring, performance monitoring). The first node in the inference path corresponding to sub-requirement D is "time dimension," and the triples corresponding to "time dimension" are (time dimension, relationship between time dimension and trend comparison, trend comparison) and (time dimension, relationship between time dimension and line graph, line graph). Therefore, it is necessary to calculate (model monitoring, relationship between model monitoring and performance monitoring, performance monitoring) and (time dimension, time...). The correlation between dimensions and trends (trend comparison) and the correlation between model monitoring (relationship between model monitoring and performance monitoring, performance monitoring) and time dimension (relationship between time dimension and line chart, line chart) are calculated. Two correlation results are obtained. If the preset correlation threshold is 0.7, if either of these results is greater than 0.7, the inference paths of sub-requirement A and sub-requirement D can be merged. The merged inference path can be referred to as the inference path corresponding to sub-requirement A and sub-requirement D. The inference paths corresponding to sub-requirement A and sub-requirement D can be referenced. Figure 5 If both correlation calculation results are less than 0.7, then the reasoning paths corresponding to sub-requirement A and sub-requirement D cannot be merged.

[0099] Assume that after the large language model undergoes a second round of reasoning, the reasoning result obtained in the second round includes four reasoning paths, denoted as follows: ① Reasoning path AB: obtained by merging the reasoning paths corresponding to sub-requirement A and sub-requirement B; ② Reasoning path AC: obtained by merging the reasoning paths corresponding to sub-requirement A and sub-requirement C; ③ Reasoning path BC: obtained by merging the reasoning paths corresponding to sub-requirement B and sub-requirement C; ④ Reasoning path CD: obtained by merging the reasoning paths corresponding to sub-requirement C and sub-requirement D.

[0100] Similar to the reasoning process in the second round, in the third round of reasoning, the reasoning results corresponding to the second round and the knowledge graphs corresponding to each sub-requirement are input into the large language model to obtain the reasoning results corresponding to the third round. The length of any reasoning path in the reasoning results corresponding to the third round is longer than the length of any reasoning path in the reasoning results corresponding to the second round. For example, the length of any reasoning path in the reasoning results corresponding to the third round is longer than the length of any one of reasoning paths AB, AC, BC, CD, etc. After the last round of reasoning in the large language model, the reasoning paths included in the obtained reasoning results can be called the longest reasoning paths. Then, from the at least one longest reasoning path obtained in the last round of reasoning, the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement is determined. The number of the longest reasoning paths with the highest semantic similarity can be one or more.

[0101] For example, for sub-requirements A to D, the third round of reasoning is the last round of reasoning. Assume that the reasoning results corresponding to the last round of reasoning include three longest reasoning paths, denoted as: ① Reasoning path ABC: obtained by merging reasoning paths AB and BC; ② Reasoning path ACD: obtained by merging reasoning paths AC and CD; ③ Reasoning path BCD: obtained by merging reasoning paths BC and CD. Assume that the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement is determined from the three longest reasoning paths as reasoning path ABC and reasoning path BCD.

[0102] Step 104: Input the longest inference path, the code information corresponding to the longest inference path, and the target inference path into the large language model to obtain the code information corresponding to the target inference path, which will be used as the code for the new monitoring requirement; the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

[0103] For example, the code information corresponding to the longest inference path is obtained by merging the code information corresponding to the sub-requirements included in the longest inference path. For instance, the code information of the longest inference path ABC is obtained by merging the code information corresponding to sub-requirements A to C, and the code information of the longest inference path BCD is obtained by merging the code information corresponding to sub-requirements B to D.

[0104] For example, the target inference path includes each sub-requirement corresponding to the newly added monitoring requirement. For instance, the target inference path includes sub-requirements A to D, denoted as target inference path ABCD.

[0105] As a further example, the longest inference path, the code information corresponding to the longest inference path, and the target inference path are input into the large language model to obtain the code information corresponding to the target inference path. For example, the code information corresponding to the longest inference paths ABC and BCD, the code information corresponding to the longest inference paths ABC and BCD respectively, and the target inference path ABCD are input into the large language model to obtain the code information corresponding to the target inference path ABCD, that is, the code corresponding to the new monitoring requirement.

[0106] Furthermore, after obtaining the code corresponding to the new monitoring requirements, unit test cases can be designed and used to test the code. Based on the test results, the code can be adjusted and optimized; for example, code that does not meet the test requirements can be modified until it does. The code corresponding to the new monitoring requirements can also be run to obtain the corresponding monitoring results, which can then be displayed in report form. This allows relevant personnel to intuitively understand the model's effectiveness and dynamically adjust model parameters based on the monitoring results to improve model performance.

[0107] The code generation method proposed in this application obtains the code corresponding to the new monitoring requirements through a large language model, which improves the automation and efficiency of code acquisition. Furthermore, by combining the knowledge graph corresponding to the sub-requirements with the large language model, the code corresponding to the new monitoring requirements is obtained by the large language model after reasoning based on the knowledge graph corresponding to the sub-requirements, thereby improving the rationality and accuracy of the code corresponding to the new monitoring requirements.

[0108] Figure 6 and Figure 7 The diagram illustrates the structure of possible code generation apparatuses provided for embodiments of this application. These code generation apparatuses can be used to implement the server functions described in the method embodiments above, and thus also achieve the beneficial effects of the method embodiments described above.

[0109] like Figure 6 As shown, the code generation apparatus 600 includes a processing unit 610 and a transceiver unit 620. The code generation apparatus 600 is used to implement the above-described... Figure 1 The server's function in the method embodiment shown is as follows:

[0110] The transceiver unit 620 is used to obtain new monitoring requirements for the model and the syntax tree corresponding to the source code of the model's code repository.

[0111] The processing unit 610 is used to determine each sub-requirement corresponding to the new monitoring requirement based on the new monitoring requirement, and to determine the code information corresponding to each sub-requirement based on the syntax tree.

[0112] The processing unit 610 is further configured to determine the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement from the knowledge graph corresponding to each sub-requirement using a large language model;

[0113] The transceiver unit 620 is further configured to input the longest inference path, the code information corresponding to the longest inference path, and the target inference path into the large language model to obtain the code information corresponding to the target inference path as the code corresponding to the new monitoring requirement; wherein, the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

[0114] In one possible design, the transceiver unit 620 is specifically used to: input the knowledge graph corresponding to each sub-requirement into the large language model during the first round of reasoning, and determine the reasoning result corresponding to the first round of reasoning;

[0115] During the i-th round of reasoning, the reasoning result obtained after the (i-1)-th round of reasoning and the knowledge graph corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the i-th round of reasoning; wherein, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning; i is a positive integer and i is greater than 1.

[0116] From at least one longest inference path obtained from the last round of inference, determine the longest inference path with the highest semantic similarity to the newly added monitoring requirement.

[0117] In one possible design, any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning, and the correlation between the first reasoning path and the second reasoning path is greater than a preset correlation threshold.

[0118] In one possible design, the processing unit 610 is used to determine the knowledge graph corresponding to each sub-requirement by: determining the keyword set corresponding to any sub-requirement; and obtaining at least one triple corresponding to the sub-requirement based on the keyword set corresponding to the sub-requirement, which serves as the knowledge graph corresponding to the sub-requirement.

[0119] Any triple includes a first keyword as the head entity, the relationship between the head entity and the tail entity, and a second keyword as the tail entity; the first keyword and the second keyword are two keywords from the keyword set corresponding to the sub-requirement.

[0120] In one possible design, the transceiver unit 620 is specifically used to input any one of the sub-requirements into a preset word segmentation component to obtain a keyword set corresponding to the sub-requirement; the processing unit 610 is specifically used to filter the keyword set corresponding to the sub-requirement based on the degree of relevance between the annotation information corresponding to the code information of each keyword in the keyword set corresponding to the sub-requirement and the function to be implemented by the sub-requirement, thereby obtaining a filtered keyword set.

[0121] In one possible design, the syntax tree consists of code files with hierarchical relationships, classes included in the code files, and functions included in the classes; the processing unit 610 is specifically used to determine the set of keywords corresponding to any sub-requirement; determine the classes and functions corresponding to each keyword in the set of keywords corresponding to the sub-requirement according to the syntax tree; and determine the code information corresponding to the sub-requirement according to the classes and functions corresponding to each keyword in the sub-requirement.

[0122] In one possible design, the processing unit 610 is further configured to design unit test cases for the code corresponding to the new monitoring requirement, and to adjust and optimize the code corresponding to the new monitoring requirement based on the test results.

[0123] For a more detailed description of the processing unit 610 and the transceiver unit 620, please refer to [link / reference needed]. Figure 1 The relevant descriptions in the method embodiments shown are directly obtained and will not be repeated here.

[0124] like Figure 7 As shown, the code generation apparatus 700 includes a processor 710 and an interface circuit 720. The processor 710 and the interface circuit 720 are coupled to each other. It is understood that the interface circuit 720 can be a transceiver or an input / output interface. Optionally, the code generation apparatus 700 may also include a memory 730 for storing instructions executed by the processor 710, or storing input data required by the processor 710 to execute instructions, or storing data generated after the processor 710 executes instructions.

[0125] When the code generation device 700 is used to implement Figure 1 In the method shown, the processor 710 is used to implement the functions of the processing unit 610, and the interface circuit 720 is used to implement the functions of the transceiver unit 620.

[0126] The unit division in this embodiment is illustrative and represents only one logical functional division. In actual implementation, other division methods may be used. Furthermore, the functional units in the various embodiments of this application can be integrated into a single processor, exist as separate physical units, or be integrated into a single unit. The integrated units described above can be implemented in hardware or as software functional units.

[0127] Although preferred embodiments of this application have been described, those skilled in the art, upon learning the basic inventive concept, can make other changes and modifications to these embodiments. Therefore, the appended claims are intended to be interpreted as including the preferred embodiments as well as all changes and modifications falling within the scope of this application.

[0128] Obviously, those skilled in the art can make various modifications and variations to this application without departing from the scope of this application. Therefore, if such modifications and variations fall within the scope of the claims of this application and their equivalents, this application also intends to include such modifications and variations.

Claims

1. A method for generating code, characterized in that, The method includes: Obtain new monitoring requirements for the model and the syntax tree corresponding to the source code of the model's code repository; Based on the newly added monitoring requirements, determine the sub-requirements corresponding to the newly added monitoring requirements, and based on the syntax tree, determine the code information corresponding to each sub-requirement; In the first round of reasoning, the knowledge graphs corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the first round of reasoning; In the i-th round of reasoning, the reasoning result obtained after the (i-1)-th round of reasoning and the knowledge graph corresponding to each sub-requirement are input into the large language model to determine the reasoning result corresponding to the i-th round of reasoning; wherein, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning, and any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)-th round of reasoning, and the correlation between the first reasoning path and the second reasoning path is greater than a preset correlation threshold; i is a positive integer and i is greater than 1; From at least one longest reasoning path obtained from the last round of reasoning, determine the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement; The longest inference path, the code information corresponding to the longest inference path, and the target inference path are input into the large language model to obtain the code information corresponding to the target inference path, which is used as the code corresponding to the new monitoring requirement; wherein, the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

2. The method as described in claim 1, characterized in that, The process of determining the knowledge graph corresponding to each sub-requirement is as follows: Determine the set of keywords corresponding to any sub-requirement; Based on the set of keywords corresponding to the sub-requirement, at least one triplet corresponding to the sub-requirement is obtained, which serves as the knowledge graph corresponding to the sub-requirement. Any triple includes a first keyword as the head entity, the relationship between the head entity and the tail entity, and a second keyword as the tail entity; the first keyword and the second keyword are two keywords from the keyword set corresponding to the sub-requirement.

3. The method as described in claim 2, characterized in that, Determine the set of keywords corresponding to any sub-requirement, including: For any one of the sub-requirements, input the sub-requirement into a preset word segmentation component to obtain the keyword set corresponding to the sub-requirement; The keyword set corresponding to the sub-requirement is filtered based on the degree of relevance between the comment information corresponding to the code information of each keyword in the keyword set corresponding to the sub-requirement and the function to be implemented by the sub-requirement, thereby obtaining the filtered keyword set.

4. The method as described in claim 1, characterized in that, The syntax tree consists of code files with hierarchical relationships, the classes included in the code files, and the functions included in the classes; The step of determining the code information corresponding to each sub-requirement based on the syntax tree includes: For any given sub-requirement, determine the set of keywords corresponding to that sub-requirement; Based on the syntax tree, determine the class and function corresponding to each keyword in the keyword set corresponding to the sub-requirement; Based on the class and function corresponding to each keyword in the keyword set corresponding to the sub-requirement, determine the code information corresponding to the sub-requirement.

5. The method as described in claim 1, characterized in that, After obtaining the code corresponding to the newly added monitoring requirement, the following is also included: Design unit test cases for the code corresponding to the new monitoring requirements and test them. Adjust and optimize the code corresponding to the new monitoring requirements based on the test results.

6. An apparatus for generating code, characterized in that, The device includes: a processing unit and a transceiver unit; The transceiver unit is used to obtain new monitoring requirements for the model and the syntax tree corresponding to the source code of the model's code repository. The processing unit is configured to determine each sub-requirement corresponding to the new monitoring requirement based on the new monitoring requirement, and to determine the code information corresponding to each sub-requirement based on the syntax tree; The processing unit is further configured to: input the knowledge graph corresponding to each sub-requirement into the large language model during the first round of reasoning to determine the reasoning result corresponding to the first round of reasoning; input the reasoning result obtained after the (i-1)th round of reasoning and the knowledge graph corresponding to each sub-requirement into the large language model to determine the reasoning result corresponding to the i-th round of reasoning; wherein, the length of any reasoning path in the reasoning result corresponding to the i-th round of reasoning is longer than the length of any reasoning path in the reasoning result corresponding to the (i-1)th round of reasoning, and any reasoning path in the reasoning result corresponding to the i-th round of reasoning is obtained by merging the first reasoning path and the second reasoning path in the reasoning result corresponding to the (i-1)th round of reasoning, and the correlation between the first reasoning path and the second reasoning path is greater than a preset correlation threshold; i is a positive integer and i is greater than 1; and determine the longest reasoning path with the highest semantic similarity to the newly added monitoring requirement from at least one longest reasoning path obtained from the last round of reasoning. The transceiver unit is further configured to input the longest inference path, the code information corresponding to the longest inference path, and the target inference path into the large language model to obtain the code information corresponding to the target inference path, which serves as the code corresponding to the newly added monitoring requirement; wherein, the code information corresponding to the longest inference path is determined based on the code information corresponding to each sub-requirement, and the target inference path includes each sub-requirement.

7. A device for generating code, characterized in that, include: A processor, and a memory communicatively connected to the processor; The memory stores computer-executed instructions; The processor executes computer execution instructions stored in the memory to implement the method as described in any one of claims 1-5.

8. A computer-readable storage medium, characterized in that, The computer-readable storage medium stores computer-executable instructions, which, when executed by a processor, are used to implement the method as described in any one of claims 1-5.

9. A computer program product, characterized in that, Includes a computer program that, when executed by a processor, implements the method of any one of claims 1-5.

Citation Information

Patent Citations

  • Knowledge graph-based large language model generation method, system and device and medium

    CN117609391A

  • Software test driven code generation method and device

    CN118445212A