Method and device for optimizing codes based on large model, electronic equipment and storage medium

Through a large-model-based method, the change function in code files is optimized, which solves the problems of low code readability and maintenance in software development, and improves development efficiency and code quality.

CN119127145APending Publication Date: 2024-12-13BEIJING BAIDU NETCOM SCI & TECH CO LTD
View PDF 0 Cites 1 Cited by

Patent Information

Application Number
CN202411281386.8
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-12
Publication Date
2024-12-13

AI Technical Summary

Technical Problem

In software development, developers need to write a lot of code to enhance the readability, maintenance and exception handling of the code, resulting in less efficient writing and optimizing code.

Method used

Through a large-model-based method, in response to the code change message sent by the client, the change function in the code file is determined and optimized, and the optimized code is sent to the client to improve the readability and maintenance of the code.

Benefits of technology

Real-time optimization and feedback of the code are realized, development efficiency is improved, and the accuracy and quality of the code are improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119127145A_ABST
    Figure CN119127145A_ABST
Patent Text Reader

Abstract

The invention discloses a method and device for optimizing codes based on a large model, electronic equipment and a storage medium, and relates to the technical field of computers, in particular to the field of artificial intelligence such as the large model. According to the specific implementation scheme, in response to a received code change message of a code file sent by a client, a change function in the code file is determined; wherein the code change message is sent when the client monitors a saving event for the code file; in response to the change function being a complete function, optimizing the change function according to the code file to obtain a code after optimization of the change function; and sending the optimized code to a client, so that the client displays the optimized code.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of computer technology, in particular to the field of artificial intelligence such as large models, and specifically to a method, device, electronic device and storage medium for optimizing code based on large models. Background Art

[0002] In software development, developers are usually involved in writing and modifying a large amount of code. In addition to business logic, they need to write a lot of additional code to enhance the readability, maintainability, and exception location processing of the code. Summary of the invention

[0003] The present application provides a method, device, electronic device and storage medium for optimizing code based on a large model.

[0004] According to one aspect of the present application, a method for optimizing code based on a large model is provided, comprising:

[0005] In response to receiving a code change message of a code file sent by a client, determining a changed function in the code file; wherein the code change message is sent by the client when a save event for the code file is detected;

[0006] In response to the changed function being a complete function, optimizing the changed function according to the code file to obtain optimized code of the changed function;

[0007] The optimized code is sent to the client so that the client displays the optimized code.

[0008] According to another aspect of the present application, a method for optimizing code based on a large model is provided, comprising:

[0009] In the process of writing a code file, in response to monitoring a save event for the code file, sending a code change message to a server;

[0010] Receiving optimized code of the changed function in the code file sent by the server; wherein the changed function is a complete function;

[0011] Show the optimized code.

[0012] According to another aspect of the present application, there is provided a device for optimizing code based on a large model, comprising:

[0013] A determination module, configured to determine a changed function in the code file in response to receiving a code change message of the code file sent by the client; wherein the code change message is sent by the client when a save event for the code file is detected;

[0014] an optimization module, configured to optimize the changed function according to the code file in response to the changed function being a complete function, so as to obtain optimized code of the changed function;

[0015] The sending module is used to send the optimized code to the client so that the client displays the optimized code.

[0016] According to another aspect of the present application, there is provided a device for optimizing code based on a large model, comprising:

[0017] A sending module, used for sending a code change message to a server in response to monitoring a save event for the code file during the process of writing the code file;

[0018] A receiving module, used to receive the optimized code of the changed function in the code file sent by the server; wherein the changed function is a complete function;

[0019] The display module is used to display the optimized code.

[0020] According to another aspect of the present application, there is provided an electronic device, including:

[0021] at least one processor; and

[0022] a memory communicatively connected to the at least one processor; wherein,

[0023] The memory stores instructions that can be executed by the at least one processor. The instructions are executed by the at least one processor so that the at least one processor can perform the method described in the above embodiment.

[0024] According to another aspect of the present application, a non-transitory computer-readable storage medium storing computer instructions is provided, wherein the computer instructions are used to enable the computer to execute the method according to the above embodiment.

[0025] According to another aspect of the present application, a computer program product is provided, including a computer program, wherein the computer program implements the steps of the method described in the above embodiment when executed by a processor.

[0026] It should be understood that the content described in this section is not intended to identify the key or important features of the embodiments of the present application, nor is it intended to limit the scope of the present application. Other features of the present application will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS

[0027] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present application.

[0028] Figure 1A schematic diagram of a flow chart of a method for optimizing code based on a large model provided in one embodiment of the present application;

[0029] Figure 2 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application;

[0030] Figure 3 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application;

[0031] Figure 4 A schematic diagram of a flow chart of a method for optimizing code based on a large model provided in another embodiment of the present application;

[0032] Figure 5 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application;

[0033] Figure 6 A schematic diagram of the structure of a device for optimizing code based on a large model provided in one embodiment of the present application;

[0034] Figure 7 A schematic diagram of the structure of a device for optimizing codes based on a large model provided in another embodiment of the present application;

[0035] Figure 8 It is a block diagram of an electronic device used to implement the method for optimizing code based on a large model in an embodiment of the present application. DETAILED DESCRIPTION

[0036] The following is a description of exemplary embodiments of the present application in conjunction with the accompanying drawings, including various details of the embodiments of the present application to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications can be made to the embodiments described herein without departing from the scope and spirit of the present application. Similarly, for the sake of clarity and conciseness, the description of well-known functions and structures is omitted in the following description.

[0037] It should be noted that the acquisition, storage, use, and processing of data in the technical solution of this application comply with the relevant provisions of national laws and regulations and do not violate public order and good morals.

[0038] The following describes the method, device, electronic device and storage medium for optimizing code based on a large model in accordance with an embodiment of the present application with reference to the accompanying drawings.

[0039] Figure 1 A flowchart of a method for optimizing code based on a large model provided in one embodiment of the present application.

