Code retrieval method and device based on large language model
By using large language models and code architecture knowledge graphs in code retrieval, combined with the dependencies of code files, the problem of insufficient code retrieval accuracy in the existing technology is solved, and more accurate and rich retrieval results are achieved, which is suitable for code Q&A and code continuation scenarios.
Patent Information
- Application Number
- CN202410800398.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-20
- Publication Date
- 2024-09-13
AI Technical Summary
Existing code retrieval methods have low accuracy and are only based on the matching of query vectors, failing to fully consider the dependencies between code blocks and other code files, resulting in insufficient accuracy and richness of retrieval results.
Use a method based on a large language model to obtain the retrieval vector of the code retrieval query, and combine it with the code architecture knowledge graph to consider the dependencies between code files to obtain the target code block and its corresponding target code file, thereby improving the accuracy of the retrieval results. sex and richness.
By considering the overall architecture of the code base, the accuracy of code retrieval and the richness of retrieval results are improved, and the accuracy of code Q&A and code continuation is enhanced.
Smart Images

Figure CN118643120A
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of data processing technology, in particular to the field of artificial intelligence technology such as large language models, big data, and cloud services. A code retrieval method, device, electronic device, and readable storage medium based on a large language model are provided. Background Art
[0002] In the prior art, when searching for codes, a search is usually performed only in a vector library of corresponding code blocks according to a query input by a user, and then at least one code block obtained by the search is used as a code search result, resulting in low accuracy of the code search result. Summary of the invention
[0003] According to a first aspect of the present disclosure, a code retrieval method based on a large language model is provided, comprising: obtaining a code retrieval query query, and obtaining a retrieval vector of the code retrieval query; obtaining a first index of a target code library, wherein the first index includes multiple code blocks and multiple code block vectors; obtaining a target code block according to the retrieval vector and the first index; obtaining a second index of the target code library, wherein the second index is a code architecture knowledge graph, wherein nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files; obtaining a target code file corresponding to the target code block according to a source code file corresponding to the target code block and the second index; and obtaining a retrieval result according to the target code block and the target code file.
[0004] According to a second aspect of the present disclosure, a code retrieval device based on a large language model is provided, comprising: a first acquisition unit, configured to acquire a code retrieval query query and obtain a retrieval vector of the code retrieval query; a second acquisition unit, configured to acquire a first index of a target code library, wherein the first index includes multiple code blocks and multiple code block vectors; a first retrieval unit, configured to acquire a target code block according to the retrieval vector and the first index; a third acquisition unit, configured to acquire a second index of the target code library, wherein the second index is a code architecture knowledge graph, wherein nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files; a second retrieval unit, configured to acquire a target code file corresponding to the target code block according to a source code file corresponding to the target code block and the second index; and a processing unit, configured to acquire a retrieval result according to the target code block and the target code file.
[0005] According to a third aspect of the present disclosure, an electronic device is provided, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method as described above.
[0006] 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 cause the computer to execute the method as described above.
[0007] According to a fifth aspect of the present disclosure, a computer program product is provided, comprising a computer program, wherein the computer program implements the method as described above when executed by a processor.
[0008] 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
[0009] The accompanying drawings are used to better understand the present solution and do not constitute a limitation of the present disclosure.
[0010] Figure 1 is a schematic diagram according to a first embodiment of the present disclosure;
[0011] Figure 2 is a schematic diagram according to a second embodiment of the present disclosure;
[0012] Figure 3 is a schematic diagram according to a third embodiment of the present disclosure;
[0013] Figure 4 is a schematic diagram according to a fourth embodiment of the present disclosure;
[0014] Figure 5 is a schematic diagram according to a fifth embodiment of the present disclosure;
[0015] Figure 6 The invention is a block diagram of an electronic device for implementing the code retrieval method based on a large language model according to an embodiment of the present disclosure. DETAILED DESCRIPTION
[0016] 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, the description of well-known functions and mechanisms is omitted in the following description.
[0017] Figure 1 Schematic diagram of the first embodiment of the present disclosure. Figure 1 As shown, the code retrieval method based on the large language model of this embodiment specifically includes the following steps:
[0018] S101, obtaining a code search query query, and obtaining a search vector of the code search query;
[0019] S102, obtaining a first index of a target code library, wherein the first index includes a plurality of code blocks and a plurality of code block vectors;
[0020] S103, acquiring a target code block according to the search vector and the first index;
[0021] S104. Obtain a second index of the target code library, where the second index is a code architecture knowledge graph, where nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files;
[0022] S105, acquiring a target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index;
[0023] S106: Obtain search results according to the target code block and the target code file.
[0024] The code retrieval method based on a large language model in this embodiment can fully consider the dependency relationship between the code blocks in the target code library and other code files. After obtaining the target code block corresponding to the code retrieval query, the code architecture knowledge graph that can reflect the overall architecture of the target code library is further used to obtain the target code file corresponding to the target code block, thereby avoiding only retrieving a single code block from the target code library as the code retrieval result, and can improve the accuracy of code retrieval and the richness of the obtained code retrieval results.
[0025] It is understandable that, when executing the above steps, this embodiment can complete the acquisition of the first index, the acquisition of the target code block, the acquisition of the second index, the acquisition of the target code file, etc. respectively by calling the large language model.
[0026] The code search query obtained by executing S101 in this embodiment is a query used to search for corresponding codes from the target code library. When executing S101 in this embodiment, the initial query input by the input end may be first obtained, and then the code search query may be further obtained according to the obtained initial query.
[0027] For example, if the initial query obtained when executing S101 in this embodiment is "Help me sort out the code logic of the payment system", this embodiment can directly use the initial query as the code retrieval query, thereby retrieving the code related to the "payment system" in the target code library; if the initial query obtained when executing S101 in this embodiment is "Continue writing the code according to the input code, the input code is code A", this embodiment can use the "code A" obtained from the initial query as the code retrieval query, thereby retrieving the code related to "code A" in the target code library.
[0028] That is to say, the code retrieval method of this embodiment can be applied to two different scenarios: "code question and answer scenario" and "code continuation scenario" (i.e., code generation scenario); if applied to the "code question and answer scenario", this embodiment uses the final code retrieval result as the answer and returns it to the input end; if applied to the "code continuation scenario", this embodiment inputs the obtained code retrieval result into the large language model, thereby obtaining the code continuation result output by the large language model and returning it to the input end.
[0029] When executing S101 to obtain the search vector of the code search query, this embodiment may use an existing embedding model or embedding method to perform embedding processing on the code search query, and use the obtained embedding vector as the search vector corresponding to the code search query.
[0030] In addition, when executing S101, this embodiment may first rewrite the code search query to generate multiple rewritten queries corresponding to the code search query, and then obtain the search vector of each rewritten query respectively.
[0031] That is to say, in this embodiment, a plurality of rewritten queries similar to the code search query can be obtained by rewriting the code search query, thereby improving the accuracy of searching the target code block in the first index.
[0032] After executing S101 to obtain the search vector of the code search query, this embodiment executes S102 to obtain the first index of the target code library; wherein the first index of this embodiment includes multiple code blocks and multiple code block vectors, and different code blocks correspond to different code block vectors.
[0033] In this embodiment, the target code library may be a code library specified by the input end, or may be a code library corresponding to the current code environment in the input end.
[0034] In this embodiment, when executing S102 to obtain the first index, the first index obtained in advance according to the target code library may be obtained, or the first index obtained in real time according to the target code library may be obtained.
[0035] When this embodiment executes S102 to obtain the first index of the target code library, the following implementation method can be adopted: obtain all code files in the target code library; after performing syntax tree parsing on each code file, divide each code file into multiple code blocks according to syntax rules; perform embedding processing on the obtained multiple code blocks respectively to obtain different code block vectors corresponding to different code blocks; obtain the first index according to the multiple code blocks and the multiple code block vectors.
[0036] In this embodiment, after executing S102 to obtain the first index, S103 is executed to obtain a target code block according to the search vector and the first index; in this embodiment, the number of the target code blocks may be one or more.
[0037] When executing S103 to obtain a target code block according to the search vector and the first index, the present embodiment may adopt the following implementation method: perform similarity calculation on the search vector and multiple code block vectors in the first index; obtain at least one target code block according to the similarity calculation result, for example, the code block corresponding to the code library vector whose similarity calculation result is ranked in the top N positions may be used as the at least one target code block, where N is a positive integer greater than or equal to 1.
[0038] After executing S103 to obtain the target code block, this embodiment executes S104 to obtain a second index of the target code library; wherein the second index is a code architecture knowledge graph, and the code architecture knowledge graph includes multiple nodes and edges between the nodes.
[0039] In this embodiment, the nodes in the code architecture knowledge graph represent code files in the target code library, and the edges between the nodes represent the dependencies between the code files in the target code library.
[0040] It can be understood that the nodes in the code architecture knowledge graph of this embodiment can use file names to represent code files, or can use directory information of code files to represent code files.
[0041] That is to say, this embodiment constructs the second index in the form of a knowledge graph, so that the second index can effectively describe the code files in the target code library and the dependencies between the code files, thereby fully reflecting the code architecture of the target code library, so that this embodiment can retrieve the target code files based on the second index.
[0042] In this embodiment, when executing S104 to obtain the second index, the second index obtained in advance according to the target code library may be obtained, or the second index obtained in real time according to the target code library may be obtained.
[0043] When executing S104 to obtain the second index, this embodiment can manually construct the second index based on the relationship between directories to which different code files in the target code library belong, that is, manually determine each node in the code architecture knowledge graph and the edges between the nodes.
[0044] After executing S104 to obtain the second index, this embodiment executes S105 to obtain the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index; wherein, the target code file corresponding to one target code block may be one or more.
[0045] It is understandable that if multiple target code blocks are obtained, then in this embodiment, when executing S105, the target code file corresponding to each target code block is obtained respectively.
[0046] In this embodiment, the source code file corresponding to the target code block is the code file to which the target code block belongs in the target code library, that is, the target code block is obtained by segmenting the source code file; for example, if the code file to which code block 1 belongs in the target code library is code file 1, when executing S103, this embodiment will use code file 1 as the source code file corresponding to code block 1.
[0047] When executing S105 and acquiring the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index, this embodiment may adopt the following implementation method: according to the source code file corresponding to the target code block, determining the source node of the corresponding source code file in the second index, for example, determining the node corresponding to the file name of the source code file as the source node, or determining the node corresponding to the directory to which the source code belongs as the source node; selecting the target node having an edge with the source node from the second index; and acquiring the target code file corresponding to the target node from the target code library.
[0048] For example, if the current target code block is code block 1, if the source code file corresponding to code block 1 is code file 1, if the source node of code file 1 in the second index is node 1, if the nodes with edges with node 1 in the second index are node 3, node 4 and node 7, if the code file corresponding to node 3 is code file 3, the code file corresponding to node 4 is code file 4, and the code file corresponding to node 7 is code file 7, then when executing S103, this embodiment will obtain code file 3, code file 4 and code file 7 from the target code library as the target code files corresponding to code block 1.
[0049] That is to say, this embodiment obtains at least one target code file corresponding to the target code block from the target code library based on the source code file corresponding to the target code block according to the nodes included in the second index and the edges between the nodes. This embodiment can achieve the acquisition of the target code file and can also improve the accuracy of the acquired target code file.
[0050] After executing S105 to obtain the target code file corresponding to the target code block, this embodiment executes S106 to obtain a search result according to the target code block and the target code file, and the search result corresponds to the code search query.
[0051] That is to say, the code retrieval results obtained in this embodiment include two parts: the target code block and its corresponding target code file. Compared with the prior art that only uses the target code block as the retrieval result, this embodiment can improve the richness of the obtained retrieval results, thereby enhancing the accuracy of subsequent use of the retrieval results for code question and answering or code continuation.
[0052] After executing S104 to obtain the search results, if the initial query is a query for the corresponding code continuation, this embodiment can also construct a prompt text (i.e., prompt) according to the target code block and the target code file corresponding to the target code block, and then obtain the code generation result output by the large language model according to the constructed prompt by calling the large language model.
[0053] In order to improve the accuracy of the constructed prompt text, the present embodiment may also construct the prompt text according to the target code block, the target code file corresponding to the target code block and the initial query.
[0054] Figure 2 is a schematic diagram according to the second embodiment of the present disclosure. Figure 2 As shown, in this embodiment, when executing S103 "obtaining the second index of the target code library", the following contents may be included:
[0055] S201, obtaining all code files in the target code library;
[0056] S202, generating a directory tree according to the directory information of all the code files and each code file in the target code library;
[0057] S203: Input the directory tree into a code architecture extraction model to obtain the output second index.
[0058] That is to say, this embodiment can also use the code architecture extraction model to obtain the second index according to the directory tree generated by all code files in the target code library. This embodiment can improve the acquisition efficiency and accuracy of the second index.
[0059] When this embodiment executes S203, the code architecture extraction model used is pre-trained; the code architecture extraction model can output the code architecture knowledge graph of the corresponding target code library based on the input directory tree of the corresponding target code library, and then use the output code architecture knowledge graph as the second index.
[0060] This embodiment can adopt the following method to pre-train the code architecture extraction model: obtain training data, the obtained training data includes a sample directory tree and an annotated code architecture knowledge graph; obtain an output predicted code architecture knowledge graph by inputting the sample directory tree into the initial neural network model; calculate the loss function value based on the obtained predicted code architecture knowledge graph and the annotated code architecture knowledge graph; adjust the parameters of the initial neural network model according to the loss function value to obtain the code architecture extraction model.
[0061] Figure 3 is a schematic diagram according to a third embodiment of the present disclosure. Figure 3 A schematic diagram of the second index in this embodiment is shown: Figure 3 The nodes in can be directory nodes, that is, different nodes correspond to different directories in the target code library, and different directories correspond to at least one code file; for example Figure 3 The Entity node in the code base corresponds to the Entity directory, the Service node corresponds to the Service directory in the code base, and so on.
[0062] Figure 4 is a schematic diagram according to a fourth embodiment of the present disclosure. Figure 4The flowchart of this embodiment when obtaining training data is shown in the figure: S401, obtain multiple high-quality code bases; S402, detect whether there are code bases to be processed, if so, execute S403, if not, execute S407; S403, filter sensitive code files in the current code base; S404, generate a sample directory tree based on the code files in the current code base; S405, obtain the annotated code architecture knowledge graph corresponding to the current code base; S406, obtain the code language and / or code framework of the current code base; S407, according to the code language and / or code framework of the code base corresponding to the training data, obtain the proportion of the code language and / or code framework of the training data ; S408, check whether the proportion of code language and / or code framework is reasonable. If so, execute S409, if not, execute S410, for example, the proportion of JAVA language training data is 30%, the proportion of Python language training data is 40%, and the proportion of other languages training data is 30%; S409, conduct random sampling review of training data; S410, remove training data with unreasonable proportions, for example, if the current proportion of JAVA language training data is 40%, then the corresponding training data will be cut off to reduce the proportion of JAVA language training data from 40% to 30%; S411, obtain the final training data to train the initial neural network model.
[0063] Figure 5 is a schematic diagram according to the fifth embodiment of the present disclosure. Figure 5 As shown, the code retrieval device 500 based on the large language model of this embodiment includes:
[0064] A first acquisition unit 501 is used to acquire a code search query and obtain a search vector of the code search query;
[0065] A second acquisition unit 502 is used to acquire a first index of a target code library, wherein the first index includes a plurality of code blocks and a plurality of code block vectors;
[0066] A first retrieval unit 503, configured to obtain a target code block according to the retrieval vector and the first index;
[0067] A third acquisition unit 504 is used to acquire a second index of the target code library, where the second index is a code architecture knowledge graph, where nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files;
[0068] A second retrieval unit 505 is configured to obtain a target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index;
[0069] The processing unit 506 is used to obtain a search result according to the target code block and the target code file.
[0070] The code search query acquired by the acquisition unit 501 is a query used to retrieve the corresponding code from the target code library; the acquisition unit 501 may first acquire the initial query input by the input end, and then further acquire the code search query according to the acquired initial query.
[0071] When obtaining the search vector of the code search query, the acquisition unit 501 may use an existing embedding model or embedding method to perform embedding processing on the code search query, and use the obtained embedding vector as the search vector corresponding to the code search query.
[0072] In addition, the acquisition unit 501 may first rewrite the code search query to generate multiple rewritten queries corresponding to the code search query, and then obtain the search vector of each rewritten query respectively.
[0073] That is, the acquisition unit 501 may also rewrite the code search query to acquire multiple rewritten queries similar to the code search query, thereby improving the accuracy of searching the target code block in the first index.
[0074] In this embodiment, after the acquisition unit 501 obtains the search vector of the code search query, the second acquisition unit 502 obtains the first index of the target code library; wherein the first index of this embodiment includes multiple code blocks and multiple code block vectors, and different code blocks correspond to different code block vectors.
[0075] In this embodiment, the target code library may be a code library specified by the input end, or may be a code library corresponding to the current code environment in the input end.
[0076] When acquiring the first index, the second acquiring unit 502 may acquire the first index obtained in advance according to the target code library, or may acquire the first index obtained in real time according to the target code library.
[0077] When the second acquisition unit 502 acquires the first index of the target code library, the following implementation method may be adopted: acquiring all code files in the target code library; performing syntax tree parsing on each code file, dividing each code file into multiple code blocks according to syntax rules; performing embedding processing on the obtained multiple code blocks respectively to obtain different code block vectors corresponding to different code blocks; and obtaining the first index according to the multiple code blocks and the multiple code block vectors.
[0078] In this embodiment, after the second acquisition unit 502 acquires the first index, the first search unit 503 acquires the target code block according to the search vector and the first index; in this embodiment, the number of the target code blocks may be one or more.
[0079] When the first retrieval unit 503 obtains the target code block according to the retrieval vector and the first index, the following implementation method can be adopted: performing similarity calculation between the retrieval vector and multiple code block vectors in the first index; obtaining at least one target code block according to the similarity calculation result, for example, the code block corresponding to the code library vector whose similarity calculation result is ranked in the top N places can be used as at least one target code block, where N is a positive integer greater than or equal to 1.
[0080] In this embodiment, after the first retrieval unit 503 obtains the target code block, the third acquisition unit 504 obtains the second index of the target code library; wherein the second index is a code architecture knowledge graph, and the code architecture knowledge graph includes multiple nodes and edges between nodes.
[0081] In this embodiment, the nodes in the code architecture knowledge graph represent code files in the target code library, and the edges between the nodes represent the dependencies between the code files in the target code library.
[0082] It can be understood that the nodes in the code architecture knowledge graph of this embodiment can use file names to represent code files, or can use directory information of code files to represent code files.
[0083] When acquiring the second index, the third acquisition unit 504 may acquire the second index obtained in advance according to the target code library, or may acquire the second index obtained in real time according to the target code library.
[0084] The third acquisition unit 504 can obtain the second index manually according to the directories to which different code files in the target code library belong and the relationship between the directories, that is, manually determine each node in the code architecture knowledge graph and the edges between the nodes.
[0085] In addition, when the third acquisition unit 504 acquires the second index of the target code library, it may include the following contents: acquiring all code files in the target code library; generating a directory tree based on the directory information of all code files and each code file in the target code library; and obtaining the output second index by inputting the directory tree into the code architecture extraction model.
[0086] The code architecture extraction model used by the third acquisition unit 504 is pre-trained; the code architecture extraction model can output the code architecture knowledge graph of the corresponding target code library based on the input directory tree of the corresponding target code library, and then use the output code architecture knowledge graph as the second index.
[0087] The code retrieval device 500 based on a large language model of this embodiment may further include a training unit 507, which is used to pre-train a code architecture extraction model in the following manner: obtaining training data, wherein the obtained training data includes a sample directory tree and an annotated code architecture knowledge graph; obtaining an output predicted code architecture knowledge graph by inputting the sample directory tree into an initial neural network model; calculating a loss function value based on the obtained predicted code architecture knowledge graph and the annotated code architecture knowledge graph; and adjusting the parameters of the initial neural network model based on the loss function value to obtain a code architecture extraction model.
[0088] In this embodiment, after the third acquisition unit 504 acquires the second index, the second search unit 505 acquires the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index; wherein, the target code file corresponding to one target code block may be one or more.
[0089] It is understandable that if multiple target code blocks are obtained, the second retrieval unit 505 will obtain the target code file corresponding to each target code block respectively.
[0090] In this embodiment, the source code file corresponding to the target code block is the code file to which the target code block belongs in the target code library, that is, the target code block is obtained by segmenting the source code file.
[0091] When the second retrieval unit 505 obtains the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index, the following implementation method may be adopted: according to the source code file corresponding to the target code block, a source node of the corresponding source code file is determined in the second index; a target node having an edge with the source node is selected from the second index; and the target code file corresponding to the target node is obtained from the target code library.
[0092] That is to say, the second retrieval unit 505 obtains at least one target code file corresponding to the target code block from the target code library based on the source code file corresponding to the target code block according to the nodes included in the second index and the edges between the nodes. This embodiment can achieve the acquisition of the target code file and can also improve the accuracy of the acquired target code file.
[0093] In this embodiment, after the second retrieval unit 505 obtains the target code file corresponding to the target code block, the processing unit 506 obtains a retrieval result according to the target code block and the target code file, and the retrieval result corresponds to the code retrieval query.
[0094] That is to say, the code retrieval results obtained in this embodiment include two parts: the target code block and its corresponding target code file. Compared with the prior art that only uses the target code block as the retrieval result, this embodiment can improve the richness of the obtained retrieval results, thereby enhancing the accuracy of subsequent use of the retrieval results for code question and answering or code continuation.
[0095] After the processing unit 506 obtains the search results, if the initial query is a query for the corresponding code continuation, this embodiment can also construct a prompt text (i.e., prompt) according to the target code block and the target code file corresponding to the target code block, and then obtain the code generation result output by the large language model according to the constructed prompt by calling the large language model.
[0096] In order to improve the accuracy of the constructed prompt text, the processing unit 504 may also construct the prompt text according to the target code block, the target code file corresponding to the target code block, and the initial query.
[0097] In the technical solution disclosed herein, the acquisition, storage and application of user personal information involved are in compliance with the provisions of relevant laws and regulations and do not violate public order and good morals.
[0098] 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.
[0099] like Figure 6 , is a block diagram of an electronic device according to a code retrieval method based on a large language model according to an embodiment of the present disclosure. The electronic device is intended to represent various forms of digital computers, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples, and are not intended to limit the implementation of the present disclosure described and / or required herein.
[0100] like Figure 6As shown, the device 500 includes a computing unit 601, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 602 or a computer program loaded from a storage unit 608 into a random access memory (RAM) 603. In the RAM 603, various programs and data required for the operation of the device 600 can also be stored. The computing unit 601, the ROM 602, and the RAM 603 are connected to each other via a bus 604. An input / output (I / O) interface 605 is also connected to the bus 604.
[0101] 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 608, 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.
[0102] 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 central processing unit (CPU), a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing chips, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 601 performs the various methods and processes described above, such as a code retrieval method based on a large language model. For example, in some embodiments, a code retrieval method based on a large language model may be implemented as a computer software program, which is tangibly included in a machine-readable medium, such as a storage unit 608.
[0103] 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 retrieval method based on the large language model described above may be performed. Alternatively, in other embodiments, the computing unit 601 may be configured to perform the code retrieval method based on the large language model in any other appropriate manner (e.g., by means of firmware).
[0104] Various implementations of the systems and techniques described herein can be implemented in digital electronic circuit systems, integrated circuit systems, field programmable gate arrays (FPGAs), application specific integrated circuits (ASICs), application specific standard products (ASSPs), systems on chips (SOCs), complex programmable logic devices (CPLDs), computer hardware, firmware, software, and / or combinations thereof. These various implementations can include: being implemented in one or more computer programs that can be executed and / or interpreted on a programmable system including at least one programmable processor, which can be a special purpose or general purpose programmable processor that can 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.
[0105] 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 vehicle positioning or positioning model training device, so that the program code, when executed by the processor or controller, causes 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.
[0106] In the context of the present disclosure, a machine-readable medium may be a tangible medium that may include 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 equipment, or any suitable combination of the foregoing. A more specific example of a machine-readable storage medium may include an electrical connection based on one or more lines, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the foregoing.
[0107] 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 a 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).
[0108] The systems and techniques described herein may be implemented in a computing system that includes back-end 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 front-end 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 back-end components, middleware components, or front-end 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: a local area network (LAN), a wide area network (WAN), and the Internet.
[0109] A computer system may include a client and a server. The client and the server are generally remote from each other and usually interact through a communication network. The relationship between the client and the server is generated by computer programs running on the corresponding computers and having a client-server relationship with each other. The server may be a cloud server, also known as a cloud computing server or a cloud host, which is a host product in the cloud computing service system to solve the defects of difficult management and weak business scalability in traditional physical hosts and VPS services ("Virtual Private Server", or "VPS" for short). The server may also be a server for a distributed system, or a server combined with a blockchain.
[0110] 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.
[0111] 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 retrieval method based on a large language model, include: Obtaining a code search query query, and obtaining a search vector of the code search query; Obtaining a first index of a target code library, wherein the first index includes a plurality of code blocks and a plurality of code block vectors; Acquire a target code block according to the search vector and the first index; Obtaining a second index of the target code library, where the second index is a code architecture knowledge graph, where nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files; Acquire a target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index; A search result is obtained according to the target code block and the target code file.
2. The method according to claim 1, in, The obtaining of the second index of the target code library comprises: Obtain all code files in the target code library; Generate a directory tree according to the directory information of all the code files and each code file in the target code library; By inputting the directory tree into a code architecture extraction model, the second index is outputted; Wherein, the code architecture extraction model is pre-trained.
3. The method according to claim 2, further comprising: include: Acquire training data, wherein the training data includes a sample directory tree and annotated code architecture knowledge graph; By inputting the sample directory tree into an initial neural network model, an output predicted code architecture knowledge graph is obtained; Calculating a loss function value according to the predicted code architecture knowledge graph and the annotated code architecture knowledge graph; The parameters of the initial neural network model are adjusted according to the loss function value to obtain the code architecture extraction model.
4. The method according to claim 1, in, The acquiring the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index comprises: According to the source code file corresponding to the target code block, determining a source node corresponding to the source code file in the second index; Selecting a target node having an edge with the source node from the second index; Obtain an object code file corresponding to the target node from the object code library.
5. The method according to claim 1, after obtaining the search result according to the target code block and the target code file, the method further comprises: Constructing a prompt text according to the target code block and the target code file; By calling the large language model, an output code generation result is obtained according to the prompt text.
6. A code retrieval device based on a large language model, include: A first acquisition unit, configured to acquire a code search query and obtain a search vector of the code search query; A second acquisition unit, configured to acquire a first index of a target code library, wherein the first index includes a plurality of code blocks and a plurality of code block vectors; A first retrieval unit, configured to obtain a target code block according to the retrieval vector and the first index; A third acquisition unit is used to acquire a second index of the target code library, where the second index is a code architecture knowledge graph, where nodes in the code architecture knowledge graph represent code files, and edges in the code architecture knowledge graph represent dependencies between code files; A second retrieval unit, configured to obtain a target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index; The processing unit is used to obtain a search result according to the target code block and the target code file.
7. The device according to claim 6, in, When acquiring the second index of the target code library, the third acquisition unit specifically performs: Obtain all code files in the target code library; Generate a directory tree according to the directory information of all the code files and each code file in the target code library; By inputting the directory tree into a code architecture extraction model, the second index is outputted; Wherein, the code architecture extraction model is pre-trained.
8. The apparatus according to claim 7, further comprising a training unit, configured to perform the following: Acquire training data, wherein the training data includes a sample directory tree and annotated code architecture knowledge graph; By inputting the sample directory tree into an initial neural network model, an output predicted code architecture knowledge graph is obtained; Calculating a loss function value according to the predicted code architecture knowledge graph and the annotated code architecture knowledge graph; The parameters of the initial neural network model are adjusted according to the loss function value to obtain the code architecture extraction model.
9. The device according to claim 6, in, When the second retrieval unit obtains the target code file corresponding to the target code block according to the source code file corresponding to the target code block and the second index, the second retrieval unit specifically performs: According to the source code file corresponding to the target code block, determining a source node corresponding to the source code file in the second index; Selecting a target node having an edge with the source node from the second index; Obtain an object code file corresponding to the target node from the object code library.
10. The device according to claim 6, in, After obtaining the search result according to the target code block and the target code file, the processing unit is further configured to execute: Constructing a prompt text according to the target code block and the target code file; By calling the large language model, an output code generation result is obtained according to the prompt text.
11. 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 5.
12. 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 5.
13. A computer program product, comprising a computer program, which, when executed by a processor, implements the method according to any one of claims 1 to 5.
Citation Information
Patent Citations
Software information organization method and device, and computer-readable medium
WO2022047623A1
Cited By
Data construction method, code question-answering method, task platform and code question-answering system
CN119149712A
Data construction, code question answering methods, task platform and code question answering system
CN119149712B
Database code extraction method and system based on large language model rapid screening
CN119226380A
Database Code Extraction Method and System Based on Rapid Screening of Large Language Models
CN119226380B
Code knowledge graph generation method and device, code generation method and device, equipment and medium
CN119721222A
