Code generation method and device, electronic equipment, storage medium and program product

By constructing a code knowledge graph and knowledge base, the problem of poor accuracy in code generation from large language models is solved, achieving more accurate and effective code generation.

CN121858085APending Publication Date: 2026-04-14BEIJING YOUZHUJU NETWORK TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
BEIJING YOUZHUJU NETWORK TECH CO LTD
Filing Date
2026-01-04
Publication Date
2026-04-14

AI Technical Summary

Technical Problem

Existing code generation methods based on large language models are difficult to meet users' needs in the actual code generation process, resulting in poor code generation accuracy.

Method used

Construct a code knowledge graph for the target domain, search for entity information that matches the code requirement description in the code knowledge graph, and search for target code snippets in the code knowledge base. Use this information to generate code that meets user requirements.

Benefits of technology

It improves the accuracy and usability of code generation, avoids the loss of code entity relationships, and enhances the precision and reliability of code generation results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121858085A_ABST
    Figure CN121858085A_ABST
Patent Text Reader

Abstract

The invention discloses a code generation method and device, electronic equipment, a storage medium and a program product, and relates to the field of computer technology, artificial intelligence technology, large model technology and large language model technology, the method comprises the following steps: obtaining code demand description of a target field; searching entity information matched with the code demand description in a code knowledge graph of the target domain; wherein the code knowledge graph is used for representing code entities in domain codes of the target domain, attribute information of the code entities and an incidence relation between the code entities; the entity information comprises at least one of first code entities matched with the code demand description, attribute information of the first code entities and an incidence relation between the first code entities; searching a code knowledge base for a target code snippet matched with the entity information; and based on the target code snippet and a code generation model, generating a target code corresponding to the code demand description. The problem of poor code generation accuracy can be solved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This application relates to the field of computer technology, specifically to code generation methods, apparatus, electronic devices, storage media, and program products. Background Technology

[0002] Artificial intelligence (AI) code generation based on natural language processing primarily utilizes the generative capabilities of large language models to convert natural language text describing user needs into specific code content that fulfills those needs. However, relying solely on the code knowledge learned during training by large language models is insufficient to meet the actual needs of users in the code generation process, resulting in poor code generation accuracy. Summary of the Invention

[0003] This application provides a code generation method, apparatus, electronic device, storage medium, and program product to solve the problem of poor code generation accuracy.

[0004] Firstly, this application provides a code generation method, including: Obtain a code requirement description for the target domain; The code knowledge graph of the target domain is used to search for entity information that matches the code requirement description; wherein the code knowledge graph is used to represent code entities in the domain code of the target domain, attribute information of the code entities, and the association relationships between the code entities; the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and the association relationships between the first code entities; Search the code knowledge base for target code snippets that match the entity information; Based on the target code snippet and the code generation model, the target code corresponding to the code requirement description is generated.

[0005] Secondly, this application provides a code generation apparatus, comprising: The information acquisition module is used to acquire code requirement descriptions for the target domain; The graph search module is used to search for entity information that matches the code requirement description in the code knowledge graph of the target domain; wherein, the code knowledge graph is used to represent code entities in the domain code of the target domain, attribute information of the code entities, and the association relationships between the code entities; the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and the association relationships between the first code entities; The code search module is used to search for target code fragments in the code knowledge base that match the entity information; The code generation module is used to generate target code corresponding to the code requirement description based on the target code fragment and the code generation model.

[0006] Thirdly, this application provides an electronic device, including: a memory and a processor, which are communicatively connected to each other. The memory stores computer instructions, and the processor executes the computer instructions to perform the code generation method described in the first aspect or any corresponding embodiment.

[0007] Fourthly, this application provides a computer-readable storage medium storing computer instructions for causing a computer to execute the code generation method described in the first aspect or any corresponding embodiment thereof.

[0008] Fifthly, this application provides a computer program product, including computer instructions for causing a computer to execute the code generation method described in the first aspect or any corresponding embodiment thereof.

[0009] The code generation method provided in this application pre-constructs a code knowledge graph to represent code entities, attribute information of code entities, and relationships between code entities in the domain code of the target domain. Therefore, upon obtaining a code requirement description for the target domain, the code knowledge graph can be used to better understand the code knowledge of the domain code and find entity information matching the code requirement description, such as first code entities, first code entities, and relationships between first code entities. Furthermore, using the entity information found in the code knowledge graph, target code fragments matching the code requirement description are accurately and effectively found in the code knowledge base. These target code fragments provide additional and accurate prompts for the code generation model, thereby accurately generating target code that meets user needs. Simultaneously, since the code knowledge graph contains relationships between code entities, it avoids missing relationships between code entities in the found target code fragments, further improving the accuracy and usability of the code generation results.

[0010] The beneficial effects of code generation devices, electronic devices, storage media, and program products correspond to the beneficial effects of code generation methods, and will not be elaborated here. Attached Figure Description

[0011] To more clearly illustrate the technical solutions in the specific embodiments of this application or the prior art, the drawings used in the description of the specific embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained from these drawings without creative effort.

