Code generation method and system for multi-source information fusion based on large language model

Through the multi-source information fusion code generation method based on large language model, the problem of lack of semantic understanding and global perspective of code completion in the existing technology is solved, and more accurate and global code completion suggestions are achieved, which improves development efficiency and code quality.

CN119512524APending Publication Date: 2025-02-25PEKING UNIV

Patent Information

Application Number
CN202411185891.2
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-27
Publication Date
2025-02-25

AI Technical Summary

Technical Problem

The existing code completion technology lacks an in-depth understanding of the semantics of the program, cannot fully utilize the code information of other related files in the project, and fails to effectively utilize external knowledge sources, resulting in the generated completion suggestions that are not accurate and global enough.

Method used

A multi-source information fusion code generation method based on a large language model is adopted. By obtaining the application programming interface information and code relationship information of the code to be completed, a pending context code snippet is obtained from the target code base matching, and a target completion code snippet is generated through the code evaluation model. Finally, a code completion prompt word is generated based on the code input cursor position.

Benefits of technology

It improves the accuracy and globality of code completion, can better understand the structure and context of the code, provide completion suggestions that are more in line with the needs of specific tasks, and significantly improves program development efficiency and code quality.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119512524A_ABST
    Figure CN119512524A_ABST
Patent Text Reader

Abstract

The invention provides a multi-source information fusion code generation method and a multi-source information fusion code generation system based on a large language model. Acquiring application programming interface information and code relation information corresponding to the to-be-complemented code from code files in a first code file path and a plurality of second code file paths; according to the application programming interface information and the code relation information, matching from a target code library to obtain an undetermined context code snippet; inputting the to-be-determined context code snippets into a code evaluation model to obtain target complemented code snippets output by the code evaluation model; and according to code input cursor position information corresponding to the to-be-completed code, obtaining a preorder code and a suffix code of the to-be-completed code, and according to the preorder code, the suffix code and the target completion code snippet, generating a code completion cue word. According to the method, a more accurate code completion result is obtained.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a code generation method and system based on multi-source information fusion of a large language model. Background Art

[0002] With the rapid development of artificial intelligence technology, code completion has become an indispensable auxiliary tool for programmers in their daily development.

[0003] Existing traditional code completion technologies still face a series of challenges. First, they lack an in-depth understanding of the semantics of program contexts and often only focus on the grammatical structure of the current input, while ignoring the role and significance of the code in the entire project, which results in the generated completion suggestions being inaccurate. Secondly, these technologies usually rely on pre-defined completion rules and templates, which limits their flexibility and extensibility, and they are unable to cope with new programming paradigms, frameworks, or libraries. In addition, existing code completion technologies are often limited to the contextual information of the current file and cannot fully utilize the code information of other related files in the project, resulting in the generated completion suggestions lacking a global perspective. Finally, these technologies do not make good use of external knowledge sources, such as documents, web pages, etc., to provide completion suggestions that are more in line with specific task requirements.

[0004] Therefore, there is an urgent need for a code generation method and system based on multi-source information fusion of a large language model to solve the above problems. Summary of the invention

[0005] In view of the problems existing in the prior art, the present invention provides a code generation method and system based on multi-source information fusion of a large language model.

[0006] The present invention provides a code generation method based on multi-source information fusion of a large language model, comprising: According to the code structure content of the code to be completed, obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths, wherein the first code file path is a file path corresponding to the code file where the code to be completed is located, and the second code file path is a file path corresponding to a historical code file before writing the code to be completed; According to the application programming interface information and the code relationship information, matching and obtaining a pending context code fragment from a target code library; Inputting the pending context code snippet into a code evaluation model to obtain a target completion code snippet output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; According to the code input cursor position information corresponding to the code to be completed, the prefix code and the suffix code of the code to be completed are obtained, and according to the prefix code, the suffix code and the target completion code fragment, a code completion prompt word is generated.

[0007] According to a code generation method based on multi-source information fusion of a large language model provided by the present invention, the method of obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths according to the code structure content of the code to be completed includes: According to the code structure content, obtain code call information, code block information and file name information corresponding to the code to be completed; Based on the abstract syntax tree, the code file in the first code file path is analyzed according to the code calling information to obtain the application programming interface information corresponding to the target application programming interface type required by the code to be completed; Based on the abstract syntax tree, according to the code block information and the file name information, the code relationship information between the code snippets in the code files corresponding to the first code file path and the multiple second code file paths and the code to be completed is obtained.

[0008] According to a code generation method based on multi-source information fusion of a large language model provided by the present invention, based on the abstract syntax tree, according to the code block information and the file name information, obtaining the code relationship information between the code snippets in the code files corresponding to the first code file path and the multiple second code file paths and the code to be completed, including: Extracting code element information from the code block information based on the abstract syntax tree, wherein the code element information includes at least key variable information, function call information, and class instantiation information; Constructing code module name path name information according to the code module name corresponding to the code to be completed; Based on a string similarity algorithm, according to the file name information corresponding to the code to be completed, similar file name information is obtained from the first code file path and the plurality of second code file paths; Based on the abstract syntax tree, and according to the inheritance chain information corresponding to the code to be completed, obtaining the parent class code information; The code relationship information is constructed based on the code element information, the code module name path name information, the similar file name information and the parent class code information.

