Private intelligent code collaborative optimization method and system based on graph retrieval
By constructing and updating a code knowledge graph in real time, and dynamically associating runtime logs with function nodes in the graph, the problem of isolation between code knowledge and runtime logs is solved. This achieves deep integration of code structure and runtime behavior, improving the efficiency and accuracy of troubleshooting and system understanding.
Patent Information
- Application Number
- CN202511888319.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-12-15
- Publication Date
- 2026-01-13
- Estimated Expiration
- 2045-12-15
AI Technical Summary
In existing technologies, code knowledge and runtime logs are isolated from each other, lacking a unified and interconnected knowledge system. This leads to a disconnect between "discovering problems" and "locating code," and the static code analysis results cannot respond in real time to project iterations and changes, posing a risk of information lag.
By acquiring the source code and runtime log data of a private project, an abstract syntax tree is generated using a Tree-sitter parser, a code knowledge graph is constructed, the code knowledge graph is updated in real time, runtime logs are associated with function nodes, and a smart agent is used to parse natural language questions into Cypher query statements to obtain query results from a graph database.
It achieves deep integration of code structure and runtime behavior, builds a unified and interconnected dynamic knowledge system, and realizes seamless connection between logging and code analysis, improving the efficiency and accuracy of fault diagnosis, impact analysis and system understanding.
Smart Images