[0012] Figure 1 This is a schematic diagram illustrating an application scenario according to an embodiment of this application; Figure 2 This is a schematic diagram of the first type of code generation method according to an embodiment of this application; Figure 3 This is a first schematic diagram of a code knowledge graph according to an embodiment of this application; Figure 4 This is a schematic diagram of a second flowchart of the code generation method according to an embodiment of this application; Figure 5 This is a schematic diagram of the third process of the code generation method according to the embodiments of this application; Figure 6 This is a schematic diagram illustrating the process of establishing a code knowledge graph according to an embodiment of this application; Figure 7 This is a schematic diagram of a domain model according to an embodiment of this application; Figure 8 This is a second schematic diagram of a code knowledge graph according to an embodiment of this application; Figure 9 This is a schematic diagram of the fourth process of the code generation method according to the embodiments of this application; Figure 10 This is a structural block diagram of a code generation apparatus according to an embodiment of this application; Figure 11 This is a schematic diagram of the hardware structure of an electronic device according to an embodiment of this application. Detailed Implementation

[0013] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0014] It is understood that before using the technical solutions disclosed in the various embodiments of this application, users should be informed of the types, scope of use, and usage scenarios of the personal information involved in this application in an appropriate manner in accordance with relevant laws and regulations, and user authorization should be obtained.

[0015] For example, upon receiving a user's active request, a prompt message is sent to the user to explicitly inform them that the requested operation will require the acquisition and use of the user's personal information. This allows the user to independently choose whether to provide personal information to the software or hardware, such as the electronic device, application, server, or storage medium performing the operations of this application's technical solution, based on the prompt message.

[0016] As an optional but non-limiting implementation, in response to a user's active request, sending a prompt message to the user can be done via a pop-up window, where the prompt message can be presented in text format. Furthermore, the pop-up window can also include a selection control allowing the user to choose "agree" or "disagree" to provide personal information to the electronic device.

[0017] It is understood that the above notification and user authorization process are merely illustrative and do not constitute a limitation on the implementation of this application. Other methods that comply with relevant laws and regulations may also be applied to the implementation of this application.

[0018] It is understood that the data involved in this technical solution (including but not limited to the data itself, the acquisition or use of the data) shall comply with the requirements of relevant laws, regulations and related provisions.

[0019] The terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance or implicitly specifying the number of technical features indicated. Therefore, a feature defined as "first" or "second" may explicitly or implicitly include one or more of that feature. In the description of this application, "multiple" means two or more, unless otherwise explicitly specified.

[0020] Artificial intelligence (AI) code generation based on natural language processing primarily utilizes the generative capabilities of large language models to convert natural language text describing user needs into specific code content that fulfills those needs. However, relying solely on the code knowledge learned during training (such as the code data used for training) is insufficient to meet the actual needs of users in the code generation process, resulting in poor code generation accuracy.

[0021] Therefore, in practical industrial software development scenarios, during the code generation process of large language models, existing code is used to provide a code generation context for writing new code. Similar to engineers writing code, large language models also need to go through the following steps when generating code: first, reading the existing code and analyzing it in conjunction with user requirements; then, using the design and implementation logic related to user requirements in the existing code, generating new code that meets user needs. For example, in the AI ​​code generation scenario for form code in the field of internet computing recommendation content, it is often necessary to rely on a knowledge base of existing form code in the recommendation content domain as a context for domain-specific AI code generation, supporting the large language model in generating new form code. It is evident that accurately recalling reusable existing code to provide the necessary code generation context for large language models is not only crucial for protecting the code generation effect but also one of the key challenges in large language model code generation.

[0022] The following are some commonly used methods for enhancing the generation of retrieval codes that can be reused: The first method is semantic embedding retrieval: the code requirement description in natural language and the existing code are jointly mapped to a vector space. The vector similarity between natural language and existing code is used for matching, and the retrieval results are added to the code generation context of the large language model.

[0023] The second method is knowledge base vector retrieval-enhanced generation: based on the user's input description of code requirements, the most relevant code snippets are retrieved from the code knowledge base or code library, and then the retrieved code snippets are concatenated into the code generation context of the large language model.

[0024] The third method is structured retrieval-enhanced generation: parsing existing code into an abstract syntax tree (AST), and recalling relevant code based on function signatures, parameter patterns, call chains, and other structures to supplement the code generation context of the large language model.

[0025] The fourth approach is context- or memory-based enhanced generation: This approach does not rely on external knowledge bases, but instead utilizes the current project context or user history as the code generation context for a large language model.

[0026] However, due to the complexity of domain concepts and the intricate relationships and dependencies within existing code, the bidding form code in internet computing-based content recommendation often has diverse interactions with dozens of other form codes related to bidding strategies, recommendation objectives, and marketing scenarios. This results in low accuracy and effectiveness of code snippet retrieval, making it difficult to guarantee high accuracy in code snippet retrieval.

[0027] Taking the aforementioned knowledge base vector retrieval enhancement generation as an example, knowledge base vector retrieval enhancement generation often involves structural decomposition and vector persistence based on the original structure of classes, methods, etc. in the code. Then, it combines the semantic similarity between the text blocks describing the user's code requirements and existing code fragments, recalling existing code fragments in a point-to-point manner using a mixture of sparse and dense vectors, providing necessary supplementary existing code context for large language models. However, this approach faces the following problems: First, it is difficult to establish a complete mapping relationship between code requirement descriptions and existing code snippets. This is due to the flexibility of natural language expression, which makes code requirement descriptions relatively diffuse. Furthermore, the tagging of code snippets (i.e., labeling the relationship between code requirement descriptions and existing code snippets) also varies, leading to many-to-many tagging situations between code requirement descriptions and existing code snippets. This makes it difficult to accurately, comprehensively, and completely establish a mapping relationship between code requirement descriptions and existing code snippets.