[0009] According to a code generation method based on multi-source information fusion of a large language model provided by the present invention, the target code library includes a local code library and a third-party code library; The step of matching the pending context code snippet from the target code library according to the application programming interface information and the code relationship information includes: According to the code relationship information, a first relationship code fragment is matched from the code files corresponding to each of the second code file paths, wherein the first relationship code fragment is a code fragment in the second code file path that has an association relationship with the code to be completed; Based on the search enhancement generation, according to the code relationship information and the application programming interface information, a second relationship code fragment is obtained by matching from the local code library and the third-party code library; The pending context code fragment is acquired according to the first relationship code fragment and the second relationship code fragment.

[0010] According to a code generation method based on multi-source information fusion of a large language model provided by the present invention, the method further includes: If the second relationship code snippet is obtained by matching the third-party code library, constructing a document summary content corresponding to the second relationship code snippet based on a natural language processing technology and a code description document corresponding to the third-party code library; The acquiring the pending context code fragment according to the first relationship code fragment and the second relationship code fragment includes: The pending context code fragment is obtained according to the first relationship code fragment, the second relationship code fragment and the document summary content.

[0011] According to a code generation method based on multi-source information fusion of a large language model provided by the present invention, the code evaluation model is trained by the following steps: Annotating the code context content in the sample code to obtain the sample code annotated with the code context information; Acquire a positive sample completion code and a negative sample completion code, and construct the positive sample completion code and the negative sample completion code with the sample code annotated with code context information into a sample pair to obtain a sample training set; The neural network model is trained by using the sample training set to obtain the code evaluation model, wherein during the training process of the neural network model, the loss function is constructed based on conditional cross mutual information.

[0012] The present invention also provides a code generation system based on multi-source information fusion of a large language model, comprising: A code parsing module, configured to obtain application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and a plurality of second code file paths according to the code structure content of the code to be completed, wherein the first code file path is a file path corresponding to the code file where the code to be completed is located, and the second code file path is a file path corresponding to a historical code file before writing the code to be completed; A code snippet retrieval module, used to match and obtain a pending context code snippet from a target code library according to the application programming interface information and the code relationship information; A code evaluation module, used for inputting the pending context code snippet into a code evaluation model to obtain a target completion code snippet output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; The completion code generation module is used to obtain the prefix code and suffix code of the code to be completed according to the code input cursor position information corresponding to the code to be completed, and generate a code completion prompt word according to the prefix code, the suffix code and the target completion code fragment.

[0013] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the program, a code generation method based on multi-source information fusion of a large language model as described above is implemented.

[0014] The present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements any of the above-described code generation methods for multi-source information fusion based on a large language model.

[0015] The present invention also provides a computer program product, including a computer program, which, when executed by a processor, implements any of the above-mentioned code generation methods for multi-source information fusion based on a large language model.

[0016] The code generation method and system based on multi-source information fusion of a large language model provided by the present invention obtains application programming interface information and code relationship information corresponding to the code to be completed in different code files through code structure content, and then matches the pending context code fragment from the target code library according to the application programming interface information and code relationship information, and then outputs the target completion code fragment by inputting the pending context code fragment into the code evaluation model, and finally constructs the obtained pre-order code, suffix code and target completion code fragment of the code to be completed into code completion prompt words according to the code input cursor position information corresponding to the code to be completed, so as to obtain a more accurate code completion result. BRIEF DESCRIPTION OF THE DRAWINGS

[0017] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0018] Figure 1 A schematic diagram of the flow of a code generation method based on multi-source information fusion of a large language model provided by the present invention; Figure 2 An overall schematic diagram of the code generation process based on multi-source information fusion based on a large language model provided by the present invention; Figure 3 A schematic diagram of the structure of a code generation system based on multi-source information fusion based on a large language model provided by the present invention; Figure 4 This is a schematic structural diagram of an electronic device provided by the present invention. DETAILED DESCRIPTION

[0019] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0020] Existing code completion technology is mainly based on static syntax analysis and predefined completion rules. Although it improves programming efficiency to a certain extent, it still has many limitations in practical applications.

[0021] First, existing code completion technologies lack a deep understanding of the semantics of program contexts and often only focus on the grammatical structure of the current input, while ignoring the semantic role of the code snippet in the entire program. This results in the generated code prompts being inaccurate or even far from the programmer's true intentions.

[0022] Secondly, most existing code completion methods rely on predefined completion rules and templates. These rules are usually designed for specific programming languages ​​​​and common coding patterns, lacking flexibility and extensibility. When faced with new programming paradigms, frameworks or libraries, predefined rules are difficult to adapt.

[0023] Furthermore, existing code completion technologies are often limited to the contextual information of the current file. They cannot fully utilize the code information of other related files in the project, resulting in the lack of a global perspective in the generated prompts. For some cross-file and cross-module code references and call relationships, the local information of a single file is often not enough to generate accurate completion suggestions. In addition, existing technologies do not make good use of external knowledge sources. During the programming process, it is often necessary to consult documents, search the web, etc. to obtain the required Application Programming Interface (API) usage and best practices information. However, existing code completion tools rarely actively combine this external information to provide prompts that are more in line with specific task requirements.