[0040] The method for optimizing code based on a large model in the embodiment of the present application can be performed by the device for optimizing code based on a large model in the embodiment of the present application, and the device can be configured in an electronic device. For example, the electronic device can be a server.

[0041] like Figure 1 As shown, the method for optimizing code based on a large model includes:

[0042] Step 101: in response to receiving a code change message of a code file sent by a client, determining a changed function in the code file.

[0043] In this application, the user can write code in the client, such as in the workspace of the code editor, and the client can save the code file at a preset time interval, or the user can save the code file by triggering the save control, and the client can send a code change message of the code file to the server when detecting the save event for the code file. The code change message may include information such as the file name of the code file and the writing status of the code file.

[0044] If the server receives a code change message sent by the client, it can obtain the code file according to the code change message and determine the changed function in the code file to optimize the changed function. The changed function can refer to the function whose code has changed or the newly edited function.

[0045] Exemplarily, the code file may be compared with the content of the last saved code file to determine the function in which the code has been changed.

[0046] Since the program is upgraded, the program version will not be continuously updated. For example, the file to be compared can be obtained from the target file path. The file to be compared is the latest version of the code file corresponding to the code file currently being written. The code file is compared with the file to be compared to determine the functions that differ between the code file and the file to be compared. The functions that differ in the code file can be determined as changed functions.

[0047] The target file path stores multiple versions of the code file currently being written.

[0048] Therefore, by comparing the code file currently being written with the latest version of the code file, the changed function in the code file being written is determined, thereby improving the accuracy of the changed function.

[0049] Step 102 : In response to the changed function being a complete function, optimizing the changed function according to the code file to obtain optimized code of the changed function.

[0050] Since the user may save the code after writing a line of code, or automatically save the code at preset intervals, the function may not be completed when the client detects the save event. Therefore, in this application, it can be determined whether the changed function is a complete function. If the changed function is a complete function, it can be considered that the changed function has been completed, and then the changed function can be optimized.

[0051] Exemplarily, whether the altered function is a complete function may be determined based on a sign defining a boundary of the function.

[0052] For example, the language specification of the programming language used by the code file can be determined based on the file name of the code file, and then whether the changed function is a complete function can be determined based on the language specification of the used programming language. For example, if the file name contains a suffix, the used programming language can be determined based on the suffix.

[0053] Exemplarily, the code file may be parsed according to the language specification of the programming language used by the code file, and the change function may be optimized according to the parsed content, such as defined constants, variables, introduced log packages, etc. Optimizing the change function may include but is not limited to inserting logs into the change function, adding comments to the change function, optimizing the size of the change function, etc.

[0054] Exemplarily, prompt information can be generated based on the language specifications of the programming language used by the changed function and code file, the code file, etc., and the prompt information is input into the large language model. The large language model determines which categories of optimization need to be performed on the changed function, performs the corresponding categories of optimization, and finally outputs the optimized code of the changed function.

[0055] Step 103: Send the optimized code to the client so that the client displays the optimized code.

[0056] Exemplarily, the server can return the optimized code of the modified function to the client in a streaming response manner, and after receiving the optimized code, the client can display the optimized code. Exemplarily, the server can also return the optimized code to the client together.

[0057] In an embodiment of the present application, if the server receives a code change message sent by the client when monitoring a code file save event, it determines the changed function in the code file, and when the changed function is a complete function, optimizes the changed function according to the code file. Thus, after the user completes writing the function, the function optimization process can be automatically triggered, and the optimized code can be fed back in real time, thereby improving the accuracy of the code and improving development efficiency.

[0058] Figure 2A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application.

[0059] like Figure 2 As shown, the method for optimizing code based on a large model includes:

[0060] Step 201: in response to receiving a code change message of a code file sent by a client, determining a changed function in the code file.

[0061] In the present application, step 201 can be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0062] Step 202 : in response to the changed function being a complete function, identifying the optimization requirement of the changed function to determine a target optimization category corresponding to the changed function.

[0063] In the present application, the optimization categories of the code may include inserting logs, adding comments, volume optimization, etc. If the changed function is a complete function, the optimization requirements of the changed function may be identified to determine which category of optimization the changed function can perform.

[0064] Exemplarily, each optimization category has a corresponding judgment rule, and the change function can be matched with the judgment rule corresponding to each optimization category. If the change function matches the judgment rule corresponding to any optimization category in each optimization category, the optimization category can be determined as the target optimization category. The target optimization category can be one or more, and this application does not limit this.

[0065] For example, the judgment rule for adding comments is "the first expression in the function body is not a string comment", and the judgment rule for inserting logs is "the number of lines in the function is greater than 12 and meets the following rules: Rule 1: branch ratio = number of log lines / number of if, elif, else, try branches (including nesting) < 50%, Rule 2: line ratio = number of log call lines / total number of function lines (excluding comments) < 8%".

[0066] In addition, the judgment rules corresponding to the same optimization category may be different for different programming languages. The judgment rules of each optimization category corresponding to the programming language can be obtained according to the programming language used in the code file, and then the target optimization category corresponding to the changed function can be determined according to the judgment rules of each optimization category.

[0067] Therefore, by matching the change function with the judgment rules corresponding to each optimization category to determine which category of optimization the change function needs to perform, and then optimizing the change function accordingly, the accuracy of code optimization can be improved, and the method is relatively simple and convenient.

[0068] Step 203 , using the optimization strategy corresponding to the target optimization category, optimize the changed function according to the code file to obtain optimized code of the changed function.

[0069] In this application, each optimization category has a corresponding optimization strategy. Different optimization categories may correspond to different optimization strategies. Then, the optimization strategy corresponding to the target optimization category can be adopted to optimize the changed function according to the code file. Among them, the optimization strategy corresponding to the same optimization category can be one or more, which is not limited.

[0070] For example, if the target optimization category corresponding to the change function is inserting logs, then based on the optimization strategy corresponding to inserting logs, a logging statement may be inserted into the change function, where the logging statement may refer to a code for generating log records.