[0028] Secondly, the key code used to represent the structured relationships within existing code suffers from context loss. Due to the limited context window size of large language models, existing code must undergo structural decomposition and code fragment tagging to supplement the code generation context of the large language model. However, code decomposition easily loses contextual information about the relationships and hierarchical relationships between code fragments, making it difficult for the large language model to fully understand the code organization and design implementation ideas. This results in fragmented and rigid generated code, leading to poor code generation performance. In other words, the code generation performance of large language models is limited by their ability to retrieve existing code and arrange its context.

[0029] As one optional application scenario in the embodiments of this application, such as Figure 1 As shown, application 101 is installed in terminal device 100, and user 130 can interact with application 101 through terminal device 100 and / or access device of terminal device 100.

[0030] For example, application 101 can be any application that provides code generation services. For instance, application 101 could be a code generation application provided externally by a service platform that deploys the code generation methods of this application. Application 101 could also be a code writing application, etc., in which a plugin for calling the code generation methods of this application is deployed, or application 101 could call the service interface of the code generation methods of this application. Figure 1 In the application scenario shown, if application 101 is active, the terminal device 100 can display the interface 102 of application 101. The interface 102 may include various pages that application 101 can provide, such as interactive pages, settings pages, code writing pages, command-line interfaces, etc.

[0031] In some embodiments, terminal device 100 is communicatively connected to server 110 to provide services to application 101. Terminal device 100 may be a mobile terminal, fixed terminal, or portable terminal, etc., including but not limited to desktop computers, laptop computers, multimedia tablets, or any combination thereof, including accessories and peripherals of these devices, or any combination thereof. In some embodiments, terminal device 100 may also support any type of interface, and server 110 may be various types of computing systems or servers capable of providing computing power, including but not limited to mainframes, edge computing nodes, computing devices in cloud environments, etc.

[0032] It should be noted that, Figure 1 This is merely an example of an application scenario and does not limit the scope of protection of this application.

[0033] The embodiments of this application will be described below with reference to the accompanying drawings. It should be understood that the pages shown in the drawings are merely examples, and various page designs are possible in practice. The various graphic elements on the page may have different arrangements and different visual representations, one or more elements may be omitted or replaced, and one or more other elements may also be present; no limitations are made in the embodiments of this application. Furthermore, the embodiments are primarily described below with reference to terminal device 100. It should be understood that the actions described relative to terminal device 100 can be performed by application 101 on terminal device 100, or can be performed by application 101 in conjunction with its server (e.g., server 110).

[0034] According to an embodiment of this application, a code generation method embodiment is provided. It should be noted that the steps shown in the flowchart in the accompanying drawings can be executed in a computer system such as a set of computer-executable instructions. Furthermore, although a logical order is shown in the flowchart, in some cases, the steps shown or described may be executed in a different order than that shown here.

[0035] This embodiment provides a code generation method that can be used in the aforementioned server 110, code generation service platform, etc. Figure 2 This is a flowchart of a code generation method according to an embodiment of this application, such as... Figure 2 As shown, the process includes the following steps: Step S201: Obtain the code requirements description for the target domain.

[0036] The code requirements description characterizes the user's code requirements for the generated code. The target domain is the domain to which the generated code belongs.

[0037] In practical applications, users can input code requirements via natural language descriptions or operation commands on the interface displayed on the terminal device. The terminal device then calls the service interface to send the code requirements to its local machine (such as a server or service platform).

[0038] Step S202: Search for entity information that matches the code requirement description in the code knowledge graph of the target domain.

[0039] Among them, the code knowledge graph is used to represent code entities in the domain code of the target domain, the attribute information of the code entities, and the relationships between the code entities.

[0040] Specifically, a code knowledge graph consists of nodes, edges between nodes, and node attributes. For example... Figure 3 As shown, nodes in the code knowledge graph are used to represent code entities, edges between nodes are used to represent the relationships between corresponding code entities, and node attributes are used to represent the attribute information of code entities.

[0041] Optionally, the code entity includes at least one of the following: fields of a domain entity, interaction events of those fields, relationships between domain entities, and associated elements of a domain entity. Relationships between domain entities include dependencies, linkages, etc. Associated elements include rendering components of a domain entity, form options, etc.

[0042] Optionally, the attribute information of a code entity includes at least one of the following: the code entity's description information, the code snippet of the code entity, and the code path of the code snippet. The code entity's description information includes the entity name and entity description, etc. The code path includes relative paths, absolute paths, etc. Furthermore, the attribute information of a code entity may also include the affected target, etc., which can be adjusted according to actual needs. For example, the attribute information of the code entity "Field" includes the field's entity name, the field's entity description, the code snippet corresponding to the field in the domain code, and the code path of the field's code snippet. The attribute information of the code entity "Dependency Condition" includes the dependency condition's entity name, the dependency condition's entity description, the dependency condition's code snippet, the code path of the dependency condition's code snippet, and the affected target of the dependency condition.

[0043] Furthermore, the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and association relationships between the first code entities.