[0024] In summary, the existing code completion technology still has deficiencies in understanding semantics, adapting to changes, integrating global information and external knowledge, etc., and the intelligent level of code completion needs to be further improved. The code generation method based on multi-source information fusion of a large language model provided by the present invention is suitable for a code editor or an integrated development environment (IDE). The present invention aims to build a more efficient, accurate and flexible code completion solution by integrating program analysis, heuristic rules, information retrieval and other technologies, thereby greatly improving program development efficiency.

[0025] Figure 1 A flowchart of a code generation method based on multi-source information fusion of a large language model provided by the present invention is shown in FIG. Figure 1 As shown, the present invention provides a code generation method based on multi-source information fusion of a large language model, comprising: Step 101, according to the code structure content of the code to be completed, obtain the application programming interface information and code relationship information corresponding to the code to be completed from the code files in the first code file path and multiple second code file paths, wherein the first code file path is the file path corresponding to the code file where the code to be completed is located, and the second code file path is the file path corresponding to the historical code file before writing the code to be completed.

[0026] In the present invention, the code to be completed is a code snippet that the user is writing but has not yet completed. The code generation method based on multi-source information fusion based on a large language model provided by the present invention will provide automatic completion suggestions based on the entered code when the user enters the code.

[0027] The first code file path refers to the file path of the code file where the code to be completed is located. The second code file path is the file path corresponding to the historical code files written previously. These files may contain API usage, function definitions, and class definitions related to the current code to be completed. By analyzing these historical code files, you can better understand the user's programming habits, commonly used libraries and APIs, and the code structure in the project.

[0028] Furthermore, during the code completion process, obtaining API information can determine the APIs that may be used by the code currently being edited, and provide completion suggestions based on this information. Code relationship information refers to the dependency relationship between code files, the calling relationship between functions, the reference relationship between variables, etc. By analyzing these relationships, you can better understand the structure and context of the code, thereby providing more accurate and useful completion suggestions.

[0029] In one embodiment, when a user is writing code and triggers the code completion function, the first code file (i.e., the code file currently being edited) is read first, and at the same time, the second code file written previously (i.e., the historical code file) is analyzed to obtain API information and code relationship information, which will be used for subsequent related fragment retrieval.

[0030] Step 102: According to the application programming interface information and the code relationship information, a pending context code fragment is matched from a target code library.

[0031] In the present invention, API information includes: API return value type and description, API calling method and context (for example, whether it needs to be called in a specific thread or context), etc. Code relationship information describes the dependencies, references and associations between different code snippets in the code library, including but not limited to: class inheritance, calling, references between variables and constants, and namespace or module dependencies.

[0032] Furthermore, the target code library is a collection of a large number of source codes, which includes third-party code libraries in addition to the code library of the entire project. These codes can be used to write in various programming languages ​​and contain various functions and features. In the present invention, the API information and code relationship information are first parsed to understand the specific meanings and relationships they express, which involves parsing API documents, code comments, metadata or other related resources. Then, the parsed information is used for querying and searching. Specifically, code snippets that match the given API information and code relationship information can be screened out from the search results through graph traversal algorithms, pattern matching algorithms or other search techniques. These code snippets can be used as input for subsequent code completion generation and suggestions for code completion.

[0033] Step 103, input the pending context code snippet into a code evaluation model to obtain a target completion code snippet output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes.

[0034] In the present invention, the next code is predicted or suggested based on a given code context (e.g., a partial implementation of a function, a method of a class, etc.). In the programming process, this function can help developers write code faster, reduce errors, and improve efficiency.

[0035] In the present invention, the code evaluation model is a trained machine learning or deep learning model, which predicts or generates appropriate completion code snippets based on a given code context. During the training process, the model continuously adjusts its internal parameters to minimize the difference between the predicted completion code and the actual completion code. When a code snippet with a pending context is input, the trained code evaluation model generates one or more possible completion code snippets based on the context.

[0036] Step 104, obtaining the prefix code and suffix code of the code to be completed according to the code input cursor position information corresponding to the code to be completed, and generating a code completion prompt word according to the prefix code, the suffix code and the target completion code fragment.

[0037] In the present invention, an integrated development environment is used to write code. In these tools, the position of the cursor (or insertion point) is a key information because it indicates the position of the code that the user is currently viewing or editing. In the scenario of code completion, the position information of the cursor is used to determine the specific code part that the user needs to complete.

[0038] Furthermore, when the cursor position information is obtained, the context of the code snippet that the user is editing can be determined, where the prefix code refers to the code before the cursor position, which contains contextual information for inferring the user's intention; the suffix code refers to the code after the cursor position, which contains some clues about what code should be entered next. By analyzing the prefix code and the suffix code, the type or structure of the code that the user may want to enter can be inferred. For example, if the cursor is in the parameter list of a function call, the prefix code may contain the function name and the parameters that have been entered, while the suffix code may contain the function's closing bracket or other subsequent code.

[0039] After obtaining the preamble code, postfix code, and target completion code snippet, code completion prompt words are generated. Specifically, the preamble code and postfix code are parsed to understand their structure and meaning, which may include identifying variable names, function names, grammatical structures, etc. Then, the information of the preamble code, postfix code, and target completion code snippet is fused together. Finally, code completion prompt words are generated based on the fused information (which can be achieved through a large language model). These prompt words can accurately reflect the code that the user may enter next, and the generated code completion prompt words can be displayed to the user in some form in the user interface of the programming environment, such as a drop-down list, a pop-up window, or an inline prompt.