[0071] For another example, if the target optimization category corresponding to the change function is adding comments, then based on the optimization strategy corresponding to adding comments, a logging statement may be added to the change function, where the logging statement may refer to code for generating logging.

[0072] Step 204: Send the optimized code to the client so that the client displays the optimized code.

[0073] In the present application, step 204 can be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0074] In an embodiment of the present application, by identifying the optimization requirements of the changed function, it is determined that the changed function needs to be optimized for the target optimization category, and then the optimization strategy corresponding to the target optimization category is adopted to optimize the changed function according to the code file. In this way, based on the precise optimization requirements, the corresponding optimization strategy can be adopted to accurately optimize the changed function, thereby improving the quality of the optimized code.

[0075] Figure 3 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application.

[0076] like Figure 3 As shown, the method for optimizing code based on a large model includes:

[0077] Step 301: in response to receiving a code change message of a code file sent by a client, determining a changed function in the code file.

[0078] In the present application, step 301 can be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0079] Step 302 : in response to the changed function being a complete function, identifying the optimization requirement of the changed function to determine a target optimization category corresponding to the changed function.

[0080] In the present application, step 302 can be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0081] Step 303: In response to the target optimization category being inserting a log, the code file is parsed to obtain context information of the changed function.

[0082] In the present application, if the target optimization category is to insert a log, the programming language used by the code file can be determined, and the code file can be parsed according to the language specification of the used programming language to obtain the context information of the changed function.

[0083] The context information may include but is not limited to the log package introduced in the change function, the variables defined for the log package, the constants defined in the code file, the related functions of the change function, etc. The related functions may refer to the functions called by the change function.

[0084] Step 304 : optimize the changed function by inserting the log category according to the changed function and the context information to obtain optimized code.

[0085] As a possible implementation method, a preset log insertion rule can be obtained, and the change function can be matched with the log insertion rule to determine the position in the change function where the logging statement needs to be inserted, so as to insert the logging statement at the position as needed and determine the log insertion position in the change function. Then, based on the context information of the change function, such as the introduced log package, variables defined for the log package, etc., the log call interface corresponding to the log insertion position is determined, and a logging statement is generated based on the log call interface, so that the logging statement can be inserted into the change function according to the log insertion position to obtain the optimized code, such as inserting the logging statement at the log insertion position in the change function.

[0086] The logging statement may refer to a code for generating logging records, and the log call interface may refer to an API (Application Programming Interface) provided by a logging framework. The log call interface is called in the logging statement to output logging records when the program is running.

[0087] For example, the log insertion rules may include inserting logging statements at locations in the code where exceptions may occur or do not meet expectations. If there is a location in the changed function where an exception may occur, then that location is where the logging statement needs to be inserted. The location where the logging statement needs to be inserted and the next line of code can be used as the log insertion location. One or more logging statements can be inserted between the location where the logging statement needs to be inserted and the next line of code.

[0088] Exemplarily, when determining the log insertion position, the change function may be parsed according to the language specification of the programming language used by the code file to determine whether there is a position in the change function that matches the log insertion rule, and if so, the log insertion position may be determined according to the position that matches the log insertion rule. The position that matches the log insertion rule may refer to the position where the log record statement needs to be inserted.

[0089] Therefore, the log insertion position can be determined based on the log insertion rules, and the log call interface can be determined based on the context information of the change function, thereby determining the log recording statement based on the log call interface, and then optimizing the insertion of log records for the change function based on the log insertion position and the log recording statement, thereby improving the code quality.

[0090] As another possible implementation method, a large language model may be used to optimize the change function by inserting log categories to obtain optimized code of the change function.

[0091] Exemplarily, the language specification and log insertion requirements of the programming language used by the code file can be obtained, and a first prompt message can be generated based on the change function, context information, language specification, log insertion requirements, etc. The first prompt message can be used to prompt the large language model to perform optimization of the inserted log category. The first prompt message is input into the large language model, and the large language model optimizes the inserted log category of the change function and outputs the optimized code of the change function.

[0092] Exemplarily, the large language model can determine the log insertion position and generate corresponding logging statements based on the change function, context information, language specifications, log insertion requirements, etc., insert logging statements into the change function based on the log insertion position, and output optimized code.

[0093] Among them, log insertion requirements may include log insertion rules, output requirements for large language models, etc. For example, log input requirements are "1. Add logs when there are exceptions or discrepancies with business processes, and the logs cannot affect system operation; 2. Do not allow the output of confidential or sensitive information; 3. Ensure that the output is meaningful and readable."

[0094] Exemplarily, while outputting the optimized code of the change function, the large language model can also output log insertion optimization suggestions. The log insertion optimization suggestions may include the total number of locations where logging statements are inserted, the specific locations where logging statements are inserted, the reason for inserting logging statements, the specific content of the inserted logging statements, etc.

[0095] Therefore, by using a large language model to optimize the insertion of log classes into change functions based on change functions, context information, etc., the precision and accuracy of log insertion are improved, and the code quality and readability are improved.

[0096] Step 305: Send the optimized code to the client so that the client displays the optimized code.

[0097] In the present application, step 305 can be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0098] In an embodiment of the present application, the code file is parsed to obtain the context information of the changed function, and the changed function is optimized by inserting the log category according to the context information of the changed function to obtain the optimized code. Thus, the context information is incorporated into the code optimization process, which improves the accuracy of the code optimization and further improves the code quality.

[0099] Figure 4 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application.

[0100] like Figure 4 As shown, the method for optimizing code based on a large model includes:

[0101] Step 401: in response to receiving a code change message of a code file sent by a client, determining a changed function in the code file.

[0102] In the present application, step 401 may be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0103] Step 402 : in response to the changed function being a complete function, identifying the optimization requirement of the changed function to determine a target optimization category corresponding to the changed function.

[0104] In the present application, step 402 may be implemented in any of the embodiments of the present application, and therefore will not be described in detail here.

