An AI programming-oriented code semantic understanding and hybrid retrieval question and answer method and system
Patent Information
- Application Number
- CN202511965845.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-24
- Publication Date
- 2026-09-22
- Estimated Expiration
- 2045-12-24
AI Technical Summary
[0003]然而,这些方法均要求使用者预先知晓代码中确切的命名或结构,无法直接以自然语言描述编程意图进行查询
本公开的示例实施例中的面向AI编程的代码语义理解与混合检索问答方法,一方面,通过解析目标代码库并构建包含语义向量索引、符号倒排索引和结构关系索引的多维度代码索引,使得系统能够将代码单元的功能语义、形式化符号和结构关联进行统一表征,进而当接收自然语言形式的代码查询问题时,系统可以并行执行语义检索、符号检索和结构检索操作,从而可以实现对开发者自然语言表述的编程意图的深度理解,无需用户预先知晓代码中确切的命名或结构即可进行查询,可以降低代码检索对特定术语记忆经验的依赖,避免传统工具依赖关键词匹配或精确符号查找的局限,可以提高查询的灵活性和易用性;另一方面,通过对语义检索、符号检索和结构检索得到的多个候选结果集进行基于排名的融合处理,并进一步利用语义评估模型对初步融合结果集进行语义相关性评估与重新排序,使得系统能够在多路径检索基础上自动优化结果优先级,可以筛选出语义契合度最高的代码单元,从而有助于提升检索结果的准确性和相关性,避免生成内容可能脱离实际意图的问题,可以减少表面相似但功能不符的噪声干扰,能够增强答案与用户真实需求的一致性;再一方面,基于重排序后的候选结果集生成结构化答案并进行交叉验证,使得系统能够从多个独立维度对答案的相关性、完整性和源码一致性进行系统性检查,可以确保生成内容与目标代码库的实际实现保持可靠关联,通过引入多维度代码索引、多路径检索结果约束、语义重排序以及交叉验证闭环机制,使生成式模型的输出过程始终受限于目标代码库的真实结构与实现逻辑,从而避免仅基于语言模型生成内容所可能产生的脱离实际代码实现的“幻觉”问题,从而能够显著提升答案的可信度和准确性。
Smart Images