[0044] It's worth noting that a code knowledge graph is a graph-shaped data structure. Therefore, in practical applications, algorithms such as maximum depth-first search (MDS) and maximum breadth-first search (BFS) can be used to find entity information in the code knowledge graph that matches the code requirement description. For example, if the code requirement description is "The user's bid is between 100 and 200; if the bid exceeds this value, input is prohibited," semantic analysis of the code requirement description reveals that it explicitly requires modifying the entity "bid." Therefore, the first code entity "bid" is found in the code knowledge graph. Then, using algorithms such as maximum depth-first search, other first code entities related to the code requirement description are searched sequentially, starting from "bid," to obtain the attribute information of the first code entities and the relationships between them, thus obtaining the entity information that matches the code requirement description.

[0045] Step S203: Search for the target code fragment that matches the entity information in the code knowledge base.

[0046] The code knowledge base is configured to store domain code for the target domain, which includes multiple code snippets.

[0047] Specifically, based on the relationships and attribute information (such as code paths) between the first code entities in the entity information, the code fragments corresponding to the first code entities are searched in the code knowledge base to obtain the target code fragments. The domain code of the target domain includes the target code fragments.

[0048] It is worth noting that the above steps S202 and S203 can be executed by an intelligent agent, other large language models, or retrieval tools. For example, by combining the understanding of the code requirement description, analyzing the entity description of the domain entity in the target domain, and using the code knowledge graph, understanding the definition of the code entity in the domain code (refer to the entity description of the code entity) and the relationship between the code entities (such as dependency relationship and association relationship), thereby recalling all code fragments that match the code requirement description, supplementing the necessary context information for subsequent code generation, and thus improving the code generation effect.

[0049] Step S204: Based on the target code snippet and the code generation model, generate the target code corresponding to the code requirement description.

[0050] Specifically, the target code snippet is used as context information in the code generation model during the code generation process, and the code generation model is used to generate the target code corresponding to the code requirement description.

[0051] Furthermore, the target code snippet is used as contextual information in the code generation model during the code generation process. Utilizing the code generation model, the contextual information, and the code data used during training, target code corresponding to the code requirement description is generated. It should be noted that the code data used during training can include code from one or more domains. This code can include code from the target domain, or it can include general code from other domains.

[0052] Optionally, the code generation model can be a large language model. Alternatively, the code generation model can also use the Transformer architecture model, a bidirectional language model, etc., without limitation.

[0053] The code generation method provided in this embodiment pre-constructs a code knowledge graph to represent code entities, attribute information, and relationships between code entities in the domain code of the target domain. Therefore, upon obtaining a code requirement description for the target domain, the code knowledge graph can be used to better understand the code knowledge of the domain code and find entity information matching the code requirement description, such as the first code entity, the first code entity itself, and the relationships between the first code entities. Furthermore, using the entity information found in the code knowledge graph, the target code fragment matching the code requirement description is accurately and effectively found in the code knowledge base. This target code fragment provides additional and accurate prompts for the code generation model, thereby accurately generating target code that meets user needs. Simultaneously, because the code knowledge graph contains relationships between code entities, it avoids missing relationships between code entities in the found target code fragment, further improving the accuracy and usability of the code generation results.

[0054] As one specific application example, see Figure 4 The code generation method of this application mainly includes the following steps: Inputting a code requirement description into a code generation model. The code generation model searches for entity information matching the code requirement description in a code knowledge graph, and then searches for relevant code fragments in a code knowledge base based on the entity information to obtain the target code fragment. The target code fragment is then added as context information to the code generation context of the code generation model. The code generation model combines the target code fragment with the code data used during training to generate and output target code that meets the code requirement description. Based on this, the code knowledge graph can be used to more accurately and effectively reference related code knowledge in the target domain, other than the code data used during training, as key context input for the code generation model, providing additional precise hints and enabling the code generation model to more effectively generate target code that meets user needs.

[0055] In some optional implementations, step S202 above, which involves searching for entity information in the code knowledge graph of the target domain that matches the code requirement description, includes: Step a1: Based on the code requirements description, perform a call plan for the code knowledge graph retrieval tool to obtain the call plan results of the retrieval tool.

[0056] The retrieval tools for the code knowledge graph include a first retrieval tool for code entities, a second retrieval tool for attribute information of code entities, and a third retrieval tool for the relationships between code entities.

[0057] Specifically, intent recognition is performed on the code requirement description to obtain intent recognition results. Based on the intent recognition results, the call planning of the code knowledge graph retrieval tool is performed to obtain the call planning results of the retrieval tool.

[0058] In practical applications, large language models can be used to plan the invocation of code knowledge graph retrieval tools based on code requirement descriptions, yielding the invocation planning results for the retrieval tools. Specifically, combining the fundamental capabilities of large language models, such as domain-specific code generation system prompts and long short-term memory in code generation, the code requirement descriptions input by users are analyzed. The fitting and generalization abilities of large language models are then used to identify the intent behind these code requirement descriptions. Subsequently, according to the actual inference needs of the large language model, the invocation planning of code knowledge graph retrieval tools is performed, resulting in the invocation planning results for the retrieval tools.

[0059] Step a2: Based on the call planning results, invoke the code knowledge graph retrieval tool to search for entity information in the code knowledge graph that matches the code requirement description.

[0060] Specifically, based on the call planning results, the code knowledge graph retrieval tool is invoked to search for the first code entity that matches the code requirement description, the attribute information of the first code entity, and the relationships between the first code entities. The retrieved entity information is used as the key code generation context through the Model Context Protocol (MCP) to accurately retrieve code snippets that meet the code requirement description from the code knowledge base as target code snippets.