[0105] Step 403 , in response to the target optimization category being adding annotations, the changed function is parsed to obtain a function definition and a function body of the changed function.

[0106] In this application, if the target optimization category corresponding to the changed function is to add comments, the changed function can be parsed according to the language specification of the programming language used by the code file to obtain the function definition and function body of the changed function. The function definition can include the function name, parameter list, etc.

[0107] Step 404 , based on the changed function, the file name of the code file, the function definition and the function body, the changed function is optimized by adding a comment category to obtain an optimized code.

[0108] For different programming languages, the location and method of adding comments may be different. For example, for Java, the comment " / **something** / , @param represents the parameter" is added on the line above the function definition. For Python, the comment """"something""",:param represents the parameter" is added on the line below the function definition.

[0109] As a possible implementation method, the programming language used by the code file is determined according to the name of the code file, and the changed function is parsed according to the language specification of the programming language to determine what content in the changed function needs to be commented on, thereby determining the comment location, and analyzing the function definition, function body, etc. according to the language specification to determine the comments to be added corresponding to the comment location, and then according to the comment location, add the comments to be added in the changed function to obtain the optimized code of the changed function, such as adding the comments to be added at the comment location in the changed function.

[0110] The comment position may refer to a position where the comment is added, such as the previous line of the function definition of the changed function.

[0111] Therefore, by determining the comment position and comments to be added according to the language specifications, function definitions, function bodies, etc. of the programming language used in the code file, the change function can be optimized by adding comment classes according to the comment position and comments to be added, which can improve the accuracy of comment addition and improve code quality.

[0112] As another possible implementation method, a second prompt information can be generated based on the file name, function definition and function body of the changed function and code file. The second prompt information can be used to prompt the large language model to perform the annotation task. The second prompt information is input into the large language model. The large language model performs annotation optimization on the changed function and outputs the optimized code of the changed function.

[0113] Exemplarily, the large language model can obtain the language specification of the programming language used by the code file according to the file name of the code file, and parse the changed function according to the language specification to determine the comment location, analyze the function definition, function body, etc. according to the language specification, generate comments to be added, and then add the comments to be added to the comment location in the changed function to obtain the optimized code, and finally output the optimized code.

[0114] Exemplarily, while outputting the optimized code of the changed function, the large language model can also output optimization suggestions for adding comments. The optimization suggestions for adding comments can include the total number of locations where comments are added, the specific locations where comments are added, the reason for adding comments, the specific content of the added comments, etc.

[0115] Therefore, by using the large language model to optimize the added annotation class for the changed function according to the file name, function definition, function body, etc. of the changed function and code file, the precision and accuracy of the added annotations are improved, and the code quality and readability are improved.

[0116] Step 405: Send the optimized code to the client so that the client displays the optimized code.

[0117] In the present application, step 405 may be implemented in any of the embodiments of the present application, so it will not be described in detail here.

[0118] In an embodiment of the present application, by parsing the changed function, obtaining the function definition and function body of the changed function, and optimizing the changed function by inserting a log category according to the function definition, the function body, the file name of the code file, etc., to obtain the optimized code, thereby optimizing the changed function by adding a comment class based on the entire code of the changed function, thereby improving the accuracy of code optimization and further improving the code quality.

[0119] Figure 5 A flowchart of a method for optimizing code based on a large model provided in another embodiment of the present application. The method for optimizing code based on a large model can be executed by a client.

[0120] like Figure 5 As shown, the method for optimizing code based on a large model includes:

[0121] Step 501 : in the process of writing a code file, in response to monitoring a save event for the code file, a code change message is sent to a server.

[0122] In this application, the user can write code in a client such as a code editor, and the client can save the code file at preset intervals, or the user can save the code file by triggering a save control. During the process of writing the code file, if the client detects a save event for the code file, it can send a code change message to the server.

[0123] Step 502: Receive optimized code of the changed function in the code file sent by the server.

[0124] After receiving the code change message, the server can determine the changed function in the code file, and when the changed function is a complete function, optimize the changed function according to the code file to obtain the optimized code of the changed function, and send the optimized code to the client. In this way, the client can receive the optimized code of the changed function in the code file.

[0125] Exemplarily, the optimization of the change function may include, but is not limited to, inserting a log into the change function, adding a comment into the change function, optimizing the volume of the change function, and the like.

[0126] Step 503, display the optimized code.

[0127] Exemplarily, the client can display the optimized code of the changed function in the workspace of the code editing interface. For example, the optimized code can be displayed in a preset area in the workspace, or in the form of a floating window in the workspace.

[0128] For example, after receiving the optimized code of the changed function, the client can first display the optimization prompt message in the work area, and then display the optimized code in the work area when the trigger operation of the optimization prompt message is detected. Thus, the optimization prompt message is first displayed in the work area, and when the user clicks to view the prompt message, the optimized code is displayed, thereby reducing the interruption to the user's code writing work and meeting the user's different display requirements for the optimized code.

[0129] For example, a code replacement control corresponding to the optimized code can be displayed in the workspace. If a trigger operation on the code replacement control is detected, the code of the changed function in the workspace can be replaced with the optimized code. Thus, the user can complete the workspace code replacement with the optimized code in one click, which is simple and convenient, and improves development efficiency.

[0130] During the process of optimizing the changed function on the server, the user on the client side may change the content of the code file displayed in the workspace. For example, if the context of the changed function in the workspace changes, the optimized code received from the server may be inaccurate. If the code of the changed function in the workspace is directly replaced with the optimized code, operation abnormalities may occur.

[0131] Based on this, when displaying the optimized code, for example, it can be determined whether the code in the code file displayed in the workspace has changed. If the code in the code file displayed in the workspace has changed, the code change prompt information can be displayed while the optimized code is displayed in the workspace. The code change prompt information is used to prompt the user that the code in the code file displayed in the workspace has changed.