Figure CN122064775B_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of software development technology, and more specifically, to a code semantic understanding and hybrid retrieval question answering method and system for AI programming. Background Technology
[0002] During code development and maintenance, developers often need to quickly understand the functionality and implementation of project code. Currently, the common practice is to use keyword-based text search tools or the symbol lookup function built into the integrated development environment (IDE).
[0003] However, these methods all require users to know the exact names or structure of the code in advance, making it impossible to directly describe the programming intent in natural language for querying. In recent years, although intelligent programming assistants capable of understanding natural language and generating answers have emerged, the content they generate often lacks a reliable correlation with the actual implementation of a specific codebase, making it difficult to ensure the accuracy of the answers. Therefore, how to enable code question-answering systems to both understand the intent expressed by developers in natural language and ensure that the provided answers are consistent and reliable with the target codebase has become a pressing technical problem to be solved.
[0004] It should be noted that the information disclosed in the background section above is only used to enhance the understanding of the background of this disclosure, and therefore may include information that does not constitute prior art known to those skilled in the art. Summary of the Invention
[0005] The purpose of this disclosure is to provide a code semantic understanding and hybrid retrieval question answering method and system for AI programming, thereby avoiding the "illusion" problem that may arise from generating content solely based on language models and deviating from actual code implementation, thus significantly improving the credibility and accuracy of the answers.
[0006] Other features and advantages of this disclosure will become apparent from the following detailed description, or may be learned in part from practice of this disclosure.
[0007] According to a first aspect of the present disclosure, a method for code semantic understanding and hybrid retrieval question answering for AI programming is provided, including: The target code library is parsed to construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. The system receives a code query question in natural language form, and based on the code query question and the multi-dimensional code index, performs retrieval operations in parallel, including at least semantic retrieval, symbol retrieval and structural retrieval, to obtain multiple candidate result sets. The multiple candidate result sets are fused without parameters based on ranking to generate a preliminary fusion result set; The code query question and the preliminary fusion result set are input into a semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and reorders them, and outputs the reordered candidate result set. Cross-validation is performed based on the reordered candidate result set, and the structured answer after successful cross-validation is output.
[0008] In some example embodiments of this disclosure, based on the foregoing scheme, the construction of a multi-dimensional code index includes: The source code in the target code library is parsed by a preset syntax parser to extract code units in the form of functions or classes. The code units include docstrings, function signatures, internal calls and dependency information. A multi-dimensional code index is constructed based on the code units.
[0009] In some example embodiments of this disclosure, based on the foregoing scheme, the construction of a multi-dimensional code index based on the code unit includes: Based on the trained code semantic model, each code unit is converted into a semantic vector, and the semantic vector index is constructed using the semantic vector. Extract the named body information from each code unit, and construct the symbol inverted index using the named body information; Based on the calling relationships, inheritance relationships, and module dependencies between the code units, a graph structure data is constructed, and a structural relationship index is formed based on the graph structure data.
[0010] In some example embodiments of this disclosure, based on the foregoing scheme, the plurality of candidate result sets include a first candidate result set under the semantic retrieval path, a second candidate result set under the symbol retrieval path, and a third candidate result set under the structure retrieval path; Based on the code query question and the multi-dimensional code index, the system performs retrieval operations in parallel, including at least semantic retrieval, symbol retrieval, and structural retrieval, to obtain multiple candidate result sets, including: The code query question is converted into a query vector, the similarity between the query vector and each semantic vector in the semantic vector index is calculated, and the first candidate result set is obtained based on the similarity ranking. Keywords are extracted from the code query question, and code units corresponding to named body information containing the keywords or fuzzily matching the keywords are matched based on the symbol inverted index to obtain the second candidate result set; Starting with the code units in the first candidate result set and / or the second candidate result set, a bounded traversal is performed on the graph structure data represented by the structural relationship index to obtain the associated code units that have a calling, inheritance or dependency relationship with the starting point, thus forming the third candidate result set.
[0011] In some example embodiments of this disclosure, based on the foregoing scheme, the step of performing ranking-based parameterless fusion of the plurality of candidate result sets to generate a preliminary fusion result set includes: For each candidate code unit that appears in the candidate result set, obtain multiple ranking data of the candidate code unit in each candidate result set; Summing the reciprocals of each ranking data point yields the weighted fusion score for each candidate code unit. All candidate code units are reordered based on the weighted fusion score to generate the preliminary fusion result set.
[0012] In some example embodiments of this disclosure, based on the foregoing scheme, the semantic evaluation model performs semantic relevance evaluation and reordering on the candidate code units in the preliminary fusion result set, and outputs a reordered candidate result set, including: The code query question is combined with each candidate code unit in the preliminary fusion result set to obtain evaluation input data; The evaluation input data is input into the semantic evaluation model to obtain an evaluation score that characterizes the semantic relevance between each candidate code unit and the code query question; All candidate code units are sorted in descending order based on the evaluation scores, and candidate code units whose evaluation scores are less than or equal to a preset score threshold are filtered out to obtain the reordered candidate result set.
[0013] In some example embodiments of this disclosure, based on the foregoing scheme, the step of performing cross-validation based on the reordered candidate result set and outputting the structured answer after successful cross-validation includes: The reordered candidate result set is converted into a structured answer, and the code query question and the structured answer are cross-validated to obtain the cross-validation results. If the cross-validation result is passed, then output the structured answer after successful cross-validation; If the cross-validation result is unsuccessful, a new structured answer is generated based on the reordered candidate result set, and the new structured answer is cross-validated again until the cross-validation result of the latest structured answer is successful, then the latest structured answer is output.
[0014] In some example embodiments of this disclosure, based on the foregoing scheme, the cross-validation of the code query question and the structured answer to obtain the cross-validation result includes: The relevance of the code query question and the structured answer is verified to obtain the relevance verification results; The integrity of the code query question and the structured answer is verified to obtain the integrity verification result; The source code of the code query question and the structured answer are matched and verified to obtain the source code matching and verification results. The code query question and the structured answer are subjected to code logic verification to obtain the code logic verification results; The cross-validation result is determined based on the correlation verification result, the integrity verification result, the source code matching verification result, and the code logic verification result.
[0015] In some example embodiments of this disclosure, based on the foregoing scheme, the relevance verification of the code query question and the structured answer to obtain the relevance verification result includes: The code query question and the structured answer are input into a trained semantic matching model, which calculates the semantic consistency score between the code query question and the structured answer. If the semantic consistency score is less than or equal to the preset consistency score threshold, the relevance verification result is determined to be unsuccessful. If the semantic consistency score is greater than the preset consistency score threshold, the relevance verification result is determined to be passed.
[0016] In some example embodiments of this disclosure, based on the foregoing scheme, the step of performing integrity verification on the code query question and the structured answer to obtain the integrity verification result includes: The structured answer is parsed using a preset syntax parser, and an abstract syntax tree is constructed in an attempt. If a grammatical error is found in the structured answer during the construction of the abstract syntax tree, or if the grammatical structure of the structured answer is determined to be incomplete, then the integrity verification result is determined to be unsuccessful; otherwise, the integrity verification result is determined to be successful.
[0017] In some example embodiments of this disclosure, based on the foregoing scheme, the step of performing source code matching verification on the code query question and the structured answer to obtain the source code matching verification result includes: Extract the identification information of the referenced target code units from the structured answer; Based on the symbol inverted index and / or the semantic vector index in the multi-dimensional code index, search for code units in the target code library that match the identification information; If no matching code unit is found, the source code matching verification result is determined to be unsuccessful; otherwise, the source code matching verification result is determined to be successful.
[0018] In some example embodiments of this disclosure, based on the foregoing scheme, the step of performing code logic verification on the code query question and the structured answer to obtain the code logic verification result includes: The code query question and the structured answer are input into an independent validation language model, which outputs an evaluation result on the logical correctness and / or internal consistency of the structured answer. If the evaluation result is "fail", then the code logic verification result is determined to be "fail"; otherwise, the code logic verification result is determined to be "pass".
[0019] In some example embodiments of this disclosure, based on the foregoing scheme, determining the cross-validation result according to the relevance verification result, the integrity verification result, the source code matching verification result, and the code logic verification result includes: The relevance verification results, the integrity verification results, the source code matching verification results, and the code logic verification results are quantified into standardized scores. The standardized score is weighted according to the preset verification weights to calculate the comprehensive confidence score. If the overall confidence score is less than or equal to the preset confidence threshold, then the cross-validation result is determined to be unsuccessful. If the overall confidence score is greater than the preset confidence threshold, then the cross-validation result is determined to be passed.
[0020] According to a second aspect of the present disclosure, a code semantic understanding and hybrid retrieval question answering device for AI programming is provided, comprising: The code parsing and indexing module is used to parse the target code library and construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. The hybrid retrieval module is used to receive code query questions in natural language form, and based on the code query questions and the multi-dimensional code index, to perform retrieval operations in parallel, including at least semantic retrieval, symbol retrieval and structural retrieval, to obtain multiple candidate result sets; The fusion and sorting module is used to perform ranking-based parameterless fusion of the multiple candidate result sets to generate a preliminary fusion result set; The semantic reordering module is used to input the code query question and the preliminary fusion result set into the semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates and reorders the candidate code units in the preliminary fusion result set and outputs the reordered candidate result set. The answer generation and verification module is used to perform cross-validation based on the reordered candidate result set and output the structured answer after successful cross-validation.
[0021] According to a third aspect of the present disclosure, an electronic device is provided, comprising: a processor; and a memory storing computer-readable instructions, wherein the computer-readable instructions, when executed by the processor, implement the code semantic understanding and hybrid retrieval question answering method for AI programming as described in the first aspect.
[0022] According to a fourth aspect of the present disclosure, a computer-readable storage medium is provided having a computer program stored thereon, which, when executed by a processor, implements the code semantic understanding and hybrid retrieval question answering method for AI programming as described in the first aspect.
[0023] The technical solutions provided in this disclosure may have the following beneficial effects: The code semantic understanding and hybrid retrieval question answering method for AI programming disclosed in the example embodiments of this invention, on the one hand, by parsing the target code library and constructing a multi-dimensional code index including a semantic vector index, a symbol inverted index, and a structural relation index, enables the system to uniformly represent the functional semantics, formal symbols, and structural associations of code units. Therefore, when receiving code query questions in natural language form, the system can perform semantic retrieval, symbol retrieval, and structural retrieval operations in parallel, thereby achieving a deep understanding of the programming intent expressed by the developer in natural language. This allows for queries without requiring the user to know the exact names or structures in the code beforehand, reducing the reliance on memorized terminology for code retrieval and avoiding the limitations of traditional tools that rely on keyword matching or precise symbol lookup, thus improving the flexibility and ease of use of queries. On the other hand, by performing ranking-based fusion processing on multiple candidate result sets obtained from semantic retrieval, symbol retrieval, and structural retrieval, and further utilizing a semantic evaluation model to assess the semantic relevance of the preliminary fusion result set, the system achieves this. The reordering process enables the system to automatically optimize result priority based on multi-path retrieval, selecting code units with the highest semantic fit. This helps improve the accuracy and relevance of retrieval results, avoids the problem of generated content deviating from actual intent, reduces noise interference from superficially similar but functionally inconsistent results, and enhances the consistency between answers and users' real needs. Furthermore, generating structured answers based on the reordered candidate result set and performing cross-validation allows the system to systematically check the relevance, completeness, and source code consistency of answers from multiple independent dimensions. This ensures that the generated content maintains a reliable connection with the actual implementation of the target codebase. By introducing multi-dimensional code indexing, multi-path retrieval result constraints, semantic reordering, and a cross-validation closed-loop mechanism, the output process of the generative model is always constrained by the actual structure and implementation logic of the target codebase. This avoids the "illusion" problem that may arise from generating content solely based on a language model, thus significantly improving the credibility and accuracy of the answers.
[0024] This disclosure addresses a common technical contradiction in code question-answering systems in related technologies, namely the difficulty in simultaneously achieving code recall coverage, answer accuracy, and result credibility in practical engineering applications. It proposes a holistic solution based on code semantic understanding and a hybrid retrieval strategy. Unlike solutions that simply superimpose multiple retrieval or generation technologies, this disclosure does not focus on localized improvements to a single stage. Instead, it constructs a comprehensive technical mechanism that spans the entire retrieval, generation, and verification process through the synergistic cooperation of technical features such as multi-path result constraints, parameter-free fusion, semantic reordering, and verification closed-loop.
[0025] Specifically, this solution employs multiple paths, including semantic retrieval, symbolic retrieval, and structural retrieval, to constrain candidate code units in parallel. This ensures high recall coverage while preventing excessive expansion of the candidate range. Furthermore, through parameter-free fusion and semantic re-ranking mechanisms, candidate results are prioritized for implementations of real code highly consistent with the query intent. Further, multi-dimensional cross-validation is introduced to form a validation loop, continuously verifying and providing feedback on the generative model output, thus strictly binding the question-answering results to the actual structure and logic of the target codebase. Through the synergistic effect of these technical features, this disclosure achieves high recall, high accuracy, and high reliability in code question answering at the overall level, overcoming the limitations of mutual constraints between different technical objectives in related technologies. This overall technical effect is not achieved independently by any single technical feature, but rather is a comprehensive effect resulting from the cooperation and mutual constraints of various technical features under a unified technical concept, thus demonstrating the holistic innovation of this solution compared to related technologies.
[0026] It should be understood that the above general description and the following detailed description are exemplary and explanatory only, and are not intended to limit this disclosure. Attached Figure Description
[0027] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this disclosure and, together with the description, serve to explain the principles of this disclosure. It is obvious that the drawings described below are merely some embodiments of this disclosure, and those skilled in the art can obtain other drawings based on these drawings without any inventive effort.
[0028] Figure 1 The illustration shows a flowchart of a code semantic understanding and hybrid retrieval question answering method for AI programming according to some embodiments of the present disclosure.
[0029] Figure 2 The illustration shows a flowchart of constructing a multi-dimensional code index according to some embodiments of the present disclosure.
[0030] Figure 3 The illustration shows a flowchart of filtering a candidate result set by multi-dimensional code indexing according to some embodiments of the present disclosure.
[0031] Figure 4 The illustration shows a flowchart of fusing multiple candidate result sets to generate a preliminary fused result set according to some embodiments of the present disclosure.
[0032] Figure 5 The illustration schematically depicts a flowchart of cross-validation of code query questions and structured answers according to some embodiments of the present disclosure.
[0033] Figure 6 The illustration shows a schematic diagram of the composition of a code semantic understanding and hybrid retrieval question answering system for AI programming according to some embodiments of the present disclosure.
[0034] Figure 7 The schematic diagram illustrates the structural schematic of a computer system of an electronic device according to some embodiments of the present disclosure.
[0035] Figure 8 A schematic diagram of a computer-readable storage medium according to some embodiments of the present disclosure is shown.
[0036] In the accompanying drawings, the same or corresponding reference numerals indicate the same or corresponding parts. Detailed Implementation
[0037] Exemplary embodiments will now be described in detail, examples of which are illustrated in the accompanying drawings. When the following description relates to the drawings, unless otherwise indicated, the same numerals in different drawings denote the same or similar elements. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with this specification. Rather, they are merely examples of apparatuses and methods consistent with some aspects of this specification as detailed in the appended claims.
[0038] The terminology used in this specification is for the purpose of describing particular embodiments only and is not intended to be limiting of this specification. The singular forms “a,” “the,” and “the” as used in this specification and the appended claims are also intended to include the plural forms unless the context clearly indicates otherwise. It should also be understood that the term “and / or” as used herein refers to and includes any and all possible combinations of one or more of the associated listed items.
[0039] It should be understood that although the terms first, second, third, etc., may be used in this specification to describe various information, this information should not be limited to these terms. These terms are only used to distinguish information of the same type from one another. For example, without departing from the scope of this specification, first information may also be referred to as second information, and similarly, second information may also be referred to as first information. Depending on the context, the word "if" as used herein may be interpreted as "when," "when," or "in response to determination."
[0040] Furthermore, the described features, structures, or characteristics can be combined in any suitable manner in one or more embodiments. Numerous specific details are provided in the following description to give a thorough understanding of embodiments of this disclosure. However, those skilled in the art will recognize that the technical solutions of this disclosure can be practiced without one or more of the specific details, or other methods, components, apparatuses, steps, etc., can be employed. In other instances, well-known methods, apparatuses, implementations, or operations are not shown or described in detail to avoid obscuring various aspects of this disclosure.
[0041] Furthermore, the accompanying drawings are for illustrative purposes only and are not necessarily drawn to scale. The block diagrams shown in the drawings are merely functional entities and do not necessarily correspond to physically independent entities. That is, these functional entities can be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0042] In this embodiment, a code semantic understanding and hybrid retrieval question answering method for AI programming is first provided. This method can be applied to terminal devices, such as mobile phones, computers and other electronic devices, or to servers. This embodiment does not make any special limitations on this, and the following description will take the server executing the method as an example. Figure 1 The illustration schematically depicts a flowchart of a code semantic understanding and hybrid retrieval question-answering method for AI programming according to some embodiments of this disclosure. Reference Figure 1 As shown, this code semantic understanding and hybrid retrieval question answering method for AI programming may include the following steps: Step S110: Parse the target code library and construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. Step S120: Receive a code query question in natural language form; based on the code query question and the multi-dimensional code index, perform retrieval operations in parallel, including at least semantic retrieval, symbol retrieval, and structural retrieval, to obtain multiple candidate result sets; Step S130: Perform ranking-based parameterless fusion on the multiple candidate result sets to generate a preliminary fusion result set; Step S140: Input the code query question and the preliminary fusion result set into the semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and reorders them, and outputs the reordered candidate result set. Step S150: Perform cross-validation based on the reordered candidate result set, and output the structured answer after successful cross-validation.
[0043] According to the code semantic understanding and hybrid retrieval question answering method for AI programming in this example embodiment, on the one hand, by parsing the target code library and constructing a multi-dimensional code index including semantic vector index, symbol inverted index, and structural relation index, the system can uniformly represent the functional semantics, formal symbols, and structural associations of code units. Therefore, when receiving code query questions in natural language form, the system can perform semantic retrieval, symbol retrieval, and structural retrieval operations in parallel, thereby achieving a deep understanding of the programming intent expressed by the developer in natural language. Users can perform queries without prior knowledge of the exact names or structures in the code, reducing the reliance on memorized terminology for code retrieval and avoiding the limitations of traditional tools that rely on keyword matching or precise symbol lookup, thus improving the flexibility and ease of use of queries. On the other hand, by analyzing multiple candidate result sets obtained from semantic retrieval, symbol retrieval, and structural retrieval... The system performs ranking-based fusion processing and further utilizes a semantic evaluation model to assess and reorder the semantic relevance of the initial fusion result set. This allows the system to automatically optimize result priority based on multi-path retrieval, selecting code units with the highest semantic fit. This helps improve the accuracy and relevance of retrieval results, avoids the problem of generated content deviating from actual intent, reduces noise interference from superficially similar but functionally inconsistent results, and enhances the consistency between answers and users' actual needs. On the other hand, the system generates structured answers based on the reordered candidate result set and performs cross-validation. This allows the system to systematically check the relevance, completeness, and source code consistency of answers from multiple independent dimensions. This ensures that the generated content maintains a reliable connection with the actual implementation of the target code library, effectively avoiding the "illusion" or disconnection phenomena common in intelligent question-answering systems, thereby significantly improving the credibility and accuracy of answers.
[0044] The following section will further explain the code semantic understanding and hybrid retrieval question answering method for AI programming in this example embodiment.
[0045] In step S110, the target code library is parsed to construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index.
[0046] In one example embodiment of this disclosure, the target code library refers to a collection of files used to store the source code of a project. For example, the target code library can be the source code repository of an enterprise backend service, the complete source code directory of an open source project, or a multi-language mixed code structure organized by language classification. As long as it can provide parsable source code text and support syntax parsing, it is acceptable. This embodiment does not impose any special limitation on the type of target code library.
[0047] Before parsing the target codebase, a file-level scan can be performed to identify the programming language types, file structures, and code units that can participate in index construction. File-level scanning can include file type identification, encoding format identification, comment block extraction, and invalid file filtering. For example, during the scanning phase, compilation artifacts (such as .o files and .class files), temporary cache directories, dependency package directories, and other non-source code files can be filtered out to ensure the accuracy of subsequent code parsing.
[0048] After the file scanning is complete, the source code can be parsed to obtain code units in the form of functions or classes. Parsing refers to using syntax analysis tools (such as an Abstract Syntax Tree (AST) parser or a syntax analysis module based on Tree-sitter rules) to structurally break down the source code according to the programming language's syntax rules, in order to extract code elements with semantic boundaries. In some optional implementations, code units can include information such as function signatures, parameter descriptions, return value descriptions, internal call relationships, module dependencies, and docstrings, as long as they reflect the behavior and structure of the code unit.
[0049] When constructing a semantic vector index, the text of the code unit can be input into a pre-trained code semantic model, such as a Bidirectional Encoder Representations from Transformers for Code (CodeBERT), GraphCodeBERT, or other embedding models with code understanding capabilities. A semantic vector is a high-dimensional numerical vector used to represent the functional semantics of code. This embodiment does not impose special limitations on the dimension or model type of the generated vector, as long as it can express the semantic similarity between code units. In an optional implementation, the semantic vector can be jointly encoded based on the function signature, logical structure, and comment information of the code unit to improve semantic relevance.
[0050] When constructing a symbol inverted index, named body information, such as function names, class names, and variable names, can be extracted from code units. Named body information can be extracted using a lexical analyzer or directly obtained from syntax tree nodes. In some optional methods, symbol names can be preprocessed using Trigram slicing, edit distance encoding, or other fuzzy matching to achieve higher fault-tolerant search capabilities. The content of the symbol inverted index may include the symbol name, its corresponding code unit identifier, and its file location; this embodiment does not limit the specific data structure format.
[0051] When constructing a structural relationship index, graph structure data can be built based on the call relationships, inheritance relationships, and module dependencies between code units. For example, code units can be used as graph nodes, and relationships involving calls or dependencies can be used as graph edges, thus forming a graph structure index that can be used for structural reasoning. The structural relationship index can be generated based on static syntax analysis, or, optionally, supplemented with call information by combining dynamic execution results, as long as it reflects the structural relationships between code units.
[0052] In step S120, a code query question in natural language form is received. Based on the code query question and the multi-dimensional code index, retrieval operations including at least semantic retrieval, symbol retrieval and structural retrieval are performed in parallel to obtain multiple candidate result sets.
[0053] In one example embodiment of this disclosure, a code query question in natural language refers to descriptive text entered by the developer in everyday language to express functional requirements or code questions. For example, a code query question in natural language may include statements such as "How to create a user account", "Where is the authentication logic of this module implemented?", and "How is the database connection injected?". Of course, the code query question can also be entered by project maintainers through interface calls, chatbot windows, or consoles, or it can be extracted from historical question and answer records. This embodiment does not limit the input method of natural language questions, as long as it can express the query intent.
[0054] After receiving a natural language code query, text preprocessing can be performed. Text preprocessing refers to standardizing the natural language code query for analysis by the subsequent retrieval module. For example, preprocessing operations may include character cleaning, word segmentation, named entity recognition (e.g., recognizing concepts such as "user," "order," and "authentication"), stop word filtering, and subword tokenization. Of course, semantic parsing techniques can also be used to identify the intent of the code query, but this embodiment does not limit this approach, as long as a query representation usable for further processing can be obtained.
[0055] During semantic retrieval, the preprocessed natural language code query question can be input into a semantic encoding model in the same space as the code semantic vector to obtain a query vector. The query vector is a high-dimensional vector data representing the semantics of a natural language query. By calculating the similarity (e.g., cosine similarity) with the code vector in the semantic vector index, a first candidate result set ranked according to semantic closeness can be obtained. This embodiment does not limit the similarity calculation method.
[0056] When performing symbolic retrieval, keywords or domain terms can be extracted from natural language code query questions to form a keyword list. Extraction methods can be rule-based, statistical models, or pre-trained language models, as long as they can identify terms with symbolic meaning. In some optional embodiments, fuzzy matching strategies can be applied to the keywords, such as using Trigram similarity or edit distance matching, to handle inconsistencies in user input spelling.
[0057] When performing structural retrieval, code units from semantic and / or symbolic retrieval results can be used as starting points for bounded traversal within the graph structure constructed by the structural relationship index. For example, one can start from a candidate function node and search along the call edge to find its called function or caller function, thus forming code fragments related to a specific logical path; or one can search along the inheritance edge to find subclasses or parent classes, thereby supplementing the understanding of the object-oriented hierarchy.
[0058] By parsing the target codebase and constructing a multi-dimensional code index including semantic vector indexes, symbol inverted indexes, and structural relation indexes, the system can uniformly represent the functional semantics, formal symbols, and structural associations of code units. When receiving code query questions in natural language, the system can perform semantic retrieval, symbol retrieval, and structural retrieval operations in parallel. This enables a deep understanding of the developer's programming intent expressed in natural language, allowing for queries without requiring users to know the exact names or structures in the code beforehand. This reduces the reliance on memorizing specific terms in code retrieval and avoids the limitations of traditional tools that rely on keyword matching or precise symbol lookup, thus improving query flexibility and ease of use.
[0059] In step S130, the multiple candidate result sets are fused without parameters based on ranking to generate a preliminary fusion result set.
[0060] In one example embodiment of this disclosure, multiple candidate result sets refer to sets of candidate code units obtained based on semantic vector indexing, symbol inverted indexing, and structural relation indexing, respectively. Each set can be sorted according to its respective relevance index. For example, the semantic retrieval result set can be sorted from high to low according to semantic similarity score, the symbol retrieval result set can be sorted according to symbol matching degree, and the structural retrieval result set can be sorted according to structural path distance or structural relevance. This embodiment does not impose special limitations on the sorting method of the candidate result sets, as long as it can reflect the retrieval priority of each path.
[0061] Before merging multiple candidate result sets, the ranking data of each candidate code unit can be standardized. Ranking data refers to the position of a candidate code unit in its respective retrieval path, such as ranking 1st, 3rd, or 10th. In some alternative methods, each ranking position can be converted into its reciprocal (e.g., the reciprocal can be expressed as 1 / (k+c), where k represents the ranking value and c represents a constant smoothing term), to more fairly reflect the ranking contributions of different retrieval paths during the fusion process.
[0062] When performing ranking fusion, each candidate code unit appearing in any candidate result set can be processed individually. For example, for a candidate code unit, if it appears in both the semantic retrieval result set and the symbolic retrieval result set, its inverse semantic ranking and inverse symbolic ranking can be taken separately and summed. If the code unit does not appear in the structural retrieval result set, its inverse structural path ranking can be considered zero. In some optional methods, the same or different weight coefficients can be set for different retrieval paths to adjust the contribution ratio of different paths during fusion, but this embodiment does not limit the weight setting method.
[0063] After completing the ranking fusion calculation, all candidate code units can be reordered from highest to lowest according to their fusion scores to obtain a preliminary fusion result set. The preliminary fusion result set refers to a list of candidate code units after considering the ranking contributions of each retrieval path; typically, code units hit by multiple retrieval paths are more likely to rank higher. In some optional methods, a truncation threshold can be set for the preliminary fusion result set, such as retaining only a few top-ranked candidates, to reduce the computational load in the subsequent semantic evaluation stage. This embodiment does not impose any special limitations on this.
[0064] In step S140, the code query question and the preliminary fusion result set are input into a semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and reorders them, and outputs the reordered candidate result set.
[0065] In one example embodiment of this disclosure, the semantic evaluation model refers to a deep learning model trained on a large-scale corpus and capable of semantic understanding across text and code. It can be used to determine the degree of semantic matching between candidate code units and user natural language queries. For example, the semantic evaluation model can be constructed using a convolutional neural network (CNN), or a network model based on a Transformer structure, or other language models with code reasoning capabilities, such as a large language model (LLM). Of course, a language model specifically optimized for code understanding can also be used to enhance the semantic alignment between candidate code units and user questions. This embodiment does not impose any special limitations on the model type of the semantic evaluation model.
[0066] After receiving the evaluation input data, the semantic evaluation model can output an evaluation score representing the relevance between candidate code units and the natural language query question. For example, this evaluation score can be a real value between 0 and 1, or it can be a semantic matching probability given in discrete rank form. This embodiment does not specify the range or generation form of the evaluation score, as long as it can be used for ranking. After obtaining the evaluation scores of all candidate code units, the candidate code units can be sorted from high to low according to the evaluation scores, forming a re-ranked candidate result set. In some optional methods, a filtering operation can be performed on candidate code units with evaluation scores below a preset threshold, so that candidate content that clearly does not conform to the query intent is eliminated at this stage, thereby improving the efficiency of the subsequent verification stage. This embodiment does not limit this approach.
[0067] By performing ranking-based fusion processing on multiple candidate result sets obtained from semantic retrieval, symbolic retrieval, and structural retrieval, and further utilizing a semantic evaluation model to assess and re-rank the semantic relevance of the preliminary fusion result set, the system can automatically optimize result priority based on multi-path retrieval. It can filter out code units with the highest semantic fit, thereby helping to improve the accuracy and relevance of retrieval results, avoid the problem that generated content may deviate from the actual intent, reduce noise interference from superficially similar but functionally inconsistent results, and enhance the consistency between the answer and the user's real needs.
[0068] In step S150, cross-validation is performed based on the reordered candidate result set, and a structured answer after successful cross-validation is output.
[0069] In one example embodiment of this disclosure, cross-validation refers to checking the relationship between candidate code units and natural language query questions from multiple validation perspectives to ensure that the final output answer is both semantically relevant to the user's question and consistent with the actual implementation of the target code library. For example, cross-validation may include various checking methods such as relevance validation, completeness validation, source code matching validation, and code logic validation. However, this embodiment does not limit the number or execution order of validation methods, as long as they can comprehensively reflect the correctness of the candidate content.
[0070] When performing cross-validation, the reordered candidate code units can be converted into structured answers. A structured answer refers to code explanations presented in a fixed format, such as code snippets, logical explanations, function usage descriptions, and input parameter descriptions.
[0071] When a structured answer passes all validation criteria, it can be output as the final answer; if it fails, a structured answer can be regenerated based on candidate code units, and the validation operation can be performed again until a structured answer that meets the requirements is obtained.
[0072] By generating structured answers based on the reordered candidate result set and performing cross-validation, the system can systematically check the relevance, completeness, and source code consistency of the answers from multiple independent dimensions. This ensures that the generated content maintains a reliable correlation with the actual implementation of the target code library, effectively avoiding the "illusion" or disconnection phenomena common in intelligent question answering systems, thereby significantly improving the credibility and accuracy of the answers.
[0073] It should be noted that, Figure 1 The method flow shown is mainly used to illustrate the logical relationship between the various functional processes in this disclosure. In the specific implementation, each step can be merged, split, or the order can be adjusted according to the actual application requirements. As long as the overall processing mechanism of multi-path result constraint, semantic evaluation and verification closed loop can be realized, it falls within the protection scope of this disclosure.
[0074] The contents of steps S110 to S150 will be described in detail below.
[0075] In one example embodiment of this disclosure, a multi-dimensional code index can be constructed in the following ways, specifically including: The source code in the target code library can be parsed using a preset syntax parser to extract code units in the form of functions or classes. The code units can include docstrings, function signatures, internal calls and dependency information, and a multi-dimensional code index can be built based on the code units.
[0076] A syntax parser is an analysis tool that can break down source code files into hierarchical structures and generate a structured representation according to the syntax rules of a programming language. For example, a syntax parser can be based on abstract syntax tree construction technology, decomposing the source code into hierarchical tree nodes through two stages: lexical analysis and syntax analysis. Alternatively, it can be based on an incremental syntax parsing framework (such as a Tree-sitter parser) to achieve a more efficient syntax decomposition process. This embodiment does not impose any special limitations on the specific implementation of the syntax parser, as long as it can correctly identify the syntax structure in the target code library.
[0077] Before performing syntax parsing on the source code, the code file can be read and standardized. Standardization refers to formatting the source code without changing the code logic, enabling the parser to more stably recognize the language structure. For example, this can involve standardizing indentation styles, removing BOM tags, standardizing line breaks, and cleaning up invalid characters. In an optional implementation, language detection can also be performed on the source code to determine which syntax rule should drive the parsing of different files. For example, it can recognize multiple language types such as Python, Java, Go, and C++. This embodiment does not impose any special limitations on the file preprocessing method.
[0078] During syntax parsing, the source code can be broken down layer by layer based on the language's corresponding syntax definition file or syntax rule table to extract function definition nodes and class structure nodes. Function definition nodes refer to the structures formed in the syntax tree with function declarations as root nodes; for example, a function definition node can be a "def" statement block in Python or a method declaration structure in Java. Class structure nodes refer to the substructures of the syntax tree corresponding to class declarations as root nodes. This embodiment does not limit the language type; as long as the parser can correctly identify the boundaries between functions and classes, code unit extraction can be achieved.
[0079] When extracting code units, important metadata from the function body or class body can be collected simultaneously. For example, a docstring is a function or class description text written in a specific format, such as a triple-quoted docstring in Python or a Javadoc comment in Java, used to describe the purpose, input parameters, return value, or usage examples of the code unit; a function signature is a structured declaration consisting of the function name, parameter list, return type, and modifiers, used to reflect the function's calling interface; internal call information refers to the syntactic behavior of calling other functions or methods within the function body, such as call chains, method references, or module call statements; dependency information refers to the usage information of external modules, classes, or constants referenced by the code unit. Of course, this embodiment does not limit the quantity or format of the collected metadata, as long as it can express the behavioral characteristics of the code unit.
[0080] In some optional implementations, a unified structured representation format can be generated for the extracted code units. For example, the code units can be converted into a dictionary structure, a JSON structure, or a custom data class format, so that the subsequent semantic indexing construction stage can directly use the structured data for semantic modeling and vectorized encoding. This embodiment does not limit the structured data format.
[0081] Multi-dimensional code indexing refers to a set of indexes that can describe the characteristics of code units from different perspectives, supporting the multi-path retrieval process for subsequent natural language code query problems. Unlike traditional indexing methods that rely solely on text keywords, multi-dimensional code indexing can simultaneously characterize code from three levels: semantic information, symbolic information, and code structure information, thereby providing a higher-level and more reliable association mechanism between natural language problems and code units.
[0082] When constructing a semantic vector index, semantic modeling can be performed on the textual information contained in each code unit. For example, semantic modeling can be based on a pre-trained code semantic model, converting the content of the code unit into a fixed-dimensional vector representation. In some alternative approaches, the input text for semantic modeling can be composed of the code unit's docstring, function signature, and internal logic fragments to enrich the semantic source of the vector representation. For example, the docstring can provide a functional description at the natural language level, the function signature can provide calling interface and parameter type information, and the internal logic fragments can reflect code behavior patterns. This embodiment does not impose special limitations on the semantic vector dimension or the code semantic model structure, as long as the vector representation can be used to measure the semantic similarity between code units.
[0083] When constructing a symbol inverted index, named body information within code units can be identified and mapped. Named body information refers to the symbols used in the code to identify function, class, or variable names; for example, named body information could be name strings such as "createUser", "AuthService", or "token_expire". In some optional implementations, the named body information can be standardized. For example, camelCase naming can be split into multiple sub-words, underscore naming format can be standardized, or case can be converted to a uniform form to enhance symbol matching capabilities. The symbol inverted index can be constructed using hash mapping, search tree structures, or segmented storage structures, as long as it enables the rapid location of the corresponding code unit from the symbol name. This embodiment does not impose any special limitations on this.
[0084] When constructing a structural relationship index, a structured graph representation can be generated based on the call relationships, inheritance relationships, or module dependencies between code units. For example, each code unit can be treated as a node in the graph, and function call behaviors can be connected as graph edges; when one type inherits from another, inheritance edges between parent and child nodes can be created in the structural graph; when a code unit depends on a module, module reference relationships can be recorded to expand relevant context paths during retrieval. In some optional methods, different edge weights can be set for different types of structural relationships, so that the graph structure can reflect the importance of different relationships. This embodiment does not limit the implementation method of the structural graph.
[0085] After constructing the semantic vector index, symbolic inverted index, and structural relation index, the three types of indexes can be managed uniformly according to a preset storage format. For example, the semantic vector index can be stored in a vector database, the symbolic inverted index in a key-value database, and the structural relation index in a graph database; alternatively, a unified data structure can be used to integrate the three types of indexes into the same storage system. This embodiment does not limit the specific storage method, as long as it supports fast access in subsequent retrieval stages.
[0086] By utilizing a pre-defined syntax parser to perform structured parsing of the target codebase and breaking down the source code into code units based on functions or classes, the system can establish a structured expression at the most basic code organization level, enabling subsequent retrieval to proceed based on code entities with clearly defined logical boundaries. Simultaneously, incorporating docstrings, function signatures, and call and dependency information into the code units gives the index construction a multi-dimensional source of information in semantics, interface, and behavior. This provides an accurate and computable basic representation for subsequent retrieval paths, making multi-dimensional index construction more refined and reliable.
[0087] In one example embodiment of this disclosure, it can be achieved through Figure 2 The steps described above implement a multi-dimensional code index built from code units. (See reference...) Figure 2 As shown, it can specifically include: Step S210: Based on the trained code semantic model, each code unit is converted into a semantic vector, and the semantic vector index is constructed using the semantic vector. Step S220: Extract the named body information from each code unit, and construct the symbol inverted index using the named body information; Step S230: Construct graph structure data based on the calling relationship, inheritance relationship and module dependency relationship between the code units, and form the structure relationship index based on the graph structure data.
[0088] Here, a code semantic model refers to a deep learning model capable of semantically encoding source code fragments. For example, a code semantic model can employ a Transformer-based model, such as CodeBERT, GraphCodeBERT, or CodeT5. These models are typically pre-trained on large-scale source code and related text data to gain the ability to understand code structure, calling patterns, comment semantics, and logical intent. It is understood that this embodiment does not limit the specific model structure of the code semantic model; any model capable of generating semantic vectors for code units can be considered a code semantic model.
[0089] When generating semantic vectors, the input content of the code unit can be formatted. For example, the function signature, docstring, and function body can be concatenated to form the model's standard input text; alternatively, the input can be organized using templates, enabling the code semantic model to recognize the context boundaries of the input. In some optional methods, the input content can be truncated, segmented, or hierarchically represented, allowing the model to process longer code units without affecting coding quality. This embodiment does not limit this approach.
[0090] When a code unit is input into a trained code semantic model, the model outputs a fixed-dimensional numerical vector representing the semantic features of that code unit. The semantic vector can be a high-dimensional vector of length 256, 512, or 768, or it can be dynamically generated by the model with embeddings of different sizes, as long as it reflects the semantic similarity relationship between code units in the vector space. For example, two functionally similar code units (such as user authentication functions in different modules) can be adjacent to each other in the vector space, while functionally unrelated code units will maintain a greater distance in the space.
[0091] After generating semantic vectors, the semantic vectors and their corresponding code unit identifiers can be stored together in the semantic vector index. In some optional implementations, the semantic vector index can be stored in a vector database to support efficient retrieval based on vector similarity; alternatively, an inverted vector file, an HNSW (Hierarchical Navigable Small World Graph) structure, or other Approximate Nearest Neighbor (ANN) structures can be used to construct the index to improve retrieval speed. This embodiment does not limit the storage structure of the semantic vectors.
[0092] Named body information refers to the name symbols used to identify logical entities within a code unit. For example, named body information can include function names, class names, variable names, constant names, or module names. As explicit identifiers in a programming language, the named body reflects the code designer's abstraction of functionality, logical responsibilities, or object semantics. For instance, names like "get_user_info", "AuthManager", and "token_cache" directly reveal the purpose of a code unit.
[0093] When extracting named body information, recognition can be performed based on syntax tree nodes, lexical tag sequences, or pattern matching rules. For example, in Python, the corresponding identifiers can be extracted through nodes such as FunctionDef, ClassDef, and Name in the AST; in Java, the named body can be extracted through method declaration nodes, class declaration nodes, and variable declaration nodes; and in C++, identifiers within a namespace can be identified by combining scope resolution rules.
[0094] After extracting the named body information, the named body information can be used as symbolic items, and its corresponding code unit identifier can be used as document items in the inverted list to construct a symbolic inverted index. In some optional methods, preprocessing can be performed on the named body name, such as case normalization, camelCase splitting, and sub-word segmentation (e.g., splitting "createUserSession" into "create", "user", and "session") to enhance the matching ability during querying. This embodiment does not limit the preprocessing method.
[0095] Symbolic inverted indexes can be constructed using hash table structures, B-tree index structures, or trie-based storage methods. For fuzzy query requirements, edit distance indexes (such as metric tree (Burkhard-Keller, BK) structures) or trigram inverted index structures can also be used to achieve "similar symbol matching" capabilities. Of course, this embodiment does not limit the data structure type of the inverted index, as long as it supports quickly locating the associated code unit through symbols.
[0096] Call relationship refers to the situation where code unit A calls code unit B, for example, a function body contains a call statement to another function; inheritance relationship refers to the parent-child structure between classes, for example, a subclass extends the logic of the parent class; module dependency relationship refers to the situation where a module references other modules through import, include, require, etc. These three types of relationships together constitute the structured dependency network within the code library.
[0097] When generating graph-structured data, code units can be used as graph nodes, and the three types of structural relationships mentioned above can be used as graph edges. For example, when function A calls function B, an edge can be created in the graph pointing from node A to node B; when class C inherits from class D, an edge in the inheritance direction can be created; when module E introduces module F, a module dependency edge can be created. In some optional methods, different edge weights can be assigned to different relationship types so that the importance of different relationships can be distinguished during subsequent structure retrieval.
[0098] The completed graph structure data can be stored in the form of an adjacency list, adjacency matrix, or a graph database (such as Neo4j). This embodiment does not limit the storage method of the graph structure, as long as it can support subsequent structure traversal.
[0099] By converting code units into semantic vectors to construct a semantic vector index, the system can replace the traditional literal matching method with a vector space that expresses the behavioral characteristics of functions, thereby promoting functional understanding at the semantic level. Simultaneously, a symbolic inverted index is constructed based on named body information, allowing the system to maintain compatibility with traditional precise symbol matching capabilities and ensuring the reliability of fine-grained code location. Furthermore, a structural relationship index is built through calls, inheritance, and dependencies, enabling the system to achieve structural-level associations across code units. This covers retrieval blind spots caused by insufficient semantic expression or inconsistent naming, thus forming a three-dimensional complementary code indexing system encompassing semantics, symbols, and structure.
[0100] In one example embodiment of this disclosure, multiple candidate result sets include a first candidate result set under the semantic retrieval path, a second candidate result set under the symbol retrieval path, and a third candidate result set under the structure retrieval path; this can be achieved through... Figure 3 The steps outlined in the document are based on code query questions and multi-dimensional code indexing. They involve parallel execution of retrieval operations, including semantic retrieval, symbolic retrieval, and structural retrieval, yielding multiple candidate result sets. (See reference...) Figure 3 As shown, it can specifically include: Step S310: Convert the code query question into a query vector, calculate the similarity between the query vector and each semantic vector in the semantic vector index, and obtain the first candidate result set based on the similarity ranking; Step S320: Extract keywords from the code query question, and match code units corresponding to named body information that contain the keywords or are fuzzily matched with the keywords based on the symbol inverted index to obtain the second candidate result set; Step S330: Starting from the code units in the first candidate result set and / or the second candidate result set, perform a bounded traversal on the graph structure data represented by the structural relationship index to obtain the associated code units that have a calling, inheritance or dependency relationship with the starting point, and form the third candidate result set.
[0101] Code query questions refer to textual descriptions that express a developer's programming intent or questions about the code. For example, code query questions could be natural language text such as "How to initialize a database connection?", "Where is the order verification logic implemented?", or "What are the reasons for user login failures?". Before performing semantic retrieval on natural language query questions, text preprocessing can be performed, such as character cleaning, language normalization, sub-word segmentation, part-of-speech tagging, or named entity recognition, to make the preprocessed text more suitable for semantic coding models.
[0102] After text preprocessing, the natural language query text can be input into a pre-trained semantic encoding model. For example, the semantic encoding model can employ a Transformer architecture, such as Bidirectional Encoder Representations from Transformers (BERT), Sentence-BERT, or other deep learning models suitable for natural language semantic representation. The semantic encoding model maps the code query question in natural language form to a fixed-dimensional query vector, which represents the semantic features of the query content in a semantic space. In some alternative approaches, a specially trained cross-modal semantic alignment model can be used to ensure that the query vector and the code semantic vector are in the same embedding space, thereby improving the alignment effect of semantic retrieval. Fine-tuning techniques can also be used to make the model more closely fit the domain language features of a specific codebase.
[0103] After obtaining the query vector, the similarity between the query vector and each semantic vector in the semantic vector index can be calculated using a similarity metric formula. For example, similarity can be represented using cosine similarity, dot product similarity, or the inverse transform of Euclidean distance. This embodiment does not impose any special limitations on the calculation method for similarity. In some optional methods, the similarity score can be normalized to give it more stable discriminative power in result ranking. Finally, the first candidate result set under the semantic retrieval path can be obtained by sorting all semantic vectors in descending order of their similarity scores. This first candidate result set can contain several top-ranked code units that are semantically similar to the natural language code query question.
[0104] Keywords refer to text units carrying potential code symbol meanings in natural language query questions, such as terms like "token," "cache," "UserService," "auth," and "init_db." These terms may be directly associated with function names, class names, variable names, constant names, or module names of code units. When extracting keywords, rule-based matching methods (such as lexical segmentation based on character patterns), statistical language models (such as TF-IDF), or deep learning models (such as entity recognition models) can be used for identification. This embodiment does not impose any special limitations on the keyword extraction method. Optional methods include symbol normalization of the extracted keywords, such as case unification, camelCase splitting, and removal of special characters, to improve the hit rate when matching with the symbolic inverted index. This embodiment does not impose any restrictions on this.
[0105] When performing symbol matching, keywords can be compared with identifiers in the inverted symbol index. For example, when a keyword completely matches a code unit symbol, that code unit can be directly added to the candidate results; when a keyword has a partial matching relationship with a symbol name, such as prefix matching, suffix matching, or subword inclusion, the code unit can also be added to the candidate set. In some optional implementations, fuzzy matching techniques can be used, such as fuzzy search based on edit distance, the Trigram matching algorithm, or the BK tree index structure, to enhance the fault tolerance of the matching. The second candidate result set obtained from the symbol retrieval path usually has high accuracy because symbol names are usually directly defined by the code developer and are closely related to functional semantics. For example, when a user query contains "AuthService", the class or module related to the authentication logic can be directly located.
[0106] The starting point for structural retrieval can come from the first candidate result set obtained from semantic retrieval, the second candidate result set obtained from symbolic retrieval, or the union of both. The starting node represents the code unit most likely to be semantically relevant to the user's query, while the purpose of structural retrieval is to further supplement logically adjacent code units along the association path, making the candidate set more complete and more in line with the real code context.
[0107] When performing structural retrieval, graph structure data constructed from the structural relationship index can be utilized. Graph structure data is a directed or undirected graph composed of code units as nodes and code structural relationships as edges. For example, when function A calls function B, a call edge from A to B can be created in the graph; when class C inherits from class D, an inheritance edge from C to D can be created; when module E depends on module F, a dependency edge from E to F can be created. In some optional methods, different weights can be set for different types of relationship edges to distinguish the importance of structural associations or retrieval priorities, but this embodiment does not impose special limitations on this.
[0108] When performing traversal on graph-structured data, depth-first search (DFS), breadth-first search (BFS), or heuristic traversal strategies can be employed. This embodiment focuses more on the "boundedness" of traversal, that is, limiting the search range during the traversal process to avoid unbounded expansion. For example, an upper limit can be set on the traversal depth to limit the number of hops along the call chain or inheritance chain; an upper limit can also be set on the number of traversed nodes to ensure that the computational load does not increase sharply due to the excessive size of the graph.
[0109] During the traversal, whenever a new node (i.e., a new code unit) is visited, it can be added to the scattered candidate result set of the structure retrieval. If the same code unit is visited repeatedly in multiple paths, its recording method can be determined based on the first visit position or path weight. This embodiment does not limit the merging strategy, as long as it can ensure that there are no logical conflicts in the candidate set. The third candidate result set obtained by structure retrieval can usually supplement the code segments not covered by semantic retrieval and symbolic retrieval. For example, when the user's query intent involves a certain logical link, semantic retrieval may locate the starting function of the link, while structure retrieval can complete the downstream logic along the function call chain, thereby presenting a complete logical region related to the user's intent.
[0110] By converting natural language queries into query vectors and performing semantic similarity ranking, the system can directly recall function-related code units based on semantic understanding. Through symbolic inverted index matching keywords or fuzzy matching of related names, the system can capture code with explicit symbolic associations, achieving rapid location of key names. Starting with semantic or symbolic candidates, a bounded traversal of the structural relationship index allows the system to expand function-related code along the code relationship graph, completing code units not recalled due to differences in semantic expression or naming inconsistencies. The parallel execution of these three retrieval paths gives the candidate set broad coverage, deep associations, and a balance between semantics and structure, laying a solid foundation for subsequent fusion.
[0111] In one example embodiment of this disclosure, it can be achieved through Figure 4 The steps described above enable ranking-based parameterless fusion of multiple candidate result sets, generating a preliminary fusion result set. (Refer to...) Figure 4 As shown, it can specifically include: Step S410: For each candidate code unit that appears in the candidate result set, obtain multiple ranking data of the candidate code unit in each candidate result set; Step S420: Sum the reciprocals of each ranking data to obtain the weighted fusion score corresponding to each candidate code unit; Step S430: Reorder all the candidate code units according to the weighted fusion score to generate the preliminary fusion result set.
[0112] Here, candidate code units refer to the code units selected in the first candidate result set under the semantic retrieval path, the second candidate result set under the symbolic retrieval path, or the third candidate result set under the structural retrieval path. Because the ranking criteria differ across retrieval dimensions—for example, semantic retrieval based on semantic similarity, symbolic retrieval based on symbolic matching degree, and structural retrieval based on structural association paths—multiple candidate result sets often exhibit different ranking distributions.
[0113] When collecting ranking data, the position of each candidate code unit in each candidate result set can be recorded. For example, if a code unit ranks 3rd in semantic retrieval results, 10th in symbol retrieval results, and does not appear in structural retrieval results, its ranking value can be recorded as 3, 10, and missing, respectively. In some optional methods, to handle missing cases, the missing ranking can be treated as an infinite value or set to a maximum ranking boundary value to indicate that the candidate code unit was not retrieved under the corresponding path. In other optional implementations, the ranking data can also be normalized so that the ranking values of different candidate result sets are on a uniformly comparable scale. For example, all ranking values can be mapped to the interval [0, 1], or a standard score transformation can be performed on the ranking values of each candidate result set to reduce the impact of differences in the scale of different retrieval paths. This embodiment does not limit this.
[0114] The reciprocal of a ranking value refers to the value obtained by taking the reciprocal of the ranking value. For example, if a code unit ranks k in a result set, its reciprocal can be expressed as 1 / k or 1 / (k+c), where c is a constant smoothing term. The purpose of introducing the constant smoothing term is to avoid overemphasizing the case of ranking 1 and to maintain numerical stability. This embodiment does not limit the existence of the constant smoothing term or its value, as long as it can reasonably distinguish the ranking differences.
[0115] By summing the inverse rankings of multiple results, code units that rank highly across multiple candidate result sets can achieve higher fusion scores. For example, when a code unit ranks highly across three search paths, its summed inverse rankings will be significantly greater than that of a code unit that only excels in a single path. In some alternative approaches, different weight parameters can be set for different search paths during the summation process to reflect their relative importance. For instance, semantic search can be given higher weight, or the weights can be dynamically adjusted based on project characteristics. This embodiment does not impose any limitations on this approach.
[0116] In the process of calculating the fusion score, a normalization strategy can also be introduced to ensure that the score range is comparable. For example, the fusion score of all candidate units can be divided by the maximum score so that the fusion score falls in the interval [0, 1]; or a weighted average score model can be constructed based on the ranking matrix to make the distribution of the fused score smoother. This embodiment is not limited to this.
[0117] Reordering refers to arranging candidate code units from highest to lowest based on their fusion scores, giving higher priority to code units that achieve high relevance across multiple retrieval paths. For example, if a code unit achieves high rankings in semantic retrieval, symbolic retrieval, and structural retrieval, its fusion score will be significantly higher than a code unit that only excels in one path, thus placing it higher in the final ranking. This embodiment does not limit the ranking algorithm, as long as it can perform a descending order based on the fusion score.
[0118] During the sorting process, the fusion scores can be evenly distributed, deduplicated, or range-corrected to ensure the stability and continuity of the sorting results. For example, the fusion scores can be numerically normalized before sorting to distribute them within a fixed range; alternatively, candidates with small score differences can be smoothed to avoid ranking fluctuations caused by minor numerical differences. This embodiment does not impose any special limitations on this approach.
[0119] In some alternative implementations, a truncation strategy can be performed on candidate code units before generating the initial fusion result set. The truncation strategy refers to retaining only a few candidates with the highest fusion scores to reduce the computational load in the subsequent semantic evaluation stage of the semantic evaluation model. For example, filtering can be performed based on a preset number (e.g., the top 50 candidates), a preset proportion (e.g., the top 20% by score), or a fusion score threshold. This embodiment does not limit the specific form of the truncation strategy.
[0120] In the final preliminary fusion result set, each candidate code unit carries its fusion score and ranking position to guide the subsequent semantic re-ranking process. In some optional approaches, rich retrieval source information can also be attached to each candidate record, such as the candidate unit's ranking in each retrieval path, its module information, or similarity indicators, so that the subsequent verification module can perform multi-dimensional cross-checks.
[0121] By acquiring multiple ranking data of candidate code units from different candidate result sets and weighting and fusing them by summing the inverse of the rankings, we can avoid the neglect of important candidates due to bias in a single search path. This ensures that the fusion result is constrained by multiple paths, presenting a more comprehensive coverage. Simultaneously, using the inverse of the rankings allows code units that appear repeatedly at the top of different search result sets to receive a natural boost, giving reliable candidates a consistent ranking advantage. The resulting preliminary fusion result set combines stability with multi-dimensional information aggregation capabilities, providing a more representative overall candidate set for the semantic re-ranking stage.
[0122] In an example embodiment of this disclosure, the semantic evaluation model can be used to evaluate and reorder the candidate code units in the preliminary fusion result set through the following steps, and output the reordered candidate result set. Specifically, this may include: The code query question can be combined with each candidate code unit in the preliminary fusion result set to obtain evaluation input data; the evaluation input data is input into the semantic evaluation model to obtain the evaluation score that represents the semantic relevance between each candidate code unit and the code query question; all candidate code units are sorted in descending order according to the evaluation score, and candidate code units with evaluation scores less than or equal to a preset score threshold are filtered to obtain the reordered candidate result set.
[0123] Before generating the evaluation input data, candidate code units can be formatted. Formatting can involve organizing source code fragments, function signatures, docstrings, or fragment summaries within the candidate code units according to a preset format for use by the semantic evaluation model. For example, the input text structure can be constructed using a template, such as "code query question + separator + candidate code unit + function description field," or a question-and-answer style concatenation method can be used to enhance the model's ability to identify the correlation between the question and the code. This embodiment does not limit the combination format, as long as the semantic evaluation model can simultaneously understand the user's intent and the code content from the combined text. In some optional methods, different granularity input formats can be generated for different candidate code units. For example, for code units containing long function bodies, their key logic fragments or summary portions can be extracted to avoid excessively long inputs; for code units with many docstrings, only the purpose description portion of the docstring can be retained to enhance semantic recognition accuracy. This embodiment is not limited to this.
[0124] When inputting evaluation data into a semantic evaluation model, the model can capture the correspondence between user intent, functional logic, structural features, and semantic keywords from the input text. For example, the semantic evaluation model can use learned semantic representations to determine whether candidate code units implement the logical function described by the user or whether they are directly related to the problem scenario described by the user. In some optional approaches, few-shot prompting strategies or instruction tuning can be used to enhance the model's evaluation capabilities. For example, example prompts can be added to the input data to help the model more accurately understand how to identify the semantic matching relationship between code and problem; additional training can also be performed on the model to make it more suitable for specific codebase scenarios. This embodiment does not limit the prompting method.
[0125] Evaluation scores can be output as probabilities, ranking scores, interval values, or internal model scores. For example, an evaluation score can be a real number between 0 and 1, representing the semantic matching degree between the code unit and the query question; it can also be a relevance score transformed from the hidden layer output of the model. This embodiment does not limit the score range or expression form, as long as it can be used for ranking.
[0126] Descending order sorting refers to ranking candidate code units from highest to lowest based on their evaluation scores, placing semantically more relevant candidates at the top. For example, if a code unit's evaluation score is significantly higher than other candidates, it will receive priority in the ranking. During the filtering phase, candidate code units with low relevance can be removed based on a preset score threshold. This threshold can be a fixed value, such as 0.3 or 0.5; or it can be dynamically determined, for example, based on the mean, quantiles, or standard deviation of the evaluation scores to adapt to changes in the candidate score distribution. The specific threshold can be customized according to the actual applicable scenario; this embodiment does not impose any special limitations on the threshold setting. In some optional implementations, a multi-layered filtering strategy can be introduced. For example, in addition to the evaluation score threshold, a maximum number of candidates can be set to limit the computational overhead in subsequent verification stages caused by an excessive number of candidates; or, deduplication can be performed based on the similarity between candidates to prevent multiple similar code units from occupying the top ranking positions.
[0127] By combining natural language queries with each candidate code unit and inputting them into a semantic evaluation model, and then evaluating their relevance based on deep semantic understanding capabilities, the system can differentiate candidate codes from the functional logic level rather than the literal feature level. Simultaneously, by sorting the evaluation scores in descending order and filtering candidates below a threshold, the result set retains code units with higher semantic consistency with the user's intent, improving the semantic accuracy of the results and reducing the computational burden of subsequent verification stages. This results in a more refined and expressive final candidate set.
[0128] In one example embodiment of this disclosure, cross-validation based on the reordered candidate result set can be performed through the following steps to output a structured answer after successful cross-validation, specifically including: The reordered candidate result set can be converted into a structured answer, and the code query question and the structured answer can be cross-validated to obtain the cross-validation result. If the cross-validation result is successful, the structured answer after successful cross-validation is output. If the cross-validation result is unsuccessful, a new structured answer is generated based on the reordered candidate result set, and the new structured answer is cross-validated again until the cross-validation result of the latest structured answer is successful, and then the latest structured answer is output.
[0129] In this context, a structured answer refers to a standardized data representation composed of candidate code units from the candidate result set, used to answer code query questions. Its format may include functional description fields, key code display fields, code location identifier fields, and contextual summary fields to aid in judgment. Of course, a structured answer may also include parameter descriptions, return value explanations, logical branch descriptions, typical call examples, or simplified abstract syntax tree summaries to more comprehensively reflect the behavioral characteristics of candidate code units; this embodiment does not impose any limitations on this.
[0130] When constructing a structured answer, the answer content can be appropriately trimmed or expanded based on the source files of the candidate code units, the complexity of the functions they contain, and the level of detail in the documentation comments. For example, for function bodies containing a large amount of implementation details, key logic segments or core processes can be extracted to avoid the structured answer becoming too long; while for code units lacking documentation information, their call chain relationships can be supplemented to improve the completeness of the answer. In some optional embodiments, code feature summaries can be extracted using code-oriented representation learning models and used as part of the structured answer to enhance the interpretability of the cross-validation stage. After the structured answer is formed, it can be paired with the original code query question and input into the cross-validation process to perform multi-dimensional verification operations such as semantic comparison, structural comparison, and logical consistency evaluation.
[0131] When performing cross-validation, the degree of matching between the structured answer and the code query question can be analyzed from aspects such as semantic relevance, content completeness, source code matching, and logical consistency. For example, a semantic matching model can be used to determine whether the functional description field of the structured answer is consistent with the intent pointed to by the query question; the code snippets in the structured answer can be parsed through syntax analysis to confirm that they are free of syntax errors and have executable capabilities; symbolic inverted indexes or semantic vector indexes can be used to verify whether the code units referenced by the structured answer actually exist in the target code library; at the same time, lightweight logic checking methods can be combined to cross-confirm whether the process description, condition judgment, and return path presented in the structured answer are consistent with the actual source code logic. This embodiment does not limit the specific validation methods, as long as the cross-validation can fully cover the semantic deviations, logical omissions, or referencing errors that may occur in the structured answer.
[0132] After cross-validation, a validation result indicating the validity of the structured answer in this round can be obtained. When the cross-validation result is deemed passed, it means that the structured answer meets the requirements in terms of semantics, structure, and code consistency, and can be directly provided to users as the final output. In the actual output process, the answer format can be adapted to the application scenario deployed by the system, such as outputting in text form, displaying in a collapsible structure, or transmitting in a unified format through the application programming interface. In some optional methods, syntax highlighting, source code jump links, or auxiliary explanatory text can also be added to improve the developer's reading experience, but these enhancements will not change the core content of the structured answer.
[0133] When cross-validation fails, a new structured answer is generated based on the reordered candidate result set, according to the reason for the failure. Reasons for failure may include the structured answer not covering key logic in the query intent, incomplete source code citations, inconsistencies between the description and actual code behavior, or contradictory information in the structured representation. In such cases, different candidate code units can be selected, or the structured content can be reorganized based on the same candidate code unit to supplement missing information or eliminate logical conflicts. For example, candidate code units that are more semantically aligned with the query intent can be selected; the expression of the structured answer can also be improved by changing the summary granularity, adjusting the field order, and adding contextual explanations.
[0134] After a new structured answer is generated, it enters the cross-validation process again to re-evaluate its semantic, structural, and logical consistency. This process is repeated until a version of the structured answer passes all validation dimensions. In system implementation, maximum iteration limits, failure mode memory mechanisms, and dynamic adjustment of validation strategies can be introduced to improve validation efficiency, but this embodiment does not limit the specific strategies. Finally, when the latest generated structured answer meets the cross-validation conditions, the latest generated structured answer can be output as the final result. Through this series of processes, the generation system possesses self-correction capabilities, ensuring that the final output structured answer achieves a high standard in terms of content accuracy, semantic reliability, and code consistency.
[0135] By converting the reordered candidate code units into structured answers and performing cross-validation, a systematic reliability check can be introduced during the answer generation stage. This ensures that the structured answers not only satisfy semantic relevance but also undergo multi-dimensional stress tests of completeness, interpretability, and real existence. If cross-validation fails, an iterative mechanism of regenerating the structured answer based on the candidate result set and validating it again can prevent incorrect answers from being directly output. This allows the generated results to gradually improve in quality and robustness through multiple convergences, ensuring that the output content is credible at the semantic, structural, and logical levels.
[0136] In one example embodiment of this disclosure, it can be achieved through Figure 5 The steps outlined in the document implement cross-validation of code query questions and structured answers to obtain cross-validation results. (Refer to...) Figure 5 As shown, it can specifically include: Step S510: Perform a relevance verification on the code query question and the structured answer to obtain the relevance verification result; Step S520: Perform integrity verification on the code query question and the structured answer to obtain the integrity verification result; Step S530: Perform source code matching verification on the code query question and the structured answer to obtain the source code matching verification result; Step S540: Perform code logic verification on the code query question and the structured answer to obtain the code logic verification result; Step S550: Determine the cross-validation result based on the correlation verification result, the integrity verification result, the source code matching verification result, and the code logic verification result.
[0137] Semantic relevance verification refers to determining whether the functional description, logical explanation, or code summary presented in the structured answer matches the semantic intent of the code query question. For example, for a query question asking "Where is the user permission verification logic executed?", semantic relevance verification will determine whether the structured answer provides code snippets, logical descriptions, or call path clues related to permission verification; if the structured answer only contains the user model creation logic and does not involve the permission processing part, the semantic relevance verification may fail.
[0138] Integrity verification refers to parsing the code snippets or semantic interpretations in a structured answer to determine whether they possess parsability, executableness, and syntactic structural integrity. For example, a preset parser can be used to attempt to construct an Abstract Syntax Tree (AST) from the code snippets in the structured answer. If a syntax error occurs during parsing, such as missing parentheses, invalid indentation, or illegal function calls, the integrity verification will fail. In some optional implementations, integrity verification can also analyze whether the logical chains in the structured answer possess the necessary structure, such as whether it contains return statements or undefined variables. These supplementary verification methods can further improve the effectiveness of the structured answer, and this embodiment does not impose any special limitations on them.
[0139] Source code matching verification refers to locating the corresponding code unit in the target code library based on the identifier information (such as function names, class names, variable names, etc.) referenced in the structured answer, using the symbolic inverted index or semantic vector index in multi-dimensional code indexing, to verify whether the code fragment referenced by the structured answer actually exists in the code library. For example, when the structured answer provides a logical description of the "validate_permission" function, source code matching verification will confirm that the function does exist and that its logical content is consistent with the behavior described in the structured answer; if the structured answer references a non-existent function name or path, the verification will be deemed unsuccessful. In some optional implementations, the relationship between the code unit and related modules can be further confirmed through structural relationship indexing, such as determining whether the referenced function is located in the correct class or module. This embodiment does not limit this, as long as it ensures that the verification can accurately determine the authenticity of the reference relationship.
[0140] Code logic verification involves inputting a structured answer along with a code query question into a separate verification language model. This model then performs reasoning verification on the structured answer, assessing its logical consistency, causal coherence, and whether the behavioral description matches expectations. The verification language model differs from the semantic evaluation model used for semantic reordering; it is typically specially trained or optimized to possess stronger logical analysis and error detection capabilities. For example, if a structured answer describes a function as "throwing an exception upon verification failure," the logic verification model analyzes the relevant code snippets in the structured answer to determine if the exception-throwing logic actually exists. If the logic described in the structured answer is inconsistent with the behavior of the source code, the logic verification may fail. The logic verification model can also identify self-contradictory descriptions, missing key process nodes, and unreachable logic in the structured answer, thereby further improving verification accuracy.
[0141] After completing the verification across the aforementioned multiple dimensions, the final cross-validation result can be determined based on the semantic relevance verification results, integrity verification results, source code matching verification results, and code logic verification results. In this embodiment, these verification results can be comprehensively judged using a weighted approach, or a rule-driven or score-driven approach can be used to determine the cross-validation result. For example, if multiple verification dimensions are all determined to be passed, the cross-validation result can be considered passed; if a core dimension (e.g., source code matching) is determined to be failed, it can be directly considered failed. Of course, the verification results can also be standardized into a unified score, and the success of cross-validation can be determined based on a preset threshold; this embodiment does not limit this approach.
[0142] By performing four types of verification—relevance, completeness, source code matching, and code logic—the system can comprehensively review structured answers from multiple perspectives, including semantic consistency, syntactic validity, true correspondence with the codebase, and logical reasoning. This allows the verification process to move beyond a single standard and form a more balanced quality assessment system. The combined mechanism of multi-dimensional verification enables potential semantic misjudgments, syntactic defects, false references, or logical inconsistencies to be detected across different dimensions, thereby significantly improving the rigor and reliability of the final answer.
[0143] In one example embodiment of this disclosure, the relevance verification of code query questions and structured answers can be performed through the following steps to obtain the relevance verification result, which may specifically include: The code query question and the structured answer can be input into a pre-trained semantic matching model. The semantic matching model calculates the semantic consistency score between the code query question and the structured answer. If the semantic consistency score is less than or equal to a preset consistency score threshold, the relevance verification result is determined to be unsuccessful. If the semantic consistency score is greater than the preset consistency score threshold, the relevance verification result is determined to be successful.
[0144] The semantic matching model can be a deep learning-based dual-tower structure model, a cross-encoder structure model, or a semantic alignment model based on the Transformer architecture. For example, the semantic matching model can be a bidirectional encoding representation model or other contrastive learning models with strong semantic discriminative capabilities. This embodiment does not limit the specific structure of the semantic matching model. When inputting a structured answer and a code query question, the semantic matching model will vectorize the content of both and capture the semantic correspondence between them through its internal context attention mechanism, representation alignment mechanism, and other methods. For example, when the code query question is "Where is the order pagination logic implemented?", while the structured answer describes the attribute definition of the order model, the semantic matching model will determine that the intents of the two are inconsistent and ultimately output a lower semantic consistency score.
[0145] After obtaining the semantic consistency score, the relevance verification result can be judged based on a preset semantic consistency score threshold. The preset semantic consistency score threshold can be a fixed constant, such as 0.5, 0.7, 0.8, or other empirical values; it can also be dynamically determined statistically, for example, by selecting the threshold that minimizes the false positive rate based on the performance of the semantic matching model on the validation set during the development phase. Optionally, the preset semantic consistency score threshold can also be dynamically adjusted according to the question category. For example, for logical reasoning questions, function positioning questions, or error troubleshooting questions, the preset semantic consistency score threshold can be appropriately increased to ensure that the structured answer provides sufficiently accurate information.
[0146] When the semantic consistency score is less than or equal to a preset semantic consistency score threshold, it can be determined that the current structured answer cannot accurately respond to the query intent at the semantic level, thus the relevance verification result is judged as failing. When the semantic consistency score is greater than the threshold, it can be determined that the structured answer and the code query question have sufficient semantic consistency, thus the relevance verification result is marked as passing. In some optional implementations, a boundary ambiguity interval processing strategy can also be introduced, that is, when the score is close to the threshold, the result is submitted to a more refined verification model or manual rules for further evaluation to avoid unstable judgments caused by boundary cases. This embodiment is not limited to this.
[0147] By inputting code query questions and structured answers into a semantic matching model and obtaining semantic consistency scores, the system can determine whether the structured answers truly address the user's intent in a model-based manner, rather than merely satisfying superficial similarity. Based on thresholds, it can determine whether an answer is "pass" or "fail," effectively filtering out semantically deviating or irrelevant outputs. This enables the verification process to have precise semantic alignment capabilities, thereby improving the semantic accuracy of the final answer.
[0148] In one example embodiment of this disclosure, the integrity verification of code query questions and structured answers can be performed through the following steps to obtain the integrity verification result, which may specifically include: The structured answer can be parsed by a preset syntax parser to attempt to build an abstract syntax tree. If a syntax error is found in the structured answer during the process of building the abstract syntax tree, or if the syntax structure of the structured answer is determined to be incomplete, the integrity verification result is determined to be unsuccessful; otherwise, the integrity verification result is determined to be successful.
[0149] The parser can select the appropriate syntax rules based on the programming language used in the code snippets in the structured answer. For example, when the code snippet is in Python, it can use Python's built-in parsing interface or a third-party abstract syntax tree (AST) building tool to parse the code; when the code snippet is in Java, Go, or other languages, it can call the corresponding language's syntax analysis module to complete the parsing. During the parsing process, the parser can perform lexical analysis, syntax tree construction, and dependency checks to identify whether there are syntax errors in the code snippets in the structured answer. For example, if the structured answer contains undefined code blocks, missing necessary statements, incomplete syntax structure, or variable references that do not conform to scope rules, the parser will throw a syntax error flag during the parsing process.
[0150] Integrity verification not only determines whether a code snippet can be successfully parsed into an abstract syntax tree, but also, by combining code context information, determines whether a structured answer is missing necessary components. For example, when a structured answer references a function but does not show the key statements inside the function body, structured analysis can determine whether the answer snippet expresses complete functional logic; or when the code snippet shown in the structured answer fails to form a closed structure (such as missing loop end markers or conditional branch closure markers), integrity verification can directly mark it as incomplete. Optionally, integrity verification can also check whether the structured answer has semantic breaks, such as missing return statements, undefined variables, or unreachable statements in the logical path. Although such logical breaks do not necessarily cause syntax parsing errors, they may affect the consistency between the structured answer and the actual source code behavior, and therefore can also be used as a supplementary factor in integrity verification.
[0151] After completing syntax parsing and structure checking, an integrity verification result can be generated based on the parsing results. If the parser finds syntax errors in a code segment or determines its structure to be incomplete during the construction of the abstract syntax tree, the integrity verification result is determined to be a failure. If the parser successfully generates the abstract syntax tree and the logical segments presented in the structured answer meet the basic syntax and structure requirements, the integrity verification result is determined to be a pass. In some optional implementations, fine-grained scoring can be provided for integrity verification based on the confidence level or structural completeness of the parsing results, providing more criteria for subsequent cross-validation. For example, different verification levels such as "completely pass," "partially pass but recommended for checking," and "completely fail" can be distinguished. This embodiment does not limit this.
[0152] By using a syntax parser to parse the structured answer and attempting to construct an abstract syntax tree, the legality of the generated content's code structure can be precisely verified. If the syntax tree construction fails or the code structure is incomplete, it can be determined that it does not meet the requirements of executability or semantic correctness, thereby preventing invalid code or syntactically broken content from entering the final answer and improving the usability and engineering feasibility of the output results.
[0153] In one example embodiment of this disclosure, source code matching verification of code query questions and structured answers can be performed through the following steps to obtain source code matching verification results, which may specifically include: The system can extract the identifier information of the referenced target code units from the structured answer; based on the symbol inverted index and / or semantic vector index in the multi-dimensional code index, it can search for code units in the target code library that match the identifier information; if no matching code unit is found, the source code matching verification result is determined to be unsuccessful, otherwise the source code matching verification result is determined to be successful.
[0154] The identification information refers to semantic clues that can uniquely or relatively uniquely locate a code unit. For example, the identification information may include, but is not limited to, function names, class names, module names, method identifiers, variable names, or other sets of identifiers that can reflect the boundaries and functions of a code unit. Of course, the identification information may also include path prefixes, namespaces, package names, or line number ranges to enhance the location capability, and this embodiment does not impose any special limitations on this.
[0155] After extracting the identification information, matching searches can be performed using the symbolic inverted index and / or semantic vector index in the multi-dimensional code index. When searching using the symbolic inverted index, the identification information extracted from the structured answer can be directly used as the query term to check if the corresponding code unit exists in the target code library. In some implementation scenarios, relying solely on the symbolic inverted index may not be sufficient to accurately locate the identification information because the identification information displayed in the structured answer may have semantic variations, inconsistent name mappings, or be abstracted by the model. Therefore, in this embodiment, matching searches can also be performed based on the semantic vector index. By calculating the similarity between the semantic vector of the identification information or code fragment extracted from the structured answer and the semantic vector of the code unit in the code library, the most likely corresponding code entity can be determined. For example, when the structured answer does not directly give a function name but displays a representative piece of code logic, this logic fragment can be encoded into a vector and similar code units can be searched in the semantic vector index to achieve semantic-based localization.
[0156] Optionally, when performing source code matching verification, the structural relationship index can be used to further confirm the accuracy of the match. The structural relationship index can represent structural relationships such as call relationships, inheritance relationships, and module dependencies between code units. When the structured answer contains a description of a certain logical chain, such as "this function is called by module A and implemented in class B", the structural relationship index can be used to confirm whether the matched code unit is in the correct call chain position, thereby further improving the matching quality.
[0157] After the search is complete, a set of candidate code units corresponding to the structured answer identifier information is obtained. If no corresponding identifier or semantically similar code unit is found from any index during the matching search, the source code matching verification result is determined to be unsuccessful. This can be attributed to the content in the structured answer not actually existing in the target code library, possibly due to model "illusion," model inference errors, missing information generation, or inconsistent data references. If the matching search successfully locates a code unit, and the content of that code unit is consistent with the code snippet or functional description shown in the structured answer, the source code matching verification result is determined to be successful.
[0158] By extracting the identifiers of referenced code units from the structured answers and searching for the actual code in the target codebase based on symbolic inverted indexes and / or semantic vector indexes, it is possible to verify whether the answer truly references real code entities within the codebase, preventing the answer from containing fictitious functions or non-existent implementation paths. This verification ensures strict consistency between the structured answers and the target codebase, enhancing the system's credibility from the perspective of source authenticity.
[0159] In one example embodiment of this disclosure, code logic verification of code query questions and structured answers can be performed through the following steps to obtain code logic verification results, which may specifically include: The code query question and structured answer can be input into an independent validation language model, which will output an evaluation result on the logical correctness and / or internal consistency of the structured answer. If the evaluation result is unsuccessful, the code logic validation result is determined to be unsuccessful; otherwise, the code logic validation result is determined to be successful.
[0160] The language model used for validation differs from the semantic evaluation model previously used for semantic reordering. It can be specially trained for tasks such as logical reasoning, causal relationship judgment, and code behavior consistency analysis, giving it a more rigorous ability to judge logical consistency.
[0161] When performing code logic verification, the structured answer and the code query question can first be adapted and combined so that they can be input into the verification language model in a unified format. For example, the query question can be placed at the beginning, followed by a functional description fragment, code fragment, and logical explanation of the structured answer, allowing the verification language model to simultaneously observe the correspondence between the query intent and the answer logic during reasoning. In some optional implementations, additional contextual hints can be provided to the verification language model, such as the file path, module name, or class structure of the code unit referenced by the structured answer, enabling the verification language model to more accurately understand the logical context behind the structured answer during the reasoning process.
[0162] When processing input, the validation language model can evaluate the logical relationships between statements in a structured answer based on its internal attention mechanism, semantic-logical alignment capabilities gained through self-supervised learning, and code understanding knowledge accumulated during training. For example, if a structured answer describes a function checking parameter validity before execution, but the code snippet lacks corresponding conditional logic, the validation language model might determine that the answer is logically inconsistent. Similarly, if a structured answer states that an exception throwing path is triggered when a certain condition is met, but the code snippet in the structured answer does not contain this exception throwing statement, the validation language model may also consider it logically incoherent.
[0163] In some alternative implementations, the verification language model can also identify potential contradictions in the structured answer, including inconsistencies between the functional description and the code snippet, disordered statement order, missing causal logic, and unexpected variable value reasoning. For example, if the structured answer describes a variable as being initialized upon entering the function, but the presented code snippet does not contain variable initialization logic, the verification language model can infer a logical error in the answer based on code semantics. In another alternative implementation, the verification language model can also identify redundant or invalid logic in the structured answer, such as code snippets containing statements that will never be executed or branch conditions that cannot be satisfied, in order to determine whether the structured answer's explanation of code behavior is truthful and credible.
[0164] After performing logical verification, the verification language model outputs an evaluation result representing logical consistency. This evaluation result can be in the form of labels, such as "pass" or "fail"; it can also be a confidence score, such as a continuous numerical value representing the consistency between the structured answer and the code behavior; in some optional ways, the verification language model can also output error location information, such as indicating which description or line of code the inconsistent logic is located in, or what type of logical deviation it is.
[0165] When the evaluation results output by the language model for verification indicate that the logical description of the structured answer is inconsistent with the intent of the code query question or the behavior of the source code, the code logic verification result can be directly determined as failing; otherwise, it can be determined as passing. Optionally, necessary thresholds can be set for logic verification, such as considering it as failing if the model confidence score is below a certain threshold, to ensure that the final structured answer used for output has sufficiently high logical reliability.
[0166] By using a language model to independently validate code query questions and structured answer inputs and obtaining evaluation results on logical correctness and consistency, the system can perform secondary judgment on the logical chain, behavioral rationality, and internal consistency of the generated answer. This makes the generation mechanism and the validation mechanism independent and complementary, enabling the capture of semantically consistent but logically erroneous outputs and improving the system's robustness in complex semantic understanding scenarios.
[0167] In one example embodiment of this disclosure, the cross-validation result can be determined based on the relevance verification result, integrity verification result, source code matching verification result, and code logic verification result through the following steps, which may specifically include: The results of relevance verification, integrity verification, source code matching verification, and code logic verification can be quantified into standardized scores. Based on preset verification weights, the standardized scores are weighted and calculated to obtain a comprehensive confidence score. If the comprehensive confidence score is less than or equal to the preset confidence threshold, the cross-validation result is determined to be unsuccessful; if the comprehensive confidence score is greater than the preset confidence threshold, the cross-validation result is determined to be successful.
[0168] Standardized scoring refers to converting discrete judgment results such as "pass" or "fail" into weighted scores with a defined range. For example, "pass" can be mapped to 1 and "fail" to 0. More granular interval values can also be used. For example, "pass" with higher confidence can be mapped to an interval of 0.8 to 1, and "pass" with lower confidence can be mapped to an interval of 0.5 to 0.8. This embodiment does not impose any special limitations on the quantification method of standardized scoring.
[0169] The preset verification weights refer to numerical settings for the importance of different verification types, used to reflect the greater criticality of certain verification dimensions in judging the reliability of structured answers. For example, in some implementations, a higher weight can be set for source code matching verification to emphasize that the structured answer must truly correspond to the target code library; in other implementations, a higher weight can be assigned to code logic verification to ensure that the structured answer has sufficient rigor at the logical reasoning level. In addition, the weight combination can be adjusted based on the actual application scenario. For example, semantic relevance verification is emphasized more in automated testing systems, while integrity verification is emphasized more in security-sensitive scenarios. This embodiment is not limited to this.
[0170] In calculating the overall confidence score, a weighted summation model can be used, where each standardized score is multiplied by its corresponding weight and then summed to obtain a numerical value representing the overall reliability of the structured answer. For example, if the logical verification weight is high and its corresponding score is low, the overall confidence score may decrease significantly, reflecting the impact of insufficient logical consistency on overall reliability. In other implementations, more complex weighting methods can be used, such as normalized weighted models, exponential weighted models, or confidence interval-based weighted models, to improve the stability and discriminative power of the score.
[0171] After obtaining the overall confidence score, the final pass rate of cross-validation can be determined based on a pre-set reliability threshold. The pre-set reliability threshold is the lower limit for distinguishing between acceptable and unacceptable answers; for example, it can be set to 0.6, 0.7, or 0.8. Its value can be dynamically adjusted based on factors such as system requirements, scenario risk level, codebase complexity, or historical data performance. When the overall confidence score is less than or equal to the pre-set reliability threshold, the structured answer is deemed unreliable, and the cross-validation result is considered a failure. When the overall confidence score is higher than the pre-set reliability threshold, the structured answer is confirmed to have passed all validation stress tests, and the cross-validation result is considered a success.
[0172] In this embodiment, after generating a structured answer, the system sequentially performs relevance verification, completeness verification, source code matching verification, and consistency verification on the structured answer. Each verification path outputs a corresponding verification result or score, and the system can calculate a comprehensive confidence index based on these multiple verification results. When the comprehensive confidence score is higher than a preset threshold, the system confirms that the current structured answer meets the output conditions and returns it to the user as the final answer; when the comprehensive confidence score is lower than the preset threshold, the system automatically triggers a regeneration process, regenerating the structured answer based on the same set of candidate codes and executing the above verification steps again. Through this method, a clear technical triggering relationship is formed between the verification results and the regeneration process, enabling the answer generation process to automatically iterate without manual intervention until the preset confidence requirements are met, thereby improving the accuracy and reliability of the answer.
[0173] By quantifying the four types of validation results into standardized scores and calculating a comprehensive confidence score based on preset weights, the system can balance the importance of different validation dimensions within a unified numerical framework, ensuring that the final result is not affected by accidental fluctuations in a single dimension. Using a confidence threshold to judge the comprehensive score provides a clear pass criterion for the cross-validation process, ensuring that the output of the structured answer is based on the convergence of multi-dimensional validation, thereby significantly enhancing the credibility and stability of the final answer.
[0174] In one example implementation, the process of answer generation and verification is illustrated by taking the natural language code query question entered by the developer as “What is the reason for the user’s login failure?”
[0175] In the example implementation, the system can first perform semantic retrieval, symbolic retrieval, and structural retrieval operations based on a multi-dimensional code index to obtain multiple candidate code units. For example, the semantic retrieval path can recall authentication processing functions related to semantics such as "login failure" and "authentication exception" as code units; the symbolic retrieval path can match login interfaces or authentication services based on keywords such as "login," "auth," and "token"; and the structural retrieval path can further expand to obtain exception handling logic or logging functions that have call or dependency relationships with the above code units as code units.
[0176] After obtaining multiple candidate result sets, the system can merge and sort the candidate code units, and input the preliminary fusion result set into a semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates and re-ranks the semantic relevance between the candidate code units and the code query question. After re-ranking, the system can prioritize code units that are highly relevant to the "login failure reason," such as target code units used to verify user credentials, verify access tokens, or record login exception information.
[0177] Subsequently, the system can generate a structured answer based on the reordered candidate result set. For example, the structured answer may include the identification information of the target code unit, the corresponding code snippet, and a logical explanation of the reason for the login failure. For example, it may explain that the login process will return a failure result when the user credential verification fails, the access token expires, or the exception capture branch is triggered.
[0178] After generating a structured answer, the system performs cross-validation on it. In the relevance verification phase, the system uses a semantic matching model to determine the semantic consistency between the structured answer and the query "reason for user login failure." In the source code matching verification phase, the system extracts the identifier information of the referenced target code units from the structured answer and searches for the corresponding source code in the target code library to confirm the actual existence of the code units involved in the answer. In the code logic verification phase, the system inputs the code query question and the structured answer into a verification language model to determine whether the logical explanation of the login failure reason in the answer is consistent with the actual execution logic of the target code unit. After all the above verifications pass, the system calculates a comprehensive confidence score based on the relevance verification results, source code matching verification results, and code logic verification results. If the comprehensive confidence score meets a preset threshold, the structured answer is output as the final question-and-answer result. If the comprehensive confidence score does not meet the preset threshold, the system regenerates the structured answer based on the reordered candidate result set and performs the verification process again until a valid structured answer is obtained.
[0179] In another embodiment, the user submits a query request to the system, such as "What is the reason for the user's login failure in a certain code repository?"
[0180] The system first processes user queries and performs parallel searches on semantic retrieval, symbolic retrieval, and structural retrieval paths. The semantic retrieval path retrieves functions or classes in the semantic index that are semantically similar to "login failure reason" based on the query's semantics. The symbolic retrieval path retrieves code units related to login, authentication, etc., in the symbolic index based on key symbolic information involved in the query. The structural retrieval path starts with the retrieval results from the semantic and symbolic retrieval paths, expands on the call graph and dependency relationships, and obtains context code that has call or dependency relationships with the candidate code units.
[0181] Subsequently, the system merges and sorts the candidate results from each search path to obtain a preliminary candidate code set, which is then input into the semantic re-ranking module. The semantic re-ranking module performs a deep semantic matching evaluation of the candidate results based on the user query and the candidate codes and their annotation information, and then reorders the candidate priorities.
[0182] After obtaining the sorted candidate codes, the system converts them into structured answers and sequentially performs relevance verification, completeness verification, source code matching verification, and consistency verification on the answers. The system calculates a comprehensive confidence score based on each verification result. When the comprehensive confidence score meets a preset threshold, the current structured answer is output as the final answer; when the comprehensive confidence score does not reach the preset threshold, the system automatically triggers a regeneration process, regenerating the structured answer and performing verification again until the output conditions are met.
[0183] Through the above process, the system can output a semantically consistent, structurally complete, and reliable answer based on the actual codebase content without human intervention.
[0184] It should be noted that although the steps of the method in this disclosure are described in a specific order in the accompanying drawings, this does not require or imply that the steps must be performed in that specific order, or that all the steps shown must be performed to achieve the desired result. Additional or alternative steps may be omitted, multiple steps may be combined into one step, and / or a step may be broken down into multiple steps.
[0185] Furthermore, this example embodiment also provides a code semantic understanding and hybrid retrieval question-answering system for AI programming. (Refer to...) Figure 6As shown, the code semantic understanding and hybrid retrieval question answering system 600 for AI programming may include at least: a code parsing and indexing module 610, a hybrid retrieval module 620, a fusion ranking module 630, a semantic re-ranking module 640, and an answer generation and verification module 650. Wherein: The code parsing and indexing module 610 is used to parse the target code library and construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. The hybrid retrieval module 620 is used to receive code query questions in natural language form, and based on the code query questions and the multi-dimensional code index, to perform retrieval operations in parallel, including at least semantic retrieval, symbol retrieval and structural retrieval, to obtain multiple candidate result sets; The fusion sorting module 630 is used to perform ranking-based parameterless fusion on the multiple candidate result sets to generate a preliminary fusion result set; The semantic reordering module 640 is used to input the code query problem and the preliminary fusion result set into the semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and reorders them, and outputs the reordered candidate result set. The answer generation and verification module 650 is used to perform cross-validation based on the reordered candidate result set and output the structured answer after the cross-validation is passed.
[0186] In some example embodiments of this disclosure, based on the foregoing scheme, the code parsing and indexing module 610 is configured as follows: The source code in the target code library is parsed by a preset syntax parser to extract code units in the form of functions or classes. The code units include docstrings, function signatures, internal calls and dependency information. A multi-dimensional code index is constructed based on the code units.
[0187] In some example embodiments of this disclosure, based on the foregoing scheme, the code parsing and indexing module 610 is configured as follows: Based on the trained code semantic model, each code unit is converted into a semantic vector, and the semantic vector index is constructed using the semantic vector. Extract the named body information from each code unit, and construct the symbol inverted index using the named body information; Based on the calling relationships, inheritance relationships, and module dependencies between the code units, a graph structure data is constructed, and a structural relationship index is formed based on the graph structure data.
[0188] In some example embodiments of this disclosure, based on the foregoing scheme, the plurality of candidate result sets include a first candidate result set under the semantic retrieval path, a second candidate result set under the symbol retrieval path, and a third candidate result set under the structure retrieval path; the hybrid retrieval module 620 is configured as follows: The code query question is converted into a query vector, the similarity between the query vector and each semantic vector in the semantic vector index is calculated, and the first candidate result set is obtained based on the similarity ranking. Keywords are extracted from the code query question, and code units corresponding to named body information containing the keywords or fuzzily matching the keywords are matched based on the symbol inverted index to obtain the second candidate result set; Starting with the code units in the first candidate result set and / or the second candidate result set, a bounded traversal is performed on the graph structure data represented by the structural relationship index to obtain the associated code units that have a calling, inheritance or dependency relationship with the starting point, thus forming the third candidate result set.
[0189] In some example embodiments of this disclosure, based on the foregoing scheme, the fusion sorting module 630 is configured as follows: For each candidate code unit that appears in the candidate result set, obtain multiple ranking data of the candidate code unit in each candidate result set; Summing the reciprocals of each ranking data point yields the weighted fusion score for each candidate code unit. All candidate code units are reordered based on the weighted fusion score to generate the preliminary fusion result set.
[0190] In some example embodiments of this disclosure, based on the foregoing scheme, the semantic reordering module 640 is configured as follows: The code query question is combined with each candidate code unit in the preliminary fusion result set to obtain evaluation input data; The evaluation input data is input into the semantic evaluation model to obtain an evaluation score that characterizes the semantic relevance between each candidate code unit and the code query question; All candidate code units are sorted in descending order based on the evaluation scores, and candidate code units whose evaluation scores are less than or equal to a preset score threshold are filtered out to obtain the reordered candidate result set.
[0191] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The reordered candidate result set is converted into a structured answer, and the code query question and the structured answer are cross-validated to obtain the cross-validation results. If the cross-validation result is passed, then output the structured answer after successful cross-validation; If the cross-validation result is unsuccessful, a new structured answer is generated based on the reordered candidate result set, and the new structured answer is cross-validated again until the cross-validation result of the latest structured answer is successful, then the latest structured answer is output.
[0192] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The relevance of the code query question and the structured answer is verified to obtain the relevance verification results; The integrity of the code query question and the structured answer is verified to obtain the integrity verification result; The source code of the code query question and the structured answer are matched and verified to obtain the source code matching and verification results. The code query question and the structured answer are subjected to code logic verification to obtain the code logic verification results; The cross-validation result is determined based on the correlation verification result, the integrity verification result, the source code matching verification result, and the code logic verification result.
[0193] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The code query question and the structured answer are input into a trained semantic matching model, which calculates the semantic consistency score between the code query question and the structured answer. If the semantic consistency score is less than or equal to the preset consistency score threshold, the relevance verification result is determined to be unsuccessful. If the semantic consistency score is greater than the preset consistency score threshold, the relevance verification result is determined to be passed.
[0194] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The structured answer is parsed using a preset syntax parser, and an abstract syntax tree is constructed in an attempt. If a grammatical error is found in the structured answer during the construction of the abstract syntax tree, or if the grammatical structure of the structured answer is determined to be incomplete, then the integrity verification result is determined to be unsuccessful; otherwise, the integrity verification result is determined to be successful.
[0195] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: Extract the identification information of the referenced target code units from the structured answer; Based on the symbol inverted index and / or the semantic vector index in the multi-dimensional code index, search for code units in the target code library that match the identification information; If no matching code unit is found, the source code matching verification result is determined to be unsuccessful; otherwise, the source code matching verification result is determined to be successful.
[0196] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The code query question and the structured answer are input into an independent validation language model, which outputs an evaluation result on the logical correctness and / or internal consistency of the structured answer. If the evaluation result is "fail", then the code logic verification result is determined to be "fail"; otherwise, the code logic verification result is determined to be "pass".
[0197] In some example embodiments of this disclosure, based on the foregoing scheme, the answer generation and verification module 650 is configured as follows: The relevance verification results, the integrity verification results, the source code matching verification results, and the code logic verification results are quantified into standardized scores. The standardized score is weighted according to the preset verification weights to calculate the comprehensive confidence score. If the overall confidence score is less than or equal to the preset confidence threshold, then the cross-validation result is determined to be unsuccessful. If the overall confidence score is greater than the preset confidence threshold, then the cross-validation result is determined to be passed.
[0198] The specific details of each module of the AI-oriented code semantic understanding and hybrid retrieval question answering system have been described in detail in the corresponding AI-oriented code semantic understanding and hybrid retrieval question answering method, so they will not be repeated here.
[0199] It should be noted that although several modules or units of the code semantic understanding and hybrid retrieval question-answering system for AI programming have been mentioned in the detailed description above, this division is not mandatory. In fact, according to the embodiments of this disclosure, the features and functions of two or more modules or units described above can be embodied in one module or unit. Conversely, the features and functions of one module or unit described above can be further divided and embodied by multiple modules or units.
[0200] The division of functional modules in this disclosure is only used to describe the functional logic relationship when implementing the code semantic understanding and hybrid retrieval question answering scheme, and does not constitute a limitation on the physical number of modules, module boundaries, or module implementation forms. The core of this disclosure is not about setting a specific number of functional modules, but about introducing semantic reordering processing based on deep semantic understanding through a mutual constraint mechanism of multi-path retrieval results, combined with a verification closed-loop mechanism, to continuously verify and provide feedback on candidate results, thereby achieving high consistency and high reliability matching between natural language queries and the actual implementation of the target code library. In specific implementations, the aforementioned multi-path result constraints, semantic reordering, and verification closed-loop mechanisms can be implemented separately by multiple functional modules, merged in the same processing module, or completed at different stages in the same processing flow; the implementation order, module division method, or integration method between functions does not affect the technical problem to be solved or the technical effect achieved by this disclosure.
[0201] It should be noted that the functional modules described in this disclosure are merely logical functional divisions for the convenience of understanding the technical solution, and are not limited to existing in the form of physically independent modules. In specific implementations, the above functions can be implemented collaboratively by one or more processing units, or they can be implemented in different logical stages or different processing flows within the same processing unit; the implementation order, integration method, or module boundary division method between the functions do not affect the technical effects achieved by this disclosure through multi-path result constraints, semantic reordering, and verification closed-loop mechanisms.
[0202] Furthermore, in an exemplary embodiment of this disclosure, an electronic device capable of implementing the above-described code semantic understanding and hybrid retrieval question-answering method for AI programming is also provided.
[0203] Those skilled in the art will understand that various aspects of this disclosure can be implemented as a system, method, or program product. Therefore, various aspects of this disclosure can be embodied in the following forms: a completely hardware embodiment, a completely software embodiment (including firmware, microcode, etc.), or an embodiment combining hardware and software aspects, collectively referred to herein as a "circuit," "module," or "system."
[0204] The following reference Figure 7To describe an electronic device 700 according to such an embodiment of the present disclosure. Figure 7 The electronic device 700 shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments disclosed herein.
[0205] like Figure 7 As shown, the electronic device 700 is manifested in the form of a general-purpose computing device. The components of the electronic device 700 may include, but are not limited to: at least one processing unit 710, at least one storage unit 720, a bus 730 connecting different system components (including storage unit 720 and processing unit 710), and a display unit 740.
[0206] The storage unit stores program code that can be executed by the processing unit 710, causing the processing unit 710 to perform the steps described in the "Exemplary Methods" section of this specification according to various exemplary embodiments of this disclosure. For example, the processing unit 710 can perform actions such as... Figure 1 Step S110, as shown, involves parsing the target code library and constructing a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relation index. Step S120 involves receiving a code query question in natural language form and, based on the code query question and the multi-dimensional code index, performing retrieval operations in parallel, including at least semantic retrieval, symbol retrieval, and structural retrieval, to obtain multiple candidate result sets. Step S130 involves performing a ranking-based parameterless fusion on the multiple candidate result sets to generate a preliminary fusion result set. Step S140 involves inputting the code query question and the preliminary fusion result set into a semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and re-ranks them, outputting a re-ranked candidate result set. Step S150 involves performing cross-validation based on the re-ranked candidate result set and outputting a structured answer after successful cross-validation.
[0207] Storage unit 720 may include readable media in the form of volatile storage units, such as random access memory (RAM) 721 and / or cache memory (Cache) 722, and may further include read-only memory (ROM) 723.
[0208] The storage unit 720 may also include a program / utility 724 having a set (at least one) of program modules 725, including but not limited to: an operating system, one or more application programs, other program modules, and program data, each or some combination of these examples may include an implementation of a network environment.
[0209] Bus 730 can represent one or more of several types of bus structures, including a memory cell bus or memory cell controller, a peripheral bus, a graphics acceleration port, a processing unit, or a local bus using any of the various bus structures.
[0210] Electronic device 700 can also communicate with one or more external devices 770 (e.g., keyboard, pointing device, Bluetooth device, etc.), and with one or more devices that enable a user to interact with electronic device 700, and / or with any device that enables electronic device 700 to communicate with one or more other computing devices (e.g., router, modem, etc.). This communication can be performed via input / output (I / O) interface 750. Furthermore, electronic device 700 can also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN), and / or public networks, such as the Internet) via network adapter 760. As shown, network adapter 760 communicates with other modules of electronic device 700 via bus 730. It should be understood that, although not shown in the figures, other hardware and / or software modules can be used in conjunction with electronic device 700, including but not limited to: microcode, device drivers, redundant processing units, external disk drive arrays, RAID systems, tape drives, and data backup storage systems.
[0211] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, terminal device, or network device, etc.) to execute the methods according to the embodiments of this disclosure.
[0212] In exemplary embodiments of this disclosure, a computer-readable storage medium is also provided, on which a program product capable of implementing the methods described above is stored. In some possible embodiments, various aspects of this disclosure may also be implemented as a program product including program code that, when the program product is run on a terminal device, causes the terminal device to perform the steps described in the "Exemplary Methods" section of this specification according to various exemplary embodiments of this disclosure.
[0213] refer to Figure 8As shown, a program product 800 for implementing the above-described AI-oriented code semantic understanding and hybrid retrieval question answering method according to embodiments of the present disclosure is described. It may employ a portable compact disc read-only memory (CD-ROM) and include program code, and may run on a terminal device, such as a personal computer. However, the program product of the present disclosure is not limited thereto. In this document, 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, apparatus, or device.
[0214] The program product may employ any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A 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 (a non-exhaustive list) include: an electrical connection having one or more wires, a portable disk, a hard disk, 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.
[0215] Computer-readable signal media 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 signal medium may also be any readable medium other than a readable storage medium, capable of sending, propagating, or transmitting programs for use by or in conjunction with an instruction execution system, apparatus, or device.
[0216] The program code contained on the readable medium may be transmitted using any suitable medium, including but not limited to wireless, wired, optical fiber, RF, etc., or any suitable combination thereof.
[0217] Program code for performing the operations of this disclosure 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 execute entirely on the user's computing device, partially on the user's computing device, as a standalone software package, partially on the user's computing device and partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0218] Furthermore, the above figures are merely illustrative of the processes included in the method according to exemplary embodiments of this disclosure and are not intended to be limiting. It is readily understood that the processes shown in the above figures do not indicate or limit the temporal order of these processes. Additionally, it is readily understood that these processes may be executed synchronously or asynchronously, for example, in multiple modules.
[0219] From the above description of the embodiments, those skilled in the art will readily understand that the exemplary embodiments described herein can be implemented by software or by combining software with necessary hardware. Therefore, the technical solutions according to the embodiments of this disclosure can be embodied in the form of a software product, which can be stored in a non-volatile storage medium (such as a CD-ROM, USB flash drive, external hard drive, etc.) or on a network, including several instructions to cause a computing device (such as a personal computer, server, touch terminal, or network device, etc.) to execute the methods according to the embodiments of this disclosure.
[0220] Other embodiments of this disclosure will readily occur to those skilled in the art upon consideration of the specification and practice of the invention disclosed herein. This application is intended to cover any variations, uses, or adaptations of this disclosure that follow the general principles of this disclosure and include common knowledge or customary techniques in the art not disclosed herein. The specification and embodiments are to be considered exemplary only, and the true scope and spirit of this disclosure are indicated by the claims.
[0221] It should be understood that this disclosure is not limited to the precise structures described above and shown in the accompanying drawings, and various modifications and changes can be made without departing from its scope. The scope of this disclosure is limited only by the appended claims.
Claims
1. A code semantic understanding and hybrid retrieval question answering method for AI programming, characterized in that, include: The target code library is parsed to construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. The system receives a code query question in natural language form, and performs retrieval operations, including at least semantic retrieval, symbol retrieval, and structural retrieval, in parallel based on the code query question and the multi-dimensional code index, to obtain multiple candidate result sets. The multiple candidate result sets include a first candidate result set under the semantic retrieval path, a second candidate result set under the symbol retrieval path, and a third candidate result set under the structural retrieval path. The multiple candidate result sets are fused without parameters based on ranking to generate a preliminary fusion result set; The code query question and the preliminary fusion result set are input into a semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates the semantic relevance of the candidate code units in the preliminary fusion result set and reorders them, and outputs the reordered candidate result set. The reordered candidate result set is converted into a structured answer, and the code query question and the structured answer are cross-validated to obtain the cross-validation results. If the cross-validation result is passed, then output the structured answer after successful cross-validation; Specifically, based on the code query question and the multi-dimensional code index, retrieval operations including at least semantic retrieval, symbol retrieval, and structural retrieval are performed in parallel to obtain multiple candidate result sets, including: The code query question is converted into a query vector, the similarity between the query vector and each semantic vector in the semantic vector index is calculated, and the first candidate result set is obtained based on the similarity ranking. Keywords are extracted from the code query question, and code units corresponding to named body information containing the keywords or fuzzily matching the keywords are matched based on the symbol inverted index to obtain the second candidate result set; Starting with the code units in the first candidate result set and / or the second candidate result set, a bounded traversal is performed on the graph structure data represented by the structural relationship index to obtain the associated code units that have a calling, inheritance or dependency relationship with the starting point, thus forming the third candidate result set; The step of performing ranking-based parameterless fusion on the multiple candidate result sets to generate a preliminary fusion result set includes: For each candidate code unit that appears in the candidate result set, obtain multiple ranking data of the candidate code unit in each candidate result set; Summing the reciprocals of each ranking data point yields the weighted fusion score for each candidate code unit. Based on the weighted fusion score, all the candidate code units are reordered to generate the preliminary fusion result set; The cross-validation of the code query question and the structured answer to obtain the cross-validation results includes: The relevance of the code query question and the structured answer is verified to obtain the relevance verification results; The integrity of the code query question and the structured answer is verified to obtain the integrity verification result; The source code of the code query question and the structured answer are matched and verified to obtain the source code matching and verification results. The code query question and the structured answer are subjected to code logic verification to obtain the code logic verification results; The cross-validation result is determined based on the correlation verification result, the integrity verification result, the source code matching verification result, and the code logic verification result.
2. The method according to claim 1, characterized in that, The construction yields a multi-dimensional code index, including: The source code in the target code library is parsed by a preset syntax parser to extract code units in the form of functions or classes. The code units include docstrings, function signatures, internal calls and dependency information. A multi-dimensional code index is constructed based on the code units.
3. The method according to claim 2, characterized in that, The multi-dimensional code index constructed based on the code unit includes: Based on the trained code semantic model, each code unit is converted into a semantic vector, and the semantic vector index is constructed using the semantic vector. Extract the named body information from each code unit, and construct the symbol inverted index using the named body information; Based on the calling relationships, inheritance relationships, and module dependencies between the code units, a graph structure data is constructed, and a structural relationship index is formed based on the graph structure data.
4. The method according to claim 1, characterized in that, The semantic evaluation model performs semantic relevance evaluation and re-ranking on the candidate code units in the preliminary fusion result set, outputting a re-ranked candidate result set, including: The code query question is combined with each candidate code unit in the preliminary fusion result set to obtain evaluation input data; The evaluation input data is input into the semantic evaluation model to obtain an evaluation score that characterizes the semantic relevance between each candidate code unit and the code query question; All candidate code units are sorted in descending order based on the evaluation scores, and candidate code units whose evaluation scores are less than or equal to a preset score threshold are filtered out to obtain the reordered candidate result set.
5. The method according to claim 1, characterized in that, The method further includes: If the cross-validation result is unsuccessful, a new structured answer is generated based on the reordered candidate result set, and the new structured answer is cross-validated again until the cross-validation result of the latest structured answer is successful, then the latest structured answer is output.
6. The method according to claim 1, characterized in that, The relevance verification of the code query question and the structured answer, to obtain the relevance verification result, includes: The code query question and the structured answer are input into a trained semantic matching model, which calculates the semantic consistency score between the code query question and the structured answer. If the semantic consistency score is less than or equal to the preset consistency score threshold, the relevance verification result is determined to be unsuccessful. If the semantic consistency score is greater than the preset consistency score threshold, the relevance verification result is determined to be passed.
7. The method according to claim 1, characterized in that, The completeness verification of the code query question and the structured answer, to obtain the completeness verification result, includes: The structured answer is parsed using a preset syntax parser, and an abstract syntax tree is constructed in an attempt. If a grammatical error is found in the structured answer during the construction of the abstract syntax tree, or if the grammatical structure of the structured answer is determined to be incomplete, then the integrity verification result is determined to be unsuccessful; otherwise, the integrity verification result is determined to be successful.
8. The method according to claim 1, characterized in that, The process of performing source code matching verification on the code query question and the structured answer to obtain the source code matching verification result includes: Extract the identification information of the referenced target code units from the structured answer; Based on the symbol inverted index and / or the semantic vector index in the multi-dimensional code index, search for code units in the target code library that match the identification information; If no matching code unit is found, the source code matching verification result is determined to be unsuccessful; otherwise, the source code matching verification result is determined to be successful.
9. The method according to claim 1, characterized in that, The step of performing code logic verification on the code query question and the structured answer to obtain the code logic verification result includes: The code query question and the structured answer are input into an independent validation language model, which outputs an evaluation result on the logical correctness and / or internal consistency of the structured answer. If the evaluation result is "fail", then the code logic verification result is determined to be "fail"; otherwise, the code logic verification result is determined to be "pass".
10. The method according to claim 1, characterized in that, The step of determining the cross-validation result based on the relevance verification result, the integrity verification result, the source code matching verification result, and the code logic verification result includes: The relevance verification results, the integrity verification results, the source code matching verification results, and the code logic verification results are quantified into standardized scores. The standardized score is weighted according to the preset verification weights to calculate the comprehensive confidence score. If the overall confidence score is less than or equal to the preset confidence threshold, then the cross-validation result is determined to be unsuccessful. If the overall confidence score is greater than the preset confidence threshold, then the cross-validation result is determined to be passed.
11. A code semantic understanding and hybrid retrieval question-answering system for AI programming, characterized in that, At least including: The code parsing and indexing module is used to parse the target code library and construct a multi-dimensional code index, which includes at least a semantic vector index, a symbol inverted index, and a structural relationship index. The hybrid retrieval module is used to receive code query questions in natural language form, and based on the code query questions and the multi-dimensional code index, to perform retrieval operations in parallel, including at least semantic retrieval, symbol retrieval and structural retrieval, to obtain multiple candidate result sets; the multiple candidate result sets include a first candidate result set under the semantic retrieval path, a second candidate result set under the symbol retrieval path and a third candidate result set under the structural retrieval path; The fusion and sorting module is used to perform ranking-based parameterless fusion of the multiple candidate result sets to generate a preliminary fusion result set; The semantic reordering module is used to input the code query question and the preliminary fusion result set into the semantic evaluation model based on deep semantic understanding. The semantic evaluation model evaluates and reorders the candidate code units in the preliminary fusion result set and outputs the reordered candidate result set. The answer generation and verification module is used to convert the reordered candidate result set into a structured answer, and to perform cross-validation on the code query question and the structured answer to obtain the cross-validation result; if the cross-validation result is successful, the structured answer after successful cross-validation is output. Specifically, based on the code query question and the multi-dimensional code index, retrieval operations including at least semantic retrieval, symbol retrieval, and structural retrieval are performed in parallel to obtain multiple candidate result sets, including: The code query question is converted into a query vector, the similarity between the query vector and each semantic vector in the semantic vector index is calculated, and the first candidate result set is obtained based on the similarity ranking. Keywords are extracted from the code query question, and code units corresponding to named body information containing the keywords or fuzzily matching the keywords are matched based on the symbol inverted index to obtain the second candidate result set; Starting with the code units in the first candidate result set and / or the second candidate result set, a bounded traversal is performed on the graph structure data represented by the structural relationship index to obtain the associated code units that have a calling, inheritance or dependency relationship with the starting point, thus forming the third candidate result set; The step of performing ranking-based parameterless fusion on the multiple candidate result sets to generate a preliminary fusion result set includes: For each candidate code unit that appears in the candidate result set, obtain multiple ranking data of the candidate code unit in each candidate result set; Summing the reciprocals of each ranking data point yields the weighted fusion score for each candidate code unit. Based on the weighted fusion score, all the candidate code units are reordered to generate the preliminary fusion result set; The cross-validation of the code query question and the structured answer to obtain the cross-validation results includes: The relevance of the code query question and the structured answer is verified to obtain the relevance verification results; The integrity of the code query question and the structured answer is verified to obtain the integrity verification result; The source code of the code query question and the structured answer are matched and verified to obtain the source code matching and verification results. The code query question and the structured answer are subjected to code logic verification to obtain the code logic verification results; The cross-validation result is determined based on the correlation verification result, the integrity verification result, the source code matching verification result, and the code logic verification result.
Citation Information
Patent Citations
System and method for natural language code search
CN117957523A
Retrieval enhanced code generation method and device, electronic equipment and storage medium
CN118778941A