[0040] The code generation method based on multi-source information fusion of a large language model provided by the present invention obtains application programming interface information and code relationship information corresponding to the code to be completed in different code files through code structure content, and then matches the pending context code fragment from the target code library according to the application programming interface information and code relationship information, and then outputs the target completion code fragment by inputting the pending context code fragment into the code evaluation model, and finally constructs the obtained pre-order code, suffix code and target completion code fragment of the code to be completed into code completion prompt words according to the code input cursor position information corresponding to the code to be completed, so as to obtain a more accurate code completion result.

[0041] On the basis of the above embodiment, the step of obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in the first code file path and multiple second code file paths according to the code structure content of the code to be completed includes: According to the code structure content, obtain code call information, code block information and file name information corresponding to the code to be completed; Based on the abstract syntax tree, the code file in the first code file path is analyzed according to the code calling information to obtain the application programming interface information corresponding to the target application programming interface type required by the code to be completed; Based on the abstract syntax tree, according to the code block information and the file name information, the code relationship information between the code snippets in the code files corresponding to the first code file path and the multiple second code file paths and the code to be completed is obtained.

[0042] In the present invention, it is necessary to accurately identify and analyze the APIs that may be used in the subsequent code snippets currently being written, and understand their role in the current code context. The specific process is as follows: First, the Abstract Syntax Tree (AST) is used to parse the code file currently being edited to obtain the code structure content, including variable declarations, function calls, class inheritance relationships, etc. In the present invention, the logical structure of the code can be obtained through AST, not just the text content, which is crucial for accurately identifying and recommending API calls.

[0043] Furthermore, by identifying the cursor position of the code currently being edited, the code environment of the position is analyzed, such as the surrounding function, the current class or the code block. In the present invention, by analyzing the cursor context, it is detected whether the current code (i.e., the code to be completed) has an API call requirement. For example, when writing a method call of an object, the API that may be called is inferred. In one embodiment, it is also necessary to detect the dependency relationship to verify the library that the current code file or code project depends on, and ensure that the recommended API is available in the library that the code project depends on.

[0044] Furthermore, through AST, code snippets related to the code to be completed are extracted, and the relationship between these code snippets and the code to be completed is analyzed to generate code relationship information, which includes function call relationship, variable reference relationship, class inheritance relationship, etc.

[0045] On the basis of the above embodiment, the acquiring, based on the abstract syntax tree and according to the code block information and the file name information, the code relationship information between the code snippets in the code files corresponding to the first code file path and the multiple second code file paths and the code to be completed includes: Extracting code element information from the code block information based on the abstract syntax tree, wherein the code element information includes at least key variable information, function call information, and class instantiation information; Constructing code module name path name information according to the code module name corresponding to the code to be completed; Based on a string similarity algorithm, according to the file name information corresponding to the code to be completed, similar file name information is obtained from the first code file path and the plurality of second code file paths; Based on the abstract syntax tree, and according to the inheritance chain information corresponding to the code to be completed, obtaining the parent class code information; The code relationship information is constructed based on the code element information, the code module name path name information, the similar file name information and the parent class code information.

[0046] In the present invention, in the code relationship analysis link, AST and other analysis tools are used to identify and utilize the intrinsic relationships in the code to improve the accuracy and relevance of code completion. Specifically, according to the reference code of the code to be completed, the usage of the current code segment in the project is searched to provide context-related completion suggestions. The present invention uses AST to analyze the currently edited code block and extracts code element information elements such as key variables, function calls, and class instantiations, thereby realizing the search for other reference points of these elements in the entire code base, including cross-file and cross-module references. Based on these code element information, the present invention analyzes the reference context, such as call parameters, overloaded versions, and wrapper functions, so as to provide the most relevant code suggestions for the follow-up.

[0047] For codes with the same path in the code module, the present invention first determines the code relationship under the same code module name or package path based on the code to be completed, so as to find APIs or code patterns that may be used in common in similar contexts. In the present invention, based on AST analysis, the code structure is mapped by the namespace or module path of the file, and additional semantic analysis is performed on the code files under the same code module name or package path, and common function calls and data type usage are marked. By analyzing the code segments with similar functions, references or suggestions can be provided in similar development scenarios.

[0048] Furthermore, the code relationship information is extracted according to the code file name. The present invention uses the file name information of the code to be completed to mine code snippets with similar or complementary functions through similarity. In one embodiment, a string similarity algorithm (such as Levenshtein distance) of file and directory names is used to find files with similar names. The present invention analyzes the code and API usage patterns of these files and compares the similarity of their AST structures, thereby recommending code modules and functions that may be related or complementary to each other based on the obtained similarity structure.

[0049] Furthermore, in an object-oriented programming environment, the inheritance chain is detected and used to provide correct completion methods and attribute completion. The present invention parses the inheritance structure of the current class and its parent class through AST, and then extracts the public methods, attributes and constructors defined by the parent class. At the same time, method calls and attribute access suggestions from the parent class are provided in the completion prompt, and overriding and polymorphism are considered.

[0050] The present invention performs the above-mentioned heuristic code snippet retrieval on the code to be completed, so that concurrent developers can obtain more accurate and efficient code completion suggestions during the code writing process. These suggestions are not only based on grammatical correctness, but also take into account the contextual relevance and potential logical structure of the code, which can significantly improve development efficiency and code quality.