[0132] Therefore, if the code in the code file displayed in the workspace changes, the user can be reminded that the code in the code file in the workspace has changed while displaying the optimized code, so as to avoid the user directly using the optimized code and causing adverse effects.

[0133] Exemplarily, the changed function can be searched in the workspace according to the original line number information of the changed function. If the changed function is not found according to the original line number information of the changed function, it can be determined that the code of the code file displayed in the workspace has changed. The original line number information can refer to the line number information of the changed function when the client sends a code change message to the server.

[0134] That is, if the original line number information of the changed function is different from the line number information of the changed function in the current workspace, it can be determined that the code in the code file displayed in the workspace has changed.

[0135] Therefore, based on the original line number information of the changed function, it is determined whether the code in the code file displayed in the workspace has changed. The method is simple and convenient.

[0136] For example, the code of the changed function in the current workspace can be compared with the code of the changed function used for optimization on the server side. If the codes are inconsistent, it means that the original text of the changed function has changed, so that it can be determined that the code in the code file displayed in the workspace has changed. The original text of the changed function can refer to the code of the changed function used by the server side for code optimization.

[0137] Therefore, it is possible to determine whether the code in the code file displayed in the workspace has changed by judging whether the original text of the changed function has changed, which is simple and convenient.

[0138] It can be understood that if the changed function is not found based on the original line number information of the changed function, or the original text of the changed function in the current workspace has changed, or the changed function is found based on the original line number information of the changed function, but the original text of the changed function has changed, it can be determined that the code in the code file displayed in the workspace has changed.

[0139] In an embodiment of the present application, during the process of a user writing code, if a save event for a code file is detected, a code change message is sent to the server, and the server can optimize the changed functions in the code file, thereby automatically triggering the function optimization process after the user finishes writing the function, and providing real-time feedback on the optimized code, thereby improving the accuracy of the code and enhancing development efficiency.

[0140] In order to implement the above embodiment, the embodiment of the present application also proposes a device for optimizing code based on a large model. Figure 6 A schematic diagram of the structure of a device for optimizing code based on a large model provided in one embodiment of the present application.

[0141] like Figure 6 As shown, the device 600 for optimizing code based on a large model includes:

[0142] A determination module 610 is used to determine a changed function in the code file in response to receiving a code change message of the code file sent by the client; wherein the code change message is sent by the client when a save event for the code file is detected;

[0143] An optimization module 620, configured to optimize the changed function according to the code file in response to the changed function being a complete function, so as to obtain optimized code of the changed function;

[0144] The sending module 630 is used to send the optimized code to the client so that the client displays the optimized code.

[0145] Optionally, the optimization module 620 is used to:

[0146] Identifying the optimization requirements of the change function to determine the target optimization category corresponding to the change function;

[0147] The optimization strategy corresponding to the target optimization category is adopted, and the change function is optimized according to the code file to obtain the optimized code of the change function.

[0148] Optionally, the optimization module 620 is used to:

[0149] Obtain the judgment rules corresponding to each optimization category;

[0150] Matching the change function with the judgment rules corresponding to each optimization category;

[0151] In response to the change function matching a determination rule corresponding to any one of the optimization categories, the any one of the optimization categories is determined as the target optimization category.

[0152] Optionally, the optimization module 620 is used to:

[0153] In response to the target optimization category being inserting a log, parsing the code file to obtain context information of the change function; wherein the context information includes one or more of the following: a log package introduced in the change function, a variable defined for the log package, a constant defined in the code file, and a related function of the change function;

[0154] According to the change function and the context information, the change function is optimized by inserting a log category to obtain the optimized code.

[0155] Optionally, the optimization module 620 is used to:

[0156] Determine the log insertion position in the change function according to the change function and the preset log insertion rule;

[0157] Determine, according to the context information, a log calling interface corresponding to the log insertion position;

[0158] Generate a log record statement according to the log call interface; wherein the log record statement refers to a code for generating a log record;

[0159] The logging statement is inserted into the change function according to the log insertion position to obtain the optimized code.

[0160] Optionally, the optimization module 620 is used to:

[0161] Obtain the language specification and log insertion requirements of the programming language used by the code file;

[0162] Generate first prompt information according to the change function, the context information, the language specification and the log insertion requirement; wherein the first prompt information is used to prompt the large language model to perform optimization of inserting log categories;

[0163] The first prompt information is input into a large language model for processing to obtain the optimized code output by the large language model.

[0164] Optionally, the optimization module 620 is used to:

[0165] In response to the target optimization category being adding a comment, parsing the changed function to obtain a function definition and a function body of the changed function;

[0166] According to the changed function, the file name of the code file, the function definition and the function body, the changed function is optimized by adding a comment category to obtain the optimized code.

[0167] Optionally, the optimization module 620 is used to:

[0168] Determine the programming language used by the code file according to the file name of the code file;

[0169] According to the language specification of the programming language, the change function is parsed to determine the comment position;

[0170] Determine the annotation to be added corresponding to the annotation position according to the language specification, the function definition and the function body

[0171] According to the comment position, the comment to be added is added to the changed function to obtain the optimized code.

[0172] Optionally, the optimization module 620 is used to:

[0173] Generate second prompt information according to the changed function, the file name of the code file, the function definition and the function body; wherein the second prompt information is used to prompt the large language model to perform the annotation adding task

[0174] The second prompt information is input into the large language model for processing to obtain the optimized code output by the large language model.

[0175] Optionally, the determining module 610 is configured to:

[0176] Obtaining a file to be compared from a target file path; wherein the file to be compared is a code file of the latest version corresponding to the code file;

[0177] The code file is compared with the file to be compared to determine functions with differences between the code file and the file to be compared, and the functions with differences in the code file are determined as changed functions.

[0178] It should be noted that the explanation of the aforementioned method embodiment of the server-side based on large model optimization code is also applicable to the device based on large model optimization code of this embodiment, so it will not be repeated here.