[0061] The code generation method provided in this embodiment first plans the invocation of a code knowledge graph retrieval tool based on the code requirement description, and then searches the code knowledge graph according to the invocation planning results. Therefore, the code requirement description can be transformed into clear search indicators through the invocation planning results, clarifying the purpose of the search behavior, thereby accurately searching for code entities, attribute information, and relationships related to the code requirement description, filtering invalid information, and improving the search efficiency of the code knowledge graph.

[0062] As a specific application example, taking a large language model as the code generation model, an intelligent agent is built to execute the code generation method of this application. See [link to application]. Figure 5 The code generation method of this application mainly includes the following process: The user inputs a code requirement description in natural language text. The intelligent agent analyzes the code requirement description through the ReAct framework and plans the invocation of the code knowledge graph retrieval tool based on the analysis results, obtaining the invocation planning result. The ReAct framework mainly consists of three key concepts: thinking, action, and observation. Thinking is the foundation for the large language model to generate behavior and basis, representing the logical reasoning process of the large language model when facing a specific task, and is a prerequisite for decision-making. Action refers to the large language model's judgment of the specific behavior to be executed. In this embodiment, it refers to selecting a suitable retrieval tool and generating the required parameters to find actions matching the code requirement description in the code knowledge graph. Observation is the large language model's acquisition of external input, which can synchronize external feedback information to the large language model to assist in further analysis or decision-making.

[0063] Furthermore, based on the call planning results, the agent searches for entity information in the code knowledge graph that matches the code requirement description. Then, using the MCP protocol, the agent uses the entity information as key code generation context, recalls the target code fragment that matches the code requirement description, supplements the code generation with necessary contextual hints, generates target code that meets the code requirement description, and returns the target code to the user's terminal device for display.

[0064] In some optional embodiments, prior to step S201 or S202 above, the code generation method of this application further includes: Step b1: Obtain the domain model of the target domain. The domain model includes the semantic description of the domain entities in the target domain, the structured information of the domain entities, and the contextual constraints of the domain entities.

[0065] Optionally, the semantic description of a domain entity, also known as entity responsibility, is used to clarify the definition of the domain entity within the target domain. For example, the semantic description of the material entity for recommended content is the material used when recommending content, such as videos, images and text, copy, anchors, landing pages, etc. The semantic description of the purpose entity for recommendation is the expected purpose of recommending content, such as improving the online recommendation effect.

[0066] Optionally, the structured information includes at least one of the following: input definitions of domain entities, output definitions of domain entities, attribute information of domain entities, and relationships between domain entities. The input definitions determine the input data of the domain entities. The output definitions determine the output data of the domain entities. Relationships include dependency relationships, containment relationships, linkage relationships, etc. The above structured information is only an optional example and can be adjusted according to actual needs.

[0067] The aforementioned context constraints are used to restrict the scope of use of domain entities. Specifically, in Domain-Driven Design (DDD), the validity and meaning of domain entities are confined to a specific bounded context, which means that the attributes, industry, and business rules of domain entities are only applicable within the boundaries of that bounded context.

[0068] Step b2: Based on the domain model, perform entity analysis on the domain code of the target domain to obtain a code knowledge graph.

[0069] Specifically, see Figure 6 Based on the domain model and using a domain-driven programming paradigm, a ternary code knowledge graph of "code entities - relationships - attribute information" is abstracted from the domain code. Specifically, this includes: 1. Abstracting entity elements such as fields, dependencies, linkages, constants, interaction events, rendering components, form options, and field interaction events from the code logic layer, based on the domain model, as code entities. 2. Abstracting relationships such as inclusion, influence, and dependency between code entities from the domain code based on the domain model. 3. Abstracting attribute information such as entity name, entity description, code snippets, code paths, and code indexes from the domain code based on the domain model. Then, a code knowledge graph is constructed based on the abstracted content.

[0070] For example, in the domain of internet computing recommendation content, the domain entity for bidding on recommended content can be abstracted from the domain code. This code entity includes its entity description, code snippets, attribute information (such as the unit of input data), and the relationships between the code entity for bidding and the bidding strategy. Furthermore, by leveraging code knowledge graphs to establish the context of code knowledge graphs with implicit complex relationships in the internet computing recommendation content domain, the direct dependencies and inclusion relationships in the domain code are effectively preserved, serving as an important code context for code generation.

[0071] The code generation method provided in this embodiment performs entity analysis on the domain code of the target domain using a domain model to obtain a code knowledge graph. Therefore, it can better understand the code knowledge of the domain code within the domain model, effectively preserving the attribute information and relationships of each code entity in the domain code, thus providing important code context for code generation.

[0072] In some alternative implementations, step b1 above includes: Step b11: Obtain the domain-specific language of the target domain.

[0073] In this context, a domain-wide language (Ubiquitous Language) is used to represent domain concepts within the target domain, specifically describing business concepts within that domain. A Ubiquitous Language is a core concept in Domain-Driven Design (DDD), referring to a consistent and precise language used by domain experts and developers throughout the software development process within the same domain to describe the domain model or business concepts. The Ubiquitous Language aims to eliminate ambiguity regarding the same concepts within the domain, ensuring a consistent understanding of the same business requirements.

[0074] Step b12: Abstract entities from the domain's common language to obtain the domain model.