[0051] Based on the above embodiment, the target code library includes a local code library and a third-party code library; The step of matching the pending context code snippet from the target code library according to the application programming interface information and the code relationship information includes: According to the code relationship information, a first relationship code fragment is matched from the code files corresponding to each of the second code file paths, wherein the first relationship code fragment is a code fragment in the second code file path that has an association relationship with the code to be completed; Based on the search enhancement generation, according to the code relationship information and the application programming interface information, a second relationship code fragment is obtained by matching from the local code library and the third-party code library; The pending context code fragment is acquired according to the first relationship code fragment and the second relationship code fragment.

[0052] In the present invention, first, a code snippet that is associated with the code to be completed, that is, a first relationship code snippet, is obtained from the historical code file. Techniques such as static code analysis, syntax tree analysis, and code dependency graph can be used to identify code snippets that are directly related to the code to be completed in terms of logic, structure, or function.

[0053] The integrated development environment has two code bases: one is the local code base, which contains the user-side code or the code that the project depends on; the other is the third-party code base, which contains various open source libraries or frameworks. The present invention retrieves the code snippets related to the code to be completed, that is, the second relationship code snippets, from these two code bases through code relationship information and API information. The code relationship information includes the calling relationship, dependency relationship and inheritance relationship between the codes, so that this information can be used to identify which code snippets may be related to the code to be completed. If the code to be completed involves an API that needs to be called, the API information can be used to retrieve the code snippets related to the API.

[0054] Furthermore, through the Retriever-Augmented Generation (RAG) technology, candidate code snippets related to the code to be completed are obtained; in the present invention, a deep learning model can be used to sort or fine-tune these candidate snippets to generate more accurate code recommendations. Finally, the first relationship code snippet and the second relationship code snippet are obtained by combining the previous steps, and various related algorithms and strategies are used to ensure that the generated context code snippet is logically, grammatically, and functionally consistent with the code to be completed.

[0055] In the present invention, the target code library is constructed based on the local code library and the third-party code library. In one embodiment, the target code library is constructed in the following manner. First, the specified code library (local) path is recursively scanned, including identifying all code files (for example, java and Py files) and listing their file structures. During the file scanning process, the dependency relationships are also understood, for example, which files depend on which libraries, which files are often modified together, etc.

[0056] Then, a static analysis tool is used to read and parse the code file to extract key information, including but not limited to function definitions, class definitions, important variables and their types. The present invention analyzes the signatures of each function and method, identifies the input and return types, and helps to implement more accurate function call suggestions in the future.

[0057] Furthermore, the configuration files and import statements in the code base are analyzed to determine the third-party libraries and their versions used by the project. By determining the dependency structure of these code bases, the availability and relevance of external functions can be determined.

[0058] Further, a code index is created. A comprehensive code index needs to be established for quick search and reference, including indexes of function names, class names, and key APIs. In the present invention, the index can support complex queries, such as finding all code snippets that call a specific function.

[0059] Furthermore, based on the usage relationship of classes and functions in the code, a model is constructed to map the connections between these elements. For example, if a class A is often used in the same file as another class B, then the two classes can be considered similar in the model. In the present invention, these relationships can be stored and processed by a graph database or similar technology, so that it can effectively support complex relationship queries and graph algorithms.

[0060] Furthermore, metadata is extracted and stored, and metadata annotation and storage are performed on various extracted code elements (such as functions, classes, variables) and their relationships. In the present invention, metadata includes but is not limited to code location, code length, modification history, and document comments, which are key clues to understanding the intent and function of the code.

[0061] Through the above steps, the present invention can comprehensively and deeply determine the structure and characteristics of the entire code base, providing a solid and efficient foundation for the subsequent generation of code completion prompts. In practical applications, the above steps can be regularly updated to reflect the latest status of the code base to ensure the accuracy and relevance of the code completion system.

[0062] Based on the above embodiment, the method further includes: If the second relationship code snippet is obtained by matching the third-party code library, constructing a document summary content corresponding to the second relationship code snippet based on a natural language processing technology and a code description document corresponding to the third-party code library; The acquiring the pending context code fragment according to the first relationship code fragment and the second relationship code fragment includes: The pending context code fragment is obtained according to the first relationship code fragment, the second relationship code fragment and the document summary content.

[0063] In the present invention, the API document summary (i.e., document summary content) is the key information extracted and organized from the API document to provide a reference for developers when completing the code. Specifically, when the acquired relational code snippet comes from a third-party code library, the official documents of the third-party library actually used in the project are first analyzed to extract the API's functional description, usage examples, parameter list, and return value description. Then, natural language processing (NLP) technology is used to extract key information from the API document and store this information in a structured manner for efficient retrieval.

[0064] Furthermore, through text summarization technology, such as automatic summarization algorithms or deep learning models (such as BERT-based summarizers), a concise and clear description of the API is generated. At the same time, each API call proposal is linked to the corresponding document summary, allowing the user to view it directly in the completion prompt. By providing a direct document link for the API, it is convenient for developers to learn in depth and solve problems.

[0065] By executing the above steps, the present invention can provide accurate and effective code completion support, help developers improve programming efficiency and reduce errors and misunderstandings in coding, and ensure the high responsiveness and relevance of the code completion system, so that developers can obtain more accurate code writing support in a shorter time.