[0179] In an embodiment of the present application, if the server receives a code change message sent by the client when monitoring a code file save event, it determines the changed function in the code file, and when the changed function is a complete function, optimizes the changed function according to the code file. Thus, after the user completes writing the function, the function optimization process can be automatically triggered, and the optimized code can be fed back in real time, thereby improving the accuracy of the code and improving development efficiency.

[0180] In order to implement the above embodiment, the embodiment of the present application also proposes another device for optimizing code based on a large model. Figure 7 A schematic diagram of the structure of a device for optimizing code based on a large model provided in another embodiment of the present application.

[0181] like Figure 7 As shown, the device 700 for optimizing code based on a large model includes:

[0182] The sending module 710 is used to send a code change message to the server in response to monitoring a save event for the code file during the process of writing the code file;

[0183] The receiving module 720 is used to receive the optimized code of the changed function in the code file sent by the server; wherein the changed function is a complete function;

[0184] The display module 730 is used to display the optimized code.

[0185] Optionally, the display module 730 is used to:

[0186] Display code optimization prompt information in the work area of ​​the code editing interface;

[0187] In response to detecting a triggering operation on the optimization prompt information, displaying the optimized code in the work area.

[0188] Optionally, the display module 730 is used to:

[0189] Determine whether the code in the code file displayed in the work area of ​​the code editing interface has changed;

[0190] In response to a code change in the code file, code change prompt information is displayed while displaying the optimized code in the workspace.

[0191] Optionally, the display module 730 is used to:

[0192] Searching for the changed function in the work area according to the original line number information of the changed function;

[0193] In response to not finding the changed function according to the original line number information, it is determined that code in the code file displayed in the workspace has changed.

[0194] Optionally, the device may further include:

[0195] The replacement module is used to replace the current code of the changed function in the workspace with the optimized code in response to detecting a trigger operation on the code replacement control.

[0196] It should be noted that the explanation of the aforementioned client-side method embodiment based on large model optimization code is also applicable to the device based on large model optimization code of this embodiment, so it will not be repeated here.

[0197] In an embodiment of the present application, during the process of a user writing code, if a save event for a code file is detected, a code change message is sent to the server, and the server can optimize the changed functions in the code file, thereby automatically triggering the function optimization process after the user finishes writing the function, and providing real-time feedback on the optimized code, thereby improving the accuracy of the code and enhancing development efficiency.

[0198] According to an embodiment of the present application, the present application also provides an electronic device, a readable storage medium and a computer program product.

[0199] Figure 8 A schematic block diagram of an example electronic device 800 that can be used to implement an embodiment of the present application is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the present application described and / or required herein.

[0200] like Figure 8As shown, the device 800 includes a computing unit 801, which can perform various appropriate actions and processes according to a computer program stored in a ROM (Read-Only Memory) 802 or a computer program loaded from a storage unit 808 to a RAM (Random Access Memory) 803. In the RAM 803, various programs and data required for the operation of the device 800 can also be stored. The computing unit 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An I / O (Input / Output) interface 805 is also connected to the bus 804.

[0201] A number of components in the device 800 are connected to the I / O interface 805, including: an input unit 806, such as a keyboard, a mouse, etc.; an output unit 807, such as various types of displays, speakers, etc.; a storage unit 808, such as a disk, an optical disk, etc.; and a communication unit 809, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 809 allows the device 800 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.

[0202] The computing unit 801 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 801 include, but are not limited to, a CPU (Central Processing Unit), a GPU (Graphic Processing Units), various dedicated AI (Artificial Intelligence) computing chips, various computing units running machine learning model algorithms, DSP (Digital Signal Processor), and any appropriate processor, controller, microcontroller, etc. The computing unit 801 performs the various methods and processes described above, such as a method based on a large model optimization code. For example, in some embodiments, the method based on a large model optimization code may be implemented as a computer software program, which is tangibly contained in a machine-readable medium, such as a storage unit 808. In some embodiments, part or all of the computer program may be loaded and / or installed on the device 800 via the ROM 802 and / or the communication unit 809. When the computer program is loaded into the RAM 803 and executed by the computing unit 801, one or more steps of the method based on the large model optimization code described above may be performed. Alternatively, in other embodiments, the computing unit 801 may be configured to execute the large model-based code optimization method in any other appropriate manner (for example, by means of firmware).

[0203] Various embodiments of the systems and techniques described above herein may be implemented in digital electronic circuit systems, integrated circuit systems, FPGAs (Field Programmable Gate Arrays), ASICs (Application-Specific Integrated Circuits), ASSPs (Application Specific Standard Products), SOCs (System On Chips), CPLDs (Complex Programmable Logic Devices), computer hardware, firmware, software, and / or combinations thereof. These various embodiments may include: being implemented in one or more computer programs that may be executed and / or interpreted on a programmable system including at least one programmable processor that may be a dedicated or general-purpose programmable processor that may receive data and instructions from a storage system, at least one input device, and at least one output device, and transmit data and instructions to the storage system, the at least one input device, and the at least one output device.

[0204] The program code for implementing the method of the present application can be written in any combination of one or more programming languages. These program codes can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the program code, when executed by the processor or controller, implements the functions / operations specified in the flow chart and / or block diagram. The program code can be executed entirely on the machine, partially on the machine, partially on the machine and partially on a remote machine as a stand-alone software package, or entirely on a remote machine or server.

[0205] In the context of the present application, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or equipment. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable medium may include, but is not limited to, an electronic, magnetic, optical, electromagnetic, infrared, or semiconductor system, device, or device, or any suitable combination of the foregoing. More specific examples of machine-readable storage media may include electrical connections based on one or more lines, portable computer disks, hard disks, RAM, ROM, EPROM (Electrically Programmable Read-Only-Memory) or flash memory, optical fiber, CD-ROM (Compact Disc Read-Only Memory), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.