[0075] Specifically, see Figure 7 This paper summarizes and abstracts different business concepts in the common language of the aforementioned domains, obtaining domain entities, their structured information, and the relationships between them. Then, based on these domain entities, their structured information, and the relationships between them, a domain model is established.

[0076] It is worth noting that this application is based on the idea of ​​domain modeling, and focuses on clarifying the characteristics of entity structuring, semantic transparency, contextual relevance and graphability, so that objects in the target domain (such as entities, events, rules, etc.) can not only be understood and manipulated by human developers, but also accurately identified, reasoned and generated by code generation models, large language models and so on.

[0077] As a specific application example, let's take the internet computing recommendation content domain as the target domain. The process of building a domain model includes the following: summarizing and abstracting the common domain language and entities of the internet computing recommendation content domain; clarifying the domain entities, semantic descriptions, input definitions, output definitions, and contextual constraints of the domain entities in the internet computing recommendation content domain, in order to build a domain model for the internet computing recommendation content domain. For example, taking the bidding for recommended content in the internet computing recommendation content domain as an example, we establish a domain entity for bidding for recommended content. The entity content of this domain entity is "bid form," the semantic description of the domain entity is "the highest amount that the brand is willing to pay per click to display recommended content in order to obtain traffic," the input of the domain entity is defined as "the bid form value configured by the brand," the output of the domain entity is defined as "bid definition, etc.," and the upper and lower constraints of the domain entity are "the bid form entity is only allowed to be displayed under bidding strategies such as cost control."

[0078] The code generation method provided in this embodiment abstracts entities from the common language of the target domain to obtain a domain model. Therefore, it facilitates understanding the various concepts in the target domain through the domain model, enabling a better understanding of the domain code and thus generating an accurate code knowledge graph.

[0079] In some alternative implementations, step b2 above includes: Step b21: Extract the code entities corresponding to the domain entities from the domain code based on the domain model. The code entities include at least one of the following: the fields of the domain entity, the interaction events of the fields, the association relationships between domain entities, and the associated elements of the domain entity.

[0080] Step b22: Extract the relationships between code entities in the domain code.

[0081] Step b23: Extract the attribute information of the code entity from the domain code.

[0082] Step b24: Construct a code knowledge graph based on code entities, the relationships between code entities, and the attribute information of code entities.

[0083] Specifically, based on the domain model and domain code, the code entities of domain entities (such as fields, dependencies, constants, etc.), the relationships between code entities (such as inclusion, influence, linkage, etc.), and the attribute information of code entities (such as entity name, entity identifier, business comments, etc.) can be clearly identified. Thus, combined with the domain-specific domain code paradigm, the domain entity can be abstracted according to the three elements of "code entity - relationship - attribute information". Figure 3 The code knowledge graph shown.

[0084] In some alternative implementations, see [link to implementation details]. Figure 8 In a code knowledge graph, nodes represent code entities, edges between nodes represent the relationships between corresponding code entities, and node attributes represent the attribute information of code entities.

[0085] For example Figure 8 As shown, Figure 8 The document shows multiple code entities, such as code entity 1, code entity 11, etc., according to... Figure 8 As indicated by the arrows, code entity 11 depends on code entity 1, and code entity 112 depends on code entity 11.

[0086] As a specific application embodiment of this application, see [link to relevant documentation]. Figure 9 Taking a large language model as an example, this paper constructs an agent to execute the code generation method of this application. The code generation method of this application mainly includes the following process: The user inputs a code requirement description in natural language text. The intent of the code requirement description is recognized, and the intent recognition result is obtained. The agent combines the basic capabilities of the large language model domain code generation system, such as prompt words and long short-term memory of code generation, to analyze the user's input code requirement description, and uses the fitting and generalization capabilities of the large language model to recognize the intent of the code requirement description. Then, according to the actual inference needs of the large language model, the agent plans the call to the retrieval tool of the code knowledge graph, and obtains the call planning result of the retrieval tool. Based on the call planning result, the agent calls the retrieval tool of the code knowledge graph to search for entity information that matches the code requirement description in the code knowledge graph. The agent uses the entity information as the key code generation context through the model context protocol, so as to accurately recall code fragments that meet the code requirement description from the code knowledge base as target code fragments. As can be seen, in this embodiment, by leveraging an agent with a contextual protocol based on a code knowledge graph, relevant code fragments can be accurately retrieved from the domain code according to the user's code generation requirements, supplementing the code generation with key context. This is an effective method for enhancing retrieval and generation. Therefore, in code scenarios where intelligent agents / large language models are used to meet user needs, the ability of model retrieval to enhance code generation is improved, optimizing the accuracy, reliability, and reusability of code generation.

[0087] In summary, this application proposes a more AI-friendly and AI-supporting enhanced retrieval and generation method for domain-driven code knowledge images. On one hand, based on the code organization of a domain-based programming paradigm, this method abstracts a ternary code knowledge graph of "code entity-relationship-attribute information" for domain code, preserving the structured relationships between domain code. On the other hand, based on the structured relationships of the code knowledge graph, it provides supplementary code relationship context to the code generation model, enabling the model to recall existing code logic on demand and generalize to generate target code. As can be seen, this application combines Domain-Driven Design and Code Knowledge Graph to avoid manually enumerating the mapping relationships between code requirement descriptions and existing code snippets. It also avoids the loss of contextualized relationships between code entities within the domain. This provides a solid foundation for the large language model to retrieve code snippets on demand, accurately recall and reuse code snippets, and flexibly arrange the context of code snippets based on these structural relationships. It fully leverages the large language model's ability to combine code generation context information to further understand user code requirement descriptions, autonomously make task decisions and plans, improve the accuracy of recalling existing code snippets, optimize the effectiveness of the large language model's code generation context supplementation, and ultimately improve the accuracy of code generation.