Figure CN121326348A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of intelligent auxiliary tools for software engineering, and in particular to a private intelligent code collaboration optimization method and system based on graph retrieval. Background Technology
[0002] In software development and operation, the source code and runtime logs of private projects are two crucial assets. The source code embodies the system's business logic and design philosophy, while the runtime logs record the system's behavioral status in real time. As projects grow in scale and architecture becomes more complex, the dependencies between code modules become increasingly intricate, and the massive amounts of log data become difficult to directly correlate with specific code logic.
[0003] Currently, optimization for private intelligent code collaboration based on graph retrieval mainly employs tools based on static code analysis. These tools parse source code to generate call graphs, dependency graphs, etc., helping developers understand the code structure. There are also independent log management platforms for collecting, storing, and retrieving logs using keywords. These technologies typically operate independently.
[0004] However, code knowledge and runtime logs are often isolated from each other, lacking a unified and interconnected knowledge system, leading to a disconnect between the "problem discovery" and "code localization" stages. Existing code analysis techniques often produce static results, failing to respond in real-time to project iterations and changes, and posing a risk of information lag. Summary of the Invention
[0005] In view of the shortcomings of the prior art, the purpose of this invention is to provide a private intelligent code collaboration optimization method based on graph retrieval. This method can solve the problem that existing technologies have issues such as code knowledge and runtime logs being isolated from each other, lacking a unified and interconnected knowledge system, leading to a disconnect between the "problem discovery" and "code location" stages. Furthermore, the code analysis results of existing technologies are often static and cannot respond in real time to project iterations and changes, posing a risk of information lag.
[0006] A first aspect of this invention proposes a private intelligent code collaboration optimization method based on graph retrieval, comprising: S1: Obtain the source code files and runtime log data of a private project; S2: The Tree-sitter parser performs syntax tree analysis on the source code file and generates the corresponding abstract syntax tree; S3: Based on the abstract syntax tree, it constructs a code knowledge graph by analyzing function call relationships, inheritance relationships, and module dependency relationships; S4: Monitor changes to project files in the code knowledge graph through code change event handlers, and update the code knowledge graph in real time when files change; S5: Establish a relationship between runtime log data and the corresponding function nodes in the code knowledge graph; S6: Based on the relationship, through the intelligent agent, the developer's natural language questions are parsed into Cypher query statements; S7: Retrieve query results from the graph database based on the Cypher query statement; S8: Format the query results and generate the final result.
[0007] A second aspect of this invention proposes a private intelligent code collaboration optimization system based on graph retrieval, comprising: a processor and a memory; The memory stores programs or instructions that can run on the processor, which, when executed by the processor, implement the steps of the private intelligent code collaboration optimization method based on graph retrieval as described in the first aspect.
[0008] A third aspect of the present invention provides a readable storage medium on which a program or instructions are stored, wherein when the program or instructions are executed by a processor, the program or instructions implement the steps of the private intelligent code collaboration optimization method based on graph retrieval as described in the first aspect.
[0009] The beneficial effects of the technical solutions provided in the embodiments of the present invention include at least the following: In this embodiment of the invention, by constructing and updating a code knowledge graph in real time, the runtime logs are dynamically associated with function nodes in the graph, effectively solving the problems of isolation between code knowledge and runtime logs, as well as the static lag of traditional code analysis. This innovation achieves deep integration of code structure and runtime behavior, constructing a unified and interconnected dynamic knowledge system. This seamlessly connects the logging stage of "discovering problems" with the analysis stage of "locating code," enabling developers to trace back to specific function logic, dependent modules, and even change history from log events based on the latest code context. This greatly improves the efficiency and accuracy of troubleshooting, impact analysis, and system understanding. Attached Figure Description
[0010] The accompanying drawings are for illustrative purposes only and are not intended to limit the invention. Throughout the drawings, the same reference numerals denote the same parts. Obviously, the drawings described below are merely some embodiments of the present invention, and those skilled in the art can obtain other drawings based on these drawings without any creative effort.
[0011] Figure 1 This is a flowchart illustrating a private intelligent code collaboration optimization method based on graph retrieval provided in an embodiment of the present invention.
[0012] Figure 2 This is a schematic diagram of a graph database provided in an embodiment of the present invention.
[0013] Figure 3 This is a schematic diagram of the structure of a private intelligent code collaboration optimization system based on graph retrieval provided in an embodiment of the present invention. Detailed Implementation
[0014] To enable those skilled in the art to better understand the technical solutions in the embodiments of the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, not all embodiments. It should be understood that these descriptions are merely exemplary and are not intended to limit the scope of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort should fall within the scope of protection of the present invention.
[0015] The following description, in conjunction with the accompanying drawings, details the private intelligent code collaboration optimization method based on graph retrieval provided by the present invention through specific embodiments and application scenarios.
[0016] Reference manual attached Figure 1 The diagram illustrates a flowchart of a private intelligent code collaboration optimization method based on graph retrieval provided in an embodiment of the present invention.
[0017] This invention provides a private intelligent code collaboration optimization method based on graph retrieval, which may include the following steps: S1: Obtain the source code files and runtime log data of the private project.
[0018] Private projects refer to software development projects whose source code access is strictly restricted and not open to the public.
[0019] Source code files refer to text files written by developers, containing program logic and data structures, and saved in a specific programming language format.
[0020] Among them, runtime log data refers to the time-series data stream that is automatically generated by the software system during runtime, recording its internal state, key events and error information.
[0021] Specifically, the system first scans the project root directory and automatically identifies the package and folder hierarchy of the code, providing path indexes for subsequent entity extraction and dependency analysis.
[0022] S2: The Tree-sitter parser performs syntax tree analysis on the source code file and generates the corresponding abstract syntax tree.
[0023] Among them, the Tree-sitter parser is an incremental parser tool that can handle multiple programming languages and is used for real-time analysis of source code structure.
[0024] Syntax tree analysis refers to the computational process of taking source code as input and constructing a structured tree representation by parsing its syntax rules. Abstract Syntax Tree (AST) refers to a tree-like abstract representation that discards specific syntactic details such as parentheses and semicolons, retaining only the program's logical structure.
[0025] In this embodiment of the invention, by converting the text form of the source code into a structured tree data model, a precise syntactic structure foundation is laid for the subsequent automated construction of the code knowledge graph.
[0026] In one possible implementation, S2 specifically includes: S201: Initialize the Tree-sitter resolver.
[0027] S202: Generate a query pattern based on the initialized Tree-sitter parser.
[0028] S203: In query mode, perform syntax parsing on each source code file to generate the corresponding abstract syntax tree.
[0029] Specifically, a Tree-sitter parser is used to perform syntax tree analysis on each source code file, extracting code entity information such as functions, classes, methods, variables, and comments, and generating a corresponding Abstract Syntax Tree (AST) for subsequent structured node construction. Parser loading and initialization: The system loads all available Tree-sitter parsers using the `load_parsers()` function. Query pattern generation: The system generates Tree-sitter query patterns for each language. Syntax tree parsing: File parsing is performed, and the AST is parsed for each source code file to generate the AST. Finally, module qualified name construction is performed.
[0030] In this embodiment of the invention, a standardized parsing process ensures that source code from different programming languages can be accurately and consistently converted into a structured abstract syntax tree, providing a reliable grammatical foundation for subsequent analysis.
[0031] S3: Based on the abstract syntax tree, it constructs a code knowledge graph by analyzing function call relationships, inheritance relationships, and module dependency relationships.
[0032] Among them, function call relationship refers to the order and dependency of mutual calls between different functions in the program. By analyzing the call chain, the program execution flow and data flow can be understood.
[0033] Inheritance refers to the parent-child hierarchical relationship between classes in object-oriented programming, enabling subclasses to inherit the characteristics and methods of their parent classes, thus forming the core reuse mechanism of object-oriented programming.
[0034] Among them, module dependency refers to the organizational structure that describes the mutual references and dependencies between different code modules, reflecting the code's organizational architecture and the degree of coupling between components.
[0035] Among them, code knowledge graph refers to a knowledge representation that organizes and manages code elements and their interrelationships in a graph structure, and fully displays the structured information of the code through nodes and edges.
[0036] In this embodiment of the invention, discrete code elements and their complex relationships are integrated into a unified and interconnected knowledge graph, realizing a systematic and visual representation of code structure and logical dependencies.
[0037] In one possible implementation, S3 specifically includes: S301: Based on the abstract syntax tree, extract function definitions, class definitions, and qualified names to generate code entity nodes.
[0038] A function definition refers to an executable code block declared in the source code using a specific syntax structure. It includes elements such as function name, parameter list, return type, and function body, and is used to encapsulate specific functional logic.
[0039] In object-oriented programming, a class definition refers to the code structure that describes an object blueprint. It is declared using keywords and includes elements such as attributes, methods, and inheritance relationships, and is used to create concrete objects with the same characteristics and behaviors.
[0040] The qualified name refers to the complete naming path used to uniquely identify code entities in programming. It usually includes multi-level namespace information such as module / package name and class name to avoid naming conflicts and ensure accurate referencing.
[0041] Among them, code entity nodes refer to the basic graph nodes that represent specific program elements (such as functions, classes, variables, etc.) in the code knowledge graph. They contain complete meta-information of the entity and serve as endpoints for relational connections.
[0042] S302: Based on code entity nodes, establish inter-function call relationships through function call relationship analysis.
[0043] S303: Based on the class definitions in the code entity nodes, establish class inheritance relationships through inheritance relationship analysis.
[0044] S304: Based on code entity nodes, establish inter-module dependencies through module dependency analysis.
[0045] S305: Integrate all code entity nodes, function call relationships, class inheritance relationships, and module dependency relationships to build a code knowledge graph.
[0046] Specifically, firstly, pattern matching is used to identify and extract all function definition nodes and class definition nodes. Then, to avoid naming conflicts and accurately locate entities, a globally unique qualified name is constructed for each extracted function or class. This qualified name is achieved by hierarchically concatenating the entity name with its module name and any possible parent class names, thus clearly representing its complete namespace path within the code project.
[0047] Furthermore, starting from the abstract syntax tree nodes of the function, the system extracts the call expressions from all its descendant nodes through pattern matching. For each call node, its syntax structure is parsed to extract the name of the called target, which may come from the function name field, identifier field, or operator overload. Finally, the system uses a name resolution function to combine the extracted target name with the context information of the current module to parse its fully qualified name, thereby accurately establishing the "CALLS" relationship edge from the caller to the callee and storing it in the code knowledge graph.
[0048] Specifically, different pattern recognition methods are employed based on the characteristics of different programming languages. For prototype-based languages (such as JavaScript), the system identifies inheritance relationships by querying specific assignment patterns in the syntax tree (e.g., `child.prototype = Object.create(parent.prototype)`). For class-based languages (such as C++ and Java), the system extracts direct inheritance relationships by directly parsing the superclass or base class declarations in class nodes. In either case, the system ultimately constructs the identified inheritance relationship as a directed edge in the code knowledge graph, in the form of "subclass INHERITS parent class".
[0049] Furthermore, by parsing import statements (such as `import` and `require`) in the source code, a mapping table is constructed from names used within the current module to their complete external module paths. Subsequently, the system uses this mapping table, combined with a predefined function registry and standard library path rules, to resolve qualified entity names used in the code to their respective module paths. Finally, these resolved module paths are used to create "DEPENDS_ON" relationship edges in the knowledge graph, precisely representing other internal modules or external libraries that the current module depends on.
[0050] Specifically, the system traverses the AST cache, analyzes function call relationships, inheritance relationships, and module dependency relationships, and automatically establishes semantic edges such as "CALLS", "IMPORTS", and "EXTENDS" in the knowledge graph to form a complete project-level code semantic network.
[0051] In this embodiment of the invention, by systematically extracting code entities and accurately establishing the calling, inheritance and dependency relationships among them, a comprehensive and interconnected code knowledge graph is constructed, providing a structured semantic foundation for in-depth code analysis and intelligent query.
[0052] S4: Monitor changes to project files in the code knowledge graph through code change event handlers, and update the code knowledge graph in real time when files are changed.
[0053] Among them, the code change event handler refers to a system component that continuously monitors the source code repository (such as Git). When it detects version control events such as commits and merges, it automatically triggers the incremental update process of the code knowledge graph to ensure that the graph and the code repository are kept in real time synchronized.
[0054] Project files refer to the basic organizational units that make up a software project. They include not only source code files (such as .py and .java) that store program logic, but also various files that support project development and operation, such as configuration files, documents, and build scripts.
[0055] Reference manual attached Figure 2 The diagram illustrates a graph database provided by an embodiment of the present invention.
[0056] Project 1 is the root node, with package 1 and file 2 as its subordinates. File 1 belongs to package 1 and contains functions within it. A The function in file 2 was called directly. BMeanwhile, File 2 associates the success log with the running instance of version 1, and aggregates relevant user-submitted reports, historical dialogue records (including debug records), and general inquiry records. This establishes a multi-dimensional association between static code structure (packages, files, function call relationships) and dynamic runtime data (versions, logs) and development collaboration information (reports, dialogues), thus constructing a unified knowledge network that supports intelligent question answering and fault tracing.
[0057] In this embodiment of the invention, an event-driven mechanism is used to ensure real-time synchronization between the code knowledge graph and the project source code, effectively solving the problem that traditional static analysis lags behind code iteration, and providing continuous and accurate code knowledge support for development and operation.
[0058] In one possible implementation, S4 specifically includes: S401: In response to changes in the project file, delete the historical node and associated edge corresponding to the changed project file from the graph database.
[0059] Graph databases refer to database systems specifically designed for storing and querying graph-structured data. They use nodes, edges, and attributes to represent and store data, making them particularly suitable for handling data with complex relationships and providing underlying storage and efficient relationship query support for code knowledge graphs.
[0060] In this context, a historical node refers to a node in the code knowledge graph that represents a specific state (such as a commit, a version, or a release) during the evolution of a software project. It records the metadata of that state (such as timestamp, committer, and version number) and associates the code entity with dynamic information such as runtime logs and event records in the time dimension.
[0061] Among them, the associated edge refers to the directed or undirected edge connecting two nodes in the graph database. It is used to clearly represent the specific semantic relationship between nodes (such as calling, inheritance, inclusion, dependency, generation, etc.) and is the foundation for building and traversing complex relationship networks in code knowledge graphs.
[0062] S402: Clear the abstract syntax tree cache and function registry associated with the changed project file in memory.
[0063] Abstract syntax tree caching refers to a memory or disk caching mechanism used to store the abstract syntax tree corresponding to the parsed source code file. Its core purpose is to avoid repeated parsing of unmodified files, thereby significantly improving performance during incremental updates or multiple analyses of the code knowledge graph.
[0064] The function registry refers to a centralized index or dictionary created and maintained during the construction of the code knowledge graph. It stores the mapping relationship between the qualified names of all functions extracted from the project and their corresponding AST nodes or graph nodes. It is mainly used for fast target parsing and association when analyzing relationships such as function calls.
[0065] S403: Re-parse the changed project file to generate the first abstract syntax tree.
[0066] S404: Based on the first abstract syntax tree, re-execute function call relationship analysis, inheritance relationship analysis, and module dependency relationship analysis.
[0067] S405: Update the analyzed nodes and relation edges to the graph database, and update the code knowledge graph in real time.
[0068] In a knowledge graph, a node is the most basic data unit, used to represent an independent entity or object. In a code knowledge graph, nodes can represent specific elements such as functions, classes, modules, versions, and developers. Each node contains a unique identifier and a set of attributes describing the characteristics of that entity.
[0069] In this context, a relation edge refers to a directed or undirected edge connecting two nodes, used to precisely describe the association type and semantics between nodes. In code knowledge graphs, relation edges define specific logical relationships through labels (such as CALLS, INHERITS, CONTAINS), forming a structured network between nodes and supporting complex graph traversal and query operations.
[0070] Specifically, the system incorporates a built-in CodeChangeEventHandler to continuously monitor changes to project files (including additions, modifications, and deletions). When a file changes, the system automatically executes the following process: Deleting the old data nodes and edges corresponding to that file from the Neo4j graph database; clearing the AST cache and function registry associated with that file in memory; re-parseing the modified file and generating a new syntax tree; recalculating function calls and dependencies based on the updated AST; and updating the knowledge graph nodes and edges in real time to ensure global consistency and integrity. Through this mechanism, this invention achieves dynamic maintenance and continuous evolution of the code knowledge graph, ensuring the timeliness and accuracy of knowledge.
[0071] In this embodiment of the invention, an event-driven incremental update mechanism is used to ensure the real-time performance of the knowledge graph while effectively avoiding the performance overhead of full reconstruction, thus significantly improving the analysis and response efficiency of large-scale codebases.
[0072] S5: Establish a relationship between the runtime log data and the corresponding function nodes in the code knowledge graph.
[0073] In this context, a function node refers to a node in the code knowledge graph that represents a specific function definition. This node not only contains the function's basic identity information (such as its qualified name), but also stores various attributes related to the function (such as parameter list, return type, and file path). It is the core entity in the graph that carries function-level code knowledge.
[0074] In a code knowledge graph, a relationship refers to a connection with specific semantics that exists between any two or more entity nodes. It is a broad term that encompasses all types of relationship edges (such as calls, inheritance, inclusion, and dependencies), describing the structural, logical, or behavioral connections between entities that together constitute the semantic network of the graph.
[0075] Specifically, the system automatically captures log information during the compilation, execution, and testing processes of the project through the LogMonitor module, including error stacks, warning messages, execution time, and runtime environment parameters. After preprocessing, the captured log data is associated with corresponding function nodes in the code knowledge graph (e.g., "TRIGGERED_BY", "FAILED_AT"), achieving a semantic binding between runtime behavior and code structure. Through this module, developers can directly locate the specific function that caused the exception, the call chain, and the corresponding commit record during queries, thereby achieving automated problem tracing and remediation suggestion generation based on GraphRAG.
[0076] In this embodiment of the invention, by establishing an explicit association between the runtime log and the code structure, the information barrier between the dynamic runtime state and the static code state is successfully broken down, providing key support for log-based fault diagnosis and code localization.
[0077] S6: Based on relationships, the intelligent agent parses the developer's natural language questions into Cypher query statements.
[0078] Among them, the intelligent agent refers to a software agent with autonomous reasoning and learning capabilities. In this system, it receives natural language input from the developer and automatically completes the entire process from problem analysis to code knowledge graph query through a series of actions such as understanding, planning, and knowledge retrieval.
[0079] Among them, natural language problems refer to query requests made by developers in everyday language (such as Chinese and English) rather than programming syntax, such as "find all modules that have called function A". The system needs to understand its semantics and convert it into a structured query.
[0080] Cypher queries are a declarative query language designed specifically for graph databases (such as Neo4j), using an intuitive symbolic pattern to describe the nodes, relationships, and paths to be searched.
[0081] In this embodiment of the invention, the natural language intent is accurately converted into a structured query by an intelligent agent, which significantly reduces the technical threshold for developers to query the code knowledge graph and realizes the naturalization and intelligence of human-computer interaction.
[0082] In one possible implementation, S6 specifically includes: S601: Receive natural language questions from developers.
[0083] S602: Recall the most relevant recall context for natural language problems from the code knowledge graph using the RAG structure.
[0084] The RAG structure refers to a system architecture for retrieval-enhanced generation, with its core process being "retrieval-generation." First, the user query is encoded into a vector, and relevant documents or information fragments are retrieved from the knowledge base (recalling the context). Then, this context, along with the original query, is submitted to a large language model to guide it in generating a more factually grounded final answer. This structure effectively combines the knowledge accuracy of intensive retrieval with the powerful reasoning capabilities of a large model.
[0085] In this context, the retrieved context refers to the set of information fragments relevant to the current question retrieved from external knowledge sources (such as code knowledge graphs and document repositories) through the Retrieval Enhanced Generation (RAG) process. These fragments serve as supplementary evidence to the large language model, aiming to improve the accuracy and factuality of its generated answers and avoid "illusions" arising solely from model parameters.
[0086] In one possible implementation, S602 specifically includes: S6021: Encoding natural language questions as vectors: .
[0087] in, This relates to natural language issues. Vector representation of natural language problems.
[0088] S6022: Based on vectors, retrieve similar nodes from the vector database to obtain a set of similar nodes: .
[0089] in, V This represents the set of candidate vectors for entities in the code knowledge graph. This indicates taking the maximum value parameter. Represents the similarity function. Indicates the first i candidate vectors This represents a set of similar nodes.
[0090] Among them, a vector database is a database system specifically designed for storing, indexing, and retrieving high-dimensional vector data. It achieves efficient approximate nearest neighbor search by calculating the similarity between vectors (such as cosine similarity). In this system, it is used to store the vectorized representations of entity nodes in the code knowledge graph, thereby enabling fast semantic-based similarity retrieval.
[0091] In this context, similar nodes refer to nodes in the vector space that are close to the query vector. In the RAG process, the natural language question is encoded into a query vector, and the vector database is searched for the node with the highest similarity. K These retrieved nodes are called similar nodes, and they constitute the recall context needed to generate the answer.
[0092] S6023: Based on the set of similar nodes, extract the description information and associations of the corresponding similar nodes from the code knowledge graph to form a recall context.
[0093] In this context, descriptive information refers to attribute data used to characterize and explain the properties of nodes or relation edges in a knowledge graph. For nodes, descriptive information may include metadata such as name, type, code location, and creation time. For relation edges, descriptive information can define their semantic features such as type, strength, and direction, and is the core manifestation of the semantic richness of the graph.
[0094] In this embodiment of the invention, by combining semantic vector retrieval with graph relationship extraction, an enhanced context rich in semantic and structural information is constructed, providing key support for subsequent accurate understanding of complex code query intent.
[0095] S603: Based on the recall context, perform semantic parsing and intent recognition on natural language questions to determine the query target.
[0096] Semantic parsing refers to the process of transforming a user's natural language query into a standardized logical form or structured query that can be understood and executed by a machine. In this system, it is responsible for deeply understanding the entities, relationships, and constraints in the problem and mapping them to the core elements of knowledge graph queries, laying the foundation for generating accurate Cypher statements.
[0097] Intent recognition is a key task in natural language processing, aiming to determine the true purpose behind user input or the type of operation they want to perform. In this scenario, it needs to accurately identify whether the developer's question is for "querying call relationships," "tracing code change history," or "retrieving runtime logs," in order to determine the overall structure and target of the query.
[0098] S604: Identify entities involved in natural language problems using a named entity recognition model: .
[0099] in, Indicates the first identified i Entity name, Indicates the first i Entity type, This represents a named entity recognition model. This represents the set of entities involved in a natural language problem.
[0100] Named entity recognition (NAME) is a natural language processing model used to automatically identify and classify specific entity information from natural language text. In this invention, the model is responsible for accurately identifying key entities such as function names, class names, module names, and developer names from questions raised by developers, and labeling them with predefined types, laying the foundation for accurately mapping these entities to corresponding nodes in the knowledge graph.
[0101] S605: Map entities to corresponding entity nodes in the code knowledge graph: .
[0102] in, The type is the first i A collection of entities of various entity types This indicates taking the maximum value parameter. Represents the similarity function. Indicates a text encoder. Indicates the first j candidate node vectors, This represents a mapping function.
[0103] S606: Generate Cypher query statements based on query targets and entity nodes.
[0104] Specifically, developers input natural language questions through the Agent interface, such as "In which module is this function called?" or "Why did the build fail recently?". The RAG (Retrieval-Augmented Generation) structure is used to retrieve the most relevant node descriptions, relationships, and code snippets from the code knowledge graph or knowledge base.
[0105] Furthermore, within the RAG (Retrieval Augmentation) framework, the system first performs vectorized encoding and semantic retrieval on the user's natural language question to recall the most relevant code knowledge context. Specifically, the system utilizes a pre-trained text encoder (such as BERT or CodeBERT) to encode the question... Q Mapped to a high-dimensional vector q Subsequently, by calculating the vector... q With all entity vectors in the knowledge graph v i The cosine similarity is calculated, and the vector database with the highest similarity is retrieved. K Each node. Ultimately, the node information retrieved (including function descriptions, class definitions, associated logs, etc.) is combined into a context set. This provides crucial structured knowledge support for generating accurate answers in subsequent large-scale models.
[0106] For example, cosine similarity is commonly used as a similarity function: .
[0107] in, Represents cosine similarity. Represents the norm.
[0108] Specifically, the large model understands the user's query intent (query structure, operation type, constraints, etc.). It identifies the entities involved in the question and maps them to entity types in Neo4j. After obtaining the recall context, the large model first performs semantic parsing and intent recognition on the natural language question, outputting the core elements of the query in a structured form, including the target entity, operation type, and constraints. Subsequently, the system uses a named entity recognition model to identify the specific entity names and their types (such as functions, classes, etc.) mentioned in the question and maps these entities to corresponding nodes in the knowledge graph. Finally, the system integrates the parsed query intent and the mapped entity nodes to automatically generate precise Cypher graph query statements, thereby achieving intelligent querying and knowledge acquisition from the code knowledge graph.
[0109] For example, the Cypher output by the model can generally follow the following form: MATCH(f1:Function{name:'Function A'})-[:CALLS]->(f2:Function). RETURN f2.name, or a more general form: .
[0110] The specific assembly logic can be defined by rules or learned automatically by the model: Components include MATCH, which defines the nodes and relationships in the graph; WHERE, used for filtering conditions (time, author, number of submissions, etc.); and RETURN, used to specify the output fields. For example: .
[0111] For example, in the full process example, enter the query "query functions that have been called". foo All functions", the output process is as follows: In the vector retrieval phase, the system recalls and functions foo The relevant node context. In the intent recognition phase, the system identifies the user's intent as "querying function call relationships." In the entity recognition phase, the system identifies the entity... e 1 = f oo and its types t 1 = Function Finally, during the Cypher generation stage, the system automatically generates the graph query statement "MATCH(f:Function)-[:CALLS]->(g:Function{name:"foo"})RETURN f".
[0112] In this embodiment of the invention, by integrating semantic retrieval, intent recognition and entity linking technologies, ambiguous natural language problems are accurately transformed into executable graph query commands, realizing end-to-end intelligent conversion from user intent to code knowledge retrieval.
[0113] S7: Retrieve query results from the graph database based on the Cypher query statement.
[0114] In this embodiment of the invention, by efficiently executing structured graph queries, abstract code knowledge is transformed into specific node relationship data, providing an accurate and reliable source of information for answering questions.
[0115] In one possible implementation, S7 specifically includes: S701: Execute the Cypher query statement to obtain a preliminary query result set from the graph database.
[0116] S702: Check the completeness of the preliminary query result set.
[0117] S703: When the initial query result set is incomplete, obtain the complete information by expanding the query or calling external tools.
[0118] S704: Combine the completed information with the preliminary query result set to form an enhanced context.
[0119] In this context, enhanced context refers to the set of information fragments that are highly relevant to the current problem, obtained by retrieving external knowledge sources (such as code knowledge graphs and document libraries) during the RAG (Retrieval Enhancement Generation) process.
[0120] S705: Based on the enhanced context, the query results are generated by merging results through an LLM call. .
[0121] in, This represents the final output query result. Represents the fusion function. This represents the initial set of query results. This indicates the output of an external tool. This indicates the processing of large language models.
[0122] LLM stands for Large Language Model, a deep learning model trained on massive amounts of text data and possessing hundreds of billions or even trillions of parameters. It can understand and generate natural language, performing complex tasks such as question answering, translation, and code generation. In a locally deployed, private environment, LLM provides development teams with intelligent assistance capabilities comparable to those using public models, while ensuring that internal code, technical solutions, and business data are not leaked.
[0123] Specifically, the system executes the generated Cypher statements to retrieve query results from the graph database. If the results are insufficient, the Agent can call external tools (such as source code file readers, runtime log analyzers, etc.) to complete the information. The system first executes the Cypher query generated by the large model, for example: MATCH (f:Function)-[:CALLS]->(g:Function {name: "foo"}) RETURN f.name Then it is executed via Neo4j driver: results = neo4j_driver.run(cypher_query).data() The result set obtained at this time is denoted as: .
[0124] For example, there are generally three types of "insufficient results": insufficient number of results, incomplete information dimensions, and semantic inconsistency or ambiguous context.
[0125] Insufficient results specifically means that the query results are empty or the number is lower than the expected threshold. , For example, when a user asks "function",A The result for "Which functions called it?" is empty, indicating that the graph is missing call edge information.
[0126] Incomplete information dimensions specifically refer to the lack of key fields required for understanding the question's semantics in the returned results. For example, if a user asks, "Which functions are called..." foo The query results only contain call relationships and lack the "Exception Log" field, indicating a missing dimension. This can be checked by examining the function's field. .
[0127] like If so, it needs to be completed.
[0128] Semantic inconsistency or ambiguous context refers to a situation where the semantics of the query results do not match the question. For example, a user asks for "the most recent submission," but the query results do not include it. timestamp Or sorting fields. The user asked, "Includes functions?" foo and bar The result shows "within the same module", but lacks module-level aggregation information. In such cases, semantic validation of the results can be performed using a large model. .
[0129] If the score is less than the threshold τ If the result is deemed "insufficient," the Agent proceeds to the "information completion" stage.
[0130] Furthermore, path completion can be divided into two categories: intra-graph completion and external completion. The first category, intra-graph completion, specifically involves automatically expanding the neighborhood query. If related nodes exist in the graph but are not directly associated, they can be expanded through adjacency: .
[0131] in, Represents a node of k Hierarchical neighbors. Allows indirect calls across levels, completing potential association functions. Then, through attribute reasoning, if a certain attribute is missing, it can be inferred from the attributes of similar nodes: .
[0132] For example, if a function's module label is missing, it can be estimated using the module labels of other functions in the same file.
[0133] The second type of external completion specifically refers to the Agent calling external toolchains when the information inside the graph is insufficient.
[0134] In this embodiment of the invention, by integrating multi-source information and optimizing intelligent results, the completeness, accuracy and readability of query results are significantly improved, ensuring that the information returned to developers is comprehensive and directly usable.
[0135] S8: Format the query results and generate the final result.
[0136] Specifically, the query results are formatted and displayed in Markdown format, including function definitions, call chain diagrams, and runtime log summaries.
[0137] Formatting refers to the process of transforming raw data or irregular query results into a predefined, easily readable format that can be further processed by machines. Its purpose is to improve the clarity, readability, and usability of information; for example, converting graph query results into well-structured JSON, clear tables, or concise natural language summaries.
[0138] In this embodiment of the invention, by converting the original map data into a clear and easy-to-read format, the presentation of the query results is significantly improved, enabling developers to quickly understand and apply the obtained information.
[0139] In one possible implementation, the process after S8 includes: S9: Store the natural language questions, Cypher query statements, and final results of this interaction in a structured manner and incorporate them into the Graph RAG knowledge index to form reusable semantic knowledge units.
[0140] Structured storage refers to the method of organizing and managing data according to a specific data model (such as tables in relational databases, nodes and edges in graph databases). It emphasizes the existence of clear relationships and constraints between data, which is different from unstructured plain text, thereby supporting efficient and accurate queries and calculations. For example, code knowledge can be stored in Neo4j as a graph structure.
[0141] The Graph RAG knowledge index is a specialized index structure built for implementing Graph RAGs. It typically consists of two parts: first, a vector index that converts entities and relations in the code knowledge graph into high-dimensional vectors for semantic similarity retrieval; and second, a graph structure index that preserves the original topological connections for relation reasoning. The combination of these two elements supports information retrieval from both semantic and structural dimensions.
[0142] Semantic knowledge units refer to the smallest logical blocks extracted from code or documents that carry independent and complete semantic information. It is not merely a keyword or fragment, but a whole with clear meaning in a specific context, such as a function definition, a class description, a complete API description, or a solution record. It is the basic unit for building high-quality knowledge indexes and recall contexts.
[0143] Specifically, the system stores the content of this dialogue (including questions, query statements, and execution results) in a structured JSON document and automatically incorporates it into the Graph RAG knowledge index to form reusable semantic knowledge units, providing fast retrieval and answer reuse for similar questions in the future.
[0144] In this embodiment of the invention, by continuously accumulating human-computer interaction experience, a self-evolving knowledge ecosystem is constructed, which significantly improves the system's adaptability and accuracy in dealing with complex queries, laying the foundation for long-term knowledge accumulation and intelligent optimization.
[0145] In practical applications, an intelligent code collaboration system that deeply integrates static code structure and dynamic runtime data is constructed. First, a Tree-sitter is used to parse the source code and generate an abstract syntax tree, which then builds a code knowledge graph containing function calls, inheritance relationships, and module dependencies. The graph is dynamically updated by monitoring code changes in real time, and runtime logs are associated with function nodes. Finally, an intelligent agent parses natural language queries into Cypher statements, combining GraphRAG enhanced retrieval and LLM intelligent processing to obtain accurate results from the code knowledge graph. This achieves a panoramic fusion of code knowledge, unifying static code structure, dynamic runtime logs, and development collaboration data, breaking down information silos. It provides an intelligent and natural interactive experience, lowering the query threshold through natural language understanding technology, enabling developers to quickly access in-depth code information. A self-evolving knowledge system is built, continuously accumulating interaction data to optimize the knowledge index, forming a virtuous cycle of becoming smarter with use. This design significantly improves code understanding efficiency, fault location speed, and team collaboration effectiveness.
[0146] The beneficial effects of the technical solutions provided in the embodiments of the present invention include at least the following: In this embodiment of the invention, by constructing and updating a code knowledge graph in real time, the runtime logs are dynamically associated with function nodes in the graph, effectively solving the problems of isolation between code knowledge and runtime logs, as well as the static lag of traditional code analysis. This innovation achieves deep integration of code structure and runtime behavior, constructing a unified and interconnected dynamic knowledge system. This seamlessly connects the logging stage of "discovering problems" with the analysis stage of "locating code," enabling developers to trace back to specific function logic, dependent modules, and even change history from log events based on the latest code context. This greatly improves the efficiency and accuracy of troubleshooting, impact analysis, and system understanding.
[0147] Reference manual attached Figure 3 The diagram shows a schematic representation of a private intelligent code collaboration optimization system based on graph retrieval, provided by an embodiment of the present invention.
[0148] This invention provides a private intelligent code collaboration optimization system 20 based on graph retrieval, comprising: a processor 201 and a memory 202; The memory 202 stores programs or instructions that can run on the processor 201. When the program or instructions are executed by the processor 201, they implement the steps of the above-described private intelligent code collaboration optimization method based on graph retrieval and achieve the same technical effect. To avoid repetition, the present invention will not elaborate further.
[0149] It should be understood that the processor 201 in this embodiment of the invention may be a central processing unit (CPU), or it may be other general-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. The general-purpose processor may be a microprocessor or any conventional processor.
[0150] It should also be understood that the memory 202 in the embodiments of the present invention can be volatile memory or non-volatile memory, or may include both volatile and non-volatile memory. The non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. The volatile memory can be random access memory (RAM), which is used as an external cache. By way of example, but not limitation, many forms of random access memory are available, such as static random access memory (SRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDR SDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM), and direct memory bus RAM (DR RAM).
[0151] The above embodiments can be implemented, in whole or in part, by software, hardware (such as circuits), firmware, or any other combination thereof. When implemented using software, the above embodiments can be implemented, in whole or in part, as a computer program product. The computer program product includes one or more computer instructions or computer programs. When the computer instructions or computer programs are loaded or executed on a computer, all or part of the processes or functions described in the embodiments of the present invention are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another website, computer, server, or data center via wired (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more sets of available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium. A semiconductor medium can be a solid-state drive.
[0152] It should be understood that, in various embodiments of the present invention, the order of the above-mentioned process numbers does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of the present invention.
[0153] Those skilled in the art will recognize that the units and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementations should not be considered beyond the scope of this invention.
[0154] Those skilled in the art will clearly understand that, for the sake of convenience and brevity, the specific working processes of the devices, apparatuses, and units described above can be referred to the corresponding processes in the foregoing method embodiments, and will not be repeated here.
[0155] In the several embodiments provided by this invention, it should be understood that the disclosed devices, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of units is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple units or components may be combined or integrated into another device, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between devices or units may be electrical, mechanical, or other forms.
[0156] The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the units can be selected to achieve the purpose of this embodiment according to actual needs.
[0157] In addition, the functional units in the various embodiments of the present invention can be integrated into one processing unit, or each unit can exist physically separately, or two or more units can be integrated into one unit.
[0158] If the aforementioned functions are implemented as software functional units and sold or used as independent products, they can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this invention, essentially, or the part that contributes to the prior art, or a portion of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this invention. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0159] This invention provides a readable storage medium comprising: storing a program or instructions on the readable storage medium, wherein when the program or instructions are executed by a processor, the program or instructions implement the steps of the above-described private intelligent code collaboration optimization method based on graph retrieval, and can achieve the same technical effect. To avoid repetition, this invention will not elaborate further.
[0160] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the embodiments of the present invention, and are not intended to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features; and these modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention. Any changes or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in the present invention should be included within the protection scope of the present invention.
Claims
1. A private intelligent code collaboration optimization method based on graph retrieval, characterized in that, include: S1: Obtain the source code files and runtime log data of a private project; S2: The source code file is analyzed using a Tree-sitter parser to generate a corresponding abstract syntax tree; S3: Based on the abstract syntax tree, construct a code knowledge graph by analyzing function call relationships, inheritance relationships, and module dependency relationships; S4: Monitor changes to project files in the code knowledge graph through a code change event handler, and update the code knowledge graph in real time when files are changed; S5: Establish a relationship between the runtime log data and the corresponding function nodes in the code knowledge graph; S6: Based on the aforementioned relationship, the developer's natural language question is parsed into a Cypher query statement using the intelligent agent; S7: Retrieve query results from the graph database based on the Cypher query statement; S8: Format the query results to generate the final result.
2. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, Following S8, the following is also included: S9: The natural language question, the Cypher query statement, and the final result of this interaction are stored in a structured manner and incorporated into the Graph RAG knowledge index to form a reusable semantic knowledge unit.
3. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, S2 specifically includes: S201: Initialize the Tree-sitter resolver; S202: Generate a query pattern based on the initialized Tree-sitter parser; S203: In the query mode, each source code file is parsed to generate the corresponding abstract syntax tree.
4. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, S3 specifically includes: S301: Based on the abstract syntax tree, extract function definitions, class definitions, and qualified names to generate code entity nodes; S302: Based on the code entity nodes, establish inter-function call relationships through function call relationship analysis; S303: Based on the class definitions in the code entity nodes, establish class inheritance relationships through inheritance relationship analysis; S304: Based on the code entity nodes, establish inter-module dependencies through module dependency analysis; S305: Integrate all the code entity nodes, the function call relationships, the class inheritance relationships, and the module dependency relationships to construct the code knowledge graph.
5. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, S4 specifically includes: S401: In response to a change in the project file, delete the historical node and associated edge corresponding to the changed project file from the graph database; S402: Clear the abstract syntax tree cache and function registry associated with the changed project file in memory; S403: Re-parse the changed project file to generate a first abstract syntax tree; S404: Based on the first abstract syntax tree, re-execute the function call relationship analysis, inheritance relationship analysis, and module dependency relationship analysis; S405: Update the obtained nodes and relationship edges to the graph database and update the code knowledge graph in real time.
6. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, S6 specifically includes: S601: Receive the natural language question provided by the developer; S602: Using the RAG structure, recall the recall context most relevant to the natural language problem from the code knowledge graph; S603: Based on the recall context, perform semantic parsing and intent recognition on the natural language question to determine the query target; S604: Identify the entities involved in the natural language problem using a named entity recognition model: ; in, Indicates the first identified i Entity name, Indicates the first i Entity type, This represents a named entity recognition model. Represents the set of entities involved in natural language problems; S605: Map the entity to the corresponding entity node in the code knowledge graph: ; in, The type is the first i A collection of entities of various entity types This indicates taking the maximum value parameter. Represents the similarity function. Indicates a text encoder. Indicates the first j candidate node vectors, Represents a mapping function; S606: Generate the Cypher query statement based on the query target and the entity node.
7. The private intelligent code collaboration optimization method based on graph retrieval according to claim 6, characterized in that, Specifically, S602 includes: S6021: Encode the natural language question into a vector: ; in, This relates to natural language issues. Vector representation of natural language problems; S6022: Based on the vector, retrieve similar nodes from the vector database to obtain a set of similar nodes: ; in, V This represents the set of candidate vectors for entities in the code knowledge graph. This indicates taking the maximum value parameter. Represents the similarity function. Indicates the first i candidate vectors Represents a set of similar nodes; S6023: Based on the set of similar nodes, extract the description information and association relationships of the corresponding similar nodes from the code knowledge graph to form the recall context.
8. The private intelligent code collaboration optimization method based on graph retrieval according to claim 1, characterized in that, Specifically, S7 includes: S701: Execute the Cypher query statement to obtain a preliminary query result set from the graph database; S702: Check the completeness of the preliminary query result set; S703: When the preliminary query result set is incomplete, complete information can be obtained by expanding the query or calling external tools; S704: Concatenate the completed information with the preliminary query result set to form an enhanced context; S705: Based on the enhanced context, the query result is generated by merging results through an LLM call: ; in, This represents the final output query result. Represents the fusion function. This represents the initial set of query results. This indicates the output of an external tool. This indicates the processing of large language models.
9. A private intelligent code collaboration optimization system based on graph retrieval, characterized in that, include: Processor and memory; The memory stores programs or instructions that can run on the processor, and when the program or instructions are executed by the processor, they implement the steps of the private intelligent code collaboration optimization method based on graph retrieval as described in any one of claims 1 to 8.
10. A readable storage medium, characterized in that, The program or instructions are stored on the readable storage medium, and when the program or instructions are executed by a processor, they implement the steps of the private intelligent code collaboration optimization method based on graph retrieval as described in any one of claims 1 to 8.
Citation Information
Patent Citations
Software knowledge graph incremental updating method based on code submission
CN115543402A
Warehouse level code problem solving system based on large language model
CN119576384A
Intelligent Bug management method and platform based on AI large model
CN120909929A
Check code reuse method based on knowledge graph, apparatus, and storage medium
WO2025179557A1
Cited By
Component calling method and device, equipment, medium and product
CN121614148A
Component calling method, device, equipment, medium and product
CN121614148B
Form generation method, form generation system, storage medium and program product
CN121680850A
Code data management method based on artificial intelligence and related device
CN122018956A
Software design intention inference and document generation method, device and system
CN122086460A