[0066] Based on the above embodiment, the code evaluation model is trained by the following steps: Annotating the code context content in the sample code to obtain the sample code annotated with the code context information; Acquire a positive sample completion code and a negative sample completion code, and construct the positive sample completion code and the negative sample completion code with the sample code annotated with code context information into a sample pair to obtain a sample training set; The neural network model is trained by using the sample training set to obtain the code evaluation model, wherein during the training process of the neural network model, the loss function is constructed based on conditional cross mutual information.

[0067] In the present invention, the code evaluation model is used to quantify and evaluate the influence and effectiveness of different code context fragments in code completion. In the present invention, the training and application of the code evaluation model are mainly based on deep learning techniques, such as Transformer Encoder, Conditional Cross-Mutual Information (CCMI) and contrastive learning.

[0068] Specifically, in the present invention, the code evaluation model adopts the Transformer Encoder architecture, which can effectively process sequence data (such as code), capture long-distance dependencies, and extract deep features. For feature extraction, the present invention uses a multi-layer Transformer encoder, each layer of which extracts and encodes complex contextual features from code snippets through a self-attention mechanism and a feedforward neural network.

[0069] Furthermore, we collect source code in multiple programming languages ​​from large code bases, including various API usage scenarios and code patterns. Then, we annotate the key contexts in the code, such as function calls, class definitions, and API usage, which will serve as self-labels for model learning.

[0070] In this paper, conditional cross mutual information (CCMI) is used as a loss function to evaluate the correlation between a code segment context and a specific code completion. CCMI evaluates the magnitude of the correlation by quantifying the information mutual correlation between the code completion and the predicted self-label under the condition of a given source code context.

[0071] Specifically, CCMI is a measure that quantifies the interdependence between two random variables given a third variable. In the present invention, the three variables are: X: code completion; Y: prediction target (correct code completion item); Z: code context. CCMI is used to measure the information correlation between the predicted code completion X and the correct code completion Y under the condition of known code context Z. The formula of CCMI is expressed as: ; Wherein, H() represents conditional entropy, which is used to measure the uncertainty of the self-standard variable under the condition of known conditional variables. In the present invention, in order to calculate CCMI in the model, the following conditions need to be estimated first: H(X|Z): the uncertainty of code completion X given the known context Z; H(Y|Z): the uncertainty of the correct code completion Y given the known context Z; H(X, Y|Z): Consider the uncertainty of both X and Y given the known context Z.

[0072] In the present invention, these conditions can be counted through samples of the training set and estimated using methods such as maximum likelihood estimation.

[0073] Furthermore, the distinguishing ability of the model is enhanced through contrastive learning. In the present invention, the code evaluation model needs to learn to distinguish positive samples (i.e., real and effective code completions) from negative samples (i.e., random or irrelevant code completions). In contrastive learning, the model aims to distinguish the correct completion X that is most relevant to the current code context Z. pos and other irrelevant (or incorrect) completions X neg In the present invention, this can be achieved by minimizing the contrast loss function, for example, using the cross entropy loss, the formula is: ; Wherein, sim() represents a similarity function, such as dot product or cosine similarity, which is used to measure the similarity between the code completion and the target. In this way, the model is trained and optimized to achieve the best code completion selection for a given context, thereby improving the accuracy and efficiency of the code completion tool.

[0074] Figure 2 The overall schematic diagram of the code generation process based on multi-source information fusion of a large language model provided by the present invention can be referred to Figure 2As shown, in the present invention, based on the fusion of multi-source information, the effectiveness and adaptability of the code completion function are optimized and enhanced. Specifically, the input and output are first defined. The input of the present invention includes the code library path, the historical code file path before the code to be completed, the file path of the current IDE (i.e., the file path of the code to be completed), and the cursor position of the code to be completed; the output is the optimized code completion prompt word.

[0075] In the present invention, to analyze and construct the target code library required for the code completion prompt, it is necessary to conduct an in-depth analysis of all code files in the current code library, including the analysis of all function definitions and third-party libraries used. Then, the code files and function definitions in the code library are formatted and stored for subsequent rapid retrieval and analysis. At the same time, the functions and documents of the third-party library are analyzed to ensure that the implementation and document description of the relevant functions can be found according to the required completion code.

[0076] Furthermore, program analysis techniques, such as abstract syntax tree analysis (AST), are used to identify APIs that may be used by the current completion code, including functions in third-party libraries and local code libraries. In the present invention, a corresponding document summary is generated for the identified API and used as part of the completion prompt.

[0077] Furthermore, AST analysis is used to identify the following code relationships: referenced code of the current completion code, code in the same path within the code module, code with similar file names, and code related to the parent class. Then, heuristic code snippet retrieval technology (such as the Jacquard similarity coefficient) is used to find code snippets similar to the code to be completed in these code relationships.

[0078] Furthermore, through a wide-area code snippet retrieval approach, for example, using the retrieval-augmented generation (RAG) method, highly relevant code snippets are retrieved in the entire code base and third-party library codes.

[0079] Furthermore, the context information of the API document context, heuristic code file context and RAG search results obtained in the above steps is merged, and the contribution of various context code segments in the predicted completion code is evaluated through the code evaluation model, and optimization and adjustment are made accordingly. Finally, according to the input file path and cursor position, the prefix code and suffix code of the position are extracted, and the final code completion prompt word is generated by combining the evaluated high contribution context using the large language model (LLM).