[0088] This embodiment also provides a code generation apparatus for implementing the above embodiments and preferred embodiments; details already described will not be repeated. As used below, the term "module" can refer to a combination of software and / or hardware that performs a predetermined function. Although the apparatus described in the following embodiments is preferably implemented in software, hardware implementation, or a combination of software and hardware, is also possible and contemplated.

[0089] This embodiment provides a code generation device, such as... Figure 10 As shown, it includes: Information acquisition module 1001 is used to acquire code requirement descriptions for the target domain; The graph search module 1002 is used to search for entity information that matches the code requirement description in the code knowledge graph of the target domain; wherein, the code knowledge graph is used to represent code entities in the domain code of the target domain, attribute information of the code entities, and the relationship between code entities; the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and the relationship between the first code entities; The code search module 1003 is used to search for target code fragments that match entity information in the code knowledge base; The code generation module 1004 is used to generate target code corresponding to the code requirement description based on the target code snippet and the code generation model.

[0090] In some optional implementations, the map lookup module 1002 includes: The tool planning unit is used to plan the invocation of the retrieval tool for the code knowledge graph based on the code requirements description, and obtain the invocation planning results of the retrieval tool. The graph search unit is used to call the code knowledge graph retrieval tool to search for entity information in the code knowledge graph that matches the code requirement description, based on the call planning results.

[0091] In some optional embodiments, the code generation apparatus of this application further includes: The model building module is used to obtain the domain model of the target domain. The domain model includes the semantic description of the domain entities in the target domain, the structured information of the domain entities, and the contextual constraints of the domain entities. The entity analysis module is used to perform entity analysis on the domain code of the target domain based on the domain model, and obtain the code knowledge graph.

[0092] In some alternative implementations, the model building module includes: The data acquisition unit is used to acquire the domain-specific language of the target domain; Entity abstraction unit is used to abstract entities from the domain's common language to obtain the domain model.

[0093] In some alternative implementations, the structured information includes at least one of the following: input definitions of domain entities, output definitions of domain entities, attribute information of domain entities, and relationships between domain entities.

[0094] In some optional implementations, the entity analysis module includes: The first extraction unit is used to extract code entities corresponding to domain entities from the domain code based on the domain model. The code entities include at least one of the following: fields of the domain entity, interaction events of the fields, relationships between domain entities, and associated elements of the domain entity. The second extraction unit is used to extract the relationships between code entities in the domain code; The third extraction unit is used to extract attribute information of code entities from the domain code; The graph construction unit is used to construct a code knowledge graph based on code entities, the relationships between code entities, and the attribute information of code entities.

[0095] In some optional implementations, the attribute information of the code entity includes at least one of the following: the code entity's description information, the code fragment of the code entity, and the code path of the code fragment of the code entity.

[0096] In some optional implementations, nodes in the code knowledge graph are used to represent code entities, edges between nodes are used to represent the relationships between corresponding code entities, and node attributes are used to represent the attribute information of code entities.

[0097] The code generation apparatus provided in this application can execute the code generation method provided in any embodiment of this application, and has the corresponding functional modules and beneficial effects of the method execution. Further functional descriptions of the various modules and units described above are the same as those in the corresponding embodiments described above, and will not be repeated here.

[0098] Figure 11 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application.

[0099] The following is a detailed reference. Figure 11 The diagram illustrates a structural schematic suitable for implementing the electronic device described in the embodiments of this application. The electronic device may include a processor (e.g., a central processing unit, graphics processor, etc.) 1101, which can perform various appropriate actions and processes according to a program stored in read-only memory (ROM) 1102 or a program loaded from memory 1108 into random access memory (RAM) 1103. The RAM 1103 also stores various programs and data required for the operation of the electronic device. The processor 1101, ROM 1102, and RAM 1103 are interconnected via a bus 1104. An input / output (I / O) interface 1105 is also connected to the bus 1104.

[0100] Typically, the following devices can be connected to I / O interface 1105: input devices 1106 including, for example, touchscreens, touchpads, keyboards, mice, cameras, microphones, accelerometers, gyroscopes, etc.; output devices 1107 including, for example, liquid crystal displays (LCDs), speakers, vibrators, etc.; memory devices 1108 including, for example, magnetic tapes, hard disks, etc.; and communication devices 1109. Communication device 1109 allows electronic devices to communicate wirelessly or wiredly with other devices to exchange data. Although Figure 11 Electronic devices with various devices are shown, but it should be understood that it is not required to implement or have all of the devices shown, and more or fewer devices may be implemented or have instead.

[0101] Specifically, according to embodiments of this application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, embodiments of this application include a computer program product comprising a computer program carried on a non-transitory computer-readable medium, the computer program containing program code for performing the methods shown in the flowcharts. In such embodiments, the computer program can be downloaded and installed from a network via communication device 1109, or installed from memory 1108, or installed from ROM 1102. When the computer program is executed by processor 1101, it performs the functions defined in the code generation method of embodiments of this application.

[0102] Figure 11 The electronic device shown is merely an example and should not impose any limitation on the functionality and scope of use of the embodiments of this application.