[0206] To provide interaction with a user, the systems and techniques described herein can be implemented on a computer having: a display device (e.g., a CRT (Cathode-Ray Tube) or LCD (Liquid Crystal Display) monitor) for displaying information to the user; and a keyboard and pointing device (e.g., a mouse or trackball) through which the user can provide input to the computer. Other types of devices can also be used to provide interaction with the user; for example, the feedback provided to the user can be any form of sensory feedback (e.g., visual feedback, auditory feedback, or tactile feedback); and input from the user can be received in any form (including acoustic input, voice input, or tactile input).

[0207] The systems and techniques described herein may be implemented in a computing system that includes backend components (e.g., as a data server), or a computing system that includes middleware components (e.g., an application server), or a computing system that includes frontend components (e.g., a user computer with a graphical user interface or a web browser through which a user can interact with implementations of the systems and techniques described herein), or a computing system that includes any combination of such backend components, middleware components, or frontend components. The components of the system may be interconnected by any form or medium of digital data communication (e.g., a communication network). Examples of communication networks include: LAN (Local Area Network), WAN (Wide Area Network), the Internet, and blockchain networks.

[0208] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or a cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services (Virtual Private Server). The server may also be a server of a distributed system, or a server combined with a blockchain.

[0209] According to an embodiment of the present application, the present application also provides a computer program product, which, when an instruction processor in the computer program product executes, executes the method for optimizing code based on a large model proposed in the above embodiment of the present application.

[0210] It should be understood that the various forms of processes shown above can be used to reorder, add or delete steps. For example, the steps recorded in this application can be executed in parallel, sequentially or in different orders, as long as the expected results of the technical solution disclosed in this application can be achieved, and this document is not limited here.

[0211] The above specific implementations do not constitute a limitation on the protection scope of this application. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions can be made according to design requirements and other factors. Any modifications, equivalent substitutions and improvements made within the spirit and principles of this application should be included in the protection scope of this application.

Claims

1. A method for optimizing code based on a large model, comprising: In response to receiving a code change message of a code file sent by a client, determining a changed function in the code file; wherein the code change message is sent by the client when a save event for the code file is detected; In response to the changed function being a complete function, optimizing the changed function according to the code file to obtain optimized code of the changed function; The optimized code is sent to the client so that the client displays the optimized code.

2. The method of claim 1, wherein: The step of optimizing the change function according to the code file to obtain optimized code of the change function includes: Identifying the optimization requirements of the change function to determine the target optimization category corresponding to the change function; The optimization strategy corresponding to the target optimization category is adopted, and the change function is optimized according to the code file to obtain the optimized code of the change function.

3. The method of claim 2, wherein: The step of identifying the optimization requirement of the change function to determine the target optimization category corresponding to the change function includes: Obtain the judgment rules corresponding to each optimization category; Matching the change function with the judgment rules corresponding to each optimization category; In response to the change function matching a determination rule corresponding to any one of the optimization categories, the any one of the optimization categories is determined as the target optimization category.

4. The method of claim 2, wherein: The adopting the optimization strategy corresponding to the target optimization category and optimizing the change function according to the code file to obtain the optimized code of the change function includes: In response to the target optimization category being inserting a log, parsing the code file to obtain context information of the change function; wherein the context information includes one or more of the following: a log package introduced in the change function, a variable defined for the log package, a constant defined in the code file, and a related function of the change function; According to the change function and the context information, the change function is optimized by inserting a log category to obtain the optimized code.

5. The method of claim 4, wherein: The optimizing the insertion log category of the change function according to the change function and the context information to obtain the optimized code includes: Determine the log insertion position in the change function according to the change function and the preset log insertion rule; Determine, according to the context information, a log calling interface corresponding to the log insertion position; Generate a log record statement according to the log call interface; wherein the log record statement refers to a code used to generate a log record; The logging statement is inserted into the change function according to the log insertion position to obtain the optimized code.

6. The method of claim 4, wherein: The optimizing the insertion log category of the change function according to the change function and the context information to obtain the optimized code includes: Obtain the language specification and log insertion requirements of the programming language used by the code file; Generate first prompt information according to the change function, the context information, the language specification and the log insertion requirement; wherein the first prompt information is used to prompt the large language model to perform optimization of inserting log categories; The first prompt information is input into a large language model for processing to obtain the optimized code output by the large language model.

7. The method of claim 2, wherein: The adopting the optimization strategy corresponding to the target optimization category and optimizing the change function according to the code file to obtain the optimized code of the change function includes: In response to the target optimization category being adding a comment, parsing the changed function to obtain a function definition and a function body of the changed function; According to the changed function, the file name of the code file, the function definition and the function body, the changed function is optimized by adding a comment category to obtain the optimized code.

8. The method of claim 7, wherein: The step of optimizing the changed function by adding a comment category according to the changed function, the file name of the code file, the function definition, and the function body to obtain the optimized code includes: Determine the programming language used by the code file according to the file name of the code file; According to the language specification of the programming language, the change function is parsed to determine the comment position; Determine the annotation to be added corresponding to the annotation position according to the language specification, the function definition and the function body According to the comment position, the comment to be added is added to the changed function to obtain the optimized code.

9. The method of claim 7, wherein: The step of optimizing the changed function by adding a comment category according to the changed function, the file name of the code file, the function definition, and the function body to obtain the optimized code includes: Generate second prompt information according to the changed function, the file name of the code file, the function definition and the function body; wherein the second prompt information is used to prompt the large language model to perform the annotation adding task The second prompt information is input into the large language model for processing to obtain the optimized code output by the large language model.

10. The method according to any one of claims 1 to 9, wherein: The determining of the changed function in the code file comprises: Obtaining a file to be compared from a target file path; wherein the file to be compared is a code file of the latest version corresponding to the code file; The code file is compared with the file to be compared to determine functions with differences between the code file and the file to be compared, and the functions with differences in the code file are determined as changed functions.

11. A method for optimizing code based on a large model, comprising: In the process of writing a code file, in response to monitoring a save event for the code file, sending a code change message to a server; Receiving optimized code of the changed function in the code file sent by the server; wherein the changed function is a complete function; Show the optimized code.