[0080] The following is a description of the code generation system based on multi-source information fusion of a large language model provided by the present invention. The code generation system based on multi-source information fusion of a large language model described below and the code generation method based on multi-source information fusion of a large language model described above can be referenced to each other.

[0081] Figure 3 The schematic diagram of the structure of the code generation system based on multi-source information fusion of a large language model provided by the present invention is as follows: Figure 3 As shown, the present invention provides a code generation system based on multi-source information fusion of a large language model, including a code parsing module 301, a code snippet retrieval module 302, a code evaluation module 303 and a completion code generation module 304, wherein the code parsing module 301 is used to obtain application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths according to the code structure content of the code to be completed, wherein the first code file path is the file path corresponding to the code file where the code to be completed is located, and the second code file path is the file path corresponding to the historical code file before writing the code to be completed; the code snippet retrieval module 302 The code evaluation module 303 is used to match the pending context code fragment from the target code library according to the application programming interface information and the code relationship information, and obtain the target completion code fragment output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; the completion code generation module 304 is used to obtain the prefix code and suffix code of the code to be completed according to the code input cursor position information corresponding to the code to be completed, and generate code completion prompt words according to the prefix code, the suffix code and the target completion code fragment.

[0082] The code generation system based on multi-source information fusion of a large language model provided by the present invention obtains application programming interface information and code relationship information corresponding to the code to be completed in different code files through code structure content, and then matches the pending context code fragment from the target code library according to the application programming interface information and code relationship information, and then outputs the target completion code fragment by inputting the pending context code fragment into the code evaluation model, and finally constructs the obtained pre-order code, suffix code and target completion code fragment of the code to be completed into code completion prompt words according to the code input cursor position information corresponding to the code to be completed, so as to obtain a more accurate code completion result.

[0083] The system provided by the present invention is used to execute the above-mentioned method embodiments. Please refer to the above-mentioned embodiments for the specific processes and detailed contents, which will not be repeated here.

[0084] Figure 4 A schematic diagram of the structure of an electronic device provided by the present invention, such as Figure 4As shown, the electronic device may include: a processor (Processor) 401, a communication interface (Communications Interface) 402, a memory (Memory) 403 and a communication bus 404, wherein the processor 401, the communication interface 402, and the memory 403 communicate with each other through the communication bus 404. The processor 401 can call the logic instructions in the memory 403 to execute the code generation method based on multi-source information fusion of the large language model, the method comprising: according to the code structure content of the code to be completed, obtaining the application programming interface information and code relationship information corresponding to the code to be completed from the code files in the first code file path and multiple second code file paths, wherein the first code file path is the file path corresponding to the code file where the code to be completed is located, and the second code file path is the file path corresponding to the historical code file before writing the code to be completed; according to the application programming interface information and the code relationship information, matching and obtaining the pending context code fragment from the target code library; inputting the pending context code fragment into the code evaluation model to obtain the target completion code fragment output by the code evaluation model, wherein the code evaluation model is trained based on the sample code annotated with the code context information and the sample completion code corresponding to the sample code; according to the code input cursor position information corresponding to the code to be completed, obtaining the preamble code and the suffix code of the code to be completed, and generating the code completion prompt word according to the preamble code, the suffix code and the target completion code fragment.

[0085] In addition, the logic instructions in the above-mentioned memory 403 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on such an understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0086] On the other hand, the present invention also provides a computer program product, which includes a computer program stored on a non-transitory computer-readable storage medium, and the computer program includes program instructions. When the program instructions are executed by a computer, the computer can execute the code generation method based on multi-source information fusion of a large language model provided by the above methods, the method comprising: according to the code structure content of the code to be completed, obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths, wherein the first code file path is the file path corresponding to the code file where the code to be completed is located, and the second code file path is the file path corresponding to the code file in the compilation Write the file path corresponding to the historical code file before the code to be completed; match the pending context code fragment from the target code library according to the application programming interface information and the code relationship information; input the pending context code fragment into the code evaluation model to obtain the target completion code fragment output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; obtain the preamble code and suffix code of the code to be completed according to the code input cursor position information corresponding to the code to be completed, and generate code completion prompt words according to the preamble code, the suffix code and the target completion code fragment.

[0087] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the code generation method based on multi-source information fusion of a large language model provided in the above-mentioned embodiments, the method comprising: according to the code structure content of the code to be completed, obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths, wherein the first code file path is the file path corresponding to the code file where the code to be completed is located, and the second code file path is the historical code file path before writing the code to be completed. corresponding file path; according to the application programming interface information and the code relationship information, matching the pending context code fragment from the target code library; inputting the pending context code fragment into the code evaluation model to obtain the target completion code fragment output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; according to the code input cursor position information corresponding to the code to be completed, obtaining the prefix code and suffix code of the code to be completed, and generating code completion prompt words according to the prefix code, the suffix code and the target completion code fragment.

[0088] The device embodiments described above are merely illustrative, wherein the units described as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0089] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0090] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

[0091] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A code generation method based on multi-source information fusion of a large language model, characterized in that: include: According to the code structure content of the code to be completed, obtaining application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and multiple second code file paths, wherein the first code file path is a file path corresponding to the code file where the code to be completed is located, and the second code file path is a file path corresponding to a historical code file before writing the code to be completed; According to the application programming interface information and the code relationship information, matching and obtaining a pending context code fragment from a target code library; Inputting the pending context code snippet into a code evaluation model to obtain a target completion code snippet output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; According to the code input cursor position information corresponding to the code to be completed, the prefix code and the suffix code of the code to be completed are obtained, and according to the prefix code, the suffix code and the target completion code fragment, a code completion prompt word is generated.