[0103] This application also provides a computer-readable storage medium. The methods described in this application can be implemented in hardware or firmware, or implemented as recordable on a storage medium, or implemented as computer code downloaded over a network and originally stored on a remote storage medium or a non-transitory machine-readable storage medium and then stored on a local storage medium. Thus, the methods described herein can be processed by software stored on a storage medium using a general-purpose computer, a dedicated processor, or programmable or dedicated hardware. The storage medium can be a magnetic disk, optical disk, read-only memory, random access memory, flash memory, hard disk, or solid-state drive, etc.; further, the storage medium can also include combinations of the above types of memory. It is understood that computers, processors, microprocessor controllers, or programmable hardware include storage components capable of storing or receiving software or computer code. When the software or computer code is accessed and executed by the computer, processor, or hardware, the code generation method shown in the above embodiments is implemented.

[0104] A portion of this application can be applied as a computer program product, such as computer program instructions, which, when executed by a computer, can invoke or provide the methods and / or technical solutions according to this application through the operation of the computer. Those skilled in the art will understand that the forms in which computer program instructions exist in a computer-readable medium include, but are not limited to, source files, executable files, installation package files, etc. Correspondingly, the ways in which computer program instructions are executed by a computer include, but are not limited to: the computer directly executing the instructions, or the computer compiling the instructions and then executing the corresponding compiled program, or the computer reading and executing the instructions, or the computer reading and installing the instructions and then executing the corresponding installed program. Here, the computer-readable medium can be any available computer-readable storage medium or communication medium accessible to a computer.

[0105] Although embodiments of this application have been described in conjunction with the accompanying drawings, those skilled in the art can make various modifications and variations without departing from the spirit and scope of this application, and all such modifications and variations fall within the scope defined by the appended claims.

Claims

1. A code generation method characterized by, include: Obtain a code requirement description for the target domain; The code knowledge graph of the target domain is used to search for entity information that matches the code requirement description; wherein the code knowledge graph is used to represent code entities in the domain code of the target domain, attribute information of the code entities, and the association relationships between the code entities; the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and the association relationships between the first code entities; Search the code knowledge base for target code snippets that match the entity information; Based on the target code snippet and the code generation model, the target code corresponding to the code requirement description is generated.

2. The method of claim 1, wherein, The step of searching for entity information in the code knowledge graph of the target domain that matches the code requirement description includes: Based on the code requirement description, the retrieval tool of the code knowledge graph is called according to the call planning, and the call planning result of the retrieval tool is obtained. Based on the call planning results, the retrieval tool of the code knowledge graph is invoked to search for entity information in the code knowledge graph that matches the code requirement description.

3. The method of claim 1, wherein, Also includes: Obtain a domain model of the target domain, the domain model including semantic descriptions of domain entities in the target domain, structured information of the domain entities, and contextual constraints of the domain entities; Based on the domain model, entity analysis is performed on the domain code of the target domain to obtain the code knowledge graph.

4. The method of claim 3, wherein, The process of obtaining the domain model of the target domain includes: Obtain the domain-specific common language of the target domain; The domain model is obtained by abstracting entities from the common language of the domain.

5. The method of claim 3, wherein, The structured information includes at least one of the following: the input definition of the domain entity, the output definition of the domain entity, the attribute information of the domain entity, and the association relationship between the domain entities.

6. The method of claim 3, wherein, The step of performing entity analysis on the domain code of the target domain based on the domain model to obtain the code knowledge graph includes: Based on the domain model, the code entity corresponding to the domain entity is extracted from the domain code. The code entity includes at least one of the following: the field of the domain entity, the interaction event of the field, the association relationship between the domain entities, and the associated element of the domain entity. Extract the relationships between the code entities in the domain code; Extract the attribute information of the code entity from the domain code; The code knowledge graph is constructed based on the code entities, the relationships between the code entities, and the attribute information of the code entities.

7. The method of claim 6, wherein, The attribute information of the code entity includes at least one of the following: the description information of the code entity, the code fragment of the code entity, and the code path of the code fragment of the code entity.

8. The method of claim 1, wherein, The nodes in the code knowledge graph are used to represent the code entities, the edges between the nodes are used to represent the association relationships between the corresponding code entities, and the node attributes are used to represent the attribute information of the code entities.

9. A code generation apparatus characterized by comprising: include: The information acquisition module is used to acquire code requirement descriptions for the target domain; The graph search module is used to search for entity information that matches the code requirement description in the code knowledge graph of the target domain; wherein, the code knowledge graph is used to represent code entities in the domain code of the target domain, attribute information of the code entities, and the association relationships between the code entities; the entity information includes at least one of the following: a first code entity that matches the code requirement description, attribute information of the first code entity, and the association relationships between the first code entities; The code search module is used to search for target code fragments in the code knowledge base that match the entity information; The code generation module is used to generate target code corresponding to the code requirement description based on the target code fragment and the code generation model.

10. An electronic device, comprising: include: A memory and a processor are communicatively connected, the memory stores computer instructions, and the processor executes the computer instructions to perform the code generation method of any one of claims 1 to 8.

11. A computer readable storage medium, characterized in that, The computer-readable storage medium stores computer instructions for causing a computer to perform the code generation method of any one of claims 1 to 8.

12. A computer program product, characterised in that, Includes computer instructions for causing a computer to perform the code generation method according to any one of claims 1 to 8.