Code semantic retrieval method, retrieval system and storage medium
By performing multi-dimensional semantic matching and context mapping on the structured meta-information of the target code repository, and combining it with artificial intelligence models, the problems of low recall rate and high operation and maintenance cost in existing code retrieval technologies have been solved, achieving low-cost, high-precision and highly versatile code retrieval.
Patent Information
- Application Number
- CN202610804579.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-06-04
- Publication Date
- 2026-08-25
AI Technical Summary
Existing code retrieval solutions suffer from low recall and accuracy, high maintenance costs, and limited application scenarios, making it difficult to achieve low-cost, high-precision, and versatile code retrieval.
By performing multi-dimensional semantic matching on the structured meta-information of the target code repository, the target code symbols are identified and context is generated. Semantic retrieval is performed using an artificial intelligence model, combined with abstract syntax tree parsing and a lightweight local database, achieving multi-dimensional semantic matching and context mapping, thereby reducing deployment and maintenance costs.
It improves the accuracy and recall of code retrieval, reduces deployment and maintenance costs, and provides a code retrieval solution that balances low cost, high retrieval accuracy, and strong versatility.
Smart Images

Figure CN122633151A_ABST
Abstract
Description
Technical Field
[0001] This specification relates to the field of artificial intelligence technology, and in particular to a code semantic retrieval method, retrieval system and storage medium. Background Technology
[0002] Code retrieval is a core underlying technology in the field of Artificial Intelligence (AI)-assisted programming. It involves locating relevant code resources in a codebase based on the user's natural language requirements. In AI-assisted programming scenarios, users typically describe their development needs in natural language. The AI must quickly locate relevant code based on these requirements and then complete the programming task based on the retrieved code. Therefore, the accuracy of code retrieval directly impacts the quality and efficiency of AI-assisted programming.
[0003] Currently, code retrieval solutions include plain text retrieval based on keyword matching, vector retrieval based on vector databases and embedding models, and symbol indexing based on language service protocols.
[0004] However, plain text retrieval solutions have low recall and precision, vector retrieval solutions have high deployment and maintenance costs, and symbol indexing solutions have limited application scenarios. There is a lack of a code retrieval solution that balances low cost, high retrieval accuracy, and strong versatility.
[0005] The information in the background section is merely information known only to the inventor and does not imply that such information had entered the public domain before the date of this application, nor does it imply that it can be considered prior art in this disclosure. Summary of the Invention
[0006] This specification provides a code semantic retrieval method, retrieval system, and storage medium, which can perform multi-dimensional semantic matching between each code symbol in the structured meta-information corresponding to the target code library and the retrieval query information, determine the target code symbol and generate the corresponding target context. The target context can be used to input artificial intelligence models to obtain semantic retrieval responses, realizing a low-cost, high-precision, and highly versatile code retrieval scheme.
[0007] To achieve the above objectives, the embodiments in this specification adopt the following technical solutions: Firstly, this specification provides a code semantic retrieval method, comprising: responding to a code library semantic retrieval request, the code library semantic retrieval request including: retrieving query information; based on the retrieved query information, performing multi-dimensional semantic matching on each code symbol in the structured meta-information corresponding to the target code library to obtain a relevance score between each code symbol and the retrieved query information, the structured meta-information being generated based on parsing code files of the target code library using an abstract syntax tree, the multi-dimensional semantic matching being used to calculate the semantic association degree between the retrieved query information and each code symbol from multiple semantic dimensions; determining target code symbols based on the relevance scores between each code symbol and the retrieved query information, and performing context mapping based on the target code symbols, the symbol description information corresponding to the target code symbols, and the call relationship information between the target code symbols to obtain a structured target context, the target context being used to represent code structure information semantically related to the retrieved query information; and inputting the target context into an artificial intelligence model to obtain a semantic retrieval response output by the artificial intelligence model.
[0008] In some embodiments, the structured metadata is generated by parsing the code files of the target code library using an abstract syntax tree, and extracting symbol definition information, call relationship information between code symbols, documentation comment information, and type signature information from the parsed abstract syntax tree. The code symbols include at least one of functions, classes, variables, interfaces, and enumerations. The call relationship information is used to characterize the logical relationship between multiple code symbols. The structured metadata includes a primary index, an inverted index, and a call graph index. The primary index is used to store symbol description information for each code symbol, the inverted index is used to store keyword mappings for documentation comment information, and the call graph index is used to store call relationships between code symbols.
[0009] In some embodiments, the multi-dimensional semantic matching includes at least two of the following: symbol name matching, document comment matching, type signature matching, call relationship matching, file path matching, and decorator matching.
[0010] In some embodiments, the call relationship matching is based on the code call graph, propagating upstream along the call relationships between symbols to determine the propagating code symbols associated with directly matched code symbols and the propagation matching score, wherein the propagation matching score is calculated by attenuating the propagation depth using an exponential decay factor.
[0011] In some embodiments, the step of performing multi-dimensional semantic matching on each code symbol in the structured meta-information corresponding to the target code library based on the retrieval query information to obtain a relevance score between each code symbol and the retrieval query information includes: determining context factors for each semantic matching dimension based on the query features of the retrieval query information, wherein the query features include function name features, function description features, and type information features, and the context factors are used to quantify the association strength between the query features and the corresponding semantic matching dimension; determining dynamic weight coefficients for each semantic matching dimension based on the product of preset base weight coefficients and the context factors; and weighting and fusing the matching scores of each semantic matching dimension based on the dynamic weight coefficients to obtain the relevance score.
[0012] In some embodiments, the step of performing context mapping based on the target code symbol, the symbol description information corresponding to the target code symbol, and the relationship information between the target code symbols to obtain a structured target context includes: extracting the symbol description information corresponding to the target code symbol from the structured metadata, wherein the symbol description information includes the symbol name, symbol type, function signature, and documentation comments of the target code symbol; dividing the target code symbol into multiple relevance levels according to a preset relevance score threshold, wherein the relevance levels include a high relevance level, a medium relevance level, and a low relevance level, wherein the high relevance level corresponds to target code symbols with a relevance score greater than a first threshold, the medium relevance level corresponds to target code symbols with a relevance score greater than or equal to a second threshold and less than or equal to the first threshold, and the low relevance level corresponds to target code symbols with a relevance score less than the second threshold; and performing context mapping based on the divided multiple relevance levels to generate the target context, wherein the target context includes the symbol description information corresponding to the target code symbols organized by relevance level and the call relationship information between the target code symbols.
[0013] In some embodiments, the method further includes: identifying the query type of the retrieved query information, the query type including function implementation query, defect location query, architecture understanding query, and refactoring optimization query; and adjusting the display priority of symbol description information at each relevant level in the target context, and the level of detail in the annotation of the calling relationship between code symbols, according to the query type.
[0014] In some embodiments, the method further includes: obtaining the context window budget of the artificial intelligence model; and, based on the information value and resource consumption of the target code symbols, under the constraint of the context window budget, using a 0-1 knapsack algorithm to determine the selection state of each target code symbol, maximizing the total information value of the target code symbols in the selection state, wherein the information value is determined according to the relevance score and the importance of the target code symbols, and the importance of the symbols is determined according to the logarithm of the sum of the number of times the target code symbol is called and the number of times other code symbols are called, and the weight coefficient of the visibility attribute.
[0015] In some embodiments, the method further includes: monitoring file system events of the target code repository to detect changes to code files, the file system events including file creation events, file modification events, file deletion events, and file renaming events; performing content verification on the changed code files to identify the changed code files, the content verification including calculating the content hash value of the code file and comparing it with the cached historical hash value; re-executing abstract syntax tree parsing on the changed code files to extract updated structured meta-information, comparing the updated structured meta-information with the cached historical structured meta-information to identify the changed code symbols; and incrementally updating the portion of the index on which the multi-dimensional semantic matching depends that corresponds to the changed code symbols based on the updated structured meta-information.
[0016] In some embodiments, the structured metadata is stored in a lightweight local database, including an SQLite database. The keyword index of the document annotation information is stored in a custom binary format, which employs variable-length integer encoding and bitmap indexing technology. The call relationship information is stored in JSON format with an adjacency list structure.
[0017] Secondly, this specification provides a retrieval system, comprising: at least one storage medium storing at least one instruction set for retrieving data from a target code library based on code semantics; and at least one processor communicatively connected to the at least one storage medium, wherein, when the data processing system is running, the at least one processor reads the at least one instruction set and implements the method described in any one of the first aspects according to the instructions of the at least one instruction set.
[0018] Thirdly, this specification also provides a computer-readable non-volatile storage medium, wherein the computer-readable non-volatile storage medium stores at least one instruction set, which, when executed by at least one processor, implements the method provided in the first aspect.
[0019] Other functionalities of the code semantic retrieval methods, retrieval systems, and storage media provided in this specification will be partially listed in the following description. The inventive aspects of the code semantic retrieval methods, retrieval systems, and storage media provided in this specification can be fully understood through practice or use of the methods, apparatus, and combinations described in the detailed examples below. Attached Figure Description
[0020] To more clearly illustrate the technical solutions in the embodiments of this specification, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of this specification. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0021] Figure 1 A schematic diagram illustrating an application scenario of the code semantic retrieval method provided according to embodiments of this specification is shown. Figure 2 A computer system 200 provided according to an embodiment of this specification is shown; Figure 3 A flowchart of a code semantic retrieval method provided according to an embodiment of this specification is shown; Figure 4 A schematic diagram illustrating the execution of a code semantic retrieval method provided according to an embodiment of this specification is shown; Figure 5 A schematic diagram illustrating an embodiment of this specification for obtaining structured meta-information corresponding to a target code library is shown. Figure 6 A schematic diagram illustrating an embodiment of a target codebase file system event listener provided according to this specification is shown; and Figure 7 A schematic diagram illustrating a method for calculating a correlation score according to an embodiment of this specification is shown. Detailed Implementation
[0022] The following description provides specific application scenarios and requirements for this specification, intended to enable those skilled in the art to make and use the contents of this specification. Various partial modifications to the disclosed embodiments will be apparent to those skilled in the art, and the general principles defined herein can be applied to other embodiments and applications without departing from the spirit and scope of this specification. Therefore, this specification is not limited to the embodiments shown, but rather to the widest scope consistent with the claims.
[0023] The terminology used herein is for the purpose of describing particular exemplary embodiments only and is not restrictive. For example, unless the context clearly indicates otherwise, the singular forms “a,” “an,” and “the” used herein may also include the plural forms. When used in this specification, the terms “comprising,” “including,” and / or “containing” mean that the associated integers, steps, operations, elements, and / or components are present, but do not exclude the presence of one or more other features, integers, steps, operations, elements, components, and / or groups, or that other features, integers, steps, operations, elements, components, and / or groups may be added to the system / method.
[0024] Considering the following description, these and other features of this specification, as well as the operation and function of the related components of the structure, and the economy of assembly and manufacture of the parts, can be significantly improved. All of these form part of this specification with reference to the accompanying drawings. However, it should be clearly understood that the drawings are for illustrative and descriptive purposes only and are not intended to limit the scope of this specification. It should also be understood that the drawings are not drawn to scale.
[0025] The flowcharts used in this specification illustrate operations implemented according to some embodiments of this specification. It should be clearly understood that the operations in the flowcharts may not be implemented in a sequential order. Instead, the operations may be implemented in reverse order or simultaneously. Furthermore, one or more additional operations may be added to the flowcharts. One or more operations may be removed from the flowcharts.
[0026] For ease of description, the terms that will appear later in this manual will be explained first.
[0027] Term 1: Abstract Syntax Tree (AST): A tree-like representation of source code used to reflect the syntactic structure of the code. Each node represents a syntactic unit in the code (such as a function, class, variable, etc.).
[0028] Term 2: Code Metadata: This refers to the structured metadata corresponding to the code. Structured metadata is the structured information extracted from the code file by parsing the AST, including function signatures, class definitions, variable declarations, comments, type information, etc.
[0029] Term 3: Code Symbol: A named entity in a code file that has independent syntactic meaning and can be recognized and extracted by AST. It is the smallest unit that carries code semantics.
[0030] Term 4: Context Mapping: This refers to the process of mapping the query intent described in natural language within the retrieval query information to relevant code files. Context mapping generates concise context information based on the semantic structure of the relevant code files. This concise context information helps the model understand the semantics of the code in the relevant code files.
[0031] Term 5: TypeScript: TypeScript is an open-source programming language that is a superset of JavaScript, essentially adding optional static typing and class-based object-oriented programming. TypeScript can be translated into JavaScript code by a compiler and run on any browser and operating system.
[0032] Term 6: JavaScript Documentation Comments (JSDoc): JSDoc is a commenting specification for JavaScript. It's used to describe the purpose, parameter types, return values, and other information in comments for objects such as functions, classes, and variables. These descriptions can be used to generate formatted API documentation and can also be used by editors and build tools for type checking, retrieval, and other functions.
[0033] Term 7: ECMAScript code inspection tool (ESlint): This refers to a static code analysis tool (lint) based on the JavaScript standard ECMA-262 (ES). ESLint uses the ESLint parser (@typescript-eslint / parser) to parse TypeScript source code into an AST conforming to the ESTree standard, and then performs static code analysis according to preset rules. In this specification, for TypeScript files, the ESLint parser can be used to parse the source code in the code file into an AST.
[0034] In the embodiments described in this specification, the artificial intelligence model can be a Large Language Model (LLM), also simply referred to as a large model. A Large Language Model is a natural language processing model based on deep learning technology. It has a high number of parameters, typically ranging from billions to hundreds of billions or even higher, and possesses powerful language understanding and generation capabilities. Large Language Models can employ the Transformer architecture or its variants (such as GPT, BERT, etc.). This architecture utilizes an attention mechanism to achieve global modeling of sequential data, efficiently handling long-distance dependencies and thus performing excellently in natural language tasks. Large Language Models can be used to perform various tasks such as text generation, language translation, text summarization, and question answering reasoning.
[0035] In some embodiments, large language models can also have the ability to understand and generate data from other modalities (such as visual, audio, etc.). In this case, large language models can also be called multimodal large language models (MLLMs). MLLMs provide a richer and more natural interactive experience by integrating multiple types of input and output, such as text, images, and sound.
[0036] It should be noted that the key technologies of large language models can be found in the detailed description in the paper "A Survey of Large Language Models" (paper number: arXiv:2303.18223v18, published on March 8, 2026), and will not be repeated here.
[0037] The following section introduces the application scenarios of this manual.
[0038] The technical solutions provided in this manual are applicable to scenarios involving AI-assisted programming. AI-assisted programming refers to the process of using artificial intelligence technology to assist developers in completing the entire software development workflow, including code writing, debugging, refactoring, and documentation generation. As an example, software project development is a common scenario for AI-assisted programming. In this scenario, AI-assisted programming tools can be used for various purposes, such as functional code generation, defect location and repair, code architecture analysis, and unit test writing. For instance, in the development of service systems in large internet companies, development teams typically need to quickly develop new service functions, fix online defects, and iteratively optimize existing modules based on the existing codebase architecture and specifications to meet the need for rapid service deployment.
[0039] However, among the relevant solutions, AI-assisted programming tools suffer from problems such as low recall and accuracy, high maintenance costs, and limited application scenarios.
[0040] Specifically, some related solutions rely on keyword-based literal matching for retrieval. In this approach, the AI-assisted programming tools cannot understand the actual semantics of the code, resulting in low recall and accuracy. Other solutions depend on vector databases and embedding models for retrieval. However, this approach is costly to deploy and difficult to scale. Still other solutions only support precise symbol queries. This approach struggles to adapt to fuzzy queries in natural language, limiting its application scenarios.
[0041] To this end, this specification provides a code semantic retrieval method, which can be executed by a retrieval system, comprising: the retrieval system responding to a code library semantic retrieval request, the code library semantic retrieval request including: retrieval query information. Then, based on the retrieval query information, the retrieval system can perform multi-dimensional semantic matching on each code symbol in the structured meta-information corresponding to the target code library to obtain a relevance score between each code symbol and the retrieval query information. The structured meta-information is generated based on parsing the code files of the target code library using an abstract syntax tree. The multi-dimensional semantic matching is used to calculate the semantic association degree between the retrieval query information and each code symbol from multiple semantic dimensions. Next, the retrieval system can determine target code symbols based on the relevance scores between each code symbol and the retrieval query information, and perform context mapping based on the target code symbols, the symbol description information corresponding to the target code symbols, and the call relationship information between the target code symbols to obtain a structured target context. The target context is used to represent code structure information semantically related to the retrieval query information. Finally, the retrieval system can input the target context into an artificial intelligence model to obtain a semantic retrieval response output by the artificial intelligence model.
[0042] In the solution provided in this specification, the retrieval system responds to a semantic retrieval request from a code repository. Based on the retrieval query information in the request, it performs multi-dimensional semantic matching on the structured meta-information corresponding to the code files in the target code repository, obtaining a relevance score between each code symbol and the retrieval query information. The structured meta-information corresponding to the code files in the target code repository is obtained by parsing the code files of the target code repository using an abstract syntax tree. Then, the retrieval system determines the target code symbols based on the relevance scores and performs context mapping by combining the symbol description information corresponding to the target code symbols and the call relationship information between the target code symbols, obtaining a structured target context representing the code structure information semantically related to the retrieval query information. Finally, the retrieval system uses the target context and an artificial intelligence model to obtain the semantic retrieval response corresponding to the semantic retrieval request from the code repository. In this way, the retrieval system can quantify the semantic association between the retrieval query information and the code symbols from multiple semantic dimensions, improving upon the limitations of pure text retrieval which only supports literal matching and symbol indexing which only supports exact queries, effectively improving the accuracy and recall rate of code retrieval. Furthermore, the retrieval system utilizes context mapping to obtain the target context. When guiding AI model reasoning based on the target context, this enables the AI model to fully understand the structural information of the code, ensuring that the output meets the architecture and specifications of the target code library. It also filters redundant information, improving the reasoning efficiency of the AI model. The solution presented in this specification eliminates the need for high-cost infrastructure such as vector databases and embedding models. High-precision semantic retrieval can be achieved based on structured meta-information parsed from abstract syntax trees, significantly reducing deployment and maintenance costs. It provides a code retrieval solution that balances low cost, high retrieval accuracy, and strong versatility.
[0043] Figure 1 A schematic diagram illustrating an application scenario of the code semantic retrieval method provided according to embodiments of this specification is shown. For example... Figure 1 As shown, the application scenario 100 may include a retrieval system 11, a target code library 12, a terminal device 13, and an artificial intelligence model 14.
[0044] refer to Figure 1The retrieval system 11 can be deployed on a device or device cluster with data processing capabilities. For example, the retrieval system 11 can be deployed on a server, server cluster, cloud server, or other such device. In this case, the physical device corresponding to the retrieval system 11 can store data or instructions for executing the code semantic retrieval method described in this specification, and can execute or be used to execute the data or instructions. In some embodiments, the device deploying the retrieval system 11 may include a hardware device with data information processing capabilities and the necessary programs required to drive the hardware device to work. The large language model 14 can be deployed on the same device or device cluster as the retrieval system 11, or on a different device or device cluster than the retrieval system 11; this specification does not impose any limitations on this.
[0045] In some embodiments, the target code library 12 may store multiple code files. The target code library 12 may be deployed on the same device or device cluster as the retrieval system 11, or it may be deployed on a different device or device cluster than the retrieval system 11. This specification does not impose any restrictions on this.
[0046] As an example, terminal device 13 may be an electronic device capable of communicating with retrieval system 11. For example, terminal device 13 may include mobile devices, tablets, laptops, built-in devices in motor vehicles, or similar content, or any combination thereof. In some embodiments, the mobile device may include wearable devices, smart mobile devices, virtual reality devices, augmented reality devices, or similar devices, or any combination thereof. In some embodiments, wearable devices include smartwatches, smart bracelets, smart glasses, etc. In some embodiments, the smart mobile device may include smartphones, personal digital assistants, gaming devices, navigation devices, etc., or any combination thereof. In some embodiments, the virtual reality device or augmented reality device may include head-mounted displays, virtual reality headsets, virtual reality glasses, virtual reality patches, augmented reality headsets, augmented reality glasses, augmented reality patches, or similar content, or any combination thereof.
[0047] In some embodiments, the terminal device 13 may have one or more applications (APPs) installed. The APPs provide the ability or interface to access the retrieval system 11. The APPs include, but are not limited to: web browser APPs, search APPs, chat APPs, shopping APPs, video APPs, financial management APPs, instant messaging tools, email clients, social media platform software, etc. In some embodiments, the terminal device 13 may have a target APP installed. The target APP is an APP corresponding to an AI-assisted programming tool, and the target APP may display a page for accessing the retrieval system 11.
[0048] In some embodiments, the target APP may, in response to a user operation, receive search query information input by the user and generate a code library semantic search request based on the search query information, and send the code library semantic search request to the retrieval system 11. The retrieval system 11 may, in response to the code library semantic search request, execute the code semantic search method provided in this specification, obtain a semantic search response, and send the semantic search response to the target APP.
[0049] It should be understood that Figure 1 The number of retrieval systems 11, target code libraries 12, terminal devices 13, and artificial intelligence models 14 in this example is merely illustrative. Depending on implementation needs, any number of retrieval systems 11, target code libraries 12, terminal devices 13, and artificial intelligence models 14 can be included.
[0050] This specification also provides a computer system 200. Figure 2 A computer system 200 according to an embodiment of this specification is shown. The computer system 200 can serve as... Figure 1 The retrieval system 11 in the middle.
[0051] In some embodiments, one or more computer systems 200 may perform one or more steps of one or more methods described or illustrated herein. In some embodiments, one or more computer systems 200 may also provide functionality described or illustrated herein, such as the ability to retrieve one or more engines in system 11 (or other arbitrary functionality).
[0052] This specification considers any suitable number of computer systems 200. The computer systems 200 are considered to take any suitable physical form. By way of example and not limitation, the computer system 200 may be an embedded computer system, a system-on-a-chip (SOC), a single-board computer system (SBC) (e.g., a computer-on-module (COM) or system-on-module (SOM)), a desktop computer system, a notebook or laptop system, an interactive kiosk, a mainframe, a grid of computer systems, a mobile phone, a personal digital assistant (PDA), a server, a tablet computer system, or any suitable combination thereof. Where appropriate, one or more computer systems 200 may be single or distributed; may span multiple locations or multiple machines; may span one or more data centers; or reside in the cloud, which may include one or more cloud components and communicate through one or more networks. Where appropriate, one or more computer systems 200 may perform one or more steps of one or more methods described or illustrated herein without substantial spatial or temporal limitations, and may perform in real-time or batch mode, or at different times and / or different locations.
[0053] In some embodiments, computer system 200 includes: processor 202, memory 204, storage 206, input / output (I / O) interface 208, communication interface 210, and interconnect structure 212. Although this specification describes and illustrates an example computer system 200 having a specific number of specific components in a specific arrangement, this specification contemplates any suitable number of any suitable components and any suitable arrangement.
[0054] In some embodiments, processor 202 is used to execute instructions. Processor 202 can retrieve instructions through internal registers, internal caches, memory 204, or storage 206, decode and execute them, and write one or more results to internal registers, internal caches, memory 204, or storage 206. Where appropriate, processor 202 may include one or more internal caches. Processor 202 may also include one or more internal registers, one or more arithmetic logic units (ALUs), and one or more processor cores. Processor 202 may be a multi-core processor and may include, but is not limited to: microcontrollers, microprocessors, RISC, ASICs, ASIPs, CPUs, GPUs, PPUs, DSPs, FPGAs, ARMs, and programmable logic devices (PLDs), or other circuits or processors capable of performing one or more functions, or any combination thereof.
[0055] In some embodiments, memory 204 may be used to store instructions to be executed by processor 202 and / or data to be operated by processor 202. Memory 204 may include main memory and may include random access memory (RAM). Where appropriate, memory 204 may be volatile memory (e.g., dynamic RAM (DRAM) or static RAM (SRAM)), or other suitable memory types. Processor 202 may couple to memory 204 via one or more memory paths for instruction and data access. Where appropriate, memory 204 may include one or more memory cells.
[0056] In some embodiments, storage 206 may be used to provide non-volatile storage for data and / or instructions. Storage 206 may include, but is not limited to: hard disk drive (HDD), solid-state drive (SSD), flash memory, ROM, erasable ROM, optical media (e.g., optical disc), magnetic media (e.g., magnetic disk or magnetic tape), USB storage device, or combinations thereof. Where appropriate, storage 206 may be located inside or outside computer system 200 and may include one or more storage control units to facilitate communication between processor 202 and storage 206.
[0057] In some embodiments, I / O interface 208 may be used to provide an interface between computer system 200 and one or more I / O devices. I / O interface 208 may include hardware, software, or a combination of both. I / O devices enable communication between a person and computer system 200 or between computer system 200 and external devices. Exemplary I / O devices may include, but are not limited to: keyboard, keypad, microphone, display, mouse, printer, scanner, speaker, camera, touch input device, stylus, trackball, tablet, and other suitable I / O devices; and I / O devices may also include one or more sensors. Where appropriate, I / O interface 208 may include one or more device or software drivers to enable processor 202 to control one or more associated I / O devices.
[0058] In some embodiments, communication interface 210 may be used to provide a communication interface between computer system 200 and one or more other computer systems 200 and / or one or more networks. Communication interface 210 may include hardware, software, or a combination of both. Communication interface 210 may support wired or wireless communication and may include a network interface controller (NIC) or a wireless adapter (e.g., a Wi-Fi adapter). Exemplary networks may include, but are not limited to: ad hoc networks, personal area networks (PANs), local area networks (LANs), wide area networks (WANs), metropolitan area networks (MANs), one or more portions of the Internet, and so on. Where appropriate, communication interface 210 may include one or more communication interfaces 210.
[0059] In some embodiments, the interconnect structure 212 is used to interconnect the components of the computer system 200. The interconnect structure 212 may include, but is not limited to: bus, switching structure, crossbar switch, on-chip interconnect network, point-to-point interconnect or other suitable interconnect structure, or any combination thereof.
[0060] This specification also provides a computer-readable non-volatile storage medium that stores a computer program. When executed by a processor, the computer program can be used to perform one or more steps of one or more methods described or shown herein, or to implement one or more functions described or shown herein. The computer-readable non-volatile storage medium may include one or more semiconductor or other integrated circuit (IC) based memories (e.g., FPGA or ASIC built-in memory / configuration memory), HDDs, HHDs, optical discs, optical disc drives (ODDs), magneto-optical discs, magneto-optical drives, floppy disks, floppy disk drives (FDDs), magnetic tapes, SSDs, RAM drives, secure digital cards, or combinations thereof, or other suitable media.
[0061] Figure 3A flowchart of a code semantic retrieval method provided according to an embodiment of this specification is shown. Figure 4 A schematic diagram illustrating the execution of a code semantic retrieval method according to an embodiment of this specification is shown. As previously described, the computer system 200 can execute the code semantic retrieval method as a retrieval system.
[0062] like Figure 3 , Figure 4 As shown, code semantic retrieval methods may include: S310: In response to a codebase semantic retrieval request, which includes retrieving query information.
[0063] In some embodiments, a semantic retrieval request for a code library is a retrieval request for code files in a target code library. The retrieved code files can be provided as reference data to a large model so that the large model can refer to the retrieved code files for AI-assisted programming.
[0064] As an example, the target code repository may store code files related to the current software project. These code files may include software function implementation code files, data structure and type definition code files, tool and general capability code files, dependency and interface declaration code files, configuration and rule code files, and architecture and module entry code files, etc.
[0065] Software function implementation code files are the code files that need to be written or revised in a software project. These files can include service class code files, controller code files, core logic code files, etc., and are used to implement functions such as user request processing, service flow orchestration, and data processing. The writing or revision of software function implementation code files must comply with the standards defined in other code files and call the interfaces or tools provided in other code files.
[0066] Data structure and type definition code files are used to define classes, interfaces, structs, enumerations, data models, type annotations, and other related content. These files can include entity code files, type declaration code files, and data structure code files, and are the primary source of code symbols, type signatures, and calling relationships.
[0067] The tool and general capability code files can provide common functions, public methods, and basic components. Tool and general capability code files can include utility class code files, general function code files, and public component code files.
[0068] Dependency and interface declaration code files can be used to declare import and export relationships, external interfaces, third-party dependencies, etc.
[0069] Configuration and rule code files can provide code-based configuration files for project runtime parameters, environment configuration, and rule definitions.
[0070] Architecture and module entry code files can serve as the startup entry point, routing entry point, and integration entry point for modules or systems, and can be used to describe the overall code structure.
[0071] In some embodiments, the search query information can be described by the user in natural language, addressing the needs of code writing. For example, the search system can display a dialog box on a preset page and receive the search query information entered by the user through the dialog box. The search query information can reflect the user's functional implementation requirements, such as "implementing the user login function"; or, the search query information can also reflect the user's defect repair requirements, such as "fixing the problem that the order status cannot be displayed after the user logs in"; or, the search query information can also reflect the user's functional expansion requirements, such as "adding a one-click export function to the interface displaying user orders".
[0072] In some embodiments, the retrieval system may also display multiple interactive interfaces on a preset page, responding to user interactions with these interfaces and generating retrieval query information based on the received interactions. For example, the preset page may include a requirement selection interface, a requirement module selection interface, a function type selection interface, a function description input interface, a constraint input / selection interface, and an associated module selection interface. The retrieval system can generate structured retrieval query information based on the options / input text corresponding to each interactive interface.
[0073] S320: Based on the retrieval query information, perform multi-dimensional semantic matching on each code symbol in the structured meta-information corresponding to the target code library to obtain the relevance score between each code symbol and the retrieval query information. The structured meta-information is generated by parsing the code files of the target code library based on the abstract syntax tree. Multi-dimensional semantic matching is used to calculate the degree of semantic association between the retrieval query information and each code symbol from multiple semantic dimensions.
[0074] In some embodiments, the retrieval system may preprocess the code files in the target code library to obtain the structured meta-information corresponding to the target code library.
[0075] Figure 5 A schematic diagram is shown illustrating an embodiment of this specification for obtaining structured metadata corresponding to a target code library.
[0076] In some embodiments, reference Figure 4 and Figure 5Structured metadata is generated by parsing the code files of the target code library using an abstract syntax tree (AST). From the parsed AST, symbol definitions, call relationships, documentation comments, and type signatures of each code symbol are extracted. Code symbols include at least one of functions, classes, variables, interfaces, and enumerations. Call relationship information represents the logical relationships between multiple code symbols. The structured metadata includes a primary index, an inverted index, and a call graph index. The primary index stores the symbol descriptions of each code symbol, the inverted index stores keyword mappings for documentation comments, and the call graph index stores the call relationships between code symbols.
[0077] As an example, the retrieval system can first determine the corresponding programming language parser based on the extension of each code file in the target code library, and then use the corresponding programming language parser to perform AST parsing on the code file, converting the source code in the code file into an abstract syntax tree.
[0078] For example, the following is a sample of source code used for user login: / ** * User service class, handling user authentication and management. / class UserService { / ** * User login method: Verify username and password and return user information. * @param username Username * @param password @returns User information object / async login(username: string, password: string): Promise <user>{ const hashedPwd = hashPassword(password); const user = await validateUser(username, hashedPwd); return generateToken(user); }” This code snippet is the core user authentication service class written in TypeScript. The JSDoc for this code snippet, " / ** * User service class, handles user authentication and management * / ", describes the purpose of the code.
[0079] The "class UserService {" is used to declare a public class named UserService, which is a container that encapsulates the execution logic of the user service class.
[0080] " / ** * User login method, verifies username and password and returns user information * @param username Username * @param password Password * @returns User information object * / " is a JSDoc-formatted method-level documentation comment that includes a description of the method's functionality, the meaning of its parameters, and an explanation of its return value. `@param` is used to annotate the meaning of the parameters, and `@returns` is used to annotate the meaning of the return value.
[0081] "async login(username: string, password: string): Promise <user>The curly brace "{" is used to declare an asynchronous method "login". "login" takes two parameters and returns a Promise. "async" marks "login" as an asynchronous function, which can use "await" to wait for the asynchronous operation to complete. "username: string" indicates that the first parameter is named "username" and is of type string; "password: string" indicates that the second parameter is named "password" and is of type string. "Promise" is used to declare an asynchronous method "login". <user>This indicates that the return value type is "Promise", and the returned value is a user information object of type "User".
[0082] The `const hashedPwd = hashPassword(password);` statement declares a constant `hashedPwd`, calls the `hashPassword` function to hash the plaintext password (i.e., the value of the parameter `password`) entered by the user, and assigns the encrypted result to `hashedPwd`.
[0083] The function `const user = await validateUser(username, hashedPwd);` declares a constant "user", calls the asynchronous function "validateUser", passes in the username and encrypted password for authentication, and uses "await" to wait for the authentication to complete. The returned user information object is then assigned to "user".
[0084] The function "return generateToken(user);" calls the "generateToken" function, takes the verified user information as input, generates an identity token, and returns the identity token as the return value of the asynchronous method.
[0085] Assuming the above code is stored in the path src / services / UserService.ts, the file extension (.ts) indicates that it is a TypeScript file. For example, the programming language parser for "UserService.ts" could be @typescript-eslint / parser.
[0086] As an example, the abstract syntax tree generated by the retrieval system after parsing the source code in the UserService.ts file can include nodes such as class declaration, method definition, parameter, and call expression, with each node carrying corresponding syntax and semantic information.
[0087] Then, the retrieval system can extract symbol definition information, call relationship information, document comment information, and type signature information from the generated abstract syntax tree.
[0088] As an example, a retrieval system can extract basic information about named entities with independent semantics from an abstract syntax tree as symbol definition information. This symbol definition information can include symbol name, symbol type, the file path to which the symbol belongs, and symbol visibility.
[0089] Taking the above source code as an example, the extractable symbol definition information includes: Code symbol: class; symbol name: UserService; symbol type: class; symbol file path: src / services / UserService.ts; symbol visibility: public by default.
[0090] Code symbol: function; symbol name: login; symbol type: function; symbol class: UserService; symbol visibility: public.
[0091] As an example, the retrieval system can extract the call and called relationships between various code symbols in the source code as call relationship information, which can be used to characterize the logical links between various code symbols in the source code.
[0092] Referring to the example above, the function "login" calls the hashPassword, validateUser, and generateToken methods in sequence. Therefore, the retrieval system can extract the call relationships: UserService.login → hashPassword, UserService.login → validateUser, UserService.login → generateToken. This call relationship information can be recorded using graph data.
[0093] As an example, the retrieval system can extract the JSDoc format documentation comments corresponding to code symbols as documentation comment information, including function descriptions, parameter descriptions, return value descriptions, etc.
[0094] Referring to the example above, the document comments that the retrieval system can extract include the class comment "User service class, handles user authentication and management" and the function "login" comment "User login method, verifies username and password and returns user information".
[0095] As an example, the retrieval system can extract the parameter types and return value types of functions, as well as the type definition information of variables and classes, as type signature information.
[0096] Referring to the example above, the type signature of the function "login" is: (username: string, password: string) → Promise <user>.
[0097] In some embodiments, the retrieval system can generate structured meta-information of the code file corresponding to the source code in the example based on the above symbol definition information, call relationship information, document comment information, and type signature information.
[0098] As an example, the primary index can store complete metadata for all code symbols in a code file. The content corresponding to a code symbol in the primary index can be recorded using a key-value pair. The key is a unique identifier for the code symbol, and the value is all descriptive information about that code symbol. The primary index can be stored in a lightweight local database. For example, a lightweight local database could be an SQLite database.
[0099] For example, the storage content corresponding to UserService.login in the primary index is: key: "src / services / UserService.ts:UserService.login:function" value: { "symbolId": "src / services / UserService.ts:UserService.login:function", "name": "login", "type": "function", "filePath": "src / services / UserService.ts", "visibility": "public", Comment: "User login method, verifies username and password and returns user information", "signature": "(username: string, password: string) =>Promise <user>", "callOut": ["hashPassword", "validateUser", "generateToken"], "callIn": [] } In some embodiments, the key can be a combination of the file path, symbol name, and symbol type corresponding to UserService.login. For example, the file path corresponding to UserService.login is "src / services / UserService.ts"; the symbol name corresponding to UserService.login is "UserService.login"; and the symbol type corresponding to UserService.login is "function". Then the unique identifier of UserService.login can be "src / services / UserService.ts:UserService.login:function".
[0100] In some embodiments, the values may include symbol definition information of the code symbol, such as "unique identifier (symbolId)", "name (name)", "type (function)", "filePath (filePath)", and "visibility (visibility)"; documentation comment information of the code symbol, such as "comment"; type signature information of the code symbol, such as "signature"; and calling relationship information of the code symbol, such as "callOut" and "callIn".
[0101] In some embodiments, the inverted index involves segmenting document comment information to establish a mapping between keywords and unique code symbols. The inverted index can be stored using a custom binary format. For example, it can be stored using a custom binary format employing variable-length integer encoding and bitmap indexing techniques.
[0102] For example, if the document comment information is "user login method, verify username and password and return user information", and one of the keywords is "login", then the retrieval system can create an inverted index entry corresponding to the keyword "login": ["src / services / UserService.ts:UserService:class","src / services / UserService.ts:UserService.login:function"].
[0103] If another keyword in the document comment information is "authentication", the retrieval system can create an inverted index entry corresponding to the keyword "authentication": ["src / services / UserService.ts:UserService:class"].
[0104] In some embodiments, the retrieval system can perform structured processing based on call relationship information to obtain a call graph index. The call graph index records the call relationships between code symbols. It includes multiple nodes, each corresponding to a code symbol, and records the relationships between other code symbols that the code symbol calls (outgoing edges) and the relationships between the code symbol being called by other code symbols (incoming edges). The call graph index can be stored in a JSON file with an adjacency list structure.
[0105] For example, the entry corresponding to UserService.login in the graph index is: { "symbolId": "src / services / UserService.ts:UserService.login:function", "callOut": ["hashPassword", "validateUser", "generateToken"], "callIn": [] } In some embodiments, the retrieval system can refer to the above method to perform abstract syntax tree parsing on the code files in the target code library, and generate a primary index, inverted index, and call graph index corresponding to each code file. Then, the retrieval system can classify and store the primary index, inverted index, and call graph index corresponding to each code file to obtain the structured metadata corresponding to the target code library. The retrieval system can perform abstract syntax tree parsing on multiple code files in parallel.
[0106] Figure 6 A schematic diagram is shown illustrating an embodiment of a target code library file system event listener provided according to this specification.
[0107] In some embodiments, the retrieval system can also monitor file system events of the target codebase to detect changes to code files. File system events include file creation events, file modification events, file deletion events, and file renaming events. When the retrieval system detects a file system event, it can perform content verification on the modified code file to identify the changed code file. Content verification includes calculating the content hash value of the code file and comparing it with the cached historical hash value. Then, the retrieval system can re-execute abstract syntax tree parsing on the modified code file to extract the updated structured metadata. This updated structured metadata is then compared with the cached historical structured metadata to identify the changed code symbols. Finally, the retrieval system can use the updated structured metadata to incrementally update the portion of the index corresponding to the changed code symbols that the multi-dimensional semantic matching relies on.
[0108] As an example, when a retrieval system detects a file system event, it can first determine the specific type of the file system event and then perform content verification on the modified code file.
[0109] For example, when a file system event is a file creation event, meaning a new code file has been added to the target codebase, the retrieval system can obtain the content hash value of the newly added code file and cache it as a historical hash value since the corresponding code did not exist before (i.e., it is not stored in the cached historical hash value). The system can then verify that the newly added code file passes content validation.
[0110] For example, when the file system event is a file modification event or a file renaming event, the retrieval system can obtain the content hash value of the modified or renamed code file and compare it with the cached historical hash value. When the content hash value differs from the historical hash value, the retrieval system can determine that the modified or renamed code file has passed the content verification.
[0111] For example, when a file system event is a file deletion event, it means that a code file has been deleted from the target code repository. In this case, the deleted code file no longer has a content hash value. The retrieval system can remove the historical hash value of the deleted code file and confirm that the deleted code file has passed content verification.
[0112] Then, the retrieval system can extract the updated structured meta-information of the code file after content verification and determine the changed code symbols.
[0113] For example, for newly added code files, the updated structured metadata can be the structured metadata of the entire code file, and the changed code symbols are all the code symbols in the structured metadata of the entire code file. For modified or renamed code files, the updated structured metadata can be the parts that differ from the original structured metadata, and the changed code symbols are the code symbols corresponding to the differing parts. For deleted code files, the updated structured metadata can be considered empty (i.e., no structured metadata exists), and the changed code symbols are all the deleted code symbols.
[0114] Finally, the retrieval system can incrementally update the part of the index corresponding to the changed code symbol in the multi-dimensional semantic matching based on the updated structured meta-information.
[0115] For example, for newly added code files, the retrieval system can write all the structured metadata corresponding to the new code file into the index on which multi-dimensional semantic matching depends. For modified or renamed code files, the retrieval system can replace the metadata of the code symbols that differ from the original structured metadata into the index on which multi-dimensional semantic matching depends. For deleted code files, the retrieval system can remove the structured metadata corresponding to the deleted code file from the index on which multi-dimensional semantic matching depends.
[0116] In some embodiments, the retrieval system may perform abstract syntax tree parsing when code files are added to the database or changed, or it may perform abstract syntax tree parsing when the retrieval system needs to access code files. This specification does not limit this.
[0117] In this embodiment, the retrieval system can automatically detect changes to code files in the target code repository by monitoring file system events. Based on content hash verification, the retrieval system can identify code files that have undergone substantial changes and re-parse and incrementally update the index of these changed code files. In this way, the retrieval system can reduce the computational overhead and time cost of updating the index when code files change, improve the update efficiency of the target code repository, reduce the resource consumption of the retrieval system, and further enhance the practicality and reliability of the retrieval system.
[0118] In this embodiment, the retrieval system parses the code files in the target code repository using an abstract syntax tree, obtaining the primary index, inverted index, and call graph index for each code file. Based on these indexes, the system then generates structured metadata for the target code repository. This approach allows the retrieval system to construct structured metadata combining the primary, inverted, and call graph indexes, fully preserving the syntactic structure and semantic relationships of the source code within the code files, thus providing a reliable data foundation for high-precision semantic retrieval. Furthermore, the structured metadata only stores descriptive and structural information about the code, without including the complete code implementation. This results in a small data volume, low storage cost, and fast index construction and updates, adapting to the rapid iteration needs of the code repository. It can also be deployed locally in a lightweight manner, reducing infrastructure construction costs.
[0119] Figure 7 A schematic diagram illustrating a method for calculating a correlation score according to an embodiment of this specification is shown.
[0120] In some embodiments, reference Figure 7 Multidimensional semantic matching includes at least two of the following: symbol name matching, document comment matching, type signature matching, call relationship matching, file path matching, and decorator matching.
[0121] The retrieval system can perform multi-dimensional semantic matching on each code symbol in the structured meta-information corresponding to the target code library based on the query features in the retrieval query information, and obtain the relevance score of each code symbol to the retrieval query information. Among them, the query features include function name features, function description features, and type information features.
[0122] As an example, suppose the relevance score is The retrieval system can then calculate using the following formula: : (Formula 1) in, The score for matching the symbol name. Weights for matching symbol names; The matching score for document annotations. Weights for matching document comments; The match score for the type signature. Weights for matching type signatures; To retrieve the matching score for the relationship, Weights for matching call relationships; The score for matching file paths. Weights for matching file paths; The matching score for the decorator. Weights for decorator matching.
[0123] In some embodiments, the weights of different matches can be dynamically adjusted based on the search query information. For example, when the search query information contains a specific function name, the weight of symbol name matching can be increased. When the search query information contains a functional description, the weight of document comment matching can be increased. When the search query information contains a specific type, the weight of type signature matching can be increased. .
[0124] As an example, the retrieval system can determine the context factors for each semantic matching dimension based on the query features of the retrieved information. Query features include function name features, function description features, and type information features. The context factors are used to quantify the correlation strength between query features and corresponding semantic matching dimensions. Then, the retrieval system can determine the dynamic weight coefficients for each semantic matching dimension based on the product of preset basic weight coefficients and context factors. Based on the dynamic weight coefficients, the matching scores of each semantic matching dimension are weighted and fused to obtain a relevance score.
[0125] The preset base weight coefficient is the one shown in the example above. "、" "、" "、" "、" "and" ".
[0126] In some embodiments, when multi-dimensional semantic matching has a total of n dimensions, the dynamic weight of the i-th dimension is... It can be calculated using Formula 2: (Formula 2) in, The preset base weight coefficients for the i-th dimension Let be the context factor of the i-th dimension.
[0127] This represents the sum of the products of the preset base weight coefficients and context factors for n dimensions. (Using...) and The ratio of the two values is used as the dynamic weight of the i-th dimension, which can normalize the dynamic weights of multiple dimensions so that the sum of the dynamic weights of the n dimensions equals 1. Here, n is an integer greater than or equal to 2, and i is an integer greater than or equal to 1 and less than or equal to n.
[0128] In some embodiments, the retrieval query information may include query Q. The calculation can be performed according to Formula 3: (Formula 3) in, This is the weight enhancement coefficient. A typical value of 0.5 indicates that the dimensional weights of features are increased by 50%. This indicates whether the feature word of the i-th dimension exists in query Q; if it exists, then... =1, when it does not exist =0.
[0129] For example, suppose there are six dimensions: Dimension 1: symbol name matching, Dimension 2: document comment matching, Dimension 3: type signature matching, Dimension 4: call relationship matching, Dimension 5: file path matching, and Dimension 6: decorator matching. Query Q contains feature words from Dimensions 3 and 6. The retrieval system can calculate the context factor `context_facto` corresponding to these six dimensions as [1, 1, 1.5, 1, 1, 1.5]. Assuming the preset base weight coefficients for these six dimensions are `base_weight` as [0.2, 0.25, 0.2, 0.15, 0.1, 0.1], then... = 0.2×1+0.25×1+0.2×1.5+0.15×1+0.1×1+0.1×1.5=1.1. The dynamic weights corresponding to the 6 dimensions are as follows:
[0130]
[0131]
[0132]
[0133]
[0134]
[0135] In this embodiment, the retrieval system first sets a preset base weight coefficient for each matching dimension, and after receiving the retrieval query information, extracts the dimension-specific feature words contained in the query information. Then, the retrieval system calculates the context factor for each dimension based on the feature word matching results, assigning a higher context factor to dimensions containing feature words. Finally, the retrieval system multiplies the preset base weight of each dimension by the context factor and performs normalization processing to obtain the dynamic weight coefficient of each dimension. In this way, the retrieval system identifies the query intent in the retrieval query information and automatically adjusts the weight allocation of each dimension according to the query intent, enabling the retrieval system to adapt to different types of query needs, thereby effectively improving the relevance and accuracy of the retrieval results.
[0136] In some embodiments, when the retrieval system calculates the relevance score based on query Q and code symbol S, it can be calculated using Formula 4: (Formula 4) in, Let be the scoring function for the i-th dimension.
[0137] In some embodiments, symbol name matching may include at least one of exact matching, fuzzy matching, camelCase splitting matching, and abbreviation matching.
[0138] Among them, exact matching means that the retrieval system compares the query features with the names of the code symbols character by character, and determines that the match is successful if and only if the two are completely identical.
[0139] Fuzzy matching, also known as the edit distance algorithm, allows retrieval systems to calculate the edit distance between query features and the names of code symbols based on the Levenshtein distance. A successful match is determined when the edit distance is less than a preset threshold (usually 2). The Levenshtein distance refers to the minimum number of single-character editing operations (insertion, deletion, replacement) required to transform one string into another.
[0140] In camelCase splitting and matching, the retrieval system can split code symbol names into multiple independent word arrays based on camelCase naming conventions, and then match them with the split words based on query features, supporting partial word matching. As an example, splitting rules may include: using uppercase letters as word separators; treating consecutive uppercase letters as a single word; and using the boundary between numbers and letters as word separators.
[0141] In abbreviation matching, the retrieval system can extract the first letter of each word after camelCase splitting to form a symbol abbreviation, and match the query features with the symbol abbreviation, supporting case-insensitive matching.
[0142] Correspondingly, the matching score for symbol name matching can also be determined based on at least one of exact matching, fuzzy matching, camelCase splitting matching, and abbreviation matching.
[0143] For example, taking exact matching, fuzzy matching, and camelCase splitting matching as examples, the retrieval system can calculate using Formula 5. : (Formula 5) in, For exact match scores, Score weights for exact matching; For fuzzy matching scores, The score weights for fuzzy matching; The matching score for camelCase splitting. The score weights for camelCase splitting and matching.
[0144] Alternatively, when the retrieval system calculates the matching score of symbol names based on query Q and code symbol S, it can also use Formula Six for calculation: (Formula 6) in, This is the score of the highest-scoring match among exact match, fuzzy match, and camelCase match. In the formula above, This represents the name of the code symbol S. Indicates the name of the query Q and code symbol. Edit distance between Indicates the name of the query Q and code symbol. The number of characters in the longest word between them. This indicates the name of the code symbol. The list of words after camelCase splitting. This indicates the number of words in the intersection of the query Q and the list of words after camelCase splitting.
[0145] For example, suppose a query feature in query Q is "logn" (a misspelling of login), the name of the code symbol. The result is "login". Since "logn" and "login" are not exactly the same, an exact match fails, and the score for an exact match is 0. When performing a fuzzy match between "logn" and "login", the edit distance is 1, and the longest word has 5 characters, so the fuzzy match score is 1 - 1 / 5 = 0.8. Simultaneously, there are no matching words in camelCase, meaning the camelCase match score is 0. , .
[0146] In this embodiment, the retrieval system obtains the matching score of symbol names using at least one symbol name matching algorithm. In this way, the retrieval system can accommodate different users' query habits, supporting not only precise symbol name queries but also fuzzy queries, partial queries, and abbreviation queries, effectively improving the problem of pure text retrieval only being able to match literal meanings. Simultaneously, the retrieval system assigns differentiated weights to different symbol name matching algorithms, ensuring that the most relevant code symbols are ranked first, resulting in more accurate and reasonable matching results, thereby improving the user experience and practicality of code retrieval.
[0147] In some embodiments, the matching score of document annotations. It can be calculated using the Term Frequency – Inverse Document Frequency (TF-IDF) formula, i.e., Formula 7: (Formula 7) (Formula 8) (Formula Nine) in, It can be calculated using formula eight. This indicates the frequency of a query feature appearing in a single document comment. This indicates the number of times query feature t appears in document comment d. This indicates the total number of words in the document comment d.
[0148] as well as, It can be calculated using Formula Nine. This indicates the rarity of a certain query feature in the documentation comments of all code files in the target code library, where N represents the total number of code symbols in all code files of the target code library. This represents the number of code symbols containing query feature t. Add 1 to avoid a denominator of 0.
[0149] As an example, when the retrieval system calculates the matching score for document comments based on query Q and code symbol S, it can also use Formula 10 for calculation: (Formula 10) Where t is a query feature in query Q.
[0150] In some embodiments, the retrieval system can determine the type signature matching score based on the parameter type matching score, the return type matching score, and the generic constraint matching score. For example, the type signature matching score can be obtained by weighted summation of the parameter type matching score, the return type matching score, and the generic constraint matching score.
[0151] As an example, when the retrieval system calculates the matching score of the type signature based on the query Q and the code symbol S, it can also use Formula Eleven: (Formula Eleven) in, This indicates the number of overlaps between the type keywords in query Q and the type keywords in code symbols S. This indicates the number of type keywords in query Q.
[0152] As an example, for parameter type matching, suppose the code notation S is: login(username: string, password: string): Promise <user>,but .
[0153] Assuming query Q is: "an asynchronous function that accepts a string parameter and returns a Promise", the retrieval system can extract the following based on query Q: .
[0154] In this case, =3, =3, meaning the parameter type matching score is 3 / 3 = 1 point.
[0155] Assuming the query Q is: "a function that accepts a string parameter and returns a Promise", the retrieval system can extract the following based on query Q: .
[0156] In this case, =2, =2, meaning the parameter type matching score is 2 / 2 = 1 point.
[0157] Assuming the query Q is "functions that accept int parameters", the retrieval system can extract the following based on query Q: .
[0158] In this case, =0, =1, meaning the parameter type matching score is 0 / 1=0.
[0159] The implementation methods for return type matching and generic constraint matching can be referenced from the implementation methods for parameter type matching, and will not be elaborated here.
[0160] In some embodiments, the retrieval system can determine the propagation code symbols and the depth of each called function based on call relationship information (i.e., code call graph) recorded through graph data.
[0161] As an example, the retrieval system can perform call relationship matching based on the code call graph. The retrieval system can propagate upstream along the call relationships between code symbols to determine the propagating code symbols associated with directly matching code symbols and their propagation matching scores. The propagation matching scores are calculated by attenuating with an exponential decay factor based on the propagation depth. A typical value for the exponential decay factor is 0.7.
[0162] For example, calling the match score of the relation match. The following formula (twelve) can be used for calculation: (Formula 12) in, The total matching score is the function called by the code symbol (i.e., the propagating code symbol). The total matching score of the propagating code symbol can be calculated by weighted summation of the matching scores of the propagating code symbol in other dimensions.
[0163] Exponential decay factor To propagate the depth of code symbols, This refers to the propagation matching score of the propagation code symbols.
[0164] As an example, when the retrieval system calculates the matching score for the call relationship based on query Q and code symbol S, it can also use Formula Thirteen for calculation: (Formula Thirteen) in, middle, denoted by , represents the total matching score of all propagating code symbols directly called by code symbol S, and C represents any one of the propagating code symbols directly called by code symbol S.
[0165] This represents the total matching score of the propagation code symbol C. It is an exponential decay factor. To propagate the depth of the code symbol C.
[0166] As an example, referring to the previous example, suppose the code symbol S is the function "login", and its call relationship includes: UserService.login → hashPassword, UserService.login → validateUser, UserService.login → generateToken. Among them, the propagated code symbols include "hashPassword", "validateUser" and "generateToken".
[0167] Since the above propagated code symbols are all represented as one-hop nodes of code symbol S in the code call graph, that is, the depth of each propagated code symbol is 1. Assuming the total matching score of "hashPassword" is 0.7, the total matching score of "validateUser" is 0.8, and the total matching score of "generateToken" is 0.6, and considering the exponential decay factor... =0.7. Therefore, the retrieval system can calculate according to Formula Thirteen: =0.7 0.7 1 +0.8 0.7 1 +0.6 0.7 1 =1.47.
[0168] In some embodiments, the retrieval system may determine the matching score of a file path match based on the directory hierarchy in the file path of the code symbol and the relevance of the directory to the service.
[0169] As an example, when the retrieval system calculates the matching score of file paths based on query Q and code symbol S, it can also use Formula Fourteen for calculation: (Formula Fourteen) Where K is the total number of path levels for code symbol S. This indicates whether the query Q matches the code symbol S at the k-th path level. This represents the weight of the k-th path level, where K is an integer greater than or equal to 1, and k is an integer greater than or equal to 1 and less than or equal to K.
[0170] As an example, referring to the previous example, code symbol S represents the function "login," and its corresponding file path is "src / services / UserService.ts." The file path includes the following hierarchy: "src" (source code root directory), "services" (service code), and "UserService" (user service code). Assuming the query Q is "How to implement user login service," then query Q matches "services" and "UserService" in the path of code symbol S. Assuming the weights of "services" and "UserService" are 0.9 and 0.8 respectively, in this case... =0.9+0.8=1.7.
[0171] In some embodiments, the retrieval system can determine the decorator match score based on the name of the decorator invoked by the code symbol. A decorator is a higher-order function that can be used to modify classes, methods, accessors, properties, or parameters. When a decorator is invoked, it receives decoration parameters from the target object, such as the target to be decorated, key names, property descriptors, etc. The decorator can enhance the functionality of the target object based on the decoration parameters, adding functionality corresponding to the target, and returning a new descriptor to the caller or directly modifying the target object to achieve the enhanced functionality.
[0172] As an example, when the retrieval system calculates the match score of decorator matching based on query Q and code symbol S, it can also be calculated using formula fifteen: (Formula Fifteen) in, This represents the set of decorator names extracted from query Q. This represents the set of decorator names invoked from the code symbol S. This indicates the number of decorator names that appear together in both Q and code symbol S. This indicates the number of decorator names that appear in query Q.
[0173] For example, if query Q includes "API interface", it can be mapped to decorator names such as "@api", "@Controller", "@Post", and "@Get". If code symbol S calls decorators with names including "@Service", "@Controller", and "@Post", then... =2, =4, =2 / 4=0.5.
[0174] In this embodiment, the retrieval system utilizes multi-dimensional semantic matching methods, including symbol name matching, document comment matching, type signature matching, call relationship matching, file path matching, and decorator matching, combined with a dynamic weight adjustment mechanism based on query features, to calculate the relevance score between each code symbol and the retrieved query information. This relevance score can be used to subsequently determine the target code symbol, generate a target context, and perform AI-assisted programming based on that context. The relevance score calculated in this way, by utilizing multiple complementary matching dimensions, reduces retrieval blind spots, allowing the relevance score to more accurately represent the degree of association between the code symbol and the retrieved query information. This results in more accurate determination of the target code symbol and a more precise match between the generated target context and the retrieved query information, leading to higher quality. Consequently, it provides high-quality reference information for AI-assisted programming, significantly improving its accuracy and practicality.
[0175] S330: Based on the relevance scores of each code symbol to the retrieval query information, determine the target code symbol, and perform context mapping based on the target code symbol, the symbol description information corresponding to the target code symbol, and the calling relationship information between the target code symbols to obtain a structured target context. The target context is used to represent the code structure information that is semantically related to the retrieval query information.
[0176] In some embodiments, reference Figure 4 The retrieval system can determine multiple target code symbols based on the relevance scores between each code symbol and the retrieval query information.
[0177] As an example, the retrieval system can divide target code symbols into multiple relevance levels according to a preset relevance score threshold. The relevance levels include high relevance level, medium relevance level and low relevance level. The high relevance level corresponds to target code symbols with a relevance score greater than the first threshold, the medium relevance level corresponds to target code symbols with a relevance score greater than or equal to the second threshold and less than or equal to the first threshold, and the low relevance level corresponds to target code symbols with a relevance score less than the second threshold.
[0178] For example, suppose the first threshold is 0.8 and the second threshold is 0.5. When the relevance score of a target code symbol is greater than 0.8, the retrieval system can determine that the target code symbol's relevance level is high relevance. When the relevance score of a target code symbol is less than or equal to 0.8 and greater than or equal to 0.5, the retrieval system can determine that the target code symbol's relevance level is medium relevance. When the relevance score of a target code symbol is less than 0.5, the retrieval system can determine that the target code symbol's relevance level is low relevance.
[0179] In some embodiments, reference Figure 4 The retrieval system can extract symbol description information corresponding to target code symbols from structured metadata. This symbol description information includes the symbol name, symbol type, function signature, and documentation comments of the target code symbols. Furthermore, the retrieval system can perform context mapping based on multiple relevance levels to generate target contexts. These target contexts include symbol description information corresponding to target code symbols organized according to relevance levels, as well as call relationship information between target code symbols.
[0180] The target context is obtained by performing a simplified mapping process on the target code symbols based on the relevance hierarchy of the target code symbols.
[0181] As an example, suppose the search query is "implement user login function". Based on the above scheme, the retrieval system can obtain the following target code symbols: "UserService", "AuthController" (user authentication), and "auth" (authentication). Among them, "UserService" has a relevance score of 0.9, which is a high relevance level; "AuthController" has a relevance score of 0.7, which is a medium relevance level; and "Auth" has a relevance score of 0.6, which is also a medium relevance level.
[0182] Then, the retrieval system can extract symbol description information corresponding to the target code symbol from the structured metadata and perform context mapping based on the divided multiple relevance levels. The symbol description information corresponding to the target code symbol can come from the metadata of the code file corresponding to the target code symbol in the structured metadata.
[0183] As an example, the retrieval system can obtain the metadata of the code file corresponding to each target code symbol as the symbol description information corresponding to the target code symbol, and further simplify the symbol description information corresponding to each target code symbol.
[0184] For example, the retrieval system can perform symbol-level simplification of the metadata of the code files corresponding to the target code symbols, retaining only the symbol signature and brief comments while deleting the implementation code. Furthermore, the retrieval system can hierarchically truncate the metadata of the code files corresponding to the target code symbols, displaying a maximum of three levels of call relationships. Simultaneously, the retrieval system can limit the number of target code symbols; for example, it can limit the number of code files at the high-relevance level to no more than five, and the total number of files to no more than 15. Finally, the retrieval system can also limit the overall number of tokens in the target context based on a preset token budget (e.g., limiting the number of tokens in the target context to within 2000).
[0185] In some embodiments, the retrieval system can perform context mapping based on the metadata of the code files corresponding to "UserService", "AuthController", and "Auth" to obtain the following target context: Highly relevant documents: src / services / UserService.ts: class UserService / / User service class, handling user authentication and management method login(username: string, password: string): Promise <user> / / User login method, verify username and password and return user information Call: hashPassword, validateUser, generateToken method logout(userId: string): Promise <void> / / User logout method Relevant files: src / controllers / AuthController.ts: class AuthController @Post(' / login') method handleLogin(req: Request, res: Response) / / Controller method that handles login requests Call: UserService.login src / utils / auth.ts: function hashPassword(password: string): string / / Password hash function function validateUser(username: string, password: string): boolean / / Verify user credentials Related type definitions src / types / User.ts: interface User id: string username: string email: string Symbolic calling relationship AuthController.handleLogin→UserService.login; UserService.login→hashPassword; UserService.login→validateUser; UserService.login→generateToken. " Among them, related type definitions are TypeScript interface, type alias, enumeration, class, and other type declarations that have a direct type dependency relationship with target code symbols (such as functions, classes, and methods). Related type definitions can record the specific parameters of the interface required by the code runtime, avoiding the repeated creation of interfaces or the misconfiguration of interface parameters in subsequent AI-assisted programming.
[0186] Furthermore, when the number of tokens in the target context exceeds the limit, the retrieval system may not generate symbolic call relationships to ensure that the generated target context meets the token number limit.
[0187] In some embodiments, when the retrieval system can generate symbolic call relationships, the relationships between multiple target code symbols can be recorded in a visual manner. As an example, the retrieval system can use call symbols to mark function call relationships, inheritance symbols to mark class inheritance relationships, implementation symbols to mark interface implementation relationships, and reference relationships to mark variable reference relationships. For instance, in the symbolic call relationship "UserService.login→validateUser", "→" represents a reference, meaning "UserService.login" references "validateUser".
[0188] In some embodiments, the retrieval system can identify the query type for retrieving information, including function implementation queries, defect location queries, architecture understanding queries, and refactoring optimization queries. Based on the query type, the system adjusts the display priority of symbol description information at each relevant level within the target context, as well as the level of detail in the annotation of call relationships between code symbols.
[0189] For example, suppose the search query is "implement user login function," and the query type is "function implementation query." Accordingly, in a function implementation query, the target context needs to focus more on providing information about function signatures, call relationships, and related utility functions. In this case, the retrieval system can increase the display priority of information about function signatures, call relationships, and related utility functions in the symbolic description information at each relevance level of the target context, and provide more detailed annotation information about call relationships.
[0190] For example, assuming the search query is "user login function error," the query type is "defect localization query." Accordingly, in a defect localization query, the target context needs to focus more on providing information about error codes, exception handling, and log output. In this case, the retrieval system can prioritize the display of information about error codes, exception handling, and log output from the symbolic descriptions at each relevant level of the target context, and provide detailed annotations of call relationships related to error propagation, while briefly annotating normal logical call relationships.
[0191] For example, suppose the search query is "query the structure of the user login module," then the query type is "architecture understanding query." Accordingly, in an architecture understanding query, the target context needs to focus more on providing information about class diagrams, module dependencies, and import / export relationships. In this case, the retrieval system can increase the display priority of information about class diagrams, module dependencies, and import / export relationships in the symbolic description information at each relevant level of the target context, and provide detailed annotations of the dependencies between modules.
[0192] For example, suppose the search query is "optimize the performance of user login function," then the query type is "refactoring and optimization query." Accordingly, in a refactoring and optimization query, the target context needs to focus more on providing information about performance-related annotations and highly complex functions. In this case, the retrieval system can increase the display priority of information about performance-related annotations and highly complex functions in the symbolic description information at each relevance level of the target context, and provide detailed annotations of call relationships involving performance bottlenecks, while briefly annotating call relationships unrelated to performance.
[0193] In this embodiment, the retrieval system can identify the query type corresponding to the retrieval query information and dynamically adjust the display priority of symbol description information at each relevant level in the target context and the level of detail in the annotation of the call relationship between code symbols based on the identified query type. In this way, the retrieval system can deeply adapt the target context information to the query intent in the retrieval query information. It achieves dynamic adjustment of the display focus of the target context for different query intents, prioritizing the display of information more relevant to the query intent. Furthermore, it can effectively reduce the situation where irrelevant or secondary information occupies the token budget, thereby improving the relevance between the target context and the retrieval query information, making the generated target context information more accurate and effective. Moreover, when the retrieval system inputs the target context obtained in this way into the artificial intelligence model, it can provide the artificial intelligence model with higher quality reference information, thereby enabling the output content of the artificial intelligence model to be more accurate and have greater practicality.
[0194] In some embodiments, the retrieval system can dynamically acquire available resources to adjust the token budget for the target context.
[0195] As an example, the retrieval system can obtain the context window budget of the artificial intelligence model and use the context window budget of the artificial intelligence model as the token budget of the adjusted target context.
[0196] Then, the retrieval system can maximize the information value of the target context under the constraint of the target context's token budget, so that the target context carries more valuable information.
[0197] In some embodiments, the retrieval system may, based on the information value and resource consumption of the target code symbols, use the 0-1 knapsack algorithm under the context window budget constraint to determine the selection state of each target code symbol, maximizing the total information value of the target code symbols in the selection state. The information value is determined based on the relevance score and the importance of the target code symbol. The symbol importance is determined based on the logarithm of the sum of the number of times the target code symbol is called and the number of times other code symbols are called, as well as the weight coefficient of the visibility attribute.
[0198] As an example, the information value of the target context This can be expressed by Formula Sixteen: (Formula Sixteen) in, Let s be the set of all object code symbols. The target code symbols in the code. b represents the information value of the target code symbol s. To select variables, This indicates that the target code symbol s is selected. This indicates that the target code symbol s is not selected.
[0199] Furthermore, the calculation formula in Formula Sixteen must be constrained by Formula Seventeen: (Formula 17) in, The number of tokens used after writing target code symbols s into the target context; Budget represents the context window budget.
[0200] It can be determined based on relevance scores and the importance of target code symbols, for example, The calculation can be performed using formula eighteen: (Formula 18) in, The relevance score for the target code symbol s. The importance of object code symbols. As an example, It can be calculated using formula nineteen: (Formula 19) in, This represents the in-degree of the target code symbol s in the code call graph (i.e., how many other code symbols call it). This represents the out-degree of the target code symbol s in the code call graph (i.e., how many other code symbols it calls). The logarithm of the sum of the number of times the target code symbol is called and the number of times other code symbols are called is _____. .as well as, The weighting factor is the visibility attribute of the target code symbol s. This weighting factor can be pre-configured to indicate how useful the code symbol is to the programming process.
[0201] The retrieval system can use formulas 18 and 19, with formula 17 as a constraint, and employ the 0-1 knapsack algorithm to solve for the maximum value of formula 16 through dynamic programming. Then, the retrieval system can rely on... Each target code symbol s corresponds to The value of is used to generate the target context.
[0202] In this embodiment, the retrieval system transforms the problem of selecting target code symbols with higher information value into a 0-1 knapsack problem and solves it using dynamic programming. In this way, with a limited token budget, the retrieval system can prioritize writing target code symbols that are more relevant to the retrieval query and more important in the target code library into the target context, thereby maximizing the information value of the target context. When the target context generated in this way is input into the artificial intelligence model, it can provide higher-quality reference information, thus making the output of the artificial intelligence model more accurate and more practical.
[0203] S340: Input the target context into the artificial intelligence model to obtain the semantic retrieval response output by the artificial intelligence model.
[0204] In some embodiments, the artificial intelligence model can be a general-purpose large language model or a domain-specific large language model fine-tuned for AI-assisted programming. The target context can be used for AI-assisted programming; for example, a retrieval system can generate target prompts based on the retrieval query information and the target context, and input the generated target prompts into the artificial intelligence model. The target prompts can guide the artificial intelligence model to use the target context as a reference to perform AI-assisted programming-level reasoning on the retrieval query information, execute AI-assisted programming tasks, and generate corresponding semantic retrieval responses.
[0205] In this scenario, after inputting the target context into the AI model, the model can retrieve the query type corresponding to the query information and output the appropriate semantic retrieval response. For example, for function implementation queries and refactoring optimization queries, the semantic retrieval response could be the generated code file; for defect location queries, the semantic retrieval response could be error messages, error correction guidance, etc.; and for architecture understanding queries, the semantic retrieval response could be the architecture information of the corresponding module, etc.
[0206] In summary, the code semantic retrieval method and system provided in this specification involve the retrieval system responding to a code library semantic retrieval request. Based on the retrieval query information in the request, the system performs multi-dimensional semantic matching on the structured meta-information corresponding to the code files in the target code library, obtaining a relevance score between each code symbol and the retrieval query information. The structured meta-information corresponding to the code files in the target code library is obtained by parsing the code files of the target code library using an abstract syntax tree. Then, the retrieval system determines the target code symbols based on the relevance scores and performs context mapping by combining the symbol description information corresponding to the target code symbols and the call relationship information between the target code symbols, obtaining a structured target context representing the code structure information semantically related to the retrieval query information. Finally, the retrieval system uses the target context and an artificial intelligence model to obtain the semantic retrieval response corresponding to the code library semantic retrieval request. In this way, the retrieval system can quantify the semantic association between the retrieval query information and code symbols from multiple semantic dimensions, improving upon the limitations of pure text retrieval which only supports literal matching and symbol indexing which only supports exact queries, effectively improving the accuracy and recall of code retrieval. Furthermore, the retrieval system utilizes context mapping to obtain the target context. When guiding AI model reasoning based on the target context, this enables the AI model to fully understand the structural information of the code, ensuring that the output meets the architecture and specifications of the target code library. It also filters redundant information, improving the reasoning efficiency of the AI model. The solution presented in this specification eliminates the need for high-cost infrastructure such as vector databases and embedding models. High-precision semantic retrieval can be achieved based on structured meta-information parsed from abstract syntax trees, significantly reducing deployment and maintenance costs. It provides a code retrieval solution that balances low cost, high retrieval accuracy, and strong versatility.
[0207] This specification, in another aspect, provides a computer-readable non-volatile storage medium storing at least one instruction set for retrieving data from a target code library based on code semantics. When the at least one instruction set is executed by a processor, it instructs the processor to perform the steps of the code semantic retrieval method described herein. In some possible implementations, various aspects of this specification can also be implemented as a program product comprising program code. When the program product is run on a computer system 200, the program code causes the computer system 200 to perform the steps of the code semantic retrieval method described herein. The program product for implementing the above method may employ a portable compact disk read-only memory (CD-ROM) containing program code and may run on the computer system 200. However, the program product of this specification is not limited thereto. In this specification, the readable storage medium may be any tangible medium containing or storing a program that may be used by or in conjunction with an instruction execution system. The program product may employ any combination of one or more readable media. The readable medium may be a readable signal medium or a readable storage medium. The readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media include: electrical connections having one or more wires, portable disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. The computer-readable storage medium may include data signals propagated in baseband or as part of a carrier wave, carrying readable program code. Such propagated data signals may take various forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination thereof. A readable storage medium may also be any readable medium other than a readable storage medium that can send, propagate, or transmit programs for use by or in connection with an instruction execution system, apparatus, or device. Program code contained on a readable storage medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof. Program code for performing the operations described herein can be written in any combination of one or more programming languages, including object-oriented programming languages such as Java and C++, and conventional procedural programming languages such as C or similar languages. The program code can be executed entirely on computer system 200, partially on computer system 200, as a standalone software package, partially on computer system 200 and partially on a remote computing device, or entirely on a remote computing device.
[0208] The foregoing has described specific embodiments of this specification. Other embodiments are within the scope of the appended claims. In some cases, the actions or steps recited in the claims may be performed in a different order than that shown in the embodiments and may still achieve the desired result. Furthermore, the processes depicted in the drawings do not necessarily require a specific or sequential order to achieve the desired result. In some embodiments, multitasking and parallel processing are possible or may be advantageous.
[0209] In summary, after reading this detailed disclosure, those skilled in the art will understand that the foregoing detailed disclosure is presented by way of example only and is not restrictive. Although not explicitly stated herein, those skilled in the art will understand that this specification requires various reasonable changes, improvements, and modifications to the embodiments. These changes, improvements, and modifications are intended to be made by this specification and are within the spirit and scope of the exemplary embodiments described herein.
[0210] Furthermore, certain terms in this specification have been used to describe embodiments of this specification. For example, "an embodiment," "an embodiment," and / or "some embodiments" mean that a particular feature, structure, or characteristic described in connection with that embodiment may be included in at least one embodiment of this specification. Therefore, it is to be emphasized and understood that two or more references to "an embodiment" or "an embodiment" or "alternative embodiment" in various parts of this specification do not necessarily refer to the same embodiment. Moreover, specific features, structures, or characteristics may be suitably combined in one or more embodiments of this specification.
[0211] It should be understood that in the foregoing description of the embodiments in this specification, various features are combined in a single embodiment, drawing, or description for the purpose of simplifying the description and to aid in understanding a feature. However, this does not mean that the combination of these features is necessary, and those skilled in the art, upon reading this specification, may readily identify some of the devices as separate embodiments. That is, the embodiments in this specification can also be understood as an integration of multiple secondary embodiments. And the content of each secondary embodiment is valid even if it contains fewer than all the features of a single foregoing disclosed embodiment.
[0212] Every patent, patent application, publication of a patent application, and other material such as articles, books, specifications, publications, documents, articles, etc., cited herein, except for those inconsistent with or conflicting with this document, or those having a restrictive effect on the widest scope of the claims, may be incorporated herein by reference for all purposes now or hereafter associated with this document. Furthermore, in the event of any inconsistency or conflict between the description, definition, and / or use of relevant terms in any material and the description, definition, and / or use of relevant terms in this document, the terms in this document shall prevail.
[0213] Finally, it should be understood that the embodiments disclosed herein are illustrative of the principles of the embodiments described in this specification. Other modified embodiments are also within the scope of this specification. Therefore, the embodiments disclosed in this specification are merely examples and not limitations. Those skilled in the art can implement the applications described in this specification using alternative configurations based on the embodiments in this specification. Therefore, the embodiments in this specification are not limited to the embodiments precisely described in the applications.< / void> < / user> < / user> < / user> < / user> < / user> < / user> < / user>
Claims
1. A code semantic retrieval method, comprising: In response to a semantic retrieval request for a code repository, the semantic retrieval request for a code repository includes: retrieving query information; Based on the retrieval query information, multi-dimensional semantic matching is performed on each code symbol in the structured meta-information corresponding to the target code library to obtain the relevance score between each code symbol and the retrieval query information. The structured meta-information is generated by parsing the code files of the target code library based on an abstract syntax tree. The multi-dimensional semantic matching is used to calculate the degree of semantic association between the retrieval query information and each code symbol from multiple semantic dimensions. Based on the relevance scores of each code symbol to the retrieval query information, target code symbols are determined. Then, based on the target code symbols, their corresponding symbol descriptions, and the call relationship information between the target code symbols, context mapping is performed to obtain a structured target context. This target context represents code structure information semantically related to the retrieval query information. The target context is input into the artificial intelligence model to obtain the semantic retrieval response output by the artificial intelligence model.
2. The method according to claim 1, wherein, The structured metadata is generated by parsing the code files of the target code library using an abstract syntax tree, and extracting symbol definition information, call relationship information between code symbols, documentation comment information, and type signature information from the parsed abstract syntax tree. The code symbols include at least one of functions, classes, variables, interfaces, and enumerations. The call relationship information is used to characterize the logical relationship between multiple code symbols. as well as The structured metadata includes a main index, an inverted index, and a call graph index. The main index is used to store symbol description information for each code symbol, the inverted index is used to store keyword mappings for document comment information, and the call graph index is used to store call relationships between code symbols.
3. The method according to claim 1, wherein, The multi-dimensional semantic matching includes at least two of the following: symbol name matching, document comment matching, type signature matching, call relationship matching, file path matching, and decorator matching.
4. The method according to claim 1, wherein, The call relationship matching is based on the code call graph, and propagates upstream along the call relationship between symbols to determine the propagated code symbols associated with the directly matched code symbols and the propagation matching score. The propagation matching score is calculated by decaying the propagation depth using an exponential decay factor.
5. The method according to claim 1, wherein, Based on the retrieval query information, multi-dimensional semantic matching is performed on each code symbol in the structured meta-information corresponding to the target code library to obtain a relevance score between each code symbol and the retrieval query information, including: Based on the query features of the retrieved query information, context factors for each semantic matching dimension are determined. The query features include function name features, function description features, and type information features. The context factors are used to quantify the correlation strength between the query features and the corresponding semantic matching dimensions. Based on the product of preset base weight coefficients and the context factors, the dynamic weight coefficients for each semantic matching dimension are determined; and Based on the dynamic weight coefficients, the matching scores of each semantic matching dimension are weighted and fused to obtain the relevance score.
6. The method according to claim 1, wherein, The step of performing context mapping based on the target code symbols, the symbol description information corresponding to the target code symbols, and the call relationship information between the target code symbols to obtain a structured target context includes: The symbol description information corresponding to the target code symbol is extracted from the structured metadata. The symbol description information includes the symbol name, symbol type, function signature, and documentation comments of the target code symbol. Based on a preset relevance score threshold, the target code symbols are divided into multiple relevance levels, including a high relevance level, a medium relevance level, and a low relevance level. The high relevance level corresponds to target code symbols with a relevance score greater than a first threshold; the medium relevance level corresponds to target code symbols with a relevance score greater than or equal to a second threshold and less than or equal to the first threshold; and the low relevance level corresponds to target code symbols with a relevance score less than the second threshold. Based on the multiple relevance levels after division, context mapping is performed to generate the target context. The target context includes symbol description information corresponding to the target code symbols organized according to the relevance levels, as well as calling relationship information between the target code symbols.
7. The method according to claim 6, wherein, Also includes: Identify the query type of the retrieved query information, which includes function implementation query, defect location query, architecture understanding query, and refactoring and optimization query; as well as Based on the query type, adjust the display priority of symbol description information at each relevance level in the target context, as well as the level of detail in the annotation of call relationships between code symbols.
8. The method according to claim 6, wherein, Also includes: Obtain the context window budget of the artificial intelligence model; as well as Based on the information value and resource consumption of the target code symbols, under the context window budget constraint, the 0-1 knapsack algorithm is used to solve the problem, determine the selection state of each target code symbol, and maximize the total information value of the target code symbols in the selection state. The information value is determined according to the relevance score and the importance of the target code symbol. The symbol importance is determined according to the logarithm of the sum of the number of times the target code symbol is called and the number of times other code symbols are called, as well as the weight coefficient of the visibility attribute.
9. The method according to claim 2, further comprising: Listen to file system events of the target code repository to detect changes to code files. The file system events include file creation events, file modification events, file deletion events, and file renaming events. The modified code files are subject to content verification to identify the modified code files whose content has changed. The content verification includes calculating the content hash value of the code file and comparing it with the cached historical hash value. The abstract syntax tree is re-executed on the modified code file to extract the updated structured meta-information. The updated structured meta-information is then compared with the cached historical structured meta-information to identify the modified code symbols. as well as Based on the updated structured meta-information, the portion of the index corresponding to the changed code symbol in the index upon which the multi-dimensional semantic matching depends is incrementally updated.
10. The method according to claim 2, wherein, The primary index is stored in a lightweight local database, which includes an SQLite database. The inverted index is stored in a custom binary format, which uses variable-length integer encoding and bitmap indexing technology. The call graph index is stored in JSON format with an adjacency table structure.
11. A retrieval system, comprising: At least one storage medium storing at least one instruction set for retrieval in a target code library based on code semantics; as well as At least one processor is communicatively connected to the at least one storage medium, wherein, when the retrieval system is running, the at least one processor reads the at least one instruction set and implements the method as described in any one of claims 1-10 according to the instructions of the at least one instruction set.
12. A computer-readable non-volatile storage medium, wherein, The computer-readable non-volatile storage medium stores at least one instruction set, which, when executed by at least one processor, implements the method as described in any one of claims 1-10.