2. The code generation method based on multi-source information fusion of a large language model according to claim 1 is characterized in that: The step of acquiring application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and a plurality of second code file paths according to the code structure content of the code to be completed includes: According to the code structure content, obtain code call information, code block information and file name information corresponding to the code to be completed; Based on the abstract syntax tree, the code file in the first code file path is analyzed according to the code calling information to obtain the application programming interface information corresponding to the target application programming interface type required by the code to be completed; Based on the abstract syntax tree, according to the code block information and the file name information, the code relationship information between the code snippets in the code files corresponding to the first code file path and the multiple second code file paths and the code to be completed is obtained.

3. The code generation method based on multi-source information fusion of a large language model according to claim 2 is characterized in that: The acquiring, based on the abstract syntax tree and according to the code block information and the file name information, the code relationship information between the code snippets in the code files corresponding to the first code file path and the plurality of second code file paths and the code to be completed includes: Extracting code element information from the code block information based on the abstract syntax tree, wherein the code element information includes at least key variable information, function call information, and class instantiation information; Constructing code module name path name information according to the code module name corresponding to the code to be completed; Based on a string similarity algorithm, according to the file name information corresponding to the code to be completed, similar file name information is obtained from the first code file path and the plurality of second code file paths; Based on the abstract syntax tree, and according to the inheritance chain information corresponding to the code to be completed, obtaining the parent class code information; The code relationship information is constructed based on the code element information, the code module name path name information, the similar file name information and the parent class code information.

4. The code generation method based on multi-source information fusion of a large language model according to claim 3 is characterized in that: The target code library includes a local code library and a third-party code library; The step of matching the pending context code snippet from the target code library according to the application programming interface information and the code relationship information includes: According to the code relationship information, a first relationship code fragment is matched from the code files corresponding to each of the second code file paths, wherein the first relationship code fragment is a code fragment in the second code file path that has an association relationship with the code to be completed; Based on the search enhancement generation, according to the code relationship information and the application programming interface information, a second relationship code fragment is obtained by matching from the local code library and the third-party code library; The pending context code fragment is acquired according to the first relationship code fragment and the second relationship code fragment.

5. The code generation method based on multi-source information fusion of a large language model according to claim 4 is characterized in that: The method further comprises: If the second relationship code snippet is obtained by matching the third-party code library, constructing a document summary content corresponding to the second relationship code snippet based on a natural language processing technology and a code description document corresponding to the third-party code library; The acquiring the pending context code fragment according to the first relationship code fragment and the second relationship code fragment includes: The pending context code fragment is obtained according to the first relationship code fragment, the second relationship code fragment and the document summary content.

6. The code generation method based on multi-source information fusion of a large language model according to claim 1 is characterized in that: The code evaluation model is trained by the following steps: Annotating the code context content in the sample code to obtain the sample code annotated with the code context information; Acquire a positive sample completion code and a negative sample completion code, and construct the positive sample completion code and the negative sample completion code with the sample code annotated with code context information into a sample pair to obtain a sample training set; The neural network model is trained by using the sample training set to obtain the code evaluation model, wherein during the training process of the neural network model, the loss function is constructed based on conditional cross mutual information.

7. A code generation system based on multi-source information fusion based on a large language model, characterized in that: include: A code parsing module, configured to obtain application programming interface information and code relationship information corresponding to the code to be completed from code files in a first code file path and a plurality of second code file paths according to the code structure content of the code to be completed, wherein the first code file path is a file path corresponding to the code file where the code to be completed is located, and the second code file path is a file path corresponding to a historical code file before writing the code to be completed; A code snippet retrieval module, used to match and obtain a pending context code snippet from a target code library according to the application programming interface information and the code relationship information; A code evaluation module, used for inputting the pending context code snippet into a code evaluation model to obtain a target completion code snippet output by the code evaluation model, wherein the code evaluation model is trained based on sample codes annotated with code context information and sample completion codes corresponding to the sample codes; The completion code generation module is used to obtain the prefix code and suffix code of the code to be completed according to the code input cursor position information corresponding to the code to be completed, and generate a code completion prompt word according to the prefix code, the suffix code and the target completion code fragment.

8. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the code generation method based on multi-source information fusion based on a large language model as described in any one of claims 1 to 6 is implemented.

9. A non-transitory computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the code generation method based on multi-source information fusion based on a large language model as claimed in any one of claims 1 to 6 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the code generation method based on multi-source information fusion based on a large language model as claimed in any one of claims 1 to 6 is implemented.

Citation Information

Patent Citations

  • Code completion method based on abstract syntax tree augmented graph model

    CN113064586A

  • Code completion method, model and tool

    CN114327609A

  • Code completion method and device, electronic equipment and medium

    CN118227106A

  • Code completion method based on artificial intelligence

    CN118276913A

  • Method, System, and Computer Program Product for Code Generation Using LLMs

    US20240281600A1

Cited By

  • Large language model code generation system and method oriented to complex requirements

    CN119938006A

  • Task code segment-oriented large model cue word optimization method and system

    CN122152359A

  • A task-oriented code segment large model prompt word optimization method and system

    CN122152359B