12. The method of claim 11, wherein: The display of the optimized code includes: Display code optimization prompt information in the work area of ​​the code editing interface; In response to detecting a triggering operation on the optimization prompt information, displaying the optimized code in the work area.

13. The method of claim 11, wherein: The display of the optimized code includes: Determine whether the code in the code file displayed in the work area of ​​the code editing interface has changed; In response to a code change in the code file, code change prompt information is displayed while displaying the optimized code in the workspace.

14. The method of claim 13, wherein: The determining whether the code in the code file displayed in the work area in the code editing interface has changed includes: Searching for the changed function in the work area according to the original line number information of the changed function; In response to not finding the changed function according to the original line number information, it is determined that code in the code file displayed in the workspace has changed.

15. The method according to any one of claims 12 to 14, further comprising: In response to detecting a trigger operation on a code replacement control, the current code of the changed function in the workspace is replaced with the optimized code.

16. A device for optimizing code based on a large model, comprising: A determination module, configured to determine a changed function in the code file in response to receiving a code change message of the code file sent by the client; wherein the code change message is sent by the client when a save event for the code file is detected; an optimization module, configured to optimize the changed function according to the code file in response to the changed function being a complete function, so as to obtain optimized code of the changed function; The sending module is used to send the optimized code to the client so that the client displays the optimized code.

17. The device of claim 16, wherein: The optimization module is used to: Identifying the optimization requirements of the change function to determine the target optimization category corresponding to the change function; The optimization strategy corresponding to the target optimization category is adopted, and the change function is optimized according to the code file to obtain the optimized code of the change function.

18. The device of claim 17, wherein: The optimization module is used to: Obtain the judgment rules corresponding to each optimization category; Matching the change function with the judgment rules corresponding to each optimization category; In response to the change function matching a determination rule corresponding to any one of the optimization categories, the any one of the optimization categories is determined as the target optimization category.

19. The device of claim 17, wherein: The optimization module is used to: In response to the target optimization category being inserting a log, parsing the code file to obtain context information of the change function; wherein the context information includes one or more of the following: a log package introduced in the change function, a variable defined for the log package, a constant defined in the code file, and a related function of the change function; According to the change function and the context information, the change function is optimized by inserting a log category to obtain the optimized code.

20. The device of claim 19, wherein: The optimization module is used to: Determine the log insertion position in the change function according to the change function and the preset log insertion rule; Determine, according to the context information, a log calling interface corresponding to the log insertion position; Generate a log record statement according to the log call interface; wherein the log record statement refers to a code used to generate a log record; The logging statement is inserted into the change function according to the log insertion position to obtain the optimized code.

21. The apparatus of claim 19, wherein: The optimization module is used to: Obtain the language specification and log insertion requirements of the programming language used by the code file; Generate first prompt information according to the change function, the context information, the language specification and the log insertion requirement; wherein the first prompt information is used to prompt the large language model to perform optimization of inserting log categories; The first prompt information is input into a large language model for processing to obtain the optimized code output by the large language model.

22. The apparatus of claim 17, wherein: The optimization module is used to: In response to the target optimization category being adding a comment, parsing the changed function to obtain a function definition and a function body of the changed function; According to the changed function, the file name of the code file, the function definition and the function body, the changed function is optimized by adding a comment category to obtain the optimized code.

23. The device of claim 22, wherein: The optimization module is used to: Determine the programming language used by the code file according to the file name of the code file; According to the language specification of the programming language, the change function is parsed to determine the comment position; Determine the annotation to be added corresponding to the annotation position according to the language specification, the function definition and the function body According to the comment position, the comment to be added is added to the changed function to obtain the optimized code.

24. The device of claim 22, wherein: The optimization module is used to: Generate second prompt information according to the changed function, the file name of the code file, the function definition and the function body; wherein the second prompt information is used to prompt the large language model to perform the annotation adding task The second prompt information is input into the large language model for processing to obtain the optimized code output by the large language model.

25. The device according to any one of claims 16 to 24, wherein: The determining module is used to: Obtaining a file to be compared from a target file path; wherein the file to be compared is a code file of the latest version corresponding to the code file; The code file is compared with the file to be compared to determine functions with differences between the code file and the file to be compared, and the functions with differences in the code file are determined as changed functions.

26. A device for optimizing code based on a large model, comprising: A sending module, used for sending a code change message to a server in response to monitoring a save event for the code file during the process of writing the code file; A receiving module, used to receive the optimized code of the changed function in the code file sent by the server; wherein the changed function is a complete function; The display module is used to display the optimized code.

27. The device of claim 26, wherein: The display module is used for: Display code optimization prompt information in the work area of ​​the code editing interface; In response to detecting a triggering operation on the optimization prompt information, displaying the optimized code in the work area.

28. The apparatus of claim 26, wherein: The display module is used for: Determine whether the code in the code file displayed in the work area of ​​the code editing interface has changed; In response to a code change in the code file, code change prompt information is displayed while displaying the optimized code in the workspace.

29. The apparatus of claim 28, wherein: The display module is used for: searching the changed function in the work area according to the original line number information of the changed function; In response to not finding the changed function according to the original line number information, it is determined that the code in the code file displayed in the workspace has changed.

30. The apparatus of any one of claims 27 to 29, further comprising: The replacement module is used to replace the current code of the changed function in the workspace with the optimized code in response to detecting a trigger operation on the code replacement control.

31. An electronic device comprising: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to execute the method of any one of claims 1-10, or to execute the method of any one of claims 11-15.

32. A non-transitory computer-readable storage medium storing computer instructions, wherein: The computer instructions are used to cause the computer to execute the method according to any one of claims 1-10, or to execute the method according to any one of claims 11-15.

33. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the steps of the method according to any one of claims 1 to 10, or implements the steps of the method according to any one of claims 11 to 15.

Citation Information

Cited By

  • Large language model-based code development method and related device

    WO2026130394A1