Code completion method and device based on large model and electronic equipment
By building code dependency graphs and querying related code elements, the accuracy problem of large models when processing cross-file information in code completion is solved, and the accuracy of generated code is improved.
Patent Information
- Application Number
- CN202411281803.9
- 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
When completing code through large models, there are accuracy problems in cross-file information processing, resulting in inaccurate recommended code generated.
By determining the code elements and their dependencies in the code file to be completed, a code dependency graph is constructed, relevant code elements are queryed and target code is generated through a big model based on their signatures, ensuring that the correlation meets the set conditions.
Improves the accuracy of large models in code completion, especially when processing cross-file information, and reduces errors and inaccuracies in generating code.
Smart Images

Figure CN119127215A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to artificial intelligence technology fields such as big models, deep learning, natural language processing, and Internet technology fields such as code recommendation, code generation, and software development. It can be applied to interactive scenarios based on artificial intelligence, such as generative search, smart assistants, code recommendation, and other application scenarios, and in particular to code completion methods, devices, and electronic devices based on big models. Background Art
[0002] The big model can handle a variety of tasks, such as code completion, knowledge questions and answers, NL2SQL (Natural Language to Structured Query Language), information extraction, etc. Taking code completion as an example, the context code at the cursor position in the code file that the user is currently editing can be input into the big model, so that the big model can output the recommended code at the cursor position based on this information. When completing code through the big model, how to improve the accuracy of the recommended code generated by the big model is a problem that needs to be solved. Summary of the invention
[0003] The present disclosure provides a code completion method, device and electronic device based on a large model.
[0004] According to a first aspect of the present disclosure, a code completion method based on a large model is provided, including: determining a first code element at a position to be completed in a first code file to be completed; determining a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs; determining, based on the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file; and generating a target code corresponding to the position to be completed through a large model based on a signature of the second code element.
[0005] According to a second aspect of the present disclosure, a code completion device based on a large model is provided, including: a first determination module, used to determine a first code element at a position to be completed in a first code file to be completed; a second determination module, used to determine a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs; a third determination module, used to determine, based on the first code element, a second code element whose correlation with the first code element meets a set condition, wherein the second code element belongs to at least one of the first code file and the second code file; and a generation module, used to generate a target code corresponding to the position to be completed through a large model based on a signature of the second code element.
[0006] According to a third aspect of the present disclosure, there is provided an electronic device, including:
[0007] at least one processor; and
[0008] a memory communicatively connected to the at least one processor; wherein,
[0009] 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 large model-based code completion method as described in the first aspect.
[0010] According to a fourth aspect of the present disclosure, 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 large model-based code completion method as described in the first aspect.
[0011] According to a fifth aspect of the present disclosure, a computer program product is provided, comprising computer instructions, which, when executed by a processor, implement the large model-based code completion method as described in the first aspect.
[0012] 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 disclosure, nor is it intended to limit the scope of the present disclosure. Other features of the present disclosure will become easily understood through the following description. BRIEF DESCRIPTION OF THE DRAWINGS
[0013] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.
[0014] Figure 1 It is a flowchart of a code completion method based on a large model provided according to an embodiment of the present disclosure;
[0015] Figure 2 is a flowchart of a code completion method based on a large model provided according to another embodiment of the present disclosure;
[0016] Figure 3 is a schematic diagram of a code dependency graph provided according to an embodiment of the present disclosure;
[0017] Figure 4 is a flowchart of a code completion method based on a large model provided according to another embodiment of the present disclosure;
[0018] Figure 5 is a structural diagram of a code completion device based on a large model provided according to an embodiment of the present disclosure;
[0019] Figure 6It is a block diagram of an electronic device used to implement the large model-based code completion method of the embodiment of the present disclosure. DETAILED DESCRIPTION
[0020] The following is a description of exemplary embodiments of the present disclosure in conjunction with the accompanying drawings, including various details of the embodiments of the present disclosure 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 may be made to the embodiments described herein without departing from the scope and spirit of the present disclosure. Similarly, for the sake of clarity and conciseness, descriptions of well-known functions and structures are omitted in the following description.
[0021] In the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, comply with the relevant laws and regulations, and do not violate public order and good morals.
[0022] The code completion method in the related art can input the context code of the cursor position in the code file currently being edited by the user into the big model, so that the big model can output the recommended code at the cursor position based on this information. In this way, the information input into the big model is only the information in the current code file. When the code to be completed at the cursor position requires cross-file information, such as when the code to be completed at the cursor position is part of the code of a class, and the class needs to call methods of other classes, the big model is prone to hallucination problems, so that the accuracy of the recommended code generated by the big model is poor.
[0023] In order to improve the accuracy of code completion, the embodiments of the present disclosure provide a code completion method, device and electronic device based on a large model.
[0024] The following describes a large model-based code completion method, device, and electronic device according to an embodiment of the present disclosure with reference to the accompanying drawings.
[0025] Among them, it should be noted that the executor of the big model-based code completion method of this embodiment is a big model-based code completion device. The big model-based code completion device can be implemented by software and / or hardware, and can be configured in an electronic device. The electronic device may include but is not limited to a terminal, a server, etc.
[0026] Figure 1 It is a flowchart of a code completion method based on a large model provided according to an embodiment of the present disclosure.
[0027] like Figure 1 As shown, the code completion method based on the large model includes:
[0028] Step 101: determine a first code element at a position to be completed in a first code file to be completed.
[0029] The first code file may be a code file opened in an IDE (Integrated Development Environment) workspace, part of which is to be completed, or other code files part of which is to be completed, and the present disclosure does not limit this. The IDE workspace refers to a logical container in the IDE for organizing and managing project files and resources. The workspace usually includes one or more development projects, each of which contains a series of code files, directories, and other resources required in the development process.
[0030] The position to be completed may be the position of the cursor in the first code file, or a position specified by a user in the first code file, which is not limited in the present disclosure.
[0031] Among them, code elements are the basic components of software programs, which may include classes, class methods, interfaces, properties, inner classes, configuration files, etc.
[0032] The first code element at the position to be completed is the code element including the position to be completed. Taking the position to be completed as the position of the cursor in the first code file as an example, the first code element at the position to be completed is the code element accessible to the cursor. It should be noted that the number of the first code elements at the position to be completed can be one or at least two.
[0033] For example, assuming that the position to be completed is the position of the cursor in the first code file, wherein a class is defined in the first code file, a method is defined in the class, the method includes a code snippet, and the cursor is located at a certain position in the code snippet, then the first code element where the position to be completed is located may include the method and the class.
[0034] Step 102: determine a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs.
[0035] A development project is a complete software application or a set of related software components, such as a standalone application, a website, a framework, a plug-in, or any other form of software implementation. In a development project, different code files may have dependencies.
[0036] The dependency between code files means that one code file needs resources provided by another code file to work properly. For example, one code file needs classes or methods provided by another code file to work properly. This dependency can be direct or indirect.
[0037] For example, code file 1 uses the class or function in code file 2 through an import statement, and code file 2 uses the method provided by code file 3 through an import statement. Then both code file 2 and code file 3 have a dependency relationship with code file 1, among which the dependency relationship between code file 1 and code file 2 is a direct one, and the dependency relationship between code file 1 and code file 3 is an indirect one.
[0038] The number of the second code files having a dependency relationship with the first code file may be one or at least two.
[0039] Step 103: Determine, based on the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file.
[0040] It can be understood that the first code file or each second code file includes at least one code element. In an exemplary embodiment, the code elements in the first code file and the second code file can be queried based on the first code element to determine a second code element whose correlation with the first code element meets the set conditions.
[0041] The number of the second code elements may be one or at least two. Each second code element may belong to the first code file, or to the second code file, or to both the first code file and the second code file.
[0042] The correlation between the second code element and the first code element, that is, the correlation degree between the second code element and the first code element. The judgment standard of the correlation between the second code element and the first code element can be set as needed. For example, when the similarity between the package name where the second code element is located and the package name where the first code element is located is high, it is determined that the correlation between the second code element and the first code element is high.
[0043] The correlation between the second code element and the first code element satisfies the set condition, which can be set as needed, for example, including: the correlation between the second code element and the first code element exceeds a correlation threshold. The correlation threshold can be set as needed, and the present disclosure does not limit this.
[0044] It should be noted that, when the number of the first code element is one, the correlation between the second code element and the first code element is the correlation between the second code element and the first code element. When the number of the first code element is at least two, the correlation between the second code element and the first code element can be understood as the correlation between the second code element and the entirety of the at least two first code elements.
[0045] Step 104: Based on the signature of the second code element, generate the target code corresponding to the position to be completed through the large model.
[0046] The signature of the second code element is an identifier for uniquely identifying the second code element, and may define the manner in which the second code element is called or referenced, etc. The signature of the second code element may include the name of the second code element, a parameter list, etc. For example, if the second code element is a class, the signature of the class may include, for example, the name of the class, the interface implemented by the class, or a combination of inherited parent classes.
[0047] Among them, the big model is a large neural network model trained with a deep learning algorithm, designed to understand and generate natural language text. The big model is trained with a large amount of text data, and can capture the complexity and nuances of the language, so as to perform various natural language processing tasks, such as text generation, question-answering systems, semantic understanding, and reasoning. This model is designed to improve the expressiveness and predictive performance of the model, and can handle more complex tasks and data, showing human-like intelligence. Among them, the big model can include GPT3 (Generative Pre-trained Transformer, Generative Pre-trained Transformer 3), GPT4, T5 (Text-to-Text Transfer Transformer, Text-to-Text Transformer), LLaMA (Large Language Model Meta AI, lightweight language model), etc.
[0048] The target code corresponding to the position to be completed is the code used to complete the position to be completed.
[0049] In an example embodiment, the signature of the second code element may be input into the large model, so that the target code corresponding to the position to be completed is output through the large model.
[0050] The code completion method based on a large model provided by the embodiment of the present disclosure determines a first code element at a position to be completed in a first code file to be completed, determines a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs, determines a second code element whose correlation with the first code element satisfies a set condition based on the first code element, wherein the second code element belongs to at least one of the first code file and the second code file, and generates a target code corresponding to the position to be completed through the large model based on the signature of the second code element. Since the second code file is a code file under the development project to which the first code file belongs and has a dependency relationship with the first code file, the second code element is a code element determined from the code elements of the first code file and the second code file and belongs to at least one of the first code file and the second code file, and the correlation between the second code element and the first code element satisfies the set conditions, when the target code corresponding to the position to be completed is generated by the large model based on the signature of the second code element, the large model can not only consider the code elements in the first code file to be completed whose correlation with the first code element satisfies the set conditions, but also consider the code elements in the second code file that has a dependency relationship with the first code file and whose correlation with the first code element satisfies the set conditions, thereby avoiding the problem of low accuracy of the target code output by the large model when code completion is required across file information, thereby improving the accuracy of code completion performed by the large model.
[0051] In order to clearly illustrate the process of determining, based on a first code element, a second code element whose correlation with the first code element satisfies a set condition, the embodiment of the present disclosure further provides a code completion method based on a large model.
[0052] Figure 2 It is a flowchart of a large model-based code completion method provided according to another embodiment of the present disclosure.
[0053] like Figure 2 As shown, the code completion method based on the large model includes:
[0054] Step 201: determine the first code element at the position to be completed in the first code file to be completed.
[0055] Step 202: Determine a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs.
[0056] Step 203: construct a code dependency graph based on the first code file and the second code file, wherein the code dependency graph includes at least two nodes and directed edges between the nodes, the nodes represent code elements, and the directed edges represent the dependency relationship between the code elements corresponding to the connected nodes.
[0057] The code dependency graph is a graphical representation in software development, which is used to describe the dependency relationship between code elements. The method of constructing the code dependency graph based on the first code file and the second code file can refer to the relevant technology and will not be repeated here.
[0058] In a possible implementation form, taking a Java program as an example, the code element includes at least one of the following: a class, an interface, a property, a class method, an inner class, and a configuration file.
[0059] Among them, the inner class is the class included in the class.
[0060] That is, each node in the code dependency graph can represent one of a class, an interface, a field, a method, an inner class, or a configuration file, and the set of code elements represented by all nodes can include at least one of a class, an interface, a field, a method, an inner class, or a configuration file. For example, suppose a code dependency graph includes 90 nodes, of which 10 nodes represent classes, 20 nodes represent interfaces, 20 nodes represent fields, 20 nodes represent class methods, 10 nodes represent inner classes, and 10 nodes represent configuration files.
[0061] In a possible implementation form, in the code dependency graph, the dependency relationship between code elements includes at least one of the following:
[0062] The inheritance (extend) relationship between classes;
[0063] The implementation relationship between classes and interfaces;
[0064] The include relationship between classes and attributes;
[0065] The containment relationship between classes and class methods;
[0066] The containment relationship between classes and inner classes;
[0067] The configuration relationship between classes and configuration files;
[0068] The function parameter input (parmer_in) relationship between classes and class methods;
[0069] The function output parameter (parmer_out) relationship between classes and class methods.
[0070] For example, a partial schematic diagram of a code dependency graph constructed based on the first code file and the second code file can be as follows: Figure 3As shown. Among them, nodes 301, 303, and 311 all represent classes. In order to distinguish the classes represented by different nodes, Figure 3 Nodes 305, 309, and 310 all represent class methods. To distinguish the class methods represented by different nodes, Figure 3 This is illustrated by class method 1, class method 2, and class method 3. Nodes 307 and 308 both represent configuration files. To facilitate distinguishing configuration files represented by different nodes, Figure 3 The configuration files 1 and 2 are used as examples. Node 302 represents an interface, node 304 represents an attribute, and node 306 represents an inner class.
[0071] Among them, the directed edge between node 301 and node 303 represents the inheritance relationship between class 1 and class 3, and the direction of the directed edge is from node 303 to node 301, indicating that the inheritance relationship between class 1 and class 3 is that class 1 inherits the properties and methods of class 3, that is, class 3 is the parent class of class 1.
[0072] The directed edge between node 303 and node 302 indicates the implementation relationship between class 1 and the interface, and the direction of the directed edge is from node 303 to node 302, indicating that the implementation relationship between class 1 and the interface is that class 1 provides specific implementations of all methods declared in the interface.
[0073] The directed edge between node 303 and node 304 indicates the inclusion relationship between class 1 and the attribute, and the direction of the directed edge points from node 303 to node 304, indicating that the inclusion relationship between class 1 and the attribute is that class 1 contains the attribute.
[0074] The directed edge between node 303 and node 305 indicates the inclusion relationship between class 1 and class method 3, and the direction of the directed edge is from node 303 to node 305, indicating that the inclusion relationship between class 1 and class method 3 is that class 1 contains class method 3.
[0075] The directed edge between node 303 and node 306 indicates the inclusion relationship between class 1 and the inner class, and the direction of the directed edge points from node 303 to node 306, indicating that the inclusion relationship between class 1 and the inner class is that class 1 contains the inner class.
[0076] The directed edge between node 303 and node 307 represents the configuration relationship between class 1 and configuration file 1, and the direction of the directed edge is from node 303 to node 307, indicating that the configuration relationship between class 1 and configuration file 1 is that configuration file 1 is used to store and manage program runtime parameters, settings, and dependencies, while class 1 is the code structure that implements these functions and logics. The directed edge between node 303 and node 308 is similar.
[0077] The directed edge between node 311 and node 309 indicates the inclusion relationship between class 2 and class method 1, and the direction of the directed edge is from node 311 to node 309, indicating that the inclusion relationship between class 2 and class method 1 is that class 2 includes class method 1. The directed edge between node 311 and node 310 is similar.
[0078] The directed edge between node 303 and node 309 represents the function input parameter relationship between class 1 and class method 1, and the direction of the directed edge is from node 303 to node 309, indicating that the function input parameter relationship between class 1 and class method 1 is that a function in class 1 receives class method 1 as a parameter.
[0079] The directed edge between node 303 and node 310 indicates the function output parameter relationship between class 1 and class method 2, and the direction of the directed edge is from node 303 to node 310, indicating that the function output parameter relationship between class 1 and class method 2 is that a function in class 1 returns class method 2 as a result.
[0080] A code dependency graph is constructed based on the first code file and the second code file, wherein the nodes in the code dependency graph represent code elements, and the code elements include at least one of classes, interfaces, attributes, class methods, inner classes, and configuration files. The directed edges between the nodes represent the dependency relationships between the code elements corresponding to the connected nodes, and the dependency relationships include at least one of the following: an inheritance relationship between classes; an implementation relationship between classes and interfaces; a containment relationship between classes and attributes; a containment relationship between classes and class methods; a containment relationship between classes and inner classes; a configuration relationship between classes and configuration files; a function input parameter relationship between classes and class methods; and a function output parameter relationship between classes and class methods. This realizes a clear representation of the dependency relationships between the above code elements based on the code dependency graph, laying a foundation for the subsequent accurate determination of the second code element based on the code dependency graph.
[0081] Step 204: determine the first node where the first code element in the code dependency graph is located.
[0082] When the number of the first code element is one, the number of the first node is one. When the number of the first code element is at least two, the number of the first node is at least two.
[0083] Step 205 , querying the code dependency graph based on the first node to obtain candidate nodes, where the candidate nodes include nodes directly or indirectly pointed to by the first node through directed edges.
[0084] In an example embodiment, for each first node, the code dependency graph may be queried based on the first node and in the direction of the directed edge, thereby obtaining a node that the first node directly or indirectly points to through the directed edge, and using the node as a candidate node. The number of candidate nodes may be one or at least two. The code element represented by the candidate node may be referred to as a candidate code element.
[0085] Among them, a candidate node is a node indirectly pointed to by the first node through a directed edge, which means that there are other nodes between the candidate node and the first node, and the direction of the directed edge is from the first node to other nodes, and finally to the candidate node. For example, node 1 points to node 2 through a directed edge, node 2 points to node 3 through a directed edge, and node 3 points to node 4 through a directed edge. Then node 1 directly points to node 2 through a directed edge, and node 1 indirectly points to node 3 and node 4 through directed edges. In the case where node 1 is the first node, the candidate nodes include node 2, node 3, and node 4.
[0086] It can be understood that since the candidate node is the node directly or indirectly pointed to by the first node through a directed edge, the relationship between the first node and the candidate node is that the code element represented by the first node directly or indirectly depends on the code element represented by the candidate node.
[0087] refer to Figure 3 Assuming that the first node is node 303, by querying the code dependency graph, the candidate nodes obtained may at least include: nodes 301, 302, 304, 305, 306, 307, 308, 309, and 310.
[0088] In a possible implementation form, the dependency relationship represented by the directed edge in the code dependency graph includes at least one of the following: an inheritance relationship and an implementation relationship; the candidate node includes at least one of the second node and the third node;
[0089] The second node is located on the target path of the first node, and is directly or indirectly pointed to by the first node through a directed edge, and the dependency relationship represented by the directed edge on the target path is an inheritance relationship or a realization relationship;
[0090] The third node is a node directly pointed to by the first node or the second node through a directed edge, and the dependency relationship represented by the directed edge between the first node or the second node and the third node is other dependency relationship except the inheritance relationship and the implementation relationship.
[0091] The target path where the first node is located may include one or more. The dependency relationship represented by each directed edge on one of the target paths is an inheritance relationship or an implementation relationship. Accordingly, the dependency relationships represented by all directed edges on a target path may include only inheritance relationships, or only implementation relationships, or both inheritance relationships and implementation relationships.
[0092] Continue to refer Figure 3 , assuming that node 303 is the first node, and assuming that node 301 points to another node A through a directed edge ( Figure 3 (not shown), and the dependency relationship represented by the directed edge is an inheritance relationship, node A represents a class, and node 301 also points to another node B through a directed edge ( Figure 3 (not shown), and the dependency relationship represented by the directed edge is an implementation relationship, and node B represents an interface. Then the path composed of the directed edge between node 303 and node 301, and the directed edge between node 301 and node A is a target path 1; the path composed of the directed edge between node 303 and node 301, and the directed edge between node 301 and node B is a target path 2; the path composed of the directed edge between node 303 and node 302 is a target path 3.
[0093] The second node may include the node 301 on the target paths 1 and 2, which is directly pointed to by the first node 303 through a directed edge, and may also include the node A on the target path 1, which is indirectly pointed to by the first node through a directed edge, and may also include the node B on the target path 2, which is indirectly pointed to by the first node 303 through a directed edge, and may also include the node 302 on the target path 3, which is directly pointed to by the first node 303 through a directed edge.
[0094] The third node may include nodes 304, 305, 306, 307, 308, 309, and 310 directly pointed to by node 303 through directed edges, and nodes 301, 302, node A, and node B directly pointed to by directed edges respectively ( Figure 3 not shown).
[0095] It should be noted that, taking nodes 303, 301 and nodes A and B as examples, since node 303 directly points to node 301 through a directed edge, the inheritance relationship between nodes 303 and 301 can be referred to as a direct inheritance relationship in the embodiments of the present disclosure; since node 303 indirectly points to node A through a directed edge, the relationship between node 303 and node A can be referred to as an indirect inheritance relationship in the embodiments of the present disclosure; since node 303 indirectly points to node B through a directed edge, the relationship between node 303 and node B can be referred to as an indirect inheritance relationship or a realization relationship in the embodiments of the present disclosure.
[0096] It is understandable that, taking classes as an example, the inheritance relationship between classes is usually not limited to two classes. For example, class 1 can inherit the properties and methods of class 2, and class 2 can inherit the properties and methods of class 3. Correspondingly, the dependency relationship between class 1 and class 3 can be called an indirect inheritance relationship. Similarly, the implementation relationship between a class and an interface is usually not limited to the class and the interface, and other dependency relationships except inheritance and implementation relationships are usually limited to two code elements.
[0097] In the embodiment of the present disclosure, by querying an infinite number of directed edges in the code dependency graph based on the first node, the candidate code element represented by the second node can be accurately determined, and the dependency relationship between the candidate code element and the first code element is a direct inheritance relationship or implementation relationship, or the dependency relationship between the candidate code element and the first code element is an indirect inheritance relationship or implementation relationship. By searching for a finite number of directed edges, the query scope of the nodes in the code dependency graph can be reduced, and the candidate code element represented by the third node can be quickly determined. The dependency relationship between the candidate code element and the first code element or the candidate code element represented by the second node is a dependency relationship other than an inheritance relationship and a dependency relationship, thereby laying a foundation for the subsequent rapid and accurate determination of the second code element from the candidate code elements.
[0098] Step 206: Determine the correlation between the candidate code element represented by the candidate node and the first code element.
[0099] In a possible implementation form, for a candidate code element represented by any candidate node, the correlation between the candidate code element and the first code element may be determined in the following manner:
[0100] Determine a correlation between the candidate code element and the first code element based on at least one of the following:
[0101] Similarity between the package name where the candidate code element is located and the package name where the first code file is located;
[0102] The distance between the first character position of the candidate code element in the first code file and the second character position of the position to be completed in the first code file;
[0103] Wherein, when the candidate code element is not in the first code file, the first character position is determined based on the character position of the first code element in the first code file.
[0104] In one possible implementation form, the difference between the first character position and the second character position can be used as the distance between the first character position and the second character position, and then the ratio of the difference to the second character position is determined, and the difference between 1 and the ratio is used as a score representing the correlation between the candidate code element and the first code element.
[0105] The character position may be a position relative to the beginning or other reference point in the first code file.
[0106] The second character position of the position to be completed in the first code file may be the character position of the cursor in the first code file, such as the character number of the cursor in the first code file.
[0107] As for the candidate code element, when the candidate code element is in the first code file, the first character position of the candidate code element in the first code file can be the starting character position of the candidate code element in the first code file. When the candidate code element is not in the first code file, the first character position of the candidate code element in the first code file can be determined based on the ratio of the character position of the first code element in the first code file to the preset value. Among them, the first code element can be the code element represented by the first node used when the node corresponding to the candidate code element is queried. When the number of the first code elements used is one, the ratio of the character position of the first code element used in the first code file to the preset value can be used as the first character position of the candidate code element in the first code file. When the number of the first code elements used is multiple, the sum of the ratios of the character positions of all the first code elements used in the first code file to the preset value can be used as the first character position of the candidate code element in the first code file. Among them, the preset value can be set as needed, such as 2.
[0108] For example, assuming that the first node where the position to be completed is located includes node a and node c, node a represents the first code element 1, and node c represents the first code element 2, wherein the character position of the first code element 1 in the first code file is the 100th character, the character position of the first code element 2 in the first code file is the 200th character, and the preset value is 2. The correlation between the candidate code element and the first code element represents the correlation between the candidate code element and the first code elements 1 and 2 as a whole.
[0109] Assume that based on node a, the code dependency graph is queried and node b is found. Based on node c, the code dependency graph is queried and node b is also found. Node b is not in the first code file, and node b represents candidate code element 3. Then the ratio of the character position of the first code element 1 in the first code file to the preset value 2 and the ratio of the character position of the first code element 2 in the first code file to the preset value 2 can be summed to obtain that the first character position of the candidate code element 3 in the first code file is the 150th character.
[0110] Assume that based on node a, the code dependency graph is queried, node d is found, node d is not in the first code file, and node d represents candidate code element 4. Then, based on the ratio of the character position of the first code element 1 in the first code file to the preset value 2, it can be determined that the first character position of the candidate code element 4 in the first code file is the 50th character.
[0111] Assuming that the second character position of the position to be completed in the first code file is the 300th character, it can be determined that the distance between the first character position of the candidate code element 3 in the first code file and the second character position of the position to be completed in the first code file is 300-150, that is, 150, and then 1-150 / 300 can be used as a score representing the correlation between the candidate code element 3 and the first code elements 1 and 2 as a whole. And it can be determined that the distance between the first character position of the candidate code element 4 in the first code file and the second character position of the position to be completed in the first code file is 300-50, that is, 250, and then 1-250 / 300 can be used as a score representing the correlation between the candidate code element 4 and the first code elements 1 and 2 as a whole.
[0112] In a possible implementation, the similarity between the package name where the candidate code element is located and the package name where the first code file is located can be determined based on the edit distance between the package name where the candidate code element is located and the package name where the first code file is located. For example, the ratio of 1 to the edit distance can be used as a score representing the similarity between the package name where the candidate code element is located and the package name where the first code file is located, and then the ratio of 1 to the edit distance can be used as a score representing the correlation between the candidate code element and the first code element.
[0113] In a possible implementation form, a score indicating the similarity between the package name where the candidate code element is located and the package name where the first code file is located can be obtained, as well as a score indicating the distance between the first character position and the second character position, wherein the first character position is the character position where the candidate code element is located in the first code file, and the second character position is the character position where the position to be completed is located in the first code file, and the weighted sum of the two scores is used as a score indicating the correlation between the candidate code element and the first code element. The weight corresponding to each score can be set as needed.
[0114] Therefore, as needed, at least one of the distance between the first character position and the second character position and the similarity between the package name where the candidate code element is located and the package name where the first code file is located can be flexibly selected to determine the correlation between the candidate code element and the first code element, wherein the first character position is the character position where the candidate code element is located in the first code file, and the second character position is the character position where the position to be completed is located in the first code file, and by comprehensively determining the correlation between the candidate code element and the first code element based on the distance between the first character position and the second character position and the similarity between the package name where the candidate code element is located and the package name where the first code file is located, the accuracy of the determined correlation can be improved.
[0115] Step 207: Determine a second code element from the candidate code elements based on the correlation.
[0116] The second code element belongs to at least one of the first code file and the second code file.
[0117] In a possible implementation form, the candidate code elements may be sorted in descending order of relevance, so that the top N candidate code elements are determined as the second code elements whose relevance to the first code element satisfies a set condition. N may be an integer greater than or equal to 1 and may be set as required.
[0118] In a possible implementation form, among the candidate code elements, the candidate code elements whose correlation with the first code element exceeds a correlation threshold may be determined as the second code elements whose correlation with the first code element satisfies a set condition.
[0119] By first determining the first node where the first code element is located in the code dependency graph, it is possible to only query the nodes directly or indirectly pointed to by the first node in the code dependency graph through directed edges, and determine the second code element from the candidate code elements based on whether the correlation between the candidate code elements represented by these nodes and the first code element meets the set conditions. In this way, the query range of the nodes in the code dependency graph can be reduced, thereby improving the efficiency of determining the second code element.
[0120] It should be noted that, through the method shown in steps 204-207, it is possible to query the code dependency graph according to the first code element to obtain a second code element whose correlation with the first code element meets the set conditions, wherein the second code element belongs to at least one of the first code file and the second code file. In a possible implementation form, it is also possible to query the code dependency graph according to the first code element in other ways to obtain a second code element whose correlation with the first code element meets the set conditions, wherein the second code element belongs to at least one of the first code file and the second code file. For example, each node except the first node in the code dependency graph can be used as a candidate node, and then the correlation between the candidate code element represented by the candidate node and the first code element is determined, and based on the correlation, the second code element is determined from the candidate code elements.
[0121] Step 208: Based on the signature of the second code element, generate the target code corresponding to the position to be completed through the large model.
[0122] Among them, the specific implementation forms of steps 201-208 and similar steps in other embodiments can refer to the detailed descriptions in other embodiments of the present disclosure, and will not be described in detail here.
[0123] In an embodiment of the present disclosure, by determining a first code element at a position to be completed in a first code file to be completed, determining a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs, and constructing a code dependency graph based on the first code file and the second code file, wherein the code dependency graph includes at least two nodes and directed edges between the nodes, the nodes represent code elements, and the directed edges represent the dependency relationship between the code elements corresponding to the connected nodes, then determining a first node in the code dependency graph where the first code element is located, querying the code dependency graph based on the first node to obtain candidate nodes, the candidate nodes including nodes directly or indirectly pointed to by the first node through directed edges, determining the correlation between the candidate code element represented by the candidate node and the first code element, and determining the second code element from the candidate code element based on the correlation, thereby realizing querying the code dependency graph according to the first code element to obtain a second code element whose correlation with the first code element meets a set condition, wherein the second code element belongs to at least one of the first code file and the second code file. Since the code dependency graph can clearly represent the dependency relationship between code elements, by constructing a code dependency graph based on the first code file and the second code file, and querying the code dependency graph based on the first code element, it is possible to accurately traverse the code elements in the first code file and the second code file based on the code dependency graph, thereby accurately determining the second code element whose correlation with the first code element meets the set conditions, and then based on the signature of the second code element, generate the target code corresponding to the position to be completed through the large model, which can further improve the accuracy of code completion through the large model.
[0124] Combine the following Figure 4 , further illustrating the large model-based code completion method provided in the embodiment of the present disclosure.
[0125] Figure 4 It is a flowchart of a large model-based code completion method provided according to another embodiment of the present disclosure.
[0126] like Figure 4 As shown, the code completion method based on the large model includes:
[0127] Step 401, obtaining a syntax tree corresponding to a first code file to be completed.
[0128] The syntax tree is a tree representation of the code, which decomposes the code into a series of structured nodes according to the grammatical rules of the language. Each node in the syntax tree represents a construct in the code, such as an expression, statement, or declaration.
[0129] Among them, the syntax tree corresponding to the first code file, that is, the tree representation of the code in the first code file, can be constructed according to the method in the relevant technology, and the present disclosure will not repeat the construction process of the syntax tree corresponding to the first code file.
[0130] Step 402, traverse the syntax tree to obtain the first code element where the position to be completed is located in the first code file.
[0131] It is understandable that in the syntax tree corresponding to the first code file, each node represents a structure in the code of the first code file, including the scope of the position to be completed in the first code file, etc., wherein the scope of the position to be completed specifies which variables, functions, classes, etc. can be accessed by the position to be completed. By traversing the syntax tree, the scope of the position to be completed can be obtained, and then the first code element where the position to be completed is located in the first code file is determined according to the scope of the position to be completed.
[0132] In a possible implementation form, the first code element where the position to be completed is located in the first code file may include a class that can be accessed by the position to be completed in the first code file.
[0133] Since the syntax tree can clearly represent the structure in the code, the first code element where the position to be completed is located in the first code file can be quickly and accurately determined by traversing the syntax tree corresponding to the first code file.
[0134] Step 403: Determine a second code file having a dependency relationship with the first code file from the development project to which the first code file belongs.
[0135] In a possible implementation form, step 403 may be implemented in the following manner:
[0136] Obtaining candidate code files under the development project to which the first code file belongs;
[0137] The candidate code files are parsed to obtain a second code file in the candidate code files that has a dependency relationship with the first code file.
[0138] The candidate code file may be any code file other than the first code file in the development project to which the first code file belongs. The number of candidate code files may be one or at least two.
[0139] Among them, for each candidate code file, by performing grammatical analysis on the candidate code file, it can be determined whether the first code file needs the resources provided by the candidate code file to work normally, or whether the candidate code file needs the resources provided by the first code file to work normally, thereby determining whether there is a dependency relationship between the candidate code file and the first code file.
[0140] By performing syntax analysis on the candidate code files under the development project to which the first code file belongs, the second code file under the development project to which the first code file belongs and which has a dependency relationship with the first code file can be accurately obtained.
[0141] In a possible implementation form, the code completion device based on the large model can periodically perform syntax analysis on each code file in the background by performing syntax analysis on other code files under the development project to which the code file belongs, to obtain a code file having a dependency relationship with the code file, so that when a first code file is accessed, a second code file having a dependency relationship with the first code file under the development project to which the first code file belongs can be directly obtained from the pre-obtained file. Thus, when code completion is required for the first code file, the second code file having a dependency relationship with the first code file under the development project to which the first code file belongs can be quickly obtained.
[0142] It is understandable that, in the above-mentioned method of obtaining files at regular intervals in the background, there may be a situation where, when the first code file is accessed, the background has not yet completed the syntax analysis of other code files under the development project to which it belongs. Therefore, in a possible implementation form, the code completion device based on the large model can obtain the second code file that has a dependency relationship with the first code file by performing syntax analysis on other candidate code files under the development project to which the first code file belongs in real time when the first code file is accessed. In this way, when it is necessary to complete the code of the first code file, the second code file that has a dependency relationship with the first code file under the development project to which it belongs can be more comprehensively obtained.
[0143] In a possible implementation form, the above two methods may be combined to comprehensively and quickly obtain a second code file that has a dependency relationship with the first code file under the development project to which the first code file belongs.
[0144] Step 404: Determine, based on the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file.
[0145] Step 405: Obtain the context code of the position to be completed in the first code file.
[0146] The context code of the position to be completed may include at least one of the codes before and after the position to be completed in the first code file.
[0147] Step 406, input the signature and context code of the second code element into the large model to obtain the target code corresponding to the position to be completed output by the large model.
[0148] In a possible implementation form, when the second code element includes a code element, the signature of the code element and the context code of the position to be completed can be input into the large model, so that the target code corresponding to the position to be completed is output through the large model.
[0149] In a possible implementation form, when the second code element includes at least two code elements, the signatures of the at least two code elements and the context code of the position to be completed can be input into the large model, so that the target code corresponding to the position to be completed can be output through the large model.
[0150] Therefore, when the target code corresponding to the position to be completed is generated by the large model, the large model can not only consider the code elements in the first code file to be completed and the second code file that has a dependency relationship with the first code file, whose correlation with the first code element meets the set conditions, but also consider the context code of the position to be completed in the first code file, so that the target code corresponding to the position to be completed can be generated based on more comprehensive information, thereby further improving the accuracy of code completion through the large model.
[0151] It can be understood that since the character length input into the large model is limited, and the longer the character length, the slower the reasoning speed of the large model, in a possible implementation form of the present disclosure, when the second code element includes at least two code elements in the same code file, the signatures of the at least two code elements can be fused to obtain a fused signature, and the fused signature and the context code of the position to be completed are input into the large model, so that the target code corresponding to the position to be completed is output through the large model.
[0152] That is, in the case where the second code element includes at least two code elements in the same code file, step 406 can be replaced by:
[0153] Merging signatures of at least two code elements to obtain a merged signature;
[0154] The fusion signature and context code are input into the large model to obtain the target code output by the large model.
[0155] The method of fusing the signatures of at least two code elements to obtain the fused signature can be set as needed. For example, the common part of the signatures of at least two code elements can be extracted, and then the common part can be quoted in the description to obtain the fused signature. Alternatively, for the signatures of at least two code elements, meaningful abbreviations can be used, and then the abbreviations can be spliced to obtain the fused signature.
[0156] By fusing the signatures of at least two code elements to obtain a fused signature when the second code element includes at least two code elements in the same code file, and then inputting the fused signature and the context code into the large model, the character length of the input large model can be shortened, thereby saving the time for the large model to infer the target code corresponding to the position to be completed, and improving the efficiency of generating the target code corresponding to the position to be completed.
[0157] In an embodiment of the present disclosure, by obtaining a syntax tree corresponding to a first code file to be completed, traversing the syntax tree, and obtaining a first code element in the first code file where the position to be completed is located, the first code element in the first code file where the position to be completed is located can be quickly and accurately determined, by determining a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs, and determining a second code element whose correlation with the first code element meets a set condition according to the first code element, wherein the second code element belongs to at least one of the first code file and the second code file, obtaining a context code of the position to be completed in the first code file, inputting a signature and a context code of the second code element into a large model, and obtaining a target code corresponding to the position to be completed output by the large model, so that the large model can not only consider the code elements in the first code file to be completed and the second code file having a dependency relationship with the first code file, whose correlation with the first code element meets the set condition, but also consider the context code of the position to be completed in the first code file, so that the target code corresponding to the position to be completed can be generated based on more comprehensive information, thereby further improving the accuracy of code completion by the large model.
[0158] In order to implement the above embodiments, the present disclosure also provides a code completion device based on a large model.
[0159] Figure 5 It is a structural diagram of a large model-based code completion device provided according to an embodiment of the present disclosure.
[0160] like Figure 5 As shown, the code completion device 500 based on the large model includes:
[0161] A first determining module 501 is used to determine a first code element in a first code file to be completed where a position to be completed is located;
[0162] A second determining module 502 is used to determine a second code file having a dependency relationship with the first code file from a development project to which the first code file belongs;
[0163] A third determining module 503 is used to determine, based on the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file;
[0164] The generating module 504 is used to generate the target code corresponding to the position to be completed through the large model based on the signature of the second code element.
[0165] As a possible implementation of the embodiment of the present disclosure, the third determining module 503 includes:
[0166] A construction submodule, configured to construct a code dependency graph based on the first code file and the second code file, wherein the code dependency graph includes at least two nodes and directed edges between the nodes, the nodes represent code elements, and the directed edges represent dependency relationships between code elements corresponding to the connected nodes;
[0167] The query submodule is used to query the code dependency graph according to the first code element to obtain the second code element.
[0168] As a possible implementation of the embodiment of the present disclosure, the query submodule includes:
[0169] A first determining unit, configured to determine a first node in the code dependency graph where a first code element is located;
[0170] A query unit, configured to query the code dependency graph based on the first node to obtain candidate nodes, wherein the candidate nodes include nodes directly or indirectly pointed to by the first node through directed edges;
[0171] A second determining unit, configured to determine a correlation between the candidate code element represented by the candidate node and the first code element;
[0172] The third determining unit is used to determine the second code element from the candidate code elements based on the correlation.
[0173] As a possible implementation of the embodiment of the present disclosure, the dependency relationship represented by the directed edge in the code dependency graph includes at least one of the following: an inheritance relationship and an implementation relationship; the candidate node includes at least one of the second node and the third node;
[0174] The second node is located on the target path of the first node, and is directly or indirectly pointed to by the first node through a directed edge, and the dependency relationship represented by the directed edge on the target path is an inheritance relationship or a realization relationship;
[0175] The third node is a node directly pointed to by the first node or the second node through a directed edge, and the dependency relationship represented by the directed edge between the first node or the second node and the third node is other dependency relationship except the inheritance relationship and the implementation relationship.
[0176] As a possible implementation manner of the embodiment of the present disclosure, the second determining unit includes:
[0177] A determination subunit is used to determine the correlation between the candidate code element and the first code element based on at least one of the following:
[0178] Similarity between the package name where the candidate code element is located and the package name where the first code file is located;
[0179] The distance between the first character position of the candidate code element in the first code file and the second character position of the position to be completed in the first code file;
[0180] Wherein, when the candidate code element is not in the first code file, the first character position is determined based on the character position of the first code element in the first code file.
[0181] As a possible implementation of the embodiment of the present disclosure, the code element includes at least one of the following: a class, an interface, a property, a class method, an inner class, and a configuration file; and the dependency relationship between the code elements includes at least one of the following:
[0182] The inheritance relationship between classes;
[0183] The implementation relationship between classes and interfaces;
[0184] The containment relationship between classes and attributes;
[0185] The containment relationship between classes and class methods;
[0186] The containment relationship between classes and inner classes;
[0187] The configuration relationship between classes and configuration files;
[0188] The function parameter relationship between classes and class methods;
[0189] The function output parameter relationship between classes and class methods.
[0190] As a possible implementation of the embodiment of the present disclosure, the first determining module 501 includes:
[0191] A first acquisition submodule, used to acquire a syntax tree corresponding to a first code file;
[0192] The second acquisition submodule is used to traverse the syntax tree to obtain the first code element where the position to be completed is located in the first code file.
[0193] As a possible implementation of the embodiment of the present disclosure, the code completion device 500 based on the large model further includes:
[0194] An acquisition module, used for acquiring context code of a position to be completed in a first code file;
[0195] Accordingly, the generating module 504 includes:
[0196] The first generation submodule is used to input the signature and context code of the second code element into the large model to obtain the target code output by the large model.
[0197] As a possible implementation of the embodiment of the present disclosure, the second code element includes at least two code elements in the same code file; the generating module 504 includes:
[0198] A second generating submodule is used to merge the signatures of at least two code elements to obtain a merged signature;
[0199] The third generation submodule is used to input the fusion signature and context code into the large model to obtain the target code output by the large model.
[0200] As a possible implementation of the embodiment of the present disclosure, the second determining module 502 includes:
[0201] A third acquisition submodule is used to acquire a candidate code file under the development project to which the first code file belongs;
[0202] The analysis submodule is used to perform syntax analysis on the candidate code files to obtain a second code file in the candidate code files that has a dependency relationship with the first code file.
[0203] It should be noted that the aforementioned explanation of the code completion method based on the large model is also applicable to the code completion device based on the large model of this embodiment, and will not be repeated here.
[0204] According to an embodiment of the present disclosure, the present disclosure also provides an electronic device, a readable storage medium and a computer program product.
[0205] Figure 6 A schematic block diagram of an example electronic device 600 that can be used to implement an embodiment of the present disclosure is shown. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workstations, 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 disclosure described and / or required herein.
[0206] like Figure 6As shown, the device 600 includes a computing unit 601, which can perform various appropriate actions and processes according to a computer program stored in a ROM (Read-Only Memory) 602 or a computer program loaded from a storage unit 605 to a RAM (Random Access Memory) 603. In RAM 603, various programs and data required for the operation of the device 600 can also be stored. The computing unit 601, ROM 602, and RAM 603 are connected to each other via a bus 604. An I / O (Input / Output) interface 605 is also connected to the bus 604.
[0207] A number of components in the device 600 are connected to the I / O interface 605, including: an input unit 606, such as a keyboard, a mouse, etc.; an output unit 607, such as various types of displays, speakers, etc.; a storage unit 605, such as a disk, an optical disk, etc.; and a communication unit 609, such as a network card, a modem, a wireless communication transceiver, etc. The communication unit 609 allows the device 600 to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks.
[0208] The computing unit 601 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 601 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 601 performs the various methods and processes described above, such as a code completion method based on a large model. For example, in some embodiments, the code completion method based on a large model may be implemented as a computer software program, which is tangibly included in a machine-readable medium, such as a storage unit 605. In some embodiments, part or all of the computer program may be loaded and / or installed on the device 600 via the ROM 602 and / or the communication unit 609. When the computer program is loaded into the RAM 603 and executed by the computing unit 601, one or more steps of the code completion method based on the large model described above may be executed. Alternatively, in other embodiments, the computing unit 601 may be configured to execute the large model-based code completion method in any other appropriate manner (for example, by means of firmware).
[0209] 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.
[0210] The program code for implementing the method of the present disclosure may be written in any combination of one or more programming languages. These program codes may 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, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The program code may 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.
[0211] In the context of the present disclosure, 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.
[0212] 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).
[0213] 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.
[0214] 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 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", or "VPS" for short). The server may also be a server of a distributed system, or a server combined with a blockchain.
[0215] It should be noted that artificial intelligence is a discipline that studies how computers can simulate certain human thought processes and intelligent behaviors (such as learning, reasoning, thinking, planning, etc.), and includes both hardware-level and software-level technologies. Artificial intelligence hardware technologies generally include technologies such as sensors, dedicated artificial intelligence chips, cloud computing, distributed storage, and big data processing; artificial intelligence software technologies mainly include computer vision technology, speech recognition technology, natural language processing technology, as well as machine learning / deep learning, big data processing technology, knowledge graph technology, and other major directions.
[0216] 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 disclosure can be executed in parallel, sequentially or in different orders, as long as the desired results of the technical solutions disclosed in this disclosure can be achieved, and this document does not limit this.
[0217] The above specific implementations do not constitute a limitation on the protection scope of the present disclosure. 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 modification, equivalent substitution and improvement made within the spirit and principle of the present disclosure shall be included in the protection scope of the present disclosure.
Claims
1. A code completion method based on a large model, include: Determine the first code element at the position to be completed in the first code file to be completed; Determine, from the development project to which the first code file belongs, a second code file having a dependency relationship with the first code file; Determine, according to the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file; Based on the signature of the second code element, a target code corresponding to the position to be completed is generated through a large model.
2. The method according to claim 1, in, The determining, according to the first code element, a second code element whose correlation with the first code element satisfies a set condition comprises: Based on the first code file and the second code file, construct a code dependency graph, wherein the code dependency graph includes at least two nodes and directed edges between the nodes, the nodes represent code elements, and the directed edges represent dependency relationships between code elements corresponding to the connected nodes; The code dependency graph is queried according to the first code element to obtain the second code element.
3. The method according to claim 2, in, The querying the code dependency graph according to the first code element to obtain the second code element includes: Determine a first node in the code dependency graph where the first code element is located; Querying the code dependency graph based on the first node to obtain candidate nodes, where the candidate nodes include nodes directly or indirectly pointed to by the first node through the directed edges; Determining a correlation between a candidate code element represented by the candidate node and the first code element; Based on the correlation, the second code element is determined from the candidate code elements.
4. The method according to claim 3, in, The dependency relationship represented by the directed edge in the code dependency graph includes at least one of the following: an inheritance relationship and an implementation relationship; the candidate node includes at least one of the second node and the third node; The second node is located on the target path where the first node is located, and is directly or indirectly pointed to by the first node through the directed edge, and the dependency relationship represented by the directed edge on the target path is the inheritance relationship or the implementation relationship; The third node is a node directly pointed to by the first node or the second node through the directed edge, and the dependency relationship represented by the directed edge between the first node or the second node and the third node is other dependency relationship except the inheritance relationship and the implementation relationship.
5. The method according to claim 3, in, The determining the correlation between the candidate code element represented by the candidate node and the first code element comprises: Determine the correlation between the candidate code element and the first code element based on at least one of the following: The similarity between the package name where the candidate code element is located and the package name where the first code file is located; The distance between a first character position of the candidate code element in the first code file and a second character position of the position to be completed in the first code file; Wherein, when the candidate code element is not in the first code file, the first character position is determined based on the character position of the first code element in the first code file.
6. The method according to claim 2, in, The code elements include at least one of the following: class, interface, property, class method, inner class, configuration file; the dependency relationship between the code elements includes at least one of the following: The inheritance relationship between the classes; The implementation relationship between the class and the interface; a containment relationship between the class and the attribute; The containment relationship between the class and the class method; The containment relationship between the class and the inner class; The configuration relationship between the class and the configuration file; The function input parameter relationship between the class and the class method; The function output parameter relationship between the class and the class method.
7. The method according to any one of claims 1 to 6, in, The determining of the first code element at the position to be completed in the first code file to be completed includes: Obtaining a syntax tree corresponding to the first code file; The syntax tree is traversed to obtain a first code element where the position to be completed is located in the first code file.
8. The method according to any one of claims 1 to 6, in, Before generating the target code corresponding to the position to be completed by the large model based on the signature of the second code element, the method further includes: Obtaining the context code of the position to be completed in the first code file; The step of generating the target code corresponding to the position to be completed by using the large model based on the signature of the second code element includes: The signature of the second code element and the context code are input into the large model to obtain the target code output by the large model.
9. The method according to claim 8, in, The second code element includes at least two code elements in the same code file; The step of generating the target code corresponding to the position to be completed by using the large model based on the signature of the second code element includes: Merging the signatures of the at least two code elements to obtain a merged signature; The fusion signature and the context code are input into the large model to obtain the target code output by the large model.
10. The method according to any one of claims 1 to 6, in, The determining, from the development project to which the first code file belongs, a second code file having a dependency relationship with the first code file comprises: Obtaining candidate code files under the development project to which the first code file belongs; The candidate code files are parsed to obtain the second code files among the candidate code files that have a dependency relationship with the first code file.
11. A code completion device based on a large model, include: A first determining module, used to determine a first code element at a position to be completed in a first code file to be completed; A second determining module, configured to determine, from the development project to which the first code file belongs, a second code file having a dependency relationship with the first code file; a third determining module, configured to determine, based on the first code element, a second code element whose correlation with the first code element satisfies a set condition, wherein the second code element belongs to at least one of the first code file and the second code file; A generation module is used to generate a target code corresponding to the position to be completed through a large model based on the signature of the second code element.
12. The device according to claim 11, in, The third determining module comprises: A construction submodule, configured to construct a code dependency graph based on the first code file and the second code file, wherein the code dependency graph includes at least two nodes and directed edges between the nodes, the nodes represent code elements, and the directed edges represent dependency relationships between code elements corresponding to the connected nodes; A query submodule is used to query the code dependency graph according to the first code element to obtain the second code element.
13. The device according to claim 12, in, The query submodule includes: A first determining unit, configured to determine a first node in the code dependency graph where the first code element is located; A query unit, configured to query the code dependency graph based on the first node to obtain candidate nodes, wherein the candidate nodes include nodes directly or indirectly pointed to by the first node through the directed edge; A second determining unit, configured to determine a correlation between a candidate code element represented by the candidate node and the first code element; A third determining unit is configured to determine the second code element from the candidate code elements based on the correlation.
14. The device according to claim 13, in, The dependency relationship represented by the directed edge in the code dependency graph includes at least one of the following: an inheritance relationship and an implementation relationship; the candidate node includes at least one of the second node and the third node; The second node is located on the target path where the first node is located, and is directly or indirectly pointed to by the first node through the directed edge, and the dependency relationship represented by the directed edge on the target path is the inheritance relationship or the implementation relationship; The third node is a node directly pointed to by the first node or the second node through the directed edge, and the dependency relationship represented by the directed edge between the first node or the second node and the third node is other dependency relationship except the inheritance relationship and the implementation relationship.
15. The device according to claim 13, in, The second determining unit includes: A determination subunit is used to determine the correlation between the candidate code element and the first code element based on at least one of the following: The similarity between the package name where the candidate code element is located and the package name where the first code file is located; The distance between a first character position of the candidate code element in the first code file and a second character position of the position to be completed in the first code file; Wherein, when the candidate code element is not in the first code file, the first character position is determined based on the character position of the first code element in the first code file.
16. The device according to claim 12, in, The code elements include at least one of the following: class, interface, property, class method, inner class, configuration file; the dependency relationship between the code elements includes at least one of the following: The inheritance relationship between the classes; The implementation relationship between the class and the interface; a containment relationship between the class and the attribute; The containment relationship between the class and the class method; The containment relationship between the class and the inner class; The configuration relationship between the class and the configuration file; The function input parameter relationship between the class and the class method; The function output parameter relationship between the class and the class method.
17. The device according to any one of claims 11 to 16, in, The first determining module includes: A first acquisition submodule, used to acquire a syntax tree corresponding to the first code file; The second acquisition submodule is used to traverse the syntax tree to obtain the first code element where the position to be completed is located in the first code file.
18. The device according to any one of claims 11 to 16, in, The device also includes: An acquisition module, used for acquiring the context code of the position to be completed in the first code file; Accordingly, the generating module includes: The first generating submodule is used to input the signature of the second code element and the context code into the large model to obtain the target code output by the large model.
19. The device according to claim 18, in, The second code element includes at least two code elements in the same code file; The generating module comprises: A second generating submodule is used to merge the signatures of the at least two code elements to obtain a merged signature; The third generation submodule is used to input the fusion signature and the context code into the large model to obtain the target code output by the large model.
20. The device according to any one of claims 11 to 16, in, The second determining module includes: A third acquisition submodule is used to acquire a candidate code file under the development project to which the first code file belongs; The analysis submodule is used to perform syntax analysis on the candidate code files to obtain the second code files in the candidate code files that have a dependency relationship with the first code file.
21. An electronic device, include: 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 perform the method according to any one of claims 1 to 10.
22. A non-transitory computer-readable storage medium storing computer instructions, in, The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 10.
23. A computer program product, comprising a computer program, which, when executed by a processor, implements the method of any one of claims 1 to 10.
Citation Information
Cited By
Code generation model training method, device and equipment
CN120873595A
